2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
38 #include "unaligned.h"
42 VLOG_DEFINE_THIS_MODULE(odp_util);
44 /* The interface between userspace and kernel uses an "OVS_*" prefix.
45 * Since this is fairly non-specific for the OVS userspace components,
46 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
47 * interactions with the datapath.
50 /* The set of characters that may separate one action or one key attribute
52 static const char *delimiters = ", \t\r\n";
54 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
55 struct ofpbuf *, struct ofpbuf *);
56 static void format_odp_key_attr(const struct nlattr *a,
57 const struct nlattr *ma,
58 const struct hmap *portno_names, struct ds *ds,
61 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
64 * - For an action whose argument has a fixed length, returned that
65 * nonnegative length in bytes.
67 * - For an action with a variable-length argument, returns -2.
69 * - For an invalid 'type', returns -1. */
71 odp_action_len(uint16_t type)
73 if (type > OVS_ACTION_ATTR_MAX) {
77 switch ((enum ovs_action_attr) type) {
78 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
79 case OVS_ACTION_ATTR_TUNNEL_PUSH: return -2;
80 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
81 case OVS_ACTION_ATTR_USERSPACE: return -2;
82 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
83 case OVS_ACTION_ATTR_POP_VLAN: return 0;
84 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
85 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
86 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
87 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
88 case OVS_ACTION_ATTR_SET: return -2;
89 case OVS_ACTION_ATTR_SET_MASKED: return -2;
90 case OVS_ACTION_ATTR_SAMPLE: return -2;
92 case OVS_ACTION_ATTR_UNSPEC:
93 case __OVS_ACTION_ATTR_MAX:
100 /* Returns a string form of 'attr'. The return value is either a statically
101 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
102 * should be at least OVS_KEY_ATTR_BUFSIZE. */
103 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
105 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
108 case OVS_KEY_ATTR_UNSPEC: return "unspec";
109 case OVS_KEY_ATTR_ENCAP: return "encap";
110 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
111 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
112 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
113 case OVS_KEY_ATTR_IN_PORT: return "in_port";
114 case OVS_KEY_ATTR_ETHERNET: return "eth";
115 case OVS_KEY_ATTR_VLAN: return "vlan";
116 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
117 case OVS_KEY_ATTR_IPV4: return "ipv4";
118 case OVS_KEY_ATTR_IPV6: return "ipv6";
119 case OVS_KEY_ATTR_TCP: return "tcp";
120 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
121 case OVS_KEY_ATTR_UDP: return "udp";
122 case OVS_KEY_ATTR_SCTP: return "sctp";
123 case OVS_KEY_ATTR_ICMP: return "icmp";
124 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
125 case OVS_KEY_ATTR_ARP: return "arp";
126 case OVS_KEY_ATTR_ND: return "nd";
127 case OVS_KEY_ATTR_MPLS: return "mpls";
128 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
129 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
131 case __OVS_KEY_ATTR_MAX:
133 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
139 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
141 size_t len = nl_attr_get_size(a);
143 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
145 const uint8_t *unspec;
148 unspec = nl_attr_get(a);
149 for (i = 0; i < len; i++) {
150 ds_put_char(ds, i ? ' ': '(');
151 ds_put_format(ds, "%02x", unspec[i]);
153 ds_put_char(ds, ')');
158 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
160 static const struct nl_policy ovs_sample_policy[] = {
161 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
162 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
164 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
166 const struct nlattr *nla_acts;
169 ds_put_cstr(ds, "sample");
171 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
172 ds_put_cstr(ds, "(error)");
176 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
179 ds_put_format(ds, "(sample=%.1f%%,", percentage);
181 ds_put_cstr(ds, "actions(");
182 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
183 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
184 format_odp_actions(ds, nla_acts, len);
185 ds_put_format(ds, "))");
189 slow_path_reason_to_string(uint32_t reason)
191 switch ((enum slow_path_reason) reason) {
192 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
201 slow_path_reason_to_explanation(enum slow_path_reason reason)
204 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
213 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
214 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
219 /* Parse masked flags in numeric format? */
220 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
221 res_flags, res_mask, &n) && n > 0) {
222 if (*res_flags & ~allowed || *res_mask & ~allowed) {
230 if (res_mask && (*s == '+' || *s == '-')) {
231 uint32_t flags = 0, mask = 0;
233 /* Parse masked flags. */
234 while (s[n] != ')') {
241 } else if (s[n] == '-') {
248 name_len = strcspn(s + n, "+-)");
250 for (bit = 1; bit; bit <<= 1) {
251 const char *fname = bit_to_string(bit);
259 if (len != name_len) {
262 if (!strncmp(s + n, fname, len)) {
264 /* bit already set. */
267 if (!(bit & allowed)) {
279 return -EINVAL; /* Unknown flag name */
289 /* Parse unmasked flags. If a flag is present, it is set, otherwise
291 while (s[n] != ')') {
292 unsigned long long int flags;
296 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
297 if (flags & ~allowed) {
300 n += n0 + (s[n + n0] == ',');
305 for (bit = 1; bit; bit <<= 1) {
306 const char *name = bit_to_string(bit);
314 if (!strncmp(s + n, name, len) &&
315 (s[n + len] == ',' || s[n + len] == ')')) {
316 if (!(bit & allowed)) {
320 n += len + (s[n + len] == ',');
332 *res_mask = UINT32_MAX;
338 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
340 static const struct nl_policy ovs_userspace_policy[] = {
341 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
342 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
344 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
347 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
348 const struct nlattr *userdata_attr;
349 const struct nlattr *tunnel_out_port_attr;
351 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
352 ds_put_cstr(ds, "userspace(error)");
356 ds_put_format(ds, "userspace(pid=%"PRIu32,
357 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
359 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
362 const uint8_t *userdata = nl_attr_get(userdata_attr);
363 size_t userdata_len = nl_attr_get_size(userdata_attr);
364 bool userdata_unspec = true;
365 union user_action_cookie cookie;
367 if (userdata_len >= sizeof cookie.type
368 && userdata_len <= sizeof cookie) {
370 memset(&cookie, 0, sizeof cookie);
371 memcpy(&cookie, userdata, userdata_len);
373 userdata_unspec = false;
375 if (userdata_len == sizeof cookie.sflow
376 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
377 ds_put_format(ds, ",sFlow("
378 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
379 vlan_tci_to_vid(cookie.sflow.vlan_tci),
380 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
381 cookie.sflow.output);
382 } else if (userdata_len == sizeof cookie.slow_path
383 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
384 ds_put_cstr(ds, ",slow_path(");
385 format_flags(ds, slow_path_reason_to_string,
386 cookie.slow_path.reason, ',');
387 ds_put_format(ds, ")");
388 } else if (userdata_len == sizeof cookie.flow_sample
389 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
390 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
391 ",collector_set_id=%"PRIu32
392 ",obs_domain_id=%"PRIu32
393 ",obs_point_id=%"PRIu32")",
394 cookie.flow_sample.probability,
395 cookie.flow_sample.collector_set_id,
396 cookie.flow_sample.obs_domain_id,
397 cookie.flow_sample.obs_point_id);
398 } else if (userdata_len >= sizeof cookie.ipfix
399 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
400 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
401 cookie.ipfix.output_odp_port);
403 userdata_unspec = true;
407 if (userdata_unspec) {
409 ds_put_format(ds, ",userdata(");
410 for (i = 0; i < userdata_len; i++) {
411 ds_put_format(ds, "%02x", userdata[i]);
413 ds_put_char(ds, ')');
417 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
418 if (tunnel_out_port_attr) {
419 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
420 nl_attr_get_u32(tunnel_out_port_attr));
423 ds_put_char(ds, ')');
427 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
429 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
430 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
431 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
432 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
434 ds_put_char(ds, ',');
436 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
437 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
438 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
439 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
441 ds_put_char(ds, ',');
443 if (!(tci & htons(VLAN_CFI))) {
444 ds_put_cstr(ds, "cfi=0");
445 ds_put_char(ds, ',');
451 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
453 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
454 mpls_lse_to_label(mpls_lse),
455 mpls_lse_to_tc(mpls_lse),
456 mpls_lse_to_ttl(mpls_lse),
457 mpls_lse_to_bos(mpls_lse));
461 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
462 const struct ovs_key_mpls *mpls_mask, int n)
465 ovs_be32 key = mpls_key->mpls_lse;
467 if (mpls_mask == NULL) {
468 format_mpls_lse(ds, key);
470 ovs_be32 mask = mpls_mask->mpls_lse;
472 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
473 mpls_lse_to_label(key), mpls_lse_to_label(mask),
474 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
475 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
476 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
481 for (i = 0; i < n; i++) {
482 ds_put_format(ds, "lse%d=%#"PRIx32,
483 i, ntohl(mpls_key[i].mpls_lse));
485 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
487 ds_put_char(ds, ',');
494 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
496 ds_put_format(ds, "recirc(%"PRIu32")", recirc_id);
500 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
502 ds_put_format(ds, "hash(");
504 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
505 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
507 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
510 ds_put_format(ds, ")");
514 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
516 const struct eth_header *eth;
517 const struct ip_header *ip;
520 eth = (const struct eth_header *)data->header;
523 ip = (const struct ip_header *)l3;
526 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
527 data->header_len, data->tnl_type);
528 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
529 ds_put_format(ds, ",src=");
530 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
531 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
534 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
535 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
536 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
537 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
538 ip->ip_proto, ip->ip_tos,
542 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
543 const struct vxlanhdr *vxh;
544 const struct udp_header *udp;
547 udp = (const struct udp_header *) (ip + 1);
548 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16"),",
549 ntohs(udp->udp_src), ntohs(udp->udp_dst));
552 vxh = (const struct vxlanhdr *) (udp + 1);
553 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
554 ntohl(get_16aligned_be32(&vxh->vx_flags)),
555 ntohl(get_16aligned_be32(&vxh->vx_vni)));
556 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
557 const struct gre_base_hdr *greh;
558 ovs_16aligned_be32 *options;
561 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
562 greh = (const struct gre_base_hdr *) l4;
564 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
565 greh->flags, ntohs(greh->protocol));
566 options = (ovs_16aligned_be32 *)(greh + 1);
567 if (greh->flags & htons(GRE_CSUM)) {
568 ds_put_format(ds, ",csum=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
571 if (greh->flags & htons(GRE_KEY)) {
572 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
575 if (greh->flags & htons(GRE_SEQ)) {
576 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
579 ds_put_format(ds, ")");
581 ds_put_format(ds, ")");
585 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
587 struct ovs_action_push_tnl *data;
589 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
591 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
592 format_odp_tnl_push_header(ds, data);
593 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
597 format_odp_action(struct ds *ds, const struct nlattr *a)
600 enum ovs_action_attr type = nl_attr_type(a);
601 const struct ovs_action_push_vlan *vlan;
604 expected_len = odp_action_len(nl_attr_type(a));
605 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
606 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
607 nl_attr_get_size(a), expected_len);
608 format_generic_odp_action(ds, a);
613 case OVS_ACTION_ATTR_OUTPUT:
614 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
616 case OVS_ACTION_ATTR_TUNNEL_POP:
617 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
619 case OVS_ACTION_ATTR_TUNNEL_PUSH:
620 format_odp_tnl_push_action(ds, a);
622 case OVS_ACTION_ATTR_USERSPACE:
623 format_odp_userspace_action(ds, a);
625 case OVS_ACTION_ATTR_RECIRC:
626 format_odp_recirc_action(ds, nl_attr_get_u32(a));
628 case OVS_ACTION_ATTR_HASH:
629 format_odp_hash_action(ds, nl_attr_get(a));
631 case OVS_ACTION_ATTR_SET_MASKED:
633 size = nl_attr_get_size(a) / 2;
634 ds_put_cstr(ds, "set(");
636 /* Masked set action not supported for tunnel key, which is bigger. */
637 if (size <= sizeof(struct ovs_key_ipv6)) {
638 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
639 sizeof(struct nlattr))];
640 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
641 sizeof(struct nlattr))];
643 mask->nla_type = attr->nla_type = nl_attr_type(a);
644 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
645 memcpy(attr + 1, (char *)(a + 1), size);
646 memcpy(mask + 1, (char *)(a + 1) + size, size);
647 format_odp_key_attr(attr, mask, NULL, ds, false);
649 format_odp_key_attr(a, NULL, NULL, ds, false);
651 ds_put_cstr(ds, ")");
653 case OVS_ACTION_ATTR_SET:
654 ds_put_cstr(ds, "set(");
655 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
656 ds_put_cstr(ds, ")");
658 case OVS_ACTION_ATTR_PUSH_VLAN:
659 vlan = nl_attr_get(a);
660 ds_put_cstr(ds, "push_vlan(");
661 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
662 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
664 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
665 ds_put_char(ds, ')');
667 case OVS_ACTION_ATTR_POP_VLAN:
668 ds_put_cstr(ds, "pop_vlan");
670 case OVS_ACTION_ATTR_PUSH_MPLS: {
671 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
672 ds_put_cstr(ds, "push_mpls(");
673 format_mpls_lse(ds, mpls->mpls_lse);
674 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
677 case OVS_ACTION_ATTR_POP_MPLS: {
678 ovs_be16 ethertype = nl_attr_get_be16(a);
679 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
682 case OVS_ACTION_ATTR_SAMPLE:
683 format_odp_sample_action(ds, a);
685 case OVS_ACTION_ATTR_UNSPEC:
686 case __OVS_ACTION_ATTR_MAX:
688 format_generic_odp_action(ds, a);
694 format_odp_actions(struct ds *ds, const struct nlattr *actions,
698 const struct nlattr *a;
701 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
703 ds_put_char(ds, ',');
705 format_odp_action(ds, a);
710 if (left == actions_len) {
711 ds_put_cstr(ds, "<empty>");
713 ds_put_format(ds, ",***%u leftover bytes*** (", left);
714 for (i = 0; i < left; i++) {
715 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
717 ds_put_char(ds, ')');
720 ds_put_cstr(ds, "drop");
724 /* Separate out parse_odp_userspace_action() function. */
726 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
729 union user_action_cookie cookie;
731 odp_port_t tunnel_out_port;
733 void *user_data = NULL;
734 size_t user_data_size = 0;
736 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
742 uint32_t probability;
743 uint32_t collector_set_id;
744 uint32_t obs_domain_id;
745 uint32_t obs_point_id;
748 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
749 "pcp=%i,output=%"SCNi32")%n",
750 &vid, &pcp, &output, &n1)) {
754 tci = vid | (pcp << VLAN_PCP_SHIFT);
759 cookie.type = USER_ACTION_COOKIE_SFLOW;
760 cookie.sflow.vlan_tci = htons(tci);
761 cookie.sflow.output = output;
763 user_data_size = sizeof cookie.sflow;
764 } else if (ovs_scan(&s[n], ",slow_path(%n",
769 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
770 cookie.slow_path.unused = 0;
771 cookie.slow_path.reason = 0;
773 res = parse_flags(&s[n], slow_path_reason_to_string,
774 &cookie.slow_path.reason,
775 SLOW_PATH_REASON_MASK, NULL);
776 if (res < 0 || s[n + res] != ')') {
782 user_data_size = sizeof cookie.slow_path;
783 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
784 "collector_set_id=%"SCNi32","
785 "obs_domain_id=%"SCNi32","
786 "obs_point_id=%"SCNi32")%n",
787 &probability, &collector_set_id,
788 &obs_domain_id, &obs_point_id, &n1)) {
791 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
792 cookie.flow_sample.probability = probability;
793 cookie.flow_sample.collector_set_id = collector_set_id;
794 cookie.flow_sample.obs_domain_id = obs_domain_id;
795 cookie.flow_sample.obs_point_id = obs_point_id;
797 user_data_size = sizeof cookie.flow_sample;
798 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
801 cookie.type = USER_ACTION_COOKIE_IPFIX;
802 cookie.ipfix.output_odp_port = u32_to_odp(output);
804 user_data_size = sizeof cookie.ipfix;
805 } else if (ovs_scan(&s[n], ",userdata(%n",
810 ofpbuf_init(&buf, 16);
811 end = ofpbuf_put_hex(&buf, &s[n], NULL);
815 user_data = ofpbuf_data(&buf);
816 user_data_size = ofpbuf_size(&buf);
823 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
824 &tunnel_out_port, &n1)) {
825 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
827 } else if (s[n] == ')') {
828 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
837 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
839 struct eth_header *eth;
840 struct ip_header *ip;
841 struct udp_header *udp;
842 struct gre_base_hdr *greh;
843 uint16_t gre_proto, dl_type, udp_src, udp_dst;
845 uint32_t tnl_type = 0, header_len = 0;
849 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
852 eth = (struct eth_header *) data->header;
853 l3 = (data->header + sizeof *eth);
854 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
855 ip = (struct ip_header *) l3;
856 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
857 "eth(dst="ETH_ADDR_SCAN_FMT",",
860 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
864 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
865 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
868 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
871 eth->eth_type = htons(dl_type);
874 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
875 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
878 &ip->ip_proto, &ip->ip_tos,
879 &ip->ip_ttl, &ip->ip_frag_off)) {
882 put_16aligned_be32(&ip->ip_src, sip);
883 put_16aligned_be32(&ip->ip_dst, dip);
886 udp = (struct udp_header *) l4;
887 greh = (struct gre_base_hdr *) l4;
888 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16"),",
889 &udp_src, &udp_dst)) {
890 struct vxlanhdr *vxh;
891 uint32_t vx_flags, vx_vni;
893 udp->udp_src = htons(udp_src);
894 udp->udp_dst = htons(udp_dst);
898 vxh = (struct vxlanhdr *) (udp + 1);
899 if (!ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
900 &vx_flags, &vx_vni)) {
903 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
904 put_16aligned_be32(&vxh->vx_vni, htonl(vx_vni));
905 tnl_type = OVS_VPORT_TYPE_VXLAN;
906 header_len = sizeof *eth + sizeof *ip +
907 sizeof *udp + sizeof *vxh;
908 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
909 &greh->flags, &gre_proto)){
911 tnl_type = OVS_VPORT_TYPE_GRE;
912 greh->protocol = htons(gre_proto);
913 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
915 if (greh->flags & htons(GRE_CSUM)) {
918 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx32, &csum)) {
921 put_16aligned_be32(options, htonl(csum));
924 if (greh->flags & htons(GRE_KEY)) {
927 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
931 put_16aligned_be32(options, htonl(key));
934 if (greh->flags & htons(GRE_SEQ)) {
937 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
940 put_16aligned_be32(options, htonl(seq));
944 if (!ovs_scan_len(s, &n, "))")) {
948 header_len = sizeof *eth + sizeof *ip +
949 ((uint8_t *) options - (uint8_t *) greh);
954 /* check tunnel meta data. */
955 if (data->tnl_type != tnl_type) {
958 if (data->header_len != header_len) {
963 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
971 parse_odp_action(const char *s, const struct simap *port_names,
972 struct ofpbuf *actions)
978 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
979 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
985 int len = strcspn(s, delimiters);
986 struct simap_node *node;
988 node = simap_find_len(port_names, s, len);
990 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
999 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1000 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1005 if (!strncmp(s, "userspace(", 10)) {
1006 return parse_odp_userspace_action(s, actions);
1009 if (!strncmp(s, "set(", 4)) {
1012 struct nlattr mask[128 / sizeof(struct nlattr)];
1013 struct ofpbuf maskbuf;
1014 struct nlattr *nested, *key;
1017 /* 'mask' is big enough to hold any key. */
1018 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1020 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1021 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1025 if (s[retval + 4] != ')') {
1029 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1032 size = nl_attr_get_size(mask);
1033 if (size == nl_attr_get_size(key)) {
1034 /* Change to masked set action if not fully masked. */
1035 if (!is_all_ones(mask + 1, size)) {
1036 key->nla_len += size;
1037 ofpbuf_put(actions, mask + 1, size);
1038 /* 'actions' may have been reallocated by ofpbuf_put(). */
1039 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1040 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1044 nl_msg_end_nested(actions, start_ofs);
1049 struct ovs_action_push_vlan push;
1050 int tpid = ETH_TYPE_VLAN;
1055 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1056 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1057 &vid, &pcp, &cfi, &n)
1058 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1059 &tpid, &vid, &pcp, &n)
1060 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1061 &tpid, &vid, &pcp, &cfi, &n)) {
1062 push.vlan_tpid = htons(tpid);
1063 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1064 | (pcp << VLAN_PCP_SHIFT)
1065 | (cfi ? VLAN_CFI : 0));
1066 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1067 &push, sizeof push);
1073 if (!strncmp(s, "pop_vlan", 8)) {
1074 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1082 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1083 && percentage >= 0. && percentage <= 100.0) {
1084 size_t sample_ofs, actions_ofs;
1087 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1088 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1089 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1090 (probability <= 0 ? 0
1091 : probability >= UINT32_MAX ? UINT32_MAX
1094 actions_ofs = nl_msg_start_nested(actions,
1095 OVS_SAMPLE_ATTR_ACTIONS);
1099 n += strspn(s + n, delimiters);
1104 retval = parse_odp_action(s + n, port_names, actions);
1110 nl_msg_end_nested(actions, actions_ofs);
1111 nl_msg_end_nested(actions, sample_ofs);
1113 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1121 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1122 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1128 struct ovs_action_push_tnl data;
1131 n = ovs_parse_tnl_push(s, &data);
1133 odp_put_tnl_push_action(actions, &data);
1142 /* Parses the string representation of datapath actions, in the format output
1143 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1144 * value. On success, the ODP actions are appended to 'actions' as a series of
1145 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1146 * way, 'actions''s data might be reallocated. */
1148 odp_actions_from_string(const char *s, const struct simap *port_names,
1149 struct ofpbuf *actions)
1153 if (!strcasecmp(s, "drop")) {
1157 old_size = ofpbuf_size(actions);
1161 s += strspn(s, delimiters);
1166 retval = parse_odp_action(s, port_names, actions);
1167 if (retval < 0 || !strchr(delimiters, s[retval])) {
1168 ofpbuf_set_size(actions, old_size);
1177 /* Returns the correct length of the payload for a flow key attribute of the
1178 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
1179 * is variable length. */
1181 odp_flow_key_attr_len(uint16_t type)
1183 if (type > OVS_KEY_ATTR_MAX) {
1187 switch ((enum ovs_key_attr) type) {
1188 case OVS_KEY_ATTR_ENCAP: return -2;
1189 case OVS_KEY_ATTR_PRIORITY: return 4;
1190 case OVS_KEY_ATTR_SKB_MARK: return 4;
1191 case OVS_KEY_ATTR_DP_HASH: return 4;
1192 case OVS_KEY_ATTR_RECIRC_ID: return 4;
1193 case OVS_KEY_ATTR_TUNNEL: return -2;
1194 case OVS_KEY_ATTR_IN_PORT: return 4;
1195 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
1196 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
1197 case OVS_KEY_ATTR_ETHERTYPE: return 2;
1198 case OVS_KEY_ATTR_MPLS: return -2;
1199 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
1200 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
1201 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
1202 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
1203 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
1204 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
1205 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
1206 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
1207 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
1208 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
1210 case OVS_KEY_ATTR_UNSPEC:
1211 case __OVS_KEY_ATTR_MAX:
1219 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1221 size_t len = nl_attr_get_size(a);
1223 const uint8_t *unspec;
1226 unspec = nl_attr_get(a);
1227 for (i = 0; i < len; i++) {
1229 ds_put_char(ds, ' ');
1231 ds_put_format(ds, "%02x", unspec[i]);
1237 ovs_frag_type_to_string(enum ovs_frag_type type)
1240 case OVS_FRAG_TYPE_NONE:
1242 case OVS_FRAG_TYPE_FIRST:
1244 case OVS_FRAG_TYPE_LATER:
1246 case __OVS_FRAG_TYPE_MAX:
1253 tunnel_key_attr_len(int type)
1256 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
1257 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
1258 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1259 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1260 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1261 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1262 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1263 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1264 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1265 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1266 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1267 case __OVS_TUNNEL_KEY_ATTR_MAX:
1273 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1275 parse_geneve_opts(const struct nlattr *attr)
1277 int opts_len = nl_attr_get_size(attr);
1278 const struct geneve_opt *opt = nl_attr_get(attr);
1280 while (opts_len > 0) {
1283 if (opts_len < sizeof(*opt)) {
1287 len = sizeof(*opt) + opt->length * 4;
1288 if (len > opts_len) {
1292 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1294 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1299 opt = opt + len / sizeof(*opt);
1306 enum odp_key_fitness
1307 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1310 const struct nlattr *a;
1312 bool unknown = false;
1314 NL_NESTED_FOR_EACH(a, left, attr) {
1315 uint16_t type = nl_attr_type(a);
1316 size_t len = nl_attr_get_size(a);
1317 int expected_len = tunnel_key_attr_len(type);
1319 if (len != expected_len && expected_len >= 0) {
1320 return ODP_FIT_ERROR;
1324 case OVS_TUNNEL_KEY_ATTR_ID:
1325 tun->tun_id = nl_attr_get_be64(a);
1326 tun->flags |= FLOW_TNL_F_KEY;
1328 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1329 tun->ip_src = nl_attr_get_be32(a);
1331 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1332 tun->ip_dst = nl_attr_get_be32(a);
1334 case OVS_TUNNEL_KEY_ATTR_TOS:
1335 tun->ip_tos = nl_attr_get_u8(a);
1337 case OVS_TUNNEL_KEY_ATTR_TTL:
1338 tun->ip_ttl = nl_attr_get_u8(a);
1341 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1342 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1344 case OVS_TUNNEL_KEY_ATTR_CSUM:
1345 tun->flags |= FLOW_TNL_F_CSUM;
1347 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1348 tun->tp_src = nl_attr_get_be16(a);
1350 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1351 tun->tp_dst = nl_attr_get_be16(a);
1353 case OVS_TUNNEL_KEY_ATTR_OAM:
1354 tun->flags |= FLOW_TNL_F_OAM;
1356 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1357 if (parse_geneve_opts(a)) {
1358 return ODP_FIT_ERROR;
1360 /* It is necessary to reproduce options exactly (including order)
1361 * so it's easiest to just echo them back. */
1366 /* Allow this to show up as unexpected, if there are unknown
1367 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1374 return ODP_FIT_ERROR;
1377 return ODP_FIT_TOO_MUCH;
1379 return ODP_FIT_PERFECT;
1383 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1387 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1389 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1390 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1391 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1393 if (tun_key->ip_src) {
1394 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1396 if (tun_key->ip_dst) {
1397 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1399 if (tun_key->ip_tos) {
1400 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1402 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1403 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1404 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1406 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1407 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1409 if (tun_key->tp_src) {
1410 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1412 if (tun_key->tp_dst) {
1413 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1415 if (tun_key->flags & FLOW_TNL_F_OAM) {
1416 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1419 nl_msg_end_nested(a, tun_key_ofs);
1423 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1425 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1429 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1431 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1432 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1434 if (attr == OVS_KEY_ATTR_IPV6) {
1435 const struct ovs_key_ipv6 *ipv6_mask = mask;
1438 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1439 == htonl(IPV6_LABEL_MASK))
1440 && ipv6_mask->ipv6_proto == UINT8_MAX
1441 && ipv6_mask->ipv6_tclass == UINT8_MAX
1442 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1443 && ipv6_mask->ipv6_frag == UINT8_MAX
1444 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1445 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1447 if (attr == OVS_KEY_ATTR_TUNNEL) {
1448 const struct flow_tnl *tun_mask = mask;
1450 return tun_mask->flags == FLOW_TNL_F_MASK
1451 && tun_mask->tun_id == OVS_BE64_MAX
1452 && tun_mask->ip_src == OVS_BE32_MAX
1453 && tun_mask->ip_dst == OVS_BE32_MAX
1454 && tun_mask->ip_tos == UINT8_MAX
1455 && tun_mask->ip_ttl == UINT8_MAX
1456 && tun_mask->tp_src == OVS_BE16_MAX
1457 && tun_mask->tp_dst == OVS_BE16_MAX;
1460 if (attr == OVS_KEY_ATTR_ARP) {
1461 /* ARP key has padding, ignore it. */
1462 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1463 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1464 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1465 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1468 return is_all_ones(mask, size);
1472 odp_mask_attr_is_exact(const struct nlattr *ma)
1474 struct flow_tnl tun_mask;
1475 enum ovs_key_attr attr = nl_attr_type(ma);
1479 if (attr == OVS_KEY_ATTR_TUNNEL) {
1480 memset(&tun_mask, 0, sizeof tun_mask);
1481 odp_tun_key_from_attr(ma, &tun_mask);
1483 size = sizeof tun_mask;
1485 mask = nl_attr_get(ma);
1486 size = nl_attr_get_size(ma);
1489 return odp_mask_is_exact(attr, mask, size);
1493 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1496 struct odp_portno_names *odp_portno_names;
1498 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1499 odp_portno_names->port_no = port_no;
1500 odp_portno_names->name = xstrdup(port_name);
1501 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1502 hash_odp_port(port_no));
1506 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1508 struct odp_portno_names *odp_portno_names;
1510 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1511 hash_odp_port(port_no), portno_names) {
1512 if (odp_portno_names->port_no == port_no) {
1513 return odp_portno_names->name;
1520 odp_portno_names_destroy(struct hmap *portno_names)
1522 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1523 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1524 hmap_node, portno_names) {
1525 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1526 free(odp_portno_names->name);
1527 free(odp_portno_names);
1531 /* Format helpers. */
1534 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1535 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1537 bool mask_empty = mask && eth_addr_is_zero(*mask);
1539 if (verbose || !mask_empty) {
1540 bool mask_full = !mask || eth_mask_is_exact(*mask);
1543 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1545 ds_put_format(ds, "%s=", name);
1546 eth_format_masked(key, *mask, ds);
1547 ds_put_char(ds, ',');
1553 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1554 const ovs_be64 *mask, bool verbose)
1556 bool mask_empty = mask && !*mask;
1558 if (verbose || !mask_empty) {
1559 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1561 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1562 if (!mask_full) { /* Partially masked. */
1563 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1565 ds_put_char(ds, ',');
1570 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1571 const ovs_be32 *mask, bool verbose)
1573 bool mask_empty = mask && !*mask;
1575 if (verbose || !mask_empty) {
1576 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1578 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1579 if (!mask_full) { /* Partially masked. */
1580 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1582 ds_put_char(ds, ',');
1587 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1588 const ovs_be32 (*mask_)[4], bool verbose)
1590 char buf[INET6_ADDRSTRLEN];
1591 const struct in6_addr *key = (const struct in6_addr *)key_;
1592 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1594 bool mask_empty = mask && ipv6_mask_is_any(mask);
1596 if (verbose || !mask_empty) {
1597 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1599 inet_ntop(AF_INET6, key, buf, sizeof buf);
1600 ds_put_format(ds, "%s=%s", name, buf);
1601 if (!mask_full) { /* Partially masked. */
1602 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1603 ds_put_format(ds, "/%s", buf);
1605 ds_put_char(ds, ',');
1610 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1611 const ovs_be32 *mask, bool verbose)
1613 bool mask_empty = mask && !*mask;
1615 if (verbose || !mask_empty) {
1616 bool mask_full = !mask
1617 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1619 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1620 if (!mask_full) { /* Partially masked. */
1621 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1623 ds_put_char(ds, ',');
1628 format_u8x(struct ds *ds, const char *name, uint8_t key,
1629 const uint8_t *mask, bool verbose)
1631 bool mask_empty = mask && !*mask;
1633 if (verbose || !mask_empty) {
1634 bool mask_full = !mask || *mask == UINT8_MAX;
1636 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1637 if (!mask_full) { /* Partially masked. */
1638 ds_put_format(ds, "/%#"PRIx8, *mask);
1640 ds_put_char(ds, ',');
1645 format_u8u(struct ds *ds, const char *name, uint8_t key,
1646 const uint8_t *mask, bool verbose)
1648 bool mask_empty = mask && !*mask;
1650 if (verbose || !mask_empty) {
1651 bool mask_full = !mask || *mask == UINT8_MAX;
1653 ds_put_format(ds, "%s=%"PRIu8, name, key);
1654 if (!mask_full) { /* Partially masked. */
1655 ds_put_format(ds, "/%#"PRIx8, *mask);
1657 ds_put_char(ds, ',');
1662 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1663 const ovs_be16 *mask, bool verbose)
1665 bool mask_empty = mask && !*mask;
1667 if (verbose || !mask_empty) {
1668 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1670 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1671 if (!mask_full) { /* Partially masked. */
1672 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1674 ds_put_char(ds, ',');
1679 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1680 const uint16_t *mask, bool verbose)
1682 bool mask_empty = mask && !*mask;
1684 if (verbose || !mask_empty) {
1685 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1687 ds_put_cstr(ds, name);
1688 ds_put_char(ds, '(');
1689 if (!mask_full) { /* Partially masked. */
1690 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1691 } else { /* Fully masked. */
1692 format_flags(ds, flow_tun_flag_to_string, key, ',');
1694 ds_put_cstr(ds, "),");
1699 format_frag(struct ds *ds, const char *name, uint8_t key,
1700 const uint8_t *mask, bool verbose)
1702 bool mask_empty = mask && !*mask;
1704 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1705 if (verbose || !mask_empty) {
1706 bool mask_full = !mask || *mask == UINT8_MAX;
1708 if (!mask_full) { /* Partially masked. */
1709 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1712 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1717 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1720 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1721 const struct hmap *portno_names, struct ds *ds,
1724 enum ovs_key_attr attr = nl_attr_type(a);
1725 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1729 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1731 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1734 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1735 if (expected_len != -2) {
1736 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1737 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1739 if (bad_key_len || bad_mask_len) {
1741 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1742 nl_attr_get_size(a), expected_len);
1744 format_generic_odp_key(a, ds);
1746 ds_put_char(ds, '/');
1748 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1749 nl_attr_get_size(ma), expected_len);
1751 format_generic_odp_key(ma, ds);
1753 ds_put_char(ds, ')');
1759 ds_put_char(ds, '(');
1761 case OVS_KEY_ATTR_ENCAP:
1762 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1763 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1764 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1766 } else if (nl_attr_get_size(a)) {
1767 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1772 case OVS_KEY_ATTR_PRIORITY:
1773 case OVS_KEY_ATTR_SKB_MARK:
1774 case OVS_KEY_ATTR_DP_HASH:
1775 case OVS_KEY_ATTR_RECIRC_ID:
1776 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1778 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1782 case OVS_KEY_ATTR_TUNNEL: {
1783 struct flow_tnl key, mask_;
1784 struct flow_tnl *mask = ma ? &mask_ : NULL;
1787 memset(mask, 0, sizeof *mask);
1788 odp_tun_key_from_attr(ma, mask);
1790 memset(&key, 0, sizeof key);
1791 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1792 ds_put_format(ds, "error");
1795 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1796 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1797 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1798 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1799 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1800 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1801 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1802 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1806 case OVS_KEY_ATTR_IN_PORT:
1807 if (portno_names && verbose && is_exact) {
1808 char *name = odp_portno_names_get(portno_names,
1809 u32_to_odp(nl_attr_get_u32(a)));
1811 ds_put_format(ds, "%s", name);
1813 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1816 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1818 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1823 case OVS_KEY_ATTR_ETHERNET: {
1824 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1825 const struct ovs_key_ethernet *key = nl_attr_get(a);
1827 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1828 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1832 case OVS_KEY_ATTR_VLAN:
1833 format_vlan_tci(ds, nl_attr_get_be16(a),
1834 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1837 case OVS_KEY_ATTR_MPLS: {
1838 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1839 const struct ovs_key_mpls *mpls_mask = NULL;
1840 size_t size = nl_attr_get_size(a);
1842 if (!size || size % sizeof *mpls_key) {
1843 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1847 mpls_mask = nl_attr_get(ma);
1848 if (size != nl_attr_get_size(ma)) {
1849 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1850 "mask length %"PRIuSIZE")",
1851 size, nl_attr_get_size(ma));
1855 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1858 case OVS_KEY_ATTR_ETHERTYPE:
1859 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1861 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1865 case OVS_KEY_ATTR_IPV4: {
1866 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1867 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1869 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1870 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1871 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1873 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1874 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1875 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1880 case OVS_KEY_ATTR_IPV6: {
1881 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1882 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1884 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1885 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1886 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1888 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1890 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1892 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1894 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1899 /* These have the same structure and format. */
1900 case OVS_KEY_ATTR_TCP:
1901 case OVS_KEY_ATTR_UDP:
1902 case OVS_KEY_ATTR_SCTP: {
1903 const struct ovs_key_tcp *key = nl_attr_get(a);
1904 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1906 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1907 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1911 case OVS_KEY_ATTR_TCP_FLAGS:
1913 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1914 ntohs(nl_attr_get_be16(a)),
1915 ntohs(nl_attr_get_be16(ma)));
1917 format_flags(ds, packet_tcp_flag_to_string,
1918 ntohs(nl_attr_get_be16(a)), ',');
1922 case OVS_KEY_ATTR_ICMP: {
1923 const struct ovs_key_icmp *key = nl_attr_get(a);
1924 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1926 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1927 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1931 case OVS_KEY_ATTR_ICMPV6: {
1932 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
1933 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
1935 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
1937 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
1942 case OVS_KEY_ATTR_ARP: {
1943 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
1944 const struct ovs_key_arp *key = nl_attr_get(a);
1946 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
1947 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
1948 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
1949 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
1950 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
1954 case OVS_KEY_ATTR_ND: {
1955 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
1956 const struct ovs_key_nd *key = nl_attr_get(a);
1958 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
1960 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
1961 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
1966 case OVS_KEY_ATTR_UNSPEC:
1967 case __OVS_KEY_ATTR_MAX:
1969 format_generic_odp_key(a, ds);
1971 ds_put_char(ds, '/');
1972 format_generic_odp_key(ma, ds);
1976 ds_put_char(ds, ')');
1979 static struct nlattr *
1980 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1982 const struct nlattr *a;
1984 int type = nl_attr_type(key);
1985 int size = nl_attr_get_size(key);
1987 if (odp_flow_key_attr_len(type) >=0) {
1988 nl_msg_put_unspec_zero(ofp, type, size);
1992 nested_mask = nl_msg_start_nested(ofp, type);
1993 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1994 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1996 nl_msg_end_nested(ofp, nested_mask);
1999 return ofpbuf_base(ofp);
2002 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2003 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2004 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2005 * non-null and 'verbose' is true, translates odp port number to its name. */
2007 odp_flow_format(const struct nlattr *key, size_t key_len,
2008 const struct nlattr *mask, size_t mask_len,
2009 const struct hmap *portno_names, struct ds *ds, bool verbose)
2012 const struct nlattr *a;
2014 bool has_ethtype_key = false;
2015 const struct nlattr *ma = NULL;
2017 bool first_field = true;
2019 ofpbuf_init(&ofp, 100);
2020 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2021 bool is_nested_attr;
2022 bool is_wildcard = false;
2023 int attr_type = nl_attr_type(a);
2025 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2026 has_ethtype_key = true;
2029 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
2031 if (mask && mask_len) {
2032 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2033 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2036 if (verbose || !is_wildcard || is_nested_attr) {
2037 if (is_wildcard && !ma) {
2038 ma = generate_all_wildcard_mask(&ofp, a);
2041 ds_put_char(ds, ',');
2043 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2044 first_field = false;
2048 ofpbuf_uninit(&ofp);
2053 if (left == key_len) {
2054 ds_put_cstr(ds, "<empty>");
2056 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2057 for (i = 0; i < left; i++) {
2058 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2060 ds_put_char(ds, ')');
2062 if (!has_ethtype_key) {
2063 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2065 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2066 ntohs(nl_attr_get_be16(ma)));
2070 ds_put_cstr(ds, "<empty>");
2074 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2075 * OVS_KEY_ATTR_* attributes in 'key'. */
2077 odp_flow_key_format(const struct nlattr *key,
2078 size_t key_len, struct ds *ds)
2080 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2084 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2086 if (!strcasecmp(s, "no")) {
2087 *type = OVS_FRAG_TYPE_NONE;
2088 } else if (!strcasecmp(s, "first")) {
2089 *type = OVS_FRAG_TYPE_FIRST;
2090 } else if (!strcasecmp(s, "later")) {
2091 *type = OVS_FRAG_TYPE_LATER;
2101 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2102 uint8_t (*mask)[ETH_ADDR_LEN])
2106 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2110 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2111 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2114 memset(mask, 0xff, sizeof *mask);
2123 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2127 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2131 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2132 IP_SCAN_ARGS(mask), &n)) {
2135 *mask = OVS_BE32_MAX;
2144 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2147 char ipv6_s[IPV6_SCAN_LEN + 1];
2149 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2150 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2154 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2155 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2158 memset(mask, 0xff, sizeof *mask);
2167 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2172 if (ovs_scan(s, "%i%n", &key_, &n)
2173 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2178 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2179 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2181 *mask = htonl(mask_);
2183 *mask = htonl(IPV6_LABEL_MASK);
2192 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2196 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2200 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2212 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2216 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2220 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2232 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2234 uint16_t key_, mask_;
2237 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2242 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2244 *mask = htons(mask_);
2246 *mask = OVS_BE16_MAX;
2255 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2257 uint64_t key_, mask_;
2260 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2263 *key = htonll(key_);
2265 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2267 *mask = htonll(mask_);
2269 *mask = OVS_BE64_MAX;
2278 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2280 uint32_t flags, fmask;
2283 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2284 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2285 if (n >= 0 && s[n] == ')') {
2296 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2298 uint32_t flags, fmask;
2301 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2302 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2304 *key = htons(flags);
2306 *mask = htons(fmask);
2314 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2318 enum ovs_frag_type frag_type;
2320 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2321 && ovs_frag_type_from_string(frag, &frag_type)) {
2334 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2335 const struct simap *port_names)
2339 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2343 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2350 } else if (port_names) {
2351 const struct simap_node *node;
2354 len = strcspn(s, ")");
2355 node = simap_find_len(port_names, s, len);
2368 /* Helper for vlan parsing. */
2369 struct ovs_key_vlan__ {
2374 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2376 const uint16_t mask = ((1U << bits) - 1) << offset;
2378 if (value >> bits) {
2382 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2387 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2390 uint16_t key_, mask_;
2393 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2396 if (set_be16_bf(key, bits, offset, key_)) {
2398 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2401 if (!set_be16_bf(mask, bits, offset, mask_)) {
2405 *mask |= htons(((1U << bits) - 1) << offset);
2415 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2417 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2421 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2423 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2427 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2429 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2434 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2436 const uint32_t mask = ((1U << bits) - 1) << offset;
2438 if (value >> bits) {
2442 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2447 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2450 uint32_t key_, mask_;
2453 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2456 if (set_be32_bf(key, bits, offset, key_)) {
2458 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2461 if (!set_be32_bf(mask, bits, offset, mask_)) {
2465 *mask |= htonl(((1U << bits) - 1) << offset);
2475 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2477 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2481 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2483 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2487 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2489 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2493 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2495 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2498 /* ATTR is compile-time constant, so only the case with correct data type
2499 * will be used. However, the compiler complains about the data type for
2500 * the other cases, so we must cast to make the compiler silent. */
2501 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2502 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2503 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2505 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2508 #define SCAN_IF(NAME) \
2509 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2510 const char *start = s; \
2515 /* Usually no special initialization is needed. */
2516 #define SCAN_BEGIN(NAME, TYPE) \
2519 memset(&skey, 0, sizeof skey); \
2520 memset(&smask, 0, sizeof smask); \
2524 /* VLAN needs special initialization. */
2525 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2527 TYPE skey = KEY_INIT; \
2528 TYPE smask = MASK_INIT; \
2532 /* Scan unnamed entry as 'TYPE' */
2533 #define SCAN_TYPE(TYPE, KEY, MASK) \
2534 len = scan_##TYPE(s, KEY, MASK); \
2540 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2541 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2542 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2543 s += strlen(NAME); \
2544 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2548 #define SCAN_FINISH() \
2549 } while (*s++ == ',' && len != 0); \
2550 if (s[-1] != ')') { \
2554 #define SCAN_FINISH_SINGLE() \
2556 if (*s++ != ')') { \
2560 #define SCAN_PUT(ATTR) \
2561 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2562 SCAN_PUT_ATTR(key, ATTR, skey); \
2564 SCAN_PUT_ATTR(mask, ATTR, smask); \
2568 #define SCAN_END(ATTR) \
2574 #define SCAN_END_SINGLE(ATTR) \
2575 SCAN_FINISH_SINGLE(); \
2580 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2581 SCAN_BEGIN(NAME, TYPE) { \
2582 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2583 } SCAN_END_SINGLE(ATTR)
2585 #define SCAN_SINGLE_NO_MASK(NAME, TYPE, SCAN_AS, ATTR) \
2586 SCAN_BEGIN(NAME, TYPE) { \
2587 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2588 } SCAN_END_SINGLE(ATTR)
2590 /* scan_port needs one extra argument. */
2591 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2592 SCAN_BEGIN(NAME, TYPE) { \
2593 len = scan_port(s, &skey, &smask, port_names); \
2598 } SCAN_END_SINGLE(ATTR)
2601 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2602 struct ofpbuf *key, struct ofpbuf *mask)
2604 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2605 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2606 SCAN_SINGLE_NO_MASK("recirc_id(", uint32_t, u32, OVS_KEY_ATTR_RECIRC_ID);
2607 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2609 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2610 SCAN_FIELD("tun_id=", be64, tun_id);
2611 SCAN_FIELD("src=", ipv4, ip_src);
2612 SCAN_FIELD("dst=", ipv4, ip_dst);
2613 SCAN_FIELD("tos=", u8, ip_tos);
2614 SCAN_FIELD("ttl=", u8, ip_ttl);
2615 SCAN_FIELD("tp_src=", be16, tp_src);
2616 SCAN_FIELD("tp_dst=", be16, tp_dst);
2617 SCAN_FIELD("flags(", tun_flags, flags);
2618 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2620 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2622 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2623 SCAN_FIELD("src=", eth, eth_src);
2624 SCAN_FIELD("dst=", eth, eth_dst);
2625 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2627 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2628 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2629 SCAN_FIELD("vid=", vid, tci);
2630 SCAN_FIELD("pcp=", pcp, tci);
2631 SCAN_FIELD("cfi=", cfi, tci);
2632 } SCAN_END(OVS_KEY_ATTR_VLAN);
2634 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2636 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2637 SCAN_FIELD("label=", mpls_label, mpls_lse);
2638 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2639 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2640 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2641 } SCAN_END(OVS_KEY_ATTR_MPLS);
2643 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2644 SCAN_FIELD("src=", ipv4, ipv4_src);
2645 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2646 SCAN_FIELD("proto=", u8, ipv4_proto);
2647 SCAN_FIELD("tos=", u8, ipv4_tos);
2648 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2649 SCAN_FIELD("frag=", frag, ipv4_frag);
2650 } SCAN_END(OVS_KEY_ATTR_IPV4);
2652 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2653 SCAN_FIELD("src=", ipv6, ipv6_src);
2654 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2655 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2656 SCAN_FIELD("proto=", u8, ipv6_proto);
2657 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2658 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2659 SCAN_FIELD("frag=", frag, ipv6_frag);
2660 } SCAN_END(OVS_KEY_ATTR_IPV6);
2662 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2663 SCAN_FIELD("src=", be16, tcp_src);
2664 SCAN_FIELD("dst=", be16, tcp_dst);
2665 } SCAN_END(OVS_KEY_ATTR_TCP);
2667 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2669 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2670 SCAN_FIELD("src=", be16, udp_src);
2671 SCAN_FIELD("dst=", be16, udp_dst);
2672 } SCAN_END(OVS_KEY_ATTR_UDP);
2674 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2675 SCAN_FIELD("src=", be16, sctp_src);
2676 SCAN_FIELD("dst=", be16, sctp_dst);
2677 } SCAN_END(OVS_KEY_ATTR_SCTP);
2679 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2680 SCAN_FIELD("type=", u8, icmp_type);
2681 SCAN_FIELD("code=", u8, icmp_code);
2682 } SCAN_END(OVS_KEY_ATTR_ICMP);
2684 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2685 SCAN_FIELD("type=", u8, icmpv6_type);
2686 SCAN_FIELD("code=", u8, icmpv6_code);
2687 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2689 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2690 SCAN_FIELD("sip=", ipv4, arp_sip);
2691 SCAN_FIELD("tip=", ipv4, arp_tip);
2692 SCAN_FIELD("op=", be16, arp_op);
2693 SCAN_FIELD("sha=", eth, arp_sha);
2694 SCAN_FIELD("tha=", eth, arp_tha);
2695 } SCAN_END(OVS_KEY_ATTR_ARP);
2697 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2698 SCAN_FIELD("target=", ipv6, nd_target);
2699 SCAN_FIELD("sll=", eth, nd_sll);
2700 SCAN_FIELD("tll=", eth, nd_tll);
2701 } SCAN_END(OVS_KEY_ATTR_ND);
2703 /* Encap open-coded. */
2704 if (!strncmp(s, "encap(", 6)) {
2705 const char *start = s;
2706 size_t encap, encap_mask = 0;
2708 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2710 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2717 s += strspn(s, ", \t\r\n");
2720 } else if (*s == ')') {
2724 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2732 nl_msg_end_nested(key, encap);
2734 nl_msg_end_nested(mask, encap_mask);
2743 /* Parses the string representation of a datapath flow key, in the
2744 * format output by odp_flow_key_format(). Returns 0 if successful,
2745 * otherwise a positive errno value. On success, the flow key is
2746 * appended to 'key' as a series of Netlink attributes. On failure, no
2747 * data is appended to 'key'. Either way, 'key''s data might be
2750 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2751 * to a port number. (Port names may be used instead of port numbers in
2754 * On success, the attributes appended to 'key' are individually syntactically
2755 * valid, but they may not be valid as a sequence. 'key' might, for example,
2756 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2758 odp_flow_from_string(const char *s, const struct simap *port_names,
2759 struct ofpbuf *key, struct ofpbuf *mask)
2761 const size_t old_size = ofpbuf_size(key);
2765 s += strspn(s, delimiters);
2770 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2772 ofpbuf_set_size(key, old_size);
2782 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
2785 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2786 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2787 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2788 * must use a zero mask for the netlink frag field, and all ones mask
2790 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
2792 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
2793 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
2794 : OVS_FRAG_TYPE_FIRST;
2797 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
2798 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
2799 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
2801 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
2803 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
2805 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
2807 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
2808 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
2810 /* These share the same layout. */
2812 struct ovs_key_tcp tcp;
2813 struct ovs_key_udp udp;
2814 struct ovs_key_sctp sctp;
2817 static void get_tp_key(const struct flow *, union ovs_key_tp *);
2818 static void put_tp_key(const union ovs_key_tp *, struct flow *);
2821 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2822 const struct flow *mask, odp_port_t odp_in_port,
2823 size_t max_mpls_depth, bool recirc, bool export_mask)
2825 struct ovs_key_ethernet *eth_key;
2827 const struct flow *data = export_mask ? mask : flow;
2829 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2831 if (flow->tunnel.ip_dst || export_mask) {
2832 tun_key_to_attr(buf, &data->tunnel);
2835 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2838 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2839 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2842 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2843 * is not the magical value "ODPP_NONE". */
2844 if (export_mask || odp_in_port != ODPP_NONE) {
2845 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2848 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2850 get_ethernet_key(data, eth_key);
2852 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2854 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2856 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2858 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2859 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2860 if (flow->vlan_tci == htons(0)) {
2867 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2868 /* For backwards compatibility with kernels that don't support
2869 * wildcarding, the following convention is used to encode the
2870 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2873 * -------- -------- -------
2874 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2875 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2876 * <none> 0xffff Any non-Ethernet II frame (except valid
2877 * 802.3 SNAP packet with valid eth_type).
2880 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2885 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2887 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2888 struct ovs_key_ipv4 *ipv4_key;
2890 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2892 get_ipv4_key(data, ipv4_key, export_mask);
2893 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2894 struct ovs_key_ipv6 *ipv6_key;
2896 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2898 get_ipv6_key(data, ipv6_key, export_mask);
2899 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2900 flow->dl_type == htons(ETH_TYPE_RARP)) {
2901 struct ovs_key_arp *arp_key;
2903 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2905 get_arp_key(data, arp_key);
2906 } else if (eth_type_mpls(flow->dl_type)) {
2907 struct ovs_key_mpls *mpls_key;
2910 n = flow_count_mpls_labels(flow, NULL);
2911 n = MIN(n, max_mpls_depth);
2912 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2913 n * sizeof *mpls_key);
2914 for (i = 0; i < n; i++) {
2915 mpls_key[i].mpls_lse = data->mpls_lse[i];
2919 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2920 if (flow->nw_proto == IPPROTO_TCP) {
2921 union ovs_key_tp *tcp_key;
2923 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2925 get_tp_key(data, tcp_key);
2926 if (data->tcp_flags) {
2927 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2929 } else if (flow->nw_proto == IPPROTO_UDP) {
2930 union ovs_key_tp *udp_key;
2932 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2934 get_tp_key(data, udp_key);
2935 } else if (flow->nw_proto == IPPROTO_SCTP) {
2936 union ovs_key_tp *sctp_key;
2938 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2940 get_tp_key(data, sctp_key);
2941 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2942 && flow->nw_proto == IPPROTO_ICMP) {
2943 struct ovs_key_icmp *icmp_key;
2945 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2947 icmp_key->icmp_type = ntohs(data->tp_src);
2948 icmp_key->icmp_code = ntohs(data->tp_dst);
2949 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2950 && flow->nw_proto == IPPROTO_ICMPV6) {
2951 struct ovs_key_icmpv6 *icmpv6_key;
2953 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2954 sizeof *icmpv6_key);
2955 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2956 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2958 if (flow->tp_dst == htons(0)
2959 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
2960 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
2961 && (!export_mask || (data->tp_src == htons(0xffff)
2962 && data->tp_dst == htons(0xffff)))) {
2964 struct ovs_key_nd *nd_key;
2966 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2968 memcpy(nd_key->nd_target, &data->nd_target,
2969 sizeof nd_key->nd_target);
2970 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2971 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2978 nl_msg_end_nested(buf, encap);
2982 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2983 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2984 * number rather than a datapath port number). Instead, if 'odp_in_port'
2985 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2988 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2989 * capable of being expanded to allow for that much space.
2991 * 'recirc' indicates support for recirculation fields. If this is true, then
2992 * these fields will always be serialised. */
2994 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2995 const struct flow *mask, odp_port_t odp_in_port,
2998 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3002 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3003 * 'buf'. 'flow' is used as a template to determine how to interpret
3004 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3005 * it doesn't indicate whether the other fields should be interpreted as
3006 * ARP, IPv4, IPv6, etc.
3008 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3009 * capable of being expanded to allow for that much space.
3011 * 'recirc' indicates support for recirculation fields. If this is true, then
3012 * these fields will always be serialised. */
3014 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
3015 const struct flow *flow, uint32_t odp_in_port_mask,
3016 size_t max_mpls_depth, bool recirc)
3018 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3019 max_mpls_depth, recirc, true);
3022 /* Generate ODP flow key from the given packet metadata */
3024 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3026 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3028 if (md->tunnel.ip_dst) {
3029 tun_key_to_attr(buf, &md->tunnel);
3032 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3034 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3035 * value "ODPP_NONE". */
3036 if (md->in_port.odp_port != ODPP_NONE) {
3037 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3041 /* Generate packet metadata from the given ODP flow key. */
3043 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3044 struct pkt_metadata *md)
3046 const struct nlattr *nla;
3048 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3049 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3050 1u << OVS_KEY_ATTR_IN_PORT;
3052 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3054 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3055 uint16_t type = nl_attr_type(nla);
3056 size_t len = nl_attr_get_size(nla);
3057 int expected_len = odp_flow_key_attr_len(type);
3059 if (len != expected_len && expected_len >= 0) {
3064 case OVS_KEY_ATTR_RECIRC_ID:
3065 md->recirc_id = nl_attr_get_u32(nla);
3066 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3068 case OVS_KEY_ATTR_DP_HASH:
3069 md->dp_hash = nl_attr_get_u32(nla);
3070 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3072 case OVS_KEY_ATTR_PRIORITY:
3073 md->skb_priority = nl_attr_get_u32(nla);
3074 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3076 case OVS_KEY_ATTR_SKB_MARK:
3077 md->pkt_mark = nl_attr_get_u32(nla);
3078 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3080 case OVS_KEY_ATTR_TUNNEL: {
3081 enum odp_key_fitness res;
3083 res = odp_tun_key_from_attr(nla, &md->tunnel);
3084 if (res == ODP_FIT_ERROR) {
3085 memset(&md->tunnel, 0, sizeof md->tunnel);
3086 } else if (res == ODP_FIT_PERFECT) {
3087 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3091 case OVS_KEY_ATTR_IN_PORT:
3092 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3093 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3099 if (!wanted_attrs) {
3100 return; /* Have everything. */
3106 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3108 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3109 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3110 key_len / sizeof(uint32_t), 0);
3114 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3115 uint64_t attrs, int out_of_range_attr,
3116 const struct nlattr *key, size_t key_len)
3121 if (VLOG_DROP_DBG(rl)) {
3126 for (i = 0; i < 64; i++) {
3127 if (attrs & (UINT64_C(1) << i)) {
3128 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3130 ds_put_format(&s, " %s",
3131 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3134 if (out_of_range_attr) {
3135 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3138 ds_put_cstr(&s, ": ");
3139 odp_flow_key_format(key, key_len, &s);
3141 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3146 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3148 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3151 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3154 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3155 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3156 return 0xff; /* Error. */
3159 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3160 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3161 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3165 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3166 const struct nlattr *attrs[], uint64_t *present_attrsp,
3167 int *out_of_range_attrp)
3169 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3170 const struct nlattr *nla;
3171 uint64_t present_attrs;
3174 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3176 *out_of_range_attrp = 0;
3177 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3178 uint16_t type = nl_attr_type(nla);
3179 size_t len = nl_attr_get_size(nla);
3180 int expected_len = odp_flow_key_attr_len(type);
3182 if (len != expected_len && expected_len >= 0) {
3183 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3185 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3186 "length %d", ovs_key_attr_to_string(type, namebuf,
3192 if (type > OVS_KEY_ATTR_MAX) {
3193 *out_of_range_attrp = type;
3195 if (present_attrs & (UINT64_C(1) << type)) {
3196 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3198 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3199 ovs_key_attr_to_string(type,
3200 namebuf, sizeof namebuf));
3204 present_attrs |= UINT64_C(1) << type;
3209 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3213 *present_attrsp = present_attrs;
3217 static enum odp_key_fitness
3218 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3219 uint64_t expected_attrs,
3220 const struct nlattr *key, size_t key_len)
3222 uint64_t missing_attrs;
3223 uint64_t extra_attrs;
3225 missing_attrs = expected_attrs & ~present_attrs;
3226 if (missing_attrs) {
3227 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3228 log_odp_key_attributes(&rl, "expected but not present",
3229 missing_attrs, 0, key, key_len);
3230 return ODP_FIT_TOO_LITTLE;
3233 extra_attrs = present_attrs & ~expected_attrs;
3234 if (extra_attrs || out_of_range_attr) {
3235 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3236 log_odp_key_attributes(&rl, "present but not expected",
3237 extra_attrs, out_of_range_attr, key, key_len);
3238 return ODP_FIT_TOO_MUCH;
3241 return ODP_FIT_PERFECT;
3245 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3246 uint64_t present_attrs, uint64_t *expected_attrs,
3247 struct flow *flow, const struct flow *src_flow)
3249 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3250 bool is_mask = flow != src_flow;
3252 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3253 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3254 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3255 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3256 ntohs(flow->dl_type));
3259 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3260 flow->dl_type != htons(0xffff)) {
3263 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3266 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3267 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3268 /* See comments in odp_flow_key_from_flow__(). */
3269 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3276 static enum odp_key_fitness
3277 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3278 uint64_t present_attrs, int out_of_range_attr,
3279 uint64_t expected_attrs, struct flow *flow,
3280 const struct nlattr *key, size_t key_len,
3281 const struct flow *src_flow)
3283 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3284 bool is_mask = src_flow != flow;
3285 const void *check_start = NULL;
3286 size_t check_len = 0;
3287 enum ovs_key_attr expected_bit = 0xff;
3289 if (eth_type_mpls(src_flow->dl_type)) {
3290 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3291 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3293 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3294 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3295 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3296 int n = size / sizeof(ovs_be32);
3299 if (!size || size % sizeof(ovs_be32)) {
3300 return ODP_FIT_ERROR;
3302 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3303 return ODP_FIT_ERROR;
3306 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3307 flow->mpls_lse[i] = mpls_lse[i];
3309 if (n > FLOW_MAX_MPLS_LABELS) {
3310 return ODP_FIT_TOO_MUCH;
3314 /* BOS may be set only in the innermost label. */
3315 for (i = 0; i < n - 1; i++) {
3316 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3317 return ODP_FIT_ERROR;
3321 /* BOS must be set in the innermost label. */
3322 if (n < FLOW_MAX_MPLS_LABELS
3323 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3324 return ODP_FIT_TOO_LITTLE;
3330 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3332 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3334 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3335 const struct ovs_key_ipv4 *ipv4_key;
3337 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3338 put_ipv4_key(ipv4_key, flow, is_mask);
3339 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3340 return ODP_FIT_ERROR;
3343 check_start = ipv4_key;
3344 check_len = sizeof *ipv4_key;
3345 expected_bit = OVS_KEY_ATTR_IPV4;
3348 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3350 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3352 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3353 const struct ovs_key_ipv6 *ipv6_key;
3355 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3356 put_ipv6_key(ipv6_key, flow, is_mask);
3357 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3358 return ODP_FIT_ERROR;
3361 check_start = ipv6_key;
3362 check_len = sizeof *ipv6_key;
3363 expected_bit = OVS_KEY_ATTR_IPV6;
3366 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3367 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3369 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3371 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3372 const struct ovs_key_arp *arp_key;
3374 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3375 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3376 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3377 "key", ntohs(arp_key->arp_op));
3378 return ODP_FIT_ERROR;
3380 put_arp_key(arp_key, flow);
3382 check_start = arp_key;
3383 check_len = sizeof *arp_key;
3384 expected_bit = OVS_KEY_ATTR_ARP;
3390 if (check_len > 0) { /* Happens only when 'is_mask'. */
3391 if (!is_all_zeros(check_start, check_len) &&
3392 flow->dl_type != htons(0xffff)) {
3393 return ODP_FIT_ERROR;
3395 expected_attrs |= UINT64_C(1) << expected_bit;
3399 expected_bit = OVS_KEY_ATTR_UNSPEC;
3400 if (src_flow->nw_proto == IPPROTO_TCP
3401 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3402 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3403 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3405 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3407 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3408 const union ovs_key_tp *tcp_key;
3410 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3411 put_tp_key(tcp_key, flow);
3412 expected_bit = OVS_KEY_ATTR_TCP;
3414 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3415 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3416 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3418 } else if (src_flow->nw_proto == IPPROTO_UDP
3419 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3420 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3421 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3423 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3425 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3426 const union ovs_key_tp *udp_key;
3428 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3429 put_tp_key(udp_key, flow);
3430 expected_bit = OVS_KEY_ATTR_UDP;
3432 } else if (src_flow->nw_proto == IPPROTO_SCTP
3433 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3434 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3435 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3437 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3439 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3440 const union ovs_key_tp *sctp_key;
3442 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3443 put_tp_key(sctp_key, flow);
3444 expected_bit = OVS_KEY_ATTR_SCTP;
3446 } else if (src_flow->nw_proto == IPPROTO_ICMP
3447 && src_flow->dl_type == htons(ETH_TYPE_IP)
3448 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3450 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3452 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3453 const struct ovs_key_icmp *icmp_key;
3455 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3456 flow->tp_src = htons(icmp_key->icmp_type);
3457 flow->tp_dst = htons(icmp_key->icmp_code);
3458 expected_bit = OVS_KEY_ATTR_ICMP;
3460 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3461 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3462 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3464 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3466 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3467 const struct ovs_key_icmpv6 *icmpv6_key;
3469 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3470 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3471 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3472 expected_bit = OVS_KEY_ATTR_ICMPV6;
3473 if (src_flow->tp_dst == htons(0) &&
3474 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3475 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3477 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3479 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3480 const struct ovs_key_nd *nd_key;
3482 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3483 memcpy(&flow->nd_target, nd_key->nd_target,
3484 sizeof flow->nd_target);
3485 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3486 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3488 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3489 (flow->tp_src != htons(0xffff) ||
3490 flow->tp_dst != htons(0xffff))) {
3491 return ODP_FIT_ERROR;
3493 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3500 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3501 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3502 return ODP_FIT_ERROR;
3504 expected_attrs |= UINT64_C(1) << expected_bit;
3509 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3513 /* Parse 802.1Q header then encapsulated L3 attributes. */
3514 static enum odp_key_fitness
3515 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3516 uint64_t present_attrs, int out_of_range_attr,
3517 uint64_t expected_attrs, struct flow *flow,
3518 const struct nlattr *key, size_t key_len,
3519 const struct flow *src_flow)
3521 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3522 bool is_mask = src_flow != flow;
3524 const struct nlattr *encap
3525 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3526 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3527 enum odp_key_fitness encap_fitness;
3528 enum odp_key_fitness fitness;
3530 /* Calculate fitness of outer attributes. */
3532 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3533 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3535 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3536 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3538 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3539 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3542 fitness = check_expectations(present_attrs, out_of_range_attr,
3543 expected_attrs, key, key_len);
3546 * Remove the TPID from dl_type since it's not the real Ethertype. */
3547 flow->dl_type = htons(0);
3548 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3549 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3552 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3553 return ODP_FIT_TOO_LITTLE;
3554 } else if (flow->vlan_tci == htons(0)) {
3555 /* Corner case for a truncated 802.1Q header. */
3556 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3557 return ODP_FIT_TOO_MUCH;
3560 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3561 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3562 "but CFI bit is not set", ntohs(flow->vlan_tci));
3563 return ODP_FIT_ERROR;
3566 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3571 /* Now parse the encapsulated attributes. */
3572 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3573 attrs, &present_attrs, &out_of_range_attr)) {
3574 return ODP_FIT_ERROR;
3578 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3579 return ODP_FIT_ERROR;
3581 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3582 expected_attrs, flow, key, key_len,
3585 /* The overall fitness is the worse of the outer and inner attributes. */
3586 return MAX(fitness, encap_fitness);
3589 static enum odp_key_fitness
3590 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3591 struct flow *flow, const struct flow *src_flow)
3593 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3594 uint64_t expected_attrs;
3595 uint64_t present_attrs;
3596 int out_of_range_attr;
3597 bool is_mask = src_flow != flow;
3599 memset(flow, 0, sizeof *flow);
3601 /* Parse attributes. */
3602 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3603 &out_of_range_attr)) {
3604 return ODP_FIT_ERROR;
3609 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3610 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3611 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3612 } else if (is_mask) {
3613 /* Always exact match recirc_id if it is not specified. */
3614 flow->recirc_id = UINT32_MAX;
3617 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3618 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3619 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3621 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3622 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3623 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3626 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3627 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3628 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3631 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3632 enum odp_key_fitness res;
3634 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3635 if (res == ODP_FIT_ERROR) {
3636 return ODP_FIT_ERROR;
3637 } else if (res == ODP_FIT_PERFECT) {
3638 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3642 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3643 flow->in_port.odp_port
3644 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3645 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3646 } else if (!is_mask) {
3647 flow->in_port.odp_port = ODPP_NONE;
3650 /* Ethernet header. */
3651 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3652 const struct ovs_key_ethernet *eth_key;
3654 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3655 put_ethernet_key(eth_key, flow);
3657 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3661 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3664 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3665 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3667 return ODP_FIT_ERROR;
3671 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3672 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3673 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3674 expected_attrs, flow, key, key_len, src_flow);
3677 flow->vlan_tci = htons(0xffff);
3678 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3679 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3680 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3683 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3684 expected_attrs, flow, key, key_len, src_flow);
3687 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3688 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3689 * 'key' fits our expectations for what a flow key should contain.
3691 * The 'in_port' will be the datapath's understanding of the port. The
3692 * caller will need to translate with odp_port_to_ofp_port() if the
3693 * OpenFlow port is needed.
3695 * This function doesn't take the packet itself as an argument because none of
3696 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3697 * it is always possible to infer which additional attribute(s) should appear
3698 * by looking at the attributes for lower-level protocols, e.g. if the network
3699 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3700 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3701 * must be absent. */
3702 enum odp_key_fitness
3703 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3706 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3709 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3710 * structure in 'mask'. 'flow' must be a previously translated flow
3711 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3712 * 'key' fits our expectations for what a flow key should contain. */
3713 enum odp_key_fitness
3714 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3715 struct flow *mask, const struct flow *flow)
3717 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3720 /* Returns 'fitness' as a string, for use in debug messages. */
3722 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3725 case ODP_FIT_PERFECT:
3727 case ODP_FIT_TOO_MUCH:
3729 case ODP_FIT_TOO_LITTLE:
3730 return "too_little";
3738 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3739 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3740 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3741 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3742 * null, then the return value is not meaningful.) */
3744 odp_put_userspace_action(uint32_t pid,
3745 const void *userdata, size_t userdata_size,
3746 odp_port_t tunnel_out_port,
3747 struct ofpbuf *odp_actions)
3749 size_t userdata_ofs;
3752 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3753 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3755 userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN;
3757 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3758 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3761 * - The kernel rejected shorter userdata with -ERANGE.
3763 * - The kernel silently dropped userdata beyond the first 8 bytes.
3765 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3766 * separately disable features that required more than 8 bytes.) */
3767 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3768 MAX(8, userdata_size)),
3769 userdata, userdata_size);
3773 if (tunnel_out_port != ODPP_NONE) {
3774 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3777 nl_msg_end_nested(odp_actions, offset);
3779 return userdata_ofs;
3783 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3784 struct ofpbuf *odp_actions)
3786 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3787 tun_key_to_attr(odp_actions, tunnel);
3788 nl_msg_end_nested(odp_actions, offset);
3792 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
3793 struct ovs_action_push_tnl *data)
3795 int size = offsetof(struct ovs_action_push_tnl, header);
3797 size += data->header_len;
3798 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
3802 /* The commit_odp_actions() function and its helpers. */
3805 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3806 const void *key, size_t key_size)
3808 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3809 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3810 nl_msg_end_nested(odp_actions, offset);
3813 /* Masked set actions have a mask following the data within the netlink
3814 * attribute. The unmasked bits in the data will be cleared as the data
3815 * is copied to the action. */
3817 commit_masked_set_action(struct ofpbuf *odp_actions,
3818 enum ovs_key_attr key_type,
3819 const void *key_, const void *mask_, size_t key_size)
3821 size_t offset = nl_msg_start_nested(odp_actions,
3822 OVS_ACTION_ATTR_SET_MASKED);
3823 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3824 const char *key = key_, *mask = mask_;
3826 memcpy(data + key_size, mask, key_size);
3827 /* Clear unmasked bits while copying. */
3828 while (key_size--) {
3829 *data++ = *key++ & *mask++;
3831 nl_msg_end_nested(odp_actions, offset);
3834 /* If any of the flow key data that ODP actions can modify are different in
3835 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3836 * 'odp_actions' that change the flow tunneling information in key from
3837 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3838 * same way. In other words, operates the same as commit_odp_actions(), but
3839 * only on tunneling information. */
3841 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3842 struct ofpbuf *odp_actions)
3844 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3845 if (flow->tunnel.ip_dst) {
3846 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3849 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3850 odp_put_tunnel_action(&base->tunnel, odp_actions);
3855 commit(enum ovs_key_attr attr, bool use_masked_set,
3856 const void *key, void *base, void *mask, size_t size,
3857 struct ofpbuf *odp_actions)
3859 if (memcmp(key, base, size)) {
3860 bool fully_masked = odp_mask_is_exact(attr, mask, size);
3862 if (use_masked_set && !fully_masked) {
3863 commit_masked_set_action(odp_actions, attr, key, mask, size);
3865 if (!fully_masked) {
3866 memset(mask, 0xff, size);
3868 commit_set_action(odp_actions, attr, key, size);
3870 memcpy(base, key, size);
3873 /* Mask bits are set when we have either read or set the corresponding
3874 * values. Masked bits will be exact-matched, no need to set them
3875 * if the value did not actually change. */
3881 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
3883 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
3884 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
3888 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
3890 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
3891 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
3895 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
3896 struct ofpbuf *odp_actions,
3897 struct flow_wildcards *wc,
3900 struct ovs_key_ethernet key, base, mask;
3902 get_ethernet_key(flow, &key);
3903 get_ethernet_key(base_flow, &base);
3904 get_ethernet_key(&wc->masks, &mask);
3906 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
3907 &key, &base, &mask, sizeof key, odp_actions)) {
3908 put_ethernet_key(&base, base_flow);
3909 put_ethernet_key(&mask, &wc->masks);
3914 pop_vlan(struct flow *base,
3915 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3917 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3919 if (base->vlan_tci & htons(VLAN_CFI)) {
3920 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3926 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3927 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3929 if (base->vlan_tci == vlan_tci) {
3933 pop_vlan(base, odp_actions, wc);
3934 if (vlan_tci & htons(VLAN_CFI)) {
3935 struct ovs_action_push_vlan vlan;
3937 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3938 vlan.vlan_tci = vlan_tci;
3939 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3940 &vlan, sizeof vlan);
3942 base->vlan_tci = vlan_tci;
3945 /* Wildcarding already done at action translation time. */
3947 commit_mpls_action(const struct flow *flow, struct flow *base,
3948 struct ofpbuf *odp_actions)
3950 int base_n = flow_count_mpls_labels(base, NULL);
3951 int flow_n = flow_count_mpls_labels(flow, NULL);
3952 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
3955 while (base_n > common_n) {
3956 if (base_n - 1 == common_n && flow_n > common_n) {
3957 /* If there is only one more LSE in base than there are common
3958 * between base and flow; and flow has at least one more LSE than
3959 * is common then the topmost LSE of base may be updated using
3961 struct ovs_key_mpls mpls_key;
3963 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
3964 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3965 &mpls_key, sizeof mpls_key);
3966 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
3969 /* Otherwise, if there more LSEs in base than are common between
3970 * base and flow then pop the topmost one. */
3974 /* If all the LSEs are to be popped and this is not the outermost
3975 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
3976 * POP_MPLS action instead of flow->dl_type.
3978 * This is because the POP_MPLS action requires its ethertype
3979 * argument to be an MPLS ethernet type but in this case
3980 * flow->dl_type will be a non-MPLS ethernet type.
3982 * When the final POP_MPLS action occurs it use flow->dl_type and
3983 * the and the resulting packet will have the desired dl_type. */
3984 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
3985 dl_type = htons(ETH_TYPE_MPLS);
3987 dl_type = flow->dl_type;
3989 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
3990 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
3996 /* If, after the above popping and setting, there are more LSEs in flow
3997 * than base then some LSEs need to be pushed. */
3998 while (base_n < flow_n) {
3999 struct ovs_action_push_mpls *mpls;
4001 mpls = nl_msg_put_unspec_zero(odp_actions,
4002 OVS_ACTION_ATTR_PUSH_MPLS,
4004 mpls->mpls_ethertype = flow->dl_type;
4005 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4006 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4007 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4013 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4015 ipv4->ipv4_src = flow->nw_src;
4016 ipv4->ipv4_dst = flow->nw_dst;
4017 ipv4->ipv4_proto = flow->nw_proto;
4018 ipv4->ipv4_tos = flow->nw_tos;
4019 ipv4->ipv4_ttl = flow->nw_ttl;
4020 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4024 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4026 flow->nw_src = ipv4->ipv4_src;
4027 flow->nw_dst = ipv4->ipv4_dst;
4028 flow->nw_proto = ipv4->ipv4_proto;
4029 flow->nw_tos = ipv4->ipv4_tos;
4030 flow->nw_ttl = ipv4->ipv4_ttl;
4031 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4035 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4036 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4039 struct ovs_key_ipv4 key, mask, base;
4041 /* Check that nw_proto and nw_frag remain unchanged. */
4042 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4043 flow->nw_frag == base_flow->nw_frag);
4045 get_ipv4_key(flow, &key, false);
4046 get_ipv4_key(base_flow, &base, false);
4047 get_ipv4_key(&wc->masks, &mask, true);
4048 mask.ipv4_proto = 0; /* Not writeable. */
4049 mask.ipv4_frag = 0; /* Not writable. */
4051 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4053 put_ipv4_key(&base, base_flow, false);
4054 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4055 put_ipv4_key(&mask, &wc->masks, true);
4061 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4063 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4064 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4065 ipv6->ipv6_label = flow->ipv6_label;
4066 ipv6->ipv6_proto = flow->nw_proto;
4067 ipv6->ipv6_tclass = flow->nw_tos;
4068 ipv6->ipv6_hlimit = flow->nw_ttl;
4069 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4073 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4075 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4076 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4077 flow->ipv6_label = ipv6->ipv6_label;
4078 flow->nw_proto = ipv6->ipv6_proto;
4079 flow->nw_tos = ipv6->ipv6_tclass;
4080 flow->nw_ttl = ipv6->ipv6_hlimit;
4081 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4085 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4086 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4089 struct ovs_key_ipv6 key, mask, base;
4091 /* Check that nw_proto and nw_frag remain unchanged. */
4092 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4093 flow->nw_frag == base_flow->nw_frag);
4095 get_ipv6_key(flow, &key, false);
4096 get_ipv6_key(base_flow, &base, false);
4097 get_ipv6_key(&wc->masks, &mask, true);
4098 mask.ipv6_proto = 0; /* Not writeable. */
4099 mask.ipv6_frag = 0; /* Not writable. */
4101 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4103 put_ipv6_key(&base, base_flow, false);
4104 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4105 put_ipv6_key(&mask, &wc->masks, true);
4111 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4113 /* ARP key has padding, clear it. */
4114 memset(arp, 0, sizeof *arp);
4116 arp->arp_sip = flow->nw_src;
4117 arp->arp_tip = flow->nw_dst;
4118 arp->arp_op = htons(flow->nw_proto);
4119 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4120 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4124 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4126 flow->nw_src = arp->arp_sip;
4127 flow->nw_dst = arp->arp_tip;
4128 flow->nw_proto = ntohs(arp->arp_op);
4129 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4130 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4133 static enum slow_path_reason
4134 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4135 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4137 struct ovs_key_arp key, mask, base;
4139 get_arp_key(flow, &key);
4140 get_arp_key(base_flow, &base);
4141 get_arp_key(&wc->masks, &mask);
4143 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4145 put_arp_key(&base, base_flow);
4146 put_arp_key(&mask, &wc->masks);
4152 static enum slow_path_reason
4153 commit_set_nw_action(const struct flow *flow, struct flow *base,
4154 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4157 /* Check if 'flow' really has an L3 header. */
4158 if (!flow->nw_proto) {
4162 switch (ntohs(base->dl_type)) {
4164 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4168 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4172 return commit_set_arp_action(flow, base, odp_actions, wc);
4178 /* TCP, UDP, and SCTP keys have the same layout. */
4179 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4180 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4183 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4185 tp->tcp.tcp_src = flow->tp_src;
4186 tp->tcp.tcp_dst = flow->tp_dst;
4190 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4192 flow->tp_src = tp->tcp.tcp_src;
4193 flow->tp_dst = tp->tcp.tcp_dst;
4197 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4198 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4201 enum ovs_key_attr key_type;
4202 union ovs_key_tp key, mask, base;
4204 /* Check if 'flow' really has an L3 header. */
4205 if (!flow->nw_proto) {
4209 if (!is_ip_any(base_flow)) {
4213 if (flow->nw_proto == IPPROTO_TCP) {
4214 key_type = OVS_KEY_ATTR_TCP;
4215 } else if (flow->nw_proto == IPPROTO_UDP) {
4216 key_type = OVS_KEY_ATTR_UDP;
4217 } else if (flow->nw_proto == IPPROTO_SCTP) {
4218 key_type = OVS_KEY_ATTR_SCTP;
4223 get_tp_key(flow, &key);
4224 get_tp_key(base_flow, &base);
4225 get_tp_key(&wc->masks, &mask);
4227 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4229 put_tp_key(&base, base_flow);
4230 put_tp_key(&mask, &wc->masks);
4235 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4236 struct ofpbuf *odp_actions,
4237 struct flow_wildcards *wc,
4240 uint32_t key, mask, base;
4242 key = flow->skb_priority;
4243 base = base_flow->skb_priority;
4244 mask = wc->masks.skb_priority;
4246 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4247 sizeof key, odp_actions)) {
4248 base_flow->skb_priority = base;
4249 wc->masks.skb_priority = mask;
4254 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4255 struct ofpbuf *odp_actions,
4256 struct flow_wildcards *wc,
4259 uint32_t key, mask, base;
4261 key = flow->pkt_mark;
4262 base = base_flow->pkt_mark;
4263 mask = wc->masks.pkt_mark;
4265 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4266 sizeof key, odp_actions)) {
4267 base_flow->pkt_mark = base;
4268 wc->masks.pkt_mark = mask;
4272 /* If any of the flow key data that ODP actions can modify are different in
4273 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4274 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4275 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4276 * in addition to this function if needed. Sets fields in 'wc' that are
4277 * used as part of the action.
4279 * Returns a reason to force processing the flow's packets into the userspace
4280 * slow path, if there is one, otherwise 0. */
4281 enum slow_path_reason
4282 commit_odp_actions(const struct flow *flow, struct flow *base,
4283 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4286 enum slow_path_reason slow;
4288 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4289 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4290 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4291 commit_mpls_action(flow, base, odp_actions);
4292 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4293 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4294 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);