]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
8bfd0fda | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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> | |
a36de779 | 27 | |
cdee00fd | 28 | #include "byte-order.h" |
064af421 | 29 | #include "coverage.h" |
758c456d | 30 | #include "dpif.h" |
064af421 BP |
31 | #include "dynamic-string.h" |
32 | #include "flow.h" | |
cdee00fd | 33 | #include "netlink.h" |
3bffc610 | 34 | #include "ofpbuf.h" |
064af421 | 35 | #include "packets.h" |
44bac24b | 36 | #include "simap.h" |
064af421 | 37 | #include "timeval.h" |
a36de779 | 38 | #include "unaligned.h" |
064af421 | 39 | #include "util.h" |
e6211adc | 40 | #include "openvswitch/vlog.h" |
34118cae BP |
41 | |
42 | VLOG_DEFINE_THIS_MODULE(odp_util); | |
064af421 | 43 | |
df2c07f4 JP |
44 | /* The interface between userspace and kernel uses an "OVS_*" prefix. |
45 | * Since this is fairly non-specific for the OVS userspace components, | |
46 | * "ODP_*" (Open vSwitch Datapath) is used as the prefix for | |
47 | * interactions with the datapath. | |
48 | */ | |
49 | ||
7202cbe5 BP |
50 | /* The set of characters that may separate one action or one key attribute |
51 | * from another. */ | |
52 | static const char *delimiters = ", \t\r\n"; | |
53 | ||
e6cc0bab AZ |
54 | static int parse_odp_key_mask_attr(const char *, const struct simap *port_names, |
55 | struct ofpbuf *, struct ofpbuf *); | |
56 | static void format_odp_key_attr(const struct nlattr *a, | |
0a37839c GS |
57 | const struct nlattr *ma, |
58 | const struct hmap *portno_names, struct ds *ds, | |
041e7168 | 59 | bool verbose); |
a052b51b | 60 | |
98403001 BP |
61 | /* Returns one the following for the action with the given OVS_ACTION_ATTR_* |
62 | * 'type': | |
63 | * | |
64 | * - For an action whose argument has a fixed length, returned that | |
65 | * nonnegative length in bytes. | |
66 | * | |
67 | * - For an action with a variable-length argument, returns -2. | |
68 | * | |
69 | * - For an invalid 'type', returns -1. */ | |
4edb9ae9 | 70 | static int |
cdee00fd BP |
71 | odp_action_len(uint16_t type) |
72 | { | |
df2c07f4 | 73 | if (type > OVS_ACTION_ATTR_MAX) { |
cdee00fd BP |
74 | return -1; |
75 | } | |
76 | ||
09ded0ad | 77 | switch ((enum ovs_action_attr) type) { |
fea393b1 | 78 | case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t); |
a36de779 PS |
79 | case OVS_ACTION_ATTR_TUNNEL_PUSH: return -2; |
80 | case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t); | |
98403001 | 81 | case OVS_ACTION_ATTR_USERSPACE: return -2; |
fea393b1 BP |
82 | case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan); |
83 | case OVS_ACTION_ATTR_POP_VLAN: return 0; | |
b02475c5 SH |
84 | case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls); |
85 | case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16); | |
347bf289 AZ |
86 | case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t); |
87 | case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash); | |
4edb9ae9 | 88 | case OVS_ACTION_ATTR_SET: return -2; |
6d670e7f | 89 | case OVS_ACTION_ATTR_SET_MASKED: return -2; |
98403001 | 90 | case OVS_ACTION_ATTR_SAMPLE: return -2; |
df2c07f4 JP |
91 | |
92 | case OVS_ACTION_ATTR_UNSPEC: | |
93 | case __OVS_ACTION_ATTR_MAX: | |
cdee00fd BP |
94 | return -1; |
95 | } | |
96 | ||
97 | return -1; | |
98 | } | |
99 | ||
e6603631 BP |
100 | /* Returns a string form of 'attr'. The return value is either a statically |
101 | * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize' | |
102 | * should be at least OVS_KEY_ATTR_BUFSIZE. */ | |
103 | enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 }; | |
744791b5 | 104 | static const char * |
e6603631 | 105 | ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) |
744791b5 | 106 | { |
744791b5 BP |
107 | switch (attr) { |
108 | case OVS_KEY_ATTR_UNSPEC: return "unspec"; | |
fea393b1 | 109 | case OVS_KEY_ATTR_ENCAP: return "encap"; |
1b567fb9 | 110 | case OVS_KEY_ATTR_PRIORITY: return "skb_priority"; |
72e8bf28 | 111 | case OVS_KEY_ATTR_SKB_MARK: return "skb_mark"; |
9b405f1a | 112 | case OVS_KEY_ATTR_TUNNEL: return "tunnel"; |
744791b5 BP |
113 | case OVS_KEY_ATTR_IN_PORT: return "in_port"; |
114 | case OVS_KEY_ATTR_ETHERNET: return "eth"; | |
fea393b1 | 115 | case OVS_KEY_ATTR_VLAN: return "vlan"; |
744791b5 BP |
116 | case OVS_KEY_ATTR_ETHERTYPE: return "eth_type"; |
117 | case OVS_KEY_ATTR_IPV4: return "ipv4"; | |
118 | case OVS_KEY_ATTR_IPV6: return "ipv6"; | |
119 | case OVS_KEY_ATTR_TCP: return "tcp"; | |
dc235f7f | 120 | case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags"; |
744791b5 | 121 | case OVS_KEY_ATTR_UDP: return "udp"; |
c6bcb685 | 122 | case OVS_KEY_ATTR_SCTP: return "sctp"; |
744791b5 BP |
123 | case OVS_KEY_ATTR_ICMP: return "icmp"; |
124 | case OVS_KEY_ATTR_ICMPV6: return "icmpv6"; | |
125 | case OVS_KEY_ATTR_ARP: return "arp"; | |
126 | case OVS_KEY_ATTR_ND: return "nd"; | |
b02475c5 | 127 | case OVS_KEY_ATTR_MPLS: return "mpls"; |
572f732a AZ |
128 | case OVS_KEY_ATTR_DP_HASH: return "dp_hash"; |
129 | case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id"; | |
744791b5 BP |
130 | |
131 | case __OVS_KEY_ATTR_MAX: | |
132 | default: | |
e6603631 BP |
133 | snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); |
134 | return namebuf; | |
744791b5 BP |
135 | } |
136 | } | |
137 | ||
cdee00fd BP |
138 | static void |
139 | format_generic_odp_action(struct ds *ds, const struct nlattr *a) | |
140 | { | |
8ba43fbd BP |
141 | size_t len = nl_attr_get_size(a); |
142 | ||
cdee00fd | 143 | ds_put_format(ds, "action%"PRId16, nl_attr_type(a)); |
8ba43fbd | 144 | if (len) { |
cdee00fd BP |
145 | const uint8_t *unspec; |
146 | unsigned int i; | |
147 | ||
148 | unspec = nl_attr_get(a); | |
8ba43fbd | 149 | for (i = 0; i < len; i++) { |
cdee00fd BP |
150 | ds_put_char(ds, i ? ' ': '('); |
151 | ds_put_format(ds, "%02x", unspec[i]); | |
152 | } | |
153 | ds_put_char(ds, ')'); | |
154 | } | |
155 | } | |
156 | ||
6ff686f2 PS |
157 | static void |
158 | format_odp_sample_action(struct ds *ds, const struct nlattr *attr) | |
159 | { | |
160 | static const struct nl_policy ovs_sample_policy[] = { | |
3548d242 BP |
161 | [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 }, |
162 | [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED } | |
6ff686f2 PS |
163 | }; |
164 | struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)]; | |
6ff686f2 PS |
165 | double percentage; |
166 | const struct nlattr *nla_acts; | |
167 | int len; | |
168 | ||
169 | ds_put_cstr(ds, "sample"); | |
170 | ||
5621afff | 171 | if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) { |
6ff686f2 PS |
172 | ds_put_cstr(ds, "(error)"); |
173 | return; | |
174 | } | |
175 | ||
176 | percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) / | |
177 | UINT32_MAX; | |
178 | ||
179 | ds_put_format(ds, "(sample=%.1f%%,", percentage); | |
180 | ||
181 | ds_put_cstr(ds, "actions("); | |
182 | nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
183 | len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
184 | format_odp_actions(ds, nla_acts, len); | |
185 | ds_put_format(ds, "))"); | |
186 | } | |
187 | ||
6a7e895f | 188 | static const char * |
04594cd5 | 189 | slow_path_reason_to_string(uint32_t reason) |
6a7e895f | 190 | { |
04594cd5 BP |
191 | switch ((enum slow_path_reason) reason) { |
192 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING; | |
193 | SLOW_PATH_REASONS | |
194 | #undef SPR | |
6a7e895f | 195 | } |
04594cd5 BP |
196 | |
197 | return NULL; | |
6a7e895f BP |
198 | } |
199 | ||
04594cd5 BP |
200 | const char * |
201 | slow_path_reason_to_explanation(enum slow_path_reason reason) | |
98f0520f | 202 | { |
04594cd5 BP |
203 | switch (reason) { |
204 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION; | |
205 | SLOW_PATH_REASONS | |
206 | #undef SPR | |
98f0520f EJ |
207 | } |
208 | ||
04594cd5 | 209 | return "<unknown>"; |
98f0520f EJ |
210 | } |
211 | ||
ec956fc7 PS |
212 | static int |
213 | parse_flags(const char *s, const char *(*bit_to_string)(uint32_t), | |
ea2735d3 | 214 | uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask) |
ec956fc7 PS |
215 | { |
216 | uint32_t result = 0; | |
ea2735d3 | 217 | int n; |
ec956fc7 | 218 | |
ea2735d3 | 219 | /* Parse masked flags in numeric format? */ |
2d18eae8 | 220 | if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n", |
ea2735d3 JR |
221 | res_flags, res_mask, &n) && n > 0) { |
222 | if (*res_flags & ~allowed || *res_mask & ~allowed) { | |
223 | return -EINVAL; | |
224 | } | |
225 | return n; | |
226 | } | |
227 | ||
2d18eae8 | 228 | n = 0; |
ec956fc7 | 229 | |
2d18eae8 | 230 | if (res_mask && (*s == '+' || *s == '-')) { |
ea2735d3 JR |
231 | uint32_t flags = 0, mask = 0; |
232 | ||
233 | /* Parse masked flags. */ | |
125fc637 | 234 | while (s[0] != ')') { |
ea2735d3 JR |
235 | bool set; |
236 | uint32_t bit; | |
237 | int name_len; | |
238 | ||
125fc637 | 239 | if (s[0] == '+') { |
ea2735d3 | 240 | set = true; |
125fc637 | 241 | } else if (s[0] == '-') { |
ea2735d3 JR |
242 | set = false; |
243 | } else { | |
244 | return -EINVAL; | |
245 | } | |
125fc637 | 246 | s++; |
ea2735d3 JR |
247 | n++; |
248 | ||
125fc637 | 249 | name_len = strcspn(s, "+-)"); |
ea2735d3 JR |
250 | |
251 | for (bit = 1; bit; bit <<= 1) { | |
252 | const char *fname = bit_to_string(bit); | |
253 | size_t len; | |
254 | ||
255 | if (!fname) { | |
256 | continue; | |
257 | } | |
258 | ||
259 | len = strlen(fname); | |
260 | if (len != name_len) { | |
261 | continue; | |
262 | } | |
125fc637 | 263 | if (!strncmp(s, fname, len)) { |
ea2735d3 JR |
264 | if (mask & bit) { |
265 | /* bit already set. */ | |
266 | return -EINVAL; | |
267 | } | |
268 | if (!(bit & allowed)) { | |
269 | return -EINVAL; | |
270 | } | |
271 | if (set) { | |
272 | flags |= bit; | |
273 | } | |
274 | mask |= bit; | |
275 | break; | |
276 | } | |
277 | } | |
278 | ||
279 | if (!bit) { | |
280 | return -EINVAL; /* Unknown flag name */ | |
281 | } | |
282 | s += name_len; | |
125fc637 | 283 | n += name_len; |
ea2735d3 | 284 | } |
ea2735d3 JR |
285 | |
286 | *res_flags = flags; | |
287 | *res_mask = mask; | |
288 | return n; | |
289 | } | |
290 | ||
291 | /* Parse unmasked flags. If a flag is present, it is set, otherwise | |
292 | * it is not set. */ | |
ec956fc7 PS |
293 | while (s[n] != ')') { |
294 | unsigned long long int flags; | |
295 | uint32_t bit; | |
296 | int n0; | |
297 | ||
c2c28dfd | 298 | if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) { |
ea2735d3 JR |
299 | if (flags & ~allowed) { |
300 | return -EINVAL; | |
301 | } | |
ec956fc7 PS |
302 | n += n0 + (s[n + n0] == ','); |
303 | result |= flags; | |
304 | continue; | |
305 | } | |
306 | ||
307 | for (bit = 1; bit; bit <<= 1) { | |
308 | const char *name = bit_to_string(bit); | |
309 | size_t len; | |
310 | ||
311 | if (!name) { | |
312 | continue; | |
313 | } | |
314 | ||
315 | len = strlen(name); | |
316 | if (!strncmp(s + n, name, len) && | |
317 | (s[n + len] == ',' || s[n + len] == ')')) { | |
ea2735d3 JR |
318 | if (!(bit & allowed)) { |
319 | return -EINVAL; | |
320 | } | |
ec956fc7 PS |
321 | result |= bit; |
322 | n += len + (s[n + len] == ','); | |
323 | break; | |
324 | } | |
325 | } | |
326 | ||
327 | if (!bit) { | |
328 | return -EINVAL; | |
329 | } | |
330 | } | |
ec956fc7 | 331 | |
ea2735d3 JR |
332 | *res_flags = result; |
333 | if (res_mask) { | |
334 | *res_mask = UINT32_MAX; | |
335 | } | |
ec956fc7 | 336 | return n; |
6a7e895f BP |
337 | } |
338 | ||
98403001 BP |
339 | static void |
340 | format_odp_userspace_action(struct ds *ds, const struct nlattr *attr) | |
341 | { | |
342 | static const struct nl_policy ovs_userspace_policy[] = { | |
3548d242 BP |
343 | [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 }, |
344 | [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC, | |
345 | .optional = true }, | |
8b7ea2d4 WZ |
346 | [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32, |
347 | .optional = true }, | |
98403001 BP |
348 | }; |
349 | struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)]; | |
e995e3df | 350 | const struct nlattr *userdata_attr; |
8b7ea2d4 | 351 | const struct nlattr *tunnel_out_port_attr; |
98403001 BP |
352 | |
353 | if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) { | |
354 | ds_put_cstr(ds, "userspace(error)"); | |
355 | return; | |
356 | } | |
357 | ||
358 | ds_put_format(ds, "userspace(pid=%"PRIu32, | |
359 | nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID])); | |
360 | ||
e995e3df | 361 | userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA]; |
29089a54 RL |
362 | |
363 | if (userdata_attr) { | |
364 | const uint8_t *userdata = nl_attr_get(userdata_attr); | |
365 | size_t userdata_len = nl_attr_get_size(userdata_attr); | |
366 | bool userdata_unspec = true; | |
1673e0e4 | 367 | union user_action_cookie cookie; |
98403001 | 368 | |
29089a54 RL |
369 | if (userdata_len >= sizeof cookie.type |
370 | && userdata_len <= sizeof cookie) { | |
371 | ||
372 | memset(&cookie, 0, sizeof cookie); | |
373 | memcpy(&cookie, userdata, userdata_len); | |
374 | ||
375 | userdata_unspec = false; | |
376 | ||
377 | if (userdata_len == sizeof cookie.sflow | |
378 | && cookie.type == USER_ACTION_COOKIE_SFLOW) { | |
379 | ds_put_format(ds, ",sFlow(" | |
380 | "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")", | |
381 | vlan_tci_to_vid(cookie.sflow.vlan_tci), | |
382 | vlan_tci_to_pcp(cookie.sflow.vlan_tci), | |
383 | cookie.sflow.output); | |
384 | } else if (userdata_len == sizeof cookie.slow_path | |
385 | && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) { | |
04594cd5 BP |
386 | ds_put_cstr(ds, ",slow_path("); |
387 | format_flags(ds, slow_path_reason_to_string, | |
388 | cookie.slow_path.reason, ','); | |
389 | ds_put_format(ds, ")"); | |
29089a54 RL |
390 | } else if (userdata_len == sizeof cookie.flow_sample |
391 | && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) { | |
392 | ds_put_format(ds, ",flow_sample(probability=%"PRIu16 | |
393 | ",collector_set_id=%"PRIu32 | |
394 | ",obs_domain_id=%"PRIu32 | |
395 | ",obs_point_id=%"PRIu32")", | |
396 | cookie.flow_sample.probability, | |
397 | cookie.flow_sample.collector_set_id, | |
398 | cookie.flow_sample.obs_domain_id, | |
399 | cookie.flow_sample.obs_point_id); | |
b075a9b8 | 400 | } else if (userdata_len >= sizeof cookie.ipfix |
29089a54 | 401 | && cookie.type == USER_ACTION_COOKIE_IPFIX) { |
8b7ea2d4 WZ |
402 | ds_put_format(ds, ",ipfix(output_port=%"PRIu32")", |
403 | cookie.ipfix.output_odp_port); | |
29089a54 RL |
404 | } else { |
405 | userdata_unspec = true; | |
406 | } | |
98403001 | 407 | } |
e995e3df | 408 | |
29089a54 RL |
409 | if (userdata_unspec) { |
410 | size_t i; | |
411 | ds_put_format(ds, ",userdata("); | |
412 | for (i = 0; i < userdata_len; i++) { | |
413 | ds_put_format(ds, "%02x", userdata[i]); | |
414 | } | |
415 | ds_put_char(ds, ')'); | |
e995e3df | 416 | } |
98403001 BP |
417 | } |
418 | ||
8b7ea2d4 WZ |
419 | tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT]; |
420 | if (tunnel_out_port_attr) { | |
421 | ds_put_format(ds, ",tunnel_out_port=%"PRIu32, | |
422 | nl_attr_get_u32(tunnel_out_port_attr)); | |
423 | } | |
424 | ||
98403001 BP |
425 | ds_put_char(ds, ')'); |
426 | } | |
427 | ||
8ddc056d | 428 | static void |
2d18eae8 | 429 | format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose) |
8ddc056d | 430 | { |
2d18eae8 JR |
431 | if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) { |
432 | ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci)); | |
433 | if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */ | |
434 | ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask)); | |
435 | }; | |
436 | ds_put_char(ds, ','); | |
437 | } | |
438 | if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) { | |
439 | ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci)); | |
440 | if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) { | |
441 | ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask)); | |
442 | } | |
443 | ds_put_char(ds, ','); | |
444 | } | |
445 | if (!(tci & htons(VLAN_CFI))) { | |
446 | ds_put_cstr(ds, "cfi=0"); | |
447 | ds_put_char(ds, ','); | |
8ddc056d | 448 | } |
2d18eae8 | 449 | ds_chomp(ds, ','); |
8ddc056d BP |
450 | } |
451 | ||
b02475c5 SH |
452 | static void |
453 | format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse) | |
454 | { | |
455 | ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d", | |
456 | mpls_lse_to_label(mpls_lse), | |
457 | mpls_lse_to_tc(mpls_lse), | |
458 | mpls_lse_to_ttl(mpls_lse), | |
459 | mpls_lse_to_bos(mpls_lse)); | |
460 | } | |
461 | ||
e6cc0bab AZ |
462 | static void |
463 | format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key, | |
8bfd0fda | 464 | const struct ovs_key_mpls *mpls_mask, int n) |
e6cc0bab | 465 | { |
8bfd0fda BP |
466 | if (n == 1) { |
467 | ovs_be32 key = mpls_key->mpls_lse; | |
e6cc0bab | 468 | |
8bfd0fda BP |
469 | if (mpls_mask == NULL) { |
470 | format_mpls_lse(ds, key); | |
471 | } else { | |
472 | ovs_be32 mask = mpls_mask->mpls_lse; | |
473 | ||
474 | ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", | |
475 | mpls_lse_to_label(key), mpls_lse_to_label(mask), | |
476 | mpls_lse_to_tc(key), mpls_lse_to_tc(mask), | |
477 | mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), | |
478 | mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); | |
479 | } | |
e6cc0bab | 480 | } else { |
8bfd0fda | 481 | int i; |
e6cc0bab | 482 | |
8bfd0fda BP |
483 | for (i = 0; i < n; i++) { |
484 | ds_put_format(ds, "lse%d=%#"PRIx32, | |
485 | i, ntohl(mpls_key[i].mpls_lse)); | |
486 | if (mpls_mask) { | |
487 | ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse)); | |
488 | } | |
489 | ds_put_char(ds, ','); | |
490 | } | |
491 | ds_chomp(ds, ','); | |
e6cc0bab AZ |
492 | } |
493 | } | |
494 | ||
572f732a | 495 | static void |
347bf289 | 496 | format_odp_recirc_action(struct ds *ds, uint32_t recirc_id) |
572f732a | 497 | { |
347bf289 AZ |
498 | ds_put_format(ds, "recirc(%"PRIu32")", recirc_id); |
499 | } | |
572f732a | 500 | |
347bf289 AZ |
501 | static void |
502 | format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act) | |
503 | { | |
504 | ds_put_format(ds, "hash("); | |
572f732a | 505 | |
347bf289 | 506 | if (hash_act->hash_alg == OVS_HASH_ALG_L4) { |
62ac1f20 | 507 | ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis); |
347bf289 AZ |
508 | } else { |
509 | ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")", | |
510 | hash_act->hash_alg); | |
511 | } | |
512 | ds_put_format(ds, ")"); | |
572f732a AZ |
513 | } |
514 | ||
e066f78f JG |
515 | static const void * |
516 | format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip) | |
517 | { | |
518 | const struct udp_header *udp; | |
519 | ||
520 | udp = (const struct udp_header *) (ip + 1); | |
8e45fe7c JG |
521 | ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),", |
522 | ntohs(udp->udp_src), ntohs(udp->udp_dst), | |
523 | ntohs(udp->udp_csum)); | |
e066f78f JG |
524 | |
525 | return udp + 1; | |
526 | } | |
527 | ||
a36de779 PS |
528 | static void |
529 | format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data) | |
530 | { | |
531 | const struct eth_header *eth; | |
532 | const struct ip_header *ip; | |
533 | const void *l3; | |
534 | ||
535 | eth = (const struct eth_header *)data->header; | |
536 | ||
537 | l3 = eth + 1; | |
538 | ip = (const struct ip_header *)l3; | |
539 | ||
540 | /* Ethernet */ | |
541 | ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=", | |
542 | data->header_len, data->tnl_type); | |
543 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst)); | |
544 | ds_put_format(ds, ",src="); | |
545 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src)); | |
546 | ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type)); | |
547 | ||
548 | /* IPv4 */ | |
549 | ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8 | |
550 | ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),", | |
551 | IP_ARGS(get_16aligned_be32(&ip->ip_src)), | |
552 | IP_ARGS(get_16aligned_be32(&ip->ip_dst)), | |
553 | ip->ip_proto, ip->ip_tos, | |
554 | ip->ip_ttl, | |
555 | ip->ip_frag_off); | |
556 | ||
557 | if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) { | |
558 | const struct vxlanhdr *vxh; | |
a36de779 | 559 | |
e066f78f | 560 | vxh = format_udp_tnl_push_header(ds, ip); |
a36de779 | 561 | |
a36de779 PS |
562 | ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")", |
563 | ntohl(get_16aligned_be32(&vxh->vx_flags)), | |
a92a3aae | 564 | ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8); |
e5a1caee JG |
565 | } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) { |
566 | const struct genevehdr *gnh; | |
567 | ||
568 | gnh = format_udp_tnl_push_header(ds, ip); | |
569 | ||
570 | ds_put_format(ds, "geneve(%svni=0x%"PRIx32")", | |
571 | gnh->oam ? "oam," : "", | |
572 | ntohl(get_16aligned_be32(&gnh->vni)) >> 8); | |
a36de779 PS |
573 | } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) { |
574 | const struct gre_base_hdr *greh; | |
575 | ovs_16aligned_be32 *options; | |
576 | void *l4; | |
577 | ||
578 | l4 = ((uint8_t *)l3 + sizeof(struct ip_header)); | |
579 | greh = (const struct gre_base_hdr *) l4; | |
580 | ||
581 | ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")", | |
e8fe6ad0 | 582 | ntohs(greh->flags), ntohs(greh->protocol)); |
a36de779 PS |
583 | options = (ovs_16aligned_be32 *)(greh + 1); |
584 | if (greh->flags & htons(GRE_CSUM)) { | |
d804d31e | 585 | ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options))); |
a36de779 PS |
586 | options++; |
587 | } | |
588 | if (greh->flags & htons(GRE_KEY)) { | |
589 | ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options))); | |
590 | options++; | |
591 | } | |
592 | if (greh->flags & htons(GRE_SEQ)) { | |
593 | ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options))); | |
594 | options++; | |
595 | } | |
596 | ds_put_format(ds, ")"); | |
597 | } | |
598 | ds_put_format(ds, ")"); | |
599 | } | |
600 | ||
601 | static void | |
602 | format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr) | |
603 | { | |
604 | struct ovs_action_push_tnl *data; | |
605 | ||
606 | data = (struct ovs_action_push_tnl *) nl_attr_get(attr); | |
607 | ||
608 | ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port); | |
609 | format_odp_tnl_push_header(ds, data); | |
610 | ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port); | |
611 | } | |
612 | ||
4edb9ae9 | 613 | static void |
cdee00fd | 614 | format_odp_action(struct ds *ds, const struct nlattr *a) |
064af421 | 615 | { |
98403001 | 616 | int expected_len; |
4edb9ae9 | 617 | enum ovs_action_attr type = nl_attr_type(a); |
fea393b1 | 618 | const struct ovs_action_push_vlan *vlan; |
6d670e7f | 619 | size_t size; |
cdee00fd | 620 | |
98403001 BP |
621 | expected_len = odp_action_len(nl_attr_type(a)); |
622 | if (expected_len != -2 && nl_attr_get_size(a) != expected_len) { | |
34582733 | 623 | ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ", |
98403001 | 624 | nl_attr_get_size(a), expected_len); |
cdee00fd BP |
625 | format_generic_odp_action(ds, a); |
626 | return; | |
627 | } | |
628 | ||
4edb9ae9 | 629 | switch (type) { |
df2c07f4 | 630 | case OVS_ACTION_ATTR_OUTPUT: |
26ce7705 | 631 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); |
064af421 | 632 | break; |
a36de779 PS |
633 | case OVS_ACTION_ATTR_TUNNEL_POP: |
634 | ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a)); | |
635 | break; | |
636 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
637 | format_odp_tnl_push_action(ds, a); | |
638 | break; | |
df2c07f4 | 639 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 640 | format_odp_userspace_action(ds, a); |
064af421 | 641 | break; |
572f732a | 642 | case OVS_ACTION_ATTR_RECIRC: |
347bf289 AZ |
643 | format_odp_recirc_action(ds, nl_attr_get_u32(a)); |
644 | break; | |
645 | case OVS_ACTION_ATTR_HASH: | |
646 | format_odp_hash_action(ds, nl_attr_get(a)); | |
572f732a | 647 | break; |
6d670e7f JR |
648 | case OVS_ACTION_ATTR_SET_MASKED: |
649 | a = nl_attr_get(a); | |
650 | size = nl_attr_get_size(a) / 2; | |
651 | ds_put_cstr(ds, "set("); | |
652 | ||
653 | /* Masked set action not supported for tunnel key, which is bigger. */ | |
654 | if (size <= sizeof(struct ovs_key_ipv6)) { | |
655 | struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6), | |
656 | sizeof(struct nlattr))]; | |
657 | struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6), | |
658 | sizeof(struct nlattr))]; | |
659 | ||
660 | mask->nla_type = attr->nla_type = nl_attr_type(a); | |
661 | mask->nla_len = attr->nla_len = NLA_HDRLEN + size; | |
662 | memcpy(attr + 1, (char *)(a + 1), size); | |
663 | memcpy(mask + 1, (char *)(a + 1) + size, size); | |
2d18eae8 | 664 | format_odp_key_attr(attr, mask, NULL, ds, false); |
6d670e7f | 665 | } else { |
2d18eae8 | 666 | format_odp_key_attr(a, NULL, NULL, ds, false); |
6d670e7f JR |
667 | } |
668 | ds_put_cstr(ds, ")"); | |
669 | break; | |
4edb9ae9 PS |
670 | case OVS_ACTION_ATTR_SET: |
671 | ds_put_cstr(ds, "set("); | |
0a37839c | 672 | format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true); |
4edb9ae9 | 673 | ds_put_cstr(ds, ")"); |
064af421 | 674 | break; |
fea393b1 BP |
675 | case OVS_ACTION_ATTR_PUSH_VLAN: |
676 | vlan = nl_attr_get(a); | |
677 | ds_put_cstr(ds, "push_vlan("); | |
678 | if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) { | |
679 | ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid)); | |
680 | } | |
2d18eae8 | 681 | format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false); |
8ddc056d | 682 | ds_put_char(ds, ')'); |
064af421 | 683 | break; |
fea393b1 BP |
684 | case OVS_ACTION_ATTR_POP_VLAN: |
685 | ds_put_cstr(ds, "pop_vlan"); | |
064af421 | 686 | break; |
b02475c5 SH |
687 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
688 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
689 | ds_put_cstr(ds, "push_mpls("); | |
690 | format_mpls_lse(ds, mpls->mpls_lse); | |
691 | ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype)); | |
692 | break; | |
693 | } | |
694 | case OVS_ACTION_ATTR_POP_MPLS: { | |
695 | ovs_be16 ethertype = nl_attr_get_be16(a); | |
696 | ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype)); | |
697 | break; | |
698 | } | |
6ff686f2 PS |
699 | case OVS_ACTION_ATTR_SAMPLE: |
700 | format_odp_sample_action(ds, a); | |
701 | break; | |
4edb9ae9 PS |
702 | case OVS_ACTION_ATTR_UNSPEC: |
703 | case __OVS_ACTION_ATTR_MAX: | |
064af421 | 704 | default: |
cdee00fd | 705 | format_generic_odp_action(ds, a); |
064af421 BP |
706 | break; |
707 | } | |
708 | } | |
709 | ||
710 | void | |
cdee00fd | 711 | format_odp_actions(struct ds *ds, const struct nlattr *actions, |
cf22f8cb | 712 | size_t actions_len) |
064af421 | 713 | { |
cdee00fd BP |
714 | if (actions_len) { |
715 | const struct nlattr *a; | |
716 | unsigned int left; | |
717 | ||
718 | NL_ATTR_FOR_EACH (a, left, actions, actions_len) { | |
719 | if (a != actions) { | |
720 | ds_put_char(ds, ','); | |
721 | } | |
722 | format_odp_action(ds, a); | |
064af421 | 723 | } |
cdee00fd | 724 | if (left) { |
3a8cfc50 BP |
725 | int i; |
726 | ||
3476fce3 BP |
727 | if (left == actions_len) { |
728 | ds_put_cstr(ds, "<empty>"); | |
729 | } | |
3a8cfc50 BP |
730 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
731 | for (i = 0; i < left; i++) { | |
732 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
733 | } | |
734 | ds_put_char(ds, ')'); | |
cdee00fd BP |
735 | } |
736 | } else { | |
064af421 BP |
737 | ds_put_cstr(ds, "drop"); |
738 | } | |
739 | } | |
7202cbe5 | 740 | |
8b7ea2d4 | 741 | /* Separate out parse_odp_userspace_action() function. */ |
7202cbe5 | 742 | static int |
8b7ea2d4 | 743 | parse_odp_userspace_action(const char *s, struct ofpbuf *actions) |
7202cbe5 | 744 | { |
8b7ea2d4 WZ |
745 | uint32_t pid; |
746 | union user_action_cookie cookie; | |
747 | struct ofpbuf buf; | |
748 | odp_port_t tunnel_out_port; | |
749 | int n = -1; | |
750 | void *user_data = NULL; | |
751 | size_t user_data_size = 0; | |
752 | ||
753 | if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) { | |
754 | return -EINVAL; | |
7202cbe5 BP |
755 | } |
756 | ||
757 | { | |
c2c28dfd BP |
758 | uint32_t output; |
759 | uint32_t probability; | |
760 | uint32_t collector_set_id; | |
761 | uint32_t obs_domain_id; | |
762 | uint32_t obs_point_id; | |
7202cbe5 | 763 | int vid, pcp; |
8b7ea2d4 WZ |
764 | int n1 = -1; |
765 | if (ovs_scan(&s[n], ",sFlow(vid=%i," | |
766 | "pcp=%i,output=%"SCNi32")%n", | |
767 | &vid, &pcp, &output, &n1)) { | |
7202cbe5 BP |
768 | uint16_t tci; |
769 | ||
8b7ea2d4 | 770 | n += n1; |
7202cbe5 BP |
771 | tci = vid | (pcp << VLAN_PCP_SHIFT); |
772 | if (tci) { | |
773 | tci |= VLAN_CFI; | |
774 | } | |
775 | ||
776 | cookie.type = USER_ACTION_COOKIE_SFLOW; | |
1673e0e4 BP |
777 | cookie.sflow.vlan_tci = htons(tci); |
778 | cookie.sflow.output = output; | |
8b7ea2d4 WZ |
779 | user_data = &cookie; |
780 | user_data_size = sizeof cookie.sflow; | |
2d18eae8 | 781 | } else if (ovs_scan(&s[n], ",slow_path(%n", |
8b7ea2d4 | 782 | &n1)) { |
04594cd5 | 783 | int res; |
6a7e895f | 784 | |
8b7ea2d4 | 785 | n += n1; |
6a7e895f BP |
786 | cookie.type = USER_ACTION_COOKIE_SLOW_PATH; |
787 | cookie.slow_path.unused = 0; | |
04594cd5 | 788 | cookie.slow_path.reason = 0; |
6a7e895f | 789 | |
04594cd5 | 790 | res = parse_flags(&s[n], slow_path_reason_to_string, |
ea2735d3 JR |
791 | &cookie.slow_path.reason, |
792 | SLOW_PATH_REASON_MASK, NULL); | |
2d18eae8 | 793 | if (res < 0 || s[n + res] != ')') { |
04594cd5 BP |
794 | return res; |
795 | } | |
2d18eae8 | 796 | n += res + 1; |
6a7e895f | 797 | |
8b7ea2d4 WZ |
798 | user_data = &cookie; |
799 | user_data_size = sizeof cookie.slow_path; | |
800 | } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32"," | |
c2c28dfd BP |
801 | "collector_set_id=%"SCNi32"," |
802 | "obs_domain_id=%"SCNi32"," | |
8b7ea2d4 WZ |
803 | "obs_point_id=%"SCNi32")%n", |
804 | &probability, &collector_set_id, | |
805 | &obs_domain_id, &obs_point_id, &n1)) { | |
806 | n += n1; | |
29089a54 RL |
807 | |
808 | cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE; | |
809 | cookie.flow_sample.probability = probability; | |
810 | cookie.flow_sample.collector_set_id = collector_set_id; | |
811 | cookie.flow_sample.obs_domain_id = obs_domain_id; | |
812 | cookie.flow_sample.obs_point_id = obs_point_id; | |
8b7ea2d4 WZ |
813 | user_data = &cookie; |
814 | user_data_size = sizeof cookie.flow_sample; | |
815 | } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n", | |
816 | &output, &n1) ) { | |
817 | n += n1; | |
29089a54 | 818 | cookie.type = USER_ACTION_COOKIE_IPFIX; |
8b7ea2d4 WZ |
819 | cookie.ipfix.output_odp_port = u32_to_odp(output); |
820 | user_data = &cookie; | |
821 | user_data_size = sizeof cookie.ipfix; | |
822 | } else if (ovs_scan(&s[n], ",userdata(%n", | |
823 | &n1)) { | |
e995e3df BP |
824 | char *end; |
825 | ||
8b7ea2d4 | 826 | n += n1; |
e995e3df BP |
827 | ofpbuf_init(&buf, 16); |
828 | end = ofpbuf_put_hex(&buf, &s[n], NULL); | |
8b7ea2d4 WZ |
829 | if (end[0] != ')') { |
830 | return -EINVAL; | |
e995e3df | 831 | } |
6fd6ed71 PS |
832 | user_data = buf.data; |
833 | user_data_size = buf.size; | |
8b7ea2d4 WZ |
834 | n = (end + 1) - s; |
835 | } | |
836 | } | |
837 | ||
838 | { | |
839 | int n1 = -1; | |
840 | if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n", | |
841 | &tunnel_out_port, &n1)) { | |
842 | odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions); | |
843 | return n + n1; | |
844 | } else if (s[n] == ')') { | |
845 | odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions); | |
846 | return n + 1; | |
847 | } | |
848 | } | |
849 | ||
850 | return -EINVAL; | |
851 | } | |
852 | ||
a36de779 PS |
853 | static int |
854 | ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data) | |
855 | { | |
856 | struct eth_header *eth; | |
857 | struct ip_header *ip; | |
858 | struct udp_header *udp; | |
859 | struct gre_base_hdr *greh; | |
e8fe6ad0 | 860 | uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum; |
a36de779 PS |
861 | ovs_be32 sip, dip; |
862 | uint32_t tnl_type = 0, header_len = 0; | |
863 | void *l3, *l4; | |
864 | int n = 0; | |
865 | ||
866 | if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) { | |
867 | return -EINVAL; | |
868 | } | |
869 | eth = (struct eth_header *) data->header; | |
870 | l3 = (data->header + sizeof *eth); | |
871 | l4 = ((uint8_t *) l3 + sizeof (struct ip_header)); | |
872 | ip = (struct ip_header *) l3; | |
873 | if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32"," | |
874 | "eth(dst="ETH_ADDR_SCAN_FMT",", | |
875 | &data->header_len, | |
876 | &data->tnl_type, | |
877 | ETH_ADDR_SCAN_ARGS(eth->eth_dst))) { | |
878 | return -EINVAL; | |
879 | } | |
880 | ||
881 | if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",", | |
882 | ETH_ADDR_SCAN_ARGS(eth->eth_src))) { | |
883 | return -EINVAL; | |
884 | } | |
885 | if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) { | |
886 | return -EINVAL; | |
887 | } | |
888 | eth->eth_type = htons(dl_type); | |
889 | ||
890 | /* IPv4 */ | |
891 | if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8 | |
892 | ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),", | |
893 | IP_SCAN_ARGS(&sip), | |
894 | IP_SCAN_ARGS(&dip), | |
895 | &ip->ip_proto, &ip->ip_tos, | |
896 | &ip->ip_ttl, &ip->ip_frag_off)) { | |
897 | return -EINVAL; | |
898 | } | |
899 | put_16aligned_be32(&ip->ip_src, sip); | |
900 | put_16aligned_be32(&ip->ip_dst, dip); | |
901 | ||
902 | /* Tunnel header */ | |
903 | udp = (struct udp_header *) l4; | |
904 | greh = (struct gre_base_hdr *) l4; | |
8e45fe7c JG |
905 | if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),", |
906 | &udp_src, &udp_dst, &csum)) { | |
e5a1caee | 907 | uint32_t vx_flags, vni; |
a36de779 PS |
908 | |
909 | udp->udp_src = htons(udp_src); | |
910 | udp->udp_dst = htons(udp_dst); | |
911 | udp->udp_len = 0; | |
8e45fe7c | 912 | udp->udp_csum = htons(csum); |
a36de779 | 913 | |
e066f78f | 914 | if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))", |
e5a1caee | 915 | &vx_flags, &vni)) { |
e066f78f JG |
916 | struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1); |
917 | ||
918 | put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags)); | |
e5a1caee | 919 | put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8)); |
e066f78f JG |
920 | tnl_type = OVS_VPORT_TYPE_VXLAN; |
921 | header_len = sizeof *eth + sizeof *ip + | |
922 | sizeof *udp + sizeof *vxh; | |
e5a1caee JG |
923 | } else if (ovs_scan_len(s, &n, "geneve(")) { |
924 | struct genevehdr *gnh = (struct genevehdr *) (udp + 1); | |
925 | ||
46e7137c | 926 | memset(gnh, 0, sizeof *gnh); |
e5a1caee JG |
927 | if (ovs_scan_len(s, &n, "oam,")) { |
928 | gnh->oam = 1; | |
929 | } | |
930 | if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) { | |
931 | return -EINVAL; | |
932 | } | |
933 | gnh->proto_type = htons(ETH_TYPE_TEB); | |
934 | put_16aligned_be32(&gnh->vni, htonl(vni << 8)); | |
935 | tnl_type = OVS_VPORT_TYPE_GENEVE; | |
936 | header_len = sizeof *eth + sizeof *ip + | |
937 | sizeof *udp + sizeof *gnh; | |
e066f78f | 938 | } else { |
a36de779 PS |
939 | return -EINVAL; |
940 | } | |
a36de779 | 941 | } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")", |
e8fe6ad0 | 942 | &gre_flags, &gre_proto)){ |
a36de779 PS |
943 | |
944 | tnl_type = OVS_VPORT_TYPE_GRE; | |
e8fe6ad0 | 945 | greh->flags = htons(gre_flags); |
a36de779 PS |
946 | greh->protocol = htons(gre_proto); |
947 | ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1); | |
948 | ||
949 | if (greh->flags & htons(GRE_CSUM)) { | |
d804d31e | 950 | if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) { |
a36de779 PS |
951 | return -EINVAL; |
952 | } | |
d804d31e JG |
953 | |
954 | memset(options, 0, sizeof *options); | |
955 | *((ovs_be16 *)options) = htons(csum); | |
a36de779 PS |
956 | options++; |
957 | } | |
958 | if (greh->flags & htons(GRE_KEY)) { | |
959 | uint32_t key; | |
960 | ||
961 | if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) { | |
962 | return -EINVAL; | |
963 | } | |
964 | ||
965 | put_16aligned_be32(options, htonl(key)); | |
966 | options++; | |
967 | } | |
968 | if (greh->flags & htons(GRE_SEQ)) { | |
969 | uint32_t seq; | |
970 | ||
971 | if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) { | |
972 | return -EINVAL; | |
973 | } | |
974 | put_16aligned_be32(options, htonl(seq)); | |
975 | options++; | |
976 | } | |
977 | ||
978 | if (!ovs_scan_len(s, &n, "))")) { | |
979 | return -EINVAL; | |
980 | } | |
981 | ||
982 | header_len = sizeof *eth + sizeof *ip + | |
983 | ((uint8_t *) options - (uint8_t *) greh); | |
984 | } else { | |
985 | return -EINVAL; | |
986 | } | |
987 | ||
988 | /* check tunnel meta data. */ | |
989 | if (data->tnl_type != tnl_type) { | |
990 | return -EINVAL; | |
991 | } | |
992 | if (data->header_len != header_len) { | |
993 | return -EINVAL; | |
994 | } | |
995 | ||
996 | /* Out port */ | |
997 | if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) { | |
998 | return -EINVAL; | |
999 | } | |
1000 | ||
1001 | return n; | |
1002 | } | |
1003 | ||
8b7ea2d4 WZ |
1004 | static int |
1005 | parse_odp_action(const char *s, const struct simap *port_names, | |
1006 | struct ofpbuf *actions) | |
1007 | { | |
1008 | { | |
1009 | uint32_t port; | |
1010 | int n; | |
1011 | ||
1012 | if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) { | |
1013 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port); | |
1014 | return n; | |
7202cbe5 BP |
1015 | } |
1016 | } | |
1017 | ||
8b7ea2d4 WZ |
1018 | if (port_names) { |
1019 | int len = strcspn(s, delimiters); | |
1020 | struct simap_node *node; | |
1021 | ||
1022 | node = simap_find_len(port_names, s, len); | |
1023 | if (node) { | |
1024 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data); | |
1025 | return len; | |
1026 | } | |
1027 | } | |
1028 | ||
d73803ca DDP |
1029 | { |
1030 | uint32_t recirc_id; | |
1031 | int n = -1; | |
1032 | ||
1033 | if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) { | |
1034 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id); | |
1035 | return n; | |
1036 | } | |
1037 | } | |
1038 | ||
8b7ea2d4 WZ |
1039 | if (!strncmp(s, "userspace(", 10)) { |
1040 | return parse_odp_userspace_action(s, actions); | |
1041 | } | |
1042 | ||
7202cbe5 BP |
1043 | if (!strncmp(s, "set(", 4)) { |
1044 | size_t start_ofs; | |
1045 | int retval; | |
6d670e7f JR |
1046 | struct nlattr mask[128 / sizeof(struct nlattr)]; |
1047 | struct ofpbuf maskbuf; | |
1048 | struct nlattr *nested, *key; | |
1049 | size_t size; | |
1050 | ||
1051 | /* 'mask' is big enough to hold any key. */ | |
1052 | ofpbuf_use_stack(&maskbuf, mask, sizeof mask); | |
7202cbe5 BP |
1053 | |
1054 | start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET); | |
6d670e7f | 1055 | retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf); |
7202cbe5 BP |
1056 | if (retval < 0) { |
1057 | return retval; | |
1058 | } | |
1059 | if (s[retval + 4] != ')') { | |
1060 | return -EINVAL; | |
1061 | } | |
6d670e7f JR |
1062 | |
1063 | nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested); | |
1064 | key = nested + 1; | |
1065 | ||
1066 | size = nl_attr_get_size(mask); | |
1067 | if (size == nl_attr_get_size(key)) { | |
1068 | /* Change to masked set action if not fully masked. */ | |
53cb9c3e | 1069 | if (!is_all_ones(mask + 1, size)) { |
6d670e7f JR |
1070 | key->nla_len += size; |
1071 | ofpbuf_put(actions, mask + 1, size); | |
1072 | /* 'actions' may have been reallocated by ofpbuf_put(). */ | |
1073 | nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested); | |
1074 | nested->nla_type = OVS_ACTION_ATTR_SET_MASKED; | |
1075 | } | |
1076 | } | |
1077 | ||
7202cbe5 BP |
1078 | nl_msg_end_nested(actions, start_ofs); |
1079 | return retval + 5; | |
1080 | } | |
1081 | ||
becffb86 BP |
1082 | { |
1083 | struct ovs_action_push_vlan push; | |
1084 | int tpid = ETH_TYPE_VLAN; | |
1085 | int vid, pcp; | |
1086 | int cfi = 1; | |
1087 | int n = -1; | |
7202cbe5 | 1088 | |
c2c28dfd BP |
1089 | if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) |
1090 | || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", | |
1091 | &vid, &pcp, &cfi, &n) | |
1092 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", | |
1093 | &tpid, &vid, &pcp, &n) | |
1094 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", | |
1095 | &tpid, &vid, &pcp, &cfi, &n)) { | |
becffb86 BP |
1096 | push.vlan_tpid = htons(tpid); |
1097 | push.vlan_tci = htons((vid << VLAN_VID_SHIFT) | |
1098 | | (pcp << VLAN_PCP_SHIFT) | |
1099 | | (cfi ? VLAN_CFI : 0)); | |
1100 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
1101 | &push, sizeof push); | |
1102 | ||
1103 | return n; | |
7202cbe5 | 1104 | } |
7202cbe5 BP |
1105 | } |
1106 | ||
becffb86 BP |
1107 | if (!strncmp(s, "pop_vlan", 8)) { |
1108 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN); | |
1109 | return 8; | |
7202cbe5 BP |
1110 | } |
1111 | ||
1112 | { | |
1113 | double percentage; | |
1114 | int n = -1; | |
1115 | ||
c2c28dfd BP |
1116 | if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) |
1117 | && percentage >= 0. && percentage <= 100.0) { | |
7202cbe5 BP |
1118 | size_t sample_ofs, actions_ofs; |
1119 | double probability; | |
1120 | ||
1121 | probability = floor(UINT32_MAX * (percentage / 100.0) + .5); | |
1122 | sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE); | |
1123 | nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY, | |
1124 | (probability <= 0 ? 0 | |
1125 | : probability >= UINT32_MAX ? UINT32_MAX | |
1126 | : probability)); | |
1127 | ||
1128 | actions_ofs = nl_msg_start_nested(actions, | |
1129 | OVS_SAMPLE_ATTR_ACTIONS); | |
1130 | for (;;) { | |
1131 | int retval; | |
1132 | ||
79d4ffe2 | 1133 | n += strspn(s + n, delimiters); |
7202cbe5 BP |
1134 | if (s[n] == ')') { |
1135 | break; | |
1136 | } | |
1137 | ||
1138 | retval = parse_odp_action(s + n, port_names, actions); | |
1139 | if (retval < 0) { | |
1140 | return retval; | |
1141 | } | |
1142 | n += retval; | |
7202cbe5 BP |
1143 | } |
1144 | nl_msg_end_nested(actions, actions_ofs); | |
1145 | nl_msg_end_nested(actions, sample_ofs); | |
1146 | ||
1147 | return s[n + 1] == ')' ? n + 2 : -EINVAL; | |
1148 | } | |
1149 | } | |
1150 | ||
a36de779 PS |
1151 | { |
1152 | uint32_t port; | |
1153 | int n; | |
1154 | ||
1155 | if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) { | |
1156 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port); | |
1157 | return n; | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | { | |
1162 | struct ovs_action_push_tnl data; | |
1163 | int n; | |
1164 | ||
1165 | n = ovs_parse_tnl_push(s, &data); | |
1166 | if (n > 0) { | |
1167 | odp_put_tnl_push_action(actions, &data); | |
1168 | return n; | |
1169 | } else if (n < 0) { | |
1170 | return n; | |
1171 | } | |
1172 | } | |
7202cbe5 BP |
1173 | return -EINVAL; |
1174 | } | |
1175 | ||
1176 | /* Parses the string representation of datapath actions, in the format output | |
1177 | * by format_odp_action(). Returns 0 if successful, otherwise a positive errno | |
1178 | * value. On success, the ODP actions are appended to 'actions' as a series of | |
1179 | * Netlink attributes. On failure, no data is appended to 'actions'. Either | |
1180 | * way, 'actions''s data might be reallocated. */ | |
1181 | int | |
44bac24b | 1182 | odp_actions_from_string(const char *s, const struct simap *port_names, |
7202cbe5 BP |
1183 | struct ofpbuf *actions) |
1184 | { | |
1185 | size_t old_size; | |
1186 | ||
1187 | if (!strcasecmp(s, "drop")) { | |
1188 | return 0; | |
1189 | } | |
1190 | ||
6fd6ed71 | 1191 | old_size = actions->size; |
7202cbe5 BP |
1192 | for (;;) { |
1193 | int retval; | |
1194 | ||
1195 | s += strspn(s, delimiters); | |
1196 | if (!*s) { | |
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | retval = parse_odp_action(s, port_names, actions); | |
1201 | if (retval < 0 || !strchr(delimiters, s[retval])) { | |
6fd6ed71 | 1202 | actions->size = old_size; |
7202cbe5 BP |
1203 | return -retval; |
1204 | } | |
1205 | s += retval; | |
1206 | } | |
1207 | ||
1208 | return 0; | |
1209 | } | |
14608a15 | 1210 | \f |
36956a7d | 1211 | /* Returns the correct length of the payload for a flow key attribute of the |
fea393b1 BP |
1212 | * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload |
1213 | * is variable length. */ | |
36956a7d BP |
1214 | static int |
1215 | odp_flow_key_attr_len(uint16_t type) | |
1216 | { | |
df2c07f4 | 1217 | if (type > OVS_KEY_ATTR_MAX) { |
36956a7d BP |
1218 | return -1; |
1219 | } | |
1220 | ||
09ded0ad | 1221 | switch ((enum ovs_key_attr) type) { |
fea393b1 | 1222 | case OVS_KEY_ATTR_ENCAP: return -2; |
abff858b | 1223 | case OVS_KEY_ATTR_PRIORITY: return 4; |
72e8bf28 | 1224 | case OVS_KEY_ATTR_SKB_MARK: return 4; |
572f732a AZ |
1225 | case OVS_KEY_ATTR_DP_HASH: return 4; |
1226 | case OVS_KEY_ATTR_RECIRC_ID: return 4; | |
9b405f1a | 1227 | case OVS_KEY_ATTR_TUNNEL: return -2; |
df2c07f4 JP |
1228 | case OVS_KEY_ATTR_IN_PORT: return 4; |
1229 | case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet); | |
fea393b1 | 1230 | case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16); |
df2c07f4 | 1231 | case OVS_KEY_ATTR_ETHERTYPE: return 2; |
8bfd0fda | 1232 | case OVS_KEY_ATTR_MPLS: return -2; |
df2c07f4 JP |
1233 | case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4); |
1234 | case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6); | |
1235 | case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp); | |
dc235f7f | 1236 | case OVS_KEY_ATTR_TCP_FLAGS: return 2; |
df2c07f4 | 1237 | case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp); |
c6bcb685 | 1238 | case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp); |
df2c07f4 JP |
1239 | case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp); |
1240 | case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6); | |
1241 | case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp); | |
1242 | case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd); | |
1243 | ||
1244 | case OVS_KEY_ATTR_UNSPEC: | |
1245 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
1246 | return -1; |
1247 | } | |
1248 | ||
1249 | return -1; | |
1250 | } | |
1251 | ||
36956a7d BP |
1252 | static void |
1253 | format_generic_odp_key(const struct nlattr *a, struct ds *ds) | |
1254 | { | |
1255 | size_t len = nl_attr_get_size(a); | |
36956a7d BP |
1256 | if (len) { |
1257 | const uint8_t *unspec; | |
1258 | unsigned int i; | |
1259 | ||
1260 | unspec = nl_attr_get(a); | |
1261 | for (i = 0; i < len; i++) { | |
e6cc0bab AZ |
1262 | if (i) { |
1263 | ds_put_char(ds, ' '); | |
1264 | } | |
36956a7d BP |
1265 | ds_put_format(ds, "%02x", unspec[i]); |
1266 | } | |
36956a7d BP |
1267 | } |
1268 | } | |
1269 | ||
7257b535 BP |
1270 | static const char * |
1271 | ovs_frag_type_to_string(enum ovs_frag_type type) | |
1272 | { | |
1273 | switch (type) { | |
1274 | case OVS_FRAG_TYPE_NONE: | |
1275 | return "no"; | |
1276 | case OVS_FRAG_TYPE_FIRST: | |
1277 | return "first"; | |
1278 | case OVS_FRAG_TYPE_LATER: | |
1279 | return "later"; | |
1280 | case __OVS_FRAG_TYPE_MAX: | |
1281 | default: | |
1282 | return "<error>"; | |
1283 | } | |
1284 | } | |
1285 | ||
9b405f1a PS |
1286 | static int |
1287 | tunnel_key_attr_len(int type) | |
29c7a2b2 | 1288 | { |
9b405f1a PS |
1289 | switch (type) { |
1290 | case OVS_TUNNEL_KEY_ATTR_ID: return 8; | |
1291 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4; | |
1292 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4; | |
1293 | case OVS_TUNNEL_KEY_ATTR_TOS: return 1; | |
1294 | case OVS_TUNNEL_KEY_ATTR_TTL: return 1; | |
1295 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0; | |
1296 | case OVS_TUNNEL_KEY_ATTR_CSUM: return 0; | |
8b7ea2d4 WZ |
1297 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2; |
1298 | case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2; | |
94872594 | 1299 | case OVS_TUNNEL_KEY_ATTR_OAM: return 0; |
c1fc1411 | 1300 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2; |
ac6073e3 | 1301 | case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: return -2; |
9b405f1a PS |
1302 | case __OVS_TUNNEL_KEY_ATTR_MAX: |
1303 | return -1; | |
29c7a2b2 | 1304 | } |
9b405f1a PS |
1305 | return -1; |
1306 | } | |
1307 | ||
c1fc1411 JG |
1308 | #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type)) |
1309 | static int | |
1310 | parse_geneve_opts(const struct nlattr *attr) | |
1311 | { | |
1312 | int opts_len = nl_attr_get_size(attr); | |
1313 | const struct geneve_opt *opt = nl_attr_get(attr); | |
1314 | ||
1315 | while (opts_len > 0) { | |
1316 | int len; | |
1317 | ||
1318 | if (opts_len < sizeof(*opt)) { | |
1319 | return -EINVAL; | |
1320 | } | |
1321 | ||
1322 | len = sizeof(*opt) + opt->length * 4; | |
1323 | if (len > opts_len) { | |
1324 | return -EINVAL; | |
1325 | } | |
1326 | ||
1327 | switch (GENEVE_OPT(opt->opt_class, opt->type)) { | |
1328 | default: | |
1329 | if (opt->type & GENEVE_CRIT_OPT_TYPE) { | |
1330 | return -EINVAL; | |
1331 | } | |
1332 | }; | |
1333 | ||
1334 | opt = opt + len / sizeof(*opt); | |
1335 | opts_len -= len; | |
1336 | }; | |
1337 | ||
1338 | return 0; | |
1339 | } | |
1340 | ||
617e10e7 SH |
1341 | enum odp_key_fitness |
1342 | odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun) | |
9b405f1a PS |
1343 | { |
1344 | unsigned int left; | |
1345 | const struct nlattr *a; | |
1346 | bool ttl = false; | |
1347 | bool unknown = false; | |
1348 | ||
1349 | NL_NESTED_FOR_EACH(a, left, attr) { | |
1350 | uint16_t type = nl_attr_type(a); | |
1351 | size_t len = nl_attr_get_size(a); | |
1352 | int expected_len = tunnel_key_attr_len(type); | |
1353 | ||
1354 | if (len != expected_len && expected_len >= 0) { | |
1355 | return ODP_FIT_ERROR; | |
1356 | } | |
1357 | ||
1358 | switch (type) { | |
1359 | case OVS_TUNNEL_KEY_ATTR_ID: | |
1360 | tun->tun_id = nl_attr_get_be64(a); | |
1361 | tun->flags |= FLOW_TNL_F_KEY; | |
1362 | break; | |
1363 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
1364 | tun->ip_src = nl_attr_get_be32(a); | |
1365 | break; | |
1366 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
1367 | tun->ip_dst = nl_attr_get_be32(a); | |
1368 | break; | |
1369 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
1370 | tun->ip_tos = nl_attr_get_u8(a); | |
1371 | break; | |
1372 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
1373 | tun->ip_ttl = nl_attr_get_u8(a); | |
1374 | ttl = true; | |
1375 | break; | |
1376 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
1377 | tun->flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
1378 | break; | |
1379 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
1380 | tun->flags |= FLOW_TNL_F_CSUM; | |
1381 | break; | |
8b7ea2d4 WZ |
1382 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: |
1383 | tun->tp_src = nl_attr_get_be16(a); | |
1384 | break; | |
1385 | case OVS_TUNNEL_KEY_ATTR_TP_DST: | |
1386 | tun->tp_dst = nl_attr_get_be16(a); | |
1387 | break; | |
94872594 JG |
1388 | case OVS_TUNNEL_KEY_ATTR_OAM: |
1389 | tun->flags |= FLOW_TNL_F_OAM; | |
1390 | break; | |
ac6073e3 MC |
1391 | case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: { |
1392 | static const struct nl_policy vxlan_opts_policy[] = { | |
1393 | [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 }, | |
1394 | }; | |
1395 | struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)]; | |
1396 | ||
1397 | if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) { | |
1398 | return ODP_FIT_ERROR; | |
1399 | } | |
1400 | ||
1401 | if (ext[OVS_VXLAN_EXT_GBP]) { | |
1402 | uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]); | |
1403 | ||
1404 | tun->gbp_id = htons(gbp & 0xFFFF); | |
1405 | tun->gbp_flags = (gbp >> 16) & 0xFF; | |
1406 | } | |
1407 | ||
1408 | break; | |
1409 | } | |
c1fc1411 JG |
1410 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: { |
1411 | if (parse_geneve_opts(a)) { | |
1412 | return ODP_FIT_ERROR; | |
1413 | } | |
1414 | /* It is necessary to reproduce options exactly (including order) | |
1415 | * so it's easiest to just echo them back. */ | |
1416 | unknown = true; | |
1417 | break; | |
1418 | } | |
9b405f1a PS |
1419 | default: |
1420 | /* Allow this to show up as unexpected, if there are unknown | |
1421 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
1422 | unknown = true; | |
1423 | break; | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | if (!ttl) { | |
1428 | return ODP_FIT_ERROR; | |
1429 | } | |
1430 | if (unknown) { | |
758c456d | 1431 | return ODP_FIT_TOO_MUCH; |
9b405f1a PS |
1432 | } |
1433 | return ODP_FIT_PERFECT; | |
1434 | } | |
1435 | ||
1436 | static void | |
1437 | tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key) | |
1438 | { | |
1439 | size_t tun_key_ofs; | |
1440 | ||
1441 | tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL); | |
1442 | ||
27a8cbbc BP |
1443 | /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */ |
1444 | if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) { | |
9b405f1a PS |
1445 | nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id); |
1446 | } | |
1447 | if (tun_key->ip_src) { | |
1448 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src); | |
1449 | } | |
1450 | if (tun_key->ip_dst) { | |
1451 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst); | |
1452 | } | |
1453 | if (tun_key->ip_tos) { | |
1454 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos); | |
1455 | } | |
1456 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl); | |
1457 | if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
1458 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
1459 | } | |
1460 | if (tun_key->flags & FLOW_TNL_F_CSUM) { | |
1461 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
1462 | } | |
8b7ea2d4 WZ |
1463 | if (tun_key->tp_src) { |
1464 | nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src); | |
1465 | } | |
1466 | if (tun_key->tp_dst) { | |
1467 | nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst); | |
1468 | } | |
94872594 JG |
1469 | if (tun_key->flags & FLOW_TNL_F_OAM) { |
1470 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM); | |
1471 | } | |
ac6073e3 MC |
1472 | if (tun_key->gbp_flags || tun_key->gbp_id) { |
1473 | size_t vxlan_opts_ofs; | |
1474 | ||
1475 | vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS); | |
1476 | nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, | |
1477 | (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id)); | |
1478 | nl_msg_end_nested(a, vxlan_opts_ofs); | |
1479 | } | |
9b405f1a PS |
1480 | |
1481 | nl_msg_end_nested(a, tun_key_ofs); | |
29c7a2b2 PS |
1482 | } |
1483 | ||
041e7168 AZ |
1484 | static bool |
1485 | odp_mask_attr_is_wildcard(const struct nlattr *ma) | |
1486 | { | |
1487 | return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma)); | |
1488 | } | |
1489 | ||
e6cc0bab | 1490 | static bool |
d23df9a8 | 1491 | odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size) |
e6cc0bab | 1492 | { |
260974dc | 1493 | if (attr == OVS_KEY_ATTR_TCP_FLAGS) { |
d23df9a8 JR |
1494 | return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX); |
1495 | } | |
1496 | if (attr == OVS_KEY_ATTR_IPV6) { | |
1497 | const struct ovs_key_ipv6 *ipv6_mask = mask; | |
260974dc | 1498 | |
d23df9a8 JR |
1499 | return |
1500 | ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK)) | |
ea2735d3 | 1501 | == htonl(IPV6_LABEL_MASK)) |
d23df9a8 JR |
1502 | && ipv6_mask->ipv6_proto == UINT8_MAX |
1503 | && ipv6_mask->ipv6_tclass == UINT8_MAX | |
1504 | && ipv6_mask->ipv6_hlimit == UINT8_MAX | |
1505 | && ipv6_mask->ipv6_frag == UINT8_MAX | |
1506 | && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src) | |
1507 | && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst); | |
1508 | } | |
1509 | if (attr == OVS_KEY_ATTR_TUNNEL) { | |
1510 | const struct flow_tnl *tun_mask = mask; | |
1511 | ||
1512 | return tun_mask->flags == FLOW_TNL_F_MASK | |
1513 | && tun_mask->tun_id == OVS_BE64_MAX | |
1514 | && tun_mask->ip_src == OVS_BE32_MAX | |
1515 | && tun_mask->ip_dst == OVS_BE32_MAX | |
1516 | && tun_mask->ip_tos == UINT8_MAX | |
1517 | && tun_mask->ip_ttl == UINT8_MAX | |
1518 | && tun_mask->tp_src == OVS_BE16_MAX | |
ac6073e3 MC |
1519 | && tun_mask->tp_dst == OVS_BE16_MAX |
1520 | && tun_mask->gbp_id == OVS_BE16_MAX | |
1521 | && tun_mask->gbp_flags == UINT8_MAX; | |
d23df9a8 JR |
1522 | } |
1523 | ||
1524 | if (attr == OVS_KEY_ATTR_ARP) { | |
1525 | /* ARP key has padding, ignore it. */ | |
1526 | BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24); | |
1527 | BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6); | |
1528 | size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN; | |
1529 | ovs_assert(((uint16_t *)mask)[size/2] == 0); | |
1530 | } | |
260974dc | 1531 | |
d23df9a8 JR |
1532 | return is_all_ones(mask, size); |
1533 | } | |
1534 | ||
1535 | static bool | |
1536 | odp_mask_attr_is_exact(const struct nlattr *ma) | |
1537 | { | |
1538 | struct flow_tnl tun_mask; | |
1539 | enum ovs_key_attr attr = nl_attr_type(ma); | |
1540 | const void *mask; | |
1541 | size_t size; | |
1542 | ||
1543 | if (attr == OVS_KEY_ATTR_TUNNEL) { | |
e6cc0bab AZ |
1544 | memset(&tun_mask, 0, sizeof tun_mask); |
1545 | odp_tun_key_from_attr(ma, &tun_mask); | |
d23df9a8 JR |
1546 | mask = &tun_mask; |
1547 | size = sizeof tun_mask; | |
e6cc0bab | 1548 | } else { |
d23df9a8 JR |
1549 | mask = nl_attr_get(ma); |
1550 | size = nl_attr_get_size(ma); | |
e6cc0bab AZ |
1551 | } |
1552 | ||
d23df9a8 | 1553 | return odp_mask_is_exact(attr, mask, size); |
e6cc0bab AZ |
1554 | } |
1555 | ||
0a37839c GS |
1556 | void |
1557 | odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no, | |
1558 | char *port_name) | |
1559 | { | |
1560 | struct odp_portno_names *odp_portno_names; | |
1561 | ||
1562 | odp_portno_names = xmalloc(sizeof *odp_portno_names); | |
1563 | odp_portno_names->port_no = port_no; | |
1564 | odp_portno_names->name = xstrdup(port_name); | |
1565 | hmap_insert(portno_names, &odp_portno_names->hmap_node, | |
1566 | hash_odp_port(port_no)); | |
1567 | } | |
1568 | ||
1569 | static char * | |
1570 | odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no) | |
1571 | { | |
1572 | struct odp_portno_names *odp_portno_names; | |
1573 | ||
1574 | HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node, | |
1575 | hash_odp_port(port_no), portno_names) { | |
1576 | if (odp_portno_names->port_no == port_no) { | |
1577 | return odp_portno_names->name; | |
1578 | } | |
1579 | } | |
1580 | return NULL; | |
1581 | } | |
1582 | ||
1583 | void | |
1584 | odp_portno_names_destroy(struct hmap *portno_names) | |
1585 | { | |
1586 | struct odp_portno_names *odp_portno_names, *odp_portno_names_next; | |
1587 | HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next, | |
1588 | hmap_node, portno_names) { | |
1589 | hmap_remove(portno_names, &odp_portno_names->hmap_node); | |
1590 | free(odp_portno_names->name); | |
1591 | free(odp_portno_names); | |
1592 | } | |
1593 | } | |
e6cc0bab | 1594 | |
2d18eae8 JR |
1595 | /* Format helpers. */ |
1596 | ||
1597 | static void | |
1598 | format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN], | |
1599 | const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose) | |
1600 | { | |
1601 | bool mask_empty = mask && eth_addr_is_zero(*mask); | |
1602 | ||
1603 | if (verbose || !mask_empty) { | |
1604 | bool mask_full = !mask || eth_mask_is_exact(*mask); | |
1605 | ||
1606 | if (mask_full) { | |
1607 | ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key)); | |
1608 | } else { | |
1609 | ds_put_format(ds, "%s=", name); | |
1610 | eth_format_masked(key, *mask, ds); | |
1611 | ds_put_char(ds, ','); | |
1612 | } | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | static void | |
1617 | format_be64(struct ds *ds, const char *name, ovs_be64 key, | |
1618 | const ovs_be64 *mask, bool verbose) | |
1619 | { | |
1620 | bool mask_empty = mask && !*mask; | |
1621 | ||
1622 | if (verbose || !mask_empty) { | |
1623 | bool mask_full = !mask || *mask == OVS_BE64_MAX; | |
1624 | ||
1625 | ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key)); | |
1626 | if (!mask_full) { /* Partially masked. */ | |
1627 | ds_put_format(ds, "/%#"PRIx64, ntohll(*mask)); | |
1628 | } | |
1629 | ds_put_char(ds, ','); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | static void | |
1634 | format_ipv4(struct ds *ds, const char *name, ovs_be32 key, | |
1635 | const ovs_be32 *mask, bool verbose) | |
1636 | { | |
1637 | bool mask_empty = mask && !*mask; | |
1638 | ||
1639 | if (verbose || !mask_empty) { | |
1640 | bool mask_full = !mask || *mask == OVS_BE32_MAX; | |
1641 | ||
1642 | ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key)); | |
1643 | if (!mask_full) { /* Partially masked. */ | |
1644 | ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask)); | |
1645 | } | |
1646 | ds_put_char(ds, ','); | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | static void | |
1651 | format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4], | |
1652 | const ovs_be32 (*mask_)[4], bool verbose) | |
1653 | { | |
1654 | char buf[INET6_ADDRSTRLEN]; | |
1655 | const struct in6_addr *key = (const struct in6_addr *)key_; | |
1656 | const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_ | |
1657 | : NULL; | |
1658 | bool mask_empty = mask && ipv6_mask_is_any(mask); | |
1659 | ||
1660 | if (verbose || !mask_empty) { | |
1661 | bool mask_full = !mask || ipv6_mask_is_exact(mask); | |
1662 | ||
1663 | inet_ntop(AF_INET6, key, buf, sizeof buf); | |
1664 | ds_put_format(ds, "%s=%s", name, buf); | |
1665 | if (!mask_full) { /* Partially masked. */ | |
1666 | inet_ntop(AF_INET6, mask, buf, sizeof buf); | |
1667 | ds_put_format(ds, "/%s", buf); | |
1668 | } | |
1669 | ds_put_char(ds, ','); | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | static void | |
1674 | format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key, | |
1675 | const ovs_be32 *mask, bool verbose) | |
1676 | { | |
1677 | bool mask_empty = mask && !*mask; | |
1678 | ||
1679 | if (verbose || !mask_empty) { | |
1680 | bool mask_full = !mask | |
1681 | || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK); | |
1682 | ||
1683 | ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key)); | |
1684 | if (!mask_full) { /* Partially masked. */ | |
1685 | ds_put_format(ds, "/%#"PRIx32, ntohl(*mask)); | |
1686 | } | |
1687 | ds_put_char(ds, ','); | |
1688 | } | |
1689 | } | |
1690 | ||
1691 | static void | |
1692 | format_u8x(struct ds *ds, const char *name, uint8_t key, | |
1693 | const uint8_t *mask, bool verbose) | |
1694 | { | |
1695 | bool mask_empty = mask && !*mask; | |
1696 | ||
1697 | if (verbose || !mask_empty) { | |
1698 | bool mask_full = !mask || *mask == UINT8_MAX; | |
1699 | ||
1700 | ds_put_format(ds, "%s=%#"PRIx8, name, key); | |
1701 | if (!mask_full) { /* Partially masked. */ | |
1702 | ds_put_format(ds, "/%#"PRIx8, *mask); | |
1703 | } | |
1704 | ds_put_char(ds, ','); | |
1705 | } | |
1706 | } | |
1707 | ||
1708 | static void | |
1709 | format_u8u(struct ds *ds, const char *name, uint8_t key, | |
1710 | const uint8_t *mask, bool verbose) | |
1711 | { | |
1712 | bool mask_empty = mask && !*mask; | |
1713 | ||
1714 | if (verbose || !mask_empty) { | |
1715 | bool mask_full = !mask || *mask == UINT8_MAX; | |
1716 | ||
1717 | ds_put_format(ds, "%s=%"PRIu8, name, key); | |
1718 | if (!mask_full) { /* Partially masked. */ | |
1719 | ds_put_format(ds, "/%#"PRIx8, *mask); | |
1720 | } | |
1721 | ds_put_char(ds, ','); | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | static void | |
1726 | format_be16(struct ds *ds, const char *name, ovs_be16 key, | |
1727 | const ovs_be16 *mask, bool verbose) | |
1728 | { | |
1729 | bool mask_empty = mask && !*mask; | |
1730 | ||
1731 | if (verbose || !mask_empty) { | |
1732 | bool mask_full = !mask || *mask == OVS_BE16_MAX; | |
1733 | ||
1734 | ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key)); | |
1735 | if (!mask_full) { /* Partially masked. */ | |
1736 | ds_put_format(ds, "/%#"PRIx16, ntohs(*mask)); | |
1737 | } | |
1738 | ds_put_char(ds, ','); | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | static void | |
1743 | format_tun_flags(struct ds *ds, const char *name, uint16_t key, | |
1744 | const uint16_t *mask, bool verbose) | |
1745 | { | |
1746 | bool mask_empty = mask && !*mask; | |
1747 | ||
1748 | if (verbose || !mask_empty) { | |
1749 | bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK; | |
1750 | ||
1751 | ds_put_cstr(ds, name); | |
1752 | ds_put_char(ds, '('); | |
1753 | if (!mask_full) { /* Partially masked. */ | |
1754 | format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask); | |
1755 | } else { /* Fully masked. */ | |
1756 | format_flags(ds, flow_tun_flag_to_string, key, ','); | |
1757 | } | |
1758 | ds_put_cstr(ds, "),"); | |
1759 | } | |
1760 | } | |
1761 | ||
1762 | static void | |
1763 | format_frag(struct ds *ds, const char *name, uint8_t key, | |
1764 | const uint8_t *mask, bool verbose) | |
1765 | { | |
1766 | bool mask_empty = mask && !*mask; | |
1767 | ||
1768 | /* ODP frag is an enumeration field; partial masks are not meaningful. */ | |
1769 | if (verbose || !mask_empty) { | |
1770 | bool mask_full = !mask || *mask == UINT8_MAX; | |
1771 | ||
1772 | if (!mask_full) { /* Partially masked. */ | |
1773 | ds_put_format(ds, "error: partial mask not supported for frag (%#" | |
1774 | PRIx8"),", *mask); | |
1775 | } else { | |
1776 | ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key)); | |
1777 | } | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL | |
1782 | ||
36956a7d | 1783 | static void |
e6cc0bab | 1784 | format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, |
0a37839c GS |
1785 | const struct hmap *portno_names, struct ds *ds, |
1786 | bool verbose) | |
36956a7d | 1787 | { |
7a8e9ed2 | 1788 | enum ovs_key_attr attr = nl_attr_type(a); |
e6603631 | 1789 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
fea393b1 | 1790 | int expected_len; |
dc8c5408 | 1791 | bool is_exact; |
36956a7d | 1792 | |
dc8c5408 | 1793 | is_exact = ma ? odp_mask_attr_is_exact(ma) : true; |
e6cc0bab | 1794 | |
e6603631 | 1795 | ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf)); |
e6cc0bab AZ |
1796 | |
1797 | { | |
1798 | expected_len = odp_flow_key_attr_len(nl_attr_type(a)); | |
1799 | if (expected_len != -2) { | |
1800 | bool bad_key_len = nl_attr_get_size(a) != expected_len; | |
54695350 | 1801 | bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len; |
e6cc0bab AZ |
1802 | |
1803 | if (bad_key_len || bad_mask_len) { | |
1804 | if (bad_key_len) { | |
34582733 | 1805 | ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(", |
54695350 | 1806 | nl_attr_get_size(a), expected_len); |
e6cc0bab AZ |
1807 | } |
1808 | format_generic_odp_key(a, ds); | |
54695350 | 1809 | if (ma) { |
e6cc0bab | 1810 | ds_put_char(ds, '/'); |
54695350 JR |
1811 | if (bad_mask_len) { |
1812 | ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(", | |
1813 | nl_attr_get_size(ma), expected_len); | |
1814 | } | |
1815 | format_generic_odp_key(ma, ds); | |
e6cc0bab | 1816 | } |
e6cc0bab AZ |
1817 | ds_put_char(ds, ')'); |
1818 | return; | |
1819 | } | |
1820 | } | |
36956a7d BP |
1821 | } |
1822 | ||
e6cc0bab | 1823 | ds_put_char(ds, '('); |
7a8e9ed2 | 1824 | switch (attr) { |
fea393b1 | 1825 | case OVS_KEY_ATTR_ENCAP: |
e6cc0bab AZ |
1826 | if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) { |
1827 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), | |
0a37839c | 1828 | nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds, |
041e7168 | 1829 | verbose); |
0a37839c GS |
1830 | } else if (nl_attr_get_size(a)) { |
1831 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL, | |
1832 | ds, verbose); | |
fea393b1 | 1833 | } |
fea393b1 BP |
1834 | break; |
1835 | ||
abff858b | 1836 | case OVS_KEY_ATTR_PRIORITY: |
72e8bf28 | 1837 | case OVS_KEY_ATTR_SKB_MARK: |
572f732a AZ |
1838 | case OVS_KEY_ATTR_DP_HASH: |
1839 | case OVS_KEY_ATTR_RECIRC_ID: | |
e6cc0bab | 1840 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); |
dc8c5408 | 1841 | if (!is_exact) { |
e6cc0bab AZ |
1842 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); |
1843 | } | |
72e8bf28 AA |
1844 | break; |
1845 | ||
ea2735d3 | 1846 | case OVS_KEY_ATTR_TUNNEL: { |
2d18eae8 JR |
1847 | struct flow_tnl key, mask_; |
1848 | struct flow_tnl *mask = ma ? &mask_ : NULL; | |
e6cc0bab | 1849 | |
2d18eae8 JR |
1850 | if (mask) { |
1851 | memset(mask, 0, sizeof *mask); | |
1852 | odp_tun_key_from_attr(ma, mask); | |
ea2735d3 | 1853 | } |
2d18eae8 JR |
1854 | memset(&key, 0, sizeof key); |
1855 | if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) { | |
ea2735d3 JR |
1856 | ds_put_format(ds, "error"); |
1857 | return; | |
1858 | } | |
2d18eae8 JR |
1859 | format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose); |
1860 | format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose); | |
1861 | format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose); | |
1862 | format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose); | |
1863 | format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose); | |
1864 | format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose); | |
1865 | format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose); | |
ac6073e3 MC |
1866 | format_be16(ds, "gbp_id", key.gbp_id, MASK(mask, gbp_id), verbose); |
1867 | format_u8x(ds, "gbp_flags", key.gbp_flags, MASK(mask, gbp_flags), verbose); | |
2d18eae8 JR |
1868 | format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose); |
1869 | ds_chomp(ds, ','); | |
356af50b | 1870 | break; |
ea2735d3 | 1871 | } |
df2c07f4 | 1872 | case OVS_KEY_ATTR_IN_PORT: |
0a37839c GS |
1873 | if (portno_names && verbose && is_exact) { |
1874 | char *name = odp_portno_names_get(portno_names, | |
1875 | u32_to_odp(nl_attr_get_u32(a))); | |
1876 | if (name) { | |
1877 | ds_put_format(ds, "%s", name); | |
1878 | } else { | |
1879 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); | |
1880 | } | |
1881 | } else { | |
1882 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); | |
1883 | if (!is_exact) { | |
1884 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); | |
1885 | } | |
e6cc0bab | 1886 | } |
36956a7d BP |
1887 | break; |
1888 | ||
2d18eae8 JR |
1889 | case OVS_KEY_ATTR_ETHERNET: { |
1890 | const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL; | |
1891 | const struct ovs_key_ethernet *key = nl_attr_get(a); | |
e6cc0bab | 1892 | |
2d18eae8 JR |
1893 | format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose); |
1894 | format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose); | |
1895 | ds_chomp(ds, ','); | |
36956a7d | 1896 | break; |
2d18eae8 | 1897 | } |
fea393b1 | 1898 | case OVS_KEY_ATTR_VLAN: |
2d18eae8 JR |
1899 | format_vlan_tci(ds, nl_attr_get_be16(a), |
1900 | ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose); | |
36956a7d BP |
1901 | break; |
1902 | ||
b02475c5 SH |
1903 | case OVS_KEY_ATTR_MPLS: { |
1904 | const struct ovs_key_mpls *mpls_key = nl_attr_get(a); | |
e6cc0bab | 1905 | const struct ovs_key_mpls *mpls_mask = NULL; |
8bfd0fda BP |
1906 | size_t size = nl_attr_get_size(a); |
1907 | ||
1908 | if (!size || size % sizeof *mpls_key) { | |
6d670e7f | 1909 | ds_put_format(ds, "(bad key length %"PRIuSIZE")", size); |
8bfd0fda BP |
1910 | return; |
1911 | } | |
dc8c5408 | 1912 | if (!is_exact) { |
e6cc0bab | 1913 | mpls_mask = nl_attr_get(ma); |
6d670e7f | 1914 | if (size != nl_attr_get_size(ma)) { |
8bfd0fda BP |
1915 | ds_put_format(ds, "(key length %"PRIuSIZE" != " |
1916 | "mask length %"PRIuSIZE")", | |
6d670e7f | 1917 | size, nl_attr_get_size(ma)); |
8bfd0fda BP |
1918 | return; |
1919 | } | |
e6cc0bab | 1920 | } |
8bfd0fda | 1921 | format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key); |
b02475c5 SH |
1922 | break; |
1923 | } | |
df2c07f4 | 1924 | case OVS_KEY_ATTR_ETHERTYPE: |
e6cc0bab | 1925 | ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a))); |
dc8c5408 | 1926 | if (!is_exact) { |
e6cc0bab AZ |
1927 | ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma))); |
1928 | } | |
36956a7d BP |
1929 | break; |
1930 | ||
2d18eae8 JR |
1931 | case OVS_KEY_ATTR_IPV4: { |
1932 | const struct ovs_key_ipv4 *key = nl_attr_get(a); | |
1933 | const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL; | |
1934 | ||
1935 | format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose); | |
1936 | format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose); | |
1937 | format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto), | |
1938 | verbose); | |
1939 | format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose); | |
1940 | format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose); | |
1941 | format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag), | |
1942 | verbose); | |
1943 | ds_chomp(ds, ','); | |
36956a7d | 1944 | break; |
2d18eae8 JR |
1945 | } |
1946 | case OVS_KEY_ATTR_IPV6: { | |
1947 | const struct ovs_key_ipv6 *key = nl_attr_get(a); | |
1948 | const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
1949 | ||
1950 | format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose); | |
1951 | format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose); | |
1952 | format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label), | |
1953 | verbose); | |
1954 | format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto), | |
1955 | verbose); | |
1956 | format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass), | |
1957 | verbose); | |
1958 | format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit), | |
1959 | verbose); | |
1960 | format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag), | |
1961 | verbose); | |
1962 | ds_chomp(ds, ','); | |
d31f1109 | 1963 | break; |
2d18eae8 JR |
1964 | } |
1965 | /* These have the same structure and format. */ | |
df2c07f4 | 1966 | case OVS_KEY_ATTR_TCP: |
2d18eae8 JR |
1967 | case OVS_KEY_ATTR_UDP: |
1968 | case OVS_KEY_ATTR_SCTP: { | |
1969 | const struct ovs_key_tcp *key = nl_attr_get(a); | |
1970 | const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL; | |
e6cc0bab | 1971 | |
2d18eae8 JR |
1972 | format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose); |
1973 | format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose); | |
1974 | ds_chomp(ds, ','); | |
36956a7d | 1975 | break; |
2d18eae8 | 1976 | } |
dc235f7f | 1977 | case OVS_KEY_ATTR_TCP_FLAGS: |
dc235f7f | 1978 | if (!is_exact) { |
ea2735d3 JR |
1979 | format_flags_masked(ds, NULL, packet_tcp_flag_to_string, |
1980 | ntohs(nl_attr_get_be16(a)), | |
1981 | ntohs(nl_attr_get_be16(ma))); | |
1982 | } else { | |
1983 | format_flags(ds, packet_tcp_flag_to_string, | |
1984 | ntohs(nl_attr_get_be16(a)), ','); | |
dc235f7f JR |
1985 | } |
1986 | break; | |
1987 | ||
2d18eae8 JR |
1988 | case OVS_KEY_ATTR_ICMP: { |
1989 | const struct ovs_key_icmp *key = nl_attr_get(a); | |
1990 | const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL; | |
e6cc0bab | 1991 | |
2d18eae8 JR |
1992 | format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose); |
1993 | format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose); | |
1994 | ds_chomp(ds, ','); | |
36956a7d | 1995 | break; |
2d18eae8 JR |
1996 | } |
1997 | case OVS_KEY_ATTR_ICMPV6: { | |
1998 | const struct ovs_key_icmpv6 *key = nl_attr_get(a); | |
1999 | const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
36956a7d | 2000 | |
2d18eae8 JR |
2001 | format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type), |
2002 | verbose); | |
2003 | format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code), | |
2004 | verbose); | |
2005 | ds_chomp(ds, ','); | |
d31f1109 | 2006 | break; |
2d18eae8 JR |
2007 | } |
2008 | case OVS_KEY_ATTR_ARP: { | |
2009 | const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL; | |
2010 | const struct ovs_key_arp *key = nl_attr_get(a); | |
d31f1109 | 2011 | |
2d18eae8 JR |
2012 | format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose); |
2013 | format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose); | |
2014 | format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose); | |
2015 | format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose); | |
2016 | format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose); | |
2017 | ds_chomp(ds, ','); | |
36956a7d | 2018 | break; |
2d18eae8 | 2019 | } |
df2c07f4 | 2020 | case OVS_KEY_ATTR_ND: { |
2d18eae8 JR |
2021 | const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL; |
2022 | const struct ovs_key_nd *key = nl_attr_get(a); | |
e6cc0bab | 2023 | |
2d18eae8 JR |
2024 | format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target), |
2025 | verbose); | |
2026 | format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose); | |
2027 | format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose); | |
685a51a5 | 2028 | |
2d18eae8 | 2029 | ds_chomp(ds, ','); |
685a51a5 JP |
2030 | break; |
2031 | } | |
7a8e9ed2 BP |
2032 | case OVS_KEY_ATTR_UNSPEC: |
2033 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
2034 | default: |
2035 | format_generic_odp_key(a, ds); | |
dc8c5408 | 2036 | if (!is_exact) { |
e6cc0bab AZ |
2037 | ds_put_char(ds, '/'); |
2038 | format_generic_odp_key(ma, ds); | |
2039 | } | |
36956a7d BP |
2040 | break; |
2041 | } | |
e6cc0bab | 2042 | ds_put_char(ds, ')'); |
36956a7d BP |
2043 | } |
2044 | ||
dc8c5408 AZ |
2045 | static struct nlattr * |
2046 | generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key) | |
2047 | { | |
2048 | const struct nlattr *a; | |
2049 | unsigned int left; | |
2050 | int type = nl_attr_type(key); | |
2051 | int size = nl_attr_get_size(key); | |
2052 | ||
2053 | if (odp_flow_key_attr_len(type) >=0) { | |
9ddf12cc | 2054 | nl_msg_put_unspec_zero(ofp, type, size); |
dc8c5408 AZ |
2055 | } else { |
2056 | size_t nested_mask; | |
2057 | ||
2058 | nested_mask = nl_msg_start_nested(ofp, type); | |
2059 | NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) { | |
2060 | generate_all_wildcard_mask(ofp, nl_attr_get(a)); | |
2061 | } | |
2062 | nl_msg_end_nested(ofp, nested_mask); | |
2063 | } | |
2064 | ||
6fd6ed71 | 2065 | return ofp->base; |
dc8c5408 AZ |
2066 | } |
2067 | ||
534a19b9 JS |
2068 | int |
2069 | odp_ufid_from_string(const char *s_, ovs_u128 *ufid) | |
2070 | { | |
2071 | const char *s = s_; | |
2072 | ||
2073 | if (ovs_scan(s, "ufid:")) { | |
2074 | size_t n; | |
2075 | ||
2076 | s += 5; | |
2077 | if (ovs_scan(s, "0x")) { | |
2078 | s += 2; | |
2079 | } | |
2080 | ||
2081 | n = strspn(s, "0123456789abcdefABCDEF"); | |
2082 | if (n != 32) { | |
2083 | return -EINVAL; | |
2084 | } | |
2085 | ||
627298c5 JS |
2086 | if (!ovs_scan(s, "%16"SCNx64"%16"SCNx64, &ufid->u64.hi, |
2087 | &ufid->u64.lo)) { | |
534a19b9 JS |
2088 | return -EINVAL; |
2089 | } | |
627298c5 | 2090 | s += n; |
534a19b9 JS |
2091 | s += strspn(s, delimiters); |
2092 | ||
2093 | return s - s_; | |
2094 | } | |
2095 | ||
2096 | return 0; | |
2097 | } | |
2098 | ||
70e5ed6f JS |
2099 | void |
2100 | odp_format_ufid(const ovs_u128 *ufid, struct ds *ds) | |
2101 | { | |
9fc7fcbc JS |
2102 | ds_put_format(ds, "ufid:%016"PRIx64"%016"PRIx64, ufid->u64.hi, |
2103 | ufid->u64.lo); | |
70e5ed6f JS |
2104 | } |
2105 | ||
36956a7d | 2106 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
e6cc0bab | 2107 | * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the |
73580638 GS |
2108 | * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is |
2109 | * non-null and 'verbose' is true, translates odp port number to its name. */ | |
14608a15 | 2110 | void |
e6cc0bab AZ |
2111 | odp_flow_format(const struct nlattr *key, size_t key_len, |
2112 | const struct nlattr *mask, size_t mask_len, | |
0a37839c | 2113 | const struct hmap *portno_names, struct ds *ds, bool verbose) |
14608a15 | 2114 | { |
36956a7d BP |
2115 | if (key_len) { |
2116 | const struct nlattr *a; | |
2117 | unsigned int left; | |
e6cc0bab AZ |
2118 | bool has_ethtype_key = false; |
2119 | const struct nlattr *ma = NULL; | |
dc8c5408 | 2120 | struct ofpbuf ofp; |
041e7168 | 2121 | bool first_field = true; |
36956a7d | 2122 | |
dc8c5408 | 2123 | ofpbuf_init(&ofp, 100); |
36956a7d | 2124 | NL_ATTR_FOR_EACH (a, left, key, key_len) { |
041e7168 AZ |
2125 | bool is_nested_attr; |
2126 | bool is_wildcard = false; | |
2127 | int attr_type = nl_attr_type(a); | |
2128 | ||
2129 | if (attr_type == OVS_KEY_ATTR_ETHERTYPE) { | |
e6cc0bab AZ |
2130 | has_ethtype_key = true; |
2131 | } | |
041e7168 AZ |
2132 | |
2133 | is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2); | |
2134 | ||
e6cc0bab AZ |
2135 | if (mask && mask_len) { |
2136 | ma = nl_attr_find__(mask, mask_len, nl_attr_type(a)); | |
041e7168 AZ |
2137 | is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true; |
2138 | } | |
2139 | ||
2140 | if (verbose || !is_wildcard || is_nested_attr) { | |
2141 | if (is_wildcard && !ma) { | |
dc8c5408 AZ |
2142 | ma = generate_all_wildcard_mask(&ofp, a); |
2143 | } | |
041e7168 AZ |
2144 | if (!first_field) { |
2145 | ds_put_char(ds, ','); | |
2146 | } | |
0a37839c | 2147 | format_odp_key_attr(a, ma, portno_names, ds, verbose); |
041e7168 | 2148 | first_field = false; |
e6cc0bab | 2149 | } |
dc8c5408 | 2150 | ofpbuf_clear(&ofp); |
36956a7d | 2151 | } |
dc8c5408 AZ |
2152 | ofpbuf_uninit(&ofp); |
2153 | ||
36956a7d | 2154 | if (left) { |
3a8cfc50 | 2155 | int i; |
1394054e | 2156 | |
36956a7d BP |
2157 | if (left == key_len) { |
2158 | ds_put_cstr(ds, "<empty>"); | |
2159 | } | |
3a8cfc50 BP |
2160 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
2161 | for (i = 0; i < left; i++) { | |
2162 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
2163 | } | |
2164 | ds_put_char(ds, ')'); | |
36956a7d | 2165 | } |
e6cc0bab AZ |
2166 | if (!has_ethtype_key) { |
2167 | ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE); | |
2168 | if (ma) { | |
2169 | ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")", | |
2170 | ntohs(nl_attr_get_be16(ma))); | |
2171 | } | |
2172 | } | |
36956a7d BP |
2173 | } else { |
2174 | ds_put_cstr(ds, "<empty>"); | |
2175 | } | |
14608a15 | 2176 | } |
064af421 | 2177 | |
e6cc0bab AZ |
2178 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
2179 | * OVS_KEY_ATTR_* attributes in 'key'. */ | |
2180 | void | |
2181 | odp_flow_key_format(const struct nlattr *key, | |
2182 | size_t key_len, struct ds *ds) | |
2183 | { | |
0a37839c | 2184 | odp_flow_format(key, key_len, NULL, 0, NULL, ds, true); |
e6cc0bab AZ |
2185 | } |
2186 | ||
7257b535 BP |
2187 | static bool |
2188 | ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type) | |
2189 | { | |
2190 | if (!strcasecmp(s, "no")) { | |
2191 | *type = OVS_FRAG_TYPE_NONE; | |
2192 | } else if (!strcasecmp(s, "first")) { | |
2193 | *type = OVS_FRAG_TYPE_FIRST; | |
2194 | } else if (!strcasecmp(s, "later")) { | |
2195 | *type = OVS_FRAG_TYPE_LATER; | |
2196 | } else { | |
2197 | return false; | |
2198 | } | |
2199 | return true; | |
2200 | } | |
2201 | ||
2d18eae8 | 2202 | /* Parsing. */ |
b02475c5 | 2203 | |
3bffc610 | 2204 | static int |
2d18eae8 JR |
2205 | scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN], |
2206 | uint8_t (*mask)[ETH_ADDR_LEN]) | |
3bffc610 | 2207 | { |
2d18eae8 | 2208 | int n; |
abff858b | 2209 | |
2d18eae8 JR |
2210 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) { |
2211 | int len = n; | |
2212 | ||
2213 | if (mask) { | |
2214 | if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n", | |
2215 | ETH_ADDR_SCAN_ARGS(*mask), &n)) { | |
2216 | len += n; | |
2217 | } else { | |
2218 | memset(mask, 0xff, sizeof *mask); | |
e6cc0bab | 2219 | } |
abff858b | 2220 | } |
2d18eae8 | 2221 | return len; |
abff858b | 2222 | } |
2d18eae8 JR |
2223 | return 0; |
2224 | } | |
abff858b | 2225 | |
2d18eae8 JR |
2226 | static int |
2227 | scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
2228 | { | |
2229 | int n; | |
72e8bf28 | 2230 | |
2d18eae8 JR |
2231 | if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) { |
2232 | int len = n; | |
2233 | ||
2234 | if (mask) { | |
2235 | if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n", | |
2236 | IP_SCAN_ARGS(mask), &n)) { | |
2237 | len += n; | |
2238 | } else { | |
2239 | *mask = OVS_BE32_MAX; | |
e6cc0bab | 2240 | } |
72e8bf28 | 2241 | } |
2d18eae8 | 2242 | return len; |
72e8bf28 | 2243 | } |
2d18eae8 JR |
2244 | return 0; |
2245 | } | |
72e8bf28 | 2246 | |
2d18eae8 JR |
2247 | static int |
2248 | scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4]) | |
2249 | { | |
2250 | int n; | |
2251 | char ipv6_s[IPV6_SCAN_LEN + 1]; | |
572f732a | 2252 | |
2d18eae8 JR |
2253 | if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n) |
2254 | && inet_pton(AF_INET6, ipv6_s, key) == 1) { | |
2255 | int len = n; | |
2256 | ||
2257 | if (mask) { | |
2258 | if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n) | |
2259 | && inet_pton(AF_INET6, ipv6_s, mask) == 1) { | |
2260 | len += n; | |
2261 | } else { | |
2262 | memset(mask, 0xff, sizeof *mask); | |
78669073 | 2263 | } |
572f732a | 2264 | } |
2d18eae8 | 2265 | return len; |
572f732a | 2266 | } |
2d18eae8 JR |
2267 | return 0; |
2268 | } | |
572f732a | 2269 | |
2d18eae8 JR |
2270 | static int |
2271 | scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
2272 | { | |
2273 | int key_, mask_; | |
2274 | int n; | |
572f732a | 2275 | |
2d18eae8 JR |
2276 | if (ovs_scan(s, "%i%n", &key_, &n) |
2277 | && (key_ & ~IPV6_LABEL_MASK) == 0) { | |
2278 | int len = n; | |
29c7a2b2 | 2279 | |
2d18eae8 JR |
2280 | *key = htonl(key_); |
2281 | if (mask) { | |
2282 | if (ovs_scan(s + len, "/%i%n", &mask_, &n) | |
2283 | && (mask_ & ~IPV6_LABEL_MASK) == 0) { | |
2284 | len += n; | |
2285 | *mask = htonl(mask_); | |
2286 | } else { | |
2287 | *mask = htonl(IPV6_LABEL_MASK); | |
e6cc0bab | 2288 | } |
35651d6a | 2289 | } |
2d18eae8 | 2290 | return len; |
35651d6a | 2291 | } |
2d18eae8 JR |
2292 | return 0; |
2293 | } | |
35651d6a | 2294 | |
2d18eae8 JR |
2295 | static int |
2296 | scan_u8(const char *s, uint8_t *key, uint8_t *mask) | |
2297 | { | |
2298 | int n; | |
3bffc610 | 2299 | |
2d18eae8 JR |
2300 | if (ovs_scan(s, "%"SCNi8"%n", key, &n)) { |
2301 | int len = n; | |
2302 | ||
2303 | if (mask) { | |
2304 | if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) { | |
2305 | len += n; | |
2306 | } else { | |
2307 | *mask = UINT8_MAX; | |
e6cc0bab | 2308 | } |
3bffc610 | 2309 | } |
2d18eae8 | 2310 | return len; |
3bffc610 | 2311 | } |
2d18eae8 JR |
2312 | return 0; |
2313 | } | |
3bffc610 | 2314 | |
2d18eae8 JR |
2315 | static int |
2316 | scan_u32(const char *s, uint32_t *key, uint32_t *mask) | |
2317 | { | |
2318 | int n; | |
e6cc0bab | 2319 | |
2d18eae8 JR |
2320 | if (ovs_scan(s, "%"SCNi32"%n", key, &n)) { |
2321 | int len = n; | |
e6cc0bab | 2322 | |
2d18eae8 JR |
2323 | if (mask) { |
2324 | if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) { | |
2325 | len += n; | |
2326 | } else { | |
2327 | *mask = UINT32_MAX; | |
e6cc0bab | 2328 | } |
b2a60db8 | 2329 | } |
2d18eae8 | 2330 | return len; |
b2a60db8 | 2331 | } |
2d18eae8 JR |
2332 | return 0; |
2333 | } | |
b2a60db8 | 2334 | |
2d18eae8 JR |
2335 | static int |
2336 | scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
2337 | { | |
2338 | uint16_t key_, mask_; | |
2339 | int n; | |
3bffc610 | 2340 | |
2d18eae8 JR |
2341 | if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) { |
2342 | int len = n; | |
e6cc0bab | 2343 | |
2d18eae8 JR |
2344 | *key = htons(key_); |
2345 | if (mask) { | |
2346 | if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) { | |
2347 | len += n; | |
2348 | *mask = htons(mask_); | |
2349 | } else { | |
2350 | *mask = OVS_BE16_MAX; | |
e6cc0bab | 2351 | } |
3bffc610 | 2352 | } |
2d18eae8 | 2353 | return len; |
3bffc610 | 2354 | } |
2d18eae8 JR |
2355 | return 0; |
2356 | } | |
3bffc610 | 2357 | |
2d18eae8 JR |
2358 | static int |
2359 | scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask) | |
2360 | { | |
2361 | uint64_t key_, mask_; | |
2362 | int n; | |
3bffc610 | 2363 | |
2d18eae8 JR |
2364 | if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) { |
2365 | int len = n; | |
2366 | ||
2367 | *key = htonll(key_); | |
2368 | if (mask) { | |
2369 | if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) { | |
2370 | len += n; | |
2371 | *mask = htonll(mask_); | |
2372 | } else { | |
2373 | *mask = OVS_BE64_MAX; | |
e6cc0bab | 2374 | } |
3bffc610 | 2375 | } |
2d18eae8 | 2376 | return len; |
3bffc610 | 2377 | } |
2d18eae8 JR |
2378 | return 0; |
2379 | } | |
3bffc610 | 2380 | |
2d18eae8 JR |
2381 | static int |
2382 | scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask) | |
2383 | { | |
2384 | uint32_t flags, fmask; | |
2385 | int n; | |
3bffc610 | 2386 | |
2d18eae8 JR |
2387 | n = parse_flags(s, flow_tun_flag_to_string, &flags, |
2388 | FLOW_TNL_F_MASK, mask ? &fmask : NULL); | |
2389 | if (n >= 0 && s[n] == ')') { | |
2390 | *key = flags; | |
2391 | if (mask) { | |
2392 | *mask = fmask; | |
3bffc610 | 2393 | } |
2d18eae8 | 2394 | return n + 1; |
3bffc610 | 2395 | } |
2d18eae8 JR |
2396 | return 0; |
2397 | } | |
3bffc610 | 2398 | |
2d18eae8 JR |
2399 | static int |
2400 | scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
2401 | { | |
2402 | uint32_t flags, fmask; | |
2403 | int n; | |
b02475c5 | 2404 | |
2d18eae8 JR |
2405 | n = parse_flags(s, packet_tcp_flag_to_string, &flags, |
2406 | TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL); | |
2407 | if (n >= 0) { | |
2408 | *key = htons(flags); | |
2409 | if (mask) { | |
2410 | *mask = htons(fmask); | |
b02475c5 | 2411 | } |
2d18eae8 | 2412 | return n; |
b02475c5 | 2413 | } |
2d18eae8 JR |
2414 | return 0; |
2415 | } | |
b02475c5 | 2416 | |
2d18eae8 JR |
2417 | static int |
2418 | scan_frag(const char *s, uint8_t *key, uint8_t *mask) | |
2419 | { | |
2420 | int n; | |
2421 | char frag[8]; | |
2422 | enum ovs_frag_type frag_type; | |
e6cc0bab | 2423 | |
2d18eae8 JR |
2424 | if (ovs_scan(s, "%7[a-z]%n", frag, &n) |
2425 | && ovs_frag_type_from_string(frag, &frag_type)) { | |
2426 | int len = n; | |
e6cc0bab | 2427 | |
2d18eae8 JR |
2428 | *key = frag_type; |
2429 | if (mask) { | |
2430 | *mask = UINT8_MAX; | |
3bffc610 | 2431 | } |
2d18eae8 | 2432 | return len; |
3bffc610 | 2433 | } |
2d18eae8 JR |
2434 | return 0; |
2435 | } | |
3bffc610 | 2436 | |
2d18eae8 JR |
2437 | static int |
2438 | scan_port(const char *s, uint32_t *key, uint32_t *mask, | |
2439 | const struct simap *port_names) | |
2440 | { | |
2441 | int n; | |
3bffc610 | 2442 | |
2d18eae8 JR |
2443 | if (ovs_scan(s, "%"SCNi32"%n", key, &n)) { |
2444 | int len = n; | |
e6cc0bab | 2445 | |
2d18eae8 JR |
2446 | if (mask) { |
2447 | if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) { | |
2448 | len += n; | |
2449 | } else { | |
2450 | *mask = UINT32_MAX; | |
e6cc0bab | 2451 | } |
3bffc610 | 2452 | } |
2d18eae8 JR |
2453 | return len; |
2454 | } else if (port_names) { | |
2455 | const struct simap_node *node; | |
2456 | int len; | |
e6cc0bab | 2457 | |
2d18eae8 JR |
2458 | len = strcspn(s, ")"); |
2459 | node = simap_find_len(port_names, s, len); | |
2460 | if (node) { | |
2461 | *key = node->data; | |
e6cc0bab AZ |
2462 | |
2463 | if (mask) { | |
2d18eae8 | 2464 | *mask = UINT32_MAX; |
e6cc0bab | 2465 | } |
2d18eae8 | 2466 | return len; |
3bffc610 BP |
2467 | } |
2468 | } | |
2d18eae8 JR |
2469 | return 0; |
2470 | } | |
3bffc610 | 2471 | |
2d18eae8 JR |
2472 | /* Helper for vlan parsing. */ |
2473 | struct ovs_key_vlan__ { | |
2474 | ovs_be16 tci; | |
2475 | }; | |
dc235f7f | 2476 | |
2d18eae8 JR |
2477 | static bool |
2478 | set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value) | |
2479 | { | |
2480 | const uint16_t mask = ((1U << bits) - 1) << offset; | |
ea2735d3 | 2481 | |
2d18eae8 JR |
2482 | if (value >> bits) { |
2483 | return false; | |
dc235f7f JR |
2484 | } |
2485 | ||
2d18eae8 JR |
2486 | *bf = htons((ntohs(*bf) & ~mask) | (value << offset)); |
2487 | return true; | |
2488 | } | |
e6cc0bab | 2489 | |
2d18eae8 JR |
2490 | static int |
2491 | scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits, | |
2492 | uint8_t offset) | |
2493 | { | |
2494 | uint16_t key_, mask_; | |
2495 | int n; | |
3bffc610 | 2496 | |
2d18eae8 JR |
2497 | if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) { |
2498 | int len = n; | |
e6cc0bab | 2499 | |
2d18eae8 | 2500 | if (set_be16_bf(key, bits, offset, key_)) { |
e6cc0bab | 2501 | if (mask) { |
2d18eae8 JR |
2502 | if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) { |
2503 | len += n; | |
2504 | ||
2505 | if (!set_be16_bf(mask, bits, offset, mask_)) { | |
2506 | return 0; | |
2507 | } | |
2508 | } else { | |
2509 | *mask |= htons(((1U << bits) - 1) << offset); | |
2510 | } | |
e6cc0bab | 2511 | } |
2d18eae8 | 2512 | return len; |
3bffc610 BP |
2513 | } |
2514 | } | |
2d18eae8 JR |
2515 | return 0; |
2516 | } | |
3bffc610 | 2517 | |
2d18eae8 JR |
2518 | static int |
2519 | scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
2520 | { | |
2521 | return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT); | |
2522 | } | |
c6bcb685 | 2523 | |
2d18eae8 JR |
2524 | static int |
2525 | scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
2526 | { | |
2527 | return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT); | |
2528 | } | |
c6bcb685 | 2529 | |
2d18eae8 JR |
2530 | static int |
2531 | scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
2532 | { | |
2533 | return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT); | |
2534 | } | |
c6bcb685 | 2535 | |
2d18eae8 JR |
2536 | /* For MPLS. */ |
2537 | static bool | |
2538 | set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value) | |
2539 | { | |
2540 | const uint32_t mask = ((1U << bits) - 1) << offset; | |
c6bcb685 | 2541 | |
2d18eae8 JR |
2542 | if (value >> bits) { |
2543 | return false; | |
c6bcb685 JS |
2544 | } |
2545 | ||
2d18eae8 JR |
2546 | *bf = htonl((ntohl(*bf) & ~mask) | (value << offset)); |
2547 | return true; | |
2548 | } | |
3bffc610 | 2549 | |
2d18eae8 JR |
2550 | static int |
2551 | scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits, | |
2552 | uint8_t offset) | |
2553 | { | |
2554 | uint32_t key_, mask_; | |
2555 | int n; | |
3bffc610 | 2556 | |
2d18eae8 JR |
2557 | if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) { |
2558 | int len = n; | |
e6cc0bab | 2559 | |
2d18eae8 | 2560 | if (set_be32_bf(key, bits, offset, key_)) { |
e6cc0bab | 2561 | if (mask) { |
2d18eae8 JR |
2562 | if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) { |
2563 | len += n; | |
3bffc610 | 2564 | |
2d18eae8 JR |
2565 | if (!set_be32_bf(mask, bits, offset, mask_)) { |
2566 | return 0; | |
2567 | } | |
2568 | } else { | |
2569 | *mask |= htonl(((1U << bits) - 1) << offset); | |
2570 | } | |
e6cc0bab | 2571 | } |
2d18eae8 | 2572 | return len; |
3bffc610 BP |
2573 | } |
2574 | } | |
2d18eae8 JR |
2575 | return 0; |
2576 | } | |
3bffc610 | 2577 | |
2d18eae8 JR |
2578 | static int |
2579 | scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
2580 | { | |
2581 | return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT); | |
2582 | } | |
3bffc610 | 2583 | |
2d18eae8 JR |
2584 | static int |
2585 | scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
2586 | { | |
2587 | return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT); | |
2588 | } | |
e6cc0bab | 2589 | |
2d18eae8 JR |
2590 | static int |
2591 | scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
2592 | { | |
2593 | return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT); | |
2594 | } | |
e6cc0bab | 2595 | |
2d18eae8 JR |
2596 | static int |
2597 | scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
2598 | { | |
2599 | return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT); | |
2600 | } | |
2601 | ||
2602 | /* ATTR is compile-time constant, so only the case with correct data type | |
2603 | * will be used. However, the compiler complains about the data type for | |
2604 | * the other cases, so we must cast to make the compiler silent. */ | |
2605 | #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \ | |
2606 | if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \ | |
2607 | tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \ | |
2608 | } else { \ | |
2609 | nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \ | |
2610 | } | |
2611 | ||
2612 | #define SCAN_IF(NAME) \ | |
2613 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
2614 | const char *start = s; \ | |
2615 | int len; \ | |
2616 | \ | |
2617 | s += strlen(NAME) | |
2618 | ||
2619 | /* Usually no special initialization is needed. */ | |
2620 | #define SCAN_BEGIN(NAME, TYPE) \ | |
2621 | SCAN_IF(NAME); \ | |
2622 | TYPE skey, smask; \ | |
2623 | memset(&skey, 0, sizeof skey); \ | |
2624 | memset(&smask, 0, sizeof smask); \ | |
2625 | do { \ | |
2626 | len = 0; | |
2627 | ||
657ac953 JR |
2628 | /* Init as fully-masked as mask will not be scanned. */ |
2629 | #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \ | |
2630 | SCAN_IF(NAME); \ | |
2631 | TYPE skey, smask; \ | |
2632 | memset(&skey, 0, sizeof skey); \ | |
2633 | memset(&smask, 0xff, sizeof smask); \ | |
2634 | do { \ | |
2635 | len = 0; | |
2636 | ||
2d18eae8 JR |
2637 | /* VLAN needs special initialization. */ |
2638 | #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \ | |
2639 | SCAN_IF(NAME); \ | |
2640 | TYPE skey = KEY_INIT; \ | |
2641 | TYPE smask = MASK_INIT; \ | |
2642 | do { \ | |
2643 | len = 0; | |
2644 | ||
2645 | /* Scan unnamed entry as 'TYPE' */ | |
2646 | #define SCAN_TYPE(TYPE, KEY, MASK) \ | |
2647 | len = scan_##TYPE(s, KEY, MASK); \ | |
2648 | if (len == 0) { \ | |
2649 | return -EINVAL; \ | |
2650 | } \ | |
2651 | s += len | |
2652 | ||
2653 | /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */ | |
2654 | #define SCAN_FIELD(NAME, TYPE, FIELD) \ | |
2655 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
2656 | s += strlen(NAME); \ | |
2657 | SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \ | |
2658 | continue; \ | |
2659 | } | |
2660 | ||
2661 | #define SCAN_FINISH() \ | |
2662 | } while (*s++ == ',' && len != 0); \ | |
2663 | if (s[-1] != ')') { \ | |
2664 | return -EINVAL; \ | |
2665 | } | |
2666 | ||
2667 | #define SCAN_FINISH_SINGLE() \ | |
2668 | } while (false); \ | |
2669 | if (*s++ != ')') { \ | |
2670 | return -EINVAL; \ | |
2671 | } | |
2672 | ||
2673 | #define SCAN_PUT(ATTR) \ | |
2674 | if (!mask || !is_all_zeros(&smask, sizeof smask)) { \ | |
2675 | SCAN_PUT_ATTR(key, ATTR, skey); \ | |
2676 | if (mask) { \ | |
2677 | SCAN_PUT_ATTR(mask, ATTR, smask); \ | |
2678 | } \ | |
2679 | } | |
2680 | ||
2681 | #define SCAN_END(ATTR) \ | |
2682 | SCAN_FINISH(); \ | |
2683 | SCAN_PUT(ATTR); \ | |
2684 | return s - start; \ | |
2685 | } | |
2686 | ||
2687 | #define SCAN_END_SINGLE(ATTR) \ | |
2688 | SCAN_FINISH_SINGLE(); \ | |
2689 | SCAN_PUT(ATTR); \ | |
2690 | return s - start; \ | |
2691 | } | |
2692 | ||
2693 | #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \ | |
2694 | SCAN_BEGIN(NAME, TYPE) { \ | |
2695 | SCAN_TYPE(SCAN_AS, &skey, &smask); \ | |
2696 | } SCAN_END_SINGLE(ATTR) | |
2697 | ||
657ac953 JR |
2698 | #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \ |
2699 | SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \ | |
2700 | SCAN_TYPE(SCAN_AS, &skey, NULL); \ | |
2d18eae8 JR |
2701 | } SCAN_END_SINGLE(ATTR) |
2702 | ||
2703 | /* scan_port needs one extra argument. */ | |
2704 | #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \ | |
2705 | SCAN_BEGIN(NAME, TYPE) { \ | |
2706 | len = scan_port(s, &skey, &smask, port_names); \ | |
2707 | if (len == 0) { \ | |
2708 | return -EINVAL; \ | |
2709 | } \ | |
2710 | s += len; \ | |
2711 | } SCAN_END_SINGLE(ATTR) | |
3bffc610 | 2712 | |
2d18eae8 JR |
2713 | static int |
2714 | parse_odp_key_mask_attr(const char *s, const struct simap *port_names, | |
2715 | struct ofpbuf *key, struct ofpbuf *mask) | |
2716 | { | |
2717 | SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY); | |
2718 | SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK); | |
657ac953 JR |
2719 | SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32, |
2720 | OVS_KEY_ATTR_RECIRC_ID); | |
2d18eae8 JR |
2721 | SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH); |
2722 | ||
2723 | SCAN_BEGIN("tunnel(", struct flow_tnl) { | |
2724 | SCAN_FIELD("tun_id=", be64, tun_id); | |
2725 | SCAN_FIELD("src=", ipv4, ip_src); | |
2726 | SCAN_FIELD("dst=", ipv4, ip_dst); | |
2727 | SCAN_FIELD("tos=", u8, ip_tos); | |
2728 | SCAN_FIELD("ttl=", u8, ip_ttl); | |
2729 | SCAN_FIELD("tp_src=", be16, tp_src); | |
2730 | SCAN_FIELD("tp_dst=", be16, tp_dst); | |
ac6073e3 MC |
2731 | SCAN_FIELD("gbp_id=", be16, gbp_id); |
2732 | SCAN_FIELD("gbp_flags=", u8, gbp_flags); | |
2d18eae8 JR |
2733 | SCAN_FIELD("flags(", tun_flags, flags); |
2734 | } SCAN_END(OVS_KEY_ATTR_TUNNEL); | |
2735 | ||
2736 | SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT); | |
2737 | ||
2738 | SCAN_BEGIN("eth(", struct ovs_key_ethernet) { | |
2739 | SCAN_FIELD("src=", eth, eth_src); | |
2740 | SCAN_FIELD("dst=", eth, eth_dst); | |
2741 | } SCAN_END(OVS_KEY_ATTR_ETHERNET); | |
2742 | ||
2743 | SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__, | |
2744 | { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) { | |
2745 | SCAN_FIELD("vid=", vid, tci); | |
2746 | SCAN_FIELD("pcp=", pcp, tci); | |
2747 | SCAN_FIELD("cfi=", cfi, tci); | |
2748 | } SCAN_END(OVS_KEY_ATTR_VLAN); | |
2749 | ||
2750 | SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE); | |
2751 | ||
2752 | SCAN_BEGIN("mpls(", struct ovs_key_mpls) { | |
2753 | SCAN_FIELD("label=", mpls_label, mpls_lse); | |
2754 | SCAN_FIELD("tc=", mpls_tc, mpls_lse); | |
2755 | SCAN_FIELD("ttl=", mpls_ttl, mpls_lse); | |
2756 | SCAN_FIELD("bos=", mpls_bos, mpls_lse); | |
2757 | } SCAN_END(OVS_KEY_ATTR_MPLS); | |
2758 | ||
2759 | SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) { | |
2760 | SCAN_FIELD("src=", ipv4, ipv4_src); | |
2761 | SCAN_FIELD("dst=", ipv4, ipv4_dst); | |
2762 | SCAN_FIELD("proto=", u8, ipv4_proto); | |
2763 | SCAN_FIELD("tos=", u8, ipv4_tos); | |
2764 | SCAN_FIELD("ttl=", u8, ipv4_ttl); | |
2765 | SCAN_FIELD("frag=", frag, ipv4_frag); | |
2766 | } SCAN_END(OVS_KEY_ATTR_IPV4); | |
2767 | ||
2768 | SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) { | |
2769 | SCAN_FIELD("src=", ipv6, ipv6_src); | |
2770 | SCAN_FIELD("dst=", ipv6, ipv6_dst); | |
2771 | SCAN_FIELD("label=", ipv6_label, ipv6_label); | |
2772 | SCAN_FIELD("proto=", u8, ipv6_proto); | |
2773 | SCAN_FIELD("tclass=", u8, ipv6_tclass); | |
2774 | SCAN_FIELD("hlimit=", u8, ipv6_hlimit); | |
2775 | SCAN_FIELD("frag=", frag, ipv6_frag); | |
2776 | } SCAN_END(OVS_KEY_ATTR_IPV6); | |
2777 | ||
2778 | SCAN_BEGIN("tcp(", struct ovs_key_tcp) { | |
2779 | SCAN_FIELD("src=", be16, tcp_src); | |
2780 | SCAN_FIELD("dst=", be16, tcp_dst); | |
2781 | } SCAN_END(OVS_KEY_ATTR_TCP); | |
2782 | ||
2783 | SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS); | |
2784 | ||
2785 | SCAN_BEGIN("udp(", struct ovs_key_udp) { | |
2786 | SCAN_FIELD("src=", be16, udp_src); | |
2787 | SCAN_FIELD("dst=", be16, udp_dst); | |
2788 | } SCAN_END(OVS_KEY_ATTR_UDP); | |
2789 | ||
2790 | SCAN_BEGIN("sctp(", struct ovs_key_sctp) { | |
2791 | SCAN_FIELD("src=", be16, sctp_src); | |
2792 | SCAN_FIELD("dst=", be16, sctp_dst); | |
2793 | } SCAN_END(OVS_KEY_ATTR_SCTP); | |
2794 | ||
2795 | SCAN_BEGIN("icmp(", struct ovs_key_icmp) { | |
2796 | SCAN_FIELD("type=", u8, icmp_type); | |
2797 | SCAN_FIELD("code=", u8, icmp_code); | |
2798 | } SCAN_END(OVS_KEY_ATTR_ICMP); | |
2799 | ||
2800 | SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) { | |
2801 | SCAN_FIELD("type=", u8, icmpv6_type); | |
2802 | SCAN_FIELD("code=", u8, icmpv6_code); | |
2803 | } SCAN_END(OVS_KEY_ATTR_ICMPV6); | |
2804 | ||
2805 | SCAN_BEGIN("arp(", struct ovs_key_arp) { | |
2806 | SCAN_FIELD("sip=", ipv4, arp_sip); | |
2807 | SCAN_FIELD("tip=", ipv4, arp_tip); | |
2808 | SCAN_FIELD("op=", be16, arp_op); | |
2809 | SCAN_FIELD("sha=", eth, arp_sha); | |
2810 | SCAN_FIELD("tha=", eth, arp_tha); | |
2811 | } SCAN_END(OVS_KEY_ATTR_ARP); | |
2812 | ||
2813 | SCAN_BEGIN("nd(", struct ovs_key_nd) { | |
2814 | SCAN_FIELD("target=", ipv6, nd_target); | |
2815 | SCAN_FIELD("sll=", eth, nd_sll); | |
2816 | SCAN_FIELD("tll=", eth, nd_tll); | |
2817 | } SCAN_END(OVS_KEY_ATTR_ND); | |
2818 | ||
2819 | /* Encap open-coded. */ | |
fea393b1 BP |
2820 | if (!strncmp(s, "encap(", 6)) { |
2821 | const char *start = s; | |
e6cc0bab | 2822 | size_t encap, encap_mask = 0; |
fea393b1 BP |
2823 | |
2824 | encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP); | |
e6cc0bab AZ |
2825 | if (mask) { |
2826 | encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP); | |
2827 | } | |
fea393b1 BP |
2828 | |
2829 | s += 6; | |
2830 | for (;;) { | |
2831 | int retval; | |
2832 | ||
70fbe375 | 2833 | s += strspn(s, delimiters); |
fea393b1 BP |
2834 | if (!*s) { |
2835 | return -EINVAL; | |
2836 | } else if (*s == ')') { | |
2837 | break; | |
2838 | } | |
2839 | ||
e6cc0bab | 2840 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
fea393b1 BP |
2841 | if (retval < 0) { |
2842 | return retval; | |
2843 | } | |
2844 | s += retval; | |
2845 | } | |
2846 | s++; | |
2847 | ||
2848 | nl_msg_end_nested(key, encap); | |
e6cc0bab AZ |
2849 | if (mask) { |
2850 | nl_msg_end_nested(mask, encap_mask); | |
2851 | } | |
fea393b1 BP |
2852 | |
2853 | return s - start; | |
2854 | } | |
2855 | ||
3bffc610 BP |
2856 | return -EINVAL; |
2857 | } | |
2858 | ||
df2c07f4 JP |
2859 | /* Parses the string representation of a datapath flow key, in the |
2860 | * format output by odp_flow_key_format(). Returns 0 if successful, | |
2861 | * otherwise a positive errno value. On success, the flow key is | |
2862 | * appended to 'key' as a series of Netlink attributes. On failure, no | |
2863 | * data is appended to 'key'. Either way, 'key''s data might be | |
2864 | * reallocated. | |
3bffc610 | 2865 | * |
44bac24b BP |
2866 | * If 'port_names' is nonnull, it points to an simap that maps from a port name |
2867 | * to a port number. (Port names may be used instead of port numbers in | |
2868 | * in_port.) | |
b2a60db8 | 2869 | * |
3bffc610 BP |
2870 | * On success, the attributes appended to 'key' are individually syntactically |
2871 | * valid, but they may not be valid as a sequence. 'key' might, for example, | |
34118cae | 2872 | * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */ |
3bffc610 | 2873 | int |
e6cc0bab AZ |
2874 | odp_flow_from_string(const char *s, const struct simap *port_names, |
2875 | struct ofpbuf *key, struct ofpbuf *mask) | |
3bffc610 | 2876 | { |
6fd6ed71 | 2877 | const size_t old_size = key->size; |
3bffc610 BP |
2878 | for (;;) { |
2879 | int retval; | |
2880 | ||
7202cbe5 | 2881 | s += strspn(s, delimiters); |
3bffc610 BP |
2882 | if (!*s) { |
2883 | return 0; | |
2884 | } | |
2885 | ||
e6cc0bab | 2886 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
3bffc610 | 2887 | if (retval < 0) { |
6fd6ed71 | 2888 | key->size = old_size; |
3bffc610 BP |
2889 | return -retval; |
2890 | } | |
2891 | s += retval; | |
2892 | } | |
2893 | ||
2894 | return 0; | |
2895 | } | |
2896 | ||
7257b535 | 2897 | static uint8_t |
3cea18ec | 2898 | ovs_to_odp_frag(uint8_t nw_frag, bool is_mask) |
7257b535 | 2899 | { |
3cea18ec JR |
2900 | if (is_mask) { |
2901 | /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type, | |
2902 | * not a set of flags or bitfields. Hence, if the struct flow nw_frag | |
2903 | * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we | |
2904 | * must use a zero mask for the netlink frag field, and all ones mask | |
2905 | * otherwise. */ | |
2906 | return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0; | |
2907 | } | |
2c0f0be1 JR |
2908 | return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE |
2909 | : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER | |
2910 | : OVS_FRAG_TYPE_FIRST; | |
7257b535 BP |
2911 | } |
2912 | ||
3cea18ec JR |
2913 | static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *); |
2914 | static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *); | |
2915 | static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *, | |
2916 | bool is_mask); | |
2917 | static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *, | |
2918 | bool is_mask); | |
2919 | static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *, | |
2920 | bool is_mask); | |
2921 | static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *, | |
2922 | bool is_mask); | |
2923 | static void get_arp_key(const struct flow *, struct ovs_key_arp *); | |
2924 | static void put_arp_key(const struct ovs_key_arp *, struct flow *); | |
e60e935b SRCSA |
2925 | static void get_nd_key(const struct flow *, struct ovs_key_nd *); |
2926 | static void put_nd_key(const struct ovs_key_nd *, struct flow *); | |
3cea18ec JR |
2927 | |
2928 | /* These share the same layout. */ | |
2929 | union ovs_key_tp { | |
2930 | struct ovs_key_tcp tcp; | |
2931 | struct ovs_key_udp udp; | |
2932 | struct ovs_key_sctp sctp; | |
2933 | }; | |
2934 | ||
2935 | static void get_tp_key(const struct flow *, union ovs_key_tp *); | |
2936 | static void put_tp_key(const union ovs_key_tp *, struct flow *); | |
431495b1 | 2937 | |
661cbcd5 | 2938 | static void |
fbfe01de AZ |
2939 | odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow, |
2940 | const struct flow *mask, odp_port_t odp_in_port, | |
7ce2769e | 2941 | size_t max_mpls_depth, bool recirc, bool export_mask) |
14608a15 | 2942 | { |
df2c07f4 | 2943 | struct ovs_key_ethernet *eth_key; |
fea393b1 | 2944 | size_t encap; |
fbfe01de | 2945 | const struct flow *data = export_mask ? mask : flow; |
661cbcd5 | 2946 | |
54bb0348 | 2947 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority); |
abff858b | 2948 | |
fbfe01de | 2949 | if (flow->tunnel.ip_dst || export_mask) { |
661cbcd5 | 2950 | tun_key_to_attr(buf, &data->tunnel); |
36956a7d BP |
2951 | } |
2952 | ||
1362e248 | 2953 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark); |
72e8bf28 | 2954 | |
7ce2769e | 2955 | if (recirc) { |
572f732a | 2956 | nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id); |
572f732a AZ |
2957 | nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash); |
2958 | } | |
2959 | ||
661cbcd5 JP |
2960 | /* Add an ingress port attribute if this is a mask or 'odp_in_port' |
2961 | * is not the magical value "ODPP_NONE". */ | |
fbfe01de | 2962 | if (export_mask || odp_in_port != ODPP_NONE) { |
4e022ec0 | 2963 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port); |
18886b60 | 2964 | } |
36956a7d | 2965 | |
df2c07f4 | 2966 | eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET, |
36956a7d | 2967 | sizeof *eth_key); |
3cea18ec | 2968 | get_ethernet_key(data, eth_key); |
36956a7d | 2969 | |
8ddc056d | 2970 | if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) { |
fbfe01de | 2971 | if (export_mask) { |
b8266395 | 2972 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
bed7d6a1 JP |
2973 | } else { |
2974 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN)); | |
2975 | } | |
661cbcd5 | 2976 | nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci); |
fea393b1 | 2977 | encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP); |
8ddc056d BP |
2978 | if (flow->vlan_tci == htons(0)) { |
2979 | goto unencap; | |
2980 | } | |
fea393b1 BP |
2981 | } else { |
2982 | encap = 0; | |
36956a7d BP |
2983 | } |
2984 | ||
2985 | if (ntohs(flow->dl_type) < ETH_TYPE_MIN) { | |
661cbcd5 JP |
2986 | /* For backwards compatibility with kernels that don't support |
2987 | * wildcarding, the following convention is used to encode the | |
2988 | * OVS_KEY_ATTR_ETHERTYPE for key and mask: | |
2989 | * | |
2990 | * key mask matches | |
2991 | * -------- -------- ------- | |
2992 | * >0x5ff 0xffff Specified Ethernet II Ethertype. | |
2993 | * >0x5ff 0 Any Ethernet II or non-Ethernet II frame. | |
2994 | * <none> 0xffff Any non-Ethernet II frame (except valid | |
2995 | * 802.3 SNAP packet with valid eth_type). | |
2996 | */ | |
fbfe01de | 2997 | if (export_mask) { |
b8266395 | 2998 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
661cbcd5 | 2999 | } |
fea393b1 | 3000 | goto unencap; |
36956a7d BP |
3001 | } |
3002 | ||
661cbcd5 | 3003 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type); |
36956a7d BP |
3004 | |
3005 | if (flow->dl_type == htons(ETH_TYPE_IP)) { | |
df2c07f4 | 3006 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 3007 | |
df2c07f4 | 3008 | ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4, |
36956a7d | 3009 | sizeof *ipv4_key); |
3cea18ec | 3010 | get_ipv4_key(data, ipv4_key, export_mask); |
d31f1109 | 3011 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
df2c07f4 | 3012 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 3013 | |
df2c07f4 | 3014 | ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6, |
d31f1109 | 3015 | sizeof *ipv6_key); |
3cea18ec | 3016 | get_ipv6_key(data, ipv6_key, export_mask); |
8087f5ff MM |
3017 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
3018 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
df2c07f4 | 3019 | struct ovs_key_arp *arp_key; |
d31f1109 | 3020 | |
3cea18ec JR |
3021 | arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP, |
3022 | sizeof *arp_key); | |
3023 | get_arp_key(data, arp_key); | |
b0a17866 | 3024 | } else if (eth_type_mpls(flow->dl_type)) { |
b02475c5 | 3025 | struct ovs_key_mpls *mpls_key; |
8bfd0fda | 3026 | int i, n; |
b02475c5 | 3027 | |
8bfd0fda BP |
3028 | n = flow_count_mpls_labels(flow, NULL); |
3029 | n = MIN(n, max_mpls_depth); | |
b02475c5 | 3030 | mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS, |
8bfd0fda BP |
3031 | n * sizeof *mpls_key); |
3032 | for (i = 0; i < n; i++) { | |
3033 | mpls_key[i].mpls_lse = data->mpls_lse[i]; | |
3034 | } | |
b02475c5 SH |
3035 | } |
3036 | ||
48cecbdc | 3037 | if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
6767a2cc | 3038 | if (flow->nw_proto == IPPROTO_TCP) { |
3cea18ec | 3039 | union ovs_key_tp *tcp_key; |
36956a7d | 3040 | |
df2c07f4 | 3041 | tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP, |
36956a7d | 3042 | sizeof *tcp_key); |
3cea18ec | 3043 | get_tp_key(data, tcp_key); |
dc235f7f JR |
3044 | if (data->tcp_flags) { |
3045 | nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags); | |
3046 | } | |
6767a2cc | 3047 | } else if (flow->nw_proto == IPPROTO_UDP) { |
3cea18ec | 3048 | union ovs_key_tp *udp_key; |
36956a7d | 3049 | |
df2c07f4 | 3050 | udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP, |
36956a7d | 3051 | sizeof *udp_key); |
3cea18ec | 3052 | get_tp_key(data, udp_key); |
c6bcb685 | 3053 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
3cea18ec | 3054 | union ovs_key_tp *sctp_key; |
c6bcb685 JS |
3055 | |
3056 | sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP, | |
3057 | sizeof *sctp_key); | |
3cea18ec | 3058 | get_tp_key(data, sctp_key); |
d31f1109 JP |
3059 | } else if (flow->dl_type == htons(ETH_TYPE_IP) |
3060 | && flow->nw_proto == IPPROTO_ICMP) { | |
df2c07f4 | 3061 | struct ovs_key_icmp *icmp_key; |
36956a7d | 3062 | |
df2c07f4 | 3063 | icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP, |
36956a7d | 3064 | sizeof *icmp_key); |
661cbcd5 JP |
3065 | icmp_key->icmp_type = ntohs(data->tp_src); |
3066 | icmp_key->icmp_code = ntohs(data->tp_dst); | |
d31f1109 JP |
3067 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6) |
3068 | && flow->nw_proto == IPPROTO_ICMPV6) { | |
df2c07f4 | 3069 | struct ovs_key_icmpv6 *icmpv6_key; |
d31f1109 | 3070 | |
df2c07f4 | 3071 | icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6, |
d31f1109 | 3072 | sizeof *icmpv6_key); |
661cbcd5 JP |
3073 | icmpv6_key->icmpv6_type = ntohs(data->tp_src); |
3074 | icmpv6_key->icmpv6_code = ntohs(data->tp_dst); | |
685a51a5 | 3075 | |
d8efdda8 JS |
3076 | if (flow->tp_dst == htons(0) |
3077 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) | |
3078 | || flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) | |
3079 | && (!export_mask || (data->tp_src == htons(0xffff) | |
3080 | && data->tp_dst == htons(0xffff)))) { | |
6f8dbd27 | 3081 | |
df2c07f4 | 3082 | struct ovs_key_nd *nd_key; |
685a51a5 | 3083 | |
df2c07f4 | 3084 | nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND, |
685a51a5 | 3085 | sizeof *nd_key); |
661cbcd5 | 3086 | memcpy(nd_key->nd_target, &data->nd_target, |
685a51a5 | 3087 | sizeof nd_key->nd_target); |
661cbcd5 JP |
3088 | memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN); |
3089 | memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN); | |
685a51a5 | 3090 | } |
36956a7d | 3091 | } |
36956a7d | 3092 | } |
fea393b1 BP |
3093 | |
3094 | unencap: | |
3095 | if (encap) { | |
3096 | nl_msg_end_nested(buf, encap); | |
3097 | } | |
36956a7d | 3098 | } |
661cbcd5 JP |
3099 | |
3100 | /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. | |
3101 | * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port | |
3102 | * number rather than a datapath port number). Instead, if 'odp_in_port' | |
3103 | * is anything other than ODPP_NONE, it is included in 'buf' as the input | |
3104 | * port. | |
3105 | * | |
3106 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
7ce2769e JS |
3107 | * capable of being expanded to allow for that much space. |
3108 | * | |
3109 | * 'recirc' indicates support for recirculation fields. If this is true, then | |
3110 | * these fields will always be serialised. */ | |
661cbcd5 JP |
3111 | void |
3112 | odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow, | |
7ce2769e JS |
3113 | const struct flow *mask, odp_port_t odp_in_port, |
3114 | bool recirc) | |
661cbcd5 | 3115 | { |
7ce2769e JS |
3116 | odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc, |
3117 | false); | |
661cbcd5 JP |
3118 | } |
3119 | ||
3120 | /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to | |
3121 | * 'buf'. 'flow' is used as a template to determine how to interpret | |
3122 | * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but | |
3123 | * it doesn't indicate whether the other fields should be interpreted as | |
3124 | * ARP, IPv4, IPv6, etc. | |
3125 | * | |
3126 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
7ce2769e JS |
3127 | * capable of being expanded to allow for that much space. |
3128 | * | |
3129 | * 'recirc' indicates support for recirculation fields. If this is true, then | |
3130 | * these fields will always be serialised. */ | |
661cbcd5 JP |
3131 | void |
3132 | odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask, | |
8bfd0fda | 3133 | const struct flow *flow, uint32_t odp_in_port_mask, |
7ce2769e | 3134 | size_t max_mpls_depth, bool recirc) |
661cbcd5 | 3135 | { |
7ce2769e JS |
3136 | odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask), |
3137 | max_mpls_depth, recirc, true); | |
661cbcd5 | 3138 | } |
36956a7d | 3139 | |
758c456d JR |
3140 | /* Generate ODP flow key from the given packet metadata */ |
3141 | void | |
3142 | odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md) | |
3143 | { | |
3144 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority); | |
3145 | ||
3146 | if (md->tunnel.ip_dst) { | |
3147 | tun_key_to_attr(buf, &md->tunnel); | |
3148 | } | |
3149 | ||
3150 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark); | |
3151 | ||
3152 | /* Add an ingress port attribute if 'odp_in_port' is not the magical | |
3153 | * value "ODPP_NONE". */ | |
b5e7e61a AZ |
3154 | if (md->in_port.odp_port != ODPP_NONE) { |
3155 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port); | |
758c456d JR |
3156 | } |
3157 | } | |
3158 | ||
3159 | /* Generate packet metadata from the given ODP flow key. */ | |
3160 | void | |
3161 | odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len, | |
3162 | struct pkt_metadata *md) | |
3163 | { | |
3164 | const struct nlattr *nla; | |
3165 | size_t left; | |
3166 | uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY | | |
3167 | 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL | | |
3168 | 1u << OVS_KEY_ATTR_IN_PORT; | |
3169 | ||
b5e7e61a | 3170 | *md = PKT_METADATA_INITIALIZER(ODPP_NONE); |
758c456d JR |
3171 | |
3172 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { | |
3173 | uint16_t type = nl_attr_type(nla); | |
3174 | size_t len = nl_attr_get_size(nla); | |
3175 | int expected_len = odp_flow_key_attr_len(type); | |
3176 | ||
3177 | if (len != expected_len && expected_len >= 0) { | |
3178 | continue; | |
3179 | } | |
3180 | ||
572f732a AZ |
3181 | switch (type) { |
3182 | case OVS_KEY_ATTR_RECIRC_ID: | |
3183 | md->recirc_id = nl_attr_get_u32(nla); | |
3184 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID); | |
3185 | break; | |
3186 | case OVS_KEY_ATTR_DP_HASH: | |
3187 | md->dp_hash = nl_attr_get_u32(nla); | |
3188 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH); | |
3189 | break; | |
3190 | case OVS_KEY_ATTR_PRIORITY: | |
758c456d JR |
3191 | md->skb_priority = nl_attr_get_u32(nla); |
3192 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY); | |
572f732a AZ |
3193 | break; |
3194 | case OVS_KEY_ATTR_SKB_MARK: | |
758c456d JR |
3195 | md->pkt_mark = nl_attr_get_u32(nla); |
3196 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK); | |
572f732a AZ |
3197 | break; |
3198 | case OVS_KEY_ATTR_TUNNEL: { | |
758c456d JR |
3199 | enum odp_key_fitness res; |
3200 | ||
3201 | res = odp_tun_key_from_attr(nla, &md->tunnel); | |
3202 | if (res == ODP_FIT_ERROR) { | |
3203 | memset(&md->tunnel, 0, sizeof md->tunnel); | |
3204 | } else if (res == ODP_FIT_PERFECT) { | |
3205 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL); | |
3206 | } | |
572f732a AZ |
3207 | break; |
3208 | } | |
3209 | case OVS_KEY_ATTR_IN_PORT: | |
b5e7e61a | 3210 | md->in_port.odp_port = nl_attr_get_odp_port(nla); |
758c456d | 3211 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT); |
572f732a AZ |
3212 | break; |
3213 | default: | |
3214 | break; | |
758c456d JR |
3215 | } |
3216 | ||
3217 | if (!wanted_attrs) { | |
3218 | return; /* Have everything. */ | |
3219 | } | |
3220 | } | |
3221 | } | |
3222 | ||
b0f7b9b5 BP |
3223 | uint32_t |
3224 | odp_flow_key_hash(const struct nlattr *key, size_t key_len) | |
3225 | { | |
3226 | BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t))); | |
db5a1019 AW |
3227 | return hash_words(ALIGNED_CAST(const uint32_t *, key), |
3228 | key_len / sizeof(uint32_t), 0); | |
b0f7b9b5 BP |
3229 | } |
3230 | ||
34118cae BP |
3231 | static void |
3232 | log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title, | |
b0f7b9b5 | 3233 | uint64_t attrs, int out_of_range_attr, |
34118cae BP |
3234 | const struct nlattr *key, size_t key_len) |
3235 | { | |
3236 | struct ds s; | |
3237 | int i; | |
3238 | ||
b0f7b9b5 | 3239 | if (VLOG_DROP_DBG(rl)) { |
34118cae BP |
3240 | return; |
3241 | } | |
3242 | ||
3243 | ds_init(&s); | |
b0f7b9b5 BP |
3244 | for (i = 0; i < 64; i++) { |
3245 | if (attrs & (UINT64_C(1) << i)) { | |
e6603631 BP |
3246 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
3247 | ||
3248 | ds_put_format(&s, " %s", | |
3249 | ovs_key_attr_to_string(i, namebuf, sizeof namebuf)); | |
34118cae BP |
3250 | } |
3251 | } | |
b0f7b9b5 BP |
3252 | if (out_of_range_attr) { |
3253 | ds_put_format(&s, " %d (and possibly others)", out_of_range_attr); | |
3254 | } | |
34118cae BP |
3255 | |
3256 | ds_put_cstr(&s, ": "); | |
3257 | odp_flow_key_format(key, key_len, &s); | |
3258 | ||
b0f7b9b5 | 3259 | VLOG_DBG("%s:%s", title, ds_cstr(&s)); |
34118cae BP |
3260 | ds_destroy(&s); |
3261 | } | |
3262 | ||
3cea18ec JR |
3263 | static uint8_t |
3264 | odp_to_ovs_frag(uint8_t odp_frag, bool is_mask) | |
7257b535 | 3265 | { |
34118cae BP |
3266 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
3267 | ||
3cea18ec JR |
3268 | if (is_mask) { |
3269 | return odp_frag ? FLOW_NW_FRAG_MASK : 0; | |
3270 | } | |
3271 | ||
9e44d715 | 3272 | if (odp_frag > OVS_FRAG_TYPE_LATER) { |
b0f7b9b5 | 3273 | VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag); |
3cea18ec | 3274 | return 0xff; /* Error. */ |
7257b535 BP |
3275 | } |
3276 | ||
3cea18ec JR |
3277 | return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0 |
3278 | : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY | |
3279 | : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER; | |
7257b535 BP |
3280 | } |
3281 | ||
b0f7b9b5 | 3282 | static bool |
fea393b1 | 3283 | parse_flow_nlattrs(const struct nlattr *key, size_t key_len, |
b0f7b9b5 BP |
3284 | const struct nlattr *attrs[], uint64_t *present_attrsp, |
3285 | int *out_of_range_attrp) | |
36956a7d | 3286 | { |
b0f7b9b5 | 3287 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
36956a7d | 3288 | const struct nlattr *nla; |
34118cae | 3289 | uint64_t present_attrs; |
36956a7d BP |
3290 | size_t left; |
3291 | ||
2aef1214 | 3292 | BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs); |
34118cae | 3293 | present_attrs = 0; |
b0f7b9b5 | 3294 | *out_of_range_attrp = 0; |
36956a7d | 3295 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { |
34118cae BP |
3296 | uint16_t type = nl_attr_type(nla); |
3297 | size_t len = nl_attr_get_size(nla); | |
3298 | int expected_len = odp_flow_key_attr_len(type); | |
3299 | ||
b0f7b9b5 | 3300 | if (len != expected_len && expected_len >= 0) { |
e6603631 BP |
3301 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
3302 | ||
34582733 | 3303 | VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have " |
e6603631 BP |
3304 | "length %d", ovs_key_attr_to_string(type, namebuf, |
3305 | sizeof namebuf), | |
b0f7b9b5 BP |
3306 | len, expected_len); |
3307 | return false; | |
34118cae BP |
3308 | } |
3309 | ||
2aef1214 | 3310 | if (type > OVS_KEY_ATTR_MAX) { |
b0f7b9b5 BP |
3311 | *out_of_range_attrp = type; |
3312 | } else { | |
3313 | if (present_attrs & (UINT64_C(1) << type)) { | |
e6603631 BP |
3314 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
3315 | ||
b0f7b9b5 | 3316 | VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key", |
e6603631 BP |
3317 | ovs_key_attr_to_string(type, |
3318 | namebuf, sizeof namebuf)); | |
b0f7b9b5 BP |
3319 | return false; |
3320 | } | |
3321 | ||
3322 | present_attrs |= UINT64_C(1) << type; | |
3323 | attrs[type] = nla; | |
3324 | } | |
34118cae BP |
3325 | } |
3326 | if (left) { | |
3327 | VLOG_ERR_RL(&rl, "trailing garbage in flow key"); | |
b0f7b9b5 | 3328 | return false; |
34118cae BP |
3329 | } |
3330 | ||
fea393b1 | 3331 | *present_attrsp = present_attrs; |
b0f7b9b5 | 3332 | return true; |
fea393b1 BP |
3333 | } |
3334 | ||
b0f7b9b5 BP |
3335 | static enum odp_key_fitness |
3336 | check_expectations(uint64_t present_attrs, int out_of_range_attr, | |
3337 | uint64_t expected_attrs, | |
fea393b1 BP |
3338 | const struct nlattr *key, size_t key_len) |
3339 | { | |
3340 | uint64_t missing_attrs; | |
3341 | uint64_t extra_attrs; | |
3342 | ||
3343 | missing_attrs = expected_attrs & ~present_attrs; | |
3344 | if (missing_attrs) { | |
b0f7b9b5 BP |
3345 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
3346 | log_odp_key_attributes(&rl, "expected but not present", | |
3347 | missing_attrs, 0, key, key_len); | |
3348 | return ODP_FIT_TOO_LITTLE; | |
fea393b1 BP |
3349 | } |
3350 | ||
3351 | extra_attrs = present_attrs & ~expected_attrs; | |
b0f7b9b5 BP |
3352 | if (extra_attrs || out_of_range_attr) { |
3353 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
3354 | log_odp_key_attributes(&rl, "present but not expected", | |
3355 | extra_attrs, out_of_range_attr, key, key_len); | |
3356 | return ODP_FIT_TOO_MUCH; | |
fea393b1 BP |
3357 | } |
3358 | ||
b0f7b9b5 | 3359 | return ODP_FIT_PERFECT; |
fea393b1 BP |
3360 | } |
3361 | ||
b0f7b9b5 BP |
3362 | static bool |
3363 | parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
3364 | uint64_t present_attrs, uint64_t *expected_attrs, | |
4a221615 | 3365 | struct flow *flow, const struct flow *src_flow) |
fea393b1 BP |
3366 | { |
3367 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 3368 | bool is_mask = flow != src_flow; |
36956a7d | 3369 | |
fea393b1 | 3370 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
34118cae | 3371 | flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); |
4a221615 | 3372 | if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) { |
34118cae BP |
3373 | VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key", |
3374 | ntohs(flow->dl_type)); | |
b0f7b9b5 | 3375 | return false; |
34118cae | 3376 | } |
4a221615 GY |
3377 | if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN && |
3378 | flow->dl_type != htons(0xffff)) { | |
3379 | return false; | |
3380 | } | |
b0f7b9b5 | 3381 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; |
34118cae | 3382 | } else { |
4a221615 GY |
3383 | if (!is_mask) { |
3384 | flow->dl_type = htons(FLOW_DL_TYPE_NONE); | |
3385 | } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) { | |
3386 | /* See comments in odp_flow_key_from_flow__(). */ | |
3387 | VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame"); | |
3388 | return false; | |
3389 | } | |
34118cae | 3390 | } |
b0f7b9b5 BP |
3391 | return true; |
3392 | } | |
3393 | ||
3394 | static enum odp_key_fitness | |
b02475c5 SH |
3395 | parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], |
3396 | uint64_t present_attrs, int out_of_range_attr, | |
3397 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 | 3398 | const struct nlattr *key, size_t key_len, |
91a77332 | 3399 | const struct flow *src_flow) |
b0f7b9b5 BP |
3400 | { |
3401 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 GY |
3402 | bool is_mask = src_flow != flow; |
3403 | const void *check_start = NULL; | |
3404 | size_t check_len = 0; | |
3405 | enum ovs_key_attr expected_bit = 0xff; | |
3406 | ||
3407 | if (eth_type_mpls(src_flow->dl_type)) { | |
7e6621e9 | 3408 | if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { |
91a77332 | 3409 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); |
7e6621e9 JS |
3410 | } |
3411 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { | |
3412 | size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]); | |
3413 | const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]); | |
3414 | int n = size / sizeof(ovs_be32); | |
3415 | int i; | |
4a221615 | 3416 | |
7e6621e9 JS |
3417 | if (!size || size % sizeof(ovs_be32)) { |
3418 | return ODP_FIT_ERROR; | |
91a77332 | 3419 | } |
8bfd0fda | 3420 | if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) { |
4a221615 GY |
3421 | return ODP_FIT_ERROR; |
3422 | } | |
8bfd0fda | 3423 | |
7e6621e9 JS |
3424 | for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) { |
3425 | flow->mpls_lse[i] = mpls_lse[i]; | |
3426 | } | |
3427 | if (n > FLOW_MAX_MPLS_LABELS) { | |
3428 | return ODP_FIT_TOO_MUCH; | |
3429 | } | |
8bfd0fda | 3430 | |
7e6621e9 JS |
3431 | if (!is_mask) { |
3432 | /* BOS may be set only in the innermost label. */ | |
3433 | for (i = 0; i < n - 1; i++) { | |
3434 | if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) { | |
3435 | return ODP_FIT_ERROR; | |
3436 | } | |
8bfd0fda | 3437 | } |
8bfd0fda | 3438 | |
7e6621e9 JS |
3439 | /* BOS must be set in the innermost label. */ |
3440 | if (n < FLOW_MAX_MPLS_LABELS | |
3441 | && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) { | |
3442 | return ODP_FIT_TOO_LITTLE; | |
3443 | } | |
8bfd0fda BP |
3444 | } |
3445 | } | |
3446 | ||
4a221615 GY |
3447 | goto done; |
3448 | } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) { | |
3449 | if (!is_mask) { | |
3450 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4; | |
b02475c5 | 3451 | } |
fea393b1 | 3452 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { |
34118cae | 3453 | const struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 3454 | |
34118cae | 3455 | ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]); |
3cea18ec JR |
3456 | put_ipv4_key(ipv4_key, flow, is_mask); |
3457 | if (flow->nw_frag > FLOW_NW_FRAG_MASK) { | |
3458 | return ODP_FIT_ERROR; | |
3459 | } | |
4a221615 | 3460 | if (is_mask) { |
4a221615 GY |
3461 | check_start = ipv4_key; |
3462 | check_len = sizeof *ipv4_key; | |
3463 | expected_bit = OVS_KEY_ATTR_IPV4; | |
36956a7d | 3464 | } |
34118cae | 3465 | } |
4a221615 GY |
3466 | } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) { |
3467 | if (!is_mask) { | |
3468 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6; | |
3469 | } | |
fea393b1 | 3470 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { |
34118cae | 3471 | const struct ovs_key_ipv6 *ipv6_key; |
36956a7d | 3472 | |
34118cae | 3473 | ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]); |
3cea18ec JR |
3474 | put_ipv6_key(ipv6_key, flow, is_mask); |
3475 | if (flow->nw_frag > FLOW_NW_FRAG_MASK) { | |
3476 | return ODP_FIT_ERROR; | |
3477 | } | |
4a221615 | 3478 | if (is_mask) { |
4a221615 GY |
3479 | check_start = ipv6_key; |
3480 | check_len = sizeof *ipv6_key; | |
3481 | expected_bit = OVS_KEY_ATTR_IPV6; | |
d31f1109 | 3482 | } |
34118cae | 3483 | } |
4a221615 GY |
3484 | } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) || |
3485 | src_flow->dl_type == htons(ETH_TYPE_RARP)) { | |
3486 | if (!is_mask) { | |
3487 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP; | |
3488 | } | |
fea393b1 | 3489 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) { |
34118cae | 3490 | const struct ovs_key_arp *arp_key; |
d31f1109 | 3491 | |
34118cae | 3492 | arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]); |
4a221615 | 3493 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
34118cae BP |
3494 | VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow " |
3495 | "key", ntohs(arp_key->arp_op)); | |
b0f7b9b5 | 3496 | return ODP_FIT_ERROR; |
36956a7d | 3497 | } |
3cea18ec | 3498 | put_arp_key(arp_key, flow); |
4a221615 GY |
3499 | if (is_mask) { |
3500 | check_start = arp_key; | |
3501 | check_len = sizeof *arp_key; | |
3502 | expected_bit = OVS_KEY_ATTR_ARP; | |
3503 | } | |
3504 | } | |
3505 | } else { | |
3506 | goto done; | |
3507 | } | |
df4eeb20 | 3508 | if (check_len > 0) { /* Happens only when 'is_mask'. */ |
4a221615 GY |
3509 | if (!is_all_zeros(check_start, check_len) && |
3510 | flow->dl_type != htons(0xffff)) { | |
3511 | return ODP_FIT_ERROR; | |
3512 | } else { | |
3513 | expected_attrs |= UINT64_C(1) << expected_bit; | |
36956a7d | 3514 | } |
36956a7d | 3515 | } |
36956a7d | 3516 | |
4a221615 GY |
3517 | expected_bit = OVS_KEY_ATTR_UNSPEC; |
3518 | if (src_flow->nw_proto == IPPROTO_TCP | |
3519 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
3520 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
3521 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
3522 | if (!is_mask) { | |
3523 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP; | |
3524 | } | |
fea393b1 | 3525 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) { |
3cea18ec | 3526 | const union ovs_key_tp *tcp_key; |
36956a7d | 3527 | |
34118cae | 3528 | tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]); |
3cea18ec | 3529 | put_tp_key(tcp_key, flow); |
4a221615 GY |
3530 | expected_bit = OVS_KEY_ATTR_TCP; |
3531 | } | |
dc235f7f JR |
3532 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) { |
3533 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS; | |
3534 | flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]); | |
3535 | } | |
4a221615 GY |
3536 | } else if (src_flow->nw_proto == IPPROTO_UDP |
3537 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
3538 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
3539 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
3540 | if (!is_mask) { | |
3541 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP; | |
7257b535 | 3542 | } |
fea393b1 | 3543 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) { |
3cea18ec | 3544 | const union ovs_key_tp *udp_key; |
34118cae BP |
3545 | |
3546 | udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]); | |
3cea18ec | 3547 | put_tp_key(udp_key, flow); |
4a221615 GY |
3548 | expected_bit = OVS_KEY_ATTR_UDP; |
3549 | } | |
12848ebf GS |
3550 | } else if (src_flow->nw_proto == IPPROTO_SCTP |
3551 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
3552 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
3553 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
c6bcb685 JS |
3554 | if (!is_mask) { |
3555 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP; | |
3556 | } | |
3557 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) { | |
3cea18ec | 3558 | const union ovs_key_tp *sctp_key; |
c6bcb685 JS |
3559 | |
3560 | sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]); | |
3cea18ec | 3561 | put_tp_key(sctp_key, flow); |
c6bcb685 JS |
3562 | expected_bit = OVS_KEY_ATTR_SCTP; |
3563 | } | |
4a221615 GY |
3564 | } else if (src_flow->nw_proto == IPPROTO_ICMP |
3565 | && src_flow->dl_type == htons(ETH_TYPE_IP) | |
3566 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
3567 | if (!is_mask) { | |
3568 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP; | |
d31f1109 | 3569 | } |
fea393b1 | 3570 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) { |
34118cae BP |
3571 | const struct ovs_key_icmp *icmp_key; |
3572 | ||
3573 | icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]); | |
3574 | flow->tp_src = htons(icmp_key->icmp_type); | |
3575 | flow->tp_dst = htons(icmp_key->icmp_code); | |
4a221615 GY |
3576 | expected_bit = OVS_KEY_ATTR_ICMP; |
3577 | } | |
3578 | } else if (src_flow->nw_proto == IPPROTO_ICMPV6 | |
3579 | && src_flow->dl_type == htons(ETH_TYPE_IPV6) | |
3580 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
3581 | if (!is_mask) { | |
3582 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6; | |
685a51a5 | 3583 | } |
fea393b1 | 3584 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) { |
34118cae BP |
3585 | const struct ovs_key_icmpv6 *icmpv6_key; |
3586 | ||
3587 | icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]); | |
3588 | flow->tp_src = htons(icmpv6_key->icmpv6_type); | |
3589 | flow->tp_dst = htons(icmpv6_key->icmpv6_code); | |
4a221615 | 3590 | expected_bit = OVS_KEY_ATTR_ICMPV6; |
6f8dbd27 GY |
3591 | if (src_flow->tp_dst == htons(0) && |
3592 | (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
3593 | src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) { | |
4a221615 GY |
3594 | if (!is_mask) { |
3595 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
3596 | } | |
fea393b1 | 3597 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) { |
34118cae BP |
3598 | const struct ovs_key_nd *nd_key; |
3599 | ||
3600 | nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]); | |
3601 | memcpy(&flow->nd_target, nd_key->nd_target, | |
3602 | sizeof flow->nd_target); | |
3603 | memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN); | |
3604 | memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN); | |
4a221615 | 3605 | if (is_mask) { |
53cb9c3e | 3606 | if (!is_all_zeros(nd_key, sizeof *nd_key) && |
6f8dbd27 GY |
3607 | (flow->tp_src != htons(0xffff) || |
3608 | flow->tp_dst != htons(0xffff))) { | |
4a221615 GY |
3609 | return ODP_FIT_ERROR; |
3610 | } else { | |
3611 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
3612 | } | |
3613 | } | |
34118cae BP |
3614 | } |
3615 | } | |
7257b535 | 3616 | } |
34118cae | 3617 | } |
4a221615 GY |
3618 | if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) { |
3619 | if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) { | |
3620 | return ODP_FIT_ERROR; | |
3621 | } else { | |
3622 | expected_attrs |= UINT64_C(1) << expected_bit; | |
3623 | } | |
3624 | } | |
7257b535 | 3625 | |
4a221615 | 3626 | done: |
b0f7b9b5 BP |
3627 | return check_expectations(present_attrs, out_of_range_attr, expected_attrs, |
3628 | key, key_len); | |
3629 | } | |
3630 | ||
3631 | /* Parse 802.1Q header then encapsulated L3 attributes. */ | |
3632 | static enum odp_key_fitness | |
3633 | parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
3634 | uint64_t present_attrs, int out_of_range_attr, | |
3635 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 GY |
3636 | const struct nlattr *key, size_t key_len, |
3637 | const struct flow *src_flow) | |
b0f7b9b5 BP |
3638 | { |
3639 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 3640 | bool is_mask = src_flow != flow; |
b0f7b9b5 BP |
3641 | |
3642 | const struct nlattr *encap | |
3643 | = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP) | |
3644 | ? attrs[OVS_KEY_ATTR_ENCAP] : NULL); | |
3645 | enum odp_key_fitness encap_fitness; | |
3646 | enum odp_key_fitness fitness; | |
b0f7b9b5 | 3647 | |
ec9f40dc | 3648 | /* Calculate fitness of outer attributes. */ |
4a221615 GY |
3649 | if (!is_mask) { |
3650 | expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) | | |
3651 | (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)); | |
3652 | } else { | |
3653 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
3654 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
3655 | } | |
3656 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) { | |
3657 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP); | |
3658 | } | |
3659 | } | |
b0f7b9b5 BP |
3660 | fitness = check_expectations(present_attrs, out_of_range_attr, |
3661 | expected_attrs, key, key_len); | |
3662 | ||
3e634810 BP |
3663 | /* Set vlan_tci. |
3664 | * Remove the TPID from dl_type since it's not the real Ethertype. */ | |
3665 | flow->dl_type = htons(0); | |
3666 | flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN) | |
3667 | ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]) | |
3668 | : htons(0)); | |
3669 | if (!is_mask) { | |
3670 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) { | |
3671 | return ODP_FIT_TOO_LITTLE; | |
3672 | } else if (flow->vlan_tci == htons(0)) { | |
3673 | /* Corner case for a truncated 802.1Q header. */ | |
3674 | if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { | |
3675 | return ODP_FIT_TOO_MUCH; | |
3676 | } | |
3677 | return fitness; | |
3678 | } else if (!(flow->vlan_tci & htons(VLAN_CFI))) { | |
3679 | VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " | |
3680 | "but CFI bit is not set", ntohs(flow->vlan_tci)); | |
3681 | return ODP_FIT_ERROR; | |
3682 | } | |
4a221615 | 3683 | } else { |
3e634810 BP |
3684 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { |
3685 | return fitness; | |
4a221615 | 3686 | } |
4a221615 GY |
3687 | } |
3688 | ||
b0f7b9b5 BP |
3689 | /* Now parse the encapsulated attributes. */ |
3690 | if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap), | |
3691 | attrs, &present_attrs, &out_of_range_attr)) { | |
3692 | return ODP_FIT_ERROR; | |
3693 | } | |
3694 | expected_attrs = 0; | |
3695 | ||
4a221615 | 3696 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) { |
b0f7b9b5 BP |
3697 | return ODP_FIT_ERROR; |
3698 | } | |
b02475c5 | 3699 | encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
3700 | expected_attrs, flow, key, key_len, |
3701 | src_flow); | |
b0f7b9b5 BP |
3702 | |
3703 | /* The overall fitness is the worse of the outer and inner attributes. */ | |
3704 | return MAX(fitness, encap_fitness); | |
3705 | } | |
3706 | ||
4a221615 GY |
3707 | static enum odp_key_fitness |
3708 | odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len, | |
3709 | struct flow *flow, const struct flow *src_flow) | |
b0f7b9b5 | 3710 | { |
b0f7b9b5 BP |
3711 | const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1]; |
3712 | uint64_t expected_attrs; | |
3713 | uint64_t present_attrs; | |
3714 | int out_of_range_attr; | |
4a221615 | 3715 | bool is_mask = src_flow != flow; |
b0f7b9b5 BP |
3716 | |
3717 | memset(flow, 0, sizeof *flow); | |
3718 | ||
3719 | /* Parse attributes. */ | |
3720 | if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs, | |
3721 | &out_of_range_attr)) { | |
3722 | return ODP_FIT_ERROR; | |
3723 | } | |
3724 | expected_attrs = 0; | |
3725 | ||
3726 | /* Metadata. */ | |
572f732a AZ |
3727 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) { |
3728 | flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]); | |
3729 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID; | |
3730 | } else if (is_mask) { | |
8c1b077f | 3731 | /* Always exact match recirc_id if it is not specified. */ |
572f732a AZ |
3732 | flow->recirc_id = UINT32_MAX; |
3733 | } | |
3734 | ||
3735 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) { | |
3736 | flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]); | |
3737 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH; | |
3738 | } | |
b0f7b9b5 | 3739 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) { |
deedf7e7 | 3740 | flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]); |
b0f7b9b5 BP |
3741 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY; |
3742 | } | |
3743 | ||
72e8bf28 | 3744 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) { |
1362e248 | 3745 | flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]); |
72e8bf28 AA |
3746 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK; |
3747 | } | |
3748 | ||
9b405f1a PS |
3749 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) { |
3750 | enum odp_key_fitness res; | |
05fb0928 | 3751 | |
617e10e7 | 3752 | res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel); |
9b405f1a PS |
3753 | if (res == ODP_FIT_ERROR) { |
3754 | return ODP_FIT_ERROR; | |
3755 | } else if (res == ODP_FIT_PERFECT) { | |
3756 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL; | |
05fb0928 JR |
3757 | } |
3758 | } | |
3759 | ||
b0f7b9b5 | 3760 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) { |
4e022ec0 AW |
3761 | flow->in_port.odp_port |
3762 | = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]); | |
b0f7b9b5 | 3763 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT; |
4a221615 | 3764 | } else if (!is_mask) { |
4e022ec0 | 3765 | flow->in_port.odp_port = ODPP_NONE; |
b0f7b9b5 BP |
3766 | } |
3767 | ||
3768 | /* Ethernet header. */ | |
3769 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) { | |
3770 | const struct ovs_key_ethernet *eth_key; | |
3771 | ||
3772 | eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]); | |
3cea18ec | 3773 | put_ethernet_key(eth_key, flow); |
4a221615 GY |
3774 | if (is_mask) { |
3775 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; | |
3776 | } | |
3777 | } | |
3778 | if (!is_mask) { | |
3779 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; | |
b0f7b9b5 | 3780 | } |
b0f7b9b5 BP |
3781 | |
3782 | /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */ | |
4a221615 GY |
3783 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, |
3784 | src_flow)) { | |
b0f7b9b5 BP |
3785 | return ODP_FIT_ERROR; |
3786 | } | |
3787 | ||
21e70add BP |
3788 | if (is_mask |
3789 | ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0 | |
3790 | : src_flow->dl_type == htons(ETH_TYPE_VLAN)) { | |
b0f7b9b5 | 3791 | return parse_8021q_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
3792 | expected_attrs, flow, key, key_len, src_flow); |
3793 | } | |
3794 | if (is_mask) { | |
3795 | flow->vlan_tci = htons(0xffff); | |
3796 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
3797 | flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]); | |
3798 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
3799 | } | |
b0f7b9b5 | 3800 | } |
b02475c5 | 3801 | return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
3802 | expected_attrs, flow, key, key_len, src_flow); |
3803 | } | |
3804 | ||
3805 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow | |
3806 | * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well | |
3807 | * 'key' fits our expectations for what a flow key should contain. | |
3808 | * | |
3809 | * The 'in_port' will be the datapath's understanding of the port. The | |
3810 | * caller will need to translate with odp_port_to_ofp_port() if the | |
3811 | * OpenFlow port is needed. | |
3812 | * | |
3813 | * This function doesn't take the packet itself as an argument because none of | |
3814 | * the currently understood OVS_KEY_ATTR_* attributes require it. Currently, | |
3815 | * it is always possible to infer which additional attribute(s) should appear | |
3816 | * by looking at the attributes for lower-level protocols, e.g. if the network | |
3817 | * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we | |
3818 | * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it | |
3819 | * must be absent. */ | |
3820 | enum odp_key_fitness | |
3821 | odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, | |
3822 | struct flow *flow) | |
3823 | { | |
3824 | return odp_flow_key_to_flow__(key, key_len, flow, flow); | |
3825 | } | |
3826 | ||
3827 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask | |
3828 | * structure in 'mask'. 'flow' must be a previously translated flow | |
3829 | * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well | |
3830 | * 'key' fits our expectations for what a flow key should contain. */ | |
3831 | enum odp_key_fitness | |
3832 | odp_flow_key_to_mask(const struct nlattr *key, size_t key_len, | |
3833 | struct flow *mask, const struct flow *flow) | |
3834 | { | |
3835 | return odp_flow_key_to_flow__(key, key_len, mask, flow); | |
14608a15 | 3836 | } |
39db78a0 | 3837 | |
6814e51f BP |
3838 | /* Returns 'fitness' as a string, for use in debug messages. */ |
3839 | const char * | |
3840 | odp_key_fitness_to_string(enum odp_key_fitness fitness) | |
3841 | { | |
3842 | switch (fitness) { | |
3843 | case ODP_FIT_PERFECT: | |
3844 | return "OK"; | |
3845 | case ODP_FIT_TOO_MUCH: | |
3846 | return "too_much"; | |
3847 | case ODP_FIT_TOO_LITTLE: | |
3848 | return "too_little"; | |
3849 | case ODP_FIT_ERROR: | |
3850 | return "error"; | |
3851 | default: | |
3852 | return "<unknown>"; | |
3853 | } | |
3854 | } | |
3855 | ||
39db78a0 | 3856 | /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies |
e995e3df BP |
3857 | * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute |
3858 | * whose contents are the 'userdata_size' bytes at 'userdata' and returns the | |
3859 | * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is | |
3860 | * null, then the return value is not meaningful.) */ | |
39db78a0 | 3861 | size_t |
e995e3df BP |
3862 | odp_put_userspace_action(uint32_t pid, |
3863 | const void *userdata, size_t userdata_size, | |
8b7ea2d4 | 3864 | odp_port_t tunnel_out_port, |
39db78a0 BP |
3865 | struct ofpbuf *odp_actions) |
3866 | { | |
e995e3df | 3867 | size_t userdata_ofs; |
39db78a0 BP |
3868 | size_t offset; |
3869 | ||
3870 | offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); | |
3871 | nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); | |
e995e3df | 3872 | if (userdata) { |
6fd6ed71 | 3873 | userdata_ofs = odp_actions->size + NLA_HDRLEN; |
96ed775f BP |
3874 | |
3875 | /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel | |
3876 | * module before Linux 3.10 required the userdata to be exactly 8 bytes | |
3877 | * long: | |
3878 | * | |
3879 | * - The kernel rejected shorter userdata with -ERANGE. | |
3880 | * | |
3881 | * - The kernel silently dropped userdata beyond the first 8 bytes. | |
3882 | * | |
3883 | * Thus, for maximum compatibility, always put at least 8 bytes. (We | |
3884 | * separately disable features that required more than 8 bytes.) */ | |
3885 | memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA, | |
3886 | MAX(8, userdata_size)), | |
3887 | userdata, userdata_size); | |
e995e3df BP |
3888 | } else { |
3889 | userdata_ofs = 0; | |
39db78a0 | 3890 | } |
8b7ea2d4 WZ |
3891 | if (tunnel_out_port != ODPP_NONE) { |
3892 | nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT, | |
3893 | tunnel_out_port); | |
3894 | } | |
39db78a0 BP |
3895 | nl_msg_end_nested(odp_actions, offset); |
3896 | ||
e995e3df | 3897 | return userdata_ofs; |
39db78a0 | 3898 | } |
b9ad7294 EJ |
3899 | |
3900 | void | |
3901 | odp_put_tunnel_action(const struct flow_tnl *tunnel, | |
3902 | struct ofpbuf *odp_actions) | |
3903 | { | |
3904 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
3905 | tun_key_to_attr(odp_actions, tunnel); | |
3906 | nl_msg_end_nested(odp_actions, offset); | |
3907 | } | |
a36de779 PS |
3908 | |
3909 | void | |
3910 | odp_put_tnl_push_action(struct ofpbuf *odp_actions, | |
3911 | struct ovs_action_push_tnl *data) | |
3912 | { | |
3913 | int size = offsetof(struct ovs_action_push_tnl, header); | |
3914 | ||
3915 | size += data->header_len; | |
3916 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size); | |
3917 | } | |
3918 | ||
5bbda0aa EJ |
3919 | \f |
3920 | /* The commit_odp_actions() function and its helpers. */ | |
3921 | ||
3922 | static void | |
3923 | commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type, | |
3924 | const void *key, size_t key_size) | |
3925 | { | |
3926 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
3927 | nl_msg_put_unspec(odp_actions, key_type, key, key_size); | |
3928 | nl_msg_end_nested(odp_actions, offset); | |
3929 | } | |
3930 | ||
6d670e7f JR |
3931 | /* Masked set actions have a mask following the data within the netlink |
3932 | * attribute. The unmasked bits in the data will be cleared as the data | |
3933 | * is copied to the action. */ | |
3934 | void | |
3935 | commit_masked_set_action(struct ofpbuf *odp_actions, | |
3936 | enum ovs_key_attr key_type, | |
3937 | const void *key_, const void *mask_, size_t key_size) | |
3938 | { | |
3939 | size_t offset = nl_msg_start_nested(odp_actions, | |
3940 | OVS_ACTION_ATTR_SET_MASKED); | |
3941 | char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2); | |
3942 | const char *key = key_, *mask = mask_; | |
3943 | ||
3944 | memcpy(data + key_size, mask, key_size); | |
3945 | /* Clear unmasked bits while copying. */ | |
3946 | while (key_size--) { | |
3947 | *data++ = *key++ & *mask++; | |
3948 | } | |
3949 | nl_msg_end_nested(odp_actions, offset); | |
3950 | } | |
3951 | ||
b9ad7294 EJ |
3952 | /* If any of the flow key data that ODP actions can modify are different in |
3953 | * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to | |
3954 | * 'odp_actions' that change the flow tunneling information in key from | |
3955 | * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the | |
3956 | * same way. In other words, operates the same as commit_odp_actions(), but | |
3957 | * only on tunneling information. */ | |
3958 | void | |
3959 | commit_odp_tunnel_action(const struct flow *flow, struct flow *base, | |
5bbda0aa EJ |
3960 | struct ofpbuf *odp_actions) |
3961 | { | |
05fb0928 JR |
3962 | /* A valid IPV4_TUNNEL must have non-zero ip_dst. */ |
3963 | if (flow->tunnel.ip_dst) { | |
fc80de30 JR |
3964 | if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) { |
3965 | return; | |
3966 | } | |
3967 | memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel); | |
b9ad7294 | 3968 | odp_put_tunnel_action(&base->tunnel, odp_actions); |
05fb0928 | 3969 | } |
5bbda0aa EJ |
3970 | } |
3971 | ||
d23df9a8 JR |
3972 | static bool |
3973 | commit(enum ovs_key_attr attr, bool use_masked_set, | |
3974 | const void *key, void *base, void *mask, size_t size, | |
3975 | struct ofpbuf *odp_actions) | |
5bbda0aa | 3976 | { |
d23df9a8 JR |
3977 | if (memcmp(key, base, size)) { |
3978 | bool fully_masked = odp_mask_is_exact(attr, mask, size); | |
5bbda0aa | 3979 | |
d23df9a8 JR |
3980 | if (use_masked_set && !fully_masked) { |
3981 | commit_masked_set_action(odp_actions, attr, key, mask, size); | |
3982 | } else { | |
3983 | if (!fully_masked) { | |
3984 | memset(mask, 0xff, size); | |
3985 | } | |
3986 | commit_set_action(odp_actions, attr, key, size); | |
3987 | } | |
3988 | memcpy(base, key, size); | |
3989 | return true; | |
3990 | } else { | |
3991 | /* Mask bits are set when we have either read or set the corresponding | |
3992 | * values. Masked bits will be exact-matched, no need to set them | |
3993 | * if the value did not actually change. */ | |
3994 | return false; | |
5bbda0aa | 3995 | } |
d23df9a8 | 3996 | } |
5bbda0aa | 3997 | |
d23df9a8 JR |
3998 | static void |
3999 | get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth) | |
4000 | { | |
4001 | memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN); | |
4002 | memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN); | |
4003 | } | |
1dd35f8a | 4004 | |
d23df9a8 JR |
4005 | static void |
4006 | put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow) | |
4007 | { | |
4008 | memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN); | |
4009 | memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN); | |
4010 | } | |
5bbda0aa | 4011 | |
d23df9a8 JR |
4012 | static void |
4013 | commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow, | |
4014 | struct ofpbuf *odp_actions, | |
4015 | struct flow_wildcards *wc, | |
4016 | bool use_masked) | |
4017 | { | |
4018 | struct ovs_key_ethernet key, base, mask; | |
5bbda0aa | 4019 | |
d23df9a8 JR |
4020 | get_ethernet_key(flow, &key); |
4021 | get_ethernet_key(base_flow, &base); | |
4022 | get_ethernet_key(&wc->masks, &mask); | |
4023 | ||
4024 | if (commit(OVS_KEY_ATTR_ETHERNET, use_masked, | |
4025 | &key, &base, &mask, sizeof key, odp_actions)) { | |
4026 | put_ethernet_key(&base, base_flow); | |
4027 | put_ethernet_key(&mask, &wc->masks); | |
4028 | } | |
5bbda0aa EJ |
4029 | } |
4030 | ||
4031 | static void | |
8bfd0fda BP |
4032 | pop_vlan(struct flow *base, |
4033 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
5bbda0aa | 4034 | { |
1dd35f8a JP |
4035 | memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci); |
4036 | ||
5bbda0aa EJ |
4037 | if (base->vlan_tci & htons(VLAN_CFI)) { |
4038 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); | |
8bfd0fda BP |
4039 | base->vlan_tci = 0; |
4040 | } | |
4041 | } | |
4042 | ||
4043 | static void | |
4044 | commit_vlan_action(ovs_be16 vlan_tci, struct flow *base, | |
4045 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
4046 | { | |
4047 | if (base->vlan_tci == vlan_tci) { | |
4048 | return; | |
5bbda0aa EJ |
4049 | } |
4050 | ||
8bfd0fda | 4051 | pop_vlan(base, odp_actions, wc); |
8fd16af2 | 4052 | if (vlan_tci & htons(VLAN_CFI)) { |
5bbda0aa EJ |
4053 | struct ovs_action_push_vlan vlan; |
4054 | ||
4055 | vlan.vlan_tpid = htons(ETH_TYPE_VLAN); | |
8fd16af2 | 4056 | vlan.vlan_tci = vlan_tci; |
5bbda0aa EJ |
4057 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, |
4058 | &vlan, sizeof vlan); | |
4059 | } | |
8fd16af2 | 4060 | base->vlan_tci = vlan_tci; |
5bbda0aa EJ |
4061 | } |
4062 | ||
22d38fca | 4063 | /* Wildcarding already done at action translation time. */ |
b02475c5 SH |
4064 | static void |
4065 | commit_mpls_action(const struct flow *flow, struct flow *base, | |
22d38fca | 4066 | struct ofpbuf *odp_actions) |
b02475c5 | 4067 | { |
22d38fca JR |
4068 | int base_n = flow_count_mpls_labels(base, NULL); |
4069 | int flow_n = flow_count_mpls_labels(flow, NULL); | |
8bfd0fda | 4070 | int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n, |
22d38fca | 4071 | NULL); |
8bfd0fda BP |
4072 | |
4073 | while (base_n > common_n) { | |
4074 | if (base_n - 1 == common_n && flow_n > common_n) { | |
4075 | /* If there is only one more LSE in base than there are common | |
4076 | * between base and flow; and flow has at least one more LSE than | |
4077 | * is common then the topmost LSE of base may be updated using | |
4078 | * set */ | |
4079 | struct ovs_key_mpls mpls_key; | |
4080 | ||
4081 | mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n]; | |
4082 | commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, | |
4083 | &mpls_key, sizeof mpls_key); | |
4084 | flow_set_mpls_lse(base, 0, mpls_key.mpls_lse); | |
4085 | common_n++; | |
4086 | } else { | |
4087 | /* Otherwise, if there more LSEs in base than are common between | |
4088 | * base and flow then pop the topmost one. */ | |
4089 | ovs_be16 dl_type; | |
4090 | bool popped; | |
4091 | ||
4092 | /* If all the LSEs are to be popped and this is not the outermost | |
4093 | * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the | |
4094 | * POP_MPLS action instead of flow->dl_type. | |
4095 | * | |
4096 | * This is because the POP_MPLS action requires its ethertype | |
4097 | * argument to be an MPLS ethernet type but in this case | |
4098 | * flow->dl_type will be a non-MPLS ethernet type. | |
4099 | * | |
4100 | * When the final POP_MPLS action occurs it use flow->dl_type and | |
4101 | * the and the resulting packet will have the desired dl_type. */ | |
4102 | if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) { | |
4103 | dl_type = htons(ETH_TYPE_MPLS); | |
4104 | } else { | |
4105 | dl_type = flow->dl_type; | |
4106 | } | |
4107 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type); | |
22d38fca | 4108 | popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL); |
8bfd0fda BP |
4109 | ovs_assert(popped); |
4110 | base_n--; | |
4111 | } | |
b02475c5 SH |
4112 | } |
4113 | ||
8bfd0fda BP |
4114 | /* If, after the above popping and setting, there are more LSEs in flow |
4115 | * than base then some LSEs need to be pushed. */ | |
4116 | while (base_n < flow_n) { | |
b02475c5 SH |
4117 | struct ovs_action_push_mpls *mpls; |
4118 | ||
8bfd0fda BP |
4119 | mpls = nl_msg_put_unspec_zero(odp_actions, |
4120 | OVS_ACTION_ATTR_PUSH_MPLS, | |
9ddf12cc | 4121 | sizeof *mpls); |
b02475c5 | 4122 | mpls->mpls_ethertype = flow->dl_type; |
8bfd0fda | 4123 | mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1]; |
22d38fca | 4124 | flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL); |
8bfd0fda BP |
4125 | flow_set_mpls_lse(base, 0, mpls->mpls_lse); |
4126 | base_n++; | |
b0a17866 | 4127 | } |
b02475c5 SH |
4128 | } |
4129 | ||
5bbda0aa | 4130 | static void |
3cea18ec | 4131 | get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask) |
5bbda0aa | 4132 | { |
d23df9a8 JR |
4133 | ipv4->ipv4_src = flow->nw_src; |
4134 | ipv4->ipv4_dst = flow->nw_dst; | |
4135 | ipv4->ipv4_proto = flow->nw_proto; | |
4136 | ipv4->ipv4_tos = flow->nw_tos; | |
4137 | ipv4->ipv4_ttl = flow->nw_ttl; | |
3cea18ec | 4138 | ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask); |
d23df9a8 | 4139 | } |
5bbda0aa | 4140 | |
d23df9a8 | 4141 | static void |
3cea18ec | 4142 | put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask) |
d23df9a8 JR |
4143 | { |
4144 | flow->nw_src = ipv4->ipv4_src; | |
4145 | flow->nw_dst = ipv4->ipv4_dst; | |
4146 | flow->nw_proto = ipv4->ipv4_proto; | |
4147 | flow->nw_tos = ipv4->ipv4_tos; | |
4148 | flow->nw_ttl = ipv4->ipv4_ttl; | |
3cea18ec | 4149 | flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask); |
d23df9a8 JR |
4150 | } |
4151 | ||
4152 | static void | |
4153 | commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow, | |
4154 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
4155 | bool use_masked) | |
4156 | { | |
4157 | struct ovs_key_ipv4 key, mask, base; | |
5bbda0aa | 4158 | |
d23df9a8 JR |
4159 | /* Check that nw_proto and nw_frag remain unchanged. */ |
4160 | ovs_assert(flow->nw_proto == base_flow->nw_proto && | |
4161 | flow->nw_frag == base_flow->nw_frag); | |
1dd35f8a | 4162 | |
3cea18ec JR |
4163 | get_ipv4_key(flow, &key, false); |
4164 | get_ipv4_key(base_flow, &base, false); | |
4165 | get_ipv4_key(&wc->masks, &mask, true); | |
d23df9a8 JR |
4166 | mask.ipv4_proto = 0; /* Not writeable. */ |
4167 | mask.ipv4_frag = 0; /* Not writable. */ | |
5bbda0aa | 4168 | |
d23df9a8 JR |
4169 | if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key, |
4170 | odp_actions)) { | |
3cea18ec | 4171 | put_ipv4_key(&base, base_flow, false); |
d23df9a8 | 4172 | if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */ |
3cea18ec | 4173 | put_ipv4_key(&mask, &wc->masks, true); |
d23df9a8 JR |
4174 | } |
4175 | } | |
5bbda0aa EJ |
4176 | } |
4177 | ||
c4f2731d | 4178 | static void |
3cea18ec | 4179 | get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask) |
c4f2731d | 4180 | { |
d23df9a8 JR |
4181 | memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src); |
4182 | memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst); | |
4183 | ipv6->ipv6_label = flow->ipv6_label; | |
4184 | ipv6->ipv6_proto = flow->nw_proto; | |
4185 | ipv6->ipv6_tclass = flow->nw_tos; | |
4186 | ipv6->ipv6_hlimit = flow->nw_ttl; | |
3cea18ec | 4187 | ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask); |
d23df9a8 | 4188 | } |
c4f2731d | 4189 | |
d23df9a8 | 4190 | static void |
3cea18ec | 4191 | put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask) |
d23df9a8 JR |
4192 | { |
4193 | memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src); | |
4194 | memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst); | |
4195 | flow->ipv6_label = ipv6->ipv6_label; | |
4196 | flow->nw_proto = ipv6->ipv6_proto; | |
4197 | flow->nw_tos = ipv6->ipv6_tclass; | |
4198 | flow->nw_ttl = ipv6->ipv6_hlimit; | |
3cea18ec | 4199 | flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask); |
d23df9a8 | 4200 | } |
c4f2731d | 4201 | |
d23df9a8 JR |
4202 | static void |
4203 | commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow, | |
4204 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
4205 | bool use_masked) | |
4206 | { | |
4207 | struct ovs_key_ipv6 key, mask, base; | |
1dd35f8a | 4208 | |
d23df9a8 JR |
4209 | /* Check that nw_proto and nw_frag remain unchanged. */ |
4210 | ovs_assert(flow->nw_proto == base_flow->nw_proto && | |
4211 | flow->nw_frag == base_flow->nw_frag); | |
c4f2731d | 4212 | |
3cea18ec JR |
4213 | get_ipv6_key(flow, &key, false); |
4214 | get_ipv6_key(base_flow, &base, false); | |
4215 | get_ipv6_key(&wc->masks, &mask, true); | |
d23df9a8 JR |
4216 | mask.ipv6_proto = 0; /* Not writeable. */ |
4217 | mask.ipv6_frag = 0; /* Not writable. */ | |
c4f2731d | 4218 | |
d23df9a8 JR |
4219 | if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key, |
4220 | odp_actions)) { | |
3cea18ec | 4221 | put_ipv6_key(&base, base_flow, false); |
d23df9a8 | 4222 | if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */ |
3cea18ec | 4223 | put_ipv6_key(&mask, &wc->masks, true); |
d23df9a8 JR |
4224 | } |
4225 | } | |
c4f2731d PS |
4226 | } |
4227 | ||
d23df9a8 JR |
4228 | static void |
4229 | get_arp_key(const struct flow *flow, struct ovs_key_arp *arp) | |
f6c8a6b1 | 4230 | { |
d23df9a8 JR |
4231 | /* ARP key has padding, clear it. */ |
4232 | memset(arp, 0, sizeof *arp); | |
f6c8a6b1 | 4233 | |
d23df9a8 JR |
4234 | arp->arp_sip = flow->nw_src; |
4235 | arp->arp_tip = flow->nw_dst; | |
4236 | arp->arp_op = htons(flow->nw_proto); | |
4237 | memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN); | |
4238 | memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN); | |
4239 | } | |
f6c8a6b1 | 4240 | |
d23df9a8 JR |
4241 | static void |
4242 | put_arp_key(const struct ovs_key_arp *arp, struct flow *flow) | |
4243 | { | |
4244 | flow->nw_src = arp->arp_sip; | |
4245 | flow->nw_dst = arp->arp_tip; | |
4246 | flow->nw_proto = ntohs(arp->arp_op); | |
4247 | memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN); | |
4248 | memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN); | |
4249 | } | |
f6c8a6b1 | 4250 | |
d23df9a8 JR |
4251 | static enum slow_path_reason |
4252 | commit_set_arp_action(const struct flow *flow, struct flow *base_flow, | |
4253 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
4254 | { | |
4255 | struct ovs_key_arp key, mask, base; | |
f6c8a6b1 | 4256 | |
d23df9a8 JR |
4257 | get_arp_key(flow, &key); |
4258 | get_arp_key(base_flow, &base); | |
4259 | get_arp_key(&wc->masks, &mask); | |
f6c8a6b1 | 4260 | |
d23df9a8 JR |
4261 | if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key, |
4262 | odp_actions)) { | |
4263 | put_arp_key(&base, base_flow); | |
4264 | put_arp_key(&mask, &wc->masks); | |
4265 | return SLOW_ACTION; | |
4266 | } | |
4267 | return 0; | |
f6c8a6b1 BP |
4268 | } |
4269 | ||
e60e935b SRCSA |
4270 | static void |
4271 | get_nd_key(const struct flow *flow, struct ovs_key_nd *nd) | |
4272 | { | |
4273 | memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target); | |
4274 | /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */ | |
4275 | memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN); | |
4276 | memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN); | |
4277 | } | |
4278 | ||
4279 | static void | |
4280 | put_nd_key(const struct ovs_key_nd *nd, struct flow *flow) | |
4281 | { | |
4282 | memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target); | |
4283 | /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */ | |
4284 | memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN); | |
4285 | memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN); | |
4286 | } | |
4287 | ||
4288 | static enum slow_path_reason | |
4289 | commit_set_nd_action(const struct flow *flow, struct flow *base_flow, | |
4290 | struct ofpbuf *odp_actions, | |
4291 | struct flow_wildcards *wc, bool use_masked) | |
4292 | { | |
4293 | struct ovs_key_nd key, mask, base; | |
4294 | ||
4295 | get_nd_key(flow, &key); | |
4296 | get_nd_key(base_flow, &base); | |
4297 | get_nd_key(&wc->masks, &mask); | |
4298 | ||
4299 | if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key, | |
4300 | odp_actions)) { | |
4301 | put_nd_key(&base, base_flow); | |
4302 | put_nd_key(&mask, &wc->masks); | |
4303 | return SLOW_ACTION; | |
4304 | } | |
4305 | ||
4306 | return 0; | |
4307 | } | |
4308 | ||
f6c8a6b1 | 4309 | static enum slow_path_reason |
c4f2731d | 4310 | commit_set_nw_action(const struct flow *flow, struct flow *base, |
d23df9a8 JR |
4311 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
4312 | bool use_masked) | |
c4f2731d | 4313 | { |
f6c8a6b1 | 4314 | /* Check if 'flow' really has an L3 header. */ |
c4f2731d | 4315 | if (!flow->nw_proto) { |
f6c8a6b1 | 4316 | return 0; |
c4f2731d PS |
4317 | } |
4318 | ||
f6c8a6b1 BP |
4319 | switch (ntohs(base->dl_type)) { |
4320 | case ETH_TYPE_IP: | |
d23df9a8 | 4321 | commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked); |
f6c8a6b1 BP |
4322 | break; |
4323 | ||
4324 | case ETH_TYPE_IPV6: | |
d23df9a8 | 4325 | commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked); |
e60e935b | 4326 | return commit_set_nd_action(flow, base, odp_actions, wc, use_masked); |
f6c8a6b1 BP |
4327 | |
4328 | case ETH_TYPE_ARP: | |
4329 | return commit_set_arp_action(flow, base, odp_actions, wc); | |
c4f2731d | 4330 | } |
f6c8a6b1 BP |
4331 | |
4332 | return 0; | |
c4f2731d PS |
4333 | } |
4334 | ||
d23df9a8 JR |
4335 | /* TCP, UDP, and SCTP keys have the same layout. */ |
4336 | BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) && | |
4337 | sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp)); | |
4338 | ||
5bbda0aa | 4339 | static void |
3cea18ec | 4340 | get_tp_key(const struct flow *flow, union ovs_key_tp *tp) |
5bbda0aa | 4341 | { |
3cea18ec JR |
4342 | tp->tcp.tcp_src = flow->tp_src; |
4343 | tp->tcp.tcp_dst = flow->tp_dst; | |
d23df9a8 JR |
4344 | } |
4345 | ||
4346 | static void | |
3cea18ec | 4347 | put_tp_key(const union ovs_key_tp *tp, struct flow *flow) |
d23df9a8 | 4348 | { |
3cea18ec JR |
4349 | flow->tp_src = tp->tcp.tcp_src; |
4350 | flow->tp_dst = tp->tcp.tcp_dst; | |
d23df9a8 JR |
4351 | } |
4352 | ||
4353 | static void | |
4354 | commit_set_port_action(const struct flow *flow, struct flow *base_flow, | |
4355 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
4356 | bool use_masked) | |
4357 | { | |
4358 | enum ovs_key_attr key_type; | |
3cea18ec | 4359 | union ovs_key_tp key, mask, base; |
d23df9a8 | 4360 | |
791a09be SH |
4361 | /* Check if 'flow' really has an L3 header. */ |
4362 | if (!flow->nw_proto) { | |
4363 | return; | |
4364 | } | |
4365 | ||
d23df9a8 | 4366 | if (!is_ip_any(base_flow)) { |
5bbda0aa EJ |
4367 | return; |
4368 | } | |
4369 | ||
5bbda0aa | 4370 | if (flow->nw_proto == IPPROTO_TCP) { |
d23df9a8 | 4371 | key_type = OVS_KEY_ATTR_TCP; |
5bbda0aa | 4372 | } else if (flow->nw_proto == IPPROTO_UDP) { |
d23df9a8 | 4373 | key_type = OVS_KEY_ATTR_UDP; |
c6bcb685 | 4374 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
d23df9a8 JR |
4375 | key_type = OVS_KEY_ATTR_SCTP; |
4376 | } else { | |
4377 | return; | |
4378 | } | |
c6bcb685 | 4379 | |
d23df9a8 JR |
4380 | get_tp_key(flow, &key); |
4381 | get_tp_key(base_flow, &base); | |
4382 | get_tp_key(&wc->masks, &mask); | |
c6bcb685 | 4383 | |
d23df9a8 JR |
4384 | if (commit(key_type, use_masked, &key, &base, &mask, sizeof key, |
4385 | odp_actions)) { | |
4386 | put_tp_key(&base, base_flow); | |
4387 | put_tp_key(&mask, &wc->masks); | |
5bbda0aa EJ |
4388 | } |
4389 | } | |
4390 | ||
4391 | static void | |
d23df9a8 | 4392 | commit_set_priority_action(const struct flow *flow, struct flow *base_flow, |
1dd35f8a | 4393 | struct ofpbuf *odp_actions, |
d23df9a8 JR |
4394 | struct flow_wildcards *wc, |
4395 | bool use_masked) | |
5bbda0aa | 4396 | { |
d23df9a8 | 4397 | uint32_t key, mask, base; |
1dd35f8a | 4398 | |
d23df9a8 JR |
4399 | key = flow->skb_priority; |
4400 | base = base_flow->skb_priority; | |
4401 | mask = wc->masks.skb_priority; | |
5bbda0aa | 4402 | |
d23df9a8 JR |
4403 | if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask, |
4404 | sizeof key, odp_actions)) { | |
4405 | base_flow->skb_priority = base; | |
4406 | wc->masks.skb_priority = mask; | |
4407 | } | |
5bbda0aa EJ |
4408 | } |
4409 | ||
72e8bf28 | 4410 | static void |
d23df9a8 | 4411 | commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow, |
1dd35f8a | 4412 | struct ofpbuf *odp_actions, |
d23df9a8 JR |
4413 | struct flow_wildcards *wc, |
4414 | bool use_masked) | |
72e8bf28 | 4415 | { |
d23df9a8 | 4416 | uint32_t key, mask, base; |
1dd35f8a | 4417 | |
d23df9a8 JR |
4418 | key = flow->pkt_mark; |
4419 | base = base_flow->pkt_mark; | |
4420 | mask = wc->masks.pkt_mark; | |
72e8bf28 | 4421 | |
d23df9a8 JR |
4422 | if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask, |
4423 | sizeof key, odp_actions)) { | |
4424 | base_flow->pkt_mark = base; | |
4425 | wc->masks.pkt_mark = mask; | |
4426 | } | |
72e8bf28 | 4427 | } |
7fd91025 | 4428 | |
5bbda0aa EJ |
4429 | /* If any of the flow key data that ODP actions can modify are different in |
4430 | * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow | |
b9ad7294 EJ |
4431 | * key from 'base' into 'flow', and then changes 'base' the same way. Does not |
4432 | * commit set_tunnel actions. Users should call commit_odp_tunnel_action() | |
1dd35f8a | 4433 | * in addition to this function if needed. Sets fields in 'wc' that are |
7fd91025 BP |
4434 | * used as part of the action. |
4435 | * | |
4436 | * Returns a reason to force processing the flow's packets into the userspace | |
4437 | * slow path, if there is one, otherwise 0. */ | |
4438 | enum slow_path_reason | |
5bbda0aa | 4439 | commit_odp_actions(const struct flow *flow, struct flow *base, |
d23df9a8 JR |
4440 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
4441 | bool use_masked) | |
5bbda0aa | 4442 | { |
f6c8a6b1 BP |
4443 | enum slow_path_reason slow; |
4444 | ||
d23df9a8 JR |
4445 | commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked); |
4446 | slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked); | |
4447 | commit_set_port_action(flow, base, odp_actions, wc, use_masked); | |
22d38fca | 4448 | commit_mpls_action(flow, base, odp_actions); |
8bfd0fda | 4449 | commit_vlan_action(flow->vlan_tci, base, odp_actions, wc); |
d23df9a8 JR |
4450 | commit_set_priority_action(flow, base, odp_actions, wc, use_masked); |
4451 | commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked); | |
7fd91025 | 4452 | |
f6c8a6b1 | 4453 | return slow; |
5bbda0aa | 4454 | } |