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