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