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