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