570d8a2579029413543e586a4e1f3ff182c9dae4
[sliver-openvswitch.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011 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-util.h"
30 #include "packets.h"
31 #include "random.h"
32 #include "shash.h"
33 #include "socket-util.h"
34 #include "unaligned.h"
35
36 #define MF_FIELD_SIZES(MEMBER)                  \
37     sizeof ((union mf_value *)0)->MEMBER,       \
38     8 * sizeof ((union mf_value *)0)->MEMBER
39
40 static const struct mf_field mf_fields[MFF_N_IDS] = {
41     /* ## -------- ## */
42     /* ## metadata ## */
43     /* ## -------- ## */
44
45     {
46         MFF_TUN_ID, "tun_id", NULL,
47         MF_FIELD_SIZES(be64),
48         MFM_FULLY, 0,
49         MFS_HEXADECIMAL,
50         MFP_NONE,
51         NXM_NX_TUN_ID,
52     }, {
53         MFF_IN_PORT, "in_port", NULL,
54         MF_FIELD_SIZES(be16),
55         MFM_NONE, FWW_IN_PORT,
56         MFS_OFP_PORT,
57         MFP_NONE,
58         NXM_OF_IN_PORT,
59     },
60
61 #define REGISTER(IDX)                           \
62     {                                           \
63         MFF_REG##IDX, "reg" #IDX, NULL,         \
64         MF_FIELD_SIZES(be32),                   \
65         MFM_FULLY, 0,                           \
66         MFS_HEXADECIMAL,                        \
67         MFP_NONE,                               \
68         NXM_NX_REG(IDX),                        \
69     }
70 #if FLOW_N_REGS > 0
71     REGISTER(0),
72 #endif
73 #if FLOW_N_REGS > 1
74     REGISTER(1),
75 #endif
76 #if FLOW_N_REGS > 2
77     REGISTER(2),
78 #endif
79 #if FLOW_N_REGS > 3
80     REGISTER(3),
81 #endif
82 #if FLOW_N_REGS > 4
83     REGISTER(4),
84 #endif
85 #if FLOW_N_REGS > 5
86 #error
87 #endif
88
89     /* ## -- ## */
90     /* ## L2 ## */
91     /* ## -- ## */
92
93     {
94         MFF_ETH_SRC, "eth_src", "dl_src",
95         MF_FIELD_SIZES(mac),
96         MFM_NONE, FWW_DL_SRC,
97         MFS_ETHERNET,
98         MFP_NONE,
99         NXM_OF_ETH_SRC,
100     }, {
101         MFF_ETH_DST, "eth_dst", "dl_dst",
102         MF_FIELD_SIZES(mac),
103         MFM_MCAST, 0,
104         MFS_ETHERNET,
105         MFP_NONE,
106         NXM_OF_ETH_DST,
107     }, {
108         MFF_ETH_TYPE, "eth_type", "dl_type",
109         MF_FIELD_SIZES(be16),
110         MFM_NONE, FWW_DL_TYPE,
111         MFS_HEXADECIMAL,
112         MFP_NONE,
113         NXM_OF_ETH_TYPE,
114     },
115
116     {
117         MFF_VLAN_TCI, "vlan_tci", NULL,
118         MF_FIELD_SIZES(be16),
119         MFM_FULLY, 0,
120         MFS_HEXADECIMAL,
121         MFP_NONE,
122         NXM_OF_VLAN_TCI,
123     }, {
124         MFF_VLAN_VID, "dl_vlan", NULL,
125         sizeof(ovs_be16), 12,
126         MFM_NONE, 0,
127         MFS_DECIMAL,
128         MFP_NONE,
129         0,
130     }, {
131         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
132         1, 3,
133         MFM_NONE, 0,
134         MFS_DECIMAL,
135         MFP_NONE,
136         0,
137     },
138
139     /* ## -- ## */
140     /* ## L3 ## */
141     /* ## -- ## */
142
143     {
144         MFF_IPV4_SRC, "ip_src", "nw_src",
145         MF_FIELD_SIZES(be32),
146         MFM_CIDR, 0,
147         MFS_IPV4,
148         MFP_IPV4,
149         NXM_OF_IP_SRC,
150     }, {
151         MFF_IPV4_DST, "ip_dst", "nw_dst",
152         MF_FIELD_SIZES(be32),
153         MFM_CIDR, 0,
154         MFS_IPV4,
155         MFP_IPV4,
156         NXM_OF_IP_DST,
157     },
158
159     {
160         MFF_IPV6_SRC, "ipv6_src", NULL,
161         MF_FIELD_SIZES(ipv6),
162         MFM_CIDR, 0,
163         MFS_IPV6,
164         MFP_IPV6,
165         NXM_NX_IPV6_SRC,
166     }, {
167         MFF_IPV6_DST, "ipv6_dst", NULL,
168         MF_FIELD_SIZES(ipv6),
169         MFM_CIDR, 0,
170         MFS_IPV6,
171         MFP_IPV6,
172         NXM_NX_IPV6_DST,
173     },
174     {
175         MFF_IPV6_LABEL, "ipv6_label", NULL,
176         4, 20,
177         MFM_NONE, FWW_IPV6_LABEL,
178         MFS_HEXADECIMAL,
179         MFP_IPV6,
180         NXM_NX_IPV6_LABEL,
181     },
182
183     {
184         MFF_IP_PROTO, "nw_proto", NULL,
185         MF_FIELD_SIZES(u8),
186         MFM_NONE, FWW_NW_PROTO,
187         MFS_DECIMAL,
188         MFP_IP_ANY,
189         NXM_OF_IP_PROTO,
190     }, {
191         MFF_IP_DSCP, "nw_tos", NULL,
192         MF_FIELD_SIZES(u8),
193         MFM_NONE, FWW_NW_DSCP,
194         MFS_DECIMAL,
195         MFP_IP_ANY,
196         NXM_OF_IP_TOS,
197     }, {
198         MFF_IP_ECN, "nw_ecn", NULL,
199         1, 2,
200         MFM_NONE, FWW_NW_ECN,
201         MFS_DECIMAL,
202         MFP_IP_ANY,
203         NXM_NX_IP_ECN,
204     }, {
205         MFF_IP_TTL, "nw_ttl", NULL,
206         MF_FIELD_SIZES(u8),
207         MFM_NONE, FWW_NW_TTL,
208         MFS_DECIMAL,
209         MFP_IP_ANY,
210         NXM_NX_IP_TTL,
211     }, {
212         MFF_IP_FRAG, "ip_frag", NULL,
213         1, 2,
214         MFM_FULLY, 0,
215         MFS_FRAG,
216         MFP_IP_ANY,
217         NXM_NX_IP_FRAG,
218     },
219
220     {
221         MFF_ARP_OP, "arp_op", NULL,
222         MF_FIELD_SIZES(be16),
223         MFM_NONE, FWW_NW_PROTO,
224         MFS_DECIMAL,
225         MFP_ARP,
226         NXM_OF_ARP_OP,
227     }, {
228         MFF_ARP_SPA, "arp_spa", NULL,
229         MF_FIELD_SIZES(be32),
230         MFM_CIDR, 0,
231         MFS_IPV4,
232         MFP_ARP,
233         NXM_OF_ARP_SPA,
234     }, {
235         MFF_ARP_TPA, "arp_tpa", NULL,
236         MF_FIELD_SIZES(be32),
237         MFM_CIDR, 0,
238         MFS_IPV4,
239         MFP_ARP,
240         NXM_OF_ARP_TPA,
241     }, {
242         MFF_ARP_SHA, "arp_sha", NULL,
243         MF_FIELD_SIZES(mac),
244         MFM_NONE, FWW_ARP_SHA,
245         MFS_ETHERNET,
246         MFP_ARP,
247         NXM_NX_ARP_SHA,
248     }, {
249         MFF_ARP_THA, "arp_tha", NULL,
250         MF_FIELD_SIZES(mac),
251         MFM_NONE, FWW_ARP_THA,
252         MFS_ETHERNET,
253         MFP_ARP,
254         NXM_NX_ARP_THA,
255     },
256
257     /* ## -- ## */
258     /* ## L4 ## */
259     /* ## -- ## */
260
261     {
262         MFF_TCP_SRC, "tcp_src", "tp_src",
263         MF_FIELD_SIZES(be16),
264         MFM_NONE, FWW_TP_SRC,
265         MFS_DECIMAL,
266         MFP_TCP,
267         NXM_OF_TCP_SRC,
268     }, {
269         MFF_TCP_DST, "tcp_dst", "tp_dst",
270         MF_FIELD_SIZES(be16),
271         MFM_NONE, FWW_TP_DST,
272         MFS_DECIMAL,
273         MFP_TCP,
274         NXM_OF_TCP_DST,
275     },
276
277     {
278         MFF_UDP_SRC, "udp_src", NULL,
279         MF_FIELD_SIZES(be16),
280         MFM_NONE, FWW_TP_SRC,
281         MFS_DECIMAL,
282         MFP_UDP,
283         NXM_OF_UDP_SRC,
284     }, {
285         MFF_UDP_DST, "udp_dst", NULL,
286         MF_FIELD_SIZES(be16),
287         MFM_NONE, FWW_TP_DST,
288         MFS_DECIMAL,
289         MFP_UDP,
290         NXM_OF_UDP_DST,
291     },
292
293     {
294         MFF_ICMP_TYPE, "icmp_type", NULL,
295         MF_FIELD_SIZES(u8),
296         MFM_NONE, FWW_TP_SRC,
297         MFS_DECIMAL,
298         MFP_ICMP_ANY,
299         NXM_OF_ICMP_TYPE,
300     }, {
301         MFF_ICMP_CODE, "icmp_code", NULL,
302         MF_FIELD_SIZES(u8),
303         MFM_NONE, FWW_TP_DST,
304         MFS_DECIMAL,
305         MFP_ICMP_ANY,
306         NXM_OF_ICMP_CODE,
307     },
308
309     /* ## ---- ## */
310     /* ## L"5" ## */
311     /* ## ---- ## */
312
313     {
314         MFF_ND_TARGET, "nd_target", NULL,
315         MF_FIELD_SIZES(ipv6),
316         MFM_NONE, FWW_ND_TARGET,
317         MFS_IPV6,
318         MFP_ND,
319         NXM_NX_ND_TARGET,
320     }, {
321         MFF_ND_SLL, "nd_sll", NULL,
322         MF_FIELD_SIZES(mac),
323         MFM_NONE, FWW_ARP_SHA,
324         MFS_ETHERNET,
325         MFP_ND_SOLICIT,
326         NXM_NX_ND_SLL,
327     }, {
328         MFF_ND_TLL, "nd_tll", NULL,
329         MF_FIELD_SIZES(mac),
330         MFM_NONE, FWW_ARP_THA,
331         MFS_ETHERNET,
332         MFP_ND_ADVERT,
333         NXM_NX_ND_TLL,
334     }
335 };
336
337 /* Returns the field with the given 'id'. */
338 const struct mf_field *
339 mf_from_id(enum mf_field_id id)
340 {
341     assert((unsigned int) id < MFF_N_IDS);
342     return &mf_fields[id];
343 }
344
345 /* Returns the field with the given 'name', or a null pointer if no field has
346  * that name. */
347 const struct mf_field *
348 mf_from_name(const char *name)
349 {
350     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
351
352     if (shash_is_empty(&mf_by_name)) {
353         const struct mf_field *mf;
354
355         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
356             shash_add_once(&mf_by_name, mf->name, mf);
357             if (mf->extra_name) {
358                 shash_add_once(&mf_by_name, mf->extra_name, mf);
359             }
360         }
361     }
362
363     return shash_find_data(&mf_by_name, name);
364 }
365
366 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
367  * specifies at least one bit in the field.
368  *
369  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
370  * meets 'mf''s prerequisites. */
371 bool
372 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
373 {
374     switch (mf->id) {
375     case MFF_IN_PORT:
376     case MFF_ETH_SRC:
377     case MFF_ETH_TYPE:
378     case MFF_IP_PROTO:
379     case MFF_IP_DSCP:
380     case MFF_IP_ECN:
381     case MFF_IP_TTL:
382     case MFF_IPV6_LABEL:
383     case MFF_ARP_OP:
384     case MFF_ARP_SHA:
385     case MFF_ARP_THA:
386     case MFF_TCP_SRC:
387     case MFF_TCP_DST:
388     case MFF_UDP_SRC:
389     case MFF_UDP_DST:
390     case MFF_ICMP_TYPE:
391     case MFF_ICMP_CODE:
392     case MFF_ND_TARGET:
393     case MFF_ND_SLL:
394     case MFF_ND_TLL:
395         assert(mf->fww_bit != 0);
396         return (wc->wildcards & mf->fww_bit) != 0;
397
398     case MFF_TUN_ID:
399         return !wc->tun_id_mask;
400
401 #if FLOW_N_REGS > 0
402     case MFF_REG0:
403 #endif
404 #if FLOW_N_REGS > 1
405     case MFF_REG1:
406 #endif
407 #if FLOW_N_REGS > 2
408     case MFF_REG2:
409 #endif
410 #if FLOW_N_REGS > 3
411     case MFF_REG3:
412 #endif
413 #if FLOW_N_REGS > 4
414     case MFF_REG4:
415 #endif
416 #if FLOW_N_REGS > 5
417 #error
418 #endif
419         return !wc->reg_masks[mf->id - MFF_REG0];
420
421     case MFF_ETH_DST:
422         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
423                 == (FWW_ETH_MCAST | FWW_DL_DST));
424
425     case MFF_VLAN_TCI:
426         return !wc->vlan_tci_mask;
427     case MFF_VLAN_VID:
428         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
429     case MFF_VLAN_PCP:
430         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
431
432     case MFF_IPV4_SRC:
433         return !wc->nw_src_mask;
434     case MFF_IPV4_DST:
435         return !wc->nw_dst_mask;
436
437     case MFF_IPV6_SRC:
438         return ipv6_mask_is_any(&wc->ipv6_src_mask);
439     case MFF_IPV6_DST:
440         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
441
442     case MFF_IP_FRAG:
443         return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
444
445     case MFF_ARP_SPA:
446         return !wc->nw_src_mask;
447     case MFF_ARP_TPA:
448         return !wc->nw_dst_mask;
449
450     case MFF_N_IDS:
451     default:
452         NOT_REACHED();
453     }
454 }
455
456 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
457  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
458  * purposes, or to 0 if it is wildcarded.
459  *
460  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
461  * meets 'mf''s prerequisites. */
462 void
463 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
464             union mf_value *mask)
465 {
466     switch (mf->id) {
467     case MFF_IN_PORT:
468     case MFF_ETH_SRC:
469     case MFF_ETH_TYPE:
470     case MFF_IP_PROTO:
471     case MFF_IP_DSCP:
472     case MFF_IP_ECN:
473     case MFF_IP_TTL:
474     case MFF_IPV6_LABEL:
475     case MFF_ARP_OP:
476     case MFF_ARP_SHA:
477     case MFF_ARP_THA:
478     case MFF_TCP_SRC:
479     case MFF_TCP_DST:
480     case MFF_UDP_SRC:
481     case MFF_UDP_DST:
482     case MFF_ICMP_TYPE:
483     case MFF_ICMP_CODE:
484     case MFF_ND_TARGET:
485     case MFF_ND_SLL:
486     case MFF_ND_TLL:
487         assert(mf->fww_bit != 0);
488         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
489         break;
490
491     case MFF_TUN_ID:
492         mask->be64 = wc->tun_id_mask;
493         break;
494
495 #if FLOW_N_REGS > 0
496     case MFF_REG0:
497 #endif
498 #if FLOW_N_REGS > 1
499     case MFF_REG1:
500 #endif
501 #if FLOW_N_REGS > 2
502     case MFF_REG2:
503 #endif
504 #if FLOW_N_REGS > 3
505     case MFF_REG3:
506 #endif
507 #if FLOW_N_REGS > 4
508     case MFF_REG4:
509 #endif
510 #if FLOW_N_REGS > 5
511 #error
512 #endif
513         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
514         break;
515
516     case MFF_ETH_DST:
517         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
518                ETH_ADDR_LEN);
519         break;
520
521     case MFF_VLAN_TCI:
522         mask->be16 = wc->vlan_tci_mask;
523         break;
524     case MFF_VLAN_VID:
525         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
526         break;
527     case MFF_VLAN_PCP:
528         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
529         break;
530
531     case MFF_IPV4_SRC:
532         mask->be32 = wc->nw_src_mask;
533         break;
534     case MFF_IPV4_DST:
535         mask->be32 = wc->nw_dst_mask;
536         break;
537
538     case MFF_IPV6_SRC:
539         mask->ipv6 = wc->ipv6_src_mask;
540         break;
541     case MFF_IPV6_DST:
542         mask->ipv6 = wc->ipv6_dst_mask;
543         break;
544
545     case MFF_IP_FRAG:
546         mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
547         break;
548
549     case MFF_ARP_SPA:
550         mask->be32 = wc->nw_src_mask;
551         break;
552     case MFF_ARP_TPA:
553         mask->be32 = wc->nw_dst_mask;
554         break;
555
556     case MFF_N_IDS:
557     default:
558         NOT_REACHED();
559     }
560 }
561
562 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
563  * if the mask is valid, false otherwise. */
564 bool
565 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
566 {
567     switch (mf->maskable) {
568     case MFM_NONE:
569         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
570                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
571
572     case MFM_FULLY:
573         return true;
574
575     case MFM_CIDR:
576         return (mf->n_bytes == 4
577                 ? ip_is_cidr(mask->be32)
578                 : ipv6_is_cidr(&mask->ipv6));
579
580     case MFM_MCAST:
581         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
582     }
583
584     NOT_REACHED();
585 }
586
587 static bool
588 is_ip_any(const struct flow *flow)
589 {
590     return (flow->dl_type == htons(ETH_TYPE_IP) ||
591             flow->dl_type == htons(ETH_TYPE_IPV6));
592 }
593
594 static bool
595 is_icmpv4(const struct flow *flow)
596 {
597     return (flow->dl_type == htons(ETH_TYPE_IP)
598             && flow->nw_proto == IPPROTO_ICMP);
599 }
600
601 static bool
602 is_icmpv6(const struct flow *flow)
603 {
604     return (flow->dl_type == htons(ETH_TYPE_IPV6)
605             && flow->nw_proto == IPPROTO_ICMPV6);
606 }
607
608 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
609 bool
610 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
611 {
612     switch (mf->prereqs) {
613     case MFP_NONE:
614         return true;
615
616     case MFP_ARP:
617         return flow->dl_type == htons(ETH_TYPE_ARP);
618     case MFP_IPV4:
619         return flow->dl_type == htons(ETH_TYPE_IP);
620     case MFP_IPV6:
621         return flow->dl_type == htons(ETH_TYPE_IPV6);
622     case MFP_IP_ANY:
623         return is_ip_any(flow);
624
625     case MFP_TCP:
626         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
627     case MFP_UDP:
628         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
629     case MFP_ICMPV6:
630         return is_icmpv6(flow);
631     case MFP_ICMP_ANY:
632         return is_icmpv4(flow) || is_icmpv6(flow);
633
634     case MFP_ND:
635         return (is_icmpv6(flow)
636                 && flow->tp_dst == htons(0)
637                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
638                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
639     case MFP_ND_SOLICIT:
640         return (is_icmpv6(flow)
641                 && flow->tp_dst == htons(0)
642                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
643     case MFP_ND_ADVERT:
644         return (is_icmpv6(flow)
645                 && flow->tp_dst == htons(0)
646                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
647     }
648
649     NOT_REACHED();
650 }
651
652 /* Returns true if 'value' may be a valid value *as part of a masked match*,
653  * false otherwise.
654  *
655  * A value is not rejected just because it is not valid for the field in
656  * question, but only if it doesn't make sense to test the bits in question at
657  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
658  * without the VLAN_CFI bit being set, but we can't reject those values because
659  * it is still legitimate to test just for those bits (see the documentation
660  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
661  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
662 bool
663 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
664 {
665     switch (mf->id) {
666     case MFF_TUN_ID:
667     case MFF_IN_PORT:
668 #if FLOW_N_REGS > 0
669     case MFF_REG0:
670 #endif
671 #if FLOW_N_REGS > 1
672     case MFF_REG1:
673 #endif
674 #if FLOW_N_REGS > 2
675     case MFF_REG2:
676 #endif
677 #if FLOW_N_REGS > 3
678     case MFF_REG3:
679 #endif
680 #if FLOW_N_REGS > 4
681     case MFF_REG4:
682 #endif
683 #if FLOW_N_REGS > 5
684 #error
685 #endif
686     case MFF_ETH_SRC:
687     case MFF_ETH_DST:
688     case MFF_ETH_TYPE:
689     case MFF_VLAN_TCI:
690     case MFF_IPV4_SRC:
691     case MFF_IPV4_DST:
692     case MFF_IPV6_SRC:
693     case MFF_IPV6_DST:
694     case MFF_IP_PROTO:
695     case MFF_IP_TTL:
696     case MFF_ARP_SPA:
697     case MFF_ARP_TPA:
698     case MFF_ARP_SHA:
699     case MFF_ARP_THA:
700     case MFF_TCP_SRC:
701     case MFF_TCP_DST:
702     case MFF_UDP_SRC:
703     case MFF_UDP_DST:
704     case MFF_ICMP_TYPE:
705     case MFF_ICMP_CODE:
706     case MFF_ND_TARGET:
707     case MFF_ND_SLL:
708     case MFF_ND_TLL:
709         return true;
710
711     case MFF_IP_DSCP:
712         return !(value->u8 & ~IP_DSCP_MASK);
713     case MFF_IP_ECN:
714         return !(value->u8 & ~IP_ECN_MASK);
715     case MFF_IP_FRAG:
716         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
717
718     case MFF_ARP_OP:
719         return !(value->be16 & htons(0xff00));
720
721     case MFF_VLAN_VID:
722         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
723
724     case MFF_VLAN_PCP:
725         return !(value->u8 & ~7);
726
727     case MFF_IPV6_LABEL:
728         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
729
730     case MFF_N_IDS:
731     default:
732         NOT_REACHED();
733     }
734 }
735
736 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
737  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
738 void
739 mf_get_value(const struct mf_field *mf, const struct flow *flow,
740              union mf_value *value)
741 {
742     switch (mf->id) {
743     case MFF_TUN_ID:
744         value->be64 = flow->tun_id;
745         break;
746
747     case MFF_IN_PORT:
748         value->be16 = htons(flow->in_port);
749         break;
750
751 #if FLOW_N_REGS > 0
752     case MFF_REG0:
753 #endif
754 #if FLOW_N_REGS > 1
755     case MFF_REG1:
756 #endif
757 #if FLOW_N_REGS > 2
758     case MFF_REG2:
759 #endif
760 #if FLOW_N_REGS > 3
761     case MFF_REG3:
762 #endif
763 #if FLOW_N_REGS > 4
764     case MFF_REG4:
765 #endif
766 #if FLOW_N_REGS > 5
767 #error
768 #endif
769         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
770         break;
771
772     case MFF_ETH_SRC:
773         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
774         break;
775
776     case MFF_ETH_DST:
777         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
778         break;
779
780     case MFF_ETH_TYPE:
781         value->be16 = flow->dl_type;
782         break;
783
784     case MFF_VLAN_TCI:
785         value->be16 = flow->vlan_tci;
786         break;
787
788     case MFF_VLAN_VID:
789         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
790         break;
791
792     case MFF_VLAN_PCP:
793         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
794         break;
795
796     case MFF_IPV4_SRC:
797         value->be32 = flow->nw_src;
798         break;
799
800     case MFF_IPV4_DST:
801         value->be32 = flow->nw_dst;
802         break;
803
804     case MFF_IPV6_SRC:
805         value->ipv6 = flow->ipv6_src;
806         break;
807
808     case MFF_IPV6_DST:
809         value->ipv6 = flow->ipv6_dst;
810         break;
811
812     case MFF_IPV6_LABEL:
813         value->be32 = flow->ipv6_label;
814         break;
815
816     case MFF_IP_PROTO:
817         value->u8 = flow->nw_proto;
818         break;
819
820     case MFF_IP_DSCP:
821         value->u8 = flow->nw_tos & IP_DSCP_MASK;
822         break;
823
824     case MFF_IP_ECN:
825         value->u8 = flow->nw_tos & IP_ECN_MASK;
826         break;
827
828     case MFF_IP_TTL:
829         value->u8 = flow->nw_ttl;
830         break;
831
832     case MFF_IP_FRAG:
833         value->u8 = flow->nw_frag;
834         break;
835
836     case MFF_ARP_OP:
837         value->be16 = htons(flow->nw_proto);
838         break;
839
840     case MFF_ARP_SPA:
841         value->be32 = flow->nw_src;
842         break;
843
844     case MFF_ARP_TPA:
845         value->be32 = flow->nw_dst;
846         break;
847
848     case MFF_ARP_SHA:
849     case MFF_ND_SLL:
850         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
851         break;
852
853     case MFF_ARP_THA:
854     case MFF_ND_TLL:
855         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
856         break;
857
858     case MFF_TCP_SRC:
859         value->be16 = flow->tp_src;
860         break;
861
862     case MFF_TCP_DST:
863         value->be16 = flow->tp_dst;
864         break;
865
866     case MFF_UDP_SRC:
867         value->be16 = flow->tp_src;
868         break;
869
870     case MFF_UDP_DST:
871         value->be16 = flow->tp_dst;
872         break;
873
874     case MFF_ICMP_TYPE:
875         value->u8 = ntohs(flow->tp_src);
876         break;
877
878     case MFF_ICMP_CODE:
879         value->u8 = ntohs(flow->tp_dst);
880         break;
881
882     case MFF_ND_TARGET:
883         value->ipv6 = flow->nd_target;
884         break;
885
886     case MFF_N_IDS:
887     default:
888         NOT_REACHED();
889     }
890 }
891
892 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
893  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
894  * prerequisites. */
895 void
896 mf_set_value(const struct mf_field *mf,
897              const union mf_value *value, struct cls_rule *rule)
898 {
899     switch (mf->id) {
900     case MFF_TUN_ID:
901         cls_rule_set_tun_id(rule, value->be64);
902         break;
903
904     case MFF_IN_PORT:
905         cls_rule_set_in_port(rule, ntohs(value->be16));
906         break;
907
908 #if FLOW_N_REGS > 0
909     case MFF_REG0:
910 #endif
911 #if FLOW_N_REGS > 1
912     case MFF_REG1:
913 #endif
914 #if FLOW_N_REGS > 2
915     case MFF_REG2:
916 #endif
917 #if FLOW_N_REGS > 3
918     case MFF_REG3:
919 #endif
920 #if FLOW_N_REGS > 4
921     case MFF_REG4:
922 #endif
923 #if FLOW_N_REGS > 5
924 #error
925 #endif
926 #if FLOW_N_REGS > 0
927         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
928         break;
929 #endif
930
931     case MFF_ETH_SRC:
932         cls_rule_set_dl_src(rule, value->mac);
933         break;
934
935     case MFF_ETH_DST:
936         cls_rule_set_dl_dst(rule, value->mac);
937         break;
938
939     case MFF_ETH_TYPE:
940         cls_rule_set_dl_type(rule, value->be16);
941         break;
942
943     case MFF_VLAN_TCI:
944         cls_rule_set_dl_tci(rule, value->be16);
945         break;
946
947     case MFF_VLAN_VID:
948         cls_rule_set_dl_vlan(rule, value->be16);
949         break;
950
951     case MFF_VLAN_PCP:
952         cls_rule_set_dl_vlan_pcp(rule, value->u8);
953         break;
954
955     case MFF_IPV4_SRC:
956         cls_rule_set_nw_src(rule, value->be32);
957         break;
958
959     case MFF_IPV4_DST:
960         cls_rule_set_nw_dst(rule, value->be32);
961         break;
962
963     case MFF_IPV6_SRC:
964         cls_rule_set_ipv6_src(rule, &value->ipv6);
965         break;
966
967     case MFF_IPV6_DST:
968         cls_rule_set_ipv6_dst(rule, &value->ipv6);
969         break;
970
971     case MFF_IPV6_LABEL:
972         cls_rule_set_ipv6_label(rule, value->be32);
973         break;
974
975     case MFF_IP_PROTO:
976         cls_rule_set_nw_proto(rule, value->u8);
977         break;
978
979     case MFF_IP_DSCP:
980         cls_rule_set_nw_dscp(rule, value->u8);
981         break;
982
983     case MFF_IP_ECN:
984         cls_rule_set_nw_ecn(rule, value->u8);
985         break;
986
987     case MFF_IP_TTL:
988         cls_rule_set_nw_ttl(rule, value->u8);
989         break;
990
991     case MFF_IP_FRAG:
992         cls_rule_set_nw_frag(rule, value->u8);
993         break;
994
995     case MFF_ARP_OP:
996         cls_rule_set_nw_proto(rule, ntohs(value->be16));
997         break;
998
999     case MFF_ARP_SPA:
1000         cls_rule_set_nw_src(rule, value->be32);
1001         break;
1002
1003     case MFF_ARP_TPA:
1004         cls_rule_set_nw_dst(rule, value->be32);
1005         break;
1006
1007     case MFF_ARP_SHA:
1008     case MFF_ND_SLL:
1009         cls_rule_set_arp_sha(rule, value->mac);
1010         break;
1011
1012     case MFF_ARP_THA:
1013     case MFF_ND_TLL:
1014         cls_rule_set_arp_tha(rule, value->mac);
1015         break;
1016
1017     case MFF_TCP_SRC:
1018         cls_rule_set_tp_src(rule, value->be16);
1019         break;
1020
1021     case MFF_TCP_DST:
1022         cls_rule_set_tp_dst(rule, value->be16);
1023         break;
1024
1025     case MFF_UDP_SRC:
1026         cls_rule_set_tp_src(rule, value->be16);
1027         break;
1028
1029     case MFF_UDP_DST:
1030         cls_rule_set_tp_dst(rule, value->be16);
1031         break;
1032
1033     case MFF_ICMP_TYPE:
1034         cls_rule_set_icmp_type(rule, value->u8);
1035         break;
1036
1037     case MFF_ICMP_CODE:
1038         cls_rule_set_icmp_code(rule, value->u8);
1039         break;
1040
1041     case MFF_ND_TARGET:
1042         cls_rule_set_nd_target(rule, &value->ipv6);
1043         break;
1044
1045     case MFF_N_IDS:
1046     default:
1047         NOT_REACHED();
1048     }
1049 }
1050
1051 /* Makes 'rule' wildcard field 'mf'.
1052  *
1053  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1054  * prerequisites. */
1055 void
1056 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1057 {
1058     switch (mf->id) {
1059     case MFF_TUN_ID:
1060         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1061         break;
1062
1063     case MFF_IN_PORT:
1064         rule->wc.wildcards |= FWW_IN_PORT;
1065         rule->flow.in_port = 0;
1066         break;
1067
1068 #if FLOW_N_REGS > 0
1069     case MFF_REG0:
1070         cls_rule_set_reg_masked(rule, 0, 0, 0);
1071         break;
1072 #endif
1073 #if FLOW_N_REGS > 1
1074     case MFF_REG1:
1075         cls_rule_set_reg_masked(rule, 1, 0, 0);
1076         break;
1077 #endif
1078 #if FLOW_N_REGS > 2
1079     case MFF_REG2:
1080         cls_rule_set_reg_masked(rule, 2, 0, 0);
1081         break;
1082 #endif
1083 #if FLOW_N_REGS > 3
1084     case MFF_REG3:
1085         cls_rule_set_reg_masked(rule, 3, 0, 0);
1086         break;
1087 #endif
1088 #if FLOW_N_REGS > 4
1089     case MFF_REG4:
1090         cls_rule_set_reg_masked(rule, 4, 0, 0);
1091         break;
1092 #endif
1093 #if FLOW_N_REGS > 5
1094 #error
1095 #endif
1096
1097     case MFF_ETH_SRC:
1098         rule->wc.wildcards |= FWW_DL_SRC;
1099         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1100         break;
1101
1102     case MFF_ETH_DST:
1103         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1104         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1105         break;
1106
1107     case MFF_ETH_TYPE:
1108         rule->wc.wildcards |= FWW_DL_TYPE;
1109         rule->flow.dl_type = htons(0);
1110         break;
1111
1112     case MFF_VLAN_TCI:
1113         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1114         break;
1115
1116     case MFF_VLAN_VID:
1117         cls_rule_set_any_vid(rule);
1118         break;
1119
1120     case MFF_VLAN_PCP:
1121         cls_rule_set_any_pcp(rule);
1122         break;
1123
1124     case MFF_IPV4_SRC:
1125     case MFF_ARP_SPA:
1126         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1127         break;
1128
1129     case MFF_IPV4_DST:
1130     case MFF_ARP_TPA:
1131         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1132         break;
1133
1134     case MFF_IPV6_SRC:
1135         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1136         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1137         break;
1138
1139     case MFF_IPV6_DST:
1140         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1141         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1142         break;
1143
1144     case MFF_IPV6_LABEL:
1145         rule->wc.wildcards |= FWW_IPV6_LABEL;
1146         rule->flow.ipv6_label = 0;
1147         break;
1148
1149     case MFF_IP_PROTO:
1150         rule->wc.wildcards |= FWW_NW_PROTO;
1151         rule->flow.nw_proto = 0;
1152         break;
1153
1154     case MFF_IP_DSCP:
1155         rule->wc.wildcards |= FWW_NW_DSCP;
1156         rule->flow.nw_tos &= ~IP_DSCP_MASK;
1157         break;
1158
1159     case MFF_IP_ECN:
1160         rule->wc.wildcards |= FWW_NW_ECN;
1161         rule->flow.nw_tos &= ~IP_ECN_MASK;
1162         break;
1163
1164     case MFF_IP_TTL:
1165         rule->wc.wildcards |= FWW_NW_TTL;
1166         rule->flow.nw_ttl = 0;
1167         break;
1168
1169     case MFF_IP_FRAG:
1170         rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1171         rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1172         break;
1173
1174     case MFF_ARP_OP:
1175         rule->wc.wildcards |= FWW_NW_PROTO;
1176         rule->flow.nw_proto = 0;
1177         break;
1178
1179     case MFF_ARP_SHA:
1180     case MFF_ND_SLL:
1181         rule->wc.wildcards |= FWW_ARP_SHA;
1182         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1183         break;
1184
1185     case MFF_ARP_THA:
1186     case MFF_ND_TLL:
1187         rule->wc.wildcards |= FWW_ARP_THA;
1188         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1189         break;
1190
1191     case MFF_TCP_SRC:
1192     case MFF_UDP_SRC:
1193     case MFF_ICMP_TYPE:
1194         rule->wc.wildcards |= FWW_TP_SRC;
1195         rule->flow.tp_src = htons(0);
1196         break;
1197
1198     case MFF_TCP_DST:
1199     case MFF_UDP_DST:
1200     case MFF_ICMP_CODE:
1201         rule->wc.wildcards |= FWW_TP_DST;
1202         rule->flow.tp_dst = htons(0);
1203         break;
1204
1205     case MFF_ND_TARGET:
1206         rule->wc.wildcards |= FWW_ND_TARGET;
1207         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1208         break;
1209
1210     case MFF_N_IDS:
1211     default:
1212         NOT_REACHED();
1213     }
1214 }
1215
1216 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1217  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1218  * with a 1-bit indicating that the corresponding value bit must match and a
1219  * 0-bit indicating a don't-care.
1220  *
1221  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1222  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1223  * call is equivalent to mf_set_wild(mf, rule).
1224  *
1225  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1226  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1227 void
1228 mf_set(const struct mf_field *mf,
1229        const union mf_value *value, const union mf_value *mask,
1230        struct cls_rule *rule)
1231 {
1232     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1233         mf_set_value(mf, value, rule);
1234         return;
1235     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1236         mf_set_wild(mf, rule);
1237         return;
1238     }
1239
1240     switch (mf->id) {
1241     case MFF_IN_PORT:
1242     case MFF_ETH_SRC:
1243     case MFF_ETH_TYPE:
1244     case MFF_VLAN_VID:
1245     case MFF_VLAN_PCP:
1246     case MFF_IPV6_LABEL:
1247     case MFF_IP_PROTO:
1248     case MFF_IP_TTL:
1249     case MFF_IP_DSCP:
1250     case MFF_IP_ECN:
1251     case MFF_ARP_OP:
1252     case MFF_ARP_SHA:
1253     case MFF_ARP_THA:
1254     case MFF_TCP_SRC:
1255     case MFF_TCP_DST:
1256     case MFF_UDP_SRC:
1257     case MFF_UDP_DST:
1258     case MFF_ICMP_TYPE:
1259     case MFF_ICMP_CODE:
1260     case MFF_ND_TARGET:
1261     case MFF_ND_SLL:
1262     case MFF_ND_TLL:
1263         NOT_REACHED();
1264
1265     case MFF_TUN_ID:
1266         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1267         break;
1268
1269 #if FLOW_N_REGS > 0
1270     case MFF_REG0:
1271 #endif
1272 #if FLOW_N_REGS > 1
1273     case MFF_REG1:
1274 #endif
1275 #if FLOW_N_REGS > 2
1276     case MFF_REG2:
1277 #endif
1278 #if FLOW_N_REGS > 3
1279     case MFF_REG3:
1280 #endif
1281 #if FLOW_N_REGS > 4
1282     case MFF_REG4:
1283 #endif
1284 #if FLOW_N_REGS > 5
1285 #error
1286 #endif
1287         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1288                                 ntohl(value->be32), ntohl(mask->be32));
1289         break;
1290
1291     case MFF_ETH_DST:
1292         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1293             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1294         }
1295         break;
1296
1297     case MFF_VLAN_TCI:
1298         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1299         break;
1300
1301     case MFF_IPV4_SRC:
1302         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1303         break;
1304
1305     case MFF_IPV4_DST:
1306         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1307         break;
1308
1309     case MFF_IPV6_SRC:
1310         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1311         break;
1312
1313     case MFF_IPV6_DST:
1314         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1315         break;
1316
1317     case MFF_IP_FRAG:
1318         cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1319         break;
1320
1321     case MFF_ARP_SPA:
1322         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1323         break;
1324
1325     case MFF_ARP_TPA:
1326         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1327         break;
1328
1329     case MFF_N_IDS:
1330     default:
1331         NOT_REACHED();
1332     }
1333 }
1334
1335 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1336  * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1337  * 'x'.
1338  *
1339  * Example: suppose that 'mf' is originally the following 2-byte field in
1340  * 'rule':
1341  *
1342  *     value == 0xe00a == 2#1110000000001010
1343  *      mask == 0xfc3f == 2#1111110000111111
1344  *
1345  * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1346  * effect (note that 0x55 is 2#1010101):
1347  *
1348  *     value == 0xd50a == 2#1101010100001010
1349  *      mask == 0xff3f == 2#1111111100111111
1350  *
1351  * The caller is responsible for ensuring that the result will be a valid
1352  * wildcard pattern for 'mf'.  The caller is responsible for ensuring that
1353  * 'rule' meets 'mf''s prerequisites. */
1354 void
1355 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1356                 unsigned int n_bits, struct cls_rule *rule)
1357 {
1358     if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1359         union mf_value value;
1360         int i;
1361
1362         for (i = mf->n_bytes - 1; i >= 0; i--) {
1363             ((uint8_t *) &value)[i] = x;
1364             x >>= 8;
1365         }
1366         mf_set_value(mf, &value, rule);
1367     } else {
1368         union mf_value value, mask;
1369         uint8_t *vp, *mp;
1370         unsigned int byte_ofs;
1371
1372         mf_get(mf, rule, &value, &mask);
1373
1374         byte_ofs = mf->n_bytes - ofs / 8;
1375         vp = &((uint8_t *) &value)[byte_ofs];
1376         mp = &((uint8_t *) &mask)[byte_ofs];
1377         if (ofs % 8) {
1378             unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1379             uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1380
1381             *--vp &= ~chunk_mask;
1382             *vp   |= chunk_mask & (x << (ofs % 8));
1383             *--mp |= chunk_mask;
1384
1385             x >>= chunk;
1386             n_bits -= chunk;
1387             ofs += chunk;
1388         }
1389         while (n_bits >= 8) {
1390             *--vp = x;
1391             *--mp = 0xff;
1392             x >>= 8;
1393             n_bits -= 8;
1394             ofs += 8;
1395         }
1396         if (n_bits) {
1397             uint8_t chunk_mask = (1 << n_bits) - 1;
1398
1399             *--vp &= ~chunk_mask;
1400             *vp   |= chunk_mask & x;
1401             *--mp |= chunk_mask;
1402         }
1403
1404         mf_set(mf, &value, &mask, rule);
1405     }
1406 }
1407
1408 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1409  * 'value' and 'mask', respectively. */
1410 void
1411 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1412        union mf_value *value, union mf_value *mask)
1413 {
1414     mf_get_value(mf, &rule->flow, value);
1415     mf_get_mask(mf, &rule->wc, mask);
1416 }
1417
1418 /* Assigns a random value for field 'mf' to 'value'. */
1419 void
1420 mf_random_value(const struct mf_field *mf, union mf_value *value)
1421 {
1422     random_bytes(value, mf->n_bytes);
1423
1424     switch (mf->id) {
1425     case MFF_TUN_ID:
1426     case MFF_IN_PORT:
1427 #if FLOW_N_REGS > 0
1428     case MFF_REG0:
1429 #endif
1430 #if FLOW_N_REGS > 1
1431     case MFF_REG1:
1432 #endif
1433 #if FLOW_N_REGS > 2
1434     case MFF_REG2:
1435 #endif
1436 #if FLOW_N_REGS > 3
1437     case MFF_REG3:
1438 #endif
1439 #if FLOW_N_REGS > 4
1440     case MFF_REG4:
1441 #endif
1442 #if FLOW_N_REGS > 5
1443 #error
1444 #endif
1445     case MFF_ETH_SRC:
1446     case MFF_ETH_DST:
1447     case MFF_ETH_TYPE:
1448     case MFF_VLAN_TCI:
1449     case MFF_IPV4_SRC:
1450     case MFF_IPV4_DST:
1451     case MFF_IPV6_SRC:
1452     case MFF_IPV6_DST:
1453     case MFF_IP_PROTO:
1454     case MFF_IP_TTL:
1455     case MFF_ARP_SPA:
1456     case MFF_ARP_TPA:
1457     case MFF_ARP_SHA:
1458     case MFF_ARP_THA:
1459     case MFF_TCP_SRC:
1460     case MFF_TCP_DST:
1461     case MFF_UDP_SRC:
1462     case MFF_UDP_DST:
1463     case MFF_ICMP_TYPE:
1464     case MFF_ICMP_CODE:
1465     case MFF_ND_TARGET:
1466     case MFF_ND_SLL:
1467     case MFF_ND_TLL:
1468         break;
1469
1470     case MFF_IPV6_LABEL:
1471         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1472         break;
1473
1474     case MFF_IP_DSCP:
1475         value->u8 &= IP_DSCP_MASK;
1476         break;
1477
1478     case MFF_IP_ECN:
1479         value->u8 &= IP_ECN_MASK;
1480         break;
1481
1482     case MFF_IP_FRAG:
1483         value->u8 &= FLOW_NW_FRAG_MASK;
1484         break;
1485
1486     case MFF_ARP_OP:
1487         value->be16 &= htons(0xff);
1488         break;
1489
1490     case MFF_VLAN_VID:
1491         value->be16 &= htons(VLAN_VID_MASK);
1492         break;
1493
1494     case MFF_VLAN_PCP:
1495         value->u8 &= 0x07;
1496         break;
1497
1498     case MFF_N_IDS:
1499     default:
1500         NOT_REACHED();
1501     }
1502 }
1503
1504 static char *
1505 mf_from_integer_string(const struct mf_field *mf, const char *s,
1506                        uint8_t *valuep, uint8_t *maskp)
1507 {
1508     unsigned long long int integer, mask;
1509     char *tail;
1510     int i;
1511
1512     errno = 0;
1513     integer = strtoull(s, &tail, 0);
1514     if (errno || (*tail != '\0' && *tail != '/')) {
1515         goto syntax_error;
1516     }
1517
1518     if (*tail == '/') {
1519         mask = strtoull(tail + 1, &tail, 0);
1520         if (errno || *tail != '\0') {
1521             goto syntax_error;
1522         }
1523     } else {
1524         mask = ULLONG_MAX;
1525     }
1526
1527     for (i = mf->n_bytes - 1; i >= 0; i--) {
1528         valuep[i] = integer;
1529         maskp[i] = mask;
1530         integer >>= 8;
1531         mask >>= 8;
1532     }
1533     if (integer) {
1534         return xasprintf("%s: value too large for %u-byte field %s",
1535                          s, mf->n_bytes, mf->name);
1536     }
1537     return NULL;
1538
1539 syntax_error:
1540     return xasprintf("%s: bad syntax for %s", s, mf->name);
1541 }
1542
1543 static char *
1544 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1545                         uint8_t mac[ETH_ADDR_LEN],
1546                         uint8_t mask[ETH_ADDR_LEN])
1547 {
1548     assert(mf->n_bytes == ETH_ADDR_LEN);
1549
1550     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1551                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1552     case ETH_ADDR_SCAN_COUNT * 2:
1553         return NULL;
1554
1555     case ETH_ADDR_SCAN_COUNT:
1556         memset(mask, 0xff, ETH_ADDR_LEN);
1557         return NULL;
1558
1559     default:
1560         return xasprintf("%s: invalid Ethernet address", s);
1561     }
1562 }
1563
1564 static char *
1565 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1566                     ovs_be32 *ip, ovs_be32 *mask)
1567 {
1568     int prefix;
1569
1570     assert(mf->n_bytes == sizeof *ip);
1571
1572     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1573                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1574         /* OK. */
1575     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1576                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1577         if (prefix <= 0 || prefix > 32) {
1578             return xasprintf("%s: network prefix bits not between 1 and "
1579                              "32", s);
1580         } else if (prefix == 32) {
1581             *mask = htonl(UINT32_MAX);
1582         } else {
1583             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1584         }
1585     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1586         *mask = htonl(UINT32_MAX);
1587     } else {
1588         return xasprintf("%s: invalid IP address", s);
1589     }
1590     return NULL;
1591 }
1592
1593 static char *
1594 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1595                     struct in6_addr *value, struct in6_addr *mask)
1596 {
1597     char *str = xstrdup(s);
1598     char *save_ptr = NULL;
1599     const char *name, *netmask;
1600     int retval;
1601
1602     assert(mf->n_bytes == sizeof *value);
1603
1604     name = strtok_r(str, "/", &save_ptr);
1605     retval = name ? lookup_ipv6(name, value) : EINVAL;
1606     if (retval) {
1607         char *err;
1608
1609         err = xasprintf("%s: could not convert to IPv6 address", str);
1610         free(str);
1611
1612         return err;
1613     }
1614
1615     netmask = strtok_r(NULL, "/", &save_ptr);
1616     if (netmask) {
1617         int prefix = atoi(netmask);
1618         if (prefix <= 0 || prefix > 128) {
1619             free(str);
1620             return xasprintf("%s: prefix bits not between 1 and 128", s);
1621         } else {
1622             *mask = ipv6_create_mask(prefix);
1623         }
1624     } else {
1625         *mask = in6addr_exact;
1626     }
1627     free(str);
1628
1629     return NULL;
1630 }
1631
1632 static char *
1633 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1634                         ovs_be16 *valuep, ovs_be16 *maskp)
1635 {
1636     uint16_t port;
1637
1638     assert(mf->n_bytes == sizeof(ovs_be16));
1639     if (ofputil_port_from_string(s, &port)) {
1640         *valuep = htons(port);
1641         *maskp = htons(UINT16_MAX);
1642         return NULL;
1643     } else {
1644         return mf_from_integer_string(mf, s,
1645                                       (uint8_t *) valuep, (uint8_t *) maskp);
1646     }
1647 }
1648
1649 struct frag_handling {
1650     const char *name;
1651     uint8_t mask;
1652     uint8_t value;
1653 };
1654
1655 static const struct frag_handling all_frags[] = {
1656 #define A FLOW_NW_FRAG_ANY
1657 #define L FLOW_NW_FRAG_LATER
1658     /* name               mask  value */
1659
1660     { "no",               A|L,  0     },
1661     { "first",            A|L,  A     },
1662     { "later",            A|L,  A|L   },
1663
1664     { "no",               A,    0     },
1665     { "yes",              A,    A     },
1666
1667     { "not_later",        L,    0     },
1668     { "later",            L,    L     },
1669 #undef A
1670 #undef L
1671 };
1672
1673 static char *
1674 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1675 {
1676     const struct frag_handling *h;
1677
1678     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1679         if (!strcasecmp(s, h->name)) {
1680             /* We force the upper bits of the mask on to make mf_parse_value()
1681              * happy (otherwise it will never think it's an exact match.) */
1682             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1683             *valuep = h->value;
1684             return NULL;
1685         }
1686     }
1687
1688     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1689                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
1690 }
1691
1692 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1693  * NULL if successful, otherwise a malloc()'d string describing the error. */
1694 char *
1695 mf_parse(const struct mf_field *mf, const char *s,
1696          union mf_value *value, union mf_value *mask)
1697 {
1698     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1699         memset(value, 0, mf->n_bytes);
1700         memset(mask, 0, mf->n_bytes);
1701         return NULL;
1702     }
1703
1704     switch (mf->string) {
1705     case MFS_DECIMAL:
1706     case MFS_HEXADECIMAL:
1707         return mf_from_integer_string(mf, s,
1708                                       (uint8_t *) value, (uint8_t *) mask);
1709
1710     case MFS_ETHERNET:
1711         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1712
1713     case MFS_IPV4:
1714         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1715
1716     case MFS_IPV6:
1717         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1718
1719     case MFS_OFP_PORT:
1720         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1721
1722     case MFS_FRAG:
1723         return mf_from_frag_string(s, &value->u8, &mask->u8);
1724     }
1725     NOT_REACHED();
1726 }
1727
1728 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1729  * successful, otherwise a malloc()'d string describing the error. */
1730 char *
1731 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1732 {
1733     union mf_value mask;
1734     char *error;
1735
1736     error = mf_parse(mf, s, value, &mask);
1737     if (error) {
1738         return error;
1739     }
1740
1741     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1742         return xasprintf("%s: wildcards not allowed here", s);
1743     }
1744     return NULL;
1745 }
1746
1747 static void
1748 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1749                          const uint8_t *maskp, struct ds *s)
1750 {
1751     unsigned long long int integer;
1752     int i;
1753
1754     assert(mf->n_bytes <= 8);
1755
1756     integer = 0;
1757     for (i = 0; i < mf->n_bytes; i++) {
1758         integer = (integer << 8) | valuep[i];
1759     }
1760     if (mf->string == MFS_HEXADECIMAL) {
1761         ds_put_format(s, "%#llx", integer);
1762     } else {
1763         ds_put_format(s, "%lld", integer);
1764     }
1765
1766     if (maskp) {
1767         unsigned long long int mask;
1768
1769         mask = 0;
1770         for (i = 0; i < mf->n_bytes; i++) {
1771             mask = (mask << 8) | maskp[i];
1772         }
1773
1774         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1775          * not sure that that a bit-mask written in decimal is ever easier to
1776          * understand than the same bit-mask written in hexadecimal. */
1777         ds_put_format(s, "/%#llx", mask);
1778     }
1779 }
1780
1781 static void
1782 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1783                       struct ds *s)
1784 {
1785     const struct frag_handling *h;
1786     uint8_t value = *valuep;
1787     uint8_t mask = *maskp;
1788
1789     value &= mask;
1790     mask &= FLOW_NW_FRAG_MASK;
1791
1792     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1793         if (value == h->value && mask == h->mask) {
1794             ds_put_cstr(s, h->name);
1795             return;
1796         }
1797     }
1798     ds_put_cstr(s, "<error>");
1799 }
1800
1801 /* Appends to 's' a string representation of field 'mf' whose value is in
1802  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
1803 void
1804 mf_format(const struct mf_field *mf,
1805           const union mf_value *value, const union mf_value *mask,
1806           struct ds *s)
1807 {
1808     if (mask) {
1809         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1810             ds_put_cstr(s, "ANY");
1811             return;
1812         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1813             mask = NULL;
1814         }
1815     }
1816
1817     switch (mf->string) {
1818     case MFS_OFP_PORT:
1819         if (!mask) {
1820             ofputil_format_port(ntohs(value->be16), s);
1821             break;
1822         }
1823         /* fall through */
1824     case MFS_DECIMAL:
1825     case MFS_HEXADECIMAL:
1826         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1827         break;
1828
1829     case MFS_ETHERNET:
1830         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1831         if (mask) {
1832             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1833         }
1834         break;
1835
1836     case MFS_IPV4:
1837         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1838                          s);
1839         break;
1840
1841     case MFS_IPV6:
1842         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1843         break;
1844
1845     case MFS_FRAG:
1846         mf_format_frag_string(&value->u8, &mask->u8, s);
1847         break;
1848
1849     default:
1850         NOT_REACHED();
1851     }
1852 }