2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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,
254 [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC,
257 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
258 const struct nlattr *userdata_attr;
259 const struct nlattr *tunnel_out_port_attr;
261 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
262 ds_put_cstr(ds, "userspace(error)");
266 ds_put_format(ds, "userspace(pid=%"PRIu32,
267 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
269 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
272 const uint8_t *userdata = nl_attr_get(userdata_attr);
273 size_t userdata_len = nl_attr_get_size(userdata_attr);
274 bool userdata_unspec = true;
275 union user_action_cookie cookie;
277 if (userdata_len >= sizeof cookie.type
278 && userdata_len <= sizeof cookie) {
280 memset(&cookie, 0, sizeof cookie);
281 memcpy(&cookie, userdata, userdata_len);
283 userdata_unspec = false;
285 if (userdata_len == sizeof cookie.sflow
286 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
287 ds_put_format(ds, ",sFlow("
288 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
289 vlan_tci_to_vid(cookie.sflow.vlan_tci),
290 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
291 cookie.sflow.output);
292 } else if (userdata_len == sizeof cookie.slow_path
293 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
294 ds_put_cstr(ds, ",slow_path(");
295 format_flags(ds, slow_path_reason_to_string,
296 cookie.slow_path.reason, ',');
297 ds_put_format(ds, ")");
298 } else if (userdata_len == sizeof cookie.flow_sample
299 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
300 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
301 ",collector_set_id=%"PRIu32
302 ",obs_domain_id=%"PRIu32
303 ",obs_point_id=%"PRIu32")",
304 cookie.flow_sample.probability,
305 cookie.flow_sample.collector_set_id,
306 cookie.flow_sample.obs_domain_id,
307 cookie.flow_sample.obs_point_id);
308 } else if (userdata_len >= sizeof cookie.ipfix
309 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
310 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
311 cookie.ipfix.output_odp_port);
313 userdata_unspec = true;
317 if (userdata_unspec) {
319 ds_put_format(ds, ",userdata(");
320 for (i = 0; i < userdata_len; i++) {
321 ds_put_format(ds, "%02x", userdata[i]);
323 ds_put_char(ds, ')');
327 if (a[OVS_USERSPACE_ATTR_ACTIONS]) {
328 ds_put_cstr(ds, ",actions");
331 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
332 if (tunnel_out_port_attr) {
333 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
334 nl_attr_get_u32(tunnel_out_port_attr));
337 ds_put_char(ds, ')');
341 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
343 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
344 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
345 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
346 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
348 ds_put_char(ds, ',');
350 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
351 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
352 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
353 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
355 ds_put_char(ds, ',');
357 if (!(tci & htons(VLAN_CFI))) {
358 ds_put_cstr(ds, "cfi=0");
359 ds_put_char(ds, ',');
365 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
367 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
368 mpls_lse_to_label(mpls_lse),
369 mpls_lse_to_tc(mpls_lse),
370 mpls_lse_to_ttl(mpls_lse),
371 mpls_lse_to_bos(mpls_lse));
375 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
376 const struct ovs_key_mpls *mpls_mask, int n)
379 ovs_be32 key = mpls_key->mpls_lse;
381 if (mpls_mask == NULL) {
382 format_mpls_lse(ds, key);
384 ovs_be32 mask = mpls_mask->mpls_lse;
386 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
387 mpls_lse_to_label(key), mpls_lse_to_label(mask),
388 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
389 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
390 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
395 for (i = 0; i < n; i++) {
396 ds_put_format(ds, "lse%d=%#"PRIx32,
397 i, ntohl(mpls_key[i].mpls_lse));
399 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
401 ds_put_char(ds, ',');
408 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
410 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
414 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
416 ds_put_format(ds, "hash(");
418 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
419 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
421 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
424 ds_put_format(ds, ")");
428 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
430 const struct udp_header *udp;
432 udp = (const struct udp_header *) (ip + 1);
433 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
434 ntohs(udp->udp_src), ntohs(udp->udp_dst),
435 ntohs(udp->udp_csum));
441 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
443 const struct eth_header *eth;
444 const struct ip_header *ip;
447 eth = (const struct eth_header *)data->header;
450 ip = (const struct ip_header *)l3;
453 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
454 data->header_len, data->tnl_type);
455 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
456 ds_put_format(ds, ",src=");
457 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
458 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
461 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
462 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
463 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
464 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
465 ip->ip_proto, ip->ip_tos,
469 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
470 const struct vxlanhdr *vxh;
472 vxh = format_udp_tnl_push_header(ds, ip);
474 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
475 ntohl(get_16aligned_be32(&vxh->vx_flags)),
476 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
477 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
478 const struct genevehdr *gnh;
480 gnh = format_udp_tnl_push_header(ds, ip);
482 ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
483 gnh->oam ? "oam," : "",
484 gnh->critical ? "crit," : "",
485 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
488 ds_put_cstr(ds, ",options(");
489 format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
491 ds_put_char(ds, ')');
494 ds_put_char(ds, ')');
495 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
496 const struct gre_base_hdr *greh;
497 ovs_16aligned_be32 *options;
500 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
501 greh = (const struct gre_base_hdr *) l4;
503 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
504 ntohs(greh->flags), ntohs(greh->protocol));
505 options = (ovs_16aligned_be32 *)(greh + 1);
506 if (greh->flags & htons(GRE_CSUM)) {
507 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
510 if (greh->flags & htons(GRE_KEY)) {
511 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
514 if (greh->flags & htons(GRE_SEQ)) {
515 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
518 ds_put_format(ds, ")");
520 ds_put_format(ds, ")");
524 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
526 struct ovs_action_push_tnl *data;
528 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
530 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
531 format_odp_tnl_push_header(ds, data);
532 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
536 format_odp_action(struct ds *ds, const struct nlattr *a)
539 enum ovs_action_attr type = nl_attr_type(a);
540 const struct ovs_action_push_vlan *vlan;
543 expected_len = odp_action_len(nl_attr_type(a));
544 if (expected_len != ATTR_LEN_VARIABLE &&
545 nl_attr_get_size(a) != expected_len) {
546 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
547 nl_attr_get_size(a), expected_len);
548 format_generic_odp_action(ds, a);
553 case OVS_ACTION_ATTR_OUTPUT:
554 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
556 case OVS_ACTION_ATTR_TUNNEL_POP:
557 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
559 case OVS_ACTION_ATTR_TUNNEL_PUSH:
560 format_odp_tnl_push_action(ds, a);
562 case OVS_ACTION_ATTR_USERSPACE:
563 format_odp_userspace_action(ds, a);
565 case OVS_ACTION_ATTR_RECIRC:
566 format_odp_recirc_action(ds, nl_attr_get_u32(a));
568 case OVS_ACTION_ATTR_HASH:
569 format_odp_hash_action(ds, nl_attr_get(a));
571 case OVS_ACTION_ATTR_SET_MASKED:
573 size = nl_attr_get_size(a) / 2;
574 ds_put_cstr(ds, "set(");
576 /* Masked set action not supported for tunnel key, which is bigger. */
577 if (size <= sizeof(struct ovs_key_ipv6)) {
578 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
579 sizeof(struct nlattr))];
580 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
581 sizeof(struct nlattr))];
583 mask->nla_type = attr->nla_type = nl_attr_type(a);
584 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
585 memcpy(attr + 1, (char *)(a + 1), size);
586 memcpy(mask + 1, (char *)(a + 1) + size, size);
587 format_odp_key_attr(attr, mask, NULL, ds, false);
589 format_odp_key_attr(a, NULL, NULL, ds, false);
591 ds_put_cstr(ds, ")");
593 case OVS_ACTION_ATTR_SET:
594 ds_put_cstr(ds, "set(");
595 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
596 ds_put_cstr(ds, ")");
598 case OVS_ACTION_ATTR_PUSH_VLAN:
599 vlan = nl_attr_get(a);
600 ds_put_cstr(ds, "push_vlan(");
601 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
602 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
604 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
605 ds_put_char(ds, ')');
607 case OVS_ACTION_ATTR_POP_VLAN:
608 ds_put_cstr(ds, "pop_vlan");
610 case OVS_ACTION_ATTR_PUSH_MPLS: {
611 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
612 ds_put_cstr(ds, "push_mpls(");
613 format_mpls_lse(ds, mpls->mpls_lse);
614 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
617 case OVS_ACTION_ATTR_POP_MPLS: {
618 ovs_be16 ethertype = nl_attr_get_be16(a);
619 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
622 case OVS_ACTION_ATTR_SAMPLE:
623 format_odp_sample_action(ds, a);
625 case OVS_ACTION_ATTR_UNSPEC:
626 case __OVS_ACTION_ATTR_MAX:
628 format_generic_odp_action(ds, a);
634 format_odp_actions(struct ds *ds, const struct nlattr *actions,
638 const struct nlattr *a;
641 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
643 ds_put_char(ds, ',');
645 format_odp_action(ds, a);
650 if (left == actions_len) {
651 ds_put_cstr(ds, "<empty>");
653 ds_put_format(ds, ",***%u leftover bytes*** (", left);
654 for (i = 0; i < left; i++) {
655 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
657 ds_put_char(ds, ')');
660 ds_put_cstr(ds, "drop");
664 /* Separate out parse_odp_userspace_action() function. */
666 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
669 union user_action_cookie cookie;
671 odp_port_t tunnel_out_port;
673 void *user_data = NULL;
674 size_t user_data_size = 0;
675 bool include_actions = false;
677 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
683 uint32_t probability;
684 uint32_t collector_set_id;
685 uint32_t obs_domain_id;
686 uint32_t obs_point_id;
689 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
690 "pcp=%i,output=%"SCNi32")%n",
691 &vid, &pcp, &output, &n1)) {
695 tci = vid | (pcp << VLAN_PCP_SHIFT);
700 cookie.type = USER_ACTION_COOKIE_SFLOW;
701 cookie.sflow.vlan_tci = htons(tci);
702 cookie.sflow.output = output;
704 user_data_size = sizeof cookie.sflow;
705 } else if (ovs_scan(&s[n], ",slow_path(%n",
710 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
711 cookie.slow_path.unused = 0;
712 cookie.slow_path.reason = 0;
714 res = parse_odp_flags(&s[n], slow_path_reason_to_string,
715 &cookie.slow_path.reason,
716 SLOW_PATH_REASON_MASK, NULL);
717 if (res < 0 || s[n + res] != ')') {
723 user_data_size = sizeof cookie.slow_path;
724 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
725 "collector_set_id=%"SCNi32","
726 "obs_domain_id=%"SCNi32","
727 "obs_point_id=%"SCNi32")%n",
728 &probability, &collector_set_id,
729 &obs_domain_id, &obs_point_id, &n1)) {
732 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
733 cookie.flow_sample.probability = probability;
734 cookie.flow_sample.collector_set_id = collector_set_id;
735 cookie.flow_sample.obs_domain_id = obs_domain_id;
736 cookie.flow_sample.obs_point_id = obs_point_id;
738 user_data_size = sizeof cookie.flow_sample;
739 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
742 cookie.type = USER_ACTION_COOKIE_IPFIX;
743 cookie.ipfix.output_odp_port = u32_to_odp(output);
745 user_data_size = sizeof cookie.ipfix;
746 } else if (ovs_scan(&s[n], ",userdata(%n",
751 ofpbuf_init(&buf, 16);
752 end = ofpbuf_put_hex(&buf, &s[n], NULL);
756 user_data = buf.data;
757 user_data_size = buf.size;
764 if (ovs_scan(&s[n], ",actions%n", &n1)) {
766 include_actions = true;
772 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
773 &tunnel_out_port, &n1)) {
774 odp_put_userspace_action(pid, user_data, user_data_size,
775 tunnel_out_port, include_actions, actions);
777 } else if (s[n] == ')') {
778 odp_put_userspace_action(pid, user_data, user_data_size,
779 ODPP_NONE, include_actions, actions);
788 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
790 struct eth_header *eth;
791 struct ip_header *ip;
792 struct udp_header *udp;
793 struct gre_base_hdr *greh;
794 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
796 uint32_t tnl_type = 0, header_len = 0;
800 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
803 eth = (struct eth_header *) data->header;
804 l3 = (data->header + sizeof *eth);
805 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
806 ip = (struct ip_header *) l3;
807 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
808 "eth(dst="ETH_ADDR_SCAN_FMT",",
811 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
815 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
816 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
819 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
822 eth->eth_type = htons(dl_type);
825 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
826 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
829 &ip->ip_proto, &ip->ip_tos,
830 &ip->ip_ttl, &ip->ip_frag_off)) {
833 put_16aligned_be32(&ip->ip_src, sip);
834 put_16aligned_be32(&ip->ip_dst, dip);
837 udp = (struct udp_header *) l4;
838 greh = (struct gre_base_hdr *) l4;
839 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
840 &udp_src, &udp_dst, &csum)) {
841 uint32_t vx_flags, vni;
843 udp->udp_src = htons(udp_src);
844 udp->udp_dst = htons(udp_dst);
846 udp->udp_csum = htons(csum);
848 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
850 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
852 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
853 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
854 tnl_type = OVS_VPORT_TYPE_VXLAN;
855 header_len = sizeof *eth + sizeof *ip +
856 sizeof *udp + sizeof *vxh;
857 } else if (ovs_scan_len(s, &n, "geneve(")) {
858 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
860 memset(gnh, 0, sizeof *gnh);
861 header_len = sizeof *eth + sizeof *ip +
862 sizeof *udp + sizeof *gnh;
864 if (ovs_scan_len(s, &n, "oam,")) {
867 if (ovs_scan_len(s, &n, "crit,")) {
870 if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
873 if (ovs_scan_len(s, &n, ",options(")) {
874 struct geneve_scan options;
877 memset(&options, 0, sizeof options);
878 len = scan_geneve(s + n, &options, NULL);
883 memcpy(gnh->options, options.d, options.len);
884 gnh->opt_len = options.len / 4;
885 header_len += options.len;
889 if (!ovs_scan_len(s, &n, "))")) {
893 gnh->proto_type = htons(ETH_TYPE_TEB);
894 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
895 tnl_type = OVS_VPORT_TYPE_GENEVE;
899 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
900 &gre_flags, &gre_proto)){
902 tnl_type = OVS_VPORT_TYPE_GRE;
903 greh->flags = htons(gre_flags);
904 greh->protocol = htons(gre_proto);
905 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
907 if (greh->flags & htons(GRE_CSUM)) {
908 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
912 memset(options, 0, sizeof *options);
913 *((ovs_be16 *)options) = htons(csum);
916 if (greh->flags & htons(GRE_KEY)) {
919 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
923 put_16aligned_be32(options, htonl(key));
926 if (greh->flags & htons(GRE_SEQ)) {
929 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
932 put_16aligned_be32(options, htonl(seq));
936 if (!ovs_scan_len(s, &n, "))")) {
940 header_len = sizeof *eth + sizeof *ip +
941 ((uint8_t *) options - (uint8_t *) greh);
946 /* check tunnel meta data. */
947 if (data->tnl_type != tnl_type) {
950 if (data->header_len != header_len) {
955 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
963 parse_odp_action(const char *s, const struct simap *port_names,
964 struct ofpbuf *actions)
970 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
971 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
977 int len = strcspn(s, delimiters);
978 struct simap_node *node;
980 node = simap_find_len(port_names, s, len);
982 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
991 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
992 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
997 if (!strncmp(s, "userspace(", 10)) {
998 return parse_odp_userspace_action(s, actions);
1001 if (!strncmp(s, "set(", 4)) {
1004 struct nlattr mask[128 / sizeof(struct nlattr)];
1005 struct ofpbuf maskbuf;
1006 struct nlattr *nested, *key;
1009 /* 'mask' is big enough to hold any key. */
1010 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1012 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1013 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1017 if (s[retval + 4] != ')') {
1021 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1024 size = nl_attr_get_size(mask);
1025 if (size == nl_attr_get_size(key)) {
1026 /* Change to masked set action if not fully masked. */
1027 if (!is_all_ones(mask + 1, size)) {
1028 key->nla_len += size;
1029 ofpbuf_put(actions, mask + 1, size);
1030 /* 'actions' may have been reallocated by ofpbuf_put(). */
1031 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1032 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1036 nl_msg_end_nested(actions, start_ofs);
1041 struct ovs_action_push_vlan push;
1042 int tpid = ETH_TYPE_VLAN;
1047 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1048 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1049 &vid, &pcp, &cfi, &n)
1050 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1051 &tpid, &vid, &pcp, &n)
1052 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1053 &tpid, &vid, &pcp, &cfi, &n)) {
1054 push.vlan_tpid = htons(tpid);
1055 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1056 | (pcp << VLAN_PCP_SHIFT)
1057 | (cfi ? VLAN_CFI : 0));
1058 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1059 &push, sizeof push);
1065 if (!strncmp(s, "pop_vlan", 8)) {
1066 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1074 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1075 && percentage >= 0. && percentage <= 100.0) {
1076 size_t sample_ofs, actions_ofs;
1079 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1080 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1081 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1082 (probability <= 0 ? 0
1083 : probability >= UINT32_MAX ? UINT32_MAX
1086 actions_ofs = nl_msg_start_nested(actions,
1087 OVS_SAMPLE_ATTR_ACTIONS);
1091 n += strspn(s + n, delimiters);
1096 retval = parse_odp_action(s + n, port_names, actions);
1102 nl_msg_end_nested(actions, actions_ofs);
1103 nl_msg_end_nested(actions, sample_ofs);
1105 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1113 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1114 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1120 struct ovs_action_push_tnl data;
1123 n = ovs_parse_tnl_push(s, &data);
1125 odp_put_tnl_push_action(actions, &data);
1134 /* Parses the string representation of datapath actions, in the format output
1135 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1136 * value. On success, the ODP actions are appended to 'actions' as a series of
1137 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1138 * way, 'actions''s data might be reallocated. */
1140 odp_actions_from_string(const char *s, const struct simap *port_names,
1141 struct ofpbuf *actions)
1145 if (!strcasecmp(s, "drop")) {
1149 old_size = actions->size;
1153 s += strspn(s, delimiters);
1158 retval = parse_odp_action(s, port_names, actions);
1159 if (retval < 0 || !strchr(delimiters, s[retval])) {
1160 actions->size = old_size;
1169 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1170 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1173 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1174 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1175 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1176 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1177 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1178 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1179 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1180 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1181 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1182 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1183 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1184 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1185 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1186 .next = ovs_vxlan_ext_attr_lens ,
1187 .next_max = OVS_VXLAN_EXT_MAX},
1190 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1191 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1192 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1193 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1194 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1195 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1196 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1197 .next = ovs_tun_key_attr_lens,
1198 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1199 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1200 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1201 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1202 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1203 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1204 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1205 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1206 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1207 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1208 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1209 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1210 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1211 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1212 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1213 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1216 /* Returns the correct length of the payload for a flow key attribute of the
1217 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1218 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1219 * payload is a nested type. */
1221 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1223 if (type > max_len) {
1224 return ATTR_LEN_INVALID;
1227 return tbl[type].len;
1231 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1233 size_t len = nl_attr_get_size(a);
1235 const uint8_t *unspec;
1238 unspec = nl_attr_get(a);
1239 for (i = 0; i < len; i++) {
1241 ds_put_char(ds, ' ');
1243 ds_put_format(ds, "%02x", unspec[i]);
1249 ovs_frag_type_to_string(enum ovs_frag_type type)
1252 case OVS_FRAG_TYPE_NONE:
1254 case OVS_FRAG_TYPE_FIRST:
1256 case OVS_FRAG_TYPE_LATER:
1258 case __OVS_FRAG_TYPE_MAX:
1264 static enum odp_key_fitness
1265 odp_tun_key_from_attr__(const struct nlattr *attr,
1266 const struct nlattr *flow_attrs, size_t flow_attr_len,
1267 const struct flow_tnl *src_tun, struct flow_tnl *tun)
1270 const struct nlattr *a;
1272 bool unknown = false;
1274 NL_NESTED_FOR_EACH(a, left, attr) {
1275 uint16_t type = nl_attr_type(a);
1276 size_t len = nl_attr_get_size(a);
1277 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1278 OVS_TUNNEL_ATTR_MAX, type);
1280 if (len != expected_len && expected_len >= 0) {
1281 return ODP_FIT_ERROR;
1285 case OVS_TUNNEL_KEY_ATTR_ID:
1286 tun->tun_id = nl_attr_get_be64(a);
1287 tun->flags |= FLOW_TNL_F_KEY;
1289 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1290 tun->ip_src = nl_attr_get_be32(a);
1292 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1293 tun->ip_dst = nl_attr_get_be32(a);
1295 case OVS_TUNNEL_KEY_ATTR_TOS:
1296 tun->ip_tos = nl_attr_get_u8(a);
1298 case OVS_TUNNEL_KEY_ATTR_TTL:
1299 tun->ip_ttl = nl_attr_get_u8(a);
1302 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1303 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1305 case OVS_TUNNEL_KEY_ATTR_CSUM:
1306 tun->flags |= FLOW_TNL_F_CSUM;
1308 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1309 tun->tp_src = nl_attr_get_be16(a);
1311 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1312 tun->tp_dst = nl_attr_get_be16(a);
1314 case OVS_TUNNEL_KEY_ATTR_OAM:
1315 tun->flags |= FLOW_TNL_F_OAM;
1317 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1318 static const struct nl_policy vxlan_opts_policy[] = {
1319 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1321 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1323 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1324 return ODP_FIT_ERROR;
1327 if (ext[OVS_VXLAN_EXT_GBP]) {
1328 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1330 tun->gbp_id = htons(gbp & 0xFFFF);
1331 tun->gbp_flags = (gbp >> 16) & 0xFF;
1336 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1337 if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1340 return ODP_FIT_ERROR;
1345 /* Allow this to show up as unexpected, if there are unknown
1346 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1353 return ODP_FIT_ERROR;
1356 return ODP_FIT_TOO_MUCH;
1358 return ODP_FIT_PERFECT;
1361 enum odp_key_fitness
1362 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1364 memset(tun, 0, sizeof *tun);
1365 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun);
1369 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1370 const struct flow_tnl *tun_flow_key,
1371 const struct ofpbuf *key_buf)
1375 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1377 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1378 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1379 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1381 if (tun_key->ip_src) {
1382 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1384 if (tun_key->ip_dst) {
1385 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1387 if (tun_key->ip_tos) {
1388 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1390 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1391 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1392 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1394 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1395 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1397 if (tun_key->tp_src) {
1398 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1400 if (tun_key->tp_dst) {
1401 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1403 if (tun_key->flags & FLOW_TNL_F_OAM) {
1404 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1406 if (tun_key->gbp_flags || tun_key->gbp_id) {
1407 size_t vxlan_opts_ofs;
1409 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1410 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1411 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1412 nl_msg_end_nested(a, vxlan_opts_ofs);
1415 if (tun_key == tun_flow_key) {
1416 tun_metadata_to_geneve_nlattr_flow(&tun_key->metadata, a);
1418 tun_metadata_to_geneve_nlattr_mask(key_buf, &tun_key->metadata,
1419 &tun_flow_key->metadata, a);
1422 nl_msg_end_nested(a, tun_key_ofs);
1426 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1428 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1432 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1434 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1435 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1437 if (attr == OVS_KEY_ATTR_IPV6) {
1438 const struct ovs_key_ipv6 *ipv6_mask = mask;
1441 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1442 == htonl(IPV6_LABEL_MASK))
1443 && ipv6_mask->ipv6_proto == UINT8_MAX
1444 && ipv6_mask->ipv6_tclass == UINT8_MAX
1445 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1446 && ipv6_mask->ipv6_frag == UINT8_MAX
1447 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1448 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1450 if (attr == OVS_KEY_ATTR_TUNNEL) {
1454 if (attr == OVS_KEY_ATTR_ARP) {
1455 /* ARP key has padding, ignore it. */
1456 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1457 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1458 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1459 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1462 return is_all_ones(mask, size);
1466 odp_mask_attr_is_exact(const struct nlattr *ma)
1468 enum ovs_key_attr attr = nl_attr_type(ma);
1472 if (attr == OVS_KEY_ATTR_TUNNEL) {
1475 mask = nl_attr_get(ma);
1476 size = nl_attr_get_size(ma);
1479 return odp_mask_is_exact(attr, mask, size);
1483 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1486 struct odp_portno_names *odp_portno_names;
1488 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1489 odp_portno_names->port_no = port_no;
1490 odp_portno_names->name = xstrdup(port_name);
1491 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1492 hash_odp_port(port_no));
1496 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1498 struct odp_portno_names *odp_portno_names;
1500 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1501 hash_odp_port(port_no), portno_names) {
1502 if (odp_portno_names->port_no == port_no) {
1503 return odp_portno_names->name;
1510 odp_portno_names_destroy(struct hmap *portno_names)
1512 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1513 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1514 hmap_node, portno_names) {
1515 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1516 free(odp_portno_names->name);
1517 free(odp_portno_names);
1521 /* Format helpers. */
1524 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1525 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1527 bool mask_empty = mask && eth_addr_is_zero(*mask);
1529 if (verbose || !mask_empty) {
1530 bool mask_full = !mask || eth_mask_is_exact(*mask);
1533 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1535 ds_put_format(ds, "%s=", name);
1536 eth_format_masked(key, *mask, ds);
1537 ds_put_char(ds, ',');
1543 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1544 const ovs_be64 *mask, bool verbose)
1546 bool mask_empty = mask && !*mask;
1548 if (verbose || !mask_empty) {
1549 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1551 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1552 if (!mask_full) { /* Partially masked. */
1553 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1555 ds_put_char(ds, ',');
1560 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1561 const ovs_be32 *mask, bool verbose)
1563 bool mask_empty = mask && !*mask;
1565 if (verbose || !mask_empty) {
1566 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1568 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1569 if (!mask_full) { /* Partially masked. */
1570 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1572 ds_put_char(ds, ',');
1577 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1578 const ovs_be32 (*mask_)[4], bool verbose)
1580 char buf[INET6_ADDRSTRLEN];
1581 const struct in6_addr *key = (const struct in6_addr *)key_;
1582 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1584 bool mask_empty = mask && ipv6_mask_is_any(mask);
1586 if (verbose || !mask_empty) {
1587 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1589 inet_ntop(AF_INET6, key, buf, sizeof buf);
1590 ds_put_format(ds, "%s=%s", name, buf);
1591 if (!mask_full) { /* Partially masked. */
1592 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1593 ds_put_format(ds, "/%s", buf);
1595 ds_put_char(ds, ',');
1600 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1601 const ovs_be32 *mask, bool verbose)
1603 bool mask_empty = mask && !*mask;
1605 if (verbose || !mask_empty) {
1606 bool mask_full = !mask
1607 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1609 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1610 if (!mask_full) { /* Partially masked. */
1611 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1613 ds_put_char(ds, ',');
1618 format_u8x(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=%#"PRIx8, name, key);
1627 if (!mask_full) { /* Partially masked. */
1628 ds_put_format(ds, "/%#"PRIx8, *mask);
1630 ds_put_char(ds, ',');
1635 format_u8u(struct ds *ds, const char *name, uint8_t key,
1636 const uint8_t *mask, bool verbose)
1638 bool mask_empty = mask && !*mask;
1640 if (verbose || !mask_empty) {
1641 bool mask_full = !mask || *mask == UINT8_MAX;
1643 ds_put_format(ds, "%s=%"PRIu8, name, key);
1644 if (!mask_full) { /* Partially masked. */
1645 ds_put_format(ds, "/%#"PRIx8, *mask);
1647 ds_put_char(ds, ',');
1652 format_be16(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=%"PRIu16, name, ntohs(key));
1661 if (!mask_full) { /* Partially masked. */
1662 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1664 ds_put_char(ds, ',');
1669 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1670 const ovs_be16 *mask, bool verbose)
1672 bool mask_empty = mask && !*mask;
1674 if (verbose || !mask_empty) {
1675 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1677 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1678 if (!mask_full) { /* Partially masked. */
1679 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1681 ds_put_char(ds, ',');
1686 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1687 const uint16_t *mask, bool verbose)
1689 bool mask_empty = mask && !*mask;
1691 if (verbose || !mask_empty) {
1692 ds_put_cstr(ds, name);
1693 ds_put_char(ds, '(');
1695 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
1696 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
1697 } else { /* Fully masked. */
1698 format_flags(ds, flow_tun_flag_to_string, key, '|');
1700 ds_put_cstr(ds, "),");
1705 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1706 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1710 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1711 if (expected_len != ATTR_LEN_VARIABLE &&
1712 expected_len != ATTR_LEN_NESTED) {
1714 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1715 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1717 if (bad_key_len || bad_mask_len) {
1719 ds_put_format(ds, "key%u", nl_attr_type(a));
1722 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1723 nl_attr_get_size(a), expected_len);
1725 format_generic_odp_key(a, ds);
1727 ds_put_char(ds, '/');
1729 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1730 nl_attr_get_size(ma), expected_len);
1732 format_generic_odp_key(ma, ds);
1734 ds_put_char(ds, ')');
1743 format_unknown_key(struct ds *ds, const struct nlattr *a,
1744 const struct nlattr *ma)
1746 ds_put_format(ds, "key%u(", nl_attr_type(a));
1747 format_generic_odp_key(a, ds);
1748 if (ma && !odp_mask_attr_is_exact(ma)) {
1749 ds_put_char(ds, '/');
1750 format_generic_odp_key(ma, ds);
1752 ds_put_cstr(ds, "),");
1756 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1757 const struct nlattr *mask_attr, struct ds *ds,
1761 const struct nlattr *a;
1764 ofpbuf_init(&ofp, 100);
1765 NL_NESTED_FOR_EACH(a, left, attr) {
1766 uint16_t type = nl_attr_type(a);
1767 const struct nlattr *ma = NULL;
1770 ma = nl_attr_find__(nl_attr_get(mask_attr),
1771 nl_attr_get_size(mask_attr), type);
1773 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1779 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1780 OVS_VXLAN_EXT_MAX, true)) {
1785 case OVS_VXLAN_EXT_GBP: {
1786 uint32_t key = nl_attr_get_u32(a);
1787 ovs_be16 id, id_mask;
1788 uint8_t flags, flags_mask;
1790 id = htons(key & 0xFFFF);
1791 flags = (key >> 16) & 0xFF;
1793 uint32_t mask = nl_attr_get_u32(ma);
1794 id_mask = htons(mask & 0xFFFF);
1795 flags_mask = (mask >> 16) & 0xFF;
1798 ds_put_cstr(ds, "gbp(");
1799 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1800 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1802 ds_put_cstr(ds, "),");
1807 format_unknown_key(ds, a, ma);
1813 ofpbuf_uninit(&ofp);
1816 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1819 format_geneve_opts(const struct geneve_opt *opt,
1820 const struct geneve_opt *mask, int opts_len,
1821 struct ds *ds, bool verbose)
1823 while (opts_len > 0) {
1825 uint8_t data_len, data_len_mask;
1827 if (opts_len < sizeof *opt) {
1828 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1829 opts_len, sizeof *opt);
1833 data_len = opt->length * 4;
1835 if (mask->length == 0x1f) {
1836 data_len_mask = UINT8_MAX;
1838 data_len_mask = mask->length;
1841 len = sizeof *opt + data_len;
1842 if (len > opts_len) {
1843 ds_put_format(ds, "opt len %u greater than remaining %u",
1848 ds_put_char(ds, '{');
1849 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1851 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1852 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1853 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1854 ds_put_hex(ds, opt + 1, data_len);
1855 if (mask && !is_all_ones(mask + 1, data_len)) {
1856 ds_put_char(ds, '/');
1857 ds_put_hex(ds, mask + 1, data_len);
1862 ds_put_char(ds, '}');
1864 opt += len / sizeof(*opt);
1866 mask += len / sizeof(*opt);
1873 format_odp_tun_geneve(const struct nlattr *attr,
1874 const struct nlattr *mask_attr, struct ds *ds,
1877 int opts_len = nl_attr_get_size(attr);
1878 const struct geneve_opt *opt = nl_attr_get(attr);
1879 const struct geneve_opt *mask = mask_attr ?
1880 nl_attr_get(mask_attr) : NULL;
1882 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1883 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1884 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1888 format_geneve_opts(opt, mask, opts_len, ds, verbose);
1892 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1893 struct ds *ds, bool verbose)
1896 const struct nlattr *a;
1898 uint16_t mask_flags = 0;
1901 ofpbuf_init(&ofp, 100);
1902 NL_NESTED_FOR_EACH(a, left, attr) {
1903 enum ovs_tunnel_key_attr type = nl_attr_type(a);
1904 const struct nlattr *ma = NULL;
1907 ma = nl_attr_find__(nl_attr_get(mask_attr),
1908 nl_attr_get_size(mask_attr), type);
1910 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
1911 OVS_TUNNEL_KEY_ATTR_MAX,
1916 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
1917 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
1922 case OVS_TUNNEL_KEY_ATTR_ID:
1923 format_be64(ds, "tun_id", nl_attr_get_be64(a),
1924 ma ? nl_attr_get(ma) : NULL, verbose);
1925 flags |= FLOW_TNL_F_KEY;
1927 mask_flags |= FLOW_TNL_F_KEY;
1930 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1931 format_ipv4(ds, "src", nl_attr_get_be32(a),
1932 ma ? nl_attr_get(ma) : NULL, verbose);
1934 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1935 format_ipv4(ds, "dst", nl_attr_get_be32(a),
1936 ma ? nl_attr_get(ma) : NULL, verbose);
1938 case OVS_TUNNEL_KEY_ATTR_TOS:
1939 format_u8x(ds, "tos", nl_attr_get_u8(a),
1940 ma ? nl_attr_get(ma) : NULL, verbose);
1942 case OVS_TUNNEL_KEY_ATTR_TTL:
1943 format_u8u(ds, "ttl", nl_attr_get_u8(a),
1944 ma ? nl_attr_get(ma) : NULL, verbose);
1946 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1947 flags |= FLOW_TNL_F_DONT_FRAGMENT;
1949 case OVS_TUNNEL_KEY_ATTR_CSUM:
1950 flags |= FLOW_TNL_F_CSUM;
1952 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1953 format_be16(ds, "tp_src", nl_attr_get_be16(a),
1954 ma ? nl_attr_get(ma) : NULL, verbose);
1956 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1957 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
1958 ma ? nl_attr_get(ma) : NULL, verbose);
1960 case OVS_TUNNEL_KEY_ATTR_OAM:
1961 flags |= FLOW_TNL_F_OAM;
1963 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1964 ds_put_cstr(ds, "vxlan(");
1965 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
1966 ds_put_cstr(ds, "),");
1968 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1969 ds_put_cstr(ds, "geneve(");
1970 format_odp_tun_geneve(a, ma, ds, verbose);
1971 ds_put_cstr(ds, "),");
1973 case __OVS_TUNNEL_KEY_ATTR_MAX:
1975 format_unknown_key(ds, a, ma);
1980 /* Flags can have a valid mask even if the attribute is not set, so
1981 * we need to collect these separately. */
1983 NL_NESTED_FOR_EACH(a, left, mask_attr) {
1984 switch (nl_attr_type(a)) {
1985 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1986 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
1988 case OVS_TUNNEL_KEY_ATTR_CSUM:
1989 mask_flags |= FLOW_TNL_F_CSUM;
1991 case OVS_TUNNEL_KEY_ATTR_OAM:
1992 mask_flags |= FLOW_TNL_F_OAM;
1998 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2001 ofpbuf_uninit(&ofp);
2005 format_frag(struct ds *ds, const char *name, uint8_t key,
2006 const uint8_t *mask, bool verbose)
2008 bool mask_empty = mask && !*mask;
2010 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2011 if (verbose || !mask_empty) {
2012 bool mask_full = !mask || *mask == UINT8_MAX;
2014 if (!mask_full) { /* Partially masked. */
2015 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2018 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2024 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2025 const struct hmap *portno_names, struct ds *ds,
2028 enum ovs_key_attr attr = nl_attr_type(a);
2029 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2032 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2034 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2036 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2037 OVS_KEY_ATTR_MAX, false)) {
2041 ds_put_char(ds, '(');
2043 case OVS_KEY_ATTR_ENCAP:
2044 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2045 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2046 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2048 } else if (nl_attr_get_size(a)) {
2049 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2054 case OVS_KEY_ATTR_PRIORITY:
2055 case OVS_KEY_ATTR_SKB_MARK:
2056 case OVS_KEY_ATTR_DP_HASH:
2057 case OVS_KEY_ATTR_RECIRC_ID:
2058 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2060 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2064 case OVS_KEY_ATTR_TUNNEL:
2065 format_odp_tun_attr(a, ma, ds, verbose);
2068 case OVS_KEY_ATTR_IN_PORT:
2069 if (portno_names && verbose && is_exact) {
2070 char *name = odp_portno_names_get(portno_names,
2071 u32_to_odp(nl_attr_get_u32(a)));
2073 ds_put_format(ds, "%s", name);
2075 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2078 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2080 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2085 case OVS_KEY_ATTR_ETHERNET: {
2086 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2087 const struct ovs_key_ethernet *key = nl_attr_get(a);
2089 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2090 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2094 case OVS_KEY_ATTR_VLAN:
2095 format_vlan_tci(ds, nl_attr_get_be16(a),
2096 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2099 case OVS_KEY_ATTR_MPLS: {
2100 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2101 const struct ovs_key_mpls *mpls_mask = NULL;
2102 size_t size = nl_attr_get_size(a);
2104 if (!size || size % sizeof *mpls_key) {
2105 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2109 mpls_mask = nl_attr_get(ma);
2110 if (size != nl_attr_get_size(ma)) {
2111 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2112 "mask length %"PRIuSIZE")",
2113 size, nl_attr_get_size(ma));
2117 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2120 case OVS_KEY_ATTR_ETHERTYPE:
2121 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2123 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2127 case OVS_KEY_ATTR_IPV4: {
2128 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2129 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2131 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2132 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2133 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2135 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2136 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2137 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2142 case OVS_KEY_ATTR_IPV6: {
2143 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2144 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2146 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2147 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2148 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2150 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2152 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2154 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2156 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2161 /* These have the same structure and format. */
2162 case OVS_KEY_ATTR_TCP:
2163 case OVS_KEY_ATTR_UDP:
2164 case OVS_KEY_ATTR_SCTP: {
2165 const struct ovs_key_tcp *key = nl_attr_get(a);
2166 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2168 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2169 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2173 case OVS_KEY_ATTR_TCP_FLAGS:
2175 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2176 ntohs(nl_attr_get_be16(a)),
2177 TCP_FLAGS(nl_attr_get_be16(ma)),
2178 TCP_FLAGS(OVS_BE16_MAX));
2180 format_flags(ds, packet_tcp_flag_to_string,
2181 ntohs(nl_attr_get_be16(a)), '|');
2185 case OVS_KEY_ATTR_ICMP: {
2186 const struct ovs_key_icmp *key = nl_attr_get(a);
2187 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2189 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2190 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2194 case OVS_KEY_ATTR_ICMPV6: {
2195 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2196 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2198 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2200 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2205 case OVS_KEY_ATTR_ARP: {
2206 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2207 const struct ovs_key_arp *key = nl_attr_get(a);
2209 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2210 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2211 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2212 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2213 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2217 case OVS_KEY_ATTR_ND: {
2218 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2219 const struct ovs_key_nd *key = nl_attr_get(a);
2221 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2223 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2224 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2229 case OVS_KEY_ATTR_UNSPEC:
2230 case __OVS_KEY_ATTR_MAX:
2232 format_generic_odp_key(a, ds);
2234 ds_put_char(ds, '/');
2235 format_generic_odp_key(ma, ds);
2239 ds_put_char(ds, ')');
2242 static struct nlattr *
2243 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2244 struct ofpbuf *ofp, const struct nlattr *key)
2246 const struct nlattr *a;
2248 int type = nl_attr_type(key);
2249 int size = nl_attr_get_size(key);
2251 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2252 nl_msg_put_unspec_zero(ofp, type, size);
2256 if (tbl[type].next) {
2257 tbl = tbl[type].next;
2258 max = tbl[type].next_max;
2261 nested_mask = nl_msg_start_nested(ofp, type);
2262 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2263 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2265 nl_msg_end_nested(ofp, nested_mask);
2272 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2276 if (ovs_scan(s, "ufid:")) {
2279 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2291 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2293 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2296 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2297 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2298 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2299 * non-null and 'verbose' is true, translates odp port number to its name. */
2301 odp_flow_format(const struct nlattr *key, size_t key_len,
2302 const struct nlattr *mask, size_t mask_len,
2303 const struct hmap *portno_names, struct ds *ds, bool verbose)
2306 const struct nlattr *a;
2308 bool has_ethtype_key = false;
2309 const struct nlattr *ma = NULL;
2311 bool first_field = true;
2313 ofpbuf_init(&ofp, 100);
2314 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2315 bool is_nested_attr;
2316 bool is_wildcard = false;
2317 int attr_type = nl_attr_type(a);
2319 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2320 has_ethtype_key = true;
2323 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2324 OVS_KEY_ATTR_MAX, attr_type) ==
2327 if (mask && mask_len) {
2328 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2329 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2332 if (verbose || !is_wildcard || is_nested_attr) {
2333 if (is_wildcard && !ma) {
2334 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2339 ds_put_char(ds, ',');
2341 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2342 first_field = false;
2346 ofpbuf_uninit(&ofp);
2351 if (left == key_len) {
2352 ds_put_cstr(ds, "<empty>");
2354 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2355 for (i = 0; i < left; i++) {
2356 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2358 ds_put_char(ds, ')');
2360 if (!has_ethtype_key) {
2361 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2363 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2364 ntohs(nl_attr_get_be16(ma)));
2368 ds_put_cstr(ds, "<empty>");
2372 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2373 * OVS_KEY_ATTR_* attributes in 'key'. */
2375 odp_flow_key_format(const struct nlattr *key,
2376 size_t key_len, struct ds *ds)
2378 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2382 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2384 if (!strcasecmp(s, "no")) {
2385 *type = OVS_FRAG_TYPE_NONE;
2386 } else if (!strcasecmp(s, "first")) {
2387 *type = OVS_FRAG_TYPE_FIRST;
2388 } else if (!strcasecmp(s, "later")) {
2389 *type = OVS_FRAG_TYPE_LATER;
2399 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2400 uint8_t (*mask)[ETH_ADDR_LEN])
2404 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2408 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2409 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2412 memset(mask, 0xff, sizeof *mask);
2421 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2425 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2429 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2430 IP_SCAN_ARGS(mask), &n)) {
2433 *mask = OVS_BE32_MAX;
2442 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2445 char ipv6_s[IPV6_SCAN_LEN + 1];
2447 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2448 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2452 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2453 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2456 memset(mask, 0xff, sizeof *mask);
2465 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2470 if (ovs_scan(s, "%i%n", &key_, &n)
2471 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2476 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2477 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2479 *mask = htonl(mask_);
2481 *mask = htonl(IPV6_LABEL_MASK);
2490 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2494 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2498 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2510 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2514 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2518 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2530 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2532 uint16_t key_, mask_;
2535 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2540 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2542 *mask = htons(mask_);
2544 *mask = OVS_BE16_MAX;
2553 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2555 uint64_t key_, mask_;
2558 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2561 *key = htonll(key_);
2563 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2565 *mask = htonll(mask_);
2567 *mask = OVS_BE64_MAX;
2576 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2578 uint32_t flags, fmask;
2581 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
2582 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2583 if (n >= 0 && s[n] == ')') {
2594 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2596 uint32_t flags, fmask;
2599 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
2600 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2602 *key = htons(flags);
2604 *mask = htons(fmask);
2612 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2616 enum ovs_frag_type frag_type;
2618 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2619 && ovs_frag_type_from_string(frag, &frag_type)) {
2632 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2633 const struct simap *port_names)
2637 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2641 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2648 } else if (port_names) {
2649 const struct simap_node *node;
2652 len = strcspn(s, ")");
2653 node = simap_find_len(port_names, s, len);
2666 /* Helper for vlan parsing. */
2667 struct ovs_key_vlan__ {
2672 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2674 const uint16_t mask = ((1U << bits) - 1) << offset;
2676 if (value >> bits) {
2680 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2685 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2688 uint16_t key_, mask_;
2691 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2694 if (set_be16_bf(key, bits, offset, key_)) {
2696 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2699 if (!set_be16_bf(mask, bits, offset, mask_)) {
2703 *mask |= htons(((1U << bits) - 1) << offset);
2713 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2715 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2719 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2721 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2725 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2727 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2732 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2734 const uint32_t mask = ((1U << bits) - 1) << offset;
2736 if (value >> bits) {
2740 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2745 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2748 uint32_t key_, mask_;
2751 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2754 if (set_be32_bf(key, bits, offset, key_)) {
2756 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2759 if (!set_be32_bf(mask, bits, offset, mask_)) {
2763 *mask |= htonl(((1U << bits) - 1) << offset);
2773 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2775 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2779 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2781 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2785 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2787 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2791 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2793 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2797 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2799 const char *s_base = s;
2800 ovs_be16 id = 0, id_mask = 0;
2801 uint8_t flags = 0, flags_mask = 0;
2803 if (!strncmp(s, "id=", 3)) {
2805 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2811 if (!strncmp(s, "flags=", 6)) {
2813 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2816 if (!strncmp(s, "))", 2)) {
2819 *key = (flags << 16) | ntohs(id);
2821 *mask = (flags_mask << 16) | ntohs(id_mask);
2831 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2833 const char *s_base = s;
2834 struct geneve_opt *opt = key->d;
2835 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2836 int len_remain = sizeof key->d;
2838 while (s[0] == '{' && len_remain >= sizeof *opt) {
2842 len_remain -= sizeof *opt;
2844 if (!strncmp(s, "class=", 6)) {
2846 s += scan_be16(s, &opt->opt_class,
2847 mask ? &opt_mask->opt_class : NULL);
2849 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2855 if (!strncmp(s, "type=", 5)) {
2857 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2859 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2865 if (!strncmp(s, "len=", 4)) {
2866 uint8_t opt_len, opt_len_mask;
2868 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2870 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2873 opt->length = opt_len / 4;
2875 opt_mask->length = opt_len_mask;
2879 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2885 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2892 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2893 data_len, (char **)&s)) {
2904 opt += 1 + data_len / 4;
2906 opt_mask += 1 + data_len / 4;
2908 len_remain -= data_len;
2913 int len = sizeof key->d - len_remain;
2927 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
2929 const uint16_t *flags = data_;
2931 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
2932 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
2934 if (*flags & FLOW_TNL_F_CSUM) {
2935 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
2937 if (*flags & FLOW_TNL_F_OAM) {
2938 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
2943 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
2945 const uint32_t *gbp = data_;
2948 size_t vxlan_opts_ofs;
2950 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
2951 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
2952 nl_msg_end_nested(a, vxlan_opts_ofs);
2957 geneve_to_attr(struct ofpbuf *a, const void *data_)
2959 const struct geneve_scan *geneve = data_;
2961 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
2965 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
2967 unsigned long call_fn = (unsigned long)FUNC; \
2969 typedef void (*fn)(struct ofpbuf *, const void *); \
2971 func(BUF, &(DATA)); \
2973 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2977 #define SCAN_IF(NAME) \
2978 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2979 const char *start = s; \
2984 /* Usually no special initialization is needed. */
2985 #define SCAN_BEGIN(NAME, TYPE) \
2988 memset(&skey, 0, sizeof skey); \
2989 memset(&smask, 0, sizeof smask); \
2993 /* Init as fully-masked as mask will not be scanned. */
2994 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2997 memset(&skey, 0, sizeof skey); \
2998 memset(&smask, 0xff, sizeof smask); \
3002 /* VLAN needs special initialization. */
3003 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3005 TYPE skey = KEY_INIT; \
3006 TYPE smask = MASK_INIT; \
3010 /* Scan unnamed entry as 'TYPE' */
3011 #define SCAN_TYPE(TYPE, KEY, MASK) \
3012 len = scan_##TYPE(s, KEY, MASK); \
3018 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3019 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3020 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3021 s += strlen(NAME); \
3022 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3026 #define SCAN_FINISH() \
3027 } while (*s++ == ',' && len != 0); \
3028 if (s[-1] != ')') { \
3032 #define SCAN_FINISH_SINGLE() \
3034 if (*s++ != ')') { \
3038 /* Beginning of nested attribute. */
3039 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3041 size_t key_offset, mask_offset; \
3042 key_offset = nl_msg_start_nested(key, ATTR); \
3044 mask_offset = nl_msg_start_nested(mask, ATTR); \
3049 #define SCAN_END_NESTED() \
3051 nl_msg_end_nested(key, key_offset); \
3053 nl_msg_end_nested(mask, mask_offset); \
3058 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3059 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3061 memset(&skey, 0, sizeof skey); \
3062 memset(&smask, 0xff, sizeof smask); \
3063 s += strlen(NAME); \
3064 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3065 SCAN_PUT(ATTR, FUNC); \
3069 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3070 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3072 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3073 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3075 #define SCAN_PUT(ATTR, FUNC) \
3076 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3077 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3079 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3083 #define SCAN_END(ATTR) \
3085 SCAN_PUT(ATTR, NULL); \
3089 #define SCAN_END_SINGLE(ATTR) \
3090 SCAN_FINISH_SINGLE(); \
3091 SCAN_PUT(ATTR, NULL); \
3095 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3096 SCAN_BEGIN(NAME, TYPE) { \
3097 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3098 } SCAN_END_SINGLE(ATTR)
3100 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3101 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3102 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3103 } SCAN_END_SINGLE(ATTR)
3105 /* scan_port needs one extra argument. */
3106 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3107 SCAN_BEGIN(NAME, TYPE) { \
3108 len = scan_port(s, &skey, &smask, port_names); \
3113 } SCAN_END_SINGLE(ATTR)
3116 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3117 struct ofpbuf *key, struct ofpbuf *mask)
3123 len = odp_ufid_from_string(s, &ufid);
3128 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3129 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3130 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3131 OVS_KEY_ATTR_RECIRC_ID);
3132 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3134 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3135 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3136 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3137 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3138 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3139 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3140 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3141 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3142 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3143 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3145 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3146 } SCAN_END_NESTED();
3148 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3150 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3151 SCAN_FIELD("src=", eth, eth_src);
3152 SCAN_FIELD("dst=", eth, eth_dst);
3153 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3155 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3156 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3157 SCAN_FIELD("vid=", vid, tci);
3158 SCAN_FIELD("pcp=", pcp, tci);
3159 SCAN_FIELD("cfi=", cfi, tci);
3160 } SCAN_END(OVS_KEY_ATTR_VLAN);
3162 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3164 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3165 SCAN_FIELD("label=", mpls_label, mpls_lse);
3166 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3167 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3168 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3169 } SCAN_END(OVS_KEY_ATTR_MPLS);
3171 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3172 SCAN_FIELD("src=", ipv4, ipv4_src);
3173 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3174 SCAN_FIELD("proto=", u8, ipv4_proto);
3175 SCAN_FIELD("tos=", u8, ipv4_tos);
3176 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3177 SCAN_FIELD("frag=", frag, ipv4_frag);
3178 } SCAN_END(OVS_KEY_ATTR_IPV4);
3180 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3181 SCAN_FIELD("src=", ipv6, ipv6_src);
3182 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3183 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3184 SCAN_FIELD("proto=", u8, ipv6_proto);
3185 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3186 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3187 SCAN_FIELD("frag=", frag, ipv6_frag);
3188 } SCAN_END(OVS_KEY_ATTR_IPV6);
3190 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3191 SCAN_FIELD("src=", be16, tcp_src);
3192 SCAN_FIELD("dst=", be16, tcp_dst);
3193 } SCAN_END(OVS_KEY_ATTR_TCP);
3195 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3197 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3198 SCAN_FIELD("src=", be16, udp_src);
3199 SCAN_FIELD("dst=", be16, udp_dst);
3200 } SCAN_END(OVS_KEY_ATTR_UDP);
3202 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3203 SCAN_FIELD("src=", be16, sctp_src);
3204 SCAN_FIELD("dst=", be16, sctp_dst);
3205 } SCAN_END(OVS_KEY_ATTR_SCTP);
3207 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3208 SCAN_FIELD("type=", u8, icmp_type);
3209 SCAN_FIELD("code=", u8, icmp_code);
3210 } SCAN_END(OVS_KEY_ATTR_ICMP);
3212 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3213 SCAN_FIELD("type=", u8, icmpv6_type);
3214 SCAN_FIELD("code=", u8, icmpv6_code);
3215 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3217 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3218 SCAN_FIELD("sip=", ipv4, arp_sip);
3219 SCAN_FIELD("tip=", ipv4, arp_tip);
3220 SCAN_FIELD("op=", be16, arp_op);
3221 SCAN_FIELD("sha=", eth, arp_sha);
3222 SCAN_FIELD("tha=", eth, arp_tha);
3223 } SCAN_END(OVS_KEY_ATTR_ARP);
3225 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3226 SCAN_FIELD("target=", ipv6, nd_target);
3227 SCAN_FIELD("sll=", eth, nd_sll);
3228 SCAN_FIELD("tll=", eth, nd_tll);
3229 } SCAN_END(OVS_KEY_ATTR_ND);
3231 /* Encap open-coded. */
3232 if (!strncmp(s, "encap(", 6)) {
3233 const char *start = s;
3234 size_t encap, encap_mask = 0;
3236 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3238 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3245 s += strspn(s, delimiters);
3248 } else if (*s == ')') {
3252 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3260 nl_msg_end_nested(key, encap);
3262 nl_msg_end_nested(mask, encap_mask);
3271 /* Parses the string representation of a datapath flow key, in the
3272 * format output by odp_flow_key_format(). Returns 0 if successful,
3273 * otherwise a positive errno value. On success, the flow key is
3274 * appended to 'key' as a series of Netlink attributes. On failure, no
3275 * data is appended to 'key'. Either way, 'key''s data might be
3278 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3279 * to a port number. (Port names may be used instead of port numbers in
3282 * On success, the attributes appended to 'key' are individually syntactically
3283 * valid, but they may not be valid as a sequence. 'key' might, for example,
3284 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3286 odp_flow_from_string(const char *s, const struct simap *port_names,
3287 struct ofpbuf *key, struct ofpbuf *mask)
3289 const size_t old_size = key->size;
3293 s += strspn(s, delimiters);
3298 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3300 key->size = old_size;
3310 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3313 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3314 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3315 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3316 * must use a zero mask for the netlink frag field, and all ones mask
3318 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3320 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3321 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3322 : OVS_FRAG_TYPE_FIRST;
3325 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3326 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3327 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3329 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3331 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3333 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3335 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3336 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3337 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3338 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3340 /* These share the same layout. */
3342 struct ovs_key_tcp tcp;
3343 struct ovs_key_udp udp;
3344 struct ovs_key_sctp sctp;
3347 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3348 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3351 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3352 bool export_mask, struct ofpbuf *buf)
3354 struct ovs_key_ethernet *eth_key;
3356 const struct flow *flow = parms->flow;
3357 const struct flow *data = export_mask ? parms->mask : parms->flow;
3359 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3361 if (flow->tunnel.ip_dst || export_mask) {
3362 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3366 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3368 if (parms->support.recirc) {
3369 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3370 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3373 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3374 * is not the magical value "ODPP_NONE". */
3375 if (export_mask || parms->odp_in_port != ODPP_NONE) {
3376 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3379 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3381 get_ethernet_key(data, eth_key);
3383 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3385 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3387 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3389 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3390 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3391 if (flow->vlan_tci == htons(0)) {
3398 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3399 /* For backwards compatibility with kernels that don't support
3400 * wildcarding, the following convention is used to encode the
3401 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3404 * -------- -------- -------
3405 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3406 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3407 * <none> 0xffff Any non-Ethernet II frame (except valid
3408 * 802.3 SNAP packet with valid eth_type).
3411 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3416 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3418 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3419 struct ovs_key_ipv4 *ipv4_key;
3421 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3423 get_ipv4_key(data, ipv4_key, export_mask);
3424 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3425 struct ovs_key_ipv6 *ipv6_key;
3427 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3429 get_ipv6_key(data, ipv6_key, export_mask);
3430 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3431 flow->dl_type == htons(ETH_TYPE_RARP)) {
3432 struct ovs_key_arp *arp_key;
3434 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3436 get_arp_key(data, arp_key);
3437 } else if (eth_type_mpls(flow->dl_type)) {
3438 struct ovs_key_mpls *mpls_key;
3441 n = flow_count_mpls_labels(flow, NULL);
3443 n = MIN(n, parms->support.max_mpls_depth);
3445 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3446 n * sizeof *mpls_key);
3447 for (i = 0; i < n; i++) {
3448 mpls_key[i].mpls_lse = data->mpls_lse[i];
3452 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3453 if (flow->nw_proto == IPPROTO_TCP) {
3454 union ovs_key_tp *tcp_key;
3456 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3458 get_tp_key(data, tcp_key);
3459 if (data->tcp_flags) {
3460 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3462 } else if (flow->nw_proto == IPPROTO_UDP) {
3463 union ovs_key_tp *udp_key;
3465 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3467 get_tp_key(data, udp_key);
3468 } else if (flow->nw_proto == IPPROTO_SCTP) {
3469 union ovs_key_tp *sctp_key;
3471 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3473 get_tp_key(data, sctp_key);
3474 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3475 && flow->nw_proto == IPPROTO_ICMP) {
3476 struct ovs_key_icmp *icmp_key;
3478 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3480 icmp_key->icmp_type = ntohs(data->tp_src);
3481 icmp_key->icmp_code = ntohs(data->tp_dst);
3482 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3483 && flow->nw_proto == IPPROTO_ICMPV6) {
3484 struct ovs_key_icmpv6 *icmpv6_key;
3486 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3487 sizeof *icmpv6_key);
3488 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3489 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3491 if (flow->tp_dst == htons(0)
3492 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3493 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3494 && (!export_mask || (data->tp_src == htons(0xffff)
3495 && data->tp_dst == htons(0xffff)))) {
3497 struct ovs_key_nd *nd_key;
3499 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3501 memcpy(nd_key->nd_target, &data->nd_target,
3502 sizeof nd_key->nd_target);
3503 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3504 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3511 nl_msg_end_nested(buf, encap);
3515 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3517 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3518 * capable of being expanded to allow for that much space. */
3520 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3523 odp_flow_key_from_flow__(parms, false, buf);
3526 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3529 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3530 * capable of being expanded to allow for that much space. */
3532 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3535 odp_flow_key_from_flow__(parms, true, buf);
3538 /* Generate ODP flow key from the given packet metadata */
3540 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3542 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3544 if (md->tunnel.ip_dst) {
3545 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3548 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3550 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3551 * value "ODPP_NONE". */
3552 if (md->in_port.odp_port != ODPP_NONE) {
3553 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3557 /* Generate packet metadata from the given ODP flow key. */
3559 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3560 struct pkt_metadata *md)
3562 const struct nlattr *nla;
3564 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3565 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3566 1u << OVS_KEY_ATTR_IN_PORT;
3568 pkt_metadata_init(md, ODPP_NONE);
3570 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3571 uint16_t type = nl_attr_type(nla);
3572 size_t len = nl_attr_get_size(nla);
3573 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3574 OVS_KEY_ATTR_MAX, type);
3576 if (len != expected_len && expected_len >= 0) {
3581 case OVS_KEY_ATTR_RECIRC_ID:
3582 md->recirc_id = nl_attr_get_u32(nla);
3583 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3585 case OVS_KEY_ATTR_DP_HASH:
3586 md->dp_hash = nl_attr_get_u32(nla);
3587 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3589 case OVS_KEY_ATTR_PRIORITY:
3590 md->skb_priority = nl_attr_get_u32(nla);
3591 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3593 case OVS_KEY_ATTR_SKB_MARK:
3594 md->pkt_mark = nl_attr_get_u32(nla);
3595 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3597 case OVS_KEY_ATTR_TUNNEL: {
3598 enum odp_key_fitness res;
3600 res = odp_tun_key_from_attr(nla, &md->tunnel);
3601 if (res == ODP_FIT_ERROR) {
3602 memset(&md->tunnel, 0, sizeof md->tunnel);
3603 } else if (res == ODP_FIT_PERFECT) {
3604 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3608 case OVS_KEY_ATTR_IN_PORT:
3609 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3610 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3616 if (!wanted_attrs) {
3617 return; /* Have everything. */
3623 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3625 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3626 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3627 key_len / sizeof(uint32_t), 0);
3631 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3632 uint64_t attrs, int out_of_range_attr,
3633 const struct nlattr *key, size_t key_len)
3638 if (VLOG_DROP_DBG(rl)) {
3643 for (i = 0; i < 64; i++) {
3644 if (attrs & (UINT64_C(1) << i)) {
3645 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3647 ds_put_format(&s, " %s",
3648 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3651 if (out_of_range_attr) {
3652 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3655 ds_put_cstr(&s, ": ");
3656 odp_flow_key_format(key, key_len, &s);
3658 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3663 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3665 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3668 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3671 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3672 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3673 return 0xff; /* Error. */
3676 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3677 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3678 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3682 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3683 const struct nlattr *attrs[], uint64_t *present_attrsp,
3684 int *out_of_range_attrp)
3686 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3687 const struct nlattr *nla;
3688 uint64_t present_attrs;
3691 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3693 *out_of_range_attrp = 0;
3694 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3695 uint16_t type = nl_attr_type(nla);
3696 size_t len = nl_attr_get_size(nla);
3697 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3698 OVS_KEY_ATTR_MAX, type);
3700 if (len != expected_len && expected_len >= 0) {
3701 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3703 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3704 "length %d", ovs_key_attr_to_string(type, namebuf,
3710 if (type > OVS_KEY_ATTR_MAX) {
3711 *out_of_range_attrp = type;
3713 if (present_attrs & (UINT64_C(1) << type)) {
3714 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3716 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3717 ovs_key_attr_to_string(type,
3718 namebuf, sizeof namebuf));
3722 present_attrs |= UINT64_C(1) << type;
3727 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3731 *present_attrsp = present_attrs;
3735 static enum odp_key_fitness
3736 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3737 uint64_t expected_attrs,
3738 const struct nlattr *key, size_t key_len)
3740 uint64_t missing_attrs;
3741 uint64_t extra_attrs;
3743 missing_attrs = expected_attrs & ~present_attrs;
3744 if (missing_attrs) {
3745 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3746 log_odp_key_attributes(&rl, "expected but not present",
3747 missing_attrs, 0, key, key_len);
3748 return ODP_FIT_TOO_LITTLE;
3751 extra_attrs = present_attrs & ~expected_attrs;
3752 if (extra_attrs || out_of_range_attr) {
3753 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3754 log_odp_key_attributes(&rl, "present but not expected",
3755 extra_attrs, out_of_range_attr, key, key_len);
3756 return ODP_FIT_TOO_MUCH;
3759 return ODP_FIT_PERFECT;
3763 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3764 uint64_t present_attrs, uint64_t *expected_attrs,
3765 struct flow *flow, const struct flow *src_flow)
3767 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3768 bool is_mask = flow != src_flow;
3770 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3771 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3772 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3773 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3774 ntohs(flow->dl_type));
3777 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3778 flow->dl_type != htons(0xffff)) {
3781 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3784 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3785 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3786 /* See comments in odp_flow_key_from_flow__(). */
3787 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3794 static enum odp_key_fitness
3795 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3796 uint64_t present_attrs, int out_of_range_attr,
3797 uint64_t expected_attrs, struct flow *flow,
3798 const struct nlattr *key, size_t key_len,
3799 const struct flow *src_flow)
3801 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3802 bool is_mask = src_flow != flow;
3803 const void *check_start = NULL;
3804 size_t check_len = 0;
3805 enum ovs_key_attr expected_bit = 0xff;
3807 if (eth_type_mpls(src_flow->dl_type)) {
3808 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3809 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3811 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3812 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3813 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3814 int n = size / sizeof(ovs_be32);
3817 if (!size || size % sizeof(ovs_be32)) {
3818 return ODP_FIT_ERROR;
3820 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3821 return ODP_FIT_ERROR;
3824 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3825 flow->mpls_lse[i] = mpls_lse[i];
3827 if (n > FLOW_MAX_MPLS_LABELS) {
3828 return ODP_FIT_TOO_MUCH;
3832 /* BOS may be set only in the innermost label. */
3833 for (i = 0; i < n - 1; i++) {
3834 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3835 return ODP_FIT_ERROR;
3839 /* BOS must be set in the innermost label. */
3840 if (n < FLOW_MAX_MPLS_LABELS
3841 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3842 return ODP_FIT_TOO_LITTLE;
3848 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3850 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3852 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3853 const struct ovs_key_ipv4 *ipv4_key;
3855 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3856 put_ipv4_key(ipv4_key, flow, is_mask);
3857 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3858 return ODP_FIT_ERROR;
3861 check_start = ipv4_key;
3862 check_len = sizeof *ipv4_key;
3863 expected_bit = OVS_KEY_ATTR_IPV4;
3866 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3868 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3870 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3871 const struct ovs_key_ipv6 *ipv6_key;
3873 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3874 put_ipv6_key(ipv6_key, flow, is_mask);
3875 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3876 return ODP_FIT_ERROR;
3879 check_start = ipv6_key;
3880 check_len = sizeof *ipv6_key;
3881 expected_bit = OVS_KEY_ATTR_IPV6;
3884 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3885 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3887 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3889 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3890 const struct ovs_key_arp *arp_key;
3892 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3893 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3894 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3895 "key", ntohs(arp_key->arp_op));
3896 return ODP_FIT_ERROR;
3898 put_arp_key(arp_key, flow);
3900 check_start = arp_key;
3901 check_len = sizeof *arp_key;
3902 expected_bit = OVS_KEY_ATTR_ARP;
3908 if (check_len > 0) { /* Happens only when 'is_mask'. */
3909 if (!is_all_zeros(check_start, check_len) &&
3910 flow->dl_type != htons(0xffff)) {
3911 return ODP_FIT_ERROR;
3913 expected_attrs |= UINT64_C(1) << expected_bit;
3917 expected_bit = OVS_KEY_ATTR_UNSPEC;
3918 if (src_flow->nw_proto == IPPROTO_TCP
3919 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3920 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3921 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3923 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3925 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3926 const union ovs_key_tp *tcp_key;
3928 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3929 put_tp_key(tcp_key, flow);
3930 expected_bit = OVS_KEY_ATTR_TCP;
3932 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3933 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3934 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3936 } else if (src_flow->nw_proto == IPPROTO_UDP
3937 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3938 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3939 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3941 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3943 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3944 const union ovs_key_tp *udp_key;
3946 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3947 put_tp_key(udp_key, flow);
3948 expected_bit = OVS_KEY_ATTR_UDP;
3950 } else if (src_flow->nw_proto == IPPROTO_SCTP
3951 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3952 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3953 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3955 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3957 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3958 const union ovs_key_tp *sctp_key;
3960 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3961 put_tp_key(sctp_key, flow);
3962 expected_bit = OVS_KEY_ATTR_SCTP;
3964 } else if (src_flow->nw_proto == IPPROTO_ICMP
3965 && src_flow->dl_type == htons(ETH_TYPE_IP)
3966 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3968 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3970 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3971 const struct ovs_key_icmp *icmp_key;
3973 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3974 flow->tp_src = htons(icmp_key->icmp_type);
3975 flow->tp_dst = htons(icmp_key->icmp_code);
3976 expected_bit = OVS_KEY_ATTR_ICMP;
3978 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3979 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3980 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3982 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3984 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3985 const struct ovs_key_icmpv6 *icmpv6_key;
3987 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3988 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3989 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3990 expected_bit = OVS_KEY_ATTR_ICMPV6;
3991 if (src_flow->tp_dst == htons(0) &&
3992 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3993 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3995 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3997 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3998 const struct ovs_key_nd *nd_key;
4000 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4001 memcpy(&flow->nd_target, nd_key->nd_target,
4002 sizeof flow->nd_target);
4003 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4004 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4006 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4007 (flow->tp_src != htons(0xffff) ||
4008 flow->tp_dst != htons(0xffff))) {
4009 return ODP_FIT_ERROR;
4011 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4018 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4019 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4020 return ODP_FIT_ERROR;
4022 expected_attrs |= UINT64_C(1) << expected_bit;
4027 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4031 /* Parse 802.1Q header then encapsulated L3 attributes. */
4032 static enum odp_key_fitness
4033 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4034 uint64_t present_attrs, int out_of_range_attr,
4035 uint64_t expected_attrs, struct flow *flow,
4036 const struct nlattr *key, size_t key_len,
4037 const struct flow *src_flow)
4039 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4040 bool is_mask = src_flow != flow;
4042 const struct nlattr *encap
4043 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4044 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4045 enum odp_key_fitness encap_fitness;
4046 enum odp_key_fitness fitness;
4048 /* Calculate fitness of outer attributes. */
4050 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4051 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4053 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4054 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4056 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4057 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4060 fitness = check_expectations(present_attrs, out_of_range_attr,
4061 expected_attrs, key, key_len);
4064 * Remove the TPID from dl_type since it's not the real Ethertype. */
4065 flow->dl_type = htons(0);
4066 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4067 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4070 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4071 return ODP_FIT_TOO_LITTLE;
4072 } else if (flow->vlan_tci == htons(0)) {
4073 /* Corner case for a truncated 802.1Q header. */
4074 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4075 return ODP_FIT_TOO_MUCH;
4078 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4079 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4080 "but CFI bit is not set", ntohs(flow->vlan_tci));
4081 return ODP_FIT_ERROR;
4084 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4089 /* Now parse the encapsulated attributes. */
4090 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4091 attrs, &present_attrs, &out_of_range_attr)) {
4092 return ODP_FIT_ERROR;
4096 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4097 return ODP_FIT_ERROR;
4099 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4100 expected_attrs, flow, key, key_len,
4103 /* The overall fitness is the worse of the outer and inner attributes. */
4104 return MAX(fitness, encap_fitness);
4107 static enum odp_key_fitness
4108 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4109 const struct nlattr *src_key, size_t src_key_len,
4110 struct flow *flow, const struct flow *src_flow)
4112 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4113 uint64_t expected_attrs;
4114 uint64_t present_attrs;
4115 int out_of_range_attr;
4116 bool is_mask = src_flow != flow;
4118 memset(flow, 0, sizeof *flow);
4120 /* Parse attributes. */
4121 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4122 &out_of_range_attr)) {
4123 return ODP_FIT_ERROR;
4128 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4129 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4130 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4131 } else if (is_mask) {
4132 /* Always exact match recirc_id if it is not specified. */
4133 flow->recirc_id = UINT32_MAX;
4136 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4137 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4138 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4140 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4141 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4142 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4145 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4146 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4147 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4150 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4151 enum odp_key_fitness res;
4153 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], src_key,
4154 src_key_len, &src_flow->tunnel,
4156 if (res == ODP_FIT_ERROR) {
4157 return ODP_FIT_ERROR;
4158 } else if (res == ODP_FIT_PERFECT) {
4159 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4163 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4164 flow->in_port.odp_port
4165 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4166 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4167 } else if (!is_mask) {
4168 flow->in_port.odp_port = ODPP_NONE;
4171 /* Ethernet header. */
4172 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4173 const struct ovs_key_ethernet *eth_key;
4175 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4176 put_ethernet_key(eth_key, flow);
4178 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4182 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4185 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4186 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4188 return ODP_FIT_ERROR;
4192 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4193 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4194 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4195 expected_attrs, flow, key, key_len, src_flow);
4198 flow->vlan_tci = htons(0xffff);
4199 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4200 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4201 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4204 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4205 expected_attrs, flow, key, key_len, src_flow);
4208 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4209 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4210 * 'key' fits our expectations for what a flow key should contain.
4212 * The 'in_port' will be the datapath's understanding of the port. The
4213 * caller will need to translate with odp_port_to_ofp_port() if the
4214 * OpenFlow port is needed.
4216 * This function doesn't take the packet itself as an argument because none of
4217 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4218 * it is always possible to infer which additional attribute(s) should appear
4219 * by looking at the attributes for lower-level protocols, e.g. if the network
4220 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4221 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4222 * must be absent. */
4223 enum odp_key_fitness
4224 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4227 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow);
4230 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4231 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
4232 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4233 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
4234 * well 'key' fits our expectations for what a flow key should contain. */
4235 enum odp_key_fitness
4236 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4237 const struct nlattr *flow_key, size_t flow_key_len,
4238 struct flow *mask, const struct flow *flow)
4240 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4244 /* Returns 'fitness' as a string, for use in debug messages. */
4246 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4249 case ODP_FIT_PERFECT:
4251 case ODP_FIT_TOO_MUCH:
4253 case ODP_FIT_TOO_LITTLE:
4254 return "too_little";
4262 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4263 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4264 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4265 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4266 * null, then the return value is not meaningful.) */
4268 odp_put_userspace_action(uint32_t pid,
4269 const void *userdata, size_t userdata_size,
4270 odp_port_t tunnel_out_port,
4271 bool include_actions,
4272 struct ofpbuf *odp_actions)
4274 size_t userdata_ofs;
4277 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4278 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4280 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4282 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4283 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4286 * - The kernel rejected shorter userdata with -ERANGE.
4288 * - The kernel silently dropped userdata beyond the first 8 bytes.
4290 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4291 * separately disable features that required more than 8 bytes.) */
4292 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4293 MAX(8, userdata_size)),
4294 userdata, userdata_size);
4298 if (tunnel_out_port != ODPP_NONE) {
4299 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4302 if (include_actions) {
4303 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
4305 nl_msg_end_nested(odp_actions, offset);
4307 return userdata_ofs;
4311 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4312 struct ofpbuf *odp_actions)
4314 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4315 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4316 nl_msg_end_nested(odp_actions, offset);
4320 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4321 struct ovs_action_push_tnl *data)
4323 int size = offsetof(struct ovs_action_push_tnl, header);
4325 size += data->header_len;
4326 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4330 /* The commit_odp_actions() function and its helpers. */
4333 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4334 const void *key, size_t key_size)
4336 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4337 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4338 nl_msg_end_nested(odp_actions, offset);
4341 /* Masked set actions have a mask following the data within the netlink
4342 * attribute. The unmasked bits in the data will be cleared as the data
4343 * is copied to the action. */
4345 commit_masked_set_action(struct ofpbuf *odp_actions,
4346 enum ovs_key_attr key_type,
4347 const void *key_, const void *mask_, size_t key_size)
4349 size_t offset = nl_msg_start_nested(odp_actions,
4350 OVS_ACTION_ATTR_SET_MASKED);
4351 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4352 const char *key = key_, *mask = mask_;
4354 memcpy(data + key_size, mask, key_size);
4355 /* Clear unmasked bits while copying. */
4356 while (key_size--) {
4357 *data++ = *key++ & *mask++;
4359 nl_msg_end_nested(odp_actions, offset);
4362 /* If any of the flow key data that ODP actions can modify are different in
4363 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4364 * 'odp_actions' that change the flow tunneling information in key from
4365 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4366 * same way. In other words, operates the same as commit_odp_actions(), but
4367 * only on tunneling information. */
4369 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4370 struct ofpbuf *odp_actions)
4372 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4373 if (flow->tunnel.ip_dst) {
4374 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4377 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4378 odp_put_tunnel_action(&base->tunnel, odp_actions);
4383 commit(enum ovs_key_attr attr, bool use_masked_set,
4384 const void *key, void *base, void *mask, size_t size,
4385 struct ofpbuf *odp_actions)
4387 if (memcmp(key, base, size)) {
4388 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4390 if (use_masked_set && !fully_masked) {
4391 commit_masked_set_action(odp_actions, attr, key, mask, size);
4393 if (!fully_masked) {
4394 memset(mask, 0xff, size);
4396 commit_set_action(odp_actions, attr, key, size);
4398 memcpy(base, key, size);
4401 /* Mask bits are set when we have either read or set the corresponding
4402 * values. Masked bits will be exact-matched, no need to set them
4403 * if the value did not actually change. */
4409 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4411 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4412 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4416 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4418 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4419 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4423 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4424 struct ofpbuf *odp_actions,
4425 struct flow_wildcards *wc,
4428 struct ovs_key_ethernet key, base, mask;
4430 get_ethernet_key(flow, &key);
4431 get_ethernet_key(base_flow, &base);
4432 get_ethernet_key(&wc->masks, &mask);
4434 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4435 &key, &base, &mask, sizeof key, odp_actions)) {
4436 put_ethernet_key(&base, base_flow);
4437 put_ethernet_key(&mask, &wc->masks);
4442 pop_vlan(struct flow *base,
4443 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4445 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4447 if (base->vlan_tci & htons(VLAN_CFI)) {
4448 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4454 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4455 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4457 if (base->vlan_tci == vlan_tci) {
4461 pop_vlan(base, odp_actions, wc);
4462 if (vlan_tci & htons(VLAN_CFI)) {
4463 struct ovs_action_push_vlan vlan;
4465 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4466 vlan.vlan_tci = vlan_tci;
4467 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4468 &vlan, sizeof vlan);
4470 base->vlan_tci = vlan_tci;
4473 /* Wildcarding already done at action translation time. */
4475 commit_mpls_action(const struct flow *flow, struct flow *base,
4476 struct ofpbuf *odp_actions)
4478 int base_n = flow_count_mpls_labels(base, NULL);
4479 int flow_n = flow_count_mpls_labels(flow, NULL);
4480 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4483 while (base_n > common_n) {
4484 if (base_n - 1 == common_n && flow_n > common_n) {
4485 /* If there is only one more LSE in base than there are common
4486 * between base and flow; and flow has at least one more LSE than
4487 * is common then the topmost LSE of base may be updated using
4489 struct ovs_key_mpls mpls_key;
4491 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4492 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4493 &mpls_key, sizeof mpls_key);
4494 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4497 /* Otherwise, if there more LSEs in base than are common between
4498 * base and flow then pop the topmost one. */
4502 /* If all the LSEs are to be popped and this is not the outermost
4503 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4504 * POP_MPLS action instead of flow->dl_type.
4506 * This is because the POP_MPLS action requires its ethertype
4507 * argument to be an MPLS ethernet type but in this case
4508 * flow->dl_type will be a non-MPLS ethernet type.
4510 * When the final POP_MPLS action occurs it use flow->dl_type and
4511 * the and the resulting packet will have the desired dl_type. */
4512 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4513 dl_type = htons(ETH_TYPE_MPLS);
4515 dl_type = flow->dl_type;
4517 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4518 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4524 /* If, after the above popping and setting, there are more LSEs in flow
4525 * than base then some LSEs need to be pushed. */
4526 while (base_n < flow_n) {
4527 struct ovs_action_push_mpls *mpls;
4529 mpls = nl_msg_put_unspec_zero(odp_actions,
4530 OVS_ACTION_ATTR_PUSH_MPLS,
4532 mpls->mpls_ethertype = flow->dl_type;
4533 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4534 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4535 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4541 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4543 ipv4->ipv4_src = flow->nw_src;
4544 ipv4->ipv4_dst = flow->nw_dst;
4545 ipv4->ipv4_proto = flow->nw_proto;
4546 ipv4->ipv4_tos = flow->nw_tos;
4547 ipv4->ipv4_ttl = flow->nw_ttl;
4548 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4552 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4554 flow->nw_src = ipv4->ipv4_src;
4555 flow->nw_dst = ipv4->ipv4_dst;
4556 flow->nw_proto = ipv4->ipv4_proto;
4557 flow->nw_tos = ipv4->ipv4_tos;
4558 flow->nw_ttl = ipv4->ipv4_ttl;
4559 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4563 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4564 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4567 struct ovs_key_ipv4 key, mask, base;
4569 /* Check that nw_proto and nw_frag remain unchanged. */
4570 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4571 flow->nw_frag == base_flow->nw_frag);
4573 get_ipv4_key(flow, &key, false);
4574 get_ipv4_key(base_flow, &base, false);
4575 get_ipv4_key(&wc->masks, &mask, true);
4576 mask.ipv4_proto = 0; /* Not writeable. */
4577 mask.ipv4_frag = 0; /* Not writable. */
4579 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4581 put_ipv4_key(&base, base_flow, false);
4582 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4583 put_ipv4_key(&mask, &wc->masks, true);
4589 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4591 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4592 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4593 ipv6->ipv6_label = flow->ipv6_label;
4594 ipv6->ipv6_proto = flow->nw_proto;
4595 ipv6->ipv6_tclass = flow->nw_tos;
4596 ipv6->ipv6_hlimit = flow->nw_ttl;
4597 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4601 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4603 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4604 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4605 flow->ipv6_label = ipv6->ipv6_label;
4606 flow->nw_proto = ipv6->ipv6_proto;
4607 flow->nw_tos = ipv6->ipv6_tclass;
4608 flow->nw_ttl = ipv6->ipv6_hlimit;
4609 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4613 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4614 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4617 struct ovs_key_ipv6 key, mask, base;
4619 /* Check that nw_proto and nw_frag remain unchanged. */
4620 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4621 flow->nw_frag == base_flow->nw_frag);
4623 get_ipv6_key(flow, &key, false);
4624 get_ipv6_key(base_flow, &base, false);
4625 get_ipv6_key(&wc->masks, &mask, true);
4626 mask.ipv6_proto = 0; /* Not writeable. */
4627 mask.ipv6_frag = 0; /* Not writable. */
4629 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4631 put_ipv6_key(&base, base_flow, false);
4632 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4633 put_ipv6_key(&mask, &wc->masks, true);
4639 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4641 /* ARP key has padding, clear it. */
4642 memset(arp, 0, sizeof *arp);
4644 arp->arp_sip = flow->nw_src;
4645 arp->arp_tip = flow->nw_dst;
4646 arp->arp_op = htons(flow->nw_proto);
4647 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4648 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4652 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4654 flow->nw_src = arp->arp_sip;
4655 flow->nw_dst = arp->arp_tip;
4656 flow->nw_proto = ntohs(arp->arp_op);
4657 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4658 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4661 static enum slow_path_reason
4662 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4663 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4665 struct ovs_key_arp key, mask, base;
4667 get_arp_key(flow, &key);
4668 get_arp_key(base_flow, &base);
4669 get_arp_key(&wc->masks, &mask);
4671 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4673 put_arp_key(&base, base_flow);
4674 put_arp_key(&mask, &wc->masks);
4681 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4683 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4684 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4685 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4686 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4690 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4692 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4693 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4694 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4695 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4698 static enum slow_path_reason
4699 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4700 struct ofpbuf *odp_actions,
4701 struct flow_wildcards *wc, bool use_masked)
4703 struct ovs_key_nd key, mask, base;
4705 get_nd_key(flow, &key);
4706 get_nd_key(base_flow, &base);
4707 get_nd_key(&wc->masks, &mask);
4709 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4711 put_nd_key(&base, base_flow);
4712 put_nd_key(&mask, &wc->masks);
4719 static enum slow_path_reason
4720 commit_set_nw_action(const struct flow *flow, struct flow *base,
4721 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4724 /* Check if 'flow' really has an L3 header. */
4725 if (!flow->nw_proto) {
4729 switch (ntohs(base->dl_type)) {
4731 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4735 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4736 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4739 return commit_set_arp_action(flow, base, odp_actions, wc);
4745 /* TCP, UDP, and SCTP keys have the same layout. */
4746 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4747 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4750 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4752 tp->tcp.tcp_src = flow->tp_src;
4753 tp->tcp.tcp_dst = flow->tp_dst;
4757 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4759 flow->tp_src = tp->tcp.tcp_src;
4760 flow->tp_dst = tp->tcp.tcp_dst;
4764 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4765 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4768 enum ovs_key_attr key_type;
4769 union ovs_key_tp key, mask, base;
4771 /* Check if 'flow' really has an L3 header. */
4772 if (!flow->nw_proto) {
4776 if (!is_ip_any(base_flow)) {
4780 if (flow->nw_proto == IPPROTO_TCP) {
4781 key_type = OVS_KEY_ATTR_TCP;
4782 } else if (flow->nw_proto == IPPROTO_UDP) {
4783 key_type = OVS_KEY_ATTR_UDP;
4784 } else if (flow->nw_proto == IPPROTO_SCTP) {
4785 key_type = OVS_KEY_ATTR_SCTP;
4790 get_tp_key(flow, &key);
4791 get_tp_key(base_flow, &base);
4792 get_tp_key(&wc->masks, &mask);
4794 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4796 put_tp_key(&base, base_flow);
4797 put_tp_key(&mask, &wc->masks);
4802 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4803 struct ofpbuf *odp_actions,
4804 struct flow_wildcards *wc,
4807 uint32_t key, mask, base;
4809 key = flow->skb_priority;
4810 base = base_flow->skb_priority;
4811 mask = wc->masks.skb_priority;
4813 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4814 sizeof key, odp_actions)) {
4815 base_flow->skb_priority = base;
4816 wc->masks.skb_priority = mask;
4821 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4822 struct ofpbuf *odp_actions,
4823 struct flow_wildcards *wc,
4826 uint32_t key, mask, base;
4828 key = flow->pkt_mark;
4829 base = base_flow->pkt_mark;
4830 mask = wc->masks.pkt_mark;
4832 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4833 sizeof key, odp_actions)) {
4834 base_flow->pkt_mark = base;
4835 wc->masks.pkt_mark = mask;
4839 /* If any of the flow key data that ODP actions can modify are different in
4840 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4841 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4842 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4843 * in addition to this function if needed. Sets fields in 'wc' that are
4844 * used as part of the action.
4846 * Returns a reason to force processing the flow's packets into the userspace
4847 * slow path, if there is one, otherwise 0. */
4848 enum slow_path_reason
4849 commit_odp_actions(const struct flow *flow, struct flow *base,
4850 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4853 enum slow_path_reason slow;
4855 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4856 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4857 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4858 commit_mpls_action(flow, base, odp_actions);
4859 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4860 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4861 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);