Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011, 2012 Nicira, Inc.
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-errors.h"
30 #include "ofp-util.h"
31 #include "packets.h"
32 #include "random.h"
33 #include "shash.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
39
40 #define MF_FIELD_SIZES(MEMBER)                  \
41     sizeof ((union mf_value *)0)->MEMBER,       \
42     8 * sizeof ((union mf_value *)0)->MEMBER
43
44 static const struct mf_field mf_fields[MFF_N_IDS] = {
45     /* ## -------- ## */
46     /* ## metadata ## */
47     /* ## -------- ## */
48
49     {
50         MFF_TUN_ID, "tun_id", NULL,
51         MF_FIELD_SIZES(be64),
52         MFM_FULLY, 0,
53         MFS_HEXADECIMAL,
54         MFP_NONE,
55         true,
56         NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
57         0, NULL,
58     }, {
59         MFF_IN_PORT, "in_port", NULL,
60         MF_FIELD_SIZES(be16),
61         MFM_NONE, FWW_IN_PORT,
62         MFS_OFP_PORT,
63         MFP_NONE,
64         false,
65         NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
66         OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
67     },
68
69 #define REGISTER(IDX)                           \
70     {                                           \
71         MFF_REG##IDX, "reg" #IDX, NULL,         \
72         MF_FIELD_SIZES(be32),                   \
73         MFM_FULLY, 0,                           \
74         MFS_HEXADECIMAL,                        \
75         MFP_NONE,                               \
76         true,                                   \
77         NXM_NX_REG(IDX),                        \
78         "NXM_NX_REG" #IDX,                      \
79         0, NULL,                                \
80     }
81 #if FLOW_N_REGS > 0
82     REGISTER(0),
83 #endif
84 #if FLOW_N_REGS > 1
85     REGISTER(1),
86 #endif
87 #if FLOW_N_REGS > 2
88     REGISTER(2),
89 #endif
90 #if FLOW_N_REGS > 3
91     REGISTER(3),
92 #endif
93 #if FLOW_N_REGS > 4
94     REGISTER(4),
95 #endif
96 #if FLOW_N_REGS > 5
97     REGISTER(5),
98 #endif
99 #if FLOW_N_REGS > 6
100     REGISTER(6),
101 #endif
102 #if FLOW_N_REGS > 7
103     REGISTER(7),
104 #endif
105 #if FLOW_N_REGS > 8
106 #error
107 #endif
108
109     /* ## -- ## */
110     /* ## L2 ## */
111     /* ## -- ## */
112
113     {
114         MFF_ETH_SRC, "eth_src", "dl_src",
115         MF_FIELD_SIZES(mac),
116         MFM_NONE, FWW_DL_SRC,
117         MFS_ETHERNET,
118         MFP_NONE,
119         true,
120         NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
121         OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
122     }, {
123         MFF_ETH_DST, "eth_dst", "dl_dst",
124         MF_FIELD_SIZES(mac),
125         MFM_MCAST, 0,
126         MFS_ETHERNET,
127         MFP_NONE,
128         true,
129         NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
130         OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
131     }, {
132         MFF_ETH_TYPE, "eth_type", "dl_type",
133         MF_FIELD_SIZES(be16),
134         MFM_NONE, FWW_DL_TYPE,
135         MFS_HEXADECIMAL,
136         MFP_NONE,
137         false,
138         NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
139         OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
140     },
141
142     {
143         MFF_VLAN_TCI, "vlan_tci", NULL,
144         MF_FIELD_SIZES(be16),
145         MFM_FULLY, 0,
146         MFS_HEXADECIMAL,
147         MFP_NONE,
148         true,
149         NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
150         0, NULL,
151     }, {
152         MFF_VLAN_VID, "dl_vlan", NULL,
153         sizeof(ovs_be16), 12,
154         MFM_NONE, 0,
155         MFS_DECIMAL,
156         MFP_NONE,
157         true,
158         0, NULL,
159         OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
160     }, {
161         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
162         1, 3,
163         MFM_NONE, 0,
164         MFS_DECIMAL,
165         MFP_NONE,
166         true,
167         0, NULL,
168         OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
169     },
170
171     /* ## -- ## */
172     /* ## L3 ## */
173     /* ## -- ## */
174
175     {
176         MFF_IPV4_SRC, "ip_src", "nw_src",
177         MF_FIELD_SIZES(be32),
178         MFM_CIDR, 0,
179         MFS_IPV4,
180         MFP_IPV4,
181         true,
182         NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
183         OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
184     }, {
185         MFF_IPV4_DST, "ip_dst", "nw_dst",
186         MF_FIELD_SIZES(be32),
187         MFM_CIDR, 0,
188         MFS_IPV4,
189         MFP_IPV4,
190         true,
191         NXM_OF_IP_DST, "NXM_OF_IP_DST",
192         OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
193     },
194
195     {
196         MFF_IPV6_SRC, "ipv6_src", NULL,
197         MF_FIELD_SIZES(ipv6),
198         MFM_CIDR, 0,
199         MFS_IPV6,
200         MFP_IPV6,
201         true,
202         NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
203         OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
204     }, {
205         MFF_IPV6_DST, "ipv6_dst", NULL,
206         MF_FIELD_SIZES(ipv6),
207         MFM_CIDR, 0,
208         MFS_IPV6,
209         MFP_IPV6,
210         true,
211         NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
212         OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
213     },
214     {
215         MFF_IPV6_LABEL, "ipv6_label", NULL,
216         4, 20,
217         MFM_NONE, FWW_IPV6_LABEL,
218         MFS_HEXADECIMAL,
219         MFP_IPV6,
220         false,
221         NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
222         OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
223     },
224
225     {
226         MFF_IP_PROTO, "nw_proto", NULL,
227         MF_FIELD_SIZES(u8),
228         MFM_NONE, FWW_NW_PROTO,
229         MFS_DECIMAL,
230         MFP_IP_ANY,
231         false,
232         NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
233         OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
234     }, {
235         MFF_IP_DSCP, "nw_tos", NULL,
236         MF_FIELD_SIZES(u8),
237         MFM_NONE, FWW_NW_DSCP,
238         MFS_DECIMAL,
239         MFP_IP_ANY,
240         true,
241         NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
242         OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
243     }, {
244         MFF_IP_ECN, "nw_ecn", NULL,
245         1, 2,
246         MFM_NONE, FWW_NW_ECN,
247         MFS_DECIMAL,
248         MFP_IP_ANY,
249         true,
250         NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
251         OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
252     }, {
253         MFF_IP_TTL, "nw_ttl", NULL,
254         MF_FIELD_SIZES(u8),
255         MFM_NONE, FWW_NW_TTL,
256         MFS_DECIMAL,
257         MFP_IP_ANY,
258         true,
259         NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
260         0, NULL,
261     }, {
262         MFF_IP_FRAG, "ip_frag", NULL,
263         1, 2,
264         MFM_FULLY, 0,
265         MFS_FRAG,
266         MFP_IP_ANY,
267         false,
268         NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
269         0, NULL,
270     },
271
272     {
273         MFF_ARP_OP, "arp_op", NULL,
274         MF_FIELD_SIZES(be16),
275         MFM_NONE, FWW_NW_PROTO,
276         MFS_DECIMAL,
277         MFP_ARP,
278         false,
279         NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
280         OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
281     }, {
282         MFF_ARP_SPA, "arp_spa", NULL,
283         MF_FIELD_SIZES(be32),
284         MFM_CIDR, 0,
285         MFS_IPV4,
286         MFP_ARP,
287         false,
288         NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
289         OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
290     }, {
291         MFF_ARP_TPA, "arp_tpa", NULL,
292         MF_FIELD_SIZES(be32),
293         MFM_CIDR, 0,
294         MFS_IPV4,
295         MFP_ARP,
296         false,
297         NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
298         OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
299     }, {
300         MFF_ARP_SHA, "arp_sha", NULL,
301         MF_FIELD_SIZES(mac),
302         MFM_NONE, FWW_ARP_SHA,
303         MFS_ETHERNET,
304         MFP_ARP,
305         false,
306         NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
307         OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
308     }, {
309         MFF_ARP_THA, "arp_tha", NULL,
310         MF_FIELD_SIZES(mac),
311         MFM_NONE, FWW_ARP_THA,
312         MFS_ETHERNET,
313         MFP_ARP,
314         false,
315         NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
316         OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
317     },
318
319     /* ## -- ## */
320     /* ## L4 ## */
321     /* ## -- ## */
322
323     {
324         MFF_TCP_SRC, "tcp_src", "tp_src",
325         MF_FIELD_SIZES(be16),
326         MFM_FULLY, 0,
327         MFS_DECIMAL,
328         MFP_TCP,
329         true,
330         NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
331         OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
332     }, {
333         MFF_TCP_DST, "tcp_dst", "tp_dst",
334         MF_FIELD_SIZES(be16),
335         MFM_FULLY, 0,
336         MFS_DECIMAL,
337         MFP_TCP,
338         true,
339         NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
340         OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
341     },
342
343     {
344         MFF_UDP_SRC, "udp_src", NULL,
345         MF_FIELD_SIZES(be16),
346         MFM_FULLY, 0,
347         MFS_DECIMAL,
348         MFP_UDP,
349         true,
350         NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
351         OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
352     }, {
353         MFF_UDP_DST, "udp_dst", NULL,
354         MF_FIELD_SIZES(be16),
355         MFM_FULLY, 0,
356         MFS_DECIMAL,
357         MFP_UDP,
358         true,
359         NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
360         OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
361     },
362
363     {
364         MFF_ICMPV4_TYPE, "icmp_type", NULL,
365         MF_FIELD_SIZES(u8),
366         MFM_NONE, 0,
367         MFS_DECIMAL,
368         MFP_ICMPV4,
369         false,
370         NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
371         OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
372     }, {
373         MFF_ICMPV4_CODE, "icmp_code", NULL,
374         MF_FIELD_SIZES(u8),
375         MFM_NONE, 0,
376         MFS_DECIMAL,
377         MFP_ICMPV4,
378         false,
379         NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
380         OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
381     },
382
383     {
384         MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
385         MF_FIELD_SIZES(u8),
386         MFM_NONE, 0,
387         MFS_DECIMAL,
388         MFP_ICMPV6,
389         false,
390         NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
391         OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
392     }, {
393         MFF_ICMPV6_CODE, "icmpv6_code", NULL,
394         MF_FIELD_SIZES(u8),
395         MFM_NONE, 0,
396         MFS_DECIMAL,
397         MFP_ICMPV6,
398         false,
399         NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
400         OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
401     },
402
403     /* ## ---- ## */
404     /* ## L"5" ## */
405     /* ## ---- ## */
406
407     {
408         MFF_ND_TARGET, "nd_target", NULL,
409         MF_FIELD_SIZES(ipv6),
410         MFM_CIDR, 0,
411         MFS_IPV6,
412         MFP_ND,
413         false,
414         NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
415         OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
416     }, {
417         MFF_ND_SLL, "nd_sll", NULL,
418         MF_FIELD_SIZES(mac),
419         MFM_NONE, FWW_ARP_SHA,
420         MFS_ETHERNET,
421         MFP_ND_SOLICIT,
422         false,
423         NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
424         OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
425     }, {
426         MFF_ND_TLL, "nd_tll", NULL,
427         MF_FIELD_SIZES(mac),
428         MFM_NONE, FWW_ARP_THA,
429         MFS_ETHERNET,
430         MFP_ND_ADVERT,
431         false,
432         NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
433         OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
434     }
435 };
436
437 struct nxm_field {
438     struct hmap_node hmap_node;
439     uint32_t nxm_header;
440     const struct mf_field *mf;
441 };
442
443 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
444
445 /* Rate limit for parse errors.  These always indicate a bug in an OpenFlow
446  * controller and so there's not much point in showing a lot of them. */
447 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
448
449 /* Returns the field with the given 'id'. */
450 const struct mf_field *
451 mf_from_id(enum mf_field_id id)
452 {
453     assert((unsigned int) id < MFF_N_IDS);
454     return &mf_fields[id];
455 }
456
457 /* Returns the field with the given 'name', or a null pointer if no field has
458  * that name. */
459 const struct mf_field *
460 mf_from_name(const char *name)
461 {
462     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
463
464     if (shash_is_empty(&mf_by_name)) {
465         const struct mf_field *mf;
466
467         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
468             shash_add_once(&mf_by_name, mf->name, mf);
469             if (mf->extra_name) {
470                 shash_add_once(&mf_by_name, mf->extra_name, mf);
471             }
472         }
473     }
474
475     return shash_find_data(&mf_by_name, name);
476 }
477
478 static void
479 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
480 {
481     struct nxm_field *f;
482
483     f = xmalloc(sizeof *f);
484     hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
485     f->nxm_header = nxm_header;
486     f->mf = mf;
487 }
488
489 static void
490 nxm_init(void)
491 {
492     const struct mf_field *mf;
493
494     for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
495         if (mf->nxm_header) {
496             add_nxm_field(mf->nxm_header, mf);
497             if (mf->maskable != MFM_NONE) {
498                 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
499             }
500         }
501     }
502
503 #ifndef NDEBUG
504     /* Verify that the header values are unique. */
505     for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
506         if (mf->nxm_header) {
507             assert(mf_from_nxm_header(mf->nxm_header) == mf);
508             if (mf->maskable != MFM_NONE) {
509                 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
510                        == mf);
511             }
512         }
513     }
514 #endif
515 }
516
517 const struct mf_field *
518 mf_from_nxm_header(uint32_t header)
519 {
520     const struct nxm_field *f;
521
522     if (hmap_is_empty(&all_nxm_fields)) {
523         nxm_init();
524     }
525
526     HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
527                              &all_nxm_fields) {
528         if (f->nxm_header == header) {
529             return f->mf;
530         }
531     }
532
533     return NULL;
534 }
535
536 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
537  * specifies at least one bit in the field.
538  *
539  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
540  * meets 'mf''s prerequisites. */
541 bool
542 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
543 {
544     switch (mf->id) {
545     case MFF_IN_PORT:
546     case MFF_ETH_SRC:
547     case MFF_ETH_TYPE:
548     case MFF_IP_PROTO:
549     case MFF_IP_DSCP:
550     case MFF_IP_ECN:
551     case MFF_IP_TTL:
552     case MFF_IPV6_LABEL:
553     case MFF_ARP_OP:
554     case MFF_ARP_SHA:
555     case MFF_ARP_THA:
556     case MFF_ND_SLL:
557     case MFF_ND_TLL:
558         assert(mf->fww_bit != 0);
559         return (wc->wildcards & mf->fww_bit) != 0;
560
561     case MFF_TUN_ID:
562         return !wc->tun_id_mask;
563
564 #if FLOW_N_REGS > 0
565     case MFF_REG0:
566 #endif
567 #if FLOW_N_REGS > 1
568     case MFF_REG1:
569 #endif
570 #if FLOW_N_REGS > 2
571     case MFF_REG2:
572 #endif
573 #if FLOW_N_REGS > 3
574     case MFF_REG3:
575 #endif
576 #if FLOW_N_REGS > 4
577     case MFF_REG4:
578 #endif
579 #if FLOW_N_REGS > 5
580     case MFF_REG5:
581 #endif
582 #if FLOW_N_REGS > 6
583     case MFF_REG6:
584 #endif
585 #if FLOW_N_REGS > 7
586     case MFF_REG7:
587 #endif
588 #if FLOW_N_REGS > 8
589 #error
590 #endif
591         return !wc->reg_masks[mf->id - MFF_REG0];
592
593     case MFF_ETH_DST:
594         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
595                 == (FWW_ETH_MCAST | FWW_DL_DST));
596
597     case MFF_VLAN_TCI:
598         return !wc->vlan_tci_mask;
599     case MFF_VLAN_VID:
600         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
601     case MFF_VLAN_PCP:
602         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
603
604     case MFF_IPV4_SRC:
605         return !wc->nw_src_mask;
606     case MFF_IPV4_DST:
607         return !wc->nw_dst_mask;
608
609     case MFF_IPV6_SRC:
610         return ipv6_mask_is_any(&wc->ipv6_src_mask);
611     case MFF_IPV6_DST:
612         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
613
614     case MFF_ND_TARGET:
615         return ipv6_mask_is_any(&wc->nd_target_mask);
616
617     case MFF_IP_FRAG:
618         return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
619
620     case MFF_ARP_SPA:
621         return !wc->nw_src_mask;
622     case MFF_ARP_TPA:
623         return !wc->nw_dst_mask;
624
625     case MFF_TCP_SRC:
626     case MFF_UDP_SRC:
627     case MFF_ICMPV4_TYPE:
628     case MFF_ICMPV6_TYPE:
629         return !wc->tp_src_mask;
630     case MFF_TCP_DST:
631     case MFF_UDP_DST:
632     case MFF_ICMPV4_CODE:
633     case MFF_ICMPV6_CODE:
634         return !wc->tp_dst_mask;
635
636     case MFF_N_IDS:
637     default:
638         NOT_REACHED();
639     }
640 }
641
642 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
643  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
644  * purposes, or to 0 if it is wildcarded.
645  *
646  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
647  * meets 'mf''s prerequisites. */
648 void
649 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
650             union mf_value *mask)
651 {
652     switch (mf->id) {
653     case MFF_IN_PORT:
654     case MFF_ETH_SRC:
655     case MFF_ETH_TYPE:
656     case MFF_IP_PROTO:
657     case MFF_IP_DSCP:
658     case MFF_IP_ECN:
659     case MFF_IP_TTL:
660     case MFF_IPV6_LABEL:
661     case MFF_ARP_OP:
662     case MFF_ARP_SHA:
663     case MFF_ARP_THA:
664     case MFF_ND_SLL:
665     case MFF_ND_TLL:
666         assert(mf->fww_bit != 0);
667         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
668         break;
669
670     case MFF_TUN_ID:
671         mask->be64 = wc->tun_id_mask;
672         break;
673
674 #if FLOW_N_REGS > 0
675     case MFF_REG0:
676 #endif
677 #if FLOW_N_REGS > 1
678     case MFF_REG1:
679 #endif
680 #if FLOW_N_REGS > 2
681     case MFF_REG2:
682 #endif
683 #if FLOW_N_REGS > 3
684     case MFF_REG3:
685 #endif
686 #if FLOW_N_REGS > 4
687     case MFF_REG4:
688 #endif
689 #if FLOW_N_REGS > 5
690     case MFF_REG5:
691 #endif
692 #if FLOW_N_REGS > 6
693     case MFF_REG6:
694 #endif
695 #if FLOW_N_REGS > 7
696     case MFF_REG7:
697 #endif
698 #if FLOW_N_REGS > 8
699 #error
700 #endif
701         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
702         break;
703
704     case MFF_ETH_DST:
705         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
706                ETH_ADDR_LEN);
707         break;
708
709     case MFF_VLAN_TCI:
710         mask->be16 = wc->vlan_tci_mask;
711         break;
712     case MFF_VLAN_VID:
713         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
714         break;
715     case MFF_VLAN_PCP:
716         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
717         break;
718
719     case MFF_IPV4_SRC:
720         mask->be32 = wc->nw_src_mask;
721         break;
722     case MFF_IPV4_DST:
723         mask->be32 = wc->nw_dst_mask;
724         break;
725
726     case MFF_IPV6_SRC:
727         mask->ipv6 = wc->ipv6_src_mask;
728         break;
729     case MFF_IPV6_DST:
730         mask->ipv6 = wc->ipv6_dst_mask;
731         break;
732
733     case MFF_ND_TARGET:
734         mask->ipv6 = wc->nd_target_mask;
735         break;
736
737     case MFF_IP_FRAG:
738         mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
739         break;
740
741     case MFF_ARP_SPA:
742         mask->be32 = wc->nw_src_mask;
743         break;
744     case MFF_ARP_TPA:
745         mask->be32 = wc->nw_dst_mask;
746         break;
747
748     case MFF_TCP_SRC:
749     case MFF_UDP_SRC:
750         mask->be16 = wc->tp_src_mask;
751         break;
752     case MFF_TCP_DST:
753     case MFF_UDP_DST:
754         mask->be16 = wc->tp_dst_mask;
755         break;
756
757     case MFF_ICMPV4_TYPE:
758     case MFF_ICMPV6_TYPE:
759         mask->u8 = ntohs(wc->tp_src_mask);
760         break;
761     case MFF_ICMPV4_CODE:
762     case MFF_ICMPV6_CODE:
763         mask->u8 = ntohs(wc->tp_dst_mask);
764         break;
765
766     case MFF_N_IDS:
767     default:
768         NOT_REACHED();
769     }
770 }
771
772 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
773  * if the mask is valid, false otherwise. */
774 bool
775 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
776 {
777     switch (mf->maskable) {
778     case MFM_NONE:
779         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
780                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
781
782     case MFM_FULLY:
783         return true;
784
785     case MFM_CIDR:
786         return (mf->n_bytes == 4
787                 ? ip_is_cidr(mask->be32)
788                 : ipv6_is_cidr(&mask->ipv6));
789
790     case MFM_MCAST:
791         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
792     }
793
794     NOT_REACHED();
795 }
796
797 static bool
798 is_ip_any(const struct flow *flow)
799 {
800     return (flow->dl_type == htons(ETH_TYPE_IP) ||
801             flow->dl_type == htons(ETH_TYPE_IPV6));
802 }
803
804 static bool
805 is_icmpv4(const struct flow *flow)
806 {
807     return (flow->dl_type == htons(ETH_TYPE_IP)
808             && flow->nw_proto == IPPROTO_ICMP);
809 }
810
811 static bool
812 is_icmpv6(const struct flow *flow)
813 {
814     return (flow->dl_type == htons(ETH_TYPE_IPV6)
815             && flow->nw_proto == IPPROTO_ICMPV6);
816 }
817
818 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
819 bool
820 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
821 {
822     switch (mf->prereqs) {
823     case MFP_NONE:
824         return true;
825
826     case MFP_ARP:
827         return flow->dl_type == htons(ETH_TYPE_ARP);
828     case MFP_IPV4:
829         return flow->dl_type == htons(ETH_TYPE_IP);
830     case MFP_IPV6:
831         return flow->dl_type == htons(ETH_TYPE_IPV6);
832     case MFP_IP_ANY:
833         return is_ip_any(flow);
834
835     case MFP_TCP:
836         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
837     case MFP_UDP:
838         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
839     case MFP_ICMPV4:
840         return is_icmpv4(flow);
841     case MFP_ICMPV6:
842         return is_icmpv6(flow);
843
844     case MFP_ND:
845         return (is_icmpv6(flow)
846                 && flow->tp_dst == htons(0)
847                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
848                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
849     case MFP_ND_SOLICIT:
850         return (is_icmpv6(flow)
851                 && flow->tp_dst == htons(0)
852                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
853     case MFP_ND_ADVERT:
854         return (is_icmpv6(flow)
855                 && flow->tp_dst == htons(0)
856                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
857     }
858
859     NOT_REACHED();
860 }
861
862 /* Returns true if 'value' may be a valid value *as part of a masked match*,
863  * false otherwise.
864  *
865  * A value is not rejected just because it is not valid for the field in
866  * question, but only if it doesn't make sense to test the bits in question at
867  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
868  * without the VLAN_CFI bit being set, but we can't reject those values because
869  * it is still legitimate to test just for those bits (see the documentation
870  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
871  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
872 bool
873 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
874 {
875     switch (mf->id) {
876     case MFF_TUN_ID:
877     case MFF_IN_PORT:
878 #if FLOW_N_REGS > 0
879     case MFF_REG0:
880 #endif
881 #if FLOW_N_REGS > 1
882     case MFF_REG1:
883 #endif
884 #if FLOW_N_REGS > 2
885     case MFF_REG2:
886 #endif
887 #if FLOW_N_REGS > 3
888     case MFF_REG3:
889 #endif
890 #if FLOW_N_REGS > 4
891     case MFF_REG4:
892 #endif
893 #if FLOW_N_REGS > 5
894     case MFF_REG5:
895 #endif
896 #if FLOW_N_REGS > 6
897     case MFF_REG6:
898 #endif
899 #if FLOW_N_REGS > 7
900     case MFF_REG7:
901 #endif
902 #if FLOW_N_REGS > 8
903 #error
904 #endif
905     case MFF_ETH_SRC:
906     case MFF_ETH_DST:
907     case MFF_ETH_TYPE:
908     case MFF_VLAN_TCI:
909     case MFF_IPV4_SRC:
910     case MFF_IPV4_DST:
911     case MFF_IPV6_SRC:
912     case MFF_IPV6_DST:
913     case MFF_IP_PROTO:
914     case MFF_IP_TTL:
915     case MFF_ARP_SPA:
916     case MFF_ARP_TPA:
917     case MFF_ARP_SHA:
918     case MFF_ARP_THA:
919     case MFF_TCP_SRC:
920     case MFF_TCP_DST:
921     case MFF_UDP_SRC:
922     case MFF_UDP_DST:
923     case MFF_ICMPV4_TYPE:
924     case MFF_ICMPV4_CODE:
925     case MFF_ICMPV6_TYPE:
926     case MFF_ICMPV6_CODE:
927     case MFF_ND_TARGET:
928     case MFF_ND_SLL:
929     case MFF_ND_TLL:
930         return true;
931
932     case MFF_IP_DSCP:
933         return !(value->u8 & ~IP_DSCP_MASK);
934     case MFF_IP_ECN:
935         return !(value->u8 & ~IP_ECN_MASK);
936     case MFF_IP_FRAG:
937         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
938
939     case MFF_ARP_OP:
940         return !(value->be16 & htons(0xff00));
941
942     case MFF_VLAN_VID:
943         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
944
945     case MFF_VLAN_PCP:
946         return !(value->u8 & ~7);
947
948     case MFF_IPV6_LABEL:
949         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
950
951     case MFF_N_IDS:
952     default:
953         NOT_REACHED();
954     }
955 }
956
957 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
958  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
959 void
960 mf_get_value(const struct mf_field *mf, const struct flow *flow,
961              union mf_value *value)
962 {
963     switch (mf->id) {
964     case MFF_TUN_ID:
965         value->be64 = flow->tun_id;
966         break;
967
968     case MFF_IN_PORT:
969         value->be16 = htons(flow->in_port);
970         break;
971
972 #if FLOW_N_REGS > 0
973     case MFF_REG0:
974 #endif
975 #if FLOW_N_REGS > 1
976     case MFF_REG1:
977 #endif
978 #if FLOW_N_REGS > 2
979     case MFF_REG2:
980 #endif
981 #if FLOW_N_REGS > 3
982     case MFF_REG3:
983 #endif
984 #if FLOW_N_REGS > 4
985     case MFF_REG4:
986 #endif
987 #if FLOW_N_REGS > 5
988     case MFF_REG5:
989 #endif
990 #if FLOW_N_REGS > 6
991     case MFF_REG6:
992 #endif
993 #if FLOW_N_REGS > 7
994     case MFF_REG7:
995 #endif
996 #if FLOW_N_REGS > 8
997 #error
998 #endif
999         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1000         break;
1001
1002     case MFF_ETH_SRC:
1003         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1004         break;
1005
1006     case MFF_ETH_DST:
1007         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1008         break;
1009
1010     case MFF_ETH_TYPE:
1011         value->be16 = flow->dl_type;
1012         break;
1013
1014     case MFF_VLAN_TCI:
1015         value->be16 = flow->vlan_tci;
1016         break;
1017
1018     case MFF_VLAN_VID:
1019         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1020         break;
1021
1022     case MFF_VLAN_PCP:
1023         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1024         break;
1025
1026     case MFF_IPV4_SRC:
1027         value->be32 = flow->nw_src;
1028         break;
1029
1030     case MFF_IPV4_DST:
1031         value->be32 = flow->nw_dst;
1032         break;
1033
1034     case MFF_IPV6_SRC:
1035         value->ipv6 = flow->ipv6_src;
1036         break;
1037
1038     case MFF_IPV6_DST:
1039         value->ipv6 = flow->ipv6_dst;
1040         break;
1041
1042     case MFF_IPV6_LABEL:
1043         value->be32 = flow->ipv6_label;
1044         break;
1045
1046     case MFF_IP_PROTO:
1047         value->u8 = flow->nw_proto;
1048         break;
1049
1050     case MFF_IP_DSCP:
1051         value->u8 = flow->nw_tos & IP_DSCP_MASK;
1052         break;
1053
1054     case MFF_IP_ECN:
1055         value->u8 = flow->nw_tos & IP_ECN_MASK;
1056         break;
1057
1058     case MFF_IP_TTL:
1059         value->u8 = flow->nw_ttl;
1060         break;
1061
1062     case MFF_IP_FRAG:
1063         value->u8 = flow->nw_frag;
1064         break;
1065
1066     case MFF_ARP_OP:
1067         value->be16 = htons(flow->nw_proto);
1068         break;
1069
1070     case MFF_ARP_SPA:
1071         value->be32 = flow->nw_src;
1072         break;
1073
1074     case MFF_ARP_TPA:
1075         value->be32 = flow->nw_dst;
1076         break;
1077
1078     case MFF_ARP_SHA:
1079     case MFF_ND_SLL:
1080         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1081         break;
1082
1083     case MFF_ARP_THA:
1084     case MFF_ND_TLL:
1085         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1086         break;
1087
1088     case MFF_TCP_SRC:
1089         value->be16 = flow->tp_src;
1090         break;
1091
1092     case MFF_TCP_DST:
1093         value->be16 = flow->tp_dst;
1094         break;
1095
1096     case MFF_UDP_SRC:
1097         value->be16 = flow->tp_src;
1098         break;
1099
1100     case MFF_UDP_DST:
1101         value->be16 = flow->tp_dst;
1102         break;
1103
1104     case MFF_ICMPV4_TYPE:
1105     case MFF_ICMPV6_TYPE:
1106         value->u8 = ntohs(flow->tp_src);
1107         break;
1108
1109     case MFF_ICMPV4_CODE:
1110     case MFF_ICMPV6_CODE:
1111         value->u8 = ntohs(flow->tp_dst);
1112         break;
1113
1114     case MFF_ND_TARGET:
1115         value->ipv6 = flow->nd_target;
1116         break;
1117
1118     case MFF_N_IDS:
1119     default:
1120         NOT_REACHED();
1121     }
1122 }
1123
1124 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1125  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1126  * prerequisites. */
1127 void
1128 mf_set_value(const struct mf_field *mf,
1129              const union mf_value *value, struct cls_rule *rule)
1130 {
1131     switch (mf->id) {
1132     case MFF_TUN_ID:
1133         cls_rule_set_tun_id(rule, value->be64);
1134         break;
1135
1136     case MFF_IN_PORT:
1137         cls_rule_set_in_port(rule, ntohs(value->be16));
1138         break;
1139
1140 #if FLOW_N_REGS > 0
1141     case MFF_REG0:
1142 #endif
1143 #if FLOW_N_REGS > 1
1144     case MFF_REG1:
1145 #endif
1146 #if FLOW_N_REGS > 2
1147     case MFF_REG2:
1148 #endif
1149 #if FLOW_N_REGS > 3
1150     case MFF_REG3:
1151 #endif
1152 #if FLOW_N_REGS > 4
1153     case MFF_REG4:
1154 #endif
1155 #if FLOW_N_REGS > 5
1156     case MFF_REG5:
1157 #endif
1158 #if FLOW_N_REGS > 6
1159     case MFF_REG6:
1160 #endif
1161 #if FLOW_N_REGS > 7
1162     case MFF_REG7:
1163 #endif
1164 #if FLOW_N_REGS > 8
1165 #error
1166 #endif
1167 #if FLOW_N_REGS > 0
1168         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1169         break;
1170 #endif
1171
1172     case MFF_ETH_SRC:
1173         cls_rule_set_dl_src(rule, value->mac);
1174         break;
1175
1176     case MFF_ETH_DST:
1177         cls_rule_set_dl_dst(rule, value->mac);
1178         break;
1179
1180     case MFF_ETH_TYPE:
1181         cls_rule_set_dl_type(rule, value->be16);
1182         break;
1183
1184     case MFF_VLAN_TCI:
1185         cls_rule_set_dl_tci(rule, value->be16);
1186         break;
1187
1188     case MFF_VLAN_VID:
1189         cls_rule_set_dl_vlan(rule, value->be16);
1190         break;
1191
1192     case MFF_VLAN_PCP:
1193         cls_rule_set_dl_vlan_pcp(rule, value->u8);
1194         break;
1195
1196     case MFF_IPV4_SRC:
1197         cls_rule_set_nw_src(rule, value->be32);
1198         break;
1199
1200     case MFF_IPV4_DST:
1201         cls_rule_set_nw_dst(rule, value->be32);
1202         break;
1203
1204     case MFF_IPV6_SRC:
1205         cls_rule_set_ipv6_src(rule, &value->ipv6);
1206         break;
1207
1208     case MFF_IPV6_DST:
1209         cls_rule_set_ipv6_dst(rule, &value->ipv6);
1210         break;
1211
1212     case MFF_IPV6_LABEL:
1213         cls_rule_set_ipv6_label(rule, value->be32);
1214         break;
1215
1216     case MFF_IP_PROTO:
1217         cls_rule_set_nw_proto(rule, value->u8);
1218         break;
1219
1220     case MFF_IP_DSCP:
1221         cls_rule_set_nw_dscp(rule, value->u8);
1222         break;
1223
1224     case MFF_IP_ECN:
1225         cls_rule_set_nw_ecn(rule, value->u8);
1226         break;
1227
1228     case MFF_IP_TTL:
1229         cls_rule_set_nw_ttl(rule, value->u8);
1230         break;
1231
1232     case MFF_IP_FRAG:
1233         cls_rule_set_nw_frag(rule, value->u8);
1234         break;
1235
1236     case MFF_ARP_OP:
1237         cls_rule_set_nw_proto(rule, ntohs(value->be16));
1238         break;
1239
1240     case MFF_ARP_SPA:
1241         cls_rule_set_nw_src(rule, value->be32);
1242         break;
1243
1244     case MFF_ARP_TPA:
1245         cls_rule_set_nw_dst(rule, value->be32);
1246         break;
1247
1248     case MFF_ARP_SHA:
1249     case MFF_ND_SLL:
1250         cls_rule_set_arp_sha(rule, value->mac);
1251         break;
1252
1253     case MFF_ARP_THA:
1254     case MFF_ND_TLL:
1255         cls_rule_set_arp_tha(rule, value->mac);
1256         break;
1257
1258     case MFF_TCP_SRC:
1259         cls_rule_set_tp_src(rule, value->be16);
1260         break;
1261
1262     case MFF_TCP_DST:
1263         cls_rule_set_tp_dst(rule, value->be16);
1264         break;
1265
1266     case MFF_UDP_SRC:
1267         cls_rule_set_tp_src(rule, value->be16);
1268         break;
1269
1270     case MFF_UDP_DST:
1271         cls_rule_set_tp_dst(rule, value->be16);
1272         break;
1273
1274     case MFF_ICMPV4_TYPE:
1275     case MFF_ICMPV6_TYPE:
1276         cls_rule_set_icmp_type(rule, value->u8);
1277         break;
1278
1279     case MFF_ICMPV4_CODE:
1280     case MFF_ICMPV6_CODE:
1281         cls_rule_set_icmp_code(rule, value->u8);
1282         break;
1283
1284     case MFF_ND_TARGET:
1285         cls_rule_set_nd_target(rule, &value->ipv6);
1286         break;
1287
1288     case MFF_N_IDS:
1289     default:
1290         NOT_REACHED();
1291     }
1292 }
1293
1294 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1295  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1296  * prerequisites. */
1297 void
1298 mf_set_flow_value(const struct mf_field *mf,
1299                   const union mf_value *value, struct flow *flow)
1300 {
1301     switch (mf->id) {
1302     case MFF_TUN_ID:
1303         flow->tun_id = value->be64;
1304         break;
1305
1306     case MFF_IN_PORT:
1307         flow->in_port = ntohs(value->be16);
1308         break;
1309
1310 #if FLOW_N_REGS > 0
1311     case MFF_REG0:
1312 #endif
1313 #if FLOW_N_REGS > 1
1314     case MFF_REG1:
1315 #endif
1316 #if FLOW_N_REGS > 2
1317     case MFF_REG2:
1318 #endif
1319 #if FLOW_N_REGS > 3
1320     case MFF_REG3:
1321 #endif
1322 #if FLOW_N_REGS > 4
1323     case MFF_REG4:
1324 #endif
1325 #if FLOW_N_REGS > 5
1326     case MFF_REG5:
1327 #endif
1328 #if FLOW_N_REGS > 6
1329     case MFF_REG6:
1330 #endif
1331 #if FLOW_N_REGS > 7
1332     case MFF_REG7:
1333 #endif
1334 #if FLOW_N_REGS > 8
1335 #error
1336 #endif
1337 #if FLOW_N_REGS > 0
1338         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1339         break;
1340 #endif
1341
1342     case MFF_ETH_SRC:
1343         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1344         break;
1345
1346     case MFF_ETH_DST:
1347         memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1348         break;
1349
1350     case MFF_ETH_TYPE:
1351         flow->dl_type = value->be16;
1352         break;
1353
1354     case MFF_VLAN_TCI:
1355         flow->vlan_tci = value->be16;
1356         break;
1357
1358     case MFF_VLAN_VID:
1359         flow_set_vlan_vid(flow, value->be16);
1360         break;
1361
1362     case MFF_VLAN_PCP:
1363         flow_set_vlan_pcp(flow, value->u8);
1364         break;
1365
1366     case MFF_IPV4_SRC:
1367         flow->nw_src = value->be32;
1368         break;
1369
1370     case MFF_IPV4_DST:
1371         flow->nw_dst = value->be32;
1372         break;
1373
1374     case MFF_IPV6_SRC:
1375         flow->ipv6_src = value->ipv6;
1376         break;
1377
1378     case MFF_IPV6_DST:
1379         flow->ipv6_dst = value->ipv6;
1380         break;
1381
1382     case MFF_IPV6_LABEL:
1383         flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1384         break;
1385
1386     case MFF_IP_PROTO:
1387         flow->nw_proto = value->u8;
1388         break;
1389
1390     case MFF_IP_DSCP:
1391         flow->nw_tos &= ~IP_DSCP_MASK;
1392         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1393         break;
1394
1395     case MFF_IP_ECN:
1396         flow->nw_tos &= ~IP_ECN_MASK;
1397         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1398         break;
1399
1400     case MFF_IP_TTL:
1401         flow->nw_ttl = value->u8;
1402         break;
1403
1404     case MFF_IP_FRAG:
1405         flow->nw_frag &= value->u8;
1406         break;
1407
1408     case MFF_ARP_OP:
1409         flow->nw_proto = ntohs(value->be16);
1410         break;
1411
1412     case MFF_ARP_SPA:
1413         flow->nw_src = value->be32;
1414         break;
1415
1416     case MFF_ARP_TPA:
1417         flow->nw_dst = value->be32;
1418         break;
1419
1420     case MFF_ARP_SHA:
1421     case MFF_ND_SLL:
1422         memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1423         break;
1424
1425     case MFF_ARP_THA:
1426     case MFF_ND_TLL:
1427         memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1428         break;
1429
1430     case MFF_TCP_SRC:
1431     case MFF_UDP_SRC:
1432         flow->tp_src = value->be16;
1433         break;
1434
1435     case MFF_TCP_DST:
1436     case MFF_UDP_DST:
1437         flow->tp_dst = value->be16;
1438         break;
1439
1440     case MFF_ICMPV4_TYPE:
1441     case MFF_ICMPV6_TYPE:
1442         flow->tp_src = htons(value->u8);
1443         break;
1444
1445     case MFF_ICMPV4_CODE:
1446     case MFF_ICMPV6_CODE:
1447         flow->tp_dst = htons(value->u8);
1448         break;
1449
1450     case MFF_ND_TARGET:
1451         flow->nd_target = value->ipv6;
1452         break;
1453
1454     case MFF_N_IDS:
1455     default:
1456         NOT_REACHED();
1457     }
1458 }
1459
1460 /* Makes 'rule' wildcard field 'mf'.
1461  *
1462  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1463  * prerequisites. */
1464 void
1465 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1466 {
1467     switch (mf->id) {
1468     case MFF_TUN_ID:
1469         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1470         break;
1471
1472     case MFF_IN_PORT:
1473         rule->wc.wildcards |= FWW_IN_PORT;
1474         rule->flow.in_port = 0;
1475         break;
1476
1477 #if FLOW_N_REGS > 0
1478     case MFF_REG0:
1479         cls_rule_set_reg_masked(rule, 0, 0, 0);
1480         break;
1481 #endif
1482 #if FLOW_N_REGS > 1
1483     case MFF_REG1:
1484         cls_rule_set_reg_masked(rule, 1, 0, 0);
1485         break;
1486 #endif
1487 #if FLOW_N_REGS > 2
1488     case MFF_REG2:
1489         cls_rule_set_reg_masked(rule, 2, 0, 0);
1490         break;
1491 #endif
1492 #if FLOW_N_REGS > 3
1493     case MFF_REG3:
1494         cls_rule_set_reg_masked(rule, 3, 0, 0);
1495         break;
1496 #endif
1497 #if FLOW_N_REGS > 4
1498     case MFF_REG4:
1499         cls_rule_set_reg_masked(rule, 4, 0, 0);
1500         break;
1501 #endif
1502 #if FLOW_N_REGS > 5
1503     case MFF_REG5:
1504         cls_rule_set_reg_masked(rule, 5, 0, 0);
1505         break;
1506 #endif
1507 #if FLOW_N_REGS > 6
1508     case MFF_REG6:
1509         cls_rule_set_reg_masked(rule, 6, 0, 0);
1510         break;
1511 #endif
1512 #if FLOW_N_REGS > 7
1513     case MFF_REG7:
1514         cls_rule_set_reg_masked(rule, 7, 0, 0);
1515         break;
1516 #endif
1517 #if FLOW_N_REGS > 8
1518 #error
1519 #endif
1520
1521     case MFF_ETH_SRC:
1522         rule->wc.wildcards |= FWW_DL_SRC;
1523         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1524         break;
1525
1526     case MFF_ETH_DST:
1527         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1528         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1529         break;
1530
1531     case MFF_ETH_TYPE:
1532         rule->wc.wildcards |= FWW_DL_TYPE;
1533         rule->flow.dl_type = htons(0);
1534         break;
1535
1536     case MFF_VLAN_TCI:
1537         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1538         break;
1539
1540     case MFF_VLAN_VID:
1541         cls_rule_set_any_vid(rule);
1542         break;
1543
1544     case MFF_VLAN_PCP:
1545         cls_rule_set_any_pcp(rule);
1546         break;
1547
1548     case MFF_IPV4_SRC:
1549     case MFF_ARP_SPA:
1550         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1551         break;
1552
1553     case MFF_IPV4_DST:
1554     case MFF_ARP_TPA:
1555         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1556         break;
1557
1558     case MFF_IPV6_SRC:
1559         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1560         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1561         break;
1562
1563     case MFF_IPV6_DST:
1564         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1565         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1566         break;
1567
1568     case MFF_IPV6_LABEL:
1569         rule->wc.wildcards |= FWW_IPV6_LABEL;
1570         rule->flow.ipv6_label = 0;
1571         break;
1572
1573     case MFF_IP_PROTO:
1574         rule->wc.wildcards |= FWW_NW_PROTO;
1575         rule->flow.nw_proto = 0;
1576         break;
1577
1578     case MFF_IP_DSCP:
1579         rule->wc.wildcards |= FWW_NW_DSCP;
1580         rule->flow.nw_tos &= ~IP_DSCP_MASK;
1581         break;
1582
1583     case MFF_IP_ECN:
1584         rule->wc.wildcards |= FWW_NW_ECN;
1585         rule->flow.nw_tos &= ~IP_ECN_MASK;
1586         break;
1587
1588     case MFF_IP_TTL:
1589         rule->wc.wildcards |= FWW_NW_TTL;
1590         rule->flow.nw_ttl = 0;
1591         break;
1592
1593     case MFF_IP_FRAG:
1594         rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1595         rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1596         break;
1597
1598     case MFF_ARP_OP:
1599         rule->wc.wildcards |= FWW_NW_PROTO;
1600         rule->flow.nw_proto = 0;
1601         break;
1602
1603     case MFF_ARP_SHA:
1604     case MFF_ND_SLL:
1605         rule->wc.wildcards |= FWW_ARP_SHA;
1606         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1607         break;
1608
1609     case MFF_ARP_THA:
1610     case MFF_ND_TLL:
1611         rule->wc.wildcards |= FWW_ARP_THA;
1612         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1613         break;
1614
1615     case MFF_TCP_SRC:
1616     case MFF_UDP_SRC:
1617     case MFF_ICMPV4_TYPE:
1618     case MFF_ICMPV6_TYPE:
1619         rule->wc.tp_src_mask = htons(0);
1620         rule->flow.tp_src = htons(0);
1621         break;
1622
1623     case MFF_TCP_DST:
1624     case MFF_UDP_DST:
1625     case MFF_ICMPV4_CODE:
1626     case MFF_ICMPV6_CODE:
1627         rule->wc.tp_dst_mask = htons(0);
1628         rule->flow.tp_dst = htons(0);
1629         break;
1630
1631     case MFF_ND_TARGET:
1632         memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1633         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1634         break;
1635
1636     case MFF_N_IDS:
1637     default:
1638         NOT_REACHED();
1639     }
1640 }
1641
1642 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1643  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1644  * with a 1-bit indicating that the corresponding value bit must match and a
1645  * 0-bit indicating a don't-care.
1646  *
1647  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1648  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1649  * call is equivalent to mf_set_wild(mf, rule).
1650  *
1651  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1652  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1653 void
1654 mf_set(const struct mf_field *mf,
1655        const union mf_value *value, const union mf_value *mask,
1656        struct cls_rule *rule)
1657 {
1658     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1659         mf_set_value(mf, value, rule);
1660         return;
1661     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1662         mf_set_wild(mf, rule);
1663         return;
1664     }
1665
1666     switch (mf->id) {
1667     case MFF_IN_PORT:
1668     case MFF_ETH_SRC:
1669     case MFF_ETH_TYPE:
1670     case MFF_VLAN_VID:
1671     case MFF_VLAN_PCP:
1672     case MFF_IPV6_LABEL:
1673     case MFF_IP_PROTO:
1674     case MFF_IP_TTL:
1675     case MFF_IP_DSCP:
1676     case MFF_IP_ECN:
1677     case MFF_ARP_OP:
1678     case MFF_ARP_SHA:
1679     case MFF_ARP_THA:
1680     case MFF_ICMPV4_TYPE:
1681     case MFF_ICMPV4_CODE:
1682     case MFF_ICMPV6_TYPE:
1683     case MFF_ICMPV6_CODE:
1684     case MFF_ND_SLL:
1685     case MFF_ND_TLL:
1686         NOT_REACHED();
1687
1688     case MFF_TUN_ID:
1689         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1690         break;
1691
1692 #if FLOW_N_REGS > 0
1693     case MFF_REG0:
1694 #endif
1695 #if FLOW_N_REGS > 1
1696     case MFF_REG1:
1697 #endif
1698 #if FLOW_N_REGS > 2
1699     case MFF_REG2:
1700 #endif
1701 #if FLOW_N_REGS > 3
1702     case MFF_REG3:
1703 #endif
1704 #if FLOW_N_REGS > 4
1705     case MFF_REG4:
1706 #endif
1707 #if FLOW_N_REGS > 5
1708     case MFF_REG5:
1709 #endif
1710 #if FLOW_N_REGS > 6
1711     case MFF_REG6:
1712 #endif
1713 #if FLOW_N_REGS > 7
1714     case MFF_REG7:
1715 #endif
1716 #if FLOW_N_REGS > 8
1717 #error
1718 #endif
1719         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1720                                 ntohl(value->be32), ntohl(mask->be32));
1721         break;
1722
1723     case MFF_ETH_DST:
1724         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1725             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1726         }
1727         break;
1728
1729     case MFF_VLAN_TCI:
1730         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1731         break;
1732
1733     case MFF_IPV4_SRC:
1734         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1735         break;
1736
1737     case MFF_IPV4_DST:
1738         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1739         break;
1740
1741     case MFF_IPV6_SRC:
1742         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1743         break;
1744
1745     case MFF_IPV6_DST:
1746         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1747         break;
1748
1749     case MFF_ND_TARGET:
1750         cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1751         break;
1752
1753     case MFF_IP_FRAG:
1754         cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1755         break;
1756
1757     case MFF_ARP_SPA:
1758         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1759         break;
1760
1761     case MFF_ARP_TPA:
1762         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1763         break;
1764
1765     case MFF_TCP_SRC:
1766     case MFF_UDP_SRC:
1767         cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1768         break;
1769
1770     case MFF_TCP_DST:
1771     case MFF_UDP_DST:
1772         cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1773         break;
1774
1775     case MFF_N_IDS:
1776     default:
1777         NOT_REACHED();
1778     }
1779 }
1780
1781 static enum ofperr
1782 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1783            const char *type)
1784 {
1785     if (!sf->field) {
1786         VLOG_WARN_RL(&rl, "unknown %s field", type);
1787     } else if (!sf->n_bits) {
1788         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1789     } else if (sf->ofs >= sf->field->n_bits) {
1790         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1791                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1792     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1793         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1794                      "of %s field %s", sf->ofs, sf->n_bits,
1795                      sf->field->n_bits, type, sf->field->name);
1796     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1797         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1798                      type, sf->field->name);
1799     } else {
1800         return 0;
1801     }
1802
1803     return OFPERR_OFPBAC_BAD_ARGUMENT;
1804 }
1805
1806 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1807  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1808  * ofp_mkerr()).  */
1809 enum ofperr
1810 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1811 {
1812     return mf_check__(sf, flow, "source");
1813 }
1814
1815 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1816  * if so, otherwise an OpenFlow error code (e.g. as returned by
1817  * ofp_mkerr()). */
1818 enum ofperr
1819 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1820 {
1821     int error = mf_check__(sf, flow, "destination");
1822     if (!error && !sf->field->writable) {
1823         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1824                      sf->field->name);
1825         return OFPERR_OFPBAC_BAD_ARGUMENT;
1826     }
1827     return error;
1828 }
1829
1830 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1831  * 'value' and 'mask', respectively. */
1832 void
1833 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1834        union mf_value *value, union mf_value *mask)
1835 {
1836     mf_get_value(mf, &rule->flow, value);
1837     mf_get_mask(mf, &rule->wc, mask);
1838 }
1839
1840 /* Assigns a random value for field 'mf' to 'value'. */
1841 void
1842 mf_random_value(const struct mf_field *mf, union mf_value *value)
1843 {
1844     random_bytes(value, mf->n_bytes);
1845
1846     switch (mf->id) {
1847     case MFF_TUN_ID:
1848     case MFF_IN_PORT:
1849 #if FLOW_N_REGS > 0
1850     case MFF_REG0:
1851 #endif
1852 #if FLOW_N_REGS > 1
1853     case MFF_REG1:
1854 #endif
1855 #if FLOW_N_REGS > 2
1856     case MFF_REG2:
1857 #endif
1858 #if FLOW_N_REGS > 3
1859     case MFF_REG3:
1860 #endif
1861 #if FLOW_N_REGS > 4
1862     case MFF_REG4:
1863 #endif
1864 #if FLOW_N_REGS > 5
1865     case MFF_REG5:
1866 #endif
1867 #if FLOW_N_REGS > 6
1868     case MFF_REG6:
1869 #endif
1870 #if FLOW_N_REGS > 7
1871     case MFF_REG7:
1872 #endif
1873 #if FLOW_N_REGS > 8
1874 #error
1875 #endif
1876     case MFF_ETH_SRC:
1877     case MFF_ETH_DST:
1878     case MFF_ETH_TYPE:
1879     case MFF_VLAN_TCI:
1880     case MFF_IPV4_SRC:
1881     case MFF_IPV4_DST:
1882     case MFF_IPV6_SRC:
1883     case MFF_IPV6_DST:
1884     case MFF_IP_PROTO:
1885     case MFF_IP_TTL:
1886     case MFF_ARP_SPA:
1887     case MFF_ARP_TPA:
1888     case MFF_ARP_SHA:
1889     case MFF_ARP_THA:
1890     case MFF_TCP_SRC:
1891     case MFF_TCP_DST:
1892     case MFF_UDP_SRC:
1893     case MFF_UDP_DST:
1894     case MFF_ICMPV4_TYPE:
1895     case MFF_ICMPV4_CODE:
1896     case MFF_ICMPV6_TYPE:
1897     case MFF_ICMPV6_CODE:
1898     case MFF_ND_TARGET:
1899     case MFF_ND_SLL:
1900     case MFF_ND_TLL:
1901         break;
1902
1903     case MFF_IPV6_LABEL:
1904         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1905         break;
1906
1907     case MFF_IP_DSCP:
1908         value->u8 &= IP_DSCP_MASK;
1909         break;
1910
1911     case MFF_IP_ECN:
1912         value->u8 &= IP_ECN_MASK;
1913         break;
1914
1915     case MFF_IP_FRAG:
1916         value->u8 &= FLOW_NW_FRAG_MASK;
1917         break;
1918
1919     case MFF_ARP_OP:
1920         value->be16 &= htons(0xff);
1921         break;
1922
1923     case MFF_VLAN_VID:
1924         value->be16 &= htons(VLAN_VID_MASK);
1925         break;
1926
1927     case MFF_VLAN_PCP:
1928         value->u8 &= 0x07;
1929         break;
1930
1931     case MFF_N_IDS:
1932     default:
1933         NOT_REACHED();
1934     }
1935 }
1936
1937 static char *
1938 mf_from_integer_string(const struct mf_field *mf, const char *s,
1939                        uint8_t *valuep, uint8_t *maskp)
1940 {
1941     unsigned long long int integer, mask;
1942     char *tail;
1943     int i;
1944
1945     errno = 0;
1946     integer = strtoull(s, &tail, 0);
1947     if (errno || (*tail != '\0' && *tail != '/')) {
1948         goto syntax_error;
1949     }
1950
1951     if (*tail == '/') {
1952         mask = strtoull(tail + 1, &tail, 0);
1953         if (errno || *tail != '\0') {
1954             goto syntax_error;
1955         }
1956     } else {
1957         mask = ULLONG_MAX;
1958     }
1959
1960     for (i = mf->n_bytes - 1; i >= 0; i--) {
1961         valuep[i] = integer;
1962         maskp[i] = mask;
1963         integer >>= 8;
1964         mask >>= 8;
1965     }
1966     if (integer) {
1967         return xasprintf("%s: value too large for %u-byte field %s",
1968                          s, mf->n_bytes, mf->name);
1969     }
1970     return NULL;
1971
1972 syntax_error:
1973     return xasprintf("%s: bad syntax for %s", s, mf->name);
1974 }
1975
1976 static char *
1977 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1978                         uint8_t mac[ETH_ADDR_LEN],
1979                         uint8_t mask[ETH_ADDR_LEN])
1980 {
1981     assert(mf->n_bytes == ETH_ADDR_LEN);
1982
1983     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1984                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1985     case ETH_ADDR_SCAN_COUNT * 2:
1986         return NULL;
1987
1988     case ETH_ADDR_SCAN_COUNT:
1989         memset(mask, 0xff, ETH_ADDR_LEN);
1990         return NULL;
1991
1992     default:
1993         return xasprintf("%s: invalid Ethernet address", s);
1994     }
1995 }
1996
1997 static char *
1998 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1999                     ovs_be32 *ip, ovs_be32 *mask)
2000 {
2001     int prefix;
2002
2003     assert(mf->n_bytes == sizeof *ip);
2004
2005     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2006                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2007         /* OK. */
2008     } else if (sscanf(s, IP_SCAN_FMT"/%d",
2009                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2010         if (prefix <= 0 || prefix > 32) {
2011             return xasprintf("%s: network prefix bits not between 1 and "
2012                              "32", s);
2013         } else if (prefix == 32) {
2014             *mask = htonl(UINT32_MAX);
2015         } else {
2016             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2017         }
2018     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2019         *mask = htonl(UINT32_MAX);
2020     } else {
2021         return xasprintf("%s: invalid IP address", s);
2022     }
2023     return NULL;
2024 }
2025
2026 static char *
2027 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2028                     struct in6_addr *value, struct in6_addr *mask)
2029 {
2030     char *str = xstrdup(s);
2031     char *save_ptr = NULL;
2032     const char *name, *netmask;
2033     int retval;
2034
2035     assert(mf->n_bytes == sizeof *value);
2036
2037     name = strtok_r(str, "/", &save_ptr);
2038     retval = name ? lookup_ipv6(name, value) : EINVAL;
2039     if (retval) {
2040         char *err;
2041
2042         err = xasprintf("%s: could not convert to IPv6 address", str);
2043         free(str);
2044
2045         return err;
2046     }
2047
2048     netmask = strtok_r(NULL, "/", &save_ptr);
2049     if (netmask) {
2050         int prefix = atoi(netmask);
2051         if (prefix <= 0 || prefix > 128) {
2052             free(str);
2053             return xasprintf("%s: prefix bits not between 1 and 128", s);
2054         } else {
2055             *mask = ipv6_create_mask(prefix);
2056         }
2057     } else {
2058         *mask = in6addr_exact;
2059     }
2060     free(str);
2061
2062     return NULL;
2063 }
2064
2065 static char *
2066 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2067                         ovs_be16 *valuep, ovs_be16 *maskp)
2068 {
2069     uint16_t port;
2070
2071     assert(mf->n_bytes == sizeof(ovs_be16));
2072     if (ofputil_port_from_string(s, &port)) {
2073         *valuep = htons(port);
2074         *maskp = htons(UINT16_MAX);
2075         return NULL;
2076     } else {
2077         return mf_from_integer_string(mf, s,
2078                                       (uint8_t *) valuep, (uint8_t *) maskp);
2079     }
2080 }
2081
2082 struct frag_handling {
2083     const char *name;
2084     uint8_t mask;
2085     uint8_t value;
2086 };
2087
2088 static const struct frag_handling all_frags[] = {
2089 #define A FLOW_NW_FRAG_ANY
2090 #define L FLOW_NW_FRAG_LATER
2091     /* name               mask  value */
2092
2093     { "no",               A|L,  0     },
2094     { "first",            A|L,  A     },
2095     { "later",            A|L,  A|L   },
2096
2097     { "no",               A,    0     },
2098     { "yes",              A,    A     },
2099
2100     { "not_later",        L,    0     },
2101     { "later",            L,    L     },
2102 #undef A
2103 #undef L
2104 };
2105
2106 static char *
2107 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2108 {
2109     const struct frag_handling *h;
2110
2111     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2112         if (!strcasecmp(s, h->name)) {
2113             /* We force the upper bits of the mask on to make mf_parse_value()
2114              * happy (otherwise it will never think it's an exact match.) */
2115             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2116             *valuep = h->value;
2117             return NULL;
2118         }
2119     }
2120
2121     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2122                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
2123 }
2124
2125 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2126  * NULL if successful, otherwise a malloc()'d string describing the error. */
2127 char *
2128 mf_parse(const struct mf_field *mf, const char *s,
2129          union mf_value *value, union mf_value *mask)
2130 {
2131     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2132         memset(value, 0, mf->n_bytes);
2133         memset(mask, 0, mf->n_bytes);
2134         return NULL;
2135     }
2136
2137     switch (mf->string) {
2138     case MFS_DECIMAL:
2139     case MFS_HEXADECIMAL:
2140         return mf_from_integer_string(mf, s,
2141                                       (uint8_t *) value, (uint8_t *) mask);
2142
2143     case MFS_ETHERNET:
2144         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2145
2146     case MFS_IPV4:
2147         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2148
2149     case MFS_IPV6:
2150         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2151
2152     case MFS_OFP_PORT:
2153         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2154
2155     case MFS_FRAG:
2156         return mf_from_frag_string(s, &value->u8, &mask->u8);
2157     }
2158     NOT_REACHED();
2159 }
2160
2161 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2162  * successful, otherwise a malloc()'d string describing the error. */
2163 char *
2164 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2165 {
2166     union mf_value mask;
2167     char *error;
2168
2169     error = mf_parse(mf, s, value, &mask);
2170     if (error) {
2171         return error;
2172     }
2173
2174     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2175         return xasprintf("%s: wildcards not allowed here", s);
2176     }
2177     return NULL;
2178 }
2179
2180 static void
2181 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2182                          const uint8_t *maskp, struct ds *s)
2183 {
2184     unsigned long long int integer;
2185     int i;
2186
2187     assert(mf->n_bytes <= 8);
2188
2189     integer = 0;
2190     for (i = 0; i < mf->n_bytes; i++) {
2191         integer = (integer << 8) | valuep[i];
2192     }
2193     if (mf->string == MFS_HEXADECIMAL) {
2194         ds_put_format(s, "%#llx", integer);
2195     } else {
2196         ds_put_format(s, "%lld", integer);
2197     }
2198
2199     if (maskp) {
2200         unsigned long long int mask;
2201
2202         mask = 0;
2203         for (i = 0; i < mf->n_bytes; i++) {
2204             mask = (mask << 8) | maskp[i];
2205         }
2206
2207         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2208          * not sure that that a bit-mask written in decimal is ever easier to
2209          * understand than the same bit-mask written in hexadecimal. */
2210         ds_put_format(s, "/%#llx", mask);
2211     }
2212 }
2213
2214 static void
2215 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2216                       struct ds *s)
2217 {
2218     const struct frag_handling *h;
2219     uint8_t value = *valuep;
2220     uint8_t mask = *maskp;
2221
2222     value &= mask;
2223     mask &= FLOW_NW_FRAG_MASK;
2224
2225     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2226         if (value == h->value && mask == h->mask) {
2227             ds_put_cstr(s, h->name);
2228             return;
2229         }
2230     }
2231     ds_put_cstr(s, "<error>");
2232 }
2233
2234 /* Appends to 's' a string representation of field 'mf' whose value is in
2235  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2236 void
2237 mf_format(const struct mf_field *mf,
2238           const union mf_value *value, const union mf_value *mask,
2239           struct ds *s)
2240 {
2241     if (mask) {
2242         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2243             ds_put_cstr(s, "ANY");
2244             return;
2245         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2246             mask = NULL;
2247         }
2248     }
2249
2250     switch (mf->string) {
2251     case MFS_OFP_PORT:
2252         if (!mask) {
2253             ofputil_format_port(ntohs(value->be16), s);
2254             break;
2255         }
2256         /* fall through */
2257     case MFS_DECIMAL:
2258     case MFS_HEXADECIMAL:
2259         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2260         break;
2261
2262     case MFS_ETHERNET:
2263         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2264         if (mask) {
2265             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2266         }
2267         break;
2268
2269     case MFS_IPV4:
2270         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2271                          s);
2272         break;
2273
2274     case MFS_IPV6:
2275         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2276         break;
2277
2278     case MFS_FRAG:
2279         mf_format_frag_string(&value->u8, &mask->u8, s);
2280         break;
2281
2282     default:
2283         NOT_REACHED();
2284     }
2285 }
2286 \f
2287 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2288  * least-significant bits in 'x'.
2289  *
2290  * See mf_set_subfield() for an example.
2291  *
2292  * The difference between this function and mf_set_subfield() is that the
2293  * latter function can only handle subfields up to 64 bits wide, whereas this
2294  * one handles the general case.  On the other hand, mf_set_subfield() is
2295  * arguably easier to use. */
2296 void
2297 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2298                   struct cls_rule *rule)
2299 {
2300     const struct mf_field *field = sf->field;
2301     union mf_value value, mask;
2302
2303     mf_get(field, rule, &value, &mask);
2304     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2305     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2306     mf_set(field, &value, &mask, rule);
2307 }
2308
2309 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2310  * least-significant bits of 'x'.
2311  *
2312  * Example: suppose that 'sf->field' is originally the following 2-byte field
2313  * in 'rule':
2314  *
2315  *     value == 0xe00a == 2#1110000000001010
2316  *      mask == 0xfc3f == 2#1111110000111111
2317  *
2318  * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2319  * sf->n_bits == 7 would have the following effect (note that 0x55 is
2320  * 2#1010101):
2321  *
2322  *     value == 0xd50a == 2#1101010100001010
2323  *      mask == 0xff3f == 2#1111111100111111
2324  *                           ^^^^^^^ affected bits
2325  *
2326  * The caller is responsible for ensuring that the result will be a valid
2327  * wildcard pattern for 'sf->field'.  The caller is responsible for ensuring
2328  * that 'rule' meets 'sf->field''s prerequisites. */
2329 void
2330 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2331                 struct cls_rule *rule)
2332 {
2333     const struct mf_field *field = sf->field;
2334     unsigned int n_bits = sf->n_bits;
2335     unsigned int ofs = sf->ofs;
2336
2337     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2338         union mf_value value;
2339         int i;
2340
2341         for (i = field->n_bytes - 1; i >= 0; i--) {
2342             ((uint8_t *) &value)[i] = x;
2343             x >>= 8;
2344         }
2345         mf_set_value(field, &value, rule);
2346     } else {
2347         union mf_value value, mask;
2348         uint8_t *vp = (uint8_t *) &value;
2349         uint8_t *mp = (uint8_t *) &mask;
2350
2351         mf_get(field, rule, &value, &mask);
2352         bitwise_put(x,          vp, field->n_bytes, ofs, n_bits);
2353         bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2354         mf_set(field, &value, &mask, rule);
2355     }
2356 }
2357
2358 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2359 void
2360 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2361                       struct flow *flow)
2362 {
2363     const struct mf_field *field = sf->field;
2364     unsigned int n_bits = sf->n_bits;
2365     unsigned int ofs = sf->ofs;
2366     union mf_value value;
2367
2368     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2369         int i;
2370
2371         for (i = field->n_bytes - 1; i >= 0; i--) {
2372             ((uint8_t *) &value)[i] = x;
2373             x >>= 8;
2374         }
2375         mf_set_flow_value(field, &value, flow);
2376     } else {
2377         mf_get_value(field, flow, &value);
2378         bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2379         mf_set_flow_value(field, &value, flow);
2380     }
2381 }
2382
2383 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2384  * reading 'flow', e.g. as checked by mf_check_src(). */
2385 void
2386 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2387                  union mf_subvalue *x)
2388 {
2389     union mf_value value;
2390
2391     mf_get_value(sf->field, flow, &value);
2392
2393     memset(x, 0, sizeof *x);
2394     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2395                  x, sizeof *x, 0,
2396                  sf->n_bits);
2397 }
2398
2399 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2400  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2401  * less. */
2402 uint64_t
2403 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2404 {
2405     union mf_value value;
2406
2407     mf_get_value(sf->field, flow, &value);
2408     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2409 }
2410
2411 /* Formats 'sf' into 's' in a format normally acceptable to
2412  * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2413  * sf->field has no NXM name.) */
2414 void
2415 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2416 {
2417     if (!sf->field) {
2418         ds_put_cstr(s, "<unknown>");
2419     } else if (sf->field->nxm_name) {
2420         ds_put_cstr(s, sf->field->nxm_name);
2421     } else if (sf->field->nxm_header) {
2422         uint32_t header = sf->field->nxm_header;
2423         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2424     } else {
2425         ds_put_cstr(s, sf->field->name);
2426     }
2427
2428     if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2429         ds_put_cstr(s, "[]");
2430     } else if (sf->n_bits == 1) {
2431         ds_put_format(s, "[%d]", sf->ofs);
2432     } else {
2433         ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2434     }
2435 }
2436
2437 static const struct mf_field *
2438 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2439 {
2440     int i;
2441
2442     *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2443     if (*wild) {
2444         name_len -= 2;
2445     }
2446
2447     for (i = 0; i < MFF_N_IDS; i++) {
2448         const struct mf_field *mf = mf_from_id(i);
2449
2450         if (mf->nxm_name
2451             && !strncmp(mf->nxm_name, name, name_len)
2452             && mf->nxm_name[name_len] == '\0') {
2453             return mf;
2454         }
2455     }
2456
2457     return NULL;
2458 }
2459
2460 /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2461  * returns NULL and advances '*sp' to the first byte following the parsed
2462  * string.  On failure, returns a malloc()'d error message, does not modify
2463  * '*sp', and does not properly initialize 'sf'.
2464  *
2465  * The syntax parsed from '*sp' takes the form "header[start..end]" where
2466  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2467  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2468  * may both be omitted (the [] are still required) to indicate an entire
2469  * field. */
2470 char *
2471 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2472 {
2473     const struct mf_field *field;
2474     const char *name;
2475     int start, end;
2476     const char *s;
2477     int name_len;
2478     bool wild;
2479
2480     s = *sp;
2481     name = s;
2482     name_len = strcspn(s, "[");
2483     if (s[name_len] != '[') {
2484         return xasprintf("%s: missing [ looking for field name", *sp);
2485     }
2486
2487     field = mf_parse_subfield_name(name, name_len, &wild);
2488     if (!field) {
2489         return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2490     }
2491
2492     s += name_len;
2493     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2494         /* Nothing to do. */
2495     } else if (sscanf(s, "[%d]", &start) == 1) {
2496         end = start;
2497     } else if (!strncmp(s, "[]", 2)) {
2498         start = 0;
2499         end = field->n_bits - 1;
2500     } else {
2501         return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2502                          "[<start>..<end>]", *sp);
2503     }
2504     s = strchr(s, ']') + 1;
2505
2506     if (start > end) {
2507         return xasprintf("%s: starting bit %d is after ending bit %d",
2508                          *sp, start, end);
2509     } else if (start >= field->n_bits) {
2510         return xasprintf("%s: starting bit %d is not valid because field is "
2511                          "only %d bits wide", *sp, start, field->n_bits);
2512     } else if (end >= field->n_bits){
2513         return xasprintf("%s: ending bit %d is not valid because field is "
2514                          "only %d bits wide", *sp, end, field->n_bits);
2515     }
2516
2517     sf->field = field;
2518     sf->ofs = start;
2519     sf->n_bits = end - start + 1;
2520
2521     *sp = s;
2522     return NULL;
2523 }
2524
2525 /* Parses a subfield from the beginning of 's' into 'sf'.  Returns the first
2526  * byte in 's' following the parsed string.
2527  *
2528  * Exits with an error message if 's' has incorrect syntax.
2529  *
2530  * The syntax parsed from 's' takes the form "header[start..end]" where
2531  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2532  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2533  * may both be omitted (the [] are still required) to indicate an entire
2534  * field.  */
2535 const char *
2536 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2537 {
2538     char *msg = mf_parse_subfield__(sf, &s);
2539     if (msg) {
2540         ovs_fatal(0, "%s", msg);
2541     }
2542     return s;
2543 }