2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
38 #include "tun-metadata.h"
39 #include "unaligned.h"
42 #include "openvswitch/vlog.h"
44 VLOG_DEFINE_THIS_MODULE(odp_util);
46 /* The interface between userspace and kernel uses an "OVS_*" prefix.
47 * Since this is fairly non-specific for the OVS userspace components,
48 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
49 * interactions with the datapath.
52 /* The set of characters that may separate one action or one key attribute
54 static const char *delimiters = ", \t\r\n";
58 const struct attr_len_tbl *next;
61 #define ATTR_LEN_INVALID -1
62 #define ATTR_LEN_VARIABLE -2
63 #define ATTR_LEN_NESTED -3
65 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
66 struct ofpbuf *, struct ofpbuf *);
67 static void format_odp_key_attr(const struct nlattr *a,
68 const struct nlattr *ma,
69 const struct hmap *portno_names, struct ds *ds,
73 struct geneve_opt d[63];
77 static int scan_geneve(const char *s, struct geneve_scan *key,
78 struct geneve_scan *mask);
79 static void format_geneve_opts(const struct geneve_opt *opt,
80 const struct geneve_opt *mask, int opts_len,
81 struct ds *, bool verbose);
83 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
84 int max, struct ofpbuf *,
85 const struct nlattr *key);
86 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
89 * - For an action whose argument has a fixed length, returned that
90 * nonnegative length in bytes.
92 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
94 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
96 odp_action_len(uint16_t type)
98 if (type > OVS_ACTION_ATTR_MAX) {
102 switch ((enum ovs_action_attr) type) {
103 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
104 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
105 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
106 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
107 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
108 case OVS_ACTION_ATTR_POP_VLAN: return 0;
109 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
110 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
111 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
112 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
113 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
114 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
115 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
117 case OVS_ACTION_ATTR_UNSPEC:
118 case __OVS_ACTION_ATTR_MAX:
119 return ATTR_LEN_INVALID;
122 return ATTR_LEN_INVALID;
125 /* Returns a string form of 'attr'. The return value is either a statically
126 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
127 * should be at least OVS_KEY_ATTR_BUFSIZE. */
128 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
130 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
133 case OVS_KEY_ATTR_UNSPEC: return "unspec";
134 case OVS_KEY_ATTR_ENCAP: return "encap";
135 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
136 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
137 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
138 case OVS_KEY_ATTR_IN_PORT: return "in_port";
139 case OVS_KEY_ATTR_ETHERNET: return "eth";
140 case OVS_KEY_ATTR_VLAN: return "vlan";
141 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
142 case OVS_KEY_ATTR_IPV4: return "ipv4";
143 case OVS_KEY_ATTR_IPV6: return "ipv6";
144 case OVS_KEY_ATTR_TCP: return "tcp";
145 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
146 case OVS_KEY_ATTR_UDP: return "udp";
147 case OVS_KEY_ATTR_SCTP: return "sctp";
148 case OVS_KEY_ATTR_ICMP: return "icmp";
149 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
150 case OVS_KEY_ATTR_ARP: return "arp";
151 case OVS_KEY_ATTR_ND: return "nd";
152 case OVS_KEY_ATTR_MPLS: return "mpls";
153 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
154 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
156 case __OVS_KEY_ATTR_MAX:
158 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
164 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
166 size_t len = nl_attr_get_size(a);
168 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
170 const uint8_t *unspec;
173 unspec = nl_attr_get(a);
174 for (i = 0; i < len; i++) {
175 ds_put_char(ds, i ? ' ': '(');
176 ds_put_format(ds, "%02x", unspec[i]);
178 ds_put_char(ds, ')');
183 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
185 static const struct nl_policy ovs_sample_policy[] = {
186 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
187 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
189 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
191 const struct nlattr *nla_acts;
194 ds_put_cstr(ds, "sample");
196 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
197 ds_put_cstr(ds, "(error)");
201 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
204 ds_put_format(ds, "(sample=%.1f%%,", percentage);
206 ds_put_cstr(ds, "actions(");
207 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
208 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
209 format_odp_actions(ds, nla_acts, len);
210 ds_put_format(ds, "))");
214 slow_path_reason_to_string(uint32_t reason)
216 switch ((enum slow_path_reason) reason) {
217 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
226 slow_path_reason_to_explanation(enum slow_path_reason reason)
229 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
238 parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t),
239 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
241 return parse_flags(s, bit_to_string, ')', NULL, NULL,
242 res_flags, allowed, res_mask);
246 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
248 static const struct nl_policy ovs_userspace_policy[] = {
249 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
250 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
252 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
255 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
256 const struct nlattr *userdata_attr;
257 const struct nlattr *tunnel_out_port_attr;
259 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
260 ds_put_cstr(ds, "userspace(error)");
264 ds_put_format(ds, "userspace(pid=%"PRIu32,
265 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
267 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
270 const uint8_t *userdata = nl_attr_get(userdata_attr);
271 size_t userdata_len = nl_attr_get_size(userdata_attr);
272 bool userdata_unspec = true;
273 union user_action_cookie cookie;
275 if (userdata_len >= sizeof cookie.type
276 && userdata_len <= sizeof cookie) {
278 memset(&cookie, 0, sizeof cookie);
279 memcpy(&cookie, userdata, userdata_len);
281 userdata_unspec = false;
283 if (userdata_len == sizeof cookie.sflow
284 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
285 ds_put_format(ds, ",sFlow("
286 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
287 vlan_tci_to_vid(cookie.sflow.vlan_tci),
288 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
289 cookie.sflow.output);
290 } else if (userdata_len == sizeof cookie.slow_path
291 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
292 ds_put_cstr(ds, ",slow_path(");
293 format_flags(ds, slow_path_reason_to_string,
294 cookie.slow_path.reason, ',');
295 ds_put_format(ds, ")");
296 } else if (userdata_len == sizeof cookie.flow_sample
297 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
298 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
299 ",collector_set_id=%"PRIu32
300 ",obs_domain_id=%"PRIu32
301 ",obs_point_id=%"PRIu32")",
302 cookie.flow_sample.probability,
303 cookie.flow_sample.collector_set_id,
304 cookie.flow_sample.obs_domain_id,
305 cookie.flow_sample.obs_point_id);
306 } else if (userdata_len >= sizeof cookie.ipfix
307 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
308 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
309 cookie.ipfix.output_odp_port);
311 userdata_unspec = true;
315 if (userdata_unspec) {
317 ds_put_format(ds, ",userdata(");
318 for (i = 0; i < userdata_len; i++) {
319 ds_put_format(ds, "%02x", userdata[i]);
321 ds_put_char(ds, ')');
325 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
326 if (tunnel_out_port_attr) {
327 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
328 nl_attr_get_u32(tunnel_out_port_attr));
331 ds_put_char(ds, ')');
335 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
337 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
338 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
339 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
340 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
342 ds_put_char(ds, ',');
344 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
345 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
346 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
347 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
349 ds_put_char(ds, ',');
351 if (!(tci & htons(VLAN_CFI))) {
352 ds_put_cstr(ds, "cfi=0");
353 ds_put_char(ds, ',');
359 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
361 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
362 mpls_lse_to_label(mpls_lse),
363 mpls_lse_to_tc(mpls_lse),
364 mpls_lse_to_ttl(mpls_lse),
365 mpls_lse_to_bos(mpls_lse));
369 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
370 const struct ovs_key_mpls *mpls_mask, int n)
373 ovs_be32 key = mpls_key->mpls_lse;
375 if (mpls_mask == NULL) {
376 format_mpls_lse(ds, key);
378 ovs_be32 mask = mpls_mask->mpls_lse;
380 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
381 mpls_lse_to_label(key), mpls_lse_to_label(mask),
382 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
383 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
384 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
389 for (i = 0; i < n; i++) {
390 ds_put_format(ds, "lse%d=%#"PRIx32,
391 i, ntohl(mpls_key[i].mpls_lse));
393 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
395 ds_put_char(ds, ',');
402 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
404 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
408 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
410 ds_put_format(ds, "hash(");
412 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
413 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
415 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
418 ds_put_format(ds, ")");
422 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
424 const struct udp_header *udp;
426 udp = (const struct udp_header *) (ip + 1);
427 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
428 ntohs(udp->udp_src), ntohs(udp->udp_dst),
429 ntohs(udp->udp_csum));
435 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
437 const struct eth_header *eth;
438 const struct ip_header *ip;
441 eth = (const struct eth_header *)data->header;
444 ip = (const struct ip_header *)l3;
447 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
448 data->header_len, data->tnl_type);
449 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
450 ds_put_format(ds, ",src=");
451 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
452 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
455 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
456 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
457 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
458 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
459 ip->ip_proto, ip->ip_tos,
463 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
464 const struct vxlanhdr *vxh;
466 vxh = format_udp_tnl_push_header(ds, ip);
468 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
469 ntohl(get_16aligned_be32(&vxh->vx_flags)),
470 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
471 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
472 const struct genevehdr *gnh;
474 gnh = format_udp_tnl_push_header(ds, ip);
476 ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
477 gnh->oam ? "oam," : "",
478 gnh->critical ? "crit," : "",
479 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
482 ds_put_cstr(ds, ",options(");
483 format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
485 ds_put_char(ds, ')');
488 ds_put_char(ds, ')');
489 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
490 const struct gre_base_hdr *greh;
491 ovs_16aligned_be32 *options;
494 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
495 greh = (const struct gre_base_hdr *) l4;
497 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
498 ntohs(greh->flags), ntohs(greh->protocol));
499 options = (ovs_16aligned_be32 *)(greh + 1);
500 if (greh->flags & htons(GRE_CSUM)) {
501 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
504 if (greh->flags & htons(GRE_KEY)) {
505 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
508 if (greh->flags & htons(GRE_SEQ)) {
509 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
512 ds_put_format(ds, ")");
514 ds_put_format(ds, ")");
518 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
520 struct ovs_action_push_tnl *data;
522 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
524 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
525 format_odp_tnl_push_header(ds, data);
526 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
530 format_odp_action(struct ds *ds, const struct nlattr *a)
533 enum ovs_action_attr type = nl_attr_type(a);
534 const struct ovs_action_push_vlan *vlan;
537 expected_len = odp_action_len(nl_attr_type(a));
538 if (expected_len != ATTR_LEN_VARIABLE &&
539 nl_attr_get_size(a) != expected_len) {
540 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
541 nl_attr_get_size(a), expected_len);
542 format_generic_odp_action(ds, a);
547 case OVS_ACTION_ATTR_OUTPUT:
548 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
550 case OVS_ACTION_ATTR_TUNNEL_POP:
551 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
553 case OVS_ACTION_ATTR_TUNNEL_PUSH:
554 format_odp_tnl_push_action(ds, a);
556 case OVS_ACTION_ATTR_USERSPACE:
557 format_odp_userspace_action(ds, a);
559 case OVS_ACTION_ATTR_RECIRC:
560 format_odp_recirc_action(ds, nl_attr_get_u32(a));
562 case OVS_ACTION_ATTR_HASH:
563 format_odp_hash_action(ds, nl_attr_get(a));
565 case OVS_ACTION_ATTR_SET_MASKED:
567 size = nl_attr_get_size(a) / 2;
568 ds_put_cstr(ds, "set(");
570 /* Masked set action not supported for tunnel key, which is bigger. */
571 if (size <= sizeof(struct ovs_key_ipv6)) {
572 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
573 sizeof(struct nlattr))];
574 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
575 sizeof(struct nlattr))];
577 mask->nla_type = attr->nla_type = nl_attr_type(a);
578 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
579 memcpy(attr + 1, (char *)(a + 1), size);
580 memcpy(mask + 1, (char *)(a + 1) + size, size);
581 format_odp_key_attr(attr, mask, NULL, ds, false);
583 format_odp_key_attr(a, NULL, NULL, ds, false);
585 ds_put_cstr(ds, ")");
587 case OVS_ACTION_ATTR_SET:
588 ds_put_cstr(ds, "set(");
589 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
590 ds_put_cstr(ds, ")");
592 case OVS_ACTION_ATTR_PUSH_VLAN:
593 vlan = nl_attr_get(a);
594 ds_put_cstr(ds, "push_vlan(");
595 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
596 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
598 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
599 ds_put_char(ds, ')');
601 case OVS_ACTION_ATTR_POP_VLAN:
602 ds_put_cstr(ds, "pop_vlan");
604 case OVS_ACTION_ATTR_PUSH_MPLS: {
605 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
606 ds_put_cstr(ds, "push_mpls(");
607 format_mpls_lse(ds, mpls->mpls_lse);
608 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
611 case OVS_ACTION_ATTR_POP_MPLS: {
612 ovs_be16 ethertype = nl_attr_get_be16(a);
613 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
616 case OVS_ACTION_ATTR_SAMPLE:
617 format_odp_sample_action(ds, a);
619 case OVS_ACTION_ATTR_UNSPEC:
620 case __OVS_ACTION_ATTR_MAX:
622 format_generic_odp_action(ds, a);
628 format_odp_actions(struct ds *ds, const struct nlattr *actions,
632 const struct nlattr *a;
635 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
637 ds_put_char(ds, ',');
639 format_odp_action(ds, a);
644 if (left == actions_len) {
645 ds_put_cstr(ds, "<empty>");
647 ds_put_format(ds, ",***%u leftover bytes*** (", left);
648 for (i = 0; i < left; i++) {
649 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
651 ds_put_char(ds, ')');
654 ds_put_cstr(ds, "drop");
658 /* Separate out parse_odp_userspace_action() function. */
660 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
663 union user_action_cookie cookie;
665 odp_port_t tunnel_out_port;
667 void *user_data = NULL;
668 size_t user_data_size = 0;
670 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
676 uint32_t probability;
677 uint32_t collector_set_id;
678 uint32_t obs_domain_id;
679 uint32_t obs_point_id;
682 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
683 "pcp=%i,output=%"SCNi32")%n",
684 &vid, &pcp, &output, &n1)) {
688 tci = vid | (pcp << VLAN_PCP_SHIFT);
693 cookie.type = USER_ACTION_COOKIE_SFLOW;
694 cookie.sflow.vlan_tci = htons(tci);
695 cookie.sflow.output = output;
697 user_data_size = sizeof cookie.sflow;
698 } else if (ovs_scan(&s[n], ",slow_path(%n",
703 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
704 cookie.slow_path.unused = 0;
705 cookie.slow_path.reason = 0;
707 res = parse_odp_flags(&s[n], slow_path_reason_to_string,
708 &cookie.slow_path.reason,
709 SLOW_PATH_REASON_MASK, NULL);
710 if (res < 0 || s[n + res] != ')') {
716 user_data_size = sizeof cookie.slow_path;
717 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
718 "collector_set_id=%"SCNi32","
719 "obs_domain_id=%"SCNi32","
720 "obs_point_id=%"SCNi32")%n",
721 &probability, &collector_set_id,
722 &obs_domain_id, &obs_point_id, &n1)) {
725 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
726 cookie.flow_sample.probability = probability;
727 cookie.flow_sample.collector_set_id = collector_set_id;
728 cookie.flow_sample.obs_domain_id = obs_domain_id;
729 cookie.flow_sample.obs_point_id = obs_point_id;
731 user_data_size = sizeof cookie.flow_sample;
732 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
735 cookie.type = USER_ACTION_COOKIE_IPFIX;
736 cookie.ipfix.output_odp_port = u32_to_odp(output);
738 user_data_size = sizeof cookie.ipfix;
739 } else if (ovs_scan(&s[n], ",userdata(%n",
744 ofpbuf_init(&buf, 16);
745 end = ofpbuf_put_hex(&buf, &s[n], NULL);
749 user_data = buf.data;
750 user_data_size = buf.size;
757 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
758 &tunnel_out_port, &n1)) {
759 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
761 } else if (s[n] == ')') {
762 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
771 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
773 struct eth_header *eth;
774 struct ip_header *ip;
775 struct udp_header *udp;
776 struct gre_base_hdr *greh;
777 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
779 uint32_t tnl_type = 0, header_len = 0;
783 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
786 eth = (struct eth_header *) data->header;
787 l3 = (data->header + sizeof *eth);
788 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
789 ip = (struct ip_header *) l3;
790 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
791 "eth(dst="ETH_ADDR_SCAN_FMT",",
794 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
798 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
799 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
802 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
805 eth->eth_type = htons(dl_type);
808 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
809 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
812 &ip->ip_proto, &ip->ip_tos,
813 &ip->ip_ttl, &ip->ip_frag_off)) {
816 put_16aligned_be32(&ip->ip_src, sip);
817 put_16aligned_be32(&ip->ip_dst, dip);
820 udp = (struct udp_header *) l4;
821 greh = (struct gre_base_hdr *) l4;
822 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
823 &udp_src, &udp_dst, &csum)) {
824 uint32_t vx_flags, vni;
826 udp->udp_src = htons(udp_src);
827 udp->udp_dst = htons(udp_dst);
829 udp->udp_csum = htons(csum);
831 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
833 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
835 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
836 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
837 tnl_type = OVS_VPORT_TYPE_VXLAN;
838 header_len = sizeof *eth + sizeof *ip +
839 sizeof *udp + sizeof *vxh;
840 } else if (ovs_scan_len(s, &n, "geneve(")) {
841 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
843 memset(gnh, 0, sizeof *gnh);
844 header_len = sizeof *eth + sizeof *ip +
845 sizeof *udp + sizeof *gnh;
847 if (ovs_scan_len(s, &n, "oam,")) {
850 if (ovs_scan_len(s, &n, "crit,")) {
853 if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
856 if (ovs_scan_len(s, &n, ",options(")) {
857 struct geneve_scan options;
860 memset(&options, 0, sizeof options);
861 len = scan_geneve(s + n, &options, NULL);
866 memcpy(gnh->options, options.d, options.len);
867 gnh->opt_len = options.len / 4;
868 header_len += options.len;
872 if (!ovs_scan_len(s, &n, "))")) {
876 gnh->proto_type = htons(ETH_TYPE_TEB);
877 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
878 tnl_type = OVS_VPORT_TYPE_GENEVE;
882 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
883 &gre_flags, &gre_proto)){
885 tnl_type = OVS_VPORT_TYPE_GRE;
886 greh->flags = htons(gre_flags);
887 greh->protocol = htons(gre_proto);
888 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
890 if (greh->flags & htons(GRE_CSUM)) {
891 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
895 memset(options, 0, sizeof *options);
896 *((ovs_be16 *)options) = htons(csum);
899 if (greh->flags & htons(GRE_KEY)) {
902 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
906 put_16aligned_be32(options, htonl(key));
909 if (greh->flags & htons(GRE_SEQ)) {
912 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
915 put_16aligned_be32(options, htonl(seq));
919 if (!ovs_scan_len(s, &n, "))")) {
923 header_len = sizeof *eth + sizeof *ip +
924 ((uint8_t *) options - (uint8_t *) greh);
929 /* check tunnel meta data. */
930 if (data->tnl_type != tnl_type) {
933 if (data->header_len != header_len) {
938 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
946 parse_odp_action(const char *s, const struct simap *port_names,
947 struct ofpbuf *actions)
953 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
954 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
960 int len = strcspn(s, delimiters);
961 struct simap_node *node;
963 node = simap_find_len(port_names, s, len);
965 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
974 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
975 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
980 if (!strncmp(s, "userspace(", 10)) {
981 return parse_odp_userspace_action(s, actions);
984 if (!strncmp(s, "set(", 4)) {
987 struct nlattr mask[128 / sizeof(struct nlattr)];
988 struct ofpbuf maskbuf;
989 struct nlattr *nested, *key;
992 /* 'mask' is big enough to hold any key. */
993 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
995 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
996 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1000 if (s[retval + 4] != ')') {
1004 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1007 size = nl_attr_get_size(mask);
1008 if (size == nl_attr_get_size(key)) {
1009 /* Change to masked set action if not fully masked. */
1010 if (!is_all_ones(mask + 1, size)) {
1011 key->nla_len += size;
1012 ofpbuf_put(actions, mask + 1, size);
1013 /* 'actions' may have been reallocated by ofpbuf_put(). */
1014 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1015 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1019 nl_msg_end_nested(actions, start_ofs);
1024 struct ovs_action_push_vlan push;
1025 int tpid = ETH_TYPE_VLAN;
1030 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1031 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1032 &vid, &pcp, &cfi, &n)
1033 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1034 &tpid, &vid, &pcp, &n)
1035 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1036 &tpid, &vid, &pcp, &cfi, &n)) {
1037 push.vlan_tpid = htons(tpid);
1038 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1039 | (pcp << VLAN_PCP_SHIFT)
1040 | (cfi ? VLAN_CFI : 0));
1041 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1042 &push, sizeof push);
1048 if (!strncmp(s, "pop_vlan", 8)) {
1049 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1057 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1058 && percentage >= 0. && percentage <= 100.0) {
1059 size_t sample_ofs, actions_ofs;
1062 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1063 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1064 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1065 (probability <= 0 ? 0
1066 : probability >= UINT32_MAX ? UINT32_MAX
1069 actions_ofs = nl_msg_start_nested(actions,
1070 OVS_SAMPLE_ATTR_ACTIONS);
1074 n += strspn(s + n, delimiters);
1079 retval = parse_odp_action(s + n, port_names, actions);
1085 nl_msg_end_nested(actions, actions_ofs);
1086 nl_msg_end_nested(actions, sample_ofs);
1088 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1096 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1097 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1103 struct ovs_action_push_tnl data;
1106 n = ovs_parse_tnl_push(s, &data);
1108 odp_put_tnl_push_action(actions, &data);
1117 /* Parses the string representation of datapath actions, in the format output
1118 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1119 * value. On success, the ODP actions are appended to 'actions' as a series of
1120 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1121 * way, 'actions''s data might be reallocated. */
1123 odp_actions_from_string(const char *s, const struct simap *port_names,
1124 struct ofpbuf *actions)
1128 if (!strcasecmp(s, "drop")) {
1132 old_size = actions->size;
1136 s += strspn(s, delimiters);
1141 retval = parse_odp_action(s, port_names, actions);
1142 if (retval < 0 || !strchr(delimiters, s[retval])) {
1143 actions->size = old_size;
1152 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1153 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1156 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1157 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1158 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1159 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1160 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1161 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1162 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1163 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1164 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1165 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1166 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1167 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1168 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1169 .next = ovs_vxlan_ext_attr_lens ,
1170 .next_max = OVS_VXLAN_EXT_MAX},
1173 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1174 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1175 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1176 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1177 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1178 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1179 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1180 .next = ovs_tun_key_attr_lens,
1181 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1182 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1183 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1184 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1185 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1186 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1187 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1188 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1189 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1190 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1191 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1192 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1193 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1194 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1195 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1196 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1199 /* Returns the correct length of the payload for a flow key attribute of the
1200 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1201 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1202 * payload is a nested type. */
1204 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1206 if (type > max_len) {
1207 return ATTR_LEN_INVALID;
1210 return tbl[type].len;
1214 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1216 size_t len = nl_attr_get_size(a);
1218 const uint8_t *unspec;
1221 unspec = nl_attr_get(a);
1222 for (i = 0; i < len; i++) {
1224 ds_put_char(ds, ' ');
1226 ds_put_format(ds, "%02x", unspec[i]);
1232 ovs_frag_type_to_string(enum ovs_frag_type type)
1235 case OVS_FRAG_TYPE_NONE:
1237 case OVS_FRAG_TYPE_FIRST:
1239 case OVS_FRAG_TYPE_LATER:
1241 case __OVS_FRAG_TYPE_MAX:
1247 static enum odp_key_fitness
1248 odp_tun_key_from_attr__(const struct nlattr *attr,
1249 const struct nlattr *flow_attrs, size_t flow_attr_len,
1250 const struct flow_tnl *src_tun, struct flow_tnl *tun)
1253 const struct nlattr *a;
1255 bool unknown = false;
1257 NL_NESTED_FOR_EACH(a, left, attr) {
1258 uint16_t type = nl_attr_type(a);
1259 size_t len = nl_attr_get_size(a);
1260 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1261 OVS_TUNNEL_ATTR_MAX, type);
1263 if (len != expected_len && expected_len >= 0) {
1264 return ODP_FIT_ERROR;
1268 case OVS_TUNNEL_KEY_ATTR_ID:
1269 tun->tun_id = nl_attr_get_be64(a);
1270 tun->flags |= FLOW_TNL_F_KEY;
1272 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1273 tun->ip_src = nl_attr_get_be32(a);
1275 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1276 tun->ip_dst = nl_attr_get_be32(a);
1278 case OVS_TUNNEL_KEY_ATTR_TOS:
1279 tun->ip_tos = nl_attr_get_u8(a);
1281 case OVS_TUNNEL_KEY_ATTR_TTL:
1282 tun->ip_ttl = nl_attr_get_u8(a);
1285 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1286 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1288 case OVS_TUNNEL_KEY_ATTR_CSUM:
1289 tun->flags |= FLOW_TNL_F_CSUM;
1291 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1292 tun->tp_src = nl_attr_get_be16(a);
1294 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1295 tun->tp_dst = nl_attr_get_be16(a);
1297 case OVS_TUNNEL_KEY_ATTR_OAM:
1298 tun->flags |= FLOW_TNL_F_OAM;
1300 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1301 static const struct nl_policy vxlan_opts_policy[] = {
1302 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1304 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1306 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1307 return ODP_FIT_ERROR;
1310 if (ext[OVS_VXLAN_EXT_GBP]) {
1311 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1313 tun->gbp_id = htons(gbp & 0xFFFF);
1314 tun->gbp_flags = (gbp >> 16) & 0xFF;
1319 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1320 if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1323 return ODP_FIT_ERROR;
1328 /* Allow this to show up as unexpected, if there are unknown
1329 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1336 return ODP_FIT_ERROR;
1339 return ODP_FIT_TOO_MUCH;
1341 return ODP_FIT_PERFECT;
1344 enum odp_key_fitness
1345 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1347 memset(tun, 0, sizeof *tun);
1348 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun);
1352 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1353 const struct flow_tnl *tun_flow_key,
1354 const struct ofpbuf *key_buf)
1358 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1360 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1361 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1362 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1364 if (tun_key->ip_src) {
1365 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1367 if (tun_key->ip_dst) {
1368 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1370 if (tun_key->ip_tos) {
1371 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1373 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1374 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1375 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1377 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1378 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1380 if (tun_key->tp_src) {
1381 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1383 if (tun_key->tp_dst) {
1384 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1386 if (tun_key->flags & FLOW_TNL_F_OAM) {
1387 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1389 if (tun_key->gbp_flags || tun_key->gbp_id) {
1390 size_t vxlan_opts_ofs;
1392 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1393 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1394 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1395 nl_msg_end_nested(a, vxlan_opts_ofs);
1398 if (tun_key == tun_flow_key) {
1399 tun_metadata_to_geneve_nlattr_flow(&tun_key->metadata, a);
1401 tun_metadata_to_geneve_nlattr_mask(key_buf, &tun_key->metadata,
1402 &tun_flow_key->metadata, a);
1405 nl_msg_end_nested(a, tun_key_ofs);
1409 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1411 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1415 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1417 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1418 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1420 if (attr == OVS_KEY_ATTR_IPV6) {
1421 const struct ovs_key_ipv6 *ipv6_mask = mask;
1424 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1425 == htonl(IPV6_LABEL_MASK))
1426 && ipv6_mask->ipv6_proto == UINT8_MAX
1427 && ipv6_mask->ipv6_tclass == UINT8_MAX
1428 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1429 && ipv6_mask->ipv6_frag == UINT8_MAX
1430 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1431 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1433 if (attr == OVS_KEY_ATTR_TUNNEL) {
1437 if (attr == OVS_KEY_ATTR_ARP) {
1438 /* ARP key has padding, ignore it. */
1439 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1440 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1441 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1442 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1445 return is_all_ones(mask, size);
1449 odp_mask_attr_is_exact(const struct nlattr *ma)
1451 enum ovs_key_attr attr = nl_attr_type(ma);
1455 if (attr == OVS_KEY_ATTR_TUNNEL) {
1458 mask = nl_attr_get(ma);
1459 size = nl_attr_get_size(ma);
1462 return odp_mask_is_exact(attr, mask, size);
1466 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1469 struct odp_portno_names *odp_portno_names;
1471 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1472 odp_portno_names->port_no = port_no;
1473 odp_portno_names->name = xstrdup(port_name);
1474 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1475 hash_odp_port(port_no));
1479 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1481 struct odp_portno_names *odp_portno_names;
1483 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1484 hash_odp_port(port_no), portno_names) {
1485 if (odp_portno_names->port_no == port_no) {
1486 return odp_portno_names->name;
1493 odp_portno_names_destroy(struct hmap *portno_names)
1495 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1496 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1497 hmap_node, portno_names) {
1498 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1499 free(odp_portno_names->name);
1500 free(odp_portno_names);
1504 /* Format helpers. */
1507 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1508 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1510 bool mask_empty = mask && eth_addr_is_zero(*mask);
1512 if (verbose || !mask_empty) {
1513 bool mask_full = !mask || eth_mask_is_exact(*mask);
1516 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1518 ds_put_format(ds, "%s=", name);
1519 eth_format_masked(key, *mask, ds);
1520 ds_put_char(ds, ',');
1526 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1527 const ovs_be64 *mask, bool verbose)
1529 bool mask_empty = mask && !*mask;
1531 if (verbose || !mask_empty) {
1532 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1534 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1535 if (!mask_full) { /* Partially masked. */
1536 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1538 ds_put_char(ds, ',');
1543 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1544 const ovs_be32 *mask, bool verbose)
1546 bool mask_empty = mask && !*mask;
1548 if (verbose || !mask_empty) {
1549 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1551 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1552 if (!mask_full) { /* Partially masked. */
1553 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1555 ds_put_char(ds, ',');
1560 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1561 const ovs_be32 (*mask_)[4], bool verbose)
1563 char buf[INET6_ADDRSTRLEN];
1564 const struct in6_addr *key = (const struct in6_addr *)key_;
1565 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1567 bool mask_empty = mask && ipv6_mask_is_any(mask);
1569 if (verbose || !mask_empty) {
1570 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1572 inet_ntop(AF_INET6, key, buf, sizeof buf);
1573 ds_put_format(ds, "%s=%s", name, buf);
1574 if (!mask_full) { /* Partially masked. */
1575 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1576 ds_put_format(ds, "/%s", buf);
1578 ds_put_char(ds, ',');
1583 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1584 const ovs_be32 *mask, bool verbose)
1586 bool mask_empty = mask && !*mask;
1588 if (verbose || !mask_empty) {
1589 bool mask_full = !mask
1590 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1592 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1593 if (!mask_full) { /* Partially masked. */
1594 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1596 ds_put_char(ds, ',');
1601 format_u8x(struct ds *ds, const char *name, uint8_t key,
1602 const uint8_t *mask, bool verbose)
1604 bool mask_empty = mask && !*mask;
1606 if (verbose || !mask_empty) {
1607 bool mask_full = !mask || *mask == UINT8_MAX;
1609 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1610 if (!mask_full) { /* Partially masked. */
1611 ds_put_format(ds, "/%#"PRIx8, *mask);
1613 ds_put_char(ds, ',');
1618 format_u8u(struct ds *ds, const char *name, uint8_t key,
1619 const uint8_t *mask, bool verbose)
1621 bool mask_empty = mask && !*mask;
1623 if (verbose || !mask_empty) {
1624 bool mask_full = !mask || *mask == UINT8_MAX;
1626 ds_put_format(ds, "%s=%"PRIu8, name, key);
1627 if (!mask_full) { /* Partially masked. */
1628 ds_put_format(ds, "/%#"PRIx8, *mask);
1630 ds_put_char(ds, ',');
1635 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1636 const ovs_be16 *mask, bool verbose)
1638 bool mask_empty = mask && !*mask;
1640 if (verbose || !mask_empty) {
1641 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1643 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1644 if (!mask_full) { /* Partially masked. */
1645 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1647 ds_put_char(ds, ',');
1652 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1653 const ovs_be16 *mask, bool verbose)
1655 bool mask_empty = mask && !*mask;
1657 if (verbose || !mask_empty) {
1658 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1660 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1661 if (!mask_full) { /* Partially masked. */
1662 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1664 ds_put_char(ds, ',');
1669 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1670 const uint16_t *mask, bool verbose)
1672 bool mask_empty = mask && !*mask;
1674 if (verbose || !mask_empty) {
1675 ds_put_cstr(ds, name);
1676 ds_put_char(ds, '(');
1678 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
1679 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
1680 } else { /* Fully masked. */
1681 format_flags(ds, flow_tun_flag_to_string, key, '|');
1683 ds_put_cstr(ds, "),");
1688 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1689 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1693 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1694 if (expected_len != ATTR_LEN_VARIABLE &&
1695 expected_len != ATTR_LEN_NESTED) {
1697 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1698 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1700 if (bad_key_len || bad_mask_len) {
1702 ds_put_format(ds, "key%u", nl_attr_type(a));
1705 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1706 nl_attr_get_size(a), expected_len);
1708 format_generic_odp_key(a, ds);
1710 ds_put_char(ds, '/');
1712 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1713 nl_attr_get_size(ma), expected_len);
1715 format_generic_odp_key(ma, ds);
1717 ds_put_char(ds, ')');
1726 format_unknown_key(struct ds *ds, const struct nlattr *a,
1727 const struct nlattr *ma)
1729 ds_put_format(ds, "key%u(", nl_attr_type(a));
1730 format_generic_odp_key(a, ds);
1731 if (ma && !odp_mask_attr_is_exact(ma)) {
1732 ds_put_char(ds, '/');
1733 format_generic_odp_key(ma, ds);
1735 ds_put_cstr(ds, "),");
1739 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1740 const struct nlattr *mask_attr, struct ds *ds,
1744 const struct nlattr *a;
1747 ofpbuf_init(&ofp, 100);
1748 NL_NESTED_FOR_EACH(a, left, attr) {
1749 uint16_t type = nl_attr_type(a);
1750 const struct nlattr *ma = NULL;
1753 ma = nl_attr_find__(nl_attr_get(mask_attr),
1754 nl_attr_get_size(mask_attr), type);
1756 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1762 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1763 OVS_VXLAN_EXT_MAX, true)) {
1768 case OVS_VXLAN_EXT_GBP: {
1769 uint32_t key = nl_attr_get_u32(a);
1770 ovs_be16 id, id_mask;
1771 uint8_t flags, flags_mask;
1773 id = htons(key & 0xFFFF);
1774 flags = (key >> 16) & 0xFF;
1776 uint32_t mask = nl_attr_get_u32(ma);
1777 id_mask = htons(mask & 0xFFFF);
1778 flags_mask = (mask >> 16) & 0xFF;
1781 ds_put_cstr(ds, "gbp(");
1782 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1783 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1785 ds_put_cstr(ds, "),");
1790 format_unknown_key(ds, a, ma);
1796 ofpbuf_uninit(&ofp);
1799 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1802 format_geneve_opts(const struct geneve_opt *opt,
1803 const struct geneve_opt *mask, int opts_len,
1804 struct ds *ds, bool verbose)
1806 while (opts_len > 0) {
1808 uint8_t data_len, data_len_mask;
1810 if (opts_len < sizeof *opt) {
1811 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1812 opts_len, sizeof *opt);
1816 data_len = opt->length * 4;
1818 if (mask->length == 0x1f) {
1819 data_len_mask = UINT8_MAX;
1821 data_len_mask = mask->length;
1824 len = sizeof *opt + data_len;
1825 if (len > opts_len) {
1826 ds_put_format(ds, "opt len %u greater than remaining %u",
1831 ds_put_char(ds, '{');
1832 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1834 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1835 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1836 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1837 ds_put_hex(ds, opt + 1, data_len);
1838 if (mask && !is_all_ones(mask + 1, data_len)) {
1839 ds_put_char(ds, '/');
1840 ds_put_hex(ds, mask + 1, data_len);
1845 ds_put_char(ds, '}');
1847 opt += len / sizeof(*opt);
1849 mask += len / sizeof(*opt);
1856 format_odp_tun_geneve(const struct nlattr *attr,
1857 const struct nlattr *mask_attr, struct ds *ds,
1860 int opts_len = nl_attr_get_size(attr);
1861 const struct geneve_opt *opt = nl_attr_get(attr);
1862 const struct geneve_opt *mask = mask_attr ?
1863 nl_attr_get(mask_attr) : NULL;
1865 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1866 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1867 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1871 format_geneve_opts(opt, mask, opts_len, ds, verbose);
1875 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1876 struct ds *ds, bool verbose)
1879 const struct nlattr *a;
1881 uint16_t mask_flags = 0;
1884 ofpbuf_init(&ofp, 100);
1885 NL_NESTED_FOR_EACH(a, left, attr) {
1886 enum ovs_tunnel_key_attr type = nl_attr_type(a);
1887 const struct nlattr *ma = NULL;
1890 ma = nl_attr_find__(nl_attr_get(mask_attr),
1891 nl_attr_get_size(mask_attr), type);
1893 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
1894 OVS_TUNNEL_KEY_ATTR_MAX,
1899 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
1900 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
1905 case OVS_TUNNEL_KEY_ATTR_ID:
1906 format_be64(ds, "tun_id", nl_attr_get_be64(a),
1907 ma ? nl_attr_get(ma) : NULL, verbose);
1908 flags |= FLOW_TNL_F_KEY;
1910 mask_flags |= FLOW_TNL_F_KEY;
1913 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1914 format_ipv4(ds, "src", nl_attr_get_be32(a),
1915 ma ? nl_attr_get(ma) : NULL, verbose);
1917 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1918 format_ipv4(ds, "dst", nl_attr_get_be32(a),
1919 ma ? nl_attr_get(ma) : NULL, verbose);
1921 case OVS_TUNNEL_KEY_ATTR_TOS:
1922 format_u8x(ds, "tos", nl_attr_get_u8(a),
1923 ma ? nl_attr_get(ma) : NULL, verbose);
1925 case OVS_TUNNEL_KEY_ATTR_TTL:
1926 format_u8u(ds, "ttl", nl_attr_get_u8(a),
1927 ma ? nl_attr_get(ma) : NULL, verbose);
1929 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1930 flags |= FLOW_TNL_F_DONT_FRAGMENT;
1932 case OVS_TUNNEL_KEY_ATTR_CSUM:
1933 flags |= FLOW_TNL_F_CSUM;
1935 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1936 format_be16(ds, "tp_src", nl_attr_get_be16(a),
1937 ma ? nl_attr_get(ma) : NULL, verbose);
1939 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1940 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
1941 ma ? nl_attr_get(ma) : NULL, verbose);
1943 case OVS_TUNNEL_KEY_ATTR_OAM:
1944 flags |= FLOW_TNL_F_OAM;
1946 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1947 ds_put_cstr(ds, "vxlan(");
1948 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
1949 ds_put_cstr(ds, "),");
1951 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1952 ds_put_cstr(ds, "geneve(");
1953 format_odp_tun_geneve(a, ma, ds, verbose);
1954 ds_put_cstr(ds, "),");
1956 case __OVS_TUNNEL_KEY_ATTR_MAX:
1958 format_unknown_key(ds, a, ma);
1963 /* Flags can have a valid mask even if the attribute is not set, so
1964 * we need to collect these separately. */
1966 NL_NESTED_FOR_EACH(a, left, mask_attr) {
1967 switch (nl_attr_type(a)) {
1968 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1969 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
1971 case OVS_TUNNEL_KEY_ATTR_CSUM:
1972 mask_flags |= FLOW_TNL_F_CSUM;
1974 case OVS_TUNNEL_KEY_ATTR_OAM:
1975 mask_flags |= FLOW_TNL_F_OAM;
1981 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
1984 ofpbuf_uninit(&ofp);
1988 format_frag(struct ds *ds, const char *name, uint8_t key,
1989 const uint8_t *mask, bool verbose)
1991 bool mask_empty = mask && !*mask;
1993 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1994 if (verbose || !mask_empty) {
1995 bool mask_full = !mask || *mask == UINT8_MAX;
1997 if (!mask_full) { /* Partially masked. */
1998 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2001 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2007 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2008 const struct hmap *portno_names, struct ds *ds,
2011 enum ovs_key_attr attr = nl_attr_type(a);
2012 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2015 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2017 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2019 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2020 OVS_KEY_ATTR_MAX, false)) {
2024 ds_put_char(ds, '(');
2026 case OVS_KEY_ATTR_ENCAP:
2027 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2028 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2029 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2031 } else if (nl_attr_get_size(a)) {
2032 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2037 case OVS_KEY_ATTR_PRIORITY:
2038 case OVS_KEY_ATTR_SKB_MARK:
2039 case OVS_KEY_ATTR_DP_HASH:
2040 case OVS_KEY_ATTR_RECIRC_ID:
2041 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2043 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2047 case OVS_KEY_ATTR_TUNNEL:
2048 format_odp_tun_attr(a, ma, ds, verbose);
2051 case OVS_KEY_ATTR_IN_PORT:
2052 if (portno_names && verbose && is_exact) {
2053 char *name = odp_portno_names_get(portno_names,
2054 u32_to_odp(nl_attr_get_u32(a)));
2056 ds_put_format(ds, "%s", name);
2058 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2061 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2063 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2068 case OVS_KEY_ATTR_ETHERNET: {
2069 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2070 const struct ovs_key_ethernet *key = nl_attr_get(a);
2072 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2073 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2077 case OVS_KEY_ATTR_VLAN:
2078 format_vlan_tci(ds, nl_attr_get_be16(a),
2079 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2082 case OVS_KEY_ATTR_MPLS: {
2083 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2084 const struct ovs_key_mpls *mpls_mask = NULL;
2085 size_t size = nl_attr_get_size(a);
2087 if (!size || size % sizeof *mpls_key) {
2088 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2092 mpls_mask = nl_attr_get(ma);
2093 if (size != nl_attr_get_size(ma)) {
2094 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2095 "mask length %"PRIuSIZE")",
2096 size, nl_attr_get_size(ma));
2100 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2103 case OVS_KEY_ATTR_ETHERTYPE:
2104 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2106 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2110 case OVS_KEY_ATTR_IPV4: {
2111 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2112 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2114 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2115 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2116 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2118 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2119 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2120 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2125 case OVS_KEY_ATTR_IPV6: {
2126 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2127 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2129 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2130 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2131 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2133 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2135 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2137 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2139 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2144 /* These have the same structure and format. */
2145 case OVS_KEY_ATTR_TCP:
2146 case OVS_KEY_ATTR_UDP:
2147 case OVS_KEY_ATTR_SCTP: {
2148 const struct ovs_key_tcp *key = nl_attr_get(a);
2149 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2151 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2152 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2156 case OVS_KEY_ATTR_TCP_FLAGS:
2158 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2159 ntohs(nl_attr_get_be16(a)),
2160 TCP_FLAGS(nl_attr_get_be16(ma)),
2161 TCP_FLAGS(OVS_BE16_MAX));
2163 format_flags(ds, packet_tcp_flag_to_string,
2164 ntohs(nl_attr_get_be16(a)), '|');
2168 case OVS_KEY_ATTR_ICMP: {
2169 const struct ovs_key_icmp *key = nl_attr_get(a);
2170 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2172 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2173 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2177 case OVS_KEY_ATTR_ICMPV6: {
2178 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2179 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2181 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2183 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2188 case OVS_KEY_ATTR_ARP: {
2189 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2190 const struct ovs_key_arp *key = nl_attr_get(a);
2192 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2193 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2194 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2195 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2196 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2200 case OVS_KEY_ATTR_ND: {
2201 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2202 const struct ovs_key_nd *key = nl_attr_get(a);
2204 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2206 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2207 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2212 case OVS_KEY_ATTR_UNSPEC:
2213 case __OVS_KEY_ATTR_MAX:
2215 format_generic_odp_key(a, ds);
2217 ds_put_char(ds, '/');
2218 format_generic_odp_key(ma, ds);
2222 ds_put_char(ds, ')');
2225 static struct nlattr *
2226 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2227 struct ofpbuf *ofp, const struct nlattr *key)
2229 const struct nlattr *a;
2231 int type = nl_attr_type(key);
2232 int size = nl_attr_get_size(key);
2234 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2235 nl_msg_put_unspec_zero(ofp, type, size);
2239 if (tbl[type].next) {
2240 tbl = tbl[type].next;
2241 max = tbl[type].next_max;
2244 nested_mask = nl_msg_start_nested(ofp, type);
2245 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2246 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2248 nl_msg_end_nested(ofp, nested_mask);
2255 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2259 if (ovs_scan(s, "ufid:")) {
2262 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2274 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2276 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2279 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2280 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2281 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2282 * non-null and 'verbose' is true, translates odp port number to its name. */
2284 odp_flow_format(const struct nlattr *key, size_t key_len,
2285 const struct nlattr *mask, size_t mask_len,
2286 const struct hmap *portno_names, struct ds *ds, bool verbose)
2289 const struct nlattr *a;
2291 bool has_ethtype_key = false;
2292 const struct nlattr *ma = NULL;
2294 bool first_field = true;
2296 ofpbuf_init(&ofp, 100);
2297 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2298 bool is_nested_attr;
2299 bool is_wildcard = false;
2300 int attr_type = nl_attr_type(a);
2302 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2303 has_ethtype_key = true;
2306 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2307 OVS_KEY_ATTR_MAX, attr_type) ==
2310 if (mask && mask_len) {
2311 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2312 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2315 if (verbose || !is_wildcard || is_nested_attr) {
2316 if (is_wildcard && !ma) {
2317 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2322 ds_put_char(ds, ',');
2324 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2325 first_field = false;
2329 ofpbuf_uninit(&ofp);
2334 if (left == key_len) {
2335 ds_put_cstr(ds, "<empty>");
2337 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2338 for (i = 0; i < left; i++) {
2339 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2341 ds_put_char(ds, ')');
2343 if (!has_ethtype_key) {
2344 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2346 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2347 ntohs(nl_attr_get_be16(ma)));
2351 ds_put_cstr(ds, "<empty>");
2355 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2356 * OVS_KEY_ATTR_* attributes in 'key'. */
2358 odp_flow_key_format(const struct nlattr *key,
2359 size_t key_len, struct ds *ds)
2361 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2365 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2367 if (!strcasecmp(s, "no")) {
2368 *type = OVS_FRAG_TYPE_NONE;
2369 } else if (!strcasecmp(s, "first")) {
2370 *type = OVS_FRAG_TYPE_FIRST;
2371 } else if (!strcasecmp(s, "later")) {
2372 *type = OVS_FRAG_TYPE_LATER;
2382 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2383 uint8_t (*mask)[ETH_ADDR_LEN])
2387 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2391 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2392 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2395 memset(mask, 0xff, sizeof *mask);
2404 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2408 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2412 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2413 IP_SCAN_ARGS(mask), &n)) {
2416 *mask = OVS_BE32_MAX;
2425 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2428 char ipv6_s[IPV6_SCAN_LEN + 1];
2430 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2431 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2435 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2436 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2439 memset(mask, 0xff, sizeof *mask);
2448 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2453 if (ovs_scan(s, "%i%n", &key_, &n)
2454 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2459 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2460 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2462 *mask = htonl(mask_);
2464 *mask = htonl(IPV6_LABEL_MASK);
2473 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2477 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2481 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2493 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2497 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2501 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2513 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2515 uint16_t key_, mask_;
2518 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2523 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2525 *mask = htons(mask_);
2527 *mask = OVS_BE16_MAX;
2536 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2538 uint64_t key_, mask_;
2541 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2544 *key = htonll(key_);
2546 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2548 *mask = htonll(mask_);
2550 *mask = OVS_BE64_MAX;
2559 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2561 uint32_t flags, fmask;
2564 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
2565 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2566 if (n >= 0 && s[n] == ')') {
2577 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2579 uint32_t flags, fmask;
2582 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
2583 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2585 *key = htons(flags);
2587 *mask = htons(fmask);
2595 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2599 enum ovs_frag_type frag_type;
2601 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2602 && ovs_frag_type_from_string(frag, &frag_type)) {
2615 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2616 const struct simap *port_names)
2620 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2624 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2631 } else if (port_names) {
2632 const struct simap_node *node;
2635 len = strcspn(s, ")");
2636 node = simap_find_len(port_names, s, len);
2649 /* Helper for vlan parsing. */
2650 struct ovs_key_vlan__ {
2655 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2657 const uint16_t mask = ((1U << bits) - 1) << offset;
2659 if (value >> bits) {
2663 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2668 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2671 uint16_t key_, mask_;
2674 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2677 if (set_be16_bf(key, bits, offset, key_)) {
2679 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2682 if (!set_be16_bf(mask, bits, offset, mask_)) {
2686 *mask |= htons(((1U << bits) - 1) << offset);
2696 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2698 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2702 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2704 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2708 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2710 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2715 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2717 const uint32_t mask = ((1U << bits) - 1) << offset;
2719 if (value >> bits) {
2723 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2728 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2731 uint32_t key_, mask_;
2734 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2737 if (set_be32_bf(key, bits, offset, key_)) {
2739 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2742 if (!set_be32_bf(mask, bits, offset, mask_)) {
2746 *mask |= htonl(((1U << bits) - 1) << offset);
2756 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2758 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2762 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2764 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2768 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2770 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2774 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2776 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2780 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2782 const char *s_base = s;
2783 ovs_be16 id = 0, id_mask = 0;
2784 uint8_t flags = 0, flags_mask = 0;
2786 if (!strncmp(s, "id=", 3)) {
2788 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2794 if (!strncmp(s, "flags=", 6)) {
2796 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2799 if (!strncmp(s, "))", 2)) {
2802 *key = (flags << 16) | ntohs(id);
2804 *mask = (flags_mask << 16) | ntohs(id_mask);
2814 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2816 const char *s_base = s;
2817 struct geneve_opt *opt = key->d;
2818 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2819 int len_remain = sizeof key->d;
2821 while (s[0] == '{' && len_remain >= sizeof *opt) {
2825 len_remain -= sizeof *opt;
2827 if (!strncmp(s, "class=", 6)) {
2829 s += scan_be16(s, &opt->opt_class,
2830 mask ? &opt_mask->opt_class : NULL);
2832 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2838 if (!strncmp(s, "type=", 5)) {
2840 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2842 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2848 if (!strncmp(s, "len=", 4)) {
2849 uint8_t opt_len, opt_len_mask;
2851 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2853 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2856 opt->length = opt_len / 4;
2858 opt_mask->length = opt_len_mask;
2862 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2868 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2875 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2876 data_len, (char **)&s)) {
2887 opt += 1 + data_len / 4;
2889 opt_mask += 1 + data_len / 4;
2891 len_remain -= data_len;
2896 int len = sizeof key->d - len_remain;
2910 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
2912 const uint16_t *flags = data_;
2914 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
2915 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
2917 if (*flags & FLOW_TNL_F_CSUM) {
2918 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
2920 if (*flags & FLOW_TNL_F_OAM) {
2921 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
2926 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
2928 const uint32_t *gbp = data_;
2931 size_t vxlan_opts_ofs;
2933 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
2934 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
2935 nl_msg_end_nested(a, vxlan_opts_ofs);
2940 geneve_to_attr(struct ofpbuf *a, const void *data_)
2942 const struct geneve_scan *geneve = data_;
2944 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
2948 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
2950 unsigned long call_fn = (unsigned long)FUNC; \
2952 typedef void (*fn)(struct ofpbuf *, const void *); \
2954 func(BUF, &(DATA)); \
2956 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2960 #define SCAN_IF(NAME) \
2961 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2962 const char *start = s; \
2967 /* Usually no special initialization is needed. */
2968 #define SCAN_BEGIN(NAME, TYPE) \
2971 memset(&skey, 0, sizeof skey); \
2972 memset(&smask, 0, sizeof smask); \
2976 /* Init as fully-masked as mask will not be scanned. */
2977 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2980 memset(&skey, 0, sizeof skey); \
2981 memset(&smask, 0xff, sizeof smask); \
2985 /* VLAN needs special initialization. */
2986 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2988 TYPE skey = KEY_INIT; \
2989 TYPE smask = MASK_INIT; \
2993 /* Scan unnamed entry as 'TYPE' */
2994 #define SCAN_TYPE(TYPE, KEY, MASK) \
2995 len = scan_##TYPE(s, KEY, MASK); \
3001 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3002 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3003 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3004 s += strlen(NAME); \
3005 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3009 #define SCAN_FINISH() \
3010 } while (*s++ == ',' && len != 0); \
3011 if (s[-1] != ')') { \
3015 #define SCAN_FINISH_SINGLE() \
3017 if (*s++ != ')') { \
3021 /* Beginning of nested attribute. */
3022 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3024 size_t key_offset, mask_offset; \
3025 key_offset = nl_msg_start_nested(key, ATTR); \
3027 mask_offset = nl_msg_start_nested(mask, ATTR); \
3032 #define SCAN_END_NESTED() \
3034 nl_msg_end_nested(key, key_offset); \
3036 nl_msg_end_nested(mask, mask_offset); \
3041 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3042 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3044 memset(&skey, 0, sizeof skey); \
3045 memset(&smask, 0xff, sizeof smask); \
3046 s += strlen(NAME); \
3047 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3048 SCAN_PUT(ATTR, FUNC); \
3052 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3053 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3055 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3056 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3058 #define SCAN_PUT(ATTR, FUNC) \
3059 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3060 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3062 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3066 #define SCAN_END(ATTR) \
3068 SCAN_PUT(ATTR, NULL); \
3072 #define SCAN_END_SINGLE(ATTR) \
3073 SCAN_FINISH_SINGLE(); \
3074 SCAN_PUT(ATTR, NULL); \
3078 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3079 SCAN_BEGIN(NAME, TYPE) { \
3080 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3081 } SCAN_END_SINGLE(ATTR)
3083 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3084 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3085 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3086 } SCAN_END_SINGLE(ATTR)
3088 /* scan_port needs one extra argument. */
3089 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3090 SCAN_BEGIN(NAME, TYPE) { \
3091 len = scan_port(s, &skey, &smask, port_names); \
3096 } SCAN_END_SINGLE(ATTR)
3099 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3100 struct ofpbuf *key, struct ofpbuf *mask)
3106 len = odp_ufid_from_string(s, &ufid);
3111 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3112 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3113 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3114 OVS_KEY_ATTR_RECIRC_ID);
3115 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3117 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3118 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3119 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3120 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3121 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3122 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3123 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3124 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3125 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3126 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3128 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3129 } SCAN_END_NESTED();
3131 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3133 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3134 SCAN_FIELD("src=", eth, eth_src);
3135 SCAN_FIELD("dst=", eth, eth_dst);
3136 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3138 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3139 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3140 SCAN_FIELD("vid=", vid, tci);
3141 SCAN_FIELD("pcp=", pcp, tci);
3142 SCAN_FIELD("cfi=", cfi, tci);
3143 } SCAN_END(OVS_KEY_ATTR_VLAN);
3145 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3147 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3148 SCAN_FIELD("label=", mpls_label, mpls_lse);
3149 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3150 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3151 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3152 } SCAN_END(OVS_KEY_ATTR_MPLS);
3154 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3155 SCAN_FIELD("src=", ipv4, ipv4_src);
3156 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3157 SCAN_FIELD("proto=", u8, ipv4_proto);
3158 SCAN_FIELD("tos=", u8, ipv4_tos);
3159 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3160 SCAN_FIELD("frag=", frag, ipv4_frag);
3161 } SCAN_END(OVS_KEY_ATTR_IPV4);
3163 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3164 SCAN_FIELD("src=", ipv6, ipv6_src);
3165 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3166 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3167 SCAN_FIELD("proto=", u8, ipv6_proto);
3168 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3169 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3170 SCAN_FIELD("frag=", frag, ipv6_frag);
3171 } SCAN_END(OVS_KEY_ATTR_IPV6);
3173 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3174 SCAN_FIELD("src=", be16, tcp_src);
3175 SCAN_FIELD("dst=", be16, tcp_dst);
3176 } SCAN_END(OVS_KEY_ATTR_TCP);
3178 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3180 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3181 SCAN_FIELD("src=", be16, udp_src);
3182 SCAN_FIELD("dst=", be16, udp_dst);
3183 } SCAN_END(OVS_KEY_ATTR_UDP);
3185 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3186 SCAN_FIELD("src=", be16, sctp_src);
3187 SCAN_FIELD("dst=", be16, sctp_dst);
3188 } SCAN_END(OVS_KEY_ATTR_SCTP);
3190 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3191 SCAN_FIELD("type=", u8, icmp_type);
3192 SCAN_FIELD("code=", u8, icmp_code);
3193 } SCAN_END(OVS_KEY_ATTR_ICMP);
3195 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3196 SCAN_FIELD("type=", u8, icmpv6_type);
3197 SCAN_FIELD("code=", u8, icmpv6_code);
3198 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3200 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3201 SCAN_FIELD("sip=", ipv4, arp_sip);
3202 SCAN_FIELD("tip=", ipv4, arp_tip);
3203 SCAN_FIELD("op=", be16, arp_op);
3204 SCAN_FIELD("sha=", eth, arp_sha);
3205 SCAN_FIELD("tha=", eth, arp_tha);
3206 } SCAN_END(OVS_KEY_ATTR_ARP);
3208 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3209 SCAN_FIELD("target=", ipv6, nd_target);
3210 SCAN_FIELD("sll=", eth, nd_sll);
3211 SCAN_FIELD("tll=", eth, nd_tll);
3212 } SCAN_END(OVS_KEY_ATTR_ND);
3214 /* Encap open-coded. */
3215 if (!strncmp(s, "encap(", 6)) {
3216 const char *start = s;
3217 size_t encap, encap_mask = 0;
3219 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3221 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3228 s += strspn(s, delimiters);
3231 } else if (*s == ')') {
3235 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3243 nl_msg_end_nested(key, encap);
3245 nl_msg_end_nested(mask, encap_mask);
3254 /* Parses the string representation of a datapath flow key, in the
3255 * format output by odp_flow_key_format(). Returns 0 if successful,
3256 * otherwise a positive errno value. On success, the flow key is
3257 * appended to 'key' as a series of Netlink attributes. On failure, no
3258 * data is appended to 'key'. Either way, 'key''s data might be
3261 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3262 * to a port number. (Port names may be used instead of port numbers in
3265 * On success, the attributes appended to 'key' are individually syntactically
3266 * valid, but they may not be valid as a sequence. 'key' might, for example,
3267 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3269 odp_flow_from_string(const char *s, const struct simap *port_names,
3270 struct ofpbuf *key, struct ofpbuf *mask)
3272 const size_t old_size = key->size;
3276 s += strspn(s, delimiters);
3281 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3283 key->size = old_size;
3293 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3296 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3297 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3298 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3299 * must use a zero mask for the netlink frag field, and all ones mask
3301 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3303 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3304 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3305 : OVS_FRAG_TYPE_FIRST;
3308 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3309 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3310 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3312 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3314 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3316 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3318 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3319 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3320 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3321 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3323 /* These share the same layout. */
3325 struct ovs_key_tcp tcp;
3326 struct ovs_key_udp udp;
3327 struct ovs_key_sctp sctp;
3330 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3331 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3334 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3335 bool export_mask, struct ofpbuf *buf)
3337 struct ovs_key_ethernet *eth_key;
3339 const struct flow *flow = parms->flow;
3340 const struct flow *data = export_mask ? parms->mask : parms->flow;
3342 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3344 if (flow->tunnel.ip_dst || export_mask) {
3345 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3349 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3351 if (parms->support.recirc) {
3352 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3353 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3356 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3357 * is not the magical value "ODPP_NONE". */
3358 if (export_mask || parms->odp_in_port != ODPP_NONE) {
3359 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3362 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3364 get_ethernet_key(data, eth_key);
3366 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3368 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3370 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3372 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3373 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3374 if (flow->vlan_tci == htons(0)) {
3381 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3382 /* For backwards compatibility with kernels that don't support
3383 * wildcarding, the following convention is used to encode the
3384 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3387 * -------- -------- -------
3388 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3389 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3390 * <none> 0xffff Any non-Ethernet II frame (except valid
3391 * 802.3 SNAP packet with valid eth_type).
3394 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3399 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3401 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3402 struct ovs_key_ipv4 *ipv4_key;
3404 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3406 get_ipv4_key(data, ipv4_key, export_mask);
3407 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3408 struct ovs_key_ipv6 *ipv6_key;
3410 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3412 get_ipv6_key(data, ipv6_key, export_mask);
3413 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3414 flow->dl_type == htons(ETH_TYPE_RARP)) {
3415 struct ovs_key_arp *arp_key;
3417 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3419 get_arp_key(data, arp_key);
3420 } else if (eth_type_mpls(flow->dl_type)) {
3421 struct ovs_key_mpls *mpls_key;
3424 n = flow_count_mpls_labels(flow, NULL);
3426 n = MIN(n, parms->support.max_mpls_depth);
3428 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3429 n * sizeof *mpls_key);
3430 for (i = 0; i < n; i++) {
3431 mpls_key[i].mpls_lse = data->mpls_lse[i];
3435 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3436 if (flow->nw_proto == IPPROTO_TCP) {
3437 union ovs_key_tp *tcp_key;
3439 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3441 get_tp_key(data, tcp_key);
3442 if (data->tcp_flags) {
3443 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3445 } else if (flow->nw_proto == IPPROTO_UDP) {
3446 union ovs_key_tp *udp_key;
3448 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3450 get_tp_key(data, udp_key);
3451 } else if (flow->nw_proto == IPPROTO_SCTP) {
3452 union ovs_key_tp *sctp_key;
3454 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3456 get_tp_key(data, sctp_key);
3457 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3458 && flow->nw_proto == IPPROTO_ICMP) {
3459 struct ovs_key_icmp *icmp_key;
3461 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3463 icmp_key->icmp_type = ntohs(data->tp_src);
3464 icmp_key->icmp_code = ntohs(data->tp_dst);
3465 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3466 && flow->nw_proto == IPPROTO_ICMPV6) {
3467 struct ovs_key_icmpv6 *icmpv6_key;
3469 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3470 sizeof *icmpv6_key);
3471 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3472 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3474 if (flow->tp_dst == htons(0)
3475 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3476 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3477 && (!export_mask || (data->tp_src == htons(0xffff)
3478 && data->tp_dst == htons(0xffff)))) {
3480 struct ovs_key_nd *nd_key;
3482 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3484 memcpy(nd_key->nd_target, &data->nd_target,
3485 sizeof nd_key->nd_target);
3486 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3487 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3494 nl_msg_end_nested(buf, encap);
3498 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3500 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3501 * capable of being expanded to allow for that much space. */
3503 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3506 odp_flow_key_from_flow__(parms, false, buf);
3509 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3512 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3513 * capable of being expanded to allow for that much space. */
3515 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3518 odp_flow_key_from_flow__(parms, true, buf);
3521 /* Generate ODP flow key from the given packet metadata */
3523 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3525 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3527 if (md->tunnel.ip_dst) {
3528 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3531 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3533 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3534 * value "ODPP_NONE". */
3535 if (md->in_port.odp_port != ODPP_NONE) {
3536 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3540 /* Generate packet metadata from the given ODP flow key. */
3542 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3543 struct pkt_metadata *md)
3545 const struct nlattr *nla;
3547 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3548 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3549 1u << OVS_KEY_ATTR_IN_PORT;
3551 pkt_metadata_init(md, ODPP_NONE);
3553 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3554 uint16_t type = nl_attr_type(nla);
3555 size_t len = nl_attr_get_size(nla);
3556 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3557 OVS_KEY_ATTR_MAX, type);
3559 if (len != expected_len && expected_len >= 0) {
3564 case OVS_KEY_ATTR_RECIRC_ID:
3565 md->recirc_id = nl_attr_get_u32(nla);
3566 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3568 case OVS_KEY_ATTR_DP_HASH:
3569 md->dp_hash = nl_attr_get_u32(nla);
3570 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3572 case OVS_KEY_ATTR_PRIORITY:
3573 md->skb_priority = nl_attr_get_u32(nla);
3574 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3576 case OVS_KEY_ATTR_SKB_MARK:
3577 md->pkt_mark = nl_attr_get_u32(nla);
3578 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3580 case OVS_KEY_ATTR_TUNNEL: {
3581 enum odp_key_fitness res;
3583 res = odp_tun_key_from_attr(nla, &md->tunnel);
3584 if (res == ODP_FIT_ERROR) {
3585 memset(&md->tunnel, 0, sizeof md->tunnel);
3586 } else if (res == ODP_FIT_PERFECT) {
3587 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3591 case OVS_KEY_ATTR_IN_PORT:
3592 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3593 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3599 if (!wanted_attrs) {
3600 return; /* Have everything. */
3606 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3608 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3609 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3610 key_len / sizeof(uint32_t), 0);
3614 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3615 uint64_t attrs, int out_of_range_attr,
3616 const struct nlattr *key, size_t key_len)
3621 if (VLOG_DROP_DBG(rl)) {
3626 for (i = 0; i < 64; i++) {
3627 if (attrs & (UINT64_C(1) << i)) {
3628 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3630 ds_put_format(&s, " %s",
3631 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3634 if (out_of_range_attr) {
3635 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3638 ds_put_cstr(&s, ": ");
3639 odp_flow_key_format(key, key_len, &s);
3641 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3646 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3648 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3651 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3654 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3655 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3656 return 0xff; /* Error. */
3659 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3660 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3661 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3665 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3666 const struct nlattr *attrs[], uint64_t *present_attrsp,
3667 int *out_of_range_attrp)
3669 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3670 const struct nlattr *nla;
3671 uint64_t present_attrs;
3674 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3676 *out_of_range_attrp = 0;
3677 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3678 uint16_t type = nl_attr_type(nla);
3679 size_t len = nl_attr_get_size(nla);
3680 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3681 OVS_KEY_ATTR_MAX, type);
3683 if (len != expected_len && expected_len >= 0) {
3684 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3686 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3687 "length %d", ovs_key_attr_to_string(type, namebuf,
3693 if (type > OVS_KEY_ATTR_MAX) {
3694 *out_of_range_attrp = type;
3696 if (present_attrs & (UINT64_C(1) << type)) {
3697 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3699 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3700 ovs_key_attr_to_string(type,
3701 namebuf, sizeof namebuf));
3705 present_attrs |= UINT64_C(1) << type;
3710 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3714 *present_attrsp = present_attrs;
3718 static enum odp_key_fitness
3719 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3720 uint64_t expected_attrs,
3721 const struct nlattr *key, size_t key_len)
3723 uint64_t missing_attrs;
3724 uint64_t extra_attrs;
3726 missing_attrs = expected_attrs & ~present_attrs;
3727 if (missing_attrs) {
3728 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3729 log_odp_key_attributes(&rl, "expected but not present",
3730 missing_attrs, 0, key, key_len);
3731 return ODP_FIT_TOO_LITTLE;
3734 extra_attrs = present_attrs & ~expected_attrs;
3735 if (extra_attrs || out_of_range_attr) {
3736 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3737 log_odp_key_attributes(&rl, "present but not expected",
3738 extra_attrs, out_of_range_attr, key, key_len);
3739 return ODP_FIT_TOO_MUCH;
3742 return ODP_FIT_PERFECT;
3746 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3747 uint64_t present_attrs, uint64_t *expected_attrs,
3748 struct flow *flow, const struct flow *src_flow)
3750 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3751 bool is_mask = flow != src_flow;
3753 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3754 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3755 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3756 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3757 ntohs(flow->dl_type));
3760 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3761 flow->dl_type != htons(0xffff)) {
3764 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3767 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3768 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3769 /* See comments in odp_flow_key_from_flow__(). */
3770 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3777 static enum odp_key_fitness
3778 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3779 uint64_t present_attrs, int out_of_range_attr,
3780 uint64_t expected_attrs, struct flow *flow,
3781 const struct nlattr *key, size_t key_len,
3782 const struct flow *src_flow)
3784 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3785 bool is_mask = src_flow != flow;
3786 const void *check_start = NULL;
3787 size_t check_len = 0;
3788 enum ovs_key_attr expected_bit = 0xff;
3790 if (eth_type_mpls(src_flow->dl_type)) {
3791 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3792 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3794 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3795 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3796 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3797 int n = size / sizeof(ovs_be32);
3800 if (!size || size % sizeof(ovs_be32)) {
3801 return ODP_FIT_ERROR;
3803 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3804 return ODP_FIT_ERROR;
3807 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3808 flow->mpls_lse[i] = mpls_lse[i];
3810 if (n > FLOW_MAX_MPLS_LABELS) {
3811 return ODP_FIT_TOO_MUCH;
3815 /* BOS may be set only in the innermost label. */
3816 for (i = 0; i < n - 1; i++) {
3817 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3818 return ODP_FIT_ERROR;
3822 /* BOS must be set in the innermost label. */
3823 if (n < FLOW_MAX_MPLS_LABELS
3824 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3825 return ODP_FIT_TOO_LITTLE;
3831 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3833 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3835 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3836 const struct ovs_key_ipv4 *ipv4_key;
3838 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3839 put_ipv4_key(ipv4_key, flow, is_mask);
3840 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3841 return ODP_FIT_ERROR;
3844 check_start = ipv4_key;
3845 check_len = sizeof *ipv4_key;
3846 expected_bit = OVS_KEY_ATTR_IPV4;
3849 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3851 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3853 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3854 const struct ovs_key_ipv6 *ipv6_key;
3856 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3857 put_ipv6_key(ipv6_key, flow, is_mask);
3858 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3859 return ODP_FIT_ERROR;
3862 check_start = ipv6_key;
3863 check_len = sizeof *ipv6_key;
3864 expected_bit = OVS_KEY_ATTR_IPV6;
3867 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3868 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3870 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3872 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3873 const struct ovs_key_arp *arp_key;
3875 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3876 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3877 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3878 "key", ntohs(arp_key->arp_op));
3879 return ODP_FIT_ERROR;
3881 put_arp_key(arp_key, flow);
3883 check_start = arp_key;
3884 check_len = sizeof *arp_key;
3885 expected_bit = OVS_KEY_ATTR_ARP;
3891 if (check_len > 0) { /* Happens only when 'is_mask'. */
3892 if (!is_all_zeros(check_start, check_len) &&
3893 flow->dl_type != htons(0xffff)) {
3894 return ODP_FIT_ERROR;
3896 expected_attrs |= UINT64_C(1) << expected_bit;
3900 expected_bit = OVS_KEY_ATTR_UNSPEC;
3901 if (src_flow->nw_proto == IPPROTO_TCP
3902 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3903 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3904 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3906 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3908 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3909 const union ovs_key_tp *tcp_key;
3911 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3912 put_tp_key(tcp_key, flow);
3913 expected_bit = OVS_KEY_ATTR_TCP;
3915 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3916 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3917 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3919 } else if (src_flow->nw_proto == IPPROTO_UDP
3920 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3921 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3922 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3924 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3926 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3927 const union ovs_key_tp *udp_key;
3929 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3930 put_tp_key(udp_key, flow);
3931 expected_bit = OVS_KEY_ATTR_UDP;
3933 } else if (src_flow->nw_proto == IPPROTO_SCTP
3934 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3935 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3936 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3938 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3940 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3941 const union ovs_key_tp *sctp_key;
3943 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3944 put_tp_key(sctp_key, flow);
3945 expected_bit = OVS_KEY_ATTR_SCTP;
3947 } else if (src_flow->nw_proto == IPPROTO_ICMP
3948 && src_flow->dl_type == htons(ETH_TYPE_IP)
3949 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3951 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3953 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3954 const struct ovs_key_icmp *icmp_key;
3956 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3957 flow->tp_src = htons(icmp_key->icmp_type);
3958 flow->tp_dst = htons(icmp_key->icmp_code);
3959 expected_bit = OVS_KEY_ATTR_ICMP;
3961 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3962 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3963 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3965 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3967 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3968 const struct ovs_key_icmpv6 *icmpv6_key;
3970 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3971 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3972 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3973 expected_bit = OVS_KEY_ATTR_ICMPV6;
3974 if (src_flow->tp_dst == htons(0) &&
3975 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3976 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3978 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3980 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3981 const struct ovs_key_nd *nd_key;
3983 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3984 memcpy(&flow->nd_target, nd_key->nd_target,
3985 sizeof flow->nd_target);
3986 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3987 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3989 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3990 (flow->tp_src != htons(0xffff) ||
3991 flow->tp_dst != htons(0xffff))) {
3992 return ODP_FIT_ERROR;
3994 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4001 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4002 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4003 return ODP_FIT_ERROR;
4005 expected_attrs |= UINT64_C(1) << expected_bit;
4010 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4014 /* Parse 802.1Q header then encapsulated L3 attributes. */
4015 static enum odp_key_fitness
4016 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4017 uint64_t present_attrs, int out_of_range_attr,
4018 uint64_t expected_attrs, struct flow *flow,
4019 const struct nlattr *key, size_t key_len,
4020 const struct flow *src_flow)
4022 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4023 bool is_mask = src_flow != flow;
4025 const struct nlattr *encap
4026 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4027 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4028 enum odp_key_fitness encap_fitness;
4029 enum odp_key_fitness fitness;
4031 /* Calculate fitness of outer attributes. */
4033 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4034 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4036 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4037 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4039 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4040 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4043 fitness = check_expectations(present_attrs, out_of_range_attr,
4044 expected_attrs, key, key_len);
4047 * Remove the TPID from dl_type since it's not the real Ethertype. */
4048 flow->dl_type = htons(0);
4049 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4050 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4053 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4054 return ODP_FIT_TOO_LITTLE;
4055 } else if (flow->vlan_tci == htons(0)) {
4056 /* Corner case for a truncated 802.1Q header. */
4057 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4058 return ODP_FIT_TOO_MUCH;
4061 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4062 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4063 "but CFI bit is not set", ntohs(flow->vlan_tci));
4064 return ODP_FIT_ERROR;
4067 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4072 /* Now parse the encapsulated attributes. */
4073 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4074 attrs, &present_attrs, &out_of_range_attr)) {
4075 return ODP_FIT_ERROR;
4079 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4080 return ODP_FIT_ERROR;
4082 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4083 expected_attrs, flow, key, key_len,
4086 /* The overall fitness is the worse of the outer and inner attributes. */
4087 return MAX(fitness, encap_fitness);
4090 static enum odp_key_fitness
4091 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4092 const struct nlattr *src_key, size_t src_key_len,
4093 struct flow *flow, const struct flow *src_flow)
4095 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4096 uint64_t expected_attrs;
4097 uint64_t present_attrs;
4098 int out_of_range_attr;
4099 bool is_mask = src_flow != flow;
4101 memset(flow, 0, sizeof *flow);
4103 /* Parse attributes. */
4104 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4105 &out_of_range_attr)) {
4106 return ODP_FIT_ERROR;
4111 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4112 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4113 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4114 } else if (is_mask) {
4115 /* Always exact match recirc_id if it is not specified. */
4116 flow->recirc_id = UINT32_MAX;
4119 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4120 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4121 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4123 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4124 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4125 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4128 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4129 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4130 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4133 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4134 enum odp_key_fitness res;
4136 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], src_key,
4137 src_key_len, &src_flow->tunnel,
4139 if (res == ODP_FIT_ERROR) {
4140 return ODP_FIT_ERROR;
4141 } else if (res == ODP_FIT_PERFECT) {
4142 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4146 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4147 flow->in_port.odp_port
4148 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4149 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4150 } else if (!is_mask) {
4151 flow->in_port.odp_port = ODPP_NONE;
4154 /* Ethernet header. */
4155 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4156 const struct ovs_key_ethernet *eth_key;
4158 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4159 put_ethernet_key(eth_key, flow);
4161 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4165 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4168 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4169 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4171 return ODP_FIT_ERROR;
4175 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4176 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4177 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4178 expected_attrs, flow, key, key_len, src_flow);
4181 flow->vlan_tci = htons(0xffff);
4182 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4183 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4184 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4187 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4188 expected_attrs, flow, key, key_len, src_flow);
4191 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4192 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4193 * 'key' fits our expectations for what a flow key should contain.
4195 * The 'in_port' will be the datapath's understanding of the port. The
4196 * caller will need to translate with odp_port_to_ofp_port() if the
4197 * OpenFlow port is needed.
4199 * This function doesn't take the packet itself as an argument because none of
4200 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4201 * it is always possible to infer which additional attribute(s) should appear
4202 * by looking at the attributes for lower-level protocols, e.g. if the network
4203 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4204 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4205 * must be absent. */
4206 enum odp_key_fitness
4207 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4210 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow);
4213 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4214 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
4215 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4216 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
4217 * well 'key' fits our expectations for what a flow key should contain. */
4218 enum odp_key_fitness
4219 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4220 const struct nlattr *flow_key, size_t flow_key_len,
4221 struct flow *mask, const struct flow *flow)
4223 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4227 /* Returns 'fitness' as a string, for use in debug messages. */
4229 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4232 case ODP_FIT_PERFECT:
4234 case ODP_FIT_TOO_MUCH:
4236 case ODP_FIT_TOO_LITTLE:
4237 return "too_little";
4245 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4246 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4247 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4248 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4249 * null, then the return value is not meaningful.) */
4251 odp_put_userspace_action(uint32_t pid,
4252 const void *userdata, size_t userdata_size,
4253 odp_port_t tunnel_out_port,
4254 struct ofpbuf *odp_actions)
4256 size_t userdata_ofs;
4259 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4260 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4262 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4264 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4265 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4268 * - The kernel rejected shorter userdata with -ERANGE.
4270 * - The kernel silently dropped userdata beyond the first 8 bytes.
4272 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4273 * separately disable features that required more than 8 bytes.) */
4274 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4275 MAX(8, userdata_size)),
4276 userdata, userdata_size);
4280 if (tunnel_out_port != ODPP_NONE) {
4281 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4284 nl_msg_end_nested(odp_actions, offset);
4286 return userdata_ofs;
4290 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4291 struct ofpbuf *odp_actions)
4293 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4294 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4295 nl_msg_end_nested(odp_actions, offset);
4299 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4300 struct ovs_action_push_tnl *data)
4302 int size = offsetof(struct ovs_action_push_tnl, header);
4304 size += data->header_len;
4305 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4309 /* The commit_odp_actions() function and its helpers. */
4312 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4313 const void *key, size_t key_size)
4315 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4316 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4317 nl_msg_end_nested(odp_actions, offset);
4320 /* Masked set actions have a mask following the data within the netlink
4321 * attribute. The unmasked bits in the data will be cleared as the data
4322 * is copied to the action. */
4324 commit_masked_set_action(struct ofpbuf *odp_actions,
4325 enum ovs_key_attr key_type,
4326 const void *key_, const void *mask_, size_t key_size)
4328 size_t offset = nl_msg_start_nested(odp_actions,
4329 OVS_ACTION_ATTR_SET_MASKED);
4330 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4331 const char *key = key_, *mask = mask_;
4333 memcpy(data + key_size, mask, key_size);
4334 /* Clear unmasked bits while copying. */
4335 while (key_size--) {
4336 *data++ = *key++ & *mask++;
4338 nl_msg_end_nested(odp_actions, offset);
4341 /* If any of the flow key data that ODP actions can modify are different in
4342 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4343 * 'odp_actions' that change the flow tunneling information in key from
4344 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4345 * same way. In other words, operates the same as commit_odp_actions(), but
4346 * only on tunneling information. */
4348 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4349 struct ofpbuf *odp_actions)
4351 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4352 if (flow->tunnel.ip_dst) {
4353 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4356 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4357 odp_put_tunnel_action(&base->tunnel, odp_actions);
4362 commit(enum ovs_key_attr attr, bool use_masked_set,
4363 const void *key, void *base, void *mask, size_t size,
4364 struct ofpbuf *odp_actions)
4366 if (memcmp(key, base, size)) {
4367 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4369 if (use_masked_set && !fully_masked) {
4370 commit_masked_set_action(odp_actions, attr, key, mask, size);
4372 if (!fully_masked) {
4373 memset(mask, 0xff, size);
4375 commit_set_action(odp_actions, attr, key, size);
4377 memcpy(base, key, size);
4380 /* Mask bits are set when we have either read or set the corresponding
4381 * values. Masked bits will be exact-matched, no need to set them
4382 * if the value did not actually change. */
4388 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4390 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4391 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4395 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4397 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4398 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4402 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4403 struct ofpbuf *odp_actions,
4404 struct flow_wildcards *wc,
4407 struct ovs_key_ethernet key, base, mask;
4409 get_ethernet_key(flow, &key);
4410 get_ethernet_key(base_flow, &base);
4411 get_ethernet_key(&wc->masks, &mask);
4413 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4414 &key, &base, &mask, sizeof key, odp_actions)) {
4415 put_ethernet_key(&base, base_flow);
4416 put_ethernet_key(&mask, &wc->masks);
4421 pop_vlan(struct flow *base,
4422 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4424 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4426 if (base->vlan_tci & htons(VLAN_CFI)) {
4427 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4433 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4434 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4436 if (base->vlan_tci == vlan_tci) {
4440 pop_vlan(base, odp_actions, wc);
4441 if (vlan_tci & htons(VLAN_CFI)) {
4442 struct ovs_action_push_vlan vlan;
4444 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4445 vlan.vlan_tci = vlan_tci;
4446 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4447 &vlan, sizeof vlan);
4449 base->vlan_tci = vlan_tci;
4452 /* Wildcarding already done at action translation time. */
4454 commit_mpls_action(const struct flow *flow, struct flow *base,
4455 struct ofpbuf *odp_actions)
4457 int base_n = flow_count_mpls_labels(base, NULL);
4458 int flow_n = flow_count_mpls_labels(flow, NULL);
4459 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4462 while (base_n > common_n) {
4463 if (base_n - 1 == common_n && flow_n > common_n) {
4464 /* If there is only one more LSE in base than there are common
4465 * between base and flow; and flow has at least one more LSE than
4466 * is common then the topmost LSE of base may be updated using
4468 struct ovs_key_mpls mpls_key;
4470 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4471 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4472 &mpls_key, sizeof mpls_key);
4473 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4476 /* Otherwise, if there more LSEs in base than are common between
4477 * base and flow then pop the topmost one. */
4481 /* If all the LSEs are to be popped and this is not the outermost
4482 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4483 * POP_MPLS action instead of flow->dl_type.
4485 * This is because the POP_MPLS action requires its ethertype
4486 * argument to be an MPLS ethernet type but in this case
4487 * flow->dl_type will be a non-MPLS ethernet type.
4489 * When the final POP_MPLS action occurs it use flow->dl_type and
4490 * the and the resulting packet will have the desired dl_type. */
4491 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4492 dl_type = htons(ETH_TYPE_MPLS);
4494 dl_type = flow->dl_type;
4496 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4497 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4503 /* If, after the above popping and setting, there are more LSEs in flow
4504 * than base then some LSEs need to be pushed. */
4505 while (base_n < flow_n) {
4506 struct ovs_action_push_mpls *mpls;
4508 mpls = nl_msg_put_unspec_zero(odp_actions,
4509 OVS_ACTION_ATTR_PUSH_MPLS,
4511 mpls->mpls_ethertype = flow->dl_type;
4512 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4513 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4514 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4520 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4522 ipv4->ipv4_src = flow->nw_src;
4523 ipv4->ipv4_dst = flow->nw_dst;
4524 ipv4->ipv4_proto = flow->nw_proto;
4525 ipv4->ipv4_tos = flow->nw_tos;
4526 ipv4->ipv4_ttl = flow->nw_ttl;
4527 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4531 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4533 flow->nw_src = ipv4->ipv4_src;
4534 flow->nw_dst = ipv4->ipv4_dst;
4535 flow->nw_proto = ipv4->ipv4_proto;
4536 flow->nw_tos = ipv4->ipv4_tos;
4537 flow->nw_ttl = ipv4->ipv4_ttl;
4538 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4542 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4543 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4546 struct ovs_key_ipv4 key, mask, base;
4548 /* Check that nw_proto and nw_frag remain unchanged. */
4549 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4550 flow->nw_frag == base_flow->nw_frag);
4552 get_ipv4_key(flow, &key, false);
4553 get_ipv4_key(base_flow, &base, false);
4554 get_ipv4_key(&wc->masks, &mask, true);
4555 mask.ipv4_proto = 0; /* Not writeable. */
4556 mask.ipv4_frag = 0; /* Not writable. */
4558 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4560 put_ipv4_key(&base, base_flow, false);
4561 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4562 put_ipv4_key(&mask, &wc->masks, true);
4568 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4570 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4571 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4572 ipv6->ipv6_label = flow->ipv6_label;
4573 ipv6->ipv6_proto = flow->nw_proto;
4574 ipv6->ipv6_tclass = flow->nw_tos;
4575 ipv6->ipv6_hlimit = flow->nw_ttl;
4576 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4580 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4582 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4583 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4584 flow->ipv6_label = ipv6->ipv6_label;
4585 flow->nw_proto = ipv6->ipv6_proto;
4586 flow->nw_tos = ipv6->ipv6_tclass;
4587 flow->nw_ttl = ipv6->ipv6_hlimit;
4588 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4592 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4593 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4596 struct ovs_key_ipv6 key, mask, base;
4598 /* Check that nw_proto and nw_frag remain unchanged. */
4599 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4600 flow->nw_frag == base_flow->nw_frag);
4602 get_ipv6_key(flow, &key, false);
4603 get_ipv6_key(base_flow, &base, false);
4604 get_ipv6_key(&wc->masks, &mask, true);
4605 mask.ipv6_proto = 0; /* Not writeable. */
4606 mask.ipv6_frag = 0; /* Not writable. */
4608 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4610 put_ipv6_key(&base, base_flow, false);
4611 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4612 put_ipv6_key(&mask, &wc->masks, true);
4618 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4620 /* ARP key has padding, clear it. */
4621 memset(arp, 0, sizeof *arp);
4623 arp->arp_sip = flow->nw_src;
4624 arp->arp_tip = flow->nw_dst;
4625 arp->arp_op = htons(flow->nw_proto);
4626 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4627 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4631 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4633 flow->nw_src = arp->arp_sip;
4634 flow->nw_dst = arp->arp_tip;
4635 flow->nw_proto = ntohs(arp->arp_op);
4636 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4637 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4640 static enum slow_path_reason
4641 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4642 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4644 struct ovs_key_arp key, mask, base;
4646 get_arp_key(flow, &key);
4647 get_arp_key(base_flow, &base);
4648 get_arp_key(&wc->masks, &mask);
4650 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4652 put_arp_key(&base, base_flow);
4653 put_arp_key(&mask, &wc->masks);
4660 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4662 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4663 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4664 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4665 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4669 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4671 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4672 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4673 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4674 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4677 static enum slow_path_reason
4678 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4679 struct ofpbuf *odp_actions,
4680 struct flow_wildcards *wc, bool use_masked)
4682 struct ovs_key_nd key, mask, base;
4684 get_nd_key(flow, &key);
4685 get_nd_key(base_flow, &base);
4686 get_nd_key(&wc->masks, &mask);
4688 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4690 put_nd_key(&base, base_flow);
4691 put_nd_key(&mask, &wc->masks);
4698 static enum slow_path_reason
4699 commit_set_nw_action(const struct flow *flow, struct flow *base,
4700 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4703 /* Check if 'flow' really has an L3 header. */
4704 if (!flow->nw_proto) {
4708 switch (ntohs(base->dl_type)) {
4710 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4714 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4715 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4718 return commit_set_arp_action(flow, base, odp_actions, wc);
4724 /* TCP, UDP, and SCTP keys have the same layout. */
4725 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4726 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4729 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4731 tp->tcp.tcp_src = flow->tp_src;
4732 tp->tcp.tcp_dst = flow->tp_dst;
4736 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4738 flow->tp_src = tp->tcp.tcp_src;
4739 flow->tp_dst = tp->tcp.tcp_dst;
4743 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4744 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4747 enum ovs_key_attr key_type;
4748 union ovs_key_tp key, mask, base;
4750 /* Check if 'flow' really has an L3 header. */
4751 if (!flow->nw_proto) {
4755 if (!is_ip_any(base_flow)) {
4759 if (flow->nw_proto == IPPROTO_TCP) {
4760 key_type = OVS_KEY_ATTR_TCP;
4761 } else if (flow->nw_proto == IPPROTO_UDP) {
4762 key_type = OVS_KEY_ATTR_UDP;
4763 } else if (flow->nw_proto == IPPROTO_SCTP) {
4764 key_type = OVS_KEY_ATTR_SCTP;
4769 get_tp_key(flow, &key);
4770 get_tp_key(base_flow, &base);
4771 get_tp_key(&wc->masks, &mask);
4773 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4775 put_tp_key(&base, base_flow);
4776 put_tp_key(&mask, &wc->masks);
4781 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4782 struct ofpbuf *odp_actions,
4783 struct flow_wildcards *wc,
4786 uint32_t key, mask, base;
4788 key = flow->skb_priority;
4789 base = base_flow->skb_priority;
4790 mask = wc->masks.skb_priority;
4792 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4793 sizeof key, odp_actions)) {
4794 base_flow->skb_priority = base;
4795 wc->masks.skb_priority = mask;
4800 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4801 struct ofpbuf *odp_actions,
4802 struct flow_wildcards *wc,
4805 uint32_t key, mask, base;
4807 key = flow->pkt_mark;
4808 base = base_flow->pkt_mark;
4809 mask = wc->masks.pkt_mark;
4811 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4812 sizeof key, odp_actions)) {
4813 base_flow->pkt_mark = base;
4814 wc->masks.pkt_mark = mask;
4818 /* If any of the flow key data that ODP actions can modify are different in
4819 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4820 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4821 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4822 * in addition to this function if needed. Sets fields in 'wc' that are
4823 * used as part of the action.
4825 * Returns a reason to force processing the flow's packets into the userspace
4826 * slow path, if there is one, otherwise 0. */
4827 enum slow_path_reason
4828 commit_odp_actions(const struct flow *flow, struct flow *base,
4829 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4832 enum slow_path_reason slow;
4834 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4835 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4836 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4837 commit_mpls_action(flow, base, odp_actions);
4838 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4839 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4840 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);