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_mask_attr(const char *, const struct simap *port_names,
52 struct ofpbuf *, struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a,
54 const struct nlattr *ma,
55 const struct hmap *portno_names, struct ds *ds,
58 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
61 * - For an action whose argument has a fixed length, returned that
62 * nonnegative length in bytes.
64 * - For an action with a variable-length argument, returns -2.
66 * - For an invalid 'type', returns -1. */
68 odp_action_len(uint16_t type)
70 if (type > OVS_ACTION_ATTR_MAX) {
74 switch ((enum ovs_action_attr) type) {
75 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
76 case OVS_ACTION_ATTR_USERSPACE: return -2;
77 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
78 case OVS_ACTION_ATTR_POP_VLAN: return 0;
79 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
80 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
81 case OVS_ACTION_ATTR_SET: return -2;
82 case OVS_ACTION_ATTR_SAMPLE: return -2;
84 case OVS_ACTION_ATTR_UNSPEC:
85 case __OVS_ACTION_ATTR_MAX:
92 /* Returns a string form of 'attr'. The return value is either a statically
93 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
94 * should be at least OVS_KEY_ATTR_BUFSIZE. */
95 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
97 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
100 case OVS_KEY_ATTR_UNSPEC: return "unspec";
101 case OVS_KEY_ATTR_ENCAP: return "encap";
102 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
103 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
104 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
105 case OVS_KEY_ATTR_IN_PORT: return "in_port";
106 case OVS_KEY_ATTR_ETHERNET: return "eth";
107 case OVS_KEY_ATTR_VLAN: return "vlan";
108 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
109 case OVS_KEY_ATTR_IPV4: return "ipv4";
110 case OVS_KEY_ATTR_IPV6: return "ipv6";
111 case OVS_KEY_ATTR_TCP: return "tcp";
112 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
113 case OVS_KEY_ATTR_UDP: return "udp";
114 case OVS_KEY_ATTR_SCTP: return "sctp";
115 case OVS_KEY_ATTR_ICMP: return "icmp";
116 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
117 case OVS_KEY_ATTR_ARP: return "arp";
118 case OVS_KEY_ATTR_ND: return "nd";
119 case OVS_KEY_ATTR_MPLS: return "mpls";
121 case __OVS_KEY_ATTR_MAX:
123 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
129 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
131 size_t len = nl_attr_get_size(a);
133 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
135 const uint8_t *unspec;
138 unspec = nl_attr_get(a);
139 for (i = 0; i < len; i++) {
140 ds_put_char(ds, i ? ' ': '(');
141 ds_put_format(ds, "%02x", unspec[i]);
143 ds_put_char(ds, ')');
148 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
150 static const struct nl_policy ovs_sample_policy[] = {
151 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */
152 { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */
153 { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */
155 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
157 const struct nlattr *nla_acts;
160 ds_put_cstr(ds, "sample");
162 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
163 ds_put_cstr(ds, "(error)");
167 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
170 ds_put_format(ds, "(sample=%.1f%%,", percentage);
172 ds_put_cstr(ds, "actions(");
173 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
174 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
175 format_odp_actions(ds, nla_acts, len);
176 ds_put_format(ds, "))");
180 slow_path_reason_to_string(uint32_t reason)
182 switch ((enum slow_path_reason) reason) {
183 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
192 slow_path_reason_to_explanation(enum slow_path_reason reason)
195 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
204 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
215 while (s[n] != ')') {
216 unsigned long long int flags;
220 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
221 n += n0 + (s[n + n0] == ',');
226 for (bit = 1; bit; bit <<= 1) {
227 const char *name = bit_to_string(bit);
235 if (!strncmp(s + n, name, len) &&
236 (s[n + len] == ',' || s[n + len] == ')')) {
238 n += len + (s[n + len] == ',');
254 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
256 static const struct nl_policy ovs_userspace_policy[] = {
257 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */
258 { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */
259 { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */
261 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
262 const struct nlattr *userdata_attr;
264 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
265 ds_put_cstr(ds, "userspace(error)");
269 ds_put_format(ds, "userspace(pid=%"PRIu32,
270 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
272 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
275 const uint8_t *userdata = nl_attr_get(userdata_attr);
276 size_t userdata_len = nl_attr_get_size(userdata_attr);
277 bool userdata_unspec = true;
278 union user_action_cookie cookie;
280 if (userdata_len >= sizeof cookie.type
281 && userdata_len <= sizeof cookie) {
283 memset(&cookie, 0, sizeof cookie);
284 memcpy(&cookie, userdata, userdata_len);
286 userdata_unspec = false;
288 if (userdata_len == sizeof cookie.sflow
289 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
290 ds_put_format(ds, ",sFlow("
291 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
292 vlan_tci_to_vid(cookie.sflow.vlan_tci),
293 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
294 cookie.sflow.output);
295 } else if (userdata_len == sizeof cookie.slow_path
296 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
297 ds_put_cstr(ds, ",slow_path(");
298 format_flags(ds, slow_path_reason_to_string,
299 cookie.slow_path.reason, ',');
300 ds_put_format(ds, ")");
301 } else if (userdata_len == sizeof cookie.flow_sample
302 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
303 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
304 ",collector_set_id=%"PRIu32
305 ",obs_domain_id=%"PRIu32
306 ",obs_point_id=%"PRIu32")",
307 cookie.flow_sample.probability,
308 cookie.flow_sample.collector_set_id,
309 cookie.flow_sample.obs_domain_id,
310 cookie.flow_sample.obs_point_id);
311 } else if (userdata_len == sizeof cookie.ipfix
312 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
313 ds_put_format(ds, ",ipfix");
315 userdata_unspec = true;
319 if (userdata_unspec) {
321 ds_put_format(ds, ",userdata(");
322 for (i = 0; i < userdata_len; i++) {
323 ds_put_format(ds, "%02x", userdata[i]);
325 ds_put_char(ds, ')');
329 ds_put_char(ds, ')');
333 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
335 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
336 vlan_tci_to_vid(vlan_tci),
337 vlan_tci_to_pcp(vlan_tci));
338 if (!(vlan_tci & htons(VLAN_CFI))) {
339 ds_put_cstr(ds, ",cfi=0");
344 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
346 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
347 mpls_lse_to_label(mpls_lse),
348 mpls_lse_to_tc(mpls_lse),
349 mpls_lse_to_ttl(mpls_lse),
350 mpls_lse_to_bos(mpls_lse));
354 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
355 const struct ovs_key_mpls *mpls_mask)
357 ovs_be32 key = mpls_key->mpls_lse;
359 if (mpls_mask == NULL) {
360 format_mpls_lse(ds, key);
362 ovs_be32 mask = mpls_mask->mpls_lse;
364 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
365 mpls_lse_to_label(key), mpls_lse_to_label(mask),
366 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
367 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
368 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
373 format_odp_action(struct ds *ds, const struct nlattr *a)
376 enum ovs_action_attr type = nl_attr_type(a);
377 const struct ovs_action_push_vlan *vlan;
379 expected_len = odp_action_len(nl_attr_type(a));
380 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
381 ds_put_format(ds, "bad length %zu, expected %d for: ",
382 nl_attr_get_size(a), expected_len);
383 format_generic_odp_action(ds, a);
388 case OVS_ACTION_ATTR_OUTPUT:
389 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
391 case OVS_ACTION_ATTR_USERSPACE:
392 format_odp_userspace_action(ds, a);
394 case OVS_ACTION_ATTR_SET:
395 ds_put_cstr(ds, "set(");
396 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
397 ds_put_cstr(ds, ")");
399 case OVS_ACTION_ATTR_PUSH_VLAN:
400 vlan = nl_attr_get(a);
401 ds_put_cstr(ds, "push_vlan(");
402 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
403 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
405 format_vlan_tci(ds, vlan->vlan_tci);
406 ds_put_char(ds, ')');
408 case OVS_ACTION_ATTR_POP_VLAN:
409 ds_put_cstr(ds, "pop_vlan");
411 case OVS_ACTION_ATTR_PUSH_MPLS: {
412 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
413 ds_put_cstr(ds, "push_mpls(");
414 format_mpls_lse(ds, mpls->mpls_lse);
415 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
418 case OVS_ACTION_ATTR_POP_MPLS: {
419 ovs_be16 ethertype = nl_attr_get_be16(a);
420 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
423 case OVS_ACTION_ATTR_SAMPLE:
424 format_odp_sample_action(ds, a);
426 case OVS_ACTION_ATTR_UNSPEC:
427 case __OVS_ACTION_ATTR_MAX:
429 format_generic_odp_action(ds, a);
435 format_odp_actions(struct ds *ds, const struct nlattr *actions,
439 const struct nlattr *a;
442 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
444 ds_put_char(ds, ',');
446 format_odp_action(ds, a);
451 if (left == actions_len) {
452 ds_put_cstr(ds, "<empty>");
454 ds_put_format(ds, ",***%u leftover bytes*** (", left);
455 for (i = 0; i < left; i++) {
456 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
458 ds_put_char(ds, ')');
461 ds_put_cstr(ds, "drop");
466 parse_odp_action(const char *s, const struct simap *port_names,
467 struct ofpbuf *actions)
469 /* Many of the sscanf calls in this function use oversized destination
470 * fields because some sscanf() implementations truncate the range of %i
471 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
472 * value of 0x7fff. The other alternatives are to allow only a single
473 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
476 * The tun_id parser has to use an alternative approach because there is no
477 * type larger than 64 bits. */
480 unsigned long long int port;
483 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
484 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
490 int len = strcspn(s, delimiters);
491 struct simap_node *node;
493 node = simap_find_len(port_names, s, len);
495 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
501 unsigned long long int pid;
502 unsigned long long int output;
503 unsigned long long int probability;
504 unsigned long long int collector_set_id;
505 unsigned long long int obs_domain_id;
506 unsigned long long int obs_point_id;
510 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
511 odp_put_userspace_action(pid, NULL, 0, actions);
513 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
514 "pcp=%i,output=%lli))%n",
515 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
516 union user_action_cookie cookie;
519 tci = vid | (pcp << VLAN_PCP_SHIFT);
524 cookie.type = USER_ACTION_COOKIE_SFLOW;
525 cookie.sflow.vlan_tci = htons(tci);
526 cookie.sflow.output = output;
527 odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
530 } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
532 union user_action_cookie cookie;
535 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
536 cookie.slow_path.unused = 0;
537 cookie.slow_path.reason = 0;
539 res = parse_flags(&s[n], slow_path_reason_to_string,
540 &cookie.slow_path.reason);
550 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
553 } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
554 "collector_set_id=%lli,obs_domain_id=%lli,"
555 "obs_point_id=%lli))%n",
556 &pid, &probability, &collector_set_id,
557 &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
558 union user_action_cookie cookie;
560 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
561 cookie.flow_sample.probability = probability;
562 cookie.flow_sample.collector_set_id = collector_set_id;
563 cookie.flow_sample.obs_domain_id = obs_domain_id;
564 cookie.flow_sample.obs_point_id = obs_point_id;
565 odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
568 } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
570 union user_action_cookie cookie;
572 cookie.type = USER_ACTION_COOKIE_IPFIX;
573 odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
576 } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
581 ofpbuf_init(&buf, 16);
582 end = ofpbuf_put_hex(&buf, &s[n], NULL);
583 if (end[0] == ')' && end[1] == ')') {
584 odp_put_userspace_action(pid, buf.data, buf.size, actions);
586 return (end + 2) - s;
591 if (!strncmp(s, "set(", 4)) {
595 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
596 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
600 if (s[retval + 4] != ')') {
603 nl_msg_end_nested(actions, start_ofs);
608 struct ovs_action_push_vlan push;
609 int tpid = ETH_TYPE_VLAN;
614 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
616 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
617 &vid, &pcp, &cfi, &n) > 0 && n > 0)
618 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
619 &tpid, &vid, &pcp, &n) > 0 && n > 0)
620 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
621 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
622 push.vlan_tpid = htons(tpid);
623 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
624 | (pcp << VLAN_PCP_SHIFT)
625 | (cfi ? VLAN_CFI : 0));
626 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
633 if (!strncmp(s, "pop_vlan", 8)) {
634 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
642 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
643 && percentage >= 0. && percentage <= 100.0
645 size_t sample_ofs, actions_ofs;
648 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
649 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
650 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
651 (probability <= 0 ? 0
652 : probability >= UINT32_MAX ? UINT32_MAX
655 actions_ofs = nl_msg_start_nested(actions,
656 OVS_SAMPLE_ATTR_ACTIONS);
660 n += strspn(s + n, delimiters);
665 retval = parse_odp_action(s + n, port_names, actions);
671 nl_msg_end_nested(actions, actions_ofs);
672 nl_msg_end_nested(actions, sample_ofs);
674 return s[n + 1] == ')' ? n + 2 : -EINVAL;
681 /* Parses the string representation of datapath actions, in the format output
682 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
683 * value. On success, the ODP actions are appended to 'actions' as a series of
684 * Netlink attributes. On failure, no data is appended to 'actions'. Either
685 * way, 'actions''s data might be reallocated. */
687 odp_actions_from_string(const char *s, const struct simap *port_names,
688 struct ofpbuf *actions)
692 if (!strcasecmp(s, "drop")) {
696 old_size = actions->size;
700 s += strspn(s, delimiters);
705 retval = parse_odp_action(s, port_names, actions);
706 if (retval < 0 || !strchr(delimiters, s[retval])) {
707 actions->size = old_size;
716 /* Returns the correct length of the payload for a flow key attribute of the
717 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
718 * is variable length. */
720 odp_flow_key_attr_len(uint16_t type)
722 if (type > OVS_KEY_ATTR_MAX) {
726 switch ((enum ovs_key_attr) type) {
727 case OVS_KEY_ATTR_ENCAP: return -2;
728 case OVS_KEY_ATTR_PRIORITY: return 4;
729 case OVS_KEY_ATTR_SKB_MARK: return 4;
730 case OVS_KEY_ATTR_TUNNEL: return -2;
731 case OVS_KEY_ATTR_IN_PORT: return 4;
732 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
733 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
734 case OVS_KEY_ATTR_ETHERTYPE: return 2;
735 case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
736 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
737 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
738 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
739 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
740 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
741 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
742 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
743 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
744 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
745 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
747 case OVS_KEY_ATTR_UNSPEC:
748 case __OVS_KEY_ATTR_MAX:
756 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
758 size_t len = nl_attr_get_size(a);
760 const uint8_t *unspec;
763 unspec = nl_attr_get(a);
764 for (i = 0; i < len; i++) {
766 ds_put_char(ds, ' ');
768 ds_put_format(ds, "%02x", unspec[i]);
774 ovs_frag_type_to_string(enum ovs_frag_type type)
777 case OVS_FRAG_TYPE_NONE:
779 case OVS_FRAG_TYPE_FIRST:
781 case OVS_FRAG_TYPE_LATER:
783 case __OVS_FRAG_TYPE_MAX:
790 tunnel_key_attr_len(int type)
793 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
794 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
795 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
796 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
797 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
798 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
799 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
800 case __OVS_TUNNEL_KEY_ATTR_MAX:
807 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
810 const struct nlattr *a;
812 bool unknown = false;
814 NL_NESTED_FOR_EACH(a, left, attr) {
815 uint16_t type = nl_attr_type(a);
816 size_t len = nl_attr_get_size(a);
817 int expected_len = tunnel_key_attr_len(type);
819 if (len != expected_len && expected_len >= 0) {
820 return ODP_FIT_ERROR;
824 case OVS_TUNNEL_KEY_ATTR_ID:
825 tun->tun_id = nl_attr_get_be64(a);
826 tun->flags |= FLOW_TNL_F_KEY;
828 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
829 tun->ip_src = nl_attr_get_be32(a);
831 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
832 tun->ip_dst = nl_attr_get_be32(a);
834 case OVS_TUNNEL_KEY_ATTR_TOS:
835 tun->ip_tos = nl_attr_get_u8(a);
837 case OVS_TUNNEL_KEY_ATTR_TTL:
838 tun->ip_ttl = nl_attr_get_u8(a);
841 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
842 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
844 case OVS_TUNNEL_KEY_ATTR_CSUM:
845 tun->flags |= FLOW_TNL_F_CSUM;
848 /* Allow this to show up as unexpected, if there are unknown
849 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
856 return ODP_FIT_ERROR;
859 return ODP_FIT_TOO_MUCH;
861 return ODP_FIT_PERFECT;
865 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
869 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
871 if (tun_key->flags & FLOW_TNL_F_KEY) {
872 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
874 if (tun_key->ip_src) {
875 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
877 if (tun_key->ip_dst) {
878 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
880 if (tun_key->ip_tos) {
881 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
883 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
884 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
885 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
887 if (tun_key->flags & FLOW_TNL_F_CSUM) {
888 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
891 nl_msg_end_nested(a, tun_key_ofs);
895 odp_mask_attr_is_wildcard(const struct nlattr *ma)
897 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
901 odp_mask_attr_is_exact(const struct nlattr *ma)
903 bool is_exact = false;
904 enum ovs_key_attr attr = nl_attr_type(ma);
906 if (attr == OVS_KEY_ATTR_TUNNEL) {
907 /* XXX this is a hack for now. Should change
908 * the exact match dection to per field
909 * instead of per attribute.
911 struct flow_tnl tun_mask;
912 memset(&tun_mask, 0, sizeof tun_mask);
913 odp_tun_key_from_attr(ma, &tun_mask);
914 if (tun_mask.flags == (FLOW_TNL_F_KEY
915 | FLOW_TNL_F_DONT_FRAGMENT
916 | FLOW_TNL_F_CSUM)) {
917 /* The flags are exact match, check the remaining fields. */
918 tun_mask.flags = 0xffff;
919 is_exact = is_all_ones((uint8_t *)&tun_mask,
920 offsetof(struct flow_tnl, ip_ttl));
923 is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
930 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
933 struct odp_portno_names *odp_portno_names;
935 odp_portno_names = xmalloc(sizeof *odp_portno_names);
936 odp_portno_names->port_no = port_no;
937 odp_portno_names->name = xstrdup(port_name);
938 hmap_insert(portno_names, &odp_portno_names->hmap_node,
939 hash_odp_port(port_no));
943 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
945 struct odp_portno_names *odp_portno_names;
947 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
948 hash_odp_port(port_no), portno_names) {
949 if (odp_portno_names->port_no == port_no) {
950 return odp_portno_names->name;
957 odp_portno_names_destroy(struct hmap *portno_names)
959 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
960 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
961 hmap_node, portno_names) {
962 hmap_remove(portno_names, &odp_portno_names->hmap_node);
963 free(odp_portno_names->name);
964 free(odp_portno_names);
969 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
970 const struct hmap *portno_names, struct ds *ds,
973 struct flow_tnl tun_key;
974 enum ovs_key_attr attr = nl_attr_type(a);
975 char namebuf[OVS_KEY_ATTR_BUFSIZE];
979 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
981 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
984 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
985 if (expected_len != -2) {
986 bool bad_key_len = nl_attr_get_size(a) != expected_len;
987 bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
989 if (bad_key_len || bad_mask_len) {
991 ds_put_format(ds, "(bad key length %zu, expected %d)(",
993 odp_flow_key_attr_len(nl_attr_type(a)));
995 format_generic_odp_key(a, ds);
997 ds_put_char(ds, '/');
998 ds_put_format(ds, "(bad mask length %zu, expected %d)(",
999 nl_attr_get_size(ma),
1000 odp_flow_key_attr_len(nl_attr_type(ma)));
1002 format_generic_odp_key(ma, ds);
1003 ds_put_char(ds, ')');
1009 ds_put_char(ds, '(');
1011 case OVS_KEY_ATTR_ENCAP:
1012 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1013 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1014 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1016 } else if (nl_attr_get_size(a)) {
1017 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1022 case OVS_KEY_ATTR_PRIORITY:
1023 case OVS_KEY_ATTR_SKB_MARK:
1024 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1026 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1030 case OVS_KEY_ATTR_TUNNEL:
1031 memset(&tun_key, 0, sizeof tun_key);
1032 if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1033 ds_put_format(ds, "error");
1034 } else if (!is_exact) {
1035 struct flow_tnl tun_mask;
1037 memset(&tun_mask, 0, sizeof tun_mask);
1038 odp_tun_key_from_attr(ma, &tun_mask);
1039 ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1040 ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1041 ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1043 ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1044 IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1045 IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1046 tun_key.ip_tos, tun_mask.ip_tos,
1047 tun_key.ip_ttl, tun_mask.ip_ttl);
1049 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1051 /* XXX This code is correct, but enabling it would break the unit
1052 test. Disable it for now until the input parser is fixed.
1054 ds_put_char(ds, '/');
1055 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1057 ds_put_char(ds, ')');
1059 ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1060 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1061 ntohll(tun_key.tun_id),
1062 IP_ARGS(tun_key.ip_src),
1063 IP_ARGS(tun_key.ip_dst),
1064 tun_key.ip_tos, tun_key.ip_ttl);
1066 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1067 ds_put_char(ds, ')');
1071 case OVS_KEY_ATTR_IN_PORT:
1072 if (portno_names && verbose && is_exact) {
1073 char *name = odp_portno_names_get(portno_names,
1074 u32_to_odp(nl_attr_get_u32(a)));
1076 ds_put_format(ds, "%s", name);
1078 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1081 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1083 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1088 case OVS_KEY_ATTR_ETHERNET:
1090 const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1091 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1093 ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1094 ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1095 ETH_ADDR_ARGS(eth_key->eth_src),
1096 ETH_ADDR_ARGS(eth_mask->eth_src),
1097 ETH_ADDR_ARGS(eth_key->eth_dst),
1098 ETH_ADDR_ARGS(eth_mask->eth_dst));
1100 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1102 ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1103 ETH_ADDR_ARGS(eth_key->eth_src),
1104 ETH_ADDR_ARGS(eth_key->eth_dst));
1108 case OVS_KEY_ATTR_VLAN:
1110 ovs_be16 vlan_tci = nl_attr_get_be16(a);
1112 ovs_be16 mask = nl_attr_get_be16(ma);
1113 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1114 vlan_tci_to_vid(vlan_tci),
1115 vlan_tci_to_vid(mask),
1116 vlan_tci_to_pcp(vlan_tci),
1117 vlan_tci_to_pcp(mask),
1118 vlan_tci_to_cfi(vlan_tci),
1119 vlan_tci_to_cfi(mask));
1121 format_vlan_tci(ds, vlan_tci);
1126 case OVS_KEY_ATTR_MPLS: {
1127 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1128 const struct ovs_key_mpls *mpls_mask = NULL;
1130 mpls_mask = nl_attr_get(ma);
1132 format_mpls(ds, mpls_key, mpls_mask);
1136 case OVS_KEY_ATTR_ETHERTYPE:
1137 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1139 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1143 case OVS_KEY_ATTR_IPV4:
1145 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1146 const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1148 ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1149 ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1150 ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1151 IP_ARGS(ipv4_key->ipv4_src),
1152 IP_ARGS(ipv4_mask->ipv4_src),
1153 IP_ARGS(ipv4_key->ipv4_dst),
1154 IP_ARGS(ipv4_mask->ipv4_dst),
1155 ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1156 ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1157 ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1158 ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1159 ipv4_mask->ipv4_frag);
1161 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1163 ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1164 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1165 IP_ARGS(ipv4_key->ipv4_src),
1166 IP_ARGS(ipv4_key->ipv4_dst),
1167 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1169 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1173 case OVS_KEY_ATTR_IPV6:
1175 const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1176 char src_str[INET6_ADDRSTRLEN];
1177 char dst_str[INET6_ADDRSTRLEN];
1178 char src_mask[INET6_ADDRSTRLEN];
1179 char dst_mask[INET6_ADDRSTRLEN];
1181 ipv6_key = nl_attr_get(a);
1182 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1183 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1185 ipv6_mask = nl_attr_get(ma);
1186 inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1187 inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1189 ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1190 ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1191 ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1192 src_str, src_mask, dst_str, dst_mask,
1193 ntohl(ipv6_key->ipv6_label),
1194 ntohl(ipv6_mask->ipv6_label),
1195 ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1196 ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1197 ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1198 ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1199 ipv6_mask->ipv6_frag);
1201 const struct ovs_key_ipv6 *ipv6_key;
1202 char src_str[INET6_ADDRSTRLEN];
1203 char dst_str[INET6_ADDRSTRLEN];
1205 ipv6_key = nl_attr_get(a);
1206 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1207 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1209 ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1210 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1211 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1212 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1213 ipv6_key->ipv6_hlimit,
1214 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1218 case OVS_KEY_ATTR_TCP:
1220 const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1221 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1223 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1224 ",dst=%"PRIu16"/%#"PRIx16,
1225 ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1226 ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1228 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1230 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1231 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1235 case OVS_KEY_ATTR_TCP_FLAGS:
1236 ds_put_format(ds, "0x%03"PRIx16, ntohs(nl_attr_get_be16(a)));
1238 ds_put_format(ds, "/0x%03"PRIx16, ntohs(nl_attr_get_be16(ma)));
1242 case OVS_KEY_ATTR_UDP:
1244 const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1245 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1247 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1248 ",dst=%"PRIu16"/%#"PRIx16,
1249 ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1250 ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1252 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1254 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1255 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1259 case OVS_KEY_ATTR_SCTP:
1261 const struct ovs_key_sctp *sctp_mask = nl_attr_get(ma);
1262 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1264 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1265 ",dst=%"PRIu16"/%#"PRIx16,
1266 ntohs(sctp_key->sctp_src), ntohs(sctp_mask->sctp_src),
1267 ntohs(sctp_key->sctp_dst), ntohs(sctp_mask->sctp_dst));
1269 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1271 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
1272 ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst));
1276 case OVS_KEY_ATTR_ICMP:
1278 const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1279 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1281 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1282 icmp_key->icmp_type, icmp_mask->icmp_type,
1283 icmp_key->icmp_code, icmp_mask->icmp_code);
1285 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1287 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1288 icmp_key->icmp_type, icmp_key->icmp_code);
1292 case OVS_KEY_ATTR_ICMPV6:
1294 const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1295 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1297 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1298 icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1299 icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1301 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1303 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1304 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1308 case OVS_KEY_ATTR_ARP:
1310 const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1311 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1313 ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1314 ",op=%"PRIu16"/%#"PRIx16
1315 ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1316 ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1317 IP_ARGS(arp_key->arp_sip),
1318 IP_ARGS(arp_mask->arp_sip),
1319 IP_ARGS(arp_key->arp_tip),
1320 IP_ARGS(arp_mask->arp_tip),
1321 ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1322 ETH_ADDR_ARGS(arp_key->arp_sha),
1323 ETH_ADDR_ARGS(arp_mask->arp_sha),
1324 ETH_ADDR_ARGS(arp_key->arp_tha),
1325 ETH_ADDR_ARGS(arp_mask->arp_tha));
1327 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1329 ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1330 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1331 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1332 ntohs(arp_key->arp_op),
1333 ETH_ADDR_ARGS(arp_key->arp_sha),
1334 ETH_ADDR_ARGS(arp_key->arp_tha));
1338 case OVS_KEY_ATTR_ND: {
1339 const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1340 char target[INET6_ADDRSTRLEN];
1342 nd_key = nl_attr_get(a);
1344 nd_mask = nl_attr_get(ma);
1347 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1348 ds_put_format(ds, "target=%s", target);
1350 inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1351 ds_put_format(ds, "/%s", target);
1354 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1355 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1356 ETH_ADDR_ARGS(nd_key->nd_sll));
1358 ds_put_format(ds, "/"ETH_ADDR_FMT,
1359 ETH_ADDR_ARGS(nd_mask->nd_sll));
1362 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1363 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1364 ETH_ADDR_ARGS(nd_key->nd_tll));
1366 ds_put_format(ds, "/"ETH_ADDR_FMT,
1367 ETH_ADDR_ARGS(nd_mask->nd_tll));
1373 case OVS_KEY_ATTR_UNSPEC:
1374 case __OVS_KEY_ATTR_MAX:
1376 format_generic_odp_key(a, ds);
1378 ds_put_char(ds, '/');
1379 format_generic_odp_key(ma, ds);
1383 ds_put_char(ds, ')');
1386 static struct nlattr *
1387 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1389 const struct nlattr *a;
1391 int type = nl_attr_type(key);
1392 int size = nl_attr_get_size(key);
1394 if (odp_flow_key_attr_len(type) >=0) {
1395 memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size);
1399 nested_mask = nl_msg_start_nested(ofp, type);
1400 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1401 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1403 nl_msg_end_nested(ofp, nested_mask);
1409 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1410 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1411 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
1412 * non-null and 'verbose' is true, translates odp port number to its name. */
1414 odp_flow_format(const struct nlattr *key, size_t key_len,
1415 const struct nlattr *mask, size_t mask_len,
1416 const struct hmap *portno_names, struct ds *ds, bool verbose)
1419 const struct nlattr *a;
1421 bool has_ethtype_key = false;
1422 const struct nlattr *ma = NULL;
1424 bool first_field = true;
1426 ofpbuf_init(&ofp, 100);
1427 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1428 bool is_nested_attr;
1429 bool is_wildcard = false;
1430 int attr_type = nl_attr_type(a);
1432 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1433 has_ethtype_key = true;
1436 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1438 if (mask && mask_len) {
1439 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1440 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1443 if (verbose || !is_wildcard || is_nested_attr) {
1444 if (is_wildcard && !ma) {
1445 ma = generate_all_wildcard_mask(&ofp, a);
1448 ds_put_char(ds, ',');
1450 format_odp_key_attr(a, ma, portno_names, ds, verbose);
1451 first_field = false;
1455 ofpbuf_uninit(&ofp);
1460 if (left == key_len) {
1461 ds_put_cstr(ds, "<empty>");
1463 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1464 for (i = 0; i < left; i++) {
1465 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1467 ds_put_char(ds, ')');
1469 if (!has_ethtype_key) {
1470 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1472 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1473 ntohs(nl_attr_get_be16(ma)));
1477 ds_put_cstr(ds, "<empty>");
1481 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1482 * OVS_KEY_ATTR_* attributes in 'key'. */
1484 odp_flow_key_format(const struct nlattr *key,
1485 size_t key_len, struct ds *ds)
1487 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
1491 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1492 const uint8_t *nd_tll, struct ofpbuf *key)
1495 memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1499 memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1502 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1506 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1507 const uint8_t *nd_tll, struct ofpbuf *key)
1509 struct ovs_key_nd nd_key;
1511 memset(&nd_key, 0, sizeof nd_key);
1513 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1517 put_nd(&nd_key, nd_sll, nd_tll, key);
1522 put_nd_mask(int n, const char *nd_target_s,
1523 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1525 struct ovs_key_nd nd_mask;
1527 memset(&nd_mask, 0xff, sizeof nd_mask);
1529 if (strlen(nd_target_s) != 0 &&
1530 inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1534 put_nd(&nd_mask, nd_sll, nd_tll, mask);
1539 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1541 if (!strcasecmp(s, "no")) {
1542 *type = OVS_FRAG_TYPE_NONE;
1543 } else if (!strcasecmp(s, "first")) {
1544 *type = OVS_FRAG_TYPE_FIRST;
1545 } else if (!strcasecmp(s, "later")) {
1546 *type = OVS_FRAG_TYPE_LATER;
1554 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1556 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1557 (mpls_tc << MPLS_TC_SHIFT) |
1558 (mpls_ttl << MPLS_TTL_SHIFT) |
1559 (mpls_bos << MPLS_BOS_SHIFT)));
1563 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1564 struct ofpbuf *key, struct ofpbuf *mask)
1566 /* Many of the sscanf calls in this function use oversized destination
1567 * fields because some sscanf() implementations truncate the range of %i
1568 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1569 * value of 0x7fff. The other alternatives are to allow only a single
1570 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1573 * The tun_id parser has to use an alternative approach because there is no
1574 * type larger than 64 bits. */
1577 unsigned long long int priority;
1578 unsigned long long int priority_mask;
1581 if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1582 &priority_mask, &n) > 0 && n > 0) {
1583 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1584 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1586 } else if (sscanf(s, "skb_priority(%lli)%n",
1587 &priority, &n) > 0 && n > 0) {
1588 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1590 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1597 unsigned long long int mark;
1598 unsigned long long int mark_mask;
1601 if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1602 &mark_mask, &n) > 0 && n > 0) {
1603 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1604 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1606 } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1607 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1609 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1617 int tos, tos_mask, ttl, ttl_mask;
1618 struct flow_tnl tun_key, tun_key_mask;
1619 unsigned long long tun_id_mask;
1622 if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1623 "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1624 "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1625 tun_id_s, &tun_id_mask,
1626 IP_SCAN_ARGS(&tun_key.ip_src),
1627 IP_SCAN_ARGS(&tun_key_mask.ip_src),
1628 IP_SCAN_ARGS(&tun_key.ip_dst),
1629 IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1630 &tos, &tos_mask, &ttl, &ttl_mask,
1635 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1636 tun_key_mask.tun_id = htonll(tun_id_mask);
1637 tun_key.ip_tos = tos;
1638 tun_key_mask.ip_tos = tos_mask;
1639 tun_key.ip_ttl = ttl;
1640 tun_key_mask.ip_ttl = ttl_mask;
1641 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1642 tun_key.flags = flags;
1643 tun_key_mask.flags = UINT16_MAX;
1653 tun_key_to_attr(key, &tun_key);
1655 tun_key_to_attr(mask, &tun_key_mask);
1658 } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1659 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1660 ",tos=%i,ttl=%i,flags%n", tun_id_s,
1661 IP_SCAN_ARGS(&tun_key.ip_src),
1662 IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1667 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1668 tun_key.ip_tos = tos;
1669 tun_key.ip_ttl = ttl;
1670 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1671 tun_key.flags = flags;
1681 tun_key_to_attr(key, &tun_key);
1684 memset(&tun_key, 0xff, sizeof tun_key);
1685 tun_key_to_attr(mask, &tun_key);
1692 unsigned long long int in_port;
1693 unsigned long long int in_port_mask;
1696 if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1697 &in_port_mask, &n) > 0 && n > 0) {
1698 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1699 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1701 } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1702 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1704 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1711 if (port_names && !strncmp(s, "in_port(", 8)) {
1713 const struct simap_node *node;
1717 name_len = strcspn(name, ")");
1718 node = simap_find_len(port_names, name, name_len);
1720 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1723 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1725 return 8 + name_len + 1;
1730 struct ovs_key_ethernet eth_key;
1731 struct ovs_key_ethernet eth_key_mask;
1734 if (mask && sscanf(s,
1735 "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1736 "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1737 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1738 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1739 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1740 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1742 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1743 ð_key, sizeof eth_key);
1744 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1745 ð_key_mask, sizeof eth_key_mask);
1747 } else if (sscanf(s,
1748 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1749 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1750 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1751 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1752 ð_key, sizeof eth_key);
1755 memset(ð_key, 0xff, sizeof eth_key);
1756 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1757 ð_key, sizeof eth_key);
1764 uint16_t vid, vid_mask;
1769 if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1770 &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1771 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1772 htons((vid << VLAN_VID_SHIFT) |
1773 (pcp << VLAN_PCP_SHIFT) |
1775 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1776 htons((vid_mask << VLAN_VID_SHIFT) |
1777 (pcp_mask << VLAN_PCP_SHIFT) |
1778 (1 << VLAN_CFI_SHIFT)));
1780 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1781 &vid, &pcp, &n) > 0 && n > 0)) {
1782 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1783 htons((vid << VLAN_VID_SHIFT) |
1784 (pcp << VLAN_PCP_SHIFT) |
1787 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1790 } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1791 &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1792 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1793 htons((vid << VLAN_VID_SHIFT) |
1794 (pcp << VLAN_PCP_SHIFT) |
1795 (cfi ? VLAN_CFI : 0)));
1796 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1797 htons((vid_mask << VLAN_VID_SHIFT) |
1798 (pcp_mask << VLAN_PCP_SHIFT) |
1799 (cfi_mask << VLAN_CFI_SHIFT)));
1801 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1802 &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1803 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1804 htons((vid << VLAN_VID_SHIFT) |
1805 (pcp << VLAN_PCP_SHIFT) |
1806 (cfi ? VLAN_CFI : 0)));
1808 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1819 if (mask && sscanf(s, "eth_type(%i/%i)%n",
1820 ð_type, ð_type_mask, &n) > 0 && n > 0) {
1821 if (eth_type != 0) {
1822 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1824 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1826 } else if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
1827 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1829 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
1836 int label, tc, ttl, bos;
1837 int label_mask, tc_mask, ttl_mask, bos_mask;
1840 if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1841 &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1842 struct ovs_key_mpls *mpls, *mpls_mask;
1844 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1846 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1848 mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1850 mpls_mask->mpls_lse = mpls_lse_from_components(
1851 label_mask, tc_mask, ttl_mask, bos_mask);
1853 } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1854 &label, &tc, &ttl, &bos, &n) > 0 &&
1856 struct ovs_key_mpls *mpls;
1858 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1860 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1862 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1864 mpls->mpls_lse = OVS_BE32_MAX;
1872 ovs_be32 ipv4_src, ipv4_src_mask;
1873 ovs_be32 ipv4_dst, ipv4_dst_mask;
1874 int ipv4_proto, ipv4_proto_mask;
1875 int ipv4_tos, ipv4_tos_mask;
1876 int ipv4_ttl, ipv4_ttl_mask;
1879 enum ovs_frag_type ipv4_frag;
1882 if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1883 "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1884 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1885 "frag=%7[a-z]/%i)%n",
1886 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1887 IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1888 &ipv4_proto, &ipv4_proto_mask,
1889 &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1890 frag, &ipv4_frag_mask, &n) > 0
1892 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1893 struct ovs_key_ipv4 ipv4_key;
1894 struct ovs_key_ipv4 ipv4_mask;
1896 ipv4_key.ipv4_src = ipv4_src;
1897 ipv4_key.ipv4_dst = ipv4_dst;
1898 ipv4_key.ipv4_proto = ipv4_proto;
1899 ipv4_key.ipv4_tos = ipv4_tos;
1900 ipv4_key.ipv4_ttl = ipv4_ttl;
1901 ipv4_key.ipv4_frag = ipv4_frag;
1902 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1903 &ipv4_key, sizeof ipv4_key);
1905 ipv4_mask.ipv4_src = ipv4_src_mask;
1906 ipv4_mask.ipv4_dst = ipv4_dst_mask;
1907 ipv4_mask.ipv4_proto = ipv4_proto_mask;
1908 ipv4_mask.ipv4_tos = ipv4_tos_mask;
1909 ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1910 ipv4_mask.ipv4_frag = ipv4_frag_mask;
1911 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1912 &ipv4_mask, sizeof ipv4_mask);
1914 } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1915 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1916 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1917 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1919 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1920 struct ovs_key_ipv4 ipv4_key;
1922 ipv4_key.ipv4_src = ipv4_src;
1923 ipv4_key.ipv4_dst = ipv4_dst;
1924 ipv4_key.ipv4_proto = ipv4_proto;
1925 ipv4_key.ipv4_tos = ipv4_tos;
1926 ipv4_key.ipv4_ttl = ipv4_ttl;
1927 ipv4_key.ipv4_frag = ipv4_frag;
1928 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1929 &ipv4_key, sizeof ipv4_key);
1932 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1933 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1934 &ipv4_key, sizeof ipv4_key);
1941 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1942 char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1943 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1944 char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1945 int ipv6_label, ipv6_label_mask;
1946 int ipv6_proto, ipv6_proto_mask;
1947 int ipv6_tclass, ipv6_tclass_mask;
1948 int ipv6_hlimit, ipv6_hlimit_mask;
1950 enum ovs_frag_type ipv6_frag;
1954 if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1955 IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1956 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1957 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1958 ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1959 &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1960 &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1961 &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1962 &ipv6_frag_mask, &n) > 0
1964 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1965 struct ovs_key_ipv6 ipv6_key;
1966 struct ovs_key_ipv6 ipv6_mask;
1968 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1969 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1970 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1971 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1975 ipv6_key.ipv6_label = htonl(ipv6_label);
1976 ipv6_key.ipv6_proto = ipv6_proto;
1977 ipv6_key.ipv6_tclass = ipv6_tclass;
1978 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1979 ipv6_key.ipv6_frag = ipv6_frag;
1980 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1981 &ipv6_key, sizeof ipv6_key);
1983 ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1984 ipv6_mask.ipv6_proto = ipv6_proto_mask;
1985 ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1986 ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1987 ipv6_mask.ipv6_frag = ipv6_frag_mask;
1988 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1989 &ipv6_mask, sizeof ipv6_mask);
1991 } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1992 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1993 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1994 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1996 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1997 struct ovs_key_ipv6 ipv6_key;
1999 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
2000 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
2003 ipv6_key.ipv6_label = htonl(ipv6_label);
2004 ipv6_key.ipv6_proto = ipv6_proto;
2005 ipv6_key.ipv6_tclass = ipv6_tclass;
2006 ipv6_key.ipv6_hlimit = ipv6_hlimit;
2007 ipv6_key.ipv6_frag = ipv6_frag;
2008 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
2009 &ipv6_key, sizeof ipv6_key);
2012 memset(&ipv6_key, 0xff, sizeof ipv6_key);
2013 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
2014 &ipv6_key, sizeof ipv6_key);
2027 if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
2028 &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
2030 struct ovs_key_tcp tcp_key;
2031 struct ovs_key_tcp tcp_mask;
2033 tcp_key.tcp_src = htons(tcp_src);
2034 tcp_key.tcp_dst = htons(tcp_dst);
2035 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2037 tcp_mask.tcp_src = htons(tcp_src_mask);
2038 tcp_mask.tcp_dst = htons(tcp_dst_mask);
2039 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2040 &tcp_mask, sizeof tcp_mask);
2042 } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
2044 struct ovs_key_tcp tcp_key;
2046 tcp_key.tcp_src = htons(tcp_src);
2047 tcp_key.tcp_dst = htons(tcp_dst);
2048 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2051 memset(&tcp_key, 0xff, sizeof tcp_key);
2052 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2053 &tcp_key, sizeof tcp_key);
2060 uint16_t tcp_flags, tcp_flags_mask;
2063 if (mask && sscanf(s, "tcp_flags(%"SCNi16"/%"SCNi16")%n",
2064 &tcp_flags, &tcp_flags_mask, &n) > 0 && n > 0) {
2065 nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags));
2066 nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags_mask));
2068 } else if (sscanf(s, "tcp_flags(%"SCNi16")%n", &tcp_flags, &n) > 0 && n > 0) {
2069 nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags));
2071 nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS,
2085 if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
2086 &udp_src, &udp_src_mask,
2087 &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
2088 struct ovs_key_udp udp_key;
2089 struct ovs_key_udp udp_mask;
2091 udp_key.udp_src = htons(udp_src);
2092 udp_key.udp_dst = htons(udp_dst);
2093 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2095 udp_mask.udp_src = htons(udp_src_mask);
2096 udp_mask.udp_dst = htons(udp_dst_mask);
2097 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2098 &udp_mask, sizeof udp_mask);
2101 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
2103 struct ovs_key_udp udp_key;
2105 udp_key.udp_src = htons(udp_src);
2106 udp_key.udp_dst = htons(udp_dst);
2107 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2110 memset(&udp_key, 0xff, sizeof udp_key);
2111 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2124 if (mask && sscanf(s, "sctp(src=%i/%i,dst=%i/%i)%n",
2125 &sctp_src, &sctp_src_mask,
2126 &sctp_dst, &sctp_dst_mask, &n) > 0 && n > 0) {
2127 struct ovs_key_sctp sctp_key;
2128 struct ovs_key_sctp sctp_mask;
2130 sctp_key.sctp_src = htons(sctp_src);
2131 sctp_key.sctp_dst = htons(sctp_dst);
2132 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2134 sctp_mask.sctp_src = htons(sctp_src_mask);
2135 sctp_mask.sctp_dst = htons(sctp_dst_mask);
2136 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP,
2137 &sctp_mask, sizeof sctp_mask);
2140 if (sscanf(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n) > 0
2142 struct ovs_key_sctp sctp_key;
2144 sctp_key.sctp_src = htons(sctp_src);
2145 sctp_key.sctp_dst = htons(sctp_dst);
2146 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2149 memset(&sctp_key, 0xff, sizeof sctp_key);
2150 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2163 if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2164 &icmp_type, &icmp_type_mask,
2165 &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2166 struct ovs_key_icmp icmp_key;
2167 struct ovs_key_icmp icmp_mask;
2169 icmp_key.icmp_type = icmp_type;
2170 icmp_key.icmp_code = icmp_code;
2171 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2172 &icmp_key, sizeof icmp_key);
2174 icmp_mask.icmp_type = icmp_type_mask;
2175 icmp_mask.icmp_code = icmp_code_mask;
2176 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2177 &icmp_mask, sizeof icmp_mask);
2179 } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2180 &icmp_type, &icmp_code, &n) > 0
2182 struct ovs_key_icmp icmp_key;
2184 icmp_key.icmp_type = icmp_type;
2185 icmp_key.icmp_code = icmp_code;
2186 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2187 &icmp_key, sizeof icmp_key);
2189 memset(&icmp_key, 0xff, sizeof icmp_key);
2190 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2198 struct ovs_key_icmpv6 icmpv6_key;
2199 struct ovs_key_icmpv6 icmpv6_mask;
2200 int icmpv6_type_mask;
2201 int icmpv6_code_mask;
2204 if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2205 &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2206 &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2208 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2209 &icmpv6_key, sizeof icmpv6_key);
2211 icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2212 icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2213 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2214 sizeof icmpv6_mask);
2216 } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2217 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2219 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2220 &icmpv6_key, sizeof icmpv6_key);
2223 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2224 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2232 ovs_be32 arp_sip, arp_sip_mask;
2233 ovs_be32 arp_tip, arp_tip_mask;
2234 int arp_op, arp_op_mask;
2235 uint8_t arp_sha[ETH_ADDR_LEN];
2236 uint8_t arp_sha_mask[ETH_ADDR_LEN];
2237 uint8_t arp_tha[ETH_ADDR_LEN];
2238 uint8_t arp_tha_mask[ETH_ADDR_LEN];
2241 if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2242 "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2243 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2244 "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2245 IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2246 IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2247 &arp_op, &arp_op_mask,
2248 ETH_ADDR_SCAN_ARGS(arp_sha),
2249 ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2250 ETH_ADDR_SCAN_ARGS(arp_tha),
2251 ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2252 struct ovs_key_arp arp_key;
2253 struct ovs_key_arp arp_mask;
2255 memset(&arp_key, 0, sizeof arp_key);
2256 arp_key.arp_sip = arp_sip;
2257 arp_key.arp_tip = arp_tip;
2258 arp_key.arp_op = htons(arp_op);
2259 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2260 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2261 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2263 arp_mask.arp_sip = arp_sip_mask;
2264 arp_mask.arp_tip = arp_tip_mask;
2265 arp_mask.arp_op = htons(arp_op_mask);
2266 memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2267 memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2268 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2269 &arp_mask, sizeof arp_mask);
2271 } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2272 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2273 IP_SCAN_ARGS(&arp_sip),
2274 IP_SCAN_ARGS(&arp_tip),
2276 ETH_ADDR_SCAN_ARGS(arp_sha),
2277 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2278 struct ovs_key_arp arp_key;
2280 memset(&arp_key, 0, sizeof arp_key);
2281 arp_key.arp_sip = arp_sip;
2282 arp_key.arp_tip = arp_tip;
2283 arp_key.arp_op = htons(arp_op);
2284 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2285 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2286 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2289 memset(&arp_key, 0xff, sizeof arp_key);
2290 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2291 &arp_key, sizeof arp_key);
2298 char nd_target_s[IPV6_SCAN_LEN + 1];
2299 char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2300 uint8_t nd_sll[ETH_ADDR_LEN];
2301 uint8_t nd_sll_mask[ETH_ADDR_LEN];
2302 uint8_t nd_tll[ETH_ADDR_LEN];
2303 uint8_t nd_tll_mask[ETH_ADDR_LEN];
2306 nd_target_mask_s[0] = 0;
2307 memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2308 memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2310 if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2311 nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2312 put_nd_key(n, nd_target_s, NULL, NULL, key);
2313 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2314 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2315 nd_target_s, &n) > 0 && n > 0) {
2316 put_nd_key(n, nd_target_s, NULL, NULL, key);
2318 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2320 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2321 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2322 nd_target_s, nd_target_mask_s,
2323 ETH_ADDR_SCAN_ARGS(nd_sll),
2324 ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2325 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2326 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2327 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2328 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2330 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2332 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2334 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2335 ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2336 nd_target_s, nd_target_mask_s,
2337 ETH_ADDR_SCAN_ARGS(nd_tll),
2338 ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2339 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2340 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2341 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2342 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2344 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2346 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2348 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2349 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2350 "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2351 nd_target_s, nd_target_mask_s,
2352 ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2353 ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2356 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2357 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2358 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2359 "tll="ETH_ADDR_SCAN_FMT")%n",
2360 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2361 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2363 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2365 put_nd_mask(n, nd_target_mask_s,
2366 nd_sll_mask, nd_tll_mask, mask);
2375 if (!strncmp(s, "encap(", 6)) {
2376 const char *start = s;
2377 size_t encap, encap_mask = 0;
2379 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2381 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2388 s += strspn(s, ", \t\r\n");
2391 } else if (*s == ')') {
2395 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2403 nl_msg_end_nested(key, encap);
2405 nl_msg_end_nested(mask, encap_mask);
2414 /* Parses the string representation of a datapath flow key, in the
2415 * format output by odp_flow_key_format(). Returns 0 if successful,
2416 * otherwise a positive errno value. On success, the flow key is
2417 * appended to 'key' as a series of Netlink attributes. On failure, no
2418 * data is appended to 'key'. Either way, 'key''s data might be
2421 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2422 * to a port number. (Port names may be used instead of port numbers in
2425 * On success, the attributes appended to 'key' are individually syntactically
2426 * valid, but they may not be valid as a sequence. 'key' might, for example,
2427 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2429 odp_flow_from_string(const char *s, const struct simap *port_names,
2430 struct ofpbuf *key, struct ofpbuf *mask)
2432 const size_t old_size = key->size;
2436 s += strspn(s, delimiters);
2441 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2443 key->size = old_size;
2453 ovs_to_odp_frag(uint8_t nw_frag)
2455 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2456 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2457 : OVS_FRAG_TYPE_LATER);
2461 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2463 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2465 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2466 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2472 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2473 const struct flow *flow, odp_port_t odp_in_port)
2476 struct ovs_key_ethernet *eth_key;
2479 /* We assume that if 'data' and 'flow' are not the same, we should
2480 * treat 'data' as a mask. */
2481 is_mask = (data != flow);
2483 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2485 if (flow->tunnel.ip_dst || is_mask) {
2486 tun_key_to_attr(buf, &data->tunnel);
2489 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2491 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2492 * is not the magical value "ODPP_NONE". */
2493 if (is_mask || odp_in_port != ODPP_NONE) {
2494 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2497 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2499 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2500 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2502 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2504 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2506 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2508 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2509 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2510 if (flow->vlan_tci == htons(0)) {
2517 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2518 /* For backwards compatibility with kernels that don't support
2519 * wildcarding, the following convention is used to encode the
2520 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2523 * -------- -------- -------
2524 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2525 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2526 * <none> 0xffff Any non-Ethernet II frame (except valid
2527 * 802.3 SNAP packet with valid eth_type).
2530 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2535 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2537 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2538 struct ovs_key_ipv4 *ipv4_key;
2540 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2542 ipv4_key->ipv4_src = data->nw_src;
2543 ipv4_key->ipv4_dst = data->nw_dst;
2544 ipv4_key->ipv4_proto = data->nw_proto;
2545 ipv4_key->ipv4_tos = data->nw_tos;
2546 ipv4_key->ipv4_ttl = data->nw_ttl;
2547 ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2548 : ovs_to_odp_frag(data->nw_frag);
2549 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2550 struct ovs_key_ipv6 *ipv6_key;
2552 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2554 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2555 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2556 ipv6_key->ipv6_label = data->ipv6_label;
2557 ipv6_key->ipv6_proto = data->nw_proto;
2558 ipv6_key->ipv6_tclass = data->nw_tos;
2559 ipv6_key->ipv6_hlimit = data->nw_ttl;
2560 ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2561 : ovs_to_odp_frag(data->nw_frag);
2562 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2563 flow->dl_type == htons(ETH_TYPE_RARP)) {
2564 struct ovs_key_arp *arp_key;
2566 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2568 memset(arp_key, 0, sizeof *arp_key);
2569 arp_key->arp_sip = data->nw_src;
2570 arp_key->arp_tip = data->nw_dst;
2571 arp_key->arp_op = htons(data->nw_proto);
2572 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2573 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2574 } else if (eth_type_mpls(flow->dl_type)) {
2575 struct ovs_key_mpls *mpls_key;
2577 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2579 mpls_key->mpls_lse = data->mpls_lse;
2582 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2583 if (flow->nw_proto == IPPROTO_TCP) {
2584 struct ovs_key_tcp *tcp_key;
2586 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2588 tcp_key->tcp_src = data->tp_src;
2589 tcp_key->tcp_dst = data->tp_dst;
2591 if (data->tcp_flags) {
2592 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2594 } else if (flow->nw_proto == IPPROTO_UDP) {
2595 struct ovs_key_udp *udp_key;
2597 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2599 udp_key->udp_src = data->tp_src;
2600 udp_key->udp_dst = data->tp_dst;
2601 } else if (flow->nw_proto == IPPROTO_SCTP) {
2602 struct ovs_key_sctp *sctp_key;
2604 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2606 sctp_key->sctp_src = data->tp_src;
2607 sctp_key->sctp_dst = data->tp_dst;
2608 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2609 && flow->nw_proto == IPPROTO_ICMP) {
2610 struct ovs_key_icmp *icmp_key;
2612 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2614 icmp_key->icmp_type = ntohs(data->tp_src);
2615 icmp_key->icmp_code = ntohs(data->tp_dst);
2616 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2617 && flow->nw_proto == IPPROTO_ICMPV6) {
2618 struct ovs_key_icmpv6 *icmpv6_key;
2620 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2621 sizeof *icmpv6_key);
2622 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2623 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2625 if (flow->tp_dst == htons(0) &&
2626 (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2627 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) &&
2628 (!is_mask || (data->tp_src == htons(0xffff) &&
2629 data->tp_dst == htons(0xffff)))) {
2631 struct ovs_key_nd *nd_key;
2633 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2635 memcpy(nd_key->nd_target, &data->nd_target,
2636 sizeof nd_key->nd_target);
2637 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2638 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2645 nl_msg_end_nested(buf, encap);
2649 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2650 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2651 * number rather than a datapath port number). Instead, if 'odp_in_port'
2652 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2655 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2656 * capable of being expanded to allow for that much space. */
2658 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2659 odp_port_t odp_in_port)
2661 odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2664 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2665 * 'buf'. 'flow' is used as a template to determine how to interpret
2666 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2667 * it doesn't indicate whether the other fields should be interpreted as
2668 * ARP, IPv4, IPv6, etc.
2670 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2671 * capable of being expanded to allow for that much space. */
2673 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2674 const struct flow *flow, uint32_t odp_in_port_mask)
2676 odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask));
2680 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2682 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2683 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2684 key_len / sizeof(uint32_t), 0);
2688 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2689 uint64_t attrs, int out_of_range_attr,
2690 const struct nlattr *key, size_t key_len)
2695 if (VLOG_DROP_DBG(rl)) {
2700 for (i = 0; i < 64; i++) {
2701 if (attrs & (UINT64_C(1) << i)) {
2702 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2704 ds_put_format(&s, " %s",
2705 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2708 if (out_of_range_attr) {
2709 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2712 ds_put_cstr(&s, ": ");
2713 odp_flow_key_format(key, key_len, &s);
2715 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2720 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2722 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2724 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2725 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2729 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2730 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2731 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2732 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2739 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2740 const struct nlattr *attrs[], uint64_t *present_attrsp,
2741 int *out_of_range_attrp)
2743 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2744 const struct nlattr *nla;
2745 uint64_t present_attrs;
2748 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2750 *out_of_range_attrp = 0;
2751 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2752 uint16_t type = nl_attr_type(nla);
2753 size_t len = nl_attr_get_size(nla);
2754 int expected_len = odp_flow_key_attr_len(type);
2756 if (len != expected_len && expected_len >= 0) {
2757 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2759 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2760 "length %d", ovs_key_attr_to_string(type, namebuf,
2766 if (type > OVS_KEY_ATTR_MAX) {
2767 *out_of_range_attrp = type;
2769 if (present_attrs & (UINT64_C(1) << type)) {
2770 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2772 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2773 ovs_key_attr_to_string(type,
2774 namebuf, sizeof namebuf));
2778 present_attrs |= UINT64_C(1) << type;
2783 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2787 *present_attrsp = present_attrs;
2791 static enum odp_key_fitness
2792 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2793 uint64_t expected_attrs,
2794 const struct nlattr *key, size_t key_len)
2796 uint64_t missing_attrs;
2797 uint64_t extra_attrs;
2799 missing_attrs = expected_attrs & ~present_attrs;
2800 if (missing_attrs) {
2801 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2802 log_odp_key_attributes(&rl, "expected but not present",
2803 missing_attrs, 0, key, key_len);
2804 return ODP_FIT_TOO_LITTLE;
2807 extra_attrs = present_attrs & ~expected_attrs;
2808 if (extra_attrs || out_of_range_attr) {
2809 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2810 log_odp_key_attributes(&rl, "present but not expected",
2811 extra_attrs, out_of_range_attr, key, key_len);
2812 return ODP_FIT_TOO_MUCH;
2815 return ODP_FIT_PERFECT;
2819 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2820 uint64_t present_attrs, uint64_t *expected_attrs,
2821 struct flow *flow, const struct flow *src_flow)
2823 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2824 bool is_mask = flow != src_flow;
2826 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2827 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2828 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2829 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2830 ntohs(flow->dl_type));
2833 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2834 flow->dl_type != htons(0xffff)) {
2837 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2840 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2841 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2842 /* See comments in odp_flow_key_from_flow__(). */
2843 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2850 static enum odp_key_fitness
2851 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2852 uint64_t present_attrs, int out_of_range_attr,
2853 uint64_t expected_attrs, struct flow *flow,
2854 const struct nlattr *key, size_t key_len,
2855 const struct flow *src_flow)
2857 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2858 bool is_mask = src_flow != flow;
2859 const void *check_start = NULL;
2860 size_t check_len = 0;
2861 enum ovs_key_attr expected_bit = 0xff;
2863 if (eth_type_mpls(src_flow->dl_type)) {
2865 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2867 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2868 return ODP_FIT_TOO_LITTLE;
2870 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2871 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
2872 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2874 if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) {
2875 return ODP_FIT_ERROR;
2877 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2880 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
2882 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2884 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2885 const struct ovs_key_ipv4 *ipv4_key;
2887 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2888 flow->nw_src = ipv4_key->ipv4_src;
2889 flow->nw_dst = ipv4_key->ipv4_dst;
2890 flow->nw_proto = ipv4_key->ipv4_proto;
2891 flow->nw_tos = ipv4_key->ipv4_tos;
2892 flow->nw_ttl = ipv4_key->ipv4_ttl;
2894 flow->nw_frag = ipv4_key->ipv4_frag;
2895 check_start = ipv4_key;
2896 check_len = sizeof *ipv4_key;
2897 expected_bit = OVS_KEY_ATTR_IPV4;
2898 } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2899 return ODP_FIT_ERROR;
2902 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
2904 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2906 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2907 const struct ovs_key_ipv6 *ipv6_key;
2909 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2910 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2911 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2912 flow->ipv6_label = ipv6_key->ipv6_label;
2913 flow->nw_proto = ipv6_key->ipv6_proto;
2914 flow->nw_tos = ipv6_key->ipv6_tclass;
2915 flow->nw_ttl = ipv6_key->ipv6_hlimit;
2917 flow->nw_frag = ipv6_key->ipv6_frag;
2918 check_start = ipv6_key;
2919 check_len = sizeof *ipv6_key;
2920 expected_bit = OVS_KEY_ATTR_IPV6;
2921 } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2922 return ODP_FIT_ERROR;
2925 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
2926 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
2928 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2930 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2931 const struct ovs_key_arp *arp_key;
2933 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2934 flow->nw_src = arp_key->arp_sip;
2935 flow->nw_dst = arp_key->arp_tip;
2936 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
2937 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2938 "key", ntohs(arp_key->arp_op));
2939 return ODP_FIT_ERROR;
2941 flow->nw_proto = ntohs(arp_key->arp_op);
2942 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2943 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2946 check_start = arp_key;
2947 check_len = sizeof *arp_key;
2948 expected_bit = OVS_KEY_ATTR_ARP;
2955 if (!is_all_zeros(check_start, check_len) &&
2956 flow->dl_type != htons(0xffff)) {
2957 return ODP_FIT_ERROR;
2959 expected_attrs |= UINT64_C(1) << expected_bit;
2963 expected_bit = OVS_KEY_ATTR_UNSPEC;
2964 if (src_flow->nw_proto == IPPROTO_TCP
2965 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2966 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2967 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2969 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2971 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2972 const struct ovs_key_tcp *tcp_key;
2974 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2975 flow->tp_src = tcp_key->tcp_src;
2976 flow->tp_dst = tcp_key->tcp_dst;
2977 expected_bit = OVS_KEY_ATTR_TCP;
2979 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
2980 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
2981 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
2983 } else if (src_flow->nw_proto == IPPROTO_UDP
2984 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2985 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2986 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2988 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2990 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2991 const struct ovs_key_udp *udp_key;
2993 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2994 flow->tp_src = udp_key->udp_src;
2995 flow->tp_dst = udp_key->udp_dst;
2996 expected_bit = OVS_KEY_ATTR_UDP;
2998 } else if (src_flow->nw_proto == IPPROTO_SCTP
2999 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3000 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3001 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3003 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3005 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3006 const struct ovs_key_sctp *sctp_key;
3008 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3009 flow->tp_src = sctp_key->sctp_src;
3010 flow->tp_dst = sctp_key->sctp_dst;
3011 expected_bit = OVS_KEY_ATTR_SCTP;
3013 } else if (src_flow->nw_proto == IPPROTO_ICMP
3014 && src_flow->dl_type == htons(ETH_TYPE_IP)
3015 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3017 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3019 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3020 const struct ovs_key_icmp *icmp_key;
3022 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3023 flow->tp_src = htons(icmp_key->icmp_type);
3024 flow->tp_dst = htons(icmp_key->icmp_code);
3025 expected_bit = OVS_KEY_ATTR_ICMP;
3027 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3028 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3029 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3031 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3033 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3034 const struct ovs_key_icmpv6 *icmpv6_key;
3036 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3037 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3038 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3039 expected_bit = OVS_KEY_ATTR_ICMPV6;
3040 if (src_flow->tp_dst == htons(0) &&
3041 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3042 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3044 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3046 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3047 const struct ovs_key_nd *nd_key;
3049 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3050 memcpy(&flow->nd_target, nd_key->nd_target,
3051 sizeof flow->nd_target);
3052 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3053 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3055 if (!is_all_zeros((const uint8_t *) nd_key,
3057 (flow->tp_src != htons(0xffff) ||
3058 flow->tp_dst != htons(0xffff))) {
3059 return ODP_FIT_ERROR;
3061 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3068 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3069 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3070 return ODP_FIT_ERROR;
3072 expected_attrs |= UINT64_C(1) << expected_bit;
3077 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3081 /* Parse 802.1Q header then encapsulated L3 attributes. */
3082 static enum odp_key_fitness
3083 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3084 uint64_t present_attrs, int out_of_range_attr,
3085 uint64_t expected_attrs, struct flow *flow,
3086 const struct nlattr *key, size_t key_len,
3087 const struct flow *src_flow)
3089 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3090 bool is_mask = src_flow != flow;
3092 const struct nlattr *encap
3093 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3094 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3095 enum odp_key_fitness encap_fitness;
3096 enum odp_key_fitness fitness;
3099 /* Calculate fitness of outer attributes. */
3101 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3102 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3104 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3105 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3107 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3108 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3111 fitness = check_expectations(present_attrs, out_of_range_attr,
3112 expected_attrs, key, key_len);
3114 /* Get the VLAN TCI value. */
3115 if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3116 return ODP_FIT_TOO_LITTLE;
3118 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3120 if (tci == htons(0)) {
3121 /* Corner case for a truncated 802.1Q header. */
3122 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3123 return ODP_FIT_TOO_MUCH;
3126 } else if (!(tci & htons(VLAN_CFI))) {
3127 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3128 "but CFI bit is not set", ntohs(tci));
3129 return ODP_FIT_ERROR;
3133 * Remove the TPID from dl_type since it's not the real Ethertype. */
3134 flow->dl_type = htons(0);
3135 flow->vlan_tci = tci;
3138 if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3141 /* Now parse the encapsulated attributes. */
3142 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3143 attrs, &present_attrs, &out_of_range_attr)) {
3144 return ODP_FIT_ERROR;
3148 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3149 return ODP_FIT_ERROR;
3151 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3152 expected_attrs, flow, key, key_len,
3155 /* The overall fitness is the worse of the outer and inner attributes. */
3156 return MAX(fitness, encap_fitness);
3159 static enum odp_key_fitness
3160 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3161 struct flow *flow, const struct flow *src_flow)
3163 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3164 uint64_t expected_attrs;
3165 uint64_t present_attrs;
3166 int out_of_range_attr;
3167 bool is_mask = src_flow != flow;
3169 memset(flow, 0, sizeof *flow);
3171 /* Parse attributes. */
3172 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3173 &out_of_range_attr)) {
3174 return ODP_FIT_ERROR;
3179 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3180 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3181 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3184 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3185 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3186 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3189 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3190 enum odp_key_fitness res;
3192 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3193 if (res == ODP_FIT_ERROR) {
3194 return ODP_FIT_ERROR;
3195 } else if (res == ODP_FIT_PERFECT) {
3196 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3200 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3201 flow->in_port.odp_port
3202 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3203 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3204 } else if (!is_mask) {
3205 flow->in_port.odp_port = ODPP_NONE;
3208 /* Ethernet header. */
3209 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3210 const struct ovs_key_ethernet *eth_key;
3212 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3213 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3214 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3216 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3220 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3223 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3224 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3226 return ODP_FIT_ERROR;
3229 if ((is_mask && (src_flow->vlan_tci & htons(VLAN_CFI))) ||
3230 (!is_mask && src_flow->dl_type == htons(ETH_TYPE_VLAN))) {
3231 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3232 expected_attrs, flow, key, key_len, src_flow);
3235 flow->vlan_tci = htons(0xffff);
3236 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3237 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3238 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3241 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3242 expected_attrs, flow, key, key_len, src_flow);
3245 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3246 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3247 * 'key' fits our expectations for what a flow key should contain.
3249 * The 'in_port' will be the datapath's understanding of the port. The
3250 * caller will need to translate with odp_port_to_ofp_port() if the
3251 * OpenFlow port is needed.
3253 * This function doesn't take the packet itself as an argument because none of
3254 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3255 * it is always possible to infer which additional attribute(s) should appear
3256 * by looking at the attributes for lower-level protocols, e.g. if the network
3257 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3258 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3259 * must be absent. */
3260 enum odp_key_fitness
3261 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3264 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3267 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3268 * structure in 'mask'. 'flow' must be a previously translated flow
3269 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3270 * 'key' fits our expectations for what a flow key should contain. */
3271 enum odp_key_fitness
3272 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3273 struct flow *mask, const struct flow *flow)
3275 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3278 /* Returns 'fitness' as a string, for use in debug messages. */
3280 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3283 case ODP_FIT_PERFECT:
3285 case ODP_FIT_TOO_MUCH:
3287 case ODP_FIT_TOO_LITTLE:
3288 return "too_little";
3296 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3297 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3298 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3299 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3300 * null, then the return value is not meaningful.) */
3302 odp_put_userspace_action(uint32_t pid,
3303 const void *userdata, size_t userdata_size,
3304 struct ofpbuf *odp_actions)
3306 size_t userdata_ofs;
3309 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3310 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3312 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3313 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3314 userdata, userdata_size);
3318 nl_msg_end_nested(odp_actions, offset);
3320 return userdata_ofs;
3324 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3325 struct ofpbuf *odp_actions)
3327 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3328 tun_key_to_attr(odp_actions, tunnel);
3329 nl_msg_end_nested(odp_actions, offset);
3332 /* The commit_odp_actions() function and its helpers. */
3335 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3336 const void *key, size_t key_size)
3338 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3339 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3340 nl_msg_end_nested(odp_actions, offset);
3344 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3345 struct ofpbuf *odp_actions)
3347 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3351 /* If any of the flow key data that ODP actions can modify are different in
3352 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3353 * 'odp_actions' that change the flow tunneling information in key from
3354 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3355 * same way. In other words, operates the same as commit_odp_actions(), but
3356 * only on tunneling information. */
3358 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3359 struct ofpbuf *odp_actions)
3361 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3362 if (flow->tunnel.ip_dst) {
3363 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3366 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3367 odp_put_tunnel_action(&base->tunnel, odp_actions);
3372 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3373 struct ofpbuf *odp_actions,
3374 struct flow_wildcards *wc)
3376 struct ovs_key_ethernet eth_key;
3378 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3379 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3383 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3384 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3386 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3387 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3389 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3390 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3392 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3393 ð_key, sizeof(eth_key));
3397 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3398 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3400 if (base->vlan_tci == vlan_tci) {
3404 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3406 if (base->vlan_tci & htons(VLAN_CFI)) {
3407 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3410 if (vlan_tci & htons(VLAN_CFI)) {
3411 struct ovs_action_push_vlan vlan;
3413 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3414 vlan.vlan_tci = vlan_tci;
3415 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3416 &vlan, sizeof vlan);
3418 base->vlan_tci = vlan_tci;
3422 commit_mpls_action(const struct flow *flow, struct flow *base,
3423 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
3424 int *mpls_depth_delta)
3426 if (flow->mpls_lse == base->mpls_lse && !*mpls_depth_delta) {
3430 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3432 switch (*mpls_depth_delta) {
3434 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3437 struct ovs_action_push_mpls *mpls;
3439 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3441 memset(mpls, 0, sizeof *mpls);
3442 mpls->mpls_ethertype = flow->dl_type;
3443 mpls->mpls_lse = flow->mpls_lse;
3447 struct ovs_key_mpls mpls_key;
3449 mpls_key.mpls_lse = flow->mpls_lse;
3450 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3451 &mpls_key, sizeof(mpls_key));
3458 base->dl_type = flow->dl_type;
3459 base->mpls_lse = flow->mpls_lse;
3460 *mpls_depth_delta = 0;
3464 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3465 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3467 struct ovs_key_ipv4 ipv4_key;
3469 if (base->nw_src == flow->nw_src &&
3470 base->nw_dst == flow->nw_dst &&
3471 base->nw_tos == flow->nw_tos &&
3472 base->nw_ttl == flow->nw_ttl &&
3473 base->nw_frag == flow->nw_frag) {
3477 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3478 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3479 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3480 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3481 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3482 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3484 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3485 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3486 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3487 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3488 ipv4_key.ipv4_proto = base->nw_proto;
3489 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3491 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3492 &ipv4_key, sizeof(ipv4_key));
3496 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3497 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3499 struct ovs_key_ipv6 ipv6_key;
3501 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3502 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3503 base->ipv6_label == flow->ipv6_label &&
3504 base->nw_tos == flow->nw_tos &&
3505 base->nw_ttl == flow->nw_ttl &&
3506 base->nw_frag == flow->nw_frag) {
3510 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3511 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3512 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3513 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3514 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3515 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3516 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3518 base->ipv6_src = flow->ipv6_src;
3519 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3520 base->ipv6_dst = flow->ipv6_dst;
3521 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3523 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3524 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3525 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3526 ipv6_key.ipv6_proto = base->nw_proto;
3527 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3529 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3530 &ipv6_key, sizeof(ipv6_key));
3533 static enum slow_path_reason
3534 commit_set_arp_action(const struct flow *flow, struct flow *base,
3535 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3537 struct ovs_key_arp arp_key;
3539 if (base->nw_src == flow->nw_src &&
3540 base->nw_dst == flow->nw_dst &&
3541 base->nw_proto == flow->nw_proto &&
3542 eth_addr_equals(base->arp_sha, flow->arp_sha) &&
3543 eth_addr_equals(base->arp_tha, flow->arp_tha)) {
3547 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3548 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3549 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3550 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
3551 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
3553 base->nw_src = flow->nw_src;
3554 base->nw_dst = flow->nw_dst;
3555 base->nw_proto = flow->nw_proto;
3556 memcpy(base->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3557 memcpy(base->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3559 arp_key.arp_sip = base->nw_src;
3560 arp_key.arp_tip = base->nw_dst;
3561 arp_key.arp_op = htons(base->nw_proto);
3562 memcpy(arp_key.arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3563 memcpy(arp_key.arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3565 commit_set_action(odp_actions, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
3570 static enum slow_path_reason
3571 commit_set_nw_action(const struct flow *flow, struct flow *base,
3572 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3574 /* Check if 'flow' really has an L3 header. */
3575 if (!flow->nw_proto) {
3579 switch (ntohs(base->dl_type)) {
3581 commit_set_ipv4_action(flow, base, odp_actions, wc);
3585 commit_set_ipv6_action(flow, base, odp_actions, wc);
3589 return commit_set_arp_action(flow, base, odp_actions, wc);
3596 commit_set_port_action(const struct flow *flow, struct flow *base,
3597 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3599 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3603 if (base->tp_src == flow->tp_src &&
3604 base->tp_dst == flow->tp_dst) {
3608 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3609 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3611 if (flow->nw_proto == IPPROTO_TCP) {
3612 struct ovs_key_tcp port_key;
3614 port_key.tcp_src = base->tp_src = flow->tp_src;
3615 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3617 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3618 &port_key, sizeof(port_key));
3620 } else if (flow->nw_proto == IPPROTO_UDP) {
3621 struct ovs_key_udp port_key;
3623 port_key.udp_src = base->tp_src = flow->tp_src;
3624 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3626 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3627 &port_key, sizeof(port_key));
3628 } else if (flow->nw_proto == IPPROTO_SCTP) {
3629 struct ovs_key_sctp port_key;
3631 port_key.sctp_src = base->tp_src = flow->tp_src;
3632 port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3634 commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3635 &port_key, sizeof(port_key));
3640 commit_set_priority_action(const struct flow *flow, struct flow *base,
3641 struct ofpbuf *odp_actions,
3642 struct flow_wildcards *wc)
3644 if (base->skb_priority == flow->skb_priority) {
3648 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3649 base->skb_priority = flow->skb_priority;
3651 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3652 &base->skb_priority, sizeof(base->skb_priority));
3656 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3657 struct ofpbuf *odp_actions,
3658 struct flow_wildcards *wc)
3660 if (base->pkt_mark == flow->pkt_mark) {
3664 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3665 base->pkt_mark = flow->pkt_mark;
3667 odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3670 /* If any of the flow key data that ODP actions can modify are different in
3671 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3672 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3673 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3674 * in addition to this function if needed. Sets fields in 'wc' that are
3675 * used as part of the action.
3677 * Returns a reason to force processing the flow's packets into the userspace
3678 * slow path, if there is one, otherwise 0. */
3679 enum slow_path_reason
3680 commit_odp_actions(const struct flow *flow, struct flow *base,
3681 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
3682 int *mpls_depth_delta)
3684 enum slow_path_reason slow;
3686 commit_set_ether_addr_action(flow, base, odp_actions, wc);
3687 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
3688 slow = commit_set_nw_action(flow, base, odp_actions, wc);
3689 commit_set_port_action(flow, base, odp_actions, wc);
3690 /* Committing MPLS actions should occur after committing nw and port
3691 * actions. This is because committing MPLS actions may alter a packet so
3692 * that it is no longer IP and thus nw and port actions are no longer valid.
3694 commit_mpls_action(flow, base, odp_actions, wc, mpls_depth_delta);
3695 commit_set_priority_action(flow, base, odp_actions, wc);
3696 commit_set_pkt_mark_action(flow, base, odp_actions, wc);