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