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