]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
fd13c6b5 | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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> |
370e373b | 25 | #include <netinet/ip6.h> |
064af421 BP |
26 | #include <stdlib.h> |
27 | #include <string.h> | |
a36de779 | 28 | |
cdee00fd | 29 | #include "byte-order.h" |
064af421 | 30 | #include "coverage.h" |
758c456d | 31 | #include "dpif.h" |
3e8a2ad1 | 32 | #include "openvswitch/dynamic-string.h" |
064af421 | 33 | #include "flow.h" |
cdee00fd | 34 | #include "netlink.h" |
64c96779 | 35 | #include "openvswitch/ofpbuf.h" |
064af421 | 36 | #include "packets.h" |
44bac24b | 37 | #include "simap.h" |
064af421 | 38 | #include "timeval.h" |
9558d2a5 | 39 | #include "tun-metadata.h" |
a36de779 | 40 | #include "unaligned.h" |
064af421 | 41 | #include "util.h" |
10e92b4f | 42 | #include "uuid.h" |
e6211adc | 43 | #include "openvswitch/vlog.h" |
8b668ee3 | 44 | #include "openvswitch/match.h" |
34118cae BP |
45 | |
46 | VLOG_DEFINE_THIS_MODULE(odp_util); | |
064af421 | 47 | |
df2c07f4 JP |
48 | /* The interface between userspace and kernel uses an "OVS_*" prefix. |
49 | * Since this is fairly non-specific for the OVS userspace components, | |
50 | * "ODP_*" (Open vSwitch Datapath) is used as the prefix for | |
51 | * interactions with the datapath. | |
52 | */ | |
53 | ||
7202cbe5 BP |
54 | /* The set of characters that may separate one action or one key attribute |
55 | * from another. */ | |
56 | static const char *delimiters = ", \t\r\n"; | |
d787ad39 | 57 | static const char *delimiters_end = ", \t\r\n)"; |
7202cbe5 | 58 | |
6b8da9e9 JG |
59 | struct attr_len_tbl { |
60 | int len; | |
61 | const struct attr_len_tbl *next; | |
62 | int next_max; | |
63 | }; | |
64 | #define ATTR_LEN_INVALID -1 | |
65 | #define ATTR_LEN_VARIABLE -2 | |
66 | #define ATTR_LEN_NESTED -3 | |
67 | ||
e6cc0bab AZ |
68 | static int parse_odp_key_mask_attr(const char *, const struct simap *port_names, |
69 | struct ofpbuf *, struct ofpbuf *); | |
70 | static void format_odp_key_attr(const struct nlattr *a, | |
0a37839c GS |
71 | const struct nlattr *ma, |
72 | const struct hmap *portno_names, struct ds *ds, | |
041e7168 | 73 | bool verbose); |
a052b51b | 74 | |
5bb08b0e JG |
75 | struct geneve_scan { |
76 | struct geneve_opt d[63]; | |
77 | int len; | |
78 | }; | |
79 | ||
80 | static int scan_geneve(const char *s, struct geneve_scan *key, | |
81 | struct geneve_scan *mask); | |
82 | static void format_geneve_opts(const struct geneve_opt *opt, | |
83 | const struct geneve_opt *mask, int opts_len, | |
84 | struct ds *, bool verbose); | |
85 | ||
65da723b JG |
86 | static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[], |
87 | int max, struct ofpbuf *, | |
88 | const struct nlattr *key); | |
89cf41ec BP |
89 | static void format_u128(struct ds *d, const ovs_32aligned_u128 *key, |
90 | const ovs_32aligned_u128 *mask, bool verbose); | |
9daf2348 JS |
91 | static int scan_u128(const char *s, ovs_u128 *value, ovs_u128 *mask); |
92 | ||
c37f7135 AZ |
93 | static int parse_odp_action(const char *s, const struct simap *port_names, |
94 | struct ofpbuf *actions); | |
95 | ||
98403001 BP |
96 | /* Returns one the following for the action with the given OVS_ACTION_ATTR_* |
97 | * 'type': | |
98 | * | |
99 | * - For an action whose argument has a fixed length, returned that | |
100 | * nonnegative length in bytes. | |
101 | * | |
6b8da9e9 | 102 | * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE. |
98403001 | 103 | * |
6b8da9e9 | 104 | * - For an invalid 'type', returns ATTR_LEN_INVALID. */ |
4edb9ae9 | 105 | static int |
cdee00fd BP |
106 | odp_action_len(uint16_t type) |
107 | { | |
df2c07f4 | 108 | if (type > OVS_ACTION_ATTR_MAX) { |
cdee00fd BP |
109 | return -1; |
110 | } | |
111 | ||
09ded0ad | 112 | switch ((enum ovs_action_attr) type) { |
fea393b1 | 113 | case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t); |
aaca4fe0 | 114 | case OVS_ACTION_ATTR_TRUNC: return sizeof(struct ovs_action_trunc); |
6b8da9e9 | 115 | case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE; |
a36de779 | 116 | case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t); |
5dddf960 | 117 | case OVS_ACTION_ATTR_METER: return sizeof(uint32_t); |
6b8da9e9 | 118 | case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE; |
fea393b1 BP |
119 | case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan); |
120 | case OVS_ACTION_ATTR_POP_VLAN: return 0; | |
b02475c5 SH |
121 | case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls); |
122 | case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16); | |
347bf289 AZ |
123 | case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t); |
124 | case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash); | |
6b8da9e9 JG |
125 | case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE; |
126 | case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE; | |
127 | case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE; | |
07659514 | 128 | case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE; |
6fcecb85 YY |
129 | case OVS_ACTION_ATTR_PUSH_ETH: return sizeof(struct ovs_action_push_eth); |
130 | case OVS_ACTION_ATTR_POP_ETH: return 0; | |
535e3acf | 131 | case OVS_ACTION_ATTR_CLONE: return ATTR_LEN_VARIABLE; |
1fc11c59 JS |
132 | case OVS_ACTION_ATTR_ENCAP_NSH: return ATTR_LEN_VARIABLE; |
133 | case OVS_ACTION_ATTR_DECAP_NSH: return 0; | |
df2c07f4 JP |
134 | |
135 | case OVS_ACTION_ATTR_UNSPEC: | |
136 | case __OVS_ACTION_ATTR_MAX: | |
6b8da9e9 | 137 | return ATTR_LEN_INVALID; |
cdee00fd BP |
138 | } |
139 | ||
6b8da9e9 | 140 | return ATTR_LEN_INVALID; |
cdee00fd BP |
141 | } |
142 | ||
e6603631 BP |
143 | /* Returns a string form of 'attr'. The return value is either a statically |
144 | * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize' | |
145 | * should be at least OVS_KEY_ATTR_BUFSIZE. */ | |
146 | enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 }; | |
744791b5 | 147 | static const char * |
e6603631 | 148 | ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) |
744791b5 | 149 | { |
744791b5 BP |
150 | switch (attr) { |
151 | case OVS_KEY_ATTR_UNSPEC: return "unspec"; | |
fea393b1 | 152 | case OVS_KEY_ATTR_ENCAP: return "encap"; |
1b567fb9 | 153 | case OVS_KEY_ATTR_PRIORITY: return "skb_priority"; |
72e8bf28 | 154 | case OVS_KEY_ATTR_SKB_MARK: return "skb_mark"; |
07659514 JS |
155 | case OVS_KEY_ATTR_CT_STATE: return "ct_state"; |
156 | case OVS_KEY_ATTR_CT_ZONE: return "ct_zone"; | |
8e53fe8c | 157 | case OVS_KEY_ATTR_CT_MARK: return "ct_mark"; |
9daf2348 | 158 | case OVS_KEY_ATTR_CT_LABELS: return "ct_label"; |
c30b4cea JR |
159 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: return "ct_tuple4"; |
160 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: return "ct_tuple6"; | |
9b405f1a | 161 | case OVS_KEY_ATTR_TUNNEL: return "tunnel"; |
744791b5 BP |
162 | case OVS_KEY_ATTR_IN_PORT: return "in_port"; |
163 | case OVS_KEY_ATTR_ETHERNET: return "eth"; | |
fea393b1 | 164 | case OVS_KEY_ATTR_VLAN: return "vlan"; |
744791b5 BP |
165 | case OVS_KEY_ATTR_ETHERTYPE: return "eth_type"; |
166 | case OVS_KEY_ATTR_IPV4: return "ipv4"; | |
167 | case OVS_KEY_ATTR_IPV6: return "ipv6"; | |
168 | case OVS_KEY_ATTR_TCP: return "tcp"; | |
dc235f7f | 169 | case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags"; |
744791b5 | 170 | case OVS_KEY_ATTR_UDP: return "udp"; |
c6bcb685 | 171 | case OVS_KEY_ATTR_SCTP: return "sctp"; |
744791b5 BP |
172 | case OVS_KEY_ATTR_ICMP: return "icmp"; |
173 | case OVS_KEY_ATTR_ICMPV6: return "icmpv6"; | |
174 | case OVS_KEY_ATTR_ARP: return "arp"; | |
175 | case OVS_KEY_ATTR_ND: return "nd"; | |
b02475c5 | 176 | case OVS_KEY_ATTR_MPLS: return "mpls"; |
572f732a AZ |
177 | case OVS_KEY_ATTR_DP_HASH: return "dp_hash"; |
178 | case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id"; | |
beb75a40 | 179 | case OVS_KEY_ATTR_PACKET_TYPE: return "packet_type"; |
3d2fbd70 | 180 | case OVS_KEY_ATTR_NSH: return "nsh"; |
744791b5 BP |
181 | |
182 | case __OVS_KEY_ATTR_MAX: | |
183 | default: | |
e6603631 BP |
184 | snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); |
185 | return namebuf; | |
744791b5 BP |
186 | } |
187 | } | |
188 | ||
cdee00fd BP |
189 | static void |
190 | format_generic_odp_action(struct ds *ds, const struct nlattr *a) | |
191 | { | |
8ba43fbd BP |
192 | size_t len = nl_attr_get_size(a); |
193 | ||
fd13c6b5 | 194 | ds_put_format(ds, "action%d", nl_attr_type(a)); |
8ba43fbd | 195 | if (len) { |
cdee00fd BP |
196 | const uint8_t *unspec; |
197 | unsigned int i; | |
198 | ||
199 | unspec = nl_attr_get(a); | |
8ba43fbd | 200 | for (i = 0; i < len; i++) { |
cdee00fd BP |
201 | ds_put_char(ds, i ? ' ': '('); |
202 | ds_put_format(ds, "%02x", unspec[i]); | |
203 | } | |
204 | ds_put_char(ds, ')'); | |
205 | } | |
206 | } | |
207 | ||
6ff686f2 | 208 | static void |
0722f341 BP |
209 | format_odp_sample_action(struct ds *ds, const struct nlattr *attr, |
210 | const struct hmap *portno_names) | |
6ff686f2 PS |
211 | { |
212 | static const struct nl_policy ovs_sample_policy[] = { | |
3548d242 BP |
213 | [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 }, |
214 | [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED } | |
6ff686f2 PS |
215 | }; |
216 | struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)]; | |
6ff686f2 PS |
217 | double percentage; |
218 | const struct nlattr *nla_acts; | |
219 | int len; | |
220 | ||
221 | ds_put_cstr(ds, "sample"); | |
222 | ||
5621afff | 223 | if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) { |
6ff686f2 PS |
224 | ds_put_cstr(ds, "(error)"); |
225 | return; | |
226 | } | |
227 | ||
228 | percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) / | |
229 | UINT32_MAX; | |
230 | ||
231 | ds_put_format(ds, "(sample=%.1f%%,", percentage); | |
232 | ||
233 | ds_put_cstr(ds, "actions("); | |
234 | nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
235 | len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
0722f341 | 236 | format_odp_actions(ds, nla_acts, len, portno_names); |
6ff686f2 PS |
237 | ds_put_format(ds, "))"); |
238 | } | |
239 | ||
535e3acf | 240 | static void |
0722f341 BP |
241 | format_odp_clone_action(struct ds *ds, const struct nlattr *attr, |
242 | const struct hmap *portno_names) | |
535e3acf AZ |
243 | { |
244 | const struct nlattr *nla_acts = nl_attr_get(attr); | |
245 | int len = nl_attr_get_size(attr); | |
246 | ||
247 | ds_put_cstr(ds, "clone"); | |
248 | ds_put_format(ds, "("); | |
0722f341 | 249 | format_odp_actions(ds, nla_acts, len, portno_names); |
535e3acf AZ |
250 | ds_put_format(ds, ")"); |
251 | } | |
252 | ||
3d2fbd70 JS |
253 | static void |
254 | format_nsh_key(struct ds *ds, const struct ovs_key_nsh *key) | |
255 | { | |
256 | ds_put_format(ds, "flags=%d", key->flags); | |
257 | ds_put_format(ds, ",mdtype=%d", key->mdtype); | |
258 | ds_put_format(ds, ",np=%d", key->np); | |
259 | ds_put_format(ds, ",spi=0x%x", | |
260 | (ntohl(key->path_hdr) & NSH_SPI_MASK) >> NSH_SPI_SHIFT); | |
261 | ds_put_format(ds, ",si=%d", | |
262 | (ntohl(key->path_hdr) & NSH_SI_MASK) >> NSH_SI_SHIFT); | |
263 | ||
264 | switch (key->mdtype) { | |
265 | case NSH_M_TYPE1: | |
266 | for (int i = 0; i < 4; i++) { | |
267 | ds_put_format(ds, ",c%d=0x%x", i + 1, ntohl(key->c[i])); | |
268 | } | |
269 | break; | |
270 | case NSH_M_TYPE2: | |
3d2fbd70 | 271 | default: |
1fc11c59 JS |
272 | /* No support for matching other metadata formats yet. */ |
273 | break; | |
3d2fbd70 JS |
274 | } |
275 | } | |
276 | ||
277 | static void | |
278 | format_uint8_masked(struct ds *s, bool *first, const char *name, | |
279 | uint8_t value, uint8_t mask) | |
280 | { | |
281 | if (mask != 0) { | |
282 | if (!*first) { | |
283 | ds_put_char(s, ','); | |
284 | } | |
285 | ds_put_format(s, "%s=", name); | |
286 | if (mask == UINT8_MAX) { | |
287 | ds_put_format(s, "%"PRIu8, value); | |
288 | } else { | |
289 | ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask); | |
290 | } | |
291 | *first = false; | |
292 | } | |
293 | } | |
294 | ||
295 | static void | |
296 | format_be32_masked(struct ds *s, bool *first, const char *name, | |
297 | ovs_be32 value, ovs_be32 mask) | |
298 | { | |
299 | if (mask != htonl(0)) { | |
300 | if (!*first) { | |
301 | ds_put_char(s, ','); | |
302 | } | |
303 | ds_put_format(s, "%s=", name); | |
304 | if (mask == OVS_BE32_MAX) { | |
305 | ds_put_format(s, "0x%"PRIx32, ntohl(value)); | |
306 | } else { | |
307 | ds_put_format(s, "0x%"PRIx32"/0x%08"PRIx32, | |
308 | ntohl(value), ntohl(mask)); | |
309 | } | |
310 | *first = false; | |
311 | } | |
312 | } | |
313 | ||
314 | static void | |
315 | format_nsh_key_mask(struct ds *ds, const struct ovs_key_nsh *key, | |
316 | const struct ovs_key_nsh *mask) | |
317 | { | |
318 | if (!mask) { | |
319 | format_nsh_key(ds, key); | |
320 | } else { | |
321 | bool first = true; | |
322 | uint32_t spi = (ntohl(key->path_hdr) & NSH_SPI_MASK) >> NSH_SPI_SHIFT; | |
323 | uint32_t spi_mask = (ntohl(mask->path_hdr) & NSH_SPI_MASK) >> | |
324 | NSH_SPI_SHIFT; | |
325 | if (spi_mask == 0x00ffffff) { | |
326 | spi_mask = UINT32_MAX; | |
327 | } | |
328 | uint8_t si = (ntohl(key->path_hdr) & NSH_SI_MASK) >> NSH_SI_SHIFT; | |
329 | uint8_t si_mask = (ntohl(mask->path_hdr) & NSH_SI_MASK) >> | |
330 | NSH_SI_SHIFT; | |
331 | ||
332 | format_uint8_masked(ds, &first, "flags", key->flags, mask->flags); | |
333 | format_uint8_masked(ds, &first, "mdtype", key->mdtype, mask->mdtype); | |
334 | format_uint8_masked(ds, &first, "np", key->np, mask->np); | |
335 | format_be32_masked(ds, &first, "spi", htonl(spi), htonl(spi_mask)); | |
336 | format_uint8_masked(ds, &first, "si", si, si_mask); | |
337 | format_be32_masked(ds, &first, "c1", key->c[0], mask->c[0]); | |
338 | format_be32_masked(ds, &first, "c2", key->c[1], mask->c[1]); | |
339 | format_be32_masked(ds, &first, "c3", key->c[2], mask->c[2]); | |
340 | format_be32_masked(ds, &first, "c4", key->c[3], mask->c[3]); | |
341 | } | |
342 | } | |
343 | ||
1fc11c59 JS |
344 | static void |
345 | format_odp_encap_nsh_action(struct ds *ds, | |
346 | const struct ovs_action_encap_nsh *encap_nsh) | |
347 | { | |
348 | uint32_t path_hdr = ntohl(encap_nsh->path_hdr); | |
349 | uint32_t spi = (path_hdr & NSH_SPI_MASK) >> NSH_SPI_SHIFT; | |
350 | uint8_t si = (path_hdr & NSH_SI_MASK) >> NSH_SI_SHIFT; | |
351 | ||
352 | ds_put_cstr(ds, "encap_nsh("); | |
353 | ds_put_format(ds, "flags=%d", encap_nsh->flags); | |
354 | ds_put_format(ds, ",mdtype=%d", encap_nsh->mdtype); | |
355 | ds_put_format(ds, ",np=%d", encap_nsh->np); | |
356 | ds_put_format(ds, ",spi=0x%x", spi); | |
357 | ds_put_format(ds, ",si=%d", si); | |
358 | switch (encap_nsh->mdtype) { | |
359 | case NSH_M_TYPE1: { | |
360 | struct nsh_md1_ctx *md1_ctx = | |
361 | ALIGNED_CAST(struct nsh_md1_ctx *, encap_nsh->metadata); | |
362 | for (int i = 0; i < 4; i++) { | |
363 | ds_put_format(ds, ",c%d=0x%x", i + 1, | |
364 | ntohl(get_16aligned_be32(&md1_ctx->c[i]))); | |
365 | } | |
366 | break; | |
367 | } | |
368 | case NSH_M_TYPE2: | |
369 | ds_put_cstr(ds, ",md2="); | |
370 | ds_put_hex(ds, encap_nsh->metadata, encap_nsh->mdlen); | |
371 | break; | |
372 | default: | |
373 | OVS_NOT_REACHED(); | |
374 | } | |
375 | ds_put_format(ds, ")"); | |
376 | } | |
377 | ||
6a7e895f | 378 | static const char * |
04594cd5 | 379 | slow_path_reason_to_string(uint32_t reason) |
6a7e895f | 380 | { |
04594cd5 BP |
381 | switch ((enum slow_path_reason) reason) { |
382 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING; | |
383 | SLOW_PATH_REASONS | |
384 | #undef SPR | |
6a7e895f | 385 | } |
04594cd5 BP |
386 | |
387 | return NULL; | |
6a7e895f BP |
388 | } |
389 | ||
04594cd5 BP |
390 | const char * |
391 | slow_path_reason_to_explanation(enum slow_path_reason reason) | |
98f0520f | 392 | { |
04594cd5 BP |
393 | switch (reason) { |
394 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION; | |
395 | SLOW_PATH_REASONS | |
396 | #undef SPR | |
98f0520f EJ |
397 | } |
398 | ||
04594cd5 | 399 | return "<unknown>"; |
98f0520f EJ |
400 | } |
401 | ||
ec956fc7 | 402 | static int |
8e4c1621 JG |
403 | parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t), |
404 | uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask) | |
ec956fc7 | 405 | { |
8e4c1621 JG |
406 | return parse_flags(s, bit_to_string, ')', NULL, NULL, |
407 | res_flags, allowed, res_mask); | |
6a7e895f BP |
408 | } |
409 | ||
98403001 | 410 | static void |
0722f341 BP |
411 | format_odp_userspace_action(struct ds *ds, const struct nlattr *attr, |
412 | const struct hmap *portno_names) | |
98403001 BP |
413 | { |
414 | static const struct nl_policy ovs_userspace_policy[] = { | |
3548d242 BP |
415 | [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 }, |
416 | [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC, | |
417 | .optional = true }, | |
8b7ea2d4 WZ |
418 | [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32, |
419 | .optional = true }, | |
7321bda3 NM |
420 | [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC, |
421 | .optional = true }, | |
98403001 BP |
422 | }; |
423 | struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)]; | |
e995e3df | 424 | const struct nlattr *userdata_attr; |
8b7ea2d4 | 425 | const struct nlattr *tunnel_out_port_attr; |
98403001 BP |
426 | |
427 | if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) { | |
428 | ds_put_cstr(ds, "userspace(error)"); | |
429 | return; | |
430 | } | |
431 | ||
432 | ds_put_format(ds, "userspace(pid=%"PRIu32, | |
433 | nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID])); | |
434 | ||
e995e3df | 435 | userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA]; |
29089a54 RL |
436 | |
437 | if (userdata_attr) { | |
438 | const uint8_t *userdata = nl_attr_get(userdata_attr); | |
439 | size_t userdata_len = nl_attr_get_size(userdata_attr); | |
440 | bool userdata_unspec = true; | |
1673e0e4 | 441 | union user_action_cookie cookie; |
98403001 | 442 | |
29089a54 RL |
443 | if (userdata_len >= sizeof cookie.type |
444 | && userdata_len <= sizeof cookie) { | |
445 | ||
446 | memset(&cookie, 0, sizeof cookie); | |
447 | memcpy(&cookie, userdata, userdata_len); | |
448 | ||
449 | userdata_unspec = false; | |
450 | ||
451 | if (userdata_len == sizeof cookie.sflow | |
452 | && cookie.type == USER_ACTION_COOKIE_SFLOW) { | |
453 | ds_put_format(ds, ",sFlow(" | |
fd13c6b5 | 454 | "vid=%"PRIu16",pcp=%d,output=%"PRIu32")", |
29089a54 RL |
455 | vlan_tci_to_vid(cookie.sflow.vlan_tci), |
456 | vlan_tci_to_pcp(cookie.sflow.vlan_tci), | |
457 | cookie.sflow.output); | |
458 | } else if (userdata_len == sizeof cookie.slow_path | |
459 | && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) { | |
04594cd5 BP |
460 | ds_put_cstr(ds, ",slow_path("); |
461 | format_flags(ds, slow_path_reason_to_string, | |
462 | cookie.slow_path.reason, ','); | |
463 | ds_put_format(ds, ")"); | |
29089a54 RL |
464 | } else if (userdata_len == sizeof cookie.flow_sample |
465 | && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) { | |
466 | ds_put_format(ds, ",flow_sample(probability=%"PRIu16 | |
467 | ",collector_set_id=%"PRIu32 | |
468 | ",obs_domain_id=%"PRIu32 | |
f69f713b | 469 | ",obs_point_id=%"PRIu32 |
0722f341 | 470 | ",output_port=", |
29089a54 RL |
471 | cookie.flow_sample.probability, |
472 | cookie.flow_sample.collector_set_id, | |
473 | cookie.flow_sample.obs_domain_id, | |
0722f341 BP |
474 | cookie.flow_sample.obs_point_id); |
475 | odp_portno_name_format(portno_names, | |
476 | cookie.flow_sample.output_odp_port, ds); | |
4930ea56 BP |
477 | if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_INGRESS) { |
478 | ds_put_cstr(ds, ",ingress"); | |
479 | } else if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_EGRESS) { | |
480 | ds_put_cstr(ds, ",egress"); | |
481 | } | |
482 | ds_put_char(ds, ')'); | |
b075a9b8 | 483 | } else if (userdata_len >= sizeof cookie.ipfix |
29089a54 | 484 | && cookie.type == USER_ACTION_COOKIE_IPFIX) { |
0722f341 BP |
485 | ds_put_format(ds, ",ipfix(output_port="); |
486 | odp_portno_name_format(portno_names, | |
487 | cookie.ipfix.output_odp_port, ds); | |
488 | ds_put_char(ds, ')'); | |
29089a54 RL |
489 | } else { |
490 | userdata_unspec = true; | |
491 | } | |
98403001 | 492 | } |
e995e3df | 493 | |
29089a54 RL |
494 | if (userdata_unspec) { |
495 | size_t i; | |
496 | ds_put_format(ds, ",userdata("); | |
497 | for (i = 0; i < userdata_len; i++) { | |
498 | ds_put_format(ds, "%02x", userdata[i]); | |
499 | } | |
500 | ds_put_char(ds, ')'); | |
e995e3df | 501 | } |
98403001 BP |
502 | } |
503 | ||
7321bda3 NM |
504 | if (a[OVS_USERSPACE_ATTR_ACTIONS]) { |
505 | ds_put_cstr(ds, ",actions"); | |
506 | } | |
507 | ||
8b7ea2d4 WZ |
508 | tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT]; |
509 | if (tunnel_out_port_attr) { | |
0722f341 BP |
510 | ds_put_format(ds, ",tunnel_out_port="); |
511 | odp_portno_name_format(portno_names, | |
512 | nl_attr_get_odp_port(tunnel_out_port_attr), ds); | |
8b7ea2d4 WZ |
513 | } |
514 | ||
98403001 BP |
515 | ds_put_char(ds, ')'); |
516 | } | |
517 | ||
8ddc056d | 518 | static void |
2d18eae8 | 519 | format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose) |
8ddc056d | 520 | { |
2d18eae8 JR |
521 | if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) { |
522 | ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci)); | |
523 | if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */ | |
524 | ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask)); | |
525 | }; | |
526 | ds_put_char(ds, ','); | |
527 | } | |
528 | if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) { | |
529 | ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci)); | |
530 | if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) { | |
531 | ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask)); | |
532 | } | |
533 | ds_put_char(ds, ','); | |
534 | } | |
535 | if (!(tci & htons(VLAN_CFI))) { | |
536 | ds_put_cstr(ds, "cfi=0"); | |
537 | ds_put_char(ds, ','); | |
8ddc056d | 538 | } |
2d18eae8 | 539 | ds_chomp(ds, ','); |
8ddc056d BP |
540 | } |
541 | ||
b02475c5 SH |
542 | static void |
543 | format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse) | |
544 | { | |
545 | ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d", | |
546 | mpls_lse_to_label(mpls_lse), | |
547 | mpls_lse_to_tc(mpls_lse), | |
548 | mpls_lse_to_ttl(mpls_lse), | |
549 | mpls_lse_to_bos(mpls_lse)); | |
550 | } | |
551 | ||
e6cc0bab AZ |
552 | static void |
553 | format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key, | |
8bfd0fda | 554 | const struct ovs_key_mpls *mpls_mask, int n) |
e6cc0bab | 555 | { |
8f79bb4d JR |
556 | for (int i = 0; i < n; i++) { |
557 | ovs_be32 key = mpls_key[i].mpls_lse; | |
e6cc0bab | 558 | |
8bfd0fda BP |
559 | if (mpls_mask == NULL) { |
560 | format_mpls_lse(ds, key); | |
561 | } else { | |
8f79bb4d | 562 | ovs_be32 mask = mpls_mask[i].mpls_lse; |
8bfd0fda BP |
563 | |
564 | ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", | |
565 | mpls_lse_to_label(key), mpls_lse_to_label(mask), | |
566 | mpls_lse_to_tc(key), mpls_lse_to_tc(mask), | |
567 | mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), | |
568 | mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); | |
569 | } | |
8f79bb4d | 570 | ds_put_char(ds, ','); |
e6cc0bab | 571 | } |
8f79bb4d | 572 | ds_chomp(ds, ','); |
e6cc0bab AZ |
573 | } |
574 | ||
572f732a | 575 | static void |
347bf289 | 576 | format_odp_recirc_action(struct ds *ds, uint32_t recirc_id) |
572f732a | 577 | { |
8f19f0a7 | 578 | ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id); |
347bf289 | 579 | } |
572f732a | 580 | |
347bf289 AZ |
581 | static void |
582 | format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act) | |
583 | { | |
584 | ds_put_format(ds, "hash("); | |
572f732a | 585 | |
347bf289 | 586 | if (hash_act->hash_alg == OVS_HASH_ALG_L4) { |
62ac1f20 | 587 | ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis); |
347bf289 AZ |
588 | } else { |
589 | ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")", | |
590 | hash_act->hash_alg); | |
591 | } | |
592 | ds_put_format(ds, ")"); | |
572f732a AZ |
593 | } |
594 | ||
e066f78f | 595 | static const void * |
370e373b | 596 | format_udp_tnl_push_header(struct ds *ds, const struct udp_header *udp) |
e066f78f | 597 | { |
8e45fe7c JG |
598 | ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),", |
599 | ntohs(udp->udp_src), ntohs(udp->udp_dst), | |
600 | ntohs(udp->udp_csum)); | |
e066f78f JG |
601 | |
602 | return udp + 1; | |
603 | } | |
604 | ||
a36de779 PS |
605 | static void |
606 | format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data) | |
607 | { | |
608 | const struct eth_header *eth; | |
a36de779 | 609 | const void *l3; |
370e373b TLSC |
610 | const void *l4; |
611 | const struct udp_header *udp; | |
a36de779 PS |
612 | |
613 | eth = (const struct eth_header *)data->header; | |
614 | ||
615 | l3 = eth + 1; | |
a36de779 PS |
616 | |
617 | /* Ethernet */ | |
fd13c6b5 | 618 | ds_put_format(ds, "header(size=%"PRIu32",type=%"PRIu32",eth(dst=", |
a36de779 PS |
619 | data->header_len, data->tnl_type); |
620 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst)); | |
621 | ds_put_format(ds, ",src="); | |
622 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src)); | |
623 | ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type)); | |
624 | ||
370e373b TLSC |
625 | if (eth->eth_type == htons(ETH_TYPE_IP)) { |
626 | /* IPv4 */ | |
8568c7a5 | 627 | const struct ip_header *ip = l3; |
370e373b TLSC |
628 | ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8 |
629 | ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),", | |
630 | IP_ARGS(get_16aligned_be32(&ip->ip_src)), | |
631 | IP_ARGS(get_16aligned_be32(&ip->ip_dst)), | |
632 | ip->ip_proto, ip->ip_tos, | |
633 | ip->ip_ttl, | |
fa47c114 | 634 | ntohs(ip->ip_frag_off)); |
370e373b TLSC |
635 | l4 = (ip + 1); |
636 | } else { | |
8568c7a5 BP |
637 | const struct ovs_16aligned_ip6_hdr *ip6 = l3; |
638 | struct in6_addr src, dst; | |
639 | memcpy(&src, &ip6->ip6_src, sizeof src); | |
640 | memcpy(&dst, &ip6->ip6_dst, sizeof dst); | |
641 | uint32_t ipv6_flow = ntohl(get_16aligned_be32(&ip6->ip6_flow)); | |
642 | ||
370e373b | 643 | ds_put_format(ds, "ipv6(src="); |
8568c7a5 | 644 | ipv6_format_addr(&src, ds); |
370e373b | 645 | ds_put_format(ds, ",dst="); |
8568c7a5 | 646 | ipv6_format_addr(&dst, ds); |
fd13c6b5 | 647 | ds_put_format(ds, ",label=%i,proto=%"PRIu8",tclass=0x%"PRIx32 |
370e373b | 648 | ",hlimit=%"PRIu8"),", |
8568c7a5 BP |
649 | ipv6_flow & IPV6_LABEL_MASK, ip6->ip6_nxt, |
650 | (ipv6_flow >> 20) & 0xff, ip6->ip6_hlim); | |
370e373b TLSC |
651 | l4 = (ip6 + 1); |
652 | } | |
653 | ||
654 | udp = (const struct udp_header *) l4; | |
a36de779 PS |
655 | |
656 | if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) { | |
657 | const struct vxlanhdr *vxh; | |
a36de779 | 658 | |
370e373b | 659 | vxh = format_udp_tnl_push_header(ds, udp); |
a36de779 | 660 | |
a36de779 PS |
661 | ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")", |
662 | ntohl(get_16aligned_be32(&vxh->vx_flags)), | |
a92a3aae | 663 | ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8); |
e5a1caee JG |
664 | } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) { |
665 | const struct genevehdr *gnh; | |
666 | ||
370e373b | 667 | gnh = format_udp_tnl_push_header(ds, udp); |
e5a1caee | 668 | |
5bb08b0e | 669 | ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32, |
e5a1caee | 670 | gnh->oam ? "oam," : "", |
5bb08b0e | 671 | gnh->critical ? "crit," : "", |
e5a1caee | 672 | ntohl(get_16aligned_be32(&gnh->vni)) >> 8); |
1fc11c59 | 673 | |
5bb08b0e JG |
674 | if (gnh->opt_len) { |
675 | ds_put_cstr(ds, ",options("); | |
676 | format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4, | |
677 | ds, false); | |
678 | ds_put_char(ds, ')'); | |
679 | } | |
680 | ||
681 | ds_put_char(ds, ')'); | |
a36de779 PS |
682 | } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) { |
683 | const struct gre_base_hdr *greh; | |
684 | ovs_16aligned_be32 *options; | |
a36de779 | 685 | |
a36de779 PS |
686 | greh = (const struct gre_base_hdr *) l4; |
687 | ||
688 | ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")", | |
e8fe6ad0 | 689 | ntohs(greh->flags), ntohs(greh->protocol)); |
a36de779 PS |
690 | options = (ovs_16aligned_be32 *)(greh + 1); |
691 | if (greh->flags & htons(GRE_CSUM)) { | |
d804d31e | 692 | ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options))); |
a36de779 PS |
693 | options++; |
694 | } | |
695 | if (greh->flags & htons(GRE_KEY)) { | |
696 | ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options))); | |
697 | options++; | |
698 | } | |
699 | if (greh->flags & htons(GRE_SEQ)) { | |
700 | ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options))); | |
701 | options++; | |
702 | } | |
703 | ds_put_format(ds, ")"); | |
704 | } | |
705 | ds_put_format(ds, ")"); | |
706 | } | |
707 | ||
708 | static void | |
0722f341 BP |
709 | format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr, |
710 | const struct hmap *portno_names) | |
a36de779 PS |
711 | { |
712 | struct ovs_action_push_tnl *data; | |
713 | ||
714 | data = (struct ovs_action_push_tnl *) nl_attr_get(attr); | |
715 | ||
0722f341 BP |
716 | ds_put_cstr(ds, "tnl_push(tnl_port("); |
717 | odp_portno_name_format(portno_names, data->tnl_port, ds); | |
718 | ds_put_cstr(ds, "),"); | |
a36de779 | 719 | format_odp_tnl_push_header(ds, data); |
0722f341 BP |
720 | ds_put_format(ds, ",out_port("); |
721 | odp_portno_name_format(portno_names, data->out_port, ds); | |
722 | ds_put_cstr(ds, "))"); | |
a36de779 PS |
723 | } |
724 | ||
9ac0aada JR |
725 | static const struct nl_policy ovs_nat_policy[] = { |
726 | [OVS_NAT_ATTR_SRC] = { .type = NL_A_FLAG, .optional = true, }, | |
727 | [OVS_NAT_ATTR_DST] = { .type = NL_A_FLAG, .optional = true, }, | |
728 | [OVS_NAT_ATTR_IP_MIN] = { .type = NL_A_UNSPEC, .optional = true, | |
729 | .min_len = sizeof(struct in_addr), | |
730 | .max_len = sizeof(struct in6_addr)}, | |
731 | [OVS_NAT_ATTR_IP_MAX] = { .type = NL_A_UNSPEC, .optional = true, | |
732 | .min_len = sizeof(struct in_addr), | |
733 | .max_len = sizeof(struct in6_addr)}, | |
734 | [OVS_NAT_ATTR_PROTO_MIN] = { .type = NL_A_U16, .optional = true, }, | |
735 | [OVS_NAT_ATTR_PROTO_MAX] = { .type = NL_A_U16, .optional = true, }, | |
736 | [OVS_NAT_ATTR_PERSISTENT] = { .type = NL_A_FLAG, .optional = true, }, | |
737 | [OVS_NAT_ATTR_PROTO_HASH] = { .type = NL_A_FLAG, .optional = true, }, | |
738 | [OVS_NAT_ATTR_PROTO_RANDOM] = { .type = NL_A_FLAG, .optional = true, }, | |
739 | }; | |
740 | ||
741 | static void | |
742 | format_odp_ct_nat(struct ds *ds, const struct nlattr *attr) | |
743 | { | |
744 | struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)]; | |
745 | size_t addr_len; | |
746 | ovs_be32 ip_min, ip_max; | |
747 | struct in6_addr ip6_min, ip6_max; | |
748 | uint16_t proto_min, proto_max; | |
749 | ||
750 | if (!nl_parse_nested(attr, ovs_nat_policy, a, ARRAY_SIZE(a))) { | |
751 | ds_put_cstr(ds, "nat(error: nl_parse_nested() failed.)"); | |
752 | return; | |
753 | } | |
754 | /* If no type, then nothing else either. */ | |
755 | if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) | |
756 | && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX] | |
757 | || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX] | |
758 | || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH] | |
759 | || a[OVS_NAT_ATTR_PROTO_RANDOM])) { | |
760 | ds_put_cstr(ds, "nat(error: options allowed only with \"src\" or \"dst\")"); | |
761 | return; | |
762 | } | |
763 | /* Both SNAT & DNAT may not be specified. */ | |
764 | if (a[OVS_NAT_ATTR_SRC] && a[OVS_NAT_ATTR_DST]) { | |
765 | ds_put_cstr(ds, "nat(error: Only one of \"src\" or \"dst\" may be present.)"); | |
766 | return; | |
767 | } | |
768 | /* proto may not appear without ip. */ | |
769 | if (!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_PROTO_MIN]) { | |
770 | ds_put_cstr(ds, "nat(error: proto but no IP.)"); | |
771 | return; | |
772 | } | |
773 | /* MAX may not appear without MIN. */ | |
774 | if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]) | |
775 | || (!a[OVS_NAT_ATTR_PROTO_MIN] && a[OVS_NAT_ATTR_PROTO_MAX])) { | |
776 | ds_put_cstr(ds, "nat(error: range max without min.)"); | |
777 | return; | |
778 | } | |
779 | /* Address sizes must match. */ | |
780 | if ((a[OVS_NAT_ATTR_IP_MIN] | |
781 | && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) && | |
782 | nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr))) | |
783 | || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX] | |
784 | && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) | |
785 | != nl_attr_get_size(a[OVS_NAT_ATTR_IP_MAX])))) { | |
786 | ds_put_cstr(ds, "nat(error: IP address sizes do not match)"); | |
787 | return; | |
788 | } | |
789 | ||
790 | addr_len = a[OVS_NAT_ATTR_IP_MIN] | |
791 | ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0; | |
792 | ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN] | |
793 | ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0; | |
794 | ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX] | |
795 | ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0; | |
796 | if (addr_len == sizeof ip6_min) { | |
797 | ip6_min = a[OVS_NAT_ATTR_IP_MIN] | |
798 | ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN]) | |
799 | : in6addr_any; | |
800 | ip6_max = a[OVS_NAT_ATTR_IP_MAX] | |
801 | ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX]) | |
802 | : in6addr_any; | |
803 | } | |
804 | proto_min = a[OVS_NAT_ATTR_PROTO_MIN] | |
805 | ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0; | |
806 | proto_max = a[OVS_NAT_ATTR_PROTO_MAX] | |
807 | ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0; | |
808 | ||
809 | if ((addr_len == sizeof(ovs_be32) | |
810 | && ip_max && ntohl(ip_min) > ntohl(ip_max)) | |
811 | || (addr_len == sizeof(struct in6_addr) | |
812 | && !ipv6_mask_is_any(&ip6_max) | |
813 | && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0) | |
814 | || (proto_max && proto_min > proto_max)) { | |
815 | ds_put_cstr(ds, "nat(range error)"); | |
816 | return; | |
817 | } | |
818 | ||
819 | ds_put_cstr(ds, "nat"); | |
820 | if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) { | |
821 | ds_put_char(ds, '('); | |
822 | if (a[OVS_NAT_ATTR_SRC]) { | |
823 | ds_put_cstr(ds, "src"); | |
824 | } else if (a[OVS_NAT_ATTR_DST]) { | |
825 | ds_put_cstr(ds, "dst"); | |
826 | } | |
827 | ||
828 | if (addr_len > 0) { | |
829 | ds_put_cstr(ds, "="); | |
830 | ||
831 | if (addr_len == sizeof ip_min) { | |
832 | ds_put_format(ds, IP_FMT, IP_ARGS(ip_min)); | |
833 | ||
834 | if (ip_max && ip_max != ip_min) { | |
835 | ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max)); | |
836 | } | |
837 | } else if (addr_len == sizeof ip6_min) { | |
838 | ipv6_format_addr_bracket(&ip6_min, ds, proto_min); | |
839 | ||
840 | if (!ipv6_mask_is_any(&ip6_max) && | |
841 | memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) { | |
842 | ds_put_char(ds, '-'); | |
843 | ipv6_format_addr_bracket(&ip6_max, ds, proto_min); | |
844 | } | |
845 | } | |
846 | if (proto_min) { | |
847 | ds_put_format(ds, ":%"PRIu16, proto_min); | |
848 | ||
849 | if (proto_max && proto_max != proto_min) { | |
850 | ds_put_format(ds, "-%"PRIu16, proto_max); | |
851 | } | |
852 | } | |
853 | } | |
854 | ds_put_char(ds, ','); | |
855 | if (a[OVS_NAT_ATTR_PERSISTENT]) { | |
856 | ds_put_cstr(ds, "persistent,"); | |
857 | } | |
858 | if (a[OVS_NAT_ATTR_PROTO_HASH]) { | |
859 | ds_put_cstr(ds, "hash,"); | |
860 | } | |
861 | if (a[OVS_NAT_ATTR_PROTO_RANDOM]) { | |
862 | ds_put_cstr(ds, "random,"); | |
863 | } | |
864 | ds_chomp(ds, ','); | |
865 | ds_put_char(ds, ')'); | |
866 | } | |
867 | } | |
868 | ||
07659514 JS |
869 | static const struct nl_policy ovs_conntrack_policy[] = { |
870 | [OVS_CT_ATTR_COMMIT] = { .type = NL_A_FLAG, .optional = true, }, | |
a76a37ef | 871 | [OVS_CT_ATTR_FORCE_COMMIT] = { .type = NL_A_FLAG, .optional = true, }, |
07659514 | 872 | [OVS_CT_ATTR_ZONE] = { .type = NL_A_U16, .optional = true, }, |
8e53fe8c JS |
873 | [OVS_CT_ATTR_MARK] = { .type = NL_A_UNSPEC, .optional = true, |
874 | .min_len = sizeof(uint32_t) * 2 }, | |
9daf2348 JS |
875 | [OVS_CT_ATTR_LABELS] = { .type = NL_A_UNSPEC, .optional = true, |
876 | .min_len = sizeof(struct ovs_key_ct_labels) * 2 }, | |
d787ad39 JS |
877 | [OVS_CT_ATTR_HELPER] = { .type = NL_A_STRING, .optional = true, |
878 | .min_len = 1, .max_len = 16 }, | |
9ac0aada | 879 | [OVS_CT_ATTR_NAT] = { .type = NL_A_UNSPEC, .optional = true }, |
07659514 JS |
880 | }; |
881 | ||
882 | static void | |
883 | format_odp_conntrack_action(struct ds *ds, const struct nlattr *attr) | |
884 | { | |
885 | struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)]; | |
89cf41ec BP |
886 | const struct { |
887 | ovs_32aligned_u128 value; | |
888 | ovs_32aligned_u128 mask; | |
889 | } *label; | |
8e53fe8c | 890 | const uint32_t *mark; |
d787ad39 | 891 | const char *helper; |
07659514 | 892 | uint16_t zone; |
a76a37ef | 893 | bool commit, force; |
9ac0aada | 894 | const struct nlattr *nat; |
07659514 JS |
895 | |
896 | if (!nl_parse_nested(attr, ovs_conntrack_policy, a, ARRAY_SIZE(a))) { | |
897 | ds_put_cstr(ds, "ct(error)"); | |
898 | return; | |
899 | } | |
900 | ||
901 | commit = a[OVS_CT_ATTR_COMMIT] ? true : false; | |
a76a37ef | 902 | force = a[OVS_CT_ATTR_FORCE_COMMIT] ? true : false; |
07659514 | 903 | zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0; |
8e53fe8c | 904 | mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL; |
9daf2348 | 905 | label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL; |
d787ad39 | 906 | helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL; |
9ac0aada | 907 | nat = a[OVS_CT_ATTR_NAT]; |
07659514 JS |
908 | |
909 | ds_put_format(ds, "ct"); | |
a76a37ef | 910 | if (commit || force || zone || mark || label || helper || nat) { |
07659514 JS |
911 | ds_put_cstr(ds, "("); |
912 | if (commit) { | |
913 | ds_put_format(ds, "commit,"); | |
914 | } | |
a76a37ef JR |
915 | if (force) { |
916 | ds_put_format(ds, "force_commit,"); | |
917 | } | |
07659514 JS |
918 | if (zone) { |
919 | ds_put_format(ds, "zone=%"PRIu16",", zone); | |
920 | } | |
8e53fe8c JS |
921 | if (mark) { |
922 | ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark, | |
923 | *(mark + 1)); | |
924 | } | |
9daf2348 JS |
925 | if (label) { |
926 | ds_put_format(ds, "label="); | |
89cf41ec | 927 | format_u128(ds, &label->value, &label->mask, true); |
121cf327 | 928 | ds_put_char(ds, ','); |
9daf2348 | 929 | } |
d787ad39 JS |
930 | if (helper) { |
931 | ds_put_format(ds, "helper=%s,", helper); | |
932 | } | |
9ac0aada JR |
933 | if (nat) { |
934 | format_odp_ct_nat(ds, nat); | |
935 | } | |
07659514 JS |
936 | ds_chomp(ds, ','); |
937 | ds_put_cstr(ds, ")"); | |
938 | } | |
939 | } | |
940 | ||
4edb9ae9 | 941 | static void |
0722f341 BP |
942 | format_odp_action(struct ds *ds, const struct nlattr *a, |
943 | const struct hmap *portno_names) | |
064af421 | 944 | { |
98403001 | 945 | int expected_len; |
4edb9ae9 | 946 | enum ovs_action_attr type = nl_attr_type(a); |
6d670e7f | 947 | size_t size; |
cdee00fd | 948 | |
98403001 | 949 | expected_len = odp_action_len(nl_attr_type(a)); |
6b8da9e9 JG |
950 | if (expected_len != ATTR_LEN_VARIABLE && |
951 | nl_attr_get_size(a) != expected_len) { | |
34582733 | 952 | ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ", |
98403001 | 953 | nl_attr_get_size(a), expected_len); |
cdee00fd BP |
954 | format_generic_odp_action(ds, a); |
955 | return; | |
956 | } | |
957 | ||
4edb9ae9 | 958 | switch (type) { |
5dddf960 JR |
959 | case OVS_ACTION_ATTR_METER: |
960 | ds_put_format(ds, "meter(%"PRIu32")", nl_attr_get_u32(a)); | |
961 | break; | |
df2c07f4 | 962 | case OVS_ACTION_ATTR_OUTPUT: |
0722f341 | 963 | odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds); |
064af421 | 964 | break; |
aaca4fe0 WT |
965 | case OVS_ACTION_ATTR_TRUNC: { |
966 | const struct ovs_action_trunc *trunc = | |
967 | nl_attr_get_unspec(a, sizeof *trunc); | |
968 | ||
969 | ds_put_format(ds, "trunc(%"PRIu32")", trunc->max_len); | |
970 | break; | |
971 | } | |
a36de779 | 972 | case OVS_ACTION_ATTR_TUNNEL_POP: |
0722f341 BP |
973 | ds_put_cstr(ds, "tnl_pop("); |
974 | odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds); | |
975 | ds_put_char(ds, ')'); | |
a36de779 PS |
976 | break; |
977 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
0722f341 | 978 | format_odp_tnl_push_action(ds, a, portno_names); |
a36de779 | 979 | break; |
df2c07f4 | 980 | case OVS_ACTION_ATTR_USERSPACE: |
0722f341 | 981 | format_odp_userspace_action(ds, a, portno_names); |
064af421 | 982 | break; |
572f732a | 983 | case OVS_ACTION_ATTR_RECIRC: |
347bf289 AZ |
984 | format_odp_recirc_action(ds, nl_attr_get_u32(a)); |
985 | break; | |
986 | case OVS_ACTION_ATTR_HASH: | |
987 | format_odp_hash_action(ds, nl_attr_get(a)); | |
572f732a | 988 | break; |
6d670e7f JR |
989 | case OVS_ACTION_ATTR_SET_MASKED: |
990 | a = nl_attr_get(a); | |
991 | size = nl_attr_get_size(a) / 2; | |
992 | ds_put_cstr(ds, "set("); | |
993 | ||
994 | /* Masked set action not supported for tunnel key, which is bigger. */ | |
995 | if (size <= sizeof(struct ovs_key_ipv6)) { | |
996 | struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6), | |
997 | sizeof(struct nlattr))]; | |
998 | struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6), | |
999 | sizeof(struct nlattr))]; | |
1000 | ||
1001 | mask->nla_type = attr->nla_type = nl_attr_type(a); | |
1002 | mask->nla_len = attr->nla_len = NLA_HDRLEN + size; | |
1003 | memcpy(attr + 1, (char *)(a + 1), size); | |
1004 | memcpy(mask + 1, (char *)(a + 1) + size, size); | |
2d18eae8 | 1005 | format_odp_key_attr(attr, mask, NULL, ds, false); |
6d670e7f | 1006 | } else { |
2d18eae8 | 1007 | format_odp_key_attr(a, NULL, NULL, ds, false); |
6d670e7f JR |
1008 | } |
1009 | ds_put_cstr(ds, ")"); | |
1010 | break; | |
4edb9ae9 PS |
1011 | case OVS_ACTION_ATTR_SET: |
1012 | ds_put_cstr(ds, "set("); | |
0a37839c | 1013 | format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true); |
4edb9ae9 | 1014 | ds_put_cstr(ds, ")"); |
6fcecb85 YY |
1015 | break; |
1016 | case OVS_ACTION_ATTR_PUSH_ETH: { | |
1017 | const struct ovs_action_push_eth *eth = nl_attr_get(a); | |
1018 | ds_put_format(ds, "push_eth(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")", | |
1019 | ETH_ADDR_ARGS(eth->addresses.eth_src), | |
1020 | ETH_ADDR_ARGS(eth->addresses.eth_dst)); | |
1021 | break; | |
1022 | } | |
1023 | case OVS_ACTION_ATTR_POP_ETH: | |
1024 | ds_put_cstr(ds, "pop_eth"); | |
064af421 | 1025 | break; |
66026437 SH |
1026 | case OVS_ACTION_ATTR_PUSH_VLAN: { |
1027 | const struct ovs_action_push_vlan *vlan = nl_attr_get(a); | |
fea393b1 BP |
1028 | ds_put_cstr(ds, "push_vlan("); |
1029 | if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) { | |
1030 | ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid)); | |
1031 | } | |
2d18eae8 | 1032 | format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false); |
8ddc056d | 1033 | ds_put_char(ds, ')'); |
064af421 | 1034 | break; |
66026437 | 1035 | } |
fea393b1 BP |
1036 | case OVS_ACTION_ATTR_POP_VLAN: |
1037 | ds_put_cstr(ds, "pop_vlan"); | |
064af421 | 1038 | break; |
b02475c5 SH |
1039 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
1040 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
1041 | ds_put_cstr(ds, "push_mpls("); | |
1042 | format_mpls_lse(ds, mpls->mpls_lse); | |
1043 | ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype)); | |
1044 | break; | |
1045 | } | |
1046 | case OVS_ACTION_ATTR_POP_MPLS: { | |
1047 | ovs_be16 ethertype = nl_attr_get_be16(a); | |
1048 | ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype)); | |
1049 | break; | |
1050 | } | |
6ff686f2 | 1051 | case OVS_ACTION_ATTR_SAMPLE: |
0722f341 | 1052 | format_odp_sample_action(ds, a, portno_names); |
6ff686f2 | 1053 | break; |
07659514 JS |
1054 | case OVS_ACTION_ATTR_CT: |
1055 | format_odp_conntrack_action(ds, a); | |
1056 | break; | |
535e3acf | 1057 | case OVS_ACTION_ATTR_CLONE: |
0722f341 | 1058 | format_odp_clone_action(ds, a, portno_names); |
535e3acf | 1059 | break; |
1fc11c59 JS |
1060 | case OVS_ACTION_ATTR_ENCAP_NSH: |
1061 | format_odp_encap_nsh_action(ds, nl_attr_get(a)); | |
1062 | break; | |
1063 | case OVS_ACTION_ATTR_DECAP_NSH: | |
1064 | ds_put_cstr(ds, "decap_nsh()"); | |
1065 | break; | |
4edb9ae9 PS |
1066 | case OVS_ACTION_ATTR_UNSPEC: |
1067 | case __OVS_ACTION_ATTR_MAX: | |
064af421 | 1068 | default: |
cdee00fd | 1069 | format_generic_odp_action(ds, a); |
064af421 BP |
1070 | break; |
1071 | } | |
1072 | } | |
1073 | ||
1074 | void | |
cdee00fd | 1075 | format_odp_actions(struct ds *ds, const struct nlattr *actions, |
0722f341 | 1076 | size_t actions_len, const struct hmap *portno_names) |
064af421 | 1077 | { |
cdee00fd BP |
1078 | if (actions_len) { |
1079 | const struct nlattr *a; | |
1080 | unsigned int left; | |
1081 | ||
1082 | NL_ATTR_FOR_EACH (a, left, actions, actions_len) { | |
1083 | if (a != actions) { | |
1084 | ds_put_char(ds, ','); | |
1085 | } | |
0722f341 | 1086 | format_odp_action(ds, a, portno_names); |
064af421 | 1087 | } |
cdee00fd | 1088 | if (left) { |
3a8cfc50 BP |
1089 | int i; |
1090 | ||
3476fce3 BP |
1091 | if (left == actions_len) { |
1092 | ds_put_cstr(ds, "<empty>"); | |
1093 | } | |
3a8cfc50 BP |
1094 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
1095 | for (i = 0; i < left; i++) { | |
1096 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
1097 | } | |
1098 | ds_put_char(ds, ')'); | |
cdee00fd BP |
1099 | } |
1100 | } else { | |
064af421 BP |
1101 | ds_put_cstr(ds, "drop"); |
1102 | } | |
1103 | } | |
7202cbe5 | 1104 | |
8b7ea2d4 | 1105 | /* Separate out parse_odp_userspace_action() function. */ |
7202cbe5 | 1106 | static int |
8b7ea2d4 | 1107 | parse_odp_userspace_action(const char *s, struct ofpbuf *actions) |
7202cbe5 | 1108 | { |
8b7ea2d4 WZ |
1109 | uint32_t pid; |
1110 | union user_action_cookie cookie; | |
1111 | struct ofpbuf buf; | |
1112 | odp_port_t tunnel_out_port; | |
1113 | int n = -1; | |
1114 | void *user_data = NULL; | |
1115 | size_t user_data_size = 0; | |
7321bda3 | 1116 | bool include_actions = false; |
9d4e54c6 | 1117 | int res; |
8b7ea2d4 WZ |
1118 | |
1119 | if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) { | |
1120 | return -EINVAL; | |
7202cbe5 BP |
1121 | } |
1122 | ||
9d4e54c6 WT |
1123 | ofpbuf_init(&buf, 16); |
1124 | ||
7202cbe5 | 1125 | { |
c2c28dfd BP |
1126 | uint32_t output; |
1127 | uint32_t probability; | |
1128 | uint32_t collector_set_id; | |
1129 | uint32_t obs_domain_id; | |
1130 | uint32_t obs_point_id; | |
7202cbe5 | 1131 | int vid, pcp; |
8b7ea2d4 WZ |
1132 | int n1 = -1; |
1133 | if (ovs_scan(&s[n], ",sFlow(vid=%i," | |
1134 | "pcp=%i,output=%"SCNi32")%n", | |
1135 | &vid, &pcp, &output, &n1)) { | |
7202cbe5 BP |
1136 | uint16_t tci; |
1137 | ||
8b7ea2d4 | 1138 | n += n1; |
7202cbe5 BP |
1139 | tci = vid | (pcp << VLAN_PCP_SHIFT); |
1140 | if (tci) { | |
1141 | tci |= VLAN_CFI; | |
1142 | } | |
1143 | ||
1144 | cookie.type = USER_ACTION_COOKIE_SFLOW; | |
1673e0e4 BP |
1145 | cookie.sflow.vlan_tci = htons(tci); |
1146 | cookie.sflow.output = output; | |
8b7ea2d4 WZ |
1147 | user_data = &cookie; |
1148 | user_data_size = sizeof cookie.sflow; | |
2d18eae8 | 1149 | } else if (ovs_scan(&s[n], ",slow_path(%n", |
8b7ea2d4 | 1150 | &n1)) { |
8b7ea2d4 | 1151 | n += n1; |
6a7e895f BP |
1152 | cookie.type = USER_ACTION_COOKIE_SLOW_PATH; |
1153 | cookie.slow_path.unused = 0; | |
04594cd5 | 1154 | cookie.slow_path.reason = 0; |
6a7e895f | 1155 | |
8e4c1621 JG |
1156 | res = parse_odp_flags(&s[n], slow_path_reason_to_string, |
1157 | &cookie.slow_path.reason, | |
1158 | SLOW_PATH_REASON_MASK, NULL); | |
2d18eae8 | 1159 | if (res < 0 || s[n + res] != ')') { |
9d4e54c6 | 1160 | goto out; |
04594cd5 | 1161 | } |
2d18eae8 | 1162 | n += res + 1; |
6a7e895f | 1163 | |
8b7ea2d4 WZ |
1164 | user_data = &cookie; |
1165 | user_data_size = sizeof cookie.slow_path; | |
1166 | } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32"," | |
c2c28dfd BP |
1167 | "collector_set_id=%"SCNi32"," |
1168 | "obs_domain_id=%"SCNi32"," | |
f69f713b | 1169 | "obs_point_id=%"SCNi32"," |
4930ea56 | 1170 | "output_port=%"SCNi32"%n", |
8b7ea2d4 | 1171 | &probability, &collector_set_id, |
f69f713b BY |
1172 | &obs_domain_id, &obs_point_id, |
1173 | &output, &n1)) { | |
8b7ea2d4 | 1174 | n += n1; |
29089a54 RL |
1175 | |
1176 | cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE; | |
1177 | cookie.flow_sample.probability = probability; | |
1178 | cookie.flow_sample.collector_set_id = collector_set_id; | |
1179 | cookie.flow_sample.obs_domain_id = obs_domain_id; | |
1180 | cookie.flow_sample.obs_point_id = obs_point_id; | |
f69f713b | 1181 | cookie.flow_sample.output_odp_port = u32_to_odp(output); |
8b7ea2d4 WZ |
1182 | user_data = &cookie; |
1183 | user_data_size = sizeof cookie.flow_sample; | |
4930ea56 BP |
1184 | |
1185 | if (ovs_scan(&s[n], ",ingress%n", &n1)) { | |
1186 | cookie.flow_sample.direction = NX_ACTION_SAMPLE_INGRESS; | |
1187 | n += n1; | |
1188 | } else if (ovs_scan(&s[n], ",egress%n", &n1)) { | |
1189 | cookie.flow_sample.direction = NX_ACTION_SAMPLE_EGRESS; | |
1190 | n += n1; | |
1191 | } else { | |
1192 | cookie.flow_sample.direction = NX_ACTION_SAMPLE_DEFAULT; | |
1193 | } | |
1194 | if (s[n] != ')') { | |
1195 | res = -EINVAL; | |
1196 | goto out; | |
1197 | } | |
1198 | n++; | |
8b7ea2d4 WZ |
1199 | } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n", |
1200 | &output, &n1) ) { | |
1201 | n += n1; | |
29089a54 | 1202 | cookie.type = USER_ACTION_COOKIE_IPFIX; |
8b7ea2d4 WZ |
1203 | cookie.ipfix.output_odp_port = u32_to_odp(output); |
1204 | user_data = &cookie; | |
1205 | user_data_size = sizeof cookie.ipfix; | |
1206 | } else if (ovs_scan(&s[n], ",userdata(%n", | |
1207 | &n1)) { | |
e995e3df BP |
1208 | char *end; |
1209 | ||
8b7ea2d4 | 1210 | n += n1; |
e995e3df | 1211 | end = ofpbuf_put_hex(&buf, &s[n], NULL); |
8b7ea2d4 | 1212 | if (end[0] != ')') { |
9d4e54c6 WT |
1213 | res = -EINVAL; |
1214 | goto out; | |
e995e3df | 1215 | } |
6fd6ed71 PS |
1216 | user_data = buf.data; |
1217 | user_data_size = buf.size; | |
8b7ea2d4 WZ |
1218 | n = (end + 1) - s; |
1219 | } | |
1220 | } | |
1221 | ||
7321bda3 NM |
1222 | { |
1223 | int n1 = -1; | |
1224 | if (ovs_scan(&s[n], ",actions%n", &n1)) { | |
1225 | n += n1; | |
1226 | include_actions = true; | |
1227 | } | |
1228 | } | |
1229 | ||
8b7ea2d4 WZ |
1230 | { |
1231 | int n1 = -1; | |
1232 | if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n", | |
1233 | &tunnel_out_port, &n1)) { | |
7321bda3 NM |
1234 | odp_put_userspace_action(pid, user_data, user_data_size, |
1235 | tunnel_out_port, include_actions, actions); | |
9d4e54c6 | 1236 | res = n + n1; |
88fc5281 | 1237 | goto out; |
8b7ea2d4 | 1238 | } else if (s[n] == ')') { |
7321bda3 NM |
1239 | odp_put_userspace_action(pid, user_data, user_data_size, |
1240 | ODPP_NONE, include_actions, actions); | |
9d4e54c6 | 1241 | res = n + 1; |
88fc5281 JS |
1242 | goto out; |
1243 | } | |
1244 | } | |
1245 | ||
1246 | { | |
1247 | struct ovs_action_push_eth push; | |
1248 | int eth_type = 0; | |
1249 | int n1 = -1; | |
1250 | ||
1251 | if (ovs_scan(&s[n], "push_eth(src="ETH_ADDR_SCAN_FMT"," | |
1252 | "dst="ETH_ADDR_SCAN_FMT",type=%i)%n", | |
1253 | ETH_ADDR_SCAN_ARGS(push.addresses.eth_src), | |
1254 | ETH_ADDR_SCAN_ARGS(push.addresses.eth_dst), | |
1255 | ð_type, &n1)) { | |
1256 | ||
1257 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_ETH, | |
1258 | &push, sizeof push); | |
1259 | ||
1260 | res = n + n1; | |
1261 | goto out; | |
8b7ea2d4 WZ |
1262 | } |
1263 | } | |
88fc5281 JS |
1264 | |
1265 | if (!strncmp(&s[n], "pop_eth", 7)) { | |
1266 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_ETH); | |
1267 | res = 7; | |
1268 | goto out; | |
1269 | } | |
1270 | ||
1271 | res = -EINVAL; | |
9d4e54c6 WT |
1272 | out: |
1273 | ofpbuf_uninit(&buf); | |
1274 | return res; | |
8b7ea2d4 WZ |
1275 | } |
1276 | ||
a36de779 PS |
1277 | static int |
1278 | ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data) | |
1279 | { | |
1280 | struct eth_header *eth; | |
1281 | struct ip_header *ip; | |
370e373b | 1282 | struct ovs_16aligned_ip6_hdr *ip6; |
a36de779 PS |
1283 | struct udp_header *udp; |
1284 | struct gre_base_hdr *greh; | |
e8fe6ad0 | 1285 | uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum; |
a36de779 | 1286 | ovs_be32 sip, dip; |
370e373b | 1287 | uint32_t tnl_type = 0, header_len = 0, ip_len = 0; |
a36de779 PS |
1288 | void *l3, *l4; |
1289 | int n = 0; | |
1290 | ||
1291 | if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) { | |
1292 | return -EINVAL; | |
1293 | } | |
1294 | eth = (struct eth_header *) data->header; | |
1620b7ea | 1295 | l3 = (struct ip_header *) (eth + 1); |
a36de779 | 1296 | ip = (struct ip_header *) l3; |
370e373b | 1297 | ip6 = (struct ovs_16aligned_ip6_hdr *) l3; |
a36de779 | 1298 | if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32"," |
9ac0aada JR |
1299 | "eth(dst="ETH_ADDR_SCAN_FMT",", |
1300 | &data->header_len, | |
1301 | &data->tnl_type, | |
1302 | ETH_ADDR_SCAN_ARGS(eth->eth_dst))) { | |
a36de779 PS |
1303 | return -EINVAL; |
1304 | } | |
1305 | ||
1306 | if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",", | |
9ac0aada | 1307 | ETH_ADDR_SCAN_ARGS(eth->eth_src))) { |
a36de779 PS |
1308 | return -EINVAL; |
1309 | } | |
1310 | if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) { | |
1311 | return -EINVAL; | |
1312 | } | |
1313 | eth->eth_type = htons(dl_type); | |
1314 | ||
370e373b TLSC |
1315 | if (eth->eth_type == htons(ETH_TYPE_IP)) { |
1316 | /* IPv4 */ | |
fa47c114 | 1317 | uint16_t ip_frag_off; |
370e373b TLSC |
1318 | if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8 |
1319 | ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),", | |
1320 | IP_SCAN_ARGS(&sip), | |
1321 | IP_SCAN_ARGS(&dip), | |
1322 | &ip->ip_proto, &ip->ip_tos, | |
fa47c114 | 1323 | &ip->ip_ttl, &ip_frag_off)) { |
370e373b TLSC |
1324 | return -EINVAL; |
1325 | } | |
1326 | put_16aligned_be32(&ip->ip_src, sip); | |
1327 | put_16aligned_be32(&ip->ip_dst, dip); | |
fa47c114 | 1328 | ip->ip_frag_off = htons(ip_frag_off); |
370e373b TLSC |
1329 | ip_len = sizeof *ip; |
1330 | } else { | |
1331 | char sip6_s[IPV6_SCAN_LEN + 1]; | |
1332 | char dip6_s[IPV6_SCAN_LEN + 1]; | |
1333 | struct in6_addr sip6, dip6; | |
1334 | uint8_t tclass; | |
1335 | uint32_t label; | |
1336 | if (!ovs_scan_len(s, &n, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT | |
1337 | ",label=%i,proto=%"SCNi8",tclass=0x%"SCNx8 | |
1338 | ",hlimit=%"SCNi8"),", | |
1339 | sip6_s, dip6_s, &label, &ip6->ip6_nxt, | |
1340 | &tclass, &ip6->ip6_hlim) | |
1341 | || (label & ~IPV6_LABEL_MASK) != 0 | |
1342 | || inet_pton(AF_INET6, sip6_s, &sip6) != 1 | |
1343 | || inet_pton(AF_INET6, dip6_s, &dip6) != 1) { | |
1344 | return -EINVAL; | |
1345 | } | |
1346 | put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) | | |
1347 | htonl(tclass << 20) | htonl(label)); | |
1348 | memcpy(&ip6->ip6_src, &sip6, sizeof(ip6->ip6_src)); | |
1349 | memcpy(&ip6->ip6_dst, &dip6, sizeof(ip6->ip6_dst)); | |
1350 | ip_len = sizeof *ip6; | |
a36de779 | 1351 | } |
a36de779 PS |
1352 | |
1353 | /* Tunnel header */ | |
370e373b | 1354 | l4 = ((uint8_t *) l3 + ip_len); |
a36de779 PS |
1355 | udp = (struct udp_header *) l4; |
1356 | greh = (struct gre_base_hdr *) l4; | |
8e45fe7c | 1357 | if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),", |
9ac0aada | 1358 | &udp_src, &udp_dst, &csum)) { |
e5a1caee | 1359 | uint32_t vx_flags, vni; |
a36de779 PS |
1360 | |
1361 | udp->udp_src = htons(udp_src); | |
1362 | udp->udp_dst = htons(udp_dst); | |
1363 | udp->udp_len = 0; | |
8e45fe7c | 1364 | udp->udp_csum = htons(csum); |
a36de779 | 1365 | |
e066f78f | 1366 | if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))", |
9ac0aada | 1367 | &vx_flags, &vni)) { |
e066f78f JG |
1368 | struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1); |
1369 | ||
1370 | put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags)); | |
e5a1caee | 1371 | put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8)); |
e066f78f | 1372 | tnl_type = OVS_VPORT_TYPE_VXLAN; |
370e373b | 1373 | header_len = sizeof *eth + ip_len + |
e066f78f | 1374 | sizeof *udp + sizeof *vxh; |
e5a1caee JG |
1375 | } else if (ovs_scan_len(s, &n, "geneve(")) { |
1376 | struct genevehdr *gnh = (struct genevehdr *) (udp + 1); | |
1377 | ||
46e7137c | 1378 | memset(gnh, 0, sizeof *gnh); |
370e373b | 1379 | header_len = sizeof *eth + ip_len + |
5bb08b0e JG |
1380 | sizeof *udp + sizeof *gnh; |
1381 | ||
e5a1caee JG |
1382 | if (ovs_scan_len(s, &n, "oam,")) { |
1383 | gnh->oam = 1; | |
1384 | } | |
5bb08b0e JG |
1385 | if (ovs_scan_len(s, &n, "crit,")) { |
1386 | gnh->critical = 1; | |
1387 | } | |
1388 | if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) { | |
e5a1caee JG |
1389 | return -EINVAL; |
1390 | } | |
5bb08b0e JG |
1391 | if (ovs_scan_len(s, &n, ",options(")) { |
1392 | struct geneve_scan options; | |
1393 | int len; | |
1394 | ||
1395 | memset(&options, 0, sizeof options); | |
1396 | len = scan_geneve(s + n, &options, NULL); | |
1397 | if (!len) { | |
1398 | return -EINVAL; | |
1399 | } | |
1400 | ||
1401 | memcpy(gnh->options, options.d, options.len); | |
1402 | gnh->opt_len = options.len / 4; | |
1403 | header_len += options.len; | |
1404 | ||
1405 | n += len; | |
1406 | } | |
1407 | if (!ovs_scan_len(s, &n, "))")) { | |
1408 | return -EINVAL; | |
1409 | } | |
1410 | ||
e5a1caee JG |
1411 | gnh->proto_type = htons(ETH_TYPE_TEB); |
1412 | put_16aligned_be32(&gnh->vni, htonl(vni << 8)); | |
1413 | tnl_type = OVS_VPORT_TYPE_GENEVE; | |
e066f78f | 1414 | } else { |
a36de779 PS |
1415 | return -EINVAL; |
1416 | } | |
a36de779 | 1417 | } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")", |
9ac0aada | 1418 | &gre_flags, &gre_proto)){ |
a36de779 PS |
1419 | |
1420 | tnl_type = OVS_VPORT_TYPE_GRE; | |
e8fe6ad0 | 1421 | greh->flags = htons(gre_flags); |
a36de779 PS |
1422 | greh->protocol = htons(gre_proto); |
1423 | ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1); | |
1424 | ||
1425 | if (greh->flags & htons(GRE_CSUM)) { | |
d804d31e | 1426 | if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) { |
a36de779 PS |
1427 | return -EINVAL; |
1428 | } | |
d804d31e JG |
1429 | |
1430 | memset(options, 0, sizeof *options); | |
1431 | *((ovs_be16 *)options) = htons(csum); | |
a36de779 PS |
1432 | options++; |
1433 | } | |
1434 | if (greh->flags & htons(GRE_KEY)) { | |
1435 | uint32_t key; | |
1436 | ||
1437 | if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) { | |
1438 | return -EINVAL; | |
1439 | } | |
1440 | ||
1441 | put_16aligned_be32(options, htonl(key)); | |
1442 | options++; | |
1443 | } | |
1444 | if (greh->flags & htons(GRE_SEQ)) { | |
1445 | uint32_t seq; | |
1446 | ||
1447 | if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) { | |
1448 | return -EINVAL; | |
1449 | } | |
1450 | put_16aligned_be32(options, htonl(seq)); | |
1451 | options++; | |
1452 | } | |
1453 | ||
1454 | if (!ovs_scan_len(s, &n, "))")) { | |
1455 | return -EINVAL; | |
1456 | } | |
1457 | ||
370e373b | 1458 | header_len = sizeof *eth + ip_len + |
a36de779 PS |
1459 | ((uint8_t *) options - (uint8_t *) greh); |
1460 | } else { | |
1461 | return -EINVAL; | |
1462 | } | |
1463 | ||
1464 | /* check tunnel meta data. */ | |
1465 | if (data->tnl_type != tnl_type) { | |
1466 | return -EINVAL; | |
1467 | } | |
1468 | if (data->header_len != header_len) { | |
1469 | return -EINVAL; | |
1470 | } | |
1471 | ||
1472 | /* Out port */ | |
1473 | if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) { | |
1474 | return -EINVAL; | |
1475 | } | |
1476 | ||
1477 | return n; | |
1478 | } | |
1479 | ||
9ac0aada JR |
1480 | struct ct_nat_params { |
1481 | bool snat; | |
1482 | bool dnat; | |
1483 | size_t addr_len; | |
1484 | union { | |
1485 | ovs_be32 ip; | |
1486 | struct in6_addr ip6; | |
1487 | } addr_min; | |
1488 | union { | |
1489 | ovs_be32 ip; | |
1490 | struct in6_addr ip6; | |
1491 | } addr_max; | |
1492 | uint16_t proto_min; | |
1493 | uint16_t proto_max; | |
1494 | bool persistent; | |
1495 | bool proto_hash; | |
1496 | bool proto_random; | |
1497 | }; | |
1498 | ||
1499 | static int | |
1500 | scan_ct_nat_range(const char *s, int *n, struct ct_nat_params *p) | |
1501 | { | |
1502 | if (ovs_scan_len(s, n, "=")) { | |
1503 | char ipv6_s[IPV6_SCAN_LEN + 1]; | |
1504 | struct in6_addr ipv6; | |
1505 | ||
1506 | if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) { | |
1507 | p->addr_len = sizeof p->addr_min.ip; | |
1508 | if (ovs_scan_len(s, n, "-")) { | |
1509 | if (!ovs_scan_len(s, n, IP_SCAN_FMT, | |
1510 | IP_SCAN_ARGS(&p->addr_max.ip))) { | |
1511 | return -EINVAL; | |
1512 | } | |
1513 | } | |
1514 | } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s) | |
1515 | || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s)) | |
1516 | && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) { | |
1517 | p->addr_len = sizeof p->addr_min.ip6; | |
1518 | p->addr_min.ip6 = ipv6; | |
1519 | if (ovs_scan_len(s, n, "-")) { | |
1520 | if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s) | |
1521 | || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s)) | |
1522 | && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) { | |
1523 | p->addr_max.ip6 = ipv6; | |
1524 | } else { | |
1525 | return -EINVAL; | |
1526 | } | |
1527 | } | |
1528 | } else { | |
1529 | return -EINVAL; | |
1530 | } | |
1531 | if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) { | |
1532 | if (ovs_scan_len(s, n, "-")) { | |
1533 | if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) { | |
1534 | return -EINVAL; | |
1535 | } | |
1536 | } | |
1537 | } | |
1538 | } | |
1539 | return 0; | |
1540 | } | |
1541 | ||
1542 | static int | |
1543 | scan_ct_nat(const char *s, struct ct_nat_params *p) | |
1544 | { | |
1545 | int n = 0; | |
1546 | ||
1547 | if (ovs_scan_len(s, &n, "nat")) { | |
1548 | memset(p, 0, sizeof *p); | |
1549 | ||
1550 | if (ovs_scan_len(s, &n, "(")) { | |
1551 | char *end; | |
1552 | int end_n; | |
1553 | ||
1554 | end = strchr(s + n, ')'); | |
1555 | if (!end) { | |
1556 | return -EINVAL; | |
1557 | } | |
1558 | end_n = end - s; | |
1559 | ||
1560 | while (n < end_n) { | |
1561 | n += strspn(s + n, delimiters); | |
1562 | if (ovs_scan_len(s, &n, "src")) { | |
1563 | int err = scan_ct_nat_range(s, &n, p); | |
1564 | if (err) { | |
1565 | return err; | |
1566 | } | |
1567 | p->snat = true; | |
1568 | continue; | |
1569 | } | |
1570 | if (ovs_scan_len(s, &n, "dst")) { | |
1571 | int err = scan_ct_nat_range(s, &n, p); | |
1572 | if (err) { | |
1573 | return err; | |
1574 | } | |
1575 | p->dnat = true; | |
1576 | continue; | |
1577 | } | |
1578 | if (ovs_scan_len(s, &n, "persistent")) { | |
1579 | p->persistent = true; | |
1580 | continue; | |
1581 | } | |
1582 | if (ovs_scan_len(s, &n, "hash")) { | |
1583 | p->proto_hash = true; | |
1584 | continue; | |
1585 | } | |
1586 | if (ovs_scan_len(s, &n, "random")) { | |
1587 | p->proto_random = true; | |
1588 | continue; | |
1589 | } | |
1590 | return -EINVAL; | |
1591 | } | |
1592 | ||
1593 | if (p->snat && p->dnat) { | |
1594 | return -EINVAL; | |
1595 | } | |
1596 | if ((p->addr_len != 0 && | |
1597 | memcmp(&p->addr_max, &in6addr_any, p->addr_len) && | |
1598 | memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) || | |
1599 | (p->proto_max && p->proto_max < p->proto_min)) { | |
1600 | return -EINVAL; | |
1601 | } | |
1602 | if (p->proto_hash && p->proto_random) { | |
1603 | return -EINVAL; | |
1604 | } | |
1605 | n++; | |
1606 | } | |
1607 | } | |
1608 | return n; | |
1609 | } | |
1610 | ||
1611 | static void | |
1612 | nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions) | |
1613 | { | |
1614 | size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT); | |
1615 | ||
1616 | if (p->snat) { | |
1617 | nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC); | |
1618 | } else if (p->dnat) { | |
1619 | nl_msg_put_flag(actions, OVS_NAT_ATTR_DST); | |
1620 | } else { | |
1621 | goto out; | |
1622 | } | |
1623 | if (p->addr_len != 0) { | |
1624 | nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min, | |
1625 | p->addr_len); | |
1626 | if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) { | |
1627 | nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max, | |
1628 | p->addr_len); | |
1629 | } | |
1630 | if (p->proto_min) { | |
1631 | nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min); | |
1632 | if (p->proto_max && p->proto_max > p->proto_min) { | |
1633 | nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max); | |
1634 | } | |
1635 | } | |
1636 | if (p->persistent) { | |
1637 | nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT); | |
1638 | } | |
1639 | if (p->proto_hash) { | |
1640 | nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH); | |
1641 | } | |
1642 | if (p->proto_random) { | |
1643 | nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM); | |
1644 | } | |
1645 | } | |
1646 | out: | |
1647 | nl_msg_end_nested(actions, start); | |
1648 | } | |
1649 | ||
07659514 JS |
1650 | static int |
1651 | parse_conntrack_action(const char *s_, struct ofpbuf *actions) | |
1652 | { | |
1653 | const char *s = s_; | |
1654 | ||
1655 | if (ovs_scan(s, "ct")) { | |
d787ad39 JS |
1656 | const char *helper = NULL; |
1657 | size_t helper_len = 0; | |
07659514 | 1658 | bool commit = false; |
a76a37ef | 1659 | bool force_commit = false; |
07659514 | 1660 | uint16_t zone = 0; |
8e53fe8c JS |
1661 | struct { |
1662 | uint32_t value; | |
1663 | uint32_t mask; | |
1664 | } ct_mark = { 0, 0 }; | |
9daf2348 JS |
1665 | struct { |
1666 | ovs_u128 value; | |
1667 | ovs_u128 mask; | |
1668 | } ct_label; | |
9ac0aada JR |
1669 | struct ct_nat_params nat_params; |
1670 | bool have_nat = false; | |
07659514 JS |
1671 | size_t start; |
1672 | char *end; | |
1673 | ||
9daf2348 JS |
1674 | memset(&ct_label, 0, sizeof(ct_label)); |
1675 | ||
07659514 JS |
1676 | s += 2; |
1677 | if (ovs_scan(s, "(")) { | |
1678 | s++; | |
9ac0aada | 1679 | find_end: |
07659514 JS |
1680 | end = strchr(s, ')'); |
1681 | if (!end) { | |
1682 | return -EINVAL; | |
1683 | } | |
1684 | ||
1685 | while (s != end) { | |
9ac0aada | 1686 | int n; |
07659514 JS |
1687 | |
1688 | s += strspn(s, delimiters); | |
1689 | if (ovs_scan(s, "commit%n", &n)) { | |
1690 | commit = true; | |
1691 | s += n; | |
1692 | continue; | |
1693 | } | |
a76a37ef JR |
1694 | if (ovs_scan(s, "force_commit%n", &n)) { |
1695 | force_commit = true; | |
1696 | s += n; | |
1697 | continue; | |
1698 | } | |
07659514 JS |
1699 | if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) { |
1700 | s += n; | |
1701 | continue; | |
1702 | } | |
8e53fe8c JS |
1703 | if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) { |
1704 | s += n; | |
1705 | n = -1; | |
1706 | if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) { | |
1707 | s += n; | |
1708 | } else { | |
1709 | ct_mark.mask = UINT32_MAX; | |
1710 | } | |
1711 | continue; | |
1712 | } | |
9daf2348 JS |
1713 | if (ovs_scan(s, "label=%n", &n)) { |
1714 | int retval; | |
1715 | ||
1716 | s += n; | |
1717 | retval = scan_u128(s, &ct_label.value, &ct_label.mask); | |
1718 | if (retval < 0) { | |
1719 | return retval; | |
1720 | } | |
1721 | s += retval; | |
1722 | continue; | |
1723 | } | |
d787ad39 JS |
1724 | if (ovs_scan(s, "helper=%n", &n)) { |
1725 | s += n; | |
1726 | helper_len = strcspn(s, delimiters_end); | |
1727 | if (!helper_len || helper_len > 15) { | |
1728 | return -EINVAL; | |
1729 | } | |
1730 | helper = s; | |
1731 | s += helper_len; | |
1732 | continue; | |
1733 | } | |
07659514 | 1734 | |
9ac0aada JR |
1735 | n = scan_ct_nat(s, &nat_params); |
1736 | if (n > 0) { | |
1737 | s += n; | |
1738 | have_nat = true; | |
1739 | ||
1740 | /* end points to the end of the nested, nat action. | |
1741 | * find the real end. */ | |
1742 | goto find_end; | |
1743 | } | |
1744 | /* Nothing matched. */ | |
07659514 JS |
1745 | return -EINVAL; |
1746 | } | |
1747 | s++; | |
1748 | } | |
a76a37ef JR |
1749 | if (commit && force_commit) { |
1750 | return -EINVAL; | |
1751 | } | |
07659514 JS |
1752 | |
1753 | start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT); | |
1754 | if (commit) { | |
1755 | nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT); | |
a76a37ef JR |
1756 | } else if (force_commit) { |
1757 | nl_msg_put_flag(actions, OVS_CT_ATTR_FORCE_COMMIT); | |
07659514 JS |
1758 | } |
1759 | if (zone) { | |
1760 | nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone); | |
1761 | } | |
8e53fe8c JS |
1762 | if (ct_mark.mask) { |
1763 | nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark, | |
1764 | sizeof(ct_mark)); | |
1765 | } | |
2ff8484b | 1766 | if (!ovs_u128_is_zero(ct_label.mask)) { |
9daf2348 JS |
1767 | nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label, |
1768 | sizeof ct_label); | |
1769 | } | |
d787ad39 JS |
1770 | if (helper) { |
1771 | nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper, | |
1772 | helper_len); | |
1773 | } | |
9ac0aada JR |
1774 | if (have_nat) { |
1775 | nl_msg_put_ct_nat(&nat_params, actions); | |
1776 | } | |
07659514 JS |
1777 | nl_msg_end_nested(actions, start); |
1778 | } | |
1779 | ||
1780 | return s - s_; | |
1781 | } | |
1782 | ||
1fc11c59 JS |
1783 | static int |
1784 | parse_odp_encap_nsh_action(const char *s, struct ofpbuf *actions) | |
1785 | { | |
1786 | int n = 0; | |
1787 | int ret = 0; | |
1788 | struct ovs_action_encap_nsh encap_nsh; | |
1789 | uint32_t spi; | |
1790 | uint8_t si; | |
1791 | uint32_t cd; | |
1792 | ||
1793 | if (!ovs_scan_len(s, &n, "encap_nsh(")) { | |
1794 | ret = -EINVAL; | |
1795 | goto out; | |
1796 | } | |
1797 | ||
1798 | /* The default is NSH_M_TYPE1 */ | |
1799 | encap_nsh.flags = 0; | |
1800 | encap_nsh.mdtype = NSH_M_TYPE1; | |
1801 | encap_nsh.mdlen = NSH_M_TYPE1_MDLEN; | |
1802 | encap_nsh.path_hdr = htonl(255); | |
1803 | memset(encap_nsh.metadata, 0, NSH_M_TYPE1_MDLEN); | |
1804 | ||
1805 | for (;;) { | |
1806 | n += strspn(s + n, delimiters); | |
1807 | if (s[n] == ')') { | |
1808 | break; | |
1809 | } | |
1810 | ||
1811 | if (ovs_scan_len(s, &n, "flags=%"SCNi8, &encap_nsh.flags)) { | |
1812 | continue; | |
1813 | } | |
1814 | if (ovs_scan_len(s, &n, "mdtype=%"SCNi8, &encap_nsh.mdtype)) { | |
1815 | switch (encap_nsh.mdtype) { | |
1816 | case NSH_M_TYPE1: | |
1817 | /* This is the default format. */; | |
1818 | break; | |
1819 | case NSH_M_TYPE2: | |
1820 | /* Length will be updated later. */ | |
1821 | encap_nsh.mdlen = 0; | |
1822 | break; | |
1823 | default: | |
1824 | ret = -EINVAL; | |
1825 | goto out; | |
1826 | } | |
1827 | continue; | |
1828 | } | |
1829 | if (ovs_scan_len(s, &n, "np=%"SCNi8, &encap_nsh.np)) { | |
1830 | continue; | |
1831 | } | |
1832 | if (ovs_scan_len(s, &n, "spi=0x%"SCNx32, &spi)) { | |
1833 | encap_nsh.path_hdr = | |
1834 | htonl(((spi << NSH_SPI_SHIFT) & NSH_SPI_MASK) | | |
1835 | (ntohl(encap_nsh.path_hdr) & ~NSH_SPI_MASK)); | |
1836 | continue; | |
1837 | } | |
1838 | if (ovs_scan_len(s, &n, "si=%"SCNi8, &si)) { | |
1839 | encap_nsh.path_hdr = | |
1840 | htonl((si << NSH_SI_SHIFT) | | |
1841 | (ntohl(encap_nsh.path_hdr) & ~NSH_SI_MASK)); | |
1842 | continue; | |
1843 | } | |
1844 | if (encap_nsh.mdtype == NSH_M_TYPE1) { | |
1845 | struct nsh_md1_ctx *md1 = | |
1846 | ALIGNED_CAST(struct nsh_md1_ctx *, encap_nsh.metadata); | |
1847 | if (ovs_scan_len(s, &n, "c1=0x%"SCNx32, &cd)) { | |
1848 | put_16aligned_be32(&md1->c[0], htonl(cd)); | |
1849 | continue; | |
1850 | } | |
1851 | if (ovs_scan_len(s, &n, "c2=0x%"SCNx32, &cd)) { | |
1852 | put_16aligned_be32(&md1->c[1], htonl(cd)); | |
1853 | continue; | |
1854 | } | |
1855 | if (ovs_scan_len(s, &n, "c3=0x%"SCNx32, &cd)) { | |
1856 | put_16aligned_be32(&md1->c[2], htonl(cd)); | |
1857 | continue; | |
1858 | } | |
1859 | if (ovs_scan_len(s, &n, "c4=0x%"SCNx32, &cd)) { | |
1860 | put_16aligned_be32(&md1->c[3], htonl(cd)); | |
1861 | continue; | |
1862 | } | |
1863 | } | |
1864 | else if (encap_nsh.mdtype == NSH_M_TYPE2) { | |
1865 | struct ofpbuf b; | |
1866 | char buf[512]; | |
1867 | size_t mdlen; | |
1868 | if (ovs_scan_len(s, &n, "md2=0x%511[0-9a-fA-F]", buf)) { | |
1869 | ofpbuf_use_stub(&b, encap_nsh.metadata, | |
1870 | OVS_ENCAP_NSH_MAX_MD_LEN); | |
1871 | ofpbuf_put_hex(&b, buf, &mdlen); | |
1872 | encap_nsh.mdlen = mdlen; | |
1873 | ofpbuf_uninit(&b); | |
1874 | } | |
1875 | continue; | |
1876 | } | |
1877 | } | |
1878 | out: | |
1879 | if (ret < 0) { | |
1880 | return ret; | |
1881 | } else { | |
1882 | size_t size = offsetof(struct ovs_action_encap_nsh, metadata) | |
1883 | + ROUND_UP(encap_nsh.mdlen, 4); | |
1884 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_ENCAP_NSH, | |
1885 | &encap_nsh, size); | |
1886 | return n; | |
1887 | } | |
1888 | } | |
1889 | ||
c37f7135 AZ |
1890 | static int |
1891 | parse_action_list(const char *s, const struct simap *port_names, | |
1892 | struct ofpbuf *actions) | |
1893 | { | |
1894 | int n = 0; | |
1895 | ||
1896 | for (;;) { | |
1897 | int retval; | |
1898 | ||
1899 | n += strspn(s + n, delimiters); | |
1900 | if (s[n] == ')') { | |
1901 | break; | |
1902 | } | |
1903 | retval = parse_odp_action(s + n, port_names, actions); | |
1904 | if (retval < 0) { | |
1905 | return retval; | |
1906 | } | |
1907 | n += retval; | |
1908 | } | |
1909 | ||
1910 | return n; | |
1911 | } | |
1912 | ||
8b7ea2d4 WZ |
1913 | static int |
1914 | parse_odp_action(const char *s, const struct simap *port_names, | |
1915 | struct ofpbuf *actions) | |
1916 | { | |
1917 | { | |
1918 | uint32_t port; | |
1919 | int n; | |
1920 | ||
1921 | if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) { | |
1922 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port); | |
1923 | return n; | |
7202cbe5 BP |
1924 | } |
1925 | } | |
1926 | ||
aaca4fe0 WT |
1927 | { |
1928 | uint32_t max_len; | |
1929 | int n; | |
1930 | ||
1931 | if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) { | |
1932 | struct ovs_action_trunc *trunc; | |
1933 | ||
1934 | trunc = nl_msg_put_unspec_uninit(actions, | |
1935 | OVS_ACTION_ATTR_TRUNC, sizeof *trunc); | |
1936 | trunc->max_len = max_len; | |
1937 | return n; | |
1938 | } | |
1939 | } | |
1940 | ||
8b7ea2d4 WZ |
1941 | if (port_names) { |
1942 | int len = strcspn(s, delimiters); | |
1943 | struct simap_node *node; | |
1944 | ||
1945 | node = simap_find_len(port_names, s, len); | |
1946 | if (node) { | |
1947 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data); | |
1948 | return len; | |
1949 | } | |
1950 | } | |
1951 | ||
d73803ca DDP |
1952 | { |
1953 | uint32_t recirc_id; | |
1954 | int n = -1; | |
1955 | ||
1956 | if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) { | |
1957 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id); | |
1958 | return n; | |
1959 | } | |
1960 | } | |
1961 | ||
8b7ea2d4 WZ |
1962 | if (!strncmp(s, "userspace(", 10)) { |
1963 | return parse_odp_userspace_action(s, actions); | |
1964 | } | |
1965 | ||
7202cbe5 BP |
1966 | if (!strncmp(s, "set(", 4)) { |
1967 | size_t start_ofs; | |
1968 | int retval; | |
6d670e7f JR |
1969 | struct nlattr mask[128 / sizeof(struct nlattr)]; |
1970 | struct ofpbuf maskbuf; | |
1971 | struct nlattr *nested, *key; | |
1972 | size_t size; | |
1973 | ||
1974 | /* 'mask' is big enough to hold any key. */ | |
1975 | ofpbuf_use_stack(&maskbuf, mask, sizeof mask); | |
7202cbe5 BP |
1976 | |
1977 | start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET); | |
6d670e7f | 1978 | retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf); |
7202cbe5 BP |
1979 | if (retval < 0) { |
1980 | return retval; | |
1981 | } | |
1982 | if (s[retval + 4] != ')') { | |
1983 | return -EINVAL; | |
1984 | } | |
6d670e7f JR |
1985 | |
1986 | nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested); | |
1987 | key = nested + 1; | |
1988 | ||
1989 | size = nl_attr_get_size(mask); | |
1990 | if (size == nl_attr_get_size(key)) { | |
1991 | /* Change to masked set action if not fully masked. */ | |
53cb9c3e | 1992 | if (!is_all_ones(mask + 1, size)) { |
34ed68a6 PB |
1993 | /* Remove padding of eariler key payload */ |
1994 | actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len; | |
1995 | ||
1996 | /* Put mask payload right after key payload */ | |
6d670e7f JR |
1997 | key->nla_len += size; |
1998 | ofpbuf_put(actions, mask + 1, size); | |
34ed68a6 PB |
1999 | |
2000 | /* Add new padding as needed */ | |
2001 | ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) - | |
2002 | key->nla_len); | |
2003 | ||
6d670e7f JR |
2004 | /* 'actions' may have been reallocated by ofpbuf_put(). */ |
2005 | nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested); | |
2006 | nested->nla_type = OVS_ACTION_ATTR_SET_MASKED; | |
2007 | } | |
2008 | } | |
2009 | ||
7202cbe5 BP |
2010 | nl_msg_end_nested(actions, start_ofs); |
2011 | return retval + 5; | |
2012 | } | |
2013 | ||
becffb86 BP |
2014 | { |
2015 | struct ovs_action_push_vlan push; | |
2016 | int tpid = ETH_TYPE_VLAN; | |
2017 | int vid, pcp; | |
2018 | int cfi = 1; | |
2019 | int n = -1; | |
7202cbe5 | 2020 | |
c2c28dfd BP |
2021 | if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) |
2022 | || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", | |
2023 | &vid, &pcp, &cfi, &n) | |
2024 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", | |
2025 | &tpid, &vid, &pcp, &n) | |
2026 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", | |
2027 | &tpid, &vid, &pcp, &cfi, &n)) { | |
becffb86 BP |
2028 | push.vlan_tpid = htons(tpid); |
2029 | push.vlan_tci = htons((vid << VLAN_VID_SHIFT) | |
2030 | | (pcp << VLAN_PCP_SHIFT) | |
2031 | | (cfi ? VLAN_CFI : 0)); | |
2032 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
2033 | &push, sizeof push); | |
2034 | ||
2035 | return n; | |
7202cbe5 | 2036 | } |
7202cbe5 BP |
2037 | } |
2038 | ||
becffb86 BP |
2039 | if (!strncmp(s, "pop_vlan", 8)) { |
2040 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN); | |
2041 | return 8; | |
7202cbe5 BP |
2042 | } |
2043 | ||
5dddf960 JR |
2044 | { |
2045 | unsigned long long int meter_id; | |
2046 | int n = -1; | |
2047 | ||
2048 | if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) { | |
2049 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id); | |
2050 | return n; | |
2051 | } | |
2052 | } | |
2053 | ||
7202cbe5 BP |
2054 | { |
2055 | double percentage; | |
2056 | int n = -1; | |
2057 | ||
c2c28dfd BP |
2058 | if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) |
2059 | && percentage >= 0. && percentage <= 100.0) { | |
7202cbe5 BP |
2060 | size_t sample_ofs, actions_ofs; |
2061 | double probability; | |
2062 | ||
2063 | probability = floor(UINT32_MAX * (percentage / 100.0) + .5); | |
2064 | sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE); | |
2065 | nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY, | |
2066 | (probability <= 0 ? 0 | |
2067 | : probability >= UINT32_MAX ? UINT32_MAX | |
2068 | : probability)); | |
2069 | ||
2070 | actions_ofs = nl_msg_start_nested(actions, | |
2071 | OVS_SAMPLE_ATTR_ACTIONS); | |
c37f7135 AZ |
2072 | int retval = parse_action_list(s + n, port_names, actions); |
2073 | if (retval < 0) | |
2074 | return retval; | |
7202cbe5 | 2075 | |
c37f7135 | 2076 | n += retval; |
7202cbe5 BP |
2077 | nl_msg_end_nested(actions, actions_ofs); |
2078 | nl_msg_end_nested(actions, sample_ofs); | |
2079 | ||
2080 | return s[n + 1] == ')' ? n + 2 : -EINVAL; | |
2081 | } | |
2082 | } | |
2083 | ||
c37f7135 AZ |
2084 | { |
2085 | if (!strncmp(s, "clone(", 6)) { | |
2086 | size_t actions_ofs; | |
2087 | int n = 6; | |
2088 | ||
2089 | actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE); | |
2090 | int retval = parse_action_list(s + n, port_names, actions); | |
2091 | if (retval < 0) { | |
2092 | return retval; | |
2093 | } | |
2094 | n += retval; | |
2095 | nl_msg_end_nested(actions, actions_ofs); | |
2096 | return n + 1; | |
2097 | } | |
2098 | } | |
2099 | ||
1fc11c59 JS |
2100 | { |
2101 | if (!strncmp(s, "encap_nsh(", 10)) { | |
2102 | int retval = parse_odp_encap_nsh_action(s, actions); | |
2103 | if (retval < 0) { | |
2104 | return retval; | |
2105 | } | |
2106 | return retval + 1; | |
2107 | } | |
2108 | } | |
2109 | ||
2110 | { | |
2111 | int n; | |
2112 | if (ovs_scan(s, "decap_nsh()%n", &n)) { | |
2113 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_DECAP_NSH); | |
2114 | return n; | |
2115 | } | |
2116 | } | |
2117 | ||
a36de779 PS |
2118 | { |
2119 | uint32_t port; | |
2120 | int n; | |
2121 | ||
2122 | if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) { | |
2123 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port); | |
2124 | return n; | |
2125 | } | |
2126 | } | |
2127 | ||
07659514 JS |
2128 | { |
2129 | int retval; | |
2130 | ||
2131 | retval = parse_conntrack_action(s, actions); | |
2132 | if (retval) { | |
2133 | return retval; | |
2134 | } | |
2135 | } | |
2136 | ||
a36de779 PS |
2137 | { |
2138 | struct ovs_action_push_tnl data; | |
2139 | int n; | |
2140 | ||
2141 | n = ovs_parse_tnl_push(s, &data); | |
2142 | if (n > 0) { | |
2143 | odp_put_tnl_push_action(actions, &data); | |
2144 | return n; | |
2145 | } else if (n < 0) { | |
2146 | return n; | |
2147 | } | |
2148 | } | |
7202cbe5 BP |
2149 | return -EINVAL; |
2150 | } | |
2151 | ||
2152 | /* Parses the string representation of datapath actions, in the format output | |
2153 | * by format_odp_action(). Returns 0 if successful, otherwise a positive errno | |
2154 | * value. On success, the ODP actions are appended to 'actions' as a series of | |
2155 | * Netlink attributes. On failure, no data is appended to 'actions'. Either | |
2156 | * way, 'actions''s data might be reallocated. */ | |
2157 | int | |
44bac24b | 2158 | odp_actions_from_string(const char *s, const struct simap *port_names, |
7202cbe5 BP |
2159 | struct ofpbuf *actions) |
2160 | { | |
2161 | size_t old_size; | |
2162 | ||
2163 | if (!strcasecmp(s, "drop")) { | |
2164 | return 0; | |
2165 | } | |
2166 | ||
6fd6ed71 | 2167 | old_size = actions->size; |
7202cbe5 BP |
2168 | for (;;) { |
2169 | int retval; | |
2170 | ||
2171 | s += strspn(s, delimiters); | |
2172 | if (!*s) { | |
2173 | return 0; | |
2174 | } | |
2175 | ||
2176 | retval = parse_odp_action(s, port_names, actions); | |
2177 | if (retval < 0 || !strchr(delimiters, s[retval])) { | |
6fd6ed71 | 2178 | actions->size = old_size; |
7202cbe5 BP |
2179 | return -retval; |
2180 | } | |
2181 | s += retval; | |
2182 | } | |
2183 | ||
2184 | return 0; | |
2185 | } | |
14608a15 | 2186 | \f |
6b8da9e9 JG |
2187 | static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = { |
2188 | [OVS_VXLAN_EXT_GBP] = { .len = 4 }, | |
2189 | }; | |
2190 | ||
2191 | static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { | |
2192 | [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 }, | |
2193 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 }, | |
2194 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 }, | |
2195 | [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 }, | |
2196 | [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 }, | |
2197 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 }, | |
2198 | [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 }, | |
2199 | [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 }, | |
2200 | [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 }, | |
2201 | [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 }, | |
2202 | [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE }, | |
2203 | [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED, | |
2204 | .next = ovs_vxlan_ext_attr_lens , | |
2205 | .next_max = OVS_VXLAN_EXT_MAX}, | |
ffe4c74f JB |
2206 | [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = 16 }, |
2207 | [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = 16 }, | |
6b8da9e9 JG |
2208 | }; |
2209 | ||
2210 | static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = { | |
2211 | [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED }, | |
2212 | [OVS_KEY_ATTR_PRIORITY] = { .len = 4 }, | |
2213 | [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 }, | |
2214 | [OVS_KEY_ATTR_DP_HASH] = { .len = 4 }, | |
2215 | [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 }, | |
2216 | [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED, | |
2217 | .next = ovs_tun_key_attr_lens, | |
2218 | .next_max = OVS_TUNNEL_KEY_ATTR_MAX }, | |
2219 | [OVS_KEY_ATTR_IN_PORT] = { .len = 4 }, | |
2220 | [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) }, | |
2221 | [OVS_KEY_ATTR_VLAN] = { .len = 2 }, | |
2222 | [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 }, | |
2223 | [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE }, | |
2224 | [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) }, | |
2225 | [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) }, | |
2226 | [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) }, | |
2227 | [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 }, | |
2228 | [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) }, | |
2229 | [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) }, | |
2230 | [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) }, | |
2231 | [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) }, | |
2232 | [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) }, | |
2233 | [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) }, | |
07659514 JS |
2234 | [OVS_KEY_ATTR_CT_STATE] = { .len = 4 }, |
2235 | [OVS_KEY_ATTR_CT_ZONE] = { .len = 2 }, | |
8e53fe8c | 2236 | [OVS_KEY_ATTR_CT_MARK] = { .len = 4 }, |
9daf2348 | 2237 | [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) }, |
c30b4cea JR |
2238 | [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) }, |
2239 | [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) }, | |
beb75a40 | 2240 | [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4 }, |
3d2fbd70 | 2241 | [OVS_KEY_ATTR_NSH] = { .len = sizeof(struct ovs_key_nsh) }, |
6b8da9e9 JG |
2242 | }; |
2243 | ||
36956a7d | 2244 | /* Returns the correct length of the payload for a flow key attribute of the |
6b8da9e9 JG |
2245 | * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE |
2246 | * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the | |
2247 | * payload is a nested type. */ | |
36956a7d | 2248 | static int |
ce9c9b8b | 2249 | odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type) |
36956a7d | 2250 | { |
ce9c9b8b | 2251 | if (type > max_type) { |
6b8da9e9 | 2252 | return ATTR_LEN_INVALID; |
36956a7d BP |
2253 | } |
2254 | ||
6b8da9e9 | 2255 | return tbl[type].len; |
36956a7d BP |
2256 | } |
2257 | ||
36956a7d BP |
2258 | static void |
2259 | format_generic_odp_key(const struct nlattr *a, struct ds *ds) | |
2260 | { | |
2261 | size_t len = nl_attr_get_size(a); | |
36956a7d BP |
2262 | if (len) { |
2263 | const uint8_t *unspec; | |
2264 | unsigned int i; | |
2265 | ||
2266 | unspec = nl_attr_get(a); | |
2267 | for (i = 0; i < len; i++) { | |
e6cc0bab AZ |
2268 | if (i) { |
2269 | ds_put_char(ds, ' '); | |
2270 | } | |
36956a7d BP |
2271 | ds_put_format(ds, "%02x", unspec[i]); |
2272 | } | |
36956a7d BP |
2273 | } |
2274 | } | |
2275 | ||
7257b535 BP |
2276 | static const char * |
2277 | ovs_frag_type_to_string(enum ovs_frag_type type) | |
2278 | { | |
2279 | switch (type) { | |
2280 | case OVS_FRAG_TYPE_NONE: | |
2281 | return "no"; | |
2282 | case OVS_FRAG_TYPE_FIRST: | |
2283 | return "first"; | |
2284 | case OVS_FRAG_TYPE_LATER: | |
2285 | return "later"; | |
2286 | case __OVS_FRAG_TYPE_MAX: | |
2287 | default: | |
2288 | return "<error>"; | |
2289 | } | |
2290 | } | |
2291 | ||
ec1f6f32 | 2292 | static enum odp_key_fitness |
8d8ab6c2 JG |
2293 | odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask, |
2294 | struct flow_tnl *tun) | |
9b405f1a PS |
2295 | { |
2296 | unsigned int left; | |
2297 | const struct nlattr *a; | |
2298 | bool ttl = false; | |
2299 | bool unknown = false; | |
2300 | ||
2301 | NL_NESTED_FOR_EACH(a, left, attr) { | |
2302 | uint16_t type = nl_attr_type(a); | |
2303 | size_t len = nl_attr_get_size(a); | |
6b8da9e9 JG |
2304 | int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens, |
2305 | OVS_TUNNEL_ATTR_MAX, type); | |
9b405f1a PS |
2306 | |
2307 | if (len != expected_len && expected_len >= 0) { | |
2308 | return ODP_FIT_ERROR; | |
2309 | } | |
2310 | ||
2311 | switch (type) { | |
2312 | case OVS_TUNNEL_KEY_ATTR_ID: | |
2313 | tun->tun_id = nl_attr_get_be64(a); | |
2314 | tun->flags |= FLOW_TNL_F_KEY; | |
2315 | break; | |
2316 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
2317 | tun->ip_src = nl_attr_get_be32(a); | |
2318 | break; | |
2319 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
2320 | tun->ip_dst = nl_attr_get_be32(a); | |
2321 | break; | |
ffe4c74f JB |
2322 | case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: |
2323 | tun->ipv6_src = nl_attr_get_in6_addr(a); | |
2324 | break; | |
2325 | case OVS_TUNNEL_KEY_ATTR_IPV6_DST: | |
2326 | tun->ipv6_dst = nl_attr_get_in6_addr(a); | |
2327 | break; | |
9b405f1a PS |
2328 | case OVS_TUNNEL_KEY_ATTR_TOS: |
2329 | tun->ip_tos = nl_attr_get_u8(a); | |
2330 | break; | |
2331 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
2332 | tun->ip_ttl = nl_attr_get_u8(a); | |
2333 | ttl = true; | |
2334 | break; | |
2335 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
2336 | tun->flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
2337 | break; | |
2338 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
2339 | tun->flags |= FLOW_TNL_F_CSUM; | |
2340 | break; | |
8b7ea2d4 WZ |
2341 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: |
2342 | tun->tp_src = nl_attr_get_be16(a); | |
2343 | break; | |
2344 | case OVS_TUNNEL_KEY_ATTR_TP_DST: | |
2345 | tun->tp_dst = nl_attr_get_be16(a); | |
2346 | break; | |
94872594 JG |
2347 | case OVS_TUNNEL_KEY_ATTR_OAM: |
2348 | tun->flags |= FLOW_TNL_F_OAM; | |
2349 | break; | |
ac6073e3 MC |
2350 | case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: { |
2351 | static const struct nl_policy vxlan_opts_policy[] = { | |
2352 | [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 }, | |
2353 | }; | |
2354 | struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)]; | |
2355 | ||
2356 | if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) { | |
2357 | return ODP_FIT_ERROR; | |
2358 | } | |
2359 | ||
2360 | if (ext[OVS_VXLAN_EXT_GBP]) { | |
2361 | uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]); | |
2362 | ||
2363 | tun->gbp_id = htons(gbp & 0xFFFF); | |
2364 | tun->gbp_flags = (gbp >> 16) & 0xFF; | |
2365 | } | |
2366 | ||
2367 | break; | |
2368 | } | |
9558d2a5 | 2369 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: |
8d8ab6c2 | 2370 | tun_metadata_from_geneve_nlattr(a, is_mask, tun); |
c1fc1411 | 2371 | break; |
9558d2a5 | 2372 | |
9b405f1a PS |
2373 | default: |
2374 | /* Allow this to show up as unexpected, if there are unknown | |
2375 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
2376 | unknown = true; | |
2377 | break; | |
2378 | } | |
2379 | } | |
2380 | ||
2381 | if (!ttl) { | |
2382 | return ODP_FIT_ERROR; | |
2383 | } | |
2384 | if (unknown) { | |
758c456d | 2385 | return ODP_FIT_TOO_MUCH; |
9b405f1a PS |
2386 | } |
2387 | return ODP_FIT_PERFECT; | |
2388 | } | |
2389 | ||
ec1f6f32 | 2390 | enum odp_key_fitness |
8d8ab6c2 | 2391 | odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun) |
ec1f6f32 | 2392 | { |
35303d71 | 2393 | memset(tun, 0, sizeof *tun); |
8d8ab6c2 | 2394 | return odp_tun_key_from_attr__(attr, false, tun); |
ec1f6f32 JG |
2395 | } |
2396 | ||
9b405f1a | 2397 | static void |
ec1f6f32 | 2398 | tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key, |
9558d2a5 JG |
2399 | const struct flow_tnl *tun_flow_key, |
2400 | const struct ofpbuf *key_buf) | |
9b405f1a PS |
2401 | { |
2402 | size_t tun_key_ofs; | |
2403 | ||
2404 | tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL); | |
2405 | ||
27a8cbbc BP |
2406 | /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */ |
2407 | if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) { | |
9b405f1a PS |
2408 | nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id); |
2409 | } | |
2410 | if (tun_key->ip_src) { | |
2411 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src); | |
2412 | } | |
2413 | if (tun_key->ip_dst) { | |
2414 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst); | |
2415 | } | |
ffe4c74f JB |
2416 | if (ipv6_addr_is_set(&tun_key->ipv6_src)) { |
2417 | nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src); | |
2418 | } | |
2419 | if (ipv6_addr_is_set(&tun_key->ipv6_dst)) { | |
2420 | nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst); | |
2421 | } | |
9b405f1a PS |
2422 | if (tun_key->ip_tos) { |
2423 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos); | |
2424 | } | |
2425 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl); | |
2426 | if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
2427 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
2428 | } | |
2429 | if (tun_key->flags & FLOW_TNL_F_CSUM) { | |
2430 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
2431 | } | |
8b7ea2d4 WZ |
2432 | if (tun_key->tp_src) { |
2433 | nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src); | |
2434 | } | |
2435 | if (tun_key->tp_dst) { | |
2436 | nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst); | |
2437 | } | |
94872594 JG |
2438 | if (tun_key->flags & FLOW_TNL_F_OAM) { |
2439 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM); | |
2440 | } | |
ac6073e3 MC |
2441 | if (tun_key->gbp_flags || tun_key->gbp_id) { |
2442 | size_t vxlan_opts_ofs; | |
2443 | ||
2444 | vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS); | |
2445 | nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, | |
2446 | (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id)); | |
2447 | nl_msg_end_nested(a, vxlan_opts_ofs); | |
2448 | } | |
6728d578 | 2449 | tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a); |
9558d2a5 | 2450 | |
9b405f1a | 2451 | nl_msg_end_nested(a, tun_key_ofs); |
29c7a2b2 PS |
2452 | } |
2453 | ||
b17298ae BP |
2454 | static bool |
2455 | odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size, | |
2456 | int constant) | |
2457 | { | |
2458 | /* Convert 'constant' to all the widths we need. C conversion rules ensure | |
2459 | * that -1 becomes all-1-bits and 0 does not change. */ | |
2460 | ovs_be16 be16 = (OVS_FORCE ovs_be16) constant; | |
2461 | uint32_t u32 = constant; | |
2462 | uint8_t u8 = constant; | |
2463 | const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any; | |
2464 | ||
2465 | switch (attr) { | |
2466 | case OVS_KEY_ATTR_UNSPEC: | |
2467 | case OVS_KEY_ATTR_ENCAP: | |
2468 | case __OVS_KEY_ATTR_MAX: | |
2469 | default: | |
2470 | return false; | |
2471 | ||
2472 | case OVS_KEY_ATTR_PRIORITY: | |
2473 | case OVS_KEY_ATTR_IN_PORT: | |
2474 | case OVS_KEY_ATTR_ETHERNET: | |
2475 | case OVS_KEY_ATTR_VLAN: | |
2476 | case OVS_KEY_ATTR_ETHERTYPE: | |
2477 | case OVS_KEY_ATTR_IPV4: | |
2478 | case OVS_KEY_ATTR_TCP: | |
2479 | case OVS_KEY_ATTR_UDP: | |
2480 | case OVS_KEY_ATTR_ICMP: | |
2481 | case OVS_KEY_ATTR_ICMPV6: | |
2482 | case OVS_KEY_ATTR_ND: | |
2483 | case OVS_KEY_ATTR_SKB_MARK: | |
2484 | case OVS_KEY_ATTR_TUNNEL: | |
2485 | case OVS_KEY_ATTR_SCTP: | |
2486 | case OVS_KEY_ATTR_DP_HASH: | |
2487 | case OVS_KEY_ATTR_RECIRC_ID: | |
2488 | case OVS_KEY_ATTR_MPLS: | |
2489 | case OVS_KEY_ATTR_CT_STATE: | |
2490 | case OVS_KEY_ATTR_CT_ZONE: | |
2491 | case OVS_KEY_ATTR_CT_MARK: | |
2492 | case OVS_KEY_ATTR_CT_LABELS: | |
2493 | case OVS_KEY_ATTR_PACKET_TYPE: | |
3d2fbd70 | 2494 | case OVS_KEY_ATTR_NSH: |
b17298ae BP |
2495 | return is_all_byte(mask, size, u8); |
2496 | ||
2497 | case OVS_KEY_ATTR_TCP_FLAGS: | |
2498 | return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16); | |
2499 | ||
2500 | case OVS_KEY_ATTR_IPV6: { | |
2501 | const struct ovs_key_ipv6 *ipv6_mask = mask; | |
2502 | return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK)) | |
2503 | == htonl(IPV6_LABEL_MASK & u32) | |
2504 | && ipv6_mask->ipv6_proto == u8 | |
2505 | && ipv6_mask->ipv6_tclass == u8 | |
2506 | && ipv6_mask->ipv6_hlimit == u8 | |
2507 | && ipv6_mask->ipv6_frag == u8 | |
2508 | && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6) | |
2509 | && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6)); | |
2510 | } | |
2511 | ||
2512 | case OVS_KEY_ATTR_ARP: | |
2513 | return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8); | |
2514 | ||
2515 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: | |
2516 | return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4, | |
2517 | ipv4_proto), u8); | |
2518 | ||
2519 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: | |
2520 | return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6, | |
2521 | ipv6_proto), u8); | |
2522 | } | |
2523 | } | |
2524 | ||
2525 | /* The caller must already have verified that 'ma' has a correct length. | |
2526 | * | |
2527 | * The main purpose of this function is formatting, to allow code to figure out | |
2528 | * whether the mask can be omitted. It doesn't try hard for attributes that | |
2529 | * contain sub-attributes, etc., because normally those would be broken down | |
2530 | * further for formatting. */ | |
041e7168 AZ |
2531 | static bool |
2532 | odp_mask_attr_is_wildcard(const struct nlattr *ma) | |
2533 | { | |
b17298ae BP |
2534 | return odp_mask_is_constant__(nl_attr_type(ma), |
2535 | nl_attr_get(ma), nl_attr_get_size(ma), 0); | |
041e7168 AZ |
2536 | } |
2537 | ||
2f73024d | 2538 | /* The caller must already have verified that 'size' is a correct length for |
b17298ae BP |
2539 | * 'attr'. |
2540 | * | |
2541 | * The main purpose of this function is formatting, to allow code to figure out | |
2542 | * whether the mask can be omitted. It doesn't try hard for attributes that | |
2543 | * contain sub-attributes, etc., because normally those would be broken down | |
2544 | * further for formatting. */ | |
e6cc0bab | 2545 | static bool |
d23df9a8 | 2546 | odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size) |
e6cc0bab | 2547 | { |
b17298ae | 2548 | return odp_mask_is_constant__(attr, mask, size, -1); |
d23df9a8 JR |
2549 | } |
2550 | ||
2f73024d | 2551 | /* The caller must already have verified that 'ma' has a correct length. */ |
d23df9a8 JR |
2552 | static bool |
2553 | odp_mask_attr_is_exact(const struct nlattr *ma) | |
2554 | { | |
d23df9a8 | 2555 | enum ovs_key_attr attr = nl_attr_type(ma); |
a4e8ed0c | 2556 | return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma)); |
e6cc0bab AZ |
2557 | } |
2558 | ||
0a37839c GS |
2559 | void |
2560 | odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no, | |
2561 | char *port_name) | |
2562 | { | |
2563 | struct odp_portno_names *odp_portno_names; | |
2564 | ||
2565 | odp_portno_names = xmalloc(sizeof *odp_portno_names); | |
2566 | odp_portno_names->port_no = port_no; | |
2567 | odp_portno_names->name = xstrdup(port_name); | |
2568 | hmap_insert(portno_names, &odp_portno_names->hmap_node, | |
2569 | hash_odp_port(port_no)); | |
2570 | } | |
2571 | ||
2572 | static char * | |
2573 | odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no) | |
2574 | { | |
0722f341 BP |
2575 | if (portno_names) { |
2576 | struct odp_portno_names *odp_portno_names; | |
0a37839c | 2577 | |
0722f341 BP |
2578 | HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node, |
2579 | hash_odp_port(port_no), portno_names) { | |
2580 | if (odp_portno_names->port_no == port_no) { | |
2581 | return odp_portno_names->name; | |
2582 | } | |
0a37839c GS |
2583 | } |
2584 | } | |
2585 | return NULL; | |
2586 | } | |
2587 | ||
2588 | void | |
2589 | odp_portno_names_destroy(struct hmap *portno_names) | |
2590 | { | |
4ec3d7c7 DDP |
2591 | struct odp_portno_names *odp_portno_names; |
2592 | ||
2593 | HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) { | |
0a37839c GS |
2594 | free(odp_portno_names->name); |
2595 | free(odp_portno_names); | |
2596 | } | |
2597 | } | |
e6cc0bab | 2598 | |
0722f341 BP |
2599 | void |
2600 | odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no, | |
2601 | struct ds *s) | |
2602 | { | |
2603 | const char *name = odp_portno_names_get(portno_names, port_no); | |
2604 | if (name) { | |
2605 | ds_put_cstr(s, name); | |
2606 | } else { | |
2607 | ds_put_format(s, "%"PRIu32, port_no); | |
2608 | } | |
2609 | } | |
2610 | ||
2d18eae8 JR |
2611 | /* Format helpers. */ |
2612 | ||
2613 | static void | |
74ff3298 JR |
2614 | format_eth(struct ds *ds, const char *name, const struct eth_addr key, |
2615 | const struct eth_addr *mask, bool verbose) | |
2d18eae8 JR |
2616 | { |
2617 | bool mask_empty = mask && eth_addr_is_zero(*mask); | |
2618 | ||
2619 | if (verbose || !mask_empty) { | |
2620 | bool mask_full = !mask || eth_mask_is_exact(*mask); | |
2621 | ||
2622 | if (mask_full) { | |
2623 | ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key)); | |
2624 | } else { | |
2625 | ds_put_format(ds, "%s=", name); | |
74ff3298 | 2626 | eth_format_masked(key, mask, ds); |
2d18eae8 JR |
2627 | ds_put_char(ds, ','); |
2628 | } | |
2629 | } | |
2630 | } | |
2631 | ||
1fc11c59 | 2632 | |
2d18eae8 JR |
2633 | static void |
2634 | format_be64(struct ds *ds, const char *name, ovs_be64 key, | |
2635 | const ovs_be64 *mask, bool verbose) | |
2636 | { | |
2637 | bool mask_empty = mask && !*mask; | |
2638 | ||
2639 | if (verbose || !mask_empty) { | |
2640 | bool mask_full = !mask || *mask == OVS_BE64_MAX; | |
2641 | ||
2642 | ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key)); | |
2643 | if (!mask_full) { /* Partially masked. */ | |
2644 | ds_put_format(ds, "/%#"PRIx64, ntohll(*mask)); | |
2645 | } | |
2646 | ds_put_char(ds, ','); | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | static void | |
2651 | format_ipv4(struct ds *ds, const char *name, ovs_be32 key, | |
2652 | const ovs_be32 *mask, bool verbose) | |
2653 | { | |
2654 | bool mask_empty = mask && !*mask; | |
2655 | ||
2656 | if (verbose || !mask_empty) { | |
2657 | bool mask_full = !mask || *mask == OVS_BE32_MAX; | |
2658 | ||
2659 | ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key)); | |
2660 | if (!mask_full) { /* Partially masked. */ | |
2661 | ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask)); | |
2662 | } | |
2663 | ds_put_char(ds, ','); | |
2664 | } | |
2665 | } | |
2666 | ||
2667 | static void | |
d15aef48 JB |
2668 | format_in6_addr(struct ds *ds, const char *name, |
2669 | const struct in6_addr *key, | |
2670 | const struct in6_addr *mask, | |
2671 | bool verbose) | |
2d18eae8 JR |
2672 | { |
2673 | char buf[INET6_ADDRSTRLEN]; | |
2d18eae8 JR |
2674 | bool mask_empty = mask && ipv6_mask_is_any(mask); |
2675 | ||
2676 | if (verbose || !mask_empty) { | |
2677 | bool mask_full = !mask || ipv6_mask_is_exact(mask); | |
2678 | ||
2679 | inet_ntop(AF_INET6, key, buf, sizeof buf); | |
2680 | ds_put_format(ds, "%s=%s", name, buf); | |
2681 | if (!mask_full) { /* Partially masked. */ | |
2682 | inet_ntop(AF_INET6, mask, buf, sizeof buf); | |
2683 | ds_put_format(ds, "/%s", buf); | |
2684 | } | |
2685 | ds_put_char(ds, ','); | |
2686 | } | |
2687 | } | |
2688 | ||
2689 | static void | |
2690 | format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key, | |
2691 | const ovs_be32 *mask, bool verbose) | |
2692 | { | |
2693 | bool mask_empty = mask && !*mask; | |
2694 | ||
2695 | if (verbose || !mask_empty) { | |
2696 | bool mask_full = !mask | |
2697 | || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK); | |
2698 | ||
2699 | ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key)); | |
2700 | if (!mask_full) { /* Partially masked. */ | |
2701 | ds_put_format(ds, "/%#"PRIx32, ntohl(*mask)); | |
2702 | } | |
2703 | ds_put_char(ds, ','); | |
2704 | } | |
2705 | } | |
2706 | ||
2707 | static void | |
2708 | format_u8x(struct ds *ds, const char *name, uint8_t key, | |
2709 | const uint8_t *mask, bool verbose) | |
2710 | { | |
2711 | bool mask_empty = mask && !*mask; | |
2712 | ||
2713 | if (verbose || !mask_empty) { | |
2714 | bool mask_full = !mask || *mask == UINT8_MAX; | |
2715 | ||
2716 | ds_put_format(ds, "%s=%#"PRIx8, name, key); | |
2717 | if (!mask_full) { /* Partially masked. */ | |
2718 | ds_put_format(ds, "/%#"PRIx8, *mask); | |
2719 | } | |
2720 | ds_put_char(ds, ','); | |
2721 | } | |
2722 | } | |
2723 | ||
2724 | static void | |
2725 | format_u8u(struct ds *ds, const char *name, uint8_t key, | |
2726 | const uint8_t *mask, bool verbose) | |
2727 | { | |
2728 | bool mask_empty = mask && !*mask; | |
2729 | ||
2730 | if (verbose || !mask_empty) { | |
2731 | bool mask_full = !mask || *mask == UINT8_MAX; | |
2732 | ||
2733 | ds_put_format(ds, "%s=%"PRIu8, name, key); | |
2734 | if (!mask_full) { /* Partially masked. */ | |
2735 | ds_put_format(ds, "/%#"PRIx8, *mask); | |
2736 | } | |
2737 | ds_put_char(ds, ','); | |
2738 | } | |
2739 | } | |
2740 | ||
2741 | static void | |
2742 | format_be16(struct ds *ds, const char *name, ovs_be16 key, | |
2743 | const ovs_be16 *mask, bool verbose) | |
2744 | { | |
2745 | bool mask_empty = mask && !*mask; | |
2746 | ||
2747 | if (verbose || !mask_empty) { | |
2748 | bool mask_full = !mask || *mask == OVS_BE16_MAX; | |
2749 | ||
2750 | ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key)); | |
2751 | if (!mask_full) { /* Partially masked. */ | |
2752 | ds_put_format(ds, "/%#"PRIx16, ntohs(*mask)); | |
2753 | } | |
2754 | ds_put_char(ds, ','); | |
2755 | } | |
2756 | } | |
2757 | ||
622a0a8e JG |
2758 | static void |
2759 | format_be16x(struct ds *ds, const char *name, ovs_be16 key, | |
2760 | const ovs_be16 *mask, bool verbose) | |
2761 | { | |
2762 | bool mask_empty = mask && !*mask; | |
2763 | ||
2764 | if (verbose || !mask_empty) { | |
2765 | bool mask_full = !mask || *mask == OVS_BE16_MAX; | |
2766 | ||
2767 | ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key)); | |
2768 | if (!mask_full) { /* Partially masked. */ | |
2769 | ds_put_format(ds, "/%#"PRIx16, ntohs(*mask)); | |
2770 | } | |
2771 | ds_put_char(ds, ','); | |
2772 | } | |
2773 | } | |
2774 | ||
2d18eae8 JR |
2775 | static void |
2776 | format_tun_flags(struct ds *ds, const char *name, uint16_t key, | |
2777 | const uint16_t *mask, bool verbose) | |
2778 | { | |
2779 | bool mask_empty = mask && !*mask; | |
2780 | ||
2781 | if (verbose || !mask_empty) { | |
2d18eae8 JR |
2782 | ds_put_cstr(ds, name); |
2783 | ds_put_char(ds, '('); | |
8e4c1621 JG |
2784 | if (mask) { |
2785 | format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, | |
2786 | *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK); | |
2d18eae8 | 2787 | } else { /* Fully masked. */ |
8e4c1621 | 2788 | format_flags(ds, flow_tun_flag_to_string, key, '|'); |
2d18eae8 JR |
2789 | } |
2790 | ds_put_cstr(ds, "),"); | |
2791 | } | |
2792 | } | |
2793 | ||
65da723b JG |
2794 | static bool |
2795 | check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma, | |
ce9c9b8b | 2796 | const struct attr_len_tbl tbl[], int max_type, bool need_key) |
65da723b JG |
2797 | { |
2798 | int expected_len; | |
2799 | ||
ce9c9b8b | 2800 | expected_len = odp_key_attr_len(tbl, max_type, nl_attr_type(a)); |
65da723b JG |
2801 | if (expected_len != ATTR_LEN_VARIABLE && |
2802 | expected_len != ATTR_LEN_NESTED) { | |
2803 | ||
2804 | bool bad_key_len = nl_attr_get_size(a) != expected_len; | |
2805 | bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len; | |
2806 | ||
2807 | if (bad_key_len || bad_mask_len) { | |
2808 | if (need_key) { | |
2809 | ds_put_format(ds, "key%u", nl_attr_type(a)); | |
2810 | } | |
2811 | if (bad_key_len) { | |
2812 | ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(", | |
2813 | nl_attr_get_size(a), expected_len); | |
2814 | } | |
2815 | format_generic_odp_key(a, ds); | |
2816 | if (ma) { | |
2817 | ds_put_char(ds, '/'); | |
2818 | if (bad_mask_len) { | |
2819 | ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(", | |
2820 | nl_attr_get_size(ma), expected_len); | |
2821 | } | |
2822 | format_generic_odp_key(ma, ds); | |
2823 | } | |
2824 | ds_put_char(ds, ')'); | |
2825 | return false; | |
2826 | } | |
2827 | } | |
2828 | ||
2829 | return true; | |
2830 | } | |
2831 | ||
2832 | static void | |
2833 | format_unknown_key(struct ds *ds, const struct nlattr *a, | |
2834 | const struct nlattr *ma) | |
2835 | { | |
2836 | ds_put_format(ds, "key%u(", nl_attr_type(a)); | |
2837 | format_generic_odp_key(a, ds); | |
2838 | if (ma && !odp_mask_attr_is_exact(ma)) { | |
2839 | ds_put_char(ds, '/'); | |
2840 | format_generic_odp_key(ma, ds); | |
2841 | } | |
2842 | ds_put_cstr(ds, "),"); | |
2843 | } | |
2844 | ||
2845 | static void | |
2846 | format_odp_tun_vxlan_opt(const struct nlattr *attr, | |
2847 | const struct nlattr *mask_attr, struct ds *ds, | |
2848 | bool verbose) | |
2849 | { | |
2850 | unsigned int left; | |
2851 | const struct nlattr *a; | |
2852 | struct ofpbuf ofp; | |
2853 | ||
2854 | ofpbuf_init(&ofp, 100); | |
2855 | NL_NESTED_FOR_EACH(a, left, attr) { | |
2856 | uint16_t type = nl_attr_type(a); | |
2857 | const struct nlattr *ma = NULL; | |
2858 | ||
2859 | if (mask_attr) { | |
2860 | ma = nl_attr_find__(nl_attr_get(mask_attr), | |
2861 | nl_attr_get_size(mask_attr), type); | |
2862 | if (!ma) { | |
2863 | ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens, | |
2864 | OVS_VXLAN_EXT_MAX, | |
2865 | &ofp, a); | |
2866 | } | |
2867 | } | |
2868 | ||
2869 | if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens, | |
2870 | OVS_VXLAN_EXT_MAX, true)) { | |
2871 | continue; | |
2872 | } | |
2873 | ||
2874 | switch (type) { | |
2875 | case OVS_VXLAN_EXT_GBP: { | |
2876 | uint32_t key = nl_attr_get_u32(a); | |
2877 | ovs_be16 id, id_mask; | |
498535c3 | 2878 | uint8_t flags, flags_mask = 0; |
65da723b JG |
2879 | |
2880 | id = htons(key & 0xFFFF); | |
2881 | flags = (key >> 16) & 0xFF; | |
2882 | if (ma) { | |
2883 | uint32_t mask = nl_attr_get_u32(ma); | |
2884 | id_mask = htons(mask & 0xFFFF); | |
2885 | flags_mask = (mask >> 16) & 0xFF; | |
2886 | } | |
2887 | ||
2888 | ds_put_cstr(ds, "gbp("); | |
2889 | format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose); | |
2890 | format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose); | |
2891 | ds_chomp(ds, ','); | |
2892 | ds_put_cstr(ds, "),"); | |
2893 | break; | |
2894 | } | |
2895 | ||
2896 | default: | |
2897 | format_unknown_key(ds, a, ma); | |
2898 | } | |
2899 | ofpbuf_clear(&ofp); | |
2900 | } | |
2901 | ||
2902 | ds_chomp(ds, ','); | |
2903 | ofpbuf_uninit(&ofp); | |
2904 | } | |
2905 | ||
622a0a8e JG |
2906 | #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL |
2907 | ||
2908 | static void | |
5bb08b0e JG |
2909 | format_geneve_opts(const struct geneve_opt *opt, |
2910 | const struct geneve_opt *mask, int opts_len, | |
2911 | struct ds *ds, bool verbose) | |
622a0a8e | 2912 | { |
622a0a8e JG |
2913 | while (opts_len > 0) { |
2914 | unsigned int len; | |
2915 | uint8_t data_len, data_len_mask; | |
2916 | ||
2917 | if (opts_len < sizeof *opt) { | |
2918 | ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE, | |
2919 | opts_len, sizeof *opt); | |
2920 | return; | |
2921 | } | |
2922 | ||
2923 | data_len = opt->length * 4; | |
2924 | if (mask) { | |
2925 | if (mask->length == 0x1f) { | |
2926 | data_len_mask = UINT8_MAX; | |
2927 | } else { | |
2928 | data_len_mask = mask->length; | |
2929 | } | |
2930 | } | |
2931 | len = sizeof *opt + data_len; | |
2932 | if (len > opts_len) { | |
2933 | ds_put_format(ds, "opt len %u greater than remaining %u", | |
2934 | len, opts_len); | |
2935 | return; | |
2936 | } | |
2937 | ||
2938 | ds_put_char(ds, '{'); | |
2939 | format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class), | |
2940 | verbose); | |
2941 | format_u8x(ds, "type", opt->type, MASK(mask, type), verbose); | |
2942 | format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose); | |
1cb20095 JG |
2943 | if (data_len && |
2944 | (verbose || !mask || !is_all_zeros(mask + 1, data_len))) { | |
622a0a8e JG |
2945 | ds_put_hex(ds, opt + 1, data_len); |
2946 | if (mask && !is_all_ones(mask + 1, data_len)) { | |
2947 | ds_put_char(ds, '/'); | |
2948 | ds_put_hex(ds, mask + 1, data_len); | |
2949 | } | |
2950 | } else { | |
2951 | ds_chomp(ds, ','); | |
2952 | } | |
2953 | ds_put_char(ds, '}'); | |
2954 | ||
2955 | opt += len / sizeof(*opt); | |
2956 | if (mask) { | |
2957 | mask += len / sizeof(*opt); | |
2958 | } | |
2959 | opts_len -= len; | |
2960 | }; | |
2961 | } | |
2962 | ||
5bb08b0e JG |
2963 | static void |
2964 | format_odp_tun_geneve(const struct nlattr *attr, | |
2965 | const struct nlattr *mask_attr, struct ds *ds, | |
2966 | bool verbose) | |
2967 | { | |
2968 | int opts_len = nl_attr_get_size(attr); | |
2969 | const struct geneve_opt *opt = nl_attr_get(attr); | |
2970 | const struct geneve_opt *mask = mask_attr ? | |
2971 | nl_attr_get(mask_attr) : NULL; | |
2972 | ||
2973 | if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) { | |
2974 | ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE, | |
2975 | nl_attr_get_size(attr), nl_attr_get_size(mask_attr)); | |
2976 | return; | |
2977 | } | |
2978 | ||
2979 | format_geneve_opts(opt, mask, opts_len, ds, verbose); | |
2980 | } | |
2981 | ||
65da723b JG |
2982 | static void |
2983 | format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr, | |
2984 | struct ds *ds, bool verbose) | |
2985 | { | |
2986 | unsigned int left; | |
2987 | const struct nlattr *a; | |
2988 | uint16_t flags = 0; | |
2989 | uint16_t mask_flags = 0; | |
2990 | struct ofpbuf ofp; | |
2991 | ||
2992 | ofpbuf_init(&ofp, 100); | |
2993 | NL_NESTED_FOR_EACH(a, left, attr) { | |
2994 | enum ovs_tunnel_key_attr type = nl_attr_type(a); | |
2995 | const struct nlattr *ma = NULL; | |
2996 | ||
2997 | if (mask_attr) { | |
2998 | ma = nl_attr_find__(nl_attr_get(mask_attr), | |
2999 | nl_attr_get_size(mask_attr), type); | |
3000 | if (!ma) { | |
3001 | ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens, | |
3002 | OVS_TUNNEL_KEY_ATTR_MAX, | |
3003 | &ofp, a); | |
3004 | } | |
3005 | } | |
3006 | ||
3007 | if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens, | |
3008 | OVS_TUNNEL_KEY_ATTR_MAX, true)) { | |
3009 | continue; | |
3010 | } | |
3011 | ||
3012 | switch (type) { | |
3013 | case OVS_TUNNEL_KEY_ATTR_ID: | |
3014 | format_be64(ds, "tun_id", nl_attr_get_be64(a), | |
3015 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3016 | flags |= FLOW_TNL_F_KEY; | |
3017 | if (ma) { | |
3018 | mask_flags |= FLOW_TNL_F_KEY; | |
3019 | } | |
3020 | break; | |
3021 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
3022 | format_ipv4(ds, "src", nl_attr_get_be32(a), | |
3023 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3024 | break; | |
3025 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
3026 | format_ipv4(ds, "dst", nl_attr_get_be32(a), | |
3027 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3028 | break; | |
ffe4c74f JB |
3029 | case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: { |
3030 | struct in6_addr ipv6_src; | |
3031 | ipv6_src = nl_attr_get_in6_addr(a); | |
3032 | format_in6_addr(ds, "ipv6_src", &ipv6_src, | |
3033 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3034 | break; | |
3035 | } | |
3036 | case OVS_TUNNEL_KEY_ATTR_IPV6_DST: { | |
3037 | struct in6_addr ipv6_dst; | |
3038 | ipv6_dst = nl_attr_get_in6_addr(a); | |
3039 | format_in6_addr(ds, "ipv6_dst", &ipv6_dst, | |
3040 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3041 | break; | |
3042 | } | |
65da723b JG |
3043 | case OVS_TUNNEL_KEY_ATTR_TOS: |
3044 | format_u8x(ds, "tos", nl_attr_get_u8(a), | |
3045 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3046 | break; | |
3047 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
3048 | format_u8u(ds, "ttl", nl_attr_get_u8(a), | |
3049 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3050 | break; | |
3051 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
3052 | flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
3053 | break; | |
3054 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
3055 | flags |= FLOW_TNL_F_CSUM; | |
3056 | break; | |
3057 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: | |
3058 | format_be16(ds, "tp_src", nl_attr_get_be16(a), | |
3059 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3060 | break; | |
3061 | case OVS_TUNNEL_KEY_ATTR_TP_DST: | |
3062 | format_be16(ds, "tp_dst", nl_attr_get_be16(a), | |
3063 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3064 | break; | |
3065 | case OVS_TUNNEL_KEY_ATTR_OAM: | |
3066 | flags |= FLOW_TNL_F_OAM; | |
3067 | break; | |
3068 | case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: | |
3069 | ds_put_cstr(ds, "vxlan("); | |
3070 | format_odp_tun_vxlan_opt(a, ma, ds, verbose); | |
3071 | ds_put_cstr(ds, "),"); | |
3072 | break; | |
3073 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: | |
622a0a8e JG |
3074 | ds_put_cstr(ds, "geneve("); |
3075 | format_odp_tun_geneve(a, ma, ds, verbose); | |
3076 | ds_put_cstr(ds, "),"); | |
3077 | break; | |
f3464818 PS |
3078 | case OVS_TUNNEL_KEY_ATTR_PAD: |
3079 | break; | |
65da723b JG |
3080 | case __OVS_TUNNEL_KEY_ATTR_MAX: |
3081 | default: | |
3082 | format_unknown_key(ds, a, ma); | |
3083 | } | |
3084 | ofpbuf_clear(&ofp); | |
3085 | } | |
3086 | ||
3087 | /* Flags can have a valid mask even if the attribute is not set, so | |
3088 | * we need to collect these separately. */ | |
3089 | if (mask_attr) { | |
3090 | NL_NESTED_FOR_EACH(a, left, mask_attr) { | |
3091 | switch (nl_attr_type(a)) { | |
3092 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
3093 | mask_flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
3094 | break; | |
3095 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
3096 | mask_flags |= FLOW_TNL_F_CSUM; | |
3097 | break; | |
3098 | case OVS_TUNNEL_KEY_ATTR_OAM: | |
3099 | mask_flags |= FLOW_TNL_F_OAM; | |
3100 | break; | |
3101 | } | |
3102 | } | |
3103 | } | |
3104 | ||
3105 | format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL, | |
3106 | verbose); | |
3107 | ds_chomp(ds, ','); | |
3108 | ofpbuf_uninit(&ofp); | |
3109 | } | |
3110 | ||
07659514 JS |
3111 | static const char * |
3112 | odp_ct_state_to_string(uint32_t flag) | |
3113 | { | |
3114 | switch (flag) { | |
3115 | case OVS_CS_F_REPLY_DIR: | |
3116 | return "rpl"; | |
3117 | case OVS_CS_F_TRACKED: | |
3118 | return "trk"; | |
3119 | case OVS_CS_F_NEW: | |
3120 | return "new"; | |
3121 | case OVS_CS_F_ESTABLISHED: | |
3122 | return "est"; | |
3123 | case OVS_CS_F_RELATED: | |
3124 | return "rel"; | |
3125 | case OVS_CS_F_INVALID: | |
3126 | return "inv"; | |
819571b5 DDP |
3127 | case OVS_CS_F_SRC_NAT: |
3128 | return "snat"; | |
3129 | case OVS_CS_F_DST_NAT: | |
3130 | return "dnat"; | |
07659514 JS |
3131 | default: |
3132 | return NULL; | |
3133 | } | |
3134 | } | |
3135 | ||
2d18eae8 JR |
3136 | static void |
3137 | format_frag(struct ds *ds, const char *name, uint8_t key, | |
08fa0266 | 3138 | const uint8_t *mask, bool verbose OVS_UNUSED) |
2d18eae8 JR |
3139 | { |
3140 | bool mask_empty = mask && !*mask; | |
08fa0266 | 3141 | bool mask_full = !mask || *mask == UINT8_MAX; |
2d18eae8 JR |
3142 | |
3143 | /* ODP frag is an enumeration field; partial masks are not meaningful. */ | |
08fa0266 PB |
3144 | if (!mask_empty && !mask_full) { |
3145 | ds_put_format(ds, "error: partial mask not supported for frag (%#" | |
3146 | PRIx8"),", *mask); | |
3147 | } else if (!mask_empty) { | |
3148 | ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key)); | |
2d18eae8 JR |
3149 | } |
3150 | } | |
3151 | ||
07659514 JS |
3152 | static bool |
3153 | mask_empty(const struct nlattr *ma) | |
3154 | { | |
3155 | const void *mask; | |
3156 | size_t n; | |
3157 | ||
3158 | if (!ma) { | |
3159 | return true; | |
3160 | } | |
3161 | mask = nl_attr_get(ma); | |
3162 | n = nl_attr_get_size(ma); | |
3163 | ||
3164 | return is_all_zeros(mask, n); | |
3165 | } | |
3166 | ||
2f73024d BP |
3167 | /* The caller must have already verified that 'a' and 'ma' have correct |
3168 | * lengths. */ | |
36956a7d | 3169 | static void |
2f73024d BP |
3170 | format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma, |
3171 | const struct hmap *portno_names, struct ds *ds, | |
3172 | bool verbose) | |
36956a7d | 3173 | { |
7a8e9ed2 | 3174 | enum ovs_key_attr attr = nl_attr_type(a); |
e6603631 | 3175 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
dc8c5408 | 3176 | bool is_exact; |
36956a7d | 3177 | |
dc8c5408 | 3178 | is_exact = ma ? odp_mask_attr_is_exact(ma) : true; |
e6cc0bab | 3179 | |
e6603631 | 3180 | ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf)); |
e6cc0bab | 3181 | |
e6cc0bab | 3182 | ds_put_char(ds, '('); |
7a8e9ed2 | 3183 | switch (attr) { |
fea393b1 | 3184 | case OVS_KEY_ATTR_ENCAP: |
e6cc0bab AZ |
3185 | if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) { |
3186 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), | |
0a37839c | 3187 | nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds, |
041e7168 | 3188 | verbose); |
0a37839c GS |
3189 | } else if (nl_attr_get_size(a)) { |
3190 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL, | |
3191 | ds, verbose); | |
fea393b1 | 3192 | } |
fea393b1 BP |
3193 | break; |
3194 | ||
abff858b | 3195 | case OVS_KEY_ATTR_PRIORITY: |
72e8bf28 | 3196 | case OVS_KEY_ATTR_SKB_MARK: |
572f732a AZ |
3197 | case OVS_KEY_ATTR_DP_HASH: |
3198 | case OVS_KEY_ATTR_RECIRC_ID: | |
e6cc0bab | 3199 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); |
dc8c5408 | 3200 | if (!is_exact) { |
e6cc0bab AZ |
3201 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); |
3202 | } | |
72e8bf28 AA |
3203 | break; |
3204 | ||
8e53fe8c JS |
3205 | case OVS_KEY_ATTR_CT_MARK: |
3206 | if (verbose || !mask_empty(ma)) { | |
3207 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); | |
3208 | if (!is_exact) { | |
3209 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); | |
3210 | } | |
3211 | } | |
3212 | break; | |
3213 | ||
07659514 JS |
3214 | case OVS_KEY_ATTR_CT_STATE: |
3215 | if (verbose) { | |
3216 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); | |
3217 | if (!is_exact) { | |
3218 | ds_put_format(ds, "/%#"PRIx32, | |
3219 | mask_empty(ma) ? 0 : nl_attr_get_u32(ma)); | |
3220 | } | |
3221 | } else if (!is_exact) { | |
3222 | format_flags_masked(ds, NULL, odp_ct_state_to_string, | |
3223 | nl_attr_get_u32(a), | |
3224 | mask_empty(ma) ? 0 : nl_attr_get_u32(ma), | |
3225 | UINT32_MAX); | |
3226 | } else { | |
3227 | format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|'); | |
3228 | } | |
3229 | break; | |
3230 | ||
3231 | case OVS_KEY_ATTR_CT_ZONE: | |
3232 | if (verbose || !mask_empty(ma)) { | |
3233 | ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a)); | |
3234 | if (!is_exact) { | |
3235 | ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma)); | |
3236 | } | |
3237 | } | |
3238 | break; | |
3239 | ||
9daf2348 | 3240 | case OVS_KEY_ATTR_CT_LABELS: { |
ab79d262 BP |
3241 | const ovs_32aligned_u128 *value = nl_attr_get(a); |
3242 | const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL; | |
9daf2348 JS |
3243 | |
3244 | format_u128(ds, value, mask, verbose); | |
3245 | break; | |
3246 | } | |
07659514 | 3247 | |
c30b4cea JR |
3248 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: { |
3249 | const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a); | |
3250 | const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL; | |
3251 | ||
3252 | format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose); | |
3253 | format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose); | |
3254 | format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto), | |
3255 | verbose); | |
3256 | format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port), | |
3257 | verbose); | |
3258 | format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port), | |
3259 | verbose); | |
3260 | ds_chomp(ds, ','); | |
3261 | break; | |
3262 | } | |
3263 | ||
3264 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: { | |
3265 | const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a); | |
3266 | const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
3267 | ||
3268 | format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src), | |
3269 | verbose); | |
3270 | format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst), | |
3271 | verbose); | |
3272 | format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto), | |
3273 | verbose); | |
3274 | format_be16(ds, "src_port", key->src_port, MASK(mask, src_port), | |
3275 | verbose); | |
3276 | format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port), | |
3277 | verbose); | |
3278 | ds_chomp(ds, ','); | |
3279 | break; | |
3280 | } | |
3281 | ||
65da723b JG |
3282 | case OVS_KEY_ATTR_TUNNEL: |
3283 | format_odp_tun_attr(a, ma, ds, verbose); | |
356af50b | 3284 | break; |
65da723b | 3285 | |
df2c07f4 | 3286 | case OVS_KEY_ATTR_IN_PORT: |
0722f341 BP |
3287 | if (is_exact) { |
3288 | odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds); | |
0a37839c GS |
3289 | } else { |
3290 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); | |
3291 | if (!is_exact) { | |
3292 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); | |
3293 | } | |
e6cc0bab | 3294 | } |
36956a7d BP |
3295 | break; |
3296 | ||
beb75a40 | 3297 | case OVS_KEY_ATTR_PACKET_TYPE: { |
3d4b2e6e JS |
3298 | ovs_be32 value = nl_attr_get_be32(a); |
3299 | ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX; | |
beb75a40 | 3300 | |
3d4b2e6e JS |
3301 | ovs_be16 ns = htons(pt_ns(value)); |
3302 | ovs_be16 ns_mask = htons(pt_ns(mask)); | |
3303 | format_be16(ds, "ns", ns, &ns_mask, verbose); | |
beb75a40 | 3304 | |
3d4b2e6e JS |
3305 | ovs_be16 ns_type = pt_ns_type_be(value); |
3306 | ovs_be16 ns_type_mask = pt_ns_type_be(mask); | |
3307 | format_be16x(ds, "id", ns_type, &ns_type_mask, verbose); | |
3308 | ||
3309 | ds_chomp(ds, ','); | |
beb75a40 JS |
3310 | break; |
3311 | } | |
3312 | ||
2d18eae8 JR |
3313 | case OVS_KEY_ATTR_ETHERNET: { |
3314 | const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL; | |
3315 | const struct ovs_key_ethernet *key = nl_attr_get(a); | |
e6cc0bab | 3316 | |
2d18eae8 JR |
3317 | format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose); |
3318 | format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose); | |
3319 | ds_chomp(ds, ','); | |
36956a7d | 3320 | break; |
2d18eae8 | 3321 | } |
fea393b1 | 3322 | case OVS_KEY_ATTR_VLAN: |
2d18eae8 JR |
3323 | format_vlan_tci(ds, nl_attr_get_be16(a), |
3324 | ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose); | |
36956a7d BP |
3325 | break; |
3326 | ||
b02475c5 SH |
3327 | case OVS_KEY_ATTR_MPLS: { |
3328 | const struct ovs_key_mpls *mpls_key = nl_attr_get(a); | |
e6cc0bab | 3329 | const struct ovs_key_mpls *mpls_mask = NULL; |
8bfd0fda BP |
3330 | size_t size = nl_attr_get_size(a); |
3331 | ||
3332 | if (!size || size % sizeof *mpls_key) { | |
6d670e7f | 3333 | ds_put_format(ds, "(bad key length %"PRIuSIZE")", size); |
8bfd0fda BP |
3334 | return; |
3335 | } | |
dc8c5408 | 3336 | if (!is_exact) { |
e6cc0bab | 3337 | mpls_mask = nl_attr_get(ma); |
6d670e7f | 3338 | if (size != nl_attr_get_size(ma)) { |
8bfd0fda BP |
3339 | ds_put_format(ds, "(key length %"PRIuSIZE" != " |
3340 | "mask length %"PRIuSIZE")", | |
6d670e7f | 3341 | size, nl_attr_get_size(ma)); |
8bfd0fda BP |
3342 | return; |
3343 | } | |
e6cc0bab | 3344 | } |
8bfd0fda | 3345 | format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key); |
b02475c5 SH |
3346 | break; |
3347 | } | |
df2c07f4 | 3348 | case OVS_KEY_ATTR_ETHERTYPE: |
e6cc0bab | 3349 | ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a))); |
dc8c5408 | 3350 | if (!is_exact) { |
e6cc0bab AZ |
3351 | ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma))); |
3352 | } | |
36956a7d BP |
3353 | break; |
3354 | ||
2d18eae8 JR |
3355 | case OVS_KEY_ATTR_IPV4: { |
3356 | const struct ovs_key_ipv4 *key = nl_attr_get(a); | |
3357 | const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL; | |
3358 | ||
3359 | format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose); | |
3360 | format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose); | |
3361 | format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto), | |
3362 | verbose); | |
3363 | format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose); | |
3364 | format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose); | |
3365 | format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag), | |
3366 | verbose); | |
3367 | ds_chomp(ds, ','); | |
36956a7d | 3368 | break; |
2d18eae8 JR |
3369 | } |
3370 | case OVS_KEY_ATTR_IPV6: { | |
3371 | const struct ovs_key_ipv6 *key = nl_attr_get(a); | |
3372 | const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
3373 | ||
932c96b7 JR |
3374 | format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src), |
3375 | verbose); | |
3376 | format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst), | |
3377 | verbose); | |
2d18eae8 JR |
3378 | format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label), |
3379 | verbose); | |
3380 | format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto), | |
932c96b7 | 3381 | verbose); |
2d18eae8 | 3382 | format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass), |
932c96b7 | 3383 | verbose); |
2d18eae8 | 3384 | format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit), |
932c96b7 | 3385 | verbose); |
2d18eae8 JR |
3386 | format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag), |
3387 | verbose); | |
3388 | ds_chomp(ds, ','); | |
d31f1109 | 3389 | break; |
2d18eae8 JR |
3390 | } |
3391 | /* These have the same structure and format. */ | |
df2c07f4 | 3392 | case OVS_KEY_ATTR_TCP: |
2d18eae8 JR |
3393 | case OVS_KEY_ATTR_UDP: |
3394 | case OVS_KEY_ATTR_SCTP: { | |
3395 | const struct ovs_key_tcp *key = nl_attr_get(a); | |
3396 | const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL; | |
e6cc0bab | 3397 | |
2d18eae8 JR |
3398 | format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose); |
3399 | format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose); | |
3400 | ds_chomp(ds, ','); | |
36956a7d | 3401 | break; |
2d18eae8 | 3402 | } |
dc235f7f | 3403 | case OVS_KEY_ATTR_TCP_FLAGS: |
dc235f7f | 3404 | if (!is_exact) { |
ea2735d3 JR |
3405 | format_flags_masked(ds, NULL, packet_tcp_flag_to_string, |
3406 | ntohs(nl_attr_get_be16(a)), | |
8e4c1621 JG |
3407 | TCP_FLAGS(nl_attr_get_be16(ma)), |
3408 | TCP_FLAGS(OVS_BE16_MAX)); | |
ea2735d3 JR |
3409 | } else { |
3410 | format_flags(ds, packet_tcp_flag_to_string, | |
8e4c1621 | 3411 | ntohs(nl_attr_get_be16(a)), '|'); |
dc235f7f JR |
3412 | } |
3413 | break; | |
3414 | ||
2d18eae8 JR |
3415 | case OVS_KEY_ATTR_ICMP: { |
3416 | const struct ovs_key_icmp *key = nl_attr_get(a); | |
3417 | const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL; | |
e6cc0bab | 3418 | |
2d18eae8 JR |
3419 | format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose); |
3420 | format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose); | |
3421 | ds_chomp(ds, ','); | |
36956a7d | 3422 | break; |
2d18eae8 JR |
3423 | } |
3424 | case OVS_KEY_ATTR_ICMPV6: { | |
3425 | const struct ovs_key_icmpv6 *key = nl_attr_get(a); | |
3426 | const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
36956a7d | 3427 | |
2d18eae8 JR |
3428 | format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type), |
3429 | verbose); | |
3430 | format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code), | |
3431 | verbose); | |
3432 | ds_chomp(ds, ','); | |
d31f1109 | 3433 | break; |
2d18eae8 JR |
3434 | } |
3435 | case OVS_KEY_ATTR_ARP: { | |
3436 | const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL; | |
3437 | const struct ovs_key_arp *key = nl_attr_get(a); | |
d31f1109 | 3438 | |
2d18eae8 JR |
3439 | format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose); |
3440 | format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose); | |
3441 | format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose); | |
3442 | format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose); | |
3443 | format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose); | |
3444 | ds_chomp(ds, ','); | |
36956a7d | 3445 | break; |
2d18eae8 | 3446 | } |
df2c07f4 | 3447 | case OVS_KEY_ATTR_ND: { |
2d18eae8 JR |
3448 | const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL; |
3449 | const struct ovs_key_nd *key = nl_attr_get(a); | |
e6cc0bab | 3450 | |
932c96b7 JR |
3451 | format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target), |
3452 | verbose); | |
2d18eae8 JR |
3453 | format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose); |
3454 | format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose); | |
685a51a5 | 3455 | |
2d18eae8 | 3456 | ds_chomp(ds, ','); |
685a51a5 JP |
3457 | break; |
3458 | } | |
3d2fbd70 JS |
3459 | case OVS_KEY_ATTR_NSH: { |
3460 | const struct ovs_key_nsh *mask = ma ? nl_attr_get(ma) : NULL; | |
3461 | const struct ovs_key_nsh *key = nl_attr_get(a); | |
3462 | format_nsh_key_mask(ds, key, mask); | |
3463 | break; | |
3464 | } | |
7a8e9ed2 BP |
3465 | case OVS_KEY_ATTR_UNSPEC: |
3466 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
3467 | default: |
3468 | format_generic_odp_key(a, ds); | |
dc8c5408 | 3469 | if (!is_exact) { |
e6cc0bab AZ |
3470 | ds_put_char(ds, '/'); |
3471 | format_generic_odp_key(ma, ds); | |
3472 | } | |
36956a7d BP |
3473 | break; |
3474 | } | |
e6cc0bab | 3475 | ds_put_char(ds, ')'); |
36956a7d BP |
3476 | } |
3477 | ||
2f73024d BP |
3478 | static void |
3479 | format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, | |
3480 | const struct hmap *portno_names, struct ds *ds, | |
3481 | bool verbose) | |
3482 | { | |
3483 | if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens, | |
3484 | OVS_KEY_ATTR_MAX, false)) { | |
3485 | format_odp_key_attr__(a, ma, portno_names, ds, verbose); | |
3486 | } | |
3487 | } | |
3488 | ||
dc8c5408 | 3489 | static struct nlattr * |
6b8da9e9 JG |
3490 | generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max, |
3491 | struct ofpbuf *ofp, const struct nlattr *key) | |
dc8c5408 AZ |
3492 | { |
3493 | const struct nlattr *a; | |
3494 | unsigned int left; | |
3495 | int type = nl_attr_type(key); | |
3496 | int size = nl_attr_get_size(key); | |
3497 | ||
6b8da9e9 | 3498 | if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) { |
9ddf12cc | 3499 | nl_msg_put_unspec_zero(ofp, type, size); |
dc8c5408 AZ |
3500 | } else { |
3501 | size_t nested_mask; | |
3502 | ||
6b8da9e9 JG |
3503 | if (tbl[type].next) { |
3504 | tbl = tbl[type].next; | |
3505 | max = tbl[type].next_max; | |
3506 | } | |
3507 | ||
dc8c5408 AZ |
3508 | nested_mask = nl_msg_start_nested(ofp, type); |
3509 | NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) { | |
6b8da9e9 | 3510 | generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a)); |
dc8c5408 AZ |
3511 | } |
3512 | nl_msg_end_nested(ofp, nested_mask); | |
3513 | } | |
3514 | ||
6fd6ed71 | 3515 | return ofp->base; |
dc8c5408 AZ |
3516 | } |
3517 | ||
9daf2348 | 3518 | static void |
ab79d262 BP |
3519 | format_u128(struct ds *ds, const ovs_32aligned_u128 *key, |
3520 | const ovs_32aligned_u128 *mask, bool verbose) | |
9daf2348 | 3521 | { |
ab79d262 BP |
3522 | if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) { |
3523 | ovs_be128 value = hton128(get_32aligned_u128(key)); | |
9daf2348 | 3524 | ds_put_hex(ds, &value, sizeof value); |
ab79d262 BP |
3525 | if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) { |
3526 | value = hton128(get_32aligned_u128(mask)); | |
9daf2348 JS |
3527 | ds_put_char(ds, '/'); |
3528 | ds_put_hex(ds, &value, sizeof value); | |
3529 | } | |
3530 | } | |
3531 | } | |
3532 | ||
b23ada8e JP |
3533 | /* Read the string from 's_' as a 128-bit value. If the string contains |
3534 | * a "/", the rest of the string will be treated as a 128-bit mask. | |
3535 | * | |
3536 | * If either the value or mask is larger than 64 bits, the string must | |
3537 | * be in hexadecimal. | |
3538 | */ | |
9daf2348 JS |
3539 | static int |
3540 | scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask) | |
3541 | { | |
3542 | char *s = CONST_CAST(char *, s_); | |
3543 | ovs_be128 be_value; | |
3544 | ovs_be128 be_mask; | |
3545 | ||
3546 | if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) { | |
32ea15f6 | 3547 | *value = ntoh128(be_value); |
9daf2348 JS |
3548 | |
3549 | if (mask) { | |
3550 | int n; | |
3551 | ||
3552 | if (ovs_scan(s, "/%n", &n)) { | |
3553 | int error; | |
3554 | ||
3555 | s += n; | |
3556 | error = parse_int_string(s, (uint8_t *)&be_mask, | |
3557 | sizeof be_mask, &s); | |
3558 | if (error) { | |
3559 | return error; | |
3560 | } | |
32ea15f6 | 3561 | *mask = ntoh128(be_mask); |
9daf2348 JS |
3562 | } else { |
3563 | *mask = OVS_U128_MAX; | |
3564 | } | |
3565 | } | |
3566 | return s - s_; | |
3567 | } | |
3568 | ||
3569 | return 0; | |
3570 | } | |
3571 | ||
534a19b9 JS |
3572 | int |
3573 | odp_ufid_from_string(const char *s_, ovs_u128 *ufid) | |
3574 | { | |
3575 | const char *s = s_; | |
3576 | ||
3577 | if (ovs_scan(s, "ufid:")) { | |
534a19b9 | 3578 | s += 5; |
534a19b9 | 3579 | |
10e92b4f | 3580 | if (!uuid_from_string_prefix((struct uuid *)ufid, s)) { |
534a19b9 JS |
3581 | return -EINVAL; |
3582 | } | |
10e92b4f | 3583 | s += UUID_LEN; |
534a19b9 JS |
3584 | |
3585 | return s - s_; | |
3586 | } | |
3587 | ||
3588 | return 0; | |
3589 | } | |
3590 | ||
70e5ed6f JS |
3591 | void |
3592 | odp_format_ufid(const ovs_u128 *ufid, struct ds *ds) | |
3593 | { | |
10e92b4f | 3594 | ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid)); |
70e5ed6f JS |
3595 | } |
3596 | ||
36956a7d | 3597 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
e6cc0bab | 3598 | * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the |
73580638 | 3599 | * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is |
d1fd1ea9 | 3600 | * non-null, translates odp port number to its name. */ |
14608a15 | 3601 | void |
e6cc0bab AZ |
3602 | odp_flow_format(const struct nlattr *key, size_t key_len, |
3603 | const struct nlattr *mask, size_t mask_len, | |
0a37839c | 3604 | const struct hmap *portno_names, struct ds *ds, bool verbose) |
14608a15 | 3605 | { |
36956a7d BP |
3606 | if (key_len) { |
3607 | const struct nlattr *a; | |
3608 | unsigned int left; | |
e6cc0bab | 3609 | bool has_ethtype_key = false; |
dc8c5408 | 3610 | struct ofpbuf ofp; |
041e7168 | 3611 | bool first_field = true; |
36956a7d | 3612 | |
dc8c5408 | 3613 | ofpbuf_init(&ofp, 100); |
36956a7d | 3614 | NL_ATTR_FOR_EACH (a, left, key, key_len) { |
2f73024d BP |
3615 | int attr_type = nl_attr_type(a); |
3616 | const struct nlattr *ma = (mask && mask_len | |
3617 | ? nl_attr_find__(mask, mask_len, | |
3618 | attr_type) | |
3619 | : NULL); | |
3620 | if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens, | |
3621 | OVS_KEY_ATTR_MAX, false)) { | |
3622 | continue; | |
3623 | } | |
3624 | ||
041e7168 AZ |
3625 | bool is_nested_attr; |
3626 | bool is_wildcard = false; | |
041e7168 AZ |
3627 | |
3628 | if (attr_type == OVS_KEY_ATTR_ETHERTYPE) { | |
e6cc0bab AZ |
3629 | has_ethtype_key = true; |
3630 | } | |
041e7168 | 3631 | |
6b8da9e9 JG |
3632 | is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens, |
3633 | OVS_KEY_ATTR_MAX, attr_type) == | |
3634 | ATTR_LEN_NESTED; | |
041e7168 | 3635 | |
e6cc0bab AZ |
3636 | if (mask && mask_len) { |
3637 | ma = nl_attr_find__(mask, mask_len, nl_attr_type(a)); | |
041e7168 AZ |
3638 | is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true; |
3639 | } | |
3640 | ||
3641 | if (verbose || !is_wildcard || is_nested_attr) { | |
3642 | if (is_wildcard && !ma) { | |
6b8da9e9 JG |
3643 | ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens, |
3644 | OVS_KEY_ATTR_MAX, | |
3645 | &ofp, a); | |
dc8c5408 | 3646 | } |
041e7168 AZ |
3647 | if (!first_field) { |
3648 | ds_put_char(ds, ','); | |
3649 | } | |
2f73024d | 3650 | format_odp_key_attr__(a, ma, portno_names, ds, verbose); |
041e7168 | 3651 | first_field = false; |
e6cc0bab | 3652 | } |
dc8c5408 | 3653 | ofpbuf_clear(&ofp); |
36956a7d | 3654 | } |
dc8c5408 AZ |
3655 | ofpbuf_uninit(&ofp); |
3656 | ||
36956a7d | 3657 | if (left) { |
3a8cfc50 | 3658 | int i; |
1394054e | 3659 | |
36956a7d BP |
3660 | if (left == key_len) { |
3661 | ds_put_cstr(ds, "<empty>"); | |
3662 | } | |
3a8cfc50 BP |
3663 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
3664 | for (i = 0; i < left; i++) { | |
3665 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
3666 | } | |
3667 | ds_put_char(ds, ')'); | |
36956a7d | 3668 | } |
e6cc0bab | 3669 | if (!has_ethtype_key) { |
2f73024d BP |
3670 | const struct nlattr *ma = nl_attr_find__(mask, mask_len, |
3671 | OVS_KEY_ATTR_ETHERTYPE); | |
e6cc0bab AZ |
3672 | if (ma) { |
3673 | ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")", | |
3674 | ntohs(nl_attr_get_be16(ma))); | |
3675 | } | |
3676 | } | |
36956a7d BP |
3677 | } else { |
3678 | ds_put_cstr(ds, "<empty>"); | |
3679 | } | |
14608a15 | 3680 | } |
064af421 | 3681 | |
e6cc0bab AZ |
3682 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
3683 | * OVS_KEY_ATTR_* attributes in 'key'. */ | |
3684 | void | |
3685 | odp_flow_key_format(const struct nlattr *key, | |
3686 | size_t key_len, struct ds *ds) | |
3687 | { | |
0a37839c | 3688 | odp_flow_format(key, key_len, NULL, 0, NULL, ds, true); |
e6cc0bab AZ |
3689 | } |
3690 | ||
7257b535 BP |
3691 | static bool |
3692 | ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type) | |
3693 | { | |
3694 | if (!strcasecmp(s, "no")) { | |
3695 | *type = OVS_FRAG_TYPE_NONE; | |
3696 | } else if (!strcasecmp(s, "first")) { | |
3697 | *type = OVS_FRAG_TYPE_FIRST; | |
3698 | } else if (!strcasecmp(s, "later")) { | |
3699 | *type = OVS_FRAG_TYPE_LATER; | |
3700 | } else { | |
3701 | return false; | |
3702 | } | |
3703 | return true; | |
3704 | } | |
3705 | ||
2d18eae8 | 3706 | /* Parsing. */ |
b02475c5 | 3707 | |
3bffc610 | 3708 | static int |
74ff3298 | 3709 | scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask) |
3bffc610 | 3710 | { |
2d18eae8 | 3711 | int n; |
abff858b | 3712 | |
74ff3298 JR |
3713 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", |
3714 | ETH_ADDR_SCAN_ARGS(*key), &n)) { | |
2d18eae8 JR |
3715 | int len = n; |
3716 | ||
3717 | if (mask) { | |
3718 | if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n", | |
3719 | ETH_ADDR_SCAN_ARGS(*mask), &n)) { | |
3720 | len += n; | |
3721 | } else { | |
3722 | memset(mask, 0xff, sizeof *mask); | |
e6cc0bab | 3723 | } |
abff858b | 3724 | } |
2d18eae8 | 3725 | return len; |
abff858b | 3726 | } |
2d18eae8 JR |
3727 | return 0; |
3728 | } | |
abff858b | 3729 | |
2d18eae8 JR |
3730 | static int |
3731 | scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
3732 | { | |
3733 | int n; | |
72e8bf28 | 3734 | |
2d18eae8 JR |
3735 | if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) { |
3736 | int len = n; | |
3737 | ||
3738 | if (mask) { | |
3739 | if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n", | |
3740 | IP_SCAN_ARGS(mask), &n)) { | |
3741 | len += n; | |
3742 | } else { | |
3743 | *mask = OVS_BE32_MAX; | |
e6cc0bab | 3744 | } |
72e8bf28 | 3745 | } |
2d18eae8 | 3746 | return len; |
72e8bf28 | 3747 | } |
2d18eae8 JR |
3748 | return 0; |
3749 | } | |
72e8bf28 | 3750 | |
2d18eae8 | 3751 | static int |
d15aef48 | 3752 | scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask) |
2d18eae8 JR |
3753 | { |
3754 | int n; | |
3755 | char ipv6_s[IPV6_SCAN_LEN + 1]; | |
572f732a | 3756 | |
2d18eae8 JR |
3757 | if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n) |
3758 | && inet_pton(AF_INET6, ipv6_s, key) == 1) { | |
3759 | int len = n; | |
3760 | ||
3761 | if (mask) { | |
3762 | if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n) | |
3763 | && inet_pton(AF_INET6, ipv6_s, mask) == 1) { | |
3764 | len += n; | |
3765 | } else { | |
3766 | memset(mask, 0xff, sizeof *mask); | |
78669073 | 3767 | } |
572f732a | 3768 | } |
2d18eae8 | 3769 | return len; |
572f732a | 3770 | } |
2d18eae8 JR |
3771 | return 0; |
3772 | } | |
572f732a | 3773 | |
2d18eae8 JR |
3774 | static int |
3775 | scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
3776 | { | |
3777 | int key_, mask_; | |
3778 | int n; | |
572f732a | 3779 | |
2d18eae8 JR |
3780 | if (ovs_scan(s, "%i%n", &key_, &n) |
3781 | && (key_ & ~IPV6_LABEL_MASK) == 0) { | |
3782 | int len = n; | |
29c7a2b2 | 3783 | |
2d18eae8 JR |
3784 | *key = htonl(key_); |
3785 | if (mask) { | |
3786 | if (ovs_scan(s + len, "/%i%n", &mask_, &n) | |
3787 | && (mask_ & ~IPV6_LABEL_MASK) == 0) { | |
3788 | len += n; | |
3789 | *mask = htonl(mask_); | |
3790 | } else { | |
3791 | *mask = htonl(IPV6_LABEL_MASK); | |
e6cc0bab | 3792 | } |
35651d6a | 3793 | } |
2d18eae8 | 3794 | return len; |
35651d6a | 3795 | } |
2d18eae8 JR |
3796 | return 0; |
3797 | } | |
35651d6a | 3798 | |
2d18eae8 JR |
3799 | static int |
3800 | scan_u8(const char *s, uint8_t *key, uint8_t *mask) | |
3801 | { | |
3802 | int n; | |
3bffc610 | 3803 | |
2d18eae8 JR |
3804 | if (ovs_scan(s, "%"SCNi8"%n", key, &n)) { |
3805 | int len = n; | |
3806 | ||
3807 | if (mask) { | |
3808 | if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) { | |
3809 | len += n; | |
3810 | } else { | |
3811 | *mask = UINT8_MAX; | |
e6cc0bab | 3812 | } |
3bffc610 | 3813 | } |
2d18eae8 | 3814 | return len; |
3bffc610 | 3815 | } |
2d18eae8 JR |
3816 | return 0; |
3817 | } | |
3bffc610 | 3818 | |
07659514 JS |
3819 | static int |
3820 | scan_u16(const char *s, uint16_t *key, uint16_t *mask) | |
3821 | { | |
3822 | int n; | |
3823 | ||
3824 | if (ovs_scan(s, "%"SCNi16"%n", key, &n)) { | |
3825 | int len = n; | |
3826 | ||
3827 | if (mask) { | |
3828 | if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) { | |
3829 | len += n; | |
3830 | } else { | |
3831 | *mask = UINT16_MAX; | |
3832 | } | |
3833 | } | |
3834 | return len; | |
3835 | } | |
3836 | return 0; | |
3837 | } | |
3838 | ||
2d18eae8 JR |
3839 | static int |
3840 | scan_u32(const char *s, uint32_t *key, uint32_t *mask) | |
3841 | { | |
3842 | int n; | |
e6cc0bab | 3843 | |
2d18eae8 JR |
3844 | if (ovs_scan(s, "%"SCNi32"%n", key, &n)) { |
3845 | int len = n; | |
e6cc0bab | 3846 | |
2d18eae8 JR |
3847 | if (mask) { |
3848 | if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) { | |
3849 | len += n; | |
3850 | } else { | |
3851 | *mask = UINT32_MAX; | |
e6cc0bab | 3852 | } |
b2a60db8 | 3853 | } |
2d18eae8 | 3854 | return len; |
b2a60db8 | 3855 | } |
2d18eae8 JR |
3856 | return 0; |
3857 | } | |
b2a60db8 | 3858 | |
2d18eae8 JR |
3859 | static int |
3860 | scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
3861 | { | |
3862 | uint16_t key_, mask_; | |
3863 | int n; | |
3bffc610 | 3864 | |
2d18eae8 JR |
3865 | if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) { |
3866 | int len = n; | |
e6cc0bab | 3867 | |
2d18eae8 JR |
3868 | *key = htons(key_); |
3869 | if (mask) { | |
3870 | if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) { | |
3871 | len += n; | |
3872 | *mask = htons(mask_); | |
3873 | } else { | |
3874 | *mask = OVS_BE16_MAX; | |
e6cc0bab | 3875 | } |
3bffc610 | 3876 | } |
2d18eae8 | 3877 | return len; |
3bffc610 | 3878 | } |
2d18eae8 JR |
3879 | return 0; |
3880 | } | |
3bffc610 | 3881 | |
3d2fbd70 JS |
3882 | static int |
3883 | scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
3884 | { | |
3885 | uint32_t key_, mask_; | |
3886 | int n; | |
3887 | ||
3888 | if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) { | |
3889 | int len = n; | |
3890 | ||
3891 | *key = htonl(key_); | |
3892 | if (mask) { | |
3893 | if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) { | |
3894 | len += n; | |
3895 | *mask = htonl(mask_); | |
3896 | } else { | |
3897 | *mask = OVS_BE32_MAX; | |
3898 | } | |
3899 | } | |
3900 | return len; | |
3901 | } | |
3902 | return 0; | |
3903 | } | |
3904 | ||
2d18eae8 JR |
3905 | static int |
3906 | scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask) | |
3907 | { | |
3908 | uint64_t key_, mask_; | |
3909 | int n; | |
3bffc610 | 3910 | |
2d18eae8 JR |
3911 | if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) { |
3912 | int len = n; | |
3913 | ||
3914 | *key = htonll(key_); | |
3915 | if (mask) { | |
3916 | if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) { | |
3917 | len += n; | |
3918 | *mask = htonll(mask_); | |
3919 | } else { | |
3920 | *mask = OVS_BE64_MAX; | |
e6cc0bab | 3921 | } |
3bffc610 | 3922 | } |
2d18eae8 | 3923 | return len; |
3bffc610 | 3924 | } |
2d18eae8 JR |
3925 | return 0; |
3926 | } | |
3bffc610 | 3927 | |
2d18eae8 JR |
3928 | static int |
3929 | scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask) | |
3930 | { | |
3931 | uint32_t flags, fmask; | |
3932 | int n; | |
3bffc610 | 3933 | |
8e4c1621 JG |
3934 | n = parse_odp_flags(s, flow_tun_flag_to_string, &flags, |
3935 | FLOW_TNL_F_MASK, mask ? &fmask : NULL); | |
2d18eae8 JR |
3936 | if (n >= 0 && s[n] == ')') { |
3937 | *key = flags; | |
3938 | if (mask) { | |
3939 | *mask = fmask; | |
3bffc610 | 3940 | } |
2d18eae8 | 3941 | return n + 1; |
3bffc610 | 3942 | } |
2d18eae8 JR |
3943 | return 0; |
3944 | } | |
3bffc610 | 3945 | |
2d18eae8 JR |
3946 | static int |
3947 | scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
3948 | { | |
3949 | uint32_t flags, fmask; | |
3950 | int n; | |
b02475c5 | 3951 | |
8e4c1621 JG |
3952 | n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags, |
3953 | TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL); | |
2d18eae8 JR |
3954 | if (n >= 0) { |
3955 | *key = htons(flags); | |
3956 | if (mask) { | |
3957 | *mask = htons(fmask); | |
b02475c5 | 3958 | } |
2d18eae8 | 3959 | return n; |
b02475c5 | 3960 | } |
2d18eae8 JR |
3961 | return 0; |
3962 | } | |
b02475c5 | 3963 | |
07659514 JS |
3964 | static uint32_t |
3965 | ovs_to_odp_ct_state(uint8_t state) | |
3966 | { | |
3967 | uint32_t odp = 0; | |
3968 | ||
fd6cd1bf BP |
3969 | #define CS_STATE(ENUM, INDEX, NAME) \ |
3970 | if (state & CS_##ENUM) { \ | |
3971 | odp |= OVS_CS_F_##ENUM; \ | |
819571b5 | 3972 | } |
fd6cd1bf BP |
3973 | CS_STATES |
3974 | #undef CS_STATE | |
07659514 JS |
3975 | |
3976 | return odp; | |
3977 | } | |
3978 | ||
3979 | static uint8_t | |
3980 | odp_to_ovs_ct_state(uint32_t flags) | |
3981 | { | |
3982 | uint32_t state = 0; | |
3983 | ||
fd6cd1bf BP |
3984 | #define CS_STATE(ENUM, INDEX, NAME) \ |
3985 | if (flags & OVS_CS_F_##ENUM) { \ | |
3986 | state |= CS_##ENUM; \ | |
819571b5 | 3987 | } |
fd6cd1bf BP |
3988 | CS_STATES |
3989 | #undef CS_STATE | |
07659514 JS |
3990 | |
3991 | return state; | |
3992 | } | |
3993 | ||
3994 | static int | |
3995 | scan_ct_state(const char *s, uint32_t *key, uint32_t *mask) | |
3996 | { | |
3997 | uint32_t flags, fmask; | |
3998 | int n; | |
3999 | ||
4000 | n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags, | |
4001 | ovs_to_odp_ct_state(CS_SUPPORTED_MASK), | |
4002 | mask ? &fmask : NULL); | |
4003 | ||
4004 | if (n >= 0) { | |
4005 | *key = flags; | |
4006 | if (mask) { | |
4007 | *mask = fmask; | |
4008 | } | |
4009 | return n; | |
4010 | } | |
4011 | return 0; | |
4012 | } | |
4013 | ||
2d18eae8 JR |
4014 | static int |
4015 | scan_frag(const char *s, uint8_t *key, uint8_t *mask) | |
4016 | { | |
4017 | int n; | |
4018 | char frag[8]; | |
4019 | enum ovs_frag_type frag_type; | |
e6cc0bab | 4020 | |
2d18eae8 JR |
4021 | if (ovs_scan(s, "%7[a-z]%n", frag, &n) |
4022 | && ovs_frag_type_from_string(frag, &frag_type)) { | |
4023 | int len = n; | |
e6cc0bab | 4024 | |
2d18eae8 JR |
4025 | *key = frag_type; |
4026 | if (mask) { | |
4027 | *mask = UINT8_MAX; | |
3bffc610 | 4028 | } |
2d18eae8 | 4029 | return len; |
3bffc610 | 4030 | } |
2d18eae8 JR |
4031 | return 0; |
4032 | } | |
3bffc610 | 4033 | |
2d18eae8 JR |
4034 | static int |
4035 | scan_port(const char *s, uint32_t *key, uint32_t *mask, | |
4036 | const struct simap *port_names) | |
4037 | { | |
4038 | int n; | |
3bffc610 | 4039 | |
2d18eae8 JR |
4040 | if (ovs_scan(s, "%"SCNi32"%n", key, &n)) { |
4041 | int len = n; | |
e6cc0bab | 4042 | |
2d18eae8 JR |
4043 | if (mask) { |
4044 | if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) { | |
4045 | len += n; | |
4046 | } else { | |
4047 | *mask = UINT32_MAX; | |
e6cc0bab | 4048 | } |
3bffc610 | 4049 | } |
2d18eae8 JR |
4050 | return len; |
4051 | } else if (port_names) { | |
4052 | const struct simap_node *node; | |
4053 | int len; | |
e6cc0bab | 4054 | |
2d18eae8 JR |
4055 | len = strcspn(s, ")"); |
4056 | node = simap_find_len(port_names, s, len); | |
4057 | if (node) { | |
4058 | *key = node->data; | |
e6cc0bab AZ |
4059 | |
4060 | if (mask) { | |
2d18eae8 | 4061 | *mask = UINT32_MAX; |
e6cc0bab | 4062 | } |
2d18eae8 | 4063 | return len; |
3bffc610 BP |
4064 | } |
4065 | } | |
2d18eae8 JR |
4066 | return 0; |
4067 | } | |
3bffc610 | 4068 | |
2d18eae8 JR |
4069 | /* Helper for vlan parsing. */ |
4070 | struct ovs_key_vlan__ { | |
4071 | ovs_be16 tci; | |
4072 | }; | |
dc235f7f | 4073 | |
2d18eae8 JR |
4074 | static bool |
4075 | set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value) | |
4076 | { | |
4077 | const uint16_t mask = ((1U << bits) - 1) << offset; | |
ea2735d3 | 4078 | |
2d18eae8 JR |
4079 | if (value >> bits) { |
4080 | return false; | |
dc235f7f JR |
4081 | } |
4082 | ||
2d18eae8 JR |
4083 | *bf = htons((ntohs(*bf) & ~mask) | (value << offset)); |
4084 | return true; | |
4085 | } | |
e6cc0bab | 4086 | |
2d18eae8 JR |
4087 | static int |
4088 | scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits, | |
4089 | uint8_t offset) | |
4090 | { | |
4091 | uint16_t key_, mask_; | |
4092 | int n; | |
3bffc610 | 4093 | |
2d18eae8 JR |
4094 | if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) { |
4095 | int len = n; | |
e6cc0bab | 4096 | |
2d18eae8 | 4097 | if (set_be16_bf(key, bits, offset, key_)) { |
e6cc0bab | 4098 | if (mask) { |
2d18eae8 JR |
4099 | if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) { |
4100 | len += n; | |
4101 | ||
4102 | if (!set_be16_bf(mask, bits, offset, mask_)) { | |
4103 | return 0; | |
4104 | } | |
4105 | } else { | |
4106 | *mask |= htons(((1U << bits) - 1) << offset); | |
4107 | } | |
e6cc0bab | 4108 | } |
2d18eae8 | 4109 | return len; |
3bffc610 BP |
4110 | } |
4111 | } | |
2d18eae8 JR |
4112 | return 0; |
4113 | } | |
3bffc610 | 4114 | |
2d18eae8 JR |
4115 | static int |
4116 | scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
4117 | { | |
4118 | return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT); | |
4119 | } | |
c6bcb685 | 4120 | |
2d18eae8 JR |
4121 | static int |
4122 | scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
4123 | { | |
4124 | return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT); | |
4125 | } | |
c6bcb685 | 4126 | |
2d18eae8 JR |
4127 | static int |
4128 | scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
4129 | { | |
4130 | return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT); | |
4131 | } | |
c6bcb685 | 4132 | |
2d18eae8 JR |
4133 | /* For MPLS. */ |
4134 | static bool | |
4135 | set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value) | |
4136 | { | |
4137 | const uint32_t mask = ((1U << bits) - 1) << offset; | |
c6bcb685 | 4138 | |
2d18eae8 JR |
4139 | if (value >> bits) { |
4140 | return false; | |
c6bcb685 JS |
4141 | } |
4142 | ||
2d18eae8 JR |
4143 | *bf = htonl((ntohl(*bf) & ~mask) | (value << offset)); |
4144 | return true; | |
4145 | } | |
3bffc610 | 4146 | |
2d18eae8 JR |
4147 | static int |
4148 | scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits, | |
4149 | uint8_t offset) | |
4150 | { | |
4151 | uint32_t key_, mask_; | |
4152 | int n; | |
3bffc610 | 4153 | |
2d18eae8 JR |
4154 | if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) { |
4155 | int len = n; | |
e6cc0bab | 4156 | |
2d18eae8 | 4157 | if (set_be32_bf(key, bits, offset, key_)) { |
e6cc0bab | 4158 | if (mask) { |
2d18eae8 JR |
4159 | if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) { |
4160 | len += n; | |
3bffc610 | 4161 | |
2d18eae8 JR |
4162 | if (!set_be32_bf(mask, bits, offset, mask_)) { |
4163 | return 0; | |
4164 | } | |
4165 | } else { | |
4166 | *mask |= htonl(((1U << bits) - 1) << offset); | |
4167 | } | |
e6cc0bab | 4168 | } |
2d18eae8 | 4169 | return len; |
3bffc610 BP |
4170 | } |
4171 | } | |
2d18eae8 JR |
4172 | return 0; |
4173 | } | |
3bffc610 | 4174 | |
2d18eae8 JR |
4175 | static int |
4176 | scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4177 | { | |
4178 | return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT); | |
4179 | } | |
3bffc610 | 4180 | |
2d18eae8 JR |
4181 | static int |
4182 | scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4183 | { | |
4184 | return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT); | |
4185 | } | |
e6cc0bab | 4186 | |
2d18eae8 JR |
4187 | static int |
4188 | scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4189 | { | |
4190 | return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT); | |
4191 | } | |
e6cc0bab | 4192 | |
2d18eae8 JR |
4193 | static int |
4194 | scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4195 | { | |
4196 | return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT); | |
4197 | } | |
4198 | ||
65da723b JG |
4199 | static int |
4200 | scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask) | |
4201 | { | |
4202 | const char *s_base = s; | |
3139b8e9 JR |
4203 | ovs_be16 id = 0, id_mask = 0; |
4204 | uint8_t flags = 0, flags_mask = 0; | |
65da723b JG |
4205 | |
4206 | if (!strncmp(s, "id=", 3)) { | |
4207 | s += 3; | |
4208 | s += scan_be16(s, &id, mask ? &id_mask : NULL); | |
65da723b JG |
4209 | } |
4210 | ||
4211 | if (s[0] == ',') { | |
4212 | s++; | |
4213 | } | |
4214 | if (!strncmp(s, "flags=", 6)) { | |
4215 | s += 6; | |
4216 | s += scan_u8(s, &flags, mask ? &flags_mask : NULL); | |
65da723b JG |
4217 | } |
4218 | ||
4219 | if (!strncmp(s, "))", 2)) { | |
4220 | s += 2; | |
4221 | ||
4222 | *key = (flags << 16) | ntohs(id); | |
4223 | if (mask) { | |
4224 | *mask = (flags_mask << 16) | ntohs(id_mask); | |
4225 | } | |
4226 | ||
4227 | return s - s_base; | |
4228 | } | |
4229 | ||
4230 | return 0; | |
4231 | } | |
4232 | ||
622a0a8e JG |
4233 | static int |
4234 | scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask) | |
4235 | { | |
4236 | const char *s_base = s; | |
c05c01cd JG |
4237 | struct geneve_opt *opt = key->d; |
4238 | struct geneve_opt *opt_mask = mask ? mask->d : NULL; | |
622a0a8e JG |
4239 | int len_remain = sizeof key->d; |
4240 | ||
4241 | while (s[0] == '{' && len_remain >= sizeof *opt) { | |
4242 | int data_len = 0; | |
4243 | ||
4244 | s++; | |
4245 | len_remain -= sizeof *opt; | |
4246 | ||
4247 | if (!strncmp(s, "class=", 6)) { | |
4248 | s += 6; | |
4249 | s += scan_be16(s, &opt->opt_class, | |
4250 | mask ? &opt_mask->opt_class : NULL); | |
4251 | } else if (mask) { | |
4252 | memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class); | |
4253 | } | |
4254 | ||
4255 | if (s[0] == ',') { | |
4256 | s++; | |
4257 | } | |
4258 | if (!strncmp(s, "type=", 5)) { | |
4259 | s += 5; | |
4260 | s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL); | |
4261 | } else if (mask) { | |
4262 | memset(&opt_mask->type, 0, sizeof opt_mask->type); | |
4263 | } | |
4264 | ||
4265 | if (s[0] == ',') { | |
4266 | s++; | |
4267 | } | |
4268 | if (!strncmp(s, "len=", 4)) { | |
4269 | uint8_t opt_len, opt_len_mask; | |
4270 | s += 4; | |
4271 | s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL); | |
4272 | ||
4273 | if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) { | |
4274 | return 0; | |
4275 | } | |
4276 | opt->length = opt_len / 4; | |
4277 | if (mask) { | |
4278 | opt_mask->length = opt_len_mask; | |
4279 | } | |
4280 | data_len = opt_len; | |
4281 | } else if (mask) { | |
4282 | memset(&opt_mask->type, 0, sizeof opt_mask->type); | |
4283 | } | |
4284 | ||
4285 | if (s[0] == ',') { | |
4286 | s++; | |
4287 | } | |
4288 | if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) { | |
4289 | return 0; | |
4290 | } | |
4291 | ||
4292 | if (mask) { | |
4293 | if (s[0] == '/') { | |
4294 | s++; | |
4295 | if (parse_int_string(s, (uint8_t *)(opt_mask + 1), | |
4296 | data_len, (char **)&s)) { | |
4297 | return 0; | |
4298 | } | |
4299 | } | |
4300 | opt_mask->r1 = 0; | |
4301 | opt_mask->r2 = 0; | |
4302 | opt_mask->r3 = 0; | |
4303 | } | |
4304 | ||
4305 | if (s[0] == '}') { | |
4306 | s++; | |
4307 | opt += 1 + data_len / 4; | |
4308 | if (mask) { | |
4309 | opt_mask += 1 + data_len / 4; | |
4310 | } | |
4311 | len_remain -= data_len; | |
4312 | } | |
4313 | } | |
4314 | ||
4315 | if (s[0] == ')') { | |
4316 | int len = sizeof key->d - len_remain; | |
4317 | ||
4318 | s++; | |
4319 | key->len = len; | |
4320 | if (mask) { | |
4321 | mask->len = len; | |
4322 | } | |
4323 | return s - s_base; | |
4324 | } | |
4325 | ||
4326 | return 0; | |
4327 | } | |
4328 | ||
65da723b JG |
4329 | static void |
4330 | tun_flags_to_attr(struct ofpbuf *a, const void *data_) | |
4331 | { | |
4332 | const uint16_t *flags = data_; | |
4333 | ||
4334 | if (*flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
4335 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
4336 | } | |
4337 | if (*flags & FLOW_TNL_F_CSUM) { | |
4338 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
4339 | } | |
4340 | if (*flags & FLOW_TNL_F_OAM) { | |
4341 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM); | |
4342 | } | |
4343 | } | |
4344 | ||
4345 | static void | |
4346 | vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_) | |
4347 | { | |
4348 | const uint32_t *gbp = data_; | |
4349 | ||
4350 | if (*gbp) { | |
4351 | size_t vxlan_opts_ofs; | |
4352 | ||
4353 | vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS); | |
4354 | nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp); | |
4355 | nl_msg_end_nested(a, vxlan_opts_ofs); | |
4356 | } | |
4357 | } | |
4358 | ||
622a0a8e JG |
4359 | static void |
4360 | geneve_to_attr(struct ofpbuf *a, const void *data_) | |
4361 | { | |
4362 | const struct geneve_scan *geneve = data_; | |
4363 | ||
4364 | nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d, | |
4365 | geneve->len); | |
4366 | } | |
4367 | ||
65da723b JG |
4368 | #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \ |
4369 | { \ | |
4370 | unsigned long call_fn = (unsigned long)FUNC; \ | |
4371 | if (call_fn) { \ | |
4372 | typedef void (*fn)(struct ofpbuf *, const void *); \ | |
4373 | fn func = FUNC; \ | |
4374 | func(BUF, &(DATA)); \ | |
4375 | } else { \ | |
4376 | nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \ | |
4377 | } \ | |
2d18eae8 JR |
4378 | } |
4379 | ||
4380 | #define SCAN_IF(NAME) \ | |
4381 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
4382 | const char *start = s; \ | |
4383 | int len; \ | |
4384 | \ | |
4385 | s += strlen(NAME) | |
4386 | ||
4387 | /* Usually no special initialization is needed. */ | |
4388 | #define SCAN_BEGIN(NAME, TYPE) \ | |
4389 | SCAN_IF(NAME); \ | |
4390 | TYPE skey, smask; \ | |
4391 | memset(&skey, 0, sizeof skey); \ | |
4392 | memset(&smask, 0, sizeof smask); \ | |
4393 | do { \ | |
4394 | len = 0; | |
4395 | ||
657ac953 JR |
4396 | /* Init as fully-masked as mask will not be scanned. */ |
4397 | #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \ | |
4398 | SCAN_IF(NAME); \ | |
4399 | TYPE skey, smask; \ | |
4400 | memset(&skey, 0, sizeof skey); \ | |
4401 | memset(&smask, 0xff, sizeof smask); \ | |
4402 | do { \ | |
4403 | len = 0; | |
4404 | ||
2d18eae8 JR |
4405 | /* VLAN needs special initialization. */ |
4406 | #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \ | |
4407 | SCAN_IF(NAME); \ | |
4408 | TYPE skey = KEY_INIT; \ | |
4409 | TYPE smask = MASK_INIT; \ | |
4410 | do { \ | |
4411 | len = 0; | |
4412 | ||
4413 | /* Scan unnamed entry as 'TYPE' */ | |
4414 | #define SCAN_TYPE(TYPE, KEY, MASK) \ | |
4415 | len = scan_##TYPE(s, KEY, MASK); \ | |
4416 | if (len == 0) { \ | |
4417 | return -EINVAL; \ | |
4418 | } \ | |
4419 | s += len | |
4420 | ||
4421 | /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */ | |
4422 | #define SCAN_FIELD(NAME, TYPE, FIELD) \ | |
4423 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
4424 | s += strlen(NAME); \ | |
4425 | SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \ | |
4426 | continue; \ | |
4427 | } | |
4428 | ||
4429 | #define SCAN_FINISH() \ | |
4430 | } while (*s++ == ',' && len != 0); \ | |
4431 | if (s[-1] != ')') { \ | |
4432 | return -EINVAL; \ | |
4433 | } | |
4434 | ||
4435 | #define SCAN_FINISH_SINGLE() \ | |
4436 | } while (false); \ | |
4437 | if (*s++ != ')') { \ | |
4438 | return -EINVAL; \ | |
4439 | } | |
4440 | ||
65da723b JG |
4441 | /* Beginning of nested attribute. */ |
4442 | #define SCAN_BEGIN_NESTED(NAME, ATTR) \ | |
4443 | SCAN_IF(NAME); \ | |
4444 | size_t key_offset, mask_offset; \ | |
4445 | key_offset = nl_msg_start_nested(key, ATTR); \ | |
4446 | if (mask) { \ | |
4447 | mask_offset = nl_msg_start_nested(mask, ATTR); \ | |
4448 | } \ | |
4449 | do { \ | |
4450 | len = 0; | |
4451 | ||
4452 | #define SCAN_END_NESTED() \ | |
4453 | SCAN_FINISH(); \ | |
4454 | nl_msg_end_nested(key, key_offset); \ | |
4455 | if (mask) { \ | |
4456 | nl_msg_end_nested(mask, mask_offset); \ | |
4457 | } \ | |
4458 | return s - start; \ | |
4459 | } | |
4460 | ||
4461 | #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \ | |
4462 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
4463 | TYPE skey, smask; \ | |
4464 | memset(&skey, 0, sizeof skey); \ | |
4465 | memset(&smask, 0xff, sizeof smask); \ | |
4466 | s += strlen(NAME); \ | |
4467 | SCAN_TYPE(SCAN_AS, &skey, &smask); \ | |
4468 | SCAN_PUT(ATTR, FUNC); \ | |
4469 | continue; \ | |
4470 | } | |
4471 | ||
4472 | #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \ | |
4473 | SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL) | |
4474 | ||
4475 | #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \ | |
4476 | SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC) | |
4477 | ||
4478 | #define SCAN_PUT(ATTR, FUNC) \ | |
00ec4017 HE |
4479 | SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \ |
4480 | if (mask) \ | |
4481 | SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \ | |
2d18eae8 JR |
4482 | |
4483 | #define SCAN_END(ATTR) \ | |
4484 | SCAN_FINISH(); \ | |
65da723b | 4485 | SCAN_PUT(ATTR, NULL); \ |
2d18eae8 JR |
4486 | return s - start; \ |
4487 | } | |
4488 | ||
8f79bb4d JR |
4489 | #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \ |
4490 | SCAN_IF(NAME); \ | |
4491 | TYPE skey[CNT], smask[CNT]; \ | |
4492 | memset(&skey, 0, sizeof skey); \ | |
4493 | memset(&smask, 0, sizeof smask); \ | |
4494 | int idx = 0, cnt = CNT; \ | |
4495 | uint64_t fields = 0; \ | |
4496 | do { \ | |
4497 | int field = 0; \ | |
4498 | len = 0; | |
4499 | ||
4500 | /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */ | |
4501 | #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \ | |
4502 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
4503 | if (fields & (1UL << field)) { \ | |
4504 | fields = 0; \ | |
4505 | if (++idx == cnt) { \ | |
4506 | break; \ | |
4507 | } \ | |
4508 | } \ | |
4509 | s += strlen(NAME); \ | |
4510 | SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \ | |
4511 | fields |= 1UL << field; \ | |
4512 | continue; \ | |
4513 | } \ | |
4514 | field++; | |
4515 | ||
4516 | #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \ | |
4517 | nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \ | |
4518 | ||
4519 | #define SCAN_PUT_ARRAY(ATTR, CNT) \ | |
4520 | SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \ | |
4521 | if (mask) { \ | |
4522 | SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \ | |
4523 | } | |
4524 | ||
4525 | #define SCAN_END_ARRAY(ATTR) \ | |
4526 | SCAN_FINISH(); \ | |
4527 | if (idx == cnt) { \ | |
4528 | return -EINVAL; \ | |
4529 | } \ | |
4530 | SCAN_PUT_ARRAY(ATTR, idx + 1); \ | |
4531 | return s - start; \ | |
4532 | } | |
4533 | ||
2d18eae8 JR |
4534 | #define SCAN_END_SINGLE(ATTR) \ |
4535 | SCAN_FINISH_SINGLE(); \ | |
65da723b | 4536 | SCAN_PUT(ATTR, NULL); \ |
2d18eae8 JR |
4537 | return s - start; \ |
4538 | } | |
4539 | ||
4540 | #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \ | |
4541 | SCAN_BEGIN(NAME, TYPE) { \ | |
4542 | SCAN_TYPE(SCAN_AS, &skey, &smask); \ | |
4543 | } SCAN_END_SINGLE(ATTR) | |
4544 | ||
657ac953 JR |
4545 | #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \ |
4546 | SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \ | |
4547 | SCAN_TYPE(SCAN_AS, &skey, NULL); \ | |
2d18eae8 JR |
4548 | } SCAN_END_SINGLE(ATTR) |
4549 | ||
4550 | /* scan_port needs one extra argument. */ | |
4551 | #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \ | |
4552 | SCAN_BEGIN(NAME, TYPE) { \ | |
4553 | len = scan_port(s, &skey, &smask, port_names); \ | |
4554 | if (len == 0) { \ | |
4555 | return -EINVAL; \ | |
4556 | } \ | |
4557 | s += len; \ | |
4558 | } SCAN_END_SINGLE(ATTR) | |
3bffc610 | 4559 | |
2d18eae8 JR |
4560 | static int |
4561 | parse_odp_key_mask_attr(const char *s, const struct simap *port_names, | |
4562 | struct ofpbuf *key, struct ofpbuf *mask) | |
4563 | { | |
10e92b4f | 4564 | /* Skip UFID. */ |
71f21279 BP |
4565 | ovs_u128 ufid; |
4566 | int ufid_len = odp_ufid_from_string(s, &ufid); | |
4567 | if (ufid_len) { | |
4568 | return ufid_len; | |
eb731b76 JS |
4569 | } |
4570 | ||
2d18eae8 JR |
4571 | SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY); |
4572 | SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK); | |
657ac953 JR |
4573 | SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32, |
4574 | OVS_KEY_ATTR_RECIRC_ID); | |
2d18eae8 JR |
4575 | SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH); |
4576 | ||
07659514 JS |
4577 | SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE); |
4578 | SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE); | |
8e53fe8c | 4579 | SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK); |
9daf2348 | 4580 | SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS); |
07659514 | 4581 | |
daf4d3c1 JR |
4582 | SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) { |
4583 | SCAN_FIELD("src=", ipv4, ipv4_src); | |
4584 | SCAN_FIELD("dst=", ipv4, ipv4_dst); | |
4585 | SCAN_FIELD("proto=", u8, ipv4_proto); | |
4586 | SCAN_FIELD("tp_src=", be16, src_port); | |
4587 | SCAN_FIELD("tp_dst=", be16, dst_port); | |
4588 | } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4); | |
4589 | ||
4590 | SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) { | |
4591 | SCAN_FIELD("src=", in6_addr, ipv6_src); | |
4592 | SCAN_FIELD("dst=", in6_addr, ipv6_dst); | |
4593 | SCAN_FIELD("proto=", u8, ipv6_proto); | |
4594 | SCAN_FIELD("tp_src=", be16, src_port); | |
4595 | SCAN_FIELD("tp_dst=", be16, dst_port); | |
4596 | } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6); | |
4597 | ||
65da723b JG |
4598 | SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) { |
4599 | SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID); | |
4600 | SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC); | |
4601 | SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST); | |
ffe4c74f JB |
4602 | SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC); |
4603 | SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST); | |
65da723b JG |
4604 | SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS); |
4605 | SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL); | |
4606 | SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC); | |
4607 | SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST); | |
4608 | SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr); | |
622a0a8e JG |
4609 | SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve, |
4610 | geneve_to_attr); | |
65da723b JG |
4611 | SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr); |
4612 | } SCAN_END_NESTED(); | |
2d18eae8 JR |
4613 | |
4614 | SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT); | |
4615 | ||
4616 | SCAN_BEGIN("eth(", struct ovs_key_ethernet) { | |
4617 | SCAN_FIELD("src=", eth, eth_src); | |
4618 | SCAN_FIELD("dst=", eth, eth_dst); | |
4619 | } SCAN_END(OVS_KEY_ATTR_ETHERNET); | |
4620 | ||
4621 | SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__, | |
4622 | { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) { | |
4623 | SCAN_FIELD("vid=", vid, tci); | |
4624 | SCAN_FIELD("pcp=", pcp, tci); | |
4625 | SCAN_FIELD("cfi=", cfi, tci); | |
4626 | } SCAN_END(OVS_KEY_ATTR_VLAN); | |
4627 | ||
4628 | SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE); | |
4629 | ||
ab83c8c5 | 4630 | SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) { |
8f79bb4d JR |
4631 | SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse); |
4632 | SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse); | |
4633 | SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse); | |
4634 | SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse); | |
4635 | } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS); | |
2d18eae8 JR |
4636 | |
4637 | SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) { | |
4638 | SCAN_FIELD("src=", ipv4, ipv4_src); | |
4639 | SCAN_FIELD("dst=", ipv4, ipv4_dst); | |
4640 | SCAN_FIELD("proto=", u8, ipv4_proto); | |
4641 | SCAN_FIELD("tos=", u8, ipv4_tos); | |
4642 | SCAN_FIELD("ttl=", u8, ipv4_ttl); | |
4643 | SCAN_FIELD("frag=", frag, ipv4_frag); | |
4644 | } SCAN_END(OVS_KEY_ATTR_IPV4); | |
4645 | ||
4646 | SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) { | |
932c96b7 JR |
4647 | SCAN_FIELD("src=", in6_addr, ipv6_src); |
4648 | SCAN_FIELD("dst=", in6_addr, ipv6_dst); | |
2d18eae8 JR |
4649 | SCAN_FIELD("label=", ipv6_label, ipv6_label); |
4650 | SCAN_FIELD("proto=", u8, ipv6_proto); | |
4651 | SCAN_FIELD("tclass=", u8, ipv6_tclass); | |
4652 | SCAN_FIELD("hlimit=", u8, ipv6_hlimit); | |
4653 | SCAN_FIELD("frag=", frag, ipv6_frag); | |
4654 | } SCAN_END(OVS_KEY_ATTR_IPV6); | |
4655 | ||
4656 | SCAN_BEGIN("tcp(", struct ovs_key_tcp) { | |
4657 | SCAN_FIELD("src=", be16, tcp_src); | |
4658 | SCAN_FIELD("dst=", be16, tcp_dst); | |
4659 | } SCAN_END(OVS_KEY_ATTR_TCP); | |
4660 | ||
4661 | SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS); | |
4662 | ||
4663 | SCAN_BEGIN("udp(", struct ovs_key_udp) { | |
4664 | SCAN_FIELD("src=", be16, udp_src); | |
4665 | SCAN_FIELD("dst=", be16, udp_dst); | |
4666 | } SCAN_END(OVS_KEY_ATTR_UDP); | |
4667 | ||
4668 | SCAN_BEGIN("sctp(", struct ovs_key_sctp) { | |
4669 | SCAN_FIELD("src=", be16, sctp_src); | |
4670 | SCAN_FIELD("dst=", be16, sctp_dst); | |
4671 | } SCAN_END(OVS_KEY_ATTR_SCTP); | |
4672 | ||
4673 | SCAN_BEGIN("icmp(", struct ovs_key_icmp) { | |
4674 | SCAN_FIELD("type=", u8, icmp_type); | |
4675 | SCAN_FIELD("code=", u8, icmp_code); | |
4676 | } SCAN_END(OVS_KEY_ATTR_ICMP); | |
4677 | ||
4678 | SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) { | |
4679 | SCAN_FIELD("type=", u8, icmpv6_type); | |
4680 | SCAN_FIELD("code=", u8, icmpv6_code); | |
4681 | } SCAN_END(OVS_KEY_ATTR_ICMPV6); | |
4682 | ||
4683 | SCAN_BEGIN("arp(", struct ovs_key_arp) { | |
4684 | SCAN_FIELD("sip=", ipv4, arp_sip); | |
4685 | SCAN_FIELD("tip=", ipv4, arp_tip); | |
4686 | SCAN_FIELD("op=", be16, arp_op); | |
4687 | SCAN_FIELD("sha=", eth, arp_sha); | |
4688 | SCAN_FIELD("tha=", eth, arp_tha); | |
4689 | } SCAN_END(OVS_KEY_ATTR_ARP); | |
4690 | ||
4691 | SCAN_BEGIN("nd(", struct ovs_key_nd) { | |
932c96b7 | 4692 | SCAN_FIELD("target=", in6_addr, nd_target); |
2d18eae8 JR |
4693 | SCAN_FIELD("sll=", eth, nd_sll); |
4694 | SCAN_FIELD("tll=", eth, nd_tll); | |
4695 | } SCAN_END(OVS_KEY_ATTR_ND); | |
4696 | ||
3d4b2e6e JS |
4697 | struct packet_type { |
4698 | ovs_be16 ns; | |
4699 | ovs_be16 id; | |
4700 | }; | |
4701 | SCAN_BEGIN("packet_type(", struct packet_type) { | |
4702 | SCAN_FIELD("ns=", be16, ns); | |
4703 | SCAN_FIELD("id=", be16, id); | |
4704 | } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE); | |
4705 | ||
3d2fbd70 JS |
4706 | SCAN_BEGIN("nsh(", struct ovs_key_nsh) { |
4707 | SCAN_FIELD("flags=", u8, flags); | |
4708 | SCAN_FIELD("mdtype=", u8, mdtype); | |
4709 | SCAN_FIELD("np=", u8, np); | |
4710 | SCAN_FIELD("path_hdr=", be32, path_hdr); | |
4711 | SCAN_FIELD("c1=", be32, c[0]); | |
4712 | SCAN_FIELD("c2=", be32, c[1]); | |
4713 | SCAN_FIELD("c3=", be32, c[2]); | |
4714 | SCAN_FIELD("c4=", be32, c[2]); | |
4715 | } SCAN_END(OVS_KEY_ATTR_NSH); | |
4716 | ||
2d18eae8 | 4717 | /* Encap open-coded. */ |
fea393b1 BP |
4718 | if (!strncmp(s, "encap(", 6)) { |
4719 | const char *start = s; | |
e6cc0bab | 4720 | size_t encap, encap_mask = 0; |
fea393b1 BP |
4721 | |
4722 | encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP); | |
e6cc0bab AZ |
4723 | if (mask) { |
4724 | encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP); | |
4725 | } | |
fea393b1 BP |
4726 | |
4727 | s += 6; | |
4728 | for (;;) { | |
4729 | int retval; | |
4730 | ||
70fbe375 | 4731 | s += strspn(s, delimiters); |
fea393b1 BP |
4732 | if (!*s) { |
4733 | return -EINVAL; | |
4734 | } else if (*s == ')') { | |
4735 | break; | |
4736 | } | |
4737 | ||
e6cc0bab | 4738 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
fea393b1 BP |
4739 | if (retval < 0) { |
4740 | return retval; | |
4741 | } | |
4742 | s += retval; | |
4743 | } | |
4744 | s++; | |
4745 | ||
4746 | nl_msg_end_nested(key, encap); | |
e6cc0bab AZ |
4747 | if (mask) { |
4748 | nl_msg_end_nested(mask, encap_mask); | |
4749 | } | |
fea393b1 BP |
4750 | |
4751 | return s - start; | |
4752 | } | |
4753 | ||
3bffc610 BP |
4754 | return -EINVAL; |
4755 | } | |
4756 | ||
df2c07f4 JP |
4757 | /* Parses the string representation of a datapath flow key, in the |
4758 | * format output by odp_flow_key_format(). Returns 0 if successful, | |
4759 | * otherwise a positive errno value. On success, the flow key is | |
4760 | * appended to 'key' as a series of Netlink attributes. On failure, no | |
4761 | * data is appended to 'key'. Either way, 'key''s data might be | |
4762 | * reallocated. | |
3bffc610 | 4763 | * |
44bac24b BP |
4764 | * If 'port_names' is nonnull, it points to an simap that maps from a port name |
4765 | * to a port number. (Port names may be used instead of port numbers in | |
4766 | * in_port.) | |
b2a60db8 | 4767 | * |
3bffc610 BP |
4768 | * On success, the attributes appended to 'key' are individually syntactically |
4769 | * valid, but they may not be valid as a sequence. 'key' might, for example, | |
34118cae | 4770 | * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */ |
3bffc610 | 4771 | int |
e6cc0bab AZ |
4772 | odp_flow_from_string(const char *s, const struct simap *port_names, |
4773 | struct ofpbuf *key, struct ofpbuf *mask) | |
3bffc610 | 4774 | { |
6fd6ed71 | 4775 | const size_t old_size = key->size; |
3bffc610 BP |
4776 | for (;;) { |
4777 | int retval; | |
4778 | ||
7202cbe5 | 4779 | s += strspn(s, delimiters); |
3bffc610 BP |
4780 | if (!*s) { |
4781 | return 0; | |
4782 | } | |
4783 | ||
e6cc0bab | 4784 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
3bffc610 | 4785 | if (retval < 0) { |
6fd6ed71 | 4786 | key->size = old_size; |
3bffc610 BP |
4787 | return -retval; |
4788 | } | |
4789 | s += retval; | |
4790 | } | |
4791 | ||
4792 | return 0; | |
4793 | } | |
4794 | ||
7257b535 | 4795 | static uint8_t |
3cea18ec | 4796 | ovs_to_odp_frag(uint8_t nw_frag, bool is_mask) |
7257b535 | 4797 | { |
3cea18ec JR |
4798 | if (is_mask) { |
4799 | /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type, | |
4800 | * not a set of flags or bitfields. Hence, if the struct flow nw_frag | |
4801 | * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we | |
4802 | * must use a zero mask for the netlink frag field, and all ones mask | |
4803 | * otherwise. */ | |
4804 | return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0; | |
4805 | } | |
2c0f0be1 JR |
4806 | return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE |
4807 | : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER | |
4808 | : OVS_FRAG_TYPE_FIRST; | |
7257b535 BP |
4809 | } |
4810 | ||
3cea18ec JR |
4811 | static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *); |
4812 | static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *); | |
4813 | static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *, | |
4814 | bool is_mask); | |
4815 | static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *, | |
4816 | bool is_mask); | |
4817 | static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *, | |
4818 | bool is_mask); | |
4819 | static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *, | |
4820 | bool is_mask); | |
4821 | static void get_arp_key(const struct flow *, struct ovs_key_arp *); | |
4822 | static void put_arp_key(const struct ovs_key_arp *, struct flow *); | |
e60e935b SRCSA |
4823 | static void get_nd_key(const struct flow *, struct ovs_key_nd *); |
4824 | static void put_nd_key(const struct ovs_key_nd *, struct flow *); | |
3d2fbd70 JS |
4825 | static void get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, |
4826 | bool is_mask); | |
4827 | static void put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow, | |
4828 | bool is_mask); | |
3cea18ec JR |
4829 | |
4830 | /* These share the same layout. */ | |
4831 | union ovs_key_tp { | |
4832 | struct ovs_key_tcp tcp; | |
4833 | struct ovs_key_udp udp; | |
4834 | struct ovs_key_sctp sctp; | |
4835 | }; | |
4836 | ||
4837 | static void get_tp_key(const struct flow *, union ovs_key_tp *); | |
4838 | static void put_tp_key(const union ovs_key_tp *, struct flow *); | |
431495b1 | 4839 | |
661cbcd5 | 4840 | static void |
5262eea1 JG |
4841 | odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms, |
4842 | bool export_mask, struct ofpbuf *buf) | |
14608a15 | 4843 | { |
df2c07f4 | 4844 | struct ovs_key_ethernet *eth_key; |
f0fb825a EG |
4845 | size_t encap[FLOW_MAX_VLAN_HEADERS] = {0}; |
4846 | size_t max_vlans; | |
5262eea1 | 4847 | const struct flow *flow = parms->flow; |
beb75a40 JS |
4848 | const struct flow *mask = parms->mask; |
4849 | const struct flow *data = export_mask ? mask : flow; | |
661cbcd5 | 4850 | |
54bb0348 | 4851 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority); |
abff858b | 4852 | |
ffe4c74f | 4853 | if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) { |
ec1f6f32 JG |
4854 | tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel, |
4855 | parms->key_buf); | |
36956a7d BP |
4856 | } |
4857 | ||
1362e248 | 4858 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark); |
72e8bf28 | 4859 | |
07659514 JS |
4860 | if (parms->support.ct_state) { |
4861 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE, | |
4862 | ovs_to_odp_ct_state(data->ct_state)); | |
4863 | } | |
4864 | if (parms->support.ct_zone) { | |
4865 | nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone); | |
4866 | } | |
8e53fe8c JS |
4867 | if (parms->support.ct_mark) { |
4868 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark); | |
4869 | } | |
9daf2348 JS |
4870 | if (parms->support.ct_label) { |
4871 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label, | |
4872 | sizeof(data->ct_label)); | |
4873 | } | |
7b5bbe5d JS |
4874 | if (flow->ct_nw_proto) { |
4875 | if (parms->support.ct_orig_tuple | |
4876 | && flow->dl_type == htons(ETH_TYPE_IP)) { | |
daf4d3c1 JR |
4877 | struct ovs_key_ct_tuple_ipv4 ct = { |
4878 | data->ct_nw_src, | |
4879 | data->ct_nw_dst, | |
4880 | data->ct_tp_src, | |
4881 | data->ct_tp_dst, | |
4882 | data->ct_nw_proto, | |
4883 | }; | |
4884 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, &ct, | |
4885 | sizeof ct); | |
7b5bbe5d JS |
4886 | } else if (parms->support.ct_orig_tuple6 |
4887 | && flow->dl_type == htons(ETH_TYPE_IPV6)) { | |
daf4d3c1 JR |
4888 | struct ovs_key_ct_tuple_ipv6 ct = { |
4889 | data->ct_ipv6_src, | |
4890 | data->ct_ipv6_dst, | |
4891 | data->ct_tp_src, | |
4892 | data->ct_tp_dst, | |
4893 | data->ct_nw_proto, | |
4894 | }; | |
4895 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, &ct, | |
4896 | sizeof ct); | |
4897 | } | |
4898 | } | |
2494ccd7 | 4899 | if (parms->support.recirc) { |
572f732a | 4900 | nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id); |
572f732a AZ |
4901 | nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash); |
4902 | } | |
4903 | ||
098d2a97 | 4904 | /* Add an ingress port attribute if this is a mask or 'in_port.odp_port' |
661cbcd5 | 4905 | * is not the magical value "ODPP_NONE". */ |
098d2a97 JG |
4906 | if (export_mask || flow->in_port.odp_port != ODPP_NONE) { |
4907 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port); | |
18886b60 | 4908 | } |
36956a7d | 4909 | |
3d4b2e6e | 4910 | nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type); |
36956a7d | 4911 | |
f0fb825a EG |
4912 | if (OVS_UNLIKELY(parms->probe)) { |
4913 | max_vlans = FLOW_MAX_VLAN_HEADERS; | |
4914 | } else { | |
4915 | max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit); | |
4916 | } | |
f0fb825a | 4917 | |
beb75a40 JS |
4918 | /* Conditionally add L2 attributes for Ethernet packets */ |
4919 | if (flow->packet_type == htonl(PT_ETH)) { | |
4920 | eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET, | |
4921 | sizeof *eth_key); | |
4922 | get_ethernet_key(data, eth_key); | |
4923 | ||
4924 | for (int encaps = 0; encaps < max_vlans; encaps++) { | |
4925 | ovs_be16 tpid = flow->vlans[encaps].tpid; | |
4926 | ||
4927 | if (flow->vlans[encaps].tci == htons(0)) { | |
4928 | if (eth_type_vlan(flow->dl_type)) { | |
4929 | /* If VLAN was truncated the tpid is in dl_type */ | |
4930 | tpid = flow->dl_type; | |
4931 | } else { | |
4932 | break; | |
4933 | } | |
f0fb825a | 4934 | } |
f0fb825a | 4935 | |
beb75a40 JS |
4936 | if (export_mask) { |
4937 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); | |
4938 | } else { | |
4939 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid); | |
4940 | } | |
4941 | nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci); | |
4942 | encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP); | |
4943 | if (flow->vlans[encaps].tci == htons(0)) { | |
4944 | goto unencap; | |
4945 | } | |
8ddc056d | 4946 | } |
36956a7d BP |
4947 | } |
4948 | ||
4949 | if (ntohs(flow->dl_type) < ETH_TYPE_MIN) { | |
661cbcd5 JP |
4950 | /* For backwards compatibility with kernels that don't support |
4951 | * wildcarding, the following convention is used to encode the | |
4952 | * OVS_KEY_ATTR_ETHERTYPE for key and mask: | |
4953 | * | |
4954 | * key mask matches | |
4955 | * -------- -------- ------- | |
4956 | * >0x5ff 0xffff Specified Ethernet II Ethertype. | |
4957 | * >0x5ff 0 Any Ethernet II or non-Ethernet II frame. | |
4958 | * <none> 0xffff Any non-Ethernet II frame (except valid | |
4959 | * 802.3 SNAP packet with valid eth_type). | |
4960 | */ | |
fbfe01de | 4961 | if (export_mask) { |
b8266395 | 4962 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
661cbcd5 | 4963 | } |
fea393b1 | 4964 | goto unencap; |
36956a7d BP |
4965 | } |
4966 | ||
661cbcd5 | 4967 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type); |
36956a7d | 4968 | |
f0fb825a EG |
4969 | if (eth_type_vlan(flow->dl_type)) { |
4970 | goto unencap; | |
4971 | } | |
4972 | ||
36956a7d | 4973 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
df2c07f4 | 4974 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 4975 | |
df2c07f4 | 4976 | ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4, |
36956a7d | 4977 | sizeof *ipv4_key); |
3cea18ec | 4978 | get_ipv4_key(data, ipv4_key, export_mask); |
d31f1109 | 4979 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
df2c07f4 | 4980 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 4981 | |
df2c07f4 | 4982 | ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6, |
d31f1109 | 4983 | sizeof *ipv6_key); |
3cea18ec | 4984 | get_ipv6_key(data, ipv6_key, export_mask); |
8087f5ff MM |
4985 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
4986 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
df2c07f4 | 4987 | struct ovs_key_arp *arp_key; |
d31f1109 | 4988 | |
3cea18ec JR |
4989 | arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP, |
4990 | sizeof *arp_key); | |
4991 | get_arp_key(data, arp_key); | |
b0a17866 | 4992 | } else if (eth_type_mpls(flow->dl_type)) { |
b02475c5 | 4993 | struct ovs_key_mpls *mpls_key; |
8bfd0fda | 4994 | int i, n; |
b02475c5 | 4995 | |
8bfd0fda | 4996 | n = flow_count_mpls_labels(flow, NULL); |
5262eea1 | 4997 | if (export_mask) { |
2494ccd7 | 4998 | n = MIN(n, parms->support.max_mpls_depth); |
5262eea1 | 4999 | } |
b02475c5 | 5000 | mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS, |
8bfd0fda BP |
5001 | n * sizeof *mpls_key); |
5002 | for (i = 0; i < n; i++) { | |
5003 | mpls_key[i].mpls_lse = data->mpls_lse[i]; | |
5004 | } | |
3d2fbd70 JS |
5005 | } else if (flow->dl_type == htons(ETH_TYPE_NSH)) { |
5006 | struct ovs_key_nsh *nsh_key; | |
5007 | ||
5008 | nsh_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_NSH, | |
5009 | sizeof *nsh_key); | |
5010 | get_nsh_key(data, nsh_key, export_mask); | |
b02475c5 SH |
5011 | } |
5012 | ||
48cecbdc | 5013 | if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
6767a2cc | 5014 | if (flow->nw_proto == IPPROTO_TCP) { |
3cea18ec | 5015 | union ovs_key_tp *tcp_key; |
36956a7d | 5016 | |
df2c07f4 | 5017 | tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP, |
36956a7d | 5018 | sizeof *tcp_key); |
3cea18ec | 5019 | get_tp_key(data, tcp_key); |
7cb32412 | 5020 | if (data->tcp_flags || (mask && mask->tcp_flags)) { |
dc235f7f JR |
5021 | nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags); |
5022 | } | |
6767a2cc | 5023 | } else if (flow->nw_proto == IPPROTO_UDP) { |
3cea18ec | 5024 | union ovs_key_tp *udp_key; |
36956a7d | 5025 | |
df2c07f4 | 5026 | udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP, |
36956a7d | 5027 | sizeof *udp_key); |
3cea18ec | 5028 | get_tp_key(data, udp_key); |
c6bcb685 | 5029 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
3cea18ec | 5030 | union ovs_key_tp *sctp_key; |
c6bcb685 JS |
5031 | |
5032 | sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP, | |
5033 | sizeof *sctp_key); | |
3cea18ec | 5034 | get_tp_key(data, sctp_key); |
d31f1109 JP |
5035 | } else if (flow->dl_type == htons(ETH_TYPE_IP) |
5036 | && flow->nw_proto == IPPROTO_ICMP) { | |
df2c07f4 | 5037 | struct ovs_key_icmp *icmp_key; |
36956a7d | 5038 | |
df2c07f4 | 5039 | icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP, |
36956a7d | 5040 | sizeof *icmp_key); |
661cbcd5 JP |
5041 | icmp_key->icmp_type = ntohs(data->tp_src); |
5042 | icmp_key->icmp_code = ntohs(data->tp_dst); | |
d31f1109 JP |
5043 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6) |
5044 | && flow->nw_proto == IPPROTO_ICMPV6) { | |
df2c07f4 | 5045 | struct ovs_key_icmpv6 *icmpv6_key; |
d31f1109 | 5046 | |
df2c07f4 | 5047 | icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6, |
d31f1109 | 5048 | sizeof *icmpv6_key); |
661cbcd5 JP |
5049 | icmpv6_key->icmpv6_type = ntohs(data->tp_src); |
5050 | icmpv6_key->icmpv6_code = ntohs(data->tp_dst); | |
685a51a5 | 5051 | |
c17fcc0a | 5052 | if (is_nd(flow, NULL) |
a63e3dc9 DDP |
5053 | /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP |
5054 | * type and code are 8 bits wide. Therefore, an exact match | |
5055 | * looks like htons(0xff), not htons(0xffff). See | |
5056 | * xlate_wc_finish() for details. */ | |
5057 | && (!export_mask || (data->tp_src == htons(0xff) | |
5058 | && data->tp_dst == htons(0xff)))) { | |
6f8dbd27 | 5059 | |
df2c07f4 | 5060 | struct ovs_key_nd *nd_key; |
685a51a5 | 5061 | |
df2c07f4 | 5062 | nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND, |
685a51a5 | 5063 | sizeof *nd_key); |
932c96b7 | 5064 | nd_key->nd_target = data->nd_target; |
74ff3298 JR |
5065 | nd_key->nd_sll = data->arp_sha; |
5066 | nd_key->nd_tll = data->arp_tha; | |
685a51a5 | 5067 | } |
36956a7d | 5068 | } |
36956a7d | 5069 | } |
fea393b1 BP |
5070 | |
5071 | unencap: | |
f0fb825a EG |
5072 | for (int encaps = max_vlans - 1; encaps >= 0; encaps--) { |
5073 | if (encap[encaps]) { | |
5074 | nl_msg_end_nested(buf, encap[encaps]); | |
5075 | } | |
fea393b1 | 5076 | } |
36956a7d | 5077 | } |
661cbcd5 JP |
5078 | |
5079 | /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. | |
661cbcd5 JP |
5080 | * |
5081 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
5262eea1 | 5082 | * capable of being expanded to allow for that much space. */ |
661cbcd5 | 5083 | void |
5262eea1 JG |
5084 | odp_flow_key_from_flow(const struct odp_flow_key_parms *parms, |
5085 | struct ofpbuf *buf) | |
661cbcd5 | 5086 | { |
5262eea1 | 5087 | odp_flow_key_from_flow__(parms, false, buf); |
661cbcd5 JP |
5088 | } |
5089 | ||
5090 | /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to | |
5262eea1 | 5091 | * 'buf'. |
661cbcd5 JP |
5092 | * |
5093 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
5262eea1 | 5094 | * capable of being expanded to allow for that much space. */ |
661cbcd5 | 5095 | void |
5262eea1 JG |
5096 | odp_flow_key_from_mask(const struct odp_flow_key_parms *parms, |
5097 | struct ofpbuf *buf) | |
661cbcd5 | 5098 | { |
5262eea1 | 5099 | odp_flow_key_from_flow__(parms, true, buf); |
661cbcd5 | 5100 | } |
36956a7d | 5101 | |
758c456d JR |
5102 | /* Generate ODP flow key from the given packet metadata */ |
5103 | void | |
beb75a40 | 5104 | odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet) |
758c456d | 5105 | { |
beb75a40 JS |
5106 | const struct pkt_metadata *md = &packet->md; |
5107 | ||
758c456d JR |
5108 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority); |
5109 | ||
ffe4c74f | 5110 | if (flow_tnl_dst_is_set(&md->tunnel)) { |
ec1f6f32 | 5111 | tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL); |
758c456d JR |
5112 | } |
5113 | ||
5114 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark); | |
5115 | ||
07659514 JS |
5116 | if (md->ct_state) { |
5117 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE, | |
5118 | ovs_to_odp_ct_state(md->ct_state)); | |
5119 | if (md->ct_zone) { | |
5120 | nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone); | |
5121 | } | |
8e53fe8c JS |
5122 | if (md->ct_mark) { |
5123 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark); | |
5124 | } | |
2ff8484b | 5125 | if (!ovs_u128_is_zero(md->ct_label)) { |
9daf2348 JS |
5126 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label, |
5127 | sizeof(md->ct_label)); | |
5128 | } | |
daf4d3c1 JR |
5129 | if (md->ct_orig_tuple_ipv6) { |
5130 | if (md->ct_orig_tuple.ipv6.ipv6_proto) { | |
5131 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, | |
5132 | &md->ct_orig_tuple.ipv6, | |
5133 | sizeof md->ct_orig_tuple.ipv6); | |
5134 | } | |
5135 | } else { | |
5136 | if (md->ct_orig_tuple.ipv4.ipv4_proto) { | |
5137 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, | |
5138 | &md->ct_orig_tuple.ipv4, | |
5139 | sizeof md->ct_orig_tuple.ipv4); | |
5140 | } | |
5141 | } | |
07659514 JS |
5142 | } |
5143 | ||
758c456d JR |
5144 | /* Add an ingress port attribute if 'odp_in_port' is not the magical |
5145 | * value "ODPP_NONE". */ | |
b5e7e61a AZ |
5146 | if (md->in_port.odp_port != ODPP_NONE) { |
5147 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port); | |
758c456d | 5148 | } |
beb75a40 JS |
5149 | |
5150 | /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */ | |
5151 | if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) { | |
5152 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, | |
5153 | pt_ns_type_be(packet->packet_type)); | |
5154 | } | |
758c456d JR |
5155 | } |
5156 | ||
5157 | /* Generate packet metadata from the given ODP flow key. */ | |
5158 | void | |
beb75a40 JS |
5159 | odp_key_to_dp_packet(const struct nlattr *key, size_t key_len, |
5160 | struct dp_packet *packet) | |
758c456d | 5161 | { |
beb75a40 | 5162 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
758c456d | 5163 | const struct nlattr *nla; |
beb75a40 JS |
5164 | struct pkt_metadata *md = &packet->md; |
5165 | ovs_be32 packet_type = htonl(PT_UNKNOWN); | |
5166 | ovs_be16 ethertype = 0; | |
758c456d | 5167 | size_t left; |
758c456d | 5168 | |
35303d71 | 5169 | pkt_metadata_init(md, ODPP_NONE); |
758c456d JR |
5170 | |
5171 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { | |
08ef583d | 5172 | enum ovs_key_attr type = nl_attr_type(nla); |
758c456d | 5173 | size_t len = nl_attr_get_size(nla); |
6b8da9e9 JG |
5174 | int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens, |
5175 | OVS_KEY_ATTR_MAX, type); | |
758c456d JR |
5176 | |
5177 | if (len != expected_len && expected_len >= 0) { | |
5178 | continue; | |
5179 | } | |
5180 | ||
572f732a AZ |
5181 | switch (type) { |
5182 | case OVS_KEY_ATTR_RECIRC_ID: | |
5183 | md->recirc_id = nl_attr_get_u32(nla); | |
572f732a AZ |
5184 | break; |
5185 | case OVS_KEY_ATTR_DP_HASH: | |
5186 | md->dp_hash = nl_attr_get_u32(nla); | |
572f732a AZ |
5187 | break; |
5188 | case OVS_KEY_ATTR_PRIORITY: | |
758c456d | 5189 | md->skb_priority = nl_attr_get_u32(nla); |
572f732a AZ |
5190 | break; |
5191 | case OVS_KEY_ATTR_SKB_MARK: | |
758c456d | 5192 | md->pkt_mark = nl_attr_get_u32(nla); |
572f732a | 5193 | break; |
07659514 JS |
5194 | case OVS_KEY_ATTR_CT_STATE: |
5195 | md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla)); | |
07659514 JS |
5196 | break; |
5197 | case OVS_KEY_ATTR_CT_ZONE: | |
5198 | md->ct_zone = nl_attr_get_u16(nla); | |
07659514 | 5199 | break; |
8e53fe8c JS |
5200 | case OVS_KEY_ATTR_CT_MARK: |
5201 | md->ct_mark = nl_attr_get_u32(nla); | |
8e53fe8c | 5202 | break; |
9daf2348 | 5203 | case OVS_KEY_ATTR_CT_LABELS: { |
ab79d262 | 5204 | md->ct_label = nl_attr_get_u128(nla); |
9daf2348 JS |
5205 | break; |
5206 | } | |
daf4d3c1 JR |
5207 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: { |
5208 | const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla); | |
5209 | md->ct_orig_tuple.ipv4 = *ct; | |
5210 | md->ct_orig_tuple_ipv6 = false; | |
daf4d3c1 JR |
5211 | break; |
5212 | } | |
5213 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: { | |
5214 | const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla); | |
5215 | ||
5216 | md->ct_orig_tuple.ipv6 = *ct; | |
5217 | md->ct_orig_tuple_ipv6 = true; | |
daf4d3c1 JR |
5218 | break; |
5219 | } | |
572f732a | 5220 | case OVS_KEY_ATTR_TUNNEL: { |
758c456d JR |
5221 | enum odp_key_fitness res; |
5222 | ||
8d8ab6c2 | 5223 | res = odp_tun_key_from_attr(nla, &md->tunnel); |
758c456d JR |
5224 | if (res == ODP_FIT_ERROR) { |
5225 | memset(&md->tunnel, 0, sizeof md->tunnel); | |
758c456d | 5226 | } |
572f732a AZ |
5227 | break; |
5228 | } | |
5229 | case OVS_KEY_ATTR_IN_PORT: | |
b5e7e61a | 5230 | md->in_port.odp_port = nl_attr_get_odp_port(nla); |
572f732a | 5231 | break; |
beb75a40 JS |
5232 | case OVS_KEY_ATTR_ETHERNET: |
5233 | /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */ | |
5234 | packet_type = htonl(PT_ETH); | |
beb75a40 JS |
5235 | break; |
5236 | case OVS_KEY_ATTR_ETHERTYPE: | |
5237 | ethertype = nl_attr_get_be16(nla); | |
beb75a40 | 5238 | break; |
08ef583d YHW |
5239 | case OVS_KEY_ATTR_UNSPEC: |
5240 | case OVS_KEY_ATTR_ENCAP: | |
5241 | case OVS_KEY_ATTR_VLAN: | |
5242 | case OVS_KEY_ATTR_IPV4: | |
5243 | case OVS_KEY_ATTR_IPV6: | |
5244 | case OVS_KEY_ATTR_TCP: | |
5245 | case OVS_KEY_ATTR_UDP: | |
5246 | case OVS_KEY_ATTR_ICMP: | |
5247 | case OVS_KEY_ATTR_ICMPV6: | |
5248 | case OVS_KEY_ATTR_ARP: | |
5249 | case OVS_KEY_ATTR_ND: | |
5250 | case OVS_KEY_ATTR_SCTP: | |
5251 | case OVS_KEY_ATTR_TCP_FLAGS: | |
5252 | case OVS_KEY_ATTR_MPLS: | |
5253 | case OVS_KEY_ATTR_PACKET_TYPE: | |
3d2fbd70 | 5254 | case OVS_KEY_ATTR_NSH: |
08ef583d | 5255 | case __OVS_KEY_ATTR_MAX: |
572f732a AZ |
5256 | default: |
5257 | break; | |
758c456d | 5258 | } |
758c456d | 5259 | } |
beb75a40 JS |
5260 | |
5261 | if (packet_type == htonl(PT_ETH)) { | |
5262 | packet->packet_type = htonl(PT_ETH); | |
5263 | } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) { | |
5264 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, | |
5265 | ntohs(ethertype)); | |
5266 | } else { | |
5267 | VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type."); | |
5268 | } | |
758c456d JR |
5269 | } |
5270 | ||
b0f7b9b5 BP |
5271 | uint32_t |
5272 | odp_flow_key_hash(const struct nlattr *key, size_t key_len) | |
5273 | { | |
5274 | BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t))); | |
0a96a21b | 5275 | return hash_bytes32(ALIGNED_CAST(const uint32_t *, key), key_len, 0); |
b0f7b9b5 BP |
5276 | } |
5277 | ||
34118cae BP |
5278 | static void |
5279 | log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title, | |
b0f7b9b5 | 5280 | uint64_t attrs, int out_of_range_attr, |
34118cae BP |
5281 | const struct nlattr *key, size_t key_len) |
5282 | { | |
5283 | struct ds s; | |
5284 | int i; | |
5285 | ||
b0f7b9b5 | 5286 | if (VLOG_DROP_DBG(rl)) { |
34118cae BP |
5287 | return; |
5288 | } | |
5289 | ||
5290 | ds_init(&s); | |
b0f7b9b5 BP |
5291 | for (i = 0; i < 64; i++) { |
5292 | if (attrs & (UINT64_C(1) << i)) { | |
e6603631 BP |
5293 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
5294 | ||
5295 | ds_put_format(&s, " %s", | |
5296 | ovs_key_attr_to_string(i, namebuf, sizeof namebuf)); | |
34118cae BP |
5297 | } |
5298 | } | |
b0f7b9b5 BP |
5299 | if (out_of_range_attr) { |
5300 | ds_put_format(&s, " %d (and possibly others)", out_of_range_attr); | |
5301 | } | |
34118cae BP |
5302 | |
5303 | ds_put_cstr(&s, ": "); | |
5304 | odp_flow_key_format(key, key_len, &s); | |
5305 | ||
b0f7b9b5 | 5306 | VLOG_DBG("%s:%s", title, ds_cstr(&s)); |
34118cae BP |
5307 | ds_destroy(&s); |
5308 | } | |
5309 | ||
3cea18ec JR |
5310 | static uint8_t |
5311 | odp_to_ovs_frag(uint8_t odp_frag, bool is_mask) | |
7257b535 | 5312 | { |
34118cae BP |
5313 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
5314 | ||
3cea18ec JR |
5315 | if (is_mask) { |
5316 | return odp_frag ? FLOW_NW_FRAG_MASK : 0; | |
5317 | } | |
5318 | ||
9e44d715 | 5319 | if (odp_frag > OVS_FRAG_TYPE_LATER) { |
b0f7b9b5 | 5320 | VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag); |
3cea18ec | 5321 | return 0xff; /* Error. */ |
7257b535 BP |
5322 | } |
5323 | ||
3cea18ec JR |
5324 | return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0 |
5325 | : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY | |
5326 | : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER; | |
7257b535 BP |
5327 | } |
5328 | ||
b0f7b9b5 | 5329 | static bool |
fea393b1 | 5330 | parse_flow_nlattrs(const struct nlattr *key, size_t key_len, |
b0f7b9b5 BP |
5331 | const struct nlattr *attrs[], uint64_t *present_attrsp, |
5332 | int *out_of_range_attrp) | |
36956a7d | 5333 | { |
b0f7b9b5 | 5334 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
36956a7d | 5335 | const struct nlattr *nla; |
34118cae | 5336 | uint64_t present_attrs; |
36956a7d BP |
5337 | size_t left; |
5338 | ||
2aef1214 | 5339 | BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs); |
34118cae | 5340 | present_attrs = 0; |
b0f7b9b5 | 5341 | *out_of_range_attrp = 0; |
36956a7d | 5342 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { |
34118cae BP |
5343 | uint16_t type = nl_attr_type(nla); |
5344 | size_t len = nl_attr_get_size(nla); | |
6b8da9e9 JG |
5345 | int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens, |
5346 | OVS_KEY_ATTR_MAX, type); | |
34118cae | 5347 | |
b0f7b9b5 | 5348 | if (len != expected_len && expected_len >= 0) { |
e6603631 BP |
5349 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
5350 | ||
34582733 | 5351 | VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have " |
e6603631 BP |
5352 | "length %d", ovs_key_attr_to_string(type, namebuf, |
5353 | sizeof namebuf), | |
b0f7b9b5 BP |
5354 | len, expected_len); |
5355 | return false; | |
34118cae BP |
5356 | } |
5357 | ||
2aef1214 | 5358 | if (type > OVS_KEY_ATTR_MAX) { |
b0f7b9b5 BP |
5359 | *out_of_range_attrp = type; |
5360 | } else { | |
5361 | if (present_attrs & (UINT64_C(1) << type)) { | |
e6603631 BP |
5362 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
5363 | ||
b0f7b9b5 | 5364 | VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key", |
e6603631 BP |
5365 | ovs_key_attr_to_string(type, |
5366 | namebuf, sizeof namebuf)); | |
b0f7b9b5 BP |
5367 | return false; |
5368 | } | |
5369 | ||
5370 | present_attrs |= UINT64_C(1) << type; | |
5371 | attrs[type] = nla; | |
5372 | } | |
34118cae BP |
5373 | } |
5374 | if (left) { | |
5375 | VLOG_ERR_RL(&rl, "trailing garbage in flow key"); | |
b0f7b9b5 | 5376 | return false; |
34118cae BP |
5377 | } |
5378 | ||
fea393b1 | 5379 | *present_attrsp = present_attrs; |
b0f7b9b5 | 5380 | return true; |
fea393b1 BP |
5381 | } |
5382 | ||
b0f7b9b5 BP |
5383 | static enum odp_key_fitness |
5384 | check_expectations(uint64_t present_attrs, int out_of_range_attr, | |
5385 | uint64_t expected_attrs, | |
fea393b1 BP |
5386 | const struct nlattr *key, size_t key_len) |
5387 | { | |
5388 | uint64_t missing_attrs; | |
5389 | uint64_t extra_attrs; | |
5390 | ||
5391 | missing_attrs = expected_attrs & ~present_attrs; | |
5392 | if (missing_attrs) { | |
b0f7b9b5 BP |
5393 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
5394 | log_odp_key_attributes(&rl, "expected but not present", | |
5395 | missing_attrs, 0, key, key_len); | |
5396 | return ODP_FIT_TOO_LITTLE; | |
fea393b1 BP |
5397 | } |
5398 | ||
5399 | extra_attrs = present_attrs & ~expected_attrs; | |
b0f7b9b5 BP |
5400 | if (extra_attrs || out_of_range_attr) { |
5401 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
5402 | log_odp_key_attributes(&rl, "present but not expected", | |
5403 | extra_attrs, out_of_range_attr, key, key_len); | |
5404 | return ODP_FIT_TOO_MUCH; | |
fea393b1 BP |
5405 | } |
5406 | ||
b0f7b9b5 | 5407 | return ODP_FIT_PERFECT; |
fea393b1 BP |
5408 | } |
5409 | ||
b0f7b9b5 BP |
5410 | static bool |
5411 | parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
5412 | uint64_t present_attrs, uint64_t *expected_attrs, | |
4a221615 | 5413 | struct flow *flow, const struct flow *src_flow) |
fea393b1 BP |
5414 | { |
5415 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 5416 | bool is_mask = flow != src_flow; |
36956a7d | 5417 | |
fea393b1 | 5418 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
34118cae | 5419 | flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); |
4a221615 | 5420 | if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) { |
34118cae BP |
5421 | VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key", |
5422 | ntohs(flow->dl_type)); | |
b0f7b9b5 | 5423 | return false; |
34118cae | 5424 | } |
4a221615 GY |
5425 | if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN && |
5426 | flow->dl_type != htons(0xffff)) { | |
5427 | return false; | |
5428 | } | |
b0f7b9b5 | 5429 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; |
34118cae | 5430 | } else { |
4a221615 | 5431 | if (!is_mask) { |
beb75a40 JS |
5432 | /* Default ethertype for well-known L3 packets. */ |
5433 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { | |
5434 | flow->dl_type = htons(ETH_TYPE_IP); | |
5435 | } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { | |
5436 | flow->dl_type = htons(ETH_TYPE_IPV6); | |
5437 | } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { | |
5438 | flow->dl_type = htons(ETH_TYPE_MPLS); | |
5439 | } else { | |
5440 | flow->dl_type = htons(FLOW_DL_TYPE_NONE); | |
5441 | } | |
5442 | } else if (src_flow->packet_type != htonl(PT_ETH)) { | |
5443 | /* dl_type is mandatory for non-Ethernet packets */ | |
5444 | flow->dl_type = htons(0xffff); | |
4a221615 GY |
5445 | } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) { |
5446 | /* See comments in odp_flow_key_from_flow__(). */ | |
5447 | VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame"); | |
5448 | return false; | |
5449 | } | |
34118cae | 5450 | } |
b0f7b9b5 BP |
5451 | return true; |
5452 | } | |
5453 | ||
5454 | static enum odp_key_fitness | |
b02475c5 SH |
5455 | parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], |
5456 | uint64_t present_attrs, int out_of_range_attr, | |
5457 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 | 5458 | const struct nlattr *key, size_t key_len, |
91a77332 | 5459 | const struct flow *src_flow) |
b0f7b9b5 BP |
5460 | { |
5461 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 GY |
5462 | bool is_mask = src_flow != flow; |
5463 | const void *check_start = NULL; | |
5464 | size_t check_len = 0; | |
5465 | enum ovs_key_attr expected_bit = 0xff; | |
5466 | ||
5467 | if (eth_type_mpls(src_flow->dl_type)) { | |
7e6621e9 | 5468 | if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { |
91a77332 | 5469 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); |
7e6621e9 JS |
5470 | } |
5471 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { | |
5472 | size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]); | |
5473 | const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]); | |
5474 | int n = size / sizeof(ovs_be32); | |
5475 | int i; | |
4a221615 | 5476 | |
7e6621e9 JS |
5477 | if (!size || size % sizeof(ovs_be32)) { |
5478 | return ODP_FIT_ERROR; | |
91a77332 | 5479 | } |
8bfd0fda | 5480 | if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) { |
4a221615 GY |
5481 | return ODP_FIT_ERROR; |
5482 | } | |
8bfd0fda | 5483 | |
7e6621e9 JS |
5484 | for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) { |
5485 | flow->mpls_lse[i] = mpls_lse[i]; | |
5486 | } | |
5487 | if (n > FLOW_MAX_MPLS_LABELS) { | |
5488 | return ODP_FIT_TOO_MUCH; | |
5489 | } | |
8bfd0fda | 5490 | |
7e6621e9 JS |
5491 | if (!is_mask) { |
5492 | /* BOS may be set only in the innermost label. */ | |
5493 | for (i = 0; i < n - 1; i++) { | |
5494 | if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) { | |
5495 | return ODP_FIT_ERROR; | |
5496 | } | |
8bfd0fda | 5497 | } |
8bfd0fda | 5498 | |
7e6621e9 JS |
5499 | /* BOS must be set in the innermost label. */ |
5500 | if (n < FLOW_MAX_MPLS_LABELS | |
5501 | && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) { | |
5502 | return ODP_FIT_TOO_LITTLE; | |
5503 | } | |
8bfd0fda BP |
5504 | } |
5505 | } | |
5506 | ||
4a221615 GY |
5507 | goto done; |
5508 | } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) { | |
5509 | if (!is_mask) { | |
5510 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4; | |
b02475c5 | 5511 | } |
fea393b1 | 5512 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { |
34118cae | 5513 | const struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 5514 | |
34118cae | 5515 | ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]); |
3cea18ec JR |
5516 | put_ipv4_key(ipv4_key, flow, is_mask); |
5517 | if (flow->nw_frag > FLOW_NW_FRAG_MASK) { | |
5518 | return ODP_FIT_ERROR; | |
5519 | } | |
4a221615 | 5520 | if (is_mask) { |
4a221615 GY |
5521 | check_start = ipv4_key; |
5522 | check_len = sizeof *ipv4_key; | |
5523 | expected_bit = OVS_KEY_ATTR_IPV4; | |
36956a7d | 5524 | } |
34118cae | 5525 | } |
4a221615 GY |
5526 | } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) { |
5527 | if (!is_mask) { | |
5528 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6; | |
5529 | } | |
fea393b1 | 5530 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { |
34118cae | 5531 | const struct ovs_key_ipv6 *ipv6_key; |
36956a7d | 5532 | |
34118cae | 5533 | ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]); |
3cea18ec JR |
5534 | put_ipv6_key(ipv6_key, flow, is_mask); |
5535 | if (flow->nw_frag > FLOW_NW_FRAG_MASK) { | |
5536 | return ODP_FIT_ERROR; | |
5537 | } | |
4a221615 | 5538 | if (is_mask) { |
4a221615 GY |
5539 | check_start = ipv6_key; |
5540 | check_len = sizeof *ipv6_key; | |
5541 | expected_bit = OVS_KEY_ATTR_IPV6; | |
d31f1109 | 5542 | } |
34118cae | 5543 | } |
4a221615 GY |
5544 | } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) || |
5545 | src_flow->dl_type == htons(ETH_TYPE_RARP)) { | |
5546 | if (!is_mask) { | |
5547 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP; | |
5548 | } | |
fea393b1 | 5549 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) { |
34118cae | 5550 | const struct ovs_key_arp *arp_key; |
d31f1109 | 5551 | |
34118cae | 5552 | arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]); |
4a221615 | 5553 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
34118cae BP |
5554 | VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow " |
5555 | "key", ntohs(arp_key->arp_op)); | |
b0f7b9b5 | 5556 | return ODP_FIT_ERROR; |
36956a7d | 5557 | } |
3cea18ec | 5558 | put_arp_key(arp_key, flow); |
4a221615 GY |
5559 | if (is_mask) { |
5560 | check_start = arp_key; | |
5561 | check_len = sizeof *arp_key; | |
5562 | expected_bit = OVS_KEY_ATTR_ARP; | |
5563 | } | |
5564 | } | |
3d2fbd70 JS |
5565 | } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) { |
5566 | if (!is_mask) { | |
5567 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH; | |
5568 | } | |
5569 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) { | |
5570 | const struct ovs_key_nsh *nsh_key; | |
5571 | ||
5572 | nsh_key = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]); | |
5573 | put_nsh_key(nsh_key, flow, false); | |
5574 | if (is_mask) { | |
5575 | check_start = nsh_key; | |
5576 | check_len = sizeof *nsh_key; | |
5577 | expected_bit = OVS_KEY_ATTR_NSH; | |
5578 | } | |
5579 | } | |
4a221615 GY |
5580 | } else { |
5581 | goto done; | |
5582 | } | |
df4eeb20 | 5583 | if (check_len > 0) { /* Happens only when 'is_mask'. */ |
4a221615 GY |
5584 | if (!is_all_zeros(check_start, check_len) && |
5585 | flow->dl_type != htons(0xffff)) { | |
5586 | return ODP_FIT_ERROR; | |
5587 | } else { | |
5588 | expected_attrs |= UINT64_C(1) << expected_bit; | |
36956a7d | 5589 | } |
36956a7d | 5590 | } |
36956a7d | 5591 | |
4a221615 GY |
5592 | expected_bit = OVS_KEY_ATTR_UNSPEC; |
5593 | if (src_flow->nw_proto == IPPROTO_TCP | |
5594 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
5595 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
5596 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
5597 | if (!is_mask) { | |
5598 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP; | |
5599 | } | |
fea393b1 | 5600 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) { |
3cea18ec | 5601 | const union ovs_key_tp *tcp_key; |
36956a7d | 5602 | |
34118cae | 5603 | tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]); |
3cea18ec | 5604 | put_tp_key(tcp_key, flow); |
4a221615 GY |
5605 | expected_bit = OVS_KEY_ATTR_TCP; |
5606 | } | |
dc235f7f JR |
5607 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) { |
5608 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS; | |
5609 | flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]); | |
5610 | } | |
4a221615 GY |
5611 | } else if (src_flow->nw_proto == IPPROTO_UDP |
5612 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
5613 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
5614 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
5615 | if (!is_mask) { | |
5616 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP; | |
7257b535 | 5617 | } |
fea393b1 | 5618 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) { |
3cea18ec | 5619 | const union ovs_key_tp *udp_key; |
34118cae BP |
5620 | |
5621 | udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]); | |
3cea18ec | 5622 | put_tp_key(udp_key, flow); |
4a221615 GY |
5623 | expected_bit = OVS_KEY_ATTR_UDP; |
5624 | } | |
12848ebf GS |
5625 | } else if (src_flow->nw_proto == IPPROTO_SCTP |
5626 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
5627 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
5628 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
c6bcb685 JS |
5629 | if (!is_mask) { |
5630 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP; | |
5631 | } | |
5632 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) { | |
3cea18ec | 5633 | const union ovs_key_tp *sctp_key; |
c6bcb685 JS |
5634 | |
5635 | sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]); | |
3cea18ec | 5636 | put_tp_key(sctp_key, flow); |
c6bcb685 JS |
5637 | expected_bit = OVS_KEY_ATTR_SCTP; |
5638 | } | |
4a221615 GY |
5639 | } else if (src_flow->nw_proto == IPPROTO_ICMP |
5640 | && src_flow->dl_type == htons(ETH_TYPE_IP) | |
5641 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
5642 | if (!is_mask) { | |
5643 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP; | |
d31f1109 | 5644 | } |
fea393b1 | 5645 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) { |
34118cae BP |
5646 | const struct ovs_key_icmp *icmp_key; |
5647 | ||
5648 | icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]); | |
5649 | flow->tp_src = htons(icmp_key->icmp_type); | |
5650 | flow->tp_dst = htons(icmp_key->icmp_code); | |
4a221615 GY |
5651 | expected_bit = OVS_KEY_ATTR_ICMP; |
5652 | } | |
5653 | } else if (src_flow->nw_proto == IPPROTO_ICMPV6 | |
5654 | && src_flow->dl_type == htons(ETH_TYPE_IPV6) | |
5655 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
5656 | if (!is_mask) { | |
5657 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6; | |
685a51a5 | 5658 | } |
fea393b1 | 5659 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) { |
34118cae BP |
5660 | const struct ovs_key_icmpv6 *icmpv6_key; |
5661 | ||
5662 | icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]); | |
5663 | flow->tp_src = htons(icmpv6_key->icmpv6_type); | |
5664 | flow->tp_dst = htons(icmpv6_key->icmpv6_code); | |
4a221615 | 5665 | expected_bit = OVS_KEY_ATTR_ICMPV6; |
c17fcc0a | 5666 | if (is_nd(src_flow, NULL)) { |
4a221615 GY |
5667 | if (!is_mask) { |
5668 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
5669 | } | |
fea393b1 | 5670 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) { |
34118cae BP |
5671 | const struct ovs_key_nd *nd_key; |
5672 | ||
5673 | nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]); | |
932c96b7 | 5674 | flow->nd_target = nd_key->nd_target; |
74ff3298 JR |
5675 | flow->arp_sha = nd_key->nd_sll; |
5676 | flow->arp_tha = nd_key->nd_tll; | |
4a221615 | 5677 | if (is_mask) { |
a63e3dc9 DDP |
5678 | /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, |
5679 | * ICMP type and code are 8 bits wide. Therefore, an | |
5680 | * exact match looks like htons(0xff), not | |
5681 | * htons(0xffff). See xlate_wc_finish() for details. | |
5682 | * */ | |
53cb9c3e | 5683 | if (!is_all_zeros(nd_key, sizeof *nd_key) && |
a63e3dc9 DDP |
5684 | (flow->tp_src != htons(0xff) || |
5685 | flow->tp_dst != htons(0xff))) { | |
4a221615 GY |
5686 | return ODP_FIT_ERROR; |
5687 | } else { | |
5688 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
5689 | } | |
5690 | } | |
34118cae BP |
5691 | } |
5692 | } | |
7257b535 | 5693 | } |
34118cae | 5694 | } |
4a221615 GY |
5695 | if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) { |
5696 | if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) { | |
5697 | return ODP_FIT_ERROR; | |
5698 | } else { | |
5699 | expected_attrs |= UINT64_C(1) << expected_bit; | |
5700 | } | |
5701 | } | |
7257b535 | 5702 | |
4a221615 | 5703 | done: |
b0f7b9b5 BP |
5704 | return check_expectations(present_attrs, out_of_range_attr, expected_attrs, |
5705 | key, key_len); | |
5706 | } | |
5707 | ||
5708 | /* Parse 802.1Q header then encapsulated L3 attributes. */ | |
5709 | static enum odp_key_fitness | |
5710 | parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
5711 | uint64_t present_attrs, int out_of_range_attr, | |
5712 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 GY |
5713 | const struct nlattr *key, size_t key_len, |
5714 | const struct flow *src_flow) | |
b0f7b9b5 BP |
5715 | { |
5716 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 5717 | bool is_mask = src_flow != flow; |
b0f7b9b5 | 5718 | |
f0fb825a | 5719 | const struct nlattr *encap; |
b0f7b9b5 | 5720 | enum odp_key_fitness encap_fitness; |
f0fb825a EG |
5721 | enum odp_key_fitness fitness = ODP_FIT_ERROR; |
5722 | int encaps = 0; | |
b0f7b9b5 | 5723 | |
f0fb825a EG |
5724 | while (encaps < flow_vlan_limit && |
5725 | (is_mask | |
5726 | ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0 | |
5727 | : eth_type_vlan(flow->dl_type))) { | |
5728 | ||
5729 | encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP) | |
5730 | ? attrs[OVS_KEY_ATTR_ENCAP] : NULL); | |
5731 | ||
5732 | /* Calculate fitness of outer attributes. */ | |
5733 | if (!is_mask) { | |
5734 | expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) | | |
5735 | (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)); | |
5736 | } else { | |
5737 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
5738 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
5739 | } | |
5740 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) { | |
5741 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP); | |
5742 | } | |
4a221615 | 5743 | } |
f0fb825a EG |
5744 | fitness = check_expectations(present_attrs, out_of_range_attr, |
5745 | expected_attrs, key, key_len); | |
5746 | ||
5747 | /* Set vlan_tci. | |
5748 | * Remove the TPID from dl_type since it's not the real Ethertype. */ | |
5749 | flow->vlans[encaps].tpid = flow->dl_type; | |
5750 | flow->dl_type = htons(0); | |
5751 | flow->vlans[encaps].tci = | |
5752 | (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN) | |
5753 | ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]) | |
5754 | : htons(0)); | |
5755 | if (!is_mask) { | |
f889568f | 5756 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) || |
5757 | !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { | |
f0fb825a EG |
5758 | return ODP_FIT_TOO_LITTLE; |
5759 | } else if (flow->vlans[encaps].tci == htons(0)) { | |
5760 | /* Corner case for a truncated 802.1Q header. */ | |
5761 | if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { | |
5762 | return ODP_FIT_TOO_MUCH; | |
5763 | } | |
5764 | return fitness; | |
5765 | } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) { | |
5766 | VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " | |
5767 | "but CFI bit is not set", | |
5768 | ntohs(flow->vlans[encaps].tci)); | |
5769 | return ODP_FIT_ERROR; | |
5770 | } | |
5771 | } else { | |
5772 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { | |
5773 | return fitness; | |
5774 | } | |
4a221615 | 5775 | } |
b0f7b9b5 | 5776 | |
f0fb825a EG |
5777 | /* Now parse the encapsulated attributes. */ |
5778 | if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap), | |
5779 | attrs, &present_attrs, &out_of_range_attr)) { | |
3e634810 BP |
5780 | return ODP_FIT_ERROR; |
5781 | } | |
f0fb825a EG |
5782 | expected_attrs = 0; |
5783 | ||
5784 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, | |
5785 | flow, src_flow)) { | |
5786 | return ODP_FIT_ERROR; | |
4a221615 | 5787 | } |
4a221615 | 5788 | |
f0fb825a | 5789 | encaps++; |
b0f7b9b5 | 5790 | } |
b0f7b9b5 | 5791 | |
b02475c5 | 5792 | encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
5793 | expected_attrs, flow, key, key_len, |
5794 | src_flow); | |
b0f7b9b5 BP |
5795 | |
5796 | /* The overall fitness is the worse of the outer and inner attributes. */ | |
5797 | return MAX(fitness, encap_fitness); | |
5798 | } | |
5799 | ||
4a221615 GY |
5800 | static enum odp_key_fitness |
5801 | odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len, | |
8d8ab6c2 | 5802 | struct flow *flow, const struct flow *src_flow) |
b0f7b9b5 | 5803 | { |
b0f7b9b5 BP |
5804 | const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1]; |
5805 | uint64_t expected_attrs; | |
5806 | uint64_t present_attrs; | |
5807 | int out_of_range_attr; | |
4a221615 | 5808 | bool is_mask = src_flow != flow; |
b0f7b9b5 BP |
5809 | |
5810 | memset(flow, 0, sizeof *flow); | |
5811 | ||
5812 | /* Parse attributes. */ | |
5813 | if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs, | |
5814 | &out_of_range_attr)) { | |
5815 | return ODP_FIT_ERROR; | |
5816 | } | |
5817 | expected_attrs = 0; | |
5818 | ||
5819 | /* Metadata. */ | |
572f732a AZ |
5820 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) { |
5821 | flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]); | |
5822 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID; | |
5823 | } else if (is_mask) { | |
8c1b077f | 5824 | /* Always exact match recirc_id if it is not specified. */ |
572f732a AZ |
5825 | flow->recirc_id = UINT32_MAX; |
5826 | } | |
5827 | ||
5828 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) { | |
5829 | flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]); | |
5830 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH; | |
5831 | } | |
b0f7b9b5 | 5832 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) { |
deedf7e7 | 5833 | flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]); |
b0f7b9b5 BP |
5834 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY; |
5835 | } | |
5836 | ||
72e8bf28 | 5837 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) { |
1362e248 | 5838 | flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]); |
72e8bf28 AA |
5839 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK; |
5840 | } | |
5841 | ||
07659514 JS |
5842 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) { |
5843 | uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]); | |
5844 | ||
5845 | flow->ct_state = odp_to_ovs_ct_state(odp_state); | |
5846 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE; | |
5847 | } | |
5848 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) { | |
5849 | flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]); | |
5850 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE; | |
5851 | } | |
8e53fe8c JS |
5852 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) { |
5853 | flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]); | |
5854 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK; | |
5855 | } | |
9daf2348 | 5856 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) { |
ab79d262 | 5857 | flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]); |
9daf2348 JS |
5858 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS; |
5859 | } | |
daf4d3c1 JR |
5860 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) { |
5861 | const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]); | |
5862 | flow->ct_nw_src = ct->ipv4_src; | |
5863 | flow->ct_nw_dst = ct->ipv4_dst; | |
5864 | flow->ct_nw_proto = ct->ipv4_proto; | |
5865 | flow->ct_tp_src = ct->src_port; | |
5866 | flow->ct_tp_dst = ct->dst_port; | |
5867 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4; | |
5868 | } | |
5869 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) { | |
5870 | const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]); | |
5871 | ||
5872 | flow->ct_ipv6_src = ct->ipv6_src; | |
5873 | flow->ct_ipv6_dst = ct->ipv6_dst; | |
5874 | flow->ct_nw_proto = ct->ipv6_proto; | |
5875 | flow->ct_tp_src = ct->src_port; | |
5876 | flow->ct_tp_dst = ct->dst_port; | |
5877 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6; | |
5878 | } | |
07659514 | 5879 | |
9b405f1a PS |
5880 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) { |
5881 | enum odp_key_fitness res; | |
05fb0928 | 5882 | |
8d8ab6c2 JG |
5883 | res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask, |
5884 | &flow->tunnel); | |
9b405f1a PS |
5885 | if (res == ODP_FIT_ERROR) { |
5886 | return ODP_FIT_ERROR; | |
5887 | } else if (res == ODP_FIT_PERFECT) { | |
5888 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL; | |
05fb0928 JR |
5889 | } |
5890 | } | |
5891 | ||
b0f7b9b5 | 5892 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) { |
4e022ec0 AW |
5893 | flow->in_port.odp_port |
5894 | = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]); | |
b0f7b9b5 | 5895 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT; |
4a221615 | 5896 | } else if (!is_mask) { |
4e022ec0 | 5897 | flow->in_port.odp_port = ODPP_NONE; |
b0f7b9b5 BP |
5898 | } |
5899 | ||
beb75a40 JS |
5900 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) { |
5901 | flow->packet_type | |
5902 | = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]); | |
5903 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE; | |
5904 | } else if (!is_mask) { | |
5905 | flow->packet_type = htonl(PT_ETH); | |
5906 | } | |
5907 | ||
5908 | /* Check for Ethernet header. */ | |
b0f7b9b5 BP |
5909 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) { |
5910 | const struct ovs_key_ethernet *eth_key; | |
5911 | ||
5912 | eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]); | |
3cea18ec | 5913 | put_ethernet_key(eth_key, flow); |
beb75a40 JS |
5914 | if (!is_mask) { |
5915 | flow->packet_type = htonl(PT_ETH); | |
4a221615 | 5916 | } |
4a221615 | 5917 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; |
b0f7b9b5 | 5918 | } |
beb75a40 JS |
5919 | else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
5920 | ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); | |
5921 | if (!is_mask) { | |
5922 | flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, | |
5923 | ntohs(ethertype)); | |
5924 | } | |
5925 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; | |
5926 | } | |
b0f7b9b5 BP |
5927 | |
5928 | /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */ | |
4a221615 | 5929 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, |
beb75a40 | 5930 | src_flow)) { |
b0f7b9b5 BP |
5931 | return ODP_FIT_ERROR; |
5932 | } | |
5933 | ||
21e70add | 5934 | if (is_mask |
f0fb825a EG |
5935 | ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0 |
5936 | : eth_type_vlan(src_flow->dl_type)) { | |
b0f7b9b5 | 5937 | return parse_8021q_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
5938 | expected_attrs, flow, key, key_len, src_flow); |
5939 | } | |
5940 | if (is_mask) { | |
449b8131 | 5941 | /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */ |
f0fb825a EG |
5942 | flow->vlans[0].tpid = htons(0xffff); |
5943 | flow->vlans[0].tci = htons(0xffff); | |
4a221615 | 5944 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { |
f0fb825a | 5945 | flow->vlans[0].tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]); |
4a221615 GY |
5946 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); |
5947 | } | |
b0f7b9b5 | 5948 | } |
b02475c5 | 5949 | return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
4a221615 GY |
5950 | expected_attrs, flow, key, key_len, src_flow); |
5951 | } | |
5952 | ||
5953 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow | |
5954 | * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well | |
5955 | * 'key' fits our expectations for what a flow key should contain. | |
5956 | * | |
5957 | * The 'in_port' will be the datapath's understanding of the port. The | |
5958 | * caller will need to translate with odp_port_to_ofp_port() if the | |
5959 | * OpenFlow port is needed. | |
5960 | * | |
5961 | * This function doesn't take the packet itself as an argument because none of | |
5962 | * the currently understood OVS_KEY_ATTR_* attributes require it. Currently, | |
5963 | * it is always possible to infer which additional attribute(s) should appear | |
5964 | * by looking at the attributes for lower-level protocols, e.g. if the network | |
5965 | * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we | |
5966 | * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it | |
5967 | * must be absent. */ | |
5968 | enum odp_key_fitness | |
5969 | odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, | |
5970 | struct flow *flow) | |
5971 | { | |
8d8ab6c2 | 5972 | return odp_flow_key_to_flow__(key, key_len, flow, flow); |
4a221615 GY |
5973 | } |
5974 | ||
8d8ab6c2 JG |
5975 | /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key' |
5976 | * to a mask structure in 'mask'. 'flow' must be a previously translated flow | |
5977 | * corresponding to 'mask' and similarly flow_key/flow_key_len must be the | |
5978 | * attributes from that flow. Returns an ODP_FIT_* value that indicates how | |
5979 | * well 'key' fits our expectations for what a flow key should contain. */ | |
5980 | enum odp_key_fitness | |
5981 | odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len, | |
5982 | struct flow_wildcards *mask, const struct flow *src_flow) | |
ca8d3442 DDP |
5983 | { |
5984 | if (mask_key_len) { | |
5985 | return odp_flow_key_to_flow__(mask_key, mask_key_len, | |
8d8ab6c2 | 5986 | &mask->masks, src_flow); |
ca8d3442 DDP |
5987 | |
5988 | } else { | |
5989 | /* A missing mask means that the flow should be exact matched. | |
5990 | * Generate an appropriate exact wildcard for the flow. */ | |
5991 | flow_wildcards_init_for_packet(mask, src_flow); | |
5992 | ||
5993 | return ODP_FIT_PERFECT; | |
5994 | } | |
5995 | } | |
39db78a0 | 5996 | |
8b668ee3 PB |
5997 | /* Converts the netlink formated key/mask to match. |
5998 | * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask | |
5999 | * disagree on the acceptable form of flow */ | |
6000 | int | |
6001 | parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len, | |
6002 | const struct nlattr *mask, size_t mask_len, | |
6003 | struct match *match) | |
6004 | { | |
6005 | enum odp_key_fitness fitness; | |
6006 | ||
6007 | fitness = odp_flow_key_to_flow(key, key_len, &match->flow); | |
6008 | if (fitness) { | |
6009 | /* This should not happen: it indicates that | |
6010 | * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on | |
6011 | * the acceptable form of a flow. Log the problem as an error, | |
6012 | * with enough details to enable debugging. */ | |
6013 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
6014 | ||
6015 | if (!VLOG_DROP_ERR(&rl)) { | |
6016 | struct ds s; | |
6017 | ||
6018 | ds_init(&s); | |
6019 | odp_flow_format(key, key_len, NULL, 0, NULL, &s, true); | |
6020 | VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s)); | |
6021 | ds_destroy(&s); | |
6022 | } | |
6023 | ||
6024 | return EINVAL; | |
6025 | } | |
6026 | ||
6027 | fitness = odp_flow_key_to_mask(mask, mask_len, &match->wc, &match->flow); | |
6028 | if (fitness) { | |
6029 | /* This should not happen: it indicates that | |
6030 | * odp_flow_key_from_mask() and odp_flow_key_to_mask() | |
6031 | * disagree on the acceptable form of a mask. Log the problem | |
6032 | * as an error, with enough details to enable debugging. */ | |
6033 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
6034 | ||
6035 | if (!VLOG_DROP_ERR(&rl)) { | |
6036 | struct ds s; | |
6037 | ||
6038 | ds_init(&s); | |
6039 | odp_flow_format(key, key_len, mask, mask_len, NULL, &s, | |
6040 | true); | |
6041 | VLOG_ERR("internal error parsing flow mask %s (%s)", | |
6042 | ds_cstr(&s), odp_key_fitness_to_string(fitness)); | |
6043 | ds_destroy(&s); | |
6044 | } | |
6045 | ||
6046 | return EINVAL; | |
6047 | } | |
6048 | ||
6049 | return 0; | |
6050 | } | |
6051 | ||
6814e51f BP |
6052 | /* Returns 'fitness' as a string, for use in debug messages. */ |
6053 | const char * | |
6054 | odp_key_fitness_to_string(enum odp_key_fitness fitness) | |
6055 | { | |
6056 | switch (fitness) { | |
6057 | case ODP_FIT_PERFECT: | |
6058 | return "OK"; | |
6059 | case ODP_FIT_TOO_MUCH: | |
6060 | return "too_much"; | |
6061 | case ODP_FIT_TOO_LITTLE: | |
6062 | return "too_little"; | |
6063 | case ODP_FIT_ERROR: | |
6064 | return "error"; | |
6065 | default: | |
6066 | return "<unknown>"; | |
6067 | } | |
6068 | } | |
6069 | ||
39db78a0 | 6070 | /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies |
e995e3df BP |
6071 | * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute |
6072 | * whose contents are the 'userdata_size' bytes at 'userdata' and returns the | |
6073 | * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is | |
6074 | * null, then the return value is not meaningful.) */ | |
39db78a0 | 6075 | size_t |
e995e3df BP |
6076 | odp_put_userspace_action(uint32_t pid, |
6077 | const void *userdata, size_t userdata_size, | |
8b7ea2d4 | 6078 | odp_port_t tunnel_out_port, |
7321bda3 | 6079 | bool include_actions, |
39db78a0 BP |
6080 | struct ofpbuf *odp_actions) |
6081 | { | |
e995e3df | 6082 | size_t userdata_ofs; |
39db78a0 BP |
6083 | size_t offset; |
6084 | ||
6085 | offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); | |
6086 | nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); | |
e995e3df | 6087 | if (userdata) { |
6fd6ed71 | 6088 | userdata_ofs = odp_actions->size + NLA_HDRLEN; |
96ed775f BP |
6089 | |
6090 | /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel | |
6091 | * module before Linux 3.10 required the userdata to be exactly 8 bytes | |
6092 | * long: | |
6093 | * | |
6094 | * - The kernel rejected shorter userdata with -ERANGE. | |
6095 | * | |
6096 | * - The kernel silently dropped userdata beyond the first 8 bytes. | |
6097 | * | |
6098 | * Thus, for maximum compatibility, always put at least 8 bytes. (We | |
6099 | * separately disable features that required more than 8 bytes.) */ | |
6100 | memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA, | |
6101 | MAX(8, userdata_size)), | |
6102 | userdata, userdata_size); | |
e995e3df BP |
6103 | } else { |
6104 | userdata_ofs = 0; | |
39db78a0 | 6105 | } |
8b7ea2d4 WZ |
6106 | if (tunnel_out_port != ODPP_NONE) { |
6107 | nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT, | |
6108 | tunnel_out_port); | |
6109 | } | |
7321bda3 NM |
6110 | if (include_actions) { |
6111 | nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS); | |
6112 | } | |
39db78a0 BP |
6113 | nl_msg_end_nested(odp_actions, offset); |
6114 | ||
e995e3df | 6115 | return userdata_ofs; |
39db78a0 | 6116 | } |
b9ad7294 | 6117 | |
88fc5281 JS |
6118 | void |
6119 | odp_put_pop_eth_action(struct ofpbuf *odp_actions) | |
6120 | { | |
6121 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH); | |
6122 | } | |
6123 | ||
6124 | void | |
6125 | odp_put_push_eth_action(struct ofpbuf *odp_actions, | |
6126 | const struct eth_addr *eth_src, | |
6127 | const struct eth_addr *eth_dst) | |
6128 | { | |
6129 | struct ovs_action_push_eth eth; | |
6130 | ||
6131 | memset(ð, 0, sizeof eth); | |
6132 | if (eth_src) { | |
6133 | eth.addresses.eth_src = *eth_src; | |
6134 | } | |
6135 | if (eth_dst) { | |
6136 | eth.addresses.eth_dst = *eth_dst; | |
6137 | } | |
6138 | ||
6139 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH, | |
6140 | ð, sizeof eth); | |
6141 | } | |
6142 | ||
b9ad7294 EJ |
6143 | void |
6144 | odp_put_tunnel_action(const struct flow_tnl *tunnel, | |
6145 | struct ofpbuf *odp_actions) | |
6146 | { | |
6147 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
ec1f6f32 | 6148 | tun_key_to_attr(odp_actions, tunnel, tunnel, NULL); |
b9ad7294 EJ |
6149 | nl_msg_end_nested(odp_actions, offset); |
6150 | } | |
a36de779 PS |
6151 | |
6152 | void | |
6153 | odp_put_tnl_push_action(struct ofpbuf *odp_actions, | |
6154 | struct ovs_action_push_tnl *data) | |
6155 | { | |
6156 | int size = offsetof(struct ovs_action_push_tnl, header); | |
6157 | ||
6158 | size += data->header_len; | |
6159 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size); | |
6160 | } | |
6161 | ||
5bbda0aa EJ |
6162 | \f |
6163 | /* The commit_odp_actions() function and its helpers. */ | |
6164 | ||
6165 | static void | |
6166 | commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type, | |
6167 | const void *key, size_t key_size) | |
6168 | { | |
6169 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
6170 | nl_msg_put_unspec(odp_actions, key_type, key, key_size); | |
6171 | nl_msg_end_nested(odp_actions, offset); | |
6172 | } | |
6173 | ||
6d670e7f JR |
6174 | /* Masked set actions have a mask following the data within the netlink |
6175 | * attribute. The unmasked bits in the data will be cleared as the data | |
6176 | * is copied to the action. */ | |
6177 | void | |
6178 | commit_masked_set_action(struct ofpbuf *odp_actions, | |
6179 | enum ovs_key_attr key_type, | |
6180 | const void *key_, const void *mask_, size_t key_size) | |
6181 | { | |
6182 | size_t offset = nl_msg_start_nested(odp_actions, | |
6183 | OVS_ACTION_ATTR_SET_MASKED); | |
6184 | char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2); | |
6185 | const char *key = key_, *mask = mask_; | |
6186 | ||
6187 | memcpy(data + key_size, mask, key_size); | |
6188 | /* Clear unmasked bits while copying. */ | |
6189 | while (key_size--) { | |
6190 | *data++ = *key++ & *mask++; | |
6191 | } | |
6192 | nl_msg_end_nested(odp_actions, offset); | |
6193 | } | |
6194 | ||
b9ad7294 EJ |
6195 | /* If any of the flow key data that ODP actions can modify are different in |
6196 | * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to | |
6197 | * 'odp_actions' that change the flow tunneling information in key from | |
6198 | * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the | |
6199 | * same way. In other words, operates the same as commit_odp_actions(), but | |
6200 | * only on tunneling information. */ | |
6201 | void | |
6202 | commit_odp_tunnel_action(const struct flow *flow, struct flow *base, | |
5bbda0aa EJ |
6203 | struct ofpbuf *odp_actions) |
6204 | { | |
ffe4c74f JB |
6205 | /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel |
6206 | * must have non-zero ipv6_dst. */ | |
6207 | if (flow_tnl_dst_is_set(&flow->tunnel)) { | |
fc80de30 JR |
6208 | if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) { |
6209 | return; | |
6210 | } | |
6211 | memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel); | |
b9ad7294 | 6212 | odp_put_tunnel_action(&base->tunnel, odp_actions); |
05fb0928 | 6213 | } |
5bbda0aa EJ |
6214 | } |
6215 | ||
d23df9a8 JR |
6216 | static bool |
6217 | commit(enum ovs_key_attr attr, bool use_masked_set, | |
6218 | const void *key, void *base, void *mask, size_t size, | |
6219 | struct ofpbuf *odp_actions) | |
5bbda0aa | 6220 | { |
d23df9a8 JR |
6221 | if (memcmp(key, base, size)) { |
6222 | bool fully_masked = odp_mask_is_exact(attr, mask, size); | |
5bbda0aa | 6223 | |
d23df9a8 JR |
6224 | if (use_masked_set && !fully_masked) { |
6225 | commit_masked_set_action(odp_actions, attr, key, mask, size); | |
6226 | } else { | |
6227 | if (!fully_masked) { | |
6228 | memset(mask, 0xff, size); | |
6229 | } | |
6230 | commit_set_action(odp_actions, attr, key, size); | |
6231 | } | |
6232 | memcpy(base, key, size); | |
6233 | return true; | |
6234 | } else { | |
6235 | /* Mask bits are set when we have either read or set the corresponding | |
6236 | * values. Masked bits will be exact-matched, no need to set them | |
6237 | * if the value did not actually change. */ | |
6238 | return false; | |
5bbda0aa | 6239 | } |
d23df9a8 | 6240 | } |
5bbda0aa | 6241 | |
d23df9a8 JR |
6242 | static void |
6243 | get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth) | |
6244 | { | |
74ff3298 JR |
6245 | eth->eth_src = flow->dl_src; |
6246 | eth->eth_dst = flow->dl_dst; | |
d23df9a8 | 6247 | } |
1dd35f8a | 6248 | |
d23df9a8 JR |
6249 | static void |
6250 | put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow) | |
6251 | { | |
74ff3298 JR |
6252 | flow->dl_src = eth->eth_src; |
6253 | flow->dl_dst = eth->eth_dst; | |
d23df9a8 | 6254 | } |
5bbda0aa | 6255 | |
d23df9a8 | 6256 | static void |
f839892a JS |
6257 | commit_set_ether_action(const struct flow *flow, struct flow *base_flow, |
6258 | struct ofpbuf *odp_actions, | |
6259 | struct flow_wildcards *wc, | |
6260 | bool use_masked) | |
d23df9a8 JR |
6261 | { |
6262 | struct ovs_key_ethernet key, base, mask; | |
5bbda0aa | 6263 | |
f839892a JS |
6264 | if (flow->packet_type != htonl(PT_ETH)) { |
6265 | return; | |
6266 | } | |
6267 | ||
d23df9a8 JR |
6268 | get_ethernet_key(flow, &key); |
6269 | get_ethernet_key(base_flow, &base); | |
6270 | get_ethernet_key(&wc->masks, &mask); | |
6271 | ||
6272 | if (commit(OVS_KEY_ATTR_ETHERNET, use_masked, | |
6273 | &key, &base, &mask, sizeof key, odp_actions)) { | |
6274 | put_ethernet_key(&base, base_flow); | |
6275 | put_ethernet_key(&mask, &wc->masks); | |
6276 | } | |
5bbda0aa EJ |
6277 | } |
6278 | ||
6279 | static void | |
f0fb825a EG |
6280 | commit_vlan_action(const struct flow* flow, struct flow *base, |
6281 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
5bbda0aa | 6282 | { |
f0fb825a EG |
6283 | int base_n = flow_count_vlan_headers(base); |
6284 | int flow_n = flow_count_vlan_headers(flow); | |
6285 | flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n); | |
1dd35f8a | 6286 | |
f0fb825a EG |
6287 | /* Pop all mismatching vlan of base, push those of flow */ |
6288 | for (; base_n >= 0; base_n--) { | |
5bbda0aa | 6289 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); |
f0fb825a | 6290 | wc->masks.vlans[base_n].qtag = OVS_BE32_MAX; |
5bbda0aa EJ |
6291 | } |
6292 | ||
f0fb825a | 6293 | for (; flow_n >= 0; flow_n--) { |
5bbda0aa EJ |
6294 | struct ovs_action_push_vlan vlan; |
6295 | ||
f0fb825a EG |
6296 | vlan.vlan_tpid = flow->vlans[flow_n].tpid; |
6297 | vlan.vlan_tci = flow->vlans[flow_n].tci; | |
5bbda0aa EJ |
6298 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, |
6299 | &vlan, sizeof vlan); | |
6300 | } | |
f0fb825a | 6301 | memcpy(base->vlans, flow->vlans, sizeof(base->vlans)); |
5bbda0aa EJ |
6302 | } |
6303 | ||
22d38fca | 6304 | /* Wildcarding already done at action translation time. */ |
b02475c5 SH |
6305 | static void |
6306 | commit_mpls_action(const struct flow *flow, struct flow *base, | |
22d38fca | 6307 | struct ofpbuf *odp_actions) |
b02475c5 | 6308 | { |
22d38fca JR |
6309 | int base_n = flow_count_mpls_labels(base, NULL); |
6310 | int flow_n = flow_count_mpls_labels(flow, NULL); | |
8bfd0fda | 6311 | int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n, |
22d38fca | 6312 | NULL); |
8bfd0fda BP |
6313 | |
6314 | while (base_n > common_n) { | |
6315 | if (base_n - 1 == common_n && flow_n > common_n) { | |
6316 | /* If there is only one more LSE in base than there are common | |
6317 | * between base and flow; and flow has at least one more LSE than | |
6318 | * is common then the topmost LSE of base may be updated using | |
6319 | * set */ | |
6320 | struct ovs_key_mpls mpls_key; | |
6321 | ||
6322 | mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n]; | |
6323 | commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, | |
6324 | &mpls_key, sizeof mpls_key); | |
6325 | flow_set_mpls_lse(base, 0, mpls_key.mpls_lse); | |
6326 | common_n++; | |
6327 | } else { | |
6328 | /* Otherwise, if there more LSEs in base than are common between | |
6329 | * base and flow then pop the topmost one. */ | |
6330 | ovs_be16 dl_type; | |
6331 | bool popped; | |
6332 | ||
6333 | /* If all the LSEs are to be popped and this is not the outermost | |
6334 | * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the | |
6335 | * POP_MPLS action instead of flow->dl_type. | |
6336 | * | |
6337 | * This is because the POP_MPLS action requires its ethertype | |
6338 | * argument to be an MPLS ethernet type but in this case | |
6339 | * flow->dl_type will be a non-MPLS ethernet type. | |
6340 | * | |
6341 | * When the final POP_MPLS action occurs it use flow->dl_type and | |
6342 | * the and the resulting packet will have the desired dl_type. */ | |
6343 | if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) { | |
6344 | dl_type = htons(ETH_TYPE_MPLS); | |
6345 | } else { | |
6346 | dl_type = flow->dl_type; | |
6347 | } | |
6348 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type); | |
22d38fca | 6349 | popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL); |
8bfd0fda BP |
6350 | ovs_assert(popped); |
6351 | base_n--; | |
6352 | } | |
b02475c5 SH |
6353 | } |
6354 | ||
8bfd0fda BP |
6355 | /* If, after the above popping and setting, there are more LSEs in flow |
6356 | * than base then some LSEs need to be pushed. */ | |
6357 | while (base_n < flow_n) { | |
b02475c5 SH |
6358 | struct ovs_action_push_mpls *mpls; |
6359 | ||
8bfd0fda BP |
6360 | mpls = nl_msg_put_unspec_zero(odp_actions, |
6361 | OVS_ACTION_ATTR_PUSH_MPLS, | |
9ddf12cc | 6362 | sizeof *mpls); |
b02475c5 | 6363 | mpls->mpls_ethertype = flow->dl_type; |
8bfd0fda | 6364 | mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1]; |
742c0ac3 JR |
6365 | /* Update base flow's MPLS stack, but do not clear L3. We need the L3 |
6366 | * headers if the flow is restored later due to returning from a patch | |
6367 | * port or group bucket. */ | |
6368 | flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL, false); | |
8bfd0fda BP |
6369 | flow_set_mpls_lse(base, 0, mpls->mpls_lse); |
6370 | base_n++; | |
b0a17866 | 6371 | } |
b02475c5 SH |
6372 | } |
6373 | ||
5bbda0aa | 6374 | static void |
3cea18ec | 6375 | get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask) |
5bbda0aa | 6376 | { |
d23df9a8 JR |
6377 | ipv4->ipv4_src = flow->nw_src; |
6378 | ipv4->ipv4_dst = flow->nw_dst; | |
6379 | ipv4->ipv4_proto = flow->nw_proto; | |
6380 | ipv4->ipv4_tos = flow->nw_tos; | |
6381 | ipv4->ipv4_ttl = flow->nw_ttl; | |
3cea18ec | 6382 | ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask); |
d23df9a8 | 6383 | } |
5bbda0aa | 6384 | |
d23df9a8 | 6385 | static void |
3cea18ec | 6386 | put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask) |
d23df9a8 JR |
6387 | { |
6388 | flow->nw_src = ipv4->ipv4_src; | |
6389 | flow->nw_dst = ipv4->ipv4_dst; | |
6390 | flow->nw_proto = ipv4->ipv4_proto; | |
6391 | flow->nw_tos = ipv4->ipv4_tos; | |
6392 | flow->nw_ttl = ipv4->ipv4_ttl; | |
3cea18ec | 6393 | flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask); |
d23df9a8 JR |
6394 | } |
6395 | ||
6396 | static void | |
6397 | commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow, | |
6398 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
6399 | bool use_masked) | |
6400 | { | |
6401 | struct ovs_key_ipv4 key, mask, base; | |
5bbda0aa | 6402 | |
d23df9a8 JR |
6403 | /* Check that nw_proto and nw_frag remain unchanged. */ |
6404 | ovs_assert(flow->nw_proto == base_flow->nw_proto && | |
6405 | flow->nw_frag == base_flow->nw_frag); | |
1dd35f8a | 6406 | |
3cea18ec JR |
6407 | get_ipv4_key(flow, &key, false); |
6408 | get_ipv4_key(base_flow, &base, false); | |
6409 | get_ipv4_key(&wc->masks, &mask, true); | |
d23df9a8 JR |
6410 | mask.ipv4_proto = 0; /* Not writeable. */ |
6411 | mask.ipv4_frag = 0; /* Not writable. */ | |
5bbda0aa | 6412 | |
d23df9a8 JR |
6413 | if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key, |
6414 | odp_actions)) { | |
3cea18ec | 6415 | put_ipv4_key(&base, base_flow, false); |
d23df9a8 | 6416 | if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */ |
3cea18ec | 6417 | put_ipv4_key(&mask, &wc->masks, true); |
d23df9a8 JR |
6418 | } |
6419 | } | |
5bbda0aa EJ |
6420 | } |
6421 | ||
c4f2731d | 6422 | static void |
3cea18ec | 6423 | get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask) |
c4f2731d | 6424 | { |
932c96b7 JR |
6425 | ipv6->ipv6_src = flow->ipv6_src; |
6426 | ipv6->ipv6_dst = flow->ipv6_dst; | |
d23df9a8 JR |
6427 | ipv6->ipv6_label = flow->ipv6_label; |
6428 | ipv6->ipv6_proto = flow->nw_proto; | |
6429 | ipv6->ipv6_tclass = flow->nw_tos; | |
6430 | ipv6->ipv6_hlimit = flow->nw_ttl; | |
3cea18ec | 6431 | ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask); |
d23df9a8 | 6432 | } |
c4f2731d | 6433 | |
d23df9a8 | 6434 | static void |
3cea18ec | 6435 | put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask) |
d23df9a8 | 6436 | { |
932c96b7 JR |
6437 | flow->ipv6_src = ipv6->ipv6_src; |
6438 | flow->ipv6_dst = ipv6->ipv6_dst; | |
d23df9a8 JR |
6439 | flow->ipv6_label = ipv6->ipv6_label; |
6440 | flow->nw_proto = ipv6->ipv6_proto; | |
6441 | flow->nw_tos = ipv6->ipv6_tclass; | |
6442 | flow->nw_ttl = ipv6->ipv6_hlimit; | |
3cea18ec | 6443 | flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask); |
d23df9a8 | 6444 | } |
c4f2731d | 6445 | |
d23df9a8 JR |
6446 | static void |
6447 | commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow, | |
6448 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
6449 | bool use_masked) | |
6450 | { | |
6451 | struct ovs_key_ipv6 key, mask, base; | |
1dd35f8a | 6452 | |
d23df9a8 JR |
6453 | /* Check that nw_proto and nw_frag remain unchanged. */ |
6454 | ovs_assert(flow->nw_proto == base_flow->nw_proto && | |
6455 | flow->nw_frag == base_flow->nw_frag); | |
c4f2731d | 6456 | |
3cea18ec JR |
6457 | get_ipv6_key(flow, &key, false); |
6458 | get_ipv6_key(base_flow, &base, false); | |
6459 | get_ipv6_key(&wc->masks, &mask, true); | |
d23df9a8 JR |
6460 | mask.ipv6_proto = 0; /* Not writeable. */ |
6461 | mask.ipv6_frag = 0; /* Not writable. */ | |
c4f2731d | 6462 | |
d23df9a8 JR |
6463 | if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key, |
6464 | odp_actions)) { | |
3cea18ec | 6465 | put_ipv6_key(&base, base_flow, false); |
d23df9a8 | 6466 | if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */ |
3cea18ec | 6467 | put_ipv6_key(&mask, &wc->masks, true); |
d23df9a8 JR |
6468 | } |
6469 | } | |
c4f2731d PS |
6470 | } |
6471 | ||
d23df9a8 JR |
6472 | static void |
6473 | get_arp_key(const struct flow *flow, struct ovs_key_arp *arp) | |
f6c8a6b1 | 6474 | { |
d23df9a8 JR |
6475 | /* ARP key has padding, clear it. */ |
6476 | memset(arp, 0, sizeof *arp); | |
f6c8a6b1 | 6477 | |
d23df9a8 JR |
6478 | arp->arp_sip = flow->nw_src; |
6479 | arp->arp_tip = flow->nw_dst; | |
6480 | arp->arp_op = htons(flow->nw_proto); | |
74ff3298 JR |
6481 | arp->arp_sha = flow->arp_sha; |
6482 | arp->arp_tha = flow->arp_tha; | |
d23df9a8 | 6483 | } |
f6c8a6b1 | 6484 | |
d23df9a8 JR |
6485 | static void |
6486 | put_arp_key(const struct ovs_key_arp *arp, struct flow *flow) | |
6487 | { | |
6488 | flow->nw_src = arp->arp_sip; | |
6489 | flow->nw_dst = arp->arp_tip; | |
6490 | flow->nw_proto = ntohs(arp->arp_op); | |
74ff3298 JR |
6491 | flow->arp_sha = arp->arp_sha; |
6492 | flow->arp_tha = arp->arp_tha; | |
d23df9a8 | 6493 | } |
f6c8a6b1 | 6494 | |
d23df9a8 JR |
6495 | static enum slow_path_reason |
6496 | commit_set_arp_action(const struct flow *flow, struct flow *base_flow, | |
6497 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
6498 | { | |
6499 | struct ovs_key_arp key, mask, base; | |
f6c8a6b1 | 6500 | |
d23df9a8 JR |
6501 | get_arp_key(flow, &key); |
6502 | get_arp_key(base_flow, &base); | |
6503 | get_arp_key(&wc->masks, &mask); | |
f6c8a6b1 | 6504 | |
d23df9a8 JR |
6505 | if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key, |
6506 | odp_actions)) { | |
6507 | put_arp_key(&base, base_flow); | |
6508 | put_arp_key(&mask, &wc->masks); | |
6509 | return SLOW_ACTION; | |
6510 | } | |
6511 | return 0; | |
f6c8a6b1 BP |
6512 | } |
6513 | ||
f6ecf944 JP |
6514 | static void |
6515 | get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp) | |
6516 | { | |
6517 | /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */ | |
6518 | icmp->icmp_type = ntohs(flow->tp_src); | |
6519 | icmp->icmp_code = ntohs(flow->tp_dst); | |
6520 | } | |
6521 | ||
6522 | static void | |
6523 | put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow) | |
6524 | { | |
6525 | /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */ | |
6526 | flow->tp_src = htons(icmp->icmp_type); | |
6527 | flow->tp_dst = htons(icmp->icmp_code); | |
6528 | } | |
6529 | ||
6530 | static enum slow_path_reason | |
6531 | commit_set_icmp_action(const struct flow *flow, struct flow *base_flow, | |
6532 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
6533 | { | |
6534 | struct ovs_key_icmp key, mask, base; | |
6535 | enum ovs_key_attr attr; | |
6536 | ||
a75636c8 | 6537 | if (is_icmpv4(flow, NULL)) { |
efa6665e | 6538 | attr = OVS_KEY_ATTR_ICMP; |
a75636c8 | 6539 | } else if (is_icmpv6(flow, NULL)) { |
efa6665e DDP |
6540 | attr = OVS_KEY_ATTR_ICMPV6; |
6541 | } else { | |
6542 | return 0; | |
6543 | } | |
6544 | ||
f6ecf944 JP |
6545 | get_icmp_key(flow, &key); |
6546 | get_icmp_key(base_flow, &base); | |
6547 | get_icmp_key(&wc->masks, &mask); | |
6548 | ||
f6ecf944 JP |
6549 | if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) { |
6550 | put_icmp_key(&base, base_flow); | |
6551 | put_icmp_key(&mask, &wc->masks); | |
6552 | return SLOW_ACTION; | |
6553 | } | |
6554 | return 0; | |
6555 | } | |
6556 | ||
e60e935b SRCSA |
6557 | static void |
6558 | get_nd_key(const struct flow *flow, struct ovs_key_nd *nd) | |
6559 | { | |
932c96b7 | 6560 | nd->nd_target = flow->nd_target; |
e60e935b | 6561 | /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */ |
74ff3298 JR |
6562 | nd->nd_sll = flow->arp_sha; |
6563 | nd->nd_tll = flow->arp_tha; | |
e60e935b SRCSA |
6564 | } |
6565 | ||
6566 | static void | |
6567 | put_nd_key(const struct ovs_key_nd *nd, struct flow *flow) | |
6568 | { | |
932c96b7 | 6569 | flow->nd_target = nd->nd_target; |
e60e935b | 6570 | /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */ |
74ff3298 JR |
6571 | flow->arp_sha = nd->nd_sll; |
6572 | flow->arp_tha = nd->nd_tll; | |
e60e935b SRCSA |
6573 | } |
6574 | ||
6575 | static enum slow_path_reason | |
6576 | commit_set_nd_action(const struct flow *flow, struct flow *base_flow, | |
6577 | struct ofpbuf *odp_actions, | |
6578 | struct flow_wildcards *wc, bool use_masked) | |
6579 | { | |
6580 | struct ovs_key_nd key, mask, base; | |
6581 | ||
6582 | get_nd_key(flow, &key); | |
6583 | get_nd_key(base_flow, &base); | |
6584 | get_nd_key(&wc->masks, &mask); | |
6585 | ||
6586 | if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key, | |
6587 | odp_actions)) { | |
6588 | put_nd_key(&base, base_flow); | |
6589 | put_nd_key(&mask, &wc->masks); | |
6590 | return SLOW_ACTION; | |
6591 | } | |
6592 | ||
6593 | return 0; | |
6594 | } | |
6595 | ||
f6c8a6b1 | 6596 | static enum slow_path_reason |
c4f2731d | 6597 | commit_set_nw_action(const struct flow *flow, struct flow *base, |
d23df9a8 JR |
6598 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
6599 | bool use_masked) | |
c4f2731d | 6600 | { |
f6c8a6b1 | 6601 | /* Check if 'flow' really has an L3 header. */ |
c4f2731d | 6602 | if (!flow->nw_proto) { |
f6c8a6b1 | 6603 | return 0; |
c4f2731d PS |
6604 | } |
6605 | ||
f6c8a6b1 BP |
6606 | switch (ntohs(base->dl_type)) { |
6607 | case ETH_TYPE_IP: | |
d23df9a8 | 6608 | commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked); |
f6c8a6b1 BP |
6609 | break; |
6610 | ||
6611 | case ETH_TYPE_IPV6: | |
d23df9a8 | 6612 | commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked); |
e60e935b | 6613 | return commit_set_nd_action(flow, base, odp_actions, wc, use_masked); |
f6c8a6b1 BP |
6614 | |
6615 | case ETH_TYPE_ARP: | |
6616 | return commit_set_arp_action(flow, base, odp_actions, wc); | |
c4f2731d | 6617 | } |
f6c8a6b1 BP |
6618 | |
6619 | return 0; | |
c4f2731d PS |
6620 | } |
6621 | ||
3d2fbd70 JS |
6622 | static void |
6623 | get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, bool is_mask) | |
6624 | { | |
6625 | nsh->flags = flow->nsh.flags; | |
6626 | nsh->mdtype = flow->nsh.mdtype; | |
6627 | nsh->np = flow->nsh.np; | |
6628 | nsh->path_hdr = htonl((ntohl(flow->nsh.spi) << NSH_SPI_SHIFT) | | |
6629 | flow->nsh.si); | |
6630 | if (is_mask) { | |
6631 | for (int i = 0; i < 4; i++) { | |
6632 | nsh->c[i] = flow->nsh.c[i]; | |
6633 | } | |
6634 | } else { | |
6635 | switch (nsh->mdtype) { | |
6636 | case NSH_M_TYPE1: | |
6637 | for (int i = 0; i < 4; i++) { | |
6638 | nsh->c[i] = flow->nsh.c[i]; | |
6639 | } | |
6640 | break; | |
6641 | case NSH_M_TYPE2: | |
1fc11c59 JS |
6642 | default: |
6643 | /* No match support for other MD formats yet. */ | |
3d2fbd70 JS |
6644 | break; |
6645 | } | |
6646 | } | |
6647 | } | |
6648 | ||
6649 | static void | |
6650 | put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow, | |
6651 | bool is_mask OVS_UNUSED) | |
6652 | { | |
6653 | flow->nsh.flags = nsh->flags; | |
6654 | flow->nsh.mdtype = nsh->mdtype; | |
6655 | flow->nsh.np = nsh->np; | |
6656 | flow->nsh.spi = htonl((ntohl(nsh->path_hdr) & NSH_SPI_MASK) >> | |
6657 | NSH_SPI_SHIFT); | |
6658 | flow->nsh.si = (ntohl(nsh->path_hdr) & NSH_SI_MASK) >> NSH_SI_SHIFT; | |
6659 | switch (nsh->mdtype) { | |
6660 | case NSH_M_TYPE1: | |
6661 | for (int i = 0; i < 4; i++) { | |
6662 | flow->nsh.c[i] = nsh->c[i]; | |
6663 | } | |
6664 | break; | |
6665 | case NSH_M_TYPE2: | |
1fc11c59 JS |
6666 | default: |
6667 | /* No match support for other MD formats yet. */ | |
3d2fbd70 JS |
6668 | memset(flow->nsh.c, 0, sizeof flow->nsh.c); |
6669 | break; | |
6670 | } | |
6671 | } | |
6672 | ||
6673 | static void | |
6674 | commit_set_nsh_action(const struct flow *flow, struct flow *base_flow, | |
6675 | struct ofpbuf *odp_actions, | |
6676 | struct flow_wildcards *wc, | |
6677 | bool use_masked) | |
6678 | { | |
6679 | struct ovs_key_nsh key, mask, base; | |
6680 | ||
6681 | if (flow->dl_type != htons(ETH_TYPE_NSH) || | |
6682 | !memcmp(&base_flow->nsh, &flow->nsh, sizeof base_flow->nsh)) { | |
6683 | return; | |
6684 | } | |
6685 | ||
6686 | /* Check that mdtype and np remain unchanged. */ | |
6687 | ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype && | |
6688 | flow->nsh.np == base_flow->nsh.np); | |
6689 | ||
6690 | get_nsh_key(flow, &key, false); | |
6691 | get_nsh_key(base_flow, &base, false); | |
6692 | get_nsh_key(&wc->masks, &mask, true); | |
6693 | mask.mdtype = 0; /* Not writable. */ | |
6694 | mask.np = 0; /* Not writable. */ | |
6695 | ||
6696 | if (commit(OVS_KEY_ATTR_NSH, use_masked, &key, &base, &mask, sizeof key, | |
6697 | odp_actions)) { | |
6698 | put_nsh_key(&base, base_flow, false); | |
6699 | if (mask.mdtype != 0) { /* Mask was changed by commit(). */ | |
6700 | put_nsh_key(&mask, &wc->masks, true); | |
6701 | } | |
6702 | } | |
6703 | } | |
6704 | ||
d23df9a8 JR |
6705 | /* TCP, UDP, and SCTP keys have the same layout. */ |
6706 | BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) && | |
6707 | sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp)); | |
6708 | ||
5bbda0aa | 6709 | static void |
3cea18ec | 6710 | get_tp_key(const struct flow *flow, union ovs_key_tp *tp) |
5bbda0aa | 6711 | { |
3cea18ec JR |
6712 | tp->tcp.tcp_src = flow->tp_src; |
6713 | tp->tcp.tcp_dst = flow->tp_dst; | |
d23df9a8 JR |
6714 | } |
6715 | ||
6716 | static void | |
3cea18ec | 6717 | put_tp_key(const union ovs_key_tp *tp, struct flow *flow) |
d23df9a8 | 6718 | { |
3cea18ec JR |
6719 | flow->tp_src = tp->tcp.tcp_src; |
6720 | flow->tp_dst = tp->tcp.tcp_dst; | |
d23df9a8 JR |
6721 | } |
6722 | ||
6723 | static void | |
6724 | commit_set_port_action(const struct flow *flow, struct flow *base_flow, | |
6725 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
6726 | bool use_masked) | |
6727 | { | |
6728 | enum ovs_key_attr key_type; | |
3cea18ec | 6729 | union ovs_key_tp key, mask, base; |
d23df9a8 | 6730 | |
791a09be SH |
6731 | /* Check if 'flow' really has an L3 header. */ |
6732 | if (!flow->nw_proto) { | |
6733 | return; | |
6734 | } | |
6735 | ||
d23df9a8 | 6736 | if (!is_ip_any(base_flow)) { |
5bbda0aa EJ |
6737 | return; |
6738 | } | |
6739 | ||
5bbda0aa | 6740 | if (flow->nw_proto == IPPROTO_TCP) { |
d23df9a8 | 6741 | key_type = OVS_KEY_ATTR_TCP; |
5bbda0aa | 6742 | } else if (flow->nw_proto == IPPROTO_UDP) { |
d23df9a8 | 6743 | key_type = OVS_KEY_ATTR_UDP; |
c6bcb685 | 6744 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
d23df9a8 JR |
6745 | key_type = OVS_KEY_ATTR_SCTP; |
6746 | } else { | |
6747 | return; | |
6748 | } | |
c6bcb685 | 6749 | |
d23df9a8 JR |
6750 | get_tp_key(flow, &key); |
6751 | get_tp_key(base_flow, &base); | |
6752 | get_tp_key(&wc->masks, &mask); | |
c6bcb685 | 6753 | |
d23df9a8 JR |
6754 | if (commit(key_type, use_masked, &key, &base, &mask, sizeof key, |
6755 | odp_actions)) { | |
6756 | put_tp_key(&base, base_flow); | |
6757 | put_tp_key(&mask, &wc->masks); | |
5bbda0aa EJ |
6758 | } |
6759 | } | |
6760 | ||
6761 | static void | |
d23df9a8 | 6762 | commit_set_priority_action(const struct flow *flow, struct flow *base_flow, |
1dd35f8a | 6763 | struct ofpbuf *odp_actions, |
d23df9a8 JR |
6764 | struct flow_wildcards *wc, |
6765 | bool use_masked) | |
5bbda0aa | 6766 | { |
d23df9a8 | 6767 | uint32_t key, mask, base; |
1dd35f8a | 6768 | |
d23df9a8 JR |
6769 | key = flow->skb_priority; |
6770 | base = base_flow->skb_priority; | |
6771 | mask = wc->masks.skb_priority; | |
5bbda0aa | 6772 | |
d23df9a8 JR |
6773 | if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask, |
6774 | sizeof key, odp_actions)) { | |
6775 | base_flow->skb_priority = base; | |
6776 | wc->masks.skb_priority = mask; | |
6777 | } | |
5bbda0aa EJ |
6778 | } |
6779 | ||
72e8bf28 | 6780 | static void |
d23df9a8 | 6781 | commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow, |
1dd35f8a | 6782 | struct ofpbuf *odp_actions, |
d23df9a8 JR |
6783 | struct flow_wildcards *wc, |
6784 | bool use_masked) | |
72e8bf28 | 6785 | { |
d23df9a8 | 6786 | uint32_t key, mask, base; |
1dd35f8a | 6787 | |
d23df9a8 JR |
6788 | key = flow->pkt_mark; |
6789 | base = base_flow->pkt_mark; | |
6790 | mask = wc->masks.pkt_mark; | |
72e8bf28 | 6791 | |
d23df9a8 JR |
6792 | if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask, |
6793 | sizeof key, odp_actions)) { | |
6794 | base_flow->pkt_mark = base; | |
6795 | wc->masks.pkt_mark = mask; | |
6796 | } | |
72e8bf28 | 6797 | } |
7fd91025 | 6798 | |
1fc11c59 JS |
6799 | static void |
6800 | odp_put_decap_nsh_action(struct ofpbuf *odp_actions) | |
6801 | { | |
6802 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_DECAP_NSH); | |
6803 | } | |
6804 | ||
6805 | static void | |
6806 | odp_put_encap_nsh_action(struct ofpbuf *odp_actions, | |
6807 | const struct flow *flow, | |
6808 | struct ofpbuf *encap_data) | |
6809 | { | |
6810 | struct ovs_action_encap_nsh encap_nsh; | |
6811 | ||
6812 | encap_nsh.flags = flow->nsh.flags; | |
6813 | encap_nsh.mdtype = flow->nsh.mdtype; | |
6814 | encap_nsh.np = flow->nsh.np; | |
6815 | encap_nsh.path_hdr = htonl((ntohl(flow->nsh.spi) << NSH_SPI_SHIFT) | | |
6816 | flow->nsh.si); | |
6817 | ||
6818 | switch (encap_nsh.mdtype) { | |
6819 | case NSH_M_TYPE1: { | |
6820 | struct nsh_md1_ctx *md1 = | |
6821 | ALIGNED_CAST(struct nsh_md1_ctx *, encap_nsh.metadata); | |
6822 | encap_nsh.mdlen = NSH_M_TYPE1_MDLEN; | |
6823 | for (int i = 0; i < 4; i++) { | |
6824 | put_16aligned_be32(&md1->c[i], flow->nsh.c[i]); | |
6825 | } | |
6826 | break; | |
6827 | } | |
6828 | case NSH_M_TYPE2: | |
6829 | if (encap_data) { | |
6830 | ovs_assert(encap_data->size < OVS_ENCAP_NSH_MAX_MD_LEN); | |
6831 | encap_nsh.mdlen = encap_data->size; | |
6832 | memcpy(encap_nsh.metadata, encap_data->data, encap_data->size); | |
6833 | } else { | |
6834 | encap_nsh.mdlen = 0; | |
6835 | } | |
6836 | break; | |
6837 | default: | |
6838 | encap_nsh.mdlen = 0; | |
6839 | break; | |
6840 | } | |
6841 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_ENCAP_NSH, | |
6842 | &encap_nsh, sizeof(encap_nsh)); | |
6843 | } | |
6844 | ||
f839892a JS |
6845 | static void |
6846 | commit_packet_type_change(const struct flow *flow, | |
6847 | struct flow *base_flow, | |
6848 | struct ofpbuf *odp_actions, | |
6849 | struct flow_wildcards *wc, | |
1fc11c59 JS |
6850 | bool pending_encap, |
6851 | struct ofpbuf *encap_data) | |
f839892a JS |
6852 | { |
6853 | if (flow->packet_type == base_flow->packet_type) { | |
6854 | return; | |
6855 | } | |
6856 | ||
6857 | if (pending_encap) { | |
6858 | switch (ntohl(flow->packet_type)) { | |
6859 | case PT_ETH: { | |
6860 | /* push_eth */ | |
6861 | odp_put_push_eth_action(odp_actions, &flow->dl_src, | |
6862 | &flow->dl_dst); | |
6863 | base_flow->packet_type = flow->packet_type; | |
6864 | base_flow->dl_src = flow->dl_src; | |
6865 | base_flow->dl_dst = flow->dl_dst; | |
6866 | break; | |
6867 | } | |
1fc11c59 JS |
6868 | case PT_NSH: |
6869 | /* encap_nsh */ | |
6870 | odp_put_encap_nsh_action(odp_actions, flow, encap_data); | |
6871 | base_flow->packet_type = flow->packet_type; | |
6872 | /* Update all packet headers in base_flow. */ | |
6873 | memcpy(&base_flow->dl_dst, &flow->dl_dst, | |
6874 | sizeof(*flow) - offsetof(struct flow, dl_dst)); | |
6875 | break; | |
f839892a | 6876 | default: |
1fc11c59 JS |
6877 | /* Only the above protocols are supported for encap. |
6878 | * The check is done at action translation. */ | |
f839892a JS |
6879 | OVS_NOT_REACHED(); |
6880 | } | |
6881 | } else { | |
1fc11c59 | 6882 | /* This is an explicit or implicit decap case. */ |
f839892a JS |
6883 | if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE && |
6884 | base_flow->packet_type == htonl(PT_ETH)) { | |
1fc11c59 | 6885 | /* Generate pop_eth and continue without recirculation. */ |
f839892a JS |
6886 | odp_put_pop_eth_action(odp_actions); |
6887 | base_flow->packet_type = flow->packet_type; | |
6888 | base_flow->dl_src = eth_addr_zero; | |
6889 | base_flow->dl_dst = eth_addr_zero; | |
6890 | } else { | |
1fc11c59 JS |
6891 | /* All other decap cases require recirculation. |
6892 | * No need to update the base flow here. */ | |
6893 | switch (ntohl(base_flow->packet_type)) { | |
6894 | case PT_NSH: | |
6895 | /* decap_nsh. */ | |
6896 | odp_put_decap_nsh_action(odp_actions); | |
6897 | break; | |
6898 | default: | |
6899 | /* Checks are done during translation. */ | |
6900 | OVS_NOT_REACHED(); | |
6901 | } | |
f839892a JS |
6902 | } |
6903 | } | |
6904 | ||
6905 | wc->masks.packet_type = OVS_BE32_MAX; | |
6906 | } | |
6907 | ||
5bbda0aa EJ |
6908 | /* If any of the flow key data that ODP actions can modify are different in |
6909 | * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow | |
b9ad7294 EJ |
6910 | * key from 'base' into 'flow', and then changes 'base' the same way. Does not |
6911 | * commit set_tunnel actions. Users should call commit_odp_tunnel_action() | |
1dd35f8a | 6912 | * in addition to this function if needed. Sets fields in 'wc' that are |
7fd91025 BP |
6913 | * used as part of the action. |
6914 | * | |
6915 | * Returns a reason to force processing the flow's packets into the userspace | |
6916 | * slow path, if there is one, otherwise 0. */ | |
6917 | enum slow_path_reason | |
5bbda0aa | 6918 | commit_odp_actions(const struct flow *flow, struct flow *base, |
d23df9a8 | 6919 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
1fc11c59 JS |
6920 | bool use_masked, bool pending_encap, |
6921 | struct ofpbuf *encap_data) | |
5bbda0aa | 6922 | { |
f6ecf944 | 6923 | enum slow_path_reason slow1, slow2; |
742c0ac3 | 6924 | bool mpls_done = false; |
f6c8a6b1 | 6925 | |
1fc11c59 JS |
6926 | commit_packet_type_change(flow, base, odp_actions, wc, |
6927 | pending_encap, encap_data); | |
f839892a | 6928 | commit_set_ether_action(flow, base, odp_actions, wc, use_masked); |
742c0ac3 JR |
6929 | /* Make packet a non-MPLS packet before committing L3/4 actions, |
6930 | * which would otherwise do nothing. */ | |
6931 | if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) { | |
6932 | commit_mpls_action(flow, base, odp_actions); | |
6933 | mpls_done = true; | |
6934 | } | |
3d2fbd70 | 6935 | commit_set_nsh_action(flow, base, odp_actions, wc, use_masked); |
f6ecf944 | 6936 | slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked); |
d23df9a8 | 6937 | commit_set_port_action(flow, base, odp_actions, wc, use_masked); |
f6ecf944 | 6938 | slow2 = commit_set_icmp_action(flow, base, odp_actions, wc); |
742c0ac3 JR |
6939 | if (!mpls_done) { |
6940 | commit_mpls_action(flow, base, odp_actions); | |
6941 | } | |
f0fb825a | 6942 | commit_vlan_action(flow, base, odp_actions, wc); |
d23df9a8 JR |
6943 | commit_set_priority_action(flow, base, odp_actions, wc, use_masked); |
6944 | commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked); | |
7fd91025 | 6945 | |
f6ecf944 | 6946 | return slow1 ? slow1 : slow2; |
5bbda0aa | 6947 | } |