]> git.proxmox.com Git - ovs.git/blob - lib/odp-util.c
odp-util: Export odp_tun_key_from_attr()
[ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include "odp-util.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "byte-order.h"
28 #include "coverage.h"
29 #include "dynamic-string.h"
30 #include "flow.h"
31 #include "netlink.h"
32 #include "ofpbuf.h"
33 #include "packets.h"
34 #include "simap.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(odp_util);
40
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.
45 */
46
47 /* The set of characters that may separate one action or one key attribute
48 * from another. */
49 static const char *delimiters = ", \t\r\n";
50
51 static int parse_odp_key_attr(const char *, const struct simap *port_names,
52 struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
54
55 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
56 * 'type':
57 *
58 * - For an action whose argument has a fixed length, returned that
59 * nonnegative length in bytes.
60 *
61 * - For an action with a variable-length argument, returns -2.
62 *
63 * - For an invalid 'type', returns -1. */
64 static int
65 odp_action_len(uint16_t type)
66 {
67 if (type > OVS_ACTION_ATTR_MAX) {
68 return -1;
69 }
70
71 switch ((enum ovs_action_attr) type) {
72 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
73 case OVS_ACTION_ATTR_USERSPACE: return -2;
74 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75 case OVS_ACTION_ATTR_POP_VLAN: return 0;
76 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
77 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
78 case OVS_ACTION_ATTR_SET: return -2;
79 case OVS_ACTION_ATTR_SAMPLE: return -2;
80
81 case OVS_ACTION_ATTR_UNSPEC:
82 case __OVS_ACTION_ATTR_MAX:
83 return -1;
84 }
85
86 return -1;
87 }
88
89 static const char *
90 ovs_key_attr_to_string(enum ovs_key_attr attr)
91 {
92 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
93
94 switch (attr) {
95 case OVS_KEY_ATTR_UNSPEC: return "unspec";
96 case OVS_KEY_ATTR_ENCAP: return "encap";
97 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
98 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
99 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
100 case OVS_KEY_ATTR_IN_PORT: return "in_port";
101 case OVS_KEY_ATTR_ETHERNET: return "eth";
102 case OVS_KEY_ATTR_VLAN: return "vlan";
103 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
104 case OVS_KEY_ATTR_IPV4: return "ipv4";
105 case OVS_KEY_ATTR_IPV6: return "ipv6";
106 case OVS_KEY_ATTR_TCP: return "tcp";
107 case OVS_KEY_ATTR_UDP: return "udp";
108 case OVS_KEY_ATTR_ICMP: return "icmp";
109 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
110 case OVS_KEY_ATTR_ARP: return "arp";
111 case OVS_KEY_ATTR_ND: return "nd";
112 case OVS_KEY_ATTR_MPLS: return "mpls";
113
114 case __OVS_KEY_ATTR_MAX:
115 default:
116 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
117 (unsigned int) attr);
118 return unknown_attr;
119 }
120 }
121
122 static void
123 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
124 {
125 size_t len = nl_attr_get_size(a);
126
127 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
128 if (len) {
129 const uint8_t *unspec;
130 unsigned int i;
131
132 unspec = nl_attr_get(a);
133 for (i = 0; i < len; i++) {
134 ds_put_char(ds, i ? ' ': '(');
135 ds_put_format(ds, "%02x", unspec[i]);
136 }
137 ds_put_char(ds, ')');
138 }
139 }
140
141 static void
142 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
143 {
144 static const struct nl_policy ovs_sample_policy[] = {
145 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
146 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
147 };
148 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
149 double percentage;
150 const struct nlattr *nla_acts;
151 int len;
152
153 ds_put_cstr(ds, "sample");
154
155 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
156 ds_put_cstr(ds, "(error)");
157 return;
158 }
159
160 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
161 UINT32_MAX;
162
163 ds_put_format(ds, "(sample=%.1f%%,", percentage);
164
165 ds_put_cstr(ds, "actions(");
166 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
167 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
168 format_odp_actions(ds, nla_acts, len);
169 ds_put_format(ds, "))");
170 }
171
172 static const char *
173 slow_path_reason_to_string(enum slow_path_reason reason)
174 {
175 switch (reason) {
176 case SLOW_CFM:
177 return "cfm";
178 case SLOW_LACP:
179 return "lacp";
180 case SLOW_STP:
181 return "stp";
182 case SLOW_BFD:
183 return "bfd";
184 case SLOW_CONTROLLER:
185 return "controller";
186 case __SLOW_MAX:
187 default:
188 return NULL;
189 }
190 }
191
192 static enum slow_path_reason
193 string_to_slow_path_reason(const char *string)
194 {
195 enum slow_path_reason i;
196
197 for (i = 1; i < __SLOW_MAX; i++) {
198 if (!strcmp(string, slow_path_reason_to_string(i))) {
199 return i;
200 }
201 }
202
203 return 0;
204 }
205
206 static int
207 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
208 uint32_t *res)
209 {
210 uint32_t result = 0;
211 int n = 0;
212
213 if (s[n] != '(') {
214 return -EINVAL;
215 }
216 n++;
217
218 while (s[n] != ')') {
219 unsigned long long int flags;
220 uint32_t bit;
221 int n0;
222
223 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
224 n += n0 + (s[n + n0] == ',');
225 result |= flags;
226 continue;
227 }
228
229 for (bit = 1; bit; bit <<= 1) {
230 const char *name = bit_to_string(bit);
231 size_t len;
232
233 if (!name) {
234 continue;
235 }
236
237 len = strlen(name);
238 if (!strncmp(s + n, name, len) &&
239 (s[n + len] == ',' || s[n + len] == ')')) {
240 result |= bit;
241 n += len + (s[n + len] == ',');
242 break;
243 }
244 }
245
246 if (!bit) {
247 return -EINVAL;
248 }
249 }
250 n++;
251
252 *res = result;
253 return n;
254 }
255
256 static void
257 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
258 {
259 static const struct nl_policy ovs_userspace_policy[] = {
260 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
261 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
262 .optional = true },
263 };
264 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
265 const struct nlattr *userdata_attr;
266
267 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
268 ds_put_cstr(ds, "userspace(error)");
269 return;
270 }
271
272 ds_put_format(ds, "userspace(pid=%"PRIu32,
273 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
274
275 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
276
277 if (userdata_attr) {
278 const uint8_t *userdata = nl_attr_get(userdata_attr);
279 size_t userdata_len = nl_attr_get_size(userdata_attr);
280 bool userdata_unspec = true;
281 union user_action_cookie cookie;
282
283 if (userdata_len >= sizeof cookie.type
284 && userdata_len <= sizeof cookie) {
285
286 memset(&cookie, 0, sizeof cookie);
287 memcpy(&cookie, userdata, userdata_len);
288
289 userdata_unspec = false;
290
291 if (userdata_len == sizeof cookie.sflow
292 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
293 ds_put_format(ds, ",sFlow("
294 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
295 vlan_tci_to_vid(cookie.sflow.vlan_tci),
296 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
297 cookie.sflow.output);
298 } else if (userdata_len == sizeof cookie.slow_path
299 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
300 const char *reason;
301 reason = slow_path_reason_to_string(cookie.slow_path.reason);
302 reason = reason ? reason : "";
303 ds_put_format(ds, ",slow_path(%s)", reason);
304 } else if (userdata_len == sizeof cookie.flow_sample
305 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
306 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
307 ",collector_set_id=%"PRIu32
308 ",obs_domain_id=%"PRIu32
309 ",obs_point_id=%"PRIu32")",
310 cookie.flow_sample.probability,
311 cookie.flow_sample.collector_set_id,
312 cookie.flow_sample.obs_domain_id,
313 cookie.flow_sample.obs_point_id);
314 } else if (userdata_len == sizeof cookie.ipfix
315 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
316 ds_put_format(ds, ",ipfix");
317 } else {
318 userdata_unspec = true;
319 }
320 }
321
322 if (userdata_unspec) {
323 size_t i;
324 ds_put_format(ds, ",userdata(");
325 for (i = 0; i < userdata_len; i++) {
326 ds_put_format(ds, "%02x", userdata[i]);
327 }
328 ds_put_char(ds, ')');
329 }
330 }
331
332 ds_put_char(ds, ')');
333 }
334
335 static void
336 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
337 {
338 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
339 vlan_tci_to_vid(vlan_tci),
340 vlan_tci_to_pcp(vlan_tci));
341 if (!(vlan_tci & htons(VLAN_CFI))) {
342 ds_put_cstr(ds, ",cfi=0");
343 }
344 }
345
346 static void
347 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
348 {
349 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
350 mpls_lse_to_label(mpls_lse),
351 mpls_lse_to_tc(mpls_lse),
352 mpls_lse_to_ttl(mpls_lse),
353 mpls_lse_to_bos(mpls_lse));
354 }
355
356 static void
357 format_odp_action(struct ds *ds, const struct nlattr *a)
358 {
359 int expected_len;
360 enum ovs_action_attr type = nl_attr_type(a);
361 const struct ovs_action_push_vlan *vlan;
362
363 expected_len = odp_action_len(nl_attr_type(a));
364 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
365 ds_put_format(ds, "bad length %zu, expected %d for: ",
366 nl_attr_get_size(a), expected_len);
367 format_generic_odp_action(ds, a);
368 return;
369 }
370
371 switch (type) {
372 case OVS_ACTION_ATTR_OUTPUT:
373 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
374 break;
375 case OVS_ACTION_ATTR_USERSPACE:
376 format_odp_userspace_action(ds, a);
377 break;
378 case OVS_ACTION_ATTR_SET:
379 ds_put_cstr(ds, "set(");
380 format_odp_key_attr(nl_attr_get(a), ds);
381 ds_put_cstr(ds, ")");
382 break;
383 case OVS_ACTION_ATTR_PUSH_VLAN:
384 vlan = nl_attr_get(a);
385 ds_put_cstr(ds, "push_vlan(");
386 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
387 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
388 }
389 format_vlan_tci(ds, vlan->vlan_tci);
390 ds_put_char(ds, ')');
391 break;
392 case OVS_ACTION_ATTR_POP_VLAN:
393 ds_put_cstr(ds, "pop_vlan");
394 break;
395 case OVS_ACTION_ATTR_PUSH_MPLS: {
396 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
397 ds_put_cstr(ds, "push_mpls(");
398 format_mpls_lse(ds, mpls->mpls_lse);
399 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
400 break;
401 }
402 case OVS_ACTION_ATTR_POP_MPLS: {
403 ovs_be16 ethertype = nl_attr_get_be16(a);
404 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
405 break;
406 }
407 case OVS_ACTION_ATTR_SAMPLE:
408 format_odp_sample_action(ds, a);
409 break;
410 case OVS_ACTION_ATTR_UNSPEC:
411 case __OVS_ACTION_ATTR_MAX:
412 default:
413 format_generic_odp_action(ds, a);
414 break;
415 }
416 }
417
418 void
419 format_odp_actions(struct ds *ds, const struct nlattr *actions,
420 size_t actions_len)
421 {
422 if (actions_len) {
423 const struct nlattr *a;
424 unsigned int left;
425
426 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
427 if (a != actions) {
428 ds_put_char(ds, ',');
429 }
430 format_odp_action(ds, a);
431 }
432 if (left) {
433 int i;
434
435 if (left == actions_len) {
436 ds_put_cstr(ds, "<empty>");
437 }
438 ds_put_format(ds, ",***%u leftover bytes*** (", left);
439 for (i = 0; i < left; i++) {
440 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
441 }
442 ds_put_char(ds, ')');
443 }
444 } else {
445 ds_put_cstr(ds, "drop");
446 }
447 }
448
449 static int
450 parse_odp_action(const char *s, const struct simap *port_names,
451 struct ofpbuf *actions)
452 {
453 /* Many of the sscanf calls in this function use oversized destination
454 * fields because some sscanf() implementations truncate the range of %i
455 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
456 * value of 0x7fff. The other alternatives are to allow only a single
457 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
458 * parsers.
459 *
460 * The tun_id parser has to use an alternative approach because there is no
461 * type larger than 64 bits. */
462
463 {
464 unsigned long long int port;
465 int n = -1;
466
467 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
468 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
469 return n;
470 }
471 }
472
473 if (port_names) {
474 int len = strcspn(s, delimiters);
475 struct simap_node *node;
476
477 node = simap_find_len(port_names, s, len);
478 if (node) {
479 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
480 return len;
481 }
482 }
483
484 {
485 unsigned long long int pid;
486 unsigned long long int output;
487 unsigned long long int probability;
488 unsigned long long int collector_set_id;
489 unsigned long long int obs_domain_id;
490 unsigned long long int obs_point_id;
491 int vid, pcp;
492 int n = -1;
493
494 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
495 odp_put_userspace_action(pid, NULL, 0, actions);
496 return n;
497 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
498 "pcp=%i,output=%lli))%n",
499 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
500 union user_action_cookie cookie;
501 uint16_t tci;
502
503 tci = vid | (pcp << VLAN_PCP_SHIFT);
504 if (tci) {
505 tci |= VLAN_CFI;
506 }
507
508 cookie.type = USER_ACTION_COOKIE_SFLOW;
509 cookie.sflow.vlan_tci = htons(tci);
510 cookie.sflow.output = output;
511 odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
512 actions);
513 return n;
514 } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0
515 && n > 0) {
516 union user_action_cookie cookie;
517 char reason[32];
518
519 if (s[n] == ')' && s[n + 1] == ')') {
520 reason[0] = '\0';
521 n += 2;
522 } else if (sscanf(s + n, "%31[^)]))", reason) > 0) {
523 n += strlen(reason) + 2;
524 } else {
525 return -EINVAL;
526 }
527
528 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
529 cookie.slow_path.unused = 0;
530 cookie.slow_path.reason = string_to_slow_path_reason(reason);
531
532 if (reason[0] && !cookie.slow_path.reason) {
533 return -EINVAL;
534 }
535
536 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
537 actions);
538 return n;
539 } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
540 "collector_set_id=%lli,obs_domain_id=%lli,"
541 "obs_point_id=%lli))%n",
542 &pid, &probability, &collector_set_id,
543 &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
544 union user_action_cookie cookie;
545
546 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
547 cookie.flow_sample.probability = probability;
548 cookie.flow_sample.collector_set_id = collector_set_id;
549 cookie.flow_sample.obs_domain_id = obs_domain_id;
550 cookie.flow_sample.obs_point_id = obs_point_id;
551 odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
552 actions);
553 return n;
554 } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
555 && n > 0) {
556 union user_action_cookie cookie;
557
558 cookie.type = USER_ACTION_COOKIE_IPFIX;
559 odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
560 actions);
561 return n;
562 } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
563 && n > 0) {
564 struct ofpbuf buf;
565 char *end;
566
567 ofpbuf_init(&buf, 16);
568 end = ofpbuf_put_hex(&buf, &s[n], NULL);
569 if (end[0] == ')' && end[1] == ')') {
570 odp_put_userspace_action(pid, buf.data, buf.size, actions);
571 ofpbuf_uninit(&buf);
572 return (end + 2) - s;
573 }
574 }
575 }
576
577 if (!strncmp(s, "set(", 4)) {
578 size_t start_ofs;
579 int retval;
580
581 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
582 retval = parse_odp_key_attr(s + 4, port_names, actions);
583 if (retval < 0) {
584 return retval;
585 }
586 if (s[retval + 4] != ')') {
587 return -EINVAL;
588 }
589 nl_msg_end_nested(actions, start_ofs);
590 return retval + 5;
591 }
592
593 {
594 struct ovs_action_push_vlan push;
595 int tpid = ETH_TYPE_VLAN;
596 int vid, pcp;
597 int cfi = 1;
598 int n = -1;
599
600 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
601 && n > 0)
602 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
603 &vid, &pcp, &cfi, &n) > 0 && n > 0)
604 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
605 &tpid, &vid, &pcp, &n) > 0 && n > 0)
606 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
607 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
608 push.vlan_tpid = htons(tpid);
609 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
610 | (pcp << VLAN_PCP_SHIFT)
611 | (cfi ? VLAN_CFI : 0));
612 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
613 &push, sizeof push);
614
615 return n;
616 }
617 }
618
619 if (!strncmp(s, "pop_vlan", 8)) {
620 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
621 return 8;
622 }
623
624 {
625 double percentage;
626 int n = -1;
627
628 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
629 && percentage >= 0. && percentage <= 100.0
630 && n > 0) {
631 size_t sample_ofs, actions_ofs;
632 double probability;
633
634 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
635 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
636 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
637 (probability <= 0 ? 0
638 : probability >= UINT32_MAX ? UINT32_MAX
639 : probability));
640
641 actions_ofs = nl_msg_start_nested(actions,
642 OVS_SAMPLE_ATTR_ACTIONS);
643 for (;;) {
644 int retval;
645
646 n += strspn(s + n, delimiters);
647 if (s[n] == ')') {
648 break;
649 }
650
651 retval = parse_odp_action(s + n, port_names, actions);
652 if (retval < 0) {
653 return retval;
654 }
655 n += retval;
656 }
657 nl_msg_end_nested(actions, actions_ofs);
658 nl_msg_end_nested(actions, sample_ofs);
659
660 return s[n + 1] == ')' ? n + 2 : -EINVAL;
661 }
662 }
663
664 return -EINVAL;
665 }
666
667 /* Parses the string representation of datapath actions, in the format output
668 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
669 * value. On success, the ODP actions are appended to 'actions' as a series of
670 * Netlink attributes. On failure, no data is appended to 'actions'. Either
671 * way, 'actions''s data might be reallocated. */
672 int
673 odp_actions_from_string(const char *s, const struct simap *port_names,
674 struct ofpbuf *actions)
675 {
676 size_t old_size;
677
678 if (!strcasecmp(s, "drop")) {
679 return 0;
680 }
681
682 old_size = actions->size;
683 for (;;) {
684 int retval;
685
686 s += strspn(s, delimiters);
687 if (!*s) {
688 return 0;
689 }
690
691 retval = parse_odp_action(s, port_names, actions);
692 if (retval < 0 || !strchr(delimiters, s[retval])) {
693 actions->size = old_size;
694 return -retval;
695 }
696 s += retval;
697 }
698
699 return 0;
700 }
701 \f
702 /* Returns the correct length of the payload for a flow key attribute of the
703 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
704 * is variable length. */
705 static int
706 odp_flow_key_attr_len(uint16_t type)
707 {
708 if (type > OVS_KEY_ATTR_MAX) {
709 return -1;
710 }
711
712 switch ((enum ovs_key_attr) type) {
713 case OVS_KEY_ATTR_ENCAP: return -2;
714 case OVS_KEY_ATTR_PRIORITY: return 4;
715 case OVS_KEY_ATTR_SKB_MARK: return 4;
716 case OVS_KEY_ATTR_TUNNEL: return -2;
717 case OVS_KEY_ATTR_IN_PORT: return 4;
718 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
719 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
720 case OVS_KEY_ATTR_ETHERTYPE: return 2;
721 case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
722 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
723 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
724 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
725 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
726 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
727 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
728 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
729 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
730
731 case OVS_KEY_ATTR_UNSPEC:
732 case __OVS_KEY_ATTR_MAX:
733 return -1;
734 }
735
736 return -1;
737 }
738
739 static void
740 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
741 {
742 size_t len = nl_attr_get_size(a);
743 if (len) {
744 const uint8_t *unspec;
745 unsigned int i;
746
747 unspec = nl_attr_get(a);
748 for (i = 0; i < len; i++) {
749 ds_put_char(ds, i ? ' ': '(');
750 ds_put_format(ds, "%02x", unspec[i]);
751 }
752 ds_put_char(ds, ')');
753 }
754 }
755
756 static const char *
757 ovs_frag_type_to_string(enum ovs_frag_type type)
758 {
759 switch (type) {
760 case OVS_FRAG_TYPE_NONE:
761 return "no";
762 case OVS_FRAG_TYPE_FIRST:
763 return "first";
764 case OVS_FRAG_TYPE_LATER:
765 return "later";
766 case __OVS_FRAG_TYPE_MAX:
767 default:
768 return "<error>";
769 }
770 }
771
772 static int
773 tunnel_key_attr_len(int type)
774 {
775 switch (type) {
776 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
777 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
778 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
779 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
780 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
781 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
782 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
783 case __OVS_TUNNEL_KEY_ATTR_MAX:
784 return -1;
785 }
786 return -1;
787 }
788
789 enum odp_key_fitness
790 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
791 {
792 unsigned int left;
793 const struct nlattr *a;
794 bool ttl = false;
795 bool unknown = false;
796
797 NL_NESTED_FOR_EACH(a, left, attr) {
798 uint16_t type = nl_attr_type(a);
799 size_t len = nl_attr_get_size(a);
800 int expected_len = tunnel_key_attr_len(type);
801
802 if (len != expected_len && expected_len >= 0) {
803 return ODP_FIT_ERROR;
804 }
805
806 switch (type) {
807 case OVS_TUNNEL_KEY_ATTR_ID:
808 tun->tun_id = nl_attr_get_be64(a);
809 tun->flags |= FLOW_TNL_F_KEY;
810 break;
811 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
812 tun->ip_src = nl_attr_get_be32(a);
813 break;
814 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
815 tun->ip_dst = nl_attr_get_be32(a);
816 break;
817 case OVS_TUNNEL_KEY_ATTR_TOS:
818 tun->ip_tos = nl_attr_get_u8(a);
819 break;
820 case OVS_TUNNEL_KEY_ATTR_TTL:
821 tun->ip_ttl = nl_attr_get_u8(a);
822 ttl = true;
823 break;
824 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
825 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
826 break;
827 case OVS_TUNNEL_KEY_ATTR_CSUM:
828 tun->flags |= FLOW_TNL_F_CSUM;
829 break;
830 default:
831 /* Allow this to show up as unexpected, if there are unknown
832 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
833 unknown = true;
834 break;
835 }
836 }
837
838 if (!ttl) {
839 return ODP_FIT_ERROR;
840 }
841 if (unknown) {
842 return ODP_FIT_TOO_MUCH;
843 }
844 return ODP_FIT_PERFECT;
845 }
846
847 static void
848 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
849 {
850 size_t tun_key_ofs;
851
852 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
853
854 if (tun_key->flags & FLOW_TNL_F_KEY) {
855 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
856 }
857 if (tun_key->ip_src) {
858 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
859 }
860 if (tun_key->ip_dst) {
861 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
862 }
863 if (tun_key->ip_tos) {
864 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
865 }
866 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
867 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
868 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
869 }
870 if (tun_key->flags & FLOW_TNL_F_CSUM) {
871 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
872 }
873
874 nl_msg_end_nested(a, tun_key_ofs);
875 }
876
877 static void
878 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
879 {
880 const struct ovs_key_ethernet *eth_key;
881 const struct ovs_key_ipv4 *ipv4_key;
882 const struct ovs_key_ipv6 *ipv6_key;
883 const struct ovs_key_tcp *tcp_key;
884 const struct ovs_key_udp *udp_key;
885 const struct ovs_key_icmp *icmp_key;
886 const struct ovs_key_icmpv6 *icmpv6_key;
887 const struct ovs_key_arp *arp_key;
888 const struct ovs_key_nd *nd_key;
889 struct flow_tnl tun_key;
890 enum ovs_key_attr attr = nl_attr_type(a);
891 int expected_len;
892
893 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
894 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
895 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
896 ds_put_format(ds, "(bad length %zu, expected %d)",
897 nl_attr_get_size(a),
898 odp_flow_key_attr_len(nl_attr_type(a)));
899 format_generic_odp_key(a, ds);
900 return;
901 }
902
903 switch (attr) {
904 case OVS_KEY_ATTR_ENCAP:
905 ds_put_cstr(ds, "(");
906 if (nl_attr_get_size(a)) {
907 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
908 }
909 ds_put_char(ds, ')');
910 break;
911
912 case OVS_KEY_ATTR_PRIORITY:
913 ds_put_format(ds, "(%#"PRIx32")", nl_attr_get_u32(a));
914 break;
915
916 case OVS_KEY_ATTR_SKB_MARK:
917 ds_put_format(ds, "(%#"PRIx32")", nl_attr_get_u32(a));
918 break;
919
920 case OVS_KEY_ATTR_TUNNEL:
921 memset(&tun_key, 0, sizeof tun_key);
922 if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
923 ds_put_format(ds, "(error)");
924 } else {
925 ds_put_format(ds, "(tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
926 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
927 ntohll(tun_key.tun_id),
928 IP_ARGS(tun_key.ip_src),
929 IP_ARGS(tun_key.ip_dst),
930 tun_key.ip_tos, tun_key.ip_ttl);
931
932 format_flags(ds, flow_tun_flag_to_string,
933 (uint32_t) tun_key.flags, ',');
934 ds_put_format(ds, "))");
935 }
936 break;
937
938 case OVS_KEY_ATTR_IN_PORT:
939 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
940 break;
941
942 case OVS_KEY_ATTR_ETHERNET:
943 eth_key = nl_attr_get(a);
944 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
945 ETH_ADDR_ARGS(eth_key->eth_src),
946 ETH_ADDR_ARGS(eth_key->eth_dst));
947 break;
948
949 case OVS_KEY_ATTR_VLAN:
950 ds_put_char(ds, '(');
951 format_vlan_tci(ds, nl_attr_get_be16(a));
952 ds_put_char(ds, ')');
953 break;
954
955 case OVS_KEY_ATTR_MPLS: {
956 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
957 ds_put_char(ds, '(');
958 format_mpls_lse(ds, mpls_key->mpls_lse);
959 ds_put_char(ds, ')');
960 break;
961 }
962
963 case OVS_KEY_ATTR_ETHERTYPE:
964 ds_put_format(ds, "(0x%04"PRIx16")",
965 ntohs(nl_attr_get_be16(a)));
966 break;
967
968 case OVS_KEY_ATTR_IPV4:
969 ipv4_key = nl_attr_get(a);
970 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
971 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
972 IP_ARGS(ipv4_key->ipv4_src),
973 IP_ARGS(ipv4_key->ipv4_dst),
974 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
975 ipv4_key->ipv4_ttl,
976 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
977 break;
978
979 case OVS_KEY_ATTR_IPV6: {
980 char src_str[INET6_ADDRSTRLEN];
981 char dst_str[INET6_ADDRSTRLEN];
982
983 ipv6_key = nl_attr_get(a);
984 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
985 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
986
987 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
988 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
989 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
990 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
991 ipv6_key->ipv6_hlimit,
992 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
993 break;
994 }
995
996 case OVS_KEY_ATTR_TCP:
997 tcp_key = nl_attr_get(a);
998 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
999 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1000 break;
1001
1002 case OVS_KEY_ATTR_UDP:
1003 udp_key = nl_attr_get(a);
1004 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
1005 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1006 break;
1007
1008 case OVS_KEY_ATTR_ICMP:
1009 icmp_key = nl_attr_get(a);
1010 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
1011 icmp_key->icmp_type, icmp_key->icmp_code);
1012 break;
1013
1014 case OVS_KEY_ATTR_ICMPV6:
1015 icmpv6_key = nl_attr_get(a);
1016 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
1017 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1018 break;
1019
1020 case OVS_KEY_ATTR_ARP:
1021 arp_key = nl_attr_get(a);
1022 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1023 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
1024 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1025 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
1026 ETH_ADDR_ARGS(arp_key->arp_tha));
1027 break;
1028
1029 case OVS_KEY_ATTR_ND: {
1030 char target[INET6_ADDRSTRLEN];
1031
1032 nd_key = nl_attr_get(a);
1033 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1034
1035 ds_put_format(ds, "(target=%s", target);
1036 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1037 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1038 ETH_ADDR_ARGS(nd_key->nd_sll));
1039 }
1040 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1041 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1042 ETH_ADDR_ARGS(nd_key->nd_tll));
1043 }
1044 ds_put_char(ds, ')');
1045 break;
1046 }
1047
1048 case OVS_KEY_ATTR_UNSPEC:
1049 case __OVS_KEY_ATTR_MAX:
1050 default:
1051 format_generic_odp_key(a, ds);
1052 break;
1053 }
1054 }
1055
1056 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1057 * OVS_KEY_ATTR_* attributes in 'key'. */
1058 void
1059 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
1060 {
1061 if (key_len) {
1062 const struct nlattr *a;
1063 unsigned int left;
1064
1065 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1066 if (a != key) {
1067 ds_put_char(ds, ',');
1068 }
1069 format_odp_key_attr(a, ds);
1070 }
1071 if (left) {
1072 int i;
1073
1074 if (left == key_len) {
1075 ds_put_cstr(ds, "<empty>");
1076 }
1077 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1078 for (i = 0; i < left; i++) {
1079 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1080 }
1081 ds_put_char(ds, ')');
1082 }
1083 } else {
1084 ds_put_cstr(ds, "<empty>");
1085 }
1086 }
1087
1088 static int
1089 put_nd_key(int n, const char *nd_target_s,
1090 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
1091 {
1092 struct ovs_key_nd nd_key;
1093
1094 memset(&nd_key, 0, sizeof nd_key);
1095 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1096 return -EINVAL;
1097 }
1098 if (nd_sll) {
1099 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
1100 }
1101 if (nd_tll) {
1102 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
1103 }
1104 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
1105 return n;
1106 }
1107
1108 static bool
1109 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1110 {
1111 if (!strcasecmp(s, "no")) {
1112 *type = OVS_FRAG_TYPE_NONE;
1113 } else if (!strcasecmp(s, "first")) {
1114 *type = OVS_FRAG_TYPE_FIRST;
1115 } else if (!strcasecmp(s, "later")) {
1116 *type = OVS_FRAG_TYPE_LATER;
1117 } else {
1118 return false;
1119 }
1120 return true;
1121 }
1122
1123 static ovs_be32
1124 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1125 {
1126 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1127 (mpls_tc << MPLS_TC_SHIFT) |
1128 (mpls_ttl << MPLS_TTL_SHIFT) |
1129 (mpls_bos << MPLS_BOS_SHIFT)));
1130 }
1131
1132 static int
1133 parse_odp_key_attr(const char *s, const struct simap *port_names,
1134 struct ofpbuf *key)
1135 {
1136 /* Many of the sscanf calls in this function use oversized destination
1137 * fields because some sscanf() implementations truncate the range of %i
1138 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1139 * value of 0x7fff. The other alternatives are to allow only a single
1140 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1141 * parsers.
1142 *
1143 * The tun_id parser has to use an alternative approach because there is no
1144 * type larger than 64 bits. */
1145
1146 {
1147 unsigned long long int priority;
1148 int n = -1;
1149
1150 if (sscanf(s, "skb_priority(%llx)%n", &priority, &n) > 0 && n > 0) {
1151 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1152 return n;
1153 }
1154 }
1155
1156 {
1157 unsigned long long int mark;
1158 int n = -1;
1159
1160 if (sscanf(s, "skb_mark(%llx)%n", &mark, &n) > 0 && n > 0) {
1161 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1162 return n;
1163 }
1164 }
1165
1166 {
1167 char tun_id_s[32];
1168 int tos, ttl;
1169 struct flow_tnl tun_key;
1170 int n = -1;
1171
1172 if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1173 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1174 ",tos=%i,ttl=%i,flags%n", tun_id_s,
1175 IP_SCAN_ARGS(&tun_key.ip_src),
1176 IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1177 &n) > 0 && n > 0) {
1178 int res;
1179 uint32_t flags;
1180
1181 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1182 tun_key.ip_tos = tos;
1183 tun_key.ip_ttl = ttl;
1184 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1185 tun_key.flags = (uint16_t) flags;
1186
1187 if (res < 0) {
1188 return res;
1189 }
1190 n += res;
1191 if (s[n] != ')') {
1192 return -EINVAL;
1193 }
1194 n++;
1195 tun_key_to_attr(key, &tun_key);
1196 return n;
1197 }
1198 }
1199
1200 {
1201 unsigned long long int in_port;
1202 int n = -1;
1203
1204 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1205 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1206 return n;
1207 }
1208 }
1209
1210 if (port_names && !strncmp(s, "in_port(", 8)) {
1211 const char *name;
1212 const struct simap_node *node;
1213 int name_len;
1214
1215 name = s + 8;
1216 name_len = strcspn(s, ")");
1217 node = simap_find_len(port_names, name, name_len);
1218 if (node) {
1219 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1220 return 8 + name_len + 1;
1221 }
1222 }
1223
1224 {
1225 struct ovs_key_ethernet eth_key;
1226 int n = -1;
1227
1228 if (sscanf(s,
1229 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1230 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1231 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1232 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1233 &eth_key, sizeof eth_key);
1234 return n;
1235 }
1236 }
1237
1238 {
1239 uint16_t vid;
1240 int pcp;
1241 int cfi;
1242 int n = -1;
1243
1244 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1245 && n > 0)) {
1246 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1247 htons((vid << VLAN_VID_SHIFT) |
1248 (pcp << VLAN_PCP_SHIFT) |
1249 VLAN_CFI));
1250 return n;
1251 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1252 &vid, &pcp, &cfi, &n) > 0
1253 && n > 0)) {
1254 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1255 htons((vid << VLAN_VID_SHIFT) |
1256 (pcp << VLAN_PCP_SHIFT) |
1257 (cfi ? VLAN_CFI : 0)));
1258 return n;
1259 }
1260 }
1261
1262 {
1263 int eth_type;
1264 int n = -1;
1265
1266 if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1267 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1268 return n;
1269 }
1270 }
1271
1272 {
1273 int label, tc, ttl, bos;
1274 int n = -1;
1275
1276 if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1277 &label, &tc, &ttl, &bos, &n) > 0 &&
1278 n > 0) {
1279 struct ovs_key_mpls *mpls;
1280
1281 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1282 sizeof *mpls);
1283 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1284 return n;
1285 }
1286 }
1287
1288 {
1289 ovs_be32 ipv4_src;
1290 ovs_be32 ipv4_dst;
1291 int ipv4_proto;
1292 int ipv4_tos;
1293 int ipv4_ttl;
1294 char frag[8];
1295 enum ovs_frag_type ipv4_frag;
1296 int n = -1;
1297
1298 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1299 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1300 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1301 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1302 && n > 0
1303 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1304 struct ovs_key_ipv4 ipv4_key;
1305
1306 ipv4_key.ipv4_src = ipv4_src;
1307 ipv4_key.ipv4_dst = ipv4_dst;
1308 ipv4_key.ipv4_proto = ipv4_proto;
1309 ipv4_key.ipv4_tos = ipv4_tos;
1310 ipv4_key.ipv4_ttl = ipv4_ttl;
1311 ipv4_key.ipv4_frag = ipv4_frag;
1312 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1313 &ipv4_key, sizeof ipv4_key);
1314 return n;
1315 }
1316 }
1317
1318 {
1319 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1320 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1321 int ipv6_label;
1322 int ipv6_proto;
1323 int ipv6_tclass;
1324 int ipv6_hlimit;
1325 char frag[8];
1326 enum ovs_frag_type ipv6_frag;
1327 int n = -1;
1328
1329 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1330 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1331 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1332 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1333 && n > 0
1334 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1335 struct ovs_key_ipv6 ipv6_key;
1336
1337 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1338 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1339 return -EINVAL;
1340 }
1341 ipv6_key.ipv6_label = htonl(ipv6_label);
1342 ipv6_key.ipv6_proto = ipv6_proto;
1343 ipv6_key.ipv6_tclass = ipv6_tclass;
1344 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1345 ipv6_key.ipv6_frag = ipv6_frag;
1346 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1347 &ipv6_key, sizeof ipv6_key);
1348 return n;
1349 }
1350 }
1351
1352 {
1353 int tcp_src;
1354 int tcp_dst;
1355 int n = -1;
1356
1357 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1358 && n > 0) {
1359 struct ovs_key_tcp tcp_key;
1360
1361 tcp_key.tcp_src = htons(tcp_src);
1362 tcp_key.tcp_dst = htons(tcp_dst);
1363 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1364 return n;
1365 }
1366 }
1367
1368 {
1369 int udp_src;
1370 int udp_dst;
1371 int n = -1;
1372
1373 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1374 && n > 0) {
1375 struct ovs_key_udp udp_key;
1376
1377 udp_key.udp_src = htons(udp_src);
1378 udp_key.udp_dst = htons(udp_dst);
1379 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1380 return n;
1381 }
1382 }
1383
1384 {
1385 int icmp_type;
1386 int icmp_code;
1387 int n = -1;
1388
1389 if (sscanf(s, "icmp(type=%i,code=%i)%n",
1390 &icmp_type, &icmp_code, &n) > 0
1391 && n > 0) {
1392 struct ovs_key_icmp icmp_key;
1393
1394 icmp_key.icmp_type = icmp_type;
1395 icmp_key.icmp_code = icmp_code;
1396 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1397 &icmp_key, sizeof icmp_key);
1398 return n;
1399 }
1400 }
1401
1402 {
1403 struct ovs_key_icmpv6 icmpv6_key;
1404 int n = -1;
1405
1406 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1407 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1408 && n > 0) {
1409 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1410 &icmpv6_key, sizeof icmpv6_key);
1411 return n;
1412 }
1413 }
1414
1415 {
1416 ovs_be32 arp_sip;
1417 ovs_be32 arp_tip;
1418 int arp_op;
1419 uint8_t arp_sha[ETH_ADDR_LEN];
1420 uint8_t arp_tha[ETH_ADDR_LEN];
1421 int n = -1;
1422
1423 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1424 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1425 IP_SCAN_ARGS(&arp_sip),
1426 IP_SCAN_ARGS(&arp_tip),
1427 &arp_op,
1428 ETH_ADDR_SCAN_ARGS(arp_sha),
1429 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1430 struct ovs_key_arp arp_key;
1431
1432 memset(&arp_key, 0, sizeof arp_key);
1433 arp_key.arp_sip = arp_sip;
1434 arp_key.arp_tip = arp_tip;
1435 arp_key.arp_op = htons(arp_op);
1436 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1437 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1438 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1439 return n;
1440 }
1441 }
1442
1443 {
1444 char nd_target_s[IPV6_SCAN_LEN + 1];
1445 uint8_t nd_sll[ETH_ADDR_LEN];
1446 uint8_t nd_tll[ETH_ADDR_LEN];
1447 int n = -1;
1448
1449 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1450 nd_target_s, &n) > 0 && n > 0) {
1451 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1452 }
1453 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1454 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1455 && n > 0) {
1456 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1457 }
1458 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1459 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1460 && n > 0) {
1461 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1462 }
1463 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1464 "tll="ETH_ADDR_SCAN_FMT")%n",
1465 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1466 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1467 && n > 0) {
1468 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1469 }
1470 }
1471
1472 if (!strncmp(s, "encap(", 6)) {
1473 const char *start = s;
1474 size_t encap;
1475
1476 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1477
1478 s += 6;
1479 for (;;) {
1480 int retval;
1481
1482 s += strspn(s, ", \t\r\n");
1483 if (!*s) {
1484 return -EINVAL;
1485 } else if (*s == ')') {
1486 break;
1487 }
1488
1489 retval = parse_odp_key_attr(s, port_names, key);
1490 if (retval < 0) {
1491 return retval;
1492 }
1493 s += retval;
1494 }
1495 s++;
1496
1497 nl_msg_end_nested(key, encap);
1498
1499 return s - start;
1500 }
1501
1502 return -EINVAL;
1503 }
1504
1505 /* Parses the string representation of a datapath flow key, in the
1506 * format output by odp_flow_key_format(). Returns 0 if successful,
1507 * otherwise a positive errno value. On success, the flow key is
1508 * appended to 'key' as a series of Netlink attributes. On failure, no
1509 * data is appended to 'key'. Either way, 'key''s data might be
1510 * reallocated.
1511 *
1512 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1513 * to a port number. (Port names may be used instead of port numbers in
1514 * in_port.)
1515 *
1516 * On success, the attributes appended to 'key' are individually syntactically
1517 * valid, but they may not be valid as a sequence. 'key' might, for example,
1518 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1519 int
1520 odp_flow_key_from_string(const char *s, const struct simap *port_names,
1521 struct ofpbuf *key)
1522 {
1523 const size_t old_size = key->size;
1524 for (;;) {
1525 int retval;
1526
1527 s += strspn(s, delimiters);
1528 if (!*s) {
1529 return 0;
1530 }
1531
1532 retval = parse_odp_key_attr(s, port_names, key);
1533 if (retval < 0) {
1534 key->size = old_size;
1535 return -retval;
1536 }
1537 s += retval;
1538 }
1539
1540 return 0;
1541 }
1542
1543 static uint8_t
1544 ovs_to_odp_frag(uint8_t nw_frag)
1545 {
1546 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1547 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1548 : OVS_FRAG_TYPE_LATER);
1549 }
1550
1551 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1552 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1553 * number rather than a datapath port number). Instead, if 'odp_in_port'
1554 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1555 * port.
1556 *
1557 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1558 * capable of being expanded to allow for that much space. */
1559 void
1560 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
1561 uint32_t odp_in_port)
1562 {
1563 struct ovs_key_ethernet *eth_key;
1564 size_t encap;
1565
1566 if (flow->skb_priority) {
1567 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1568 }
1569
1570 if (flow->tunnel.ip_dst) {
1571 tun_key_to_attr(buf, &flow->tunnel);
1572 }
1573
1574 if (flow->skb_mark) {
1575 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, flow->skb_mark);
1576 }
1577
1578 if (odp_in_port != OVSP_NONE) {
1579 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
1580 }
1581
1582 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1583 sizeof *eth_key);
1584 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1585 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1586
1587 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1588 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1589 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1590 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1591 if (flow->vlan_tci == htons(0)) {
1592 goto unencap;
1593 }
1594 } else {
1595 encap = 0;
1596 }
1597
1598 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1599 goto unencap;
1600 }
1601
1602 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1603
1604 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1605 struct ovs_key_ipv4 *ipv4_key;
1606
1607 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1608 sizeof *ipv4_key);
1609 ipv4_key->ipv4_src = flow->nw_src;
1610 ipv4_key->ipv4_dst = flow->nw_dst;
1611 ipv4_key->ipv4_proto = flow->nw_proto;
1612 ipv4_key->ipv4_tos = flow->nw_tos;
1613 ipv4_key->ipv4_ttl = flow->nw_ttl;
1614 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1615 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1616 struct ovs_key_ipv6 *ipv6_key;
1617
1618 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1619 sizeof *ipv6_key);
1620 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1621 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1622 ipv6_key->ipv6_label = flow->ipv6_label;
1623 ipv6_key->ipv6_proto = flow->nw_proto;
1624 ipv6_key->ipv6_tclass = flow->nw_tos;
1625 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1626 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1627 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1628 flow->dl_type == htons(ETH_TYPE_RARP)) {
1629 struct ovs_key_arp *arp_key;
1630
1631 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1632 sizeof *arp_key);
1633 memset(arp_key, 0, sizeof *arp_key);
1634 arp_key->arp_sip = flow->nw_src;
1635 arp_key->arp_tip = flow->nw_dst;
1636 arp_key->arp_op = htons(flow->nw_proto);
1637 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1638 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1639 }
1640
1641 if (flow->mpls_depth) {
1642 struct ovs_key_mpls *mpls_key;
1643
1644 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
1645 sizeof *mpls_key);
1646 mpls_key->mpls_lse = flow->mpls_lse;
1647 }
1648
1649 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1650 if (flow->nw_proto == IPPROTO_TCP) {
1651 struct ovs_key_tcp *tcp_key;
1652
1653 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1654 sizeof *tcp_key);
1655 tcp_key->tcp_src = flow->tp_src;
1656 tcp_key->tcp_dst = flow->tp_dst;
1657 } else if (flow->nw_proto == IPPROTO_UDP) {
1658 struct ovs_key_udp *udp_key;
1659
1660 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1661 sizeof *udp_key);
1662 udp_key->udp_src = flow->tp_src;
1663 udp_key->udp_dst = flow->tp_dst;
1664 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1665 && flow->nw_proto == IPPROTO_ICMP) {
1666 struct ovs_key_icmp *icmp_key;
1667
1668 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1669 sizeof *icmp_key);
1670 icmp_key->icmp_type = ntohs(flow->tp_src);
1671 icmp_key->icmp_code = ntohs(flow->tp_dst);
1672 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1673 && flow->nw_proto == IPPROTO_ICMPV6) {
1674 struct ovs_key_icmpv6 *icmpv6_key;
1675
1676 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1677 sizeof *icmpv6_key);
1678 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1679 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1680
1681 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1682 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1683 struct ovs_key_nd *nd_key;
1684
1685 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1686 sizeof *nd_key);
1687 memcpy(nd_key->nd_target, &flow->nd_target,
1688 sizeof nd_key->nd_target);
1689 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1690 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1691 }
1692 }
1693 }
1694
1695 unencap:
1696 if (encap) {
1697 nl_msg_end_nested(buf, encap);
1698 }
1699 }
1700
1701 uint32_t
1702 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1703 {
1704 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1705 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1706 }
1707
1708 static void
1709 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1710 uint64_t attrs, int out_of_range_attr,
1711 const struct nlattr *key, size_t key_len)
1712 {
1713 struct ds s;
1714 int i;
1715
1716 if (VLOG_DROP_DBG(rl)) {
1717 return;
1718 }
1719
1720 ds_init(&s);
1721 for (i = 0; i < 64; i++) {
1722 if (attrs & (UINT64_C(1) << i)) {
1723 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1724 }
1725 }
1726 if (out_of_range_attr) {
1727 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1728 }
1729
1730 ds_put_cstr(&s, ": ");
1731 odp_flow_key_format(key, key_len, &s);
1732
1733 VLOG_DBG("%s:%s", title, ds_cstr(&s));
1734 ds_destroy(&s);
1735 }
1736
1737 static bool
1738 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1739 {
1740 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1741
1742 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1743 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1744 return false;
1745 }
1746
1747 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1748 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1749 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1750 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1751 }
1752 }
1753 return true;
1754 }
1755
1756 static bool
1757 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1758 const struct nlattr *attrs[], uint64_t *present_attrsp,
1759 int *out_of_range_attrp)
1760 {
1761 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1762 const struct nlattr *nla;
1763 uint64_t present_attrs;
1764 size_t left;
1765
1766 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
1767 present_attrs = 0;
1768 *out_of_range_attrp = 0;
1769 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1770 uint16_t type = nl_attr_type(nla);
1771 size_t len = nl_attr_get_size(nla);
1772 int expected_len = odp_flow_key_attr_len(type);
1773
1774 if (len != expected_len && expected_len >= 0) {
1775 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1776 "length %d", ovs_key_attr_to_string(type),
1777 len, expected_len);
1778 return false;
1779 }
1780
1781 if (type > OVS_KEY_ATTR_MAX) {
1782 *out_of_range_attrp = type;
1783 } else {
1784 if (present_attrs & (UINT64_C(1) << type)) {
1785 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1786 ovs_key_attr_to_string(type));
1787 return false;
1788 }
1789
1790 present_attrs |= UINT64_C(1) << type;
1791 attrs[type] = nla;
1792 }
1793 }
1794 if (left) {
1795 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1796 return false;
1797 }
1798
1799 *present_attrsp = present_attrs;
1800 return true;
1801 }
1802
1803 static enum odp_key_fitness
1804 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1805 uint64_t expected_attrs,
1806 const struct nlattr *key, size_t key_len)
1807 {
1808 uint64_t missing_attrs;
1809 uint64_t extra_attrs;
1810
1811 missing_attrs = expected_attrs & ~present_attrs;
1812 if (missing_attrs) {
1813 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1814 log_odp_key_attributes(&rl, "expected but not present",
1815 missing_attrs, 0, key, key_len);
1816 return ODP_FIT_TOO_LITTLE;
1817 }
1818
1819 extra_attrs = present_attrs & ~expected_attrs;
1820 if (extra_attrs || out_of_range_attr) {
1821 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1822 log_odp_key_attributes(&rl, "present but not expected",
1823 extra_attrs, out_of_range_attr, key, key_len);
1824 return ODP_FIT_TOO_MUCH;
1825 }
1826
1827 return ODP_FIT_PERFECT;
1828 }
1829
1830 static bool
1831 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1832 uint64_t present_attrs, uint64_t *expected_attrs,
1833 struct flow *flow)
1834 {
1835 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1836
1837 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1838 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1839 if (ntohs(flow->dl_type) < 1536) {
1840 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1841 ntohs(flow->dl_type));
1842 return false;
1843 }
1844 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1845 } else {
1846 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1847 }
1848 return true;
1849 }
1850
1851 static enum odp_key_fitness
1852 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1853 uint64_t present_attrs, int out_of_range_attr,
1854 uint64_t expected_attrs, struct flow *flow,
1855 const struct nlattr *key, size_t key_len)
1856 {
1857 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1858
1859 if (eth_type_mpls(flow->dl_type)) {
1860 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
1861
1862 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
1863 return ODP_FIT_TOO_LITTLE;
1864 }
1865 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
1866 flow->mpls_depth++;
1867 } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
1868 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1869 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1870 const struct ovs_key_ipv4 *ipv4_key;
1871
1872 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1873 flow->nw_src = ipv4_key->ipv4_src;
1874 flow->nw_dst = ipv4_key->ipv4_dst;
1875 flow->nw_proto = ipv4_key->ipv4_proto;
1876 flow->nw_tos = ipv4_key->ipv4_tos;
1877 flow->nw_ttl = ipv4_key->ipv4_ttl;
1878 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1879 return ODP_FIT_ERROR;
1880 }
1881 }
1882 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1883 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1884 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1885 const struct ovs_key_ipv6 *ipv6_key;
1886
1887 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1888 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1889 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1890 flow->ipv6_label = ipv6_key->ipv6_label;
1891 flow->nw_proto = ipv6_key->ipv6_proto;
1892 flow->nw_tos = ipv6_key->ipv6_tclass;
1893 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1894 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1895 return ODP_FIT_ERROR;
1896 }
1897 }
1898 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1899 flow->dl_type == htons(ETH_TYPE_RARP)) {
1900 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1901 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1902 const struct ovs_key_arp *arp_key;
1903
1904 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1905 flow->nw_src = arp_key->arp_sip;
1906 flow->nw_dst = arp_key->arp_tip;
1907 if (arp_key->arp_op & htons(0xff00)) {
1908 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1909 "key", ntohs(arp_key->arp_op));
1910 return ODP_FIT_ERROR;
1911 }
1912 flow->nw_proto = ntohs(arp_key->arp_op);
1913 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1914 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1915 }
1916 }
1917
1918 if (flow->nw_proto == IPPROTO_TCP
1919 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1920 flow->dl_type == htons(ETH_TYPE_IPV6))
1921 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1922 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1923 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1924 const struct ovs_key_tcp *tcp_key;
1925
1926 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1927 flow->tp_src = tcp_key->tcp_src;
1928 flow->tp_dst = tcp_key->tcp_dst;
1929 }
1930 } else if (flow->nw_proto == IPPROTO_UDP
1931 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1932 flow->dl_type == htons(ETH_TYPE_IPV6))
1933 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1934 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1935 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1936 const struct ovs_key_udp *udp_key;
1937
1938 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1939 flow->tp_src = udp_key->udp_src;
1940 flow->tp_dst = udp_key->udp_dst;
1941 }
1942 } else if (flow->nw_proto == IPPROTO_ICMP
1943 && flow->dl_type == htons(ETH_TYPE_IP)
1944 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1945 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1946 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1947 const struct ovs_key_icmp *icmp_key;
1948
1949 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1950 flow->tp_src = htons(icmp_key->icmp_type);
1951 flow->tp_dst = htons(icmp_key->icmp_code);
1952 }
1953 } else if (flow->nw_proto == IPPROTO_ICMPV6
1954 && flow->dl_type == htons(ETH_TYPE_IPV6)
1955 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1956 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1957 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1958 const struct ovs_key_icmpv6 *icmpv6_key;
1959
1960 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1961 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1962 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1963
1964 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1965 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1966 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1967 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1968 const struct ovs_key_nd *nd_key;
1969
1970 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1971 memcpy(&flow->nd_target, nd_key->nd_target,
1972 sizeof flow->nd_target);
1973 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1974 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1975 }
1976 }
1977 }
1978 }
1979
1980 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1981 key, key_len);
1982 }
1983
1984 /* Parse 802.1Q header then encapsulated L3 attributes. */
1985 static enum odp_key_fitness
1986 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1987 uint64_t present_attrs, int out_of_range_attr,
1988 uint64_t expected_attrs, struct flow *flow,
1989 const struct nlattr *key, size_t key_len)
1990 {
1991 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1992
1993 const struct nlattr *encap
1994 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1995 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1996 enum odp_key_fitness encap_fitness;
1997 enum odp_key_fitness fitness;
1998 ovs_be16 tci;
1999
2000 /* Calculate fitness of outer attributes. */
2001 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
2002 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
2003 fitness = check_expectations(present_attrs, out_of_range_attr,
2004 expected_attrs, key, key_len);
2005
2006 /* Get the VLAN TCI value. */
2007 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
2008 return ODP_FIT_TOO_LITTLE;
2009 }
2010 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
2011 if (tci == htons(0)) {
2012 /* Corner case for a truncated 802.1Q header. */
2013 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
2014 return ODP_FIT_TOO_MUCH;
2015 }
2016 return fitness;
2017 } else if (!(tci & htons(VLAN_CFI))) {
2018 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
2019 "but CFI bit is not set", ntohs(tci));
2020 return ODP_FIT_ERROR;
2021 }
2022
2023 /* Set vlan_tci.
2024 * Remove the TPID from dl_type since it's not the real Ethertype. */
2025 flow->vlan_tci = tci;
2026 flow->dl_type = htons(0);
2027
2028 /* Now parse the encapsulated attributes. */
2029 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
2030 attrs, &present_attrs, &out_of_range_attr)) {
2031 return ODP_FIT_ERROR;
2032 }
2033 expected_attrs = 0;
2034
2035 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2036 return ODP_FIT_ERROR;
2037 }
2038 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2039 expected_attrs, flow, key, key_len);
2040
2041 /* The overall fitness is the worse of the outer and inner attributes. */
2042 return MAX(fitness, encap_fitness);
2043 }
2044
2045 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2046 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2047 * 'key' fits our expectations for what a flow key should contain.
2048 *
2049 * The 'in_port' will be the datapath's understanding of the port. The
2050 * caller will need to translate with odp_port_to_ofp_port() if the
2051 * OpenFlow port is needed.
2052 *
2053 * This function doesn't take the packet itself as an argument because none of
2054 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2055 * it is always possible to infer which additional attribute(s) should appear
2056 * by looking at the attributes for lower-level protocols, e.g. if the network
2057 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2058 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2059 * must be absent. */
2060 enum odp_key_fitness
2061 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2062 struct flow *flow)
2063 {
2064 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2065 uint64_t expected_attrs;
2066 uint64_t present_attrs;
2067 int out_of_range_attr;
2068
2069 memset(flow, 0, sizeof *flow);
2070
2071 /* Parse attributes. */
2072 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2073 &out_of_range_attr)) {
2074 return ODP_FIT_ERROR;
2075 }
2076 expected_attrs = 0;
2077
2078 /* Metadata. */
2079 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2080 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2081 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2082 }
2083
2084 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2085 flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2086 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2087 }
2088
2089 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2090 enum odp_key_fitness res;
2091
2092 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2093 if (res == ODP_FIT_ERROR) {
2094 return ODP_FIT_ERROR;
2095 } else if (res == ODP_FIT_PERFECT) {
2096 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2097 }
2098 }
2099
2100 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2101 flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
2102 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2103 } else {
2104 flow->in_port = OVSP_NONE;
2105 }
2106
2107 /* Ethernet header. */
2108 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2109 const struct ovs_key_ethernet *eth_key;
2110
2111 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2112 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2113 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2114 }
2115 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2116
2117 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2118 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2119 return ODP_FIT_ERROR;
2120 }
2121
2122 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2123 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2124 expected_attrs, flow, key, key_len);
2125 }
2126 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2127 expected_attrs, flow, key, key_len);
2128 }
2129
2130 /* Returns 'fitness' as a string, for use in debug messages. */
2131 const char *
2132 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2133 {
2134 switch (fitness) {
2135 case ODP_FIT_PERFECT:
2136 return "OK";
2137 case ODP_FIT_TOO_MUCH:
2138 return "too_much";
2139 case ODP_FIT_TOO_LITTLE:
2140 return "too_little";
2141 case ODP_FIT_ERROR:
2142 return "error";
2143 default:
2144 return "<unknown>";
2145 }
2146 }
2147
2148 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2149 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2150 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2151 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2152 * null, then the return value is not meaningful.) */
2153 size_t
2154 odp_put_userspace_action(uint32_t pid,
2155 const void *userdata, size_t userdata_size,
2156 struct ofpbuf *odp_actions)
2157 {
2158 size_t userdata_ofs;
2159 size_t offset;
2160
2161 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
2162 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
2163 if (userdata) {
2164 userdata_ofs = odp_actions->size + NLA_HDRLEN;
2165 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
2166 userdata, userdata_size);
2167 } else {
2168 userdata_ofs = 0;
2169 }
2170 nl_msg_end_nested(odp_actions, offset);
2171
2172 return userdata_ofs;
2173 }
2174
2175 void
2176 odp_put_tunnel_action(const struct flow_tnl *tunnel,
2177 struct ofpbuf *odp_actions)
2178 {
2179 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2180 tun_key_to_attr(odp_actions, tunnel);
2181 nl_msg_end_nested(odp_actions, offset);
2182 }
2183 \f
2184 /* The commit_odp_actions() function and its helpers. */
2185
2186 static void
2187 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
2188 const void *key, size_t key_size)
2189 {
2190 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2191 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
2192 nl_msg_end_nested(odp_actions, offset);
2193 }
2194
2195 void
2196 odp_put_skb_mark_action(const uint32_t skb_mark,
2197 struct ofpbuf *odp_actions)
2198 {
2199 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
2200 sizeof(skb_mark));
2201 }
2202
2203 /* If any of the flow key data that ODP actions can modify are different in
2204 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2205 * 'odp_actions' that change the flow tunneling information in key from
2206 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2207 * same way. In other words, operates the same as commit_odp_actions(), but
2208 * only on tunneling information. */
2209 void
2210 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
2211 struct ofpbuf *odp_actions)
2212 {
2213 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2214 if (flow->tunnel.ip_dst) {
2215 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
2216 return;
2217 }
2218 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
2219 odp_put_tunnel_action(&base->tunnel, odp_actions);
2220 }
2221 }
2222
2223 static void
2224 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
2225 struct ofpbuf *odp_actions)
2226 {
2227 struct ovs_key_ethernet eth_key;
2228
2229 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2230 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2231 return;
2232 }
2233
2234 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2235 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2236
2237 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2238 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2239
2240 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2241 &eth_key, sizeof(eth_key));
2242 }
2243
2244 static void
2245 commit_vlan_action(const struct flow *flow, struct flow *base,
2246 struct ofpbuf *odp_actions)
2247 {
2248 if (base->vlan_tci == flow->vlan_tci) {
2249 return;
2250 }
2251
2252 if (base->vlan_tci & htons(VLAN_CFI)) {
2253 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2254 }
2255
2256 if (flow->vlan_tci & htons(VLAN_CFI)) {
2257 struct ovs_action_push_vlan vlan;
2258
2259 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2260 vlan.vlan_tci = flow->vlan_tci;
2261 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2262 &vlan, sizeof vlan);
2263 }
2264 base->vlan_tci = flow->vlan_tci;
2265 }
2266
2267 static void
2268 commit_mpls_action(const struct flow *flow, struct flow *base,
2269 struct ofpbuf *odp_actions)
2270 {
2271 if (flow->mpls_lse == base->mpls_lse &&
2272 flow->mpls_depth == base->mpls_depth) {
2273 return;
2274 }
2275
2276 if (flow->mpls_depth < base->mpls_depth) {
2277 if (base->mpls_depth - flow->mpls_depth > 1) {
2278 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2279 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
2280 " a single mpls_pop action");
2281 }
2282
2283 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
2284 } else if (flow->mpls_depth > base->mpls_depth) {
2285 struct ovs_action_push_mpls *mpls;
2286
2287 if (flow->mpls_depth - base->mpls_depth > 1) {
2288 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2289 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
2290 " a single mpls_push action");
2291 }
2292
2293 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
2294 sizeof *mpls);
2295 memset(mpls, 0, sizeof *mpls);
2296 mpls->mpls_ethertype = flow->dl_type;
2297 mpls->mpls_lse = flow->mpls_lse;
2298 } else {
2299 struct ovs_key_mpls mpls_key;
2300
2301 mpls_key.mpls_lse = flow->mpls_lse;
2302 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
2303 &mpls_key, sizeof(mpls_key));
2304 }
2305
2306 base->dl_type = flow->dl_type;
2307 base->mpls_lse = flow->mpls_lse;
2308 base->mpls_depth = flow->mpls_depth;
2309 }
2310
2311 static void
2312 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2313 struct ofpbuf *odp_actions)
2314 {
2315 struct ovs_key_ipv4 ipv4_key;
2316
2317 if (base->nw_src == flow->nw_src &&
2318 base->nw_dst == flow->nw_dst &&
2319 base->nw_tos == flow->nw_tos &&
2320 base->nw_ttl == flow->nw_ttl &&
2321 base->nw_frag == flow->nw_frag) {
2322 return;
2323 }
2324
2325 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2326 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2327 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2328 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2329 ipv4_key.ipv4_proto = base->nw_proto;
2330 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2331
2332 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2333 &ipv4_key, sizeof(ipv4_key));
2334 }
2335
2336 static void
2337 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2338 struct ofpbuf *odp_actions)
2339 {
2340 struct ovs_key_ipv6 ipv6_key;
2341
2342 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2343 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2344 base->ipv6_label == flow->ipv6_label &&
2345 base->nw_tos == flow->nw_tos &&
2346 base->nw_ttl == flow->nw_ttl &&
2347 base->nw_frag == flow->nw_frag) {
2348 return;
2349 }
2350
2351 base->ipv6_src = flow->ipv6_src;
2352 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2353 base->ipv6_dst = flow->ipv6_dst;
2354 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2355
2356 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2357 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2358 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2359 ipv6_key.ipv6_proto = base->nw_proto;
2360 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2361
2362 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2363 &ipv6_key, sizeof(ipv6_key));
2364 }
2365
2366 static void
2367 commit_set_nw_action(const struct flow *flow, struct flow *base,
2368 struct ofpbuf *odp_actions)
2369 {
2370 /* Check if flow really have an IP header. */
2371 if (!flow->nw_proto) {
2372 return;
2373 }
2374
2375 if (base->dl_type == htons(ETH_TYPE_IP)) {
2376 commit_set_ipv4_action(flow, base, odp_actions);
2377 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2378 commit_set_ipv6_action(flow, base, odp_actions);
2379 }
2380 }
2381
2382 static void
2383 commit_set_port_action(const struct flow *flow, struct flow *base,
2384 struct ofpbuf *odp_actions)
2385 {
2386 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
2387 return;
2388 }
2389
2390 if (base->tp_src == flow->tp_src &&
2391 base->tp_dst == flow->tp_dst) {
2392 return;
2393 }
2394
2395 if (flow->nw_proto == IPPROTO_TCP) {
2396 struct ovs_key_tcp port_key;
2397
2398 port_key.tcp_src = base->tp_src = flow->tp_src;
2399 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2400
2401 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2402 &port_key, sizeof(port_key));
2403
2404 } else if (flow->nw_proto == IPPROTO_UDP) {
2405 struct ovs_key_udp port_key;
2406
2407 port_key.udp_src = base->tp_src = flow->tp_src;
2408 port_key.udp_dst = base->tp_dst = flow->tp_dst;
2409
2410 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2411 &port_key, sizeof(port_key));
2412 }
2413 }
2414
2415 static void
2416 commit_set_priority_action(const struct flow *flow, struct flow *base,
2417 struct ofpbuf *odp_actions)
2418 {
2419 if (base->skb_priority == flow->skb_priority) {
2420 return;
2421 }
2422 base->skb_priority = flow->skb_priority;
2423
2424 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2425 &base->skb_priority, sizeof(base->skb_priority));
2426 }
2427
2428 static void
2429 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2430 struct ofpbuf *odp_actions)
2431 {
2432 if (base->skb_mark == flow->skb_mark) {
2433 return;
2434 }
2435 base->skb_mark = flow->skb_mark;
2436
2437 odp_put_skb_mark_action(base->skb_mark, odp_actions);
2438 }
2439 /* If any of the flow key data that ODP actions can modify are different in
2440 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2441 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2442 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2443 * in addition to this function if needed. */
2444 void
2445 commit_odp_actions(const struct flow *flow, struct flow *base,
2446 struct ofpbuf *odp_actions)
2447 {
2448 commit_set_ether_addr_action(flow, base, odp_actions);
2449 commit_vlan_action(flow, base, odp_actions);
2450 commit_set_nw_action(flow, base, odp_actions);
2451 commit_set_port_action(flow, base, odp_actions);
2452 /* Committing MPLS actions should occur after committing nw and port
2453 * actions. This is because committing MPLS actions may alter a packet so
2454 * that it is no longer IP and thus nw and port actions are no longer valid.
2455 */
2456 commit_mpls_action(flow, base, odp_actions);
2457 commit_set_priority_action(flow, base, odp_actions);
2458 commit_set_skb_mark_action(flow, base, odp_actions);
2459 }