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