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";
58 const struct attr_len_tbl *next;
61 #define ATTR_LEN_INVALID -1
62 #define ATTR_LEN_VARIABLE -2
63 #define ATTR_LEN_NESTED -3
65 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
66 struct ofpbuf *, struct ofpbuf *);
67 static void format_odp_key_attr(const struct nlattr *a,
68 const struct nlattr *ma,
69 const struct hmap *portno_names, struct ds *ds,
72 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
73 int max, struct ofpbuf *,
74 const struct nlattr *key);
75 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
78 * - For an action whose argument has a fixed length, returned that
79 * nonnegative length in bytes.
81 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
83 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
85 odp_action_len(uint16_t type)
87 if (type > OVS_ACTION_ATTR_MAX) {
91 switch ((enum ovs_action_attr) type) {
92 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
93 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
94 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
95 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
96 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
97 case OVS_ACTION_ATTR_POP_VLAN: return 0;
98 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
99 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
100 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
101 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
102 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
103 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
104 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
106 case OVS_ACTION_ATTR_UNSPEC:
107 case __OVS_ACTION_ATTR_MAX:
108 return ATTR_LEN_INVALID;
111 return ATTR_LEN_INVALID;
114 /* Returns a string form of 'attr'. The return value is either a statically
115 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
116 * should be at least OVS_KEY_ATTR_BUFSIZE. */
117 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
119 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
122 case OVS_KEY_ATTR_UNSPEC: return "unspec";
123 case OVS_KEY_ATTR_ENCAP: return "encap";
124 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
125 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
126 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
127 case OVS_KEY_ATTR_IN_PORT: return "in_port";
128 case OVS_KEY_ATTR_ETHERNET: return "eth";
129 case OVS_KEY_ATTR_VLAN: return "vlan";
130 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
131 case OVS_KEY_ATTR_IPV4: return "ipv4";
132 case OVS_KEY_ATTR_IPV6: return "ipv6";
133 case OVS_KEY_ATTR_TCP: return "tcp";
134 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
135 case OVS_KEY_ATTR_UDP: return "udp";
136 case OVS_KEY_ATTR_SCTP: return "sctp";
137 case OVS_KEY_ATTR_ICMP: return "icmp";
138 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
139 case OVS_KEY_ATTR_ARP: return "arp";
140 case OVS_KEY_ATTR_ND: return "nd";
141 case OVS_KEY_ATTR_MPLS: return "mpls";
142 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
143 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
145 case __OVS_KEY_ATTR_MAX:
147 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
153 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
155 size_t len = nl_attr_get_size(a);
157 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
159 const uint8_t *unspec;
162 unspec = nl_attr_get(a);
163 for (i = 0; i < len; i++) {
164 ds_put_char(ds, i ? ' ': '(');
165 ds_put_format(ds, "%02x", unspec[i]);
167 ds_put_char(ds, ')');
172 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
174 static const struct nl_policy ovs_sample_policy[] = {
175 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
176 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
178 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
180 const struct nlattr *nla_acts;
183 ds_put_cstr(ds, "sample");
185 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
186 ds_put_cstr(ds, "(error)");
190 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
193 ds_put_format(ds, "(sample=%.1f%%,", percentage);
195 ds_put_cstr(ds, "actions(");
196 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
197 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
198 format_odp_actions(ds, nla_acts, len);
199 ds_put_format(ds, "))");
203 slow_path_reason_to_string(uint32_t reason)
205 switch ((enum slow_path_reason) reason) {
206 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
215 slow_path_reason_to_explanation(enum slow_path_reason reason)
218 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
227 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
228 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
233 /* Parse masked flags in numeric format? */
234 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
235 res_flags, res_mask, &n) && n > 0) {
236 if (*res_flags & ~allowed || *res_mask & ~allowed) {
244 if (res_mask && (*s == '+' || *s == '-')) {
245 uint32_t flags = 0, mask = 0;
247 /* Parse masked flags. */
248 while (s[0] != ')') {
255 } else if (s[0] == '-') {
263 name_len = strcspn(s, "+-)");
265 for (bit = 1; bit; bit <<= 1) {
266 const char *fname = bit_to_string(bit);
274 if (len != name_len) {
277 if (!strncmp(s, fname, len)) {
279 /* bit already set. */
282 if (!(bit & allowed)) {
294 return -EINVAL; /* Unknown flag name */
305 /* Parse unmasked flags. If a flag is present, it is set, otherwise
307 while (s[n] != ')') {
308 unsigned long long int flags;
312 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
313 if (flags & ~allowed) {
316 n += n0 + (s[n + n0] == ',');
321 for (bit = 1; bit; bit <<= 1) {
322 const char *name = bit_to_string(bit);
330 if (!strncmp(s + n, name, len) &&
331 (s[n + len] == ',' || s[n + len] == ')')) {
332 if (!(bit & allowed)) {
336 n += len + (s[n + len] == ',');
348 *res_mask = UINT32_MAX;
354 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
356 static const struct nl_policy ovs_userspace_policy[] = {
357 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
358 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
360 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
363 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
364 const struct nlattr *userdata_attr;
365 const struct nlattr *tunnel_out_port_attr;
367 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
368 ds_put_cstr(ds, "userspace(error)");
372 ds_put_format(ds, "userspace(pid=%"PRIu32,
373 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
375 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
378 const uint8_t *userdata = nl_attr_get(userdata_attr);
379 size_t userdata_len = nl_attr_get_size(userdata_attr);
380 bool userdata_unspec = true;
381 union user_action_cookie cookie;
383 if (userdata_len >= sizeof cookie.type
384 && userdata_len <= sizeof cookie) {
386 memset(&cookie, 0, sizeof cookie);
387 memcpy(&cookie, userdata, userdata_len);
389 userdata_unspec = false;
391 if (userdata_len == sizeof cookie.sflow
392 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
393 ds_put_format(ds, ",sFlow("
394 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
395 vlan_tci_to_vid(cookie.sflow.vlan_tci),
396 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
397 cookie.sflow.output);
398 } else if (userdata_len == sizeof cookie.slow_path
399 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
400 ds_put_cstr(ds, ",slow_path(");
401 format_flags(ds, slow_path_reason_to_string,
402 cookie.slow_path.reason, ',');
403 ds_put_format(ds, ")");
404 } else if (userdata_len == sizeof cookie.flow_sample
405 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
406 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
407 ",collector_set_id=%"PRIu32
408 ",obs_domain_id=%"PRIu32
409 ",obs_point_id=%"PRIu32")",
410 cookie.flow_sample.probability,
411 cookie.flow_sample.collector_set_id,
412 cookie.flow_sample.obs_domain_id,
413 cookie.flow_sample.obs_point_id);
414 } else if (userdata_len >= sizeof cookie.ipfix
415 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
416 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
417 cookie.ipfix.output_odp_port);
419 userdata_unspec = true;
423 if (userdata_unspec) {
425 ds_put_format(ds, ",userdata(");
426 for (i = 0; i < userdata_len; i++) {
427 ds_put_format(ds, "%02x", userdata[i]);
429 ds_put_char(ds, ')');
433 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
434 if (tunnel_out_port_attr) {
435 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
436 nl_attr_get_u32(tunnel_out_port_attr));
439 ds_put_char(ds, ')');
443 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
445 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
446 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
447 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
448 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
450 ds_put_char(ds, ',');
452 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
453 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
454 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
455 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
457 ds_put_char(ds, ',');
459 if (!(tci & htons(VLAN_CFI))) {
460 ds_put_cstr(ds, "cfi=0");
461 ds_put_char(ds, ',');
467 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
469 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
470 mpls_lse_to_label(mpls_lse),
471 mpls_lse_to_tc(mpls_lse),
472 mpls_lse_to_ttl(mpls_lse),
473 mpls_lse_to_bos(mpls_lse));
477 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
478 const struct ovs_key_mpls *mpls_mask, int n)
481 ovs_be32 key = mpls_key->mpls_lse;
483 if (mpls_mask == NULL) {
484 format_mpls_lse(ds, key);
486 ovs_be32 mask = mpls_mask->mpls_lse;
488 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
489 mpls_lse_to_label(key), mpls_lse_to_label(mask),
490 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
491 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
492 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
497 for (i = 0; i < n; i++) {
498 ds_put_format(ds, "lse%d=%#"PRIx32,
499 i, ntohl(mpls_key[i].mpls_lse));
501 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
503 ds_put_char(ds, ',');
510 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
512 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
516 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
518 ds_put_format(ds, "hash(");
520 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
521 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
523 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
526 ds_put_format(ds, ")");
530 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
532 const struct udp_header *udp;
534 udp = (const struct udp_header *) (ip + 1);
535 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
536 ntohs(udp->udp_src), ntohs(udp->udp_dst),
537 ntohs(udp->udp_csum));
543 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
545 const struct eth_header *eth;
546 const struct ip_header *ip;
549 eth = (const struct eth_header *)data->header;
552 ip = (const struct ip_header *)l3;
555 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
556 data->header_len, data->tnl_type);
557 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
558 ds_put_format(ds, ",src=");
559 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
560 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
563 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
564 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
565 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
566 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
567 ip->ip_proto, ip->ip_tos,
571 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
572 const struct vxlanhdr *vxh;
574 vxh = format_udp_tnl_push_header(ds, ip);
576 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
577 ntohl(get_16aligned_be32(&vxh->vx_flags)),
578 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
579 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
580 const struct genevehdr *gnh;
582 gnh = format_udp_tnl_push_header(ds, ip);
584 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
585 gnh->oam ? "oam," : "",
586 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
587 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
588 const struct gre_base_hdr *greh;
589 ovs_16aligned_be32 *options;
592 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
593 greh = (const struct gre_base_hdr *) l4;
595 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
596 ntohs(greh->flags), ntohs(greh->protocol));
597 options = (ovs_16aligned_be32 *)(greh + 1);
598 if (greh->flags & htons(GRE_CSUM)) {
599 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
602 if (greh->flags & htons(GRE_KEY)) {
603 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
606 if (greh->flags & htons(GRE_SEQ)) {
607 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
610 ds_put_format(ds, ")");
612 ds_put_format(ds, ")");
616 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
618 struct ovs_action_push_tnl *data;
620 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
622 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
623 format_odp_tnl_push_header(ds, data);
624 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
628 format_odp_action(struct ds *ds, const struct nlattr *a)
631 enum ovs_action_attr type = nl_attr_type(a);
632 const struct ovs_action_push_vlan *vlan;
635 expected_len = odp_action_len(nl_attr_type(a));
636 if (expected_len != ATTR_LEN_VARIABLE &&
637 nl_attr_get_size(a) != expected_len) {
638 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
639 nl_attr_get_size(a), expected_len);
640 format_generic_odp_action(ds, a);
645 case OVS_ACTION_ATTR_OUTPUT:
646 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
648 case OVS_ACTION_ATTR_TUNNEL_POP:
649 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
651 case OVS_ACTION_ATTR_TUNNEL_PUSH:
652 format_odp_tnl_push_action(ds, a);
654 case OVS_ACTION_ATTR_USERSPACE:
655 format_odp_userspace_action(ds, a);
657 case OVS_ACTION_ATTR_RECIRC:
658 format_odp_recirc_action(ds, nl_attr_get_u32(a));
660 case OVS_ACTION_ATTR_HASH:
661 format_odp_hash_action(ds, nl_attr_get(a));
663 case OVS_ACTION_ATTR_SET_MASKED:
665 size = nl_attr_get_size(a) / 2;
666 ds_put_cstr(ds, "set(");
668 /* Masked set action not supported for tunnel key, which is bigger. */
669 if (size <= sizeof(struct ovs_key_ipv6)) {
670 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
671 sizeof(struct nlattr))];
672 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
673 sizeof(struct nlattr))];
675 mask->nla_type = attr->nla_type = nl_attr_type(a);
676 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
677 memcpy(attr + 1, (char *)(a + 1), size);
678 memcpy(mask + 1, (char *)(a + 1) + size, size);
679 format_odp_key_attr(attr, mask, NULL, ds, false);
681 format_odp_key_attr(a, NULL, NULL, ds, false);
683 ds_put_cstr(ds, ")");
685 case OVS_ACTION_ATTR_SET:
686 ds_put_cstr(ds, "set(");
687 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
688 ds_put_cstr(ds, ")");
690 case OVS_ACTION_ATTR_PUSH_VLAN:
691 vlan = nl_attr_get(a);
692 ds_put_cstr(ds, "push_vlan(");
693 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
694 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
696 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
697 ds_put_char(ds, ')');
699 case OVS_ACTION_ATTR_POP_VLAN:
700 ds_put_cstr(ds, "pop_vlan");
702 case OVS_ACTION_ATTR_PUSH_MPLS: {
703 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
704 ds_put_cstr(ds, "push_mpls(");
705 format_mpls_lse(ds, mpls->mpls_lse);
706 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
709 case OVS_ACTION_ATTR_POP_MPLS: {
710 ovs_be16 ethertype = nl_attr_get_be16(a);
711 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
714 case OVS_ACTION_ATTR_SAMPLE:
715 format_odp_sample_action(ds, a);
717 case OVS_ACTION_ATTR_UNSPEC:
718 case __OVS_ACTION_ATTR_MAX:
720 format_generic_odp_action(ds, a);
726 format_odp_actions(struct ds *ds, const struct nlattr *actions,
730 const struct nlattr *a;
733 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
735 ds_put_char(ds, ',');
737 format_odp_action(ds, a);
742 if (left == actions_len) {
743 ds_put_cstr(ds, "<empty>");
745 ds_put_format(ds, ",***%u leftover bytes*** (", left);
746 for (i = 0; i < left; i++) {
747 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
749 ds_put_char(ds, ')');
752 ds_put_cstr(ds, "drop");
756 /* Separate out parse_odp_userspace_action() function. */
758 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
761 union user_action_cookie cookie;
763 odp_port_t tunnel_out_port;
765 void *user_data = NULL;
766 size_t user_data_size = 0;
768 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
774 uint32_t probability;
775 uint32_t collector_set_id;
776 uint32_t obs_domain_id;
777 uint32_t obs_point_id;
780 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
781 "pcp=%i,output=%"SCNi32")%n",
782 &vid, &pcp, &output, &n1)) {
786 tci = vid | (pcp << VLAN_PCP_SHIFT);
791 cookie.type = USER_ACTION_COOKIE_SFLOW;
792 cookie.sflow.vlan_tci = htons(tci);
793 cookie.sflow.output = output;
795 user_data_size = sizeof cookie.sflow;
796 } else if (ovs_scan(&s[n], ",slow_path(%n",
801 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
802 cookie.slow_path.unused = 0;
803 cookie.slow_path.reason = 0;
805 res = parse_flags(&s[n], slow_path_reason_to_string,
806 &cookie.slow_path.reason,
807 SLOW_PATH_REASON_MASK, NULL);
808 if (res < 0 || s[n + res] != ')') {
814 user_data_size = sizeof cookie.slow_path;
815 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
816 "collector_set_id=%"SCNi32","
817 "obs_domain_id=%"SCNi32","
818 "obs_point_id=%"SCNi32")%n",
819 &probability, &collector_set_id,
820 &obs_domain_id, &obs_point_id, &n1)) {
823 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
824 cookie.flow_sample.probability = probability;
825 cookie.flow_sample.collector_set_id = collector_set_id;
826 cookie.flow_sample.obs_domain_id = obs_domain_id;
827 cookie.flow_sample.obs_point_id = obs_point_id;
829 user_data_size = sizeof cookie.flow_sample;
830 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
833 cookie.type = USER_ACTION_COOKIE_IPFIX;
834 cookie.ipfix.output_odp_port = u32_to_odp(output);
836 user_data_size = sizeof cookie.ipfix;
837 } else if (ovs_scan(&s[n], ",userdata(%n",
842 ofpbuf_init(&buf, 16);
843 end = ofpbuf_put_hex(&buf, &s[n], NULL);
847 user_data = buf.data;
848 user_data_size = buf.size;
855 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
856 &tunnel_out_port, &n1)) {
857 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
859 } else if (s[n] == ')') {
860 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
869 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
871 struct eth_header *eth;
872 struct ip_header *ip;
873 struct udp_header *udp;
874 struct gre_base_hdr *greh;
875 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
877 uint32_t tnl_type = 0, header_len = 0;
881 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
884 eth = (struct eth_header *) data->header;
885 l3 = (data->header + sizeof *eth);
886 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
887 ip = (struct ip_header *) l3;
888 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
889 "eth(dst="ETH_ADDR_SCAN_FMT",",
892 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
896 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
897 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
900 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
903 eth->eth_type = htons(dl_type);
906 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
907 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
910 &ip->ip_proto, &ip->ip_tos,
911 &ip->ip_ttl, &ip->ip_frag_off)) {
914 put_16aligned_be32(&ip->ip_src, sip);
915 put_16aligned_be32(&ip->ip_dst, dip);
918 udp = (struct udp_header *) l4;
919 greh = (struct gre_base_hdr *) l4;
920 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
921 &udp_src, &udp_dst, &csum)) {
922 uint32_t vx_flags, vni;
924 udp->udp_src = htons(udp_src);
925 udp->udp_dst = htons(udp_dst);
927 udp->udp_csum = htons(csum);
929 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
931 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
933 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
934 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
935 tnl_type = OVS_VPORT_TYPE_VXLAN;
936 header_len = sizeof *eth + sizeof *ip +
937 sizeof *udp + sizeof *vxh;
938 } else if (ovs_scan_len(s, &n, "geneve(")) {
939 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
941 memset(gnh, 0, sizeof *gnh);
942 if (ovs_scan_len(s, &n, "oam,")) {
945 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
948 gnh->proto_type = htons(ETH_TYPE_TEB);
949 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
950 tnl_type = OVS_VPORT_TYPE_GENEVE;
951 header_len = sizeof *eth + sizeof *ip +
952 sizeof *udp + sizeof *gnh;
956 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
957 &gre_flags, &gre_proto)){
959 tnl_type = OVS_VPORT_TYPE_GRE;
960 greh->flags = htons(gre_flags);
961 greh->protocol = htons(gre_proto);
962 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
964 if (greh->flags & htons(GRE_CSUM)) {
965 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
969 memset(options, 0, sizeof *options);
970 *((ovs_be16 *)options) = htons(csum);
973 if (greh->flags & htons(GRE_KEY)) {
976 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
980 put_16aligned_be32(options, htonl(key));
983 if (greh->flags & htons(GRE_SEQ)) {
986 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
989 put_16aligned_be32(options, htonl(seq));
993 if (!ovs_scan_len(s, &n, "))")) {
997 header_len = sizeof *eth + sizeof *ip +
998 ((uint8_t *) options - (uint8_t *) greh);
1003 /* check tunnel meta data. */
1004 if (data->tnl_type != tnl_type) {
1007 if (data->header_len != header_len) {
1012 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1020 parse_odp_action(const char *s, const struct simap *port_names,
1021 struct ofpbuf *actions)
1027 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1028 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1034 int len = strcspn(s, delimiters);
1035 struct simap_node *node;
1037 node = simap_find_len(port_names, s, len);
1039 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1048 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1049 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1054 if (!strncmp(s, "userspace(", 10)) {
1055 return parse_odp_userspace_action(s, actions);
1058 if (!strncmp(s, "set(", 4)) {
1061 struct nlattr mask[128 / sizeof(struct nlattr)];
1062 struct ofpbuf maskbuf;
1063 struct nlattr *nested, *key;
1066 /* 'mask' is big enough to hold any key. */
1067 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1069 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1070 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1074 if (s[retval + 4] != ')') {
1078 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1081 size = nl_attr_get_size(mask);
1082 if (size == nl_attr_get_size(key)) {
1083 /* Change to masked set action if not fully masked. */
1084 if (!is_all_ones(mask + 1, size)) {
1085 key->nla_len += size;
1086 ofpbuf_put(actions, mask + 1, size);
1087 /* 'actions' may have been reallocated by ofpbuf_put(). */
1088 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1089 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1093 nl_msg_end_nested(actions, start_ofs);
1098 struct ovs_action_push_vlan push;
1099 int tpid = ETH_TYPE_VLAN;
1104 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1105 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1106 &vid, &pcp, &cfi, &n)
1107 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1108 &tpid, &vid, &pcp, &n)
1109 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1110 &tpid, &vid, &pcp, &cfi, &n)) {
1111 push.vlan_tpid = htons(tpid);
1112 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1113 | (pcp << VLAN_PCP_SHIFT)
1114 | (cfi ? VLAN_CFI : 0));
1115 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1116 &push, sizeof push);
1122 if (!strncmp(s, "pop_vlan", 8)) {
1123 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1131 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1132 && percentage >= 0. && percentage <= 100.0) {
1133 size_t sample_ofs, actions_ofs;
1136 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1137 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1138 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1139 (probability <= 0 ? 0
1140 : probability >= UINT32_MAX ? UINT32_MAX
1143 actions_ofs = nl_msg_start_nested(actions,
1144 OVS_SAMPLE_ATTR_ACTIONS);
1148 n += strspn(s + n, delimiters);
1153 retval = parse_odp_action(s + n, port_names, actions);
1159 nl_msg_end_nested(actions, actions_ofs);
1160 nl_msg_end_nested(actions, sample_ofs);
1162 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1170 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1171 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1177 struct ovs_action_push_tnl data;
1180 n = ovs_parse_tnl_push(s, &data);
1182 odp_put_tnl_push_action(actions, &data);
1191 /* Parses the string representation of datapath actions, in the format output
1192 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1193 * value. On success, the ODP actions are appended to 'actions' as a series of
1194 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1195 * way, 'actions''s data might be reallocated. */
1197 odp_actions_from_string(const char *s, const struct simap *port_names,
1198 struct ofpbuf *actions)
1202 if (!strcasecmp(s, "drop")) {
1206 old_size = actions->size;
1210 s += strspn(s, delimiters);
1215 retval = parse_odp_action(s, port_names, actions);
1216 if (retval < 0 || !strchr(delimiters, s[retval])) {
1217 actions->size = old_size;
1226 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1227 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1230 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1231 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1232 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1233 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1234 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1235 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1236 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1237 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1238 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1239 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1240 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1241 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1242 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1243 .next = ovs_vxlan_ext_attr_lens ,
1244 .next_max = OVS_VXLAN_EXT_MAX},
1247 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1248 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1249 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1250 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1251 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1252 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1253 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1254 .next = ovs_tun_key_attr_lens,
1255 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1256 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1257 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1258 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1259 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1260 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1261 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1262 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1263 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1264 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1265 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1266 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1267 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1268 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1269 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1270 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1273 /* Returns the correct length of the payload for a flow key attribute of the
1274 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1275 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1276 * payload is a nested type. */
1278 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1280 if (type > max_len) {
1281 return ATTR_LEN_INVALID;
1284 return tbl[type].len;
1288 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1290 size_t len = nl_attr_get_size(a);
1292 const uint8_t *unspec;
1295 unspec = nl_attr_get(a);
1296 for (i = 0; i < len; i++) {
1298 ds_put_char(ds, ' ');
1300 ds_put_format(ds, "%02x", unspec[i]);
1306 ovs_frag_type_to_string(enum ovs_frag_type type)
1309 case OVS_FRAG_TYPE_NONE:
1311 case OVS_FRAG_TYPE_FIRST:
1313 case OVS_FRAG_TYPE_LATER:
1315 case __OVS_FRAG_TYPE_MAX:
1321 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1323 parse_geneve_opts(const struct nlattr *attr)
1325 int opts_len = nl_attr_get_size(attr);
1326 const struct geneve_opt *opt = nl_attr_get(attr);
1328 while (opts_len > 0) {
1331 if (opts_len < sizeof(*opt)) {
1335 len = sizeof(*opt) + opt->length * 4;
1336 if (len > opts_len) {
1340 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1342 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1347 opt = opt + len / sizeof(*opt);
1354 enum odp_key_fitness
1355 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1358 const struct nlattr *a;
1360 bool unknown = false;
1362 NL_NESTED_FOR_EACH(a, left, attr) {
1363 uint16_t type = nl_attr_type(a);
1364 size_t len = nl_attr_get_size(a);
1365 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1366 OVS_TUNNEL_ATTR_MAX, type);
1368 if (len != expected_len && expected_len >= 0) {
1369 return ODP_FIT_ERROR;
1373 case OVS_TUNNEL_KEY_ATTR_ID:
1374 tun->tun_id = nl_attr_get_be64(a);
1375 tun->flags |= FLOW_TNL_F_KEY;
1377 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1378 tun->ip_src = nl_attr_get_be32(a);
1380 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1381 tun->ip_dst = nl_attr_get_be32(a);
1383 case OVS_TUNNEL_KEY_ATTR_TOS:
1384 tun->ip_tos = nl_attr_get_u8(a);
1386 case OVS_TUNNEL_KEY_ATTR_TTL:
1387 tun->ip_ttl = nl_attr_get_u8(a);
1390 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1391 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1393 case OVS_TUNNEL_KEY_ATTR_CSUM:
1394 tun->flags |= FLOW_TNL_F_CSUM;
1396 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1397 tun->tp_src = nl_attr_get_be16(a);
1399 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1400 tun->tp_dst = nl_attr_get_be16(a);
1402 case OVS_TUNNEL_KEY_ATTR_OAM:
1403 tun->flags |= FLOW_TNL_F_OAM;
1405 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1406 static const struct nl_policy vxlan_opts_policy[] = {
1407 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1409 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1411 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1412 return ODP_FIT_ERROR;
1415 if (ext[OVS_VXLAN_EXT_GBP]) {
1416 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1418 tun->gbp_id = htons(gbp & 0xFFFF);
1419 tun->gbp_flags = (gbp >> 16) & 0xFF;
1424 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1425 if (parse_geneve_opts(a)) {
1426 return ODP_FIT_ERROR;
1428 /* It is necessary to reproduce options exactly (including order)
1429 * so it's easiest to just echo them back. */
1434 /* Allow this to show up as unexpected, if there are unknown
1435 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1442 return ODP_FIT_ERROR;
1445 return ODP_FIT_TOO_MUCH;
1447 return ODP_FIT_PERFECT;
1451 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1455 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1457 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1458 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1459 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1461 if (tun_key->ip_src) {
1462 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1464 if (tun_key->ip_dst) {
1465 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1467 if (tun_key->ip_tos) {
1468 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1470 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1471 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1472 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1474 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1475 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1477 if (tun_key->tp_src) {
1478 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1480 if (tun_key->tp_dst) {
1481 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1483 if (tun_key->flags & FLOW_TNL_F_OAM) {
1484 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1486 if (tun_key->gbp_flags || tun_key->gbp_id) {
1487 size_t vxlan_opts_ofs;
1489 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1490 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1491 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1492 nl_msg_end_nested(a, vxlan_opts_ofs);
1495 nl_msg_end_nested(a, tun_key_ofs);
1499 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1501 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1505 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1507 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1508 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1510 if (attr == OVS_KEY_ATTR_IPV6) {
1511 const struct ovs_key_ipv6 *ipv6_mask = mask;
1514 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1515 == htonl(IPV6_LABEL_MASK))
1516 && ipv6_mask->ipv6_proto == UINT8_MAX
1517 && ipv6_mask->ipv6_tclass == UINT8_MAX
1518 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1519 && ipv6_mask->ipv6_frag == UINT8_MAX
1520 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1521 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1523 if (attr == OVS_KEY_ATTR_TUNNEL) {
1524 const struct flow_tnl *tun_mask = mask;
1526 return tun_mask->flags == FLOW_TNL_F_MASK
1527 && tun_mask->tun_id == OVS_BE64_MAX
1528 && tun_mask->ip_src == OVS_BE32_MAX
1529 && tun_mask->ip_dst == OVS_BE32_MAX
1530 && tun_mask->ip_tos == UINT8_MAX
1531 && tun_mask->ip_ttl == UINT8_MAX
1532 && tun_mask->tp_src == OVS_BE16_MAX
1533 && tun_mask->tp_dst == OVS_BE16_MAX
1534 && tun_mask->gbp_id == OVS_BE16_MAX
1535 && tun_mask->gbp_flags == UINT8_MAX;
1538 if (attr == OVS_KEY_ATTR_ARP) {
1539 /* ARP key has padding, ignore it. */
1540 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1541 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1542 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1543 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1546 return is_all_ones(mask, size);
1550 odp_mask_attr_is_exact(const struct nlattr *ma)
1552 struct flow_tnl tun_mask;
1553 enum ovs_key_attr attr = nl_attr_type(ma);
1557 if (attr == OVS_KEY_ATTR_TUNNEL) {
1558 memset(&tun_mask, 0, sizeof tun_mask);
1559 odp_tun_key_from_attr(ma, &tun_mask);
1561 size = sizeof tun_mask;
1563 mask = nl_attr_get(ma);
1564 size = nl_attr_get_size(ma);
1567 return odp_mask_is_exact(attr, mask, size);
1571 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1574 struct odp_portno_names *odp_portno_names;
1576 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1577 odp_portno_names->port_no = port_no;
1578 odp_portno_names->name = xstrdup(port_name);
1579 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1580 hash_odp_port(port_no));
1584 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1586 struct odp_portno_names *odp_portno_names;
1588 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1589 hash_odp_port(port_no), portno_names) {
1590 if (odp_portno_names->port_no == port_no) {
1591 return odp_portno_names->name;
1598 odp_portno_names_destroy(struct hmap *portno_names)
1600 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1601 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1602 hmap_node, portno_names) {
1603 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1604 free(odp_portno_names->name);
1605 free(odp_portno_names);
1609 /* Format helpers. */
1612 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1613 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1615 bool mask_empty = mask && eth_addr_is_zero(*mask);
1617 if (verbose || !mask_empty) {
1618 bool mask_full = !mask || eth_mask_is_exact(*mask);
1621 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1623 ds_put_format(ds, "%s=", name);
1624 eth_format_masked(key, *mask, ds);
1625 ds_put_char(ds, ',');
1631 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1632 const ovs_be64 *mask, bool verbose)
1634 bool mask_empty = mask && !*mask;
1636 if (verbose || !mask_empty) {
1637 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1639 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1640 if (!mask_full) { /* Partially masked. */
1641 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1643 ds_put_char(ds, ',');
1648 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1649 const ovs_be32 *mask, bool verbose)
1651 bool mask_empty = mask && !*mask;
1653 if (verbose || !mask_empty) {
1654 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1656 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1657 if (!mask_full) { /* Partially masked. */
1658 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1660 ds_put_char(ds, ',');
1665 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1666 const ovs_be32 (*mask_)[4], bool verbose)
1668 char buf[INET6_ADDRSTRLEN];
1669 const struct in6_addr *key = (const struct in6_addr *)key_;
1670 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1672 bool mask_empty = mask && ipv6_mask_is_any(mask);
1674 if (verbose || !mask_empty) {
1675 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1677 inet_ntop(AF_INET6, key, buf, sizeof buf);
1678 ds_put_format(ds, "%s=%s", name, buf);
1679 if (!mask_full) { /* Partially masked. */
1680 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1681 ds_put_format(ds, "/%s", buf);
1683 ds_put_char(ds, ',');
1688 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1689 const ovs_be32 *mask, bool verbose)
1691 bool mask_empty = mask && !*mask;
1693 if (verbose || !mask_empty) {
1694 bool mask_full = !mask
1695 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1697 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1698 if (!mask_full) { /* Partially masked. */
1699 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1701 ds_put_char(ds, ',');
1706 format_u8x(struct ds *ds, const char *name, uint8_t key,
1707 const uint8_t *mask, bool verbose)
1709 bool mask_empty = mask && !*mask;
1711 if (verbose || !mask_empty) {
1712 bool mask_full = !mask || *mask == UINT8_MAX;
1714 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1715 if (!mask_full) { /* Partially masked. */
1716 ds_put_format(ds, "/%#"PRIx8, *mask);
1718 ds_put_char(ds, ',');
1723 format_u8u(struct ds *ds, const char *name, uint8_t key,
1724 const uint8_t *mask, bool verbose)
1726 bool mask_empty = mask && !*mask;
1728 if (verbose || !mask_empty) {
1729 bool mask_full = !mask || *mask == UINT8_MAX;
1731 ds_put_format(ds, "%s=%"PRIu8, name, key);
1732 if (!mask_full) { /* Partially masked. */
1733 ds_put_format(ds, "/%#"PRIx8, *mask);
1735 ds_put_char(ds, ',');
1740 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1741 const ovs_be16 *mask, bool verbose)
1743 bool mask_empty = mask && !*mask;
1745 if (verbose || !mask_empty) {
1746 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1748 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1749 if (!mask_full) { /* Partially masked. */
1750 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1752 ds_put_char(ds, ',');
1757 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1758 const ovs_be16 *mask, bool verbose)
1760 bool mask_empty = mask && !*mask;
1762 if (verbose || !mask_empty) {
1763 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1765 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1766 if (!mask_full) { /* Partially masked. */
1767 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1769 ds_put_char(ds, ',');
1774 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1775 const uint16_t *mask, bool verbose)
1777 bool mask_empty = mask && !*mask;
1779 if (verbose || !mask_empty) {
1780 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1782 ds_put_cstr(ds, name);
1783 ds_put_char(ds, '(');
1784 if (!mask_full) { /* Partially masked. */
1785 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1786 } else { /* Fully masked. */
1787 format_flags(ds, flow_tun_flag_to_string, key, ',');
1789 ds_put_cstr(ds, "),");
1794 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1795 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1799 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1800 if (expected_len != ATTR_LEN_VARIABLE &&
1801 expected_len != ATTR_LEN_NESTED) {
1803 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1804 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1806 if (bad_key_len || bad_mask_len) {
1808 ds_put_format(ds, "key%u", nl_attr_type(a));
1811 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1812 nl_attr_get_size(a), expected_len);
1814 format_generic_odp_key(a, ds);
1816 ds_put_char(ds, '/');
1818 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1819 nl_attr_get_size(ma), expected_len);
1821 format_generic_odp_key(ma, ds);
1823 ds_put_char(ds, ')');
1832 format_unknown_key(struct ds *ds, const struct nlattr *a,
1833 const struct nlattr *ma)
1835 ds_put_format(ds, "key%u(", nl_attr_type(a));
1836 format_generic_odp_key(a, ds);
1837 if (ma && !odp_mask_attr_is_exact(ma)) {
1838 ds_put_char(ds, '/');
1839 format_generic_odp_key(ma, ds);
1841 ds_put_cstr(ds, "),");
1845 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1846 const struct nlattr *mask_attr, struct ds *ds,
1850 const struct nlattr *a;
1853 ofpbuf_init(&ofp, 100);
1854 NL_NESTED_FOR_EACH(a, left, attr) {
1855 uint16_t type = nl_attr_type(a);
1856 const struct nlattr *ma = NULL;
1859 ma = nl_attr_find__(nl_attr_get(mask_attr),
1860 nl_attr_get_size(mask_attr), type);
1862 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1868 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1869 OVS_VXLAN_EXT_MAX, true)) {
1874 case OVS_VXLAN_EXT_GBP: {
1875 uint32_t key = nl_attr_get_u32(a);
1876 ovs_be16 id, id_mask;
1877 uint8_t flags, flags_mask;
1879 id = htons(key & 0xFFFF);
1880 flags = (key >> 16) & 0xFF;
1882 uint32_t mask = nl_attr_get_u32(ma);
1883 id_mask = htons(mask & 0xFFFF);
1884 flags_mask = (mask >> 16) & 0xFF;
1887 ds_put_cstr(ds, "gbp(");
1888 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1889 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1891 ds_put_cstr(ds, "),");
1896 format_unknown_key(ds, a, ma);
1902 ofpbuf_uninit(&ofp);
1905 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1908 format_odp_tun_geneve(const struct nlattr *attr,
1909 const struct nlattr *mask_attr, struct ds *ds,
1912 int opts_len = nl_attr_get_size(attr);
1913 const struct geneve_opt *opt = nl_attr_get(attr);
1914 const struct geneve_opt *mask = mask_attr ?
1915 nl_attr_get(mask_attr) : NULL;
1917 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1918 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1919 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1923 while (opts_len > 0) {
1925 uint8_t data_len, data_len_mask;
1927 if (opts_len < sizeof *opt) {
1928 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1929 opts_len, sizeof *opt);
1933 data_len = opt->length * 4;
1935 if (mask->length == 0x1f) {
1936 data_len_mask = UINT8_MAX;
1938 data_len_mask = mask->length;
1941 len = sizeof *opt + data_len;
1942 if (len > opts_len) {
1943 ds_put_format(ds, "opt len %u greater than remaining %u",
1948 ds_put_char(ds, '{');
1949 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1951 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1952 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1953 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1954 ds_put_hex(ds, opt + 1, data_len);
1955 if (mask && !is_all_ones(mask + 1, data_len)) {
1956 ds_put_char(ds, '/');
1957 ds_put_hex(ds, mask + 1, data_len);
1962 ds_put_char(ds, '}');
1964 opt += len / sizeof(*opt);
1966 mask += len / sizeof(*opt);
1973 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1974 struct ds *ds, bool verbose)
1977 const struct nlattr *a;
1979 uint16_t mask_flags = 0;
1982 ofpbuf_init(&ofp, 100);
1983 NL_NESTED_FOR_EACH(a, left, attr) {
1984 enum ovs_tunnel_key_attr type = nl_attr_type(a);
1985 const struct nlattr *ma = NULL;
1988 ma = nl_attr_find__(nl_attr_get(mask_attr),
1989 nl_attr_get_size(mask_attr), type);
1991 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
1992 OVS_TUNNEL_KEY_ATTR_MAX,
1997 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
1998 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2003 case OVS_TUNNEL_KEY_ATTR_ID:
2004 format_be64(ds, "tun_id", nl_attr_get_be64(a),
2005 ma ? nl_attr_get(ma) : NULL, verbose);
2006 flags |= FLOW_TNL_F_KEY;
2008 mask_flags |= FLOW_TNL_F_KEY;
2011 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2012 format_ipv4(ds, "src", nl_attr_get_be32(a),
2013 ma ? nl_attr_get(ma) : NULL, verbose);
2015 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2016 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2017 ma ? nl_attr_get(ma) : NULL, verbose);
2019 case OVS_TUNNEL_KEY_ATTR_TOS:
2020 format_u8x(ds, "tos", nl_attr_get_u8(a),
2021 ma ? nl_attr_get(ma) : NULL, verbose);
2023 case OVS_TUNNEL_KEY_ATTR_TTL:
2024 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2025 ma ? nl_attr_get(ma) : NULL, verbose);
2027 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2028 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2030 case OVS_TUNNEL_KEY_ATTR_CSUM:
2031 flags |= FLOW_TNL_F_CSUM;
2033 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2034 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2035 ma ? nl_attr_get(ma) : NULL, verbose);
2037 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2038 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2039 ma ? nl_attr_get(ma) : NULL, verbose);
2041 case OVS_TUNNEL_KEY_ATTR_OAM:
2042 flags |= FLOW_TNL_F_OAM;
2044 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2045 ds_put_cstr(ds, "vxlan(");
2046 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2047 ds_put_cstr(ds, "),");
2049 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2050 ds_put_cstr(ds, "geneve(");
2051 format_odp_tun_geneve(a, ma, ds, verbose);
2052 ds_put_cstr(ds, "),");
2054 case __OVS_TUNNEL_KEY_ATTR_MAX:
2056 format_unknown_key(ds, a, ma);
2061 /* Flags can have a valid mask even if the attribute is not set, so
2062 * we need to collect these separately. */
2064 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2065 switch (nl_attr_type(a)) {
2066 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2067 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2069 case OVS_TUNNEL_KEY_ATTR_CSUM:
2070 mask_flags |= FLOW_TNL_F_CSUM;
2072 case OVS_TUNNEL_KEY_ATTR_OAM:
2073 mask_flags |= FLOW_TNL_F_OAM;
2079 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2082 ofpbuf_uninit(&ofp);
2086 format_frag(struct ds *ds, const char *name, uint8_t key,
2087 const uint8_t *mask, bool verbose)
2089 bool mask_empty = mask && !*mask;
2091 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2092 if (verbose || !mask_empty) {
2093 bool mask_full = !mask || *mask == UINT8_MAX;
2095 if (!mask_full) { /* Partially masked. */
2096 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2099 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2105 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2106 const struct hmap *portno_names, struct ds *ds,
2109 enum ovs_key_attr attr = nl_attr_type(a);
2110 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2113 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2115 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2117 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2118 OVS_KEY_ATTR_MAX, false)) {
2122 ds_put_char(ds, '(');
2124 case OVS_KEY_ATTR_ENCAP:
2125 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2126 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2127 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2129 } else if (nl_attr_get_size(a)) {
2130 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2135 case OVS_KEY_ATTR_PRIORITY:
2136 case OVS_KEY_ATTR_SKB_MARK:
2137 case OVS_KEY_ATTR_DP_HASH:
2138 case OVS_KEY_ATTR_RECIRC_ID:
2139 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2141 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2145 case OVS_KEY_ATTR_TUNNEL:
2146 format_odp_tun_attr(a, ma, ds, verbose);
2149 case OVS_KEY_ATTR_IN_PORT:
2150 if (portno_names && verbose && is_exact) {
2151 char *name = odp_portno_names_get(portno_names,
2152 u32_to_odp(nl_attr_get_u32(a)));
2154 ds_put_format(ds, "%s", name);
2156 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2159 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2161 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2166 case OVS_KEY_ATTR_ETHERNET: {
2167 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2168 const struct ovs_key_ethernet *key = nl_attr_get(a);
2170 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2171 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2175 case OVS_KEY_ATTR_VLAN:
2176 format_vlan_tci(ds, nl_attr_get_be16(a),
2177 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2180 case OVS_KEY_ATTR_MPLS: {
2181 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2182 const struct ovs_key_mpls *mpls_mask = NULL;
2183 size_t size = nl_attr_get_size(a);
2185 if (!size || size % sizeof *mpls_key) {
2186 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2190 mpls_mask = nl_attr_get(ma);
2191 if (size != nl_attr_get_size(ma)) {
2192 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2193 "mask length %"PRIuSIZE")",
2194 size, nl_attr_get_size(ma));
2198 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2201 case OVS_KEY_ATTR_ETHERTYPE:
2202 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2204 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2208 case OVS_KEY_ATTR_IPV4: {
2209 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2210 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2212 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2213 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2214 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2216 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2217 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2218 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2223 case OVS_KEY_ATTR_IPV6: {
2224 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2225 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2227 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2228 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2229 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2231 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2233 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2235 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2237 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2242 /* These have the same structure and format. */
2243 case OVS_KEY_ATTR_TCP:
2244 case OVS_KEY_ATTR_UDP:
2245 case OVS_KEY_ATTR_SCTP: {
2246 const struct ovs_key_tcp *key = nl_attr_get(a);
2247 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2249 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2250 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2254 case OVS_KEY_ATTR_TCP_FLAGS:
2256 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2257 ntohs(nl_attr_get_be16(a)),
2258 ntohs(nl_attr_get_be16(ma)));
2260 format_flags(ds, packet_tcp_flag_to_string,
2261 ntohs(nl_attr_get_be16(a)), ',');
2265 case OVS_KEY_ATTR_ICMP: {
2266 const struct ovs_key_icmp *key = nl_attr_get(a);
2267 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2269 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2270 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2274 case OVS_KEY_ATTR_ICMPV6: {
2275 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2276 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2278 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2280 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2285 case OVS_KEY_ATTR_ARP: {
2286 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2287 const struct ovs_key_arp *key = nl_attr_get(a);
2289 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2290 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2291 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2292 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2293 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2297 case OVS_KEY_ATTR_ND: {
2298 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2299 const struct ovs_key_nd *key = nl_attr_get(a);
2301 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2303 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2304 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2309 case OVS_KEY_ATTR_UNSPEC:
2310 case __OVS_KEY_ATTR_MAX:
2312 format_generic_odp_key(a, ds);
2314 ds_put_char(ds, '/');
2315 format_generic_odp_key(ma, ds);
2319 ds_put_char(ds, ')');
2322 static struct nlattr *
2323 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2324 struct ofpbuf *ofp, const struct nlattr *key)
2326 const struct nlattr *a;
2328 int type = nl_attr_type(key);
2329 int size = nl_attr_get_size(key);
2331 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2332 nl_msg_put_unspec_zero(ofp, type, size);
2336 if (tbl[type].next) {
2337 tbl = tbl[type].next;
2338 max = tbl[type].next_max;
2341 nested_mask = nl_msg_start_nested(ofp, type);
2342 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2343 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2345 nl_msg_end_nested(ofp, nested_mask);
2352 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2356 if (ovs_scan(s, "ufid:")) {
2360 if (ovs_scan(s, "0x")) {
2364 n = strspn(s, hex_chars);
2369 if (!ovs_scan(s, "%16"SCNx64"%16"SCNx64, &ufid->u64.hi,
2374 s += strspn(s, delimiters);
2383 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2385 ds_put_format(ds, "ufid:%016"PRIx64"%016"PRIx64, ufid->u64.hi,
2389 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2390 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2391 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2392 * non-null and 'verbose' is true, translates odp port number to its name. */
2394 odp_flow_format(const struct nlattr *key, size_t key_len,
2395 const struct nlattr *mask, size_t mask_len,
2396 const struct hmap *portno_names, struct ds *ds, bool verbose)
2399 const struct nlattr *a;
2401 bool has_ethtype_key = false;
2402 const struct nlattr *ma = NULL;
2404 bool first_field = true;
2406 ofpbuf_init(&ofp, 100);
2407 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2408 bool is_nested_attr;
2409 bool is_wildcard = false;
2410 int attr_type = nl_attr_type(a);
2412 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2413 has_ethtype_key = true;
2416 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2417 OVS_KEY_ATTR_MAX, attr_type) ==
2420 if (mask && mask_len) {
2421 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2422 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2425 if (verbose || !is_wildcard || is_nested_attr) {
2426 if (is_wildcard && !ma) {
2427 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2432 ds_put_char(ds, ',');
2434 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2435 first_field = false;
2439 ofpbuf_uninit(&ofp);
2444 if (left == key_len) {
2445 ds_put_cstr(ds, "<empty>");
2447 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2448 for (i = 0; i < left; i++) {
2449 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2451 ds_put_char(ds, ')');
2453 if (!has_ethtype_key) {
2454 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2456 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2457 ntohs(nl_attr_get_be16(ma)));
2461 ds_put_cstr(ds, "<empty>");
2465 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2466 * OVS_KEY_ATTR_* attributes in 'key'. */
2468 odp_flow_key_format(const struct nlattr *key,
2469 size_t key_len, struct ds *ds)
2471 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2475 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2477 if (!strcasecmp(s, "no")) {
2478 *type = OVS_FRAG_TYPE_NONE;
2479 } else if (!strcasecmp(s, "first")) {
2480 *type = OVS_FRAG_TYPE_FIRST;
2481 } else if (!strcasecmp(s, "later")) {
2482 *type = OVS_FRAG_TYPE_LATER;
2492 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2493 uint8_t (*mask)[ETH_ADDR_LEN])
2497 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2501 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2502 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2505 memset(mask, 0xff, sizeof *mask);
2514 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2518 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2522 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2523 IP_SCAN_ARGS(mask), &n)) {
2526 *mask = OVS_BE32_MAX;
2535 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2538 char ipv6_s[IPV6_SCAN_LEN + 1];
2540 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2541 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2545 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2546 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2549 memset(mask, 0xff, sizeof *mask);
2558 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2563 if (ovs_scan(s, "%i%n", &key_, &n)
2564 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2569 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2570 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2572 *mask = htonl(mask_);
2574 *mask = htonl(IPV6_LABEL_MASK);
2583 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2587 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2591 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2603 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2607 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2611 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2623 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2625 uint16_t key_, mask_;
2628 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2633 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2635 *mask = htons(mask_);
2637 *mask = OVS_BE16_MAX;
2646 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2648 uint64_t key_, mask_;
2651 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2654 *key = htonll(key_);
2656 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2658 *mask = htonll(mask_);
2660 *mask = OVS_BE64_MAX;
2669 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2671 uint32_t flags, fmask;
2674 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2675 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2676 if (n >= 0 && s[n] == ')') {
2687 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2689 uint32_t flags, fmask;
2692 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2693 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2695 *key = htons(flags);
2697 *mask = htons(fmask);
2705 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2709 enum ovs_frag_type frag_type;
2711 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2712 && ovs_frag_type_from_string(frag, &frag_type)) {
2725 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2726 const struct simap *port_names)
2730 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2734 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2741 } else if (port_names) {
2742 const struct simap_node *node;
2745 len = strcspn(s, ")");
2746 node = simap_find_len(port_names, s, len);
2759 /* Helper for vlan parsing. */
2760 struct ovs_key_vlan__ {
2765 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2767 const uint16_t mask = ((1U << bits) - 1) << offset;
2769 if (value >> bits) {
2773 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2778 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2781 uint16_t key_, mask_;
2784 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2787 if (set_be16_bf(key, bits, offset, key_)) {
2789 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2792 if (!set_be16_bf(mask, bits, offset, mask_)) {
2796 *mask |= htons(((1U << bits) - 1) << offset);
2806 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2808 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2812 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2814 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2818 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2820 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2825 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2827 const uint32_t mask = ((1U << bits) - 1) << offset;
2829 if (value >> bits) {
2833 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2838 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2841 uint32_t key_, mask_;
2844 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2847 if (set_be32_bf(key, bits, offset, key_)) {
2849 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2852 if (!set_be32_bf(mask, bits, offset, mask_)) {
2856 *mask |= htonl(((1U << bits) - 1) << offset);
2866 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2868 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2872 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2874 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2878 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2880 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2884 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2886 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2890 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2892 const char *s_base = s;
2893 ovs_be16 id = 0, id_mask = 0;
2894 uint8_t flags = 0, flags_mask = 0;
2896 if (!strncmp(s, "id=", 3)) {
2898 s += scan_be16(s, &id, mask ? &id_mask : NULL);
2904 if (!strncmp(s, "flags=", 6)) {
2906 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2909 if (!strncmp(s, "))", 2)) {
2912 *key = (flags << 16) | ntohs(id);
2914 *mask = (flags_mask << 16) | ntohs(id_mask);
2923 struct geneve_scan {
2924 struct geneve_opt d[63];
2929 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2931 const char *s_base = s;
2932 struct geneve_opt *opt = key->d;
2933 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2934 int len_remain = sizeof key->d;
2936 while (s[0] == '{' && len_remain >= sizeof *opt) {
2940 len_remain -= sizeof *opt;
2942 if (!strncmp(s, "class=", 6)) {
2944 s += scan_be16(s, &opt->opt_class,
2945 mask ? &opt_mask->opt_class : NULL);
2947 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2953 if (!strncmp(s, "type=", 5)) {
2955 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2957 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2963 if (!strncmp(s, "len=", 4)) {
2964 uint8_t opt_len, opt_len_mask;
2966 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2968 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2971 opt->length = opt_len / 4;
2973 opt_mask->length = opt_len_mask;
2977 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2983 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2990 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2991 data_len, (char **)&s)) {
3002 opt += 1 + data_len / 4;
3004 opt_mask += 1 + data_len / 4;
3006 len_remain -= data_len;
3011 int len = sizeof key->d - len_remain;
3025 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3027 const uint16_t *flags = data_;
3029 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3030 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3032 if (*flags & FLOW_TNL_F_CSUM) {
3033 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3035 if (*flags & FLOW_TNL_F_OAM) {
3036 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3041 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3043 const uint32_t *gbp = data_;
3046 size_t vxlan_opts_ofs;
3048 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3049 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3050 nl_msg_end_nested(a, vxlan_opts_ofs);
3055 geneve_to_attr(struct ofpbuf *a, const void *data_)
3057 const struct geneve_scan *geneve = data_;
3059 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3063 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3065 unsigned long call_fn = (unsigned long)FUNC; \
3067 typedef void (*fn)(struct ofpbuf *, const void *); \
3069 func(BUF, &(DATA)); \
3071 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3075 #define SCAN_IF(NAME) \
3076 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3077 const char *start = s; \
3082 /* Usually no special initialization is needed. */
3083 #define SCAN_BEGIN(NAME, TYPE) \
3086 memset(&skey, 0, sizeof skey); \
3087 memset(&smask, 0, sizeof smask); \
3091 /* Init as fully-masked as mask will not be scanned. */
3092 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3095 memset(&skey, 0, sizeof skey); \
3096 memset(&smask, 0xff, sizeof smask); \
3100 /* VLAN needs special initialization. */
3101 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3103 TYPE skey = KEY_INIT; \
3104 TYPE smask = MASK_INIT; \
3108 /* Scan unnamed entry as 'TYPE' */
3109 #define SCAN_TYPE(TYPE, KEY, MASK) \
3110 len = scan_##TYPE(s, KEY, MASK); \
3116 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3117 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3118 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3119 s += strlen(NAME); \
3120 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3124 #define SCAN_FINISH() \
3125 } while (*s++ == ',' && len != 0); \
3126 if (s[-1] != ')') { \
3130 #define SCAN_FINISH_SINGLE() \
3132 if (*s++ != ')') { \
3136 /* Beginning of nested attribute. */
3137 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3139 size_t key_offset, mask_offset; \
3140 key_offset = nl_msg_start_nested(key, ATTR); \
3142 mask_offset = nl_msg_start_nested(mask, ATTR); \
3147 #define SCAN_END_NESTED() \
3149 nl_msg_end_nested(key, key_offset); \
3151 nl_msg_end_nested(mask, mask_offset); \
3156 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3157 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3159 memset(&skey, 0, sizeof skey); \
3160 memset(&smask, 0xff, sizeof smask); \
3161 s += strlen(NAME); \
3162 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3163 SCAN_PUT(ATTR, FUNC); \
3167 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3168 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3170 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3171 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3173 #define SCAN_PUT(ATTR, FUNC) \
3174 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3175 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3177 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3181 #define SCAN_END(ATTR) \
3183 SCAN_PUT(ATTR, NULL); \
3187 #define SCAN_END_SINGLE(ATTR) \
3188 SCAN_FINISH_SINGLE(); \
3189 SCAN_PUT(ATTR, NULL); \
3193 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3194 SCAN_BEGIN(NAME, TYPE) { \
3195 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3196 } SCAN_END_SINGLE(ATTR)
3198 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3199 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3200 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3201 } SCAN_END_SINGLE(ATTR)
3203 /* scan_port needs one extra argument. */
3204 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3205 SCAN_BEGIN(NAME, TYPE) { \
3206 len = scan_port(s, &skey, &smask, port_names); \
3211 } SCAN_END_SINGLE(ATTR)
3214 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3215 struct ofpbuf *key, struct ofpbuf *mask)
3217 if (!strncmp(s, "ufid:", 5)) {
3218 const char *start = s;
3222 s += strspn(s, hex_chars);
3223 s += strspn(s, delimiters);
3228 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3229 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3230 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3231 OVS_KEY_ATTR_RECIRC_ID);
3232 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3234 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3235 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3236 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3237 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3238 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3239 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3240 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3241 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3242 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3243 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3245 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3246 } SCAN_END_NESTED();
3248 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3250 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3251 SCAN_FIELD("src=", eth, eth_src);
3252 SCAN_FIELD("dst=", eth, eth_dst);
3253 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3255 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3256 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3257 SCAN_FIELD("vid=", vid, tci);
3258 SCAN_FIELD("pcp=", pcp, tci);
3259 SCAN_FIELD("cfi=", cfi, tci);
3260 } SCAN_END(OVS_KEY_ATTR_VLAN);
3262 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3264 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3265 SCAN_FIELD("label=", mpls_label, mpls_lse);
3266 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3267 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3268 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3269 } SCAN_END(OVS_KEY_ATTR_MPLS);
3271 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3272 SCAN_FIELD("src=", ipv4, ipv4_src);
3273 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3274 SCAN_FIELD("proto=", u8, ipv4_proto);
3275 SCAN_FIELD("tos=", u8, ipv4_tos);
3276 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3277 SCAN_FIELD("frag=", frag, ipv4_frag);
3278 } SCAN_END(OVS_KEY_ATTR_IPV4);
3280 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3281 SCAN_FIELD("src=", ipv6, ipv6_src);
3282 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3283 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3284 SCAN_FIELD("proto=", u8, ipv6_proto);
3285 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3286 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3287 SCAN_FIELD("frag=", frag, ipv6_frag);
3288 } SCAN_END(OVS_KEY_ATTR_IPV6);
3290 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3291 SCAN_FIELD("src=", be16, tcp_src);
3292 SCAN_FIELD("dst=", be16, tcp_dst);
3293 } SCAN_END(OVS_KEY_ATTR_TCP);
3295 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3297 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3298 SCAN_FIELD("src=", be16, udp_src);
3299 SCAN_FIELD("dst=", be16, udp_dst);
3300 } SCAN_END(OVS_KEY_ATTR_UDP);
3302 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3303 SCAN_FIELD("src=", be16, sctp_src);
3304 SCAN_FIELD("dst=", be16, sctp_dst);
3305 } SCAN_END(OVS_KEY_ATTR_SCTP);
3307 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3308 SCAN_FIELD("type=", u8, icmp_type);
3309 SCAN_FIELD("code=", u8, icmp_code);
3310 } SCAN_END(OVS_KEY_ATTR_ICMP);
3312 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3313 SCAN_FIELD("type=", u8, icmpv6_type);
3314 SCAN_FIELD("code=", u8, icmpv6_code);
3315 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3317 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3318 SCAN_FIELD("sip=", ipv4, arp_sip);
3319 SCAN_FIELD("tip=", ipv4, arp_tip);
3320 SCAN_FIELD("op=", be16, arp_op);
3321 SCAN_FIELD("sha=", eth, arp_sha);
3322 SCAN_FIELD("tha=", eth, arp_tha);
3323 } SCAN_END(OVS_KEY_ATTR_ARP);
3325 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3326 SCAN_FIELD("target=", ipv6, nd_target);
3327 SCAN_FIELD("sll=", eth, nd_sll);
3328 SCAN_FIELD("tll=", eth, nd_tll);
3329 } SCAN_END(OVS_KEY_ATTR_ND);
3331 /* Encap open-coded. */
3332 if (!strncmp(s, "encap(", 6)) {
3333 const char *start = s;
3334 size_t encap, encap_mask = 0;
3336 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3338 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3345 s += strspn(s, delimiters);
3348 } else if (*s == ')') {
3352 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3360 nl_msg_end_nested(key, encap);
3362 nl_msg_end_nested(mask, encap_mask);
3371 /* Parses the string representation of a datapath flow key, in the
3372 * format output by odp_flow_key_format(). Returns 0 if successful,
3373 * otherwise a positive errno value. On success, the flow key is
3374 * appended to 'key' as a series of Netlink attributes. On failure, no
3375 * data is appended to 'key'. Either way, 'key''s data might be
3378 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3379 * to a port number. (Port names may be used instead of port numbers in
3382 * On success, the attributes appended to 'key' are individually syntactically
3383 * valid, but they may not be valid as a sequence. 'key' might, for example,
3384 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3386 odp_flow_from_string(const char *s, const struct simap *port_names,
3387 struct ofpbuf *key, struct ofpbuf *mask)
3389 const size_t old_size = key->size;
3393 s += strspn(s, delimiters);
3398 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3400 key->size = old_size;
3410 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3413 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3414 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3415 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3416 * must use a zero mask for the netlink frag field, and all ones mask
3418 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3420 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3421 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3422 : OVS_FRAG_TYPE_FIRST;
3425 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3426 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3427 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3429 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3431 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3433 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3435 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3436 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3437 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3438 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3440 /* These share the same layout. */
3442 struct ovs_key_tcp tcp;
3443 struct ovs_key_udp udp;
3444 struct ovs_key_sctp sctp;
3447 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3448 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3451 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
3452 const struct flow *mask, odp_port_t odp_in_port,
3453 size_t max_mpls_depth, bool recirc, bool export_mask)
3455 struct ovs_key_ethernet *eth_key;
3457 const struct flow *data = export_mask ? mask : flow;
3459 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3461 if (flow->tunnel.ip_dst || export_mask) {
3462 tun_key_to_attr(buf, &data->tunnel);
3465 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3468 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3469 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3472 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3473 * is not the magical value "ODPP_NONE". */
3474 if (export_mask || odp_in_port != ODPP_NONE) {
3475 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
3478 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3480 get_ethernet_key(data, eth_key);
3482 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3484 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3486 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3488 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3489 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3490 if (flow->vlan_tci == htons(0)) {
3497 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3498 /* For backwards compatibility with kernels that don't support
3499 * wildcarding, the following convention is used to encode the
3500 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3503 * -------- -------- -------
3504 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3505 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3506 * <none> 0xffff Any non-Ethernet II frame (except valid
3507 * 802.3 SNAP packet with valid eth_type).
3510 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3515 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3517 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3518 struct ovs_key_ipv4 *ipv4_key;
3520 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3522 get_ipv4_key(data, ipv4_key, export_mask);
3523 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3524 struct ovs_key_ipv6 *ipv6_key;
3526 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3528 get_ipv6_key(data, ipv6_key, export_mask);
3529 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3530 flow->dl_type == htons(ETH_TYPE_RARP)) {
3531 struct ovs_key_arp *arp_key;
3533 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3535 get_arp_key(data, arp_key);
3536 } else if (eth_type_mpls(flow->dl_type)) {
3537 struct ovs_key_mpls *mpls_key;
3540 n = flow_count_mpls_labels(flow, NULL);
3541 n = MIN(n, max_mpls_depth);
3542 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3543 n * sizeof *mpls_key);
3544 for (i = 0; i < n; i++) {
3545 mpls_key[i].mpls_lse = data->mpls_lse[i];
3549 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3550 if (flow->nw_proto == IPPROTO_TCP) {
3551 union ovs_key_tp *tcp_key;
3553 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3555 get_tp_key(data, tcp_key);
3556 if (data->tcp_flags) {
3557 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3559 } else if (flow->nw_proto == IPPROTO_UDP) {
3560 union ovs_key_tp *udp_key;
3562 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3564 get_tp_key(data, udp_key);
3565 } else if (flow->nw_proto == IPPROTO_SCTP) {
3566 union ovs_key_tp *sctp_key;
3568 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3570 get_tp_key(data, sctp_key);
3571 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3572 && flow->nw_proto == IPPROTO_ICMP) {
3573 struct ovs_key_icmp *icmp_key;
3575 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3577 icmp_key->icmp_type = ntohs(data->tp_src);
3578 icmp_key->icmp_code = ntohs(data->tp_dst);
3579 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3580 && flow->nw_proto == IPPROTO_ICMPV6) {
3581 struct ovs_key_icmpv6 *icmpv6_key;
3583 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3584 sizeof *icmpv6_key);
3585 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3586 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3588 if (flow->tp_dst == htons(0)
3589 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3590 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3591 && (!export_mask || (data->tp_src == htons(0xffff)
3592 && data->tp_dst == htons(0xffff)))) {
3594 struct ovs_key_nd *nd_key;
3596 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3598 memcpy(nd_key->nd_target, &data->nd_target,
3599 sizeof nd_key->nd_target);
3600 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3601 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3608 nl_msg_end_nested(buf, encap);
3612 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3613 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3614 * number rather than a datapath port number). Instead, if 'odp_in_port'
3615 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3618 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3619 * capable of being expanded to allow for that much space.
3621 * 'recirc' indicates support for recirculation fields. If this is true, then
3622 * these fields will always be serialised. */
3624 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
3625 const struct flow *mask, odp_port_t odp_in_port,
3628 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3632 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3633 * 'buf'. 'flow' is used as a template to determine how to interpret
3634 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3635 * it doesn't indicate whether the other fields should be interpreted as
3636 * ARP, IPv4, IPv6, etc.
3638 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3639 * capable of being expanded to allow for that much space.
3641 * 'recirc' indicates support for recirculation fields. If this is true, then
3642 * these fields will always be serialised. */
3644 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
3645 const struct flow *flow, uint32_t odp_in_port_mask,
3646 size_t max_mpls_depth, bool recirc)
3648 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3649 max_mpls_depth, recirc, true);
3652 /* Generate ODP flow key from the given packet metadata */
3654 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3656 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3658 if (md->tunnel.ip_dst) {
3659 tun_key_to_attr(buf, &md->tunnel);
3662 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3664 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3665 * value "ODPP_NONE". */
3666 if (md->in_port.odp_port != ODPP_NONE) {
3667 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3671 /* Generate packet metadata from the given ODP flow key. */
3673 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3674 struct pkt_metadata *md)
3676 const struct nlattr *nla;
3678 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3679 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3680 1u << OVS_KEY_ATTR_IN_PORT;
3682 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3684 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3685 uint16_t type = nl_attr_type(nla);
3686 size_t len = nl_attr_get_size(nla);
3687 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3688 OVS_KEY_ATTR_MAX, type);
3690 if (len != expected_len && expected_len >= 0) {
3695 case OVS_KEY_ATTR_RECIRC_ID:
3696 md->recirc_id = nl_attr_get_u32(nla);
3697 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3699 case OVS_KEY_ATTR_DP_HASH:
3700 md->dp_hash = nl_attr_get_u32(nla);
3701 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3703 case OVS_KEY_ATTR_PRIORITY:
3704 md->skb_priority = nl_attr_get_u32(nla);
3705 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3707 case OVS_KEY_ATTR_SKB_MARK:
3708 md->pkt_mark = nl_attr_get_u32(nla);
3709 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3711 case OVS_KEY_ATTR_TUNNEL: {
3712 enum odp_key_fitness res;
3714 res = odp_tun_key_from_attr(nla, &md->tunnel);
3715 if (res == ODP_FIT_ERROR) {
3716 memset(&md->tunnel, 0, sizeof md->tunnel);
3717 } else if (res == ODP_FIT_PERFECT) {
3718 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3722 case OVS_KEY_ATTR_IN_PORT:
3723 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3724 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3730 if (!wanted_attrs) {
3731 return; /* Have everything. */
3737 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3739 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3740 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3741 key_len / sizeof(uint32_t), 0);
3745 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3746 uint64_t attrs, int out_of_range_attr,
3747 const struct nlattr *key, size_t key_len)
3752 if (VLOG_DROP_DBG(rl)) {
3757 for (i = 0; i < 64; i++) {
3758 if (attrs & (UINT64_C(1) << i)) {
3759 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3761 ds_put_format(&s, " %s",
3762 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3765 if (out_of_range_attr) {
3766 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3769 ds_put_cstr(&s, ": ");
3770 odp_flow_key_format(key, key_len, &s);
3772 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3777 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3779 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3782 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3785 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3786 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3787 return 0xff; /* Error. */
3790 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3791 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3792 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3796 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3797 const struct nlattr *attrs[], uint64_t *present_attrsp,
3798 int *out_of_range_attrp)
3800 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3801 const struct nlattr *nla;
3802 uint64_t present_attrs;
3805 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3807 *out_of_range_attrp = 0;
3808 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3809 uint16_t type = nl_attr_type(nla);
3810 size_t len = nl_attr_get_size(nla);
3811 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3812 OVS_KEY_ATTR_MAX, type);
3814 if (len != expected_len && expected_len >= 0) {
3815 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3817 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3818 "length %d", ovs_key_attr_to_string(type, namebuf,
3824 if (type > OVS_KEY_ATTR_MAX) {
3825 *out_of_range_attrp = type;
3827 if (present_attrs & (UINT64_C(1) << type)) {
3828 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3830 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3831 ovs_key_attr_to_string(type,
3832 namebuf, sizeof namebuf));
3836 present_attrs |= UINT64_C(1) << type;
3841 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3845 *present_attrsp = present_attrs;
3849 static enum odp_key_fitness
3850 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3851 uint64_t expected_attrs,
3852 const struct nlattr *key, size_t key_len)
3854 uint64_t missing_attrs;
3855 uint64_t extra_attrs;
3857 missing_attrs = expected_attrs & ~present_attrs;
3858 if (missing_attrs) {
3859 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3860 log_odp_key_attributes(&rl, "expected but not present",
3861 missing_attrs, 0, key, key_len);
3862 return ODP_FIT_TOO_LITTLE;
3865 extra_attrs = present_attrs & ~expected_attrs;
3866 if (extra_attrs || out_of_range_attr) {
3867 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3868 log_odp_key_attributes(&rl, "present but not expected",
3869 extra_attrs, out_of_range_attr, key, key_len);
3870 return ODP_FIT_TOO_MUCH;
3873 return ODP_FIT_PERFECT;
3877 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3878 uint64_t present_attrs, uint64_t *expected_attrs,
3879 struct flow *flow, const struct flow *src_flow)
3881 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3882 bool is_mask = flow != src_flow;
3884 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3885 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3886 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3887 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3888 ntohs(flow->dl_type));
3891 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3892 flow->dl_type != htons(0xffff)) {
3895 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3898 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3899 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3900 /* See comments in odp_flow_key_from_flow__(). */
3901 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3908 static enum odp_key_fitness
3909 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3910 uint64_t present_attrs, int out_of_range_attr,
3911 uint64_t expected_attrs, struct flow *flow,
3912 const struct nlattr *key, size_t key_len,
3913 const struct flow *src_flow)
3915 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3916 bool is_mask = src_flow != flow;
3917 const void *check_start = NULL;
3918 size_t check_len = 0;
3919 enum ovs_key_attr expected_bit = 0xff;
3921 if (eth_type_mpls(src_flow->dl_type)) {
3922 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3923 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3925 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3926 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3927 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3928 int n = size / sizeof(ovs_be32);
3931 if (!size || size % sizeof(ovs_be32)) {
3932 return ODP_FIT_ERROR;
3934 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3935 return ODP_FIT_ERROR;
3938 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3939 flow->mpls_lse[i] = mpls_lse[i];
3941 if (n > FLOW_MAX_MPLS_LABELS) {
3942 return ODP_FIT_TOO_MUCH;
3946 /* BOS may be set only in the innermost label. */
3947 for (i = 0; i < n - 1; i++) {
3948 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3949 return ODP_FIT_ERROR;
3953 /* BOS must be set in the innermost label. */
3954 if (n < FLOW_MAX_MPLS_LABELS
3955 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3956 return ODP_FIT_TOO_LITTLE;
3962 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3964 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3966 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3967 const struct ovs_key_ipv4 *ipv4_key;
3969 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3970 put_ipv4_key(ipv4_key, flow, is_mask);
3971 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3972 return ODP_FIT_ERROR;
3975 check_start = ipv4_key;
3976 check_len = sizeof *ipv4_key;
3977 expected_bit = OVS_KEY_ATTR_IPV4;
3980 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3982 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3984 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3985 const struct ovs_key_ipv6 *ipv6_key;
3987 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3988 put_ipv6_key(ipv6_key, flow, is_mask);
3989 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3990 return ODP_FIT_ERROR;
3993 check_start = ipv6_key;
3994 check_len = sizeof *ipv6_key;
3995 expected_bit = OVS_KEY_ATTR_IPV6;
3998 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3999 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
4001 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
4003 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
4004 const struct ovs_key_arp *arp_key;
4006 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
4007 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
4008 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
4009 "key", ntohs(arp_key->arp_op));
4010 return ODP_FIT_ERROR;
4012 put_arp_key(arp_key, flow);
4014 check_start = arp_key;
4015 check_len = sizeof *arp_key;
4016 expected_bit = OVS_KEY_ATTR_ARP;
4022 if (check_len > 0) { /* Happens only when 'is_mask'. */
4023 if (!is_all_zeros(check_start, check_len) &&
4024 flow->dl_type != htons(0xffff)) {
4025 return ODP_FIT_ERROR;
4027 expected_attrs |= UINT64_C(1) << expected_bit;
4031 expected_bit = OVS_KEY_ATTR_UNSPEC;
4032 if (src_flow->nw_proto == IPPROTO_TCP
4033 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4034 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4035 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4037 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4039 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4040 const union ovs_key_tp *tcp_key;
4042 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4043 put_tp_key(tcp_key, flow);
4044 expected_bit = OVS_KEY_ATTR_TCP;
4046 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4047 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4048 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4050 } else if (src_flow->nw_proto == IPPROTO_UDP
4051 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4052 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4053 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4055 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4057 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4058 const union ovs_key_tp *udp_key;
4060 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4061 put_tp_key(udp_key, flow);
4062 expected_bit = OVS_KEY_ATTR_UDP;
4064 } else if (src_flow->nw_proto == IPPROTO_SCTP
4065 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4066 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4067 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4069 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4071 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4072 const union ovs_key_tp *sctp_key;
4074 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4075 put_tp_key(sctp_key, flow);
4076 expected_bit = OVS_KEY_ATTR_SCTP;
4078 } else if (src_flow->nw_proto == IPPROTO_ICMP
4079 && src_flow->dl_type == htons(ETH_TYPE_IP)
4080 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4082 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4084 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4085 const struct ovs_key_icmp *icmp_key;
4087 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4088 flow->tp_src = htons(icmp_key->icmp_type);
4089 flow->tp_dst = htons(icmp_key->icmp_code);
4090 expected_bit = OVS_KEY_ATTR_ICMP;
4092 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4093 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4094 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4096 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4098 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4099 const struct ovs_key_icmpv6 *icmpv6_key;
4101 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4102 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4103 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4104 expected_bit = OVS_KEY_ATTR_ICMPV6;
4105 if (src_flow->tp_dst == htons(0) &&
4106 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4107 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4109 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4111 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4112 const struct ovs_key_nd *nd_key;
4114 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4115 memcpy(&flow->nd_target, nd_key->nd_target,
4116 sizeof flow->nd_target);
4117 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4118 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4120 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4121 (flow->tp_src != htons(0xffff) ||
4122 flow->tp_dst != htons(0xffff))) {
4123 return ODP_FIT_ERROR;
4125 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4132 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4133 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4134 return ODP_FIT_ERROR;
4136 expected_attrs |= UINT64_C(1) << expected_bit;
4141 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4145 /* Parse 802.1Q header then encapsulated L3 attributes. */
4146 static enum odp_key_fitness
4147 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4148 uint64_t present_attrs, int out_of_range_attr,
4149 uint64_t expected_attrs, struct flow *flow,
4150 const struct nlattr *key, size_t key_len,
4151 const struct flow *src_flow)
4153 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4154 bool is_mask = src_flow != flow;
4156 const struct nlattr *encap
4157 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4158 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4159 enum odp_key_fitness encap_fitness;
4160 enum odp_key_fitness fitness;
4162 /* Calculate fitness of outer attributes. */
4164 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4165 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4167 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4168 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4170 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4171 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4174 fitness = check_expectations(present_attrs, out_of_range_attr,
4175 expected_attrs, key, key_len);
4178 * Remove the TPID from dl_type since it's not the real Ethertype. */
4179 flow->dl_type = htons(0);
4180 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4181 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4184 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4185 return ODP_FIT_TOO_LITTLE;
4186 } else if (flow->vlan_tci == htons(0)) {
4187 /* Corner case for a truncated 802.1Q header. */
4188 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4189 return ODP_FIT_TOO_MUCH;
4192 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4193 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4194 "but CFI bit is not set", ntohs(flow->vlan_tci));
4195 return ODP_FIT_ERROR;
4198 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4203 /* Now parse the encapsulated attributes. */
4204 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4205 attrs, &present_attrs, &out_of_range_attr)) {
4206 return ODP_FIT_ERROR;
4210 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4211 return ODP_FIT_ERROR;
4213 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4214 expected_attrs, flow, key, key_len,
4217 /* The overall fitness is the worse of the outer and inner attributes. */
4218 return MAX(fitness, encap_fitness);
4221 static enum odp_key_fitness
4222 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4223 struct flow *flow, const struct flow *src_flow)
4225 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4226 uint64_t expected_attrs;
4227 uint64_t present_attrs;
4228 int out_of_range_attr;
4229 bool is_mask = src_flow != flow;
4231 memset(flow, 0, sizeof *flow);
4233 /* Parse attributes. */
4234 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4235 &out_of_range_attr)) {
4236 return ODP_FIT_ERROR;
4241 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4242 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4243 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4244 } else if (is_mask) {
4245 /* Always exact match recirc_id if it is not specified. */
4246 flow->recirc_id = UINT32_MAX;
4249 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4250 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4251 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4253 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4254 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4255 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4258 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4259 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4260 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4263 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4264 enum odp_key_fitness res;
4266 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
4267 if (res == ODP_FIT_ERROR) {
4268 return ODP_FIT_ERROR;
4269 } else if (res == ODP_FIT_PERFECT) {
4270 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4274 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4275 flow->in_port.odp_port
4276 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4277 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4278 } else if (!is_mask) {
4279 flow->in_port.odp_port = ODPP_NONE;
4282 /* Ethernet header. */
4283 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4284 const struct ovs_key_ethernet *eth_key;
4286 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4287 put_ethernet_key(eth_key, flow);
4289 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4293 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4296 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4297 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4299 return ODP_FIT_ERROR;
4303 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4304 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4305 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4306 expected_attrs, flow, key, key_len, src_flow);
4309 flow->vlan_tci = htons(0xffff);
4310 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4311 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4312 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4315 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4316 expected_attrs, flow, key, key_len, src_flow);
4319 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4320 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4321 * 'key' fits our expectations for what a flow key should contain.
4323 * The 'in_port' will be the datapath's understanding of the port. The
4324 * caller will need to translate with odp_port_to_ofp_port() if the
4325 * OpenFlow port is needed.
4327 * This function doesn't take the packet itself as an argument because none of
4328 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4329 * it is always possible to infer which additional attribute(s) should appear
4330 * by looking at the attributes for lower-level protocols, e.g. if the network
4331 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4332 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4333 * must be absent. */
4334 enum odp_key_fitness
4335 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4338 return odp_flow_key_to_flow__(key, key_len, flow, flow);
4341 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
4342 * structure in 'mask'. 'flow' must be a previously translated flow
4343 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
4344 * 'key' fits our expectations for what a flow key should contain. */
4345 enum odp_key_fitness
4346 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
4347 struct flow *mask, const struct flow *flow)
4349 return odp_flow_key_to_flow__(key, key_len, mask, flow);
4352 /* Returns 'fitness' as a string, for use in debug messages. */
4354 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4357 case ODP_FIT_PERFECT:
4359 case ODP_FIT_TOO_MUCH:
4361 case ODP_FIT_TOO_LITTLE:
4362 return "too_little";
4370 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4371 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4372 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4373 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4374 * null, then the return value is not meaningful.) */
4376 odp_put_userspace_action(uint32_t pid,
4377 const void *userdata, size_t userdata_size,
4378 odp_port_t tunnel_out_port,
4379 struct ofpbuf *odp_actions)
4381 size_t userdata_ofs;
4384 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4385 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4387 userdata_ofs = odp_actions->size + NLA_HDRLEN;
4389 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4390 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4393 * - The kernel rejected shorter userdata with -ERANGE.
4395 * - The kernel silently dropped userdata beyond the first 8 bytes.
4397 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4398 * separately disable features that required more than 8 bytes.) */
4399 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4400 MAX(8, userdata_size)),
4401 userdata, userdata_size);
4405 if (tunnel_out_port != ODPP_NONE) {
4406 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4409 nl_msg_end_nested(odp_actions, offset);
4411 return userdata_ofs;
4415 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4416 struct ofpbuf *odp_actions)
4418 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4419 tun_key_to_attr(odp_actions, tunnel);
4420 nl_msg_end_nested(odp_actions, offset);
4424 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4425 struct ovs_action_push_tnl *data)
4427 int size = offsetof(struct ovs_action_push_tnl, header);
4429 size += data->header_len;
4430 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4434 /* The commit_odp_actions() function and its helpers. */
4437 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4438 const void *key, size_t key_size)
4440 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4441 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4442 nl_msg_end_nested(odp_actions, offset);
4445 /* Masked set actions have a mask following the data within the netlink
4446 * attribute. The unmasked bits in the data will be cleared as the data
4447 * is copied to the action. */
4449 commit_masked_set_action(struct ofpbuf *odp_actions,
4450 enum ovs_key_attr key_type,
4451 const void *key_, const void *mask_, size_t key_size)
4453 size_t offset = nl_msg_start_nested(odp_actions,
4454 OVS_ACTION_ATTR_SET_MASKED);
4455 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4456 const char *key = key_, *mask = mask_;
4458 memcpy(data + key_size, mask, key_size);
4459 /* Clear unmasked bits while copying. */
4460 while (key_size--) {
4461 *data++ = *key++ & *mask++;
4463 nl_msg_end_nested(odp_actions, offset);
4466 /* If any of the flow key data that ODP actions can modify are different in
4467 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4468 * 'odp_actions' that change the flow tunneling information in key from
4469 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4470 * same way. In other words, operates the same as commit_odp_actions(), but
4471 * only on tunneling information. */
4473 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4474 struct ofpbuf *odp_actions)
4476 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4477 if (flow->tunnel.ip_dst) {
4478 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4481 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4482 odp_put_tunnel_action(&base->tunnel, odp_actions);
4487 commit(enum ovs_key_attr attr, bool use_masked_set,
4488 const void *key, void *base, void *mask, size_t size,
4489 struct ofpbuf *odp_actions)
4491 if (memcmp(key, base, size)) {
4492 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4494 if (use_masked_set && !fully_masked) {
4495 commit_masked_set_action(odp_actions, attr, key, mask, size);
4497 if (!fully_masked) {
4498 memset(mask, 0xff, size);
4500 commit_set_action(odp_actions, attr, key, size);
4502 memcpy(base, key, size);
4505 /* Mask bits are set when we have either read or set the corresponding
4506 * values. Masked bits will be exact-matched, no need to set them
4507 * if the value did not actually change. */
4513 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4515 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4516 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4520 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4522 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4523 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4527 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4528 struct ofpbuf *odp_actions,
4529 struct flow_wildcards *wc,
4532 struct ovs_key_ethernet key, base, mask;
4534 get_ethernet_key(flow, &key);
4535 get_ethernet_key(base_flow, &base);
4536 get_ethernet_key(&wc->masks, &mask);
4538 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4539 &key, &base, &mask, sizeof key, odp_actions)) {
4540 put_ethernet_key(&base, base_flow);
4541 put_ethernet_key(&mask, &wc->masks);
4546 pop_vlan(struct flow *base,
4547 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4549 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4551 if (base->vlan_tci & htons(VLAN_CFI)) {
4552 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4558 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4559 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4561 if (base->vlan_tci == vlan_tci) {
4565 pop_vlan(base, odp_actions, wc);
4566 if (vlan_tci & htons(VLAN_CFI)) {
4567 struct ovs_action_push_vlan vlan;
4569 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4570 vlan.vlan_tci = vlan_tci;
4571 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4572 &vlan, sizeof vlan);
4574 base->vlan_tci = vlan_tci;
4577 /* Wildcarding already done at action translation time. */
4579 commit_mpls_action(const struct flow *flow, struct flow *base,
4580 struct ofpbuf *odp_actions)
4582 int base_n = flow_count_mpls_labels(base, NULL);
4583 int flow_n = flow_count_mpls_labels(flow, NULL);
4584 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4587 while (base_n > common_n) {
4588 if (base_n - 1 == common_n && flow_n > common_n) {
4589 /* If there is only one more LSE in base than there are common
4590 * between base and flow; and flow has at least one more LSE than
4591 * is common then the topmost LSE of base may be updated using
4593 struct ovs_key_mpls mpls_key;
4595 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4596 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4597 &mpls_key, sizeof mpls_key);
4598 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4601 /* Otherwise, if there more LSEs in base than are common between
4602 * base and flow then pop the topmost one. */
4606 /* If all the LSEs are to be popped and this is not the outermost
4607 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4608 * POP_MPLS action instead of flow->dl_type.
4610 * This is because the POP_MPLS action requires its ethertype
4611 * argument to be an MPLS ethernet type but in this case
4612 * flow->dl_type will be a non-MPLS ethernet type.
4614 * When the final POP_MPLS action occurs it use flow->dl_type and
4615 * the and the resulting packet will have the desired dl_type. */
4616 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4617 dl_type = htons(ETH_TYPE_MPLS);
4619 dl_type = flow->dl_type;
4621 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4622 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4628 /* If, after the above popping and setting, there are more LSEs in flow
4629 * than base then some LSEs need to be pushed. */
4630 while (base_n < flow_n) {
4631 struct ovs_action_push_mpls *mpls;
4633 mpls = nl_msg_put_unspec_zero(odp_actions,
4634 OVS_ACTION_ATTR_PUSH_MPLS,
4636 mpls->mpls_ethertype = flow->dl_type;
4637 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4638 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4639 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4645 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4647 ipv4->ipv4_src = flow->nw_src;
4648 ipv4->ipv4_dst = flow->nw_dst;
4649 ipv4->ipv4_proto = flow->nw_proto;
4650 ipv4->ipv4_tos = flow->nw_tos;
4651 ipv4->ipv4_ttl = flow->nw_ttl;
4652 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4656 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4658 flow->nw_src = ipv4->ipv4_src;
4659 flow->nw_dst = ipv4->ipv4_dst;
4660 flow->nw_proto = ipv4->ipv4_proto;
4661 flow->nw_tos = ipv4->ipv4_tos;
4662 flow->nw_ttl = ipv4->ipv4_ttl;
4663 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4667 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4668 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4671 struct ovs_key_ipv4 key, mask, base;
4673 /* Check that nw_proto and nw_frag remain unchanged. */
4674 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4675 flow->nw_frag == base_flow->nw_frag);
4677 get_ipv4_key(flow, &key, false);
4678 get_ipv4_key(base_flow, &base, false);
4679 get_ipv4_key(&wc->masks, &mask, true);
4680 mask.ipv4_proto = 0; /* Not writeable. */
4681 mask.ipv4_frag = 0; /* Not writable. */
4683 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4685 put_ipv4_key(&base, base_flow, false);
4686 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4687 put_ipv4_key(&mask, &wc->masks, true);
4693 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4695 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4696 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4697 ipv6->ipv6_label = flow->ipv6_label;
4698 ipv6->ipv6_proto = flow->nw_proto;
4699 ipv6->ipv6_tclass = flow->nw_tos;
4700 ipv6->ipv6_hlimit = flow->nw_ttl;
4701 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4705 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4707 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4708 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4709 flow->ipv6_label = ipv6->ipv6_label;
4710 flow->nw_proto = ipv6->ipv6_proto;
4711 flow->nw_tos = ipv6->ipv6_tclass;
4712 flow->nw_ttl = ipv6->ipv6_hlimit;
4713 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4717 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4718 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4721 struct ovs_key_ipv6 key, mask, base;
4723 /* Check that nw_proto and nw_frag remain unchanged. */
4724 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4725 flow->nw_frag == base_flow->nw_frag);
4727 get_ipv6_key(flow, &key, false);
4728 get_ipv6_key(base_flow, &base, false);
4729 get_ipv6_key(&wc->masks, &mask, true);
4730 mask.ipv6_proto = 0; /* Not writeable. */
4731 mask.ipv6_frag = 0; /* Not writable. */
4733 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4735 put_ipv6_key(&base, base_flow, false);
4736 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4737 put_ipv6_key(&mask, &wc->masks, true);
4743 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4745 /* ARP key has padding, clear it. */
4746 memset(arp, 0, sizeof *arp);
4748 arp->arp_sip = flow->nw_src;
4749 arp->arp_tip = flow->nw_dst;
4750 arp->arp_op = htons(flow->nw_proto);
4751 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4752 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4756 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4758 flow->nw_src = arp->arp_sip;
4759 flow->nw_dst = arp->arp_tip;
4760 flow->nw_proto = ntohs(arp->arp_op);
4761 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4762 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4765 static enum slow_path_reason
4766 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4767 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4769 struct ovs_key_arp key, mask, base;
4771 get_arp_key(flow, &key);
4772 get_arp_key(base_flow, &base);
4773 get_arp_key(&wc->masks, &mask);
4775 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4777 put_arp_key(&base, base_flow);
4778 put_arp_key(&mask, &wc->masks);
4785 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4787 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4788 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4789 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4790 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4794 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4796 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4797 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4798 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4799 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4802 static enum slow_path_reason
4803 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4804 struct ofpbuf *odp_actions,
4805 struct flow_wildcards *wc, bool use_masked)
4807 struct ovs_key_nd key, mask, base;
4809 get_nd_key(flow, &key);
4810 get_nd_key(base_flow, &base);
4811 get_nd_key(&wc->masks, &mask);
4813 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4815 put_nd_key(&base, base_flow);
4816 put_nd_key(&mask, &wc->masks);
4823 static enum slow_path_reason
4824 commit_set_nw_action(const struct flow *flow, struct flow *base,
4825 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4828 /* Check if 'flow' really has an L3 header. */
4829 if (!flow->nw_proto) {
4833 switch (ntohs(base->dl_type)) {
4835 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4839 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4840 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4843 return commit_set_arp_action(flow, base, odp_actions, wc);
4849 /* TCP, UDP, and SCTP keys have the same layout. */
4850 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4851 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4854 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4856 tp->tcp.tcp_src = flow->tp_src;
4857 tp->tcp.tcp_dst = flow->tp_dst;
4861 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4863 flow->tp_src = tp->tcp.tcp_src;
4864 flow->tp_dst = tp->tcp.tcp_dst;
4868 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4869 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4872 enum ovs_key_attr key_type;
4873 union ovs_key_tp key, mask, base;
4875 /* Check if 'flow' really has an L3 header. */
4876 if (!flow->nw_proto) {
4880 if (!is_ip_any(base_flow)) {
4884 if (flow->nw_proto == IPPROTO_TCP) {
4885 key_type = OVS_KEY_ATTR_TCP;
4886 } else if (flow->nw_proto == IPPROTO_UDP) {
4887 key_type = OVS_KEY_ATTR_UDP;
4888 } else if (flow->nw_proto == IPPROTO_SCTP) {
4889 key_type = OVS_KEY_ATTR_SCTP;
4894 get_tp_key(flow, &key);
4895 get_tp_key(base_flow, &base);
4896 get_tp_key(&wc->masks, &mask);
4898 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4900 put_tp_key(&base, base_flow);
4901 put_tp_key(&mask, &wc->masks);
4906 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4907 struct ofpbuf *odp_actions,
4908 struct flow_wildcards *wc,
4911 uint32_t key, mask, base;
4913 key = flow->skb_priority;
4914 base = base_flow->skb_priority;
4915 mask = wc->masks.skb_priority;
4917 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4918 sizeof key, odp_actions)) {
4919 base_flow->skb_priority = base;
4920 wc->masks.skb_priority = mask;
4925 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4926 struct ofpbuf *odp_actions,
4927 struct flow_wildcards *wc,
4930 uint32_t key, mask, base;
4932 key = flow->pkt_mark;
4933 base = base_flow->pkt_mark;
4934 mask = wc->masks.pkt_mark;
4936 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4937 sizeof key, odp_actions)) {
4938 base_flow->pkt_mark = base;
4939 wc->masks.pkt_mark = mask;
4943 /* If any of the flow key data that ODP actions can modify are different in
4944 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4945 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4946 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4947 * in addition to this function if needed. Sets fields in 'wc' that are
4948 * used as part of the action.
4950 * Returns a reason to force processing the flow's packets into the userspace
4951 * slow path, if there is one, otherwise 0. */
4952 enum slow_path_reason
4953 commit_odp_actions(const struct flow *flow, struct flow *base,
4954 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4957 enum slow_path_reason slow;
4959 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4960 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4961 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4962 commit_mpls_action(flow, base, odp_actions);
4963 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4964 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4965 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);