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