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 memset(tun, 0, sizeof *tun);
1467 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun);
1471 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1472 const struct flow_tnl *tun_flow_key,
1473 const struct ofpbuf *key_buf)
1477 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1479 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1480 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1481 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1483 if (tun_key->ip_src) {
1484 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1486 if (tun_key->ip_dst) {
1487 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1489 if (tun_key->ip_tos) {
1490 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1492 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1493 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1494 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1496 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1497 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1499 if (tun_key->tp_src) {
1500 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1502 if (tun_key->tp_dst) {
1503 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1505 if (tun_key->flags & FLOW_TNL_F_OAM) {
1506 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1508 if (tun_key->gbp_flags || tun_key->gbp_id) {
1509 size_t vxlan_opts_ofs;
1511 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1512 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1513 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1514 nl_msg_end_nested(a, vxlan_opts_ofs);
1517 if (tun_key == tun_flow_key) {
1518 tun_metadata_to_geneve_nlattr_flow(&tun_key->metadata, a);
1520 tun_metadata_to_geneve_nlattr_mask(key_buf, &tun_key->metadata,
1521 &tun_flow_key->metadata, a);
1524 nl_msg_end_nested(a, tun_key_ofs);
1528 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1530 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1534 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1536 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1537 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1539 if (attr == OVS_KEY_ATTR_IPV6) {
1540 const struct ovs_key_ipv6 *ipv6_mask = mask;
1543 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1544 == htonl(IPV6_LABEL_MASK))
1545 && ipv6_mask->ipv6_proto == UINT8_MAX
1546 && ipv6_mask->ipv6_tclass == UINT8_MAX
1547 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1548 && ipv6_mask->ipv6_frag == UINT8_MAX
1549 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1550 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1552 if (attr == OVS_KEY_ATTR_TUNNEL) {
1556 if (attr == OVS_KEY_ATTR_ARP) {
1557 /* ARP key has padding, ignore it. */
1558 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1559 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1560 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1561 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1564 return is_all_ones(mask, size);
1568 odp_mask_attr_is_exact(const struct nlattr *ma)
1570 enum ovs_key_attr attr = nl_attr_type(ma);
1574 if (attr == OVS_KEY_ATTR_TUNNEL) {
1577 mask = nl_attr_get(ma);
1578 size = nl_attr_get_size(ma);
1581 return odp_mask_is_exact(attr, mask, size);
1585 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1588 struct odp_portno_names *odp_portno_names;
1590 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1591 odp_portno_names->port_no = port_no;
1592 odp_portno_names->name = xstrdup(port_name);
1593 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1594 hash_odp_port(port_no));
1598 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1600 struct odp_portno_names *odp_portno_names;
1602 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1603 hash_odp_port(port_no), portno_names) {
1604 if (odp_portno_names->port_no == port_no) {
1605 return odp_portno_names->name;
1612 odp_portno_names_destroy(struct hmap *portno_names)
1614 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1615 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1616 hmap_node, portno_names) {
1617 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1618 free(odp_portno_names->name);
1619 free(odp_portno_names);
1623 /* Format helpers. */
1626 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1627 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1629 bool mask_empty = mask && eth_addr_is_zero(*mask);
1631 if (verbose || !mask_empty) {
1632 bool mask_full = !mask || eth_mask_is_exact(*mask);
1635 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1637 ds_put_format(ds, "%s=", name);
1638 eth_format_masked(key, *mask, ds);
1639 ds_put_char(ds, ',');
1645 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1646 const ovs_be64 *mask, bool verbose)
1648 bool mask_empty = mask && !*mask;
1650 if (verbose || !mask_empty) {
1651 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1653 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1654 if (!mask_full) { /* Partially masked. */
1655 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1657 ds_put_char(ds, ',');
1662 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1663 const ovs_be32 *mask, bool verbose)
1665 bool mask_empty = mask && !*mask;
1667 if (verbose || !mask_empty) {
1668 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1670 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1671 if (!mask_full) { /* Partially masked. */
1672 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1674 ds_put_char(ds, ',');
1679 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1680 const ovs_be32 (*mask_)[4], bool verbose)
1682 char buf[INET6_ADDRSTRLEN];
1683 const struct in6_addr *key = (const struct in6_addr *)key_;
1684 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1686 bool mask_empty = mask && ipv6_mask_is_any(mask);
1688 if (verbose || !mask_empty) {
1689 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1691 inet_ntop(AF_INET6, key, buf, sizeof buf);
1692 ds_put_format(ds, "%s=%s", name, buf);
1693 if (!mask_full) { /* Partially masked. */
1694 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1695 ds_put_format(ds, "/%s", buf);
1697 ds_put_char(ds, ',');
1702 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1703 const ovs_be32 *mask, bool verbose)
1705 bool mask_empty = mask && !*mask;
1707 if (verbose || !mask_empty) {
1708 bool mask_full = !mask
1709 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1711 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1712 if (!mask_full) { /* Partially masked. */
1713 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1715 ds_put_char(ds, ',');
1720 format_u8x(struct ds *ds, const char *name, uint8_t key,
1721 const uint8_t *mask, bool verbose)
1723 bool mask_empty = mask && !*mask;
1725 if (verbose || !mask_empty) {
1726 bool mask_full = !mask || *mask == UINT8_MAX;
1728 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1729 if (!mask_full) { /* Partially masked. */
1730 ds_put_format(ds, "/%#"PRIx8, *mask);
1732 ds_put_char(ds, ',');
1737 format_u8u(struct ds *ds, const char *name, uint8_t key,
1738 const uint8_t *mask, bool verbose)
1740 bool mask_empty = mask && !*mask;
1742 if (verbose || !mask_empty) {
1743 bool mask_full = !mask || *mask == UINT8_MAX;
1745 ds_put_format(ds, "%s=%"PRIu8, name, key);
1746 if (!mask_full) { /* Partially masked. */
1747 ds_put_format(ds, "/%#"PRIx8, *mask);
1749 ds_put_char(ds, ',');
1754 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1755 const ovs_be16 *mask, bool verbose)
1757 bool mask_empty = mask && !*mask;
1759 if (verbose || !mask_empty) {
1760 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1762 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1763 if (!mask_full) { /* Partially masked. */
1764 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1766 ds_put_char(ds, ',');
1771 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1772 const ovs_be16 *mask, bool verbose)
1774 bool mask_empty = mask && !*mask;
1776 if (verbose || !mask_empty) {
1777 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1779 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1780 if (!mask_full) { /* Partially masked. */
1781 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1783 ds_put_char(ds, ',');
1788 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1789 const uint16_t *mask, bool verbose)
1791 bool mask_empty = mask && !*mask;
1793 if (verbose || !mask_empty) {
1794 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1796 ds_put_cstr(ds, name);
1797 ds_put_char(ds, '(');
1798 if (!mask_full) { /* Partially masked. */
1799 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1800 } else { /* Fully masked. */
1801 format_flags(ds, flow_tun_flag_to_string, key, ',');
1803 ds_put_cstr(ds, "),");
1808 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1809 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1813 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1814 if (expected_len != ATTR_LEN_VARIABLE &&
1815 expected_len != ATTR_LEN_NESTED) {
1817 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1818 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1820 if (bad_key_len || bad_mask_len) {
1822 ds_put_format(ds, "key%u", nl_attr_type(a));
1825 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1826 nl_attr_get_size(a), expected_len);
1828 format_generic_odp_key(a, ds);
1830 ds_put_char(ds, '/');
1832 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1833 nl_attr_get_size(ma), expected_len);
1835 format_generic_odp_key(ma, ds);
1837 ds_put_char(ds, ')');
1846 format_unknown_key(struct ds *ds, const struct nlattr *a,
1847 const struct nlattr *ma)
1849 ds_put_format(ds, "key%u(", nl_attr_type(a));
1850 format_generic_odp_key(a, ds);
1851 if (ma && !odp_mask_attr_is_exact(ma)) {
1852 ds_put_char(ds, '/');
1853 format_generic_odp_key(ma, ds);
1855 ds_put_cstr(ds, "),");
1859 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1860 const struct nlattr *mask_attr, struct ds *ds,
1864 const struct nlattr *a;
1867 ofpbuf_init(&ofp, 100);
1868 NL_NESTED_FOR_EACH(a, left, attr) {
1869 uint16_t type = nl_attr_type(a);
1870 const struct nlattr *ma = NULL;
1873 ma = nl_attr_find__(nl_attr_get(mask_attr),
1874 nl_attr_get_size(mask_attr), type);
1876 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1882 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1883 OVS_VXLAN_EXT_MAX, true)) {
1888 case OVS_VXLAN_EXT_GBP: {
1889 uint32_t key = nl_attr_get_u32(a);
1890 ovs_be16 id, id_mask;
1891 uint8_t flags, flags_mask;
1893 id = htons(key & 0xFFFF);
1894 flags = (key >> 16) & 0xFF;
1896 uint32_t mask = nl_attr_get_u32(ma);
1897 id_mask = htons(mask & 0xFFFF);
1898 flags_mask = (mask >> 16) & 0xFF;
1901 ds_put_cstr(ds, "gbp(");
1902 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1903 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1905 ds_put_cstr(ds, "),");
1910 format_unknown_key(ds, a, ma);
1916 ofpbuf_uninit(&ofp);
1919 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1922 format_geneve_opts(const struct geneve_opt *opt,
1923 const struct geneve_opt *mask, int opts_len,
1924 struct ds *ds, bool verbose)
1926 while (opts_len > 0) {
1928 uint8_t data_len, data_len_mask;
1930 if (opts_len < sizeof *opt) {
1931 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1932 opts_len, sizeof *opt);
1936 data_len = opt->length * 4;
1938 if (mask->length == 0x1f) {
1939 data_len_mask = UINT8_MAX;
1941 data_len_mask = mask->length;
1944 len = sizeof *opt + data_len;
1945 if (len > opts_len) {
1946 ds_put_format(ds, "opt len %u greater than remaining %u",
1951 ds_put_char(ds, '{');
1952 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1954 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1955 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1956 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1957 ds_put_hex(ds, opt + 1, data_len);
1958 if (mask && !is_all_ones(mask + 1, data_len)) {
1959 ds_put_char(ds, '/');
1960 ds_put_hex(ds, mask + 1, data_len);
1965 ds_put_char(ds, '}');
1967 opt += len / sizeof(*opt);
1969 mask += len / sizeof(*opt);
1976 format_odp_tun_geneve(const struct nlattr *attr,
1977 const struct nlattr *mask_attr, struct ds *ds,
1980 int opts_len = nl_attr_get_size(attr);
1981 const struct geneve_opt *opt = nl_attr_get(attr);
1982 const struct geneve_opt *mask = mask_attr ?
1983 nl_attr_get(mask_attr) : NULL;
1985 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1986 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1987 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1991 format_geneve_opts(opt, mask, opts_len, ds, verbose);
1995 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1996 struct ds *ds, bool verbose)
1999 const struct nlattr *a;
2001 uint16_t mask_flags = 0;
2004 ofpbuf_init(&ofp, 100);
2005 NL_NESTED_FOR_EACH(a, left, attr) {
2006 enum ovs_tunnel_key_attr type = nl_attr_type(a);
2007 const struct nlattr *ma = NULL;
2010 ma = nl_attr_find__(nl_attr_get(mask_attr),
2011 nl_attr_get_size(mask_attr), type);
2013 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
2014 OVS_TUNNEL_KEY_ATTR_MAX,
2019 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
2020 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2025 case OVS_TUNNEL_KEY_ATTR_ID:
2026 format_be64(ds, "tun_id", nl_attr_get_be64(a),
2027 ma ? nl_attr_get(ma) : NULL, verbose);
2028 flags |= FLOW_TNL_F_KEY;
2030 mask_flags |= FLOW_TNL_F_KEY;
2033 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2034 format_ipv4(ds, "src", nl_attr_get_be32(a),
2035 ma ? nl_attr_get(ma) : NULL, verbose);
2037 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2038 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2039 ma ? nl_attr_get(ma) : NULL, verbose);
2041 case OVS_TUNNEL_KEY_ATTR_TOS:
2042 format_u8x(ds, "tos", nl_attr_get_u8(a),
2043 ma ? nl_attr_get(ma) : NULL, verbose);
2045 case OVS_TUNNEL_KEY_ATTR_TTL:
2046 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2047 ma ? nl_attr_get(ma) : NULL, verbose);
2049 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2050 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2052 case OVS_TUNNEL_KEY_ATTR_CSUM:
2053 flags |= FLOW_TNL_F_CSUM;
2055 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2056 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2057 ma ? nl_attr_get(ma) : NULL, verbose);
2059 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2060 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2061 ma ? nl_attr_get(ma) : NULL, verbose);
2063 case OVS_TUNNEL_KEY_ATTR_OAM:
2064 flags |= FLOW_TNL_F_OAM;
2066 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2067 ds_put_cstr(ds, "vxlan(");
2068 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2069 ds_put_cstr(ds, "),");
2071 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2072 ds_put_cstr(ds, "geneve(");
2073 format_odp_tun_geneve(a, ma, ds, verbose);
2074 ds_put_cstr(ds, "),");
2076 case __OVS_TUNNEL_KEY_ATTR_MAX:
2078 format_unknown_key(ds, a, ma);
2083 /* Flags can have a valid mask even if the attribute is not set, so
2084 * we need to collect these separately. */
2086 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2087 switch (nl_attr_type(a)) {
2088 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2089 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2091 case OVS_TUNNEL_KEY_ATTR_CSUM:
2092 mask_flags |= FLOW_TNL_F_CSUM;
2094 case OVS_TUNNEL_KEY_ATTR_OAM:
2095 mask_flags |= FLOW_TNL_F_OAM;
2101 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2104 ofpbuf_uninit(&ofp);
2108 format_frag(struct ds *ds, const char *name, uint8_t key,
2109 const uint8_t *mask, bool verbose)
2111 bool mask_empty = mask && !*mask;
2113 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2114 if (verbose || !mask_empty) {
2115 bool mask_full = !mask || *mask == UINT8_MAX;
2117 if (!mask_full) { /* Partially masked. */
2118 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2121 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2127 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2128 const struct hmap *portno_names, struct ds *ds,
2131 enum ovs_key_attr attr = nl_attr_type(a);
2132 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2135 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2137 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2139 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2140 OVS_KEY_ATTR_MAX, false)) {
2144 ds_put_char(ds, '(');
2146 case OVS_KEY_ATTR_ENCAP:
2147 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2148 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2149 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2151 } else if (nl_attr_get_size(a)) {
2152 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2157 case OVS_KEY_ATTR_PRIORITY:
2158 case OVS_KEY_ATTR_SKB_MARK:
2159 case OVS_KEY_ATTR_DP_HASH:
2160 case OVS_KEY_ATTR_RECIRC_ID:
2161 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2163 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2167 case OVS_KEY_ATTR_TUNNEL:
2168 format_odp_tun_attr(a, ma, ds, verbose);
2171 case OVS_KEY_ATTR_IN_PORT:
2172 if (portno_names && verbose && is_exact) {
2173 char *name = odp_portno_names_get(portno_names,
2174 u32_to_odp(nl_attr_get_u32(a)));
2176 ds_put_format(ds, "%s", name);
2178 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2181 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2183 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2188 case OVS_KEY_ATTR_ETHERNET: {
2189 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2190 const struct ovs_key_ethernet *key = nl_attr_get(a);
2192 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2193 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2197 case OVS_KEY_ATTR_VLAN:
2198 format_vlan_tci(ds, nl_attr_get_be16(a),
2199 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2202 case OVS_KEY_ATTR_MPLS: {
2203 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2204 const struct ovs_key_mpls *mpls_mask = NULL;
2205 size_t size = nl_attr_get_size(a);
2207 if (!size || size % sizeof *mpls_key) {
2208 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2212 mpls_mask = nl_attr_get(ma);
2213 if (size != nl_attr_get_size(ma)) {
2214 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2215 "mask length %"PRIuSIZE")",
2216 size, nl_attr_get_size(ma));
2220 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2223 case OVS_KEY_ATTR_ETHERTYPE:
2224 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2226 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2230 case OVS_KEY_ATTR_IPV4: {
2231 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2232 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2234 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2235 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2236 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2238 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2239 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2240 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2245 case OVS_KEY_ATTR_IPV6: {
2246 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2247 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2249 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2250 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2251 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2253 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2255 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2257 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2259 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2264 /* These have the same structure and format. */
2265 case OVS_KEY_ATTR_TCP:
2266 case OVS_KEY_ATTR_UDP:
2267 case OVS_KEY_ATTR_SCTP: {
2268 const struct ovs_key_tcp *key = nl_attr_get(a);
2269 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2271 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2272 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2276 case OVS_KEY_ATTR_TCP_FLAGS:
2278 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2279 ntohs(nl_attr_get_be16(a)),
2280 ntohs(nl_attr_get_be16(ma)));
2282 format_flags(ds, packet_tcp_flag_to_string,
2283 ntohs(nl_attr_get_be16(a)), ',');
2287 case OVS_KEY_ATTR_ICMP: {
2288 const struct ovs_key_icmp *key = nl_attr_get(a);
2289 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2291 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2292 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2296 case OVS_KEY_ATTR_ICMPV6: {
2297 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2298 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2300 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2302 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2307 case OVS_KEY_ATTR_ARP: {
2308 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2309 const struct ovs_key_arp *key = nl_attr_get(a);
2311 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2312 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2313 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2314 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2315 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2319 case OVS_KEY_ATTR_ND: {
2320 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2321 const struct ovs_key_nd *key = nl_attr_get(a);
2323 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2325 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2326 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2331 case OVS_KEY_ATTR_UNSPEC:
2332 case __OVS_KEY_ATTR_MAX:
2334 format_generic_odp_key(a, ds);
2336 ds_put_char(ds, '/');
2337 format_generic_odp_key(ma, ds);
2341 ds_put_char(ds, ')');
2344 static struct nlattr *
2345 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2346 struct ofpbuf *ofp, const struct nlattr *key)
2348 const struct nlattr *a;
2350 int type = nl_attr_type(key);
2351 int size = nl_attr_get_size(key);
2353 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2354 nl_msg_put_unspec_zero(ofp, type, size);
2358 if (tbl[type].next) {
2359 tbl = tbl[type].next;
2360 max = tbl[type].next_max;
2363 nested_mask = nl_msg_start_nested(ofp, type);
2364 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2365 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2367 nl_msg_end_nested(ofp, nested_mask);
2374 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2378 if (ovs_scan(s, "ufid:")) {
2381 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2393 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2395 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2398 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2399 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2400 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2401 * non-null and 'verbose' is true, translates odp port number to its name. */
2403 odp_flow_format(const struct nlattr *key, size_t key_len,
2404 const struct nlattr *mask, size_t mask_len,
2405 const struct hmap *portno_names, struct ds *ds, bool verbose)
2408 const struct nlattr *a;
2410 bool has_ethtype_key = false;
2411 const struct nlattr *ma = NULL;
2413 bool first_field = true;
2415 ofpbuf_init(&ofp, 100);
2416 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2417 bool is_nested_attr;
2418 bool is_wildcard = false;
2419 int attr_type = nl_attr_type(a);
2421 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2422 has_ethtype_key = true;
2425 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2426 OVS_KEY_ATTR_MAX, attr_type) ==
2429 if (mask && mask_len) {
2430 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2431 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2434 if (verbose || !is_wildcard || is_nested_attr) {
2435 if (is_wildcard && !ma) {
2436 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2441 ds_put_char(ds, ',');
2443 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2444 first_field = false;
2448 ofpbuf_uninit(&ofp);
2453 if (left == key_len) {
2454 ds_put_cstr(ds, "<empty>");
2456 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2457 for (i = 0; i < left; i++) {
2458 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2460 ds_put_char(ds, ')');
2462 if (!has_ethtype_key) {
2463 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2465 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2466 ntohs(nl_attr_get_be16(ma)));
2470 ds_put_cstr(ds, "<empty>");
2474 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2475 * OVS_KEY_ATTR_* attributes in 'key'. */
2477 odp_flow_key_format(const struct nlattr *key,
2478 size_t key_len, struct ds *ds)
2480 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2484 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2486 if (!strcasecmp(s, "no")) {
2487 *type = OVS_FRAG_TYPE_NONE;
2488 } else if (!strcasecmp(s, "first")) {
2489 *type = OVS_FRAG_TYPE_FIRST;
2490 } else if (!strcasecmp(s, "later")) {
2491 *type = OVS_FRAG_TYPE_LATER;
2501 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2502 uint8_t (*mask)[ETH_ADDR_LEN])
2506 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2510 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2511 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2514 memset(mask, 0xff, sizeof *mask);
2523 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2527 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2531 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2532 IP_SCAN_ARGS(mask), &n)) {
2535 *mask = OVS_BE32_MAX;
2544 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2547 char ipv6_s[IPV6_SCAN_LEN + 1];
2549 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2550 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2554 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2555 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2558 memset(mask, 0xff, sizeof *mask);
2567 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2572 if (ovs_scan(s, "%i%n", &key_, &n)
2573 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2578 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2579 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2581 *mask = htonl(mask_);
2583 *mask = htonl(IPV6_LABEL_MASK);
2592 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2596 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2600 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2612 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2616 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2620 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2632 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2634 uint16_t key_, mask_;
2637 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2642 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2644 *mask = htons(mask_);
2646 *mask = OVS_BE16_MAX;
2655 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2657 uint64_t key_, mask_;
2660 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2663 *key = htonll(key_);
2665 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2667 *mask = htonll(mask_);
2669 *mask = OVS_BE64_MAX;
2678 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2680 uint32_t flags, fmask;
2683 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2684 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2685 if (n >= 0 && s[n] == ')') {
2696 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2698 uint32_t flags, fmask;
2701 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2702 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2704 *key = htons(flags);
2706 *mask = htons(fmask);
2714 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2718 enum ovs_frag_type frag_type;
2720 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2721 && ovs_frag_type_from_string(frag, &frag_type)) {
2734 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2735 const struct simap *port_names)
2739 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2743 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2750 } else if (port_names) {
2751 const struct simap_node *node;
2754 len = strcspn(s, ")");
2755 node = simap_find_len(port_names, s, len);
2768 /* Helper for vlan parsing. */
2769 struct ovs_key_vlan__ {
2774 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2776 const uint16_t mask = ((1U << bits) - 1) << offset;
2778 if (value >> bits) {
2782 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2787 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2790 uint16_t key_, mask_;
2793 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2796 if (set_be16_bf(key, bits, offset, key_)) {
2798 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2801 if (!set_be16_bf(mask, bits, offset, mask_)) {
2805 *mask |= htons(((1U << bits) - 1) << offset);
2815 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2817 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2821 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2823 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2827 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2829 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2834 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2836 const uint32_t mask = ((1U << bits) - 1) << offset;
2838 if (value >> bits) {
2842 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2847 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2850 uint32_t key_, mask_;
2853 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2856 if (set_be32_bf(key, bits, offset, key_)) {
2858 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2861 if (!set_be32_bf(mask, bits, offset, mask_)) {
2865 *mask |= htonl(((1U << bits) - 1) << offset);
2875 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2877 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2881 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2883 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2887 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2889 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2893 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2895 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2899 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2901 const char *s_base = s;
2902 ovs_be16 id = 0, id_mask = 0;
2903 uint8_t flags = 0, flags_mask = 0;
2905 if (!strncmp(s, "id=", 3)) {
2907 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2913 if (!strncmp(s, "flags=", 6)) {
2915 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2918 if (!strncmp(s, "))", 2)) {
2921 *key = (flags << 16) | ntohs(id);
2923 *mask = (flags_mask << 16) | ntohs(id_mask);
2933 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2935 const char *s_base = s;
2936 struct geneve_opt *opt = key->d;
2937 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2938 int len_remain = sizeof key->d;
2940 while (s[0] == '{' && len_remain >= sizeof *opt) {
2944 len_remain -= sizeof *opt;
2946 if (!strncmp(s, "class=", 6)) {
2948 s += scan_be16(s, &opt->opt_class,
2949 mask ? &opt_mask->opt_class : NULL);
2951 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2957 if (!strncmp(s, "type=", 5)) {
2959 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2961 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2967 if (!strncmp(s, "len=", 4)) {
2968 uint8_t opt_len, opt_len_mask;
2970 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2972 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2975 opt->length = opt_len / 4;
2977 opt_mask->length = opt_len_mask;
2981 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2987 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2994 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2995 data_len, (char **)&s)) {
3006 opt += 1 + data_len / 4;
3008 opt_mask += 1 + data_len / 4;
3010 len_remain -= data_len;
3015 int len = sizeof key->d - len_remain;
3029 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3031 const uint16_t *flags = data_;
3033 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3034 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3036 if (*flags & FLOW_TNL_F_CSUM) {
3037 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3039 if (*flags & FLOW_TNL_F_OAM) {
3040 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3045 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3047 const uint32_t *gbp = data_;
3050 size_t vxlan_opts_ofs;
3052 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3053 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3054 nl_msg_end_nested(a, vxlan_opts_ofs);
3059 geneve_to_attr(struct ofpbuf *a, const void *data_)
3061 const struct geneve_scan *geneve = data_;
3063 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3067 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3069 unsigned long call_fn = (unsigned long)FUNC; \
3071 typedef void (*fn)(struct ofpbuf *, const void *); \
3073 func(BUF, &(DATA)); \
3075 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3079 #define SCAN_IF(NAME) \
3080 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3081 const char *start = s; \
3086 /* Usually no special initialization is needed. */
3087 #define SCAN_BEGIN(NAME, TYPE) \
3090 memset(&skey, 0, sizeof skey); \
3091 memset(&smask, 0, sizeof smask); \
3095 /* Init as fully-masked as mask will not be scanned. */
3096 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3099 memset(&skey, 0, sizeof skey); \
3100 memset(&smask, 0xff, sizeof smask); \
3104 /* VLAN needs special initialization. */
3105 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3107 TYPE skey = KEY_INIT; \
3108 TYPE smask = MASK_INIT; \
3112 /* Scan unnamed entry as 'TYPE' */
3113 #define SCAN_TYPE(TYPE, KEY, MASK) \
3114 len = scan_##TYPE(s, KEY, MASK); \
3120 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3121 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3122 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3123 s += strlen(NAME); \
3124 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3128 #define SCAN_FINISH() \
3129 } while (*s++ == ',' && len != 0); \
3130 if (s[-1] != ')') { \
3134 #define SCAN_FINISH_SINGLE() \
3136 if (*s++ != ')') { \
3140 /* Beginning of nested attribute. */
3141 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3143 size_t key_offset, mask_offset; \
3144 key_offset = nl_msg_start_nested(key, ATTR); \
3146 mask_offset = nl_msg_start_nested(mask, ATTR); \
3151 #define SCAN_END_NESTED() \
3153 nl_msg_end_nested(key, key_offset); \
3155 nl_msg_end_nested(mask, mask_offset); \
3160 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3161 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3163 memset(&skey, 0, sizeof skey); \
3164 memset(&smask, 0xff, sizeof smask); \
3165 s += strlen(NAME); \
3166 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3167 SCAN_PUT(ATTR, FUNC); \
3171 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3172 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3174 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3175 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3177 #define SCAN_PUT(ATTR, FUNC) \
3178 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3179 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3181 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3185 #define SCAN_END(ATTR) \
3187 SCAN_PUT(ATTR, NULL); \
3191 #define SCAN_END_SINGLE(ATTR) \
3192 SCAN_FINISH_SINGLE(); \
3193 SCAN_PUT(ATTR, NULL); \
3197 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3198 SCAN_BEGIN(NAME, TYPE) { \
3199 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3200 } SCAN_END_SINGLE(ATTR)
3202 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3203 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3204 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3205 } SCAN_END_SINGLE(ATTR)
3207 /* scan_port needs one extra argument. */
3208 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3209 SCAN_BEGIN(NAME, TYPE) { \
3210 len = scan_port(s, &skey, &smask, port_names); \
3215 } SCAN_END_SINGLE(ATTR)
3218 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3219 struct ofpbuf *key, struct ofpbuf *mask)
3225 len = odp_ufid_from_string(s, &ufid);
3230 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3231 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3232 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3233 OVS_KEY_ATTR_RECIRC_ID);
3234 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3236 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3237 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3238 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3239 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3240 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3241 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3242 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3243 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3244 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3245 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3247 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3248 } SCAN_END_NESTED();
3250 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3252 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3253 SCAN_FIELD("src=", eth, eth_src);
3254 SCAN_FIELD("dst=", eth, eth_dst);
3255 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3257 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3258 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3259 SCAN_FIELD("vid=", vid, tci);
3260 SCAN_FIELD("pcp=", pcp, tci);
3261 SCAN_FIELD("cfi=", cfi, tci);
3262 } SCAN_END(OVS_KEY_ATTR_VLAN);
3264 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3266 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3267 SCAN_FIELD("label=", mpls_label, mpls_lse);
3268 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3269 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3270 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3271 } SCAN_END(OVS_KEY_ATTR_MPLS);
3273 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3274 SCAN_FIELD("src=", ipv4, ipv4_src);
3275 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3276 SCAN_FIELD("proto=", u8, ipv4_proto);
3277 SCAN_FIELD("tos=", u8, ipv4_tos);
3278 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3279 SCAN_FIELD("frag=", frag, ipv4_frag);
3280 } SCAN_END(OVS_KEY_ATTR_IPV4);
3282 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3283 SCAN_FIELD("src=", ipv6, ipv6_src);
3284 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3285 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3286 SCAN_FIELD("proto=", u8, ipv6_proto);
3287 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3288 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3289 SCAN_FIELD("frag=", frag, ipv6_frag);
3290 } SCAN_END(OVS_KEY_ATTR_IPV6);
3292 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3293 SCAN_FIELD("src=", be16, tcp_src);
3294 SCAN_FIELD("dst=", be16, tcp_dst);
3295 } SCAN_END(OVS_KEY_ATTR_TCP);
3297 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3299 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3300 SCAN_FIELD("src=", be16, udp_src);
3301 SCAN_FIELD("dst=", be16, udp_dst);
3302 } SCAN_END(OVS_KEY_ATTR_UDP);
3304 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3305 SCAN_FIELD("src=", be16, sctp_src);
3306 SCAN_FIELD("dst=", be16, sctp_dst);
3307 } SCAN_END(OVS_KEY_ATTR_SCTP);
3309 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3310 SCAN_FIELD("type=", u8, icmp_type);
3311 SCAN_FIELD("code=", u8, icmp_code);
3312 } SCAN_END(OVS_KEY_ATTR_ICMP);
3314 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3315 SCAN_FIELD("type=", u8, icmpv6_type);
3316 SCAN_FIELD("code=", u8, icmpv6_code);
3317 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3319 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3320 SCAN_FIELD("sip=", ipv4, arp_sip);
3321 SCAN_FIELD("tip=", ipv4, arp_tip);
3322 SCAN_FIELD("op=", be16, arp_op);
3323 SCAN_FIELD("sha=", eth, arp_sha);
3324 SCAN_FIELD("tha=", eth, arp_tha);
3325 } SCAN_END(OVS_KEY_ATTR_ARP);
3327 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3328 SCAN_FIELD("target=", ipv6, nd_target);
3329 SCAN_FIELD("sll=", eth, nd_sll);
3330 SCAN_FIELD("tll=", eth, nd_tll);
3331 } SCAN_END(OVS_KEY_ATTR_ND);
3333 /* Encap open-coded. */
3334 if (!strncmp(s, "encap(", 6)) {
3335 const char *start = s;
3336 size_t encap, encap_mask = 0;
3338 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3340 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3347 s += strspn(s, delimiters);
3350 } else if (*s == ')') {
3354 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3362 nl_msg_end_nested(key, encap);
3364 nl_msg_end_nested(mask, encap_mask);
3373 /* Parses the string representation of a datapath flow key, in the
3374 * format output by odp_flow_key_format(). Returns 0 if successful,
3375 * otherwise a positive errno value. On success, the flow key is
3376 * appended to 'key' as a series of Netlink attributes. On failure, no
3377 * data is appended to 'key'. Either way, 'key''s data might be
3380 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3381 * to a port number. (Port names may be used instead of port numbers in
3384 * On success, the attributes appended to 'key' are individually syntactically
3385 * valid, but they may not be valid as a sequence. 'key' might, for example,
3386 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3388 odp_flow_from_string(const char *s, const struct simap *port_names,
3389 struct ofpbuf *key, struct ofpbuf *mask)
3391 const size_t old_size = key->size;
3395 s += strspn(s, delimiters);
3400 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3402 key->size = old_size;
3412 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3415 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3416 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3417 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3418 * must use a zero mask for the netlink frag field, and all ones mask
3420 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3422 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3423 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3424 : OVS_FRAG_TYPE_FIRST;
3427 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3428 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3429 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3431 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3433 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3435 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3437 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3438 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3439 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3440 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3442 /* These share the same layout. */
3444 struct ovs_key_tcp tcp;
3445 struct ovs_key_udp udp;
3446 struct ovs_key_sctp sctp;
3449 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3450 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3453 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3454 bool export_mask, struct ofpbuf *buf)
3456 struct ovs_key_ethernet *eth_key;
3458 const struct flow *flow = parms->flow;
3459 const struct flow *data = export_mask ? parms->mask : parms->flow;
3461 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3463 if (flow->tunnel.ip_dst || export_mask) {
3464 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3468 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3470 if (parms->recirc) {
3471 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3472 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3475 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3476 * is not the magical value "ODPP_NONE". */
3477 if (export_mask || parms->odp_in_port != ODPP_NONE) {
3478 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3481 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3483 get_ethernet_key(data, eth_key);
3485 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3487 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3489 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3491 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3492 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3493 if (flow->vlan_tci == htons(0)) {
3500 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3501 /* For backwards compatibility with kernels that don't support
3502 * wildcarding, the following convention is used to encode the
3503 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3506 * -------- -------- -------
3507 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3508 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3509 * <none> 0xffff Any non-Ethernet II frame (except valid
3510 * 802.3 SNAP packet with valid eth_type).
3513 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3518 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3520 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3521 struct ovs_key_ipv4 *ipv4_key;
3523 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3525 get_ipv4_key(data, ipv4_key, export_mask);
3526 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3527 struct ovs_key_ipv6 *ipv6_key;
3529 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3531 get_ipv6_key(data, ipv6_key, export_mask);
3532 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3533 flow->dl_type == htons(ETH_TYPE_RARP)) {
3534 struct ovs_key_arp *arp_key;
3536 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3538 get_arp_key(data, arp_key);
3539 } else if (eth_type_mpls(flow->dl_type)) {
3540 struct ovs_key_mpls *mpls_key;
3543 n = flow_count_mpls_labels(flow, NULL);
3545 n = MIN(n, parms->max_mpls_depth);
3547 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3548 n * sizeof *mpls_key);
3549 for (i = 0; i < n; i++) {
3550 mpls_key[i].mpls_lse = data->mpls_lse[i];
3554 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3555 if (flow->nw_proto == IPPROTO_TCP) {
3556 union ovs_key_tp *tcp_key;
3558 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3560 get_tp_key(data, tcp_key);
3561 if (data->tcp_flags) {
3562 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3564 } else if (flow->nw_proto == IPPROTO_UDP) {
3565 union ovs_key_tp *udp_key;
3567 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3569 get_tp_key(data, udp_key);
3570 } else if (flow->nw_proto == IPPROTO_SCTP) {
3571 union ovs_key_tp *sctp_key;
3573 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3575 get_tp_key(data, sctp_key);
3576 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3577 && flow->nw_proto == IPPROTO_ICMP) {
3578 struct ovs_key_icmp *icmp_key;
3580 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3582 icmp_key->icmp_type = ntohs(data->tp_src);
3583 icmp_key->icmp_code = ntohs(data->tp_dst);
3584 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3585 && flow->nw_proto == IPPROTO_ICMPV6) {
3586 struct ovs_key_icmpv6 *icmpv6_key;
3588 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3589 sizeof *icmpv6_key);
3590 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3591 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3593 if (flow->tp_dst == htons(0)
3594 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3595 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3596 && (!export_mask || (data->tp_src == htons(0xffff)
3597 && data->tp_dst == htons(0xffff)))) {
3599 struct ovs_key_nd *nd_key;
3601 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3603 memcpy(nd_key->nd_target, &data->nd_target,
3604 sizeof nd_key->nd_target);
3605 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3606 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3613 nl_msg_end_nested(buf, encap);
3617 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3619 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3620 * capable of being expanded to allow for that much space. */
3622 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3625 odp_flow_key_from_flow__(parms, false, buf);
3628 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3631 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3632 * capable of being expanded to allow for that much space. */
3634 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3637 odp_flow_key_from_flow__(parms, true, buf);
3640 /* Generate ODP flow key from the given packet metadata */
3642 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3644 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3646 if (md->tunnel.ip_dst) {
3647 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3650 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3652 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3653 * value "ODPP_NONE". */
3654 if (md->in_port.odp_port != ODPP_NONE) {
3655 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3659 /* Generate packet metadata from the given ODP flow key. */
3661 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3662 struct pkt_metadata *md)
3664 const struct nlattr *nla;
3666 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3667 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3668 1u << OVS_KEY_ATTR_IN_PORT;
3670 pkt_metadata_init(md, ODPP_NONE);
3672 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3673 uint16_t type = nl_attr_type(nla);
3674 size_t len = nl_attr_get_size(nla);
3675 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3676 OVS_KEY_ATTR_MAX, type);
3678 if (len != expected_len && expected_len >= 0) {
3683 case OVS_KEY_ATTR_RECIRC_ID:
3684 md->recirc_id = nl_attr_get_u32(nla);
3685 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3687 case OVS_KEY_ATTR_DP_HASH:
3688 md->dp_hash = nl_attr_get_u32(nla);
3689 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3691 case OVS_KEY_ATTR_PRIORITY:
3692 md->skb_priority = nl_attr_get_u32(nla);
3693 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3695 case OVS_KEY_ATTR_SKB_MARK:
3696 md->pkt_mark = nl_attr_get_u32(nla);
3697 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3699 case OVS_KEY_ATTR_TUNNEL: {
3700 enum odp_key_fitness res;
3702 res = odp_tun_key_from_attr(nla, &md->tunnel);
3703 if (res == ODP_FIT_ERROR) {
3704 memset(&md->tunnel, 0, sizeof md->tunnel);
3705 } else if (res == ODP_FIT_PERFECT) {
3706 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3710 case OVS_KEY_ATTR_IN_PORT:
3711 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3712 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3718 if (!wanted_attrs) {
3719 return; /* Have everything. */
3725 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3727 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3728 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3729 key_len / sizeof(uint32_t), 0);
3733 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3734 uint64_t attrs, int out_of_range_attr,
3735 const struct nlattr *key, size_t key_len)
3740 if (VLOG_DROP_DBG(rl)) {
3745 for (i = 0; i < 64; i++) {
3746 if (attrs & (UINT64_C(1) << i)) {
3747 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3749 ds_put_format(&s, " %s",
3750 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3753 if (out_of_range_attr) {
3754 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3757 ds_put_cstr(&s, ": ");
3758 odp_flow_key_format(key, key_len, &s);
3760 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3765 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3767 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3770 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3773 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3774 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3775 return 0xff; /* Error. */
3778 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3779 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3780 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3784 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3785 const struct nlattr *attrs[], uint64_t *present_attrsp,
3786 int *out_of_range_attrp)
3788 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3789 const struct nlattr *nla;
3790 uint64_t present_attrs;
3793 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3795 *out_of_range_attrp = 0;
3796 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3797 uint16_t type = nl_attr_type(nla);
3798 size_t len = nl_attr_get_size(nla);
3799 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3800 OVS_KEY_ATTR_MAX, type);
3802 if (len != expected_len && expected_len >= 0) {
3803 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3805 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3806 "length %d", ovs_key_attr_to_string(type, namebuf,
3812 if (type > OVS_KEY_ATTR_MAX) {
3813 *out_of_range_attrp = type;
3815 if (present_attrs & (UINT64_C(1) << type)) {
3816 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3818 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3819 ovs_key_attr_to_string(type,
3820 namebuf, sizeof namebuf));
3824 present_attrs |= UINT64_C(1) << type;
3829 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3833 *present_attrsp = present_attrs;
3837 static enum odp_key_fitness
3838 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3839 uint64_t expected_attrs,
3840 const struct nlattr *key, size_t key_len)
3842 uint64_t missing_attrs;
3843 uint64_t extra_attrs;
3845 missing_attrs = expected_attrs & ~present_attrs;
3846 if (missing_attrs) {
3847 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3848 log_odp_key_attributes(&rl, "expected but not present",
3849 missing_attrs, 0, key, key_len);
3850 return ODP_FIT_TOO_LITTLE;
3853 extra_attrs = present_attrs & ~expected_attrs;
3854 if (extra_attrs || out_of_range_attr) {
3855 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3856 log_odp_key_attributes(&rl, "present but not expected",
3857 extra_attrs, out_of_range_attr, key, key_len);
3858 return ODP_FIT_TOO_MUCH;
3861 return ODP_FIT_PERFECT;
3865 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3866 uint64_t present_attrs, uint64_t *expected_attrs,
3867 struct flow *flow, const struct flow *src_flow)
3869 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3870 bool is_mask = flow != src_flow;
3872 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3873 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3874 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3875 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3876 ntohs(flow->dl_type));
3879 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3880 flow->dl_type != htons(0xffff)) {
3883 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3886 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3887 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3888 /* See comments in odp_flow_key_from_flow__(). */
3889 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3896 static enum odp_key_fitness
3897 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3898 uint64_t present_attrs, int out_of_range_attr,
3899 uint64_t expected_attrs, struct flow *flow,
3900 const struct nlattr *key, size_t key_len,
3901 const struct flow *src_flow)
3903 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3904 bool is_mask = src_flow != flow;
3905 const void *check_start = NULL;
3906 size_t check_len = 0;
3907 enum ovs_key_attr expected_bit = 0xff;
3909 if (eth_type_mpls(src_flow->dl_type)) {
3910 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3911 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3913 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3914 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3915 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3916 int n = size / sizeof(ovs_be32);
3919 if (!size || size % sizeof(ovs_be32)) {
3920 return ODP_FIT_ERROR;
3922 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3923 return ODP_FIT_ERROR;
3926 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3927 flow->mpls_lse[i] = mpls_lse[i];
3929 if (n > FLOW_MAX_MPLS_LABELS) {
3930 return ODP_FIT_TOO_MUCH;
3934 /* BOS may be set only in the innermost label. */
3935 for (i = 0; i < n - 1; i++) {
3936 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3937 return ODP_FIT_ERROR;
3941 /* BOS must be set in the innermost label. */
3942 if (n < FLOW_MAX_MPLS_LABELS
3943 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3944 return ODP_FIT_TOO_LITTLE;
3950 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3952 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3954 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3955 const struct ovs_key_ipv4 *ipv4_key;
3957 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3958 put_ipv4_key(ipv4_key, flow, is_mask);
3959 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3960 return ODP_FIT_ERROR;
3963 check_start = ipv4_key;
3964 check_len = sizeof *ipv4_key;
3965 expected_bit = OVS_KEY_ATTR_IPV4;
3968 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3970 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3972 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3973 const struct ovs_key_ipv6 *ipv6_key;
3975 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3976 put_ipv6_key(ipv6_key, flow, is_mask);
3977 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3978 return ODP_FIT_ERROR;
3981 check_start = ipv6_key;
3982 check_len = sizeof *ipv6_key;
3983 expected_bit = OVS_KEY_ATTR_IPV6;
3986 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3987 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3989 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3991 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3992 const struct ovs_key_arp *arp_key;
3994 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3995 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3996 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3997 "key", ntohs(arp_key->arp_op));
3998 return ODP_FIT_ERROR;
4000 put_arp_key(arp_key, flow);
4002 check_start = arp_key;
4003 check_len = sizeof *arp_key;
4004 expected_bit = OVS_KEY_ATTR_ARP;
4010 if (check_len > 0) { /* Happens only when 'is_mask'. */
4011 if (!is_all_zeros(check_start, check_len) &&
4012 flow->dl_type != htons(0xffff)) {
4013 return ODP_FIT_ERROR;
4015 expected_attrs |= UINT64_C(1) << expected_bit;
4019 expected_bit = OVS_KEY_ATTR_UNSPEC;
4020 if (src_flow->nw_proto == IPPROTO_TCP
4021 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4022 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4023 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4025 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4027 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4028 const union ovs_key_tp *tcp_key;
4030 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4031 put_tp_key(tcp_key, flow);
4032 expected_bit = OVS_KEY_ATTR_TCP;
4034 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4035 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4036 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4038 } else if (src_flow->nw_proto == IPPROTO_UDP
4039 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4040 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4041 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4043 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4045 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4046 const union ovs_key_tp *udp_key;
4048 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4049 put_tp_key(udp_key, flow);
4050 expected_bit = OVS_KEY_ATTR_UDP;
4052 } else if (src_flow->nw_proto == IPPROTO_SCTP
4053 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4054 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4055 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4057 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4059 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4060 const union ovs_key_tp *sctp_key;
4062 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4063 put_tp_key(sctp_key, flow);
4064 expected_bit = OVS_KEY_ATTR_SCTP;
4066 } else if (src_flow->nw_proto == IPPROTO_ICMP
4067 && src_flow->dl_type == htons(ETH_TYPE_IP)
4068 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4070 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4072 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4073 const struct ovs_key_icmp *icmp_key;
4075 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4076 flow->tp_src = htons(icmp_key->icmp_type);
4077 flow->tp_dst = htons(icmp_key->icmp_code);
4078 expected_bit = OVS_KEY_ATTR_ICMP;
4080 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4081 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4082 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4084 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4086 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4087 const struct ovs_key_icmpv6 *icmpv6_key;
4089 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4090 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4091 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4092 expected_bit = OVS_KEY_ATTR_ICMPV6;
4093 if (src_flow->tp_dst == htons(0) &&
4094 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4095 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4097 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4099 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4100 const struct ovs_key_nd *nd_key;
4102 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4103 memcpy(&flow->nd_target, nd_key->nd_target,
4104 sizeof flow->nd_target);
4105 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4106 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4108 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4109 (flow->tp_src != htons(0xffff) ||
4110 flow->tp_dst != htons(0xffff))) {
4111 return ODP_FIT_ERROR;
4113 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4120 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4121 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4122 return ODP_FIT_ERROR;
4124 expected_attrs |= UINT64_C(1) << expected_bit;
4129 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4133 /* Parse 802.1Q header then encapsulated L3 attributes. */
4134 static enum odp_key_fitness
4135 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4136 uint64_t present_attrs, int out_of_range_attr,
4137 uint64_t expected_attrs, struct flow *flow,
4138 const struct nlattr *key, size_t key_len,
4139 const struct flow *src_flow)
4141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4142 bool is_mask = src_flow != flow;
4144 const struct nlattr *encap
4145 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4146 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4147 enum odp_key_fitness encap_fitness;
4148 enum odp_key_fitness fitness;
4150 /* Calculate fitness of outer attributes. */
4152 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4153 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4155 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4156 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4158 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4159 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4162 fitness = check_expectations(present_attrs, out_of_range_attr,
4163 expected_attrs, key, key_len);
4166 * Remove the TPID from dl_type since it's not the real Ethertype. */
4167 flow->dl_type = htons(0);
4168 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4169 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4172 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4173 return ODP_FIT_TOO_LITTLE;
4174 } else if (flow->vlan_tci == htons(0)) {
4175 /* Corner case for a truncated 802.1Q header. */
4176 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4177 return ODP_FIT_TOO_MUCH;
4180 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4181 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4182 "but CFI bit is not set", ntohs(flow->vlan_tci));
4183 return ODP_FIT_ERROR;
4186 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4191 /* Now parse the encapsulated attributes. */
4192 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4193 attrs, &present_attrs, &out_of_range_attr)) {
4194 return ODP_FIT_ERROR;
4198 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4199 return ODP_FIT_ERROR;
4201 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4202 expected_attrs, flow, key, key_len,
4205 /* The overall fitness is the worse of the outer and inner attributes. */
4206 return MAX(fitness, encap_fitness);
4209 static enum odp_key_fitness
4210 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4211 const struct nlattr *src_key, size_t src_key_len,
4212 struct flow *flow, const struct flow *src_flow)
4214 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4215 uint64_t expected_attrs;
4216 uint64_t present_attrs;
4217 int out_of_range_attr;
4218 bool is_mask = src_flow != flow;
4220 memset(flow, 0, sizeof *flow);
4222 /* Parse attributes. */
4223 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4224 &out_of_range_attr)) {
4225 return ODP_FIT_ERROR;
4230 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4231 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4232 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4233 } else if (is_mask) {
4234 /* Always exact match recirc_id if it is not specified. */
4235 flow->recirc_id = UINT32_MAX;
4238 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4239 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4240 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4242 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4243 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4244 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4247 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4248 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4249 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4252 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4253 enum odp_key_fitness res;
4255 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], src_key,
4256 src_key_len, &src_flow->tunnel,
4258 if (res == ODP_FIT_ERROR) {
4259 return ODP_FIT_ERROR;
4260 } else if (res == ODP_FIT_PERFECT) {
4261 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4265 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4266 flow->in_port.odp_port
4267 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4268 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4269 } else if (!is_mask) {
4270 flow->in_port.odp_port = ODPP_NONE;
4273 /* Ethernet header. */
4274 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4275 const struct ovs_key_ethernet *eth_key;
4277 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4278 put_ethernet_key(eth_key, flow);
4280 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4284 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4287 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4288 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4290 return ODP_FIT_ERROR;
4294 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4295 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4296 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4297 expected_attrs, flow, key, key_len, src_flow);
4300 flow->vlan_tci = htons(0xffff);
4301 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4302 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4303 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4306 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4307 expected_attrs, flow, key, key_len, src_flow);
4310 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4311 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4312 * 'key' fits our expectations for what a flow key should contain.
4314 * The 'in_port' will be the datapath's understanding of the port. The
4315 * caller will need to translate with odp_port_to_ofp_port() if the
4316 * OpenFlow port is needed.
4318 * This function doesn't take the packet itself as an argument because none of
4319 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4320 * it is always possible to infer which additional attribute(s) should appear
4321 * by looking at the attributes for lower-level protocols, e.g. if the network
4322 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4323 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4324 * must be absent. */
4325 enum odp_key_fitness
4326 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4329 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow);
4332 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4333 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
4334 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4335 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
4336 * well 'key' fits our expectations for what a flow key should contain. */
4337 enum odp_key_fitness
4338 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4339 const struct nlattr *flow_key, size_t flow_key_len,
4340 struct flow *mask, const struct flow *flow)
4342 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4346 /* Returns 'fitness' as a string, for use in debug messages. */
4348 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4351 case ODP_FIT_PERFECT:
4353 case ODP_FIT_TOO_MUCH:
4355 case ODP_FIT_TOO_LITTLE:
4356 return "too_little";
4364 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4365 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4366 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4367 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4368 * null, then the return value is not meaningful.) */
4370 odp_put_userspace_action(uint32_t pid,
4371 const void *userdata, size_t userdata_size,
4372 odp_port_t tunnel_out_port,
4373 struct ofpbuf *odp_actions)
4375 size_t userdata_ofs;
4378 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4379 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4381 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4383 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4384 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4387 * - The kernel rejected shorter userdata with -ERANGE.
4389 * - The kernel silently dropped userdata beyond the first 8 bytes.
4391 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4392 * separately disable features that required more than 8 bytes.) */
4393 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4394 MAX(8, userdata_size)),
4395 userdata, userdata_size);
4399 if (tunnel_out_port != ODPP_NONE) {
4400 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4403 nl_msg_end_nested(odp_actions, offset);
4405 return userdata_ofs;
4409 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4410 struct ofpbuf *odp_actions)
4412 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4413 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4414 nl_msg_end_nested(odp_actions, offset);
4418 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4419 struct ovs_action_push_tnl *data)
4421 int size = offsetof(struct ovs_action_push_tnl, header);
4423 size += data->header_len;
4424 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4428 /* The commit_odp_actions() function and its helpers. */
4431 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4432 const void *key, size_t key_size)
4434 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4435 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4436 nl_msg_end_nested(odp_actions, offset);
4439 /* Masked set actions have a mask following the data within the netlink
4440 * attribute. The unmasked bits in the data will be cleared as the data
4441 * is copied to the action. */
4443 commit_masked_set_action(struct ofpbuf *odp_actions,
4444 enum ovs_key_attr key_type,
4445 const void *key_, const void *mask_, size_t key_size)
4447 size_t offset = nl_msg_start_nested(odp_actions,
4448 OVS_ACTION_ATTR_SET_MASKED);
4449 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4450 const char *key = key_, *mask = mask_;
4452 memcpy(data + key_size, mask, key_size);
4453 /* Clear unmasked bits while copying. */
4454 while (key_size--) {
4455 *data++ = *key++ & *mask++;
4457 nl_msg_end_nested(odp_actions, offset);
4460 /* If any of the flow key data that ODP actions can modify are different in
4461 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4462 * 'odp_actions' that change the flow tunneling information in key from
4463 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4464 * same way. In other words, operates the same as commit_odp_actions(), but
4465 * only on tunneling information. */
4467 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4468 struct ofpbuf *odp_actions)
4470 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4471 if (flow->tunnel.ip_dst) {
4472 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4475 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4476 odp_put_tunnel_action(&base->tunnel, odp_actions);
4481 commit(enum ovs_key_attr attr, bool use_masked_set,
4482 const void *key, void *base, void *mask, size_t size,
4483 struct ofpbuf *odp_actions)
4485 if (memcmp(key, base, size)) {
4486 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4488 if (use_masked_set && !fully_masked) {
4489 commit_masked_set_action(odp_actions, attr, key, mask, size);
4491 if (!fully_masked) {
4492 memset(mask, 0xff, size);
4494 commit_set_action(odp_actions, attr, key, size);
4496 memcpy(base, key, size);
4499 /* Mask bits are set when we have either read or set the corresponding
4500 * values. Masked bits will be exact-matched, no need to set them
4501 * if the value did not actually change. */
4507 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4509 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4510 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4514 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4516 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4517 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4521 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4522 struct ofpbuf *odp_actions,
4523 struct flow_wildcards *wc,
4526 struct ovs_key_ethernet key, base, mask;
4528 get_ethernet_key(flow, &key);
4529 get_ethernet_key(base_flow, &base);
4530 get_ethernet_key(&wc->masks, &mask);
4532 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4533 &key, &base, &mask, sizeof key, odp_actions)) {
4534 put_ethernet_key(&base, base_flow);
4535 put_ethernet_key(&mask, &wc->masks);
4540 pop_vlan(struct flow *base,
4541 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4543 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4545 if (base->vlan_tci & htons(VLAN_CFI)) {
4546 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4552 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4553 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4555 if (base->vlan_tci == vlan_tci) {
4559 pop_vlan(base, odp_actions, wc);
4560 if (vlan_tci & htons(VLAN_CFI)) {
4561 struct ovs_action_push_vlan vlan;
4563 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4564 vlan.vlan_tci = vlan_tci;
4565 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4566 &vlan, sizeof vlan);
4568 base->vlan_tci = vlan_tci;
4571 /* Wildcarding already done at action translation time. */
4573 commit_mpls_action(const struct flow *flow, struct flow *base,
4574 struct ofpbuf *odp_actions)
4576 int base_n = flow_count_mpls_labels(base, NULL);
4577 int flow_n = flow_count_mpls_labels(flow, NULL);
4578 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4581 while (base_n > common_n) {
4582 if (base_n - 1 == common_n && flow_n > common_n) {
4583 /* If there is only one more LSE in base than there are common
4584 * between base and flow; and flow has at least one more LSE than
4585 * is common then the topmost LSE of base may be updated using
4587 struct ovs_key_mpls mpls_key;
4589 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4590 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4591 &mpls_key, sizeof mpls_key);
4592 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4595 /* Otherwise, if there more LSEs in base than are common between
4596 * base and flow then pop the topmost one. */
4600 /* If all the LSEs are to be popped and this is not the outermost
4601 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4602 * POP_MPLS action instead of flow->dl_type.
4604 * This is because the POP_MPLS action requires its ethertype
4605 * argument to be an MPLS ethernet type but in this case
4606 * flow->dl_type will be a non-MPLS ethernet type.
4608 * When the final POP_MPLS action occurs it use flow->dl_type and
4609 * the and the resulting packet will have the desired dl_type. */
4610 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4611 dl_type = htons(ETH_TYPE_MPLS);
4613 dl_type = flow->dl_type;
4615 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4616 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4622 /* If, after the above popping and setting, there are more LSEs in flow
4623 * than base then some LSEs need to be pushed. */
4624 while (base_n < flow_n) {
4625 struct ovs_action_push_mpls *mpls;
4627 mpls = nl_msg_put_unspec_zero(odp_actions,
4628 OVS_ACTION_ATTR_PUSH_MPLS,
4630 mpls->mpls_ethertype = flow->dl_type;
4631 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4632 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4633 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4639 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4641 ipv4->ipv4_src = flow->nw_src;
4642 ipv4->ipv4_dst = flow->nw_dst;
4643 ipv4->ipv4_proto = flow->nw_proto;
4644 ipv4->ipv4_tos = flow->nw_tos;
4645 ipv4->ipv4_ttl = flow->nw_ttl;
4646 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4650 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4652 flow->nw_src = ipv4->ipv4_src;
4653 flow->nw_dst = ipv4->ipv4_dst;
4654 flow->nw_proto = ipv4->ipv4_proto;
4655 flow->nw_tos = ipv4->ipv4_tos;
4656 flow->nw_ttl = ipv4->ipv4_ttl;
4657 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4661 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4662 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4665 struct ovs_key_ipv4 key, mask, base;
4667 /* Check that nw_proto and nw_frag remain unchanged. */
4668 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4669 flow->nw_frag == base_flow->nw_frag);
4671 get_ipv4_key(flow, &key, false);
4672 get_ipv4_key(base_flow, &base, false);
4673 get_ipv4_key(&wc->masks, &mask, true);
4674 mask.ipv4_proto = 0; /* Not writeable. */
4675 mask.ipv4_frag = 0; /* Not writable. */
4677 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4679 put_ipv4_key(&base, base_flow, false);
4680 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4681 put_ipv4_key(&mask, &wc->masks, true);
4687 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4689 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4690 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4691 ipv6->ipv6_label = flow->ipv6_label;
4692 ipv6->ipv6_proto = flow->nw_proto;
4693 ipv6->ipv6_tclass = flow->nw_tos;
4694 ipv6->ipv6_hlimit = flow->nw_ttl;
4695 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4699 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4701 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4702 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4703 flow->ipv6_label = ipv6->ipv6_label;
4704 flow->nw_proto = ipv6->ipv6_proto;
4705 flow->nw_tos = ipv6->ipv6_tclass;
4706 flow->nw_ttl = ipv6->ipv6_hlimit;
4707 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4711 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4712 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4715 struct ovs_key_ipv6 key, mask, base;
4717 /* Check that nw_proto and nw_frag remain unchanged. */
4718 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4719 flow->nw_frag == base_flow->nw_frag);
4721 get_ipv6_key(flow, &key, false);
4722 get_ipv6_key(base_flow, &base, false);
4723 get_ipv6_key(&wc->masks, &mask, true);
4724 mask.ipv6_proto = 0; /* Not writeable. */
4725 mask.ipv6_frag = 0; /* Not writable. */
4727 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4729 put_ipv6_key(&base, base_flow, false);
4730 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4731 put_ipv6_key(&mask, &wc->masks, true);
4737 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4739 /* ARP key has padding, clear it. */
4740 memset(arp, 0, sizeof *arp);
4742 arp->arp_sip = flow->nw_src;
4743 arp->arp_tip = flow->nw_dst;
4744 arp->arp_op = htons(flow->nw_proto);
4745 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4746 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4750 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4752 flow->nw_src = arp->arp_sip;
4753 flow->nw_dst = arp->arp_tip;
4754 flow->nw_proto = ntohs(arp->arp_op);
4755 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4756 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4759 static enum slow_path_reason
4760 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4761 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4763 struct ovs_key_arp key, mask, base;
4765 get_arp_key(flow, &key);
4766 get_arp_key(base_flow, &base);
4767 get_arp_key(&wc->masks, &mask);
4769 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4771 put_arp_key(&base, base_flow);
4772 put_arp_key(&mask, &wc->masks);
4779 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4781 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4782 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4783 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4784 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4788 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4790 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4791 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4792 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4793 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4796 static enum slow_path_reason
4797 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4798 struct ofpbuf *odp_actions,
4799 struct flow_wildcards *wc, bool use_masked)
4801 struct ovs_key_nd key, mask, base;
4803 get_nd_key(flow, &key);
4804 get_nd_key(base_flow, &base);
4805 get_nd_key(&wc->masks, &mask);
4807 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4809 put_nd_key(&base, base_flow);
4810 put_nd_key(&mask, &wc->masks);
4817 static enum slow_path_reason
4818 commit_set_nw_action(const struct flow *flow, struct flow *base,
4819 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4822 /* Check if 'flow' really has an L3 header. */
4823 if (!flow->nw_proto) {
4827 switch (ntohs(base->dl_type)) {
4829 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4833 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4834 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4837 return commit_set_arp_action(flow, base, odp_actions, wc);
4843 /* TCP, UDP, and SCTP keys have the same layout. */
4844 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4845 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4848 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4850 tp->tcp.tcp_src = flow->tp_src;
4851 tp->tcp.tcp_dst = flow->tp_dst;
4855 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4857 flow->tp_src = tp->tcp.tcp_src;
4858 flow->tp_dst = tp->tcp.tcp_dst;
4862 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4863 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4866 enum ovs_key_attr key_type;
4867 union ovs_key_tp key, mask, base;
4869 /* Check if 'flow' really has an L3 header. */
4870 if (!flow->nw_proto) {
4874 if (!is_ip_any(base_flow)) {
4878 if (flow->nw_proto == IPPROTO_TCP) {
4879 key_type = OVS_KEY_ATTR_TCP;
4880 } else if (flow->nw_proto == IPPROTO_UDP) {
4881 key_type = OVS_KEY_ATTR_UDP;
4882 } else if (flow->nw_proto == IPPROTO_SCTP) {
4883 key_type = OVS_KEY_ATTR_SCTP;
4888 get_tp_key(flow, &key);
4889 get_tp_key(base_flow, &base);
4890 get_tp_key(&wc->masks, &mask);
4892 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4894 put_tp_key(&base, base_flow);
4895 put_tp_key(&mask, &wc->masks);
4900 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4901 struct ofpbuf *odp_actions,
4902 struct flow_wildcards *wc,
4905 uint32_t key, mask, base;
4907 key = flow->skb_priority;
4908 base = base_flow->skb_priority;
4909 mask = wc->masks.skb_priority;
4911 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4912 sizeof key, odp_actions)) {
4913 base_flow->skb_priority = base;
4914 wc->masks.skb_priority = mask;
4919 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4920 struct ofpbuf *odp_actions,
4921 struct flow_wildcards *wc,
4924 uint32_t key, mask, base;
4926 key = flow->pkt_mark;
4927 base = base_flow->pkt_mark;
4928 mask = wc->masks.pkt_mark;
4930 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4931 sizeof key, odp_actions)) {
4932 base_flow->pkt_mark = base;
4933 wc->masks.pkt_mark = mask;
4937 /* If any of the flow key data that ODP actions can modify are different in
4938 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4939 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4940 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4941 * in addition to this function if needed. Sets fields in 'wc' that are
4942 * used as part of the action.
4944 * Returns a reason to force processing the flow's packets into the userspace
4945 * slow path, if there is one, otherwise 0. */
4946 enum slow_path_reason
4947 commit_odp_actions(const struct flow *flow, struct flow *base,
4948 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4951 enum slow_path_reason slow;
4953 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4954 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4955 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4956 commit_mpls_action(flow, base, odp_actions);
4957 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4958 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4959 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);