~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/packets.h

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
 
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3
3
 *
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 };
46
46
 
 
47
static const uint8_t eth_addr_bfd[ETH_ADDR_LEN] OVS_UNUSED
 
48
    = { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 };
 
49
 
47
50
static inline bool eth_addr_is_broadcast(const uint8_t ea[6])
48
51
{
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 *);
142
145
 
 
146
uint16_t eth_mpls_depth(const struct ofpbuf *packet);
 
147
 
 
148
void set_ethertype(struct ofpbuf *packet, ovs_be16 eth_type);
 
149
 
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]);
149
156
 
 
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);
 
160
 
 
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,
 
166
                             ovs_be32 label);
 
167
 
150
168
/* Example:
151
169
 *
152
170
 * uint8_t mac[ETH_ADDR_LEN];
186
204
#define ETH_TYPE_MPLS          0x8847
187
205
#define ETH_TYPE_MPLS_MCAST    0x8848
188
206
 
 
207
static inline bool eth_type_mpls(ovs_be16 eth_type)
 
208
{
 
209
    return eth_type == htons(ETH_TYPE_MPLS) ||
 
210
        eth_type == htons(ETH_TYPE_MPLS_MCAST);
 
211
}
 
212
 
189
213
/* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
190
214
 * lengths. */
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)
 
223
OVS_PACKED(
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));
 
228
});
204
229
BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
205
230
 
206
231
#define LLC_DSAP_SNAP 0xaa
208
233
#define LLC_CNTL_SNAP 3
209
234
 
210
235
#define LLC_HEADER_LEN 3
 
236
OVS_PACKED(
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));
 
241
});
216
242
BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
217
243
 
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
 
247
OVS_PACKED(
221
248
struct snap_header {
222
249
    uint8_t snap_org[3];
223
250
    ovs_be16 snap_type;
224
 
} __attribute__((packed));
 
251
});
225
252
BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
226
253
 
227
254
#define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
 
255
OVS_PACKED(
228
256
struct llc_snap_header {
229
257
    struct llc_header llc;
230
258
    struct snap_header snap;
231
 
} __attribute__((packed));
 
259
});
232
260
BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
233
261
 
234
262
#define VLAN_VID_MASK 0x0fff
238
266
#define VLAN_PCP_SHIFT 13
239
267
 
240
268
#define VLAN_CFI 0x1000
 
269
#define VLAN_CFI_SHIFT 12
241
270
 
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;
256
285
}
257
286
 
 
287
/* Given the vlan_tci field from an 802.1Q header, in network byte order,
 
288
 * returns the Canonical Format Indicator (CFI). */
 
289
static inline int
 
290
vlan_tci_to_cfi(ovs_be16 vlan_tci)
 
291
{
 
292
    return (vlan_tci & htons(VLAN_CFI)) != 0;
 
293
}
 
294
 
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));
264
301
 
265
302
#define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
 
303
OVS_PACKED(
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));
 
310
});
273
311
BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
274
312
 
 
313
/* MPLS related definitions */
 
314
#define MPLS_TTL_MASK       0x000000ff
 
315
#define MPLS_TTL_SHIFT      0
 
316
 
 
317
#define MPLS_BOS_MASK       0x00000100
 
318
#define MPLS_BOS_SHIFT      8
 
319
 
 
320
#define MPLS_TC_MASK        0x00000e00
 
321
#define MPLS_TC_SHIFT       9
 
322
 
 
323
#define MPLS_LABEL_MASK     0xfffff000
 
324
#define MPLS_LABEL_SHIFT    12
 
325
 
 
326
#define MPLS_HLEN           4
 
327
 
 
328
struct mpls_hdr {
 
329
    ovs_be32 mpls_lse;
 
330
};
 
331
BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
 
332
 
 
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)
 
337
{
 
338
    return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
 
339
}
 
340
 
 
341
/* Given a mpls label stack entry in network byte order
 
342
 * return mpls tc */
 
343
static inline uint8_t
 
344
mpls_lse_to_tc(ovs_be32 mpls_lse)
 
345
{
 
346
    return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
 
347
}
 
348
 
 
349
/* Given a mpls label stack entry in network byte order
 
350
 * return mpls ttl */
 
351
static inline uint8_t
 
352
mpls_lse_to_ttl(ovs_be32 mpls_lse)
 
353
{
 
354
    return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
 
355
}
 
356
 
 
357
/* Set TTL in mpls lse. */
 
358
static inline void
 
359
flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
 
360
{
 
361
    *mpls_lse &= ~htonl(MPLS_TTL_MASK);
 
362
    *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
 
363
}
 
364
 
 
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)
 
369
{
 
370
    return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
 
371
}
 
372
 
275
373
#define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
276
374
#define IP_ARGS(ip)                             \
277
375
    ntohl(ip) >> 24,                            \
413
511
#define ARP_OP_RARP 3
414
512
 
415
513
#define ARP_ETH_HEADER_LEN 28
 
514
OVS_PACKED(
416
515
struct arp_eth_header {
417
516
    /* Generic members. */
418
517
    ovs_be16 ar_hrd;           /* Hardware type. */
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));
 
528
});
430
529
BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
431
530
 
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);
469
568
}
470
569
 
 
570
static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
 
571
{
 
572
    return dl_type == htons(ETH_TYPE_IP)
 
573
        || dl_type == htons(ETH_TYPE_IPV6);
 
574
}
 
575
 
471
576
static inline bool is_ip_any(const struct flow *flow)
472
577
{
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);
475
579
}
476
580
 
477
581
void format_ipv6_addr(char *addr_str, const struct in6_addr *addr);