2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
38 #include "unaligned.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(odp_util);
44 /* The interface between userspace and kernel uses an "OVS_*" prefix.
45 * Since this is fairly non-specific for the OVS userspace components,
46 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
47 * interactions with the datapath.
50 /* The set of characters that may separate one action or one key attribute
52 static const char *delimiters = ", \t\r\n";
54 static const char *hex_chars = "0123456789abcdefABCDEF";
56 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
57 struct ofpbuf *, struct ofpbuf *);
58 static void format_odp_key_attr(const struct nlattr *a,
59 const struct nlattr *ma,
60 const struct hmap *portno_names, struct ds *ds,
63 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
66 * - For an action whose argument has a fixed length, returned that
67 * nonnegative length in bytes.
69 * - For an action with a variable-length argument, returns -2.
71 * - For an invalid 'type', returns -1. */
73 odp_action_len(uint16_t type)
75 if (type > OVS_ACTION_ATTR_MAX) {
79 switch ((enum ovs_action_attr) type) {
80 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
81 case OVS_ACTION_ATTR_TUNNEL_PUSH: return -2;
82 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
83 case OVS_ACTION_ATTR_USERSPACE: return -2;
84 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
85 case OVS_ACTION_ATTR_POP_VLAN: return 0;
86 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
87 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
88 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
89 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
90 case OVS_ACTION_ATTR_SET: return -2;
91 case OVS_ACTION_ATTR_SET_MASKED: return -2;
92 case OVS_ACTION_ATTR_SAMPLE: return -2;
94 case OVS_ACTION_ATTR_UNSPEC:
95 case __OVS_ACTION_ATTR_MAX:
102 /* Returns a string form of 'attr'. The return value is either a statically
103 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
104 * should be at least OVS_KEY_ATTR_BUFSIZE. */
105 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
107 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
110 case OVS_KEY_ATTR_UNSPEC: return "unspec";
111 case OVS_KEY_ATTR_ENCAP: return "encap";
112 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
113 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
114 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
115 case OVS_KEY_ATTR_IN_PORT: return "in_port";
116 case OVS_KEY_ATTR_ETHERNET: return "eth";
117 case OVS_KEY_ATTR_VLAN: return "vlan";
118 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
119 case OVS_KEY_ATTR_IPV4: return "ipv4";
120 case OVS_KEY_ATTR_IPV6: return "ipv6";
121 case OVS_KEY_ATTR_TCP: return "tcp";
122 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
123 case OVS_KEY_ATTR_UDP: return "udp";
124 case OVS_KEY_ATTR_SCTP: return "sctp";
125 case OVS_KEY_ATTR_ICMP: return "icmp";
126 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
127 case OVS_KEY_ATTR_ARP: return "arp";
128 case OVS_KEY_ATTR_ND: return "nd";
129 case OVS_KEY_ATTR_MPLS: return "mpls";
130 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
131 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
133 case __OVS_KEY_ATTR_MAX:
135 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
141 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
143 size_t len = nl_attr_get_size(a);
145 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
147 const uint8_t *unspec;
150 unspec = nl_attr_get(a);
151 for (i = 0; i < len; i++) {
152 ds_put_char(ds, i ? ' ': '(');
153 ds_put_format(ds, "%02x", unspec[i]);
155 ds_put_char(ds, ')');
160 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
162 static const struct nl_policy ovs_sample_policy[] = {
163 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
164 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
166 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
168 const struct nlattr *nla_acts;
171 ds_put_cstr(ds, "sample");
173 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
174 ds_put_cstr(ds, "(error)");
178 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
181 ds_put_format(ds, "(sample=%.1f%%,", percentage);
183 ds_put_cstr(ds, "actions(");
184 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
185 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
186 format_odp_actions(ds, nla_acts, len);
187 ds_put_format(ds, "))");
191 slow_path_reason_to_string(uint32_t reason)
193 switch ((enum slow_path_reason) reason) {
194 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
203 slow_path_reason_to_explanation(enum slow_path_reason reason)
206 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
215 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
216 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
221 /* Parse masked flags in numeric format? */
222 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
223 res_flags, res_mask, &n) && n > 0) {
224 if (*res_flags & ~allowed || *res_mask & ~allowed) {
232 if (res_mask && (*s == '+' || *s == '-')) {
233 uint32_t flags = 0, mask = 0;
235 /* Parse masked flags. */
236 while (s[0] != ')') {
243 } else if (s[0] == '-') {
251 name_len = strcspn(s, "+-)");
253 for (bit = 1; bit; bit <<= 1) {
254 const char *fname = bit_to_string(bit);
262 if (len != name_len) {
265 if (!strncmp(s, fname, len)) {
267 /* bit already set. */
270 if (!(bit & allowed)) {
282 return -EINVAL; /* Unknown flag name */
293 /* Parse unmasked flags. If a flag is present, it is set, otherwise
295 while (s[n] != ')') {
296 unsigned long long int flags;
300 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
301 if (flags & ~allowed) {
304 n += n0 + (s[n + n0] == ',');
309 for (bit = 1; bit; bit <<= 1) {
310 const char *name = bit_to_string(bit);
318 if (!strncmp(s + n, name, len) &&
319 (s[n + len] == ',' || s[n + len] == ')')) {
320 if (!(bit & allowed)) {
324 n += len + (s[n + len] == ',');
336 *res_mask = UINT32_MAX;
342 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
344 static const struct nl_policy ovs_userspace_policy[] = {
345 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
346 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
348 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
351 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
352 const struct nlattr *userdata_attr;
353 const struct nlattr *tunnel_out_port_attr;
355 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
356 ds_put_cstr(ds, "userspace(error)");
360 ds_put_format(ds, "userspace(pid=%"PRIu32,
361 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
363 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
366 const uint8_t *userdata = nl_attr_get(userdata_attr);
367 size_t userdata_len = nl_attr_get_size(userdata_attr);
368 bool userdata_unspec = true;
369 union user_action_cookie cookie;
371 if (userdata_len >= sizeof cookie.type
372 && userdata_len <= sizeof cookie) {
374 memset(&cookie, 0, sizeof cookie);
375 memcpy(&cookie, userdata, userdata_len);
377 userdata_unspec = false;
379 if (userdata_len == sizeof cookie.sflow
380 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
381 ds_put_format(ds, ",sFlow("
382 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
383 vlan_tci_to_vid(cookie.sflow.vlan_tci),
384 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
385 cookie.sflow.output);
386 } else if (userdata_len == sizeof cookie.slow_path
387 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
388 ds_put_cstr(ds, ",slow_path(");
389 format_flags(ds, slow_path_reason_to_string,
390 cookie.slow_path.reason, ',');
391 ds_put_format(ds, ")");
392 } else if (userdata_len == sizeof cookie.flow_sample
393 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
394 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
395 ",collector_set_id=%"PRIu32
396 ",obs_domain_id=%"PRIu32
397 ",obs_point_id=%"PRIu32")",
398 cookie.flow_sample.probability,
399 cookie.flow_sample.collector_set_id,
400 cookie.flow_sample.obs_domain_id,
401 cookie.flow_sample.obs_point_id);
402 } else if (userdata_len >= sizeof cookie.ipfix
403 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
404 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
405 cookie.ipfix.output_odp_port);
407 userdata_unspec = true;
411 if (userdata_unspec) {
413 ds_put_format(ds, ",userdata(");
414 for (i = 0; i < userdata_len; i++) {
415 ds_put_format(ds, "%02x", userdata[i]);
417 ds_put_char(ds, ')');
421 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
422 if (tunnel_out_port_attr) {
423 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
424 nl_attr_get_u32(tunnel_out_port_attr));
427 ds_put_char(ds, ')');
431 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
433 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
434 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
435 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
436 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
438 ds_put_char(ds, ',');
440 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
441 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
442 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
443 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
445 ds_put_char(ds, ',');
447 if (!(tci & htons(VLAN_CFI))) {
448 ds_put_cstr(ds, "cfi=0");
449 ds_put_char(ds, ',');
455 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
457 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
458 mpls_lse_to_label(mpls_lse),
459 mpls_lse_to_tc(mpls_lse),
460 mpls_lse_to_ttl(mpls_lse),
461 mpls_lse_to_bos(mpls_lse));
465 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
466 const struct ovs_key_mpls *mpls_mask, int n)
469 ovs_be32 key = mpls_key->mpls_lse;
471 if (mpls_mask == NULL) {
472 format_mpls_lse(ds, key);
474 ovs_be32 mask = mpls_mask->mpls_lse;
476 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
477 mpls_lse_to_label(key), mpls_lse_to_label(mask),
478 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
479 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
480 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
485 for (i = 0; i < n; i++) {
486 ds_put_format(ds, "lse%d=%#"PRIx32,
487 i, ntohl(mpls_key[i].mpls_lse));
489 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
491 ds_put_char(ds, ',');
498 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
500 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
504 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
506 ds_put_format(ds, "hash(");
508 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
509 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
511 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
514 ds_put_format(ds, ")");
518 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
520 const struct udp_header *udp;
522 udp = (const struct udp_header *) (ip + 1);
523 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
524 ntohs(udp->udp_src), ntohs(udp->udp_dst),
525 ntohs(udp->udp_csum));
531 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
533 const struct eth_header *eth;
534 const struct ip_header *ip;
537 eth = (const struct eth_header *)data->header;
540 ip = (const struct ip_header *)l3;
543 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
544 data->header_len, data->tnl_type);
545 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
546 ds_put_format(ds, ",src=");
547 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
548 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
551 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
552 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
553 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
554 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
555 ip->ip_proto, ip->ip_tos,
559 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
560 const struct vxlanhdr *vxh;
562 vxh = format_udp_tnl_push_header(ds, ip);
564 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
565 ntohl(get_16aligned_be32(&vxh->vx_flags)),
566 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
567 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
568 const struct genevehdr *gnh;
570 gnh = format_udp_tnl_push_header(ds, ip);
572 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
573 gnh->oam ? "oam," : "",
574 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
575 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
576 const struct gre_base_hdr *greh;
577 ovs_16aligned_be32 *options;
580 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
581 greh = (const struct gre_base_hdr *) l4;
583 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
584 ntohs(greh->flags), ntohs(greh->protocol));
585 options = (ovs_16aligned_be32 *)(greh + 1);
586 if (greh->flags & htons(GRE_CSUM)) {
587 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
590 if (greh->flags & htons(GRE_KEY)) {
591 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
594 if (greh->flags & htons(GRE_SEQ)) {
595 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
598 ds_put_format(ds, ")");
600 ds_put_format(ds, ")");
604 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
606 struct ovs_action_push_tnl *data;
608 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
610 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
611 format_odp_tnl_push_header(ds, data);
612 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
616 format_odp_action(struct ds *ds, const struct nlattr *a)
619 enum ovs_action_attr type = nl_attr_type(a);
620 const struct ovs_action_push_vlan *vlan;
623 expected_len = odp_action_len(nl_attr_type(a));
624 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
625 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
626 nl_attr_get_size(a), expected_len);
627 format_generic_odp_action(ds, a);
632 case OVS_ACTION_ATTR_OUTPUT:
633 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
635 case OVS_ACTION_ATTR_TUNNEL_POP:
636 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
638 case OVS_ACTION_ATTR_TUNNEL_PUSH:
639 format_odp_tnl_push_action(ds, a);
641 case OVS_ACTION_ATTR_USERSPACE:
642 format_odp_userspace_action(ds, a);
644 case OVS_ACTION_ATTR_RECIRC:
645 format_odp_recirc_action(ds, nl_attr_get_u32(a));
647 case OVS_ACTION_ATTR_HASH:
648 format_odp_hash_action(ds, nl_attr_get(a));
650 case OVS_ACTION_ATTR_SET_MASKED:
652 size = nl_attr_get_size(a) / 2;
653 ds_put_cstr(ds, "set(");
655 /* Masked set action not supported for tunnel key, which is bigger. */
656 if (size <= sizeof(struct ovs_key_ipv6)) {
657 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
658 sizeof(struct nlattr))];
659 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
660 sizeof(struct nlattr))];
662 mask->nla_type = attr->nla_type = nl_attr_type(a);
663 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
664 memcpy(attr + 1, (char *)(a + 1), size);
665 memcpy(mask + 1, (char *)(a + 1) + size, size);
666 format_odp_key_attr(attr, mask, NULL, ds, false);
668 format_odp_key_attr(a, NULL, NULL, ds, false);
670 ds_put_cstr(ds, ")");
672 case OVS_ACTION_ATTR_SET:
673 ds_put_cstr(ds, "set(");
674 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
675 ds_put_cstr(ds, ")");
677 case OVS_ACTION_ATTR_PUSH_VLAN:
678 vlan = nl_attr_get(a);
679 ds_put_cstr(ds, "push_vlan(");
680 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
681 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
683 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
684 ds_put_char(ds, ')');
686 case OVS_ACTION_ATTR_POP_VLAN:
687 ds_put_cstr(ds, "pop_vlan");
689 case OVS_ACTION_ATTR_PUSH_MPLS: {
690 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
691 ds_put_cstr(ds, "push_mpls(");
692 format_mpls_lse(ds, mpls->mpls_lse);
693 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
696 case OVS_ACTION_ATTR_POP_MPLS: {
697 ovs_be16 ethertype = nl_attr_get_be16(a);
698 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
701 case OVS_ACTION_ATTR_SAMPLE:
702 format_odp_sample_action(ds, a);
704 case OVS_ACTION_ATTR_UNSPEC:
705 case __OVS_ACTION_ATTR_MAX:
707 format_generic_odp_action(ds, a);
713 format_odp_actions(struct ds *ds, const struct nlattr *actions,
717 const struct nlattr *a;
720 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
722 ds_put_char(ds, ',');
724 format_odp_action(ds, a);
729 if (left == actions_len) {
730 ds_put_cstr(ds, "<empty>");
732 ds_put_format(ds, ",***%u leftover bytes*** (", left);
733 for (i = 0; i < left; i++) {
734 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
736 ds_put_char(ds, ')');
739 ds_put_cstr(ds, "drop");
743 /* Separate out parse_odp_userspace_action() function. */
745 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
748 union user_action_cookie cookie;
750 odp_port_t tunnel_out_port;
752 void *user_data = NULL;
753 size_t user_data_size = 0;
755 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
761 uint32_t probability;
762 uint32_t collector_set_id;
763 uint32_t obs_domain_id;
764 uint32_t obs_point_id;
767 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
768 "pcp=%i,output=%"SCNi32")%n",
769 &vid, &pcp, &output, &n1)) {
773 tci = vid | (pcp << VLAN_PCP_SHIFT);
778 cookie.type = USER_ACTION_COOKIE_SFLOW;
779 cookie.sflow.vlan_tci = htons(tci);
780 cookie.sflow.output = output;
782 user_data_size = sizeof cookie.sflow;
783 } else if (ovs_scan(&s[n], ",slow_path(%n",
788 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
789 cookie.slow_path.unused = 0;
790 cookie.slow_path.reason = 0;
792 res = parse_flags(&s[n], slow_path_reason_to_string,
793 &cookie.slow_path.reason,
794 SLOW_PATH_REASON_MASK, NULL);
795 if (res < 0 || s[n + res] != ')') {
801 user_data_size = sizeof cookie.slow_path;
802 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
803 "collector_set_id=%"SCNi32","
804 "obs_domain_id=%"SCNi32","
805 "obs_point_id=%"SCNi32")%n",
806 &probability, &collector_set_id,
807 &obs_domain_id, &obs_point_id, &n1)) {
810 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
811 cookie.flow_sample.probability = probability;
812 cookie.flow_sample.collector_set_id = collector_set_id;
813 cookie.flow_sample.obs_domain_id = obs_domain_id;
814 cookie.flow_sample.obs_point_id = obs_point_id;
816 user_data_size = sizeof cookie.flow_sample;
817 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
820 cookie.type = USER_ACTION_COOKIE_IPFIX;
821 cookie.ipfix.output_odp_port = u32_to_odp(output);
823 user_data_size = sizeof cookie.ipfix;
824 } else if (ovs_scan(&s[n], ",userdata(%n",
829 ofpbuf_init(&buf, 16);
830 end = ofpbuf_put_hex(&buf, &s[n], NULL);
834 user_data = buf.data;
835 user_data_size = buf.size;
842 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
843 &tunnel_out_port, &n1)) {
844 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
846 } else if (s[n] == ')') {
847 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
856 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
858 struct eth_header *eth;
859 struct ip_header *ip;
860 struct udp_header *udp;
861 struct gre_base_hdr *greh;
862 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
864 uint32_t tnl_type = 0, header_len = 0;
868 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
871 eth = (struct eth_header *) data->header;
872 l3 = (data->header + sizeof *eth);
873 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
874 ip = (struct ip_header *) l3;
875 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
876 "eth(dst="ETH_ADDR_SCAN_FMT",",
879 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
883 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
884 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
887 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
890 eth->eth_type = htons(dl_type);
893 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
894 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
897 &ip->ip_proto, &ip->ip_tos,
898 &ip->ip_ttl, &ip->ip_frag_off)) {
901 put_16aligned_be32(&ip->ip_src, sip);
902 put_16aligned_be32(&ip->ip_dst, dip);
905 udp = (struct udp_header *) l4;
906 greh = (struct gre_base_hdr *) l4;
907 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
908 &udp_src, &udp_dst, &csum)) {
909 uint32_t vx_flags, vni;
911 udp->udp_src = htons(udp_src);
912 udp->udp_dst = htons(udp_dst);
914 udp->udp_csum = htons(csum);
916 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
918 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
920 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
921 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
922 tnl_type = OVS_VPORT_TYPE_VXLAN;
923 header_len = sizeof *eth + sizeof *ip +
924 sizeof *udp + sizeof *vxh;
925 } else if (ovs_scan_len(s, &n, "geneve(")) {
926 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
928 memset(gnh, 0, sizeof *gnh);
929 if (ovs_scan_len(s, &n, "oam,")) {
932 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
935 gnh->proto_type = htons(ETH_TYPE_TEB);
936 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
937 tnl_type = OVS_VPORT_TYPE_GENEVE;
938 header_len = sizeof *eth + sizeof *ip +
939 sizeof *udp + sizeof *gnh;
943 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
944 &gre_flags, &gre_proto)){
946 tnl_type = OVS_VPORT_TYPE_GRE;
947 greh->flags = htons(gre_flags);
948 greh->protocol = htons(gre_proto);
949 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
951 if (greh->flags & htons(GRE_CSUM)) {
952 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
956 memset(options, 0, sizeof *options);
957 *((ovs_be16 *)options) = htons(csum);
960 if (greh->flags & htons(GRE_KEY)) {
963 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
967 put_16aligned_be32(options, htonl(key));
970 if (greh->flags & htons(GRE_SEQ)) {
973 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
976 put_16aligned_be32(options, htonl(seq));
980 if (!ovs_scan_len(s, &n, "))")) {
984 header_len = sizeof *eth + sizeof *ip +
985 ((uint8_t *) options - (uint8_t *) greh);
990 /* check tunnel meta data. */
991 if (data->tnl_type != tnl_type) {
994 if (data->header_len != header_len) {
999 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1007 parse_odp_action(const char *s, const struct simap *port_names,
1008 struct ofpbuf *actions)
1014 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1015 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1021 int len = strcspn(s, delimiters);
1022 struct simap_node *node;
1024 node = simap_find_len(port_names, s, len);
1026 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1035 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1036 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1041 if (!strncmp(s, "userspace(", 10)) {
1042 return parse_odp_userspace_action(s, actions);
1045 if (!strncmp(s, "set(", 4)) {
1048 struct nlattr mask[128 / sizeof(struct nlattr)];
1049 struct ofpbuf maskbuf;
1050 struct nlattr *nested, *key;
1053 /* 'mask' is big enough to hold any key. */
1054 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1056 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1057 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1061 if (s[retval + 4] != ')') {
1065 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1068 size = nl_attr_get_size(mask);
1069 if (size == nl_attr_get_size(key)) {
1070 /* Change to masked set action if not fully masked. */
1071 if (!is_all_ones(mask + 1, size)) {
1072 key->nla_len += size;
1073 ofpbuf_put(actions, mask + 1, size);
1074 /* 'actions' may have been reallocated by ofpbuf_put(). */
1075 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1076 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1080 nl_msg_end_nested(actions, start_ofs);
1085 struct ovs_action_push_vlan push;
1086 int tpid = ETH_TYPE_VLAN;
1091 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1092 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1093 &vid, &pcp, &cfi, &n)
1094 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1095 &tpid, &vid, &pcp, &n)
1096 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1097 &tpid, &vid, &pcp, &cfi, &n)) {
1098 push.vlan_tpid = htons(tpid);
1099 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1100 | (pcp << VLAN_PCP_SHIFT)
1101 | (cfi ? VLAN_CFI : 0));
1102 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1103 &push, sizeof push);
1109 if (!strncmp(s, "pop_vlan", 8)) {
1110 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1118 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1119 && percentage >= 0. && percentage <= 100.0) {
1120 size_t sample_ofs, actions_ofs;
1123 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1124 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1125 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1126 (probability <= 0 ? 0
1127 : probability >= UINT32_MAX ? UINT32_MAX
1130 actions_ofs = nl_msg_start_nested(actions,
1131 OVS_SAMPLE_ATTR_ACTIONS);
1135 n += strspn(s + n, delimiters);
1140 retval = parse_odp_action(s + n, port_names, actions);
1146 nl_msg_end_nested(actions, actions_ofs);
1147 nl_msg_end_nested(actions, sample_ofs);
1149 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1157 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1158 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1164 struct ovs_action_push_tnl data;
1167 n = ovs_parse_tnl_push(s, &data);
1169 odp_put_tnl_push_action(actions, &data);
1178 /* Parses the string representation of datapath actions, in the format output
1179 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1180 * value. On success, the ODP actions are appended to 'actions' as a series of
1181 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1182 * way, 'actions''s data might be reallocated. */
1184 odp_actions_from_string(const char *s, const struct simap *port_names,
1185 struct ofpbuf *actions)
1189 if (!strcasecmp(s, "drop")) {
1193 old_size = actions->size;
1197 s += strspn(s, delimiters);
1202 retval = parse_odp_action(s, port_names, actions);
1203 if (retval < 0 || !strchr(delimiters, s[retval])) {
1204 actions->size = old_size;
1213 /* Returns the correct length of the payload for a flow key attribute of the
1214 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
1215 * is variable length. */
1217 odp_flow_key_attr_len(uint16_t type)
1219 if (type > OVS_KEY_ATTR_MAX) {
1223 switch ((enum ovs_key_attr) type) {
1224 case OVS_KEY_ATTR_ENCAP: return -2;
1225 case OVS_KEY_ATTR_PRIORITY: return 4;
1226 case OVS_KEY_ATTR_SKB_MARK: return 4;
1227 case OVS_KEY_ATTR_DP_HASH: return 4;
1228 case OVS_KEY_ATTR_RECIRC_ID: return 4;
1229 case OVS_KEY_ATTR_TUNNEL: return -2;
1230 case OVS_KEY_ATTR_IN_PORT: return 4;
1231 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
1232 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
1233 case OVS_KEY_ATTR_ETHERTYPE: return 2;
1234 case OVS_KEY_ATTR_MPLS: return -2;
1235 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
1236 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
1237 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
1238 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
1239 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
1240 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
1241 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
1242 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
1243 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
1244 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
1246 case OVS_KEY_ATTR_UNSPEC:
1247 case __OVS_KEY_ATTR_MAX:
1255 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1257 size_t len = nl_attr_get_size(a);
1259 const uint8_t *unspec;
1262 unspec = nl_attr_get(a);
1263 for (i = 0; i < len; i++) {
1265 ds_put_char(ds, ' ');
1267 ds_put_format(ds, "%02x", unspec[i]);
1273 ovs_frag_type_to_string(enum ovs_frag_type type)
1276 case OVS_FRAG_TYPE_NONE:
1278 case OVS_FRAG_TYPE_FIRST:
1280 case OVS_FRAG_TYPE_LATER:
1282 case __OVS_FRAG_TYPE_MAX:
1289 tunnel_key_attr_len(int type)
1292 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
1293 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
1294 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1295 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1296 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1297 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1298 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1299 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1300 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1301 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1302 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1303 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: return -2;
1304 case __OVS_TUNNEL_KEY_ATTR_MAX:
1310 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1312 parse_geneve_opts(const struct nlattr *attr)
1314 int opts_len = nl_attr_get_size(attr);
1315 const struct geneve_opt *opt = nl_attr_get(attr);
1317 while (opts_len > 0) {
1320 if (opts_len < sizeof(*opt)) {
1324 len = sizeof(*opt) + opt->length * 4;
1325 if (len > opts_len) {
1329 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1331 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1336 opt = opt + len / sizeof(*opt);
1343 enum odp_key_fitness
1344 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1347 const struct nlattr *a;
1349 bool unknown = false;
1351 NL_NESTED_FOR_EACH(a, left, attr) {
1352 uint16_t type = nl_attr_type(a);
1353 size_t len = nl_attr_get_size(a);
1354 int expected_len = tunnel_key_attr_len(type);
1356 if (len != expected_len && expected_len >= 0) {
1357 return ODP_FIT_ERROR;
1361 case OVS_TUNNEL_KEY_ATTR_ID:
1362 tun->tun_id = nl_attr_get_be64(a);
1363 tun->flags |= FLOW_TNL_F_KEY;
1365 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1366 tun->ip_src = nl_attr_get_be32(a);
1368 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1369 tun->ip_dst = nl_attr_get_be32(a);
1371 case OVS_TUNNEL_KEY_ATTR_TOS:
1372 tun->ip_tos = nl_attr_get_u8(a);
1374 case OVS_TUNNEL_KEY_ATTR_TTL:
1375 tun->ip_ttl = nl_attr_get_u8(a);
1378 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1379 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1381 case OVS_TUNNEL_KEY_ATTR_CSUM:
1382 tun->flags |= FLOW_TNL_F_CSUM;
1384 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1385 tun->tp_src = nl_attr_get_be16(a);
1387 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1388 tun->tp_dst = nl_attr_get_be16(a);
1390 case OVS_TUNNEL_KEY_ATTR_OAM:
1391 tun->flags |= FLOW_TNL_F_OAM;
1393 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1394 static const struct nl_policy vxlan_opts_policy[] = {
1395 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1397 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1399 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1400 return ODP_FIT_ERROR;
1403 if (ext[OVS_VXLAN_EXT_GBP]) {
1404 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1406 tun->gbp_id = htons(gbp & 0xFFFF);
1407 tun->gbp_flags = (gbp >> 16) & 0xFF;
1412 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1413 if (parse_geneve_opts(a)) {
1414 return ODP_FIT_ERROR;
1416 /* It is necessary to reproduce options exactly (including order)
1417 * so it's easiest to just echo them back. */
1422 /* Allow this to show up as unexpected, if there are unknown
1423 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1430 return ODP_FIT_ERROR;
1433 return ODP_FIT_TOO_MUCH;
1435 return ODP_FIT_PERFECT;
1439 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1443 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1445 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1446 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1447 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1449 if (tun_key->ip_src) {
1450 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1452 if (tun_key->ip_dst) {
1453 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1455 if (tun_key->ip_tos) {
1456 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1458 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1459 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1460 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1462 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1463 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1465 if (tun_key->tp_src) {
1466 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1468 if (tun_key->tp_dst) {
1469 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1471 if (tun_key->flags & FLOW_TNL_F_OAM) {
1472 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1474 if (tun_key->gbp_flags || tun_key->gbp_id) {
1475 size_t vxlan_opts_ofs;
1477 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1478 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1479 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1480 nl_msg_end_nested(a, vxlan_opts_ofs);
1483 nl_msg_end_nested(a, tun_key_ofs);
1487 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1489 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1493 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1495 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1496 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1498 if (attr == OVS_KEY_ATTR_IPV6) {
1499 const struct ovs_key_ipv6 *ipv6_mask = mask;
1502 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1503 == htonl(IPV6_LABEL_MASK))
1504 && ipv6_mask->ipv6_proto == UINT8_MAX
1505 && ipv6_mask->ipv6_tclass == UINT8_MAX
1506 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1507 && ipv6_mask->ipv6_frag == UINT8_MAX
1508 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1509 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1511 if (attr == OVS_KEY_ATTR_TUNNEL) {
1512 const struct flow_tnl *tun_mask = mask;
1514 return tun_mask->flags == FLOW_TNL_F_MASK
1515 && tun_mask->tun_id == OVS_BE64_MAX
1516 && tun_mask->ip_src == OVS_BE32_MAX
1517 && tun_mask->ip_dst == OVS_BE32_MAX
1518 && tun_mask->ip_tos == UINT8_MAX
1519 && tun_mask->ip_ttl == UINT8_MAX
1520 && tun_mask->tp_src == OVS_BE16_MAX
1521 && tun_mask->tp_dst == OVS_BE16_MAX
1522 && tun_mask->gbp_id == OVS_BE16_MAX
1523 && tun_mask->gbp_flags == UINT8_MAX;
1526 if (attr == OVS_KEY_ATTR_ARP) {
1527 /* ARP key has padding, ignore it. */
1528 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1529 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1530 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1531 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1534 return is_all_ones(mask, size);
1538 odp_mask_attr_is_exact(const struct nlattr *ma)
1540 struct flow_tnl tun_mask;
1541 enum ovs_key_attr attr = nl_attr_type(ma);
1545 if (attr == OVS_KEY_ATTR_TUNNEL) {
1546 memset(&tun_mask, 0, sizeof tun_mask);
1547 odp_tun_key_from_attr(ma, &tun_mask);
1549 size = sizeof tun_mask;
1551 mask = nl_attr_get(ma);
1552 size = nl_attr_get_size(ma);
1555 return odp_mask_is_exact(attr, mask, size);
1559 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1562 struct odp_portno_names *odp_portno_names;
1564 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1565 odp_portno_names->port_no = port_no;
1566 odp_portno_names->name = xstrdup(port_name);
1567 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1568 hash_odp_port(port_no));
1572 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1574 struct odp_portno_names *odp_portno_names;
1576 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1577 hash_odp_port(port_no), portno_names) {
1578 if (odp_portno_names->port_no == port_no) {
1579 return odp_portno_names->name;
1586 odp_portno_names_destroy(struct hmap *portno_names)
1588 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1589 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1590 hmap_node, portno_names) {
1591 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1592 free(odp_portno_names->name);
1593 free(odp_portno_names);
1597 /* Format helpers. */
1600 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1601 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1603 bool mask_empty = mask && eth_addr_is_zero(*mask);
1605 if (verbose || !mask_empty) {
1606 bool mask_full = !mask || eth_mask_is_exact(*mask);
1609 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1611 ds_put_format(ds, "%s=", name);
1612 eth_format_masked(key, *mask, ds);
1613 ds_put_char(ds, ',');
1619 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1620 const ovs_be64 *mask, bool verbose)
1622 bool mask_empty = mask && !*mask;
1624 if (verbose || !mask_empty) {
1625 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1627 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1628 if (!mask_full) { /* Partially masked. */
1629 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1631 ds_put_char(ds, ',');
1636 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1637 const ovs_be32 *mask, bool verbose)
1639 bool mask_empty = mask && !*mask;
1641 if (verbose || !mask_empty) {
1642 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1644 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1645 if (!mask_full) { /* Partially masked. */
1646 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1648 ds_put_char(ds, ',');
1653 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1654 const ovs_be32 (*mask_)[4], bool verbose)
1656 char buf[INET6_ADDRSTRLEN];
1657 const struct in6_addr *key = (const struct in6_addr *)key_;
1658 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1660 bool mask_empty = mask && ipv6_mask_is_any(mask);
1662 if (verbose || !mask_empty) {
1663 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1665 inet_ntop(AF_INET6, key, buf, sizeof buf);
1666 ds_put_format(ds, "%s=%s", name, buf);
1667 if (!mask_full) { /* Partially masked. */
1668 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1669 ds_put_format(ds, "/%s", buf);
1671 ds_put_char(ds, ',');
1676 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1677 const ovs_be32 *mask, bool verbose)
1679 bool mask_empty = mask && !*mask;
1681 if (verbose || !mask_empty) {
1682 bool mask_full = !mask
1683 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1685 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1686 if (!mask_full) { /* Partially masked. */
1687 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1689 ds_put_char(ds, ',');
1694 format_u8x(struct ds *ds, const char *name, uint8_t key,
1695 const uint8_t *mask, bool verbose)
1697 bool mask_empty = mask && !*mask;
1699 if (verbose || !mask_empty) {
1700 bool mask_full = !mask || *mask == UINT8_MAX;
1702 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1703 if (!mask_full) { /* Partially masked. */
1704 ds_put_format(ds, "/%#"PRIx8, *mask);
1706 ds_put_char(ds, ',');
1711 format_u8u(struct ds *ds, const char *name, uint8_t key,
1712 const uint8_t *mask, bool verbose)
1714 bool mask_empty = mask && !*mask;
1716 if (verbose || !mask_empty) {
1717 bool mask_full = !mask || *mask == UINT8_MAX;
1719 ds_put_format(ds, "%s=%"PRIu8, name, key);
1720 if (!mask_full) { /* Partially masked. */
1721 ds_put_format(ds, "/%#"PRIx8, *mask);
1723 ds_put_char(ds, ',');
1728 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1729 const ovs_be16 *mask, bool verbose)
1731 bool mask_empty = mask && !*mask;
1733 if (verbose || !mask_empty) {
1734 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1736 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1737 if (!mask_full) { /* Partially masked. */
1738 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1740 ds_put_char(ds, ',');
1745 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1746 const uint16_t *mask, bool verbose)
1748 bool mask_empty = mask && !*mask;
1750 if (verbose || !mask_empty) {
1751 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1753 ds_put_cstr(ds, name);
1754 ds_put_char(ds, '(');
1755 if (!mask_full) { /* Partially masked. */
1756 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1757 } else { /* Fully masked. */
1758 format_flags(ds, flow_tun_flag_to_string, key, ',');
1760 ds_put_cstr(ds, "),");
1765 format_frag(struct ds *ds, const char *name, uint8_t key,
1766 const uint8_t *mask, bool verbose)
1768 bool mask_empty = mask && !*mask;
1770 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1771 if (verbose || !mask_empty) {
1772 bool mask_full = !mask || *mask == UINT8_MAX;
1774 if (!mask_full) { /* Partially masked. */
1775 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1778 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1783 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1786 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1787 const struct hmap *portno_names, struct ds *ds,
1790 enum ovs_key_attr attr = nl_attr_type(a);
1791 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1795 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1797 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1800 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1801 if (expected_len != -2) {
1802 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1803 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1805 if (bad_key_len || bad_mask_len) {
1807 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1808 nl_attr_get_size(a), expected_len);
1810 format_generic_odp_key(a, ds);
1812 ds_put_char(ds, '/');
1814 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1815 nl_attr_get_size(ma), expected_len);
1817 format_generic_odp_key(ma, ds);
1819 ds_put_char(ds, ')');
1825 ds_put_char(ds, '(');
1827 case OVS_KEY_ATTR_ENCAP:
1828 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1829 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1830 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1832 } else if (nl_attr_get_size(a)) {
1833 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1838 case OVS_KEY_ATTR_PRIORITY:
1839 case OVS_KEY_ATTR_SKB_MARK:
1840 case OVS_KEY_ATTR_DP_HASH:
1841 case OVS_KEY_ATTR_RECIRC_ID:
1842 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1844 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1848 case OVS_KEY_ATTR_TUNNEL: {
1849 struct flow_tnl key, mask_;
1850 struct flow_tnl *mask = ma ? &mask_ : NULL;
1853 memset(mask, 0, sizeof *mask);
1854 odp_tun_key_from_attr(ma, mask);
1856 memset(&key, 0, sizeof key);
1857 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1858 ds_put_format(ds, "error");
1861 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1862 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1863 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1864 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1865 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1866 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1867 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1868 format_be16(ds, "gbp_id", key.gbp_id, MASK(mask, gbp_id), verbose);
1869 format_u8x(ds, "gbp_flags", key.gbp_flags, MASK(mask, gbp_flags), verbose);
1870 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1874 case OVS_KEY_ATTR_IN_PORT:
1875 if (portno_names && verbose && is_exact) {
1876 char *name = odp_portno_names_get(portno_names,
1877 u32_to_odp(nl_attr_get_u32(a)));
1879 ds_put_format(ds, "%s", name);
1881 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1884 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1886 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1891 case OVS_KEY_ATTR_ETHERNET: {
1892 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1893 const struct ovs_key_ethernet *key = nl_attr_get(a);
1895 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1896 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1900 case OVS_KEY_ATTR_VLAN:
1901 format_vlan_tci(ds, nl_attr_get_be16(a),
1902 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1905 case OVS_KEY_ATTR_MPLS: {
1906 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1907 const struct ovs_key_mpls *mpls_mask = NULL;
1908 size_t size = nl_attr_get_size(a);
1910 if (!size || size % sizeof *mpls_key) {
1911 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1915 mpls_mask = nl_attr_get(ma);
1916 if (size != nl_attr_get_size(ma)) {
1917 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1918 "mask length %"PRIuSIZE")",
1919 size, nl_attr_get_size(ma));
1923 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1926 case OVS_KEY_ATTR_ETHERTYPE:
1927 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1929 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1933 case OVS_KEY_ATTR_IPV4: {
1934 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1935 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1937 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1938 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1939 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1941 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1942 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1943 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1948 case OVS_KEY_ATTR_IPV6: {
1949 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1950 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1952 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1953 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1954 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1956 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1958 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1960 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1962 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1967 /* These have the same structure and format. */
1968 case OVS_KEY_ATTR_TCP:
1969 case OVS_KEY_ATTR_UDP:
1970 case OVS_KEY_ATTR_SCTP: {
1971 const struct ovs_key_tcp *key = nl_attr_get(a);
1972 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1974 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1975 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1979 case OVS_KEY_ATTR_TCP_FLAGS:
1981 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1982 ntohs(nl_attr_get_be16(a)),
1983 ntohs(nl_attr_get_be16(ma)));
1985 format_flags(ds, packet_tcp_flag_to_string,
1986 ntohs(nl_attr_get_be16(a)), ',');
1990 case OVS_KEY_ATTR_ICMP: {
1991 const struct ovs_key_icmp *key = nl_attr_get(a);
1992 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1994 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1995 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1999 case OVS_KEY_ATTR_ICMPV6: {
2000 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2001 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2003 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2005 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2010 case OVS_KEY_ATTR_ARP: {
2011 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2012 const struct ovs_key_arp *key = nl_attr_get(a);
2014 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2015 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2016 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2017 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2018 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2022 case OVS_KEY_ATTR_ND: {
2023 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2024 const struct ovs_key_nd *key = nl_attr_get(a);
2026 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2028 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2029 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2034 case OVS_KEY_ATTR_UNSPEC:
2035 case __OVS_KEY_ATTR_MAX:
2037 format_generic_odp_key(a, ds);
2039 ds_put_char(ds, '/');
2040 format_generic_odp_key(ma, ds);
2044 ds_put_char(ds, ')');
2047 static struct nlattr *
2048 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
2050 const struct nlattr *a;
2052 int type = nl_attr_type(key);
2053 int size = nl_attr_get_size(key);
2055 if (odp_flow_key_attr_len(type) >=0) {
2056 nl_msg_put_unspec_zero(ofp, type, size);
2060 nested_mask = nl_msg_start_nested(ofp, type);
2061 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2062 generate_all_wildcard_mask(ofp, nl_attr_get(a));
2064 nl_msg_end_nested(ofp, nested_mask);
2071 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2075 if (ovs_scan(s, "ufid:")) {
2079 if (ovs_scan(s, "0x")) {
2083 n = strspn(s, hex_chars);
2088 if (!ovs_scan(s, "%16"SCNx64"%16"SCNx64, &ufid->u64.hi,
2093 s += strspn(s, delimiters);
2102 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2104 ds_put_format(ds, "ufid:%016"PRIx64"%016"PRIx64, ufid->u64.hi,
2108 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2109 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2110 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2111 * non-null and 'verbose' is true, translates odp port number to its name. */
2113 odp_flow_format(const struct nlattr *key, size_t key_len,
2114 const struct nlattr *mask, size_t mask_len,
2115 const struct hmap *portno_names, struct ds *ds, bool verbose)
2118 const struct nlattr *a;
2120 bool has_ethtype_key = false;
2121 const struct nlattr *ma = NULL;
2123 bool first_field = true;
2125 ofpbuf_init(&ofp, 100);
2126 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2127 bool is_nested_attr;
2128 bool is_wildcard = false;
2129 int attr_type = nl_attr_type(a);
2131 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2132 has_ethtype_key = true;
2135 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
2137 if (mask && mask_len) {
2138 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2139 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2142 if (verbose || !is_wildcard || is_nested_attr) {
2143 if (is_wildcard && !ma) {
2144 ma = generate_all_wildcard_mask(&ofp, a);
2147 ds_put_char(ds, ',');
2149 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2150 first_field = false;
2154 ofpbuf_uninit(&ofp);
2159 if (left == key_len) {
2160 ds_put_cstr(ds, "<empty>");
2162 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2163 for (i = 0; i < left; i++) {
2164 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2166 ds_put_char(ds, ')');
2168 if (!has_ethtype_key) {
2169 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2171 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2172 ntohs(nl_attr_get_be16(ma)));
2176 ds_put_cstr(ds, "<empty>");
2180 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2181 * OVS_KEY_ATTR_* attributes in 'key'. */
2183 odp_flow_key_format(const struct nlattr *key,
2184 size_t key_len, struct ds *ds)
2186 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2190 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2192 if (!strcasecmp(s, "no")) {
2193 *type = OVS_FRAG_TYPE_NONE;
2194 } else if (!strcasecmp(s, "first")) {
2195 *type = OVS_FRAG_TYPE_FIRST;
2196 } else if (!strcasecmp(s, "later")) {
2197 *type = OVS_FRAG_TYPE_LATER;
2207 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2208 uint8_t (*mask)[ETH_ADDR_LEN])
2212 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2216 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2217 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2220 memset(mask, 0xff, sizeof *mask);
2229 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2233 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2237 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2238 IP_SCAN_ARGS(mask), &n)) {
2241 *mask = OVS_BE32_MAX;
2250 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2253 char ipv6_s[IPV6_SCAN_LEN + 1];
2255 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2256 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2260 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2261 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2264 memset(mask, 0xff, sizeof *mask);
2273 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2278 if (ovs_scan(s, "%i%n", &key_, &n)
2279 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2284 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2285 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2287 *mask = htonl(mask_);
2289 *mask = htonl(IPV6_LABEL_MASK);
2298 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2302 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2306 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2318 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2322 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2326 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2338 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2340 uint16_t key_, mask_;
2343 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2348 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2350 *mask = htons(mask_);
2352 *mask = OVS_BE16_MAX;
2361 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2363 uint64_t key_, mask_;
2366 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2369 *key = htonll(key_);
2371 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2373 *mask = htonll(mask_);
2375 *mask = OVS_BE64_MAX;
2384 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2386 uint32_t flags, fmask;
2389 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2390 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2391 if (n >= 0 && s[n] == ')') {
2402 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2404 uint32_t flags, fmask;
2407 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2408 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2410 *key = htons(flags);
2412 *mask = htons(fmask);
2420 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2424 enum ovs_frag_type frag_type;
2426 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2427 && ovs_frag_type_from_string(frag, &frag_type)) {
2440 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2441 const struct simap *port_names)
2445 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2449 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2456 } else if (port_names) {
2457 const struct simap_node *node;
2460 len = strcspn(s, ")");
2461 node = simap_find_len(port_names, s, len);
2474 /* Helper for vlan parsing. */
2475 struct ovs_key_vlan__ {
2480 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2482 const uint16_t mask = ((1U << bits) - 1) << offset;
2484 if (value >> bits) {
2488 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2493 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2496 uint16_t key_, mask_;
2499 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2502 if (set_be16_bf(key, bits, offset, key_)) {
2504 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2507 if (!set_be16_bf(mask, bits, offset, mask_)) {
2511 *mask |= htons(((1U << bits) - 1) << offset);
2521 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2523 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2527 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2529 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2533 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2535 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2540 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2542 const uint32_t mask = ((1U << bits) - 1) << offset;
2544 if (value >> bits) {
2548 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2553 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2556 uint32_t key_, mask_;
2559 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2562 if (set_be32_bf(key, bits, offset, key_)) {
2564 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2567 if (!set_be32_bf(mask, bits, offset, mask_)) {
2571 *mask |= htonl(((1U << bits) - 1) << offset);
2581 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2583 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2587 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2589 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2593 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2595 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2599 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2601 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2604 /* ATTR is compile-time constant, so only the case with correct data type
2605 * will be used. However, the compiler complains about the data type for
2606 * the other cases, so we must cast to make the compiler silent. */
2607 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2608 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2609 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2611 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2614 #define SCAN_IF(NAME) \
2615 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2616 const char *start = s; \
2621 /* Usually no special initialization is needed. */
2622 #define SCAN_BEGIN(NAME, TYPE) \
2625 memset(&skey, 0, sizeof skey); \
2626 memset(&smask, 0, sizeof smask); \
2630 /* Init as fully-masked as mask will not be scanned. */
2631 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2634 memset(&skey, 0, sizeof skey); \
2635 memset(&smask, 0xff, sizeof smask); \
2639 /* VLAN needs special initialization. */
2640 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2642 TYPE skey = KEY_INIT; \
2643 TYPE smask = MASK_INIT; \
2647 /* Scan unnamed entry as 'TYPE' */
2648 #define SCAN_TYPE(TYPE, KEY, MASK) \
2649 len = scan_##TYPE(s, KEY, MASK); \
2655 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2656 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2657 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2658 s += strlen(NAME); \
2659 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2663 #define SCAN_FINISH() \
2664 } while (*s++ == ',' && len != 0); \
2665 if (s[-1] != ')') { \
2669 #define SCAN_FINISH_SINGLE() \
2671 if (*s++ != ')') { \
2675 #define SCAN_PUT(ATTR) \
2676 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2677 SCAN_PUT_ATTR(key, ATTR, skey); \
2679 SCAN_PUT_ATTR(mask, ATTR, smask); \
2683 #define SCAN_END(ATTR) \
2689 #define SCAN_END_SINGLE(ATTR) \
2690 SCAN_FINISH_SINGLE(); \
2695 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2696 SCAN_BEGIN(NAME, TYPE) { \
2697 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2698 } SCAN_END_SINGLE(ATTR)
2700 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
2701 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
2702 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2703 } SCAN_END_SINGLE(ATTR)
2705 /* scan_port needs one extra argument. */
2706 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2707 SCAN_BEGIN(NAME, TYPE) { \
2708 len = scan_port(s, &skey, &smask, port_names); \
2713 } SCAN_END_SINGLE(ATTR)
2716 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2717 struct ofpbuf *key, struct ofpbuf *mask)
2719 if (!strncmp(s, "ufid:", 5)) {
2720 const char *start = s;
2724 s += strspn(s, hex_chars);
2725 s += strspn(s, delimiters);
2730 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2731 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2732 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
2733 OVS_KEY_ATTR_RECIRC_ID);
2734 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2736 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2737 SCAN_FIELD("tun_id=", be64, tun_id);
2738 SCAN_FIELD("src=", ipv4, ip_src);
2739 SCAN_FIELD("dst=", ipv4, ip_dst);
2740 SCAN_FIELD("tos=", u8, ip_tos);
2741 SCAN_FIELD("ttl=", u8, ip_ttl);
2742 SCAN_FIELD("tp_src=", be16, tp_src);
2743 SCAN_FIELD("tp_dst=", be16, tp_dst);
2744 SCAN_FIELD("gbp_id=", be16, gbp_id);
2745 SCAN_FIELD("gbp_flags=", u8, gbp_flags);
2746 SCAN_FIELD("flags(", tun_flags, flags);
2747 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2749 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2751 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2752 SCAN_FIELD("src=", eth, eth_src);
2753 SCAN_FIELD("dst=", eth, eth_dst);
2754 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2756 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2757 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2758 SCAN_FIELD("vid=", vid, tci);
2759 SCAN_FIELD("pcp=", pcp, tci);
2760 SCAN_FIELD("cfi=", cfi, tci);
2761 } SCAN_END(OVS_KEY_ATTR_VLAN);
2763 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2765 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2766 SCAN_FIELD("label=", mpls_label, mpls_lse);
2767 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2768 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2769 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2770 } SCAN_END(OVS_KEY_ATTR_MPLS);
2772 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2773 SCAN_FIELD("src=", ipv4, ipv4_src);
2774 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2775 SCAN_FIELD("proto=", u8, ipv4_proto);
2776 SCAN_FIELD("tos=", u8, ipv4_tos);
2777 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2778 SCAN_FIELD("frag=", frag, ipv4_frag);
2779 } SCAN_END(OVS_KEY_ATTR_IPV4);
2781 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2782 SCAN_FIELD("src=", ipv6, ipv6_src);
2783 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2784 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2785 SCAN_FIELD("proto=", u8, ipv6_proto);
2786 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2787 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2788 SCAN_FIELD("frag=", frag, ipv6_frag);
2789 } SCAN_END(OVS_KEY_ATTR_IPV6);
2791 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2792 SCAN_FIELD("src=", be16, tcp_src);
2793 SCAN_FIELD("dst=", be16, tcp_dst);
2794 } SCAN_END(OVS_KEY_ATTR_TCP);
2796 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2798 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2799 SCAN_FIELD("src=", be16, udp_src);
2800 SCAN_FIELD("dst=", be16, udp_dst);
2801 } SCAN_END(OVS_KEY_ATTR_UDP);
2803 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2804 SCAN_FIELD("src=", be16, sctp_src);
2805 SCAN_FIELD("dst=", be16, sctp_dst);
2806 } SCAN_END(OVS_KEY_ATTR_SCTP);
2808 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2809 SCAN_FIELD("type=", u8, icmp_type);
2810 SCAN_FIELD("code=", u8, icmp_code);
2811 } SCAN_END(OVS_KEY_ATTR_ICMP);
2813 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2814 SCAN_FIELD("type=", u8, icmpv6_type);
2815 SCAN_FIELD("code=", u8, icmpv6_code);
2816 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2818 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2819 SCAN_FIELD("sip=", ipv4, arp_sip);
2820 SCAN_FIELD("tip=", ipv4, arp_tip);
2821 SCAN_FIELD("op=", be16, arp_op);
2822 SCAN_FIELD("sha=", eth, arp_sha);
2823 SCAN_FIELD("tha=", eth, arp_tha);
2824 } SCAN_END(OVS_KEY_ATTR_ARP);
2826 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2827 SCAN_FIELD("target=", ipv6, nd_target);
2828 SCAN_FIELD("sll=", eth, nd_sll);
2829 SCAN_FIELD("tll=", eth, nd_tll);
2830 } SCAN_END(OVS_KEY_ATTR_ND);
2832 /* Encap open-coded. */
2833 if (!strncmp(s, "encap(", 6)) {
2834 const char *start = s;
2835 size_t encap, encap_mask = 0;
2837 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2839 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2846 s += strspn(s, delimiters);
2849 } else if (*s == ')') {
2853 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2861 nl_msg_end_nested(key, encap);
2863 nl_msg_end_nested(mask, encap_mask);
2872 /* Parses the string representation of a datapath flow key, in the
2873 * format output by odp_flow_key_format(). Returns 0 if successful,
2874 * otherwise a positive errno value. On success, the flow key is
2875 * appended to 'key' as a series of Netlink attributes. On failure, no
2876 * data is appended to 'key'. Either way, 'key''s data might be
2879 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2880 * to a port number. (Port names may be used instead of port numbers in
2883 * On success, the attributes appended to 'key' are individually syntactically
2884 * valid, but they may not be valid as a sequence. 'key' might, for example,
2885 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2887 odp_flow_from_string(const char *s, const struct simap *port_names,
2888 struct ofpbuf *key, struct ofpbuf *mask)
2890 const size_t old_size = key->size;
2894 s += strspn(s, delimiters);
2899 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2901 key->size = old_size;
2911 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
2914 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2915 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2916 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2917 * must use a zero mask for the netlink frag field, and all ones mask
2919 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
2921 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
2922 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
2923 : OVS_FRAG_TYPE_FIRST;
2926 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
2927 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
2928 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
2930 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
2932 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
2934 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
2936 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
2937 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
2938 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
2939 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
2941 /* These share the same layout. */
2943 struct ovs_key_tcp tcp;
2944 struct ovs_key_udp udp;
2945 struct ovs_key_sctp sctp;
2948 static void get_tp_key(const struct flow *, union ovs_key_tp *);
2949 static void put_tp_key(const union ovs_key_tp *, struct flow *);
2952 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2953 const struct flow *mask, odp_port_t odp_in_port,
2954 size_t max_mpls_depth, bool recirc, bool export_mask)
2956 struct ovs_key_ethernet *eth_key;
2958 const struct flow *data = export_mask ? mask : flow;
2960 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2962 if (flow->tunnel.ip_dst || export_mask) {
2963 tun_key_to_attr(buf, &data->tunnel);
2966 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2969 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2970 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2973 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2974 * is not the magical value "ODPP_NONE". */
2975 if (export_mask || odp_in_port != ODPP_NONE) {
2976 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2979 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2981 get_ethernet_key(data, eth_key);
2983 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2985 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2987 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2989 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2990 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2991 if (flow->vlan_tci == htons(0)) {
2998 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2999 /* For backwards compatibility with kernels that don't support
3000 * wildcarding, the following convention is used to encode the
3001 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3004 * -------- -------- -------
3005 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3006 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3007 * <none> 0xffff Any non-Ethernet II frame (except valid
3008 * 802.3 SNAP packet with valid eth_type).
3011 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3016 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3018 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3019 struct ovs_key_ipv4 *ipv4_key;
3021 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3023 get_ipv4_key(data, ipv4_key, export_mask);
3024 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3025 struct ovs_key_ipv6 *ipv6_key;
3027 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3029 get_ipv6_key(data, ipv6_key, export_mask);
3030 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3031 flow->dl_type == htons(ETH_TYPE_RARP)) {
3032 struct ovs_key_arp *arp_key;
3034 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3036 get_arp_key(data, arp_key);
3037 } else if (eth_type_mpls(flow->dl_type)) {
3038 struct ovs_key_mpls *mpls_key;
3041 n = flow_count_mpls_labels(flow, NULL);
3042 n = MIN(n, max_mpls_depth);
3043 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3044 n * sizeof *mpls_key);
3045 for (i = 0; i < n; i++) {
3046 mpls_key[i].mpls_lse = data->mpls_lse[i];
3050 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3051 if (flow->nw_proto == IPPROTO_TCP) {
3052 union ovs_key_tp *tcp_key;
3054 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3056 get_tp_key(data, tcp_key);
3057 if (data->tcp_flags) {
3058 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3060 } else if (flow->nw_proto == IPPROTO_UDP) {
3061 union ovs_key_tp *udp_key;
3063 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3065 get_tp_key(data, udp_key);
3066 } else if (flow->nw_proto == IPPROTO_SCTP) {
3067 union ovs_key_tp *sctp_key;
3069 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3071 get_tp_key(data, sctp_key);
3072 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3073 && flow->nw_proto == IPPROTO_ICMP) {
3074 struct ovs_key_icmp *icmp_key;
3076 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3078 icmp_key->icmp_type = ntohs(data->tp_src);
3079 icmp_key->icmp_code = ntohs(data->tp_dst);
3080 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3081 && flow->nw_proto == IPPROTO_ICMPV6) {
3082 struct ovs_key_icmpv6 *icmpv6_key;
3084 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3085 sizeof *icmpv6_key);
3086 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3087 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3089 if (flow->tp_dst == htons(0)
3090 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3091 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3092 && (!export_mask || (data->tp_src == htons(0xffff)
3093 && data->tp_dst == htons(0xffff)))) {
3095 struct ovs_key_nd *nd_key;
3097 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3099 memcpy(nd_key->nd_target, &data->nd_target,
3100 sizeof nd_key->nd_target);
3101 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3102 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3109 nl_msg_end_nested(buf, encap);
3113 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3114 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3115 * number rather than a datapath port number). Instead, if 'odp_in_port'
3116 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3119 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3120 * capable of being expanded to allow for that much space.
3122 * 'recirc' indicates support for recirculation fields. If this is true, then
3123 * these fields will always be serialised. */
3125 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
3126 const struct flow *mask, odp_port_t odp_in_port,
3129 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3133 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3134 * 'buf'. 'flow' is used as a template to determine how to interpret
3135 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3136 * it doesn't indicate whether the other fields should be interpreted as
3137 * ARP, IPv4, IPv6, etc.
3139 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3140 * capable of being expanded to allow for that much space.
3142 * 'recirc' indicates support for recirculation fields. If this is true, then
3143 * these fields will always be serialised. */
3145 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
3146 const struct flow *flow, uint32_t odp_in_port_mask,
3147 size_t max_mpls_depth, bool recirc)
3149 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3150 max_mpls_depth, recirc, true);
3153 /* Generate ODP flow key from the given packet metadata */
3155 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3157 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3159 if (md->tunnel.ip_dst) {
3160 tun_key_to_attr(buf, &md->tunnel);
3163 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3165 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3166 * value "ODPP_NONE". */
3167 if (md->in_port.odp_port != ODPP_NONE) {
3168 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3172 /* Generate packet metadata from the given ODP flow key. */
3174 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3175 struct pkt_metadata *md)
3177 const struct nlattr *nla;
3179 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3180 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3181 1u << OVS_KEY_ATTR_IN_PORT;
3183 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3185 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3186 uint16_t type = nl_attr_type(nla);
3187 size_t len = nl_attr_get_size(nla);
3188 int expected_len = odp_flow_key_attr_len(type);
3190 if (len != expected_len && expected_len >= 0) {
3195 case OVS_KEY_ATTR_RECIRC_ID:
3196 md->recirc_id = nl_attr_get_u32(nla);
3197 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3199 case OVS_KEY_ATTR_DP_HASH:
3200 md->dp_hash = nl_attr_get_u32(nla);
3201 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3203 case OVS_KEY_ATTR_PRIORITY:
3204 md->skb_priority = nl_attr_get_u32(nla);
3205 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3207 case OVS_KEY_ATTR_SKB_MARK:
3208 md->pkt_mark = nl_attr_get_u32(nla);
3209 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3211 case OVS_KEY_ATTR_TUNNEL: {
3212 enum odp_key_fitness res;
3214 res = odp_tun_key_from_attr(nla, &md->tunnel);
3215 if (res == ODP_FIT_ERROR) {
3216 memset(&md->tunnel, 0, sizeof md->tunnel);
3217 } else if (res == ODP_FIT_PERFECT) {
3218 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3222 case OVS_KEY_ATTR_IN_PORT:
3223 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3224 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3230 if (!wanted_attrs) {
3231 return; /* Have everything. */
3237 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3239 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3240 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3241 key_len / sizeof(uint32_t), 0);
3245 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3246 uint64_t attrs, int out_of_range_attr,
3247 const struct nlattr *key, size_t key_len)
3252 if (VLOG_DROP_DBG(rl)) {
3257 for (i = 0; i < 64; i++) {
3258 if (attrs & (UINT64_C(1) << i)) {
3259 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3261 ds_put_format(&s, " %s",
3262 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3265 if (out_of_range_attr) {
3266 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3269 ds_put_cstr(&s, ": ");
3270 odp_flow_key_format(key, key_len, &s);
3272 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3277 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3279 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3282 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3285 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3286 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3287 return 0xff; /* Error. */
3290 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3291 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3292 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3296 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3297 const struct nlattr *attrs[], uint64_t *present_attrsp,
3298 int *out_of_range_attrp)
3300 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3301 const struct nlattr *nla;
3302 uint64_t present_attrs;
3305 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3307 *out_of_range_attrp = 0;
3308 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3309 uint16_t type = nl_attr_type(nla);
3310 size_t len = nl_attr_get_size(nla);
3311 int expected_len = odp_flow_key_attr_len(type);
3313 if (len != expected_len && expected_len >= 0) {
3314 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3316 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3317 "length %d", ovs_key_attr_to_string(type, namebuf,
3323 if (type > OVS_KEY_ATTR_MAX) {
3324 *out_of_range_attrp = type;
3326 if (present_attrs & (UINT64_C(1) << type)) {
3327 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3329 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3330 ovs_key_attr_to_string(type,
3331 namebuf, sizeof namebuf));
3335 present_attrs |= UINT64_C(1) << type;
3340 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3344 *present_attrsp = present_attrs;
3348 static enum odp_key_fitness
3349 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3350 uint64_t expected_attrs,
3351 const struct nlattr *key, size_t key_len)
3353 uint64_t missing_attrs;
3354 uint64_t extra_attrs;
3356 missing_attrs = expected_attrs & ~present_attrs;
3357 if (missing_attrs) {
3358 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3359 log_odp_key_attributes(&rl, "expected but not present",
3360 missing_attrs, 0, key, key_len);
3361 return ODP_FIT_TOO_LITTLE;
3364 extra_attrs = present_attrs & ~expected_attrs;
3365 if (extra_attrs || out_of_range_attr) {
3366 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3367 log_odp_key_attributes(&rl, "present but not expected",
3368 extra_attrs, out_of_range_attr, key, key_len);
3369 return ODP_FIT_TOO_MUCH;
3372 return ODP_FIT_PERFECT;
3376 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3377 uint64_t present_attrs, uint64_t *expected_attrs,
3378 struct flow *flow, const struct flow *src_flow)
3380 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3381 bool is_mask = flow != src_flow;
3383 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3384 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3385 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3386 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3387 ntohs(flow->dl_type));
3390 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3391 flow->dl_type != htons(0xffff)) {
3394 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3397 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3398 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3399 /* See comments in odp_flow_key_from_flow__(). */
3400 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3407 static enum odp_key_fitness
3408 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3409 uint64_t present_attrs, int out_of_range_attr,
3410 uint64_t expected_attrs, struct flow *flow,
3411 const struct nlattr *key, size_t key_len,
3412 const struct flow *src_flow)
3414 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3415 bool is_mask = src_flow != flow;
3416 const void *check_start = NULL;
3417 size_t check_len = 0;
3418 enum ovs_key_attr expected_bit = 0xff;
3420 if (eth_type_mpls(src_flow->dl_type)) {
3421 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3422 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3424 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3425 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3426 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3427 int n = size / sizeof(ovs_be32);
3430 if (!size || size % sizeof(ovs_be32)) {
3431 return ODP_FIT_ERROR;
3433 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3434 return ODP_FIT_ERROR;
3437 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3438 flow->mpls_lse[i] = mpls_lse[i];
3440 if (n > FLOW_MAX_MPLS_LABELS) {
3441 return ODP_FIT_TOO_MUCH;
3445 /* BOS may be set only in the innermost label. */
3446 for (i = 0; i < n - 1; i++) {
3447 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3448 return ODP_FIT_ERROR;
3452 /* BOS must be set in the innermost label. */
3453 if (n < FLOW_MAX_MPLS_LABELS
3454 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3455 return ODP_FIT_TOO_LITTLE;
3461 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3463 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3465 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3466 const struct ovs_key_ipv4 *ipv4_key;
3468 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3469 put_ipv4_key(ipv4_key, flow, is_mask);
3470 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3471 return ODP_FIT_ERROR;
3474 check_start = ipv4_key;
3475 check_len = sizeof *ipv4_key;
3476 expected_bit = OVS_KEY_ATTR_IPV4;
3479 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3481 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3483 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3484 const struct ovs_key_ipv6 *ipv6_key;
3486 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3487 put_ipv6_key(ipv6_key, flow, is_mask);
3488 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3489 return ODP_FIT_ERROR;
3492 check_start = ipv6_key;
3493 check_len = sizeof *ipv6_key;
3494 expected_bit = OVS_KEY_ATTR_IPV6;
3497 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3498 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3500 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3502 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3503 const struct ovs_key_arp *arp_key;
3505 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3506 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3507 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3508 "key", ntohs(arp_key->arp_op));
3509 return ODP_FIT_ERROR;
3511 put_arp_key(arp_key, flow);
3513 check_start = arp_key;
3514 check_len = sizeof *arp_key;
3515 expected_bit = OVS_KEY_ATTR_ARP;
3521 if (check_len > 0) { /* Happens only when 'is_mask'. */
3522 if (!is_all_zeros(check_start, check_len) &&
3523 flow->dl_type != htons(0xffff)) {
3524 return ODP_FIT_ERROR;
3526 expected_attrs |= UINT64_C(1) << expected_bit;
3530 expected_bit = OVS_KEY_ATTR_UNSPEC;
3531 if (src_flow->nw_proto == IPPROTO_TCP
3532 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3533 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3534 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3536 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3538 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3539 const union ovs_key_tp *tcp_key;
3541 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3542 put_tp_key(tcp_key, flow);
3543 expected_bit = OVS_KEY_ATTR_TCP;
3545 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3546 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3547 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3549 } else if (src_flow->nw_proto == IPPROTO_UDP
3550 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3551 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3552 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3554 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3556 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3557 const union ovs_key_tp *udp_key;
3559 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3560 put_tp_key(udp_key, flow);
3561 expected_bit = OVS_KEY_ATTR_UDP;
3563 } else if (src_flow->nw_proto == IPPROTO_SCTP
3564 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3565 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3566 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3568 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3570 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3571 const union ovs_key_tp *sctp_key;
3573 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3574 put_tp_key(sctp_key, flow);
3575 expected_bit = OVS_KEY_ATTR_SCTP;
3577 } else if (src_flow->nw_proto == IPPROTO_ICMP
3578 && src_flow->dl_type == htons(ETH_TYPE_IP)
3579 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3581 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3583 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3584 const struct ovs_key_icmp *icmp_key;
3586 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3587 flow->tp_src = htons(icmp_key->icmp_type);
3588 flow->tp_dst = htons(icmp_key->icmp_code);
3589 expected_bit = OVS_KEY_ATTR_ICMP;
3591 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3592 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3593 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3595 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3597 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3598 const struct ovs_key_icmpv6 *icmpv6_key;
3600 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3601 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3602 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3603 expected_bit = OVS_KEY_ATTR_ICMPV6;
3604 if (src_flow->tp_dst == htons(0) &&
3605 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3606 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3608 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3610 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3611 const struct ovs_key_nd *nd_key;
3613 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3614 memcpy(&flow->nd_target, nd_key->nd_target,
3615 sizeof flow->nd_target);
3616 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3617 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3619 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3620 (flow->tp_src != htons(0xffff) ||
3621 flow->tp_dst != htons(0xffff))) {
3622 return ODP_FIT_ERROR;
3624 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3631 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3632 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3633 return ODP_FIT_ERROR;
3635 expected_attrs |= UINT64_C(1) << expected_bit;
3640 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3644 /* Parse 802.1Q header then encapsulated L3 attributes. */
3645 static enum odp_key_fitness
3646 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3647 uint64_t present_attrs, int out_of_range_attr,
3648 uint64_t expected_attrs, struct flow *flow,
3649 const struct nlattr *key, size_t key_len,
3650 const struct flow *src_flow)
3652 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3653 bool is_mask = src_flow != flow;
3655 const struct nlattr *encap
3656 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3657 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3658 enum odp_key_fitness encap_fitness;
3659 enum odp_key_fitness fitness;
3661 /* Calculate fitness of outer attributes. */
3663 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3664 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3666 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3667 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3669 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3670 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3673 fitness = check_expectations(present_attrs, out_of_range_attr,
3674 expected_attrs, key, key_len);
3677 * Remove the TPID from dl_type since it's not the real Ethertype. */
3678 flow->dl_type = htons(0);
3679 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3680 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3683 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3684 return ODP_FIT_TOO_LITTLE;
3685 } else if (flow->vlan_tci == htons(0)) {
3686 /* Corner case for a truncated 802.1Q header. */
3687 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3688 return ODP_FIT_TOO_MUCH;
3691 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3692 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3693 "but CFI bit is not set", ntohs(flow->vlan_tci));
3694 return ODP_FIT_ERROR;
3697 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3702 /* Now parse the encapsulated attributes. */
3703 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3704 attrs, &present_attrs, &out_of_range_attr)) {
3705 return ODP_FIT_ERROR;
3709 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3710 return ODP_FIT_ERROR;
3712 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3713 expected_attrs, flow, key, key_len,
3716 /* The overall fitness is the worse of the outer and inner attributes. */
3717 return MAX(fitness, encap_fitness);
3720 static enum odp_key_fitness
3721 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3722 struct flow *flow, const struct flow *src_flow)
3724 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3725 uint64_t expected_attrs;
3726 uint64_t present_attrs;
3727 int out_of_range_attr;
3728 bool is_mask = src_flow != flow;
3730 memset(flow, 0, sizeof *flow);
3732 /* Parse attributes. */
3733 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3734 &out_of_range_attr)) {
3735 return ODP_FIT_ERROR;
3740 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3741 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3742 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3743 } else if (is_mask) {
3744 /* Always exact match recirc_id if it is not specified. */
3745 flow->recirc_id = UINT32_MAX;
3748 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3749 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3750 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3752 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3753 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3754 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3757 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3758 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3759 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3762 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3763 enum odp_key_fitness res;
3765 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3766 if (res == ODP_FIT_ERROR) {
3767 return ODP_FIT_ERROR;
3768 } else if (res == ODP_FIT_PERFECT) {
3769 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3773 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3774 flow->in_port.odp_port
3775 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3776 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3777 } else if (!is_mask) {
3778 flow->in_port.odp_port = ODPP_NONE;
3781 /* Ethernet header. */
3782 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3783 const struct ovs_key_ethernet *eth_key;
3785 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3786 put_ethernet_key(eth_key, flow);
3788 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3792 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3795 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3796 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3798 return ODP_FIT_ERROR;
3802 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3803 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3804 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3805 expected_attrs, flow, key, key_len, src_flow);
3808 flow->vlan_tci = htons(0xffff);
3809 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3810 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3811 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3814 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3815 expected_attrs, flow, key, key_len, src_flow);
3818 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3819 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3820 * 'key' fits our expectations for what a flow key should contain.
3822 * The 'in_port' will be the datapath's understanding of the port. The
3823 * caller will need to translate with odp_port_to_ofp_port() if the
3824 * OpenFlow port is needed.
3826 * This function doesn't take the packet itself as an argument because none of
3827 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3828 * it is always possible to infer which additional attribute(s) should appear
3829 * by looking at the attributes for lower-level protocols, e.g. if the network
3830 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3831 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3832 * must be absent. */
3833 enum odp_key_fitness
3834 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3837 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3840 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3841 * structure in 'mask'. 'flow' must be a previously translated flow
3842 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3843 * 'key' fits our expectations for what a flow key should contain. */
3844 enum odp_key_fitness
3845 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3846 struct flow *mask, const struct flow *flow)
3848 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3851 /* Returns 'fitness' as a string, for use in debug messages. */
3853 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3856 case ODP_FIT_PERFECT:
3858 case ODP_FIT_TOO_MUCH:
3860 case ODP_FIT_TOO_LITTLE:
3861 return "too_little";
3869 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3870 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3871 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3872 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3873 * null, then the return value is not meaningful.) */
3875 odp_put_userspace_action(uint32_t pid,
3876 const void *userdata, size_t userdata_size,
3877 odp_port_t tunnel_out_port,
3878 struct ofpbuf *odp_actions)
3880 size_t userdata_ofs;
3883 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3884 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3886 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3888 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3889 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3892 * - The kernel rejected shorter userdata with -ERANGE.
3894 * - The kernel silently dropped userdata beyond the first 8 bytes.
3896 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3897 * separately disable features that required more than 8 bytes.) */
3898 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3899 MAX(8, userdata_size)),
3900 userdata, userdata_size);
3904 if (tunnel_out_port != ODPP_NONE) {
3905 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3908 nl_msg_end_nested(odp_actions, offset);
3910 return userdata_ofs;
3914 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3915 struct ofpbuf *odp_actions)
3917 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3918 tun_key_to_attr(odp_actions, tunnel);
3919 nl_msg_end_nested(odp_actions, offset);
3923 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
3924 struct ovs_action_push_tnl *data)
3926 int size = offsetof(struct ovs_action_push_tnl, header);
3928 size += data->header_len;
3929 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
3933 /* The commit_odp_actions() function and its helpers. */
3936 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3937 const void *key, size_t key_size)
3939 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3940 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3941 nl_msg_end_nested(odp_actions, offset);
3944 /* Masked set actions have a mask following the data within the netlink
3945 * attribute. The unmasked bits in the data will be cleared as the data
3946 * is copied to the action. */
3948 commit_masked_set_action(struct ofpbuf *odp_actions,
3949 enum ovs_key_attr key_type,
3950 const void *key_, const void *mask_, size_t key_size)
3952 size_t offset = nl_msg_start_nested(odp_actions,
3953 OVS_ACTION_ATTR_SET_MASKED);
3954 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3955 const char *key = key_, *mask = mask_;
3957 memcpy(data + key_size, mask, key_size);
3958 /* Clear unmasked bits while copying. */
3959 while (key_size--) {
3960 *data++ = *key++ & *mask++;
3962 nl_msg_end_nested(odp_actions, offset);
3965 /* If any of the flow key data that ODP actions can modify are different in
3966 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3967 * 'odp_actions' that change the flow tunneling information in key from
3968 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3969 * same way. In other words, operates the same as commit_odp_actions(), but
3970 * only on tunneling information. */
3972 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3973 struct ofpbuf *odp_actions)
3975 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3976 if (flow->tunnel.ip_dst) {
3977 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3980 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3981 odp_put_tunnel_action(&base->tunnel, odp_actions);
3986 commit(enum ovs_key_attr attr, bool use_masked_set,
3987 const void *key, void *base, void *mask, size_t size,
3988 struct ofpbuf *odp_actions)
3990 if (memcmp(key, base, size)) {
3991 bool fully_masked = odp_mask_is_exact(attr, mask, size);
3993 if (use_masked_set && !fully_masked) {
3994 commit_masked_set_action(odp_actions, attr, key, mask, size);
3996 if (!fully_masked) {
3997 memset(mask, 0xff, size);
3999 commit_set_action(odp_actions, attr, key, size);
4001 memcpy(base, key, size);
4004 /* Mask bits are set when we have either read or set the corresponding
4005 * values. Masked bits will be exact-matched, no need to set them
4006 * if the value did not actually change. */
4012 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4014 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4015 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4019 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4021 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4022 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4026 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4027 struct ofpbuf *odp_actions,
4028 struct flow_wildcards *wc,
4031 struct ovs_key_ethernet key, base, mask;
4033 get_ethernet_key(flow, &key);
4034 get_ethernet_key(base_flow, &base);
4035 get_ethernet_key(&wc->masks, &mask);
4037 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4038 &key, &base, &mask, sizeof key, odp_actions)) {
4039 put_ethernet_key(&base, base_flow);
4040 put_ethernet_key(&mask, &wc->masks);
4045 pop_vlan(struct flow *base,
4046 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4048 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4050 if (base->vlan_tci & htons(VLAN_CFI)) {
4051 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4057 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4058 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4060 if (base->vlan_tci == vlan_tci) {
4064 pop_vlan(base, odp_actions, wc);
4065 if (vlan_tci & htons(VLAN_CFI)) {
4066 struct ovs_action_push_vlan vlan;
4068 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4069 vlan.vlan_tci = vlan_tci;
4070 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4071 &vlan, sizeof vlan);
4073 base->vlan_tci = vlan_tci;
4076 /* Wildcarding already done at action translation time. */
4078 commit_mpls_action(const struct flow *flow, struct flow *base,
4079 struct ofpbuf *odp_actions)
4081 int base_n = flow_count_mpls_labels(base, NULL);
4082 int flow_n = flow_count_mpls_labels(flow, NULL);
4083 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4086 while (base_n > common_n) {
4087 if (base_n - 1 == common_n && flow_n > common_n) {
4088 /* If there is only one more LSE in base than there are common
4089 * between base and flow; and flow has at least one more LSE than
4090 * is common then the topmost LSE of base may be updated using
4092 struct ovs_key_mpls mpls_key;
4094 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4095 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4096 &mpls_key, sizeof mpls_key);
4097 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4100 /* Otherwise, if there more LSEs in base than are common between
4101 * base and flow then pop the topmost one. */
4105 /* If all the LSEs are to be popped and this is not the outermost
4106 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4107 * POP_MPLS action instead of flow->dl_type.
4109 * This is because the POP_MPLS action requires its ethertype
4110 * argument to be an MPLS ethernet type but in this case
4111 * flow->dl_type will be a non-MPLS ethernet type.
4113 * When the final POP_MPLS action occurs it use flow->dl_type and
4114 * the and the resulting packet will have the desired dl_type. */
4115 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4116 dl_type = htons(ETH_TYPE_MPLS);
4118 dl_type = flow->dl_type;
4120 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4121 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4127 /* If, after the above popping and setting, there are more LSEs in flow
4128 * than base then some LSEs need to be pushed. */
4129 while (base_n < flow_n) {
4130 struct ovs_action_push_mpls *mpls;
4132 mpls = nl_msg_put_unspec_zero(odp_actions,
4133 OVS_ACTION_ATTR_PUSH_MPLS,
4135 mpls->mpls_ethertype = flow->dl_type;
4136 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4137 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4138 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4144 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4146 ipv4->ipv4_src = flow->nw_src;
4147 ipv4->ipv4_dst = flow->nw_dst;
4148 ipv4->ipv4_proto = flow->nw_proto;
4149 ipv4->ipv4_tos = flow->nw_tos;
4150 ipv4->ipv4_ttl = flow->nw_ttl;
4151 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4155 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4157 flow->nw_src = ipv4->ipv4_src;
4158 flow->nw_dst = ipv4->ipv4_dst;
4159 flow->nw_proto = ipv4->ipv4_proto;
4160 flow->nw_tos = ipv4->ipv4_tos;
4161 flow->nw_ttl = ipv4->ipv4_ttl;
4162 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4166 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4167 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4170 struct ovs_key_ipv4 key, mask, base;
4172 /* Check that nw_proto and nw_frag remain unchanged. */
4173 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4174 flow->nw_frag == base_flow->nw_frag);
4176 get_ipv4_key(flow, &key, false);
4177 get_ipv4_key(base_flow, &base, false);
4178 get_ipv4_key(&wc->masks, &mask, true);
4179 mask.ipv4_proto = 0; /* Not writeable. */
4180 mask.ipv4_frag = 0; /* Not writable. */
4182 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4184 put_ipv4_key(&base, base_flow, false);
4185 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4186 put_ipv4_key(&mask, &wc->masks, true);
4192 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4194 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4195 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4196 ipv6->ipv6_label = flow->ipv6_label;
4197 ipv6->ipv6_proto = flow->nw_proto;
4198 ipv6->ipv6_tclass = flow->nw_tos;
4199 ipv6->ipv6_hlimit = flow->nw_ttl;
4200 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4204 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4206 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4207 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4208 flow->ipv6_label = ipv6->ipv6_label;
4209 flow->nw_proto = ipv6->ipv6_proto;
4210 flow->nw_tos = ipv6->ipv6_tclass;
4211 flow->nw_ttl = ipv6->ipv6_hlimit;
4212 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4216 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4217 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4220 struct ovs_key_ipv6 key, mask, base;
4222 /* Check that nw_proto and nw_frag remain unchanged. */
4223 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4224 flow->nw_frag == base_flow->nw_frag);
4226 get_ipv6_key(flow, &key, false);
4227 get_ipv6_key(base_flow, &base, false);
4228 get_ipv6_key(&wc->masks, &mask, true);
4229 mask.ipv6_proto = 0; /* Not writeable. */
4230 mask.ipv6_frag = 0; /* Not writable. */
4232 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4234 put_ipv6_key(&base, base_flow, false);
4235 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4236 put_ipv6_key(&mask, &wc->masks, true);
4242 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4244 /* ARP key has padding, clear it. */
4245 memset(arp, 0, sizeof *arp);
4247 arp->arp_sip = flow->nw_src;
4248 arp->arp_tip = flow->nw_dst;
4249 arp->arp_op = htons(flow->nw_proto);
4250 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4251 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4255 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4257 flow->nw_src = arp->arp_sip;
4258 flow->nw_dst = arp->arp_tip;
4259 flow->nw_proto = ntohs(arp->arp_op);
4260 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4261 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4264 static enum slow_path_reason
4265 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4266 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4268 struct ovs_key_arp key, mask, base;
4270 get_arp_key(flow, &key);
4271 get_arp_key(base_flow, &base);
4272 get_arp_key(&wc->masks, &mask);
4274 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4276 put_arp_key(&base, base_flow);
4277 put_arp_key(&mask, &wc->masks);
4284 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4286 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4287 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4288 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4289 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4293 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4295 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4296 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4297 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4298 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4301 static enum slow_path_reason
4302 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4303 struct ofpbuf *odp_actions,
4304 struct flow_wildcards *wc, bool use_masked)
4306 struct ovs_key_nd key, mask, base;
4308 get_nd_key(flow, &key);
4309 get_nd_key(base_flow, &base);
4310 get_nd_key(&wc->masks, &mask);
4312 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4314 put_nd_key(&base, base_flow);
4315 put_nd_key(&mask, &wc->masks);
4322 static enum slow_path_reason
4323 commit_set_nw_action(const struct flow *flow, struct flow *base,
4324 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4327 /* Check if 'flow' really has an L3 header. */
4328 if (!flow->nw_proto) {
4332 switch (ntohs(base->dl_type)) {
4334 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4338 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4339 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4342 return commit_set_arp_action(flow, base, odp_actions, wc);
4348 /* TCP, UDP, and SCTP keys have the same layout. */
4349 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4350 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4353 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4355 tp->tcp.tcp_src = flow->tp_src;
4356 tp->tcp.tcp_dst = flow->tp_dst;
4360 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4362 flow->tp_src = tp->tcp.tcp_src;
4363 flow->tp_dst = tp->tcp.tcp_dst;
4367 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4368 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4371 enum ovs_key_attr key_type;
4372 union ovs_key_tp key, mask, base;
4374 /* Check if 'flow' really has an L3 header. */
4375 if (!flow->nw_proto) {
4379 if (!is_ip_any(base_flow)) {
4383 if (flow->nw_proto == IPPROTO_TCP) {
4384 key_type = OVS_KEY_ATTR_TCP;
4385 } else if (flow->nw_proto == IPPROTO_UDP) {
4386 key_type = OVS_KEY_ATTR_UDP;
4387 } else if (flow->nw_proto == IPPROTO_SCTP) {
4388 key_type = OVS_KEY_ATTR_SCTP;
4393 get_tp_key(flow, &key);
4394 get_tp_key(base_flow, &base);
4395 get_tp_key(&wc->masks, &mask);
4397 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4399 put_tp_key(&base, base_flow);
4400 put_tp_key(&mask, &wc->masks);
4405 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4406 struct ofpbuf *odp_actions,
4407 struct flow_wildcards *wc,
4410 uint32_t key, mask, base;
4412 key = flow->skb_priority;
4413 base = base_flow->skb_priority;
4414 mask = wc->masks.skb_priority;
4416 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4417 sizeof key, odp_actions)) {
4418 base_flow->skb_priority = base;
4419 wc->masks.skb_priority = mask;
4424 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4425 struct ofpbuf *odp_actions,
4426 struct flow_wildcards *wc,
4429 uint32_t key, mask, base;
4431 key = flow->pkt_mark;
4432 base = base_flow->pkt_mark;
4433 mask = wc->masks.pkt_mark;
4435 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4436 sizeof key, odp_actions)) {
4437 base_flow->pkt_mark = base;
4438 wc->masks.pkt_mark = mask;
4442 /* If any of the flow key data that ODP actions can modify are different in
4443 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4444 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4445 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4446 * in addition to this function if needed. Sets fields in 'wc' that are
4447 * used as part of the action.
4449 * Returns a reason to force processing the flow's packets into the userspace
4450 * slow path, if there is one, otherwise 0. */
4451 enum slow_path_reason
4452 commit_odp_actions(const struct flow *flow, struct flow *base,
4453 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4456 enum slow_path_reason slow;
4458 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4459 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4460 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4461 commit_mpls_action(flow, base, odp_actions);
4462 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4463 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4464 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);