Support matching IPv6 flow label.
[sliver-openvswitch.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011 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
17 #include <config.h>
18
19 #include "meta-flow.h"
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
26
27 #include "classifier.h"
28 #include "dynamic-string.h"
29 #include "ofp-util.h"
30 #include "packets.h"
31 #include "random.h"
32 #include "shash.h"
33 #include "socket-util.h"
34 #include "unaligned.h"
35
36 #define MF_FIELD_SIZES(MEMBER)                  \
37     sizeof ((union mf_value *)0)->MEMBER,       \
38     8 * sizeof ((union mf_value *)0)->MEMBER
39
40 static const struct mf_field mf_fields[MFF_N_IDS] = {
41     /* ## -------- ## */
42     /* ## metadata ## */
43     /* ## -------- ## */
44
45     {
46         MFF_TUN_ID, "tun_id", NULL,
47         MF_FIELD_SIZES(be64),
48         MFM_FULLY, 0,
49         MFS_HEXADECIMAL,
50         MFP_NONE,
51         NXM_NX_TUN_ID,
52     }, {
53         MFF_IN_PORT, "in_port", NULL,
54         MF_FIELD_SIZES(be16),
55         MFM_NONE, FWW_IN_PORT,
56         MFS_OFP_PORT,
57         MFP_NONE,
58         NXM_OF_IN_PORT,
59     },
60
61 #define REGISTER(IDX)                           \
62     {                                           \
63         MFF_REG##IDX, "reg" #IDX, NULL,         \
64         MF_FIELD_SIZES(be32),                   \
65         MFM_FULLY, 0,                           \
66         MFS_HEXADECIMAL,                        \
67         MFP_NONE,                               \
68         NXM_NX_REG(IDX),                        \
69     }
70 #if FLOW_N_REGS > 0
71     REGISTER(0),
72 #endif
73 #if FLOW_N_REGS > 1
74     REGISTER(1),
75 #endif
76 #if FLOW_N_REGS > 2
77     REGISTER(2),
78 #endif
79 #if FLOW_N_REGS > 3
80     REGISTER(3),
81 #endif
82 #if FLOW_N_REGS > 4
83     REGISTER(4),
84 #endif
85 #if FLOW_N_REGS > 5
86 #error
87 #endif
88
89     /* ## -- ## */
90     /* ## L2 ## */
91     /* ## -- ## */
92
93     {
94         MFF_ETH_SRC, "eth_src", "dl_src",
95         MF_FIELD_SIZES(mac),
96         MFM_NONE, FWW_DL_SRC,
97         MFS_ETHERNET,
98         MFP_NONE,
99         NXM_OF_ETH_SRC,
100     }, {
101         MFF_ETH_DST, "eth_dst", "dl_dst",
102         MF_FIELD_SIZES(mac),
103         MFM_MCAST, 0,
104         MFS_ETHERNET,
105         MFP_NONE,
106         NXM_OF_ETH_DST,
107     }, {
108         MFF_ETH_TYPE, "eth_type", "dl_type",
109         MF_FIELD_SIZES(be16),
110         MFM_NONE, FWW_DL_TYPE,
111         MFS_HEXADECIMAL,
112         MFP_NONE,
113         NXM_OF_ETH_TYPE,
114     },
115
116     {
117         MFF_VLAN_TCI, "vlan_tci", NULL,
118         MF_FIELD_SIZES(be16),
119         MFM_FULLY, 0,
120         MFS_HEXADECIMAL,
121         MFP_NONE,
122         NXM_OF_VLAN_TCI,
123     }, {
124         MFF_VLAN_VID, "dl_vlan", NULL,
125         sizeof(ovs_be16), 12,
126         MFM_NONE, 0,
127         MFS_DECIMAL,
128         MFP_NONE,
129         0,
130     }, {
131         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
132         1, 3,
133         MFM_NONE, 0,
134         MFS_DECIMAL,
135         MFP_NONE,
136         0,
137     },
138
139     /* ## -- ## */
140     /* ## L3 ## */
141     /* ## -- ## */
142
143     {
144         MFF_IPV4_SRC, "ip_src", "nw_src",
145         MF_FIELD_SIZES(be32),
146         MFM_CIDR, 0,
147         MFS_IPV4,
148         MFP_IPV4,
149         NXM_OF_IP_SRC,
150     }, {
151         MFF_IPV4_DST, "ip_dst", "nw_dst",
152         MF_FIELD_SIZES(be32),
153         MFM_CIDR, 0,
154         MFS_IPV4,
155         MFP_IPV4,
156         NXM_OF_IP_DST,
157     },
158
159     {
160         MFF_IPV6_SRC, "ipv6_src", NULL,
161         MF_FIELD_SIZES(ipv6),
162         MFM_CIDR, 0,
163         MFS_IPV6,
164         MFP_IPV6,
165         NXM_NX_IPV6_SRC,
166     }, {
167         MFF_IPV6_DST, "ipv6_dst", NULL,
168         MF_FIELD_SIZES(ipv6),
169         MFM_CIDR, 0,
170         MFS_IPV6,
171         MFP_IPV6,
172         NXM_NX_IPV6_DST,
173     },
174     {
175         MFF_IPV6_LABEL, "ipv6_label", NULL,
176         4, 20,
177         MFM_NONE, FWW_IPV6_LABEL,
178         MFS_HEXADECIMAL,
179         MFP_IPV6,
180         NXM_NX_IPV6_LABEL,
181     },
182
183     {
184         MFF_IP_PROTO, "nw_proto", NULL,
185         MF_FIELD_SIZES(u8),
186         MFM_NONE, FWW_NW_PROTO,
187         MFS_DECIMAL,
188         MFP_IP_ANY,
189         NXM_OF_IP_PROTO,
190     }, {
191         MFF_IP_TOS, "nw_tos", NULL,
192         MF_FIELD_SIZES(u8),
193         MFM_NONE, 0,
194         MFS_DECIMAL,
195         MFP_IP_ANY,
196         NXM_OF_IP_TOS,
197     }, {
198         MFF_IP_FRAG, "ip_frag", NULL,
199         1, 2,
200         MFM_FULLY, 0,
201         MFS_FRAG,
202         MFP_IP_ANY,
203         NXM_NX_IP_FRAG,
204     },
205
206     {
207         MFF_ARP_OP, "arp_op", NULL,
208         MF_FIELD_SIZES(be16),
209         MFM_NONE, FWW_NW_PROTO,
210         MFS_DECIMAL,
211         MFP_ARP,
212         NXM_OF_ARP_OP,
213     }, {
214         MFF_ARP_SPA, "arp_spa", NULL,
215         MF_FIELD_SIZES(be32),
216         MFM_CIDR, 0,
217         MFS_IPV4,
218         MFP_ARP,
219         NXM_OF_ARP_SPA,
220     }, {
221         MFF_ARP_TPA, "arp_tpa", NULL,
222         MF_FIELD_SIZES(be32),
223         MFM_CIDR, 0,
224         MFS_IPV4,
225         MFP_ARP,
226         NXM_OF_ARP_TPA,
227     }, {
228         MFF_ARP_SHA, "arp_sha", NULL,
229         MF_FIELD_SIZES(mac),
230         MFM_NONE, FWW_ARP_SHA,
231         MFS_ETHERNET,
232         MFP_ARP,
233         NXM_NX_ARP_SHA,
234     }, {
235         MFF_ARP_THA, "arp_tha", NULL,
236         MF_FIELD_SIZES(mac),
237         MFM_NONE, FWW_ARP_THA,
238         MFS_ETHERNET,
239         MFP_ARP,
240         NXM_NX_ARP_THA,
241     },
242
243     /* ## -- ## */
244     /* ## L4 ## */
245     /* ## -- ## */
246
247     {
248         MFF_TCP_SRC, "tcp_src", "tp_src",
249         MF_FIELD_SIZES(be16),
250         MFM_NONE, FWW_TP_SRC,
251         MFS_DECIMAL,
252         MFP_TCP,
253         NXM_OF_TCP_SRC,
254     }, {
255         MFF_TCP_DST, "tcp_dst", "tp_dst",
256         MF_FIELD_SIZES(be16),
257         MFM_NONE, FWW_TP_DST,
258         MFS_DECIMAL,
259         MFP_TCP,
260         NXM_OF_TCP_DST,
261     },
262
263     {
264         MFF_UDP_SRC, "udp_src", NULL,
265         MF_FIELD_SIZES(be16),
266         MFM_NONE, FWW_TP_SRC,
267         MFS_DECIMAL,
268         MFP_UDP,
269         NXM_OF_UDP_SRC,
270     }, {
271         MFF_UDP_DST, "udp_dst", NULL,
272         MF_FIELD_SIZES(be16),
273         MFM_NONE, FWW_TP_DST,
274         MFS_DECIMAL,
275         MFP_UDP,
276         NXM_OF_UDP_DST,
277     },
278
279     {
280         MFF_ICMP_TYPE, "icmp_type", NULL,
281         MF_FIELD_SIZES(u8),
282         MFM_NONE, FWW_TP_SRC,
283         MFS_DECIMAL,
284         MFP_ICMP_ANY,
285         NXM_OF_ICMP_TYPE,
286     }, {
287         MFF_ICMP_CODE, "icmp_code", NULL,
288         MF_FIELD_SIZES(u8),
289         MFM_NONE, FWW_TP_DST,
290         MFS_DECIMAL,
291         MFP_ICMP_ANY,
292         NXM_OF_ICMP_CODE,
293     },
294
295     /* ## ---- ## */
296     /* ## L"5" ## */
297     /* ## ---- ## */
298
299     {
300         MFF_ND_TARGET, "nd_target", NULL,
301         MF_FIELD_SIZES(ipv6),
302         MFM_NONE, FWW_ND_TARGET,
303         MFS_IPV6,
304         MFP_ND,
305         NXM_NX_ND_TARGET,
306     }, {
307         MFF_ND_SLL, "nd_sll", NULL,
308         MF_FIELD_SIZES(mac),
309         MFM_NONE, FWW_ARP_SHA,
310         MFS_ETHERNET,
311         MFP_ND_SOLICIT,
312         NXM_NX_ND_SLL,
313     }, {
314         MFF_ND_TLL, "nd_tll", NULL,
315         MF_FIELD_SIZES(mac),
316         MFM_NONE, FWW_ARP_THA,
317         MFS_ETHERNET,
318         MFP_ND_ADVERT,
319         NXM_NX_ND_TLL,
320     }
321 };
322
323 /* Returns the field with the given 'id'. */
324 const struct mf_field *
325 mf_from_id(enum mf_field_id id)
326 {
327     assert((unsigned int) id < MFF_N_IDS);
328     return &mf_fields[id];
329 }
330
331 /* Returns the field with the given 'name', or a null pointer if no field has
332  * that name. */
333 const struct mf_field *
334 mf_from_name(const char *name)
335 {
336     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
337
338     if (shash_is_empty(&mf_by_name)) {
339         const struct mf_field *mf;
340
341         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
342             shash_add_once(&mf_by_name, mf->name, mf);
343             if (mf->extra_name) {
344                 shash_add_once(&mf_by_name, mf->extra_name, mf);
345             }
346         }
347     }
348
349     return shash_find_data(&mf_by_name, name);
350 }
351
352 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
353  * specifies at least one bit in the field.
354  *
355  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
356  * meets 'mf''s prerequisites. */
357 bool
358 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
359 {
360     switch (mf->id) {
361     case MFF_IN_PORT:
362     case MFF_ETH_SRC:
363     case MFF_ETH_TYPE:
364     case MFF_IP_PROTO:
365     case MFF_IPV6_LABEL:
366     case MFF_ARP_OP:
367     case MFF_ARP_SHA:
368     case MFF_ARP_THA:
369     case MFF_TCP_SRC:
370     case MFF_TCP_DST:
371     case MFF_UDP_SRC:
372     case MFF_UDP_DST:
373     case MFF_ICMP_TYPE:
374     case MFF_ICMP_CODE:
375     case MFF_ND_TARGET:
376     case MFF_ND_SLL:
377     case MFF_ND_TLL:
378         assert(mf->fww_bit != 0);
379         return (wc->wildcards & mf->fww_bit) != 0;
380
381     case MFF_TUN_ID:
382         return !wc->tun_id_mask;
383
384 #if FLOW_N_REGS > 0
385     case MFF_REG0:
386 #endif
387 #if FLOW_N_REGS > 1
388     case MFF_REG1:
389 #endif
390 #if FLOW_N_REGS > 2
391     case MFF_REG2:
392 #endif
393 #if FLOW_N_REGS > 3
394     case MFF_REG3:
395 #endif
396 #if FLOW_N_REGS > 4
397     case MFF_REG4:
398 #endif
399 #if FLOW_N_REGS > 5
400 #error
401 #endif
402         return !wc->reg_masks[mf->id - MFF_REG0];
403
404     case MFF_ETH_DST:
405         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
406                 == (FWW_ETH_MCAST | FWW_DL_DST));
407
408     case MFF_VLAN_TCI:
409         return !wc->vlan_tci_mask;
410     case MFF_VLAN_VID:
411         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
412     case MFF_VLAN_PCP:
413         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
414
415     case MFF_IPV4_SRC:
416         return !wc->nw_src_mask;
417     case MFF_IPV4_DST:
418         return !wc->nw_dst_mask;
419
420     case MFF_IPV6_SRC:
421         return ipv6_mask_is_any(&wc->ipv6_src_mask);
422     case MFF_IPV6_DST:
423         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
424
425     case MFF_IP_TOS:
426         return !(wc->tos_frag_mask & IP_DSCP_MASK);
427     case MFF_IP_FRAG:
428         return !(wc->tos_frag_mask & FLOW_FRAG_MASK);
429
430     case MFF_ARP_SPA:
431         return !wc->nw_src_mask;
432     case MFF_ARP_TPA:
433         return !wc->nw_dst_mask;
434
435     case MFF_N_IDS:
436     default:
437         NOT_REACHED();
438     }
439 }
440
441 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
442  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
443  * purposes, or to 0 if it is wildcarded.
444  *
445  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
446  * meets 'mf''s prerequisites. */
447 void
448 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
449             union mf_value *mask)
450 {
451     switch (mf->id) {
452     case MFF_IN_PORT:
453     case MFF_ETH_SRC:
454     case MFF_ETH_TYPE:
455     case MFF_IP_PROTO:
456     case MFF_IPV6_LABEL:
457     case MFF_ARP_OP:
458     case MFF_ARP_SHA:
459     case MFF_ARP_THA:
460     case MFF_TCP_SRC:
461     case MFF_TCP_DST:
462     case MFF_UDP_SRC:
463     case MFF_UDP_DST:
464     case MFF_ICMP_TYPE:
465     case MFF_ICMP_CODE:
466     case MFF_ND_TARGET:
467     case MFF_ND_SLL:
468     case MFF_ND_TLL:
469         assert(mf->fww_bit != 0);
470         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
471         break;
472
473     case MFF_TUN_ID:
474         mask->be64 = wc->tun_id_mask;
475         break;
476
477 #if FLOW_N_REGS > 0
478     case MFF_REG0:
479 #endif
480 #if FLOW_N_REGS > 1
481     case MFF_REG1:
482 #endif
483 #if FLOW_N_REGS > 2
484     case MFF_REG2:
485 #endif
486 #if FLOW_N_REGS > 3
487     case MFF_REG3:
488 #endif
489 #if FLOW_N_REGS > 4
490     case MFF_REG4:
491 #endif
492 #if FLOW_N_REGS > 5
493 #error
494 #endif
495         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
496         break;
497
498     case MFF_ETH_DST:
499         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
500                ETH_ADDR_LEN);
501         break;
502
503     case MFF_VLAN_TCI:
504         mask->be16 = wc->vlan_tci_mask;
505         break;
506     case MFF_VLAN_VID:
507         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
508         break;
509     case MFF_VLAN_PCP:
510         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
511         break;
512
513     case MFF_IPV4_SRC:
514         mask->be32 = wc->nw_src_mask;
515         break;
516     case MFF_IPV4_DST:
517         mask->be32 = wc->nw_dst_mask;
518         break;
519
520     case MFF_IPV6_SRC:
521         mask->ipv6 = wc->ipv6_src_mask;
522         break;
523     case MFF_IPV6_DST:
524         mask->ipv6 = wc->ipv6_dst_mask;
525         break;
526
527     case MFF_IP_TOS:
528         mask->u8 = wc->tos_frag_mask & IP_DSCP_MASK;
529         break;
530     case MFF_IP_FRAG:
531         mask->u8 = wc->tos_frag_mask & FLOW_FRAG_MASK;
532         break;
533
534     case MFF_ARP_SPA:
535         mask->be32 = wc->nw_src_mask;
536         break;
537     case MFF_ARP_TPA:
538         mask->be32 = wc->nw_dst_mask;
539         break;
540
541     case MFF_N_IDS:
542     default:
543         NOT_REACHED();
544     }
545 }
546
547 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
548  * if the mask is valid, false otherwise. */
549 bool
550 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
551 {
552     switch (mf->maskable) {
553     case MFM_NONE:
554         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
555                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
556
557     case MFM_FULLY:
558         return true;
559
560     case MFM_CIDR:
561         return (mf->n_bytes == 4
562                 ? ip_is_cidr(mask->be32)
563                 : ipv6_is_cidr(&mask->ipv6));
564
565     case MFM_MCAST:
566         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
567     }
568
569     NOT_REACHED();
570 }
571
572 static bool
573 is_ip_any(const struct flow *flow)
574 {
575     return (flow->dl_type == htons(ETH_TYPE_IP) ||
576             flow->dl_type == htons(ETH_TYPE_IPV6));
577 }
578
579 static bool
580 is_icmpv4(const struct flow *flow)
581 {
582     return (flow->dl_type == htons(ETH_TYPE_IP)
583             && flow->nw_proto == IPPROTO_ICMP);
584 }
585
586 static bool
587 is_icmpv6(const struct flow *flow)
588 {
589     return (flow->dl_type == htons(ETH_TYPE_IPV6)
590             && flow->nw_proto == IPPROTO_ICMPV6);
591 }
592
593 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
594 bool
595 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
596 {
597     switch (mf->prereqs) {
598     case MFP_NONE:
599         return true;
600
601     case MFP_ARP:
602         return flow->dl_type == htons(ETH_TYPE_ARP);
603     case MFP_IPV4:
604         return flow->dl_type == htons(ETH_TYPE_IP);
605     case MFP_IPV6:
606         return flow->dl_type == htons(ETH_TYPE_IPV6);
607     case MFP_IP_ANY:
608         return is_ip_any(flow);
609
610     case MFP_TCP:
611         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
612     case MFP_UDP:
613         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
614     case MFP_ICMPV6:
615         return is_icmpv6(flow);
616     case MFP_ICMP_ANY:
617         return is_icmpv4(flow) || is_icmpv6(flow);
618
619     case MFP_ND:
620         return (is_icmpv6(flow)
621                 && flow->tp_dst == htons(0)
622                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
623                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
624     case MFP_ND_SOLICIT:
625         return (is_icmpv6(flow)
626                 && flow->tp_dst == htons(0)
627                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
628     case MFP_ND_ADVERT:
629         return (is_icmpv6(flow)
630                 && flow->tp_dst == htons(0)
631                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
632     }
633
634     NOT_REACHED();
635 }
636
637 /* Returns true if 'value' may be a valid value *as part of a masked match*,
638  * false otherwise.
639  *
640  * A value is not rejected just because it is not valid for the field in
641  * question, but only if it doesn't make sense to test the bits in question at
642  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
643  * without the VLAN_CFI bit being set, but we can't reject those values because
644  * it is still legitimate to test just for those bits (see the documentation
645  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
646  * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
647 bool
648 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
649 {
650     switch (mf->id) {
651     case MFF_TUN_ID:
652     case MFF_IN_PORT:
653 #if FLOW_N_REGS > 0
654     case MFF_REG0:
655 #endif
656 #if FLOW_N_REGS > 1
657     case MFF_REG1:
658 #endif
659 #if FLOW_N_REGS > 2
660     case MFF_REG2:
661 #endif
662 #if FLOW_N_REGS > 3
663     case MFF_REG3:
664 #endif
665 #if FLOW_N_REGS > 4
666     case MFF_REG4:
667 #endif
668 #if FLOW_N_REGS > 5
669 #error
670 #endif
671     case MFF_ETH_SRC:
672     case MFF_ETH_DST:
673     case MFF_ETH_TYPE:
674     case MFF_VLAN_TCI:
675     case MFF_IPV4_SRC:
676     case MFF_IPV4_DST:
677     case MFF_IPV6_SRC:
678     case MFF_IPV6_DST:
679     case MFF_IP_PROTO:
680     case MFF_ARP_SPA:
681     case MFF_ARP_TPA:
682     case MFF_ARP_SHA:
683     case MFF_ARP_THA:
684     case MFF_TCP_SRC:
685     case MFF_TCP_DST:
686     case MFF_UDP_SRC:
687     case MFF_UDP_DST:
688     case MFF_ICMP_TYPE:
689     case MFF_ICMP_CODE:
690     case MFF_ND_TARGET:
691     case MFF_ND_SLL:
692     case MFF_ND_TLL:
693         return true;
694
695     case MFF_IP_TOS:
696         return !(value->u8 & ~IP_DSCP_MASK);
697     case MFF_IP_FRAG:
698         return !(value->u8 & ~FLOW_FRAG_MASK);
699
700     case MFF_ARP_OP:
701         return !(value->be16 & htons(0xff00));
702
703     case MFF_VLAN_VID:
704         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
705
706     case MFF_VLAN_PCP:
707         return !(value->u8 & ~7);
708
709     case MFF_IPV6_LABEL:
710         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
711
712     case MFF_N_IDS:
713     default:
714         NOT_REACHED();
715     }
716 }
717
718 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
719  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
720 void
721 mf_get_value(const struct mf_field *mf, const struct flow *flow,
722              union mf_value *value)
723 {
724     switch (mf->id) {
725     case MFF_TUN_ID:
726         value->be64 = flow->tun_id;
727         break;
728
729     case MFF_IN_PORT:
730         value->be16 = htons(flow->in_port);
731         break;
732
733 #if FLOW_N_REGS > 0
734     case MFF_REG0:
735 #endif
736 #if FLOW_N_REGS > 1
737     case MFF_REG1:
738 #endif
739 #if FLOW_N_REGS > 2
740     case MFF_REG2:
741 #endif
742 #if FLOW_N_REGS > 3
743     case MFF_REG3:
744 #endif
745 #if FLOW_N_REGS > 4
746     case MFF_REG4:
747 #endif
748 #if FLOW_N_REGS > 5
749 #error
750 #endif
751         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
752         break;
753
754     case MFF_ETH_SRC:
755         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
756         break;
757
758     case MFF_ETH_DST:
759         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
760         break;
761
762     case MFF_ETH_TYPE:
763         value->be16 = flow->dl_type;
764         break;
765
766     case MFF_VLAN_TCI:
767         value->be16 = flow->vlan_tci;
768         break;
769
770     case MFF_VLAN_VID:
771         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
772         break;
773
774     case MFF_VLAN_PCP:
775         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
776         break;
777
778     case MFF_IPV4_SRC:
779         value->be32 = flow->nw_src;
780         break;
781
782     case MFF_IPV4_DST:
783         value->be32 = flow->nw_dst;
784         break;
785
786     case MFF_IPV6_SRC:
787         value->ipv6 = flow->ipv6_src;
788         break;
789
790     case MFF_IPV6_DST:
791         value->ipv6 = flow->ipv6_dst;
792         break;
793
794     case MFF_IPV6_LABEL:
795         value->be32 = flow->ipv6_label;
796         break;
797
798     case MFF_IP_PROTO:
799         value->u8 = flow->nw_proto;
800         break;
801
802     case MFF_IP_TOS:
803         value->u8 = flow->tos_frag & IP_DSCP_MASK;
804         break;
805
806     case MFF_IP_FRAG:
807         value->u8 = flow->tos_frag & FLOW_FRAG_MASK;
808         break;
809
810     case MFF_ARP_OP:
811         value->be16 = htons(flow->nw_proto);
812         break;
813
814     case MFF_ARP_SPA:
815         value->be32 = flow->nw_src;
816         break;
817
818     case MFF_ARP_TPA:
819         value->be32 = flow->nw_dst;
820         break;
821
822     case MFF_ARP_SHA:
823     case MFF_ND_SLL:
824         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
825         break;
826
827     case MFF_ARP_THA:
828     case MFF_ND_TLL:
829         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
830         break;
831
832     case MFF_TCP_SRC:
833         value->be16 = flow->tp_src;
834         break;
835
836     case MFF_TCP_DST:
837         value->be16 = flow->tp_dst;
838         break;
839
840     case MFF_UDP_SRC:
841         value->be16 = flow->tp_src;
842         break;
843
844     case MFF_UDP_DST:
845         value->be16 = flow->tp_dst;
846         break;
847
848     case MFF_ICMP_TYPE:
849         value->u8 = ntohs(flow->tp_src);
850         break;
851
852     case MFF_ICMP_CODE:
853         value->u8 = ntohs(flow->tp_dst);
854         break;
855
856     case MFF_ND_TARGET:
857         value->ipv6 = flow->nd_target;
858         break;
859
860     case MFF_N_IDS:
861     default:
862         NOT_REACHED();
863     }
864 }
865
866 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
867  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
868  * prerequisites. */
869 void
870 mf_set_value(const struct mf_field *mf,
871              const union mf_value *value, struct cls_rule *rule)
872 {
873     switch (mf->id) {
874     case MFF_TUN_ID:
875         cls_rule_set_tun_id(rule, value->be64);
876         break;
877
878     case MFF_IN_PORT:
879         cls_rule_set_in_port(rule, ntohs(value->be16));
880         break;
881
882 #if FLOW_N_REGS > 0
883     case MFF_REG0:
884 #endif
885 #if FLOW_N_REGS > 1
886     case MFF_REG1:
887 #endif
888 #if FLOW_N_REGS > 2
889     case MFF_REG2:
890 #endif
891 #if FLOW_N_REGS > 3
892     case MFF_REG3:
893 #endif
894 #if FLOW_N_REGS > 4
895     case MFF_REG4:
896 #endif
897 #if FLOW_N_REGS > 5
898 #error
899 #endif
900 #if FLOW_N_REGS > 0
901         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
902         break;
903 #endif
904
905     case MFF_ETH_SRC:
906         cls_rule_set_dl_src(rule, value->mac);
907         break;
908
909     case MFF_ETH_DST:
910         cls_rule_set_dl_dst(rule, value->mac);
911         break;
912
913     case MFF_ETH_TYPE:
914         cls_rule_set_dl_type(rule, value->be16);
915         break;
916
917     case MFF_VLAN_TCI:
918         cls_rule_set_dl_tci(rule, value->be16);
919         break;
920
921     case MFF_VLAN_VID:
922         cls_rule_set_dl_vlan(rule, value->be16);
923         break;
924
925     case MFF_VLAN_PCP:
926         cls_rule_set_dl_vlan_pcp(rule, value->u8);
927         break;
928
929     case MFF_IPV4_SRC:
930         cls_rule_set_nw_src(rule, value->be32);
931         break;
932
933     case MFF_IPV4_DST:
934         cls_rule_set_nw_dst(rule, value->be32);
935         break;
936
937     case MFF_IPV6_SRC:
938         cls_rule_set_ipv6_src(rule, &value->ipv6);
939         break;
940
941     case MFF_IPV6_DST:
942         cls_rule_set_ipv6_dst(rule, &value->ipv6);
943         break;
944
945     case MFF_IPV6_LABEL:
946         cls_rule_set_ipv6_label(rule, value->be32);
947         break;
948
949     case MFF_IP_PROTO:
950         cls_rule_set_nw_proto(rule, value->u8);
951         break;
952
953     case MFF_IP_TOS:
954         cls_rule_set_nw_tos(rule, value->u8);
955         break;
956
957     case MFF_IP_FRAG:
958         cls_rule_set_frag(rule, value->u8);
959         break;
960
961     case MFF_ARP_OP:
962         cls_rule_set_nw_proto(rule, ntohs(value->be16));
963         break;
964
965     case MFF_ARP_SPA:
966         cls_rule_set_nw_src(rule, value->be32);
967         break;
968
969     case MFF_ARP_TPA:
970         cls_rule_set_nw_dst(rule, value->be32);
971         break;
972
973     case MFF_ARP_SHA:
974     case MFF_ND_SLL:
975         cls_rule_set_arp_sha(rule, value->mac);
976         break;
977
978     case MFF_ARP_THA:
979     case MFF_ND_TLL:
980         cls_rule_set_arp_tha(rule, value->mac);
981         break;
982
983     case MFF_TCP_SRC:
984         cls_rule_set_tp_src(rule, value->be16);
985         break;
986
987     case MFF_TCP_DST:
988         cls_rule_set_tp_dst(rule, value->be16);
989         break;
990
991     case MFF_UDP_SRC:
992         cls_rule_set_tp_src(rule, value->be16);
993         break;
994
995     case MFF_UDP_DST:
996         cls_rule_set_tp_dst(rule, value->be16);
997         break;
998
999     case MFF_ICMP_TYPE:
1000         cls_rule_set_icmp_type(rule, value->u8);
1001         break;
1002
1003     case MFF_ICMP_CODE:
1004         cls_rule_set_icmp_code(rule, value->u8);
1005         break;
1006
1007     case MFF_ND_TARGET:
1008         cls_rule_set_nd_target(rule, &value->ipv6);
1009         break;
1010
1011     case MFF_N_IDS:
1012     default:
1013         NOT_REACHED();
1014     }
1015 }
1016
1017 /* Makes 'rule' wildcard field 'mf'.
1018  *
1019  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1020  * prerequisites. */
1021 void
1022 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1023 {
1024     switch (mf->id) {
1025     case MFF_TUN_ID:
1026         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1027         break;
1028
1029     case MFF_IN_PORT:
1030         rule->wc.wildcards |= FWW_IN_PORT;
1031         rule->flow.in_port = 0;
1032         break;
1033
1034 #if FLOW_N_REGS > 0
1035     case MFF_REG0:
1036         cls_rule_set_reg_masked(rule, 0, 0, 0);
1037         break;
1038 #endif
1039 #if FLOW_N_REGS > 1
1040     case MFF_REG1:
1041         cls_rule_set_reg_masked(rule, 1, 0, 0);
1042         break;
1043 #endif
1044 #if FLOW_N_REGS > 2
1045     case MFF_REG2:
1046         cls_rule_set_reg_masked(rule, 2, 0, 0);
1047         break;
1048 #endif
1049 #if FLOW_N_REGS > 3
1050     case MFF_REG3:
1051         cls_rule_set_reg_masked(rule, 3, 0, 0);
1052         break;
1053 #endif
1054 #if FLOW_N_REGS > 4
1055     case MFF_REG4:
1056         cls_rule_set_reg_masked(rule, 4, 0, 0);
1057         break;
1058 #endif
1059 #if FLOW_N_REGS > 5
1060 #error
1061 #endif
1062
1063     case MFF_ETH_SRC:
1064         rule->wc.wildcards |= FWW_DL_SRC;
1065         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1066         break;
1067
1068     case MFF_ETH_DST:
1069         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1070         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1071         break;
1072
1073     case MFF_ETH_TYPE:
1074         rule->wc.wildcards |= FWW_DL_TYPE;
1075         rule->flow.dl_type = htons(0);
1076         break;
1077
1078     case MFF_VLAN_TCI:
1079         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1080         break;
1081
1082     case MFF_VLAN_VID:
1083         cls_rule_set_any_vid(rule);
1084         break;
1085
1086     case MFF_VLAN_PCP:
1087         cls_rule_set_any_pcp(rule);
1088         break;
1089
1090     case MFF_IPV4_SRC:
1091     case MFF_ARP_SPA:
1092         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1093         break;
1094
1095     case MFF_IPV4_DST:
1096     case MFF_ARP_TPA:
1097         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1098         break;
1099
1100     case MFF_IPV6_SRC:
1101         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1102         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1103         break;
1104
1105     case MFF_IPV6_DST:
1106         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1107         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1108         break;
1109
1110     case MFF_IPV6_LABEL:
1111         rule->wc.wildcards |= FWW_IPV6_LABEL;
1112         rule->flow.ipv6_label = 0;
1113         break;
1114
1115     case MFF_IP_PROTO:
1116         rule->wc.wildcards |= FWW_NW_PROTO;
1117         rule->flow.nw_proto = 0;
1118         break;
1119
1120     case MFF_IP_TOS:
1121         rule->wc.tos_frag_mask |= IP_DSCP_MASK;
1122         rule->flow.tos_frag &= ~IP_DSCP_MASK;
1123         break;
1124
1125     case MFF_IP_FRAG:
1126         rule->wc.tos_frag_mask |= FLOW_FRAG_MASK;
1127         rule->flow.tos_frag &= ~FLOW_FRAG_MASK;
1128         break;
1129
1130     case MFF_ARP_OP:
1131         rule->wc.wildcards |= FWW_NW_PROTO;
1132         rule->flow.nw_proto = 0;
1133         break;
1134
1135     case MFF_ARP_SHA:
1136     case MFF_ND_SLL:
1137         rule->wc.wildcards |= FWW_ARP_SHA;
1138         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1139         break;
1140
1141     case MFF_ARP_THA:
1142     case MFF_ND_TLL:
1143         rule->wc.wildcards |= FWW_ARP_THA;
1144         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1145         break;
1146
1147     case MFF_TCP_SRC:
1148     case MFF_UDP_SRC:
1149     case MFF_ICMP_TYPE:
1150         rule->wc.wildcards |= FWW_TP_SRC;
1151         rule->flow.tp_src = htons(0);
1152         break;
1153
1154     case MFF_TCP_DST:
1155     case MFF_UDP_DST:
1156     case MFF_ICMP_CODE:
1157         rule->wc.wildcards |= FWW_TP_DST;
1158         rule->flow.tp_dst = htons(0);
1159         break;
1160
1161     case MFF_ND_TARGET:
1162         rule->wc.wildcards |= FWW_ND_TARGET;
1163         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1164         break;
1165
1166     case MFF_N_IDS:
1167     default:
1168         NOT_REACHED();
1169     }
1170 }
1171
1172 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1173  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1174  * with a 1-bit indicating that the corresponding value bit must match and a
1175  * 0-bit indicating a don't-care.
1176  *
1177  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1178  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1179  * call is equivalent to mf_set_wild(mf, rule).
1180  *
1181  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1182  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1183 void
1184 mf_set(const struct mf_field *mf,
1185        const union mf_value *value, const union mf_value *mask,
1186        struct cls_rule *rule)
1187 {
1188     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1189         mf_set_value(mf, value, rule);
1190         return;
1191     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1192         mf_set_wild(mf, rule);
1193         return;
1194     }
1195
1196     switch (mf->id) {
1197     case MFF_IN_PORT:
1198     case MFF_ETH_SRC:
1199     case MFF_ETH_TYPE:
1200     case MFF_VLAN_VID:
1201     case MFF_VLAN_PCP:
1202     case MFF_IPV6_LABEL:
1203     case MFF_IP_PROTO:
1204     case MFF_IP_TOS:
1205     case MFF_ARP_OP:
1206     case MFF_ARP_SHA:
1207     case MFF_ARP_THA:
1208     case MFF_TCP_SRC:
1209     case MFF_TCP_DST:
1210     case MFF_UDP_SRC:
1211     case MFF_UDP_DST:
1212     case MFF_ICMP_TYPE:
1213     case MFF_ICMP_CODE:
1214     case MFF_ND_TARGET:
1215     case MFF_ND_SLL:
1216     case MFF_ND_TLL:
1217         NOT_REACHED();
1218
1219     case MFF_TUN_ID:
1220         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1221         break;
1222
1223 #if FLOW_N_REGS > 0
1224     case MFF_REG0:
1225 #endif
1226 #if FLOW_N_REGS > 1
1227     case MFF_REG1:
1228 #endif
1229 #if FLOW_N_REGS > 2
1230     case MFF_REG2:
1231 #endif
1232 #if FLOW_N_REGS > 3
1233     case MFF_REG3:
1234 #endif
1235 #if FLOW_N_REGS > 4
1236     case MFF_REG4:
1237 #endif
1238 #if FLOW_N_REGS > 5
1239 #error
1240 #endif
1241         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1242                                 ntohl(value->be32), ntohl(mask->be32));
1243         break;
1244
1245     case MFF_ETH_DST:
1246         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1247             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1248         }
1249         break;
1250
1251     case MFF_VLAN_TCI:
1252         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1253         break;
1254
1255     case MFF_IPV4_SRC:
1256         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1257         break;
1258
1259     case MFF_IPV4_DST:
1260         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1261         break;
1262
1263     case MFF_IPV6_SRC:
1264         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1265         break;
1266
1267     case MFF_IPV6_DST:
1268         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1269         break;
1270
1271     case MFF_IP_FRAG:
1272         cls_rule_set_frag_masked(rule, value->u8, mask->u8);
1273         break;
1274
1275     case MFF_ARP_SPA:
1276         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1277         break;
1278
1279     case MFF_ARP_TPA:
1280         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1281         break;
1282
1283     case MFF_N_IDS:
1284     default:
1285         NOT_REACHED();
1286     }
1287 }
1288
1289 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1290  * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1291  * 'x'.
1292  *
1293  * Example: suppose that 'mf' is originally the following 2-byte field in
1294  * 'rule':
1295  *
1296  *     value == 0xe00a == 2#1110000000001010
1297  *      mask == 0xfc3f == 2#1111110000111111
1298  *
1299  * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1300  * effect (note that 0x55 is 2#1010101):
1301  *
1302  *     value == 0xd50a == 2#1101010100001010
1303  *      mask == 0xff3f == 2#1111111100111111
1304  *
1305  * The caller is responsible for ensuring that the result will be a valid
1306  * wildcard pattern for 'mf'.  The caller is responsible for ensuring that
1307  * 'rule' meets 'mf''s prerequisites. */
1308 void
1309 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1310                 unsigned int n_bits, struct cls_rule *rule)
1311 {
1312     if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1313         union mf_value value;
1314         int i;
1315
1316         for (i = mf->n_bytes - 1; i >= 0; i--) {
1317             ((uint8_t *) &value)[i] = x;
1318             x >>= 8;
1319         }
1320         mf_set_value(mf, &value, rule);
1321     } else {
1322         union mf_value value, mask;
1323         uint8_t *vp, *mp;
1324         unsigned int byte_ofs;
1325
1326         mf_get(mf, rule, &value, &mask);
1327
1328         byte_ofs = mf->n_bytes - ofs / 8;
1329         vp = &((uint8_t *) &value)[byte_ofs];
1330         mp = &((uint8_t *) &mask)[byte_ofs];
1331         if (ofs % 8) {
1332             unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1333             uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1334
1335             *--vp &= ~chunk_mask;
1336             *vp   |= chunk_mask & (x << (ofs % 8));
1337             *--mp |= chunk_mask;
1338
1339             x >>= chunk;
1340             n_bits -= chunk;
1341             ofs += chunk;
1342         }
1343         while (n_bits >= 8) {
1344             *--vp = x;
1345             *--mp = 0xff;
1346             x >>= 8;
1347             n_bits -= 8;
1348             ofs += 8;
1349         }
1350         if (n_bits) {
1351             uint8_t chunk_mask = (1 << n_bits) - 1;
1352
1353             *--vp &= ~chunk_mask;
1354             *vp   |= chunk_mask & x;
1355             *--mp |= chunk_mask;
1356         }
1357
1358         mf_set(mf, &value, &mask, rule);
1359     }
1360 }
1361
1362 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1363  * 'value' and 'mask', respectively. */
1364 void
1365 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1366        union mf_value *value, union mf_value *mask)
1367 {
1368     mf_get_value(mf, &rule->flow, value);
1369     mf_get_mask(mf, &rule->wc, mask);
1370 }
1371
1372 /* Assigns a random value for field 'mf' to 'value'. */
1373 void
1374 mf_random_value(const struct mf_field *mf, union mf_value *value)
1375 {
1376     random_bytes(value, mf->n_bytes);
1377
1378     switch (mf->id) {
1379     case MFF_TUN_ID:
1380     case MFF_IN_PORT:
1381 #if FLOW_N_REGS > 0
1382     case MFF_REG0:
1383 #endif
1384 #if FLOW_N_REGS > 1
1385     case MFF_REG1:
1386 #endif
1387 #if FLOW_N_REGS > 2
1388     case MFF_REG2:
1389 #endif
1390 #if FLOW_N_REGS > 3
1391     case MFF_REG3:
1392 #endif
1393 #if FLOW_N_REGS > 4
1394     case MFF_REG4:
1395 #endif
1396 #if FLOW_N_REGS > 5
1397 #error
1398 #endif
1399     case MFF_ETH_SRC:
1400     case MFF_ETH_DST:
1401     case MFF_ETH_TYPE:
1402     case MFF_VLAN_TCI:
1403     case MFF_IPV4_SRC:
1404     case MFF_IPV4_DST:
1405     case MFF_IPV6_SRC:
1406     case MFF_IPV6_DST:
1407     case MFF_IP_PROTO:
1408     case MFF_ARP_SPA:
1409     case MFF_ARP_TPA:
1410     case MFF_ARP_SHA:
1411     case MFF_ARP_THA:
1412     case MFF_TCP_SRC:
1413     case MFF_TCP_DST:
1414     case MFF_UDP_SRC:
1415     case MFF_UDP_DST:
1416     case MFF_ICMP_TYPE:
1417     case MFF_ICMP_CODE:
1418     case MFF_ND_TARGET:
1419     case MFF_ND_SLL:
1420     case MFF_ND_TLL:
1421         break;
1422
1423     case MFF_IPV6_LABEL:
1424         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1425         break;
1426
1427     case MFF_IP_TOS:
1428         value->u8 &= ~0x03;
1429         break;
1430
1431     case MFF_IP_FRAG:
1432         value->u8 &= FLOW_FRAG_MASK;
1433         break;
1434
1435     case MFF_ARP_OP:
1436         value->be16 &= htons(0xff);
1437         break;
1438
1439     case MFF_VLAN_VID:
1440         value->be16 &= htons(VLAN_VID_MASK);
1441         break;
1442
1443     case MFF_VLAN_PCP:
1444         value->u8 &= 0x07;
1445         break;
1446
1447     case MFF_N_IDS:
1448     default:
1449         NOT_REACHED();
1450     }
1451 }
1452
1453 static char *
1454 mf_from_integer_string(const struct mf_field *mf, const char *s,
1455                        uint8_t *valuep, uint8_t *maskp)
1456 {
1457     unsigned long long int integer, mask;
1458     char *tail;
1459     int i;
1460
1461     errno = 0;
1462     integer = strtoull(s, &tail, 0);
1463     if (errno || (*tail != '\0' && *tail != '/')) {
1464         goto syntax_error;
1465     }
1466
1467     if (*tail == '/') {
1468         mask = strtoull(tail + 1, &tail, 0);
1469         if (errno || *tail != '\0') {
1470             goto syntax_error;
1471         }
1472     } else {
1473         mask = ULLONG_MAX;
1474     }
1475
1476     for (i = mf->n_bytes - 1; i >= 0; i--) {
1477         valuep[i] = integer;
1478         maskp[i] = mask;
1479         integer >>= 8;
1480         mask >>= 8;
1481     }
1482     if (integer) {
1483         return xasprintf("%s: value too large for %u-byte field %s",
1484                          s, mf->n_bytes, mf->name);
1485     }
1486     return NULL;
1487
1488 syntax_error:
1489     return xasprintf("%s: bad syntax for %s", s, mf->name);
1490 }
1491
1492 static char *
1493 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1494                         uint8_t mac[ETH_ADDR_LEN],
1495                         uint8_t mask[ETH_ADDR_LEN])
1496 {
1497     assert(mf->n_bytes == ETH_ADDR_LEN);
1498
1499     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1500                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1501     case ETH_ADDR_SCAN_COUNT * 2:
1502         return NULL;
1503
1504     case ETH_ADDR_SCAN_COUNT:
1505         memset(mask, 0xff, ETH_ADDR_LEN);
1506         return NULL;
1507
1508     default:
1509         return xasprintf("%s: invalid Ethernet address", s);
1510     }
1511 }
1512
1513 static char *
1514 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1515                     ovs_be32 *ip, ovs_be32 *mask)
1516 {
1517     int prefix;
1518
1519     assert(mf->n_bytes == sizeof *ip);
1520
1521     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1522                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1523         /* OK. */
1524     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1525                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1526         if (prefix <= 0 || prefix > 32) {
1527             return xasprintf("%s: network prefix bits not between 1 and "
1528                              "32", s);
1529         } else if (prefix == 32) {
1530             *mask = htonl(UINT32_MAX);
1531         } else {
1532             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1533         }
1534     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1535         *mask = htonl(UINT32_MAX);
1536     } else {
1537         return xasprintf("%s: invalid IP address", s);
1538     }
1539     return NULL;
1540 }
1541
1542 static char *
1543 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1544                     struct in6_addr *value, struct in6_addr *mask)
1545 {
1546     char *str = xstrdup(s);
1547     char *save_ptr = NULL;
1548     const char *name, *netmask;
1549     int retval;
1550
1551     assert(mf->n_bytes == sizeof *value);
1552
1553     name = strtok_r(str, "/", &save_ptr);
1554     retval = name ? lookup_ipv6(name, value) : EINVAL;
1555     if (retval) {
1556         char *err;
1557
1558         err = xasprintf("%s: could not convert to IPv6 address", str);
1559         free(str);
1560
1561         return err;
1562     }
1563
1564     netmask = strtok_r(NULL, "/", &save_ptr);
1565     if (netmask) {
1566         int prefix = atoi(netmask);
1567         if (prefix <= 0 || prefix > 128) {
1568             free(str);
1569             return xasprintf("%s: prefix bits not between 1 and 128", s);
1570         } else {
1571             *mask = ipv6_create_mask(prefix);
1572         }
1573     } else {
1574         *mask = in6addr_exact;
1575     }
1576     free(str);
1577
1578     return NULL;
1579 }
1580
1581 static char *
1582 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1583                         ovs_be16 *valuep, ovs_be16 *maskp)
1584 {
1585     uint16_t port;
1586
1587     assert(mf->n_bytes == sizeof(ovs_be16));
1588     if (ofputil_port_from_string(s, &port)) {
1589         *valuep = htons(port);
1590         *maskp = htons(UINT16_MAX);
1591         return NULL;
1592     } else {
1593         return mf_from_integer_string(mf, s,
1594                                       (uint8_t *) valuep, (uint8_t *) maskp);
1595     }
1596 }
1597
1598 struct frag_handling {
1599     const char *name;
1600     uint8_t mask;
1601     uint8_t value;
1602 };
1603
1604 static const struct frag_handling all_frags[] = {
1605 #define A FLOW_FRAG_ANY
1606 #define L FLOW_FRAG_LATER
1607     /* name               mask  value */
1608
1609     { "no",               A|L,  0     },
1610     { "first",            A|L,  A     },
1611     { "later",            A|L,  A|L   },
1612
1613     { "no",               A,    0     },
1614     { "yes",              A,    A     },
1615
1616     { "not_later",        L,    0     },
1617     { "later",            L,    L     },
1618 #undef A
1619 #undef L
1620 };
1621
1622 static char *
1623 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1624 {
1625     const struct frag_handling *h;
1626
1627     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1628         if (!strcasecmp(s, h->name)) {
1629             /* We force the upper bits of the mask on to make mf_parse_value()
1630              * happy (otherwise it will never think it's an exact match.) */
1631             *maskp = h->mask | ~FLOW_FRAG_MASK;
1632             *valuep = h->value;
1633             return NULL;
1634         }
1635     }
1636
1637     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1638                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
1639 }
1640
1641 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1642  * NULL if successful, otherwise a malloc()'d string describing the error. */
1643 char *
1644 mf_parse(const struct mf_field *mf, const char *s,
1645          union mf_value *value, union mf_value *mask)
1646 {
1647     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1648         memset(value, 0, mf->n_bytes);
1649         memset(mask, 0, mf->n_bytes);
1650         return NULL;
1651     }
1652
1653     switch (mf->string) {
1654     case MFS_DECIMAL:
1655     case MFS_HEXADECIMAL:
1656         return mf_from_integer_string(mf, s,
1657                                       (uint8_t *) value, (uint8_t *) mask);
1658
1659     case MFS_ETHERNET:
1660         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1661
1662     case MFS_IPV4:
1663         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1664
1665     case MFS_IPV6:
1666         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1667
1668     case MFS_OFP_PORT:
1669         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1670
1671     case MFS_FRAG:
1672         return mf_from_frag_string(s, &value->u8, &mask->u8);
1673     }
1674     NOT_REACHED();
1675 }
1676
1677 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1678  * successful, otherwise a malloc()'d string describing the error. */
1679 char *
1680 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1681 {
1682     union mf_value mask;
1683     char *error;
1684
1685     error = mf_parse(mf, s, value, &mask);
1686     if (error) {
1687         return error;
1688     }
1689
1690     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1691         return xasprintf("%s: wildcards not allowed here", s);
1692     }
1693     return NULL;
1694 }
1695
1696 static void
1697 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1698                          const uint8_t *maskp, struct ds *s)
1699 {
1700     unsigned long long int integer;
1701     int i;
1702
1703     assert(mf->n_bytes <= 8);
1704
1705     integer = 0;
1706     for (i = 0; i < mf->n_bytes; i++) {
1707         integer = (integer << 8) | valuep[i];
1708     }
1709     if (mf->string == MFS_HEXADECIMAL) {
1710         ds_put_format(s, "%#llx", integer);
1711     } else {
1712         ds_put_format(s, "%lld", integer);
1713     }
1714
1715     if (maskp) {
1716         unsigned long long int mask;
1717
1718         mask = 0;
1719         for (i = 0; i < mf->n_bytes; i++) {
1720             mask = (mask << 8) | maskp[i];
1721         }
1722
1723         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1724          * not sure that that a bit-mask written in decimal is ever easier to
1725          * understand than the same bit-mask written in hexadecimal. */
1726         ds_put_format(s, "/%#llx", mask);
1727     }
1728 }
1729
1730 static void
1731 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1732                       struct ds *s)
1733 {
1734     const struct frag_handling *h;
1735     uint8_t value = *valuep;
1736     uint8_t mask = *maskp;
1737
1738     value &= mask;
1739     mask &= FLOW_FRAG_MASK;
1740
1741     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1742         if (value == h->value && mask == h->mask) {
1743             ds_put_cstr(s, h->name);
1744             return;
1745         }
1746     }
1747     ds_put_cstr(s, "<error>");
1748 }
1749
1750 /* Appends to 's' a string representation of field 'mf' whose value is in
1751  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
1752 void
1753 mf_format(const struct mf_field *mf,
1754           const union mf_value *value, const union mf_value *mask,
1755           struct ds *s)
1756 {
1757     if (mask) {
1758         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1759             ds_put_cstr(s, "ANY");
1760             return;
1761         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1762             mask = NULL;
1763         }
1764     }
1765
1766     switch (mf->string) {
1767     case MFS_OFP_PORT:
1768         if (!mask) {
1769             ofputil_format_port(ntohs(value->be16), s);
1770             break;
1771         }
1772         /* fall through */
1773     case MFS_DECIMAL:
1774     case MFS_HEXADECIMAL:
1775         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1776         break;
1777
1778     case MFS_ETHERNET:
1779         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1780         if (mask) {
1781             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1782         }
1783         break;
1784
1785     case MFS_IPV4:
1786         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1787                          s);
1788         break;
1789
1790     case MFS_IPV6:
1791         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1792         break;
1793
1794     case MFS_FRAG:
1795         mf_format_frag_string(&value->u8, &mask->u8, s);
1796         break;
1797
1798     default:
1799         NOT_REACHED();
1800     }
1801 }