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