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 int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
55 struct ofpbuf *, struct ofpbuf *);
56 static void format_odp_key_attr(const struct nlattr *a,
57 const struct nlattr *ma,
58 const struct hmap *portno_names, struct ds *ds,
61 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
64 * - For an action whose argument has a fixed length, returned that
65 * nonnegative length in bytes.
67 * - For an action with a variable-length argument, returns -2.
69 * - For an invalid 'type', returns -1. */
71 odp_action_len(uint16_t type)
73 if (type > OVS_ACTION_ATTR_MAX) {
77 switch ((enum ovs_action_attr) type) {
78 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
79 case OVS_ACTION_ATTR_TUNNEL_PUSH: return -2;
80 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
81 case OVS_ACTION_ATTR_USERSPACE: return -2;
82 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
83 case OVS_ACTION_ATTR_POP_VLAN: return 0;
84 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
85 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
86 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
87 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
88 case OVS_ACTION_ATTR_SET: return -2;
89 case OVS_ACTION_ATTR_SET_MASKED: return -2;
90 case OVS_ACTION_ATTR_SAMPLE: return -2;
92 case OVS_ACTION_ATTR_UNSPEC:
93 case __OVS_ACTION_ATTR_MAX:
100 /* Returns a string form of 'attr'. The return value is either a statically
101 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
102 * should be at least OVS_KEY_ATTR_BUFSIZE. */
103 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
105 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
108 case OVS_KEY_ATTR_UNSPEC: return "unspec";
109 case OVS_KEY_ATTR_ENCAP: return "encap";
110 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
111 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
112 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
113 case OVS_KEY_ATTR_IN_PORT: return "in_port";
114 case OVS_KEY_ATTR_ETHERNET: return "eth";
115 case OVS_KEY_ATTR_VLAN: return "vlan";
116 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
117 case OVS_KEY_ATTR_IPV4: return "ipv4";
118 case OVS_KEY_ATTR_IPV6: return "ipv6";
119 case OVS_KEY_ATTR_TCP: return "tcp";
120 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
121 case OVS_KEY_ATTR_UDP: return "udp";
122 case OVS_KEY_ATTR_SCTP: return "sctp";
123 case OVS_KEY_ATTR_ICMP: return "icmp";
124 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
125 case OVS_KEY_ATTR_ARP: return "arp";
126 case OVS_KEY_ATTR_ND: return "nd";
127 case OVS_KEY_ATTR_MPLS: return "mpls";
128 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
129 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
131 case __OVS_KEY_ATTR_MAX:
133 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
139 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
141 size_t len = nl_attr_get_size(a);
143 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
145 const uint8_t *unspec;
148 unspec = nl_attr_get(a);
149 for (i = 0; i < len; i++) {
150 ds_put_char(ds, i ? ' ': '(');
151 ds_put_format(ds, "%02x", unspec[i]);
153 ds_put_char(ds, ')');
158 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
160 static const struct nl_policy ovs_sample_policy[] = {
161 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
162 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
164 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
166 const struct nlattr *nla_acts;
169 ds_put_cstr(ds, "sample");
171 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
172 ds_put_cstr(ds, "(error)");
176 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
179 ds_put_format(ds, "(sample=%.1f%%,", percentage);
181 ds_put_cstr(ds, "actions(");
182 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
183 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
184 format_odp_actions(ds, nla_acts, len);
185 ds_put_format(ds, "))");
189 slow_path_reason_to_string(uint32_t reason)
191 switch ((enum slow_path_reason) reason) {
192 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
201 slow_path_reason_to_explanation(enum slow_path_reason reason)
204 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
213 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
214 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
219 /* Parse masked flags in numeric format? */
220 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
221 res_flags, res_mask, &n) && n > 0) {
222 if (*res_flags & ~allowed || *res_mask & ~allowed) {
230 if (res_mask && (*s == '+' || *s == '-')) {
231 uint32_t flags = 0, mask = 0;
233 /* Parse masked flags. */
234 while (s[0] != ')') {
241 } else if (s[0] == '-') {
249 name_len = strcspn(s, "+-)");
251 for (bit = 1; bit; bit <<= 1) {
252 const char *fname = bit_to_string(bit);
260 if (len != name_len) {
263 if (!strncmp(s, fname, len)) {
265 /* bit already set. */
268 if (!(bit & allowed)) {
280 return -EINVAL; /* Unknown flag name */
291 /* Parse unmasked flags. If a flag is present, it is set, otherwise
293 while (s[n] != ')') {
294 unsigned long long int flags;
298 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
299 if (flags & ~allowed) {
302 n += n0 + (s[n + n0] == ',');
307 for (bit = 1; bit; bit <<= 1) {
308 const char *name = bit_to_string(bit);
316 if (!strncmp(s + n, name, len) &&
317 (s[n + len] == ',' || s[n + len] == ')')) {
318 if (!(bit & allowed)) {
322 n += len + (s[n + len] == ',');
334 *res_mask = UINT32_MAX;
340 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
342 static const struct nl_policy ovs_userspace_policy[] = {
343 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
344 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
346 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
349 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
350 const struct nlattr *userdata_attr;
351 const struct nlattr *tunnel_out_port_attr;
353 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
354 ds_put_cstr(ds, "userspace(error)");
358 ds_put_format(ds, "userspace(pid=%"PRIu32,
359 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
361 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
364 const uint8_t *userdata = nl_attr_get(userdata_attr);
365 size_t userdata_len = nl_attr_get_size(userdata_attr);
366 bool userdata_unspec = true;
367 union user_action_cookie cookie;
369 if (userdata_len >= sizeof cookie.type
370 && userdata_len <= sizeof cookie) {
372 memset(&cookie, 0, sizeof cookie);
373 memcpy(&cookie, userdata, userdata_len);
375 userdata_unspec = false;
377 if (userdata_len == sizeof cookie.sflow
378 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
379 ds_put_format(ds, ",sFlow("
380 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
381 vlan_tci_to_vid(cookie.sflow.vlan_tci),
382 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
383 cookie.sflow.output);
384 } else if (userdata_len == sizeof cookie.slow_path
385 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
386 ds_put_cstr(ds, ",slow_path(");
387 format_flags(ds, slow_path_reason_to_string,
388 cookie.slow_path.reason, ',');
389 ds_put_format(ds, ")");
390 } else if (userdata_len == sizeof cookie.flow_sample
391 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
392 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
393 ",collector_set_id=%"PRIu32
394 ",obs_domain_id=%"PRIu32
395 ",obs_point_id=%"PRIu32")",
396 cookie.flow_sample.probability,
397 cookie.flow_sample.collector_set_id,
398 cookie.flow_sample.obs_domain_id,
399 cookie.flow_sample.obs_point_id);
400 } else if (userdata_len >= sizeof cookie.ipfix
401 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
402 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
403 cookie.ipfix.output_odp_port);
405 userdata_unspec = true;
409 if (userdata_unspec) {
411 ds_put_format(ds, ",userdata(");
412 for (i = 0; i < userdata_len; i++) {
413 ds_put_format(ds, "%02x", userdata[i]);
415 ds_put_char(ds, ')');
419 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
420 if (tunnel_out_port_attr) {
421 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
422 nl_attr_get_u32(tunnel_out_port_attr));
425 ds_put_char(ds, ')');
429 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
431 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
432 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
433 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
434 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
436 ds_put_char(ds, ',');
438 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
439 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
440 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
441 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
443 ds_put_char(ds, ',');
445 if (!(tci & htons(VLAN_CFI))) {
446 ds_put_cstr(ds, "cfi=0");
447 ds_put_char(ds, ',');
453 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
455 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
456 mpls_lse_to_label(mpls_lse),
457 mpls_lse_to_tc(mpls_lse),
458 mpls_lse_to_ttl(mpls_lse),
459 mpls_lse_to_bos(mpls_lse));
463 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
464 const struct ovs_key_mpls *mpls_mask, int n)
467 ovs_be32 key = mpls_key->mpls_lse;
469 if (mpls_mask == NULL) {
470 format_mpls_lse(ds, key);
472 ovs_be32 mask = mpls_mask->mpls_lse;
474 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
475 mpls_lse_to_label(key), mpls_lse_to_label(mask),
476 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
477 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
478 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
483 for (i = 0; i < n; i++) {
484 ds_put_format(ds, "lse%d=%#"PRIx32,
485 i, ntohl(mpls_key[i].mpls_lse));
487 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
489 ds_put_char(ds, ',');
496 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
498 ds_put_format(ds, "recirc(%"PRIu32")", recirc_id);
502 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
504 ds_put_format(ds, "hash(");
506 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
507 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
509 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
512 ds_put_format(ds, ")");
516 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
518 const struct udp_header *udp;
520 udp = (const struct udp_header *) (ip + 1);
521 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
522 ntohs(udp->udp_src), ntohs(udp->udp_dst),
523 ntohs(udp->udp_csum));
529 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
531 const struct eth_header *eth;
532 const struct ip_header *ip;
535 eth = (const struct eth_header *)data->header;
538 ip = (const struct ip_header *)l3;
541 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
542 data->header_len, data->tnl_type);
543 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
544 ds_put_format(ds, ",src=");
545 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
546 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
549 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
550 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
551 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
552 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
553 ip->ip_proto, ip->ip_tos,
557 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
558 const struct vxlanhdr *vxh;
560 vxh = format_udp_tnl_push_header(ds, ip);
562 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
563 ntohl(get_16aligned_be32(&vxh->vx_flags)),
564 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
565 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
566 const struct genevehdr *gnh;
568 gnh = format_udp_tnl_push_header(ds, ip);
570 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
571 gnh->oam ? "oam," : "",
572 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
573 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
574 const struct gre_base_hdr *greh;
575 ovs_16aligned_be32 *options;
578 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
579 greh = (const struct gre_base_hdr *) l4;
581 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
582 ntohs(greh->flags), ntohs(greh->protocol));
583 options = (ovs_16aligned_be32 *)(greh + 1);
584 if (greh->flags & htons(GRE_CSUM)) {
585 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
588 if (greh->flags & htons(GRE_KEY)) {
589 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
592 if (greh->flags & htons(GRE_SEQ)) {
593 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
596 ds_put_format(ds, ")");
598 ds_put_format(ds, ")");
602 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
604 struct ovs_action_push_tnl *data;
606 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
608 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
609 format_odp_tnl_push_header(ds, data);
610 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
614 format_odp_action(struct ds *ds, const struct nlattr *a)
617 enum ovs_action_attr type = nl_attr_type(a);
618 const struct ovs_action_push_vlan *vlan;
621 expected_len = odp_action_len(nl_attr_type(a));
622 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
623 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
624 nl_attr_get_size(a), expected_len);
625 format_generic_odp_action(ds, a);
630 case OVS_ACTION_ATTR_OUTPUT:
631 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
633 case OVS_ACTION_ATTR_TUNNEL_POP:
634 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
636 case OVS_ACTION_ATTR_TUNNEL_PUSH:
637 format_odp_tnl_push_action(ds, a);
639 case OVS_ACTION_ATTR_USERSPACE:
640 format_odp_userspace_action(ds, a);
642 case OVS_ACTION_ATTR_RECIRC:
643 format_odp_recirc_action(ds, nl_attr_get_u32(a));
645 case OVS_ACTION_ATTR_HASH:
646 format_odp_hash_action(ds, nl_attr_get(a));
648 case OVS_ACTION_ATTR_SET_MASKED:
650 size = nl_attr_get_size(a) / 2;
651 ds_put_cstr(ds, "set(");
653 /* Masked set action not supported for tunnel key, which is bigger. */
654 if (size <= sizeof(struct ovs_key_ipv6)) {
655 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
656 sizeof(struct nlattr))];
657 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
658 sizeof(struct nlattr))];
660 mask->nla_type = attr->nla_type = nl_attr_type(a);
661 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
662 memcpy(attr + 1, (char *)(a + 1), size);
663 memcpy(mask + 1, (char *)(a + 1) + size, size);
664 format_odp_key_attr(attr, mask, NULL, ds, false);
666 format_odp_key_attr(a, NULL, NULL, ds, false);
668 ds_put_cstr(ds, ")");
670 case OVS_ACTION_ATTR_SET:
671 ds_put_cstr(ds, "set(");
672 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
673 ds_put_cstr(ds, ")");
675 case OVS_ACTION_ATTR_PUSH_VLAN:
676 vlan = nl_attr_get(a);
677 ds_put_cstr(ds, "push_vlan(");
678 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
679 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
681 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
682 ds_put_char(ds, ')');
684 case OVS_ACTION_ATTR_POP_VLAN:
685 ds_put_cstr(ds, "pop_vlan");
687 case OVS_ACTION_ATTR_PUSH_MPLS: {
688 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
689 ds_put_cstr(ds, "push_mpls(");
690 format_mpls_lse(ds, mpls->mpls_lse);
691 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
694 case OVS_ACTION_ATTR_POP_MPLS: {
695 ovs_be16 ethertype = nl_attr_get_be16(a);
696 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
699 case OVS_ACTION_ATTR_SAMPLE:
700 format_odp_sample_action(ds, a);
702 case OVS_ACTION_ATTR_UNSPEC:
703 case __OVS_ACTION_ATTR_MAX:
705 format_generic_odp_action(ds, a);
711 format_odp_actions(struct ds *ds, const struct nlattr *actions,
715 const struct nlattr *a;
718 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
720 ds_put_char(ds, ',');
722 format_odp_action(ds, a);
727 if (left == actions_len) {
728 ds_put_cstr(ds, "<empty>");
730 ds_put_format(ds, ",***%u leftover bytes*** (", left);
731 for (i = 0; i < left; i++) {
732 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
734 ds_put_char(ds, ')');
737 ds_put_cstr(ds, "drop");
741 /* Separate out parse_odp_userspace_action() function. */
743 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
746 union user_action_cookie cookie;
748 odp_port_t tunnel_out_port;
750 void *user_data = NULL;
751 size_t user_data_size = 0;
753 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
759 uint32_t probability;
760 uint32_t collector_set_id;
761 uint32_t obs_domain_id;
762 uint32_t obs_point_id;
765 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
766 "pcp=%i,output=%"SCNi32")%n",
767 &vid, &pcp, &output, &n1)) {
771 tci = vid | (pcp << VLAN_PCP_SHIFT);
776 cookie.type = USER_ACTION_COOKIE_SFLOW;
777 cookie.sflow.vlan_tci = htons(tci);
778 cookie.sflow.output = output;
780 user_data_size = sizeof cookie.sflow;
781 } else if (ovs_scan(&s[n], ",slow_path(%n",
786 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
787 cookie.slow_path.unused = 0;
788 cookie.slow_path.reason = 0;
790 res = parse_flags(&s[n], slow_path_reason_to_string,
791 &cookie.slow_path.reason,
792 SLOW_PATH_REASON_MASK, NULL);
793 if (res < 0 || s[n + res] != ')') {
799 user_data_size = sizeof cookie.slow_path;
800 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
801 "collector_set_id=%"SCNi32","
802 "obs_domain_id=%"SCNi32","
803 "obs_point_id=%"SCNi32")%n",
804 &probability, &collector_set_id,
805 &obs_domain_id, &obs_point_id, &n1)) {
808 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
809 cookie.flow_sample.probability = probability;
810 cookie.flow_sample.collector_set_id = collector_set_id;
811 cookie.flow_sample.obs_domain_id = obs_domain_id;
812 cookie.flow_sample.obs_point_id = obs_point_id;
814 user_data_size = sizeof cookie.flow_sample;
815 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
818 cookie.type = USER_ACTION_COOKIE_IPFIX;
819 cookie.ipfix.output_odp_port = u32_to_odp(output);
821 user_data_size = sizeof cookie.ipfix;
822 } else if (ovs_scan(&s[n], ",userdata(%n",
827 ofpbuf_init(&buf, 16);
828 end = ofpbuf_put_hex(&buf, &s[n], NULL);
832 user_data = buf.data;
833 user_data_size = buf.size;
840 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
841 &tunnel_out_port, &n1)) {
842 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
844 } else if (s[n] == ')') {
845 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
854 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
856 struct eth_header *eth;
857 struct ip_header *ip;
858 struct udp_header *udp;
859 struct gre_base_hdr *greh;
860 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
862 uint32_t tnl_type = 0, header_len = 0;
866 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
869 eth = (struct eth_header *) data->header;
870 l3 = (data->header + sizeof *eth);
871 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
872 ip = (struct ip_header *) l3;
873 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
874 "eth(dst="ETH_ADDR_SCAN_FMT",",
877 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
881 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
882 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
885 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
888 eth->eth_type = htons(dl_type);
891 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
892 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
895 &ip->ip_proto, &ip->ip_tos,
896 &ip->ip_ttl, &ip->ip_frag_off)) {
899 put_16aligned_be32(&ip->ip_src, sip);
900 put_16aligned_be32(&ip->ip_dst, dip);
903 udp = (struct udp_header *) l4;
904 greh = (struct gre_base_hdr *) l4;
905 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
906 &udp_src, &udp_dst, &csum)) {
907 uint32_t vx_flags, vni;
909 udp->udp_src = htons(udp_src);
910 udp->udp_dst = htons(udp_dst);
912 udp->udp_csum = htons(csum);
914 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
916 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
918 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
919 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
920 tnl_type = OVS_VPORT_TYPE_VXLAN;
921 header_len = sizeof *eth + sizeof *ip +
922 sizeof *udp + sizeof *vxh;
923 } else if (ovs_scan_len(s, &n, "geneve(")) {
924 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
926 memset(gnh, 0, sizeof *gnh);
927 if (ovs_scan_len(s, &n, "oam,")) {
930 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
933 gnh->proto_type = htons(ETH_TYPE_TEB);
934 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
935 tnl_type = OVS_VPORT_TYPE_GENEVE;
936 header_len = sizeof *eth + sizeof *ip +
937 sizeof *udp + sizeof *gnh;
941 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
942 &gre_flags, &gre_proto)){
944 tnl_type = OVS_VPORT_TYPE_GRE;
945 greh->flags = htons(gre_flags);
946 greh->protocol = htons(gre_proto);
947 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
949 if (greh->flags & htons(GRE_CSUM)) {
950 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
954 memset(options, 0, sizeof *options);
955 *((ovs_be16 *)options) = htons(csum);
958 if (greh->flags & htons(GRE_KEY)) {
961 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
965 put_16aligned_be32(options, htonl(key));
968 if (greh->flags & htons(GRE_SEQ)) {
971 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
974 put_16aligned_be32(options, htonl(seq));
978 if (!ovs_scan_len(s, &n, "))")) {
982 header_len = sizeof *eth + sizeof *ip +
983 ((uint8_t *) options - (uint8_t *) greh);
988 /* check tunnel meta data. */
989 if (data->tnl_type != tnl_type) {
992 if (data->header_len != header_len) {
997 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1005 parse_odp_action(const char *s, const struct simap *port_names,
1006 struct ofpbuf *actions)
1012 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1013 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1019 int len = strcspn(s, delimiters);
1020 struct simap_node *node;
1022 node = simap_find_len(port_names, s, len);
1024 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1033 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1034 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1039 if (!strncmp(s, "userspace(", 10)) {
1040 return parse_odp_userspace_action(s, actions);
1043 if (!strncmp(s, "set(", 4)) {
1046 struct nlattr mask[128 / sizeof(struct nlattr)];
1047 struct ofpbuf maskbuf;
1048 struct nlattr *nested, *key;
1051 /* 'mask' is big enough to hold any key. */
1052 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1054 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1055 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1059 if (s[retval + 4] != ')') {
1063 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1066 size = nl_attr_get_size(mask);
1067 if (size == nl_attr_get_size(key)) {
1068 /* Change to masked set action if not fully masked. */
1069 if (!is_all_ones(mask + 1, size)) {
1070 key->nla_len += size;
1071 ofpbuf_put(actions, mask + 1, size);
1072 /* 'actions' may have been reallocated by ofpbuf_put(). */
1073 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1074 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1078 nl_msg_end_nested(actions, start_ofs);
1083 struct ovs_action_push_vlan push;
1084 int tpid = ETH_TYPE_VLAN;
1089 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1090 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1091 &vid, &pcp, &cfi, &n)
1092 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1093 &tpid, &vid, &pcp, &n)
1094 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1095 &tpid, &vid, &pcp, &cfi, &n)) {
1096 push.vlan_tpid = htons(tpid);
1097 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1098 | (pcp << VLAN_PCP_SHIFT)
1099 | (cfi ? VLAN_CFI : 0));
1100 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1101 &push, sizeof push);
1107 if (!strncmp(s, "pop_vlan", 8)) {
1108 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1116 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1117 && percentage >= 0. && percentage <= 100.0) {
1118 size_t sample_ofs, actions_ofs;
1121 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1122 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1123 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1124 (probability <= 0 ? 0
1125 : probability >= UINT32_MAX ? UINT32_MAX
1128 actions_ofs = nl_msg_start_nested(actions,
1129 OVS_SAMPLE_ATTR_ACTIONS);
1133 n += strspn(s + n, delimiters);
1138 retval = parse_odp_action(s + n, port_names, actions);
1144 nl_msg_end_nested(actions, actions_ofs);
1145 nl_msg_end_nested(actions, sample_ofs);
1147 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1155 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1156 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1162 struct ovs_action_push_tnl data;
1165 n = ovs_parse_tnl_push(s, &data);
1167 odp_put_tnl_push_action(actions, &data);
1176 /* Parses the string representation of datapath actions, in the format output
1177 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1178 * value. On success, the ODP actions are appended to 'actions' as a series of
1179 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1180 * way, 'actions''s data might be reallocated. */
1182 odp_actions_from_string(const char *s, const struct simap *port_names,
1183 struct ofpbuf *actions)
1187 if (!strcasecmp(s, "drop")) {
1191 old_size = actions->size;
1195 s += strspn(s, delimiters);
1200 retval = parse_odp_action(s, port_names, actions);
1201 if (retval < 0 || !strchr(delimiters, s[retval])) {
1202 actions->size = old_size;
1211 /* Returns the correct length of the payload for a flow key attribute of the
1212 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
1213 * is variable length. */
1215 odp_flow_key_attr_len(uint16_t type)
1217 if (type > OVS_KEY_ATTR_MAX) {
1221 switch ((enum ovs_key_attr) type) {
1222 case OVS_KEY_ATTR_ENCAP: return -2;
1223 case OVS_KEY_ATTR_PRIORITY: return 4;
1224 case OVS_KEY_ATTR_SKB_MARK: return 4;
1225 case OVS_KEY_ATTR_DP_HASH: return 4;
1226 case OVS_KEY_ATTR_RECIRC_ID: return 4;
1227 case OVS_KEY_ATTR_TUNNEL: return -2;
1228 case OVS_KEY_ATTR_IN_PORT: return 4;
1229 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
1230 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
1231 case OVS_KEY_ATTR_ETHERTYPE: return 2;
1232 case OVS_KEY_ATTR_MPLS: return -2;
1233 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
1234 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
1235 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
1236 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
1237 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
1238 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
1239 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
1240 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
1241 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
1242 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
1244 case OVS_KEY_ATTR_UNSPEC:
1245 case __OVS_KEY_ATTR_MAX:
1253 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1255 size_t len = nl_attr_get_size(a);
1257 const uint8_t *unspec;
1260 unspec = nl_attr_get(a);
1261 for (i = 0; i < len; i++) {
1263 ds_put_char(ds, ' ');
1265 ds_put_format(ds, "%02x", unspec[i]);
1271 ovs_frag_type_to_string(enum ovs_frag_type type)
1274 case OVS_FRAG_TYPE_NONE:
1276 case OVS_FRAG_TYPE_FIRST:
1278 case OVS_FRAG_TYPE_LATER:
1280 case __OVS_FRAG_TYPE_MAX:
1287 tunnel_key_attr_len(int type)
1290 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
1291 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
1292 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1293 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1294 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1295 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1296 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1297 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1298 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1299 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1300 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1301 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: return -2;
1302 case __OVS_TUNNEL_KEY_ATTR_MAX:
1308 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1310 parse_geneve_opts(const struct nlattr *attr)
1312 int opts_len = nl_attr_get_size(attr);
1313 const struct geneve_opt *opt = nl_attr_get(attr);
1315 while (opts_len > 0) {
1318 if (opts_len < sizeof(*opt)) {
1322 len = sizeof(*opt) + opt->length * 4;
1323 if (len > opts_len) {
1327 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1329 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1334 opt = opt + len / sizeof(*opt);
1341 enum odp_key_fitness
1342 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1345 const struct nlattr *a;
1347 bool unknown = false;
1349 NL_NESTED_FOR_EACH(a, left, attr) {
1350 uint16_t type = nl_attr_type(a);
1351 size_t len = nl_attr_get_size(a);
1352 int expected_len = tunnel_key_attr_len(type);
1354 if (len != expected_len && expected_len >= 0) {
1355 return ODP_FIT_ERROR;
1359 case OVS_TUNNEL_KEY_ATTR_ID:
1360 tun->tun_id = nl_attr_get_be64(a);
1361 tun->flags |= FLOW_TNL_F_KEY;
1363 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1364 tun->ip_src = nl_attr_get_be32(a);
1366 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1367 tun->ip_dst = nl_attr_get_be32(a);
1369 case OVS_TUNNEL_KEY_ATTR_TOS:
1370 tun->ip_tos = nl_attr_get_u8(a);
1372 case OVS_TUNNEL_KEY_ATTR_TTL:
1373 tun->ip_ttl = nl_attr_get_u8(a);
1376 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1377 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1379 case OVS_TUNNEL_KEY_ATTR_CSUM:
1380 tun->flags |= FLOW_TNL_F_CSUM;
1382 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1383 tun->tp_src = nl_attr_get_be16(a);
1385 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1386 tun->tp_dst = nl_attr_get_be16(a);
1388 case OVS_TUNNEL_KEY_ATTR_OAM:
1389 tun->flags |= FLOW_TNL_F_OAM;
1391 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1392 static const struct nl_policy vxlan_opts_policy[] = {
1393 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1395 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1397 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1398 return ODP_FIT_ERROR;
1401 if (ext[OVS_VXLAN_EXT_GBP]) {
1402 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1404 tun->gbp_id = htons(gbp & 0xFFFF);
1405 tun->gbp_flags = (gbp >> 16) & 0xFF;
1410 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1411 if (parse_geneve_opts(a)) {
1412 return ODP_FIT_ERROR;
1414 /* It is necessary to reproduce options exactly (including order)
1415 * so it's easiest to just echo them back. */
1420 /* Allow this to show up as unexpected, if there are unknown
1421 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1428 return ODP_FIT_ERROR;
1431 return ODP_FIT_TOO_MUCH;
1433 return ODP_FIT_PERFECT;
1437 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1441 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1443 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1444 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1445 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1447 if (tun_key->ip_src) {
1448 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1450 if (tun_key->ip_dst) {
1451 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1453 if (tun_key->ip_tos) {
1454 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1456 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1457 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1458 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1460 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1461 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1463 if (tun_key->tp_src) {
1464 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1466 if (tun_key->tp_dst) {
1467 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1469 if (tun_key->flags & FLOW_TNL_F_OAM) {
1470 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1472 if (tun_key->gbp_flags || tun_key->gbp_id) {
1473 size_t vxlan_opts_ofs;
1475 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1476 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1477 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1478 nl_msg_end_nested(a, vxlan_opts_ofs);
1481 nl_msg_end_nested(a, tun_key_ofs);
1485 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1487 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1491 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1493 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1494 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1496 if (attr == OVS_KEY_ATTR_IPV6) {
1497 const struct ovs_key_ipv6 *ipv6_mask = mask;
1500 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1501 == htonl(IPV6_LABEL_MASK))
1502 && ipv6_mask->ipv6_proto == UINT8_MAX
1503 && ipv6_mask->ipv6_tclass == UINT8_MAX
1504 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1505 && ipv6_mask->ipv6_frag == UINT8_MAX
1506 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1507 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1509 if (attr == OVS_KEY_ATTR_TUNNEL) {
1510 const struct flow_tnl *tun_mask = mask;
1512 return tun_mask->flags == FLOW_TNL_F_MASK
1513 && tun_mask->tun_id == OVS_BE64_MAX
1514 && tun_mask->ip_src == OVS_BE32_MAX
1515 && tun_mask->ip_dst == OVS_BE32_MAX
1516 && tun_mask->ip_tos == UINT8_MAX
1517 && tun_mask->ip_ttl == UINT8_MAX
1518 && tun_mask->tp_src == OVS_BE16_MAX
1519 && tun_mask->tp_dst == OVS_BE16_MAX
1520 && tun_mask->gbp_id == OVS_BE16_MAX
1521 && tun_mask->gbp_flags == UINT8_MAX;
1524 if (attr == OVS_KEY_ATTR_ARP) {
1525 /* ARP key has padding, ignore it. */
1526 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1527 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1528 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1529 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1532 return is_all_ones(mask, size);
1536 odp_mask_attr_is_exact(const struct nlattr *ma)
1538 struct flow_tnl tun_mask;
1539 enum ovs_key_attr attr = nl_attr_type(ma);
1543 if (attr == OVS_KEY_ATTR_TUNNEL) {
1544 memset(&tun_mask, 0, sizeof tun_mask);
1545 odp_tun_key_from_attr(ma, &tun_mask);
1547 size = sizeof tun_mask;
1549 mask = nl_attr_get(ma);
1550 size = nl_attr_get_size(ma);
1553 return odp_mask_is_exact(attr, mask, size);
1557 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1560 struct odp_portno_names *odp_portno_names;
1562 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1563 odp_portno_names->port_no = port_no;
1564 odp_portno_names->name = xstrdup(port_name);
1565 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1566 hash_odp_port(port_no));
1570 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1572 struct odp_portno_names *odp_portno_names;
1574 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1575 hash_odp_port(port_no), portno_names) {
1576 if (odp_portno_names->port_no == port_no) {
1577 return odp_portno_names->name;
1584 odp_portno_names_destroy(struct hmap *portno_names)
1586 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1587 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1588 hmap_node, portno_names) {
1589 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1590 free(odp_portno_names->name);
1591 free(odp_portno_names);
1595 /* Format helpers. */
1598 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1599 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1601 bool mask_empty = mask && eth_addr_is_zero(*mask);
1603 if (verbose || !mask_empty) {
1604 bool mask_full = !mask || eth_mask_is_exact(*mask);
1607 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1609 ds_put_format(ds, "%s=", name);
1610 eth_format_masked(key, *mask, ds);
1611 ds_put_char(ds, ',');
1617 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1618 const ovs_be64 *mask, bool verbose)
1620 bool mask_empty = mask && !*mask;
1622 if (verbose || !mask_empty) {
1623 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1625 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1626 if (!mask_full) { /* Partially masked. */
1627 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1629 ds_put_char(ds, ',');
1634 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1635 const ovs_be32 *mask, bool verbose)
1637 bool mask_empty = mask && !*mask;
1639 if (verbose || !mask_empty) {
1640 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1642 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1643 if (!mask_full) { /* Partially masked. */
1644 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1646 ds_put_char(ds, ',');
1651 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1652 const ovs_be32 (*mask_)[4], bool verbose)
1654 char buf[INET6_ADDRSTRLEN];
1655 const struct in6_addr *key = (const struct in6_addr *)key_;
1656 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1658 bool mask_empty = mask && ipv6_mask_is_any(mask);
1660 if (verbose || !mask_empty) {
1661 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1663 inet_ntop(AF_INET6, key, buf, sizeof buf);
1664 ds_put_format(ds, "%s=%s", name, buf);
1665 if (!mask_full) { /* Partially masked. */
1666 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1667 ds_put_format(ds, "/%s", buf);
1669 ds_put_char(ds, ',');
1674 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1675 const ovs_be32 *mask, bool verbose)
1677 bool mask_empty = mask && !*mask;
1679 if (verbose || !mask_empty) {
1680 bool mask_full = !mask
1681 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1683 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1684 if (!mask_full) { /* Partially masked. */
1685 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1687 ds_put_char(ds, ',');
1692 format_u8x(struct ds *ds, const char *name, uint8_t key,
1693 const uint8_t *mask, bool verbose)
1695 bool mask_empty = mask && !*mask;
1697 if (verbose || !mask_empty) {
1698 bool mask_full = !mask || *mask == UINT8_MAX;
1700 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1701 if (!mask_full) { /* Partially masked. */
1702 ds_put_format(ds, "/%#"PRIx8, *mask);
1704 ds_put_char(ds, ',');
1709 format_u8u(struct ds *ds, const char *name, uint8_t key,
1710 const uint8_t *mask, bool verbose)
1712 bool mask_empty = mask && !*mask;
1714 if (verbose || !mask_empty) {
1715 bool mask_full = !mask || *mask == UINT8_MAX;
1717 ds_put_format(ds, "%s=%"PRIu8, name, key);
1718 if (!mask_full) { /* Partially masked. */
1719 ds_put_format(ds, "/%#"PRIx8, *mask);
1721 ds_put_char(ds, ',');
1726 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1727 const ovs_be16 *mask, bool verbose)
1729 bool mask_empty = mask && !*mask;
1731 if (verbose || !mask_empty) {
1732 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1734 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1735 if (!mask_full) { /* Partially masked. */
1736 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1738 ds_put_char(ds, ',');
1743 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1744 const uint16_t *mask, bool verbose)
1746 bool mask_empty = mask && !*mask;
1748 if (verbose || !mask_empty) {
1749 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1751 ds_put_cstr(ds, name);
1752 ds_put_char(ds, '(');
1753 if (!mask_full) { /* Partially masked. */
1754 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1755 } else { /* Fully masked. */
1756 format_flags(ds, flow_tun_flag_to_string, key, ',');
1758 ds_put_cstr(ds, "),");
1763 format_frag(struct ds *ds, const char *name, uint8_t key,
1764 const uint8_t *mask, bool verbose)
1766 bool mask_empty = mask && !*mask;
1768 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1769 if (verbose || !mask_empty) {
1770 bool mask_full = !mask || *mask == UINT8_MAX;
1772 if (!mask_full) { /* Partially masked. */
1773 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1776 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1781 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1784 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1785 const struct hmap *portno_names, struct ds *ds,
1788 enum ovs_key_attr attr = nl_attr_type(a);
1789 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1793 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1795 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1798 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1799 if (expected_len != -2) {
1800 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1801 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1803 if (bad_key_len || bad_mask_len) {
1805 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1806 nl_attr_get_size(a), expected_len);
1808 format_generic_odp_key(a, ds);
1810 ds_put_char(ds, '/');
1812 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1813 nl_attr_get_size(ma), expected_len);
1815 format_generic_odp_key(ma, ds);
1817 ds_put_char(ds, ')');
1823 ds_put_char(ds, '(');
1825 case OVS_KEY_ATTR_ENCAP:
1826 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1827 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1828 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1830 } else if (nl_attr_get_size(a)) {
1831 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1836 case OVS_KEY_ATTR_PRIORITY:
1837 case OVS_KEY_ATTR_SKB_MARK:
1838 case OVS_KEY_ATTR_DP_HASH:
1839 case OVS_KEY_ATTR_RECIRC_ID:
1840 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1842 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1846 case OVS_KEY_ATTR_TUNNEL: {
1847 struct flow_tnl key, mask_;
1848 struct flow_tnl *mask = ma ? &mask_ : NULL;
1851 memset(mask, 0, sizeof *mask);
1852 odp_tun_key_from_attr(ma, mask);
1854 memset(&key, 0, sizeof key);
1855 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1856 ds_put_format(ds, "error");
1859 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1860 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1861 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1862 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1863 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1864 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1865 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1866 format_be16(ds, "gbp_id", key.gbp_id, MASK(mask, gbp_id), verbose);
1867 format_u8x(ds, "gbp_flags", key.gbp_flags, MASK(mask, gbp_flags), verbose);
1868 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1872 case OVS_KEY_ATTR_IN_PORT:
1873 if (portno_names && verbose && is_exact) {
1874 char *name = odp_portno_names_get(portno_names,
1875 u32_to_odp(nl_attr_get_u32(a)));
1877 ds_put_format(ds, "%s", name);
1879 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1882 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1884 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1889 case OVS_KEY_ATTR_ETHERNET: {
1890 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1891 const struct ovs_key_ethernet *key = nl_attr_get(a);
1893 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1894 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1898 case OVS_KEY_ATTR_VLAN:
1899 format_vlan_tci(ds, nl_attr_get_be16(a),
1900 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1903 case OVS_KEY_ATTR_MPLS: {
1904 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1905 const struct ovs_key_mpls *mpls_mask = NULL;
1906 size_t size = nl_attr_get_size(a);
1908 if (!size || size % sizeof *mpls_key) {
1909 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1913 mpls_mask = nl_attr_get(ma);
1914 if (size != nl_attr_get_size(ma)) {
1915 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1916 "mask length %"PRIuSIZE")",
1917 size, nl_attr_get_size(ma));
1921 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1924 case OVS_KEY_ATTR_ETHERTYPE:
1925 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1927 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1931 case OVS_KEY_ATTR_IPV4: {
1932 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1933 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1935 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1936 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1937 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1939 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1940 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1941 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1946 case OVS_KEY_ATTR_IPV6: {
1947 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1948 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1950 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1951 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1952 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1954 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1956 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1958 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1960 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1965 /* These have the same structure and format. */
1966 case OVS_KEY_ATTR_TCP:
1967 case OVS_KEY_ATTR_UDP:
1968 case OVS_KEY_ATTR_SCTP: {
1969 const struct ovs_key_tcp *key = nl_attr_get(a);
1970 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1972 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1973 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1977 case OVS_KEY_ATTR_TCP_FLAGS:
1979 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1980 ntohs(nl_attr_get_be16(a)),
1981 ntohs(nl_attr_get_be16(ma)));
1983 format_flags(ds, packet_tcp_flag_to_string,
1984 ntohs(nl_attr_get_be16(a)), ',');
1988 case OVS_KEY_ATTR_ICMP: {
1989 const struct ovs_key_icmp *key = nl_attr_get(a);
1990 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1992 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1993 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1997 case OVS_KEY_ATTR_ICMPV6: {
1998 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
1999 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2001 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2003 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2008 case OVS_KEY_ATTR_ARP: {
2009 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2010 const struct ovs_key_arp *key = nl_attr_get(a);
2012 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2013 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2014 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2015 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2016 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2020 case OVS_KEY_ATTR_ND: {
2021 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2022 const struct ovs_key_nd *key = nl_attr_get(a);
2024 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2026 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2027 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2032 case OVS_KEY_ATTR_UNSPEC:
2033 case __OVS_KEY_ATTR_MAX:
2035 format_generic_odp_key(a, ds);
2037 ds_put_char(ds, '/');
2038 format_generic_odp_key(ma, ds);
2042 ds_put_char(ds, ')');
2045 static struct nlattr *
2046 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
2048 const struct nlattr *a;
2050 int type = nl_attr_type(key);
2051 int size = nl_attr_get_size(key);
2053 if (odp_flow_key_attr_len(type) >=0) {
2054 nl_msg_put_unspec_zero(ofp, type, size);
2058 nested_mask = nl_msg_start_nested(ofp, type);
2059 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2060 generate_all_wildcard_mask(ofp, nl_attr_get(a));
2062 nl_msg_end_nested(ofp, nested_mask);
2069 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2073 if (ovs_scan(s, "ufid:")) {
2077 if (ovs_scan(s, "0x")) {
2081 n = strspn(s, "0123456789abcdefABCDEF");
2086 if (!ovs_scan(s, "%16"SCNx64"%16"SCNx64, &ufid->u64.hi,
2091 s += strspn(s, delimiters);
2100 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2102 ds_put_format(ds, "ufid:%016"PRIx64"%016"PRIx64, ufid->u64.hi,
2106 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2107 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2108 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2109 * non-null and 'verbose' is true, translates odp port number to its name. */
2111 odp_flow_format(const struct nlattr *key, size_t key_len,
2112 const struct nlattr *mask, size_t mask_len,
2113 const struct hmap *portno_names, struct ds *ds, bool verbose)
2116 const struct nlattr *a;
2118 bool has_ethtype_key = false;
2119 const struct nlattr *ma = NULL;
2121 bool first_field = true;
2123 ofpbuf_init(&ofp, 100);
2124 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2125 bool is_nested_attr;
2126 bool is_wildcard = false;
2127 int attr_type = nl_attr_type(a);
2129 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2130 has_ethtype_key = true;
2133 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
2135 if (mask && mask_len) {
2136 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2137 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2140 if (verbose || !is_wildcard || is_nested_attr) {
2141 if (is_wildcard && !ma) {
2142 ma = generate_all_wildcard_mask(&ofp, a);
2145 ds_put_char(ds, ',');
2147 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2148 first_field = false;
2152 ofpbuf_uninit(&ofp);
2157 if (left == key_len) {
2158 ds_put_cstr(ds, "<empty>");
2160 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2161 for (i = 0; i < left; i++) {
2162 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2164 ds_put_char(ds, ')');
2166 if (!has_ethtype_key) {
2167 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2169 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2170 ntohs(nl_attr_get_be16(ma)));
2174 ds_put_cstr(ds, "<empty>");
2178 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2179 * OVS_KEY_ATTR_* attributes in 'key'. */
2181 odp_flow_key_format(const struct nlattr *key,
2182 size_t key_len, struct ds *ds)
2184 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2188 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2190 if (!strcasecmp(s, "no")) {
2191 *type = OVS_FRAG_TYPE_NONE;
2192 } else if (!strcasecmp(s, "first")) {
2193 *type = OVS_FRAG_TYPE_FIRST;
2194 } else if (!strcasecmp(s, "later")) {
2195 *type = OVS_FRAG_TYPE_LATER;
2205 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2206 uint8_t (*mask)[ETH_ADDR_LEN])
2210 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2214 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2215 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2218 memset(mask, 0xff, sizeof *mask);
2227 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2231 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2235 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2236 IP_SCAN_ARGS(mask), &n)) {
2239 *mask = OVS_BE32_MAX;
2248 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2251 char ipv6_s[IPV6_SCAN_LEN + 1];
2253 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2254 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2258 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2259 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2262 memset(mask, 0xff, sizeof *mask);
2271 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2276 if (ovs_scan(s, "%i%n", &key_, &n)
2277 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2282 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2283 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2285 *mask = htonl(mask_);
2287 *mask = htonl(IPV6_LABEL_MASK);
2296 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2300 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2304 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2316 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2320 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2324 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2336 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2338 uint16_t key_, mask_;
2341 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2346 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2348 *mask = htons(mask_);
2350 *mask = OVS_BE16_MAX;
2359 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2361 uint64_t key_, mask_;
2364 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2367 *key = htonll(key_);
2369 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2371 *mask = htonll(mask_);
2373 *mask = OVS_BE64_MAX;
2382 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2384 uint32_t flags, fmask;
2387 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2388 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2389 if (n >= 0 && s[n] == ')') {
2400 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2402 uint32_t flags, fmask;
2405 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2406 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2408 *key = htons(flags);
2410 *mask = htons(fmask);
2418 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2422 enum ovs_frag_type frag_type;
2424 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2425 && ovs_frag_type_from_string(frag, &frag_type)) {
2438 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2439 const struct simap *port_names)
2443 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2447 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2454 } else if (port_names) {
2455 const struct simap_node *node;
2458 len = strcspn(s, ")");
2459 node = simap_find_len(port_names, s, len);
2472 /* Helper for vlan parsing. */
2473 struct ovs_key_vlan__ {
2478 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2480 const uint16_t mask = ((1U << bits) - 1) << offset;
2482 if (value >> bits) {
2486 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2491 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2494 uint16_t key_, mask_;
2497 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2500 if (set_be16_bf(key, bits, offset, key_)) {
2502 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2505 if (!set_be16_bf(mask, bits, offset, mask_)) {
2509 *mask |= htons(((1U << bits) - 1) << offset);
2519 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2521 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2525 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2527 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2531 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2533 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2538 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2540 const uint32_t mask = ((1U << bits) - 1) << offset;
2542 if (value >> bits) {
2546 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2551 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2554 uint32_t key_, mask_;
2557 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2560 if (set_be32_bf(key, bits, offset, key_)) {
2562 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2565 if (!set_be32_bf(mask, bits, offset, mask_)) {
2569 *mask |= htonl(((1U << bits) - 1) << offset);
2579 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2581 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2585 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2587 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2591 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2593 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2597 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2599 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2602 /* ATTR is compile-time constant, so only the case with correct data type
2603 * will be used. However, the compiler complains about the data type for
2604 * the other cases, so we must cast to make the compiler silent. */
2605 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2606 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2607 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2609 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2612 #define SCAN_IF(NAME) \
2613 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2614 const char *start = s; \
2619 /* Usually no special initialization is needed. */
2620 #define SCAN_BEGIN(NAME, TYPE) \
2623 memset(&skey, 0, sizeof skey); \
2624 memset(&smask, 0, sizeof smask); \
2628 /* Init as fully-masked as mask will not be scanned. */
2629 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2632 memset(&skey, 0, sizeof skey); \
2633 memset(&smask, 0xff, sizeof smask); \
2637 /* VLAN needs special initialization. */
2638 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2640 TYPE skey = KEY_INIT; \
2641 TYPE smask = MASK_INIT; \
2645 /* Scan unnamed entry as 'TYPE' */
2646 #define SCAN_TYPE(TYPE, KEY, MASK) \
2647 len = scan_##TYPE(s, KEY, MASK); \
2653 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2654 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2655 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2656 s += strlen(NAME); \
2657 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2661 #define SCAN_FINISH() \
2662 } while (*s++ == ',' && len != 0); \
2663 if (s[-1] != ')') { \
2667 #define SCAN_FINISH_SINGLE() \
2669 if (*s++ != ')') { \
2673 #define SCAN_PUT(ATTR) \
2674 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2675 SCAN_PUT_ATTR(key, ATTR, skey); \
2677 SCAN_PUT_ATTR(mask, ATTR, smask); \
2681 #define SCAN_END(ATTR) \
2687 #define SCAN_END_SINGLE(ATTR) \
2688 SCAN_FINISH_SINGLE(); \
2693 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2694 SCAN_BEGIN(NAME, TYPE) { \
2695 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2696 } SCAN_END_SINGLE(ATTR)
2698 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
2699 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
2700 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2701 } SCAN_END_SINGLE(ATTR)
2703 /* scan_port needs one extra argument. */
2704 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2705 SCAN_BEGIN(NAME, TYPE) { \
2706 len = scan_port(s, &skey, &smask, port_names); \
2711 } SCAN_END_SINGLE(ATTR)
2714 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2715 struct ofpbuf *key, struct ofpbuf *mask)
2717 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2718 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2719 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
2720 OVS_KEY_ATTR_RECIRC_ID);
2721 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2723 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2724 SCAN_FIELD("tun_id=", be64, tun_id);
2725 SCAN_FIELD("src=", ipv4, ip_src);
2726 SCAN_FIELD("dst=", ipv4, ip_dst);
2727 SCAN_FIELD("tos=", u8, ip_tos);
2728 SCAN_FIELD("ttl=", u8, ip_ttl);
2729 SCAN_FIELD("tp_src=", be16, tp_src);
2730 SCAN_FIELD("tp_dst=", be16, tp_dst);
2731 SCAN_FIELD("gbp_id=", be16, gbp_id);
2732 SCAN_FIELD("gbp_flags=", u8, gbp_flags);
2733 SCAN_FIELD("flags(", tun_flags, flags);
2734 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2736 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2738 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2739 SCAN_FIELD("src=", eth, eth_src);
2740 SCAN_FIELD("dst=", eth, eth_dst);
2741 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2743 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2744 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2745 SCAN_FIELD("vid=", vid, tci);
2746 SCAN_FIELD("pcp=", pcp, tci);
2747 SCAN_FIELD("cfi=", cfi, tci);
2748 } SCAN_END(OVS_KEY_ATTR_VLAN);
2750 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2752 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2753 SCAN_FIELD("label=", mpls_label, mpls_lse);
2754 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2755 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2756 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2757 } SCAN_END(OVS_KEY_ATTR_MPLS);
2759 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2760 SCAN_FIELD("src=", ipv4, ipv4_src);
2761 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2762 SCAN_FIELD("proto=", u8, ipv4_proto);
2763 SCAN_FIELD("tos=", u8, ipv4_tos);
2764 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2765 SCAN_FIELD("frag=", frag, ipv4_frag);
2766 } SCAN_END(OVS_KEY_ATTR_IPV4);
2768 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2769 SCAN_FIELD("src=", ipv6, ipv6_src);
2770 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2771 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2772 SCAN_FIELD("proto=", u8, ipv6_proto);
2773 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2774 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2775 SCAN_FIELD("frag=", frag, ipv6_frag);
2776 } SCAN_END(OVS_KEY_ATTR_IPV6);
2778 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2779 SCAN_FIELD("src=", be16, tcp_src);
2780 SCAN_FIELD("dst=", be16, tcp_dst);
2781 } SCAN_END(OVS_KEY_ATTR_TCP);
2783 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2785 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2786 SCAN_FIELD("src=", be16, udp_src);
2787 SCAN_FIELD("dst=", be16, udp_dst);
2788 } SCAN_END(OVS_KEY_ATTR_UDP);
2790 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2791 SCAN_FIELD("src=", be16, sctp_src);
2792 SCAN_FIELD("dst=", be16, sctp_dst);
2793 } SCAN_END(OVS_KEY_ATTR_SCTP);
2795 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2796 SCAN_FIELD("type=", u8, icmp_type);
2797 SCAN_FIELD("code=", u8, icmp_code);
2798 } SCAN_END(OVS_KEY_ATTR_ICMP);
2800 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2801 SCAN_FIELD("type=", u8, icmpv6_type);
2802 SCAN_FIELD("code=", u8, icmpv6_code);
2803 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2805 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2806 SCAN_FIELD("sip=", ipv4, arp_sip);
2807 SCAN_FIELD("tip=", ipv4, arp_tip);
2808 SCAN_FIELD("op=", be16, arp_op);
2809 SCAN_FIELD("sha=", eth, arp_sha);
2810 SCAN_FIELD("tha=", eth, arp_tha);
2811 } SCAN_END(OVS_KEY_ATTR_ARP);
2813 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2814 SCAN_FIELD("target=", ipv6, nd_target);
2815 SCAN_FIELD("sll=", eth, nd_sll);
2816 SCAN_FIELD("tll=", eth, nd_tll);
2817 } SCAN_END(OVS_KEY_ATTR_ND);
2819 /* Encap open-coded. */
2820 if (!strncmp(s, "encap(", 6)) {
2821 const char *start = s;
2822 size_t encap, encap_mask = 0;
2824 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2826 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2833 s += strspn(s, delimiters);
2836 } else if (*s == ')') {
2840 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2848 nl_msg_end_nested(key, encap);
2850 nl_msg_end_nested(mask, encap_mask);
2859 /* Parses the string representation of a datapath flow key, in the
2860 * format output by odp_flow_key_format(). Returns 0 if successful,
2861 * otherwise a positive errno value. On success, the flow key is
2862 * appended to 'key' as a series of Netlink attributes. On failure, no
2863 * data is appended to 'key'. Either way, 'key''s data might be
2866 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2867 * to a port number. (Port names may be used instead of port numbers in
2870 * On success, the attributes appended to 'key' are individually syntactically
2871 * valid, but they may not be valid as a sequence. 'key' might, for example,
2872 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2874 odp_flow_from_string(const char *s, const struct simap *port_names,
2875 struct ofpbuf *key, struct ofpbuf *mask)
2877 const size_t old_size = key->size;
2881 s += strspn(s, delimiters);
2886 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2888 key->size = old_size;
2898 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
2901 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2902 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2903 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2904 * must use a zero mask for the netlink frag field, and all ones mask
2906 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
2908 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
2909 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
2910 : OVS_FRAG_TYPE_FIRST;
2913 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
2914 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
2915 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
2917 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
2919 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
2921 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
2923 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
2924 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
2925 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
2926 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
2928 /* These share the same layout. */
2930 struct ovs_key_tcp tcp;
2931 struct ovs_key_udp udp;
2932 struct ovs_key_sctp sctp;
2935 static void get_tp_key(const struct flow *, union ovs_key_tp *);
2936 static void put_tp_key(const union ovs_key_tp *, struct flow *);
2939 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2940 const struct flow *mask, odp_port_t odp_in_port,
2941 size_t max_mpls_depth, bool recirc, bool export_mask)
2943 struct ovs_key_ethernet *eth_key;
2945 const struct flow *data = export_mask ? mask : flow;
2947 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2949 if (flow->tunnel.ip_dst || export_mask) {
2950 tun_key_to_attr(buf, &data->tunnel);
2953 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2956 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2957 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2960 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2961 * is not the magical value "ODPP_NONE". */
2962 if (export_mask || odp_in_port != ODPP_NONE) {
2963 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2966 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2968 get_ethernet_key(data, eth_key);
2970 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2972 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2974 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2976 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2977 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2978 if (flow->vlan_tci == htons(0)) {
2985 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2986 /* For backwards compatibility with kernels that don't support
2987 * wildcarding, the following convention is used to encode the
2988 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2991 * -------- -------- -------
2992 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2993 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2994 * <none> 0xffff Any non-Ethernet II frame (except valid
2995 * 802.3 SNAP packet with valid eth_type).
2998 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3003 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3005 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3006 struct ovs_key_ipv4 *ipv4_key;
3008 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3010 get_ipv4_key(data, ipv4_key, export_mask);
3011 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3012 struct ovs_key_ipv6 *ipv6_key;
3014 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3016 get_ipv6_key(data, ipv6_key, export_mask);
3017 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3018 flow->dl_type == htons(ETH_TYPE_RARP)) {
3019 struct ovs_key_arp *arp_key;
3021 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3023 get_arp_key(data, arp_key);
3024 } else if (eth_type_mpls(flow->dl_type)) {
3025 struct ovs_key_mpls *mpls_key;
3028 n = flow_count_mpls_labels(flow, NULL);
3029 n = MIN(n, max_mpls_depth);
3030 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3031 n * sizeof *mpls_key);
3032 for (i = 0; i < n; i++) {
3033 mpls_key[i].mpls_lse = data->mpls_lse[i];
3037 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3038 if (flow->nw_proto == IPPROTO_TCP) {
3039 union ovs_key_tp *tcp_key;
3041 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3043 get_tp_key(data, tcp_key);
3044 if (data->tcp_flags) {
3045 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3047 } else if (flow->nw_proto == IPPROTO_UDP) {
3048 union ovs_key_tp *udp_key;
3050 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3052 get_tp_key(data, udp_key);
3053 } else if (flow->nw_proto == IPPROTO_SCTP) {
3054 union ovs_key_tp *sctp_key;
3056 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3058 get_tp_key(data, sctp_key);
3059 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3060 && flow->nw_proto == IPPROTO_ICMP) {
3061 struct ovs_key_icmp *icmp_key;
3063 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3065 icmp_key->icmp_type = ntohs(data->tp_src);
3066 icmp_key->icmp_code = ntohs(data->tp_dst);
3067 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3068 && flow->nw_proto == IPPROTO_ICMPV6) {
3069 struct ovs_key_icmpv6 *icmpv6_key;
3071 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3072 sizeof *icmpv6_key);
3073 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3074 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3076 if (flow->tp_dst == htons(0)
3077 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3078 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3079 && (!export_mask || (data->tp_src == htons(0xffff)
3080 && data->tp_dst == htons(0xffff)))) {
3082 struct ovs_key_nd *nd_key;
3084 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3086 memcpy(nd_key->nd_target, &data->nd_target,
3087 sizeof nd_key->nd_target);
3088 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3089 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3096 nl_msg_end_nested(buf, encap);
3100 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3101 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3102 * number rather than a datapath port number). Instead, if 'odp_in_port'
3103 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3106 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3107 * capable of being expanded to allow for that much space.
3109 * 'recirc' indicates support for recirculation fields. If this is true, then
3110 * these fields will always be serialised. */
3112 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
3113 const struct flow *mask, odp_port_t odp_in_port,
3116 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3120 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3121 * 'buf'. 'flow' is used as a template to determine how to interpret
3122 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3123 * it doesn't indicate whether the other fields should be interpreted as
3124 * ARP, IPv4, IPv6, etc.
3126 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3127 * capable of being expanded to allow for that much space.
3129 * 'recirc' indicates support for recirculation fields. If this is true, then
3130 * these fields will always be serialised. */
3132 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
3133 const struct flow *flow, uint32_t odp_in_port_mask,
3134 size_t max_mpls_depth, bool recirc)
3136 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3137 max_mpls_depth, recirc, true);
3140 /* Generate ODP flow key from the given packet metadata */
3142 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3144 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3146 if (md->tunnel.ip_dst) {
3147 tun_key_to_attr(buf, &md->tunnel);
3150 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3152 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3153 * value "ODPP_NONE". */
3154 if (md->in_port.odp_port != ODPP_NONE) {
3155 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3159 /* Generate packet metadata from the given ODP flow key. */
3161 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3162 struct pkt_metadata *md)
3164 const struct nlattr *nla;
3166 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3167 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3168 1u << OVS_KEY_ATTR_IN_PORT;
3170 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3172 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3173 uint16_t type = nl_attr_type(nla);
3174 size_t len = nl_attr_get_size(nla);
3175 int expected_len = odp_flow_key_attr_len(type);
3177 if (len != expected_len && expected_len >= 0) {
3182 case OVS_KEY_ATTR_RECIRC_ID:
3183 md->recirc_id = nl_attr_get_u32(nla);
3184 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3186 case OVS_KEY_ATTR_DP_HASH:
3187 md->dp_hash = nl_attr_get_u32(nla);
3188 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3190 case OVS_KEY_ATTR_PRIORITY:
3191 md->skb_priority = nl_attr_get_u32(nla);
3192 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3194 case OVS_KEY_ATTR_SKB_MARK:
3195 md->pkt_mark = nl_attr_get_u32(nla);
3196 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3198 case OVS_KEY_ATTR_TUNNEL: {
3199 enum odp_key_fitness res;
3201 res = odp_tun_key_from_attr(nla, &md->tunnel);
3202 if (res == ODP_FIT_ERROR) {
3203 memset(&md->tunnel, 0, sizeof md->tunnel);
3204 } else if (res == ODP_FIT_PERFECT) {
3205 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3209 case OVS_KEY_ATTR_IN_PORT:
3210 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3211 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3217 if (!wanted_attrs) {
3218 return; /* Have everything. */
3224 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3226 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3227 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3228 key_len / sizeof(uint32_t), 0);
3232 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3233 uint64_t attrs, int out_of_range_attr,
3234 const struct nlattr *key, size_t key_len)
3239 if (VLOG_DROP_DBG(rl)) {
3244 for (i = 0; i < 64; i++) {
3245 if (attrs & (UINT64_C(1) << i)) {
3246 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3248 ds_put_format(&s, " %s",
3249 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3252 if (out_of_range_attr) {
3253 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3256 ds_put_cstr(&s, ": ");
3257 odp_flow_key_format(key, key_len, &s);
3259 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3264 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3266 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3269 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3272 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3273 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3274 return 0xff; /* Error. */
3277 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3278 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3279 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3283 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3284 const struct nlattr *attrs[], uint64_t *present_attrsp,
3285 int *out_of_range_attrp)
3287 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3288 const struct nlattr *nla;
3289 uint64_t present_attrs;
3292 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3294 *out_of_range_attrp = 0;
3295 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3296 uint16_t type = nl_attr_type(nla);
3297 size_t len = nl_attr_get_size(nla);
3298 int expected_len = odp_flow_key_attr_len(type);
3300 if (len != expected_len && expected_len >= 0) {
3301 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3303 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3304 "length %d", ovs_key_attr_to_string(type, namebuf,
3310 if (type > OVS_KEY_ATTR_MAX) {
3311 *out_of_range_attrp = type;
3313 if (present_attrs & (UINT64_C(1) << type)) {
3314 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3316 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3317 ovs_key_attr_to_string(type,
3318 namebuf, sizeof namebuf));
3322 present_attrs |= UINT64_C(1) << type;
3327 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3331 *present_attrsp = present_attrs;
3335 static enum odp_key_fitness
3336 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3337 uint64_t expected_attrs,
3338 const struct nlattr *key, size_t key_len)
3340 uint64_t missing_attrs;
3341 uint64_t extra_attrs;
3343 missing_attrs = expected_attrs & ~present_attrs;
3344 if (missing_attrs) {
3345 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3346 log_odp_key_attributes(&rl, "expected but not present",
3347 missing_attrs, 0, key, key_len);
3348 return ODP_FIT_TOO_LITTLE;
3351 extra_attrs = present_attrs & ~expected_attrs;
3352 if (extra_attrs || out_of_range_attr) {
3353 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3354 log_odp_key_attributes(&rl, "present but not expected",
3355 extra_attrs, out_of_range_attr, key, key_len);
3356 return ODP_FIT_TOO_MUCH;
3359 return ODP_FIT_PERFECT;
3363 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3364 uint64_t present_attrs, uint64_t *expected_attrs,
3365 struct flow *flow, const struct flow *src_flow)
3367 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3368 bool is_mask = flow != src_flow;
3370 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3371 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3372 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3373 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3374 ntohs(flow->dl_type));
3377 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3378 flow->dl_type != htons(0xffff)) {
3381 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3384 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3385 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3386 /* See comments in odp_flow_key_from_flow__(). */
3387 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3394 static enum odp_key_fitness
3395 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3396 uint64_t present_attrs, int out_of_range_attr,
3397 uint64_t expected_attrs, struct flow *flow,
3398 const struct nlattr *key, size_t key_len,
3399 const struct flow *src_flow)
3401 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3402 bool is_mask = src_flow != flow;
3403 const void *check_start = NULL;
3404 size_t check_len = 0;
3405 enum ovs_key_attr expected_bit = 0xff;
3407 if (eth_type_mpls(src_flow->dl_type)) {
3408 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3409 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3411 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3412 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3413 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3414 int n = size / sizeof(ovs_be32);
3417 if (!size || size % sizeof(ovs_be32)) {
3418 return ODP_FIT_ERROR;
3420 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3421 return ODP_FIT_ERROR;
3424 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3425 flow->mpls_lse[i] = mpls_lse[i];
3427 if (n > FLOW_MAX_MPLS_LABELS) {
3428 return ODP_FIT_TOO_MUCH;
3432 /* BOS may be set only in the innermost label. */
3433 for (i = 0; i < n - 1; i++) {
3434 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3435 return ODP_FIT_ERROR;
3439 /* BOS must be set in the innermost label. */
3440 if (n < FLOW_MAX_MPLS_LABELS
3441 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3442 return ODP_FIT_TOO_LITTLE;
3448 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3450 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3452 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3453 const struct ovs_key_ipv4 *ipv4_key;
3455 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3456 put_ipv4_key(ipv4_key, flow, is_mask);
3457 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3458 return ODP_FIT_ERROR;
3461 check_start = ipv4_key;
3462 check_len = sizeof *ipv4_key;
3463 expected_bit = OVS_KEY_ATTR_IPV4;
3466 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3468 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3470 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3471 const struct ovs_key_ipv6 *ipv6_key;
3473 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3474 put_ipv6_key(ipv6_key, flow, is_mask);
3475 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3476 return ODP_FIT_ERROR;
3479 check_start = ipv6_key;
3480 check_len = sizeof *ipv6_key;
3481 expected_bit = OVS_KEY_ATTR_IPV6;
3484 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3485 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3487 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3489 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3490 const struct ovs_key_arp *arp_key;
3492 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3493 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3494 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3495 "key", ntohs(arp_key->arp_op));
3496 return ODP_FIT_ERROR;
3498 put_arp_key(arp_key, flow);
3500 check_start = arp_key;
3501 check_len = sizeof *arp_key;
3502 expected_bit = OVS_KEY_ATTR_ARP;
3508 if (check_len > 0) { /* Happens only when 'is_mask'. */
3509 if (!is_all_zeros(check_start, check_len) &&
3510 flow->dl_type != htons(0xffff)) {
3511 return ODP_FIT_ERROR;
3513 expected_attrs |= UINT64_C(1) << expected_bit;
3517 expected_bit = OVS_KEY_ATTR_UNSPEC;
3518 if (src_flow->nw_proto == IPPROTO_TCP
3519 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3520 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3521 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3523 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3525 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3526 const union ovs_key_tp *tcp_key;
3528 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3529 put_tp_key(tcp_key, flow);
3530 expected_bit = OVS_KEY_ATTR_TCP;
3532 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3533 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3534 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3536 } else if (src_flow->nw_proto == IPPROTO_UDP
3537 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3538 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3539 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3541 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3543 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3544 const union ovs_key_tp *udp_key;
3546 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3547 put_tp_key(udp_key, flow);
3548 expected_bit = OVS_KEY_ATTR_UDP;
3550 } else if (src_flow->nw_proto == IPPROTO_SCTP
3551 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3552 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3553 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3555 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3557 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3558 const union ovs_key_tp *sctp_key;
3560 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3561 put_tp_key(sctp_key, flow);
3562 expected_bit = OVS_KEY_ATTR_SCTP;
3564 } else if (src_flow->nw_proto == IPPROTO_ICMP
3565 && src_flow->dl_type == htons(ETH_TYPE_IP)
3566 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3568 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3570 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3571 const struct ovs_key_icmp *icmp_key;
3573 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3574 flow->tp_src = htons(icmp_key->icmp_type);
3575 flow->tp_dst = htons(icmp_key->icmp_code);
3576 expected_bit = OVS_KEY_ATTR_ICMP;
3578 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3579 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3580 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3582 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3584 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3585 const struct ovs_key_icmpv6 *icmpv6_key;
3587 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3588 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3589 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3590 expected_bit = OVS_KEY_ATTR_ICMPV6;
3591 if (src_flow->tp_dst == htons(0) &&
3592 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3593 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3595 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3597 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3598 const struct ovs_key_nd *nd_key;
3600 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3601 memcpy(&flow->nd_target, nd_key->nd_target,
3602 sizeof flow->nd_target);
3603 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3604 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3606 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3607 (flow->tp_src != htons(0xffff) ||
3608 flow->tp_dst != htons(0xffff))) {
3609 return ODP_FIT_ERROR;
3611 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3618 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3619 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3620 return ODP_FIT_ERROR;
3622 expected_attrs |= UINT64_C(1) << expected_bit;
3627 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3631 /* Parse 802.1Q header then encapsulated L3 attributes. */
3632 static enum odp_key_fitness
3633 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3634 uint64_t present_attrs, int out_of_range_attr,
3635 uint64_t expected_attrs, struct flow *flow,
3636 const struct nlattr *key, size_t key_len,
3637 const struct flow *src_flow)
3639 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3640 bool is_mask = src_flow != flow;
3642 const struct nlattr *encap
3643 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3644 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3645 enum odp_key_fitness encap_fitness;
3646 enum odp_key_fitness fitness;
3648 /* Calculate fitness of outer attributes. */
3650 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3651 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3653 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3654 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3656 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3657 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3660 fitness = check_expectations(present_attrs, out_of_range_attr,
3661 expected_attrs, key, key_len);
3664 * Remove the TPID from dl_type since it's not the real Ethertype. */
3665 flow->dl_type = htons(0);
3666 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3667 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3670 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3671 return ODP_FIT_TOO_LITTLE;
3672 } else if (flow->vlan_tci == htons(0)) {
3673 /* Corner case for a truncated 802.1Q header. */
3674 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3675 return ODP_FIT_TOO_MUCH;
3678 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3679 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3680 "but CFI bit is not set", ntohs(flow->vlan_tci));
3681 return ODP_FIT_ERROR;
3684 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3689 /* Now parse the encapsulated attributes. */
3690 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3691 attrs, &present_attrs, &out_of_range_attr)) {
3692 return ODP_FIT_ERROR;
3696 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3697 return ODP_FIT_ERROR;
3699 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3700 expected_attrs, flow, key, key_len,
3703 /* The overall fitness is the worse of the outer and inner attributes. */
3704 return MAX(fitness, encap_fitness);
3707 static enum odp_key_fitness
3708 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3709 struct flow *flow, const struct flow *src_flow)
3711 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3712 uint64_t expected_attrs;
3713 uint64_t present_attrs;
3714 int out_of_range_attr;
3715 bool is_mask = src_flow != flow;
3717 memset(flow, 0, sizeof *flow);
3719 /* Parse attributes. */
3720 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3721 &out_of_range_attr)) {
3722 return ODP_FIT_ERROR;
3727 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3728 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3729 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3730 } else if (is_mask) {
3731 /* Always exact match recirc_id if it is not specified. */
3732 flow->recirc_id = UINT32_MAX;
3735 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3736 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3737 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3739 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3740 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3741 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3744 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3745 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3746 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3749 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3750 enum odp_key_fitness res;
3752 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3753 if (res == ODP_FIT_ERROR) {
3754 return ODP_FIT_ERROR;
3755 } else if (res == ODP_FIT_PERFECT) {
3756 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3760 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3761 flow->in_port.odp_port
3762 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3763 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3764 } else if (!is_mask) {
3765 flow->in_port.odp_port = ODPP_NONE;
3768 /* Ethernet header. */
3769 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3770 const struct ovs_key_ethernet *eth_key;
3772 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3773 put_ethernet_key(eth_key, flow);
3775 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3779 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3782 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3783 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3785 return ODP_FIT_ERROR;
3789 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3790 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3791 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3792 expected_attrs, flow, key, key_len, src_flow);
3795 flow->vlan_tci = htons(0xffff);
3796 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3797 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3798 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3801 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3802 expected_attrs, flow, key, key_len, src_flow);
3805 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3806 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3807 * 'key' fits our expectations for what a flow key should contain.
3809 * The 'in_port' will be the datapath's understanding of the port. The
3810 * caller will need to translate with odp_port_to_ofp_port() if the
3811 * OpenFlow port is needed.
3813 * This function doesn't take the packet itself as an argument because none of
3814 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3815 * it is always possible to infer which additional attribute(s) should appear
3816 * by looking at the attributes for lower-level protocols, e.g. if the network
3817 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3818 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3819 * must be absent. */
3820 enum odp_key_fitness
3821 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3824 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3827 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3828 * structure in 'mask'. 'flow' must be a previously translated flow
3829 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3830 * 'key' fits our expectations for what a flow key should contain. */
3831 enum odp_key_fitness
3832 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3833 struct flow *mask, const struct flow *flow)
3835 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3838 /* Returns 'fitness' as a string, for use in debug messages. */
3840 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3843 case ODP_FIT_PERFECT:
3845 case ODP_FIT_TOO_MUCH:
3847 case ODP_FIT_TOO_LITTLE:
3848 return "too_little";
3856 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3857 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3858 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3859 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3860 * null, then the return value is not meaningful.) */
3862 odp_put_userspace_action(uint32_t pid,
3863 const void *userdata, size_t userdata_size,
3864 odp_port_t tunnel_out_port,
3865 struct ofpbuf *odp_actions)
3867 size_t userdata_ofs;
3870 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3871 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3873 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3875 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3876 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3879 * - The kernel rejected shorter userdata with -ERANGE.
3881 * - The kernel silently dropped userdata beyond the first 8 bytes.
3883 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3884 * separately disable features that required more than 8 bytes.) */
3885 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3886 MAX(8, userdata_size)),
3887 userdata, userdata_size);
3891 if (tunnel_out_port != ODPP_NONE) {
3892 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3895 nl_msg_end_nested(odp_actions, offset);
3897 return userdata_ofs;
3901 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3902 struct ofpbuf *odp_actions)
3904 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3905 tun_key_to_attr(odp_actions, tunnel);
3906 nl_msg_end_nested(odp_actions, offset);
3910 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
3911 struct ovs_action_push_tnl *data)
3913 int size = offsetof(struct ovs_action_push_tnl, header);
3915 size += data->header_len;
3916 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
3920 /* The commit_odp_actions() function and its helpers. */
3923 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3924 const void *key, size_t key_size)
3926 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3927 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3928 nl_msg_end_nested(odp_actions, offset);
3931 /* Masked set actions have a mask following the data within the netlink
3932 * attribute. The unmasked bits in the data will be cleared as the data
3933 * is copied to the action. */
3935 commit_masked_set_action(struct ofpbuf *odp_actions,
3936 enum ovs_key_attr key_type,
3937 const void *key_, const void *mask_, size_t key_size)
3939 size_t offset = nl_msg_start_nested(odp_actions,
3940 OVS_ACTION_ATTR_SET_MASKED);
3941 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3942 const char *key = key_, *mask = mask_;
3944 memcpy(data + key_size, mask, key_size);
3945 /* Clear unmasked bits while copying. */
3946 while (key_size--) {
3947 *data++ = *key++ & *mask++;
3949 nl_msg_end_nested(odp_actions, offset);
3952 /* If any of the flow key data that ODP actions can modify are different in
3953 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3954 * 'odp_actions' that change the flow tunneling information in key from
3955 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3956 * same way. In other words, operates the same as commit_odp_actions(), but
3957 * only on tunneling information. */
3959 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3960 struct ofpbuf *odp_actions)
3962 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3963 if (flow->tunnel.ip_dst) {
3964 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3967 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3968 odp_put_tunnel_action(&base->tunnel, odp_actions);
3973 commit(enum ovs_key_attr attr, bool use_masked_set,
3974 const void *key, void *base, void *mask, size_t size,
3975 struct ofpbuf *odp_actions)
3977 if (memcmp(key, base, size)) {
3978 bool fully_masked = odp_mask_is_exact(attr, mask, size);
3980 if (use_masked_set && !fully_masked) {
3981 commit_masked_set_action(odp_actions, attr, key, mask, size);
3983 if (!fully_masked) {
3984 memset(mask, 0xff, size);
3986 commit_set_action(odp_actions, attr, key, size);
3988 memcpy(base, key, size);
3991 /* Mask bits are set when we have either read or set the corresponding
3992 * values. Masked bits will be exact-matched, no need to set them
3993 * if the value did not actually change. */
3999 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4001 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4002 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4006 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4008 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4009 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4013 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4014 struct ofpbuf *odp_actions,
4015 struct flow_wildcards *wc,
4018 struct ovs_key_ethernet key, base, mask;
4020 get_ethernet_key(flow, &key);
4021 get_ethernet_key(base_flow, &base);
4022 get_ethernet_key(&wc->masks, &mask);
4024 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4025 &key, &base, &mask, sizeof key, odp_actions)) {
4026 put_ethernet_key(&base, base_flow);
4027 put_ethernet_key(&mask, &wc->masks);
4032 pop_vlan(struct flow *base,
4033 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4035 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4037 if (base->vlan_tci & htons(VLAN_CFI)) {
4038 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4044 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4045 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4047 if (base->vlan_tci == vlan_tci) {
4051 pop_vlan(base, odp_actions, wc);
4052 if (vlan_tci & htons(VLAN_CFI)) {
4053 struct ovs_action_push_vlan vlan;
4055 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4056 vlan.vlan_tci = vlan_tci;
4057 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4058 &vlan, sizeof vlan);
4060 base->vlan_tci = vlan_tci;
4063 /* Wildcarding already done at action translation time. */
4065 commit_mpls_action(const struct flow *flow, struct flow *base,
4066 struct ofpbuf *odp_actions)
4068 int base_n = flow_count_mpls_labels(base, NULL);
4069 int flow_n = flow_count_mpls_labels(flow, NULL);
4070 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4073 while (base_n > common_n) {
4074 if (base_n - 1 == common_n && flow_n > common_n) {
4075 /* If there is only one more LSE in base than there are common
4076 * between base and flow; and flow has at least one more LSE than
4077 * is common then the topmost LSE of base may be updated using
4079 struct ovs_key_mpls mpls_key;
4081 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4082 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4083 &mpls_key, sizeof mpls_key);
4084 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4087 /* Otherwise, if there more LSEs in base than are common between
4088 * base and flow then pop the topmost one. */
4092 /* If all the LSEs are to be popped and this is not the outermost
4093 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4094 * POP_MPLS action instead of flow->dl_type.
4096 * This is because the POP_MPLS action requires its ethertype
4097 * argument to be an MPLS ethernet type but in this case
4098 * flow->dl_type will be a non-MPLS ethernet type.
4100 * When the final POP_MPLS action occurs it use flow->dl_type and
4101 * the and the resulting packet will have the desired dl_type. */
4102 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4103 dl_type = htons(ETH_TYPE_MPLS);
4105 dl_type = flow->dl_type;
4107 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4108 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4114 /* If, after the above popping and setting, there are more LSEs in flow
4115 * than base then some LSEs need to be pushed. */
4116 while (base_n < flow_n) {
4117 struct ovs_action_push_mpls *mpls;
4119 mpls = nl_msg_put_unspec_zero(odp_actions,
4120 OVS_ACTION_ATTR_PUSH_MPLS,
4122 mpls->mpls_ethertype = flow->dl_type;
4123 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4124 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4125 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4131 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4133 ipv4->ipv4_src = flow->nw_src;
4134 ipv4->ipv4_dst = flow->nw_dst;
4135 ipv4->ipv4_proto = flow->nw_proto;
4136 ipv4->ipv4_tos = flow->nw_tos;
4137 ipv4->ipv4_ttl = flow->nw_ttl;
4138 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4142 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4144 flow->nw_src = ipv4->ipv4_src;
4145 flow->nw_dst = ipv4->ipv4_dst;
4146 flow->nw_proto = ipv4->ipv4_proto;
4147 flow->nw_tos = ipv4->ipv4_tos;
4148 flow->nw_ttl = ipv4->ipv4_ttl;
4149 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4153 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4154 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4157 struct ovs_key_ipv4 key, mask, base;
4159 /* Check that nw_proto and nw_frag remain unchanged. */
4160 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4161 flow->nw_frag == base_flow->nw_frag);
4163 get_ipv4_key(flow, &key, false);
4164 get_ipv4_key(base_flow, &base, false);
4165 get_ipv4_key(&wc->masks, &mask, true);
4166 mask.ipv4_proto = 0; /* Not writeable. */
4167 mask.ipv4_frag = 0; /* Not writable. */
4169 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4171 put_ipv4_key(&base, base_flow, false);
4172 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4173 put_ipv4_key(&mask, &wc->masks, true);
4179 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4181 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4182 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4183 ipv6->ipv6_label = flow->ipv6_label;
4184 ipv6->ipv6_proto = flow->nw_proto;
4185 ipv6->ipv6_tclass = flow->nw_tos;
4186 ipv6->ipv6_hlimit = flow->nw_ttl;
4187 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4191 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4193 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4194 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4195 flow->ipv6_label = ipv6->ipv6_label;
4196 flow->nw_proto = ipv6->ipv6_proto;
4197 flow->nw_tos = ipv6->ipv6_tclass;
4198 flow->nw_ttl = ipv6->ipv6_hlimit;
4199 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4203 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4204 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4207 struct ovs_key_ipv6 key, mask, base;
4209 /* Check that nw_proto and nw_frag remain unchanged. */
4210 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4211 flow->nw_frag == base_flow->nw_frag);
4213 get_ipv6_key(flow, &key, false);
4214 get_ipv6_key(base_flow, &base, false);
4215 get_ipv6_key(&wc->masks, &mask, true);
4216 mask.ipv6_proto = 0; /* Not writeable. */
4217 mask.ipv6_frag = 0; /* Not writable. */
4219 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4221 put_ipv6_key(&base, base_flow, false);
4222 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4223 put_ipv6_key(&mask, &wc->masks, true);
4229 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4231 /* ARP key has padding, clear it. */
4232 memset(arp, 0, sizeof *arp);
4234 arp->arp_sip = flow->nw_src;
4235 arp->arp_tip = flow->nw_dst;
4236 arp->arp_op = htons(flow->nw_proto);
4237 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4238 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4242 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4244 flow->nw_src = arp->arp_sip;
4245 flow->nw_dst = arp->arp_tip;
4246 flow->nw_proto = ntohs(arp->arp_op);
4247 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4248 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4251 static enum slow_path_reason
4252 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4253 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4255 struct ovs_key_arp key, mask, base;
4257 get_arp_key(flow, &key);
4258 get_arp_key(base_flow, &base);
4259 get_arp_key(&wc->masks, &mask);
4261 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4263 put_arp_key(&base, base_flow);
4264 put_arp_key(&mask, &wc->masks);
4271 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4273 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4274 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4275 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4276 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4280 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4282 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4283 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4284 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4285 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4288 static enum slow_path_reason
4289 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4290 struct ofpbuf *odp_actions,
4291 struct flow_wildcards *wc, bool use_masked)
4293 struct ovs_key_nd key, mask, base;
4295 get_nd_key(flow, &key);
4296 get_nd_key(base_flow, &base);
4297 get_nd_key(&wc->masks, &mask);
4299 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4301 put_nd_key(&base, base_flow);
4302 put_nd_key(&mask, &wc->masks);
4309 static enum slow_path_reason
4310 commit_set_nw_action(const struct flow *flow, struct flow *base,
4311 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4314 /* Check if 'flow' really has an L3 header. */
4315 if (!flow->nw_proto) {
4319 switch (ntohs(base->dl_type)) {
4321 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4325 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4326 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4329 return commit_set_arp_action(flow, base, odp_actions, wc);
4335 /* TCP, UDP, and SCTP keys have the same layout. */
4336 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4337 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4340 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4342 tp->tcp.tcp_src = flow->tp_src;
4343 tp->tcp.tcp_dst = flow->tp_dst;
4347 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4349 flow->tp_src = tp->tcp.tcp_src;
4350 flow->tp_dst = tp->tcp.tcp_dst;
4354 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4355 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4358 enum ovs_key_attr key_type;
4359 union ovs_key_tp key, mask, base;
4361 /* Check if 'flow' really has an L3 header. */
4362 if (!flow->nw_proto) {
4366 if (!is_ip_any(base_flow)) {
4370 if (flow->nw_proto == IPPROTO_TCP) {
4371 key_type = OVS_KEY_ATTR_TCP;
4372 } else if (flow->nw_proto == IPPROTO_UDP) {
4373 key_type = OVS_KEY_ATTR_UDP;
4374 } else if (flow->nw_proto == IPPROTO_SCTP) {
4375 key_type = OVS_KEY_ATTR_SCTP;
4380 get_tp_key(flow, &key);
4381 get_tp_key(base_flow, &base);
4382 get_tp_key(&wc->masks, &mask);
4384 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4386 put_tp_key(&base, base_flow);
4387 put_tp_key(&mask, &wc->masks);
4392 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4393 struct ofpbuf *odp_actions,
4394 struct flow_wildcards *wc,
4397 uint32_t key, mask, base;
4399 key = flow->skb_priority;
4400 base = base_flow->skb_priority;
4401 mask = wc->masks.skb_priority;
4403 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4404 sizeof key, odp_actions)) {
4405 base_flow->skb_priority = base;
4406 wc->masks.skb_priority = mask;
4411 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4412 struct ofpbuf *odp_actions,
4413 struct flow_wildcards *wc,
4416 uint32_t key, mask, base;
4418 key = flow->pkt_mark;
4419 base = base_flow->pkt_mark;
4420 mask = wc->masks.pkt_mark;
4422 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4423 sizeof key, odp_actions)) {
4424 base_flow->pkt_mark = base;
4425 wc->masks.pkt_mark = mask;
4429 /* If any of the flow key data that ODP actions can modify are different in
4430 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4431 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4432 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4433 * in addition to this function if needed. Sets fields in 'wc' that are
4434 * used as part of the action.
4436 * Returns a reason to force processing the flow's packets into the userspace
4437 * slow path, if there is one, otherwise 0. */
4438 enum slow_path_reason
4439 commit_odp_actions(const struct flow *flow, struct flow *base,
4440 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4443 enum slow_path_reason slow;
4445 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4446 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4447 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4448 commit_mpls_action(flow, base, odp_actions);
4449 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4450 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4451 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);