2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
38 #include "unaligned.h"
41 #include "openvswitch/vlog.h"
43 VLOG_DEFINE_THIS_MODULE(odp_util);
45 /* The interface between userspace and kernel uses an "OVS_*" prefix.
46 * Since this is fairly non-specific for the OVS userspace components,
47 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
48 * interactions with the datapath.
51 /* The set of characters that may separate one action or one key attribute
53 static const char *delimiters = ", \t\r\n";
57 const struct attr_len_tbl *next;
60 #define ATTR_LEN_INVALID -1
61 #define ATTR_LEN_VARIABLE -2
62 #define ATTR_LEN_NESTED -3
64 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
65 struct ofpbuf *, struct ofpbuf *);
66 static void format_odp_key_attr(const struct nlattr *a,
67 const struct nlattr *ma,
68 const struct hmap *portno_names, struct ds *ds,
71 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
72 int max, struct ofpbuf *,
73 const struct nlattr *key);
74 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
77 * - For an action whose argument has a fixed length, returned that
78 * nonnegative length in bytes.
80 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
82 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
84 odp_action_len(uint16_t type)
86 if (type > OVS_ACTION_ATTR_MAX) {
90 switch ((enum ovs_action_attr) type) {
91 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
92 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
93 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
94 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
95 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
96 case OVS_ACTION_ATTR_POP_VLAN: return 0;
97 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
98 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
99 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
100 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
101 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
102 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
103 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
105 case OVS_ACTION_ATTR_UNSPEC:
106 case __OVS_ACTION_ATTR_MAX:
107 return ATTR_LEN_INVALID;
110 return ATTR_LEN_INVALID;
113 /* Returns a string form of 'attr'. The return value is either a statically
114 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
115 * should be at least OVS_KEY_ATTR_BUFSIZE. */
116 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
118 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
121 case OVS_KEY_ATTR_UNSPEC: return "unspec";
122 case OVS_KEY_ATTR_ENCAP: return "encap";
123 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
124 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
125 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
126 case OVS_KEY_ATTR_IN_PORT: return "in_port";
127 case OVS_KEY_ATTR_ETHERNET: return "eth";
128 case OVS_KEY_ATTR_VLAN: return "vlan";
129 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
130 case OVS_KEY_ATTR_IPV4: return "ipv4";
131 case OVS_KEY_ATTR_IPV6: return "ipv6";
132 case OVS_KEY_ATTR_TCP: return "tcp";
133 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
134 case OVS_KEY_ATTR_UDP: return "udp";
135 case OVS_KEY_ATTR_SCTP: return "sctp";
136 case OVS_KEY_ATTR_ICMP: return "icmp";
137 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
138 case OVS_KEY_ATTR_ARP: return "arp";
139 case OVS_KEY_ATTR_ND: return "nd";
140 case OVS_KEY_ATTR_MPLS: return "mpls";
141 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
142 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
144 case __OVS_KEY_ATTR_MAX:
146 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
152 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
154 size_t len = nl_attr_get_size(a);
156 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
158 const uint8_t *unspec;
161 unspec = nl_attr_get(a);
162 for (i = 0; i < len; i++) {
163 ds_put_char(ds, i ? ' ': '(');
164 ds_put_format(ds, "%02x", unspec[i]);
166 ds_put_char(ds, ')');
171 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
173 static const struct nl_policy ovs_sample_policy[] = {
174 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
175 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
177 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
179 const struct nlattr *nla_acts;
182 ds_put_cstr(ds, "sample");
184 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
185 ds_put_cstr(ds, "(error)");
189 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
192 ds_put_format(ds, "(sample=%.1f%%,", percentage);
194 ds_put_cstr(ds, "actions(");
195 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
196 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
197 format_odp_actions(ds, nla_acts, len);
198 ds_put_format(ds, "))");
202 slow_path_reason_to_string(uint32_t reason)
204 switch ((enum slow_path_reason) reason) {
205 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
214 slow_path_reason_to_explanation(enum slow_path_reason reason)
217 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
226 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
227 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
232 /* Parse masked flags in numeric format? */
233 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
234 res_flags, res_mask, &n) && n > 0) {
235 if (*res_flags & ~allowed || *res_mask & ~allowed) {
243 if (res_mask && (*s == '+' || *s == '-')) {
244 uint32_t flags = 0, mask = 0;
246 /* Parse masked flags. */
247 while (s[0] != ')') {
254 } else if (s[0] == '-') {
262 name_len = strcspn(s, "+-)");
264 for (bit = 1; bit; bit <<= 1) {
265 const char *fname = bit_to_string(bit);
273 if (len != name_len) {
276 if (!strncmp(s, fname, len)) {
278 /* bit already set. */
281 if (!(bit & allowed)) {
293 return -EINVAL; /* Unknown flag name */
304 /* Parse unmasked flags. If a flag is present, it is set, otherwise
306 while (s[n] != ')') {
307 unsigned long long int flags;
311 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
312 if (flags & ~allowed) {
315 n += n0 + (s[n + n0] == ',');
320 for (bit = 1; bit; bit <<= 1) {
321 const char *name = bit_to_string(bit);
329 if (!strncmp(s + n, name, len) &&
330 (s[n + len] == ',' || s[n + len] == ')')) {
331 if (!(bit & allowed)) {
335 n += len + (s[n + len] == ',');
347 *res_mask = UINT32_MAX;
353 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
355 static const struct nl_policy ovs_userspace_policy[] = {
356 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
357 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
359 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
362 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
363 const struct nlattr *userdata_attr;
364 const struct nlattr *tunnel_out_port_attr;
366 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
367 ds_put_cstr(ds, "userspace(error)");
371 ds_put_format(ds, "userspace(pid=%"PRIu32,
372 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
374 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
377 const uint8_t *userdata = nl_attr_get(userdata_attr);
378 size_t userdata_len = nl_attr_get_size(userdata_attr);
379 bool userdata_unspec = true;
380 union user_action_cookie cookie;
382 if (userdata_len >= sizeof cookie.type
383 && userdata_len <= sizeof cookie) {
385 memset(&cookie, 0, sizeof cookie);
386 memcpy(&cookie, userdata, userdata_len);
388 userdata_unspec = false;
390 if (userdata_len == sizeof cookie.sflow
391 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
392 ds_put_format(ds, ",sFlow("
393 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
394 vlan_tci_to_vid(cookie.sflow.vlan_tci),
395 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
396 cookie.sflow.output);
397 } else if (userdata_len == sizeof cookie.slow_path
398 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
399 ds_put_cstr(ds, ",slow_path(");
400 format_flags(ds, slow_path_reason_to_string,
401 cookie.slow_path.reason, ',');
402 ds_put_format(ds, ")");
403 } else if (userdata_len == sizeof cookie.flow_sample
404 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
405 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
406 ",collector_set_id=%"PRIu32
407 ",obs_domain_id=%"PRIu32
408 ",obs_point_id=%"PRIu32")",
409 cookie.flow_sample.probability,
410 cookie.flow_sample.collector_set_id,
411 cookie.flow_sample.obs_domain_id,
412 cookie.flow_sample.obs_point_id);
413 } else if (userdata_len >= sizeof cookie.ipfix
414 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
415 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
416 cookie.ipfix.output_odp_port);
418 userdata_unspec = true;
422 if (userdata_unspec) {
424 ds_put_format(ds, ",userdata(");
425 for (i = 0; i < userdata_len; i++) {
426 ds_put_format(ds, "%02x", userdata[i]);
428 ds_put_char(ds, ')');
432 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
433 if (tunnel_out_port_attr) {
434 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
435 nl_attr_get_u32(tunnel_out_port_attr));
438 ds_put_char(ds, ')');
442 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
444 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
445 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
446 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
447 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
449 ds_put_char(ds, ',');
451 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
452 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
453 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
454 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
456 ds_put_char(ds, ',');
458 if (!(tci & htons(VLAN_CFI))) {
459 ds_put_cstr(ds, "cfi=0");
460 ds_put_char(ds, ',');
466 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
468 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
469 mpls_lse_to_label(mpls_lse),
470 mpls_lse_to_tc(mpls_lse),
471 mpls_lse_to_ttl(mpls_lse),
472 mpls_lse_to_bos(mpls_lse));
476 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
477 const struct ovs_key_mpls *mpls_mask, int n)
480 ovs_be32 key = mpls_key->mpls_lse;
482 if (mpls_mask == NULL) {
483 format_mpls_lse(ds, key);
485 ovs_be32 mask = mpls_mask->mpls_lse;
487 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
488 mpls_lse_to_label(key), mpls_lse_to_label(mask),
489 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
490 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
491 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
496 for (i = 0; i < n; i++) {
497 ds_put_format(ds, "lse%d=%#"PRIx32,
498 i, ntohl(mpls_key[i].mpls_lse));
500 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
502 ds_put_char(ds, ',');
509 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
511 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
515 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
517 ds_put_format(ds, "hash(");
519 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
520 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
522 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
525 ds_put_format(ds, ")");
529 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
531 const struct udp_header *udp;
533 udp = (const struct udp_header *) (ip + 1);
534 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
535 ntohs(udp->udp_src), ntohs(udp->udp_dst),
536 ntohs(udp->udp_csum));
542 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
544 const struct eth_header *eth;
545 const struct ip_header *ip;
548 eth = (const struct eth_header *)data->header;
551 ip = (const struct ip_header *)l3;
554 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
555 data->header_len, data->tnl_type);
556 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
557 ds_put_format(ds, ",src=");
558 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
559 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
562 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
563 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
564 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
565 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
566 ip->ip_proto, ip->ip_tos,
570 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
571 const struct vxlanhdr *vxh;
573 vxh = format_udp_tnl_push_header(ds, ip);
575 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
576 ntohl(get_16aligned_be32(&vxh->vx_flags)),
577 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
578 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
579 const struct genevehdr *gnh;
581 gnh = format_udp_tnl_push_header(ds, ip);
583 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
584 gnh->oam ? "oam," : "",
585 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
586 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
587 const struct gre_base_hdr *greh;
588 ovs_16aligned_be32 *options;
591 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
592 greh = (const struct gre_base_hdr *) l4;
594 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
595 ntohs(greh->flags), ntohs(greh->protocol));
596 options = (ovs_16aligned_be32 *)(greh + 1);
597 if (greh->flags & htons(GRE_CSUM)) {
598 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
601 if (greh->flags & htons(GRE_KEY)) {
602 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
605 if (greh->flags & htons(GRE_SEQ)) {
606 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
609 ds_put_format(ds, ")");
611 ds_put_format(ds, ")");
615 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
617 struct ovs_action_push_tnl *data;
619 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
621 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
622 format_odp_tnl_push_header(ds, data);
623 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
627 format_odp_action(struct ds *ds, const struct nlattr *a)
630 enum ovs_action_attr type = nl_attr_type(a);
631 const struct ovs_action_push_vlan *vlan;
634 expected_len = odp_action_len(nl_attr_type(a));
635 if (expected_len != ATTR_LEN_VARIABLE &&
636 nl_attr_get_size(a) != expected_len) {
637 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
638 nl_attr_get_size(a), expected_len);
639 format_generic_odp_action(ds, a);
644 case OVS_ACTION_ATTR_OUTPUT:
645 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
647 case OVS_ACTION_ATTR_TUNNEL_POP:
648 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
650 case OVS_ACTION_ATTR_TUNNEL_PUSH:
651 format_odp_tnl_push_action(ds, a);
653 case OVS_ACTION_ATTR_USERSPACE:
654 format_odp_userspace_action(ds, a);
656 case OVS_ACTION_ATTR_RECIRC:
657 format_odp_recirc_action(ds, nl_attr_get_u32(a));
659 case OVS_ACTION_ATTR_HASH:
660 format_odp_hash_action(ds, nl_attr_get(a));
662 case OVS_ACTION_ATTR_SET_MASKED:
664 size = nl_attr_get_size(a) / 2;
665 ds_put_cstr(ds, "set(");
667 /* Masked set action not supported for tunnel key, which is bigger. */
668 if (size <= sizeof(struct ovs_key_ipv6)) {
669 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
670 sizeof(struct nlattr))];
671 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
672 sizeof(struct nlattr))];
674 mask->nla_type = attr->nla_type = nl_attr_type(a);
675 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
676 memcpy(attr + 1, (char *)(a + 1), size);
677 memcpy(mask + 1, (char *)(a + 1) + size, size);
678 format_odp_key_attr(attr, mask, NULL, ds, false);
680 format_odp_key_attr(a, NULL, NULL, ds, false);
682 ds_put_cstr(ds, ")");
684 case OVS_ACTION_ATTR_SET:
685 ds_put_cstr(ds, "set(");
686 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
687 ds_put_cstr(ds, ")");
689 case OVS_ACTION_ATTR_PUSH_VLAN:
690 vlan = nl_attr_get(a);
691 ds_put_cstr(ds, "push_vlan(");
692 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
693 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
695 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
696 ds_put_char(ds, ')');
698 case OVS_ACTION_ATTR_POP_VLAN:
699 ds_put_cstr(ds, "pop_vlan");
701 case OVS_ACTION_ATTR_PUSH_MPLS: {
702 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
703 ds_put_cstr(ds, "push_mpls(");
704 format_mpls_lse(ds, mpls->mpls_lse);
705 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
708 case OVS_ACTION_ATTR_POP_MPLS: {
709 ovs_be16 ethertype = nl_attr_get_be16(a);
710 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
713 case OVS_ACTION_ATTR_SAMPLE:
714 format_odp_sample_action(ds, a);
716 case OVS_ACTION_ATTR_UNSPEC:
717 case __OVS_ACTION_ATTR_MAX:
719 format_generic_odp_action(ds, a);
725 format_odp_actions(struct ds *ds, const struct nlattr *actions,
729 const struct nlattr *a;
732 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
734 ds_put_char(ds, ',');
736 format_odp_action(ds, a);
741 if (left == actions_len) {
742 ds_put_cstr(ds, "<empty>");
744 ds_put_format(ds, ",***%u leftover bytes*** (", left);
745 for (i = 0; i < left; i++) {
746 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
748 ds_put_char(ds, ')');
751 ds_put_cstr(ds, "drop");
755 /* Separate out parse_odp_userspace_action() function. */
757 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
760 union user_action_cookie cookie;
762 odp_port_t tunnel_out_port;
764 void *user_data = NULL;
765 size_t user_data_size = 0;
767 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
773 uint32_t probability;
774 uint32_t collector_set_id;
775 uint32_t obs_domain_id;
776 uint32_t obs_point_id;
779 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
780 "pcp=%i,output=%"SCNi32")%n",
781 &vid, &pcp, &output, &n1)) {
785 tci = vid | (pcp << VLAN_PCP_SHIFT);
790 cookie.type = USER_ACTION_COOKIE_SFLOW;
791 cookie.sflow.vlan_tci = htons(tci);
792 cookie.sflow.output = output;
794 user_data_size = sizeof cookie.sflow;
795 } else if (ovs_scan(&s[n], ",slow_path(%n",
800 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
801 cookie.slow_path.unused = 0;
802 cookie.slow_path.reason = 0;
804 res = parse_flags(&s[n], slow_path_reason_to_string,
805 &cookie.slow_path.reason,
806 SLOW_PATH_REASON_MASK, NULL);
807 if (res < 0 || s[n + res] != ')') {
813 user_data_size = sizeof cookie.slow_path;
814 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
815 "collector_set_id=%"SCNi32","
816 "obs_domain_id=%"SCNi32","
817 "obs_point_id=%"SCNi32")%n",
818 &probability, &collector_set_id,
819 &obs_domain_id, &obs_point_id, &n1)) {
822 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
823 cookie.flow_sample.probability = probability;
824 cookie.flow_sample.collector_set_id = collector_set_id;
825 cookie.flow_sample.obs_domain_id = obs_domain_id;
826 cookie.flow_sample.obs_point_id = obs_point_id;
828 user_data_size = sizeof cookie.flow_sample;
829 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
832 cookie.type = USER_ACTION_COOKIE_IPFIX;
833 cookie.ipfix.output_odp_port = u32_to_odp(output);
835 user_data_size = sizeof cookie.ipfix;
836 } else if (ovs_scan(&s[n], ",userdata(%n",
841 ofpbuf_init(&buf, 16);
842 end = ofpbuf_put_hex(&buf, &s[n], NULL);
846 user_data = buf.data;
847 user_data_size = buf.size;
854 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
855 &tunnel_out_port, &n1)) {
856 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
858 } else if (s[n] == ')') {
859 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
868 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
870 struct eth_header *eth;
871 struct ip_header *ip;
872 struct udp_header *udp;
873 struct gre_base_hdr *greh;
874 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
876 uint32_t tnl_type = 0, header_len = 0;
880 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
883 eth = (struct eth_header *) data->header;
884 l3 = (data->header + sizeof *eth);
885 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
886 ip = (struct ip_header *) l3;
887 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
888 "eth(dst="ETH_ADDR_SCAN_FMT",",
891 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
895 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
896 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
899 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
902 eth->eth_type = htons(dl_type);
905 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
906 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
909 &ip->ip_proto, &ip->ip_tos,
910 &ip->ip_ttl, &ip->ip_frag_off)) {
913 put_16aligned_be32(&ip->ip_src, sip);
914 put_16aligned_be32(&ip->ip_dst, dip);
917 udp = (struct udp_header *) l4;
918 greh = (struct gre_base_hdr *) l4;
919 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
920 &udp_src, &udp_dst, &csum)) {
921 uint32_t vx_flags, vni;
923 udp->udp_src = htons(udp_src);
924 udp->udp_dst = htons(udp_dst);
926 udp->udp_csum = htons(csum);
928 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
930 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
932 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
933 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
934 tnl_type = OVS_VPORT_TYPE_VXLAN;
935 header_len = sizeof *eth + sizeof *ip +
936 sizeof *udp + sizeof *vxh;
937 } else if (ovs_scan_len(s, &n, "geneve(")) {
938 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
940 memset(gnh, 0, sizeof *gnh);
941 if (ovs_scan_len(s, &n, "oam,")) {
944 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
947 gnh->proto_type = htons(ETH_TYPE_TEB);
948 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
949 tnl_type = OVS_VPORT_TYPE_GENEVE;
950 header_len = sizeof *eth + sizeof *ip +
951 sizeof *udp + sizeof *gnh;
955 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
956 &gre_flags, &gre_proto)){
958 tnl_type = OVS_VPORT_TYPE_GRE;
959 greh->flags = htons(gre_flags);
960 greh->protocol = htons(gre_proto);
961 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
963 if (greh->flags & htons(GRE_CSUM)) {
964 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
968 memset(options, 0, sizeof *options);
969 *((ovs_be16 *)options) = htons(csum);
972 if (greh->flags & htons(GRE_KEY)) {
975 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
979 put_16aligned_be32(options, htonl(key));
982 if (greh->flags & htons(GRE_SEQ)) {
985 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
988 put_16aligned_be32(options, htonl(seq));
992 if (!ovs_scan_len(s, &n, "))")) {
996 header_len = sizeof *eth + sizeof *ip +
997 ((uint8_t *) options - (uint8_t *) greh);
1002 /* check tunnel meta data. */
1003 if (data->tnl_type != tnl_type) {
1006 if (data->header_len != header_len) {
1011 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1019 parse_odp_action(const char *s, const struct simap *port_names,
1020 struct ofpbuf *actions)
1026 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1027 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1033 int len = strcspn(s, delimiters);
1034 struct simap_node *node;
1036 node = simap_find_len(port_names, s, len);
1038 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1047 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1048 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1053 if (!strncmp(s, "userspace(", 10)) {
1054 return parse_odp_userspace_action(s, actions);
1057 if (!strncmp(s, "set(", 4)) {
1060 struct nlattr mask[128 / sizeof(struct nlattr)];
1061 struct ofpbuf maskbuf;
1062 struct nlattr *nested, *key;
1065 /* 'mask' is big enough to hold any key. */
1066 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1068 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1069 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1073 if (s[retval + 4] != ')') {
1077 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1080 size = nl_attr_get_size(mask);
1081 if (size == nl_attr_get_size(key)) {
1082 /* Change to masked set action if not fully masked. */
1083 if (!is_all_ones(mask + 1, size)) {
1084 key->nla_len += size;
1085 ofpbuf_put(actions, mask + 1, size);
1086 /* 'actions' may have been reallocated by ofpbuf_put(). */
1087 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1088 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1092 nl_msg_end_nested(actions, start_ofs);
1097 struct ovs_action_push_vlan push;
1098 int tpid = ETH_TYPE_VLAN;
1103 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1104 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1105 &vid, &pcp, &cfi, &n)
1106 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1107 &tpid, &vid, &pcp, &n)
1108 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1109 &tpid, &vid, &pcp, &cfi, &n)) {
1110 push.vlan_tpid = htons(tpid);
1111 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1112 | (pcp << VLAN_PCP_SHIFT)
1113 | (cfi ? VLAN_CFI : 0));
1114 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1115 &push, sizeof push);
1121 if (!strncmp(s, "pop_vlan", 8)) {
1122 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1130 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1131 && percentage >= 0. && percentage <= 100.0) {
1132 size_t sample_ofs, actions_ofs;
1135 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1136 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1137 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1138 (probability <= 0 ? 0
1139 : probability >= UINT32_MAX ? UINT32_MAX
1142 actions_ofs = nl_msg_start_nested(actions,
1143 OVS_SAMPLE_ATTR_ACTIONS);
1147 n += strspn(s + n, delimiters);
1152 retval = parse_odp_action(s + n, port_names, actions);
1158 nl_msg_end_nested(actions, actions_ofs);
1159 nl_msg_end_nested(actions, sample_ofs);
1161 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1169 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1170 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1176 struct ovs_action_push_tnl data;
1179 n = ovs_parse_tnl_push(s, &data);
1181 odp_put_tnl_push_action(actions, &data);
1190 /* Parses the string representation of datapath actions, in the format output
1191 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1192 * value. On success, the ODP actions are appended to 'actions' as a series of
1193 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1194 * way, 'actions''s data might be reallocated. */
1196 odp_actions_from_string(const char *s, const struct simap *port_names,
1197 struct ofpbuf *actions)
1201 if (!strcasecmp(s, "drop")) {
1205 old_size = actions->size;
1209 s += strspn(s, delimiters);
1214 retval = parse_odp_action(s, port_names, actions);
1215 if (retval < 0 || !strchr(delimiters, s[retval])) {
1216 actions->size = old_size;
1225 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1226 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1229 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1230 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1231 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1232 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1233 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1234 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1235 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1236 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1237 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1238 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1239 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1240 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1241 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1242 .next = ovs_vxlan_ext_attr_lens ,
1243 .next_max = OVS_VXLAN_EXT_MAX},
1246 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1247 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1248 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1249 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1250 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1251 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1252 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1253 .next = ovs_tun_key_attr_lens,
1254 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1255 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1256 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1257 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1258 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1259 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1260 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1261 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1262 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1263 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1264 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1265 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1266 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1267 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1268 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1269 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1272 /* Returns the correct length of the payload for a flow key attribute of the
1273 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1274 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1275 * payload is a nested type. */
1277 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1279 if (type > max_len) {
1280 return ATTR_LEN_INVALID;
1283 return tbl[type].len;
1287 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1289 size_t len = nl_attr_get_size(a);
1291 const uint8_t *unspec;
1294 unspec = nl_attr_get(a);
1295 for (i = 0; i < len; i++) {
1297 ds_put_char(ds, ' ');
1299 ds_put_format(ds, "%02x", unspec[i]);
1305 ovs_frag_type_to_string(enum ovs_frag_type type)
1308 case OVS_FRAG_TYPE_NONE:
1310 case OVS_FRAG_TYPE_FIRST:
1312 case OVS_FRAG_TYPE_LATER:
1314 case __OVS_FRAG_TYPE_MAX:
1320 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1322 parse_geneve_opts(const struct nlattr *attr)
1324 int opts_len = nl_attr_get_size(attr);
1325 const struct geneve_opt *opt = nl_attr_get(attr);
1327 while (opts_len > 0) {
1330 if (opts_len < sizeof(*opt)) {
1334 len = sizeof(*opt) + opt->length * 4;
1335 if (len > opts_len) {
1339 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1341 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1346 opt = opt + len / sizeof(*opt);
1353 static enum odp_key_fitness
1354 odp_tun_key_from_attr__(const struct nlattr *attr,
1355 const struct nlattr *flow_attrs OVS_UNUSED,
1356 size_t flow_attr_len OVS_UNUSED,
1357 const struct flow_tnl *src_tun OVS_UNUSED,
1358 struct flow_tnl *tun)
1361 const struct nlattr *a;
1363 bool unknown = false;
1365 NL_NESTED_FOR_EACH(a, left, attr) {
1366 uint16_t type = nl_attr_type(a);
1367 size_t len = nl_attr_get_size(a);
1368 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1369 OVS_TUNNEL_ATTR_MAX, type);
1371 if (len != expected_len && expected_len >= 0) {
1372 return ODP_FIT_ERROR;
1376 case OVS_TUNNEL_KEY_ATTR_ID:
1377 tun->tun_id = nl_attr_get_be64(a);
1378 tun->flags |= FLOW_TNL_F_KEY;
1380 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1381 tun->ip_src = nl_attr_get_be32(a);
1383 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1384 tun->ip_dst = nl_attr_get_be32(a);
1386 case OVS_TUNNEL_KEY_ATTR_TOS:
1387 tun->ip_tos = nl_attr_get_u8(a);
1389 case OVS_TUNNEL_KEY_ATTR_TTL:
1390 tun->ip_ttl = nl_attr_get_u8(a);
1393 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1394 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1396 case OVS_TUNNEL_KEY_ATTR_CSUM:
1397 tun->flags |= FLOW_TNL_F_CSUM;
1399 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1400 tun->tp_src = nl_attr_get_be16(a);
1402 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1403 tun->tp_dst = nl_attr_get_be16(a);
1405 case OVS_TUNNEL_KEY_ATTR_OAM:
1406 tun->flags |= FLOW_TNL_F_OAM;
1408 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1409 static const struct nl_policy vxlan_opts_policy[] = {
1410 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1412 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1414 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1415 return ODP_FIT_ERROR;
1418 if (ext[OVS_VXLAN_EXT_GBP]) {
1419 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1421 tun->gbp_id = htons(gbp & 0xFFFF);
1422 tun->gbp_flags = (gbp >> 16) & 0xFF;
1427 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1428 if (parse_geneve_opts(a)) {
1429 return ODP_FIT_ERROR;
1431 /* It is necessary to reproduce options exactly (including order)
1432 * so it's easiest to just echo them back. */
1437 /* Allow this to show up as unexpected, if there are unknown
1438 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1445 return ODP_FIT_ERROR;
1448 return ODP_FIT_TOO_MUCH;
1450 return ODP_FIT_PERFECT;
1453 enum odp_key_fitness
1454 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1456 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun);
1460 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1461 const struct flow_tnl *tun_flow_key OVS_UNUSED,
1462 const struct ofpbuf *key_buf OVS_UNUSED)
1466 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1468 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1469 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1470 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1472 if (tun_key->ip_src) {
1473 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1475 if (tun_key->ip_dst) {
1476 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1478 if (tun_key->ip_tos) {
1479 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1481 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1482 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1483 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1485 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1486 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1488 if (tun_key->tp_src) {
1489 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1491 if (tun_key->tp_dst) {
1492 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1494 if (tun_key->flags & FLOW_TNL_F_OAM) {
1495 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1497 if (tun_key->gbp_flags || tun_key->gbp_id) {
1498 size_t vxlan_opts_ofs;
1500 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1501 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1502 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1503 nl_msg_end_nested(a, vxlan_opts_ofs);
1506 nl_msg_end_nested(a, tun_key_ofs);
1510 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1512 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1516 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1518 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1519 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1521 if (attr == OVS_KEY_ATTR_IPV6) {
1522 const struct ovs_key_ipv6 *ipv6_mask = mask;
1525 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1526 == htonl(IPV6_LABEL_MASK))
1527 && ipv6_mask->ipv6_proto == UINT8_MAX
1528 && ipv6_mask->ipv6_tclass == UINT8_MAX
1529 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1530 && ipv6_mask->ipv6_frag == UINT8_MAX
1531 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1532 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1534 if (attr == OVS_KEY_ATTR_TUNNEL) {
1538 if (attr == OVS_KEY_ATTR_ARP) {
1539 /* ARP key has padding, ignore it. */
1540 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1541 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1542 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1543 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1546 return is_all_ones(mask, size);
1550 odp_mask_attr_is_exact(const struct nlattr *ma)
1552 enum ovs_key_attr attr = nl_attr_type(ma);
1556 if (attr == OVS_KEY_ATTR_TUNNEL) {
1559 mask = nl_attr_get(ma);
1560 size = nl_attr_get_size(ma);
1563 return odp_mask_is_exact(attr, mask, size);
1567 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1570 struct odp_portno_names *odp_portno_names;
1572 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1573 odp_portno_names->port_no = port_no;
1574 odp_portno_names->name = xstrdup(port_name);
1575 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1576 hash_odp_port(port_no));
1580 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1582 struct odp_portno_names *odp_portno_names;
1584 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1585 hash_odp_port(port_no), portno_names) {
1586 if (odp_portno_names->port_no == port_no) {
1587 return odp_portno_names->name;
1594 odp_portno_names_destroy(struct hmap *portno_names)
1596 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1597 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1598 hmap_node, portno_names) {
1599 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1600 free(odp_portno_names->name);
1601 free(odp_portno_names);
1605 /* Format helpers. */
1608 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1609 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1611 bool mask_empty = mask && eth_addr_is_zero(*mask);
1613 if (verbose || !mask_empty) {
1614 bool mask_full = !mask || eth_mask_is_exact(*mask);
1617 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1619 ds_put_format(ds, "%s=", name);
1620 eth_format_masked(key, *mask, ds);
1621 ds_put_char(ds, ',');
1627 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1628 const ovs_be64 *mask, bool verbose)
1630 bool mask_empty = mask && !*mask;
1632 if (verbose || !mask_empty) {
1633 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1635 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1636 if (!mask_full) { /* Partially masked. */
1637 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1639 ds_put_char(ds, ',');
1644 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1645 const ovs_be32 *mask, bool verbose)
1647 bool mask_empty = mask && !*mask;
1649 if (verbose || !mask_empty) {
1650 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1652 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1653 if (!mask_full) { /* Partially masked. */
1654 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1656 ds_put_char(ds, ',');
1661 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1662 const ovs_be32 (*mask_)[4], bool verbose)
1664 char buf[INET6_ADDRSTRLEN];
1665 const struct in6_addr *key = (const struct in6_addr *)key_;
1666 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1668 bool mask_empty = mask && ipv6_mask_is_any(mask);
1670 if (verbose || !mask_empty) {
1671 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1673 inet_ntop(AF_INET6, key, buf, sizeof buf);
1674 ds_put_format(ds, "%s=%s", name, buf);
1675 if (!mask_full) { /* Partially masked. */
1676 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1677 ds_put_format(ds, "/%s", buf);
1679 ds_put_char(ds, ',');
1684 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1685 const ovs_be32 *mask, bool verbose)
1687 bool mask_empty = mask && !*mask;
1689 if (verbose || !mask_empty) {
1690 bool mask_full = !mask
1691 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1693 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1694 if (!mask_full) { /* Partially masked. */
1695 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1697 ds_put_char(ds, ',');
1702 format_u8x(struct ds *ds, const char *name, uint8_t key,
1703 const uint8_t *mask, bool verbose)
1705 bool mask_empty = mask && !*mask;
1707 if (verbose || !mask_empty) {
1708 bool mask_full = !mask || *mask == UINT8_MAX;
1710 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1711 if (!mask_full) { /* Partially masked. */
1712 ds_put_format(ds, "/%#"PRIx8, *mask);
1714 ds_put_char(ds, ',');
1719 format_u8u(struct ds *ds, const char *name, uint8_t key,
1720 const uint8_t *mask, bool verbose)
1722 bool mask_empty = mask && !*mask;
1724 if (verbose || !mask_empty) {
1725 bool mask_full = !mask || *mask == UINT8_MAX;
1727 ds_put_format(ds, "%s=%"PRIu8, name, key);
1728 if (!mask_full) { /* Partially masked. */
1729 ds_put_format(ds, "/%#"PRIx8, *mask);
1731 ds_put_char(ds, ',');
1736 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1737 const ovs_be16 *mask, bool verbose)
1739 bool mask_empty = mask && !*mask;
1741 if (verbose || !mask_empty) {
1742 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1744 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1745 if (!mask_full) { /* Partially masked. */
1746 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1748 ds_put_char(ds, ',');
1753 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1754 const ovs_be16 *mask, bool verbose)
1756 bool mask_empty = mask && !*mask;
1758 if (verbose || !mask_empty) {
1759 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1761 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1762 if (!mask_full) { /* Partially masked. */
1763 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1765 ds_put_char(ds, ',');
1770 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1771 const uint16_t *mask, bool verbose)
1773 bool mask_empty = mask && !*mask;
1775 if (verbose || !mask_empty) {
1776 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1778 ds_put_cstr(ds, name);
1779 ds_put_char(ds, '(');
1780 if (!mask_full) { /* Partially masked. */
1781 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1782 } else { /* Fully masked. */
1783 format_flags(ds, flow_tun_flag_to_string, key, ',');
1785 ds_put_cstr(ds, "),");
1790 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1791 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1795 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1796 if (expected_len != ATTR_LEN_VARIABLE &&
1797 expected_len != ATTR_LEN_NESTED) {
1799 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1800 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1802 if (bad_key_len || bad_mask_len) {
1804 ds_put_format(ds, "key%u", nl_attr_type(a));
1807 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1808 nl_attr_get_size(a), expected_len);
1810 format_generic_odp_key(a, ds);
1812 ds_put_char(ds, '/');
1814 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1815 nl_attr_get_size(ma), expected_len);
1817 format_generic_odp_key(ma, ds);
1819 ds_put_char(ds, ')');
1828 format_unknown_key(struct ds *ds, const struct nlattr *a,
1829 const struct nlattr *ma)
1831 ds_put_format(ds, "key%u(", nl_attr_type(a));
1832 format_generic_odp_key(a, ds);
1833 if (ma && !odp_mask_attr_is_exact(ma)) {
1834 ds_put_char(ds, '/');
1835 format_generic_odp_key(ma, ds);
1837 ds_put_cstr(ds, "),");
1841 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1842 const struct nlattr *mask_attr, struct ds *ds,
1846 const struct nlattr *a;
1849 ofpbuf_init(&ofp, 100);
1850 NL_NESTED_FOR_EACH(a, left, attr) {
1851 uint16_t type = nl_attr_type(a);
1852 const struct nlattr *ma = NULL;
1855 ma = nl_attr_find__(nl_attr_get(mask_attr),
1856 nl_attr_get_size(mask_attr), type);
1858 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1864 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1865 OVS_VXLAN_EXT_MAX, true)) {
1870 case OVS_VXLAN_EXT_GBP: {
1871 uint32_t key = nl_attr_get_u32(a);
1872 ovs_be16 id, id_mask;
1873 uint8_t flags, flags_mask;
1875 id = htons(key & 0xFFFF);
1876 flags = (key >> 16) & 0xFF;
1878 uint32_t mask = nl_attr_get_u32(ma);
1879 id_mask = htons(mask & 0xFFFF);
1880 flags_mask = (mask >> 16) & 0xFF;
1883 ds_put_cstr(ds, "gbp(");
1884 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1885 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1887 ds_put_cstr(ds, "),");
1892 format_unknown_key(ds, a, ma);
1898 ofpbuf_uninit(&ofp);
1901 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1904 format_odp_tun_geneve(const struct nlattr *attr,
1905 const struct nlattr *mask_attr, struct ds *ds,
1908 int opts_len = nl_attr_get_size(attr);
1909 const struct geneve_opt *opt = nl_attr_get(attr);
1910 const struct geneve_opt *mask = mask_attr ?
1911 nl_attr_get(mask_attr) : NULL;
1913 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1914 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1915 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1919 while (opts_len > 0) {
1921 uint8_t data_len, data_len_mask;
1923 if (opts_len < sizeof *opt) {
1924 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1925 opts_len, sizeof *opt);
1929 data_len = opt->length * 4;
1931 if (mask->length == 0x1f) {
1932 data_len_mask = UINT8_MAX;
1934 data_len_mask = mask->length;
1937 len = sizeof *opt + data_len;
1938 if (len > opts_len) {
1939 ds_put_format(ds, "opt len %u greater than remaining %u",
1944 ds_put_char(ds, '{');
1945 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1947 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1948 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1949 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1950 ds_put_hex(ds, opt + 1, data_len);
1951 if (mask && !is_all_ones(mask + 1, data_len)) {
1952 ds_put_char(ds, '/');
1953 ds_put_hex(ds, mask + 1, data_len);
1958 ds_put_char(ds, '}');
1960 opt += len / sizeof(*opt);
1962 mask += len / sizeof(*opt);
1969 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1970 struct ds *ds, bool verbose)
1973 const struct nlattr *a;
1975 uint16_t mask_flags = 0;
1978 ofpbuf_init(&ofp, 100);
1979 NL_NESTED_FOR_EACH(a, left, attr) {
1980 enum ovs_tunnel_key_attr type = nl_attr_type(a);
1981 const struct nlattr *ma = NULL;
1984 ma = nl_attr_find__(nl_attr_get(mask_attr),
1985 nl_attr_get_size(mask_attr), type);
1987 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
1988 OVS_TUNNEL_KEY_ATTR_MAX,
1993 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
1994 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
1999 case OVS_TUNNEL_KEY_ATTR_ID:
2000 format_be64(ds, "tun_id", nl_attr_get_be64(a),
2001 ma ? nl_attr_get(ma) : NULL, verbose);
2002 flags |= FLOW_TNL_F_KEY;
2004 mask_flags |= FLOW_TNL_F_KEY;
2007 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2008 format_ipv4(ds, "src", nl_attr_get_be32(a),
2009 ma ? nl_attr_get(ma) : NULL, verbose);
2011 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2012 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2013 ma ? nl_attr_get(ma) : NULL, verbose);
2015 case OVS_TUNNEL_KEY_ATTR_TOS:
2016 format_u8x(ds, "tos", nl_attr_get_u8(a),
2017 ma ? nl_attr_get(ma) : NULL, verbose);
2019 case OVS_TUNNEL_KEY_ATTR_TTL:
2020 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2021 ma ? nl_attr_get(ma) : NULL, verbose);
2023 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2024 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2026 case OVS_TUNNEL_KEY_ATTR_CSUM:
2027 flags |= FLOW_TNL_F_CSUM;
2029 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2030 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2031 ma ? nl_attr_get(ma) : NULL, verbose);
2033 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2034 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2035 ma ? nl_attr_get(ma) : NULL, verbose);
2037 case OVS_TUNNEL_KEY_ATTR_OAM:
2038 flags |= FLOW_TNL_F_OAM;
2040 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2041 ds_put_cstr(ds, "vxlan(");
2042 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2043 ds_put_cstr(ds, "),");
2045 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2046 ds_put_cstr(ds, "geneve(");
2047 format_odp_tun_geneve(a, ma, ds, verbose);
2048 ds_put_cstr(ds, "),");
2050 case __OVS_TUNNEL_KEY_ATTR_MAX:
2052 format_unknown_key(ds, a, ma);
2057 /* Flags can have a valid mask even if the attribute is not set, so
2058 * we need to collect these separately. */
2060 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2061 switch (nl_attr_type(a)) {
2062 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2063 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2065 case OVS_TUNNEL_KEY_ATTR_CSUM:
2066 mask_flags |= FLOW_TNL_F_CSUM;
2068 case OVS_TUNNEL_KEY_ATTR_OAM:
2069 mask_flags |= FLOW_TNL_F_OAM;
2075 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2078 ofpbuf_uninit(&ofp);
2082 format_frag(struct ds *ds, const char *name, uint8_t key,
2083 const uint8_t *mask, bool verbose)
2085 bool mask_empty = mask && !*mask;
2087 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2088 if (verbose || !mask_empty) {
2089 bool mask_full = !mask || *mask == UINT8_MAX;
2091 if (!mask_full) { /* Partially masked. */
2092 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2095 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2101 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2102 const struct hmap *portno_names, struct ds *ds,
2105 enum ovs_key_attr attr = nl_attr_type(a);
2106 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2109 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2111 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2113 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2114 OVS_KEY_ATTR_MAX, false)) {
2118 ds_put_char(ds, '(');
2120 case OVS_KEY_ATTR_ENCAP:
2121 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2122 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2123 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2125 } else if (nl_attr_get_size(a)) {
2126 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2131 case OVS_KEY_ATTR_PRIORITY:
2132 case OVS_KEY_ATTR_SKB_MARK:
2133 case OVS_KEY_ATTR_DP_HASH:
2134 case OVS_KEY_ATTR_RECIRC_ID:
2135 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2137 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2141 case OVS_KEY_ATTR_TUNNEL:
2142 format_odp_tun_attr(a, ma, ds, verbose);
2145 case OVS_KEY_ATTR_IN_PORT:
2146 if (portno_names && verbose && is_exact) {
2147 char *name = odp_portno_names_get(portno_names,
2148 u32_to_odp(nl_attr_get_u32(a)));
2150 ds_put_format(ds, "%s", name);
2152 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2155 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2157 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2162 case OVS_KEY_ATTR_ETHERNET: {
2163 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2164 const struct ovs_key_ethernet *key = nl_attr_get(a);
2166 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2167 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2171 case OVS_KEY_ATTR_VLAN:
2172 format_vlan_tci(ds, nl_attr_get_be16(a),
2173 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2176 case OVS_KEY_ATTR_MPLS: {
2177 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2178 const struct ovs_key_mpls *mpls_mask = NULL;
2179 size_t size = nl_attr_get_size(a);
2181 if (!size || size % sizeof *mpls_key) {
2182 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2186 mpls_mask = nl_attr_get(ma);
2187 if (size != nl_attr_get_size(ma)) {
2188 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2189 "mask length %"PRIuSIZE")",
2190 size, nl_attr_get_size(ma));
2194 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2197 case OVS_KEY_ATTR_ETHERTYPE:
2198 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2200 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2204 case OVS_KEY_ATTR_IPV4: {
2205 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2206 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2208 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2209 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2210 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2212 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2213 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2214 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2219 case OVS_KEY_ATTR_IPV6: {
2220 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2221 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2223 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2224 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2225 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2227 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2229 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2231 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2233 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2238 /* These have the same structure and format. */
2239 case OVS_KEY_ATTR_TCP:
2240 case OVS_KEY_ATTR_UDP:
2241 case OVS_KEY_ATTR_SCTP: {
2242 const struct ovs_key_tcp *key = nl_attr_get(a);
2243 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2245 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2246 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2250 case OVS_KEY_ATTR_TCP_FLAGS:
2252 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2253 ntohs(nl_attr_get_be16(a)),
2254 ntohs(nl_attr_get_be16(ma)));
2256 format_flags(ds, packet_tcp_flag_to_string,
2257 ntohs(nl_attr_get_be16(a)), ',');
2261 case OVS_KEY_ATTR_ICMP: {
2262 const struct ovs_key_icmp *key = nl_attr_get(a);
2263 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2265 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2266 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2270 case OVS_KEY_ATTR_ICMPV6: {
2271 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2272 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2274 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2276 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2281 case OVS_KEY_ATTR_ARP: {
2282 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2283 const struct ovs_key_arp *key = nl_attr_get(a);
2285 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2286 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2287 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2288 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2289 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2293 case OVS_KEY_ATTR_ND: {
2294 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2295 const struct ovs_key_nd *key = nl_attr_get(a);
2297 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2299 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2300 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2305 case OVS_KEY_ATTR_UNSPEC:
2306 case __OVS_KEY_ATTR_MAX:
2308 format_generic_odp_key(a, ds);
2310 ds_put_char(ds, '/');
2311 format_generic_odp_key(ma, ds);
2315 ds_put_char(ds, ')');
2318 static struct nlattr *
2319 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2320 struct ofpbuf *ofp, const struct nlattr *key)
2322 const struct nlattr *a;
2324 int type = nl_attr_type(key);
2325 int size = nl_attr_get_size(key);
2327 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2328 nl_msg_put_unspec_zero(ofp, type, size);
2332 if (tbl[type].next) {
2333 tbl = tbl[type].next;
2334 max = tbl[type].next_max;
2337 nested_mask = nl_msg_start_nested(ofp, type);
2338 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2339 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2341 nl_msg_end_nested(ofp, nested_mask);
2348 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2352 if (ovs_scan(s, "ufid:")) {
2355 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2367 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2369 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2372 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2373 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2374 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2375 * non-null and 'verbose' is true, translates odp port number to its name. */
2377 odp_flow_format(const struct nlattr *key, size_t key_len,
2378 const struct nlattr *mask, size_t mask_len,
2379 const struct hmap *portno_names, struct ds *ds, bool verbose)
2382 const struct nlattr *a;
2384 bool has_ethtype_key = false;
2385 const struct nlattr *ma = NULL;
2387 bool first_field = true;
2389 ofpbuf_init(&ofp, 100);
2390 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2391 bool is_nested_attr;
2392 bool is_wildcard = false;
2393 int attr_type = nl_attr_type(a);
2395 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2396 has_ethtype_key = true;
2399 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2400 OVS_KEY_ATTR_MAX, attr_type) ==
2403 if (mask && mask_len) {
2404 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2405 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2408 if (verbose || !is_wildcard || is_nested_attr) {
2409 if (is_wildcard && !ma) {
2410 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2415 ds_put_char(ds, ',');
2417 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2418 first_field = false;
2422 ofpbuf_uninit(&ofp);
2427 if (left == key_len) {
2428 ds_put_cstr(ds, "<empty>");
2430 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2431 for (i = 0; i < left; i++) {
2432 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2434 ds_put_char(ds, ')');
2436 if (!has_ethtype_key) {
2437 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2439 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2440 ntohs(nl_attr_get_be16(ma)));
2444 ds_put_cstr(ds, "<empty>");
2448 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2449 * OVS_KEY_ATTR_* attributes in 'key'. */
2451 odp_flow_key_format(const struct nlattr *key,
2452 size_t key_len, struct ds *ds)
2454 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2458 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2460 if (!strcasecmp(s, "no")) {
2461 *type = OVS_FRAG_TYPE_NONE;
2462 } else if (!strcasecmp(s, "first")) {
2463 *type = OVS_FRAG_TYPE_FIRST;
2464 } else if (!strcasecmp(s, "later")) {
2465 *type = OVS_FRAG_TYPE_LATER;
2475 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2476 uint8_t (*mask)[ETH_ADDR_LEN])
2480 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2484 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2485 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2488 memset(mask, 0xff, sizeof *mask);
2497 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2501 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2505 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2506 IP_SCAN_ARGS(mask), &n)) {
2509 *mask = OVS_BE32_MAX;
2518 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2521 char ipv6_s[IPV6_SCAN_LEN + 1];
2523 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2524 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2528 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2529 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2532 memset(mask, 0xff, sizeof *mask);
2541 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2546 if (ovs_scan(s, "%i%n", &key_, &n)
2547 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2552 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2553 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2555 *mask = htonl(mask_);
2557 *mask = htonl(IPV6_LABEL_MASK);
2566 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2570 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2574 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2586 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2590 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2594 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2606 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2608 uint16_t key_, mask_;
2611 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2616 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2618 *mask = htons(mask_);
2620 *mask = OVS_BE16_MAX;
2629 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2631 uint64_t key_, mask_;
2634 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2637 *key = htonll(key_);
2639 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2641 *mask = htonll(mask_);
2643 *mask = OVS_BE64_MAX;
2652 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2654 uint32_t flags, fmask;
2657 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2658 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2659 if (n >= 0 && s[n] == ')') {
2670 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2672 uint32_t flags, fmask;
2675 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2676 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2678 *key = htons(flags);
2680 *mask = htons(fmask);
2688 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2692 enum ovs_frag_type frag_type;
2694 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2695 && ovs_frag_type_from_string(frag, &frag_type)) {
2708 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2709 const struct simap *port_names)
2713 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2717 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2724 } else if (port_names) {
2725 const struct simap_node *node;
2728 len = strcspn(s, ")");
2729 node = simap_find_len(port_names, s, len);
2742 /* Helper for vlan parsing. */
2743 struct ovs_key_vlan__ {
2748 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2750 const uint16_t mask = ((1U << bits) - 1) << offset;
2752 if (value >> bits) {
2756 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2761 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2764 uint16_t key_, mask_;
2767 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2770 if (set_be16_bf(key, bits, offset, key_)) {
2772 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2775 if (!set_be16_bf(mask, bits, offset, mask_)) {
2779 *mask |= htons(((1U << bits) - 1) << offset);
2789 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2791 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2795 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2797 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2801 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2803 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2808 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2810 const uint32_t mask = ((1U << bits) - 1) << offset;
2812 if (value >> bits) {
2816 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2821 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2824 uint32_t key_, mask_;
2827 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2830 if (set_be32_bf(key, bits, offset, key_)) {
2832 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2835 if (!set_be32_bf(mask, bits, offset, mask_)) {
2839 *mask |= htonl(((1U << bits) - 1) << offset);
2849 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2851 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2855 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2857 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2861 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2863 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2867 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2869 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2873 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2875 const char *s_base = s;
2876 ovs_be16 id = 0, id_mask = 0;
2877 uint8_t flags = 0, flags_mask = 0;
2879 if (!strncmp(s, "id=", 3)) {
2881 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2887 if (!strncmp(s, "flags=", 6)) {
2889 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2892 if (!strncmp(s, "))", 2)) {
2895 *key = (flags << 16) | ntohs(id);
2897 *mask = (flags_mask << 16) | ntohs(id_mask);
2906 struct geneve_scan {
2907 struct geneve_opt d[63];
2912 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2914 const char *s_base = s;
2915 struct geneve_opt *opt = key->d;
2916 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2917 int len_remain = sizeof key->d;
2919 while (s[0] == '{' && len_remain >= sizeof *opt) {
2923 len_remain -= sizeof *opt;
2925 if (!strncmp(s, "class=", 6)) {
2927 s += scan_be16(s, &opt->opt_class,
2928 mask ? &opt_mask->opt_class : NULL);
2930 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2936 if (!strncmp(s, "type=", 5)) {
2938 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2940 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2946 if (!strncmp(s, "len=", 4)) {
2947 uint8_t opt_len, opt_len_mask;
2949 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2951 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2954 opt->length = opt_len / 4;
2956 opt_mask->length = opt_len_mask;
2960 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2966 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2973 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2974 data_len, (char **)&s)) {
2985 opt += 1 + data_len / 4;
2987 opt_mask += 1 + data_len / 4;
2989 len_remain -= data_len;
2994 int len = sizeof key->d - len_remain;
3008 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3010 const uint16_t *flags = data_;
3012 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3013 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3015 if (*flags & FLOW_TNL_F_CSUM) {
3016 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3018 if (*flags & FLOW_TNL_F_OAM) {
3019 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3024 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3026 const uint32_t *gbp = data_;
3029 size_t vxlan_opts_ofs;
3031 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3032 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3033 nl_msg_end_nested(a, vxlan_opts_ofs);
3038 geneve_to_attr(struct ofpbuf *a, const void *data_)
3040 const struct geneve_scan *geneve = data_;
3042 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3046 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3048 unsigned long call_fn = (unsigned long)FUNC; \
3050 typedef void (*fn)(struct ofpbuf *, const void *); \
3052 func(BUF, &(DATA)); \
3054 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3058 #define SCAN_IF(NAME) \
3059 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3060 const char *start = s; \
3065 /* Usually no special initialization is needed. */
3066 #define SCAN_BEGIN(NAME, TYPE) \
3069 memset(&skey, 0, sizeof skey); \
3070 memset(&smask, 0, sizeof smask); \
3074 /* Init as fully-masked as mask will not be scanned. */
3075 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3078 memset(&skey, 0, sizeof skey); \
3079 memset(&smask, 0xff, sizeof smask); \
3083 /* VLAN needs special initialization. */
3084 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3086 TYPE skey = KEY_INIT; \
3087 TYPE smask = MASK_INIT; \
3091 /* Scan unnamed entry as 'TYPE' */
3092 #define SCAN_TYPE(TYPE, KEY, MASK) \
3093 len = scan_##TYPE(s, KEY, MASK); \
3099 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3100 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3101 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3102 s += strlen(NAME); \
3103 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3107 #define SCAN_FINISH() \
3108 } while (*s++ == ',' && len != 0); \
3109 if (s[-1] != ')') { \
3113 #define SCAN_FINISH_SINGLE() \
3115 if (*s++ != ')') { \
3119 /* Beginning of nested attribute. */
3120 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3122 size_t key_offset, mask_offset; \
3123 key_offset = nl_msg_start_nested(key, ATTR); \
3125 mask_offset = nl_msg_start_nested(mask, ATTR); \
3130 #define SCAN_END_NESTED() \
3132 nl_msg_end_nested(key, key_offset); \
3134 nl_msg_end_nested(mask, mask_offset); \
3139 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3140 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3142 memset(&skey, 0, sizeof skey); \
3143 memset(&smask, 0xff, sizeof smask); \
3144 s += strlen(NAME); \
3145 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3146 SCAN_PUT(ATTR, FUNC); \
3150 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3151 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3153 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3154 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3156 #define SCAN_PUT(ATTR, FUNC) \
3157 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3158 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3160 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3164 #define SCAN_END(ATTR) \
3166 SCAN_PUT(ATTR, NULL); \
3170 #define SCAN_END_SINGLE(ATTR) \
3171 SCAN_FINISH_SINGLE(); \
3172 SCAN_PUT(ATTR, NULL); \
3176 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3177 SCAN_BEGIN(NAME, TYPE) { \
3178 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3179 } SCAN_END_SINGLE(ATTR)
3181 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3182 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3183 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3184 } SCAN_END_SINGLE(ATTR)
3186 /* scan_port needs one extra argument. */
3187 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3188 SCAN_BEGIN(NAME, TYPE) { \
3189 len = scan_port(s, &skey, &smask, port_names); \
3194 } SCAN_END_SINGLE(ATTR)
3197 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3198 struct ofpbuf *key, struct ofpbuf *mask)
3204 len = odp_ufid_from_string(s, &ufid);
3209 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3210 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3211 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3212 OVS_KEY_ATTR_RECIRC_ID);
3213 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3215 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3216 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3217 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3218 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3219 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3220 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3221 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3222 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3223 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3224 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3226 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3227 } SCAN_END_NESTED();
3229 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3231 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3232 SCAN_FIELD("src=", eth, eth_src);
3233 SCAN_FIELD("dst=", eth, eth_dst);
3234 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3236 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3237 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3238 SCAN_FIELD("vid=", vid, tci);
3239 SCAN_FIELD("pcp=", pcp, tci);
3240 SCAN_FIELD("cfi=", cfi, tci);
3241 } SCAN_END(OVS_KEY_ATTR_VLAN);
3243 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3245 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3246 SCAN_FIELD("label=", mpls_label, mpls_lse);
3247 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3248 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3249 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3250 } SCAN_END(OVS_KEY_ATTR_MPLS);
3252 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3253 SCAN_FIELD("src=", ipv4, ipv4_src);
3254 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3255 SCAN_FIELD("proto=", u8, ipv4_proto);
3256 SCAN_FIELD("tos=", u8, ipv4_tos);
3257 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3258 SCAN_FIELD("frag=", frag, ipv4_frag);
3259 } SCAN_END(OVS_KEY_ATTR_IPV4);
3261 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3262 SCAN_FIELD("src=", ipv6, ipv6_src);
3263 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3264 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3265 SCAN_FIELD("proto=", u8, ipv6_proto);
3266 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3267 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3268 SCAN_FIELD("frag=", frag, ipv6_frag);
3269 } SCAN_END(OVS_KEY_ATTR_IPV6);
3271 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3272 SCAN_FIELD("src=", be16, tcp_src);
3273 SCAN_FIELD("dst=", be16, tcp_dst);
3274 } SCAN_END(OVS_KEY_ATTR_TCP);
3276 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3278 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3279 SCAN_FIELD("src=", be16, udp_src);
3280 SCAN_FIELD("dst=", be16, udp_dst);
3281 } SCAN_END(OVS_KEY_ATTR_UDP);
3283 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3284 SCAN_FIELD("src=", be16, sctp_src);
3285 SCAN_FIELD("dst=", be16, sctp_dst);
3286 } SCAN_END(OVS_KEY_ATTR_SCTP);
3288 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3289 SCAN_FIELD("type=", u8, icmp_type);
3290 SCAN_FIELD("code=", u8, icmp_code);
3291 } SCAN_END(OVS_KEY_ATTR_ICMP);
3293 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3294 SCAN_FIELD("type=", u8, icmpv6_type);
3295 SCAN_FIELD("code=", u8, icmpv6_code);
3296 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3298 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3299 SCAN_FIELD("sip=", ipv4, arp_sip);
3300 SCAN_FIELD("tip=", ipv4, arp_tip);
3301 SCAN_FIELD("op=", be16, arp_op);
3302 SCAN_FIELD("sha=", eth, arp_sha);
3303 SCAN_FIELD("tha=", eth, arp_tha);
3304 } SCAN_END(OVS_KEY_ATTR_ARP);
3306 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3307 SCAN_FIELD("target=", ipv6, nd_target);
3308 SCAN_FIELD("sll=", eth, nd_sll);
3309 SCAN_FIELD("tll=", eth, nd_tll);
3310 } SCAN_END(OVS_KEY_ATTR_ND);
3312 /* Encap open-coded. */
3313 if (!strncmp(s, "encap(", 6)) {
3314 const char *start = s;
3315 size_t encap, encap_mask = 0;
3317 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3319 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3326 s += strspn(s, delimiters);
3329 } else if (*s == ')') {
3333 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3341 nl_msg_end_nested(key, encap);
3343 nl_msg_end_nested(mask, encap_mask);
3352 /* Parses the string representation of a datapath flow key, in the
3353 * format output by odp_flow_key_format(). Returns 0 if successful,
3354 * otherwise a positive errno value. On success, the flow key is
3355 * appended to 'key' as a series of Netlink attributes. On failure, no
3356 * data is appended to 'key'. Either way, 'key''s data might be
3359 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3360 * to a port number. (Port names may be used instead of port numbers in
3363 * On success, the attributes appended to 'key' are individually syntactically
3364 * valid, but they may not be valid as a sequence. 'key' might, for example,
3365 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3367 odp_flow_from_string(const char *s, const struct simap *port_names,
3368 struct ofpbuf *key, struct ofpbuf *mask)
3370 const size_t old_size = key->size;
3374 s += strspn(s, delimiters);
3379 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3381 key->size = old_size;
3391 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3394 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3395 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3396 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3397 * must use a zero mask for the netlink frag field, and all ones mask
3399 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3401 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3402 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3403 : OVS_FRAG_TYPE_FIRST;
3406 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3407 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3408 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3410 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3412 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3414 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3416 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3417 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3418 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3419 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3421 /* These share the same layout. */
3423 struct ovs_key_tcp tcp;
3424 struct ovs_key_udp udp;
3425 struct ovs_key_sctp sctp;
3428 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3429 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3432 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3433 bool export_mask, struct ofpbuf *buf)
3435 struct ovs_key_ethernet *eth_key;
3437 const struct flow *flow = parms->flow;
3438 const struct flow *data = export_mask ? parms->mask : parms->flow;
3440 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3442 if (flow->tunnel.ip_dst || export_mask) {
3443 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3447 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3449 if (parms->recirc) {
3450 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3451 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3454 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3455 * is not the magical value "ODPP_NONE". */
3456 if (export_mask || parms->odp_in_port != ODPP_NONE) {
3457 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3460 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3462 get_ethernet_key(data, eth_key);
3464 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3466 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3468 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3470 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3471 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3472 if (flow->vlan_tci == htons(0)) {
3479 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3480 /* For backwards compatibility with kernels that don't support
3481 * wildcarding, the following convention is used to encode the
3482 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3485 * -------- -------- -------
3486 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3487 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3488 * <none> 0xffff Any non-Ethernet II frame (except valid
3489 * 802.3 SNAP packet with valid eth_type).
3492 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3497 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3499 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3500 struct ovs_key_ipv4 *ipv4_key;
3502 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3504 get_ipv4_key(data, ipv4_key, export_mask);
3505 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3506 struct ovs_key_ipv6 *ipv6_key;
3508 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3510 get_ipv6_key(data, ipv6_key, export_mask);
3511 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3512 flow->dl_type == htons(ETH_TYPE_RARP)) {
3513 struct ovs_key_arp *arp_key;
3515 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3517 get_arp_key(data, arp_key);
3518 } else if (eth_type_mpls(flow->dl_type)) {
3519 struct ovs_key_mpls *mpls_key;
3522 n = flow_count_mpls_labels(flow, NULL);
3524 n = MIN(n, parms->max_mpls_depth);
3526 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3527 n * sizeof *mpls_key);
3528 for (i = 0; i < n; i++) {
3529 mpls_key[i].mpls_lse = data->mpls_lse[i];
3533 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3534 if (flow->nw_proto == IPPROTO_TCP) {
3535 union ovs_key_tp *tcp_key;
3537 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3539 get_tp_key(data, tcp_key);
3540 if (data->tcp_flags) {
3541 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3543 } else if (flow->nw_proto == IPPROTO_UDP) {
3544 union ovs_key_tp *udp_key;
3546 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3548 get_tp_key(data, udp_key);
3549 } else if (flow->nw_proto == IPPROTO_SCTP) {
3550 union ovs_key_tp *sctp_key;
3552 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3554 get_tp_key(data, sctp_key);
3555 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3556 && flow->nw_proto == IPPROTO_ICMP) {
3557 struct ovs_key_icmp *icmp_key;
3559 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3561 icmp_key->icmp_type = ntohs(data->tp_src);
3562 icmp_key->icmp_code = ntohs(data->tp_dst);
3563 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3564 && flow->nw_proto == IPPROTO_ICMPV6) {
3565 struct ovs_key_icmpv6 *icmpv6_key;
3567 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3568 sizeof *icmpv6_key);
3569 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3570 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3572 if (flow->tp_dst == htons(0)
3573 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3574 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3575 && (!export_mask || (data->tp_src == htons(0xffff)
3576 && data->tp_dst == htons(0xffff)))) {
3578 struct ovs_key_nd *nd_key;
3580 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3582 memcpy(nd_key->nd_target, &data->nd_target,
3583 sizeof nd_key->nd_target);
3584 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3585 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3592 nl_msg_end_nested(buf, encap);
3596 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3598 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3599 * capable of being expanded to allow for that much space. */
3601 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3604 odp_flow_key_from_flow__(parms, false, buf);
3607 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3610 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3611 * capable of being expanded to allow for that much space. */
3613 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3616 odp_flow_key_from_flow__(parms, true, buf);
3619 /* Generate ODP flow key from the given packet metadata */
3621 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3623 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3625 if (md->tunnel.ip_dst) {
3626 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3629 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3631 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3632 * value "ODPP_NONE". */
3633 if (md->in_port.odp_port != ODPP_NONE) {
3634 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3638 /* Generate packet metadata from the given ODP flow key. */
3640 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3641 struct pkt_metadata *md)
3643 const struct nlattr *nla;
3645 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3646 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3647 1u << OVS_KEY_ATTR_IN_PORT;
3649 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3651 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3652 uint16_t type = nl_attr_type(nla);
3653 size_t len = nl_attr_get_size(nla);
3654 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3655 OVS_KEY_ATTR_MAX, type);
3657 if (len != expected_len && expected_len >= 0) {
3662 case OVS_KEY_ATTR_RECIRC_ID:
3663 md->recirc_id = nl_attr_get_u32(nla);
3664 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3666 case OVS_KEY_ATTR_DP_HASH:
3667 md->dp_hash = nl_attr_get_u32(nla);
3668 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3670 case OVS_KEY_ATTR_PRIORITY:
3671 md->skb_priority = nl_attr_get_u32(nla);
3672 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3674 case OVS_KEY_ATTR_SKB_MARK:
3675 md->pkt_mark = nl_attr_get_u32(nla);
3676 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3678 case OVS_KEY_ATTR_TUNNEL: {
3679 enum odp_key_fitness res;
3681 res = odp_tun_key_from_attr(nla, &md->tunnel);
3682 if (res == ODP_FIT_ERROR) {
3683 memset(&md->tunnel, 0, sizeof md->tunnel);
3684 } else if (res == ODP_FIT_PERFECT) {
3685 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3689 case OVS_KEY_ATTR_IN_PORT:
3690 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3691 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3697 if (!wanted_attrs) {
3698 return; /* Have everything. */
3704 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3706 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3707 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3708 key_len / sizeof(uint32_t), 0);
3712 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3713 uint64_t attrs, int out_of_range_attr,
3714 const struct nlattr *key, size_t key_len)
3719 if (VLOG_DROP_DBG(rl)) {
3724 for (i = 0; i < 64; i++) {
3725 if (attrs & (UINT64_C(1) << i)) {
3726 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3728 ds_put_format(&s, " %s",
3729 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3732 if (out_of_range_attr) {
3733 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3736 ds_put_cstr(&s, ": ");
3737 odp_flow_key_format(key, key_len, &s);
3739 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3744 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3746 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3749 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3752 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3753 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3754 return 0xff; /* Error. */
3757 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3758 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3759 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3763 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3764 const struct nlattr *attrs[], uint64_t *present_attrsp,
3765 int *out_of_range_attrp)
3767 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3768 const struct nlattr *nla;
3769 uint64_t present_attrs;
3772 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3774 *out_of_range_attrp = 0;
3775 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3776 uint16_t type = nl_attr_type(nla);
3777 size_t len = nl_attr_get_size(nla);
3778 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3779 OVS_KEY_ATTR_MAX, type);
3781 if (len != expected_len && expected_len >= 0) {
3782 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3784 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3785 "length %d", ovs_key_attr_to_string(type, namebuf,
3791 if (type > OVS_KEY_ATTR_MAX) {
3792 *out_of_range_attrp = type;
3794 if (present_attrs & (UINT64_C(1) << type)) {
3795 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3797 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3798 ovs_key_attr_to_string(type,
3799 namebuf, sizeof namebuf));
3803 present_attrs |= UINT64_C(1) << type;
3808 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3812 *present_attrsp = present_attrs;
3816 static enum odp_key_fitness
3817 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3818 uint64_t expected_attrs,
3819 const struct nlattr *key, size_t key_len)
3821 uint64_t missing_attrs;
3822 uint64_t extra_attrs;
3824 missing_attrs = expected_attrs & ~present_attrs;
3825 if (missing_attrs) {
3826 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3827 log_odp_key_attributes(&rl, "expected but not present",
3828 missing_attrs, 0, key, key_len);
3829 return ODP_FIT_TOO_LITTLE;
3832 extra_attrs = present_attrs & ~expected_attrs;
3833 if (extra_attrs || out_of_range_attr) {
3834 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3835 log_odp_key_attributes(&rl, "present but not expected",
3836 extra_attrs, out_of_range_attr, key, key_len);
3837 return ODP_FIT_TOO_MUCH;
3840 return ODP_FIT_PERFECT;
3844 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3845 uint64_t present_attrs, uint64_t *expected_attrs,
3846 struct flow *flow, const struct flow *src_flow)
3848 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3849 bool is_mask = flow != src_flow;
3851 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3852 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3853 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3854 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3855 ntohs(flow->dl_type));
3858 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3859 flow->dl_type != htons(0xffff)) {
3862 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3865 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3866 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3867 /* See comments in odp_flow_key_from_flow__(). */
3868 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3875 static enum odp_key_fitness
3876 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3877 uint64_t present_attrs, int out_of_range_attr,
3878 uint64_t expected_attrs, struct flow *flow,
3879 const struct nlattr *key, size_t key_len,
3880 const struct flow *src_flow)
3882 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3883 bool is_mask = src_flow != flow;
3884 const void *check_start = NULL;
3885 size_t check_len = 0;
3886 enum ovs_key_attr expected_bit = 0xff;
3888 if (eth_type_mpls(src_flow->dl_type)) {
3889 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3890 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3892 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3893 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3894 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3895 int n = size / sizeof(ovs_be32);
3898 if (!size || size % sizeof(ovs_be32)) {
3899 return ODP_FIT_ERROR;
3901 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3902 return ODP_FIT_ERROR;
3905 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3906 flow->mpls_lse[i] = mpls_lse[i];
3908 if (n > FLOW_MAX_MPLS_LABELS) {
3909 return ODP_FIT_TOO_MUCH;
3913 /* BOS may be set only in the innermost label. */
3914 for (i = 0; i < n - 1; i++) {
3915 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3916 return ODP_FIT_ERROR;
3920 /* BOS must be set in the innermost label. */
3921 if (n < FLOW_MAX_MPLS_LABELS
3922 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3923 return ODP_FIT_TOO_LITTLE;
3929 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3931 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3933 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3934 const struct ovs_key_ipv4 *ipv4_key;
3936 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3937 put_ipv4_key(ipv4_key, flow, is_mask);
3938 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3939 return ODP_FIT_ERROR;
3942 check_start = ipv4_key;
3943 check_len = sizeof *ipv4_key;
3944 expected_bit = OVS_KEY_ATTR_IPV4;
3947 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3949 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3951 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3952 const struct ovs_key_ipv6 *ipv6_key;
3954 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3955 put_ipv6_key(ipv6_key, flow, is_mask);
3956 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3957 return ODP_FIT_ERROR;
3960 check_start = ipv6_key;
3961 check_len = sizeof *ipv6_key;
3962 expected_bit = OVS_KEY_ATTR_IPV6;
3965 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3966 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3968 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3970 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3971 const struct ovs_key_arp *arp_key;
3973 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3974 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3975 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3976 "key", ntohs(arp_key->arp_op));
3977 return ODP_FIT_ERROR;
3979 put_arp_key(arp_key, flow);
3981 check_start = arp_key;
3982 check_len = sizeof *arp_key;
3983 expected_bit = OVS_KEY_ATTR_ARP;
3989 if (check_len > 0) { /* Happens only when 'is_mask'. */
3990 if (!is_all_zeros(check_start, check_len) &&
3991 flow->dl_type != htons(0xffff)) {
3992 return ODP_FIT_ERROR;
3994 expected_attrs |= UINT64_C(1) << expected_bit;
3998 expected_bit = OVS_KEY_ATTR_UNSPEC;
3999 if (src_flow->nw_proto == IPPROTO_TCP
4000 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4001 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4002 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4004 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4006 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4007 const union ovs_key_tp *tcp_key;
4009 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4010 put_tp_key(tcp_key, flow);
4011 expected_bit = OVS_KEY_ATTR_TCP;
4013 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4014 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4015 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4017 } else if (src_flow->nw_proto == IPPROTO_UDP
4018 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4019 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4020 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4022 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4024 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4025 const union ovs_key_tp *udp_key;
4027 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4028 put_tp_key(udp_key, flow);
4029 expected_bit = OVS_KEY_ATTR_UDP;
4031 } else if (src_flow->nw_proto == IPPROTO_SCTP
4032 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4033 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4034 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4036 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4038 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4039 const union ovs_key_tp *sctp_key;
4041 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4042 put_tp_key(sctp_key, flow);
4043 expected_bit = OVS_KEY_ATTR_SCTP;
4045 } else if (src_flow->nw_proto == IPPROTO_ICMP
4046 && src_flow->dl_type == htons(ETH_TYPE_IP)
4047 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4049 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4051 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4052 const struct ovs_key_icmp *icmp_key;
4054 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4055 flow->tp_src = htons(icmp_key->icmp_type);
4056 flow->tp_dst = htons(icmp_key->icmp_code);
4057 expected_bit = OVS_KEY_ATTR_ICMP;
4059 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4060 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4061 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4063 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4065 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4066 const struct ovs_key_icmpv6 *icmpv6_key;
4068 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4069 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4070 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4071 expected_bit = OVS_KEY_ATTR_ICMPV6;
4072 if (src_flow->tp_dst == htons(0) &&
4073 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4074 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4076 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4078 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4079 const struct ovs_key_nd *nd_key;
4081 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4082 memcpy(&flow->nd_target, nd_key->nd_target,
4083 sizeof flow->nd_target);
4084 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4085 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4087 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4088 (flow->tp_src != htons(0xffff) ||
4089 flow->tp_dst != htons(0xffff))) {
4090 return ODP_FIT_ERROR;
4092 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4099 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4100 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4101 return ODP_FIT_ERROR;
4103 expected_attrs |= UINT64_C(1) << expected_bit;
4108 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4112 /* Parse 802.1Q header then encapsulated L3 attributes. */
4113 static enum odp_key_fitness
4114 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4115 uint64_t present_attrs, int out_of_range_attr,
4116 uint64_t expected_attrs, struct flow *flow,
4117 const struct nlattr *key, size_t key_len,
4118 const struct flow *src_flow)
4120 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4121 bool is_mask = src_flow != flow;
4123 const struct nlattr *encap
4124 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4125 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4126 enum odp_key_fitness encap_fitness;
4127 enum odp_key_fitness fitness;
4129 /* Calculate fitness of outer attributes. */
4131 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4132 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4134 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4135 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4137 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4138 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4141 fitness = check_expectations(present_attrs, out_of_range_attr,
4142 expected_attrs, key, key_len);
4145 * Remove the TPID from dl_type since it's not the real Ethertype. */
4146 flow->dl_type = htons(0);
4147 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4148 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4151 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4152 return ODP_FIT_TOO_LITTLE;
4153 } else if (flow->vlan_tci == htons(0)) {
4154 /* Corner case for a truncated 802.1Q header. */
4155 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4156 return ODP_FIT_TOO_MUCH;
4159 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4160 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4161 "but CFI bit is not set", ntohs(flow->vlan_tci));
4162 return ODP_FIT_ERROR;
4165 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4170 /* Now parse the encapsulated attributes. */
4171 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4172 attrs, &present_attrs, &out_of_range_attr)) {
4173 return ODP_FIT_ERROR;
4177 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4178 return ODP_FIT_ERROR;
4180 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4181 expected_attrs, flow, key, key_len,
4184 /* The overall fitness is the worse of the outer and inner attributes. */
4185 return MAX(fitness, encap_fitness);
4188 static enum odp_key_fitness
4189 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4190 const struct nlattr *src_key, size_t src_key_len,
4191 struct flow *flow, const struct flow *src_flow)
4193 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4194 uint64_t expected_attrs;
4195 uint64_t present_attrs;
4196 int out_of_range_attr;
4197 bool is_mask = src_flow != flow;
4199 memset(flow, 0, sizeof *flow);
4201 /* Parse attributes. */
4202 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4203 &out_of_range_attr)) {
4204 return ODP_FIT_ERROR;
4209 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4210 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4211 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4212 } else if (is_mask) {
4213 /* Always exact match recirc_id if it is not specified. */
4214 flow->recirc_id = UINT32_MAX;
4217 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4218 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4219 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4221 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4222 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4223 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4226 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4227 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4228 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4231 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4232 enum odp_key_fitness res;
4234 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], src_key,
4235 src_key_len, &src_flow->tunnel,
4237 if (res == ODP_FIT_ERROR) {
4238 return ODP_FIT_ERROR;
4239 } else if (res == ODP_FIT_PERFECT) {
4240 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4244 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4245 flow->in_port.odp_port
4246 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4247 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4248 } else if (!is_mask) {
4249 flow->in_port.odp_port = ODPP_NONE;
4252 /* Ethernet header. */
4253 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4254 const struct ovs_key_ethernet *eth_key;
4256 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4257 put_ethernet_key(eth_key, flow);
4259 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4263 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4266 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4267 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4269 return ODP_FIT_ERROR;
4273 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4274 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4275 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4276 expected_attrs, flow, key, key_len, src_flow);
4279 flow->vlan_tci = htons(0xffff);
4280 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4281 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4282 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4285 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4286 expected_attrs, flow, key, key_len, src_flow);
4289 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4290 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4291 * 'key' fits our expectations for what a flow key should contain.
4293 * The 'in_port' will be the datapath's understanding of the port. The
4294 * caller will need to translate with odp_port_to_ofp_port() if the
4295 * OpenFlow port is needed.
4297 * This function doesn't take the packet itself as an argument because none of
4298 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4299 * it is always possible to infer which additional attribute(s) should appear
4300 * by looking at the attributes for lower-level protocols, e.g. if the network
4301 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4302 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4303 * must be absent. */
4304 enum odp_key_fitness
4305 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4308 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow);
4311 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4312 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
4313 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4314 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
4315 * well 'key' fits our expectations for what a flow key should contain. */
4316 enum odp_key_fitness
4317 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4318 const struct nlattr *flow_key, size_t flow_key_len,
4319 struct flow *mask, const struct flow *flow)
4321 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4325 /* Returns 'fitness' as a string, for use in debug messages. */
4327 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4330 case ODP_FIT_PERFECT:
4332 case ODP_FIT_TOO_MUCH:
4334 case ODP_FIT_TOO_LITTLE:
4335 return "too_little";
4343 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4344 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4345 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4346 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4347 * null, then the return value is not meaningful.) */
4349 odp_put_userspace_action(uint32_t pid,
4350 const void *userdata, size_t userdata_size,
4351 odp_port_t tunnel_out_port,
4352 struct ofpbuf *odp_actions)
4354 size_t userdata_ofs;
4357 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4358 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4360 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4362 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4363 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4366 * - The kernel rejected shorter userdata with -ERANGE.
4368 * - The kernel silently dropped userdata beyond the first 8 bytes.
4370 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4371 * separately disable features that required more than 8 bytes.) */
4372 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4373 MAX(8, userdata_size)),
4374 userdata, userdata_size);
4378 if (tunnel_out_port != ODPP_NONE) {
4379 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4382 nl_msg_end_nested(odp_actions, offset);
4384 return userdata_ofs;
4388 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4389 struct ofpbuf *odp_actions)
4391 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4392 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4393 nl_msg_end_nested(odp_actions, offset);
4397 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4398 struct ovs_action_push_tnl *data)
4400 int size = offsetof(struct ovs_action_push_tnl, header);
4402 size += data->header_len;
4403 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4407 /* The commit_odp_actions() function and its helpers. */
4410 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4411 const void *key, size_t key_size)
4413 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4414 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4415 nl_msg_end_nested(odp_actions, offset);
4418 /* Masked set actions have a mask following the data within the netlink
4419 * attribute. The unmasked bits in the data will be cleared as the data
4420 * is copied to the action. */
4422 commit_masked_set_action(struct ofpbuf *odp_actions,
4423 enum ovs_key_attr key_type,
4424 const void *key_, const void *mask_, size_t key_size)
4426 size_t offset = nl_msg_start_nested(odp_actions,
4427 OVS_ACTION_ATTR_SET_MASKED);
4428 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4429 const char *key = key_, *mask = mask_;
4431 memcpy(data + key_size, mask, key_size);
4432 /* Clear unmasked bits while copying. */
4433 while (key_size--) {
4434 *data++ = *key++ & *mask++;
4436 nl_msg_end_nested(odp_actions, offset);
4439 /* If any of the flow key data that ODP actions can modify are different in
4440 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4441 * 'odp_actions' that change the flow tunneling information in key from
4442 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4443 * same way. In other words, operates the same as commit_odp_actions(), but
4444 * only on tunneling information. */
4446 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4447 struct ofpbuf *odp_actions)
4449 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4450 if (flow->tunnel.ip_dst) {
4451 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4454 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4455 odp_put_tunnel_action(&base->tunnel, odp_actions);
4460 commit(enum ovs_key_attr attr, bool use_masked_set,
4461 const void *key, void *base, void *mask, size_t size,
4462 struct ofpbuf *odp_actions)
4464 if (memcmp(key, base, size)) {
4465 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4467 if (use_masked_set && !fully_masked) {
4468 commit_masked_set_action(odp_actions, attr, key, mask, size);
4470 if (!fully_masked) {
4471 memset(mask, 0xff, size);
4473 commit_set_action(odp_actions, attr, key, size);
4475 memcpy(base, key, size);
4478 /* Mask bits are set when we have either read or set the corresponding
4479 * values. Masked bits will be exact-matched, no need to set them
4480 * if the value did not actually change. */
4486 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4488 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4489 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4493 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4495 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4496 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4500 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4501 struct ofpbuf *odp_actions,
4502 struct flow_wildcards *wc,
4505 struct ovs_key_ethernet key, base, mask;
4507 get_ethernet_key(flow, &key);
4508 get_ethernet_key(base_flow, &base);
4509 get_ethernet_key(&wc->masks, &mask);
4511 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4512 &key, &base, &mask, sizeof key, odp_actions)) {
4513 put_ethernet_key(&base, base_flow);
4514 put_ethernet_key(&mask, &wc->masks);
4519 pop_vlan(struct flow *base,
4520 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4522 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4524 if (base->vlan_tci & htons(VLAN_CFI)) {
4525 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4531 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4532 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4534 if (base->vlan_tci == vlan_tci) {
4538 pop_vlan(base, odp_actions, wc);
4539 if (vlan_tci & htons(VLAN_CFI)) {
4540 struct ovs_action_push_vlan vlan;
4542 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4543 vlan.vlan_tci = vlan_tci;
4544 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4545 &vlan, sizeof vlan);
4547 base->vlan_tci = vlan_tci;
4550 /* Wildcarding already done at action translation time. */
4552 commit_mpls_action(const struct flow *flow, struct flow *base,
4553 struct ofpbuf *odp_actions)
4555 int base_n = flow_count_mpls_labels(base, NULL);
4556 int flow_n = flow_count_mpls_labels(flow, NULL);
4557 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4560 while (base_n > common_n) {
4561 if (base_n - 1 == common_n && flow_n > common_n) {
4562 /* If there is only one more LSE in base than there are common
4563 * between base and flow; and flow has at least one more LSE than
4564 * is common then the topmost LSE of base may be updated using
4566 struct ovs_key_mpls mpls_key;
4568 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4569 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4570 &mpls_key, sizeof mpls_key);
4571 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4574 /* Otherwise, if there more LSEs in base than are common between
4575 * base and flow then pop the topmost one. */
4579 /* If all the LSEs are to be popped and this is not the outermost
4580 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4581 * POP_MPLS action instead of flow->dl_type.
4583 * This is because the POP_MPLS action requires its ethertype
4584 * argument to be an MPLS ethernet type but in this case
4585 * flow->dl_type will be a non-MPLS ethernet type.
4587 * When the final POP_MPLS action occurs it use flow->dl_type and
4588 * the and the resulting packet will have the desired dl_type. */
4589 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4590 dl_type = htons(ETH_TYPE_MPLS);
4592 dl_type = flow->dl_type;
4594 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4595 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4601 /* If, after the above popping and setting, there are more LSEs in flow
4602 * than base then some LSEs need to be pushed. */
4603 while (base_n < flow_n) {
4604 struct ovs_action_push_mpls *mpls;
4606 mpls = nl_msg_put_unspec_zero(odp_actions,
4607 OVS_ACTION_ATTR_PUSH_MPLS,
4609 mpls->mpls_ethertype = flow->dl_type;
4610 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4611 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4612 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4618 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4620 ipv4->ipv4_src = flow->nw_src;
4621 ipv4->ipv4_dst = flow->nw_dst;
4622 ipv4->ipv4_proto = flow->nw_proto;
4623 ipv4->ipv4_tos = flow->nw_tos;
4624 ipv4->ipv4_ttl = flow->nw_ttl;
4625 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4629 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4631 flow->nw_src = ipv4->ipv4_src;
4632 flow->nw_dst = ipv4->ipv4_dst;
4633 flow->nw_proto = ipv4->ipv4_proto;
4634 flow->nw_tos = ipv4->ipv4_tos;
4635 flow->nw_ttl = ipv4->ipv4_ttl;
4636 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4640 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4641 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4644 struct ovs_key_ipv4 key, mask, base;
4646 /* Check that nw_proto and nw_frag remain unchanged. */
4647 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4648 flow->nw_frag == base_flow->nw_frag);
4650 get_ipv4_key(flow, &key, false);
4651 get_ipv4_key(base_flow, &base, false);
4652 get_ipv4_key(&wc->masks, &mask, true);
4653 mask.ipv4_proto = 0; /* Not writeable. */
4654 mask.ipv4_frag = 0; /* Not writable. */
4656 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4658 put_ipv4_key(&base, base_flow, false);
4659 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4660 put_ipv4_key(&mask, &wc->masks, true);
4666 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4668 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4669 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4670 ipv6->ipv6_label = flow->ipv6_label;
4671 ipv6->ipv6_proto = flow->nw_proto;
4672 ipv6->ipv6_tclass = flow->nw_tos;
4673 ipv6->ipv6_hlimit = flow->nw_ttl;
4674 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4678 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4680 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4681 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4682 flow->ipv6_label = ipv6->ipv6_label;
4683 flow->nw_proto = ipv6->ipv6_proto;
4684 flow->nw_tos = ipv6->ipv6_tclass;
4685 flow->nw_ttl = ipv6->ipv6_hlimit;
4686 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4690 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4691 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4694 struct ovs_key_ipv6 key, mask, base;
4696 /* Check that nw_proto and nw_frag remain unchanged. */
4697 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4698 flow->nw_frag == base_flow->nw_frag);
4700 get_ipv6_key(flow, &key, false);
4701 get_ipv6_key(base_flow, &base, false);
4702 get_ipv6_key(&wc->masks, &mask, true);
4703 mask.ipv6_proto = 0; /* Not writeable. */
4704 mask.ipv6_frag = 0; /* Not writable. */
4706 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4708 put_ipv6_key(&base, base_flow, false);
4709 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4710 put_ipv6_key(&mask, &wc->masks, true);
4716 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4718 /* ARP key has padding, clear it. */
4719 memset(arp, 0, sizeof *arp);
4721 arp->arp_sip = flow->nw_src;
4722 arp->arp_tip = flow->nw_dst;
4723 arp->arp_op = htons(flow->nw_proto);
4724 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4725 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4729 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4731 flow->nw_src = arp->arp_sip;
4732 flow->nw_dst = arp->arp_tip;
4733 flow->nw_proto = ntohs(arp->arp_op);
4734 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4735 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4738 static enum slow_path_reason
4739 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4740 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4742 struct ovs_key_arp key, mask, base;
4744 get_arp_key(flow, &key);
4745 get_arp_key(base_flow, &base);
4746 get_arp_key(&wc->masks, &mask);
4748 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4750 put_arp_key(&base, base_flow);
4751 put_arp_key(&mask, &wc->masks);
4758 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4760 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4761 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4762 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4763 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4767 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4769 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4770 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4771 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4772 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4775 static enum slow_path_reason
4776 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4777 struct ofpbuf *odp_actions,
4778 struct flow_wildcards *wc, bool use_masked)
4780 struct ovs_key_nd key, mask, base;
4782 get_nd_key(flow, &key);
4783 get_nd_key(base_flow, &base);
4784 get_nd_key(&wc->masks, &mask);
4786 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4788 put_nd_key(&base, base_flow);
4789 put_nd_key(&mask, &wc->masks);
4796 static enum slow_path_reason
4797 commit_set_nw_action(const struct flow *flow, struct flow *base,
4798 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4801 /* Check if 'flow' really has an L3 header. */
4802 if (!flow->nw_proto) {
4806 switch (ntohs(base->dl_type)) {
4808 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4812 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4813 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4816 return commit_set_arp_action(flow, base, odp_actions, wc);
4822 /* TCP, UDP, and SCTP keys have the same layout. */
4823 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4824 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4827 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4829 tp->tcp.tcp_src = flow->tp_src;
4830 tp->tcp.tcp_dst = flow->tp_dst;
4834 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4836 flow->tp_src = tp->tcp.tcp_src;
4837 flow->tp_dst = tp->tcp.tcp_dst;
4841 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4842 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4845 enum ovs_key_attr key_type;
4846 union ovs_key_tp key, mask, base;
4848 /* Check if 'flow' really has an L3 header. */
4849 if (!flow->nw_proto) {
4853 if (!is_ip_any(base_flow)) {
4857 if (flow->nw_proto == IPPROTO_TCP) {
4858 key_type = OVS_KEY_ATTR_TCP;
4859 } else if (flow->nw_proto == IPPROTO_UDP) {
4860 key_type = OVS_KEY_ATTR_UDP;
4861 } else if (flow->nw_proto == IPPROTO_SCTP) {
4862 key_type = OVS_KEY_ATTR_SCTP;
4867 get_tp_key(flow, &key);
4868 get_tp_key(base_flow, &base);
4869 get_tp_key(&wc->masks, &mask);
4871 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4873 put_tp_key(&base, base_flow);
4874 put_tp_key(&mask, &wc->masks);
4879 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4880 struct ofpbuf *odp_actions,
4881 struct flow_wildcards *wc,
4884 uint32_t key, mask, base;
4886 key = flow->skb_priority;
4887 base = base_flow->skb_priority;
4888 mask = wc->masks.skb_priority;
4890 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4891 sizeof key, odp_actions)) {
4892 base_flow->skb_priority = base;
4893 wc->masks.skb_priority = mask;
4898 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4899 struct ofpbuf *odp_actions,
4900 struct flow_wildcards *wc,
4903 uint32_t key, mask, base;
4905 key = flow->pkt_mark;
4906 base = base_flow->pkt_mark;
4907 mask = wc->masks.pkt_mark;
4909 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4910 sizeof key, odp_actions)) {
4911 base_flow->pkt_mark = base;
4912 wc->masks.pkt_mark = mask;
4916 /* If any of the flow key data that ODP actions can modify are different in
4917 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4918 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4919 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4920 * in addition to this function if needed. Sets fields in 'wc' that are
4921 * used as part of the action.
4923 * Returns a reason to force processing the flow's packets into the userspace
4924 * slow path, if there is one, otherwise 0. */
4925 enum slow_path_reason
4926 commit_odp_actions(const struct flow *flow, struct flow *base,
4927 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4930 enum slow_path_reason slow;
4932 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4933 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4934 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4935 commit_mpls_action(flow, base, odp_actions);
4936 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4937 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4938 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);