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