2
* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
2
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
44
44
static const uint8_t eth_addr_lacp[ETH_ADDR_LEN] OVS_UNUSED
45
45
= { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
47
static const uint8_t eth_addr_bfd[ETH_ADDR_LEN] OVS_UNUSED
48
= { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 };
47
50
static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
49
52
return (ea[0] & ea[1] & ea[2] & ea[3] & ea[4] & ea[5]) == 0xff;
140
143
void eth_push_vlan(struct ofpbuf *, ovs_be16 tci);
141
144
void eth_pop_vlan(struct ofpbuf *);
146
uint16_t eth_mpls_depth(const struct ofpbuf *packet);
148
void set_ethertype(struct ofpbuf *packet, ovs_be16 eth_type);
143
150
const char *eth_from_hex(const char *hex, struct ofpbuf **packetp);
144
151
void eth_format_masked(const uint8_t eth[ETH_ADDR_LEN],
145
152
const uint8_t mask[ETH_ADDR_LEN], struct ds *s);
147
154
const uint8_t mask[ETH_ADDR_LEN],
148
155
uint8_t dst[ETH_ADDR_LEN]);
157
void set_mpls_lse(struct ofpbuf *, ovs_be32 label);
158
void push_mpls(struct ofpbuf *packet, ovs_be16 ethtype, ovs_be32 lse);
159
void pop_mpls(struct ofpbuf *, ovs_be16 ethtype);
161
void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
162
void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
163
void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
164
void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
165
ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
152
170
* uint8_t mac[ETH_ADDR_LEN];
186
204
#define ETH_TYPE_MPLS 0x8847
187
205
#define ETH_TYPE_MPLS_MCAST 0x8848
207
static inline bool eth_type_mpls(ovs_be16 eth_type)
209
return eth_type == htons(ETH_TYPE_MPLS) ||
210
eth_type == htons(ETH_TYPE_MPLS_MCAST);
189
213
/* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame
191
215
#define ETH_TYPE_MIN 0x600
196
220
#define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
197
221
#define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
198
222
#define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
199
224
struct eth_header {
200
225
uint8_t eth_dst[ETH_ADDR_LEN];
201
226
uint8_t eth_src[ETH_ADDR_LEN];
202
227
ovs_be16 eth_type;
203
} __attribute__((packed));
204
229
BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
206
231
#define LLC_DSAP_SNAP 0xaa
208
233
#define LLC_CNTL_SNAP 3
210
235
#define LLC_HEADER_LEN 3
211
237
struct llc_header {
212
238
uint8_t llc_dsap;
213
239
uint8_t llc_ssap;
214
240
uint8_t llc_cntl;
215
} __attribute__((packed));
216
242
BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
218
244
#define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
219
245
sizeof(SNAP_ORG_ETHERNET) == 3. */
220
246
#define SNAP_HEADER_LEN 5
221
248
struct snap_header {
222
249
uint8_t snap_org[3];
223
250
ovs_be16 snap_type;
224
} __attribute__((packed));
225
252
BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
227
254
#define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
228
256
struct llc_snap_header {
229
257
struct llc_header llc;
230
258
struct snap_header snap;
231
} __attribute__((packed));
232
260
BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
234
262
#define VLAN_VID_MASK 0x0fff
238
266
#define VLAN_PCP_SHIFT 13
240
268
#define VLAN_CFI 0x1000
269
#define VLAN_CFI_SHIFT 12
242
271
/* Given the vlan_tci field from an 802.1Q header, in network byte order,
243
272
* returns the VLAN ID in host byte order. */
255
284
return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
287
/* Given the vlan_tci field from an 802.1Q header, in network byte order,
288
* returns the Canonical Format Indicator (CFI). */
290
vlan_tci_to_cfi(ovs_be16 vlan_tci)
292
return (vlan_tci & htons(VLAN_CFI)) != 0;
258
295
#define VLAN_HEADER_LEN 4
259
296
struct vlan_header {
260
297
ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */
263
300
BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
265
302
#define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
266
304
struct vlan_eth_header {
267
305
uint8_t veth_dst[ETH_ADDR_LEN];
268
306
uint8_t veth_src[ETH_ADDR_LEN];
269
307
ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */
270
308
ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */
271
309
ovs_be16 veth_next_type;
272
} __attribute__((packed));
273
311
BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
313
/* MPLS related definitions */
314
#define MPLS_TTL_MASK 0x000000ff
315
#define MPLS_TTL_SHIFT 0
317
#define MPLS_BOS_MASK 0x00000100
318
#define MPLS_BOS_SHIFT 8
320
#define MPLS_TC_MASK 0x00000e00
321
#define MPLS_TC_SHIFT 9
323
#define MPLS_LABEL_MASK 0xfffff000
324
#define MPLS_LABEL_SHIFT 12
331
BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
333
/* Given a mpls label stack entry in network byte order
334
* return mpls label in host byte order */
335
static inline uint32_t
336
mpls_lse_to_label(ovs_be32 mpls_lse)
338
return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
341
/* Given a mpls label stack entry in network byte order
343
static inline uint8_t
344
mpls_lse_to_tc(ovs_be32 mpls_lse)
346
return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
349
/* Given a mpls label stack entry in network byte order
351
static inline uint8_t
352
mpls_lse_to_ttl(ovs_be32 mpls_lse)
354
return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
357
/* Set TTL in mpls lse. */
359
flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
361
*mpls_lse &= ~htonl(MPLS_TTL_MASK);
362
*mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
365
/* Given a mpls label stack entry in network byte order
366
* return mpls BoS bit */
367
static inline uint8_t
368
mpls_lse_to_bos(ovs_be32 mpls_lse)
370
return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
275
373
#define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
276
374
#define IP_ARGS(ip) \
277
375
ntohl(ip) >> 24, \
426
525
ovs_be32 ar_spa; /* Sender protocol address. */
427
526
uint8_t ar_tha[ETH_ADDR_LEN]; /* Target hardware address. */
428
527
ovs_be32 ar_tpa; /* Target protocol address. */
429
} __attribute__((packed));
430
529
BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
432
531
/* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
468
567
return ipv6_addr_equals(mask, &in6addr_exact);
570
static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
572
return dl_type == htons(ETH_TYPE_IP)
573
|| dl_type == htons(ETH_TYPE_IPV6);
471
576
static inline bool is_ip_any(const struct flow *flow)
473
return flow->dl_type == htons(ETH_TYPE_IP)
474
|| flow->dl_type == htons(ETH_TYPE_IPV6);
578
return dl_type_is_ip_any(flow->dl_type);
477
581
void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);