2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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>
27 #include "byte-order.h"
29 #include "dynamic-string.h"
39 VLOG_DEFINE_THIS_MODULE(odp_util);
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42 * Since this is fairly non-specific for the OVS userspace components,
43 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44 * interactions with the datapath.
47 /* The set of characters that may separate one action or one key attribute
49 static const char *delimiters = ", \t\r\n";
51 static int parse_odp_key_attr(const char *, const struct simap *port_names,
53 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
55 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
58 * - For an action whose argument has a fixed length, returned that
59 * nonnegative length in bytes.
61 * - For an action with a variable-length argument, returns -2.
63 * - For an invalid 'type', returns -1. */
65 odp_action_len(uint16_t type)
67 if (type > OVS_ACTION_ATTR_MAX) {
71 switch ((enum ovs_action_attr) type) {
72 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
73 case OVS_ACTION_ATTR_USERSPACE: return -2;
74 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75 case OVS_ACTION_ATTR_POP_VLAN: return 0;
76 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
77 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
78 case OVS_ACTION_ATTR_SET: return -2;
79 case OVS_ACTION_ATTR_SAMPLE: return -2;
81 case OVS_ACTION_ATTR_UNSPEC:
82 case __OVS_ACTION_ATTR_MAX:
90 ovs_key_attr_to_string(enum ovs_key_attr attr)
92 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
95 case OVS_KEY_ATTR_UNSPEC: return "unspec";
96 case OVS_KEY_ATTR_ENCAP: return "encap";
97 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
98 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
99 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
100 case OVS_KEY_ATTR_IN_PORT: return "in_port";
101 case OVS_KEY_ATTR_ETHERNET: return "eth";
102 case OVS_KEY_ATTR_VLAN: return "vlan";
103 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
104 case OVS_KEY_ATTR_IPV4: return "ipv4";
105 case OVS_KEY_ATTR_IPV6: return "ipv6";
106 case OVS_KEY_ATTR_TCP: return "tcp";
107 case OVS_KEY_ATTR_UDP: return "udp";
108 case OVS_KEY_ATTR_ICMP: return "icmp";
109 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
110 case OVS_KEY_ATTR_ARP: return "arp";
111 case OVS_KEY_ATTR_ND: return "nd";
112 case OVS_KEY_ATTR_MPLS: return "mpls";
114 case __OVS_KEY_ATTR_MAX:
116 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
117 (unsigned int) attr);
123 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
125 size_t len = nl_attr_get_size(a);
127 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
129 const uint8_t *unspec;
132 unspec = nl_attr_get(a);
133 for (i = 0; i < len; i++) {
134 ds_put_char(ds, i ? ' ': '(');
135 ds_put_format(ds, "%02x", unspec[i]);
137 ds_put_char(ds, ')');
142 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
144 static const struct nl_policy ovs_sample_policy[] = {
145 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
146 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
148 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
150 const struct nlattr *nla_acts;
153 ds_put_cstr(ds, "sample");
155 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
156 ds_put_cstr(ds, "(error)");
160 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
163 ds_put_format(ds, "(sample=%.1f%%,", percentage);
165 ds_put_cstr(ds, "actions(");
166 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
167 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
168 format_odp_actions(ds, nla_acts, len);
169 ds_put_format(ds, "))");
173 slow_path_reason_to_string(uint32_t data)
175 enum slow_path_reason bit = (enum slow_path_reason) data;
188 case SLOW_CONTROLLER:
198 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
209 while (s[n] != ')') {
210 unsigned long long int flags;
214 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
215 n += n0 + (s[n + n0] == ',');
220 for (bit = 1; bit; bit <<= 1) {
221 const char *name = bit_to_string(bit);
229 if (!strncmp(s + n, name, len) &&
230 (s[n + len] == ',' || s[n + len] == ')')) {
232 n += len + (s[n + len] == ',');
248 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
250 static const struct nl_policy ovs_userspace_policy[] = {
251 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
252 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
255 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
256 const struct nlattr *userdata_attr;
258 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
259 ds_put_cstr(ds, "userspace(error)");
263 ds_put_format(ds, "userspace(pid=%"PRIu32,
264 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
266 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
269 const uint8_t *userdata = nl_attr_get(userdata_attr);
270 size_t userdata_len = nl_attr_get_size(userdata_attr);
271 bool userdata_unspec = true;
272 union user_action_cookie cookie;
274 if (userdata_len >= sizeof cookie.type
275 && userdata_len <= sizeof cookie) {
277 memset(&cookie, 0, sizeof cookie);
278 memcpy(&cookie, userdata, userdata_len);
280 userdata_unspec = false;
282 if (userdata_len == sizeof cookie.sflow
283 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
284 ds_put_format(ds, ",sFlow("
285 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
286 vlan_tci_to_vid(cookie.sflow.vlan_tci),
287 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
288 cookie.sflow.output);
289 } else if (userdata_len == sizeof cookie.slow_path
290 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
291 ds_put_cstr(ds, ",slow_path(");
292 format_flags(ds, slow_path_reason_to_string,
293 cookie.slow_path.reason, ',');
294 ds_put_format(ds, ")");
295 } else if (userdata_len == sizeof cookie.flow_sample
296 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
297 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
298 ",collector_set_id=%"PRIu32
299 ",obs_domain_id=%"PRIu32
300 ",obs_point_id=%"PRIu32")",
301 cookie.flow_sample.probability,
302 cookie.flow_sample.collector_set_id,
303 cookie.flow_sample.obs_domain_id,
304 cookie.flow_sample.obs_point_id);
305 } else if (userdata_len == sizeof cookie.ipfix
306 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
307 ds_put_format(ds, ",ipfix");
309 userdata_unspec = true;
313 if (userdata_unspec) {
315 ds_put_format(ds, ",userdata(");
316 for (i = 0; i < userdata_len; i++) {
317 ds_put_format(ds, "%02x", userdata[i]);
319 ds_put_char(ds, ')');
323 ds_put_char(ds, ')');
327 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
329 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
330 vlan_tci_to_vid(vlan_tci),
331 vlan_tci_to_pcp(vlan_tci));
332 if (!(vlan_tci & htons(VLAN_CFI))) {
333 ds_put_cstr(ds, ",cfi=0");
338 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
340 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
341 mpls_lse_to_label(mpls_lse),
342 mpls_lse_to_tc(mpls_lse),
343 mpls_lse_to_ttl(mpls_lse),
344 mpls_lse_to_bos(mpls_lse));
348 format_odp_action(struct ds *ds, const struct nlattr *a)
351 enum ovs_action_attr type = nl_attr_type(a);
352 const struct ovs_action_push_vlan *vlan;
354 expected_len = odp_action_len(nl_attr_type(a));
355 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
356 ds_put_format(ds, "bad length %zu, expected %d for: ",
357 nl_attr_get_size(a), expected_len);
358 format_generic_odp_action(ds, a);
363 case OVS_ACTION_ATTR_OUTPUT:
364 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
366 case OVS_ACTION_ATTR_USERSPACE:
367 format_odp_userspace_action(ds, a);
369 case OVS_ACTION_ATTR_SET:
370 ds_put_cstr(ds, "set(");
371 format_odp_key_attr(nl_attr_get(a), ds);
372 ds_put_cstr(ds, ")");
374 case OVS_ACTION_ATTR_PUSH_VLAN:
375 vlan = nl_attr_get(a);
376 ds_put_cstr(ds, "push_vlan(");
377 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
378 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
380 format_vlan_tci(ds, vlan->vlan_tci);
381 ds_put_char(ds, ')');
383 case OVS_ACTION_ATTR_POP_VLAN:
384 ds_put_cstr(ds, "pop_vlan");
386 case OVS_ACTION_ATTR_PUSH_MPLS: {
387 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
388 ds_put_cstr(ds, "push_mpls(");
389 format_mpls_lse(ds, mpls->mpls_lse);
390 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
393 case OVS_ACTION_ATTR_POP_MPLS: {
394 ovs_be16 ethertype = nl_attr_get_be16(a);
395 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
398 case OVS_ACTION_ATTR_SAMPLE:
399 format_odp_sample_action(ds, a);
401 case OVS_ACTION_ATTR_UNSPEC:
402 case __OVS_ACTION_ATTR_MAX:
404 format_generic_odp_action(ds, a);
410 format_odp_actions(struct ds *ds, const struct nlattr *actions,
414 const struct nlattr *a;
417 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
419 ds_put_char(ds, ',');
421 format_odp_action(ds, a);
426 if (left == actions_len) {
427 ds_put_cstr(ds, "<empty>");
429 ds_put_format(ds, ",***%u leftover bytes*** (", left);
430 for (i = 0; i < left; i++) {
431 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
433 ds_put_char(ds, ')');
436 ds_put_cstr(ds, "drop");
441 parse_odp_action(const char *s, const struct simap *port_names,
442 struct ofpbuf *actions)
444 /* Many of the sscanf calls in this function use oversized destination
445 * fields because some sscanf() implementations truncate the range of %i
446 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
447 * value of 0x7fff. The other alternatives are to allow only a single
448 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
451 * The tun_id parser has to use an alternative approach because there is no
452 * type larger than 64 bits. */
455 unsigned long long int port;
458 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
459 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
465 int len = strcspn(s, delimiters);
466 struct simap_node *node;
468 node = simap_find_len(port_names, s, len);
470 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
476 unsigned long long int pid;
477 unsigned long long int output;
478 unsigned long long int probability;
479 unsigned long long int collector_set_id;
480 unsigned long long int obs_domain_id;
481 unsigned long long int obs_point_id;
485 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
486 odp_put_userspace_action(pid, NULL, 0, actions);
488 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
489 "pcp=%i,output=%lli))%n",
490 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
491 union user_action_cookie cookie;
494 tci = vid | (pcp << VLAN_PCP_SHIFT);
499 cookie.type = USER_ACTION_COOKIE_SFLOW;
500 cookie.sflow.vlan_tci = htons(tci);
501 cookie.sflow.output = output;
502 odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
505 } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
507 union user_action_cookie cookie;
510 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
511 cookie.slow_path.unused = 0;
512 cookie.slow_path.reason = 0;
514 res = parse_flags(&s[n], slow_path_reason_to_string,
515 &cookie.slow_path.reason);
525 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
528 } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
529 "collector_set_id=%lli,obs_domain_id=%lli,"
530 "obs_point_id=%lli))%n",
531 &pid, &probability, &collector_set_id,
532 &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
533 union user_action_cookie cookie;
535 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
536 cookie.flow_sample.probability = probability;
537 cookie.flow_sample.collector_set_id = collector_set_id;
538 cookie.flow_sample.obs_domain_id = obs_domain_id;
539 cookie.flow_sample.obs_point_id = obs_point_id;
540 odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
543 } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
545 union user_action_cookie cookie;
547 cookie.type = USER_ACTION_COOKIE_IPFIX;
548 odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
551 } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
556 ofpbuf_init(&buf, 16);
557 end = ofpbuf_put_hex(&buf, &s[n], NULL);
558 if (end[0] == ')' && end[1] == ')') {
559 odp_put_userspace_action(pid, buf.data, buf.size, actions);
561 return (end + 2) - s;
566 if (!strncmp(s, "set(", 4)) {
570 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
571 retval = parse_odp_key_attr(s + 4, port_names, actions);
575 if (s[retval + 4] != ')') {
578 nl_msg_end_nested(actions, start_ofs);
583 struct ovs_action_push_vlan push;
584 int tpid = ETH_TYPE_VLAN;
589 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
591 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
592 &vid, &pcp, &cfi, &n) > 0 && n > 0)
593 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
594 &tpid, &vid, &pcp, &n) > 0 && n > 0)
595 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
596 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
597 push.vlan_tpid = htons(tpid);
598 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
599 | (pcp << VLAN_PCP_SHIFT)
600 | (cfi ? VLAN_CFI : 0));
601 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
608 if (!strncmp(s, "pop_vlan", 8)) {
609 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
617 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
618 && percentage >= 0. && percentage <= 100.0
620 size_t sample_ofs, actions_ofs;
623 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
624 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
625 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
626 (probability <= 0 ? 0
627 : probability >= UINT32_MAX ? UINT32_MAX
630 actions_ofs = nl_msg_start_nested(actions,
631 OVS_SAMPLE_ATTR_ACTIONS);
635 n += strspn(s + n, delimiters);
640 retval = parse_odp_action(s + n, port_names, actions);
646 nl_msg_end_nested(actions, actions_ofs);
647 nl_msg_end_nested(actions, sample_ofs);
649 return s[n + 1] == ')' ? n + 2 : -EINVAL;
656 /* Parses the string representation of datapath actions, in the format output
657 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
658 * value. On success, the ODP actions are appended to 'actions' as a series of
659 * Netlink attributes. On failure, no data is appended to 'actions'. Either
660 * way, 'actions''s data might be reallocated. */
662 odp_actions_from_string(const char *s, const struct simap *port_names,
663 struct ofpbuf *actions)
667 if (!strcasecmp(s, "drop")) {
671 old_size = actions->size;
675 s += strspn(s, delimiters);
680 retval = parse_odp_action(s, port_names, actions);
681 if (retval < 0 || !strchr(delimiters, s[retval])) {
682 actions->size = old_size;
691 /* Returns the correct length of the payload for a flow key attribute of the
692 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
693 * is variable length. */
695 odp_flow_key_attr_len(uint16_t type)
697 if (type > OVS_KEY_ATTR_MAX) {
701 switch ((enum ovs_key_attr) type) {
702 case OVS_KEY_ATTR_ENCAP: return -2;
703 case OVS_KEY_ATTR_PRIORITY: return 4;
704 case OVS_KEY_ATTR_SKB_MARK: return 4;
705 case OVS_KEY_ATTR_TUNNEL: return -2;
706 case OVS_KEY_ATTR_IN_PORT: return 4;
707 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
708 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
709 case OVS_KEY_ATTR_ETHERTYPE: return 2;
710 case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
711 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
712 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
713 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
714 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
715 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
716 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
717 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
718 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
720 case OVS_KEY_ATTR_UNSPEC:
721 case __OVS_KEY_ATTR_MAX:
729 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
731 size_t len = nl_attr_get_size(a);
733 const uint8_t *unspec;
736 unspec = nl_attr_get(a);
737 for (i = 0; i < len; i++) {
738 ds_put_char(ds, i ? ' ': '(');
739 ds_put_format(ds, "%02x", unspec[i]);
741 ds_put_char(ds, ')');
746 ovs_frag_type_to_string(enum ovs_frag_type type)
749 case OVS_FRAG_TYPE_NONE:
751 case OVS_FRAG_TYPE_FIRST:
753 case OVS_FRAG_TYPE_LATER:
755 case __OVS_FRAG_TYPE_MAX:
762 tunnel_key_attr_len(int type)
765 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
766 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
767 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
768 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
769 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
770 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
771 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
772 case __OVS_TUNNEL_KEY_ATTR_MAX:
778 static enum odp_key_fitness
779 tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
782 const struct nlattr *a;
784 bool unknown = false;
786 NL_NESTED_FOR_EACH(a, left, attr) {
787 uint16_t type = nl_attr_type(a);
788 size_t len = nl_attr_get_size(a);
789 int expected_len = tunnel_key_attr_len(type);
791 if (len != expected_len && expected_len >= 0) {
792 return ODP_FIT_ERROR;
796 case OVS_TUNNEL_KEY_ATTR_ID:
797 tun->tun_id = nl_attr_get_be64(a);
798 tun->flags |= FLOW_TNL_F_KEY;
800 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
801 tun->ip_src = nl_attr_get_be32(a);
803 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
804 tun->ip_dst = nl_attr_get_be32(a);
806 case OVS_TUNNEL_KEY_ATTR_TOS:
807 tun->ip_tos = nl_attr_get_u8(a);
809 case OVS_TUNNEL_KEY_ATTR_TTL:
810 tun->ip_ttl = nl_attr_get_u8(a);
813 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
814 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
816 case OVS_TUNNEL_KEY_ATTR_CSUM:
817 tun->flags |= FLOW_TNL_F_CSUM;
820 /* Allow this to show up as unexpected, if there are unknown
821 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
828 return ODP_FIT_ERROR;
831 return ODP_FIT_TOO_MUCH;
833 return ODP_FIT_PERFECT;
837 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
841 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
843 if (tun_key->flags & FLOW_TNL_F_KEY) {
844 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
846 if (tun_key->ip_src) {
847 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
849 if (tun_key->ip_dst) {
850 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
852 if (tun_key->ip_tos) {
853 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
855 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
856 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
857 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
859 if (tun_key->flags & FLOW_TNL_F_CSUM) {
860 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
863 nl_msg_end_nested(a, tun_key_ofs);
867 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
869 const struct ovs_key_ethernet *eth_key;
870 const struct ovs_key_ipv4 *ipv4_key;
871 const struct ovs_key_ipv6 *ipv6_key;
872 const struct ovs_key_tcp *tcp_key;
873 const struct ovs_key_udp *udp_key;
874 const struct ovs_key_icmp *icmp_key;
875 const struct ovs_key_icmpv6 *icmpv6_key;
876 const struct ovs_key_arp *arp_key;
877 const struct ovs_key_nd *nd_key;
878 struct flow_tnl tun_key;
879 enum ovs_key_attr attr = nl_attr_type(a);
882 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
883 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
884 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
885 ds_put_format(ds, "(bad length %zu, expected %d)",
887 odp_flow_key_attr_len(nl_attr_type(a)));
888 format_generic_odp_key(a, ds);
893 case OVS_KEY_ATTR_ENCAP:
894 ds_put_cstr(ds, "(");
895 if (nl_attr_get_size(a)) {
896 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
898 ds_put_char(ds, ')');
901 case OVS_KEY_ATTR_PRIORITY:
902 ds_put_format(ds, "(%#"PRIx32")", nl_attr_get_u32(a));
905 case OVS_KEY_ATTR_SKB_MARK:
906 ds_put_format(ds, "(%#"PRIx32")", nl_attr_get_u32(a));
909 case OVS_KEY_ATTR_TUNNEL:
910 memset(&tun_key, 0, sizeof tun_key);
911 if (tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
912 ds_put_format(ds, "(error)");
914 ds_put_format(ds, "(tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
915 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
916 ntohll(tun_key.tun_id),
917 IP_ARGS(tun_key.ip_src),
918 IP_ARGS(tun_key.ip_dst),
919 tun_key.ip_tos, tun_key.ip_ttl);
921 format_flags(ds, flow_tun_flag_to_string,
922 (uint32_t) tun_key.flags, ',');
923 ds_put_format(ds, "))");
927 case OVS_KEY_ATTR_IN_PORT:
928 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
931 case OVS_KEY_ATTR_ETHERNET:
932 eth_key = nl_attr_get(a);
933 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
934 ETH_ADDR_ARGS(eth_key->eth_src),
935 ETH_ADDR_ARGS(eth_key->eth_dst));
938 case OVS_KEY_ATTR_VLAN:
939 ds_put_char(ds, '(');
940 format_vlan_tci(ds, nl_attr_get_be16(a));
941 ds_put_char(ds, ')');
944 case OVS_KEY_ATTR_MPLS: {
945 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
946 ds_put_char(ds, '(');
947 format_mpls_lse(ds, mpls_key->mpls_lse);
948 ds_put_char(ds, ')');
952 case OVS_KEY_ATTR_ETHERTYPE:
953 ds_put_format(ds, "(0x%04"PRIx16")",
954 ntohs(nl_attr_get_be16(a)));
957 case OVS_KEY_ATTR_IPV4:
958 ipv4_key = nl_attr_get(a);
959 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
960 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
961 IP_ARGS(ipv4_key->ipv4_src),
962 IP_ARGS(ipv4_key->ipv4_dst),
963 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
965 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
968 case OVS_KEY_ATTR_IPV6: {
969 char src_str[INET6_ADDRSTRLEN];
970 char dst_str[INET6_ADDRSTRLEN];
972 ipv6_key = nl_attr_get(a);
973 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
974 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
976 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
977 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
978 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
979 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
980 ipv6_key->ipv6_hlimit,
981 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
985 case OVS_KEY_ATTR_TCP:
986 tcp_key = nl_attr_get(a);
987 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
988 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
991 case OVS_KEY_ATTR_UDP:
992 udp_key = nl_attr_get(a);
993 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
994 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
997 case OVS_KEY_ATTR_ICMP:
998 icmp_key = nl_attr_get(a);
999 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
1000 icmp_key->icmp_type, icmp_key->icmp_code);
1003 case OVS_KEY_ATTR_ICMPV6:
1004 icmpv6_key = nl_attr_get(a);
1005 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
1006 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1009 case OVS_KEY_ATTR_ARP:
1010 arp_key = nl_attr_get(a);
1011 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1012 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
1013 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1014 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
1015 ETH_ADDR_ARGS(arp_key->arp_tha));
1018 case OVS_KEY_ATTR_ND: {
1019 char target[INET6_ADDRSTRLEN];
1021 nd_key = nl_attr_get(a);
1022 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1024 ds_put_format(ds, "(target=%s", target);
1025 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1026 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1027 ETH_ADDR_ARGS(nd_key->nd_sll));
1029 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1030 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1031 ETH_ADDR_ARGS(nd_key->nd_tll));
1033 ds_put_char(ds, ')');
1037 case OVS_KEY_ATTR_UNSPEC:
1038 case __OVS_KEY_ATTR_MAX:
1040 format_generic_odp_key(a, ds);
1045 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1046 * OVS_KEY_ATTR_* attributes in 'key'. */
1048 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
1051 const struct nlattr *a;
1054 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1056 ds_put_char(ds, ',');
1058 format_odp_key_attr(a, ds);
1063 if (left == key_len) {
1064 ds_put_cstr(ds, "<empty>");
1066 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1067 for (i = 0; i < left; i++) {
1068 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1070 ds_put_char(ds, ')');
1073 ds_put_cstr(ds, "<empty>");
1078 put_nd_key(int n, const char *nd_target_s,
1079 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
1081 struct ovs_key_nd nd_key;
1083 memset(&nd_key, 0, sizeof nd_key);
1084 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1088 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
1091 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
1093 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
1098 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1100 if (!strcasecmp(s, "no")) {
1101 *type = OVS_FRAG_TYPE_NONE;
1102 } else if (!strcasecmp(s, "first")) {
1103 *type = OVS_FRAG_TYPE_FIRST;
1104 } else if (!strcasecmp(s, "later")) {
1105 *type = OVS_FRAG_TYPE_LATER;
1113 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1115 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1116 (mpls_tc << MPLS_TC_SHIFT) |
1117 (mpls_ttl << MPLS_TTL_SHIFT) |
1118 (mpls_bos << MPLS_BOS_SHIFT)));
1122 parse_odp_key_attr(const char *s, const struct simap *port_names,
1125 /* Many of the sscanf calls in this function use oversized destination
1126 * fields because some sscanf() implementations truncate the range of %i
1127 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1128 * value of 0x7fff. The other alternatives are to allow only a single
1129 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1132 * The tun_id parser has to use an alternative approach because there is no
1133 * type larger than 64 bits. */
1136 unsigned long long int priority;
1139 if (sscanf(s, "skb_priority(%llx)%n", &priority, &n) > 0 && n > 0) {
1140 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1146 unsigned long long int mark;
1149 if (sscanf(s, "skb_mark(%llx)%n", &mark, &n) > 0 && n > 0) {
1150 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1158 struct flow_tnl tun_key;
1161 if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1162 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1163 ",tos=%i,ttl=%i,flags%n", tun_id_s,
1164 IP_SCAN_ARGS(&tun_key.ip_src),
1165 IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1170 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1171 tun_key.ip_tos = tos;
1172 tun_key.ip_ttl = ttl;
1173 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1174 tun_key.flags = (uint16_t) flags;
1184 tun_key_to_attr(key, &tun_key);
1190 unsigned long long int in_port;
1193 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1194 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1199 if (port_names && !strncmp(s, "in_port(", 8)) {
1201 const struct simap_node *node;
1205 name_len = strcspn(s, ")");
1206 node = simap_find_len(port_names, name, name_len);
1208 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1209 return 8 + name_len + 1;
1214 struct ovs_key_ethernet eth_key;
1218 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1219 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1220 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1221 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1222 ð_key, sizeof eth_key);
1233 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1235 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1236 htons((vid << VLAN_VID_SHIFT) |
1237 (pcp << VLAN_PCP_SHIFT) |
1240 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1241 &vid, &pcp, &cfi, &n) > 0
1243 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1244 htons((vid << VLAN_VID_SHIFT) |
1245 (pcp << VLAN_PCP_SHIFT) |
1246 (cfi ? VLAN_CFI : 0)));
1255 if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
1256 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1262 int label, tc, ttl, bos;
1265 if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1266 &label, &tc, &ttl, &bos, &n) > 0 &&
1268 struct ovs_key_mpls *mpls;
1270 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1272 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1284 enum ovs_frag_type ipv4_frag;
1287 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1288 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1289 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1290 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1292 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1293 struct ovs_key_ipv4 ipv4_key;
1295 ipv4_key.ipv4_src = ipv4_src;
1296 ipv4_key.ipv4_dst = ipv4_dst;
1297 ipv4_key.ipv4_proto = ipv4_proto;
1298 ipv4_key.ipv4_tos = ipv4_tos;
1299 ipv4_key.ipv4_ttl = ipv4_ttl;
1300 ipv4_key.ipv4_frag = ipv4_frag;
1301 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1302 &ipv4_key, sizeof ipv4_key);
1308 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1309 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1315 enum ovs_frag_type ipv6_frag;
1318 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1319 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1320 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1321 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1323 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1324 struct ovs_key_ipv6 ipv6_key;
1326 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1327 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1330 ipv6_key.ipv6_label = htonl(ipv6_label);
1331 ipv6_key.ipv6_proto = ipv6_proto;
1332 ipv6_key.ipv6_tclass = ipv6_tclass;
1333 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1334 ipv6_key.ipv6_frag = ipv6_frag;
1335 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1336 &ipv6_key, sizeof ipv6_key);
1346 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1348 struct ovs_key_tcp tcp_key;
1350 tcp_key.tcp_src = htons(tcp_src);
1351 tcp_key.tcp_dst = htons(tcp_dst);
1352 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1362 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1364 struct ovs_key_udp udp_key;
1366 udp_key.udp_src = htons(udp_src);
1367 udp_key.udp_dst = htons(udp_dst);
1368 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1378 if (sscanf(s, "icmp(type=%i,code=%i)%n",
1379 &icmp_type, &icmp_code, &n) > 0
1381 struct ovs_key_icmp icmp_key;
1383 icmp_key.icmp_type = icmp_type;
1384 icmp_key.icmp_code = icmp_code;
1385 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1386 &icmp_key, sizeof icmp_key);
1392 struct ovs_key_icmpv6 icmpv6_key;
1395 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1396 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1398 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1399 &icmpv6_key, sizeof icmpv6_key);
1408 uint8_t arp_sha[ETH_ADDR_LEN];
1409 uint8_t arp_tha[ETH_ADDR_LEN];
1412 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1413 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1414 IP_SCAN_ARGS(&arp_sip),
1415 IP_SCAN_ARGS(&arp_tip),
1417 ETH_ADDR_SCAN_ARGS(arp_sha),
1418 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1419 struct ovs_key_arp arp_key;
1421 memset(&arp_key, 0, sizeof arp_key);
1422 arp_key.arp_sip = arp_sip;
1423 arp_key.arp_tip = arp_tip;
1424 arp_key.arp_op = htons(arp_op);
1425 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1426 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1427 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1433 char nd_target_s[IPV6_SCAN_LEN + 1];
1434 uint8_t nd_sll[ETH_ADDR_LEN];
1435 uint8_t nd_tll[ETH_ADDR_LEN];
1438 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1439 nd_target_s, &n) > 0 && n > 0) {
1440 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1442 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1443 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1445 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1447 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1448 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1450 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1452 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1453 "tll="ETH_ADDR_SCAN_FMT")%n",
1454 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1455 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1457 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1461 if (!strncmp(s, "encap(", 6)) {
1462 const char *start = s;
1465 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1471 s += strspn(s, ", \t\r\n");
1474 } else if (*s == ')') {
1478 retval = parse_odp_key_attr(s, port_names, key);
1486 nl_msg_end_nested(key, encap);
1494 /* Parses the string representation of a datapath flow key, in the
1495 * format output by odp_flow_key_format(). Returns 0 if successful,
1496 * otherwise a positive errno value. On success, the flow key is
1497 * appended to 'key' as a series of Netlink attributes. On failure, no
1498 * data is appended to 'key'. Either way, 'key''s data might be
1501 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1502 * to a port number. (Port names may be used instead of port numbers in
1505 * On success, the attributes appended to 'key' are individually syntactically
1506 * valid, but they may not be valid as a sequence. 'key' might, for example,
1507 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1509 odp_flow_key_from_string(const char *s, const struct simap *port_names,
1512 const size_t old_size = key->size;
1516 s += strspn(s, delimiters);
1521 retval = parse_odp_key_attr(s, port_names, key);
1523 key->size = old_size;
1533 ovs_to_odp_frag(uint8_t nw_frag)
1535 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1536 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1537 : OVS_FRAG_TYPE_LATER);
1540 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1541 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1542 * number rather than a datapath port number). Instead, if 'odp_in_port'
1543 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1546 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1547 * capable of being expanded to allow for that much space. */
1549 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
1550 uint32_t odp_in_port)
1552 struct ovs_key_ethernet *eth_key;
1555 if (flow->skb_priority) {
1556 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1559 if (flow->tunnel.ip_dst) {
1560 tun_key_to_attr(buf, &flow->tunnel);
1563 if (flow->skb_mark) {
1564 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, flow->skb_mark);
1567 if (odp_in_port != OVSP_NONE) {
1568 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
1571 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1573 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1574 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1576 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1577 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1578 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1579 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1580 if (flow->vlan_tci == htons(0)) {
1587 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1591 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1593 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1594 struct ovs_key_ipv4 *ipv4_key;
1596 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1598 ipv4_key->ipv4_src = flow->nw_src;
1599 ipv4_key->ipv4_dst = flow->nw_dst;
1600 ipv4_key->ipv4_proto = flow->nw_proto;
1601 ipv4_key->ipv4_tos = flow->nw_tos;
1602 ipv4_key->ipv4_ttl = flow->nw_ttl;
1603 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1604 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1605 struct ovs_key_ipv6 *ipv6_key;
1607 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1609 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1610 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1611 ipv6_key->ipv6_label = flow->ipv6_label;
1612 ipv6_key->ipv6_proto = flow->nw_proto;
1613 ipv6_key->ipv6_tclass = flow->nw_tos;
1614 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1615 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1616 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1617 flow->dl_type == htons(ETH_TYPE_RARP)) {
1618 struct ovs_key_arp *arp_key;
1620 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1622 memset(arp_key, 0, sizeof *arp_key);
1623 arp_key->arp_sip = flow->nw_src;
1624 arp_key->arp_tip = flow->nw_dst;
1625 arp_key->arp_op = htons(flow->nw_proto);
1626 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1627 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1630 if (flow->mpls_depth) {
1631 struct ovs_key_mpls *mpls_key;
1633 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
1635 mpls_key->mpls_lse = flow->mpls_lse;
1638 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1639 if (flow->nw_proto == IPPROTO_TCP) {
1640 struct ovs_key_tcp *tcp_key;
1642 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1644 tcp_key->tcp_src = flow->tp_src;
1645 tcp_key->tcp_dst = flow->tp_dst;
1646 } else if (flow->nw_proto == IPPROTO_UDP) {
1647 struct ovs_key_udp *udp_key;
1649 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1651 udp_key->udp_src = flow->tp_src;
1652 udp_key->udp_dst = flow->tp_dst;
1653 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1654 && flow->nw_proto == IPPROTO_ICMP) {
1655 struct ovs_key_icmp *icmp_key;
1657 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1659 icmp_key->icmp_type = ntohs(flow->tp_src);
1660 icmp_key->icmp_code = ntohs(flow->tp_dst);
1661 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1662 && flow->nw_proto == IPPROTO_ICMPV6) {
1663 struct ovs_key_icmpv6 *icmpv6_key;
1665 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1666 sizeof *icmpv6_key);
1667 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1668 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1670 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1671 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1672 struct ovs_key_nd *nd_key;
1674 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1676 memcpy(nd_key->nd_target, &flow->nd_target,
1677 sizeof nd_key->nd_target);
1678 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1679 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1686 nl_msg_end_nested(buf, encap);
1691 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1693 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1694 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1698 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1699 uint64_t attrs, int out_of_range_attr,
1700 const struct nlattr *key, size_t key_len)
1705 if (VLOG_DROP_DBG(rl)) {
1710 for (i = 0; i < 64; i++) {
1711 if (attrs & (UINT64_C(1) << i)) {
1712 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1715 if (out_of_range_attr) {
1716 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1719 ds_put_cstr(&s, ": ");
1720 odp_flow_key_format(key, key_len, &s);
1722 VLOG_DBG("%s:%s", title, ds_cstr(&s));
1727 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1729 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1731 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1732 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1736 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1737 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1738 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1739 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1746 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1747 const struct nlattr *attrs[], uint64_t *present_attrsp,
1748 int *out_of_range_attrp)
1750 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1751 const struct nlattr *nla;
1752 uint64_t present_attrs;
1755 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
1757 *out_of_range_attrp = 0;
1758 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1759 uint16_t type = nl_attr_type(nla);
1760 size_t len = nl_attr_get_size(nla);
1761 int expected_len = odp_flow_key_attr_len(type);
1763 if (len != expected_len && expected_len >= 0) {
1764 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1765 "length %d", ovs_key_attr_to_string(type),
1770 if (type > OVS_KEY_ATTR_MAX) {
1771 *out_of_range_attrp = type;
1773 if (present_attrs & (UINT64_C(1) << type)) {
1774 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1775 ovs_key_attr_to_string(type));
1779 present_attrs |= UINT64_C(1) << type;
1784 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1788 *present_attrsp = present_attrs;
1792 static enum odp_key_fitness
1793 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1794 uint64_t expected_attrs,
1795 const struct nlattr *key, size_t key_len)
1797 uint64_t missing_attrs;
1798 uint64_t extra_attrs;
1800 missing_attrs = expected_attrs & ~present_attrs;
1801 if (missing_attrs) {
1802 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1803 log_odp_key_attributes(&rl, "expected but not present",
1804 missing_attrs, 0, key, key_len);
1805 return ODP_FIT_TOO_LITTLE;
1808 extra_attrs = present_attrs & ~expected_attrs;
1809 if (extra_attrs || out_of_range_attr) {
1810 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1811 log_odp_key_attributes(&rl, "present but not expected",
1812 extra_attrs, out_of_range_attr, key, key_len);
1813 return ODP_FIT_TOO_MUCH;
1816 return ODP_FIT_PERFECT;
1820 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1821 uint64_t present_attrs, uint64_t *expected_attrs,
1824 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1826 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1827 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1828 if (ntohs(flow->dl_type) < 1536) {
1829 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1830 ntohs(flow->dl_type));
1833 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1835 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1840 static enum odp_key_fitness
1841 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1842 uint64_t present_attrs, int out_of_range_attr,
1843 uint64_t expected_attrs, struct flow *flow,
1844 const struct nlattr *key, size_t key_len)
1846 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1848 if (eth_type_mpls(flow->dl_type)) {
1849 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
1851 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
1852 return ODP_FIT_TOO_LITTLE;
1854 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
1856 } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
1857 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1858 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1859 const struct ovs_key_ipv4 *ipv4_key;
1861 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1862 flow->nw_src = ipv4_key->ipv4_src;
1863 flow->nw_dst = ipv4_key->ipv4_dst;
1864 flow->nw_proto = ipv4_key->ipv4_proto;
1865 flow->nw_tos = ipv4_key->ipv4_tos;
1866 flow->nw_ttl = ipv4_key->ipv4_ttl;
1867 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1868 return ODP_FIT_ERROR;
1871 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1872 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1873 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1874 const struct ovs_key_ipv6 *ipv6_key;
1876 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1877 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1878 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1879 flow->ipv6_label = ipv6_key->ipv6_label;
1880 flow->nw_proto = ipv6_key->ipv6_proto;
1881 flow->nw_tos = ipv6_key->ipv6_tclass;
1882 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1883 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1884 return ODP_FIT_ERROR;
1887 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1888 flow->dl_type == htons(ETH_TYPE_RARP)) {
1889 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1890 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1891 const struct ovs_key_arp *arp_key;
1893 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1894 flow->nw_src = arp_key->arp_sip;
1895 flow->nw_dst = arp_key->arp_tip;
1896 if (arp_key->arp_op & htons(0xff00)) {
1897 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1898 "key", ntohs(arp_key->arp_op));
1899 return ODP_FIT_ERROR;
1901 flow->nw_proto = ntohs(arp_key->arp_op);
1902 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1903 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1907 if (flow->nw_proto == IPPROTO_TCP
1908 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1909 flow->dl_type == htons(ETH_TYPE_IPV6))
1910 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1911 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1912 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1913 const struct ovs_key_tcp *tcp_key;
1915 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1916 flow->tp_src = tcp_key->tcp_src;
1917 flow->tp_dst = tcp_key->tcp_dst;
1919 } else if (flow->nw_proto == IPPROTO_UDP
1920 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1921 flow->dl_type == htons(ETH_TYPE_IPV6))
1922 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1923 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1924 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1925 const struct ovs_key_udp *udp_key;
1927 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1928 flow->tp_src = udp_key->udp_src;
1929 flow->tp_dst = udp_key->udp_dst;
1931 } else if (flow->nw_proto == IPPROTO_ICMP
1932 && flow->dl_type == htons(ETH_TYPE_IP)
1933 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1934 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1935 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1936 const struct ovs_key_icmp *icmp_key;
1938 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1939 flow->tp_src = htons(icmp_key->icmp_type);
1940 flow->tp_dst = htons(icmp_key->icmp_code);
1942 } else if (flow->nw_proto == IPPROTO_ICMPV6
1943 && flow->dl_type == htons(ETH_TYPE_IPV6)
1944 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1945 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1946 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1947 const struct ovs_key_icmpv6 *icmpv6_key;
1949 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1950 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1951 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1953 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1954 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1955 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1956 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1957 const struct ovs_key_nd *nd_key;
1959 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1960 memcpy(&flow->nd_target, nd_key->nd_target,
1961 sizeof flow->nd_target);
1962 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1963 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1969 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1973 /* Parse 802.1Q header then encapsulated L3 attributes. */
1974 static enum odp_key_fitness
1975 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1976 uint64_t present_attrs, int out_of_range_attr,
1977 uint64_t expected_attrs, struct flow *flow,
1978 const struct nlattr *key, size_t key_len)
1980 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1982 const struct nlattr *encap
1983 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1984 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1985 enum odp_key_fitness encap_fitness;
1986 enum odp_key_fitness fitness;
1989 /* Calulate fitness of outer attributes. */
1990 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1991 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1992 fitness = check_expectations(present_attrs, out_of_range_attr,
1993 expected_attrs, key, key_len);
1995 /* Get the VLAN TCI value. */
1996 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1997 return ODP_FIT_TOO_LITTLE;
1999 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
2000 if (tci == htons(0)) {
2001 /* Corner case for a truncated 802.1Q header. */
2002 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
2003 return ODP_FIT_TOO_MUCH;
2006 } else if (!(tci & htons(VLAN_CFI))) {
2007 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
2008 "but CFI bit is not set", ntohs(tci));
2009 return ODP_FIT_ERROR;
2013 * Remove the TPID from dl_type since it's not the real Ethertype. */
2014 flow->vlan_tci = tci;
2015 flow->dl_type = htons(0);
2017 /* Now parse the encapsulated attributes. */
2018 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
2019 attrs, &present_attrs, &out_of_range_attr)) {
2020 return ODP_FIT_ERROR;
2024 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2025 return ODP_FIT_ERROR;
2027 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2028 expected_attrs, flow, key, key_len);
2030 /* The overall fitness is the worse of the outer and inner attributes. */
2031 return MAX(fitness, encap_fitness);
2034 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2035 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2036 * 'key' fits our expectations for what a flow key should contain.
2038 * The 'in_port' will be the datapath's understanding of the port. The
2039 * caller will need to translate with odp_port_to_ofp_port() if the
2040 * OpenFlow port is needed.
2042 * This function doesn't take the packet itself as an argument because none of
2043 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2044 * it is always possible to infer which additional attribute(s) should appear
2045 * by looking at the attributes for lower-level protocols, e.g. if the network
2046 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2047 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2048 * must be absent. */
2049 enum odp_key_fitness
2050 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2053 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2054 uint64_t expected_attrs;
2055 uint64_t present_attrs;
2056 int out_of_range_attr;
2058 memset(flow, 0, sizeof *flow);
2060 /* Parse attributes. */
2061 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2062 &out_of_range_attr)) {
2063 return ODP_FIT_ERROR;
2068 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2069 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2070 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2073 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2074 flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2075 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2078 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2079 enum odp_key_fitness res;
2081 res = tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2082 if (res == ODP_FIT_ERROR) {
2083 return ODP_FIT_ERROR;
2084 } else if (res == ODP_FIT_PERFECT) {
2085 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2089 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2090 flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
2091 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2093 flow->in_port = OVSP_NONE;
2096 /* Ethernet header. */
2097 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2098 const struct ovs_key_ethernet *eth_key;
2100 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2101 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2102 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2104 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2106 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2107 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2108 return ODP_FIT_ERROR;
2111 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2112 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2113 expected_attrs, flow, key, key_len);
2115 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2116 expected_attrs, flow, key, key_len);
2119 /* Returns 'fitness' as a string, for use in debug messages. */
2121 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2124 case ODP_FIT_PERFECT:
2126 case ODP_FIT_TOO_MUCH:
2128 case ODP_FIT_TOO_LITTLE:
2129 return "too_little";
2137 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2138 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2139 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2140 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2141 * null, then the return value is not meaningful.) */
2143 odp_put_userspace_action(uint32_t pid,
2144 const void *userdata, size_t userdata_size,
2145 struct ofpbuf *odp_actions)
2147 size_t userdata_ofs;
2150 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
2151 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
2153 userdata_ofs = odp_actions->size + NLA_HDRLEN;
2154 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
2155 userdata, userdata_size);
2159 nl_msg_end_nested(odp_actions, offset);
2161 return userdata_ofs;
2165 odp_put_tunnel_action(const struct flow_tnl *tunnel,
2166 struct ofpbuf *odp_actions)
2168 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2169 tun_key_to_attr(odp_actions, tunnel);
2170 nl_msg_end_nested(odp_actions, offset);
2173 /* The commit_odp_actions() function and its helpers. */
2176 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
2177 const void *key, size_t key_size)
2179 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2180 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
2181 nl_msg_end_nested(odp_actions, offset);
2185 odp_put_skb_mark_action(const uint32_t skb_mark,
2186 struct ofpbuf *odp_actions)
2188 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
2192 /* If any of the flow key data that ODP actions can modify are different in
2193 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2194 * 'odp_actions' that change the flow tunneling information in key from
2195 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2196 * same way. In other words, operates the same as commit_odp_actions(), but
2197 * only on tunneling information. */
2199 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
2200 struct ofpbuf *odp_actions)
2202 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2203 if (flow->tunnel.ip_dst) {
2204 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
2207 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
2208 odp_put_tunnel_action(&base->tunnel, odp_actions);
2213 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
2214 struct ofpbuf *odp_actions)
2216 struct ovs_key_ethernet eth_key;
2218 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2219 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2223 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2224 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2226 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2227 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2229 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2230 ð_key, sizeof(eth_key));
2234 commit_vlan_action(const struct flow *flow, struct flow *base,
2235 struct ofpbuf *odp_actions)
2237 if (base->vlan_tci == flow->vlan_tci) {
2241 if (base->vlan_tci & htons(VLAN_CFI)) {
2242 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2245 if (flow->vlan_tci & htons(VLAN_CFI)) {
2246 struct ovs_action_push_vlan vlan;
2248 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2249 vlan.vlan_tci = flow->vlan_tci;
2250 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2251 &vlan, sizeof vlan);
2253 base->vlan_tci = flow->vlan_tci;
2257 commit_mpls_action(const struct flow *flow, struct flow *base,
2258 struct ofpbuf *odp_actions)
2260 if (flow->mpls_lse == base->mpls_lse &&
2261 flow->mpls_depth == base->mpls_depth) {
2265 if (flow->mpls_depth < base->mpls_depth) {
2266 if (base->mpls_depth - flow->mpls_depth > 1) {
2267 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2268 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
2269 " a single mpls_pop action");
2272 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
2273 } else if (flow->mpls_depth > base->mpls_depth) {
2274 struct ovs_action_push_mpls *mpls;
2276 if (flow->mpls_depth - base->mpls_depth > 1) {
2277 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2278 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
2279 " a single mpls_push action");
2282 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
2284 memset(mpls, 0, sizeof *mpls);
2285 mpls->mpls_ethertype = flow->dl_type;
2286 mpls->mpls_lse = flow->mpls_lse;
2288 struct ovs_key_mpls mpls_key;
2290 mpls_key.mpls_lse = flow->mpls_lse;
2291 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
2292 &mpls_key, sizeof(mpls_key));
2295 base->dl_type = flow->dl_type;
2296 base->mpls_lse = flow->mpls_lse;
2297 base->mpls_depth = flow->mpls_depth;
2301 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2302 struct ofpbuf *odp_actions)
2304 struct ovs_key_ipv4 ipv4_key;
2306 if (base->nw_src == flow->nw_src &&
2307 base->nw_dst == flow->nw_dst &&
2308 base->nw_tos == flow->nw_tos &&
2309 base->nw_ttl == flow->nw_ttl &&
2310 base->nw_frag == flow->nw_frag) {
2314 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2315 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2316 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2317 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2318 ipv4_key.ipv4_proto = base->nw_proto;
2319 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2321 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2322 &ipv4_key, sizeof(ipv4_key));
2326 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2327 struct ofpbuf *odp_actions)
2329 struct ovs_key_ipv6 ipv6_key;
2331 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2332 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2333 base->ipv6_label == flow->ipv6_label &&
2334 base->nw_tos == flow->nw_tos &&
2335 base->nw_ttl == flow->nw_ttl &&
2336 base->nw_frag == flow->nw_frag) {
2340 base->ipv6_src = flow->ipv6_src;
2341 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2342 base->ipv6_dst = flow->ipv6_dst;
2343 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2345 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2346 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2347 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2348 ipv6_key.ipv6_proto = base->nw_proto;
2349 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2351 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2352 &ipv6_key, sizeof(ipv6_key));
2356 commit_set_nw_action(const struct flow *flow, struct flow *base,
2357 struct ofpbuf *odp_actions)
2359 /* Check if flow really have an IP header. */
2360 if (!flow->nw_proto) {
2364 if (base->dl_type == htons(ETH_TYPE_IP)) {
2365 commit_set_ipv4_action(flow, base, odp_actions);
2366 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2367 commit_set_ipv6_action(flow, base, odp_actions);
2372 commit_set_port_action(const struct flow *flow, struct flow *base,
2373 struct ofpbuf *odp_actions)
2375 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
2379 if (base->tp_src == flow->tp_src &&
2380 base->tp_dst == flow->tp_dst) {
2384 if (flow->nw_proto == IPPROTO_TCP) {
2385 struct ovs_key_tcp port_key;
2387 port_key.tcp_src = base->tp_src = flow->tp_src;
2388 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2390 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2391 &port_key, sizeof(port_key));
2393 } else if (flow->nw_proto == IPPROTO_UDP) {
2394 struct ovs_key_udp port_key;
2396 port_key.udp_src = base->tp_src = flow->tp_src;
2397 port_key.udp_dst = base->tp_dst = flow->tp_dst;
2399 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2400 &port_key, sizeof(port_key));
2405 commit_set_priority_action(const struct flow *flow, struct flow *base,
2406 struct ofpbuf *odp_actions)
2408 if (base->skb_priority == flow->skb_priority) {
2411 base->skb_priority = flow->skb_priority;
2413 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2414 &base->skb_priority, sizeof(base->skb_priority));
2418 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2419 struct ofpbuf *odp_actions)
2421 if (base->skb_mark == flow->skb_mark) {
2424 base->skb_mark = flow->skb_mark;
2426 odp_put_skb_mark_action(base->skb_mark, odp_actions);
2428 /* If any of the flow key data that ODP actions can modify are different in
2429 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2430 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2431 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2432 * in addition to this function if needed. */
2434 commit_odp_actions(const struct flow *flow, struct flow *base,
2435 struct ofpbuf *odp_actions)
2437 commit_set_ether_addr_action(flow, base, odp_actions);
2438 commit_vlan_action(flow, base, odp_actions);
2439 commit_set_nw_action(flow, base, odp_actions);
2440 commit_set_port_action(flow, base, odp_actions);
2441 /* Commiting MPLS actions should occur after committing nw and port
2442 * actions. This is because committing MPLS actions may alter a packet so
2443 * that it is no longer IP and thus nw and port actions are no longer valid.
2445 commit_mpls_action(flow, base, odp_actions);
2446 commit_set_priority_action(flow, base, odp_actions);
2447 commit_set_skb_mark_action(flow, base, odp_actions);