Add support for matching Ethernet multicast frames.
[sliver-openvswitch.git] / lib / flow.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <config.h>
17 #include <sys/types.h>
18 #include "flow.h"
19 #include <inttypes.h>
20 #include <netinet/in.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "byte-order.h"
24 #include "coverage.h"
25 #include "dynamic-string.h"
26 #include "hash.h"
27 #include "ofpbuf.h"
28 #include "openflow/openflow.h"
29 #include "openvswitch/datapath-protocol.h"
30 #include "packets.h"
31 #include "unaligned.h"
32 #include "vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(flow);
35
36 static struct arp_eth_header *
37 pull_arp(struct ofpbuf *packet)
38 {
39     return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
40 }
41
42 static struct ip_header *
43 pull_ip(struct ofpbuf *packet)
44 {
45     if (packet->size >= IP_HEADER_LEN) {
46         struct ip_header *ip = packet->data;
47         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
48         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
49             return ofpbuf_pull(packet, ip_len);
50         }
51     }
52     return NULL;
53 }
54
55 static struct tcp_header *
56 pull_tcp(struct ofpbuf *packet)
57 {
58     if (packet->size >= TCP_HEADER_LEN) {
59         struct tcp_header *tcp = packet->data;
60         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
61         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
62             return ofpbuf_pull(packet, tcp_len);
63         }
64     }
65     return NULL;
66 }
67
68 static struct udp_header *
69 pull_udp(struct ofpbuf *packet)
70 {
71     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
72 }
73
74 static struct icmp_header *
75 pull_icmp(struct ofpbuf *packet)
76 {
77     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
78 }
79
80 static void
81 parse_vlan(struct ofpbuf *b, struct flow *flow)
82 {
83     struct qtag_prefix {
84         ovs_be16 eth_type;      /* ETH_TYPE_VLAN */
85         ovs_be16 tci;
86     };
87
88     if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
89         struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
90         flow->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
91         flow->dl_vlan_pcp = vlan_tci_to_pcp(qp->tci);
92     }
93 }
94
95 static ovs_be16
96 parse_ethertype(struct ofpbuf *b)
97 {
98     struct llc_snap_header *llc;
99     ovs_be16 proto;
100
101     proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
102     if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF) {
103         return proto;
104     }
105
106     if (b->size < sizeof *llc) {
107         return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
108     }
109
110     llc = b->data;
111     if (llc->llc.llc_dsap != LLC_DSAP_SNAP
112         || llc->llc.llc_ssap != LLC_SSAP_SNAP
113         || llc->llc.llc_cntl != LLC_CNTL_SNAP
114         || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
115                   sizeof llc->snap.snap_org)) {
116         return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
117     }
118
119     ofpbuf_pull(b, sizeof *llc);
120     return llc->snap.snap_type;
121 }
122
123 /* Initializes 'flow' members from 'packet', 'tun_id', and 'in_port.
124  * Initializes 'packet' header pointers as follows:
125  *
126  *    - packet->l2 to the start of the Ethernet header.
127  *
128  *    - packet->l3 to just past the Ethernet header, or just past the
129  *      vlan_header if one is present, to the first byte of the payload of the
130  *      Ethernet frame.
131  *
132  *    - packet->l4 to just past the IPv4 header, if one is present and has a
133  *      correct length, and otherwise NULL.
134  *
135  *    - packet->l7 to just past the TCP or UDP or ICMP header, if one is
136  *      present and has a correct length, and otherwise NULL.
137  */
138 int
139 flow_extract(struct ofpbuf *packet, ovs_be32 tun_id, uint16_t in_port,
140              struct flow *flow)
141 {
142     struct ofpbuf b = *packet;
143     struct eth_header *eth;
144     int retval = 0;
145
146     COVERAGE_INC(flow_extract);
147
148     memset(flow, 0, sizeof *flow);
149     flow->tun_id = tun_id;
150     flow->in_port = in_port;
151     flow->dl_vlan = htons(OFP_VLAN_NONE);
152
153     packet->l2 = b.data;
154     packet->l3 = NULL;
155     packet->l4 = NULL;
156     packet->l7 = NULL;
157
158     if (b.size < sizeof *eth) {
159         return 0;
160     }
161
162     /* Link layer. */
163     eth = b.data;
164     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
165     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
166
167     /* dl_type, dl_vlan, dl_vlan_pcp. */
168     ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
169     if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
170         parse_vlan(&b, flow);
171     }
172     flow->dl_type = parse_ethertype(&b);
173
174     /* Network layer. */
175     packet->l3 = b.data;
176     if (flow->dl_type == htons(ETH_TYPE_IP)) {
177         const struct ip_header *nh = pull_ip(&b);
178         if (nh) {
179             flow->nw_src = get_unaligned_u32(&nh->ip_src);
180             flow->nw_dst = get_unaligned_u32(&nh->ip_dst);
181             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
182             flow->nw_proto = nh->ip_proto;
183             packet->l4 = b.data;
184             if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
185                 if (flow->nw_proto == IP_TYPE_TCP) {
186                     const struct tcp_header *tcp = pull_tcp(&b);
187                     if (tcp) {
188                         flow->tp_src = tcp->tcp_src;
189                         flow->tp_dst = tcp->tcp_dst;
190                         packet->l7 = b.data;
191                     }
192                 } else if (flow->nw_proto == IP_TYPE_UDP) {
193                     const struct udp_header *udp = pull_udp(&b);
194                     if (udp) {
195                         flow->tp_src = udp->udp_src;
196                         flow->tp_dst = udp->udp_dst;
197                         packet->l7 = b.data;
198                     }
199                 } else if (flow->nw_proto == IP_TYPE_ICMP) {
200                     const struct icmp_header *icmp = pull_icmp(&b);
201                     if (icmp) {
202                         flow->icmp_type = htons(icmp->icmp_type);
203                         flow->icmp_code = htons(icmp->icmp_code);
204                         packet->l7 = b.data;
205                     }
206                 }
207             } else {
208                 retval = 1;
209             }
210         }
211     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
212         const struct arp_eth_header *arp = pull_arp(&b);
213         if (arp && arp->ar_hrd == htons(1)
214             && arp->ar_pro == htons(ETH_TYPE_IP)
215             && arp->ar_hln == ETH_ADDR_LEN
216             && arp->ar_pln == 4) {
217             /* We only match on the lower 8 bits of the opcode. */
218             if (ntohs(arp->ar_op) <= 0xff) {
219                 flow->nw_proto = ntohs(arp->ar_op);
220             }
221
222             if ((flow->nw_proto == ARP_OP_REQUEST)
223                 || (flow->nw_proto == ARP_OP_REPLY)) {
224                 flow->nw_src = arp->ar_spa;
225                 flow->nw_dst = arp->ar_tpa;
226             }
227         }
228     }
229     return retval;
230 }
231
232 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
233  * arguments must have been initialized through a call to flow_extract().
234  */
235 void
236 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
237         struct odp_flow_stats *stats)
238 {
239     memset(stats, '\0', sizeof(*stats));
240
241     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
242         if ((flow->nw_proto == IP_TYPE_TCP) && packet->l7) {
243             struct tcp_header *tcp = packet->l4;
244             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
245         }
246     }
247
248     stats->n_bytes = packet->size;
249     stats->n_packets = 1;
250 }
251
252 /* Extract 'flow' with 'wildcards' into the OpenFlow match structure
253  * 'match'.  'flow_format' should be one of NXFF_*. */
254 void
255 flow_to_match(const struct flow *flow, uint32_t wildcards,
256               int flow_format, struct ofp_match *match)
257 {
258     wildcards &= (flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL
259                   : OFPFW_ALL);
260     match->wildcards = htonl(wildcards);
261
262     match->in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
263                            : flow->in_port);
264     match->dl_vlan = flow->dl_vlan;
265     match->dl_vlan_pcp = flow->dl_vlan_pcp;
266     memcpy(match->dl_src, flow->dl_src, ETH_ADDR_LEN);
267     memcpy(match->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
268     match->dl_type = flow->dl_type;
269     match->nw_src = flow->nw_src;
270     match->nw_dst = flow->nw_dst;
271     match->nw_tos = flow->nw_tos;
272     match->nw_proto = flow->nw_proto;
273     match->tp_src = flow->tp_src;
274     match->tp_dst = flow->tp_dst;
275     memset(match->pad1, '\0', sizeof match->pad1);
276     memset(match->pad2, '\0', sizeof match->pad2);
277 }
278
279 void
280 flow_from_match(const struct ofp_match *match, int flow_format,
281                 ovs_be64 cookie, struct flow *flow,
282                 struct flow_wildcards *wc)
283 {
284     uint32_t wildcards = ntohl(match->wildcards) & OVSFW_ALL;
285
286     flow->tun_id = 0;
287     if (flow_format != NXFF_TUN_ID_FROM_COOKIE) {
288         wildcards |= NXFW_TUN_ID;
289     } else {
290         if (!(wildcards & NXFW_TUN_ID)) {
291             flow->tun_id = htonl(ntohll(cookie) >> 32);
292         }
293     }
294     if (wildcards & OFPFW_DL_DST) {
295         /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
296          * internally to OVS it excludes the multicast bit, which has to be set
297          * separately with FWW_ETH_MCAST. */
298         wildcards |= FWW_ETH_MCAST;
299     }
300     flow_wildcards_init(wc, wildcards);
301
302     flow->nw_src = match->nw_src;
303     flow->nw_dst = match->nw_dst;
304     flow->in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
305                      : ntohs(match->in_port));
306     flow->dl_vlan = match->dl_vlan;
307     flow->dl_vlan_pcp = match->dl_vlan_pcp;
308     flow->dl_type = match->dl_type;
309     flow->tp_src = match->tp_src;
310     flow->tp_dst = match->tp_dst;
311     memcpy(flow->dl_src, match->dl_src, ETH_ADDR_LEN);
312     memcpy(flow->dl_dst, match->dl_dst, ETH_ADDR_LEN);
313     flow->nw_tos = match->nw_tos;
314     flow->nw_proto = match->nw_proto;
315 }
316
317 char *
318 flow_to_string(const struct flow *flow)
319 {
320     struct ds ds = DS_EMPTY_INITIALIZER;
321     flow_format(&ds, flow);
322     return ds_cstr(&ds);
323 }
324
325 void
326 flow_format(struct ds *ds, const struct flow *flow)
327 {
328     ds_put_format(ds, "tunnel%08"PRIx32":in_port%04"PRIx16
329                       ":vlan%"PRIu16":pcp%"PRIu8
330                       " mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
331                       " type%04"PRIx16
332                       " proto%"PRIu8
333                       " tos%"PRIu8
334                       " ip"IP_FMT"->"IP_FMT
335                       " port%"PRIu16"->%"PRIu16,
336                   ntohl(flow->tun_id),
337                   flow->in_port,
338                   ntohs(flow->dl_vlan),
339                   flow->dl_vlan_pcp,
340                   ETH_ADDR_ARGS(flow->dl_src),
341                   ETH_ADDR_ARGS(flow->dl_dst),
342                   ntohs(flow->dl_type),
343                   flow->nw_proto,
344                   flow->nw_tos,
345                   IP_ARGS(&flow->nw_src),
346                   IP_ARGS(&flow->nw_dst),
347                   ntohs(flow->tp_src),
348                   ntohs(flow->tp_dst));
349 }
350
351 void
352 flow_print(FILE *stream, const struct flow *flow)
353 {
354     char *s = flow_to_string(flow);
355     fputs(s, stream);
356     free(s);
357 }
358 \f
359 /* flow_wildcards functions. */
360
361 /* Given the wildcard bit count in bits 'shift' through 'shift + 5' (inclusive)
362  * of 'wildcards', returns a 32-bit bit mask with a 1 in each bit that must
363  * match and a 0 in each bit that is wildcarded.
364  *
365  * The bits in 'wildcards' are in the format used in enum ofp_flow_wildcards: 0
366  * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
367  * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
368  * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
369  * wildcarded. */
370 ovs_be32
371 flow_nw_bits_to_mask(uint32_t wildcards, int shift)
372 {
373     wildcards = (wildcards >> shift) & 0x3f;
374     return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
375 }
376
377 /* Return 'wildcards' in "normal form":
378  *
379  *   - Forces unknown bits to 0.
380  *
381  *   - Forces nw_src and nw_dst masks greater than 32 to exactly 32.
382  */
383 static inline uint32_t
384 flow_wildcards_normalize(uint32_t wildcards)
385 {
386     wildcards &= wildcards & (OVSFW_ALL | FWW_ALL);
387     if (wildcards & (0x20 << OFPFW_NW_SRC_SHIFT)) {
388         wildcards &= ~(0x1f << OFPFW_NW_SRC_SHIFT);
389     }
390     if (wildcards & (0x20 << OFPFW_NW_DST_SHIFT)) {
391         wildcards &= ~(0x1f << OFPFW_NW_DST_SHIFT);
392     }
393     return wildcards;
394 }
395
396 /* Initializes 'wc' from 'wildcards', which may be any combination of the
397  * OFPFW_* and OVSFW_* wildcard bits.
398  *
399  * All registers (NXM_NX_REG*) are always completely wildcarded, because
400  * 'wildcards' doesn't have enough bits to give the details on which
401  * particular bits should be wildcarded (if any).  The caller may use
402  * flow_wildcards_set_reg_mask() to update the register wildcard masks. */
403 void
404 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
405 {
406     wc->wildcards = flow_wildcards_normalize(wildcards) | FWW_REGS;
407     wc->nw_src_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_SRC_SHIFT);
408     wc->nw_dst_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_DST_SHIFT);
409     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
410 }
411
412 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
413  * wildcard any bits or fields. */
414 void
415 flow_wildcards_init_exact(struct flow_wildcards *wc)
416 {
417     wc->wildcards = 0;
418     wc->nw_src_mask = htonl(UINT32_MAX);
419     wc->nw_dst_mask = htonl(UINT32_MAX);
420     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
421 }
422
423 static inline uint32_t
424 combine_nw_bits(uint32_t wb1, uint32_t wb2, int shift)
425 {
426     uint32_t sb1 = (wb1 >> shift) & 0x3f;
427     uint32_t sb2 = (wb2 >> shift) & 0x3f;
428     return MAX(sb1, sb2) << shift;
429 }
430
431 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
432  * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
433  * 'src1' or 'src2' or both.  */
434 void
435 flow_wildcards_combine(struct flow_wildcards *dst,
436                        const struct flow_wildcards *src1,
437                        const struct flow_wildcards *src2)
438 {
439     uint32_t wb1 = src1->wildcards;
440     uint32_t wb2 = src2->wildcards;
441     int i;
442
443     dst->wildcards = (wb1 | wb2) & ~(OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK);
444     dst->wildcards |= combine_nw_bits(wb1, wb2, OFPFW_NW_SRC_SHIFT);
445     dst->wildcards |= combine_nw_bits(wb1, wb2, OFPFW_NW_DST_SHIFT);
446     dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
447     dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
448     for (i = 0; i < FLOW_N_REGS; i++) {
449         dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
450     }
451 }
452
453 /* Returns a hash of the wildcards in 'wc'. */
454 uint32_t
455 flow_wildcards_hash(const struct flow_wildcards *wc)
456 {
457     /* There is no need to include nw_src_mask or nw_dst_mask because they do
458      * not add any information (they can be computed from wc->wildcards).  */
459     BUILD_ASSERT_DECL(sizeof wc->wildcards == 4);
460     BUILD_ASSERT_DECL(sizeof wc->reg_masks == 4 * FLOW_N_REGS);
461     BUILD_ASSERT_DECL(offsetof(struct flow_wildcards, wildcards) == 0);
462     BUILD_ASSERT_DECL(offsetof(struct flow_wildcards, reg_masks) == 4);
463     return hash_words((const uint32_t *) wc, 1 + FLOW_N_REGS, 0);
464 }
465
466 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
467  * different. */
468 bool
469 flow_wildcards_equal(const struct flow_wildcards *a,
470                      const struct flow_wildcards *b)
471 {
472     int i;
473
474     if (a->wildcards != b->wildcards) {
475         return false;
476     }
477
478     for (i = 0; i < FLOW_N_REGS; i++) {
479         if (a->reg_masks[i] != b->reg_masks[i]) {
480             return false;
481         }
482     }
483
484     return true;
485 }
486
487 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
488  * 'b', false otherwise. */
489 bool
490 flow_wildcards_has_extra(const struct flow_wildcards *a,
491                          const struct flow_wildcards *b)
492 {
493     int i;
494
495     for (i = 0; i < FLOW_N_REGS; i++) {
496         if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
497             return true;
498         }
499     }
500
501 #define OFPFW_NW_MASK (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK)
502     return ((a->wildcards & ~(b->wildcards | OFPFW_NW_MASK))
503             || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
504             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask);
505 }
506
507 static int
508 count_ones(ovs_be32 mask)
509 {
510 #if __GNUC__ >= 4
511     return __builtin_popcount(mask);
512 #else
513     int bits;
514
515     for (bits = 0; mask; bits++) {
516         mask &= mask - 1;
517     }
518
519     return bits;
520 #endif
521 }
522
523 static bool
524 set_nw_mask(struct flow_wildcards *wc, ovs_be32 mask,
525             ovs_be32 *maskp, int shift)
526 {
527     int wcbits = 32 - count_ones(mask);
528     if (flow_nw_bits_to_mask(wcbits, 0) == mask) {
529         wc->wildcards &= ~(0x3f << shift);
530         wc->wildcards |= wcbits << shift;
531         *maskp = mask;
532         return true;
533     } else {
534         return false;
535     }
536 }
537
538 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
539  * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
540  * false if 'mask' is not a CIDR mask.  */
541 bool
542 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
543 {
544     return set_nw_mask(wc, mask, &wc->nw_src_mask, OFPFW_NW_SRC_SHIFT);
545 }
546
547 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
548  * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
549  * false if 'mask' is not a CIDR mask.  */
550 bool
551 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
552 {
553     return set_nw_mask(wc, mask, &wc->nw_dst_mask, OFPFW_NW_DST_SHIFT);
554 }
555
556 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
557  * (A 0-bit indicates a wildcard bit.) */
558 void
559 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
560 {
561     if (mask != wc->reg_masks[idx]) {
562         wc->reg_masks[idx] = mask;
563         if (mask != UINT32_MAX) {
564             wc->wildcards |= FWW_REGS;
565         } else {
566             int i;
567
568             for (i = 0; i < FLOW_N_REGS; i++) {
569                 if (wc->reg_masks[i] != UINT32_MAX) {
570                     wc->wildcards |= FWW_REGS;
571                     return;
572                 }
573             }
574             wc->wildcards &= ~FWW_REGS;
575         }
576     }
577 }