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