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 "tun-metadata.h"
39 #include "unaligned.h"
42 #include "openvswitch/vlog.h"
44 VLOG_DEFINE_THIS_MODULE(odp_util);
46 /* The interface between userspace and kernel uses an "OVS_*" prefix.
47 * Since this is fairly non-specific for the OVS userspace components,
48 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
49 * interactions with the datapath.
52 /* The set of characters that may separate one action or one key attribute
54 static const char *delimiters = ", \t\r\n";
58 const struct attr_len_tbl *next;
61 #define ATTR_LEN_INVALID -1
62 #define ATTR_LEN_VARIABLE -2
63 #define ATTR_LEN_NESTED -3
65 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
66 struct ofpbuf *, struct ofpbuf *);
67 static void format_odp_key_attr(const struct nlattr *a,
68 const struct nlattr *ma,
69 const struct hmap *portno_names, struct ds *ds,
73 struct geneve_opt d[63];
77 static int scan_geneve(const char *s, struct geneve_scan *key,
78 struct geneve_scan *mask);
79 static void format_geneve_opts(const struct geneve_opt *opt,
80 const struct geneve_opt *mask, int opts_len,
81 struct ds *, bool verbose);
83 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
84 int max, struct ofpbuf *,
85 const struct nlattr *key);
86 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
89 * - For an action whose argument has a fixed length, returned that
90 * nonnegative length in bytes.
92 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
94 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
96 odp_action_len(uint16_t type)
98 if (type > OVS_ACTION_ATTR_MAX) {
102 switch ((enum ovs_action_attr) type) {
103 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
104 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
105 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
106 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
107 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
108 case OVS_ACTION_ATTR_POP_VLAN: return 0;
109 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
110 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
111 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
112 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
113 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
114 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
115 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
117 case OVS_ACTION_ATTR_UNSPEC:
118 case __OVS_ACTION_ATTR_MAX:
119 return ATTR_LEN_INVALID;
122 return ATTR_LEN_INVALID;
125 /* Returns a string form of 'attr'. The return value is either a statically
126 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
127 * should be at least OVS_KEY_ATTR_BUFSIZE. */
128 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
130 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
133 case OVS_KEY_ATTR_UNSPEC: return "unspec";
134 case OVS_KEY_ATTR_ENCAP: return "encap";
135 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
136 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
137 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
138 case OVS_KEY_ATTR_IN_PORT: return "in_port";
139 case OVS_KEY_ATTR_ETHERNET: return "eth";
140 case OVS_KEY_ATTR_VLAN: return "vlan";
141 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
142 case OVS_KEY_ATTR_IPV4: return "ipv4";
143 case OVS_KEY_ATTR_IPV6: return "ipv6";
144 case OVS_KEY_ATTR_TCP: return "tcp";
145 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
146 case OVS_KEY_ATTR_UDP: return "udp";
147 case OVS_KEY_ATTR_SCTP: return "sctp";
148 case OVS_KEY_ATTR_ICMP: return "icmp";
149 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
150 case OVS_KEY_ATTR_ARP: return "arp";
151 case OVS_KEY_ATTR_ND: return "nd";
152 case OVS_KEY_ATTR_MPLS: return "mpls";
153 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
154 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
156 case __OVS_KEY_ATTR_MAX:
158 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
164 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
166 size_t len = nl_attr_get_size(a);
168 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
170 const uint8_t *unspec;
173 unspec = nl_attr_get(a);
174 for (i = 0; i < len; i++) {
175 ds_put_char(ds, i ? ' ': '(');
176 ds_put_format(ds, "%02x", unspec[i]);
178 ds_put_char(ds, ')');
183 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
185 static const struct nl_policy ovs_sample_policy[] = {
186 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
187 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
189 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
191 const struct nlattr *nla_acts;
194 ds_put_cstr(ds, "sample");
196 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
197 ds_put_cstr(ds, "(error)");
201 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
204 ds_put_format(ds, "(sample=%.1f%%,", percentage);
206 ds_put_cstr(ds, "actions(");
207 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
208 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
209 format_odp_actions(ds, nla_acts, len);
210 ds_put_format(ds, "))");
214 slow_path_reason_to_string(uint32_t reason)
216 switch ((enum slow_path_reason) reason) {
217 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
226 slow_path_reason_to_explanation(enum slow_path_reason reason)
229 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
238 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
239 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
244 /* Parse masked flags in numeric format? */
245 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
246 res_flags, res_mask, &n) && n > 0) {
247 if (*res_flags & ~allowed || *res_mask & ~allowed) {
255 if (res_mask && (*s == '+' || *s == '-')) {
256 uint32_t flags = 0, mask = 0;
258 /* Parse masked flags. */
259 while (s[0] != ')') {
266 } else if (s[0] == '-') {
274 name_len = strcspn(s, "+-)");
276 for (bit = 1; bit; bit <<= 1) {
277 const char *fname = bit_to_string(bit);
285 if (len != name_len) {
288 if (!strncmp(s, fname, len)) {
290 /* bit already set. */
293 if (!(bit & allowed)) {
305 return -EINVAL; /* Unknown flag name */
316 /* Parse unmasked flags. If a flag is present, it is set, otherwise
318 while (s[n] != ')') {
319 unsigned long long int flags;
323 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
324 if (flags & ~allowed) {
327 n += n0 + (s[n + n0] == ',');
332 for (bit = 1; bit; bit <<= 1) {
333 const char *name = bit_to_string(bit);
341 if (!strncmp(s + n, name, len) &&
342 (s[n + len] == ',' || s[n + len] == ')')) {
343 if (!(bit & allowed)) {
347 n += len + (s[n + len] == ',');
359 *res_mask = UINT32_MAX;
365 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
367 static const struct nl_policy ovs_userspace_policy[] = {
368 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
369 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
371 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
374 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
375 const struct nlattr *userdata_attr;
376 const struct nlattr *tunnel_out_port_attr;
378 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
379 ds_put_cstr(ds, "userspace(error)");
383 ds_put_format(ds, "userspace(pid=%"PRIu32,
384 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
386 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
389 const uint8_t *userdata = nl_attr_get(userdata_attr);
390 size_t userdata_len = nl_attr_get_size(userdata_attr);
391 bool userdata_unspec = true;
392 union user_action_cookie cookie;
394 if (userdata_len >= sizeof cookie.type
395 && userdata_len <= sizeof cookie) {
397 memset(&cookie, 0, sizeof cookie);
398 memcpy(&cookie, userdata, userdata_len);
400 userdata_unspec = false;
402 if (userdata_len == sizeof cookie.sflow
403 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
404 ds_put_format(ds, ",sFlow("
405 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
406 vlan_tci_to_vid(cookie.sflow.vlan_tci),
407 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
408 cookie.sflow.output);
409 } else if (userdata_len == sizeof cookie.slow_path
410 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
411 ds_put_cstr(ds, ",slow_path(");
412 format_flags(ds, slow_path_reason_to_string,
413 cookie.slow_path.reason, ',');
414 ds_put_format(ds, ")");
415 } else if (userdata_len == sizeof cookie.flow_sample
416 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
417 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
418 ",collector_set_id=%"PRIu32
419 ",obs_domain_id=%"PRIu32
420 ",obs_point_id=%"PRIu32")",
421 cookie.flow_sample.probability,
422 cookie.flow_sample.collector_set_id,
423 cookie.flow_sample.obs_domain_id,
424 cookie.flow_sample.obs_point_id);
425 } else if (userdata_len >= sizeof cookie.ipfix
426 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
427 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
428 cookie.ipfix.output_odp_port);
430 userdata_unspec = true;
434 if (userdata_unspec) {
436 ds_put_format(ds, ",userdata(");
437 for (i = 0; i < userdata_len; i++) {
438 ds_put_format(ds, "%02x", userdata[i]);
440 ds_put_char(ds, ')');
444 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
445 if (tunnel_out_port_attr) {
446 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
447 nl_attr_get_u32(tunnel_out_port_attr));
450 ds_put_char(ds, ')');
454 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
456 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
457 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
458 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
459 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
461 ds_put_char(ds, ',');
463 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
464 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
465 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
466 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
468 ds_put_char(ds, ',');
470 if (!(tci & htons(VLAN_CFI))) {
471 ds_put_cstr(ds, "cfi=0");
472 ds_put_char(ds, ',');
478 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
480 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
481 mpls_lse_to_label(mpls_lse),
482 mpls_lse_to_tc(mpls_lse),
483 mpls_lse_to_ttl(mpls_lse),
484 mpls_lse_to_bos(mpls_lse));
488 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
489 const struct ovs_key_mpls *mpls_mask, int n)
492 ovs_be32 key = mpls_key->mpls_lse;
494 if (mpls_mask == NULL) {
495 format_mpls_lse(ds, key);
497 ovs_be32 mask = mpls_mask->mpls_lse;
499 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
500 mpls_lse_to_label(key), mpls_lse_to_label(mask),
501 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
502 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
503 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
508 for (i = 0; i < n; i++) {
509 ds_put_format(ds, "lse%d=%#"PRIx32,
510 i, ntohl(mpls_key[i].mpls_lse));
512 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
514 ds_put_char(ds, ',');
521 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
523 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
527 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
529 ds_put_format(ds, "hash(");
531 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
532 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
534 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
537 ds_put_format(ds, ")");
541 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
543 const struct udp_header *udp;
545 udp = (const struct udp_header *) (ip + 1);
546 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
547 ntohs(udp->udp_src), ntohs(udp->udp_dst),
548 ntohs(udp->udp_csum));
554 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
556 const struct eth_header *eth;
557 const struct ip_header *ip;
560 eth = (const struct eth_header *)data->header;
563 ip = (const struct ip_header *)l3;
566 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
567 data->header_len, data->tnl_type);
568 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
569 ds_put_format(ds, ",src=");
570 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
571 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
574 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
575 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
576 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
577 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
578 ip->ip_proto, ip->ip_tos,
582 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
583 const struct vxlanhdr *vxh;
585 vxh = format_udp_tnl_push_header(ds, ip);
587 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
588 ntohl(get_16aligned_be32(&vxh->vx_flags)),
589 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
590 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
591 const struct genevehdr *gnh;
593 gnh = format_udp_tnl_push_header(ds, ip);
595 ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
596 gnh->oam ? "oam," : "",
597 gnh->critical ? "crit," : "",
598 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
601 ds_put_cstr(ds, ",options(");
602 format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
604 ds_put_char(ds, ')');
607 ds_put_char(ds, ')');
608 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
609 const struct gre_base_hdr *greh;
610 ovs_16aligned_be32 *options;
613 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
614 greh = (const struct gre_base_hdr *) l4;
616 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
617 ntohs(greh->flags), ntohs(greh->protocol));
618 options = (ovs_16aligned_be32 *)(greh + 1);
619 if (greh->flags & htons(GRE_CSUM)) {
620 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
623 if (greh->flags & htons(GRE_KEY)) {
624 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
627 if (greh->flags & htons(GRE_SEQ)) {
628 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
631 ds_put_format(ds, ")");
633 ds_put_format(ds, ")");
637 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
639 struct ovs_action_push_tnl *data;
641 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
643 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
644 format_odp_tnl_push_header(ds, data);
645 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
649 format_odp_action(struct ds *ds, const struct nlattr *a)
652 enum ovs_action_attr type = nl_attr_type(a);
653 const struct ovs_action_push_vlan *vlan;
656 expected_len = odp_action_len(nl_attr_type(a));
657 if (expected_len != ATTR_LEN_VARIABLE &&
658 nl_attr_get_size(a) != expected_len) {
659 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
660 nl_attr_get_size(a), expected_len);
661 format_generic_odp_action(ds, a);
666 case OVS_ACTION_ATTR_OUTPUT:
667 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
669 case OVS_ACTION_ATTR_TUNNEL_POP:
670 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
672 case OVS_ACTION_ATTR_TUNNEL_PUSH:
673 format_odp_tnl_push_action(ds, a);
675 case OVS_ACTION_ATTR_USERSPACE:
676 format_odp_userspace_action(ds, a);
678 case OVS_ACTION_ATTR_RECIRC:
679 format_odp_recirc_action(ds, nl_attr_get_u32(a));
681 case OVS_ACTION_ATTR_HASH:
682 format_odp_hash_action(ds, nl_attr_get(a));
684 case OVS_ACTION_ATTR_SET_MASKED:
686 size = nl_attr_get_size(a) / 2;
687 ds_put_cstr(ds, "set(");
689 /* Masked set action not supported for tunnel key, which is bigger. */
690 if (size <= sizeof(struct ovs_key_ipv6)) {
691 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
692 sizeof(struct nlattr))];
693 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
694 sizeof(struct nlattr))];
696 mask->nla_type = attr->nla_type = nl_attr_type(a);
697 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
698 memcpy(attr + 1, (char *)(a + 1), size);
699 memcpy(mask + 1, (char *)(a + 1) + size, size);
700 format_odp_key_attr(attr, mask, NULL, ds, false);
702 format_odp_key_attr(a, NULL, NULL, ds, false);
704 ds_put_cstr(ds, ")");
706 case OVS_ACTION_ATTR_SET:
707 ds_put_cstr(ds, "set(");
708 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
709 ds_put_cstr(ds, ")");
711 case OVS_ACTION_ATTR_PUSH_VLAN:
712 vlan = nl_attr_get(a);
713 ds_put_cstr(ds, "push_vlan(");
714 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
715 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
717 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
718 ds_put_char(ds, ')');
720 case OVS_ACTION_ATTR_POP_VLAN:
721 ds_put_cstr(ds, "pop_vlan");
723 case OVS_ACTION_ATTR_PUSH_MPLS: {
724 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
725 ds_put_cstr(ds, "push_mpls(");
726 format_mpls_lse(ds, mpls->mpls_lse);
727 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
730 case OVS_ACTION_ATTR_POP_MPLS: {
731 ovs_be16 ethertype = nl_attr_get_be16(a);
732 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
735 case OVS_ACTION_ATTR_SAMPLE:
736 format_odp_sample_action(ds, a);
738 case OVS_ACTION_ATTR_UNSPEC:
739 case __OVS_ACTION_ATTR_MAX:
741 format_generic_odp_action(ds, a);
747 format_odp_actions(struct ds *ds, const struct nlattr *actions,
751 const struct nlattr *a;
754 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
756 ds_put_char(ds, ',');
758 format_odp_action(ds, a);
763 if (left == actions_len) {
764 ds_put_cstr(ds, "<empty>");
766 ds_put_format(ds, ",***%u leftover bytes*** (", left);
767 for (i = 0; i < left; i++) {
768 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
770 ds_put_char(ds, ')');
773 ds_put_cstr(ds, "drop");
777 /* Separate out parse_odp_userspace_action() function. */
779 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
782 union user_action_cookie cookie;
784 odp_port_t tunnel_out_port;
786 void *user_data = NULL;
787 size_t user_data_size = 0;
789 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
795 uint32_t probability;
796 uint32_t collector_set_id;
797 uint32_t obs_domain_id;
798 uint32_t obs_point_id;
801 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
802 "pcp=%i,output=%"SCNi32")%n",
803 &vid, &pcp, &output, &n1)) {
807 tci = vid | (pcp << VLAN_PCP_SHIFT);
812 cookie.type = USER_ACTION_COOKIE_SFLOW;
813 cookie.sflow.vlan_tci = htons(tci);
814 cookie.sflow.output = output;
816 user_data_size = sizeof cookie.sflow;
817 } else if (ovs_scan(&s[n], ",slow_path(%n",
822 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
823 cookie.slow_path.unused = 0;
824 cookie.slow_path.reason = 0;
826 res = parse_flags(&s[n], slow_path_reason_to_string,
827 &cookie.slow_path.reason,
828 SLOW_PATH_REASON_MASK, NULL);
829 if (res < 0 || s[n + res] != ')') {
835 user_data_size = sizeof cookie.slow_path;
836 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
837 "collector_set_id=%"SCNi32","
838 "obs_domain_id=%"SCNi32","
839 "obs_point_id=%"SCNi32")%n",
840 &probability, &collector_set_id,
841 &obs_domain_id, &obs_point_id, &n1)) {
844 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
845 cookie.flow_sample.probability = probability;
846 cookie.flow_sample.collector_set_id = collector_set_id;
847 cookie.flow_sample.obs_domain_id = obs_domain_id;
848 cookie.flow_sample.obs_point_id = obs_point_id;
850 user_data_size = sizeof cookie.flow_sample;
851 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
854 cookie.type = USER_ACTION_COOKIE_IPFIX;
855 cookie.ipfix.output_odp_port = u32_to_odp(output);
857 user_data_size = sizeof cookie.ipfix;
858 } else if (ovs_scan(&s[n], ",userdata(%n",
863 ofpbuf_init(&buf, 16);
864 end = ofpbuf_put_hex(&buf, &s[n], NULL);
868 user_data = buf.data;
869 user_data_size = buf.size;
876 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
877 &tunnel_out_port, &n1)) {
878 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
880 } else if (s[n] == ')') {
881 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
890 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
892 struct eth_header *eth;
893 struct ip_header *ip;
894 struct udp_header *udp;
895 struct gre_base_hdr *greh;
896 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
898 uint32_t tnl_type = 0, header_len = 0;
902 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
905 eth = (struct eth_header *) data->header;
906 l3 = (data->header + sizeof *eth);
907 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
908 ip = (struct ip_header *) l3;
909 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
910 "eth(dst="ETH_ADDR_SCAN_FMT",",
913 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
917 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
918 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
921 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
924 eth->eth_type = htons(dl_type);
927 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
928 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
931 &ip->ip_proto, &ip->ip_tos,
932 &ip->ip_ttl, &ip->ip_frag_off)) {
935 put_16aligned_be32(&ip->ip_src, sip);
936 put_16aligned_be32(&ip->ip_dst, dip);
939 udp = (struct udp_header *) l4;
940 greh = (struct gre_base_hdr *) l4;
941 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
942 &udp_src, &udp_dst, &csum)) {
943 uint32_t vx_flags, vni;
945 udp->udp_src = htons(udp_src);
946 udp->udp_dst = htons(udp_dst);
948 udp->udp_csum = htons(csum);
950 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
952 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
954 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
955 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
956 tnl_type = OVS_VPORT_TYPE_VXLAN;
957 header_len = sizeof *eth + sizeof *ip +
958 sizeof *udp + sizeof *vxh;
959 } else if (ovs_scan_len(s, &n, "geneve(")) {
960 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
962 memset(gnh, 0, sizeof *gnh);
963 header_len = sizeof *eth + sizeof *ip +
964 sizeof *udp + sizeof *gnh;
966 if (ovs_scan_len(s, &n, "oam,")) {
969 if (ovs_scan_len(s, &n, "crit,")) {
972 if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
975 if (ovs_scan_len(s, &n, ",options(")) {
976 struct geneve_scan options;
979 memset(&options, 0, sizeof options);
980 len = scan_geneve(s + n, &options, NULL);
985 memcpy(gnh->options, options.d, options.len);
986 gnh->opt_len = options.len / 4;
987 header_len += options.len;
991 if (!ovs_scan_len(s, &n, "))")) {
995 gnh->proto_type = htons(ETH_TYPE_TEB);
996 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
997 tnl_type = OVS_VPORT_TYPE_GENEVE;
1001 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
1002 &gre_flags, &gre_proto)){
1004 tnl_type = OVS_VPORT_TYPE_GRE;
1005 greh->flags = htons(gre_flags);
1006 greh->protocol = htons(gre_proto);
1007 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
1009 if (greh->flags & htons(GRE_CSUM)) {
1010 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
1014 memset(options, 0, sizeof *options);
1015 *((ovs_be16 *)options) = htons(csum);
1018 if (greh->flags & htons(GRE_KEY)) {
1021 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
1025 put_16aligned_be32(options, htonl(key));
1028 if (greh->flags & htons(GRE_SEQ)) {
1031 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
1034 put_16aligned_be32(options, htonl(seq));
1038 if (!ovs_scan_len(s, &n, "))")) {
1042 header_len = sizeof *eth + sizeof *ip +
1043 ((uint8_t *) options - (uint8_t *) greh);
1048 /* check tunnel meta data. */
1049 if (data->tnl_type != tnl_type) {
1052 if (data->header_len != header_len) {
1057 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1065 parse_odp_action(const char *s, const struct simap *port_names,
1066 struct ofpbuf *actions)
1072 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1073 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1079 int len = strcspn(s, delimiters);
1080 struct simap_node *node;
1082 node = simap_find_len(port_names, s, len);
1084 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1093 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1094 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1099 if (!strncmp(s, "userspace(", 10)) {
1100 return parse_odp_userspace_action(s, actions);
1103 if (!strncmp(s, "set(", 4)) {
1106 struct nlattr mask[128 / sizeof(struct nlattr)];
1107 struct ofpbuf maskbuf;
1108 struct nlattr *nested, *key;
1111 /* 'mask' is big enough to hold any key. */
1112 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1114 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1115 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1119 if (s[retval + 4] != ')') {
1123 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1126 size = nl_attr_get_size(mask);
1127 if (size == nl_attr_get_size(key)) {
1128 /* Change to masked set action if not fully masked. */
1129 if (!is_all_ones(mask + 1, size)) {
1130 key->nla_len += size;
1131 ofpbuf_put(actions, mask + 1, size);
1132 /* 'actions' may have been reallocated by ofpbuf_put(). */
1133 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1134 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1138 nl_msg_end_nested(actions, start_ofs);
1143 struct ovs_action_push_vlan push;
1144 int tpid = ETH_TYPE_VLAN;
1149 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1150 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1151 &vid, &pcp, &cfi, &n)
1152 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1153 &tpid, &vid, &pcp, &n)
1154 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1155 &tpid, &vid, &pcp, &cfi, &n)) {
1156 push.vlan_tpid = htons(tpid);
1157 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1158 | (pcp << VLAN_PCP_SHIFT)
1159 | (cfi ? VLAN_CFI : 0));
1160 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1161 &push, sizeof push);
1167 if (!strncmp(s, "pop_vlan", 8)) {
1168 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1176 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1177 && percentage >= 0. && percentage <= 100.0) {
1178 size_t sample_ofs, actions_ofs;
1181 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1182 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1183 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1184 (probability <= 0 ? 0
1185 : probability >= UINT32_MAX ? UINT32_MAX
1188 actions_ofs = nl_msg_start_nested(actions,
1189 OVS_SAMPLE_ATTR_ACTIONS);
1193 n += strspn(s + n, delimiters);
1198 retval = parse_odp_action(s + n, port_names, actions);
1204 nl_msg_end_nested(actions, actions_ofs);
1205 nl_msg_end_nested(actions, sample_ofs);
1207 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1215 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1216 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1222 struct ovs_action_push_tnl data;
1225 n = ovs_parse_tnl_push(s, &data);
1227 odp_put_tnl_push_action(actions, &data);
1236 /* Parses the string representation of datapath actions, in the format output
1237 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1238 * value. On success, the ODP actions are appended to 'actions' as a series of
1239 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1240 * way, 'actions''s data might be reallocated. */
1242 odp_actions_from_string(const char *s, const struct simap *port_names,
1243 struct ofpbuf *actions)
1247 if (!strcasecmp(s, "drop")) {
1251 old_size = actions->size;
1255 s += strspn(s, delimiters);
1260 retval = parse_odp_action(s, port_names, actions);
1261 if (retval < 0 || !strchr(delimiters, s[retval])) {
1262 actions->size = old_size;
1271 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1272 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1275 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1276 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1277 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1278 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1279 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1280 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1281 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1282 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1283 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1284 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1285 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1286 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1287 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1288 .next = ovs_vxlan_ext_attr_lens ,
1289 .next_max = OVS_VXLAN_EXT_MAX},
1292 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1293 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1294 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1295 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1296 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1297 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1298 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1299 .next = ovs_tun_key_attr_lens,
1300 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1301 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1302 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1303 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1304 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1305 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1306 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1307 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1308 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1309 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1310 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1311 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1312 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1313 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1314 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1315 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1318 /* Returns the correct length of the payload for a flow key attribute of the
1319 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1320 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1321 * payload is a nested type. */
1323 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1325 if (type > max_len) {
1326 return ATTR_LEN_INVALID;
1329 return tbl[type].len;
1333 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1335 size_t len = nl_attr_get_size(a);
1337 const uint8_t *unspec;
1340 unspec = nl_attr_get(a);
1341 for (i = 0; i < len; i++) {
1343 ds_put_char(ds, ' ');
1345 ds_put_format(ds, "%02x", unspec[i]);
1351 ovs_frag_type_to_string(enum ovs_frag_type type)
1354 case OVS_FRAG_TYPE_NONE:
1356 case OVS_FRAG_TYPE_FIRST:
1358 case OVS_FRAG_TYPE_LATER:
1360 case __OVS_FRAG_TYPE_MAX:
1366 static enum odp_key_fitness
1367 odp_tun_key_from_attr__(const struct nlattr *attr,
1368 const struct nlattr *flow_attrs, size_t flow_attr_len,
1369 const struct flow_tnl *src_tun, struct flow_tnl *tun)
1372 const struct nlattr *a;
1374 bool unknown = false;
1376 NL_NESTED_FOR_EACH(a, left, attr) {
1377 uint16_t type = nl_attr_type(a);
1378 size_t len = nl_attr_get_size(a);
1379 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1380 OVS_TUNNEL_ATTR_MAX, type);
1382 if (len != expected_len && expected_len >= 0) {
1383 return ODP_FIT_ERROR;
1387 case OVS_TUNNEL_KEY_ATTR_ID:
1388 tun->tun_id = nl_attr_get_be64(a);
1389 tun->flags |= FLOW_TNL_F_KEY;
1391 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1392 tun->ip_src = nl_attr_get_be32(a);
1394 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1395 tun->ip_dst = nl_attr_get_be32(a);
1397 case OVS_TUNNEL_KEY_ATTR_TOS:
1398 tun->ip_tos = nl_attr_get_u8(a);
1400 case OVS_TUNNEL_KEY_ATTR_TTL:
1401 tun->ip_ttl = nl_attr_get_u8(a);
1404 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1405 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1407 case OVS_TUNNEL_KEY_ATTR_CSUM:
1408 tun->flags |= FLOW_TNL_F_CSUM;
1410 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1411 tun->tp_src = nl_attr_get_be16(a);
1413 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1414 tun->tp_dst = nl_attr_get_be16(a);
1416 case OVS_TUNNEL_KEY_ATTR_OAM:
1417 tun->flags |= FLOW_TNL_F_OAM;
1419 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1420 static const struct nl_policy vxlan_opts_policy[] = {
1421 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1423 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1425 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1426 return ODP_FIT_ERROR;
1429 if (ext[OVS_VXLAN_EXT_GBP]) {
1430 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1432 tun->gbp_id = htons(gbp & 0xFFFF);
1433 tun->gbp_flags = (gbp >> 16) & 0xFF;
1438 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1439 if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1442 return ODP_FIT_ERROR;
1447 /* Allow this to show up as unexpected, if there are unknown
1448 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1455 return ODP_FIT_ERROR;
1458 return ODP_FIT_TOO_MUCH;
1460 return ODP_FIT_PERFECT;
1463 enum odp_key_fitness
1464 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1466 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun);
1470 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1471 const struct flow_tnl *tun_flow_key,
1472 const struct ofpbuf *key_buf)
1476 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1478 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1479 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1480 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1482 if (tun_key->ip_src) {
1483 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1485 if (tun_key->ip_dst) {
1486 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1488 if (tun_key->ip_tos) {
1489 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1491 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1492 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1493 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1495 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1496 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1498 if (tun_key->tp_src) {
1499 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1501 if (tun_key->tp_dst) {
1502 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1504 if (tun_key->flags & FLOW_TNL_F_OAM) {
1505 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1507 if (tun_key->gbp_flags || tun_key->gbp_id) {
1508 size_t vxlan_opts_ofs;
1510 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1511 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1512 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1513 nl_msg_end_nested(a, vxlan_opts_ofs);
1516 if (tun_key == tun_flow_key) {
1517 tun_metadata_to_geneve_nlattr_flow(&tun_key->metadata, a);
1519 tun_metadata_to_geneve_nlattr_mask(key_buf, &tun_key->metadata,
1520 &tun_flow_key->metadata, a);
1523 nl_msg_end_nested(a, tun_key_ofs);
1527 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1529 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1533 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1535 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1536 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1538 if (attr == OVS_KEY_ATTR_IPV6) {
1539 const struct ovs_key_ipv6 *ipv6_mask = mask;
1542 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1543 == htonl(IPV6_LABEL_MASK))
1544 && ipv6_mask->ipv6_proto == UINT8_MAX
1545 && ipv6_mask->ipv6_tclass == UINT8_MAX
1546 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1547 && ipv6_mask->ipv6_frag == UINT8_MAX
1548 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1549 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1551 if (attr == OVS_KEY_ATTR_TUNNEL) {
1555 if (attr == OVS_KEY_ATTR_ARP) {
1556 /* ARP key has padding, ignore it. */
1557 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1558 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1559 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1560 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1563 return is_all_ones(mask, size);
1567 odp_mask_attr_is_exact(const struct nlattr *ma)
1569 enum ovs_key_attr attr = nl_attr_type(ma);
1573 if (attr == OVS_KEY_ATTR_TUNNEL) {
1576 mask = nl_attr_get(ma);
1577 size = nl_attr_get_size(ma);
1580 return odp_mask_is_exact(attr, mask, size);
1584 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1587 struct odp_portno_names *odp_portno_names;
1589 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1590 odp_portno_names->port_no = port_no;
1591 odp_portno_names->name = xstrdup(port_name);
1592 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1593 hash_odp_port(port_no));
1597 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1599 struct odp_portno_names *odp_portno_names;
1601 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1602 hash_odp_port(port_no), portno_names) {
1603 if (odp_portno_names->port_no == port_no) {
1604 return odp_portno_names->name;
1611 odp_portno_names_destroy(struct hmap *portno_names)
1613 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1614 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1615 hmap_node, portno_names) {
1616 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1617 free(odp_portno_names->name);
1618 free(odp_portno_names);
1622 /* Format helpers. */
1625 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1626 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1628 bool mask_empty = mask && eth_addr_is_zero(*mask);
1630 if (verbose || !mask_empty) {
1631 bool mask_full = !mask || eth_mask_is_exact(*mask);
1634 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1636 ds_put_format(ds, "%s=", name);
1637 eth_format_masked(key, *mask, ds);
1638 ds_put_char(ds, ',');
1644 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1645 const ovs_be64 *mask, bool verbose)
1647 bool mask_empty = mask && !*mask;
1649 if (verbose || !mask_empty) {
1650 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1652 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1653 if (!mask_full) { /* Partially masked. */
1654 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1656 ds_put_char(ds, ',');
1661 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1662 const ovs_be32 *mask, bool verbose)
1664 bool mask_empty = mask && !*mask;
1666 if (verbose || !mask_empty) {
1667 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1669 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1670 if (!mask_full) { /* Partially masked. */
1671 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1673 ds_put_char(ds, ',');
1678 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1679 const ovs_be32 (*mask_)[4], bool verbose)
1681 char buf[INET6_ADDRSTRLEN];
1682 const struct in6_addr *key = (const struct in6_addr *)key_;
1683 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1685 bool mask_empty = mask && ipv6_mask_is_any(mask);
1687 if (verbose || !mask_empty) {
1688 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1690 inet_ntop(AF_INET6, key, buf, sizeof buf);
1691 ds_put_format(ds, "%s=%s", name, buf);
1692 if (!mask_full) { /* Partially masked. */
1693 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1694 ds_put_format(ds, "/%s", buf);
1696 ds_put_char(ds, ',');
1701 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1702 const ovs_be32 *mask, bool verbose)
1704 bool mask_empty = mask && !*mask;
1706 if (verbose || !mask_empty) {
1707 bool mask_full = !mask
1708 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1710 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1711 if (!mask_full) { /* Partially masked. */
1712 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1714 ds_put_char(ds, ',');
1719 format_u8x(struct ds *ds, const char *name, uint8_t key,
1720 const uint8_t *mask, bool verbose)
1722 bool mask_empty = mask && !*mask;
1724 if (verbose || !mask_empty) {
1725 bool mask_full = !mask || *mask == UINT8_MAX;
1727 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1728 if (!mask_full) { /* Partially masked. */
1729 ds_put_format(ds, "/%#"PRIx8, *mask);
1731 ds_put_char(ds, ',');
1736 format_u8u(struct ds *ds, const char *name, uint8_t key,
1737 const uint8_t *mask, bool verbose)
1739 bool mask_empty = mask && !*mask;
1741 if (verbose || !mask_empty) {
1742 bool mask_full = !mask || *mask == UINT8_MAX;
1744 ds_put_format(ds, "%s=%"PRIu8, name, key);
1745 if (!mask_full) { /* Partially masked. */
1746 ds_put_format(ds, "/%#"PRIx8, *mask);
1748 ds_put_char(ds, ',');
1753 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1754 const ovs_be16 *mask, bool verbose)
1756 bool mask_empty = mask && !*mask;
1758 if (verbose || !mask_empty) {
1759 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1761 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1762 if (!mask_full) { /* Partially masked. */
1763 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1765 ds_put_char(ds, ',');
1770 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1771 const ovs_be16 *mask, bool verbose)
1773 bool mask_empty = mask && !*mask;
1775 if (verbose || !mask_empty) {
1776 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1778 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1779 if (!mask_full) { /* Partially masked. */
1780 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1782 ds_put_char(ds, ',');
1787 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1788 const uint16_t *mask, bool verbose)
1790 bool mask_empty = mask && !*mask;
1792 if (verbose || !mask_empty) {
1793 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1795 ds_put_cstr(ds, name);
1796 ds_put_char(ds, '(');
1797 if (!mask_full) { /* Partially masked. */
1798 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1799 } else { /* Fully masked. */
1800 format_flags(ds, flow_tun_flag_to_string, key, ',');
1802 ds_put_cstr(ds, "),");
1807 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1808 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1812 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1813 if (expected_len != ATTR_LEN_VARIABLE &&
1814 expected_len != ATTR_LEN_NESTED) {
1816 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1817 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1819 if (bad_key_len || bad_mask_len) {
1821 ds_put_format(ds, "key%u", nl_attr_type(a));
1824 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1825 nl_attr_get_size(a), expected_len);
1827 format_generic_odp_key(a, ds);
1829 ds_put_char(ds, '/');
1831 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1832 nl_attr_get_size(ma), expected_len);
1834 format_generic_odp_key(ma, ds);
1836 ds_put_char(ds, ')');
1845 format_unknown_key(struct ds *ds, const struct nlattr *a,
1846 const struct nlattr *ma)
1848 ds_put_format(ds, "key%u(", nl_attr_type(a));
1849 format_generic_odp_key(a, ds);
1850 if (ma && !odp_mask_attr_is_exact(ma)) {
1851 ds_put_char(ds, '/');
1852 format_generic_odp_key(ma, ds);
1854 ds_put_cstr(ds, "),");
1858 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1859 const struct nlattr *mask_attr, struct ds *ds,
1863 const struct nlattr *a;
1866 ofpbuf_init(&ofp, 100);
1867 NL_NESTED_FOR_EACH(a, left, attr) {
1868 uint16_t type = nl_attr_type(a);
1869 const struct nlattr *ma = NULL;
1872 ma = nl_attr_find__(nl_attr_get(mask_attr),
1873 nl_attr_get_size(mask_attr), type);
1875 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1881 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1882 OVS_VXLAN_EXT_MAX, true)) {
1887 case OVS_VXLAN_EXT_GBP: {
1888 uint32_t key = nl_attr_get_u32(a);
1889 ovs_be16 id, id_mask;
1890 uint8_t flags, flags_mask;
1892 id = htons(key & 0xFFFF);
1893 flags = (key >> 16) & 0xFF;
1895 uint32_t mask = nl_attr_get_u32(ma);
1896 id_mask = htons(mask & 0xFFFF);
1897 flags_mask = (mask >> 16) & 0xFF;
1900 ds_put_cstr(ds, "gbp(");
1901 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1902 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1904 ds_put_cstr(ds, "),");
1909 format_unknown_key(ds, a, ma);
1915 ofpbuf_uninit(&ofp);
1918 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1921 format_geneve_opts(const struct geneve_opt *opt,
1922 const struct geneve_opt *mask, int opts_len,
1923 struct ds *ds, bool verbose)
1925 while (opts_len > 0) {
1927 uint8_t data_len, data_len_mask;
1929 if (opts_len < sizeof *opt) {
1930 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1931 opts_len, sizeof *opt);
1935 data_len = opt->length * 4;
1937 if (mask->length == 0x1f) {
1938 data_len_mask = UINT8_MAX;
1940 data_len_mask = mask->length;
1943 len = sizeof *opt + data_len;
1944 if (len > opts_len) {
1945 ds_put_format(ds, "opt len %u greater than remaining %u",
1950 ds_put_char(ds, '{');
1951 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1953 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1954 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1955 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1956 ds_put_hex(ds, opt + 1, data_len);
1957 if (mask && !is_all_ones(mask + 1, data_len)) {
1958 ds_put_char(ds, '/');
1959 ds_put_hex(ds, mask + 1, data_len);
1964 ds_put_char(ds, '}');
1966 opt += len / sizeof(*opt);
1968 mask += len / sizeof(*opt);
1975 format_odp_tun_geneve(const struct nlattr *attr,
1976 const struct nlattr *mask_attr, struct ds *ds,
1979 int opts_len = nl_attr_get_size(attr);
1980 const struct geneve_opt *opt = nl_attr_get(attr);
1981 const struct geneve_opt *mask = mask_attr ?
1982 nl_attr_get(mask_attr) : NULL;
1984 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1985 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1986 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1990 format_geneve_opts(opt, mask, opts_len, ds, verbose);
1994 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1995 struct ds *ds, bool verbose)
1998 const struct nlattr *a;
2000 uint16_t mask_flags = 0;
2003 ofpbuf_init(&ofp, 100);
2004 NL_NESTED_FOR_EACH(a, left, attr) {
2005 enum ovs_tunnel_key_attr type = nl_attr_type(a);
2006 const struct nlattr *ma = NULL;
2009 ma = nl_attr_find__(nl_attr_get(mask_attr),
2010 nl_attr_get_size(mask_attr), type);
2012 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
2013 OVS_TUNNEL_KEY_ATTR_MAX,
2018 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
2019 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2024 case OVS_TUNNEL_KEY_ATTR_ID:
2025 format_be64(ds, "tun_id", nl_attr_get_be64(a),
2026 ma ? nl_attr_get(ma) : NULL, verbose);
2027 flags |= FLOW_TNL_F_KEY;
2029 mask_flags |= FLOW_TNL_F_KEY;
2032 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2033 format_ipv4(ds, "src", nl_attr_get_be32(a),
2034 ma ? nl_attr_get(ma) : NULL, verbose);
2036 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2037 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2038 ma ? nl_attr_get(ma) : NULL, verbose);
2040 case OVS_TUNNEL_KEY_ATTR_TOS:
2041 format_u8x(ds, "tos", nl_attr_get_u8(a),
2042 ma ? nl_attr_get(ma) : NULL, verbose);
2044 case OVS_TUNNEL_KEY_ATTR_TTL:
2045 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2046 ma ? nl_attr_get(ma) : NULL, verbose);
2048 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2049 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2051 case OVS_TUNNEL_KEY_ATTR_CSUM:
2052 flags |= FLOW_TNL_F_CSUM;
2054 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2055 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2056 ma ? nl_attr_get(ma) : NULL, verbose);
2058 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2059 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2060 ma ? nl_attr_get(ma) : NULL, verbose);
2062 case OVS_TUNNEL_KEY_ATTR_OAM:
2063 flags |= FLOW_TNL_F_OAM;
2065 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2066 ds_put_cstr(ds, "vxlan(");
2067 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2068 ds_put_cstr(ds, "),");
2070 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2071 ds_put_cstr(ds, "geneve(");
2072 format_odp_tun_geneve(a, ma, ds, verbose);
2073 ds_put_cstr(ds, "),");
2075 case __OVS_TUNNEL_KEY_ATTR_MAX:
2077 format_unknown_key(ds, a, ma);
2082 /* Flags can have a valid mask even if the attribute is not set, so
2083 * we need to collect these separately. */
2085 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2086 switch (nl_attr_type(a)) {
2087 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2088 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2090 case OVS_TUNNEL_KEY_ATTR_CSUM:
2091 mask_flags |= FLOW_TNL_F_CSUM;
2093 case OVS_TUNNEL_KEY_ATTR_OAM:
2094 mask_flags |= FLOW_TNL_F_OAM;
2100 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2103 ofpbuf_uninit(&ofp);
2107 format_frag(struct ds *ds, const char *name, uint8_t key,
2108 const uint8_t *mask, bool verbose)
2110 bool mask_empty = mask && !*mask;
2112 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2113 if (verbose || !mask_empty) {
2114 bool mask_full = !mask || *mask == UINT8_MAX;
2116 if (!mask_full) { /* Partially masked. */
2117 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2120 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2126 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2127 const struct hmap *portno_names, struct ds *ds,
2130 enum ovs_key_attr attr = nl_attr_type(a);
2131 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2134 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2136 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2138 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2139 OVS_KEY_ATTR_MAX, false)) {
2143 ds_put_char(ds, '(');
2145 case OVS_KEY_ATTR_ENCAP:
2146 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2147 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2148 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2150 } else if (nl_attr_get_size(a)) {
2151 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2156 case OVS_KEY_ATTR_PRIORITY:
2157 case OVS_KEY_ATTR_SKB_MARK:
2158 case OVS_KEY_ATTR_DP_HASH:
2159 case OVS_KEY_ATTR_RECIRC_ID:
2160 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2162 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2166 case OVS_KEY_ATTR_TUNNEL:
2167 format_odp_tun_attr(a, ma, ds, verbose);
2170 case OVS_KEY_ATTR_IN_PORT:
2171 if (portno_names && verbose && is_exact) {
2172 char *name = odp_portno_names_get(portno_names,
2173 u32_to_odp(nl_attr_get_u32(a)));
2175 ds_put_format(ds, "%s", name);
2177 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2180 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2182 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2187 case OVS_KEY_ATTR_ETHERNET: {
2188 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2189 const struct ovs_key_ethernet *key = nl_attr_get(a);
2191 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2192 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2196 case OVS_KEY_ATTR_VLAN:
2197 format_vlan_tci(ds, nl_attr_get_be16(a),
2198 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2201 case OVS_KEY_ATTR_MPLS: {
2202 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2203 const struct ovs_key_mpls *mpls_mask = NULL;
2204 size_t size = nl_attr_get_size(a);
2206 if (!size || size % sizeof *mpls_key) {
2207 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2211 mpls_mask = nl_attr_get(ma);
2212 if (size != nl_attr_get_size(ma)) {
2213 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2214 "mask length %"PRIuSIZE")",
2215 size, nl_attr_get_size(ma));
2219 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2222 case OVS_KEY_ATTR_ETHERTYPE:
2223 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2225 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2229 case OVS_KEY_ATTR_IPV4: {
2230 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2231 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2233 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2234 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2235 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2237 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2238 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2239 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2244 case OVS_KEY_ATTR_IPV6: {
2245 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2246 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2248 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2249 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2250 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2252 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2254 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2256 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2258 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2263 /* These have the same structure and format. */
2264 case OVS_KEY_ATTR_TCP:
2265 case OVS_KEY_ATTR_UDP:
2266 case OVS_KEY_ATTR_SCTP: {
2267 const struct ovs_key_tcp *key = nl_attr_get(a);
2268 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2270 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2271 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2275 case OVS_KEY_ATTR_TCP_FLAGS:
2277 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2278 ntohs(nl_attr_get_be16(a)),
2279 ntohs(nl_attr_get_be16(ma)));
2281 format_flags(ds, packet_tcp_flag_to_string,
2282 ntohs(nl_attr_get_be16(a)), ',');
2286 case OVS_KEY_ATTR_ICMP: {
2287 const struct ovs_key_icmp *key = nl_attr_get(a);
2288 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2290 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2291 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2295 case OVS_KEY_ATTR_ICMPV6: {
2296 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2297 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2299 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2301 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2306 case OVS_KEY_ATTR_ARP: {
2307 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2308 const struct ovs_key_arp *key = nl_attr_get(a);
2310 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2311 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2312 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2313 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2314 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2318 case OVS_KEY_ATTR_ND: {
2319 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2320 const struct ovs_key_nd *key = nl_attr_get(a);
2322 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2324 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2325 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2330 case OVS_KEY_ATTR_UNSPEC:
2331 case __OVS_KEY_ATTR_MAX:
2333 format_generic_odp_key(a, ds);
2335 ds_put_char(ds, '/');
2336 format_generic_odp_key(ma, ds);
2340 ds_put_char(ds, ')');
2343 static struct nlattr *
2344 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2345 struct ofpbuf *ofp, const struct nlattr *key)
2347 const struct nlattr *a;
2349 int type = nl_attr_type(key);
2350 int size = nl_attr_get_size(key);
2352 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2353 nl_msg_put_unspec_zero(ofp, type, size);
2357 if (tbl[type].next) {
2358 tbl = tbl[type].next;
2359 max = tbl[type].next_max;
2362 nested_mask = nl_msg_start_nested(ofp, type);
2363 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2364 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2366 nl_msg_end_nested(ofp, nested_mask);
2373 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2377 if (ovs_scan(s, "ufid:")) {
2380 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2392 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2394 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2397 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2398 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2399 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2400 * non-null and 'verbose' is true, translates odp port number to its name. */
2402 odp_flow_format(const struct nlattr *key, size_t key_len,
2403 const struct nlattr *mask, size_t mask_len,
2404 const struct hmap *portno_names, struct ds *ds, bool verbose)
2407 const struct nlattr *a;
2409 bool has_ethtype_key = false;
2410 const struct nlattr *ma = NULL;
2412 bool first_field = true;
2414 ofpbuf_init(&ofp, 100);
2415 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2416 bool is_nested_attr;
2417 bool is_wildcard = false;
2418 int attr_type = nl_attr_type(a);
2420 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2421 has_ethtype_key = true;
2424 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2425 OVS_KEY_ATTR_MAX, attr_type) ==
2428 if (mask && mask_len) {
2429 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2430 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2433 if (verbose || !is_wildcard || is_nested_attr) {
2434 if (is_wildcard && !ma) {
2435 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2440 ds_put_char(ds, ',');
2442 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2443 first_field = false;
2447 ofpbuf_uninit(&ofp);
2452 if (left == key_len) {
2453 ds_put_cstr(ds, "<empty>");
2455 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2456 for (i = 0; i < left; i++) {
2457 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2459 ds_put_char(ds, ')');
2461 if (!has_ethtype_key) {
2462 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2464 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2465 ntohs(nl_attr_get_be16(ma)));
2469 ds_put_cstr(ds, "<empty>");
2473 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2474 * OVS_KEY_ATTR_* attributes in 'key'. */
2476 odp_flow_key_format(const struct nlattr *key,
2477 size_t key_len, struct ds *ds)
2479 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2483 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2485 if (!strcasecmp(s, "no")) {
2486 *type = OVS_FRAG_TYPE_NONE;
2487 } else if (!strcasecmp(s, "first")) {
2488 *type = OVS_FRAG_TYPE_FIRST;
2489 } else if (!strcasecmp(s, "later")) {
2490 *type = OVS_FRAG_TYPE_LATER;
2500 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2501 uint8_t (*mask)[ETH_ADDR_LEN])
2505 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2509 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2510 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2513 memset(mask, 0xff, sizeof *mask);
2522 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2526 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2530 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2531 IP_SCAN_ARGS(mask), &n)) {
2534 *mask = OVS_BE32_MAX;
2543 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2546 char ipv6_s[IPV6_SCAN_LEN + 1];
2548 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2549 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2553 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2554 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2557 memset(mask, 0xff, sizeof *mask);
2566 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2571 if (ovs_scan(s, "%i%n", &key_, &n)
2572 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2577 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2578 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2580 *mask = htonl(mask_);
2582 *mask = htonl(IPV6_LABEL_MASK);
2591 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2595 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2599 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2611 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2615 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2619 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2631 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2633 uint16_t key_, mask_;
2636 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2641 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2643 *mask = htons(mask_);
2645 *mask = OVS_BE16_MAX;
2654 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2656 uint64_t key_, mask_;
2659 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2662 *key = htonll(key_);
2664 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2666 *mask = htonll(mask_);
2668 *mask = OVS_BE64_MAX;
2677 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2679 uint32_t flags, fmask;
2682 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2683 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2684 if (n >= 0 && s[n] == ')') {
2695 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2697 uint32_t flags, fmask;
2700 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2701 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2703 *key = htons(flags);
2705 *mask = htons(fmask);
2713 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2717 enum ovs_frag_type frag_type;
2719 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2720 && ovs_frag_type_from_string(frag, &frag_type)) {
2733 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2734 const struct simap *port_names)
2738 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2742 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2749 } else if (port_names) {
2750 const struct simap_node *node;
2753 len = strcspn(s, ")");
2754 node = simap_find_len(port_names, s, len);
2767 /* Helper for vlan parsing. */
2768 struct ovs_key_vlan__ {
2773 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2775 const uint16_t mask = ((1U << bits) - 1) << offset;
2777 if (value >> bits) {
2781 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2786 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2789 uint16_t key_, mask_;
2792 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2795 if (set_be16_bf(key, bits, offset, key_)) {
2797 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2800 if (!set_be16_bf(mask, bits, offset, mask_)) {
2804 *mask |= htons(((1U << bits) - 1) << offset);
2814 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2816 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2820 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2822 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2826 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2828 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2833 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2835 const uint32_t mask = ((1U << bits) - 1) << offset;
2837 if (value >> bits) {
2841 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2846 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2849 uint32_t key_, mask_;
2852 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2855 if (set_be32_bf(key, bits, offset, key_)) {
2857 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2860 if (!set_be32_bf(mask, bits, offset, mask_)) {
2864 *mask |= htonl(((1U << bits) - 1) << offset);
2874 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2876 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2880 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2882 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2886 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2888 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2892 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2894 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2898 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2900 const char *s_base = s;
2901 ovs_be16 id = 0, id_mask = 0;
2902 uint8_t flags = 0, flags_mask = 0;
2904 if (!strncmp(s, "id=", 3)) {
2906 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2912 if (!strncmp(s, "flags=", 6)) {
2914 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2917 if (!strncmp(s, "))", 2)) {
2920 *key = (flags << 16) | ntohs(id);
2922 *mask = (flags_mask << 16) | ntohs(id_mask);
2932 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2934 const char *s_base = s;
2935 struct geneve_opt *opt = key->d;
2936 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2937 int len_remain = sizeof key->d;
2939 while (s[0] == '{' && len_remain >= sizeof *opt) {
2943 len_remain -= sizeof *opt;
2945 if (!strncmp(s, "class=", 6)) {
2947 s += scan_be16(s, &opt->opt_class,
2948 mask ? &opt_mask->opt_class : NULL);
2950 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2956 if (!strncmp(s, "type=", 5)) {
2958 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2960 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2966 if (!strncmp(s, "len=", 4)) {
2967 uint8_t opt_len, opt_len_mask;
2969 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2971 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2974 opt->length = opt_len / 4;
2976 opt_mask->length = opt_len_mask;
2980 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2986 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2993 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2994 data_len, (char **)&s)) {
3005 opt += 1 + data_len / 4;
3007 opt_mask += 1 + data_len / 4;
3009 len_remain -= data_len;
3014 int len = sizeof key->d - len_remain;
3028 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3030 const uint16_t *flags = data_;
3032 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3033 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3035 if (*flags & FLOW_TNL_F_CSUM) {
3036 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3038 if (*flags & FLOW_TNL_F_OAM) {
3039 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3044 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3046 const uint32_t *gbp = data_;
3049 size_t vxlan_opts_ofs;
3051 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3052 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3053 nl_msg_end_nested(a, vxlan_opts_ofs);
3058 geneve_to_attr(struct ofpbuf *a, const void *data_)
3060 const struct geneve_scan *geneve = data_;
3062 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3066 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3068 unsigned long call_fn = (unsigned long)FUNC; \
3070 typedef void (*fn)(struct ofpbuf *, const void *); \
3072 func(BUF, &(DATA)); \
3074 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3078 #define SCAN_IF(NAME) \
3079 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3080 const char *start = s; \
3085 /* Usually no special initialization is needed. */
3086 #define SCAN_BEGIN(NAME, TYPE) \
3089 memset(&skey, 0, sizeof skey); \
3090 memset(&smask, 0, sizeof smask); \
3094 /* Init as fully-masked as mask will not be scanned. */
3095 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3098 memset(&skey, 0, sizeof skey); \
3099 memset(&smask, 0xff, sizeof smask); \
3103 /* VLAN needs special initialization. */
3104 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3106 TYPE skey = KEY_INIT; \
3107 TYPE smask = MASK_INIT; \
3111 /* Scan unnamed entry as 'TYPE' */
3112 #define SCAN_TYPE(TYPE, KEY, MASK) \
3113 len = scan_##TYPE(s, KEY, MASK); \
3119 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3120 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3121 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3122 s += strlen(NAME); \
3123 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3127 #define SCAN_FINISH() \
3128 } while (*s++ == ',' && len != 0); \
3129 if (s[-1] != ')') { \
3133 #define SCAN_FINISH_SINGLE() \
3135 if (*s++ != ')') { \
3139 /* Beginning of nested attribute. */
3140 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3142 size_t key_offset, mask_offset; \
3143 key_offset = nl_msg_start_nested(key, ATTR); \
3145 mask_offset = nl_msg_start_nested(mask, ATTR); \
3150 #define SCAN_END_NESTED() \
3152 nl_msg_end_nested(key, key_offset); \
3154 nl_msg_end_nested(mask, mask_offset); \
3159 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3160 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3162 memset(&skey, 0, sizeof skey); \
3163 memset(&smask, 0xff, sizeof smask); \
3164 s += strlen(NAME); \
3165 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3166 SCAN_PUT(ATTR, FUNC); \
3170 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3171 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3173 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3174 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3176 #define SCAN_PUT(ATTR, FUNC) \
3177 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3178 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3180 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3184 #define SCAN_END(ATTR) \
3186 SCAN_PUT(ATTR, NULL); \
3190 #define SCAN_END_SINGLE(ATTR) \
3191 SCAN_FINISH_SINGLE(); \
3192 SCAN_PUT(ATTR, NULL); \
3196 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3197 SCAN_BEGIN(NAME, TYPE) { \
3198 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3199 } SCAN_END_SINGLE(ATTR)
3201 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3202 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3203 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3204 } SCAN_END_SINGLE(ATTR)
3206 /* scan_port needs one extra argument. */
3207 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3208 SCAN_BEGIN(NAME, TYPE) { \
3209 len = scan_port(s, &skey, &smask, port_names); \
3214 } SCAN_END_SINGLE(ATTR)
3217 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3218 struct ofpbuf *key, struct ofpbuf *mask)
3224 len = odp_ufid_from_string(s, &ufid);
3229 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3230 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3231 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3232 OVS_KEY_ATTR_RECIRC_ID);
3233 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3235 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3236 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3237 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3238 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3239 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3240 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3241 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3242 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3243 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3244 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3246 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3247 } SCAN_END_NESTED();
3249 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3251 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3252 SCAN_FIELD("src=", eth, eth_src);
3253 SCAN_FIELD("dst=", eth, eth_dst);
3254 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3256 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3257 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3258 SCAN_FIELD("vid=", vid, tci);
3259 SCAN_FIELD("pcp=", pcp, tci);
3260 SCAN_FIELD("cfi=", cfi, tci);
3261 } SCAN_END(OVS_KEY_ATTR_VLAN);
3263 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3265 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3266 SCAN_FIELD("label=", mpls_label, mpls_lse);
3267 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3268 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3269 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3270 } SCAN_END(OVS_KEY_ATTR_MPLS);
3272 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3273 SCAN_FIELD("src=", ipv4, ipv4_src);
3274 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3275 SCAN_FIELD("proto=", u8, ipv4_proto);
3276 SCAN_FIELD("tos=", u8, ipv4_tos);
3277 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3278 SCAN_FIELD("frag=", frag, ipv4_frag);
3279 } SCAN_END(OVS_KEY_ATTR_IPV4);
3281 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3282 SCAN_FIELD("src=", ipv6, ipv6_src);
3283 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3284 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3285 SCAN_FIELD("proto=", u8, ipv6_proto);
3286 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3287 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3288 SCAN_FIELD("frag=", frag, ipv6_frag);
3289 } SCAN_END(OVS_KEY_ATTR_IPV6);
3291 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3292 SCAN_FIELD("src=", be16, tcp_src);
3293 SCAN_FIELD("dst=", be16, tcp_dst);
3294 } SCAN_END(OVS_KEY_ATTR_TCP);
3296 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3298 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3299 SCAN_FIELD("src=", be16, udp_src);
3300 SCAN_FIELD("dst=", be16, udp_dst);
3301 } SCAN_END(OVS_KEY_ATTR_UDP);
3303 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3304 SCAN_FIELD("src=", be16, sctp_src);
3305 SCAN_FIELD("dst=", be16, sctp_dst);
3306 } SCAN_END(OVS_KEY_ATTR_SCTP);
3308 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3309 SCAN_FIELD("type=", u8, icmp_type);
3310 SCAN_FIELD("code=", u8, icmp_code);
3311 } SCAN_END(OVS_KEY_ATTR_ICMP);
3313 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3314 SCAN_FIELD("type=", u8, icmpv6_type);
3315 SCAN_FIELD("code=", u8, icmpv6_code);
3316 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3318 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3319 SCAN_FIELD("sip=", ipv4, arp_sip);
3320 SCAN_FIELD("tip=", ipv4, arp_tip);
3321 SCAN_FIELD("op=", be16, arp_op);
3322 SCAN_FIELD("sha=", eth, arp_sha);
3323 SCAN_FIELD("tha=", eth, arp_tha);
3324 } SCAN_END(OVS_KEY_ATTR_ARP);
3326 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3327 SCAN_FIELD("target=", ipv6, nd_target);
3328 SCAN_FIELD("sll=", eth, nd_sll);
3329 SCAN_FIELD("tll=", eth, nd_tll);
3330 } SCAN_END(OVS_KEY_ATTR_ND);
3332 /* Encap open-coded. */
3333 if (!strncmp(s, "encap(", 6)) {
3334 const char *start = s;
3335 size_t encap, encap_mask = 0;
3337 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3339 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3346 s += strspn(s, delimiters);
3349 } else if (*s == ')') {
3353 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3361 nl_msg_end_nested(key, encap);
3363 nl_msg_end_nested(mask, encap_mask);
3372 /* Parses the string representation of a datapath flow key, in the
3373 * format output by odp_flow_key_format(). Returns 0 if successful,
3374 * otherwise a positive errno value. On success, the flow key is
3375 * appended to 'key' as a series of Netlink attributes. On failure, no
3376 * data is appended to 'key'. Either way, 'key''s data might be
3379 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3380 * to a port number. (Port names may be used instead of port numbers in
3383 * On success, the attributes appended to 'key' are individually syntactically
3384 * valid, but they may not be valid as a sequence. 'key' might, for example,
3385 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3387 odp_flow_from_string(const char *s, const struct simap *port_names,
3388 struct ofpbuf *key, struct ofpbuf *mask)
3390 const size_t old_size = key->size;
3394 s += strspn(s, delimiters);
3399 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3401 key->size = old_size;
3411 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3414 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3415 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3416 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3417 * must use a zero mask for the netlink frag field, and all ones mask
3419 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3421 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3422 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3423 : OVS_FRAG_TYPE_FIRST;
3426 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3427 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3428 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3430 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3432 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3434 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3436 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3437 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3438 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3439 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3441 /* These share the same layout. */
3443 struct ovs_key_tcp tcp;
3444 struct ovs_key_udp udp;
3445 struct ovs_key_sctp sctp;
3448 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3449 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3452 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3453 bool export_mask, struct ofpbuf *buf)
3455 struct ovs_key_ethernet *eth_key;
3457 const struct flow *flow = parms->flow;
3458 const struct flow *data = export_mask ? parms->mask : parms->flow;
3460 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3462 if (flow->tunnel.ip_dst || export_mask) {
3463 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3467 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3469 if (parms->recirc) {
3470 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3471 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3474 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3475 * is not the magical value "ODPP_NONE". */
3476 if (export_mask || parms->odp_in_port != ODPP_NONE) {
3477 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3480 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3482 get_ethernet_key(data, eth_key);
3484 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3486 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3488 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3490 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3491 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3492 if (flow->vlan_tci == htons(0)) {
3499 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3500 /* For backwards compatibility with kernels that don't support
3501 * wildcarding, the following convention is used to encode the
3502 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3505 * -------- -------- -------
3506 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3507 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3508 * <none> 0xffff Any non-Ethernet II frame (except valid
3509 * 802.3 SNAP packet with valid eth_type).
3512 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3517 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3519 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3520 struct ovs_key_ipv4 *ipv4_key;
3522 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3524 get_ipv4_key(data, ipv4_key, export_mask);
3525 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3526 struct ovs_key_ipv6 *ipv6_key;
3528 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3530 get_ipv6_key(data, ipv6_key, export_mask);
3531 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3532 flow->dl_type == htons(ETH_TYPE_RARP)) {
3533 struct ovs_key_arp *arp_key;
3535 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3537 get_arp_key(data, arp_key);
3538 } else if (eth_type_mpls(flow->dl_type)) {
3539 struct ovs_key_mpls *mpls_key;
3542 n = flow_count_mpls_labels(flow, NULL);
3544 n = MIN(n, parms->max_mpls_depth);
3546 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3547 n * sizeof *mpls_key);
3548 for (i = 0; i < n; i++) {
3549 mpls_key[i].mpls_lse = data->mpls_lse[i];
3553 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3554 if (flow->nw_proto == IPPROTO_TCP) {
3555 union ovs_key_tp *tcp_key;
3557 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3559 get_tp_key(data, tcp_key);
3560 if (data->tcp_flags) {
3561 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3563 } else if (flow->nw_proto == IPPROTO_UDP) {
3564 union ovs_key_tp *udp_key;
3566 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3568 get_tp_key(data, udp_key);
3569 } else if (flow->nw_proto == IPPROTO_SCTP) {
3570 union ovs_key_tp *sctp_key;
3572 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3574 get_tp_key(data, sctp_key);
3575 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3576 && flow->nw_proto == IPPROTO_ICMP) {
3577 struct ovs_key_icmp *icmp_key;
3579 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3581 icmp_key->icmp_type = ntohs(data->tp_src);
3582 icmp_key->icmp_code = ntohs(data->tp_dst);
3583 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3584 && flow->nw_proto == IPPROTO_ICMPV6) {
3585 struct ovs_key_icmpv6 *icmpv6_key;
3587 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3588 sizeof *icmpv6_key);
3589 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3590 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3592 if (flow->tp_dst == htons(0)
3593 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3594 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3595 && (!export_mask || (data->tp_src == htons(0xffff)
3596 && data->tp_dst == htons(0xffff)))) {
3598 struct ovs_key_nd *nd_key;
3600 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3602 memcpy(nd_key->nd_target, &data->nd_target,
3603 sizeof nd_key->nd_target);
3604 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3605 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3612 nl_msg_end_nested(buf, encap);
3616 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3618 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3619 * capable of being expanded to allow for that much space. */
3621 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3624 odp_flow_key_from_flow__(parms, false, buf);
3627 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3630 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3631 * capable of being expanded to allow for that much space. */
3633 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3636 odp_flow_key_from_flow__(parms, true, buf);
3639 /* Generate ODP flow key from the given packet metadata */
3641 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3643 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3645 if (md->tunnel.ip_dst) {
3646 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3649 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3651 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3652 * value "ODPP_NONE". */
3653 if (md->in_port.odp_port != ODPP_NONE) {
3654 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3658 /* Generate packet metadata from the given ODP flow key. */
3660 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3661 struct pkt_metadata *md)
3663 const struct nlattr *nla;
3665 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3666 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3667 1u << OVS_KEY_ATTR_IN_PORT;
3669 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3671 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3672 uint16_t type = nl_attr_type(nla);
3673 size_t len = nl_attr_get_size(nla);
3674 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3675 OVS_KEY_ATTR_MAX, type);
3677 if (len != expected_len && expected_len >= 0) {
3682 case OVS_KEY_ATTR_RECIRC_ID:
3683 md->recirc_id = nl_attr_get_u32(nla);
3684 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3686 case OVS_KEY_ATTR_DP_HASH:
3687 md->dp_hash = nl_attr_get_u32(nla);
3688 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3690 case OVS_KEY_ATTR_PRIORITY:
3691 md->skb_priority = nl_attr_get_u32(nla);
3692 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3694 case OVS_KEY_ATTR_SKB_MARK:
3695 md->pkt_mark = nl_attr_get_u32(nla);
3696 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3698 case OVS_KEY_ATTR_TUNNEL: {
3699 enum odp_key_fitness res;
3701 res = odp_tun_key_from_attr(nla, &md->tunnel);
3702 if (res == ODP_FIT_ERROR) {
3703 memset(&md->tunnel, 0, sizeof md->tunnel);
3704 } else if (res == ODP_FIT_PERFECT) {
3705 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3709 case OVS_KEY_ATTR_IN_PORT:
3710 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3711 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3717 if (!wanted_attrs) {
3718 return; /* Have everything. */
3724 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3726 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3727 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3728 key_len / sizeof(uint32_t), 0);
3732 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3733 uint64_t attrs, int out_of_range_attr,
3734 const struct nlattr *key, size_t key_len)
3739 if (VLOG_DROP_DBG(rl)) {
3744 for (i = 0; i < 64; i++) {
3745 if (attrs & (UINT64_C(1) << i)) {
3746 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3748 ds_put_format(&s, " %s",
3749 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3752 if (out_of_range_attr) {
3753 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3756 ds_put_cstr(&s, ": ");
3757 odp_flow_key_format(key, key_len, &s);
3759 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3764 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3766 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3769 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3772 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3773 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3774 return 0xff; /* Error. */
3777 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3778 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3779 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3783 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3784 const struct nlattr *attrs[], uint64_t *present_attrsp,
3785 int *out_of_range_attrp)
3787 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3788 const struct nlattr *nla;
3789 uint64_t present_attrs;
3792 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3794 *out_of_range_attrp = 0;
3795 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3796 uint16_t type = nl_attr_type(nla);
3797 size_t len = nl_attr_get_size(nla);
3798 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3799 OVS_KEY_ATTR_MAX, type);
3801 if (len != expected_len && expected_len >= 0) {
3802 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3804 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3805 "length %d", ovs_key_attr_to_string(type, namebuf,
3811 if (type > OVS_KEY_ATTR_MAX) {
3812 *out_of_range_attrp = type;
3814 if (present_attrs & (UINT64_C(1) << type)) {
3815 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3817 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3818 ovs_key_attr_to_string(type,
3819 namebuf, sizeof namebuf));
3823 present_attrs |= UINT64_C(1) << type;
3828 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3832 *present_attrsp = present_attrs;
3836 static enum odp_key_fitness
3837 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3838 uint64_t expected_attrs,
3839 const struct nlattr *key, size_t key_len)
3841 uint64_t missing_attrs;
3842 uint64_t extra_attrs;
3844 missing_attrs = expected_attrs & ~present_attrs;
3845 if (missing_attrs) {
3846 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3847 log_odp_key_attributes(&rl, "expected but not present",
3848 missing_attrs, 0, key, key_len);
3849 return ODP_FIT_TOO_LITTLE;
3852 extra_attrs = present_attrs & ~expected_attrs;
3853 if (extra_attrs || out_of_range_attr) {
3854 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3855 log_odp_key_attributes(&rl, "present but not expected",
3856 extra_attrs, out_of_range_attr, key, key_len);
3857 return ODP_FIT_TOO_MUCH;
3860 return ODP_FIT_PERFECT;
3864 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3865 uint64_t present_attrs, uint64_t *expected_attrs,
3866 struct flow *flow, const struct flow *src_flow)
3868 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3869 bool is_mask = flow != src_flow;
3871 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3872 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3873 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3874 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3875 ntohs(flow->dl_type));
3878 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3879 flow->dl_type != htons(0xffff)) {
3882 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3885 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3886 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3887 /* See comments in odp_flow_key_from_flow__(). */
3888 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3895 static enum odp_key_fitness
3896 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3897 uint64_t present_attrs, int out_of_range_attr,
3898 uint64_t expected_attrs, struct flow *flow,
3899 const struct nlattr *key, size_t key_len,
3900 const struct flow *src_flow)
3902 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3903 bool is_mask = src_flow != flow;
3904 const void *check_start = NULL;
3905 size_t check_len = 0;
3906 enum ovs_key_attr expected_bit = 0xff;
3908 if (eth_type_mpls(src_flow->dl_type)) {
3909 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3910 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3912 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3913 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3914 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3915 int n = size / sizeof(ovs_be32);
3918 if (!size || size % sizeof(ovs_be32)) {
3919 return ODP_FIT_ERROR;
3921 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3922 return ODP_FIT_ERROR;
3925 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3926 flow->mpls_lse[i] = mpls_lse[i];
3928 if (n > FLOW_MAX_MPLS_LABELS) {
3929 return ODP_FIT_TOO_MUCH;
3933 /* BOS may be set only in the innermost label. */
3934 for (i = 0; i < n - 1; i++) {
3935 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3936 return ODP_FIT_ERROR;
3940 /* BOS must be set in the innermost label. */
3941 if (n < FLOW_MAX_MPLS_LABELS
3942 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3943 return ODP_FIT_TOO_LITTLE;
3949 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3951 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3953 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3954 const struct ovs_key_ipv4 *ipv4_key;
3956 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3957 put_ipv4_key(ipv4_key, flow, is_mask);
3958 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3959 return ODP_FIT_ERROR;
3962 check_start = ipv4_key;
3963 check_len = sizeof *ipv4_key;
3964 expected_bit = OVS_KEY_ATTR_IPV4;
3967 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3969 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3971 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3972 const struct ovs_key_ipv6 *ipv6_key;
3974 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3975 put_ipv6_key(ipv6_key, flow, is_mask);
3976 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3977 return ODP_FIT_ERROR;
3980 check_start = ipv6_key;
3981 check_len = sizeof *ipv6_key;
3982 expected_bit = OVS_KEY_ATTR_IPV6;
3985 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3986 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3988 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3990 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3991 const struct ovs_key_arp *arp_key;
3993 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3994 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3995 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3996 "key", ntohs(arp_key->arp_op));
3997 return ODP_FIT_ERROR;
3999 put_arp_key(arp_key, flow);
4001 check_start = arp_key;
4002 check_len = sizeof *arp_key;
4003 expected_bit = OVS_KEY_ATTR_ARP;
4009 if (check_len > 0) { /* Happens only when 'is_mask'. */
4010 if (!is_all_zeros(check_start, check_len) &&
4011 flow->dl_type != htons(0xffff)) {
4012 return ODP_FIT_ERROR;
4014 expected_attrs |= UINT64_C(1) << expected_bit;
4018 expected_bit = OVS_KEY_ATTR_UNSPEC;
4019 if (src_flow->nw_proto == IPPROTO_TCP
4020 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4021 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4022 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4024 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4026 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4027 const union ovs_key_tp *tcp_key;
4029 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4030 put_tp_key(tcp_key, flow);
4031 expected_bit = OVS_KEY_ATTR_TCP;
4033 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4034 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4035 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4037 } else if (src_flow->nw_proto == IPPROTO_UDP
4038 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4039 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4040 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4042 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4044 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4045 const union ovs_key_tp *udp_key;
4047 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4048 put_tp_key(udp_key, flow);
4049 expected_bit = OVS_KEY_ATTR_UDP;
4051 } else if (src_flow->nw_proto == IPPROTO_SCTP
4052 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4053 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4054 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4056 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4058 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4059 const union ovs_key_tp *sctp_key;
4061 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4062 put_tp_key(sctp_key, flow);
4063 expected_bit = OVS_KEY_ATTR_SCTP;
4065 } else if (src_flow->nw_proto == IPPROTO_ICMP
4066 && src_flow->dl_type == htons(ETH_TYPE_IP)
4067 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4069 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4071 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4072 const struct ovs_key_icmp *icmp_key;
4074 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4075 flow->tp_src = htons(icmp_key->icmp_type);
4076 flow->tp_dst = htons(icmp_key->icmp_code);
4077 expected_bit = OVS_KEY_ATTR_ICMP;
4079 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4080 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4081 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4083 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4085 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4086 const struct ovs_key_icmpv6 *icmpv6_key;
4088 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4089 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4090 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4091 expected_bit = OVS_KEY_ATTR_ICMPV6;
4092 if (src_flow->tp_dst == htons(0) &&
4093 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4094 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4096 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4098 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4099 const struct ovs_key_nd *nd_key;
4101 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4102 memcpy(&flow->nd_target, nd_key->nd_target,
4103 sizeof flow->nd_target);
4104 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4105 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4107 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4108 (flow->tp_src != htons(0xffff) ||
4109 flow->tp_dst != htons(0xffff))) {
4110 return ODP_FIT_ERROR;
4112 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4119 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4120 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4121 return ODP_FIT_ERROR;
4123 expected_attrs |= UINT64_C(1) << expected_bit;
4128 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4132 /* Parse 802.1Q header then encapsulated L3 attributes. */
4133 static enum odp_key_fitness
4134 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4135 uint64_t present_attrs, int out_of_range_attr,
4136 uint64_t expected_attrs, struct flow *flow,
4137 const struct nlattr *key, size_t key_len,
4138 const struct flow *src_flow)
4140 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4141 bool is_mask = src_flow != flow;
4143 const struct nlattr *encap
4144 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4145 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4146 enum odp_key_fitness encap_fitness;
4147 enum odp_key_fitness fitness;
4149 /* Calculate fitness of outer attributes. */
4151 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4152 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4154 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4155 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4157 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4158 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4161 fitness = check_expectations(present_attrs, out_of_range_attr,
4162 expected_attrs, key, key_len);
4165 * Remove the TPID from dl_type since it's not the real Ethertype. */
4166 flow->dl_type = htons(0);
4167 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4168 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4171 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4172 return ODP_FIT_TOO_LITTLE;
4173 } else if (flow->vlan_tci == htons(0)) {
4174 /* Corner case for a truncated 802.1Q header. */
4175 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4176 return ODP_FIT_TOO_MUCH;
4179 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4180 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4181 "but CFI bit is not set", ntohs(flow->vlan_tci));
4182 return ODP_FIT_ERROR;
4185 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4190 /* Now parse the encapsulated attributes. */
4191 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4192 attrs, &present_attrs, &out_of_range_attr)) {
4193 return ODP_FIT_ERROR;
4197 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4198 return ODP_FIT_ERROR;
4200 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4201 expected_attrs, flow, key, key_len,
4204 /* The overall fitness is the worse of the outer and inner attributes. */
4205 return MAX(fitness, encap_fitness);
4208 static enum odp_key_fitness
4209 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4210 const struct nlattr *src_key, size_t src_key_len,
4211 struct flow *flow, const struct flow *src_flow)
4213 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4214 uint64_t expected_attrs;
4215 uint64_t present_attrs;
4216 int out_of_range_attr;
4217 bool is_mask = src_flow != flow;
4219 memset(flow, 0, sizeof *flow);
4221 /* Parse attributes. */
4222 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4223 &out_of_range_attr)) {
4224 return ODP_FIT_ERROR;
4229 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4230 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4231 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4232 } else if (is_mask) {
4233 /* Always exact match recirc_id if it is not specified. */
4234 flow->recirc_id = UINT32_MAX;
4237 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4238 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4239 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4241 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4242 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4243 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4246 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4247 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4248 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4251 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4252 enum odp_key_fitness res;
4254 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], src_key,
4255 src_key_len, &src_flow->tunnel,
4257 if (res == ODP_FIT_ERROR) {
4258 return ODP_FIT_ERROR;
4259 } else if (res == ODP_FIT_PERFECT) {
4260 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4264 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4265 flow->in_port.odp_port
4266 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4267 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4268 } else if (!is_mask) {
4269 flow->in_port.odp_port = ODPP_NONE;
4272 /* Ethernet header. */
4273 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4274 const struct ovs_key_ethernet *eth_key;
4276 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4277 put_ethernet_key(eth_key, flow);
4279 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4283 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4286 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4287 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4289 return ODP_FIT_ERROR;
4293 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4294 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4295 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4296 expected_attrs, flow, key, key_len, src_flow);
4299 flow->vlan_tci = htons(0xffff);
4300 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4301 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4302 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4305 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4306 expected_attrs, flow, key, key_len, src_flow);
4309 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4310 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4311 * 'key' fits our expectations for what a flow key should contain.
4313 * The 'in_port' will be the datapath's understanding of the port. The
4314 * caller will need to translate with odp_port_to_ofp_port() if the
4315 * OpenFlow port is needed.
4317 * This function doesn't take the packet itself as an argument because none of
4318 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4319 * it is always possible to infer which additional attribute(s) should appear
4320 * by looking at the attributes for lower-level protocols, e.g. if the network
4321 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4322 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4323 * must be absent. */
4324 enum odp_key_fitness
4325 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4328 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow);
4331 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4332 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
4333 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4334 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
4335 * well 'key' fits our expectations for what a flow key should contain. */
4336 enum odp_key_fitness
4337 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4338 const struct nlattr *flow_key, size_t flow_key_len,
4339 struct flow *mask, const struct flow *flow)
4341 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4345 /* Returns 'fitness' as a string, for use in debug messages. */
4347 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4350 case ODP_FIT_PERFECT:
4352 case ODP_FIT_TOO_MUCH:
4354 case ODP_FIT_TOO_LITTLE:
4355 return "too_little";
4363 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4364 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4365 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4366 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4367 * null, then the return value is not meaningful.) */
4369 odp_put_userspace_action(uint32_t pid,
4370 const void *userdata, size_t userdata_size,
4371 odp_port_t tunnel_out_port,
4372 struct ofpbuf *odp_actions)
4374 size_t userdata_ofs;
4377 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4378 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4380 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4382 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4383 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4386 * - The kernel rejected shorter userdata with -ERANGE.
4388 * - The kernel silently dropped userdata beyond the first 8 bytes.
4390 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4391 * separately disable features that required more than 8 bytes.) */
4392 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4393 MAX(8, userdata_size)),
4394 userdata, userdata_size);
4398 if (tunnel_out_port != ODPP_NONE) {
4399 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4402 nl_msg_end_nested(odp_actions, offset);
4404 return userdata_ofs;
4408 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4409 struct ofpbuf *odp_actions)
4411 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4412 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4413 nl_msg_end_nested(odp_actions, offset);
4417 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4418 struct ovs_action_push_tnl *data)
4420 int size = offsetof(struct ovs_action_push_tnl, header);
4422 size += data->header_len;
4423 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4427 /* The commit_odp_actions() function and its helpers. */
4430 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4431 const void *key, size_t key_size)
4433 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4434 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4435 nl_msg_end_nested(odp_actions, offset);
4438 /* Masked set actions have a mask following the data within the netlink
4439 * attribute. The unmasked bits in the data will be cleared as the data
4440 * is copied to the action. */
4442 commit_masked_set_action(struct ofpbuf *odp_actions,
4443 enum ovs_key_attr key_type,
4444 const void *key_, const void *mask_, size_t key_size)
4446 size_t offset = nl_msg_start_nested(odp_actions,
4447 OVS_ACTION_ATTR_SET_MASKED);
4448 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4449 const char *key = key_, *mask = mask_;
4451 memcpy(data + key_size, mask, key_size);
4452 /* Clear unmasked bits while copying. */
4453 while (key_size--) {
4454 *data++ = *key++ & *mask++;
4456 nl_msg_end_nested(odp_actions, offset);
4459 /* If any of the flow key data that ODP actions can modify are different in
4460 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4461 * 'odp_actions' that change the flow tunneling information in key from
4462 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4463 * same way. In other words, operates the same as commit_odp_actions(), but
4464 * only on tunneling information. */
4466 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4467 struct ofpbuf *odp_actions)
4469 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4470 if (flow->tunnel.ip_dst) {
4471 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4474 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4475 odp_put_tunnel_action(&base->tunnel, odp_actions);
4480 commit(enum ovs_key_attr attr, bool use_masked_set,
4481 const void *key, void *base, void *mask, size_t size,
4482 struct ofpbuf *odp_actions)
4484 if (memcmp(key, base, size)) {
4485 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4487 if (use_masked_set && !fully_masked) {
4488 commit_masked_set_action(odp_actions, attr, key, mask, size);
4490 if (!fully_masked) {
4491 memset(mask, 0xff, size);
4493 commit_set_action(odp_actions, attr, key, size);
4495 memcpy(base, key, size);
4498 /* Mask bits are set when we have either read or set the corresponding
4499 * values. Masked bits will be exact-matched, no need to set them
4500 * if the value did not actually change. */
4506 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4508 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4509 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4513 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4515 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4516 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4520 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4521 struct ofpbuf *odp_actions,
4522 struct flow_wildcards *wc,
4525 struct ovs_key_ethernet key, base, mask;
4527 get_ethernet_key(flow, &key);
4528 get_ethernet_key(base_flow, &base);
4529 get_ethernet_key(&wc->masks, &mask);
4531 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4532 &key, &base, &mask, sizeof key, odp_actions)) {
4533 put_ethernet_key(&base, base_flow);
4534 put_ethernet_key(&mask, &wc->masks);
4539 pop_vlan(struct flow *base,
4540 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4542 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4544 if (base->vlan_tci & htons(VLAN_CFI)) {
4545 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4551 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4552 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4554 if (base->vlan_tci == vlan_tci) {
4558 pop_vlan(base, odp_actions, wc);
4559 if (vlan_tci & htons(VLAN_CFI)) {
4560 struct ovs_action_push_vlan vlan;
4562 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4563 vlan.vlan_tci = vlan_tci;
4564 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4565 &vlan, sizeof vlan);
4567 base->vlan_tci = vlan_tci;
4570 /* Wildcarding already done at action translation time. */
4572 commit_mpls_action(const struct flow *flow, struct flow *base,
4573 struct ofpbuf *odp_actions)
4575 int base_n = flow_count_mpls_labels(base, NULL);
4576 int flow_n = flow_count_mpls_labels(flow, NULL);
4577 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4580 while (base_n > common_n) {
4581 if (base_n - 1 == common_n && flow_n > common_n) {
4582 /* If there is only one more LSE in base than there are common
4583 * between base and flow; and flow has at least one more LSE than
4584 * is common then the topmost LSE of base may be updated using
4586 struct ovs_key_mpls mpls_key;
4588 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4589 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4590 &mpls_key, sizeof mpls_key);
4591 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4594 /* Otherwise, if there more LSEs in base than are common between
4595 * base and flow then pop the topmost one. */
4599 /* If all the LSEs are to be popped and this is not the outermost
4600 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4601 * POP_MPLS action instead of flow->dl_type.
4603 * This is because the POP_MPLS action requires its ethertype
4604 * argument to be an MPLS ethernet type but in this case
4605 * flow->dl_type will be a non-MPLS ethernet type.
4607 * When the final POP_MPLS action occurs it use flow->dl_type and
4608 * the and the resulting packet will have the desired dl_type. */
4609 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4610 dl_type = htons(ETH_TYPE_MPLS);
4612 dl_type = flow->dl_type;
4614 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4615 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4621 /* If, after the above popping and setting, there are more LSEs in flow
4622 * than base then some LSEs need to be pushed. */
4623 while (base_n < flow_n) {
4624 struct ovs_action_push_mpls *mpls;
4626 mpls = nl_msg_put_unspec_zero(odp_actions,
4627 OVS_ACTION_ATTR_PUSH_MPLS,
4629 mpls->mpls_ethertype = flow->dl_type;
4630 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4631 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4632 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4638 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4640 ipv4->ipv4_src = flow->nw_src;
4641 ipv4->ipv4_dst = flow->nw_dst;
4642 ipv4->ipv4_proto = flow->nw_proto;
4643 ipv4->ipv4_tos = flow->nw_tos;
4644 ipv4->ipv4_ttl = flow->nw_ttl;
4645 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4649 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4651 flow->nw_src = ipv4->ipv4_src;
4652 flow->nw_dst = ipv4->ipv4_dst;
4653 flow->nw_proto = ipv4->ipv4_proto;
4654 flow->nw_tos = ipv4->ipv4_tos;
4655 flow->nw_ttl = ipv4->ipv4_ttl;
4656 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4660 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4661 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4664 struct ovs_key_ipv4 key, mask, base;
4666 /* Check that nw_proto and nw_frag remain unchanged. */
4667 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4668 flow->nw_frag == base_flow->nw_frag);
4670 get_ipv4_key(flow, &key, false);
4671 get_ipv4_key(base_flow, &base, false);
4672 get_ipv4_key(&wc->masks, &mask, true);
4673 mask.ipv4_proto = 0; /* Not writeable. */
4674 mask.ipv4_frag = 0; /* Not writable. */
4676 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4678 put_ipv4_key(&base, base_flow, false);
4679 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4680 put_ipv4_key(&mask, &wc->masks, true);
4686 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4688 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4689 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4690 ipv6->ipv6_label = flow->ipv6_label;
4691 ipv6->ipv6_proto = flow->nw_proto;
4692 ipv6->ipv6_tclass = flow->nw_tos;
4693 ipv6->ipv6_hlimit = flow->nw_ttl;
4694 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4698 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4700 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4701 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4702 flow->ipv6_label = ipv6->ipv6_label;
4703 flow->nw_proto = ipv6->ipv6_proto;
4704 flow->nw_tos = ipv6->ipv6_tclass;
4705 flow->nw_ttl = ipv6->ipv6_hlimit;
4706 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4710 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4711 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4714 struct ovs_key_ipv6 key, mask, base;
4716 /* Check that nw_proto and nw_frag remain unchanged. */
4717 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4718 flow->nw_frag == base_flow->nw_frag);
4720 get_ipv6_key(flow, &key, false);
4721 get_ipv6_key(base_flow, &base, false);
4722 get_ipv6_key(&wc->masks, &mask, true);
4723 mask.ipv6_proto = 0; /* Not writeable. */
4724 mask.ipv6_frag = 0; /* Not writable. */
4726 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4728 put_ipv6_key(&base, base_flow, false);
4729 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4730 put_ipv6_key(&mask, &wc->masks, true);
4736 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4738 /* ARP key has padding, clear it. */
4739 memset(arp, 0, sizeof *arp);
4741 arp->arp_sip = flow->nw_src;
4742 arp->arp_tip = flow->nw_dst;
4743 arp->arp_op = htons(flow->nw_proto);
4744 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4745 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4749 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4751 flow->nw_src = arp->arp_sip;
4752 flow->nw_dst = arp->arp_tip;
4753 flow->nw_proto = ntohs(arp->arp_op);
4754 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4755 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4758 static enum slow_path_reason
4759 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4760 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4762 struct ovs_key_arp key, mask, base;
4764 get_arp_key(flow, &key);
4765 get_arp_key(base_flow, &base);
4766 get_arp_key(&wc->masks, &mask);
4768 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4770 put_arp_key(&base, base_flow);
4771 put_arp_key(&mask, &wc->masks);
4778 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4780 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4781 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4782 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4783 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4787 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4789 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4790 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4791 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4792 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4795 static enum slow_path_reason
4796 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4797 struct ofpbuf *odp_actions,
4798 struct flow_wildcards *wc, bool use_masked)
4800 struct ovs_key_nd key, mask, base;
4802 get_nd_key(flow, &key);
4803 get_nd_key(base_flow, &base);
4804 get_nd_key(&wc->masks, &mask);
4806 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4808 put_nd_key(&base, base_flow);
4809 put_nd_key(&mask, &wc->masks);
4816 static enum slow_path_reason
4817 commit_set_nw_action(const struct flow *flow, struct flow *base,
4818 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4821 /* Check if 'flow' really has an L3 header. */
4822 if (!flow->nw_proto) {
4826 switch (ntohs(base->dl_type)) {
4828 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4832 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4833 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4836 return commit_set_arp_action(flow, base, odp_actions, wc);
4842 /* TCP, UDP, and SCTP keys have the same layout. */
4843 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4844 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4847 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4849 tp->tcp.tcp_src = flow->tp_src;
4850 tp->tcp.tcp_dst = flow->tp_dst;
4854 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4856 flow->tp_src = tp->tcp.tcp_src;
4857 flow->tp_dst = tp->tcp.tcp_dst;
4861 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4862 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4865 enum ovs_key_attr key_type;
4866 union ovs_key_tp key, mask, base;
4868 /* Check if 'flow' really has an L3 header. */
4869 if (!flow->nw_proto) {
4873 if (!is_ip_any(base_flow)) {
4877 if (flow->nw_proto == IPPROTO_TCP) {
4878 key_type = OVS_KEY_ATTR_TCP;
4879 } else if (flow->nw_proto == IPPROTO_UDP) {
4880 key_type = OVS_KEY_ATTR_UDP;
4881 } else if (flow->nw_proto == IPPROTO_SCTP) {
4882 key_type = OVS_KEY_ATTR_SCTP;
4887 get_tp_key(flow, &key);
4888 get_tp_key(base_flow, &base);
4889 get_tp_key(&wc->masks, &mask);
4891 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4893 put_tp_key(&base, base_flow);
4894 put_tp_key(&mask, &wc->masks);
4899 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4900 struct ofpbuf *odp_actions,
4901 struct flow_wildcards *wc,
4904 uint32_t key, mask, base;
4906 key = flow->skb_priority;
4907 base = base_flow->skb_priority;
4908 mask = wc->masks.skb_priority;
4910 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4911 sizeof key, odp_actions)) {
4912 base_flow->skb_priority = base;
4913 wc->masks.skb_priority = mask;
4918 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4919 struct ofpbuf *odp_actions,
4920 struct flow_wildcards *wc,
4923 uint32_t key, mask, base;
4925 key = flow->pkt_mark;
4926 base = base_flow->pkt_mark;
4927 mask = wc->masks.pkt_mark;
4929 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4930 sizeof key, odp_actions)) {
4931 base_flow->pkt_mark = base;
4932 wc->masks.pkt_mark = mask;
4936 /* If any of the flow key data that ODP actions can modify are different in
4937 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4938 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4939 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4940 * in addition to this function if needed. Sets fields in 'wc' that are
4941 * used as part of the action.
4943 * Returns a reason to force processing the flow's packets into the userspace
4944 * slow path, if there is one, otherwise 0. */
4945 enum slow_path_reason
4946 commit_odp_actions(const struct flow *flow, struct flow *base,
4947 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4950 enum slow_path_reason slow;
4952 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4953 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4954 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4955 commit_mpls_action(flow, base, odp_actions);
4956 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4957 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4958 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);