]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
48cecbdc | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
bed69b3e | 18 | #include <arpa/inet.h> |
064af421 | 19 | #include "odp-util.h" |
36956a7d | 20 | #include <errno.h> |
064af421 | 21 | #include <inttypes.h> |
7202cbe5 | 22 | #include <math.h> |
6ca00f6f | 23 | #include <netinet/in.h> |
685a51a5 | 24 | #include <netinet/icmp6.h> |
064af421 BP |
25 | #include <stdlib.h> |
26 | #include <string.h> | |
cdee00fd | 27 | #include "byte-order.h" |
064af421 | 28 | #include "coverage.h" |
758c456d | 29 | #include "dpif.h" |
064af421 BP |
30 | #include "dynamic-string.h" |
31 | #include "flow.h" | |
cdee00fd | 32 | #include "netlink.h" |
3bffc610 | 33 | #include "ofpbuf.h" |
064af421 | 34 | #include "packets.h" |
44bac24b | 35 | #include "simap.h" |
064af421 BP |
36 | #include "timeval.h" |
37 | #include "util.h" | |
34118cae BP |
38 | #include "vlog.h" |
39 | ||
40 | VLOG_DEFINE_THIS_MODULE(odp_util); | |
064af421 | 41 | |
df2c07f4 JP |
42 | /* The interface between userspace and kernel uses an "OVS_*" prefix. |
43 | * Since this is fairly non-specific for the OVS userspace components, | |
44 | * "ODP_*" (Open vSwitch Datapath) is used as the prefix for | |
45 | * interactions with the datapath. | |
46 | */ | |
47 | ||
7202cbe5 BP |
48 | /* The set of characters that may separate one action or one key attribute |
49 | * from another. */ | |
50 | static const char *delimiters = ", \t\r\n"; | |
51 | ||
e6cc0bab AZ |
52 | static int parse_odp_key_mask_attr(const char *, const struct simap *port_names, |
53 | struct ofpbuf *, struct ofpbuf *); | |
54 | static void format_odp_key_attr(const struct nlattr *a, | |
0a37839c GS |
55 | const struct nlattr *ma, |
56 | const struct hmap *portno_names, struct ds *ds, | |
041e7168 | 57 | bool verbose); |
a052b51b | 58 | |
98403001 BP |
59 | /* Returns one the following for the action with the given OVS_ACTION_ATTR_* |
60 | * 'type': | |
61 | * | |
62 | * - For an action whose argument has a fixed length, returned that | |
63 | * nonnegative length in bytes. | |
64 | * | |
65 | * - For an action with a variable-length argument, returns -2. | |
66 | * | |
67 | * - For an invalid 'type', returns -1. */ | |
4edb9ae9 | 68 | static int |
cdee00fd BP |
69 | odp_action_len(uint16_t type) |
70 | { | |
df2c07f4 | 71 | if (type > OVS_ACTION_ATTR_MAX) { |
cdee00fd BP |
72 | return -1; |
73 | } | |
74 | ||
09ded0ad | 75 | switch ((enum ovs_action_attr) type) { |
fea393b1 | 76 | case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t); |
98403001 | 77 | case OVS_ACTION_ATTR_USERSPACE: return -2; |
fea393b1 BP |
78 | case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan); |
79 | case OVS_ACTION_ATTR_POP_VLAN: return 0; | |
b02475c5 SH |
80 | case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls); |
81 | case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16); | |
4edb9ae9 | 82 | case OVS_ACTION_ATTR_SET: return -2; |
98403001 | 83 | case OVS_ACTION_ATTR_SAMPLE: return -2; |
df2c07f4 JP |
84 | |
85 | case OVS_ACTION_ATTR_UNSPEC: | |
86 | case __OVS_ACTION_ATTR_MAX: | |
cdee00fd BP |
87 | return -1; |
88 | } | |
89 | ||
90 | return -1; | |
91 | } | |
92 | ||
e6603631 BP |
93 | /* Returns a string form of 'attr'. The return value is either a statically |
94 | * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize' | |
95 | * should be at least OVS_KEY_ATTR_BUFSIZE. */ | |
96 | enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 }; | |
744791b5 | 97 | static const char * |
e6603631 | 98 | ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) |
744791b5 | 99 | { |
744791b5 BP |
100 | switch (attr) { |
101 | case OVS_KEY_ATTR_UNSPEC: return "unspec"; | |
fea393b1 | 102 | case OVS_KEY_ATTR_ENCAP: return "encap"; |
1b567fb9 | 103 | case OVS_KEY_ATTR_PRIORITY: return "skb_priority"; |
72e8bf28 | 104 | case OVS_KEY_ATTR_SKB_MARK: return "skb_mark"; |
9b405f1a | 105 | case OVS_KEY_ATTR_TUNNEL: return "tunnel"; |
744791b5 BP |
106 | case OVS_KEY_ATTR_IN_PORT: return "in_port"; |
107 | case OVS_KEY_ATTR_ETHERNET: return "eth"; | |
fea393b1 | 108 | case OVS_KEY_ATTR_VLAN: return "vlan"; |
744791b5 BP |
109 | case OVS_KEY_ATTR_ETHERTYPE: return "eth_type"; |
110 | case OVS_KEY_ATTR_IPV4: return "ipv4"; | |
111 | case OVS_KEY_ATTR_IPV6: return "ipv6"; | |
112 | case OVS_KEY_ATTR_TCP: return "tcp"; | |
dc235f7f | 113 | case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags"; |
744791b5 | 114 | case OVS_KEY_ATTR_UDP: return "udp"; |
c6bcb685 | 115 | case OVS_KEY_ATTR_SCTP: return "sctp"; |
744791b5 BP |
116 | case OVS_KEY_ATTR_ICMP: return "icmp"; |
117 | case OVS_KEY_ATTR_ICMPV6: return "icmpv6"; | |
118 | case OVS_KEY_ATTR_ARP: return "arp"; | |
119 | case OVS_KEY_ATTR_ND: return "nd"; | |
b02475c5 | 120 | case OVS_KEY_ATTR_MPLS: return "mpls"; |
744791b5 BP |
121 | |
122 | case __OVS_KEY_ATTR_MAX: | |
123 | default: | |
e6603631 BP |
124 | snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); |
125 | return namebuf; | |
744791b5 BP |
126 | } |
127 | } | |
128 | ||
cdee00fd BP |
129 | static void |
130 | format_generic_odp_action(struct ds *ds, const struct nlattr *a) | |
131 | { | |
8ba43fbd BP |
132 | size_t len = nl_attr_get_size(a); |
133 | ||
cdee00fd | 134 | ds_put_format(ds, "action%"PRId16, nl_attr_type(a)); |
8ba43fbd | 135 | if (len) { |
cdee00fd BP |
136 | const uint8_t *unspec; |
137 | unsigned int i; | |
138 | ||
139 | unspec = nl_attr_get(a); | |
8ba43fbd | 140 | for (i = 0; i < len; i++) { |
cdee00fd BP |
141 | ds_put_char(ds, i ? ' ': '('); |
142 | ds_put_format(ds, "%02x", unspec[i]); | |
143 | } | |
144 | ds_put_char(ds, ')'); | |
145 | } | |
146 | } | |
147 | ||
6ff686f2 PS |
148 | static void |
149 | format_odp_sample_action(struct ds *ds, const struct nlattr *attr) | |
150 | { | |
151 | static const struct nl_policy ovs_sample_policy[] = { | |
3815d6c2 LS |
152 | { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */ |
153 | { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */ | |
154 | { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */ | |
6ff686f2 PS |
155 | }; |
156 | struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)]; | |
6ff686f2 PS |
157 | double percentage; |
158 | const struct nlattr *nla_acts; | |
159 | int len; | |
160 | ||
161 | ds_put_cstr(ds, "sample"); | |
162 | ||
5621afff | 163 | if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) { |
6ff686f2 PS |
164 | ds_put_cstr(ds, "(error)"); |
165 | return; | |
166 | } | |
167 | ||
168 | percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) / | |
169 | UINT32_MAX; | |
170 | ||
171 | ds_put_format(ds, "(sample=%.1f%%,", percentage); | |
172 | ||
173 | ds_put_cstr(ds, "actions("); | |
174 | nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
175 | len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
176 | format_odp_actions(ds, nla_acts, len); | |
177 | ds_put_format(ds, "))"); | |
178 | } | |
179 | ||
6a7e895f | 180 | static const char * |
04594cd5 | 181 | slow_path_reason_to_string(uint32_t reason) |
6a7e895f | 182 | { |
04594cd5 BP |
183 | switch ((enum slow_path_reason) reason) { |
184 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING; | |
185 | SLOW_PATH_REASONS | |
186 | #undef SPR | |
6a7e895f | 187 | } |
04594cd5 BP |
188 | |
189 | return NULL; | |
6a7e895f BP |
190 | } |
191 | ||
04594cd5 BP |
192 | const char * |
193 | slow_path_reason_to_explanation(enum slow_path_reason reason) | |
98f0520f | 194 | { |
04594cd5 BP |
195 | switch (reason) { |
196 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION; | |
197 | SLOW_PATH_REASONS | |
198 | #undef SPR | |
98f0520f EJ |
199 | } |
200 | ||
04594cd5 | 201 | return "<unknown>"; |
98f0520f EJ |
202 | } |
203 | ||
ec956fc7 PS |
204 | static int |
205 | parse_flags(const char *s, const char *(*bit_to_string)(uint32_t), | |
206 | uint32_t *res) | |
207 | { | |
208 | uint32_t result = 0; | |
209 | int n = 0; | |
210 | ||
211 | if (s[n] != '(') { | |
212 | return -EINVAL; | |
213 | } | |
214 | n++; | |
215 | ||
216 | while (s[n] != ')') { | |
217 | unsigned long long int flags; | |
218 | uint32_t bit; | |
219 | int n0; | |
220 | ||
c2c28dfd | 221 | if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) { |
ec956fc7 PS |
222 | n += n0 + (s[n + n0] == ','); |
223 | result |= flags; | |
224 | continue; | |
225 | } | |
226 | ||
227 | for (bit = 1; bit; bit <<= 1) { | |
228 | const char *name = bit_to_string(bit); | |
229 | size_t len; | |
230 | ||
231 | if (!name) { | |
232 | continue; | |
233 | } | |
234 | ||
235 | len = strlen(name); | |
236 | if (!strncmp(s + n, name, len) && | |
237 | (s[n + len] == ',' || s[n + len] == ')')) { | |
238 | result |= bit; | |
239 | n += len + (s[n + len] == ','); | |
240 | break; | |
241 | } | |
242 | } | |
243 | ||
244 | if (!bit) { | |
245 | return -EINVAL; | |
246 | } | |
247 | } | |
248 | n++; | |
249 | ||
250 | *res = result; | |
251 | return n; | |
6a7e895f BP |
252 | } |
253 | ||
98403001 BP |
254 | static void |
255 | format_odp_userspace_action(struct ds *ds, const struct nlattr *attr) | |
256 | { | |
257 | static const struct nl_policy ovs_userspace_policy[] = { | |
3815d6c2 LS |
258 | { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */ |
259 | { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */ | |
260 | { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */ | |
98403001 BP |
261 | }; |
262 | struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)]; | |
e995e3df | 263 | const struct nlattr *userdata_attr; |
98403001 BP |
264 | |
265 | if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) { | |
266 | ds_put_cstr(ds, "userspace(error)"); | |
267 | return; | |
268 | } | |
269 | ||
270 | ds_put_format(ds, "userspace(pid=%"PRIu32, | |
271 | nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID])); | |
272 | ||
e995e3df | 273 | userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA]; |
29089a54 RL |
274 | |
275 | if (userdata_attr) { | |
276 | const uint8_t *userdata = nl_attr_get(userdata_attr); | |
277 | size_t userdata_len = nl_attr_get_size(userdata_attr); | |
278 | bool userdata_unspec = true; | |
1673e0e4 | 279 | union user_action_cookie cookie; |
98403001 | 280 | |
29089a54 RL |
281 | if (userdata_len >= sizeof cookie.type |
282 | && userdata_len <= sizeof cookie) { | |
283 | ||
284 | memset(&cookie, 0, sizeof cookie); | |
285 | memcpy(&cookie, userdata, userdata_len); | |
286 | ||
287 | userdata_unspec = false; | |
288 | ||
289 | if (userdata_len == sizeof cookie.sflow | |
290 | && cookie.type == USER_ACTION_COOKIE_SFLOW) { | |
291 | ds_put_format(ds, ",sFlow(" | |
292 | "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")", | |
293 | vlan_tci_to_vid(cookie.sflow.vlan_tci), | |
294 | vlan_tci_to_pcp(cookie.sflow.vlan_tci), | |
295 | cookie.sflow.output); | |
296 | } else if (userdata_len == sizeof cookie.slow_path | |
297 | && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) { | |
04594cd5 BP |
298 | ds_put_cstr(ds, ",slow_path("); |
299 | format_flags(ds, slow_path_reason_to_string, | |
300 | cookie.slow_path.reason, ','); | |
301 | ds_put_format(ds, ")"); | |
29089a54 RL |
302 | } else if (userdata_len == sizeof cookie.flow_sample |
303 | && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) { | |
304 | ds_put_format(ds, ",flow_sample(probability=%"PRIu16 | |
305 | ",collector_set_id=%"PRIu32 | |
306 | ",obs_domain_id=%"PRIu32 | |
307 | ",obs_point_id=%"PRIu32")", | |
308 | cookie.flow_sample.probability, | |
309 | cookie.flow_sample.collector_set_id, | |
310 | cookie.flow_sample.obs_domain_id, | |
311 | cookie.flow_sample.obs_point_id); | |
b075a9b8 | 312 | } else if (userdata_len >= sizeof cookie.ipfix |
29089a54 RL |
313 | && cookie.type == USER_ACTION_COOKIE_IPFIX) { |
314 | ds_put_format(ds, ",ipfix"); | |
315 | } else { | |
316 | userdata_unspec = true; | |
317 | } | |
98403001 | 318 | } |
e995e3df | 319 | |
29089a54 RL |
320 | if (userdata_unspec) { |
321 | size_t i; | |
322 | ds_put_format(ds, ",userdata("); | |
323 | for (i = 0; i < userdata_len; i++) { | |
324 | ds_put_format(ds, "%02x", userdata[i]); | |
325 | } | |
326 | ds_put_char(ds, ')'); | |
e995e3df | 327 | } |
98403001 BP |
328 | } |
329 | ||
330 | ds_put_char(ds, ')'); | |
331 | } | |
332 | ||
8ddc056d BP |
333 | static void |
334 | format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci) | |
335 | { | |
336 | ds_put_format(ds, "vid=%"PRIu16",pcp=%d", | |
337 | vlan_tci_to_vid(vlan_tci), | |
338 | vlan_tci_to_pcp(vlan_tci)); | |
339 | if (!(vlan_tci & htons(VLAN_CFI))) { | |
340 | ds_put_cstr(ds, ",cfi=0"); | |
341 | } | |
342 | } | |
343 | ||
b02475c5 SH |
344 | static void |
345 | format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse) | |
346 | { | |
347 | ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d", | |
348 | mpls_lse_to_label(mpls_lse), | |
349 | mpls_lse_to_tc(mpls_lse), | |
350 | mpls_lse_to_ttl(mpls_lse), | |
351 | mpls_lse_to_bos(mpls_lse)); | |
352 | } | |
353 | ||
e6cc0bab AZ |
354 | static void |
355 | format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key, | |
356 | const struct ovs_key_mpls *mpls_mask) | |
357 | { | |
358 | ovs_be32 key = mpls_key->mpls_lse; | |
359 | ||
360 | if (mpls_mask == NULL) { | |
361 | format_mpls_lse(ds, key); | |
362 | } else { | |
363 | ovs_be32 mask = mpls_mask->mpls_lse; | |
364 | ||
365 | ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", | |
366 | mpls_lse_to_label(key), mpls_lse_to_label(mask), | |
367 | mpls_lse_to_tc(key), mpls_lse_to_tc(mask), | |
368 | mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), | |
369 | mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); | |
370 | } | |
371 | } | |
372 | ||
4edb9ae9 | 373 | static void |
cdee00fd | 374 | format_odp_action(struct ds *ds, const struct nlattr *a) |
064af421 | 375 | { |
98403001 | 376 | int expected_len; |
4edb9ae9 | 377 | enum ovs_action_attr type = nl_attr_type(a); |
fea393b1 | 378 | const struct ovs_action_push_vlan *vlan; |
cdee00fd | 379 | |
98403001 BP |
380 | expected_len = odp_action_len(nl_attr_type(a)); |
381 | if (expected_len != -2 && nl_attr_get_size(a) != expected_len) { | |
34582733 | 382 | ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ", |
98403001 | 383 | nl_attr_get_size(a), expected_len); |
cdee00fd BP |
384 | format_generic_odp_action(ds, a); |
385 | return; | |
386 | } | |
387 | ||
4edb9ae9 | 388 | switch (type) { |
df2c07f4 | 389 | case OVS_ACTION_ATTR_OUTPUT: |
26ce7705 | 390 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); |
064af421 | 391 | break; |
df2c07f4 | 392 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 393 | format_odp_userspace_action(ds, a); |
064af421 | 394 | break; |
4edb9ae9 PS |
395 | case OVS_ACTION_ATTR_SET: |
396 | ds_put_cstr(ds, "set("); | |
0a37839c | 397 | format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true); |
4edb9ae9 | 398 | ds_put_cstr(ds, ")"); |
064af421 | 399 | break; |
fea393b1 BP |
400 | case OVS_ACTION_ATTR_PUSH_VLAN: |
401 | vlan = nl_attr_get(a); | |
402 | ds_put_cstr(ds, "push_vlan("); | |
403 | if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) { | |
404 | ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid)); | |
405 | } | |
8ddc056d BP |
406 | format_vlan_tci(ds, vlan->vlan_tci); |
407 | ds_put_char(ds, ')'); | |
064af421 | 408 | break; |
fea393b1 BP |
409 | case OVS_ACTION_ATTR_POP_VLAN: |
410 | ds_put_cstr(ds, "pop_vlan"); | |
064af421 | 411 | break; |
b02475c5 SH |
412 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
413 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
414 | ds_put_cstr(ds, "push_mpls("); | |
415 | format_mpls_lse(ds, mpls->mpls_lse); | |
416 | ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype)); | |
417 | break; | |
418 | } | |
419 | case OVS_ACTION_ATTR_POP_MPLS: { | |
420 | ovs_be16 ethertype = nl_attr_get_be16(a); | |
421 | ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype)); | |
422 | break; | |
423 | } | |
6ff686f2 PS |
424 | case OVS_ACTION_ATTR_SAMPLE: |
425 | format_odp_sample_action(ds, a); | |
426 | break; | |
4edb9ae9 PS |
427 | case OVS_ACTION_ATTR_UNSPEC: |
428 | case __OVS_ACTION_ATTR_MAX: | |
064af421 | 429 | default: |
cdee00fd | 430 | format_generic_odp_action(ds, a); |
064af421 BP |
431 | break; |
432 | } | |
433 | } | |
434 | ||
435 | void | |
cdee00fd | 436 | format_odp_actions(struct ds *ds, const struct nlattr *actions, |
cf22f8cb | 437 | size_t actions_len) |
064af421 | 438 | { |
cdee00fd BP |
439 | if (actions_len) { |
440 | const struct nlattr *a; | |
441 | unsigned int left; | |
442 | ||
443 | NL_ATTR_FOR_EACH (a, left, actions, actions_len) { | |
444 | if (a != actions) { | |
445 | ds_put_char(ds, ','); | |
446 | } | |
447 | format_odp_action(ds, a); | |
064af421 | 448 | } |
cdee00fd | 449 | if (left) { |
3a8cfc50 BP |
450 | int i; |
451 | ||
3476fce3 BP |
452 | if (left == actions_len) { |
453 | ds_put_cstr(ds, "<empty>"); | |
454 | } | |
3a8cfc50 BP |
455 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
456 | for (i = 0; i < left; i++) { | |
457 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
458 | } | |
459 | ds_put_char(ds, ')'); | |
cdee00fd BP |
460 | } |
461 | } else { | |
064af421 BP |
462 | ds_put_cstr(ds, "drop"); |
463 | } | |
464 | } | |
7202cbe5 BP |
465 | |
466 | static int | |
44bac24b | 467 | parse_odp_action(const char *s, const struct simap *port_names, |
7202cbe5 BP |
468 | struct ofpbuf *actions) |
469 | { | |
7202cbe5 | 470 | { |
c2c28dfd BP |
471 | uint32_t port; |
472 | int n; | |
7202cbe5 | 473 | |
c2c28dfd | 474 | if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) { |
7202cbe5 BP |
475 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port); |
476 | return n; | |
477 | } | |
478 | } | |
479 | ||
480 | if (port_names) { | |
481 | int len = strcspn(s, delimiters); | |
44bac24b | 482 | struct simap_node *node; |
7202cbe5 | 483 | |
44bac24b | 484 | node = simap_find_len(port_names, s, len); |
7202cbe5 | 485 | if (node) { |
44bac24b | 486 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data); |
7202cbe5 BP |
487 | return len; |
488 | } | |
489 | } | |
490 | ||
491 | { | |
c2c28dfd BP |
492 | uint32_t pid; |
493 | uint32_t output; | |
494 | uint32_t probability; | |
495 | uint32_t collector_set_id; | |
496 | uint32_t obs_domain_id; | |
497 | uint32_t obs_point_id; | |
7202cbe5 BP |
498 | int vid, pcp; |
499 | int n = -1; | |
500 | ||
c2c28dfd | 501 | if (ovs_scan(s, "userspace(pid=%"SCNi32")%n", &pid, &n)) { |
e995e3df | 502 | odp_put_userspace_action(pid, NULL, 0, actions); |
7202cbe5 | 503 | return n; |
c2c28dfd BP |
504 | } else if (ovs_scan(s, "userspace(pid=%"SCNi32",sFlow(vid=%i," |
505 | "pcp=%i,output=%"SCNi32"))%n", | |
506 | &pid, &vid, &pcp, &output, &n)) { | |
1673e0e4 | 507 | union user_action_cookie cookie; |
7202cbe5 BP |
508 | uint16_t tci; |
509 | ||
510 | tci = vid | (pcp << VLAN_PCP_SHIFT); | |
511 | if (tci) { | |
512 | tci |= VLAN_CFI; | |
513 | } | |
514 | ||
515 | cookie.type = USER_ACTION_COOKIE_SFLOW; | |
1673e0e4 BP |
516 | cookie.sflow.vlan_tci = htons(tci); |
517 | cookie.sflow.output = output; | |
29089a54 RL |
518 | odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow, |
519 | actions); | |
6a7e895f | 520 | return n; |
c2c28dfd BP |
521 | } else if (ovs_scan(s, "userspace(pid=%"SCNi32",slow_path%n", |
522 | &pid, &n)) { | |
6a7e895f | 523 | union user_action_cookie cookie; |
04594cd5 | 524 | int res; |
6a7e895f BP |
525 | |
526 | cookie.type = USER_ACTION_COOKIE_SLOW_PATH; | |
527 | cookie.slow_path.unused = 0; | |
04594cd5 | 528 | cookie.slow_path.reason = 0; |
6a7e895f | 529 | |
04594cd5 BP |
530 | res = parse_flags(&s[n], slow_path_reason_to_string, |
531 | &cookie.slow_path.reason); | |
532 | if (res < 0) { | |
533 | return res; | |
534 | } | |
535 | n += res; | |
536 | if (s[n] != ')') { | |
6a7e895f BP |
537 | return -EINVAL; |
538 | } | |
04594cd5 | 539 | n++; |
6a7e895f | 540 | |
29089a54 RL |
541 | odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, |
542 | actions); | |
543 | return n; | |
c2c28dfd BP |
544 | } else if (ovs_scan(s, "userspace(pid=%"SCNi32"," |
545 | "flow_sample(probability=%"SCNi32"," | |
546 | "collector_set_id=%"SCNi32"," | |
547 | "obs_domain_id=%"SCNi32"," | |
548 | "obs_point_id=%"SCNi32"))%n", | |
549 | &pid, &probability, &collector_set_id, | |
550 | &obs_domain_id, &obs_point_id, &n)) { | |
29089a54 RL |
551 | union user_action_cookie cookie; |
552 | ||
553 | cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE; | |
554 | cookie.flow_sample.probability = probability; | |
555 | cookie.flow_sample.collector_set_id = collector_set_id; | |
556 | cookie.flow_sample.obs_domain_id = obs_domain_id; | |
557 | cookie.flow_sample.obs_point_id = obs_point_id; | |
558 | odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample, | |
559 | actions); | |
7202cbe5 | 560 | return n; |
c2c28dfd | 561 | } else if (ovs_scan(s, "userspace(pid=%"SCNi32",ipfix)%n", &pid, &n)) { |
29089a54 | 562 | union user_action_cookie cookie; |
7202cbe5 | 563 | |
29089a54 RL |
564 | cookie.type = USER_ACTION_COOKIE_IPFIX; |
565 | odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix, | |
e995e3df | 566 | actions); |
7202cbe5 | 567 | return n; |
c2c28dfd BP |
568 | } else if (ovs_scan(s, "userspace(pid=%"SCNi32",userdata(%n", |
569 | &pid, &n)) { | |
e995e3df BP |
570 | struct ofpbuf buf; |
571 | char *end; | |
572 | ||
573 | ofpbuf_init(&buf, 16); | |
574 | end = ofpbuf_put_hex(&buf, &s[n], NULL); | |
575 | if (end[0] == ')' && end[1] == ')') { | |
576 | odp_put_userspace_action(pid, buf.data, buf.size, actions); | |
577 | ofpbuf_uninit(&buf); | |
578 | return (end + 2) - s; | |
579 | } | |
7202cbe5 BP |
580 | } |
581 | } | |
582 | ||
583 | if (!strncmp(s, "set(", 4)) { | |
584 | size_t start_ofs; | |
585 | int retval; | |
586 | ||
587 | start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET); | |
e6cc0bab | 588 | retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL); |
7202cbe5 BP |
589 | if (retval < 0) { |
590 | return retval; | |
591 | } | |
592 | if (s[retval + 4] != ')') { | |
593 | return -EINVAL; | |
594 | } | |
595 | nl_msg_end_nested(actions, start_ofs); | |
596 | return retval + 5; | |
597 | } | |
598 | ||
becffb86 BP |
599 | { |
600 | struct ovs_action_push_vlan push; | |
601 | int tpid = ETH_TYPE_VLAN; | |
602 | int vid, pcp; | |
603 | int cfi = 1; | |
604 | int n = -1; | |
7202cbe5 | 605 | |
c2c28dfd BP |
606 | if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) |
607 | || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", | |
608 | &vid, &pcp, &cfi, &n) | |
609 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", | |
610 | &tpid, &vid, &pcp, &n) | |
611 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", | |
612 | &tpid, &vid, &pcp, &cfi, &n)) { | |
becffb86 BP |
613 | push.vlan_tpid = htons(tpid); |
614 | push.vlan_tci = htons((vid << VLAN_VID_SHIFT) | |
615 | | (pcp << VLAN_PCP_SHIFT) | |
616 | | (cfi ? VLAN_CFI : 0)); | |
617 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
618 | &push, sizeof push); | |
619 | ||
620 | return n; | |
7202cbe5 | 621 | } |
7202cbe5 BP |
622 | } |
623 | ||
becffb86 BP |
624 | if (!strncmp(s, "pop_vlan", 8)) { |
625 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN); | |
626 | return 8; | |
7202cbe5 BP |
627 | } |
628 | ||
629 | { | |
630 | double percentage; | |
631 | int n = -1; | |
632 | ||
c2c28dfd BP |
633 | if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) |
634 | && percentage >= 0. && percentage <= 100.0) { | |
7202cbe5 BP |
635 | size_t sample_ofs, actions_ofs; |
636 | double probability; | |
637 | ||
638 | probability = floor(UINT32_MAX * (percentage / 100.0) + .5); | |
639 | sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE); | |
640 | nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY, | |
641 | (probability <= 0 ? 0 | |
642 | : probability >= UINT32_MAX ? UINT32_MAX | |
643 | : probability)); | |
644 | ||
645 | actions_ofs = nl_msg_start_nested(actions, | |
646 | OVS_SAMPLE_ATTR_ACTIONS); | |
647 | for (;;) { | |
648 | int retval; | |
649 | ||
79d4ffe2 | 650 | n += strspn(s + n, delimiters); |
7202cbe5 BP |
651 | if (s[n] == ')') { |
652 | break; | |
653 | } | |
654 | ||
655 | retval = parse_odp_action(s + n, port_names, actions); | |
656 | if (retval < 0) { | |
657 | return retval; | |
658 | } | |
659 | n += retval; | |
7202cbe5 BP |
660 | } |
661 | nl_msg_end_nested(actions, actions_ofs); | |
662 | nl_msg_end_nested(actions, sample_ofs); | |
663 | ||
664 | return s[n + 1] == ')' ? n + 2 : -EINVAL; | |
665 | } | |
666 | } | |
667 | ||
668 | return -EINVAL; | |
669 | } | |
670 | ||
671 | /* Parses the string representation of datapath actions, in the format output | |
672 | * by format_odp_action(). Returns 0 if successful, otherwise a positive errno | |
673 | * value. On success, the ODP actions are appended to 'actions' as a series of | |
674 | * Netlink attributes. On failure, no data is appended to 'actions'. Either | |
675 | * way, 'actions''s data might be reallocated. */ | |
676 | int | |
44bac24b | 677 | odp_actions_from_string(const char *s, const struct simap *port_names, |
7202cbe5 BP |
678 | struct ofpbuf *actions) |
679 | { | |
680 | size_t old_size; | |
681 | ||
682 | if (!strcasecmp(s, "drop")) { | |
683 | return 0; | |
684 | } | |
685 | ||
686 | old_size = actions->size; | |
687 | for (;;) { | |
688 | int retval; | |
689 | ||
690 | s += strspn(s, delimiters); | |
691 | if (!*s) { | |
692 | return 0; | |
693 | } | |
694 | ||
695 | retval = parse_odp_action(s, port_names, actions); | |
696 | if (retval < 0 || !strchr(delimiters, s[retval])) { | |
697 | actions->size = old_size; | |
698 | return -retval; | |
699 | } | |
700 | s += retval; | |
701 | } | |
702 | ||
703 | return 0; | |
704 | } | |
14608a15 | 705 | \f |
36956a7d | 706 | /* Returns the correct length of the payload for a flow key attribute of the |
fea393b1 BP |
707 | * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload |
708 | * is variable length. */ | |
36956a7d BP |
709 | static int |
710 | odp_flow_key_attr_len(uint16_t type) | |
711 | { | |
df2c07f4 | 712 | if (type > OVS_KEY_ATTR_MAX) { |
36956a7d BP |
713 | return -1; |
714 | } | |
715 | ||
09ded0ad | 716 | switch ((enum ovs_key_attr) type) { |
fea393b1 | 717 | case OVS_KEY_ATTR_ENCAP: return -2; |
abff858b | 718 | case OVS_KEY_ATTR_PRIORITY: return 4; |
72e8bf28 | 719 | case OVS_KEY_ATTR_SKB_MARK: return 4; |
9b405f1a | 720 | case OVS_KEY_ATTR_TUNNEL: return -2; |
df2c07f4 JP |
721 | case OVS_KEY_ATTR_IN_PORT: return 4; |
722 | case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet); | |
fea393b1 | 723 | case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16); |
df2c07f4 | 724 | case OVS_KEY_ATTR_ETHERTYPE: return 2; |
b02475c5 | 725 | case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls); |
df2c07f4 JP |
726 | case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4); |
727 | case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6); | |
728 | case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp); | |
dc235f7f | 729 | case OVS_KEY_ATTR_TCP_FLAGS: return 2; |
df2c07f4 | 730 | case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp); |
c6bcb685 | 731 | case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp); |
df2c07f4 JP |
732 | case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp); |
733 | case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6); | |
734 | case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp); | |
735 | case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd); | |
736 | ||
737 | case OVS_KEY_ATTR_UNSPEC: | |
738 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
739 | return -1; |
740 | } | |
741 | ||
742 | return -1; | |
743 | } | |
744 | ||
36956a7d BP |
745 | static void |
746 | format_generic_odp_key(const struct nlattr *a, struct ds *ds) | |
747 | { | |
748 | size_t len = nl_attr_get_size(a); | |
36956a7d BP |
749 | if (len) { |
750 | const uint8_t *unspec; | |
751 | unsigned int i; | |
752 | ||
753 | unspec = nl_attr_get(a); | |
754 | for (i = 0; i < len; i++) { | |
e6cc0bab AZ |
755 | if (i) { |
756 | ds_put_char(ds, ' '); | |
757 | } | |
36956a7d BP |
758 | ds_put_format(ds, "%02x", unspec[i]); |
759 | } | |
36956a7d BP |
760 | } |
761 | } | |
762 | ||
7257b535 BP |
763 | static const char * |
764 | ovs_frag_type_to_string(enum ovs_frag_type type) | |
765 | { | |
766 | switch (type) { | |
767 | case OVS_FRAG_TYPE_NONE: | |
768 | return "no"; | |
769 | case OVS_FRAG_TYPE_FIRST: | |
770 | return "first"; | |
771 | case OVS_FRAG_TYPE_LATER: | |
772 | return "later"; | |
773 | case __OVS_FRAG_TYPE_MAX: | |
774 | default: | |
775 | return "<error>"; | |
776 | } | |
777 | } | |
778 | ||
9b405f1a PS |
779 | static int |
780 | tunnel_key_attr_len(int type) | |
29c7a2b2 | 781 | { |
9b405f1a PS |
782 | switch (type) { |
783 | case OVS_TUNNEL_KEY_ATTR_ID: return 8; | |
784 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4; | |
785 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4; | |
786 | case OVS_TUNNEL_KEY_ATTR_TOS: return 1; | |
787 | case OVS_TUNNEL_KEY_ATTR_TTL: return 1; | |
788 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0; | |
789 | case OVS_TUNNEL_KEY_ATTR_CSUM: return 0; | |
790 | case __OVS_TUNNEL_KEY_ATTR_MAX: | |
791 | return -1; | |
29c7a2b2 | 792 | } |
9b405f1a PS |
793 | return -1; |
794 | } | |
795 | ||
617e10e7 SH |
796 | enum odp_key_fitness |
797 | odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun) | |
9b405f1a PS |
798 | { |
799 | unsigned int left; | |
800 | const struct nlattr *a; | |
801 | bool ttl = false; | |
802 | bool unknown = false; | |
803 | ||
804 | NL_NESTED_FOR_EACH(a, left, attr) { | |
805 | uint16_t type = nl_attr_type(a); | |
806 | size_t len = nl_attr_get_size(a); | |
807 | int expected_len = tunnel_key_attr_len(type); | |
808 | ||
809 | if (len != expected_len && expected_len >= 0) { | |
810 | return ODP_FIT_ERROR; | |
811 | } | |
812 | ||
813 | switch (type) { | |
814 | case OVS_TUNNEL_KEY_ATTR_ID: | |
815 | tun->tun_id = nl_attr_get_be64(a); | |
816 | tun->flags |= FLOW_TNL_F_KEY; | |
817 | break; | |
818 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
819 | tun->ip_src = nl_attr_get_be32(a); | |
820 | break; | |
821 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
822 | tun->ip_dst = nl_attr_get_be32(a); | |
823 | break; | |
824 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
825 | tun->ip_tos = nl_attr_get_u8(a); | |
826 | break; | |
827 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
828 | tun->ip_ttl = nl_attr_get_u8(a); | |
829 | ttl = true; | |
830 | break; | |
831 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
832 | tun->flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
833 | break; | |
834 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
835 | tun->flags |= FLOW_TNL_F_CSUM; | |
836 | break; | |
837 | default: | |
838 | /* Allow this to show up as unexpected, if there are unknown | |
839 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
840 | unknown = true; | |
841 | break; | |
842 | } | |
843 | } | |
844 | ||
845 | if (!ttl) { | |
846 | return ODP_FIT_ERROR; | |
847 | } | |
848 | if (unknown) { | |
758c456d | 849 | return ODP_FIT_TOO_MUCH; |
9b405f1a PS |
850 | } |
851 | return ODP_FIT_PERFECT; | |
852 | } | |
853 | ||
854 | static void | |
855 | tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key) | |
856 | { | |
857 | size_t tun_key_ofs; | |
858 | ||
859 | tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL); | |
860 | ||
861 | if (tun_key->flags & FLOW_TNL_F_KEY) { | |
862 | nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id); | |
863 | } | |
864 | if (tun_key->ip_src) { | |
865 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src); | |
866 | } | |
867 | if (tun_key->ip_dst) { | |
868 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst); | |
869 | } | |
870 | if (tun_key->ip_tos) { | |
871 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos); | |
872 | } | |
873 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl); | |
874 | if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
875 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
876 | } | |
877 | if (tun_key->flags & FLOW_TNL_F_CSUM) { | |
878 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
879 | } | |
880 | ||
881 | nl_msg_end_nested(a, tun_key_ofs); | |
29c7a2b2 PS |
882 | } |
883 | ||
041e7168 AZ |
884 | static bool |
885 | odp_mask_attr_is_wildcard(const struct nlattr *ma) | |
886 | { | |
887 | return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma)); | |
888 | } | |
889 | ||
e6cc0bab AZ |
890 | static bool |
891 | odp_mask_attr_is_exact(const struct nlattr *ma) | |
892 | { | |
893 | bool is_exact = false; | |
894 | enum ovs_key_attr attr = nl_attr_type(ma); | |
895 | ||
896 | if (attr == OVS_KEY_ATTR_TUNNEL) { | |
897 | /* XXX this is a hack for now. Should change | |
898 | * the exact match dection to per field | |
899 | * instead of per attribute. | |
900 | */ | |
901 | struct flow_tnl tun_mask; | |
902 | memset(&tun_mask, 0, sizeof tun_mask); | |
903 | odp_tun_key_from_attr(ma, &tun_mask); | |
904 | if (tun_mask.flags == (FLOW_TNL_F_KEY | |
905 | | FLOW_TNL_F_DONT_FRAGMENT | |
906 | | FLOW_TNL_F_CSUM)) { | |
907 | /* The flags are exact match, check the remaining fields. */ | |
908 | tun_mask.flags = 0xffff; | |
909 | is_exact = is_all_ones((uint8_t *)&tun_mask, | |
910 | offsetof(struct flow_tnl, ip_ttl)); | |
911 | } | |
912 | } else { | |
913 | is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma)); | |
914 | } | |
915 | ||
916 | return is_exact; | |
917 | } | |
918 | ||
0a37839c GS |
919 | void |
920 | odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no, | |
921 | char *port_name) | |
922 | { | |
923 | struct odp_portno_names *odp_portno_names; | |
924 | ||
925 | odp_portno_names = xmalloc(sizeof *odp_portno_names); | |
926 | odp_portno_names->port_no = port_no; | |
927 | odp_portno_names->name = xstrdup(port_name); | |
928 | hmap_insert(portno_names, &odp_portno_names->hmap_node, | |
929 | hash_odp_port(port_no)); | |
930 | } | |
931 | ||
932 | static char * | |
933 | odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no) | |
934 | { | |
935 | struct odp_portno_names *odp_portno_names; | |
936 | ||
937 | HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node, | |
938 | hash_odp_port(port_no), portno_names) { | |
939 | if (odp_portno_names->port_no == port_no) { | |
940 | return odp_portno_names->name; | |
941 | } | |
942 | } | |
943 | return NULL; | |
944 | } | |
945 | ||
946 | void | |
947 | odp_portno_names_destroy(struct hmap *portno_names) | |
948 | { | |
949 | struct odp_portno_names *odp_portno_names, *odp_portno_names_next; | |
950 | HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next, | |
951 | hmap_node, portno_names) { | |
952 | hmap_remove(portno_names, &odp_portno_names->hmap_node); | |
953 | free(odp_portno_names->name); | |
954 | free(odp_portno_names); | |
955 | } | |
956 | } | |
e6cc0bab | 957 | |
36956a7d | 958 | static void |
e6cc0bab | 959 | format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, |
0a37839c GS |
960 | const struct hmap *portno_names, struct ds *ds, |
961 | bool verbose) | |
36956a7d | 962 | { |
9b405f1a | 963 | struct flow_tnl tun_key; |
7a8e9ed2 | 964 | enum ovs_key_attr attr = nl_attr_type(a); |
e6603631 | 965 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
fea393b1 | 966 | int expected_len; |
dc8c5408 | 967 | bool is_exact; |
36956a7d | 968 | |
dc8c5408 | 969 | is_exact = ma ? odp_mask_attr_is_exact(ma) : true; |
e6cc0bab | 970 | |
e6603631 | 971 | ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf)); |
e6cc0bab AZ |
972 | |
973 | { | |
974 | expected_len = odp_flow_key_attr_len(nl_attr_type(a)); | |
975 | if (expected_len != -2) { | |
976 | bool bad_key_len = nl_attr_get_size(a) != expected_len; | |
977 | bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len; | |
978 | ||
979 | if (bad_key_len || bad_mask_len) { | |
980 | if (bad_key_len) { | |
34582733 | 981 | ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(", |
e6cc0bab AZ |
982 | nl_attr_get_size(a), |
983 | odp_flow_key_attr_len(nl_attr_type(a))); | |
984 | } | |
985 | format_generic_odp_key(a, ds); | |
986 | if (bad_mask_len) { | |
987 | ds_put_char(ds, '/'); | |
34582733 | 988 | ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(", |
e6cc0bab AZ |
989 | nl_attr_get_size(ma), |
990 | odp_flow_key_attr_len(nl_attr_type(ma))); | |
991 | } | |
992 | format_generic_odp_key(ma, ds); | |
993 | ds_put_char(ds, ')'); | |
994 | return; | |
995 | } | |
996 | } | |
36956a7d BP |
997 | } |
998 | ||
e6cc0bab | 999 | ds_put_char(ds, '('); |
7a8e9ed2 | 1000 | switch (attr) { |
fea393b1 | 1001 | case OVS_KEY_ATTR_ENCAP: |
e6cc0bab AZ |
1002 | if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) { |
1003 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), | |
0a37839c | 1004 | nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds, |
041e7168 | 1005 | verbose); |
0a37839c GS |
1006 | } else if (nl_attr_get_size(a)) { |
1007 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL, | |
1008 | ds, verbose); | |
fea393b1 | 1009 | } |
fea393b1 BP |
1010 | break; |
1011 | ||
abff858b | 1012 | case OVS_KEY_ATTR_PRIORITY: |
72e8bf28 | 1013 | case OVS_KEY_ATTR_SKB_MARK: |
e6cc0bab | 1014 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); |
dc8c5408 | 1015 | if (!is_exact) { |
e6cc0bab AZ |
1016 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); |
1017 | } | |
72e8bf28 AA |
1018 | break; |
1019 | ||
9b405f1a PS |
1020 | case OVS_KEY_ATTR_TUNNEL: |
1021 | memset(&tun_key, 0, sizeof tun_key); | |
617e10e7 | 1022 | if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) { |
e6cc0bab | 1023 | ds_put_format(ds, "error"); |
dc8c5408 | 1024 | } else if (!is_exact) { |
e6cc0bab AZ |
1025 | struct flow_tnl tun_mask; |
1026 | ||
1027 | memset(&tun_mask, 0, sizeof tun_mask); | |
1028 | odp_tun_key_from_attr(ma, &tun_mask); | |
1029 | ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64 | |
1030 | ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT | |
1031 | ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8 | |
1032 | ",flags(", | |
1033 | ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id), | |
1034 | IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src), | |
1035 | IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst), | |
1036 | tun_key.ip_tos, tun_mask.ip_tos, | |
1037 | tun_key.ip_ttl, tun_mask.ip_ttl); | |
1038 | ||
1039 | format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ','); | |
1040 | ||
1041 | /* XXX This code is correct, but enabling it would break the unit | |
1042 | test. Disable it for now until the input parser is fixed. | |
1043 | ||
1044 | ds_put_char(ds, '/'); | |
1045 | format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ','); | |
1046 | */ | |
1047 | ds_put_char(ds, ')'); | |
9b405f1a | 1048 | } else { |
e6cc0bab | 1049 | ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT"," |
9b405f1a PS |
1050 | "tos=0x%"PRIx8",ttl=%"PRIu8",flags(", |
1051 | ntohll(tun_key.tun_id), | |
1052 | IP_ARGS(tun_key.ip_src), | |
1053 | IP_ARGS(tun_key.ip_dst), | |
1054 | tun_key.ip_tos, tun_key.ip_ttl); | |
1055 | ||
e6cc0bab AZ |
1056 | format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ','); |
1057 | ds_put_char(ds, ')'); | |
9b405f1a | 1058 | } |
356af50b KM |
1059 | break; |
1060 | ||
df2c07f4 | 1061 | case OVS_KEY_ATTR_IN_PORT: |
0a37839c GS |
1062 | if (portno_names && verbose && is_exact) { |
1063 | char *name = odp_portno_names_get(portno_names, | |
1064 | u32_to_odp(nl_attr_get_u32(a))); | |
1065 | if (name) { | |
1066 | ds_put_format(ds, "%s", name); | |
1067 | } else { | |
1068 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); | |
1069 | } | |
1070 | } else { | |
1071 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); | |
1072 | if (!is_exact) { | |
1073 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); | |
1074 | } | |
e6cc0bab | 1075 | } |
36956a7d BP |
1076 | break; |
1077 | ||
df2c07f4 | 1078 | case OVS_KEY_ATTR_ETHERNET: |
dc8c5408 | 1079 | if (!is_exact) { |
e6cc0bab AZ |
1080 | const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma); |
1081 | const struct ovs_key_ethernet *eth_key = nl_attr_get(a); | |
1082 | ||
1083 | ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT | |
1084 | ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT, | |
1085 | ETH_ADDR_ARGS(eth_key->eth_src), | |
1086 | ETH_ADDR_ARGS(eth_mask->eth_src), | |
1087 | ETH_ADDR_ARGS(eth_key->eth_dst), | |
1088 | ETH_ADDR_ARGS(eth_mask->eth_dst)); | |
1089 | } else { | |
1090 | const struct ovs_key_ethernet *eth_key = nl_attr_get(a); | |
1091 | ||
1092 | ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT, | |
1093 | ETH_ADDR_ARGS(eth_key->eth_src), | |
1094 | ETH_ADDR_ARGS(eth_key->eth_dst)); | |
1095 | } | |
36956a7d BP |
1096 | break; |
1097 | ||
fea393b1 | 1098 | case OVS_KEY_ATTR_VLAN: |
e6cc0bab AZ |
1099 | { |
1100 | ovs_be16 vlan_tci = nl_attr_get_be16(a); | |
dc8c5408 | 1101 | if (!is_exact) { |
e6cc0bab | 1102 | ovs_be16 mask = nl_attr_get_be16(ma); |
2d39507c | 1103 | ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d", |
e6cc0bab AZ |
1104 | vlan_tci_to_vid(vlan_tci), |
1105 | vlan_tci_to_vid(mask), | |
1106 | vlan_tci_to_pcp(vlan_tci), | |
b06b6a39 | 1107 | vlan_tci_to_pcp(mask), |
e6cc0bab AZ |
1108 | vlan_tci_to_cfi(vlan_tci), |
1109 | vlan_tci_to_cfi(mask)); | |
1110 | } else { | |
1111 | format_vlan_tci(ds, vlan_tci); | |
1112 | } | |
1113 | } | |
36956a7d BP |
1114 | break; |
1115 | ||
b02475c5 SH |
1116 | case OVS_KEY_ATTR_MPLS: { |
1117 | const struct ovs_key_mpls *mpls_key = nl_attr_get(a); | |
e6cc0bab | 1118 | const struct ovs_key_mpls *mpls_mask = NULL; |
dc8c5408 | 1119 | if (!is_exact) { |
e6cc0bab AZ |
1120 | mpls_mask = nl_attr_get(ma); |
1121 | } | |
1122 | format_mpls(ds, mpls_key, mpls_mask); | |
b02475c5 SH |
1123 | break; |
1124 | } | |
1125 | ||
df2c07f4 | 1126 | case OVS_KEY_ATTR_ETHERTYPE: |
e6cc0bab | 1127 | ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a))); |
dc8c5408 | 1128 | if (!is_exact) { |
e6cc0bab AZ |
1129 | ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma))); |
1130 | } | |
36956a7d BP |
1131 | break; |
1132 | ||
df2c07f4 | 1133 | case OVS_KEY_ATTR_IPV4: |
dc8c5408 | 1134 | if (!is_exact) { |
e6cc0bab AZ |
1135 | const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a); |
1136 | const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma); | |
1137 | ||
1138 | ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT | |
1139 | ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8 | |
1140 | ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8, | |
1141 | IP_ARGS(ipv4_key->ipv4_src), | |
1142 | IP_ARGS(ipv4_mask->ipv4_src), | |
1143 | IP_ARGS(ipv4_key->ipv4_dst), | |
1144 | IP_ARGS(ipv4_mask->ipv4_dst), | |
1145 | ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto, | |
1146 | ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos, | |
1147 | ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl, | |
1148 | ovs_frag_type_to_string(ipv4_key->ipv4_frag), | |
1149 | ipv4_mask->ipv4_frag); | |
1150 | } else { | |
1151 | const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a); | |
1152 | ||
1153 | ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8 | |
1154 | ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s", | |
1155 | IP_ARGS(ipv4_key->ipv4_src), | |
1156 | IP_ARGS(ipv4_key->ipv4_dst), | |
1157 | ipv4_key->ipv4_proto, ipv4_key->ipv4_tos, | |
1158 | ipv4_key->ipv4_ttl, | |
1159 | ovs_frag_type_to_string(ipv4_key->ipv4_frag)); | |
1160 | } | |
36956a7d BP |
1161 | break; |
1162 | ||
e6cc0bab | 1163 | case OVS_KEY_ATTR_IPV6: |
dc8c5408 | 1164 | if (!is_exact) { |
e6cc0bab AZ |
1165 | const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask; |
1166 | char src_str[INET6_ADDRSTRLEN]; | |
1167 | char dst_str[INET6_ADDRSTRLEN]; | |
1168 | char src_mask[INET6_ADDRSTRLEN]; | |
1169 | char dst_mask[INET6_ADDRSTRLEN]; | |
1170 | ||
1171 | ipv6_key = nl_attr_get(a); | |
1172 | inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str); | |
1173 | inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str); | |
1174 | ||
1175 | ipv6_mask = nl_attr_get(ma); | |
1176 | inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask); | |
1177 | inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask); | |
1178 | ||
1179 | ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32 | |
1180 | ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8 | |
1181 | ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8, | |
1182 | src_str, src_mask, dst_str, dst_mask, | |
1183 | ntohl(ipv6_key->ipv6_label), | |
1184 | ntohl(ipv6_mask->ipv6_label), | |
1185 | ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto, | |
1186 | ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass, | |
1187 | ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit, | |
1188 | ovs_frag_type_to_string(ipv6_key->ipv6_frag), | |
1189 | ipv6_mask->ipv6_frag); | |
1190 | } else { | |
1191 | const struct ovs_key_ipv6 *ipv6_key; | |
1192 | char src_str[INET6_ADDRSTRLEN]; | |
1193 | char dst_str[INET6_ADDRSTRLEN]; | |
1194 | ||
1195 | ipv6_key = nl_attr_get(a); | |
1196 | inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str); | |
1197 | inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str); | |
1198 | ||
1199 | ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8 | |
1200 | ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s", | |
1201 | src_str, dst_str, ntohl(ipv6_key->ipv6_label), | |
1202 | ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass, | |
1203 | ipv6_key->ipv6_hlimit, | |
1204 | ovs_frag_type_to_string(ipv6_key->ipv6_frag)); | |
1205 | } | |
d31f1109 | 1206 | break; |
d31f1109 | 1207 | |
df2c07f4 | 1208 | case OVS_KEY_ATTR_TCP: |
dc8c5408 | 1209 | if (!is_exact) { |
e6cc0bab AZ |
1210 | const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma); |
1211 | const struct ovs_key_tcp *tcp_key = nl_attr_get(a); | |
1212 | ||
1213 | ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16 | |
1214 | ",dst=%"PRIu16"/%#"PRIx16, | |
1215 | ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src), | |
1216 | ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst)); | |
1217 | } else { | |
1218 | const struct ovs_key_tcp *tcp_key = nl_attr_get(a); | |
1219 | ||
1220 | ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16, | |
1221 | ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst)); | |
1222 | } | |
36956a7d BP |
1223 | break; |
1224 | ||
dc235f7f JR |
1225 | case OVS_KEY_ATTR_TCP_FLAGS: |
1226 | ds_put_format(ds, "0x%03"PRIx16, ntohs(nl_attr_get_be16(a))); | |
1227 | if (!is_exact) { | |
1228 | ds_put_format(ds, "/0x%03"PRIx16, ntohs(nl_attr_get_be16(ma))); | |
1229 | } | |
1230 | break; | |
1231 | ||
df2c07f4 | 1232 | case OVS_KEY_ATTR_UDP: |
dc8c5408 | 1233 | if (!is_exact) { |
e6cc0bab AZ |
1234 | const struct ovs_key_udp *udp_mask = nl_attr_get(ma); |
1235 | const struct ovs_key_udp *udp_key = nl_attr_get(a); | |
1236 | ||
1237 | ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16 | |
1238 | ",dst=%"PRIu16"/%#"PRIx16, | |
1239 | ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src), | |
1240 | ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst)); | |
1241 | } else { | |
1242 | const struct ovs_key_udp *udp_key = nl_attr_get(a); | |
1243 | ||
1244 | ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16, | |
1245 | ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst)); | |
1246 | } | |
36956a7d BP |
1247 | break; |
1248 | ||
c6bcb685 JS |
1249 | case OVS_KEY_ATTR_SCTP: |
1250 | if (ma) { | |
1251 | const struct ovs_key_sctp *sctp_mask = nl_attr_get(ma); | |
1252 | const struct ovs_key_sctp *sctp_key = nl_attr_get(a); | |
1253 | ||
1254 | ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16 | |
1255 | ",dst=%"PRIu16"/%#"PRIx16, | |
1256 | ntohs(sctp_key->sctp_src), ntohs(sctp_mask->sctp_src), | |
1257 | ntohs(sctp_key->sctp_dst), ntohs(sctp_mask->sctp_dst)); | |
1258 | } else { | |
1259 | const struct ovs_key_sctp *sctp_key = nl_attr_get(a); | |
1260 | ||
1261 | ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")", | |
1262 | ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst)); | |
1263 | } | |
1264 | break; | |
1265 | ||
df2c07f4 | 1266 | case OVS_KEY_ATTR_ICMP: |
dc8c5408 | 1267 | if (!is_exact) { |
e6cc0bab AZ |
1268 | const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma); |
1269 | const struct ovs_key_icmp *icmp_key = nl_attr_get(a); | |
1270 | ||
1271 | ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8, | |
1272 | icmp_key->icmp_type, icmp_mask->icmp_type, | |
1273 | icmp_key->icmp_code, icmp_mask->icmp_code); | |
1274 | } else { | |
1275 | const struct ovs_key_icmp *icmp_key = nl_attr_get(a); | |
1276 | ||
1277 | ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8, | |
1278 | icmp_key->icmp_type, icmp_key->icmp_code); | |
1279 | } | |
36956a7d BP |
1280 | break; |
1281 | ||
df2c07f4 | 1282 | case OVS_KEY_ATTR_ICMPV6: |
dc8c5408 | 1283 | if (!is_exact) { |
e6cc0bab AZ |
1284 | const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma); |
1285 | const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a); | |
1286 | ||
1287 | ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8, | |
1288 | icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type, | |
1289 | icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code); | |
1290 | } else { | |
1291 | const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a); | |
1292 | ||
1293 | ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8, | |
1294 | icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code); | |
1295 | } | |
d31f1109 JP |
1296 | break; |
1297 | ||
df2c07f4 | 1298 | case OVS_KEY_ATTR_ARP: |
dc8c5408 | 1299 | if (!is_exact) { |
e6cc0bab AZ |
1300 | const struct ovs_key_arp *arp_mask = nl_attr_get(ma); |
1301 | const struct ovs_key_arp *arp_key = nl_attr_get(a); | |
1302 | ||
1303 | ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT | |
1304 | ",op=%"PRIu16"/%#"PRIx16 | |
1305 | ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT | |
1306 | ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT, | |
1307 | IP_ARGS(arp_key->arp_sip), | |
1308 | IP_ARGS(arp_mask->arp_sip), | |
1309 | IP_ARGS(arp_key->arp_tip), | |
1310 | IP_ARGS(arp_mask->arp_tip), | |
1311 | ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op), | |
1312 | ETH_ADDR_ARGS(arp_key->arp_sha), | |
1313 | ETH_ADDR_ARGS(arp_mask->arp_sha), | |
1314 | ETH_ADDR_ARGS(arp_key->arp_tha), | |
1315 | ETH_ADDR_ARGS(arp_mask->arp_tha)); | |
1316 | } else { | |
1317 | const struct ovs_key_arp *arp_key = nl_attr_get(a); | |
1318 | ||
1319 | ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16"," | |
1320 | "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT, | |
1321 | IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip), | |
1322 | ntohs(arp_key->arp_op), | |
1323 | ETH_ADDR_ARGS(arp_key->arp_sha), | |
1324 | ETH_ADDR_ARGS(arp_key->arp_tha)); | |
1325 | } | |
36956a7d BP |
1326 | break; |
1327 | ||
df2c07f4 | 1328 | case OVS_KEY_ATTR_ND: { |
dc8c5408 | 1329 | const struct ovs_key_nd *nd_key, *nd_mask = NULL; |
685a51a5 JP |
1330 | char target[INET6_ADDRSTRLEN]; |
1331 | ||
1332 | nd_key = nl_attr_get(a); | |
dc8c5408 AZ |
1333 | if (!is_exact) { |
1334 | nd_mask = nl_attr_get(ma); | |
1335 | } | |
e6cc0bab | 1336 | |
685a51a5 | 1337 | inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target); |
e6cc0bab | 1338 | ds_put_format(ds, "target=%s", target); |
dc8c5408 | 1339 | if (!is_exact) { |
e6cc0bab AZ |
1340 | inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target); |
1341 | ds_put_format(ds, "/%s", target); | |
1342 | } | |
685a51a5 | 1343 | |
685a51a5 JP |
1344 | if (!eth_addr_is_zero(nd_key->nd_sll)) { |
1345 | ds_put_format(ds, ",sll="ETH_ADDR_FMT, | |
1346 | ETH_ADDR_ARGS(nd_key->nd_sll)); | |
dc8c5408 | 1347 | if (!is_exact) { |
e6cc0bab AZ |
1348 | ds_put_format(ds, "/"ETH_ADDR_FMT, |
1349 | ETH_ADDR_ARGS(nd_mask->nd_sll)); | |
1350 | } | |
685a51a5 JP |
1351 | } |
1352 | if (!eth_addr_is_zero(nd_key->nd_tll)) { | |
1353 | ds_put_format(ds, ",tll="ETH_ADDR_FMT, | |
1354 | ETH_ADDR_ARGS(nd_key->nd_tll)); | |
dc8c5408 | 1355 | if (!is_exact) { |
e6cc0bab AZ |
1356 | ds_put_format(ds, "/"ETH_ADDR_FMT, |
1357 | ETH_ADDR_ARGS(nd_mask->nd_tll)); | |
1358 | } | |
685a51a5 | 1359 | } |
685a51a5 JP |
1360 | break; |
1361 | } | |
1362 | ||
7a8e9ed2 BP |
1363 | case OVS_KEY_ATTR_UNSPEC: |
1364 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
1365 | default: |
1366 | format_generic_odp_key(a, ds); | |
dc8c5408 | 1367 | if (!is_exact) { |
e6cc0bab AZ |
1368 | ds_put_char(ds, '/'); |
1369 | format_generic_odp_key(ma, ds); | |
1370 | } | |
36956a7d BP |
1371 | break; |
1372 | } | |
e6cc0bab | 1373 | ds_put_char(ds, ')'); |
36956a7d BP |
1374 | } |
1375 | ||
dc8c5408 AZ |
1376 | static struct nlattr * |
1377 | generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key) | |
1378 | { | |
1379 | const struct nlattr *a; | |
1380 | unsigned int left; | |
1381 | int type = nl_attr_type(key); | |
1382 | int size = nl_attr_get_size(key); | |
1383 | ||
1384 | if (odp_flow_key_attr_len(type) >=0) { | |
9ddf12cc | 1385 | nl_msg_put_unspec_zero(ofp, type, size); |
dc8c5408 AZ |
1386 | } else { |
1387 | size_t nested_mask; | |
1388 | ||
1389 | nested_mask = nl_msg_start_nested(ofp, type); | |
1390 | NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) { | |
1391 | generate_all_wildcard_mask(ofp, nl_attr_get(a)); | |
1392 | } | |
1393 | nl_msg_end_nested(ofp, nested_mask); | |
1394 | } | |
1395 | ||
1396 | return ofp->base; | |
1397 | } | |
1398 | ||
36956a7d | 1399 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
e6cc0bab | 1400 | * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the |
73580638 GS |
1401 | * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is |
1402 | * non-null and 'verbose' is true, translates odp port number to its name. */ | |
14608a15 | 1403 | void |
e6cc0bab AZ |
1404 | odp_flow_format(const struct nlattr *key, size_t key_len, |
1405 | const struct nlattr *mask, size_t mask_len, | |
0a37839c | 1406 | const struct hmap *portno_names, struct ds *ds, bool verbose) |
14608a15 | 1407 | { |
36956a7d BP |
1408 | if (key_len) { |
1409 | const struct nlattr *a; | |
1410 | unsigned int left; | |
e6cc0bab AZ |
1411 | bool has_ethtype_key = false; |
1412 | const struct nlattr *ma = NULL; | |
dc8c5408 | 1413 | struct ofpbuf ofp; |
041e7168 | 1414 | bool first_field = true; |
36956a7d | 1415 | |
dc8c5408 | 1416 | ofpbuf_init(&ofp, 100); |
36956a7d | 1417 | NL_ATTR_FOR_EACH (a, left, key, key_len) { |
041e7168 AZ |
1418 | bool is_nested_attr; |
1419 | bool is_wildcard = false; | |
1420 | int attr_type = nl_attr_type(a); | |
1421 | ||
1422 | if (attr_type == OVS_KEY_ATTR_ETHERTYPE) { | |
e6cc0bab AZ |
1423 | has_ethtype_key = true; |
1424 | } | |
041e7168 AZ |
1425 | |
1426 | is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2); | |
1427 | ||
e6cc0bab AZ |
1428 | if (mask && mask_len) { |
1429 | ma = nl_attr_find__(mask, mask_len, nl_attr_type(a)); | |
041e7168 AZ |
1430 | is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true; |
1431 | } | |
1432 | ||
1433 | if (verbose || !is_wildcard || is_nested_attr) { | |
1434 | if (is_wildcard && !ma) { | |
dc8c5408 AZ |
1435 | ma = generate_all_wildcard_mask(&ofp, a); |
1436 | } | |
041e7168 AZ |
1437 | if (!first_field) { |
1438 | ds_put_char(ds, ','); | |
1439 | } | |
0a37839c | 1440 | format_odp_key_attr(a, ma, portno_names, ds, verbose); |
041e7168 | 1441 | first_field = false; |
e6cc0bab | 1442 | } |
dc8c5408 | 1443 | ofpbuf_clear(&ofp); |
36956a7d | 1444 | } |
dc8c5408 AZ |
1445 | ofpbuf_uninit(&ofp); |
1446 | ||
36956a7d | 1447 | if (left) { |
3a8cfc50 | 1448 | int i; |
1394054e | 1449 | |
36956a7d BP |
1450 | if (left == key_len) { |
1451 | ds_put_cstr(ds, "<empty>"); | |
1452 | } | |
3a8cfc50 BP |
1453 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
1454 | for (i = 0; i < left; i++) { | |
1455 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
1456 | } | |
1457 | ds_put_char(ds, ')'); | |
36956a7d | 1458 | } |
e6cc0bab AZ |
1459 | if (!has_ethtype_key) { |
1460 | ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE); | |
1461 | if (ma) { | |
1462 | ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")", | |
1463 | ntohs(nl_attr_get_be16(ma))); | |
1464 | } | |
1465 | } | |
36956a7d BP |
1466 | } else { |
1467 | ds_put_cstr(ds, "<empty>"); | |
1468 | } | |
14608a15 | 1469 | } |
064af421 | 1470 | |
e6cc0bab AZ |
1471 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
1472 | * OVS_KEY_ATTR_* attributes in 'key'. */ | |
1473 | void | |
1474 | odp_flow_key_format(const struct nlattr *key, | |
1475 | size_t key_len, struct ds *ds) | |
1476 | { | |
0a37839c | 1477 | odp_flow_format(key, key_len, NULL, 0, NULL, ds, true); |
e6cc0bab AZ |
1478 | } |
1479 | ||
dc8c5408 AZ |
1480 | static void |
1481 | put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll, | |
1482 | const uint8_t *nd_tll, struct ofpbuf *key) | |
1483 | { | |
1484 | if (nd_sll) { | |
1485 | memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN); | |
1486 | } | |
1487 | ||
1488 | if (nd_tll) { | |
1489 | memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN); | |
1490 | } | |
1491 | ||
1492 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key); | |
1493 | } | |
1494 | ||
3bffc610 | 1495 | static int |
dc8c5408 AZ |
1496 | put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll, |
1497 | const uint8_t *nd_tll, struct ofpbuf *key) | |
3bffc610 | 1498 | { |
df2c07f4 | 1499 | struct ovs_key_nd nd_key; |
3bffc610 BP |
1500 | |
1501 | memset(&nd_key, 0, sizeof nd_key); | |
dc8c5408 | 1502 | |
3bffc610 BP |
1503 | if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) { |
1504 | return -EINVAL; | |
1505 | } | |
dc8c5408 AZ |
1506 | |
1507 | put_nd(&nd_key, nd_sll, nd_tll, key); | |
1508 | return n; | |
1509 | } | |
1510 | ||
1511 | static int | |
1512 | put_nd_mask(int n, const char *nd_target_s, | |
1513 | const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask) | |
1514 | { | |
1515 | struct ovs_key_nd nd_mask; | |
1516 | ||
1517 | memset(&nd_mask, 0xff, sizeof nd_mask); | |
1518 | ||
1519 | if (strlen(nd_target_s) != 0 && | |
1520 | inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) { | |
1521 | return -EINVAL; | |
3bffc610 | 1522 | } |
dc8c5408 AZ |
1523 | |
1524 | put_nd(&nd_mask, nd_sll, nd_tll, mask); | |
3bffc610 BP |
1525 | return n; |
1526 | } | |
1527 | ||
7257b535 BP |
1528 | static bool |
1529 | ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type) | |
1530 | { | |
1531 | if (!strcasecmp(s, "no")) { | |
1532 | *type = OVS_FRAG_TYPE_NONE; | |
1533 | } else if (!strcasecmp(s, "first")) { | |
1534 | *type = OVS_FRAG_TYPE_FIRST; | |
1535 | } else if (!strcasecmp(s, "later")) { | |
1536 | *type = OVS_FRAG_TYPE_LATER; | |
1537 | } else { | |
1538 | return false; | |
1539 | } | |
1540 | return true; | |
1541 | } | |
1542 | ||
b02475c5 SH |
1543 | static ovs_be32 |
1544 | mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos) | |
1545 | { | |
1546 | return (htonl((mpls_label << MPLS_LABEL_SHIFT) | | |
1547 | (mpls_tc << MPLS_TC_SHIFT) | | |
1548 | (mpls_ttl << MPLS_TTL_SHIFT) | | |
1549 | (mpls_bos << MPLS_BOS_SHIFT))); | |
1550 | } | |
1551 | ||
3bffc610 | 1552 | static int |
e6cc0bab AZ |
1553 | parse_odp_key_mask_attr(const char *s, const struct simap *port_names, |
1554 | struct ofpbuf *key, struct ofpbuf *mask) | |
3bffc610 | 1555 | { |
abff858b | 1556 | { |
c2c28dfd BP |
1557 | uint32_t priority; |
1558 | uint32_t priority_mask; | |
abff858b PS |
1559 | int n = -1; |
1560 | ||
c2c28dfd BP |
1561 | if (mask && ovs_scan(s, "skb_priority(%"SCNi32"/%"SCNi32")%n", |
1562 | &priority, &priority_mask, &n)) { | |
e6cc0bab AZ |
1563 | nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority); |
1564 | nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask); | |
1565 | return n; | |
c2c28dfd | 1566 | } else if (ovs_scan(s, "skb_priority(%"SCNi32")%n", &priority, &n)) { |
abff858b | 1567 | nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority); |
e6cc0bab AZ |
1568 | if (mask) { |
1569 | nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX); | |
1570 | } | |
abff858b PS |
1571 | return n; |
1572 | } | |
1573 | } | |
1574 | ||
72e8bf28 | 1575 | { |
c2c28dfd BP |
1576 | uint32_t mark; |
1577 | uint32_t mark_mask; | |
72e8bf28 AA |
1578 | int n = -1; |
1579 | ||
c2c28dfd BP |
1580 | if (mask && ovs_scan(s, "skb_mark(%"SCNi32"/%"SCNi32")%n", &mark, |
1581 | &mark_mask, &n)) { | |
e6cc0bab AZ |
1582 | nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark); |
1583 | nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask); | |
1584 | return n; | |
c2c28dfd | 1585 | } else if (ovs_scan(s, "skb_mark(%"SCNi32")%n", &mark, &n)) { |
72e8bf28 | 1586 | nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark); |
e6cc0bab AZ |
1587 | if (mask) { |
1588 | nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX); | |
1589 | } | |
72e8bf28 AA |
1590 | return n; |
1591 | } | |
1592 | } | |
1593 | ||
35651d6a | 1594 | { |
c2c28dfd | 1595 | uint64_t tun_id, tun_id_mask; |
e6cc0bab | 1596 | struct flow_tnl tun_key, tun_key_mask; |
35651d6a JG |
1597 | int n = -1; |
1598 | ||
c2c28dfd BP |
1599 | if (mask && ovs_scan(s, "tunnel(tun_id=%"SCNi64"/%"SCNi64"," |
1600 | "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT | |
1601 | "/"IP_SCAN_FMT",tos=%"SCNi8"/%"SCNi8"," | |
1602 | "ttl=%"SCNi8"/%"SCNi8",flags%n", | |
1603 | &tun_id, &tun_id_mask, | |
1604 | IP_SCAN_ARGS(&tun_key.ip_src), | |
1605 | IP_SCAN_ARGS(&tun_key_mask.ip_src), | |
1606 | IP_SCAN_ARGS(&tun_key.ip_dst), | |
1607 | IP_SCAN_ARGS(&tun_key_mask.ip_dst), | |
1608 | &tun_key.ip_tos, &tun_key_mask.ip_tos, | |
1609 | &tun_key.ip_ttl, &tun_key_mask.ip_ttl, &n)) { | |
e6cc0bab AZ |
1610 | int res; |
1611 | uint32_t flags; | |
1612 | ||
c2c28dfd | 1613 | tun_key.tun_id = htonll(tun_id); |
e6cc0bab | 1614 | tun_key_mask.tun_id = htonll(tun_id_mask); |
e6cc0bab AZ |
1615 | res = parse_flags(&s[n], flow_tun_flag_to_string, &flags); |
1616 | tun_key.flags = flags; | |
1617 | tun_key_mask.flags = UINT16_MAX; | |
1618 | ||
1619 | if (res < 0) { | |
1620 | return res; | |
1621 | } | |
1622 | n += res; | |
1623 | if (s[n] != ')') { | |
1624 | return -EINVAL; | |
1625 | } | |
1626 | n++; | |
1627 | tun_key_to_attr(key, &tun_key); | |
1628 | if (mask) { | |
1629 | tun_key_to_attr(mask, &tun_key_mask); | |
1630 | } | |
1631 | return n; | |
c2c28dfd BP |
1632 | } else if (ovs_scan(s, "tunnel(tun_id=%"SCNi64"," |
1633 | "src="IP_SCAN_FMT",dst="IP_SCAN_FMT | |
1634 | ",tos=%"SCNi8",ttl=%"SCNi8",flags%n", &tun_id, | |
1635 | IP_SCAN_ARGS(&tun_key.ip_src), | |
1636 | IP_SCAN_ARGS(&tun_key.ip_dst), | |
1637 | &tun_key.ip_tos, &tun_key.ip_ttl, &n)) { | |
29c7a2b2 | 1638 | int res; |
9b405f1a | 1639 | uint32_t flags; |
29c7a2b2 | 1640 | |
c2c28dfd | 1641 | tun_key.tun_id = htonll(tun_id); |
9b405f1a | 1642 | res = parse_flags(&s[n], flow_tun_flag_to_string, &flags); |
e6cc0bab | 1643 | tun_key.flags = flags; |
29c7a2b2 | 1644 | |
29c7a2b2 PS |
1645 | if (res < 0) { |
1646 | return res; | |
1647 | } | |
1648 | n += res; | |
1649 | if (s[n] != ')') { | |
1650 | return -EINVAL; | |
1651 | } | |
1652 | n++; | |
9b405f1a | 1653 | tun_key_to_attr(key, &tun_key); |
e6cc0bab AZ |
1654 | |
1655 | if (mask) { | |
1656 | memset(&tun_key, 0xff, sizeof tun_key); | |
1657 | tun_key_to_attr(mask, &tun_key); | |
1658 | } | |
35651d6a JG |
1659 | return n; |
1660 | } | |
1661 | } | |
1662 | ||
3bffc610 | 1663 | { |
c2c28dfd BP |
1664 | uint32_t in_port; |
1665 | uint32_t in_port_mask; | |
3bffc610 BP |
1666 | int n = -1; |
1667 | ||
c2c28dfd BP |
1668 | if (mask && ovs_scan(s, "in_port(%"SCNi32"/%"SCNi32")%n", |
1669 | &in_port, &in_port_mask, &n)) { | |
e6cc0bab AZ |
1670 | nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port); |
1671 | nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask); | |
1672 | return n; | |
c2c28dfd | 1673 | } else if (ovs_scan(s, "in_port(%"SCNi32")%n", &in_port, &n)) { |
df2c07f4 | 1674 | nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port); |
e6cc0bab AZ |
1675 | if (mask) { |
1676 | nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX); | |
1677 | } | |
3bffc610 BP |
1678 | return n; |
1679 | } | |
1680 | } | |
1681 | ||
e6cc0bab | 1682 | |
b2a60db8 BP |
1683 | if (port_names && !strncmp(s, "in_port(", 8)) { |
1684 | const char *name; | |
44bac24b | 1685 | const struct simap_node *node; |
b2a60db8 BP |
1686 | int name_len; |
1687 | ||
1688 | name = s + 8; | |
c46c613a | 1689 | name_len = strcspn(name, ")"); |
44bac24b | 1690 | node = simap_find_len(port_names, name, name_len); |
b2a60db8 | 1691 | if (node) { |
44bac24b | 1692 | nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data); |
e6cc0bab AZ |
1693 | |
1694 | if (mask) { | |
1695 | nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX); | |
1696 | } | |
b2a60db8 BP |
1697 | return 8 + name_len + 1; |
1698 | } | |
1699 | } | |
1700 | ||
3bffc610 | 1701 | { |
df2c07f4 | 1702 | struct ovs_key_ethernet eth_key; |
e6cc0bab | 1703 | struct ovs_key_ethernet eth_key_mask; |
3bffc610 BP |
1704 | int n = -1; |
1705 | ||
c2c28dfd BP |
1706 | if (mask && ovs_scan(s, |
1707 | "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," | |
1708 | "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
1709 | ETH_ADDR_SCAN_ARGS(eth_key.eth_src), | |
1710 | ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src), | |
1711 | ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), | |
1712 | ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n)) { | |
e6cc0bab AZ |
1713 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET, |
1714 | ð_key, sizeof eth_key); | |
1715 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET, | |
1716 | ð_key_mask, sizeof eth_key_mask); | |
1717 | return n; | |
c2c28dfd BP |
1718 | } else if (ovs_scan(s, "eth(src="ETH_ADDR_SCAN_FMT"," |
1719 | "dst="ETH_ADDR_SCAN_FMT")%n", | |
1720 | ETH_ADDR_SCAN_ARGS(eth_key.eth_src), | |
1721 | ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n)) { | |
df2c07f4 | 1722 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET, |
3bffc610 | 1723 | ð_key, sizeof eth_key); |
e6cc0bab AZ |
1724 | |
1725 | if (mask) { | |
1726 | memset(ð_key, 0xff, sizeof eth_key); | |
1727 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET, | |
1728 | ð_key, sizeof eth_key); | |
1729 | } | |
3bffc610 BP |
1730 | return n; |
1731 | } | |
1732 | } | |
1733 | ||
1734 | { | |
c2c28dfd | 1735 | int vid, vid_mask; |
e6cc0bab AZ |
1736 | int pcp, pcp_mask; |
1737 | int cfi, cfi_mask; | |
3bffc610 BP |
1738 | int n = -1; |
1739 | ||
c2c28dfd BP |
1740 | if (mask && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i)%n", |
1741 | &vid, &vid_mask, &pcp, &pcp_mask, &n)) { | |
e6cc0bab AZ |
1742 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, |
1743 | htons((vid << VLAN_VID_SHIFT) | | |
1744 | (pcp << VLAN_PCP_SHIFT) | | |
1745 | VLAN_CFI)); | |
1746 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, | |
1747 | htons((vid_mask << VLAN_VID_SHIFT) | | |
1748 | (pcp_mask << VLAN_PCP_SHIFT) | | |
1749 | (1 << VLAN_CFI_SHIFT))); | |
1750 | return n; | |
c2c28dfd | 1751 | } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)) { |
fea393b1 BP |
1752 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, |
1753 | htons((vid << VLAN_VID_SHIFT) | | |
8ddc056d BP |
1754 | (pcp << VLAN_PCP_SHIFT) | |
1755 | VLAN_CFI)); | |
e6cc0bab | 1756 | if (mask) { |
b8266395 | 1757 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX); |
e6cc0bab AZ |
1758 | } |
1759 | return n; | |
c2c28dfd BP |
1760 | } else if (mask |
1761 | && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i,cfi=%i/%i)%n", | |
1762 | &vid, &vid_mask, &pcp, &pcp_mask, | |
1763 | &cfi, &cfi_mask, &n)) { | |
e6cc0bab AZ |
1764 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, |
1765 | htons((vid << VLAN_VID_SHIFT) | | |
1766 | (pcp << VLAN_PCP_SHIFT) | | |
1767 | (cfi ? VLAN_CFI : 0))); | |
1768 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, | |
1769 | htons((vid_mask << VLAN_VID_SHIFT) | | |
1770 | (pcp_mask << VLAN_PCP_SHIFT) | | |
1771 | (cfi_mask << VLAN_CFI_SHIFT))); | |
8ddc056d | 1772 | return n; |
c2c28dfd BP |
1773 | } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i,cfi=%i)%n", |
1774 | &vid, &pcp, &cfi, &n)) { | |
8ddc056d BP |
1775 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, |
1776 | htons((vid << VLAN_VID_SHIFT) | | |
1777 | (pcp << VLAN_PCP_SHIFT) | | |
1778 | (cfi ? VLAN_CFI : 0))); | |
e6cc0bab | 1779 | if (mask) { |
b8266395 | 1780 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX); |
e6cc0bab | 1781 | } |
3bffc610 BP |
1782 | return n; |
1783 | } | |
1784 | } | |
1785 | ||
1786 | { | |
fac2e516 | 1787 | int eth_type; |
e6cc0bab | 1788 | int eth_type_mask; |
3bffc610 BP |
1789 | int n = -1; |
1790 | ||
c2c28dfd BP |
1791 | if (mask && ovs_scan(s, "eth_type(%i/%i)%n", |
1792 | ð_type, ð_type_mask, &n)) { | |
e6cc0bab AZ |
1793 | if (eth_type != 0) { |
1794 | nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type)); | |
1795 | } | |
1796 | nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask)); | |
1797 | return n; | |
c2c28dfd | 1798 | } else if (ovs_scan(s, "eth_type(%i)%n", ð_type, &n)) { |
df2c07f4 | 1799 | nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type)); |
e6cc0bab | 1800 | if (mask) { |
b8266395 | 1801 | nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
e6cc0bab | 1802 | } |
3bffc610 BP |
1803 | return n; |
1804 | } | |
1805 | } | |
1806 | ||
b02475c5 SH |
1807 | { |
1808 | int label, tc, ttl, bos; | |
e6cc0bab | 1809 | int label_mask, tc_mask, ttl_mask, bos_mask; |
b02475c5 SH |
1810 | int n = -1; |
1811 | ||
c2c28dfd BP |
1812 | if (mask && ovs_scan(s, "mpls(label=%i/%i,tc=%i/%i," |
1813 | "ttl=%i/%i,bos=%i/%i)%n", | |
1814 | &label, &label_mask, &tc, &tc_mask, | |
1815 | &ttl, &ttl_mask, &bos, &bos_mask, &n)) { | |
e6cc0bab AZ |
1816 | struct ovs_key_mpls *mpls, *mpls_mask; |
1817 | ||
1818 | mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS, | |
1819 | sizeof *mpls); | |
1820 | mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos); | |
1821 | ||
1822 | mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS, | |
1823 | sizeof *mpls_mask); | |
1824 | mpls_mask->mpls_lse = mpls_lse_from_components( | |
1825 | label_mask, tc_mask, ttl_mask, bos_mask); | |
1826 | return n; | |
c2c28dfd BP |
1827 | } else if (ovs_scan(s, "mpls(label=%i,tc=%i,ttl=%i,bos=%i)%n", |
1828 | &label, &tc, &ttl, &bos, &n)) { | |
b02475c5 SH |
1829 | struct ovs_key_mpls *mpls; |
1830 | ||
1831 | mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS, | |
1832 | sizeof *mpls); | |
20131cef | 1833 | mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos); |
e6cc0bab AZ |
1834 | if (mask) { |
1835 | mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS, | |
1836 | sizeof *mpls); | |
b8266395 | 1837 | mpls->mpls_lse = OVS_BE32_MAX; |
e6cc0bab | 1838 | } |
b02475c5 SH |
1839 | return n; |
1840 | } | |
1841 | } | |
1842 | ||
e6cc0bab | 1843 | |
3bffc610 | 1844 | { |
c2c28dfd BP |
1845 | struct ovs_key_ipv4 ipv4_key; |
1846 | struct ovs_key_ipv4 ipv4_mask; | |
1847 | ||
7257b535 BP |
1848 | char frag[8]; |
1849 | enum ovs_frag_type ipv4_frag; | |
3bffc610 BP |
1850 | int n = -1; |
1851 | ||
c2c28dfd BP |
1852 | if (mask |
1853 | && ovs_scan(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT"," | |
1854 | "dst="IP_SCAN_FMT"/"IP_SCAN_FMT"," | |
1855 | "proto=%"SCNi8"/%"SCNi8"," | |
1856 | "tos=%"SCNi8"/%"SCNi8"," | |
1857 | "ttl=%"SCNi8"/%"SCNi8"," | |
1858 | "frag=%7[a-z]/%"SCNi8")%n", | |
1859 | IP_SCAN_ARGS(&ipv4_key.ipv4_src), | |
1860 | IP_SCAN_ARGS(&ipv4_mask.ipv4_src), | |
1861 | IP_SCAN_ARGS(&ipv4_key.ipv4_dst), | |
1862 | IP_SCAN_ARGS(&ipv4_mask.ipv4_dst), | |
1863 | &ipv4_key.ipv4_proto, &ipv4_mask.ipv4_proto, | |
1864 | &ipv4_key.ipv4_tos, &ipv4_mask.ipv4_tos, | |
1865 | &ipv4_key.ipv4_ttl, &ipv4_mask.ipv4_ttl, | |
1866 | frag, &ipv4_mask.ipv4_frag, &n) | |
e6cc0bab | 1867 | && ovs_frag_type_from_string(frag, &ipv4_frag)) { |
e6cc0bab AZ |
1868 | ipv4_key.ipv4_frag = ipv4_frag; |
1869 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4, | |
1870 | &ipv4_key, sizeof ipv4_key); | |
1871 | ||
e6cc0bab AZ |
1872 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4, |
1873 | &ipv4_mask, sizeof ipv4_mask); | |
1874 | return n; | |
c2c28dfd BP |
1875 | } else if (ovs_scan(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT"," |
1876 | "proto=%"SCNi8",tos=%"SCNi8",ttl=%"SCNi8"," | |
1877 | "frag=%7[a-z])%n", | |
1878 | IP_SCAN_ARGS(&ipv4_key.ipv4_src), | |
1879 | IP_SCAN_ARGS(&ipv4_key.ipv4_dst), | |
1880 | &ipv4_key.ipv4_proto, | |
1881 | &ipv4_key.ipv4_tos, | |
1882 | &ipv4_key.ipv4_ttl, | |
1883 | frag, &n) > 0 | |
1884 | && ovs_frag_type_from_string(frag, &ipv4_frag)) { | |
7257b535 | 1885 | ipv4_key.ipv4_frag = ipv4_frag; |
df2c07f4 | 1886 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4, |
3bffc610 | 1887 | &ipv4_key, sizeof ipv4_key); |
e6cc0bab AZ |
1888 | |
1889 | if (mask) { | |
1890 | memset(&ipv4_key, 0xff, sizeof ipv4_key); | |
1891 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4, | |
1892 | &ipv4_key, sizeof ipv4_key); | |
1893 | } | |
3bffc610 BP |
1894 | return n; |
1895 | } | |
1896 | } | |
1897 | ||
1898 | { | |
1899 | char ipv6_src_s[IPV6_SCAN_LEN + 1]; | |
e6cc0bab | 1900 | char ipv6_src_mask_s[IPV6_SCAN_LEN + 1]; |
3bffc610 | 1901 | char ipv6_dst_s[IPV6_SCAN_LEN + 1]; |
e6cc0bab AZ |
1902 | char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1]; |
1903 | int ipv6_label, ipv6_label_mask; | |
1904 | int ipv6_proto, ipv6_proto_mask; | |
1905 | int ipv6_tclass, ipv6_tclass_mask; | |
1906 | int ipv6_hlimit, ipv6_hlimit_mask; | |
7257b535 BP |
1907 | char frag[8]; |
1908 | enum ovs_frag_type ipv6_frag; | |
e6cc0bab | 1909 | int ipv6_frag_mask; |
3bffc610 BP |
1910 | int n = -1; |
1911 | ||
c2c28dfd BP |
1912 | if (mask && ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst=" |
1913 | IPV6_SCAN_FMT"/"IPV6_SCAN_FMT"," | |
1914 | "label=%i/%i,proto=%i/%i,tclass=%i/%i," | |
1915 | "hlimit=%i/%i,frag=%7[a-z]/%i)%n", | |
1916 | ipv6_src_s, ipv6_src_mask_s, | |
1917 | ipv6_dst_s, ipv6_dst_mask_s, | |
1918 | &ipv6_label, &ipv6_label_mask, &ipv6_proto, | |
1919 | &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask, | |
1920 | &ipv6_hlimit, &ipv6_hlimit_mask, frag, | |
1921 | &ipv6_frag_mask, &n) | |
e6cc0bab AZ |
1922 | && ovs_frag_type_from_string(frag, &ipv6_frag)) { |
1923 | struct ovs_key_ipv6 ipv6_key; | |
1924 | struct ovs_key_ipv6 ipv6_mask; | |
1925 | ||
1926 | if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 || | |
1927 | inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 || | |
1928 | inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 || | |
1929 | inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) { | |
1930 | return -EINVAL; | |
1931 | } | |
1932 | ||
1933 | ipv6_key.ipv6_label = htonl(ipv6_label); | |
1934 | ipv6_key.ipv6_proto = ipv6_proto; | |
1935 | ipv6_key.ipv6_tclass = ipv6_tclass; | |
1936 | ipv6_key.ipv6_hlimit = ipv6_hlimit; | |
1937 | ipv6_key.ipv6_frag = ipv6_frag; | |
1938 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6, | |
1939 | &ipv6_key, sizeof ipv6_key); | |
1940 | ||
1941 | ipv6_mask.ipv6_label = htonl(ipv6_label_mask); | |
1942 | ipv6_mask.ipv6_proto = ipv6_proto_mask; | |
1943 | ipv6_mask.ipv6_tclass = ipv6_tclass_mask; | |
1944 | ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask; | |
1945 | ipv6_mask.ipv6_frag = ipv6_frag_mask; | |
1946 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6, | |
1947 | &ipv6_mask, sizeof ipv6_mask); | |
1948 | return n; | |
c2c28dfd BP |
1949 | } else if (ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT"," |
1950 | "label=%i,proto=%i,tclass=%i,hlimit=%i," | |
1951 | "frag=%7[a-z])%n", | |
1952 | ipv6_src_s, ipv6_dst_s, &ipv6_label, | |
1953 | &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) | |
1954 | && ovs_frag_type_from_string(frag, &ipv6_frag)) { | |
df2c07f4 | 1955 | struct ovs_key_ipv6 ipv6_key; |
3bffc610 | 1956 | |
3bffc610 BP |
1957 | if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 || |
1958 | inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) { | |
1959 | return -EINVAL; | |
1960 | } | |
fa8223b7 | 1961 | ipv6_key.ipv6_label = htonl(ipv6_label); |
3bffc610 | 1962 | ipv6_key.ipv6_proto = ipv6_proto; |
60258dcb | 1963 | ipv6_key.ipv6_tclass = ipv6_tclass; |
a61680c6 | 1964 | ipv6_key.ipv6_hlimit = ipv6_hlimit; |
7257b535 | 1965 | ipv6_key.ipv6_frag = ipv6_frag; |
df2c07f4 | 1966 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6, |
3bffc610 | 1967 | &ipv6_key, sizeof ipv6_key); |
e6cc0bab AZ |
1968 | |
1969 | if (mask) { | |
1970 | memset(&ipv6_key, 0xff, sizeof ipv6_key); | |
1971 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6, | |
1972 | &ipv6_key, sizeof ipv6_key); | |
1973 | } | |
3bffc610 BP |
1974 | return n; |
1975 | } | |
1976 | } | |
1977 | ||
1978 | { | |
1979 | int tcp_src; | |
1980 | int tcp_dst; | |
e6cc0bab AZ |
1981 | int tcp_src_mask; |
1982 | int tcp_dst_mask; | |
3bffc610 BP |
1983 | int n = -1; |
1984 | ||
c2c28dfd BP |
1985 | if (mask && ovs_scan(s, "tcp(src=%i/%i,dst=%i/%i)%n", |
1986 | &tcp_src, &tcp_src_mask, &tcp_dst, | |
1987 | &tcp_dst_mask, &n)) { | |
df2c07f4 | 1988 | struct ovs_key_tcp tcp_key; |
e6cc0bab | 1989 | struct ovs_key_tcp tcp_mask; |
3bffc610 BP |
1990 | |
1991 | tcp_key.tcp_src = htons(tcp_src); | |
1992 | tcp_key.tcp_dst = htons(tcp_dst); | |
df2c07f4 | 1993 | nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key); |
e6cc0bab AZ |
1994 | |
1995 | tcp_mask.tcp_src = htons(tcp_src_mask); | |
1996 | tcp_mask.tcp_dst = htons(tcp_dst_mask); | |
1997 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP, | |
1998 | &tcp_mask, sizeof tcp_mask); | |
1999 | return n; | |
c2c28dfd BP |
2000 | } else if (ovs_scan(s, "tcp(src=%i,dst=%i)%n", |
2001 | &tcp_src, &tcp_dst, &n)) { | |
e6cc0bab AZ |
2002 | struct ovs_key_tcp tcp_key; |
2003 | ||
2004 | tcp_key.tcp_src = htons(tcp_src); | |
2005 | tcp_key.tcp_dst = htons(tcp_dst); | |
2006 | nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key); | |
2007 | ||
2008 | if (mask) { | |
2009 | memset(&tcp_key, 0xff, sizeof tcp_key); | |
2010 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP, | |
2011 | &tcp_key, sizeof tcp_key); | |
2012 | } | |
3bffc610 BP |
2013 | return n; |
2014 | } | |
2015 | } | |
2016 | ||
dc235f7f JR |
2017 | { |
2018 | uint16_t tcp_flags, tcp_flags_mask; | |
2019 | int n = -1; | |
2020 | ||
c2c28dfd BP |
2021 | if (mask && ovs_scan(s, "tcp_flags(%"SCNi16"/%"SCNi16")%n", |
2022 | &tcp_flags, &tcp_flags_mask, &n) > 0 && n > 0) { | |
dc235f7f JR |
2023 | nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags)); |
2024 | nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags_mask)); | |
2025 | return n; | |
c2c28dfd | 2026 | } else if (ovs_scan(s, "tcp_flags(%"SCNi16")%n", &tcp_flags, &n)) { |
dc235f7f JR |
2027 | nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags)); |
2028 | if (mask) { | |
2029 | nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, | |
2030 | htons(UINT16_MAX)); | |
2031 | } | |
2032 | return n; | |
2033 | } | |
2034 | } | |
2035 | ||
3bffc610 BP |
2036 | { |
2037 | int udp_src; | |
2038 | int udp_dst; | |
e6cc0bab AZ |
2039 | int udp_src_mask; |
2040 | int udp_dst_mask; | |
3bffc610 BP |
2041 | int n = -1; |
2042 | ||
c2c28dfd BP |
2043 | if (mask && ovs_scan(s, "udp(src=%i/%i,dst=%i/%i)%n", |
2044 | &udp_src, &udp_src_mask, | |
2045 | &udp_dst, &udp_dst_mask, &n)) { | |
e6cc0bab AZ |
2046 | struct ovs_key_udp udp_key; |
2047 | struct ovs_key_udp udp_mask; | |
2048 | ||
2049 | udp_key.udp_src = htons(udp_src); | |
2050 | udp_key.udp_dst = htons(udp_dst); | |
2051 | nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key); | |
2052 | ||
2053 | udp_mask.udp_src = htons(udp_src_mask); | |
2054 | udp_mask.udp_dst = htons(udp_dst_mask); | |
2055 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, | |
2056 | &udp_mask, sizeof udp_mask); | |
2057 | return n; | |
2058 | } | |
c2c28dfd | 2059 | if (ovs_scan(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n)) { |
df2c07f4 | 2060 | struct ovs_key_udp udp_key; |
3bffc610 BP |
2061 | |
2062 | udp_key.udp_src = htons(udp_src); | |
2063 | udp_key.udp_dst = htons(udp_dst); | |
df2c07f4 | 2064 | nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key); |
e6cc0bab AZ |
2065 | |
2066 | if (mask) { | |
2067 | memset(&udp_key, 0xff, sizeof udp_key); | |
2068 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key); | |
2069 | } | |
3bffc610 BP |
2070 | return n; |
2071 | } | |
2072 | } | |
2073 | ||
c6bcb685 JS |
2074 | { |
2075 | int sctp_src; | |
2076 | int sctp_dst; | |
2077 | int sctp_src_mask; | |
2078 | int sctp_dst_mask; | |
2079 | int n = -1; | |
2080 | ||
c2c28dfd BP |
2081 | if (mask && ovs_scan(s, "sctp(src=%i/%i,dst=%i/%i)%n", |
2082 | &sctp_src, &sctp_src_mask, | |
2083 | &sctp_dst, &sctp_dst_mask, &n)) { | |
c6bcb685 JS |
2084 | struct ovs_key_sctp sctp_key; |
2085 | struct ovs_key_sctp sctp_mask; | |
2086 | ||
2087 | sctp_key.sctp_src = htons(sctp_src); | |
2088 | sctp_key.sctp_dst = htons(sctp_dst); | |
2089 | nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key); | |
2090 | ||
2091 | sctp_mask.sctp_src = htons(sctp_src_mask); | |
2092 | sctp_mask.sctp_dst = htons(sctp_dst_mask); | |
2093 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, | |
2094 | &sctp_mask, sizeof sctp_mask); | |
2095 | return n; | |
2096 | } | |
c2c28dfd | 2097 | if (ovs_scan(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n)) { |
c6bcb685 JS |
2098 | struct ovs_key_sctp sctp_key; |
2099 | ||
2100 | sctp_key.sctp_src = htons(sctp_src); | |
2101 | sctp_key.sctp_dst = htons(sctp_dst); | |
2102 | nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key); | |
2103 | ||
2104 | if (mask) { | |
2105 | memset(&sctp_key, 0xff, sizeof sctp_key); | |
2106 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key); | |
2107 | } | |
2108 | return n; | |
2109 | } | |
2110 | } | |
2111 | ||
3bffc610 | 2112 | { |
c2c28dfd BP |
2113 | struct ovs_key_icmp icmp_key; |
2114 | struct ovs_key_icmp icmp_mask; | |
3bffc610 BP |
2115 | int n = -1; |
2116 | ||
c2c28dfd BP |
2117 | if (mask && ovs_scan(s, "icmp(type=%"SCNi8"/%"SCNi8"," |
2118 | "code=%"SCNi8"/%"SCNi8")%n", | |
2119 | &icmp_key.icmp_type, &icmp_mask.icmp_type, | |
2120 | &icmp_key.icmp_code, &icmp_mask.icmp_code, &n)) { | |
e6cc0bab AZ |
2121 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP, |
2122 | &icmp_key, sizeof icmp_key); | |
e6cc0bab AZ |
2123 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, |
2124 | &icmp_mask, sizeof icmp_mask); | |
2125 | return n; | |
c2c28dfd BP |
2126 | } else if (ovs_scan(s, "icmp(type=%"SCNi8",code=%"SCNi8")%n", |
2127 | &icmp_key.icmp_type, &icmp_key.icmp_code, &n)) { | |
df2c07f4 | 2128 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP, |
3bffc610 | 2129 | &icmp_key, sizeof icmp_key); |
e6cc0bab AZ |
2130 | if (mask) { |
2131 | memset(&icmp_key, 0xff, sizeof icmp_key); | |
2132 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key, | |
2133 | sizeof icmp_key); | |
2134 | } | |
3bffc610 BP |
2135 | return n; |
2136 | } | |
2137 | } | |
2138 | ||
2139 | { | |
df2c07f4 | 2140 | struct ovs_key_icmpv6 icmpv6_key; |
e6cc0bab | 2141 | struct ovs_key_icmpv6 icmpv6_mask; |
3bffc610 BP |
2142 | int n = -1; |
2143 | ||
c2c28dfd BP |
2144 | if (mask && ovs_scan(s, "icmpv6(type=%"SCNi8"/%"SCNi8"," |
2145 | "code=%"SCNi8"/%"SCNi8")%n", | |
2146 | &icmpv6_key.icmpv6_type, &icmpv6_mask.icmpv6_type, | |
2147 | &icmpv6_key.icmpv6_code, &icmpv6_mask.icmpv6_code, | |
2148 | &n)) { | |
e6cc0bab AZ |
2149 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6, |
2150 | &icmpv6_key, sizeof icmpv6_key); | |
e6cc0bab AZ |
2151 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask, |
2152 | sizeof icmpv6_mask); | |
2153 | return n; | |
c2c28dfd BP |
2154 | } else if (ovs_scan(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n", |
2155 | &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code, | |
2156 | &n)) { | |
df2c07f4 | 2157 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6, |
3bffc610 | 2158 | &icmpv6_key, sizeof icmpv6_key); |
e6cc0bab AZ |
2159 | |
2160 | if (mask) { | |
2161 | memset(&icmpv6_key, 0xff, sizeof icmpv6_key); | |
2162 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key, | |
2163 | sizeof icmpv6_key); | |
2164 | } | |
3bffc610 BP |
2165 | return n; |
2166 | } | |
2167 | } | |
2168 | ||
2169 | { | |
c2c28dfd BP |
2170 | struct ovs_key_arp arp_key; |
2171 | struct ovs_key_arp arp_mask; | |
2172 | uint16_t arp_op, arp_op_mask; | |
3bffc610 BP |
2173 | int n = -1; |
2174 | ||
c2c28dfd BP |
2175 | if (mask && ovs_scan(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT"," |
2176 | "tip="IP_SCAN_FMT"/"IP_SCAN_FMT"," | |
2177 | "op=%"SCNi16"/%"SCNi16"," | |
2178 | "sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," | |
2179 | "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2180 | IP_SCAN_ARGS(&arp_key.arp_sip), | |
2181 | IP_SCAN_ARGS(&arp_mask.arp_sip), | |
2182 | IP_SCAN_ARGS(&arp_key.arp_tip), | |
2183 | IP_SCAN_ARGS(&arp_mask.arp_tip), | |
2184 | &arp_op, &arp_op_mask, | |
2185 | ETH_ADDR_SCAN_ARGS(arp_key.arp_sha), | |
2186 | ETH_ADDR_SCAN_ARGS(arp_mask.arp_sha), | |
2187 | ETH_ADDR_SCAN_ARGS(arp_key.arp_tha), | |
2188 | ETH_ADDR_SCAN_ARGS(arp_mask.arp_tha), &n)) { | |
e6cc0bab | 2189 | arp_key.arp_op = htons(arp_op); |
e6cc0bab | 2190 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); |
e6cc0bab | 2191 | arp_mask.arp_op = htons(arp_op_mask); |
e6cc0bab AZ |
2192 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP, |
2193 | &arp_mask, sizeof arp_mask); | |
2194 | return n; | |
c2c28dfd BP |
2195 | } else if (ovs_scan(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT"," |
2196 | "op=%"SCNi16",sha="ETH_ADDR_SCAN_FMT"," | |
2197 | "tha="ETH_ADDR_SCAN_FMT")%n", | |
2198 | IP_SCAN_ARGS(&arp_key.arp_sip), | |
2199 | IP_SCAN_ARGS(&arp_key.arp_tip), | |
2200 | &arp_op, | |
2201 | ETH_ADDR_SCAN_ARGS(arp_key.arp_sha), | |
2202 | ETH_ADDR_SCAN_ARGS(arp_key.arp_tha), &n)) { | |
3bffc610 | 2203 | arp_key.arp_op = htons(arp_op); |
df2c07f4 | 2204 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); |
e6cc0bab AZ |
2205 | |
2206 | if (mask) { | |
2207 | memset(&arp_key, 0xff, sizeof arp_key); | |
2208 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP, | |
2209 | &arp_key, sizeof arp_key); | |
2210 | } | |
3bffc610 BP |
2211 | return n; |
2212 | } | |
2213 | } | |
2214 | ||
2215 | { | |
2216 | char nd_target_s[IPV6_SCAN_LEN + 1]; | |
e6cc0bab | 2217 | char nd_target_mask_s[IPV6_SCAN_LEN + 1]; |
3bffc610 | 2218 | uint8_t nd_sll[ETH_ADDR_LEN]; |
e6cc0bab | 2219 | uint8_t nd_sll_mask[ETH_ADDR_LEN]; |
3bffc610 | 2220 | uint8_t nd_tll[ETH_ADDR_LEN]; |
e6cc0bab | 2221 | uint8_t nd_tll_mask[ETH_ADDR_LEN]; |
3bffc610 BP |
2222 | int n = -1; |
2223 | ||
dc8c5408 AZ |
2224 | nd_target_mask_s[0] = 0; |
2225 | memset(nd_sll_mask, 0xff, sizeof nd_sll_mask); | |
2226 | memset(nd_tll_mask, 0xff, sizeof nd_tll_mask); | |
e6cc0bab | 2227 | |
c2c28dfd BP |
2228 | if (mask && ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n", |
2229 | nd_target_s, nd_target_mask_s, &n)) { | |
e6cc0bab | 2230 | put_nd_key(n, nd_target_s, NULL, NULL, key); |
dc8c5408 | 2231 | put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask); |
c2c28dfd BP |
2232 | } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT")%n", |
2233 | nd_target_s, &n)) { | |
e6cc0bab AZ |
2234 | put_nd_key(n, nd_target_s, NULL, NULL, key); |
2235 | if (mask) { | |
dc8c5408 | 2236 | put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask); |
e6cc0bab | 2237 | } |
c2c28dfd BP |
2238 | } else if (mask && |
2239 | ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT | |
2240 | ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2241 | nd_target_s, nd_target_mask_s, | |
2242 | ETH_ADDR_SCAN_ARGS(nd_sll), | |
2243 | ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n)) { | |
e6cc0bab | 2244 | put_nd_key(n, nd_target_s, nd_sll, NULL, key); |
dc8c5408 | 2245 | put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask); |
c2c28dfd BP |
2246 | } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT"," |
2247 | "sll="ETH_ADDR_SCAN_FMT")%n", | |
2248 | nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n)) { | |
e6cc0bab AZ |
2249 | put_nd_key(n, nd_target_s, nd_sll, NULL, key); |
2250 | if (mask) { | |
dc8c5408 | 2251 | put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask); |
e6cc0bab | 2252 | } |
c2c28dfd BP |
2253 | } else if (mask && |
2254 | ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT | |
2255 | ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2256 | nd_target_s, nd_target_mask_s, | |
2257 | ETH_ADDR_SCAN_ARGS(nd_tll), | |
2258 | ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n)) { | |
e6cc0bab | 2259 | put_nd_key(n, nd_target_s, NULL, nd_tll, key); |
dc8c5408 | 2260 | put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask); |
c2c28dfd BP |
2261 | } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT"," |
2262 | "tll="ETH_ADDR_SCAN_FMT")%n", | |
2263 | nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n)) { | |
e6cc0bab AZ |
2264 | put_nd_key(n, nd_target_s, NULL, nd_tll, key); |
2265 | if (mask) { | |
dc8c5408 | 2266 | put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask); |
e6cc0bab | 2267 | } |
c2c28dfd BP |
2268 | } else if (mask && |
2269 | ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT | |
2270 | ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," | |
2271 | "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2272 | nd_target_s, nd_target_mask_s, | |
2273 | ETH_ADDR_SCAN_ARGS(nd_sll), | |
2274 | ETH_ADDR_SCAN_ARGS(nd_sll_mask), | |
2275 | ETH_ADDR_SCAN_ARGS(nd_tll), | |
2276 | ETH_ADDR_SCAN_ARGS(nd_tll_mask), | |
2277 | &n)) { | |
e6cc0bab | 2278 | put_nd_key(n, nd_target_s, nd_sll, nd_tll, key); |
dc8c5408 | 2279 | put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask); |
c2c28dfd BP |
2280 | } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT"," |
2281 | "sll="ETH_ADDR_SCAN_FMT"," | |
2282 | "tll="ETH_ADDR_SCAN_FMT")%n", | |
2283 | nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), | |
2284 | ETH_ADDR_SCAN_ARGS(nd_tll), &n)) { | |
e6cc0bab AZ |
2285 | put_nd_key(n, nd_target_s, nd_sll, nd_tll, key); |
2286 | if (mask) { | |
dc8c5408 AZ |
2287 | put_nd_mask(n, nd_target_mask_s, |
2288 | nd_sll_mask, nd_tll_mask, mask); | |
e6cc0bab | 2289 | } |
3bffc610 | 2290 | } |
e6cc0bab AZ |
2291 | |
2292 | if (n != -1) | |
2293 | return n; | |
2294 | ||
3bffc610 BP |
2295 | } |
2296 | ||
fea393b1 BP |
2297 | if (!strncmp(s, "encap(", 6)) { |
2298 | const char *start = s; | |
e6cc0bab | 2299 | size_t encap, encap_mask = 0; |
fea393b1 BP |
2300 | |
2301 | encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP); | |
e6cc0bab AZ |
2302 | if (mask) { |
2303 | encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP); | |
2304 | } | |
fea393b1 BP |
2305 | |
2306 | s += 6; | |
2307 | for (;;) { | |
2308 | int retval; | |
2309 | ||
2310 | s += strspn(s, ", \t\r\n"); | |
2311 | if (!*s) { | |
2312 | return -EINVAL; | |
2313 | } else if (*s == ')') { | |
2314 | break; | |
2315 | } | |
2316 | ||
e6cc0bab | 2317 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
fea393b1 BP |
2318 | if (retval < 0) { |
2319 | return retval; | |
2320 | } | |
2321 | s += retval; | |
2322 | } | |
2323 | s++; | |
2324 | ||
2325 | nl_msg_end_nested(key, encap); | |
e6cc0bab AZ |
2326 | if (mask) { |
2327 | nl_msg_end_nested(mask, encap_mask); | |
2328 | } | |
fea393b1 BP |
2329 | |
2330 | return s - start; | |
2331 | } | |
2332 | ||
3bffc610 BP |
2333 | return -EINVAL; |
2334 | } | |
2335 | ||
df2c07f4 JP |
2336 | /* Parses the string representation of a datapath flow key, in the |
2337 | * format output by odp_flow_key_format(). Returns 0 if successful, | |
2338 | * otherwise a positive errno value. On success, the flow key is | |
2339 | * appended to 'key' as a series of Netlink attributes. On failure, no | |
2340 | * data is appended to 'key'. Either way, 'key''s data might be | |
2341 | * reallocated. | |
3bffc610 | 2342 | * |
44bac24b BP |
2343 | * If 'port_names' is nonnull, it points to an simap that maps from a port name |
2344 | * to a port number. (Port names may be used instead of port numbers in | |
2345 | * in_port.) | |
b2a60db8 | 2346 | * |
3bffc610 BP |
2347 | * On success, the attributes appended to 'key' are individually syntactically |
2348 | * valid, but they may not be valid as a sequence. 'key' might, for example, | |
34118cae | 2349 | * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */ |
3bffc610 | 2350 | int |
e6cc0bab AZ |
2351 | odp_flow_from_string(const char *s, const struct simap *port_names, |
2352 | struct ofpbuf *key, struct ofpbuf *mask) | |
3bffc610 BP |
2353 | { |
2354 | const size_t old_size = key->size; | |
2355 | for (;;) { | |
2356 | int retval; | |
2357 | ||
7202cbe5 | 2358 | s += strspn(s, delimiters); |
3bffc610 BP |
2359 | if (!*s) { |
2360 | return 0; | |
2361 | } | |
2362 | ||
e6cc0bab | 2363 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
3bffc610 BP |
2364 | if (retval < 0) { |
2365 | key->size = old_size; | |
2366 | return -retval; | |
2367 | } | |
2368 | s += retval; | |
2369 | } | |
2370 | ||
2371 | return 0; | |
2372 | } | |
2373 | ||
7257b535 | 2374 | static uint8_t |
c4f2731d | 2375 | ovs_to_odp_frag(uint8_t nw_frag) |
7257b535 | 2376 | { |
c4f2731d PS |
2377 | return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE |
2378 | : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST | |
2379 | : OVS_FRAG_TYPE_LATER); | |
7257b535 BP |
2380 | } |
2381 | ||
431495b1 AZ |
2382 | static uint8_t |
2383 | ovs_to_odp_frag_mask(uint8_t nw_frag_mask) | |
2384 | { | |
2385 | uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER); | |
2386 | ||
2387 | frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0; | |
2388 | frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0; | |
2389 | ||
2390 | return frag_mask; | |
2391 | } | |
2392 | ||
661cbcd5 JP |
2393 | static void |
2394 | odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, | |
2395 | const struct flow *flow, odp_port_t odp_in_port) | |
14608a15 | 2396 | { |
661cbcd5 | 2397 | bool is_mask; |
df2c07f4 | 2398 | struct ovs_key_ethernet *eth_key; |
fea393b1 | 2399 | size_t encap; |
36956a7d | 2400 | |
661cbcd5 JP |
2401 | /* We assume that if 'data' and 'flow' are not the same, we should |
2402 | * treat 'data' as a mask. */ | |
2403 | is_mask = (data != flow); | |
2404 | ||
54bb0348 | 2405 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority); |
abff858b | 2406 | |
01fcdfc6 | 2407 | if (flow->tunnel.ip_dst || is_mask) { |
661cbcd5 | 2408 | tun_key_to_attr(buf, &data->tunnel); |
36956a7d BP |
2409 | } |
2410 | ||
1362e248 | 2411 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark); |
72e8bf28 | 2412 | |
661cbcd5 JP |
2413 | /* Add an ingress port attribute if this is a mask or 'odp_in_port' |
2414 | * is not the magical value "ODPP_NONE". */ | |
2415 | if (is_mask || odp_in_port != ODPP_NONE) { | |
4e022ec0 | 2416 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port); |
18886b60 | 2417 | } |
36956a7d | 2418 | |
df2c07f4 | 2419 | eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET, |
36956a7d | 2420 | sizeof *eth_key); |
661cbcd5 JP |
2421 | memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN); |
2422 | memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN); | |
36956a7d | 2423 | |
8ddc056d | 2424 | if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) { |
bed7d6a1 | 2425 | if (is_mask) { |
b8266395 | 2426 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
bed7d6a1 JP |
2427 | } else { |
2428 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN)); | |
2429 | } | |
661cbcd5 | 2430 | nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci); |
fea393b1 | 2431 | encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP); |
8ddc056d BP |
2432 | if (flow->vlan_tci == htons(0)) { |
2433 | goto unencap; | |
2434 | } | |
fea393b1 BP |
2435 | } else { |
2436 | encap = 0; | |
36956a7d BP |
2437 | } |
2438 | ||
2439 | if (ntohs(flow->dl_type) < ETH_TYPE_MIN) { | |
661cbcd5 JP |
2440 | /* For backwards compatibility with kernels that don't support |
2441 | * wildcarding, the following convention is used to encode the | |
2442 | * OVS_KEY_ATTR_ETHERTYPE for key and mask: | |
2443 | * | |
2444 | * key mask matches | |
2445 | * -------- -------- ------- | |
2446 | * >0x5ff 0xffff Specified Ethernet II Ethertype. | |
2447 | * >0x5ff 0 Any Ethernet II or non-Ethernet II frame. | |
2448 | * <none> 0xffff Any non-Ethernet II frame (except valid | |
2449 | * 802.3 SNAP packet with valid eth_type). | |
2450 | */ | |
2451 | if (is_mask) { | |
b8266395 | 2452 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
661cbcd5 | 2453 | } |
fea393b1 | 2454 | goto unencap; |
36956a7d BP |
2455 | } |
2456 | ||
661cbcd5 | 2457 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type); |
36956a7d BP |
2458 | |
2459 | if (flow->dl_type == htons(ETH_TYPE_IP)) { | |
df2c07f4 | 2460 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 2461 | |
df2c07f4 | 2462 | ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4, |
36956a7d | 2463 | sizeof *ipv4_key); |
661cbcd5 JP |
2464 | ipv4_key->ipv4_src = data->nw_src; |
2465 | ipv4_key->ipv4_dst = data->nw_dst; | |
2466 | ipv4_key->ipv4_proto = data->nw_proto; | |
2467 | ipv4_key->ipv4_tos = data->nw_tos; | |
2468 | ipv4_key->ipv4_ttl = data->nw_ttl; | |
431495b1 AZ |
2469 | ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag) |
2470 | : ovs_to_odp_frag(data->nw_frag); | |
d31f1109 | 2471 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
df2c07f4 | 2472 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 2473 | |
df2c07f4 | 2474 | ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6, |
d31f1109 | 2475 | sizeof *ipv6_key); |
661cbcd5 JP |
2476 | memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src); |
2477 | memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst); | |
2478 | ipv6_key->ipv6_label = data->ipv6_label; | |
2479 | ipv6_key->ipv6_proto = data->nw_proto; | |
2480 | ipv6_key->ipv6_tclass = data->nw_tos; | |
2481 | ipv6_key->ipv6_hlimit = data->nw_ttl; | |
431495b1 AZ |
2482 | ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag) |
2483 | : ovs_to_odp_frag(data->nw_frag); | |
8087f5ff MM |
2484 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
2485 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
df2c07f4 | 2486 | struct ovs_key_arp *arp_key; |
d31f1109 | 2487 | |
9ddf12cc BP |
2488 | arp_key = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_ARP, |
2489 | sizeof *arp_key); | |
661cbcd5 JP |
2490 | arp_key->arp_sip = data->nw_src; |
2491 | arp_key->arp_tip = data->nw_dst; | |
2492 | arp_key->arp_op = htons(data->nw_proto); | |
2493 | memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN); | |
2494 | memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN); | |
b0a17866 | 2495 | } else if (eth_type_mpls(flow->dl_type)) { |
b02475c5 SH |
2496 | struct ovs_key_mpls *mpls_key; |
2497 | ||
2498 | mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS, | |
2499 | sizeof *mpls_key); | |
661cbcd5 | 2500 | mpls_key->mpls_lse = data->mpls_lse; |
b02475c5 SH |
2501 | } |
2502 | ||
48cecbdc | 2503 | if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
6767a2cc | 2504 | if (flow->nw_proto == IPPROTO_TCP) { |
df2c07f4 | 2505 | struct ovs_key_tcp *tcp_key; |
36956a7d | 2506 | |
df2c07f4 | 2507 | tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP, |
36956a7d | 2508 | sizeof *tcp_key); |
661cbcd5 JP |
2509 | tcp_key->tcp_src = data->tp_src; |
2510 | tcp_key->tcp_dst = data->tp_dst; | |
dc235f7f JR |
2511 | |
2512 | if (data->tcp_flags) { | |
2513 | nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags); | |
2514 | } | |
6767a2cc | 2515 | } else if (flow->nw_proto == IPPROTO_UDP) { |
df2c07f4 | 2516 | struct ovs_key_udp *udp_key; |
36956a7d | 2517 | |
df2c07f4 | 2518 | udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP, |
36956a7d | 2519 | sizeof *udp_key); |
661cbcd5 JP |
2520 | udp_key->udp_src = data->tp_src; |
2521 | udp_key->udp_dst = data->tp_dst; | |
c6bcb685 JS |
2522 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
2523 | struct ovs_key_sctp *sctp_key; | |
2524 | ||
2525 | sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP, | |
2526 | sizeof *sctp_key); | |
2527 | sctp_key->sctp_src = data->tp_src; | |
2528 | sctp_key->sctp_dst = data->tp_dst; | |
d31f1109 JP |
2529 | } else if (flow->dl_type == htons(ETH_TYPE_IP) |
2530 | && flow->nw_proto == IPPROTO_ICMP) { | |
df2c07f4 | 2531 | struct ovs_key_icmp *icmp_key; |
36956a7d | 2532 | |
df2c07f4 | 2533 | icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP, |
36956a7d | 2534 | sizeof *icmp_key); |
661cbcd5 JP |
2535 | icmp_key->icmp_type = ntohs(data->tp_src); |
2536 | icmp_key->icmp_code = ntohs(data->tp_dst); | |
d31f1109 JP |
2537 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6) |
2538 | && flow->nw_proto == IPPROTO_ICMPV6) { | |
df2c07f4 | 2539 | struct ovs_key_icmpv6 *icmpv6_key; |
d31f1109 | 2540 | |
df2c07f4 | 2541 | icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6, |
d31f1109 | 2542 | sizeof *icmpv6_key); |
661cbcd5 JP |
2543 | icmpv6_key->icmpv6_type = ntohs(data->tp_src); |
2544 | icmpv6_key->icmpv6_code = ntohs(data->tp_dst); | |
685a51a5 | 2545 | |
6f8dbd27 GY |
2546 | if (flow->tp_dst == htons(0) && |
2547 | (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
2548 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) && | |
2549 | (!is_mask || (data->tp_src == htons(0xffff) && | |
2550 | data->tp_dst == htons(0xffff)))) { | |
2551 | ||
df2c07f4 | 2552 | struct ovs_key_nd *nd_key; |
685a51a5 | 2553 | |
df2c07f4 | 2554 | nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND, |
685a51a5 | 2555 | sizeof *nd_key); |
661cbcd5 | 2556 | memcpy(nd_key->nd_target, &data->nd_target, |
685a51a5 | 2557 | sizeof nd_key->nd_target); |
661cbcd5 JP |
2558 | memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN); |
2559 | memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN); | |
685a51a5 | 2560 | } |
36956a7d | 2561 | } |
36956a7d | 2562 | } |
fea393b1 BP |
2563 | |
2564 | unencap: | |
2565 | if (encap) { | |
2566 | nl_msg_end_nested(buf, encap); | |
2567 | } | |
36956a7d | 2568 | } |
661cbcd5 JP |
2569 | |
2570 | /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. | |
2571 | * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port | |
2572 | * number rather than a datapath port number). Instead, if 'odp_in_port' | |
2573 | * is anything other than ODPP_NONE, it is included in 'buf' as the input | |
2574 | * port. | |
2575 | * | |
2576 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
2577 | * capable of being expanded to allow for that much space. */ | |
2578 | void | |
2579 | odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow, | |
2580 | odp_port_t odp_in_port) | |
2581 | { | |
2582 | odp_flow_key_from_flow__(buf, flow, flow, odp_in_port); | |
2583 | } | |
2584 | ||
2585 | /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to | |
2586 | * 'buf'. 'flow' is used as a template to determine how to interpret | |
2587 | * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but | |
2588 | * it doesn't indicate whether the other fields should be interpreted as | |
2589 | * ARP, IPv4, IPv6, etc. | |
2590 | * | |
2591 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
2592 | * capable of being expanded to allow for that much space. */ | |
2593 | void | |
2594 | odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask, | |
2595 | const struct flow *flow, uint32_t odp_in_port_mask) | |
2596 | { | |
2597 | odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask)); | |
2598 | } | |
36956a7d | 2599 | |
758c456d JR |
2600 | /* Generate ODP flow key from the given packet metadata */ |
2601 | void | |
2602 | odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md) | |
2603 | { | |
2604 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority); | |
2605 | ||
2606 | if (md->tunnel.ip_dst) { | |
2607 | tun_key_to_attr(buf, &md->tunnel); | |
2608 | } | |
2609 | ||
2610 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark); | |
2611 | ||
2612 | /* Add an ingress port attribute if 'odp_in_port' is not the magical | |
2613 | * value "ODPP_NONE". */ | |
2614 | if (md->in_port != ODPP_NONE) { | |
2615 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port); | |
2616 | } | |
2617 | } | |
2618 | ||
2619 | /* Generate packet metadata from the given ODP flow key. */ | |
2620 | void | |
2621 | odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len, | |
2622 | struct pkt_metadata *md) | |
2623 | { | |
2624 | const struct nlattr *nla; | |
2625 | size_t left; | |
2626 | uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY | | |
2627 | 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL | | |
2628 | 1u << OVS_KEY_ATTR_IN_PORT; | |
2629 | ||
2630 | memset(md, 0, sizeof *md); | |
2631 | md->in_port = ODPP_NONE; | |
2632 | ||
2633 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { | |
2634 | uint16_t type = nl_attr_type(nla); | |
2635 | size_t len = nl_attr_get_size(nla); | |
2636 | int expected_len = odp_flow_key_attr_len(type); | |
2637 | ||
2638 | if (len != expected_len && expected_len >= 0) { | |
2639 | continue; | |
2640 | } | |
2641 | ||
2642 | if (type == OVS_KEY_ATTR_PRIORITY) { | |
2643 | md->skb_priority = nl_attr_get_u32(nla); | |
2644 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY); | |
2645 | } else if (type == OVS_KEY_ATTR_SKB_MARK) { | |
2646 | md->pkt_mark = nl_attr_get_u32(nla); | |
2647 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK); | |
2648 | } else if (type == OVS_KEY_ATTR_TUNNEL) { | |
2649 | enum odp_key_fitness res; | |
2650 | ||
2651 | res = odp_tun_key_from_attr(nla, &md->tunnel); | |
2652 | if (res == ODP_FIT_ERROR) { | |
2653 | memset(&md->tunnel, 0, sizeof md->tunnel); | |
2654 | } else if (res == ODP_FIT_PERFECT) { | |
2655 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL); | |
2656 | } | |
2657 | } else if (type == OVS_KEY_ATTR_IN_PORT) { | |
2658 | md->in_port = nl_attr_get_odp_port(nla); | |
2659 | wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT); | |
2660 | } | |
2661 | ||
2662 | if (!wanted_attrs) { | |
2663 | return; /* Have everything. */ | |
2664 | } | |
2665 | } | |
2666 | } | |
2667 | ||
b0f7b9b5 BP |
2668 | uint32_t |
2669 | odp_flow_key_hash(const struct nlattr *key, size_t key_len) | |
2670 | { | |
2671 | BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t))); | |
db5a1019 AW |
2672 | return hash_words(ALIGNED_CAST(const uint32_t *, key), |
2673 | key_len / sizeof(uint32_t), 0); | |
b0f7b9b5 BP |
2674 | } |
2675 | ||
34118cae BP |
2676 | static void |
2677 | log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title, | |
b0f7b9b5 | 2678 | uint64_t attrs, int out_of_range_attr, |
34118cae BP |
2679 | const struct nlattr *key, size_t key_len) |
2680 | { | |
2681 | struct ds s; | |
2682 | int i; | |
2683 | ||
b0f7b9b5 | 2684 | if (VLOG_DROP_DBG(rl)) { |
34118cae BP |
2685 | return; |
2686 | } | |
2687 | ||
2688 | ds_init(&s); | |
b0f7b9b5 BP |
2689 | for (i = 0; i < 64; i++) { |
2690 | if (attrs & (UINT64_C(1) << i)) { | |
e6603631 BP |
2691 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
2692 | ||
2693 | ds_put_format(&s, " %s", | |
2694 | ovs_key_attr_to_string(i, namebuf, sizeof namebuf)); | |
34118cae BP |
2695 | } |
2696 | } | |
b0f7b9b5 BP |
2697 | if (out_of_range_attr) { |
2698 | ds_put_format(&s, " %d (and possibly others)", out_of_range_attr); | |
2699 | } | |
34118cae BP |
2700 | |
2701 | ds_put_cstr(&s, ": "); | |
2702 | odp_flow_key_format(key, key_len, &s); | |
2703 | ||
b0f7b9b5 | 2704 | VLOG_DBG("%s:%s", title, ds_cstr(&s)); |
34118cae BP |
2705 | ds_destroy(&s); |
2706 | } | |
2707 | ||
7257b535 | 2708 | static bool |
9e44d715 | 2709 | odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow) |
7257b535 | 2710 | { |
34118cae BP |
2711 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
2712 | ||
9e44d715 | 2713 | if (odp_frag > OVS_FRAG_TYPE_LATER) { |
b0f7b9b5 | 2714 | VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag); |
7257b535 BP |
2715 | return false; |
2716 | } | |
2717 | ||
7257b535 | 2718 | if (odp_frag != OVS_FRAG_TYPE_NONE) { |
eadef313 | 2719 | flow->nw_frag |= FLOW_NW_FRAG_ANY; |
7257b535 | 2720 | if (odp_frag == OVS_FRAG_TYPE_LATER) { |
eadef313 | 2721 | flow->nw_frag |= FLOW_NW_FRAG_LATER; |
7257b535 BP |
2722 | } |
2723 | } | |
2724 | return true; | |
2725 | } | |
2726 | ||
b0f7b9b5 | 2727 | static bool |
fea393b1 | 2728 | parse_flow_nlattrs(const struct nlattr *key, size_t key_len, |
b0f7b9b5 BP |
2729 | const struct nlattr *attrs[], uint64_t *present_attrsp, |
2730 | int *out_of_range_attrp) | |
36956a7d | 2731 | { |
b0f7b9b5 | 2732 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
36956a7d | 2733 | const struct nlattr *nla; |
34118cae | 2734 | uint64_t present_attrs; |
36956a7d BP |
2735 | size_t left; |
2736 | ||
2aef1214 | 2737 | BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs); |
34118cae | 2738 | present_attrs = 0; |
b0f7b9b5 | 2739 | *out_of_range_attrp = 0; |
36956a7d | 2740 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { |
34118cae BP |
2741 | uint16_t type = nl_attr_type(nla); |
2742 | size_t len = nl_attr_get_size(nla); | |
2743 | int expected_len = odp_flow_key_attr_len(type); | |
2744 | ||
b0f7b9b5 | 2745 | if (len != expected_len && expected_len >= 0) { |
e6603631 BP |
2746 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
2747 | ||
34582733 | 2748 | VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have " |
e6603631 BP |
2749 | "length %d", ovs_key_attr_to_string(type, namebuf, |
2750 | sizeof namebuf), | |
b0f7b9b5 BP |
2751 | len, expected_len); |
2752 | return false; | |
34118cae BP |
2753 | } |
2754 | ||
2aef1214 | 2755 | if (type > OVS_KEY_ATTR_MAX) { |
b0f7b9b5 BP |
2756 | *out_of_range_attrp = type; |
2757 | } else { | |
2758 | if (present_attrs & (UINT64_C(1) << type)) { | |
e6603631 BP |
2759 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
2760 | ||
b0f7b9b5 | 2761 | VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key", |
e6603631 BP |
2762 | ovs_key_attr_to_string(type, |
2763 | namebuf, sizeof namebuf)); | |
b0f7b9b5 BP |
2764 | return false; |
2765 | } | |
2766 | ||
2767 | present_attrs |= UINT64_C(1) << type; | |
2768 | attrs[type] = nla; | |
2769 | } | |
34118cae BP |
2770 | } |
2771 | if (left) { | |
2772 | VLOG_ERR_RL(&rl, "trailing garbage in flow key"); | |
b0f7b9b5 | 2773 | return false; |
34118cae BP |
2774 | } |
2775 | ||
fea393b1 | 2776 | *present_attrsp = present_attrs; |
b0f7b9b5 | 2777 | return true; |
fea393b1 BP |
2778 | } |
2779 | ||
b0f7b9b5 BP |
2780 | static enum odp_key_fitness |
2781 | check_expectations(uint64_t present_attrs, int out_of_range_attr, | |
2782 | uint64_t expected_attrs, | |
fea393b1 BP |
2783 | const struct nlattr *key, size_t key_len) |
2784 | { | |
2785 | uint64_t missing_attrs; | |
2786 | uint64_t extra_attrs; | |
2787 | ||
2788 | missing_attrs = expected_attrs & ~present_attrs; | |
2789 | if (missing_attrs) { | |
b0f7b9b5 BP |
2790 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
2791 | log_odp_key_attributes(&rl, "expected but not present", | |
2792 | missing_attrs, 0, key, key_len); | |
2793 | return ODP_FIT_TOO_LITTLE; | |
fea393b1 BP |
2794 | } |
2795 | ||
2796 | extra_attrs = present_attrs & ~expected_attrs; | |
b0f7b9b5 BP |
2797 | if (extra_attrs || out_of_range_attr) { |
2798 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
2799 | log_odp_key_attributes(&rl, "present but not expected", | |
2800 | extra_attrs, out_of_range_attr, key, key_len); | |
2801 | return ODP_FIT_TOO_MUCH; | |
fea393b1 BP |
2802 | } |
2803 | ||
b0f7b9b5 | 2804 | return ODP_FIT_PERFECT; |
fea393b1 BP |
2805 | } |
2806 | ||
b0f7b9b5 BP |
2807 | static bool |
2808 | parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
2809 | uint64_t present_attrs, uint64_t *expected_attrs, | |
4a221615 | 2810 | struct flow *flow, const struct flow *src_flow) |
fea393b1 BP |
2811 | { |
2812 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 2813 | bool is_mask = flow != src_flow; |
36956a7d | 2814 | |
fea393b1 | 2815 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
34118cae | 2816 | flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); |
4a221615 | 2817 | if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) { |
34118cae BP |
2818 | VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key", |
2819 | ntohs(flow->dl_type)); | |
b0f7b9b5 | 2820 | return false; |
34118cae | 2821 | } |
4a221615 GY |
2822 | if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN && |
2823 | flow->dl_type != htons(0xffff)) { | |
2824 | return false; | |
2825 | } | |
b0f7b9b5 | 2826 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; |
34118cae | 2827 | } else { |
4a221615 GY |
2828 | if (!is_mask) { |
2829 | flow->dl_type = htons(FLOW_DL_TYPE_NONE); | |
2830 | } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) { | |
2831 | /* See comments in odp_flow_key_from_flow__(). */ | |
2832 | VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame"); | |
2833 | return false; | |
2834 | } | |
34118cae | 2835 | } |
b0f7b9b5 BP |
2836 | return true; |
2837 | } | |
2838 | ||
2839 | static enum odp_key_fitness | |
b02475c5 SH |
2840 | parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], |
2841 | uint64_t present_attrs, int out_of_range_attr, | |
2842 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 | 2843 | const struct nlattr *key, size_t key_len, |
91a77332 | 2844 | const struct flow *src_flow) |
b0f7b9b5 BP |
2845 | { |
2846 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 GY |
2847 | bool is_mask = src_flow != flow; |
2848 | const void *check_start = NULL; | |
2849 | size_t check_len = 0; | |
2850 | enum ovs_key_attr expected_bit = 0xff; | |
2851 | ||
2852 | if (eth_type_mpls(src_flow->dl_type)) { | |
91a77332 GS |
2853 | if (!is_mask) { |
2854 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); | |
4a221615 | 2855 | |
91a77332 GS |
2856 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) { |
2857 | return ODP_FIT_TOO_LITTLE; | |
2858 | } | |
2859 | flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]); | |
4a221615 GY |
2860 | } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { |
2861 | flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]); | |
2862 | ||
2863 | if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) { | |
2864 | return ODP_FIT_ERROR; | |
2865 | } | |
2866 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); | |
4a221615 GY |
2867 | } |
2868 | goto done; | |
2869 | } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) { | |
2870 | if (!is_mask) { | |
2871 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4; | |
b02475c5 | 2872 | } |
fea393b1 | 2873 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { |
34118cae | 2874 | const struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 2875 | |
34118cae | 2876 | ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]); |
36956a7d BP |
2877 | flow->nw_src = ipv4_key->ipv4_src; |
2878 | flow->nw_dst = ipv4_key->ipv4_dst; | |
2879 | flow->nw_proto = ipv4_key->ipv4_proto; | |
eadef313 | 2880 | flow->nw_tos = ipv4_key->ipv4_tos; |
a61680c6 | 2881 | flow->nw_ttl = ipv4_key->ipv4_ttl; |
4a221615 | 2882 | if (is_mask) { |
91a77332 | 2883 | flow->nw_frag = ipv4_key->ipv4_frag; |
4a221615 GY |
2884 | check_start = ipv4_key; |
2885 | check_len = sizeof *ipv4_key; | |
2886 | expected_bit = OVS_KEY_ATTR_IPV4; | |
2887 | } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) { | |
b0f7b9b5 | 2888 | return ODP_FIT_ERROR; |
36956a7d | 2889 | } |
34118cae | 2890 | } |
4a221615 GY |
2891 | } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) { |
2892 | if (!is_mask) { | |
2893 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6; | |
2894 | } | |
fea393b1 | 2895 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { |
34118cae | 2896 | const struct ovs_key_ipv6 *ipv6_key; |
36956a7d | 2897 | |
34118cae | 2898 | ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]); |
d31f1109 JP |
2899 | memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src); |
2900 | memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst); | |
fa8223b7 | 2901 | flow->ipv6_label = ipv6_key->ipv6_label; |
d31f1109 | 2902 | flow->nw_proto = ipv6_key->ipv6_proto; |
eadef313 | 2903 | flow->nw_tos = ipv6_key->ipv6_tclass; |
a61680c6 | 2904 | flow->nw_ttl = ipv6_key->ipv6_hlimit; |
4a221615 | 2905 | if (is_mask) { |
91a77332 | 2906 | flow->nw_frag = ipv6_key->ipv6_frag; |
4a221615 GY |
2907 | check_start = ipv6_key; |
2908 | check_len = sizeof *ipv6_key; | |
2909 | expected_bit = OVS_KEY_ATTR_IPV6; | |
2910 | } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) { | |
b0f7b9b5 | 2911 | return ODP_FIT_ERROR; |
d31f1109 | 2912 | } |
34118cae | 2913 | } |
4a221615 GY |
2914 | } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) || |
2915 | src_flow->dl_type == htons(ETH_TYPE_RARP)) { | |
2916 | if (!is_mask) { | |
2917 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP; | |
2918 | } | |
fea393b1 | 2919 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) { |
34118cae | 2920 | const struct ovs_key_arp *arp_key; |
d31f1109 | 2921 | |
34118cae | 2922 | arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]); |
36956a7d BP |
2923 | flow->nw_src = arp_key->arp_sip; |
2924 | flow->nw_dst = arp_key->arp_tip; | |
4a221615 | 2925 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
34118cae BP |
2926 | VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow " |
2927 | "key", ntohs(arp_key->arp_op)); | |
b0f7b9b5 | 2928 | return ODP_FIT_ERROR; |
36956a7d BP |
2929 | } |
2930 | flow->nw_proto = ntohs(arp_key->arp_op); | |
bad68a99 JP |
2931 | memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN); |
2932 | memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN); | |
4a221615 GY |
2933 | |
2934 | if (is_mask) { | |
2935 | check_start = arp_key; | |
2936 | check_len = sizeof *arp_key; | |
2937 | expected_bit = OVS_KEY_ATTR_ARP; | |
2938 | } | |
2939 | } | |
2940 | } else { | |
2941 | goto done; | |
2942 | } | |
df4eeb20 | 2943 | if (check_len > 0) { /* Happens only when 'is_mask'. */ |
4a221615 GY |
2944 | if (!is_all_zeros(check_start, check_len) && |
2945 | flow->dl_type != htons(0xffff)) { | |
2946 | return ODP_FIT_ERROR; | |
2947 | } else { | |
2948 | expected_attrs |= UINT64_C(1) << expected_bit; | |
36956a7d | 2949 | } |
36956a7d | 2950 | } |
36956a7d | 2951 | |
4a221615 GY |
2952 | expected_bit = OVS_KEY_ATTR_UNSPEC; |
2953 | if (src_flow->nw_proto == IPPROTO_TCP | |
2954 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
2955 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
2956 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
2957 | if (!is_mask) { | |
2958 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP; | |
2959 | } | |
fea393b1 | 2960 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) { |
34118cae | 2961 | const struct ovs_key_tcp *tcp_key; |
36956a7d | 2962 | |
34118cae BP |
2963 | tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]); |
2964 | flow->tp_src = tcp_key->tcp_src; | |
2965 | flow->tp_dst = tcp_key->tcp_dst; | |
4a221615 GY |
2966 | expected_bit = OVS_KEY_ATTR_TCP; |
2967 | } | |
dc235f7f JR |
2968 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) { |
2969 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS; | |
2970 | flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]); | |
2971 | } | |
4a221615 GY |
2972 | } else if (src_flow->nw_proto == IPPROTO_UDP |
2973 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
2974 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
2975 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
2976 | if (!is_mask) { | |
2977 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP; | |
7257b535 | 2978 | } |
fea393b1 | 2979 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) { |
34118cae BP |
2980 | const struct ovs_key_udp *udp_key; |
2981 | ||
2982 | udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]); | |
2983 | flow->tp_src = udp_key->udp_src; | |
2984 | flow->tp_dst = udp_key->udp_dst; | |
4a221615 GY |
2985 | expected_bit = OVS_KEY_ATTR_UDP; |
2986 | } | |
12848ebf GS |
2987 | } else if (src_flow->nw_proto == IPPROTO_SCTP |
2988 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
2989 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
2990 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
c6bcb685 JS |
2991 | if (!is_mask) { |
2992 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP; | |
2993 | } | |
2994 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) { | |
2995 | const struct ovs_key_sctp *sctp_key; | |
2996 | ||
2997 | sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]); | |
2998 | flow->tp_src = sctp_key->sctp_src; | |
2999 | flow->tp_dst = sctp_key->sctp_dst; | |
3000 | expected_bit = OVS_KEY_ATTR_SCTP; | |
3001 | } | |
4a221615 GY |
3002 | } else if (src_flow->nw_proto == IPPROTO_ICMP |
3003 | && src_flow->dl_type == htons(ETH_TYPE_IP) | |
3004 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
3005 | if (!is_mask) { | |
3006 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP; | |
d31f1109 | 3007 | } |
fea393b1 | 3008 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) { |
34118cae BP |
3009 | const struct ovs_key_icmp *icmp_key; |
3010 | ||
3011 | icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]); | |
3012 | flow->tp_src = htons(icmp_key->icmp_type); | |
3013 | flow->tp_dst = htons(icmp_key->icmp_code); | |
4a221615 GY |
3014 | expected_bit = OVS_KEY_ATTR_ICMP; |
3015 | } | |
3016 | } else if (src_flow->nw_proto == IPPROTO_ICMPV6 | |
3017 | && src_flow->dl_type == htons(ETH_TYPE_IPV6) | |
3018 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
3019 | if (!is_mask) { | |
3020 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6; | |
685a51a5 | 3021 | } |
fea393b1 | 3022 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) { |
34118cae BP |
3023 | const struct ovs_key_icmpv6 *icmpv6_key; |
3024 | ||
3025 | icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]); | |
3026 | flow->tp_src = htons(icmpv6_key->icmpv6_type); | |
3027 | flow->tp_dst = htons(icmpv6_key->icmpv6_code); | |
4a221615 | 3028 | expected_bit = OVS_KEY_ATTR_ICMPV6; |
6f8dbd27 GY |
3029 | if (src_flow->tp_dst == htons(0) && |
3030 | (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
3031 | src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) { | |
4a221615 GY |
3032 | if (!is_mask) { |
3033 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
3034 | } | |
fea393b1 | 3035 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) { |
34118cae BP |
3036 | const struct ovs_key_nd *nd_key; |
3037 | ||
3038 | nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]); | |
3039 | memcpy(&flow->nd_target, nd_key->nd_target, | |
3040 | sizeof flow->nd_target); | |
3041 | memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN); | |
3042 | memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN); | |
4a221615 GY |
3043 | if (is_mask) { |
3044 | if (!is_all_zeros((const uint8_t *) nd_key, | |
3045 | sizeof *nd_key) && | |
6f8dbd27 GY |
3046 | (flow->tp_src != htons(0xffff) || |
3047 | flow->tp_dst != htons(0xffff))) { | |
4a221615 GY |
3048 | return ODP_FIT_ERROR; |
3049 | } else { | |
3050 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
3051 | } | |
3052 | } | |
34118cae BP |
3053 | } |
3054 | } | |
7257b535 | 3055 | } |
34118cae | 3056 | } |
4a221615 GY |
3057 | if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) { |
3058 | if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) { | |
3059 | return ODP_FIT_ERROR; | |
3060 | } else { | |
3061 | expected_attrs |= UINT64_C(1) << expected_bit; | |
3062 | } | |
3063 | } | |
7257b535 | 3064 | |
4a221615 | 3065 | done: |
b0f7b9b5 BP |
3066 | return check_expectations(present_attrs, out_of_range_attr, expected_attrs, |
3067 | key, key_len); | |
3068 | } | |
3069 | ||
3070 | /* Parse 802.1Q header then encapsulated L3 attributes. */ | |
3071 | static enum odp_key_fitness | |
3072 | parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
3073 | uint64_t present_attrs, int out_of_range_attr, | |
3074 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 GY |
3075 | const struct nlattr *key, size_t key_len, |
3076 | const struct flow *src_flow) | |
b0f7b9b5 BP |
3077 | { |
3078 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 3079 | bool is_mask = src_flow != flow; |
b0f7b9b5 BP |
3080 | |
3081 | const struct nlattr *encap | |
3082 | = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP) | |
3083 | ? attrs[OVS_KEY_ATTR_ENCAP] : NULL); | |
3084 | enum odp_key_fitness encap_fitness; | |
3085 | enum odp_key_fitness fitness; | |
3086 | ovs_be16 tci; | |
3087 | ||
ec9f40dc | 3088 | /* Calculate fitness of outer attributes. */ |
4a221615 GY |
3089 | if (!is_mask) { |
3090 | expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) | | |
3091 | (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)); | |
3092 | } else { | |
3093 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
3094 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
3095 | } | |
3096 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) { | |
3097 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP); | |
3098 | } | |
3099 | } | |
b0f7b9b5 BP |
3100 | fitness = check_expectations(present_attrs, out_of_range_attr, |
3101 | expected_attrs, key, key_len); | |
3102 | ||
3103 | /* Get the VLAN TCI value. */ | |
4a221615 | 3104 | if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) { |
b0f7b9b5 | 3105 | return ODP_FIT_TOO_LITTLE; |
4a221615 | 3106 | } else { |
5054d6fd BP |
3107 | tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3108 | ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]) | |
3109 | : htons(0)); | |
4a221615 | 3110 | if (!is_mask) { |
91a77332 | 3111 | if (tci == htons(0)) { |
4a221615 | 3112 | /* Corner case for a truncated 802.1Q header. */ |
91a77332 GS |
3113 | if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { |
3114 | return ODP_FIT_TOO_MUCH; | |
3115 | } | |
3116 | return fitness; | |
3117 | } else if (!(tci & htons(VLAN_CFI))) { | |
3118 | VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " | |
3119 | "but CFI bit is not set", ntohs(tci)); | |
3120 | return ODP_FIT_ERROR; | |
3121 | } | |
4a221615 GY |
3122 | } |
3123 | /* Set vlan_tci. | |
3124 | * Remove the TPID from dl_type since it's not the real Ethertype. */ | |
3125 | flow->dl_type = htons(0); | |
3126 | flow->vlan_tci = tci; | |
3127 | } | |
3128 | ||
3129 | if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { | |
b0f7b9b5 | 3130 | return fitness; |
b0f7b9b5 | 3131 | } |
b0f7b9b5 BP |
3132 | /* Now parse the encapsulated attributes. */ |
3133 | if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap), | |
3134 | attrs, &present_attrs, &out_of_range_attr)) { | |
3135 | return ODP_FIT_ERROR; | |
3136 | } | |
3137 | expected_attrs = 0; | |
3138 | ||
4a221615 | 3139 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) { |
b0f7b9b5 BP |
3140 | return ODP_FIT_ERROR; |
3141 | } | |
b02475c5 | 3142 | encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
3143 | expected_attrs, flow, key, key_len, |
3144 | src_flow); | |
b0f7b9b5 BP |
3145 | |
3146 | /* The overall fitness is the worse of the outer and inner attributes. */ | |
3147 | return MAX(fitness, encap_fitness); | |
3148 | } | |
3149 | ||
4a221615 GY |
3150 | static enum odp_key_fitness |
3151 | odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len, | |
3152 | struct flow *flow, const struct flow *src_flow) | |
b0f7b9b5 | 3153 | { |
b0f7b9b5 BP |
3154 | const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1]; |
3155 | uint64_t expected_attrs; | |
3156 | uint64_t present_attrs; | |
3157 | int out_of_range_attr; | |
4a221615 | 3158 | bool is_mask = src_flow != flow; |
b0f7b9b5 BP |
3159 | |
3160 | memset(flow, 0, sizeof *flow); | |
3161 | ||
3162 | /* Parse attributes. */ | |
3163 | if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs, | |
3164 | &out_of_range_attr)) { | |
3165 | return ODP_FIT_ERROR; | |
3166 | } | |
3167 | expected_attrs = 0; | |
3168 | ||
3169 | /* Metadata. */ | |
3170 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) { | |
deedf7e7 | 3171 | flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]); |
b0f7b9b5 BP |
3172 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY; |
3173 | } | |
3174 | ||
72e8bf28 | 3175 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) { |
1362e248 | 3176 | flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]); |
72e8bf28 AA |
3177 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK; |
3178 | } | |
3179 | ||
9b405f1a PS |
3180 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) { |
3181 | enum odp_key_fitness res; | |
05fb0928 | 3182 | |
617e10e7 | 3183 | res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel); |
9b405f1a PS |
3184 | if (res == ODP_FIT_ERROR) { |
3185 | return ODP_FIT_ERROR; | |
3186 | } else if (res == ODP_FIT_PERFECT) { | |
3187 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL; | |
05fb0928 JR |
3188 | } |
3189 | } | |
3190 | ||
b0f7b9b5 | 3191 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) { |
4e022ec0 AW |
3192 | flow->in_port.odp_port |
3193 | = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]); | |
b0f7b9b5 | 3194 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT; |
4a221615 | 3195 | } else if (!is_mask) { |
4e022ec0 | 3196 | flow->in_port.odp_port = ODPP_NONE; |
b0f7b9b5 BP |
3197 | } |
3198 | ||
3199 | /* Ethernet header. */ | |
3200 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) { | |
3201 | const struct ovs_key_ethernet *eth_key; | |
3202 | ||
3203 | eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]); | |
3204 | memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN); | |
3205 | memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN); | |
4a221615 GY |
3206 | if (is_mask) { |
3207 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; | |
3208 | } | |
3209 | } | |
3210 | if (!is_mask) { | |
3211 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; | |
b0f7b9b5 | 3212 | } |
b0f7b9b5 BP |
3213 | |
3214 | /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */ | |
4a221615 GY |
3215 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, |
3216 | src_flow)) { | |
b0f7b9b5 BP |
3217 | return ODP_FIT_ERROR; |
3218 | } | |
3219 | ||
21e70add BP |
3220 | if (is_mask |
3221 | ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0 | |
3222 | : src_flow->dl_type == htons(ETH_TYPE_VLAN)) { | |
b0f7b9b5 | 3223 | return parse_8021q_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
3224 | expected_attrs, flow, key, key_len, src_flow); |
3225 | } | |
3226 | if (is_mask) { | |
3227 | flow->vlan_tci = htons(0xffff); | |
3228 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
3229 | flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]); | |
3230 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
3231 | } | |
b0f7b9b5 | 3232 | } |
b02475c5 | 3233 | return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
3234 | expected_attrs, flow, key, key_len, src_flow); |
3235 | } | |
3236 | ||
3237 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow | |
3238 | * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well | |
3239 | * 'key' fits our expectations for what a flow key should contain. | |
3240 | * | |
3241 | * The 'in_port' will be the datapath's understanding of the port. The | |
3242 | * caller will need to translate with odp_port_to_ofp_port() if the | |
3243 | * OpenFlow port is needed. | |
3244 | * | |
3245 | * This function doesn't take the packet itself as an argument because none of | |
3246 | * the currently understood OVS_KEY_ATTR_* attributes require it. Currently, | |
3247 | * it is always possible to infer which additional attribute(s) should appear | |
3248 | * by looking at the attributes for lower-level protocols, e.g. if the network | |
3249 | * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we | |
3250 | * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it | |
3251 | * must be absent. */ | |
3252 | enum odp_key_fitness | |
3253 | odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, | |
3254 | struct flow *flow) | |
3255 | { | |
3256 | return odp_flow_key_to_flow__(key, key_len, flow, flow); | |
3257 | } | |
3258 | ||
3259 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask | |
3260 | * structure in 'mask'. 'flow' must be a previously translated flow | |
3261 | * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well | |
3262 | * 'key' fits our expectations for what a flow key should contain. */ | |
3263 | enum odp_key_fitness | |
3264 | odp_flow_key_to_mask(const struct nlattr *key, size_t key_len, | |
3265 | struct flow *mask, const struct flow *flow) | |
3266 | { | |
3267 | return odp_flow_key_to_flow__(key, key_len, mask, flow); | |
14608a15 | 3268 | } |
39db78a0 | 3269 | |
6814e51f BP |
3270 | /* Returns 'fitness' as a string, for use in debug messages. */ |
3271 | const char * | |
3272 | odp_key_fitness_to_string(enum odp_key_fitness fitness) | |
3273 | { | |
3274 | switch (fitness) { | |
3275 | case ODP_FIT_PERFECT: | |
3276 | return "OK"; | |
3277 | case ODP_FIT_TOO_MUCH: | |
3278 | return "too_much"; | |
3279 | case ODP_FIT_TOO_LITTLE: | |
3280 | return "too_little"; | |
3281 | case ODP_FIT_ERROR: | |
3282 | return "error"; | |
3283 | default: | |
3284 | return "<unknown>"; | |
3285 | } | |
3286 | } | |
3287 | ||
39db78a0 | 3288 | /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies |
e995e3df BP |
3289 | * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute |
3290 | * whose contents are the 'userdata_size' bytes at 'userdata' and returns the | |
3291 | * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is | |
3292 | * null, then the return value is not meaningful.) */ | |
39db78a0 | 3293 | size_t |
e995e3df BP |
3294 | odp_put_userspace_action(uint32_t pid, |
3295 | const void *userdata, size_t userdata_size, | |
39db78a0 BP |
3296 | struct ofpbuf *odp_actions) |
3297 | { | |
e995e3df | 3298 | size_t userdata_ofs; |
39db78a0 BP |
3299 | size_t offset; |
3300 | ||
3301 | offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); | |
3302 | nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); | |
e995e3df BP |
3303 | if (userdata) { |
3304 | userdata_ofs = odp_actions->size + NLA_HDRLEN; | |
96ed775f BP |
3305 | |
3306 | /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel | |
3307 | * module before Linux 3.10 required the userdata to be exactly 8 bytes | |
3308 | * long: | |
3309 | * | |
3310 | * - The kernel rejected shorter userdata with -ERANGE. | |
3311 | * | |
3312 | * - The kernel silently dropped userdata beyond the first 8 bytes. | |
3313 | * | |
3314 | * Thus, for maximum compatibility, always put at least 8 bytes. (We | |
3315 | * separately disable features that required more than 8 bytes.) */ | |
3316 | memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA, | |
3317 | MAX(8, userdata_size)), | |
3318 | userdata, userdata_size); | |
e995e3df BP |
3319 | } else { |
3320 | userdata_ofs = 0; | |
39db78a0 BP |
3321 | } |
3322 | nl_msg_end_nested(odp_actions, offset); | |
3323 | ||
e995e3df | 3324 | return userdata_ofs; |
39db78a0 | 3325 | } |
b9ad7294 EJ |
3326 | |
3327 | void | |
3328 | odp_put_tunnel_action(const struct flow_tnl *tunnel, | |
3329 | struct ofpbuf *odp_actions) | |
3330 | { | |
3331 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
3332 | tun_key_to_attr(odp_actions, tunnel); | |
3333 | nl_msg_end_nested(odp_actions, offset); | |
3334 | } | |
5bbda0aa EJ |
3335 | \f |
3336 | /* The commit_odp_actions() function and its helpers. */ | |
3337 | ||
3338 | static void | |
3339 | commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type, | |
3340 | const void *key, size_t key_size) | |
3341 | { | |
3342 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
3343 | nl_msg_put_unspec(odp_actions, key_type, key, key_size); | |
3344 | nl_msg_end_nested(odp_actions, offset); | |
3345 | } | |
3346 | ||
09a0d2e7 | 3347 | void |
1362e248 | 3348 | odp_put_pkt_mark_action(const uint32_t pkt_mark, |
09a0d2e7 AA |
3349 | struct ofpbuf *odp_actions) |
3350 | { | |
1362e248 JG |
3351 | commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark, |
3352 | sizeof(pkt_mark)); | |
09a0d2e7 AA |
3353 | } |
3354 | ||
b9ad7294 EJ |
3355 | /* If any of the flow key data that ODP actions can modify are different in |
3356 | * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to | |
3357 | * 'odp_actions' that change the flow tunneling information in key from | |
3358 | * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the | |
3359 | * same way. In other words, operates the same as commit_odp_actions(), but | |
3360 | * only on tunneling information. */ | |
3361 | void | |
3362 | commit_odp_tunnel_action(const struct flow *flow, struct flow *base, | |
5bbda0aa EJ |
3363 | struct ofpbuf *odp_actions) |
3364 | { | |
05fb0928 JR |
3365 | /* A valid IPV4_TUNNEL must have non-zero ip_dst. */ |
3366 | if (flow->tunnel.ip_dst) { | |
fc80de30 JR |
3367 | if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) { |
3368 | return; | |
3369 | } | |
3370 | memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel); | |
b9ad7294 | 3371 | odp_put_tunnel_action(&base->tunnel, odp_actions); |
05fb0928 | 3372 | } |
5bbda0aa EJ |
3373 | } |
3374 | ||
3375 | static void | |
3376 | commit_set_ether_addr_action(const struct flow *flow, struct flow *base, | |
1dd35f8a JP |
3377 | struct ofpbuf *odp_actions, |
3378 | struct flow_wildcards *wc) | |
5bbda0aa EJ |
3379 | { |
3380 | struct ovs_key_ethernet eth_key; | |
3381 | ||
3382 | if (eth_addr_equals(base->dl_src, flow->dl_src) && | |
3383 | eth_addr_equals(base->dl_dst, flow->dl_dst)) { | |
3384 | return; | |
3385 | } | |
3386 | ||
1dd35f8a JP |
3387 | memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src); |
3388 | memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst); | |
3389 | ||
5bbda0aa EJ |
3390 | memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN); |
3391 | memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN); | |
3392 | ||
3393 | memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN); | |
3394 | memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN); | |
3395 | ||
3396 | commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET, | |
3397 | ð_key, sizeof(eth_key)); | |
3398 | } | |
3399 | ||
3400 | static void | |
8fd16af2 | 3401 | commit_vlan_action(ovs_be16 vlan_tci, struct flow *base, |
1dd35f8a | 3402 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa | 3403 | { |
8fd16af2 | 3404 | if (base->vlan_tci == vlan_tci) { |
5bbda0aa EJ |
3405 | return; |
3406 | } | |
3407 | ||
1dd35f8a JP |
3408 | memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci); |
3409 | ||
5bbda0aa EJ |
3410 | if (base->vlan_tci & htons(VLAN_CFI)) { |
3411 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); | |
3412 | } | |
3413 | ||
8fd16af2 | 3414 | if (vlan_tci & htons(VLAN_CFI)) { |
5bbda0aa EJ |
3415 | struct ovs_action_push_vlan vlan; |
3416 | ||
3417 | vlan.vlan_tpid = htons(ETH_TYPE_VLAN); | |
8fd16af2 | 3418 | vlan.vlan_tci = vlan_tci; |
5bbda0aa EJ |
3419 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, |
3420 | &vlan, sizeof vlan); | |
3421 | } | |
8fd16af2 | 3422 | base->vlan_tci = vlan_tci; |
5bbda0aa EJ |
3423 | } |
3424 | ||
b02475c5 SH |
3425 | static void |
3426 | commit_mpls_action(const struct flow *flow, struct flow *base, | |
b0a17866 SH |
3427 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
3428 | int *mpls_depth_delta) | |
b02475c5 | 3429 | { |
b0a17866 | 3430 | if (flow->mpls_lse == base->mpls_lse && !*mpls_depth_delta) { |
b02475c5 SH |
3431 | return; |
3432 | } | |
3433 | ||
1dd35f8a JP |
3434 | memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse); |
3435 | ||
b0a17866 SH |
3436 | switch (*mpls_depth_delta) { |
3437 | case -1: | |
b02475c5 | 3438 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type); |
b0a17866 SH |
3439 | break; |
3440 | case 1: { | |
b02475c5 SH |
3441 | struct ovs_action_push_mpls *mpls; |
3442 | ||
9ddf12cc BP |
3443 | mpls = nl_msg_put_unspec_zero(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS, |
3444 | sizeof *mpls); | |
b02475c5 SH |
3445 | mpls->mpls_ethertype = flow->dl_type; |
3446 | mpls->mpls_lse = flow->mpls_lse; | |
b0a17866 SH |
3447 | break; |
3448 | } | |
3449 | case 0: { | |
b02475c5 SH |
3450 | struct ovs_key_mpls mpls_key; |
3451 | ||
20131cef | 3452 | mpls_key.mpls_lse = flow->mpls_lse; |
b02475c5 SH |
3453 | commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, |
3454 | &mpls_key, sizeof(mpls_key)); | |
b0a17866 SH |
3455 | break; |
3456 | } | |
3457 | default: | |
428b2edd | 3458 | OVS_NOT_REACHED(); |
b02475c5 SH |
3459 | } |
3460 | ||
3461 | base->dl_type = flow->dl_type; | |
3462 | base->mpls_lse = flow->mpls_lse; | |
b0a17866 | 3463 | *mpls_depth_delta = 0; |
b02475c5 SH |
3464 | } |
3465 | ||
5bbda0aa | 3466 | static void |
c4f2731d | 3467 | commit_set_ipv4_action(const struct flow *flow, struct flow *base, |
1dd35f8a | 3468 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa EJ |
3469 | { |
3470 | struct ovs_key_ipv4 ipv4_key; | |
3471 | ||
5bbda0aa EJ |
3472 | if (base->nw_src == flow->nw_src && |
3473 | base->nw_dst == flow->nw_dst && | |
3474 | base->nw_tos == flow->nw_tos && | |
3475 | base->nw_ttl == flow->nw_ttl && | |
3476 | base->nw_frag == flow->nw_frag) { | |
3477 | return; | |
3478 | } | |
3479 | ||
1dd35f8a JP |
3480 | memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src); |
3481 | memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst); | |
3482 | memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos); | |
3483 | memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl); | |
3484 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
3485 | memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag); | |
3486 | ||
5bbda0aa EJ |
3487 | ipv4_key.ipv4_src = base->nw_src = flow->nw_src; |
3488 | ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst; | |
3489 | ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos; | |
3490 | ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl; | |
3491 | ipv4_key.ipv4_proto = base->nw_proto; | |
c4f2731d | 3492 | ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag); |
5bbda0aa EJ |
3493 | |
3494 | commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4, | |
3495 | &ipv4_key, sizeof(ipv4_key)); | |
3496 | } | |
3497 | ||
c4f2731d PS |
3498 | static void |
3499 | commit_set_ipv6_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3500 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
c4f2731d PS |
3501 | { |
3502 | struct ovs_key_ipv6 ipv6_key; | |
3503 | ||
3504 | if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) && | |
3505 | ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) && | |
3506 | base->ipv6_label == flow->ipv6_label && | |
3507 | base->nw_tos == flow->nw_tos && | |
3508 | base->nw_ttl == flow->nw_ttl && | |
3509 | base->nw_frag == flow->nw_frag) { | |
3510 | return; | |
3511 | } | |
3512 | ||
1dd35f8a JP |
3513 | memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src); |
3514 | memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst); | |
3515 | memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label); | |
3516 | memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos); | |
3517 | memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl); | |
3518 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
3519 | memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag); | |
3520 | ||
c4f2731d PS |
3521 | base->ipv6_src = flow->ipv6_src; |
3522 | memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src)); | |
3523 | base->ipv6_dst = flow->ipv6_dst; | |
3524 | memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst)); | |
3525 | ||
3526 | ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label; | |
3527 | ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos; | |
3528 | ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl; | |
3529 | ipv6_key.ipv6_proto = base->nw_proto; | |
3530 | ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag); | |
3531 | ||
3532 | commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6, | |
3533 | &ipv6_key, sizeof(ipv6_key)); | |
3534 | } | |
3535 | ||
f6c8a6b1 BP |
3536 | static enum slow_path_reason |
3537 | commit_set_arp_action(const struct flow *flow, struct flow *base, | |
3538 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
3539 | { | |
3540 | struct ovs_key_arp arp_key; | |
3541 | ||
3542 | if (base->nw_src == flow->nw_src && | |
3543 | base->nw_dst == flow->nw_dst && | |
3544 | base->nw_proto == flow->nw_proto && | |
3545 | eth_addr_equals(base->arp_sha, flow->arp_sha) && | |
3546 | eth_addr_equals(base->arp_tha, flow->arp_tha)) { | |
3547 | return 0; | |
3548 | } | |
3549 | ||
3550 | memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src); | |
3551 | memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst); | |
3552 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
3553 | memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha); | |
3554 | memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha); | |
3555 | ||
3556 | base->nw_src = flow->nw_src; | |
3557 | base->nw_dst = flow->nw_dst; | |
3558 | base->nw_proto = flow->nw_proto; | |
3559 | memcpy(base->arp_sha, flow->arp_sha, ETH_ADDR_LEN); | |
3560 | memcpy(base->arp_tha, flow->arp_tha, ETH_ADDR_LEN); | |
3561 | ||
3562 | arp_key.arp_sip = base->nw_src; | |
3563 | arp_key.arp_tip = base->nw_dst; | |
3564 | arp_key.arp_op = htons(base->nw_proto); | |
3565 | memcpy(arp_key.arp_sha, flow->arp_sha, ETH_ADDR_LEN); | |
3566 | memcpy(arp_key.arp_tha, flow->arp_tha, ETH_ADDR_LEN); | |
3567 | ||
3568 | commit_set_action(odp_actions, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); | |
3569 | ||
3570 | return SLOW_ACTION; | |
3571 | } | |
3572 | ||
3573 | static enum slow_path_reason | |
c4f2731d | 3574 | commit_set_nw_action(const struct flow *flow, struct flow *base, |
1dd35f8a | 3575 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
c4f2731d | 3576 | { |
f6c8a6b1 | 3577 | /* Check if 'flow' really has an L3 header. */ |
c4f2731d | 3578 | if (!flow->nw_proto) { |
f6c8a6b1 | 3579 | return 0; |
c4f2731d PS |
3580 | } |
3581 | ||
f6c8a6b1 BP |
3582 | switch (ntohs(base->dl_type)) { |
3583 | case ETH_TYPE_IP: | |
1dd35f8a | 3584 | commit_set_ipv4_action(flow, base, odp_actions, wc); |
f6c8a6b1 BP |
3585 | break; |
3586 | ||
3587 | case ETH_TYPE_IPV6: | |
1dd35f8a | 3588 | commit_set_ipv6_action(flow, base, odp_actions, wc); |
f6c8a6b1 BP |
3589 | break; |
3590 | ||
3591 | case ETH_TYPE_ARP: | |
3592 | return commit_set_arp_action(flow, base, odp_actions, wc); | |
c4f2731d | 3593 | } |
f6c8a6b1 BP |
3594 | |
3595 | return 0; | |
c4f2731d PS |
3596 | } |
3597 | ||
5bbda0aa EJ |
3598 | static void |
3599 | commit_set_port_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3600 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa | 3601 | { |
1b035ef2 | 3602 | if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) { |
5bbda0aa EJ |
3603 | return; |
3604 | } | |
3605 | ||
3606 | if (base->tp_src == flow->tp_src && | |
3607 | base->tp_dst == flow->tp_dst) { | |
3608 | return; | |
3609 | } | |
3610 | ||
1dd35f8a JP |
3611 | memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src); |
3612 | memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst); | |
3613 | ||
5bbda0aa EJ |
3614 | if (flow->nw_proto == IPPROTO_TCP) { |
3615 | struct ovs_key_tcp port_key; | |
3616 | ||
3617 | port_key.tcp_src = base->tp_src = flow->tp_src; | |
3618 | port_key.tcp_dst = base->tp_dst = flow->tp_dst; | |
3619 | ||
3620 | commit_set_action(odp_actions, OVS_KEY_ATTR_TCP, | |
3621 | &port_key, sizeof(port_key)); | |
3622 | ||
3623 | } else if (flow->nw_proto == IPPROTO_UDP) { | |
3624 | struct ovs_key_udp port_key; | |
3625 | ||
3626 | port_key.udp_src = base->tp_src = flow->tp_src; | |
3627 | port_key.udp_dst = base->tp_dst = flow->tp_dst; | |
3628 | ||
3629 | commit_set_action(odp_actions, OVS_KEY_ATTR_UDP, | |
3630 | &port_key, sizeof(port_key)); | |
c6bcb685 JS |
3631 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
3632 | struct ovs_key_sctp port_key; | |
3633 | ||
3634 | port_key.sctp_src = base->tp_src = flow->tp_src; | |
3635 | port_key.sctp_dst = base->tp_dst = flow->tp_dst; | |
3636 | ||
3637 | commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP, | |
3638 | &port_key, sizeof(port_key)); | |
5bbda0aa EJ |
3639 | } |
3640 | } | |
3641 | ||
3642 | static void | |
3643 | commit_set_priority_action(const struct flow *flow, struct flow *base, | |
1dd35f8a JP |
3644 | struct ofpbuf *odp_actions, |
3645 | struct flow_wildcards *wc) | |
5bbda0aa | 3646 | { |
deedf7e7 | 3647 | if (base->skb_priority == flow->skb_priority) { |
5bbda0aa EJ |
3648 | return; |
3649 | } | |
1dd35f8a JP |
3650 | |
3651 | memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority); | |
deedf7e7 | 3652 | base->skb_priority = flow->skb_priority; |
5bbda0aa EJ |
3653 | |
3654 | commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY, | |
deedf7e7 | 3655 | &base->skb_priority, sizeof(base->skb_priority)); |
5bbda0aa EJ |
3656 | } |
3657 | ||
72e8bf28 | 3658 | static void |
1362e248 | 3659 | commit_set_pkt_mark_action(const struct flow *flow, struct flow *base, |
1dd35f8a JP |
3660 | struct ofpbuf *odp_actions, |
3661 | struct flow_wildcards *wc) | |
72e8bf28 | 3662 | { |
1362e248 | 3663 | if (base->pkt_mark == flow->pkt_mark) { |
72e8bf28 AA |
3664 | return; |
3665 | } | |
1dd35f8a | 3666 | |
1362e248 JG |
3667 | memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark); |
3668 | base->pkt_mark = flow->pkt_mark; | |
72e8bf28 | 3669 | |
1362e248 | 3670 | odp_put_pkt_mark_action(base->pkt_mark, odp_actions); |
72e8bf28 | 3671 | } |
7fd91025 | 3672 | |
5bbda0aa EJ |
3673 | /* If any of the flow key data that ODP actions can modify are different in |
3674 | * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow | |
b9ad7294 EJ |
3675 | * key from 'base' into 'flow', and then changes 'base' the same way. Does not |
3676 | * commit set_tunnel actions. Users should call commit_odp_tunnel_action() | |
1dd35f8a | 3677 | * in addition to this function if needed. Sets fields in 'wc' that are |
7fd91025 BP |
3678 | * used as part of the action. |
3679 | * | |
3680 | * Returns a reason to force processing the flow's packets into the userspace | |
3681 | * slow path, if there is one, otherwise 0. */ | |
3682 | enum slow_path_reason | |
5bbda0aa | 3683 | commit_odp_actions(const struct flow *flow, struct flow *base, |
b0a17866 SH |
3684 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
3685 | int *mpls_depth_delta) | |
5bbda0aa | 3686 | { |
f6c8a6b1 BP |
3687 | enum slow_path_reason slow; |
3688 | ||
1dd35f8a | 3689 | commit_set_ether_addr_action(flow, base, odp_actions, wc); |
8fd16af2 | 3690 | commit_vlan_action(flow->vlan_tci, base, odp_actions, wc); |
f6c8a6b1 | 3691 | slow = commit_set_nw_action(flow, base, odp_actions, wc); |
1dd35f8a | 3692 | commit_set_port_action(flow, base, odp_actions, wc); |
ec9f40dc | 3693 | /* Committing MPLS actions should occur after committing nw and port |
1b035ef2 SH |
3694 | * actions. This is because committing MPLS actions may alter a packet so |
3695 | * that it is no longer IP and thus nw and port actions are no longer valid. | |
3696 | */ | |
b0a17866 | 3697 | commit_mpls_action(flow, base, odp_actions, wc, mpls_depth_delta); |
1dd35f8a | 3698 | commit_set_priority_action(flow, base, odp_actions, wc); |
1362e248 | 3699 | commit_set_pkt_mark_action(flow, base, odp_actions, wc); |
7fd91025 | 3700 | |
f6c8a6b1 | 3701 | return slow; |
5bbda0aa | 3702 | } |