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