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,
1271 const struct nlattr *a;
1273 bool unknown = false;
1275 NL_NESTED_FOR_EACH(a, left, attr) {
1276 uint16_t type = nl_attr_type(a);
1277 size_t len = nl_attr_get_size(a);
1278 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1279 OVS_TUNNEL_ATTR_MAX, type);
1281 if (len != expected_len && expected_len >= 0) {
1282 return ODP_FIT_ERROR;
1286 case OVS_TUNNEL_KEY_ATTR_ID:
1287 tun->tun_id = nl_attr_get_be64(a);
1288 tun->flags |= FLOW_TNL_F_KEY;
1290 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1291 tun->ip_src = nl_attr_get_be32(a);
1293 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1294 tun->ip_dst = nl_attr_get_be32(a);
1296 case OVS_TUNNEL_KEY_ATTR_TOS:
1297 tun->ip_tos = nl_attr_get_u8(a);
1299 case OVS_TUNNEL_KEY_ATTR_TTL:
1300 tun->ip_ttl = nl_attr_get_u8(a);
1303 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1304 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1306 case OVS_TUNNEL_KEY_ATTR_CSUM:
1307 tun->flags |= FLOW_TNL_F_CSUM;
1309 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1310 tun->tp_src = nl_attr_get_be16(a);
1312 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1313 tun->tp_dst = nl_attr_get_be16(a);
1315 case OVS_TUNNEL_KEY_ATTR_OAM:
1316 tun->flags |= FLOW_TNL_F_OAM;
1318 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1319 static const struct nl_policy vxlan_opts_policy[] = {
1320 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1322 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1324 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1325 return ODP_FIT_ERROR;
1328 if (ext[OVS_VXLAN_EXT_GBP]) {
1329 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1331 tun->gbp_id = htons(gbp & 0xFFFF);
1332 tun->gbp_flags = (gbp >> 16) & 0xFF;
1337 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1338 if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1339 src_tun, udpif, tun)) {
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, bool udpif,
1363 struct flow_tnl *tun)
1365 memset(tun, 0, sizeof *tun);
1366 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun, udpif);
1370 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1371 const struct flow_tnl *tun_flow_key,
1372 const struct ofpbuf *key_buf)
1376 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1378 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1379 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1380 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1382 if (tun_key->ip_src) {
1383 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1385 if (tun_key->ip_dst) {
1386 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1388 if (tun_key->ip_tos) {
1389 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1391 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1392 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1393 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1395 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1396 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1398 if (tun_key->tp_src) {
1399 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1401 if (tun_key->tp_dst) {
1402 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1404 if (tun_key->flags & FLOW_TNL_F_OAM) {
1405 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1407 if (tun_key->gbp_flags || tun_key->gbp_id) {
1408 size_t vxlan_opts_ofs;
1410 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1411 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1412 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1413 nl_msg_end_nested(a, vxlan_opts_ofs);
1415 tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
1417 nl_msg_end_nested(a, tun_key_ofs);
1421 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1423 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1427 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1429 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1430 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1432 if (attr == OVS_KEY_ATTR_IPV6) {
1433 const struct ovs_key_ipv6 *ipv6_mask = mask;
1436 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1437 == htonl(IPV6_LABEL_MASK))
1438 && ipv6_mask->ipv6_proto == UINT8_MAX
1439 && ipv6_mask->ipv6_tclass == UINT8_MAX
1440 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1441 && ipv6_mask->ipv6_frag == UINT8_MAX
1442 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1443 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1445 if (attr == OVS_KEY_ATTR_TUNNEL) {
1449 if (attr == OVS_KEY_ATTR_ARP) {
1450 /* ARP key has padding, ignore it. */
1451 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1452 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1453 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1454 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1457 return is_all_ones(mask, size);
1461 odp_mask_attr_is_exact(const struct nlattr *ma)
1463 enum ovs_key_attr attr = nl_attr_type(ma);
1467 if (attr == OVS_KEY_ATTR_TUNNEL) {
1470 mask = nl_attr_get(ma);
1471 size = nl_attr_get_size(ma);
1474 return odp_mask_is_exact(attr, mask, size);
1478 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1481 struct odp_portno_names *odp_portno_names;
1483 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1484 odp_portno_names->port_no = port_no;
1485 odp_portno_names->name = xstrdup(port_name);
1486 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1487 hash_odp_port(port_no));
1491 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1493 struct odp_portno_names *odp_portno_names;
1495 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1496 hash_odp_port(port_no), portno_names) {
1497 if (odp_portno_names->port_no == port_no) {
1498 return odp_portno_names->name;
1505 odp_portno_names_destroy(struct hmap *portno_names)
1507 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1508 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1509 hmap_node, portno_names) {
1510 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1511 free(odp_portno_names->name);
1512 free(odp_portno_names);
1516 /* Format helpers. */
1519 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1520 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1522 bool mask_empty = mask && eth_addr_is_zero(*mask);
1524 if (verbose || !mask_empty) {
1525 bool mask_full = !mask || eth_mask_is_exact(*mask);
1528 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1530 ds_put_format(ds, "%s=", name);
1531 eth_format_masked(key, *mask, ds);
1532 ds_put_char(ds, ',');
1538 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1539 const ovs_be64 *mask, bool verbose)
1541 bool mask_empty = mask && !*mask;
1543 if (verbose || !mask_empty) {
1544 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1546 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1547 if (!mask_full) { /* Partially masked. */
1548 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1550 ds_put_char(ds, ',');
1555 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1556 const ovs_be32 *mask, bool verbose)
1558 bool mask_empty = mask && !*mask;
1560 if (verbose || !mask_empty) {
1561 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1563 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1564 if (!mask_full) { /* Partially masked. */
1565 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1567 ds_put_char(ds, ',');
1572 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1573 const ovs_be32 (*mask_)[4], bool verbose)
1575 char buf[INET6_ADDRSTRLEN];
1576 const struct in6_addr *key = (const struct in6_addr *)key_;
1577 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1579 bool mask_empty = mask && ipv6_mask_is_any(mask);
1581 if (verbose || !mask_empty) {
1582 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1584 inet_ntop(AF_INET6, key, buf, sizeof buf);
1585 ds_put_format(ds, "%s=%s", name, buf);
1586 if (!mask_full) { /* Partially masked. */
1587 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1588 ds_put_format(ds, "/%s", buf);
1590 ds_put_char(ds, ',');
1595 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1596 const ovs_be32 *mask, bool verbose)
1598 bool mask_empty = mask && !*mask;
1600 if (verbose || !mask_empty) {
1601 bool mask_full = !mask
1602 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1604 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1605 if (!mask_full) { /* Partially masked. */
1606 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1608 ds_put_char(ds, ',');
1613 format_u8x(struct ds *ds, const char *name, uint8_t key,
1614 const uint8_t *mask, bool verbose)
1616 bool mask_empty = mask && !*mask;
1618 if (verbose || !mask_empty) {
1619 bool mask_full = !mask || *mask == UINT8_MAX;
1621 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1622 if (!mask_full) { /* Partially masked. */
1623 ds_put_format(ds, "/%#"PRIx8, *mask);
1625 ds_put_char(ds, ',');
1630 format_u8u(struct ds *ds, const char *name, uint8_t key,
1631 const uint8_t *mask, bool verbose)
1633 bool mask_empty = mask && !*mask;
1635 if (verbose || !mask_empty) {
1636 bool mask_full = !mask || *mask == UINT8_MAX;
1638 ds_put_format(ds, "%s=%"PRIu8, name, key);
1639 if (!mask_full) { /* Partially masked. */
1640 ds_put_format(ds, "/%#"PRIx8, *mask);
1642 ds_put_char(ds, ',');
1647 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1648 const ovs_be16 *mask, bool verbose)
1650 bool mask_empty = mask && !*mask;
1652 if (verbose || !mask_empty) {
1653 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1655 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1656 if (!mask_full) { /* Partially masked. */
1657 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1659 ds_put_char(ds, ',');
1664 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1665 const ovs_be16 *mask, bool verbose)
1667 bool mask_empty = mask && !*mask;
1669 if (verbose || !mask_empty) {
1670 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1672 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1673 if (!mask_full) { /* Partially masked. */
1674 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1676 ds_put_char(ds, ',');
1681 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1682 const uint16_t *mask, bool verbose)
1684 bool mask_empty = mask && !*mask;
1686 if (verbose || !mask_empty) {
1687 ds_put_cstr(ds, name);
1688 ds_put_char(ds, '(');
1690 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
1691 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
1692 } else { /* Fully masked. */
1693 format_flags(ds, flow_tun_flag_to_string, key, '|');
1695 ds_put_cstr(ds, "),");
1700 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1701 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1705 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1706 if (expected_len != ATTR_LEN_VARIABLE &&
1707 expected_len != ATTR_LEN_NESTED) {
1709 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1710 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1712 if (bad_key_len || bad_mask_len) {
1714 ds_put_format(ds, "key%u", nl_attr_type(a));
1717 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1718 nl_attr_get_size(a), expected_len);
1720 format_generic_odp_key(a, ds);
1722 ds_put_char(ds, '/');
1724 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1725 nl_attr_get_size(ma), expected_len);
1727 format_generic_odp_key(ma, ds);
1729 ds_put_char(ds, ')');
1738 format_unknown_key(struct ds *ds, const struct nlattr *a,
1739 const struct nlattr *ma)
1741 ds_put_format(ds, "key%u(", nl_attr_type(a));
1742 format_generic_odp_key(a, ds);
1743 if (ma && !odp_mask_attr_is_exact(ma)) {
1744 ds_put_char(ds, '/');
1745 format_generic_odp_key(ma, ds);
1747 ds_put_cstr(ds, "),");
1751 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1752 const struct nlattr *mask_attr, struct ds *ds,
1756 const struct nlattr *a;
1759 ofpbuf_init(&ofp, 100);
1760 NL_NESTED_FOR_EACH(a, left, attr) {
1761 uint16_t type = nl_attr_type(a);
1762 const struct nlattr *ma = NULL;
1765 ma = nl_attr_find__(nl_attr_get(mask_attr),
1766 nl_attr_get_size(mask_attr), type);
1768 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1774 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1775 OVS_VXLAN_EXT_MAX, true)) {
1780 case OVS_VXLAN_EXT_GBP: {
1781 uint32_t key = nl_attr_get_u32(a);
1782 ovs_be16 id, id_mask;
1783 uint8_t flags, flags_mask;
1785 id = htons(key & 0xFFFF);
1786 flags = (key >> 16) & 0xFF;
1788 uint32_t mask = nl_attr_get_u32(ma);
1789 id_mask = htons(mask & 0xFFFF);
1790 flags_mask = (mask >> 16) & 0xFF;
1793 ds_put_cstr(ds, "gbp(");
1794 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1795 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1797 ds_put_cstr(ds, "),");
1802 format_unknown_key(ds, a, ma);
1808 ofpbuf_uninit(&ofp);
1811 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1814 format_geneve_opts(const struct geneve_opt *opt,
1815 const struct geneve_opt *mask, int opts_len,
1816 struct ds *ds, bool verbose)
1818 while (opts_len > 0) {
1820 uint8_t data_len, data_len_mask;
1822 if (opts_len < sizeof *opt) {
1823 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1824 opts_len, sizeof *opt);
1828 data_len = opt->length * 4;
1830 if (mask->length == 0x1f) {
1831 data_len_mask = UINT8_MAX;
1833 data_len_mask = mask->length;
1836 len = sizeof *opt + data_len;
1837 if (len > opts_len) {
1838 ds_put_format(ds, "opt len %u greater than remaining %u",
1843 ds_put_char(ds, '{');
1844 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1846 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1847 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1848 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1849 ds_put_hex(ds, opt + 1, data_len);
1850 if (mask && !is_all_ones(mask + 1, data_len)) {
1851 ds_put_char(ds, '/');
1852 ds_put_hex(ds, mask + 1, data_len);
1857 ds_put_char(ds, '}');
1859 opt += len / sizeof(*opt);
1861 mask += len / sizeof(*opt);
1868 format_odp_tun_geneve(const struct nlattr *attr,
1869 const struct nlattr *mask_attr, struct ds *ds,
1872 int opts_len = nl_attr_get_size(attr);
1873 const struct geneve_opt *opt = nl_attr_get(attr);
1874 const struct geneve_opt *mask = mask_attr ?
1875 nl_attr_get(mask_attr) : NULL;
1877 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1878 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1879 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1883 format_geneve_opts(opt, mask, opts_len, ds, verbose);
1887 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1888 struct ds *ds, bool verbose)
1891 const struct nlattr *a;
1893 uint16_t mask_flags = 0;
1896 ofpbuf_init(&ofp, 100);
1897 NL_NESTED_FOR_EACH(a, left, attr) {
1898 enum ovs_tunnel_key_attr type = nl_attr_type(a);
1899 const struct nlattr *ma = NULL;
1902 ma = nl_attr_find__(nl_attr_get(mask_attr),
1903 nl_attr_get_size(mask_attr), type);
1905 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
1906 OVS_TUNNEL_KEY_ATTR_MAX,
1911 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
1912 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
1917 case OVS_TUNNEL_KEY_ATTR_ID:
1918 format_be64(ds, "tun_id", nl_attr_get_be64(a),
1919 ma ? nl_attr_get(ma) : NULL, verbose);
1920 flags |= FLOW_TNL_F_KEY;
1922 mask_flags |= FLOW_TNL_F_KEY;
1925 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1926 format_ipv4(ds, "src", nl_attr_get_be32(a),
1927 ma ? nl_attr_get(ma) : NULL, verbose);
1929 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1930 format_ipv4(ds, "dst", nl_attr_get_be32(a),
1931 ma ? nl_attr_get(ma) : NULL, verbose);
1933 case OVS_TUNNEL_KEY_ATTR_TOS:
1934 format_u8x(ds, "tos", nl_attr_get_u8(a),
1935 ma ? nl_attr_get(ma) : NULL, verbose);
1937 case OVS_TUNNEL_KEY_ATTR_TTL:
1938 format_u8u(ds, "ttl", nl_attr_get_u8(a),
1939 ma ? nl_attr_get(ma) : NULL, verbose);
1941 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1942 flags |= FLOW_TNL_F_DONT_FRAGMENT;
1944 case OVS_TUNNEL_KEY_ATTR_CSUM:
1945 flags |= FLOW_TNL_F_CSUM;
1947 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1948 format_be16(ds, "tp_src", nl_attr_get_be16(a),
1949 ma ? nl_attr_get(ma) : NULL, verbose);
1951 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1952 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
1953 ma ? nl_attr_get(ma) : NULL, verbose);
1955 case OVS_TUNNEL_KEY_ATTR_OAM:
1956 flags |= FLOW_TNL_F_OAM;
1958 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1959 ds_put_cstr(ds, "vxlan(");
1960 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
1961 ds_put_cstr(ds, "),");
1963 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1964 ds_put_cstr(ds, "geneve(");
1965 format_odp_tun_geneve(a, ma, ds, verbose);
1966 ds_put_cstr(ds, "),");
1968 case __OVS_TUNNEL_KEY_ATTR_MAX:
1970 format_unknown_key(ds, a, ma);
1975 /* Flags can have a valid mask even if the attribute is not set, so
1976 * we need to collect these separately. */
1978 NL_NESTED_FOR_EACH(a, left, mask_attr) {
1979 switch (nl_attr_type(a)) {
1980 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1981 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
1983 case OVS_TUNNEL_KEY_ATTR_CSUM:
1984 mask_flags |= FLOW_TNL_F_CSUM;
1986 case OVS_TUNNEL_KEY_ATTR_OAM:
1987 mask_flags |= FLOW_TNL_F_OAM;
1993 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
1996 ofpbuf_uninit(&ofp);
2000 format_frag(struct ds *ds, const char *name, uint8_t key,
2001 const uint8_t *mask, bool verbose)
2003 bool mask_empty = mask && !*mask;
2005 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2006 if (verbose || !mask_empty) {
2007 bool mask_full = !mask || *mask == UINT8_MAX;
2009 if (!mask_full) { /* Partially masked. */
2010 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2013 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2019 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2020 const struct hmap *portno_names, struct ds *ds,
2023 enum ovs_key_attr attr = nl_attr_type(a);
2024 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2027 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2029 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2031 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2032 OVS_KEY_ATTR_MAX, false)) {
2036 ds_put_char(ds, '(');
2038 case OVS_KEY_ATTR_ENCAP:
2039 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2040 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2041 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2043 } else if (nl_attr_get_size(a)) {
2044 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2049 case OVS_KEY_ATTR_PRIORITY:
2050 case OVS_KEY_ATTR_SKB_MARK:
2051 case OVS_KEY_ATTR_DP_HASH:
2052 case OVS_KEY_ATTR_RECIRC_ID:
2053 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2055 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2059 case OVS_KEY_ATTR_TUNNEL:
2060 format_odp_tun_attr(a, ma, ds, verbose);
2063 case OVS_KEY_ATTR_IN_PORT:
2064 if (portno_names && verbose && is_exact) {
2065 char *name = odp_portno_names_get(portno_names,
2066 u32_to_odp(nl_attr_get_u32(a)));
2068 ds_put_format(ds, "%s", name);
2070 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2073 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2075 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2080 case OVS_KEY_ATTR_ETHERNET: {
2081 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2082 const struct ovs_key_ethernet *key = nl_attr_get(a);
2084 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2085 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2089 case OVS_KEY_ATTR_VLAN:
2090 format_vlan_tci(ds, nl_attr_get_be16(a),
2091 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2094 case OVS_KEY_ATTR_MPLS: {
2095 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2096 const struct ovs_key_mpls *mpls_mask = NULL;
2097 size_t size = nl_attr_get_size(a);
2099 if (!size || size % sizeof *mpls_key) {
2100 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2104 mpls_mask = nl_attr_get(ma);
2105 if (size != nl_attr_get_size(ma)) {
2106 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2107 "mask length %"PRIuSIZE")",
2108 size, nl_attr_get_size(ma));
2112 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2115 case OVS_KEY_ATTR_ETHERTYPE:
2116 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2118 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2122 case OVS_KEY_ATTR_IPV4: {
2123 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2124 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2126 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2127 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2128 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2130 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2131 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2132 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2137 case OVS_KEY_ATTR_IPV6: {
2138 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2139 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2141 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2142 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2143 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2145 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2147 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2149 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2151 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2156 /* These have the same structure and format. */
2157 case OVS_KEY_ATTR_TCP:
2158 case OVS_KEY_ATTR_UDP:
2159 case OVS_KEY_ATTR_SCTP: {
2160 const struct ovs_key_tcp *key = nl_attr_get(a);
2161 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2163 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2164 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2168 case OVS_KEY_ATTR_TCP_FLAGS:
2170 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2171 ntohs(nl_attr_get_be16(a)),
2172 TCP_FLAGS(nl_attr_get_be16(ma)),
2173 TCP_FLAGS(OVS_BE16_MAX));
2175 format_flags(ds, packet_tcp_flag_to_string,
2176 ntohs(nl_attr_get_be16(a)), '|');
2180 case OVS_KEY_ATTR_ICMP: {
2181 const struct ovs_key_icmp *key = nl_attr_get(a);
2182 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2184 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2185 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2189 case OVS_KEY_ATTR_ICMPV6: {
2190 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2191 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2193 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2195 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2200 case OVS_KEY_ATTR_ARP: {
2201 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2202 const struct ovs_key_arp *key = nl_attr_get(a);
2204 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2205 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2206 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2207 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2208 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2212 case OVS_KEY_ATTR_ND: {
2213 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2214 const struct ovs_key_nd *key = nl_attr_get(a);
2216 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2218 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2219 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2224 case OVS_KEY_ATTR_UNSPEC:
2225 case __OVS_KEY_ATTR_MAX:
2227 format_generic_odp_key(a, ds);
2229 ds_put_char(ds, '/');
2230 format_generic_odp_key(ma, ds);
2234 ds_put_char(ds, ')');
2237 static struct nlattr *
2238 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2239 struct ofpbuf *ofp, const struct nlattr *key)
2241 const struct nlattr *a;
2243 int type = nl_attr_type(key);
2244 int size = nl_attr_get_size(key);
2246 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2247 nl_msg_put_unspec_zero(ofp, type, size);
2251 if (tbl[type].next) {
2252 tbl = tbl[type].next;
2253 max = tbl[type].next_max;
2256 nested_mask = nl_msg_start_nested(ofp, type);
2257 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2258 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2260 nl_msg_end_nested(ofp, nested_mask);
2267 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2271 if (ovs_scan(s, "ufid:")) {
2274 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2286 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2288 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2291 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2292 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2293 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2294 * non-null and 'verbose' is true, translates odp port number to its name. */
2296 odp_flow_format(const struct nlattr *key, size_t key_len,
2297 const struct nlattr *mask, size_t mask_len,
2298 const struct hmap *portno_names, struct ds *ds, bool verbose)
2301 const struct nlattr *a;
2303 bool has_ethtype_key = false;
2304 const struct nlattr *ma = NULL;
2306 bool first_field = true;
2308 ofpbuf_init(&ofp, 100);
2309 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2310 bool is_nested_attr;
2311 bool is_wildcard = false;
2312 int attr_type = nl_attr_type(a);
2314 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2315 has_ethtype_key = true;
2318 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2319 OVS_KEY_ATTR_MAX, attr_type) ==
2322 if (mask && mask_len) {
2323 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2324 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2327 if (verbose || !is_wildcard || is_nested_attr) {
2328 if (is_wildcard && !ma) {
2329 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2334 ds_put_char(ds, ',');
2336 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2337 first_field = false;
2341 ofpbuf_uninit(&ofp);
2346 if (left == key_len) {
2347 ds_put_cstr(ds, "<empty>");
2349 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2350 for (i = 0; i < left; i++) {
2351 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2353 ds_put_char(ds, ')');
2355 if (!has_ethtype_key) {
2356 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2358 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2359 ntohs(nl_attr_get_be16(ma)));
2363 ds_put_cstr(ds, "<empty>");
2367 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2368 * OVS_KEY_ATTR_* attributes in 'key'. */
2370 odp_flow_key_format(const struct nlattr *key,
2371 size_t key_len, struct ds *ds)
2373 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2377 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2379 if (!strcasecmp(s, "no")) {
2380 *type = OVS_FRAG_TYPE_NONE;
2381 } else if (!strcasecmp(s, "first")) {
2382 *type = OVS_FRAG_TYPE_FIRST;
2383 } else if (!strcasecmp(s, "later")) {
2384 *type = OVS_FRAG_TYPE_LATER;
2394 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2395 uint8_t (*mask)[ETH_ADDR_LEN])
2399 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2403 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2404 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2407 memset(mask, 0xff, sizeof *mask);
2416 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2420 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2424 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2425 IP_SCAN_ARGS(mask), &n)) {
2428 *mask = OVS_BE32_MAX;
2437 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2440 char ipv6_s[IPV6_SCAN_LEN + 1];
2442 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2443 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2447 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2448 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2451 memset(mask, 0xff, sizeof *mask);
2460 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2465 if (ovs_scan(s, "%i%n", &key_, &n)
2466 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2471 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2472 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2474 *mask = htonl(mask_);
2476 *mask = htonl(IPV6_LABEL_MASK);
2485 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2489 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2493 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2505 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2509 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2513 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2525 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2527 uint16_t key_, mask_;
2530 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2535 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2537 *mask = htons(mask_);
2539 *mask = OVS_BE16_MAX;
2548 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2550 uint64_t key_, mask_;
2553 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2556 *key = htonll(key_);
2558 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2560 *mask = htonll(mask_);
2562 *mask = OVS_BE64_MAX;
2571 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2573 uint32_t flags, fmask;
2576 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
2577 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2578 if (n >= 0 && s[n] == ')') {
2589 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2591 uint32_t flags, fmask;
2594 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
2595 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2597 *key = htons(flags);
2599 *mask = htons(fmask);
2607 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2611 enum ovs_frag_type frag_type;
2613 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2614 && ovs_frag_type_from_string(frag, &frag_type)) {
2627 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2628 const struct simap *port_names)
2632 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2636 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2643 } else if (port_names) {
2644 const struct simap_node *node;
2647 len = strcspn(s, ")");
2648 node = simap_find_len(port_names, s, len);
2661 /* Helper for vlan parsing. */
2662 struct ovs_key_vlan__ {
2667 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2669 const uint16_t mask = ((1U << bits) - 1) << offset;
2671 if (value >> bits) {
2675 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2680 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2683 uint16_t key_, mask_;
2686 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2689 if (set_be16_bf(key, bits, offset, key_)) {
2691 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2694 if (!set_be16_bf(mask, bits, offset, mask_)) {
2698 *mask |= htons(((1U << bits) - 1) << offset);
2708 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2710 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2714 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2716 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2720 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2722 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2727 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2729 const uint32_t mask = ((1U << bits) - 1) << offset;
2731 if (value >> bits) {
2735 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2740 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2743 uint32_t key_, mask_;
2746 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2749 if (set_be32_bf(key, bits, offset, key_)) {
2751 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2754 if (!set_be32_bf(mask, bits, offset, mask_)) {
2758 *mask |= htonl(((1U << bits) - 1) << offset);
2768 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2770 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2774 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2776 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2780 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2782 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2786 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2788 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2792 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2794 const char *s_base = s;
2795 ovs_be16 id = 0, id_mask = 0;
2796 uint8_t flags = 0, flags_mask = 0;
2798 if (!strncmp(s, "id=", 3)) {
2800 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2806 if (!strncmp(s, "flags=", 6)) {
2808 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2811 if (!strncmp(s, "))", 2)) {
2814 *key = (flags << 16) | ntohs(id);
2816 *mask = (flags_mask << 16) | ntohs(id_mask);
2826 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2828 const char *s_base = s;
2829 struct geneve_opt *opt = key->d;
2830 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2831 int len_remain = sizeof key->d;
2833 while (s[0] == '{' && len_remain >= sizeof *opt) {
2837 len_remain -= sizeof *opt;
2839 if (!strncmp(s, "class=", 6)) {
2841 s += scan_be16(s, &opt->opt_class,
2842 mask ? &opt_mask->opt_class : NULL);
2844 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2850 if (!strncmp(s, "type=", 5)) {
2852 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2854 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2860 if (!strncmp(s, "len=", 4)) {
2861 uint8_t opt_len, opt_len_mask;
2863 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2865 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2868 opt->length = opt_len / 4;
2870 opt_mask->length = opt_len_mask;
2874 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2880 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2887 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2888 data_len, (char **)&s)) {
2899 opt += 1 + data_len / 4;
2901 opt_mask += 1 + data_len / 4;
2903 len_remain -= data_len;
2908 int len = sizeof key->d - len_remain;
2922 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
2924 const uint16_t *flags = data_;
2926 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
2927 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
2929 if (*flags & FLOW_TNL_F_CSUM) {
2930 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
2932 if (*flags & FLOW_TNL_F_OAM) {
2933 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
2938 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
2940 const uint32_t *gbp = data_;
2943 size_t vxlan_opts_ofs;
2945 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
2946 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
2947 nl_msg_end_nested(a, vxlan_opts_ofs);
2952 geneve_to_attr(struct ofpbuf *a, const void *data_)
2954 const struct geneve_scan *geneve = data_;
2956 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
2960 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
2962 unsigned long call_fn = (unsigned long)FUNC; \
2964 typedef void (*fn)(struct ofpbuf *, const void *); \
2966 func(BUF, &(DATA)); \
2968 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2972 #define SCAN_IF(NAME) \
2973 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2974 const char *start = s; \
2979 /* Usually no special initialization is needed. */
2980 #define SCAN_BEGIN(NAME, TYPE) \
2983 memset(&skey, 0, sizeof skey); \
2984 memset(&smask, 0, sizeof smask); \
2988 /* Init as fully-masked as mask will not be scanned. */
2989 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2992 memset(&skey, 0, sizeof skey); \
2993 memset(&smask, 0xff, sizeof smask); \
2997 /* VLAN needs special initialization. */
2998 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3000 TYPE skey = KEY_INIT; \
3001 TYPE smask = MASK_INIT; \
3005 /* Scan unnamed entry as 'TYPE' */
3006 #define SCAN_TYPE(TYPE, KEY, MASK) \
3007 len = scan_##TYPE(s, KEY, MASK); \
3013 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3014 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3015 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3016 s += strlen(NAME); \
3017 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3021 #define SCAN_FINISH() \
3022 } while (*s++ == ',' && len != 0); \
3023 if (s[-1] != ')') { \
3027 #define SCAN_FINISH_SINGLE() \
3029 if (*s++ != ')') { \
3033 /* Beginning of nested attribute. */
3034 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3036 size_t key_offset, mask_offset; \
3037 key_offset = nl_msg_start_nested(key, ATTR); \
3039 mask_offset = nl_msg_start_nested(mask, ATTR); \
3044 #define SCAN_END_NESTED() \
3046 nl_msg_end_nested(key, key_offset); \
3048 nl_msg_end_nested(mask, mask_offset); \
3053 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3054 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3056 memset(&skey, 0, sizeof skey); \
3057 memset(&smask, 0xff, sizeof smask); \
3058 s += strlen(NAME); \
3059 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3060 SCAN_PUT(ATTR, FUNC); \
3064 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3065 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3067 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3068 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3070 #define SCAN_PUT(ATTR, FUNC) \
3071 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3072 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3074 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3078 #define SCAN_END(ATTR) \
3080 SCAN_PUT(ATTR, NULL); \
3084 #define SCAN_END_SINGLE(ATTR) \
3085 SCAN_FINISH_SINGLE(); \
3086 SCAN_PUT(ATTR, NULL); \
3090 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3091 SCAN_BEGIN(NAME, TYPE) { \
3092 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3093 } SCAN_END_SINGLE(ATTR)
3095 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3096 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3097 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3098 } SCAN_END_SINGLE(ATTR)
3100 /* scan_port needs one extra argument. */
3101 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3102 SCAN_BEGIN(NAME, TYPE) { \
3103 len = scan_port(s, &skey, &smask, port_names); \
3108 } SCAN_END_SINGLE(ATTR)
3111 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3112 struct ofpbuf *key, struct ofpbuf *mask)
3118 len = odp_ufid_from_string(s, &ufid);
3123 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3124 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3125 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3126 OVS_KEY_ATTR_RECIRC_ID);
3127 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3129 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3130 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3131 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3132 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3133 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3134 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3135 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3136 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3137 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3138 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3140 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3141 } SCAN_END_NESTED();
3143 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3145 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3146 SCAN_FIELD("src=", eth, eth_src);
3147 SCAN_FIELD("dst=", eth, eth_dst);
3148 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3150 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3151 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3152 SCAN_FIELD("vid=", vid, tci);
3153 SCAN_FIELD("pcp=", pcp, tci);
3154 SCAN_FIELD("cfi=", cfi, tci);
3155 } SCAN_END(OVS_KEY_ATTR_VLAN);
3157 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3159 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3160 SCAN_FIELD("label=", mpls_label, mpls_lse);
3161 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3162 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3163 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3164 } SCAN_END(OVS_KEY_ATTR_MPLS);
3166 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3167 SCAN_FIELD("src=", ipv4, ipv4_src);
3168 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3169 SCAN_FIELD("proto=", u8, ipv4_proto);
3170 SCAN_FIELD("tos=", u8, ipv4_tos);
3171 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3172 SCAN_FIELD("frag=", frag, ipv4_frag);
3173 } SCAN_END(OVS_KEY_ATTR_IPV4);
3175 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3176 SCAN_FIELD("src=", ipv6, ipv6_src);
3177 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3178 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3179 SCAN_FIELD("proto=", u8, ipv6_proto);
3180 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3181 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3182 SCAN_FIELD("frag=", frag, ipv6_frag);
3183 } SCAN_END(OVS_KEY_ATTR_IPV6);
3185 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3186 SCAN_FIELD("src=", be16, tcp_src);
3187 SCAN_FIELD("dst=", be16, tcp_dst);
3188 } SCAN_END(OVS_KEY_ATTR_TCP);
3190 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3192 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3193 SCAN_FIELD("src=", be16, udp_src);
3194 SCAN_FIELD("dst=", be16, udp_dst);
3195 } SCAN_END(OVS_KEY_ATTR_UDP);
3197 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3198 SCAN_FIELD("src=", be16, sctp_src);
3199 SCAN_FIELD("dst=", be16, sctp_dst);
3200 } SCAN_END(OVS_KEY_ATTR_SCTP);
3202 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3203 SCAN_FIELD("type=", u8, icmp_type);
3204 SCAN_FIELD("code=", u8, icmp_code);
3205 } SCAN_END(OVS_KEY_ATTR_ICMP);
3207 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3208 SCAN_FIELD("type=", u8, icmpv6_type);
3209 SCAN_FIELD("code=", u8, icmpv6_code);
3210 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3212 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3213 SCAN_FIELD("sip=", ipv4, arp_sip);
3214 SCAN_FIELD("tip=", ipv4, arp_tip);
3215 SCAN_FIELD("op=", be16, arp_op);
3216 SCAN_FIELD("sha=", eth, arp_sha);
3217 SCAN_FIELD("tha=", eth, arp_tha);
3218 } SCAN_END(OVS_KEY_ATTR_ARP);
3220 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3221 SCAN_FIELD("target=", ipv6, nd_target);
3222 SCAN_FIELD("sll=", eth, nd_sll);
3223 SCAN_FIELD("tll=", eth, nd_tll);
3224 } SCAN_END(OVS_KEY_ATTR_ND);
3226 /* Encap open-coded. */
3227 if (!strncmp(s, "encap(", 6)) {
3228 const char *start = s;
3229 size_t encap, encap_mask = 0;
3231 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3233 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3240 s += strspn(s, delimiters);
3243 } else if (*s == ')') {
3247 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3255 nl_msg_end_nested(key, encap);
3257 nl_msg_end_nested(mask, encap_mask);
3266 /* Parses the string representation of a datapath flow key, in the
3267 * format output by odp_flow_key_format(). Returns 0 if successful,
3268 * otherwise a positive errno value. On success, the flow key is
3269 * appended to 'key' as a series of Netlink attributes. On failure, no
3270 * data is appended to 'key'. Either way, 'key''s data might be
3273 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3274 * to a port number. (Port names may be used instead of port numbers in
3277 * On success, the attributes appended to 'key' are individually syntactically
3278 * valid, but they may not be valid as a sequence. 'key' might, for example,
3279 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3281 odp_flow_from_string(const char *s, const struct simap *port_names,
3282 struct ofpbuf *key, struct ofpbuf *mask)
3284 const size_t old_size = key->size;
3288 s += strspn(s, delimiters);
3293 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3295 key->size = old_size;
3305 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3308 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3309 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3310 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3311 * must use a zero mask for the netlink frag field, and all ones mask
3313 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3315 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3316 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3317 : OVS_FRAG_TYPE_FIRST;
3320 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3321 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3322 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3324 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3326 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3328 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3330 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3331 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3332 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3333 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3335 /* These share the same layout. */
3337 struct ovs_key_tcp tcp;
3338 struct ovs_key_udp udp;
3339 struct ovs_key_sctp sctp;
3342 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3343 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3346 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3347 bool export_mask, struct ofpbuf *buf)
3349 struct ovs_key_ethernet *eth_key;
3351 const struct flow *flow = parms->flow;
3352 const struct flow *data = export_mask ? parms->mask : parms->flow;
3354 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3356 if (flow->tunnel.ip_dst || export_mask) {
3357 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3361 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3363 if (parms->support.recirc) {
3364 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3365 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3368 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3369 * is not the magical value "ODPP_NONE". */
3370 if (export_mask || parms->odp_in_port != ODPP_NONE) {
3371 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3374 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3376 get_ethernet_key(data, eth_key);
3378 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3380 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3382 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3384 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3385 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3386 if (flow->vlan_tci == htons(0)) {
3393 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3394 /* For backwards compatibility with kernels that don't support
3395 * wildcarding, the following convention is used to encode the
3396 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3399 * -------- -------- -------
3400 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3401 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3402 * <none> 0xffff Any non-Ethernet II frame (except valid
3403 * 802.3 SNAP packet with valid eth_type).
3406 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3411 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3413 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3414 struct ovs_key_ipv4 *ipv4_key;
3416 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3418 get_ipv4_key(data, ipv4_key, export_mask);
3419 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3420 struct ovs_key_ipv6 *ipv6_key;
3422 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3424 get_ipv6_key(data, ipv6_key, export_mask);
3425 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3426 flow->dl_type == htons(ETH_TYPE_RARP)) {
3427 struct ovs_key_arp *arp_key;
3429 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3431 get_arp_key(data, arp_key);
3432 } else if (eth_type_mpls(flow->dl_type)) {
3433 struct ovs_key_mpls *mpls_key;
3436 n = flow_count_mpls_labels(flow, NULL);
3438 n = MIN(n, parms->support.max_mpls_depth);
3440 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3441 n * sizeof *mpls_key);
3442 for (i = 0; i < n; i++) {
3443 mpls_key[i].mpls_lse = data->mpls_lse[i];
3447 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3448 if (flow->nw_proto == IPPROTO_TCP) {
3449 union ovs_key_tp *tcp_key;
3451 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3453 get_tp_key(data, tcp_key);
3454 if (data->tcp_flags) {
3455 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3457 } else if (flow->nw_proto == IPPROTO_UDP) {
3458 union ovs_key_tp *udp_key;
3460 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3462 get_tp_key(data, udp_key);
3463 } else if (flow->nw_proto == IPPROTO_SCTP) {
3464 union ovs_key_tp *sctp_key;
3466 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3468 get_tp_key(data, sctp_key);
3469 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3470 && flow->nw_proto == IPPROTO_ICMP) {
3471 struct ovs_key_icmp *icmp_key;
3473 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3475 icmp_key->icmp_type = ntohs(data->tp_src);
3476 icmp_key->icmp_code = ntohs(data->tp_dst);
3477 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3478 && flow->nw_proto == IPPROTO_ICMPV6) {
3479 struct ovs_key_icmpv6 *icmpv6_key;
3481 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3482 sizeof *icmpv6_key);
3483 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3484 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3486 if (flow->tp_dst == htons(0)
3487 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3488 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3489 && (!export_mask || (data->tp_src == htons(0xffff)
3490 && data->tp_dst == htons(0xffff)))) {
3492 struct ovs_key_nd *nd_key;
3494 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3496 memcpy(nd_key->nd_target, &data->nd_target,
3497 sizeof nd_key->nd_target);
3498 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3499 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3506 nl_msg_end_nested(buf, encap);
3510 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3512 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3513 * capable of being expanded to allow for that much space. */
3515 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3518 odp_flow_key_from_flow__(parms, false, buf);
3521 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3524 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3525 * capable of being expanded to allow for that much space. */
3527 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3530 odp_flow_key_from_flow__(parms, true, buf);
3533 /* Generate ODP flow key from the given packet metadata */
3535 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3537 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3539 if (md->tunnel.ip_dst) {
3540 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3543 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3545 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3546 * value "ODPP_NONE". */
3547 if (md->in_port.odp_port != ODPP_NONE) {
3548 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3552 /* Generate packet metadata from the given ODP flow key. */
3554 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3555 struct pkt_metadata *md)
3557 const struct nlattr *nla;
3559 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3560 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3561 1u << OVS_KEY_ATTR_IN_PORT;
3563 pkt_metadata_init(md, ODPP_NONE);
3565 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3566 uint16_t type = nl_attr_type(nla);
3567 size_t len = nl_attr_get_size(nla);
3568 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3569 OVS_KEY_ATTR_MAX, type);
3571 if (len != expected_len && expected_len >= 0) {
3576 case OVS_KEY_ATTR_RECIRC_ID:
3577 md->recirc_id = nl_attr_get_u32(nla);
3578 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3580 case OVS_KEY_ATTR_DP_HASH:
3581 md->dp_hash = nl_attr_get_u32(nla);
3582 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3584 case OVS_KEY_ATTR_PRIORITY:
3585 md->skb_priority = nl_attr_get_u32(nla);
3586 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3588 case OVS_KEY_ATTR_SKB_MARK:
3589 md->pkt_mark = nl_attr_get_u32(nla);
3590 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3592 case OVS_KEY_ATTR_TUNNEL: {
3593 enum odp_key_fitness res;
3595 res = odp_tun_key_from_attr(nla, true, &md->tunnel);
3596 if (res == ODP_FIT_ERROR) {
3597 memset(&md->tunnel, 0, sizeof md->tunnel);
3598 } else if (res == ODP_FIT_PERFECT) {
3599 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3603 case OVS_KEY_ATTR_IN_PORT:
3604 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3605 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3611 if (!wanted_attrs) {
3612 return; /* Have everything. */
3618 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3620 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3621 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3622 key_len / sizeof(uint32_t), 0);
3626 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3627 uint64_t attrs, int out_of_range_attr,
3628 const struct nlattr *key, size_t key_len)
3633 if (VLOG_DROP_DBG(rl)) {
3638 for (i = 0; i < 64; i++) {
3639 if (attrs & (UINT64_C(1) << i)) {
3640 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3642 ds_put_format(&s, " %s",
3643 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3646 if (out_of_range_attr) {
3647 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3650 ds_put_cstr(&s, ": ");
3651 odp_flow_key_format(key, key_len, &s);
3653 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3658 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3660 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3663 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3666 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3667 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3668 return 0xff; /* Error. */
3671 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3672 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3673 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3677 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3678 const struct nlattr *attrs[], uint64_t *present_attrsp,
3679 int *out_of_range_attrp)
3681 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3682 const struct nlattr *nla;
3683 uint64_t present_attrs;
3686 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3688 *out_of_range_attrp = 0;
3689 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3690 uint16_t type = nl_attr_type(nla);
3691 size_t len = nl_attr_get_size(nla);
3692 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3693 OVS_KEY_ATTR_MAX, type);
3695 if (len != expected_len && expected_len >= 0) {
3696 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3698 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3699 "length %d", ovs_key_attr_to_string(type, namebuf,
3705 if (type > OVS_KEY_ATTR_MAX) {
3706 *out_of_range_attrp = type;
3708 if (present_attrs & (UINT64_C(1) << type)) {
3709 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3711 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3712 ovs_key_attr_to_string(type,
3713 namebuf, sizeof namebuf));
3717 present_attrs |= UINT64_C(1) << type;
3722 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3726 *present_attrsp = present_attrs;
3730 static enum odp_key_fitness
3731 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3732 uint64_t expected_attrs,
3733 const struct nlattr *key, size_t key_len)
3735 uint64_t missing_attrs;
3736 uint64_t extra_attrs;
3738 missing_attrs = expected_attrs & ~present_attrs;
3739 if (missing_attrs) {
3740 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3741 log_odp_key_attributes(&rl, "expected but not present",
3742 missing_attrs, 0, key, key_len);
3743 return ODP_FIT_TOO_LITTLE;
3746 extra_attrs = present_attrs & ~expected_attrs;
3747 if (extra_attrs || out_of_range_attr) {
3748 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3749 log_odp_key_attributes(&rl, "present but not expected",
3750 extra_attrs, out_of_range_attr, key, key_len);
3751 return ODP_FIT_TOO_MUCH;
3754 return ODP_FIT_PERFECT;
3758 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3759 uint64_t present_attrs, uint64_t *expected_attrs,
3760 struct flow *flow, const struct flow *src_flow)
3762 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3763 bool is_mask = flow != src_flow;
3765 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3766 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3767 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3768 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3769 ntohs(flow->dl_type));
3772 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3773 flow->dl_type != htons(0xffff)) {
3776 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3779 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3780 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3781 /* See comments in odp_flow_key_from_flow__(). */
3782 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3789 static enum odp_key_fitness
3790 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3791 uint64_t present_attrs, int out_of_range_attr,
3792 uint64_t expected_attrs, struct flow *flow,
3793 const struct nlattr *key, size_t key_len,
3794 const struct flow *src_flow)
3796 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3797 bool is_mask = src_flow != flow;
3798 const void *check_start = NULL;
3799 size_t check_len = 0;
3800 enum ovs_key_attr expected_bit = 0xff;
3802 if (eth_type_mpls(src_flow->dl_type)) {
3803 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3804 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3806 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3807 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3808 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3809 int n = size / sizeof(ovs_be32);
3812 if (!size || size % sizeof(ovs_be32)) {
3813 return ODP_FIT_ERROR;
3815 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3816 return ODP_FIT_ERROR;
3819 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3820 flow->mpls_lse[i] = mpls_lse[i];
3822 if (n > FLOW_MAX_MPLS_LABELS) {
3823 return ODP_FIT_TOO_MUCH;
3827 /* BOS may be set only in the innermost label. */
3828 for (i = 0; i < n - 1; i++) {
3829 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3830 return ODP_FIT_ERROR;
3834 /* BOS must be set in the innermost label. */
3835 if (n < FLOW_MAX_MPLS_LABELS
3836 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3837 return ODP_FIT_TOO_LITTLE;
3843 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3845 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3847 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3848 const struct ovs_key_ipv4 *ipv4_key;
3850 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3851 put_ipv4_key(ipv4_key, flow, is_mask);
3852 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3853 return ODP_FIT_ERROR;
3856 check_start = ipv4_key;
3857 check_len = sizeof *ipv4_key;
3858 expected_bit = OVS_KEY_ATTR_IPV4;
3861 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3863 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3865 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3866 const struct ovs_key_ipv6 *ipv6_key;
3868 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3869 put_ipv6_key(ipv6_key, flow, is_mask);
3870 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3871 return ODP_FIT_ERROR;
3874 check_start = ipv6_key;
3875 check_len = sizeof *ipv6_key;
3876 expected_bit = OVS_KEY_ATTR_IPV6;
3879 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3880 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3882 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3884 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3885 const struct ovs_key_arp *arp_key;
3887 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3888 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3889 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3890 "key", ntohs(arp_key->arp_op));
3891 return ODP_FIT_ERROR;
3893 put_arp_key(arp_key, flow);
3895 check_start = arp_key;
3896 check_len = sizeof *arp_key;
3897 expected_bit = OVS_KEY_ATTR_ARP;
3903 if (check_len > 0) { /* Happens only when 'is_mask'. */
3904 if (!is_all_zeros(check_start, check_len) &&
3905 flow->dl_type != htons(0xffff)) {
3906 return ODP_FIT_ERROR;
3908 expected_attrs |= UINT64_C(1) << expected_bit;
3912 expected_bit = OVS_KEY_ATTR_UNSPEC;
3913 if (src_flow->nw_proto == IPPROTO_TCP
3914 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3915 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3916 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3918 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3920 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3921 const union ovs_key_tp *tcp_key;
3923 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3924 put_tp_key(tcp_key, flow);
3925 expected_bit = OVS_KEY_ATTR_TCP;
3927 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3928 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3929 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3931 } else if (src_flow->nw_proto == IPPROTO_UDP
3932 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3933 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3934 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3936 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3938 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3939 const union ovs_key_tp *udp_key;
3941 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3942 put_tp_key(udp_key, flow);
3943 expected_bit = OVS_KEY_ATTR_UDP;
3945 } else if (src_flow->nw_proto == IPPROTO_SCTP
3946 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3947 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3948 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3950 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3952 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3953 const union ovs_key_tp *sctp_key;
3955 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3956 put_tp_key(sctp_key, flow);
3957 expected_bit = OVS_KEY_ATTR_SCTP;
3959 } else if (src_flow->nw_proto == IPPROTO_ICMP
3960 && src_flow->dl_type == htons(ETH_TYPE_IP)
3961 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3963 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3965 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3966 const struct ovs_key_icmp *icmp_key;
3968 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3969 flow->tp_src = htons(icmp_key->icmp_type);
3970 flow->tp_dst = htons(icmp_key->icmp_code);
3971 expected_bit = OVS_KEY_ATTR_ICMP;
3973 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3974 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3975 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3977 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3979 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3980 const struct ovs_key_icmpv6 *icmpv6_key;
3982 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3983 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3984 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3985 expected_bit = OVS_KEY_ATTR_ICMPV6;
3986 if (src_flow->tp_dst == htons(0) &&
3987 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3988 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3990 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3992 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3993 const struct ovs_key_nd *nd_key;
3995 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3996 memcpy(&flow->nd_target, nd_key->nd_target,
3997 sizeof flow->nd_target);
3998 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3999 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4001 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4002 (flow->tp_src != htons(0xffff) ||
4003 flow->tp_dst != htons(0xffff))) {
4004 return ODP_FIT_ERROR;
4006 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4013 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4014 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4015 return ODP_FIT_ERROR;
4017 expected_attrs |= UINT64_C(1) << expected_bit;
4022 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4026 /* Parse 802.1Q header then encapsulated L3 attributes. */
4027 static enum odp_key_fitness
4028 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4029 uint64_t present_attrs, int out_of_range_attr,
4030 uint64_t expected_attrs, struct flow *flow,
4031 const struct nlattr *key, size_t key_len,
4032 const struct flow *src_flow)
4034 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4035 bool is_mask = src_flow != flow;
4037 const struct nlattr *encap
4038 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4039 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4040 enum odp_key_fitness encap_fitness;
4041 enum odp_key_fitness fitness;
4043 /* Calculate fitness of outer attributes. */
4045 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4046 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4048 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4049 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4051 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4052 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4055 fitness = check_expectations(present_attrs, out_of_range_attr,
4056 expected_attrs, key, key_len);
4059 * Remove the TPID from dl_type since it's not the real Ethertype. */
4060 flow->dl_type = htons(0);
4061 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4062 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4065 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4066 return ODP_FIT_TOO_LITTLE;
4067 } else if (flow->vlan_tci == htons(0)) {
4068 /* Corner case for a truncated 802.1Q header. */
4069 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4070 return ODP_FIT_TOO_MUCH;
4073 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4074 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4075 "but CFI bit is not set", ntohs(flow->vlan_tci));
4076 return ODP_FIT_ERROR;
4079 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4084 /* Now parse the encapsulated attributes. */
4085 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4086 attrs, &present_attrs, &out_of_range_attr)) {
4087 return ODP_FIT_ERROR;
4091 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4092 return ODP_FIT_ERROR;
4094 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4095 expected_attrs, flow, key, key_len,
4098 /* The overall fitness is the worse of the outer and inner attributes. */
4099 return MAX(fitness, encap_fitness);
4102 static enum odp_key_fitness
4103 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4104 const struct nlattr *src_key, size_t src_key_len,
4105 struct flow *flow, const struct flow *src_flow,
4108 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4109 uint64_t expected_attrs;
4110 uint64_t present_attrs;
4111 int out_of_range_attr;
4112 bool is_mask = src_flow != flow;
4114 memset(flow, 0, sizeof *flow);
4116 /* Parse attributes. */
4117 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4118 &out_of_range_attr)) {
4119 return ODP_FIT_ERROR;
4124 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4125 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4126 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4127 } else if (is_mask) {
4128 /* Always exact match recirc_id if it is not specified. */
4129 flow->recirc_id = UINT32_MAX;
4132 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4133 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4134 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4136 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4137 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4138 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4141 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4142 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4143 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4146 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4147 enum odp_key_fitness res;
4149 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL],
4150 is_mask ? src_key : NULL,
4151 src_key_len, &src_flow->tunnel,
4152 &flow->tunnel, udpif);
4153 if (res == ODP_FIT_ERROR) {
4154 return ODP_FIT_ERROR;
4155 } else if (res == ODP_FIT_PERFECT) {
4156 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4160 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4161 flow->in_port.odp_port
4162 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4163 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4164 } else if (!is_mask) {
4165 flow->in_port.odp_port = ODPP_NONE;
4168 /* Ethernet header. */
4169 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4170 const struct ovs_key_ethernet *eth_key;
4172 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4173 put_ethernet_key(eth_key, flow);
4175 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4179 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4182 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4183 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4185 return ODP_FIT_ERROR;
4189 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4190 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4191 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4192 expected_attrs, flow, key, key_len, src_flow);
4195 flow->vlan_tci = htons(0xffff);
4196 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4197 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4198 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4201 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4202 expected_attrs, flow, key, key_len, src_flow);
4205 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4206 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4207 * 'key' fits our expectations for what a flow key should contain.
4209 * The 'in_port' will be the datapath's understanding of the port. The
4210 * caller will need to translate with odp_port_to_ofp_port() if the
4211 * OpenFlow port is needed.
4213 * This function doesn't take the packet itself as an argument because none of
4214 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4215 * it is always possible to infer which additional attribute(s) should appear
4216 * by looking at the attributes for lower-level protocols, e.g. if the network
4217 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4218 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4219 * must be absent. */
4220 enum odp_key_fitness
4221 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4224 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, false);
4227 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4228 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
4229 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4230 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
4231 * well 'key' fits our expectations for what a flow key should contain. */
4232 enum odp_key_fitness
4233 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4234 const struct nlattr *flow_key, size_t flow_key_len,
4235 struct flow *mask, const struct flow *flow)
4237 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4241 /* These functions are similar to their non-"_udpif" variants but output a
4242 * 'flow' that is suitable for fast-path packet processing.
4244 * Some fields have different representation for flow setup and per-
4245 * packet processing (i.e. different between ofproto-dpif and userspace
4246 * datapath). In particular, with the non-"_udpif" functions, struct
4247 * tun_metadata is in the per-flow format (using 'present.map' and 'opts.u8');
4248 * with these functions, struct tun_metadata is in the per-packet format
4249 * (using 'present.len' and 'opts.gnv'). */
4250 enum odp_key_fitness
4251 odp_flow_key_to_flow_udpif(const struct nlattr *key, size_t key_len,
4254 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, true);
4257 enum odp_key_fitness
4258 odp_flow_key_to_mask_udpif(const struct nlattr *mask_key, size_t mask_key_len,
4259 const struct nlattr *flow_key, size_t flow_key_len,
4260 struct flow *mask, const struct flow *flow)
4262 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4266 /* Returns 'fitness' as a string, for use in debug messages. */
4268 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4271 case ODP_FIT_PERFECT:
4273 case ODP_FIT_TOO_MUCH:
4275 case ODP_FIT_TOO_LITTLE:
4276 return "too_little";
4284 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4285 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4286 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4287 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4288 * null, then the return value is not meaningful.) */
4290 odp_put_userspace_action(uint32_t pid,
4291 const void *userdata, size_t userdata_size,
4292 odp_port_t tunnel_out_port,
4293 bool include_actions,
4294 struct ofpbuf *odp_actions)
4296 size_t userdata_ofs;
4299 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4300 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4302 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4304 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4305 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4308 * - The kernel rejected shorter userdata with -ERANGE.
4310 * - The kernel silently dropped userdata beyond the first 8 bytes.
4312 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4313 * separately disable features that required more than 8 bytes.) */
4314 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4315 MAX(8, userdata_size)),
4316 userdata, userdata_size);
4320 if (tunnel_out_port != ODPP_NONE) {
4321 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4324 if (include_actions) {
4325 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
4327 nl_msg_end_nested(odp_actions, offset);
4329 return userdata_ofs;
4333 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4334 struct ofpbuf *odp_actions)
4336 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4337 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4338 nl_msg_end_nested(odp_actions, offset);
4342 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4343 struct ovs_action_push_tnl *data)
4345 int size = offsetof(struct ovs_action_push_tnl, header);
4347 size += data->header_len;
4348 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4352 /* The commit_odp_actions() function and its helpers. */
4355 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4356 const void *key, size_t key_size)
4358 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4359 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4360 nl_msg_end_nested(odp_actions, offset);
4363 /* Masked set actions have a mask following the data within the netlink
4364 * attribute. The unmasked bits in the data will be cleared as the data
4365 * is copied to the action. */
4367 commit_masked_set_action(struct ofpbuf *odp_actions,
4368 enum ovs_key_attr key_type,
4369 const void *key_, const void *mask_, size_t key_size)
4371 size_t offset = nl_msg_start_nested(odp_actions,
4372 OVS_ACTION_ATTR_SET_MASKED);
4373 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4374 const char *key = key_, *mask = mask_;
4376 memcpy(data + key_size, mask, key_size);
4377 /* Clear unmasked bits while copying. */
4378 while (key_size--) {
4379 *data++ = *key++ & *mask++;
4381 nl_msg_end_nested(odp_actions, offset);
4384 /* If any of the flow key data that ODP actions can modify are different in
4385 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4386 * 'odp_actions' that change the flow tunneling information in key from
4387 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4388 * same way. In other words, operates the same as commit_odp_actions(), but
4389 * only on tunneling information. */
4391 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4392 struct ofpbuf *odp_actions)
4394 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4395 if (flow->tunnel.ip_dst) {
4396 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4399 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4400 odp_put_tunnel_action(&base->tunnel, odp_actions);
4405 commit(enum ovs_key_attr attr, bool use_masked_set,
4406 const void *key, void *base, void *mask, size_t size,
4407 struct ofpbuf *odp_actions)
4409 if (memcmp(key, base, size)) {
4410 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4412 if (use_masked_set && !fully_masked) {
4413 commit_masked_set_action(odp_actions, attr, key, mask, size);
4415 if (!fully_masked) {
4416 memset(mask, 0xff, size);
4418 commit_set_action(odp_actions, attr, key, size);
4420 memcpy(base, key, size);
4423 /* Mask bits are set when we have either read or set the corresponding
4424 * values. Masked bits will be exact-matched, no need to set them
4425 * if the value did not actually change. */
4431 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4433 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4434 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4438 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4440 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4441 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4445 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4446 struct ofpbuf *odp_actions,
4447 struct flow_wildcards *wc,
4450 struct ovs_key_ethernet key, base, mask;
4452 get_ethernet_key(flow, &key);
4453 get_ethernet_key(base_flow, &base);
4454 get_ethernet_key(&wc->masks, &mask);
4456 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4457 &key, &base, &mask, sizeof key, odp_actions)) {
4458 put_ethernet_key(&base, base_flow);
4459 put_ethernet_key(&mask, &wc->masks);
4464 pop_vlan(struct flow *base,
4465 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4467 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4469 if (base->vlan_tci & htons(VLAN_CFI)) {
4470 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4476 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4477 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4479 if (base->vlan_tci == vlan_tci) {
4483 pop_vlan(base, odp_actions, wc);
4484 if (vlan_tci & htons(VLAN_CFI)) {
4485 struct ovs_action_push_vlan vlan;
4487 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4488 vlan.vlan_tci = vlan_tci;
4489 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4490 &vlan, sizeof vlan);
4492 base->vlan_tci = vlan_tci;
4495 /* Wildcarding already done at action translation time. */
4497 commit_mpls_action(const struct flow *flow, struct flow *base,
4498 struct ofpbuf *odp_actions)
4500 int base_n = flow_count_mpls_labels(base, NULL);
4501 int flow_n = flow_count_mpls_labels(flow, NULL);
4502 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4505 while (base_n > common_n) {
4506 if (base_n - 1 == common_n && flow_n > common_n) {
4507 /* If there is only one more LSE in base than there are common
4508 * between base and flow; and flow has at least one more LSE than
4509 * is common then the topmost LSE of base may be updated using
4511 struct ovs_key_mpls mpls_key;
4513 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4514 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4515 &mpls_key, sizeof mpls_key);
4516 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4519 /* Otherwise, if there more LSEs in base than are common between
4520 * base and flow then pop the topmost one. */
4524 /* If all the LSEs are to be popped and this is not the outermost
4525 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4526 * POP_MPLS action instead of flow->dl_type.
4528 * This is because the POP_MPLS action requires its ethertype
4529 * argument to be an MPLS ethernet type but in this case
4530 * flow->dl_type will be a non-MPLS ethernet type.
4532 * When the final POP_MPLS action occurs it use flow->dl_type and
4533 * the and the resulting packet will have the desired dl_type. */
4534 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4535 dl_type = htons(ETH_TYPE_MPLS);
4537 dl_type = flow->dl_type;
4539 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4540 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4546 /* If, after the above popping and setting, there are more LSEs in flow
4547 * than base then some LSEs need to be pushed. */
4548 while (base_n < flow_n) {
4549 struct ovs_action_push_mpls *mpls;
4551 mpls = nl_msg_put_unspec_zero(odp_actions,
4552 OVS_ACTION_ATTR_PUSH_MPLS,
4554 mpls->mpls_ethertype = flow->dl_type;
4555 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4556 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4557 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4563 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4565 ipv4->ipv4_src = flow->nw_src;
4566 ipv4->ipv4_dst = flow->nw_dst;
4567 ipv4->ipv4_proto = flow->nw_proto;
4568 ipv4->ipv4_tos = flow->nw_tos;
4569 ipv4->ipv4_ttl = flow->nw_ttl;
4570 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4574 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4576 flow->nw_src = ipv4->ipv4_src;
4577 flow->nw_dst = ipv4->ipv4_dst;
4578 flow->nw_proto = ipv4->ipv4_proto;
4579 flow->nw_tos = ipv4->ipv4_tos;
4580 flow->nw_ttl = ipv4->ipv4_ttl;
4581 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4585 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4586 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4589 struct ovs_key_ipv4 key, mask, base;
4591 /* Check that nw_proto and nw_frag remain unchanged. */
4592 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4593 flow->nw_frag == base_flow->nw_frag);
4595 get_ipv4_key(flow, &key, false);
4596 get_ipv4_key(base_flow, &base, false);
4597 get_ipv4_key(&wc->masks, &mask, true);
4598 mask.ipv4_proto = 0; /* Not writeable. */
4599 mask.ipv4_frag = 0; /* Not writable. */
4601 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4603 put_ipv4_key(&base, base_flow, false);
4604 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4605 put_ipv4_key(&mask, &wc->masks, true);
4611 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4613 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4614 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4615 ipv6->ipv6_label = flow->ipv6_label;
4616 ipv6->ipv6_proto = flow->nw_proto;
4617 ipv6->ipv6_tclass = flow->nw_tos;
4618 ipv6->ipv6_hlimit = flow->nw_ttl;
4619 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4623 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4625 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4626 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4627 flow->ipv6_label = ipv6->ipv6_label;
4628 flow->nw_proto = ipv6->ipv6_proto;
4629 flow->nw_tos = ipv6->ipv6_tclass;
4630 flow->nw_ttl = ipv6->ipv6_hlimit;
4631 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4635 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4636 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4639 struct ovs_key_ipv6 key, mask, base;
4641 /* Check that nw_proto and nw_frag remain unchanged. */
4642 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4643 flow->nw_frag == base_flow->nw_frag);
4645 get_ipv6_key(flow, &key, false);
4646 get_ipv6_key(base_flow, &base, false);
4647 get_ipv6_key(&wc->masks, &mask, true);
4648 mask.ipv6_proto = 0; /* Not writeable. */
4649 mask.ipv6_frag = 0; /* Not writable. */
4651 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4653 put_ipv6_key(&base, base_flow, false);
4654 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4655 put_ipv6_key(&mask, &wc->masks, true);
4661 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4663 /* ARP key has padding, clear it. */
4664 memset(arp, 0, sizeof *arp);
4666 arp->arp_sip = flow->nw_src;
4667 arp->arp_tip = flow->nw_dst;
4668 arp->arp_op = htons(flow->nw_proto);
4669 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4670 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4674 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4676 flow->nw_src = arp->arp_sip;
4677 flow->nw_dst = arp->arp_tip;
4678 flow->nw_proto = ntohs(arp->arp_op);
4679 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4680 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4683 static enum slow_path_reason
4684 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4685 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4687 struct ovs_key_arp key, mask, base;
4689 get_arp_key(flow, &key);
4690 get_arp_key(base_flow, &base);
4691 get_arp_key(&wc->masks, &mask);
4693 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4695 put_arp_key(&base, base_flow);
4696 put_arp_key(&mask, &wc->masks);
4703 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4705 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4706 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4707 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4708 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4712 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4714 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4715 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4716 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4717 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4720 static enum slow_path_reason
4721 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4722 struct ofpbuf *odp_actions,
4723 struct flow_wildcards *wc, bool use_masked)
4725 struct ovs_key_nd key, mask, base;
4727 get_nd_key(flow, &key);
4728 get_nd_key(base_flow, &base);
4729 get_nd_key(&wc->masks, &mask);
4731 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4733 put_nd_key(&base, base_flow);
4734 put_nd_key(&mask, &wc->masks);
4741 static enum slow_path_reason
4742 commit_set_nw_action(const struct flow *flow, struct flow *base,
4743 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4746 /* Check if 'flow' really has an L3 header. */
4747 if (!flow->nw_proto) {
4751 switch (ntohs(base->dl_type)) {
4753 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4757 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4758 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4761 return commit_set_arp_action(flow, base, odp_actions, wc);
4767 /* TCP, UDP, and SCTP keys have the same layout. */
4768 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4769 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4772 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4774 tp->tcp.tcp_src = flow->tp_src;
4775 tp->tcp.tcp_dst = flow->tp_dst;
4779 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4781 flow->tp_src = tp->tcp.tcp_src;
4782 flow->tp_dst = tp->tcp.tcp_dst;
4786 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4787 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4790 enum ovs_key_attr key_type;
4791 union ovs_key_tp key, mask, base;
4793 /* Check if 'flow' really has an L3 header. */
4794 if (!flow->nw_proto) {
4798 if (!is_ip_any(base_flow)) {
4802 if (flow->nw_proto == IPPROTO_TCP) {
4803 key_type = OVS_KEY_ATTR_TCP;
4804 } else if (flow->nw_proto == IPPROTO_UDP) {
4805 key_type = OVS_KEY_ATTR_UDP;
4806 } else if (flow->nw_proto == IPPROTO_SCTP) {
4807 key_type = OVS_KEY_ATTR_SCTP;
4812 get_tp_key(flow, &key);
4813 get_tp_key(base_flow, &base);
4814 get_tp_key(&wc->masks, &mask);
4816 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4818 put_tp_key(&base, base_flow);
4819 put_tp_key(&mask, &wc->masks);
4824 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4825 struct ofpbuf *odp_actions,
4826 struct flow_wildcards *wc,
4829 uint32_t key, mask, base;
4831 key = flow->skb_priority;
4832 base = base_flow->skb_priority;
4833 mask = wc->masks.skb_priority;
4835 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4836 sizeof key, odp_actions)) {
4837 base_flow->skb_priority = base;
4838 wc->masks.skb_priority = mask;
4843 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4844 struct ofpbuf *odp_actions,
4845 struct flow_wildcards *wc,
4848 uint32_t key, mask, base;
4850 key = flow->pkt_mark;
4851 base = base_flow->pkt_mark;
4852 mask = wc->masks.pkt_mark;
4854 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4855 sizeof key, odp_actions)) {
4856 base_flow->pkt_mark = base;
4857 wc->masks.pkt_mark = mask;
4861 /* If any of the flow key data that ODP actions can modify are different in
4862 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4863 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4864 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4865 * in addition to this function if needed. Sets fields in 'wc' that are
4866 * used as part of the action.
4868 * Returns a reason to force processing the flow's packets into the userspace
4869 * slow path, if there is one, otherwise 0. */
4870 enum slow_path_reason
4871 commit_odp_actions(const struct flow *flow, struct flow *base,
4872 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4875 enum slow_path_reason slow;
4877 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4878 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4879 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4880 commit_mpls_action(flow, base, odp_actions);
4881 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4882 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4883 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);