]>
Commit | Line | Data |
---|---|---|
09246b99 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2010, 2011, 2012 Nicira, Inc. |
09246b99 BP |
3 | * |
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: | |
7 | * | |
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. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "nx-match.h" | |
20 | ||
685a51a5 JP |
21 | #include <netinet/icmp6.h> |
22 | ||
09246b99 BP |
23 | #include "classifier.h" |
24 | #include "dynamic-string.h" | |
6a885fd0 | 25 | #include "meta-flow.h" |
f25d0cf3 | 26 | #include "ofp-actions.h" |
90bf1e07 | 27 | #include "ofp-errors.h" |
09246b99 BP |
28 | #include "ofp-util.h" |
29 | #include "ofpbuf.h" | |
30 | #include "openflow/nicira-ext.h" | |
31 | #include "packets.h" | |
32 | #include "unaligned.h" | |
ddc4f8e2 | 33 | #include "util.h" |
09246b99 BP |
34 | #include "vlog.h" |
35 | ||
36 | VLOG_DEFINE_THIS_MODULE(nx_match); | |
37 | ||
38 | /* Rate limit for nx_match parse errors. These always indicate a bug in the | |
39 | * peer and so there's not much point in showing a lot of them. */ | |
40 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
41 | ||
09246b99 BP |
42 | /* Returns the width of the data for a field with the given 'header', in |
43 | * bytes. */ | |
4291acd2 | 44 | int |
09246b99 BP |
45 | nxm_field_bytes(uint32_t header) |
46 | { | |
47 | unsigned int length = NXM_LENGTH(header); | |
48 | return NXM_HASMASK(header) ? length / 2 : length; | |
49 | } | |
b6c9e612 BP |
50 | |
51 | /* Returns the width of the data for a field with the given 'header', in | |
52 | * bits. */ | |
4291acd2 | 53 | int |
b6c9e612 BP |
54 | nxm_field_bits(uint32_t header) |
55 | { | |
56 | return nxm_field_bytes(header) * 8; | |
57 | } | |
6a885fd0 BP |
58 | \f |
59 | /* nx_pull_match() and helpers. */ | |
09246b99 BP |
60 | |
61 | static uint32_t | |
62 | nx_entry_ok(const void *p, unsigned int match_len) | |
63 | { | |
64 | unsigned int payload_len; | |
65 | ovs_be32 header_be; | |
66 | uint32_t header; | |
67 | ||
68 | if (match_len < 4) { | |
69 | if (match_len) { | |
74f868c6 BP |
70 | VLOG_DBG_RL(&rl, "nx_match ends with partial (%u-byte) nxm_header", |
71 | match_len); | |
09246b99 BP |
72 | } |
73 | return 0; | |
74 | } | |
75 | memcpy(&header_be, p, 4); | |
76 | header = ntohl(header_be); | |
77 | ||
78 | payload_len = NXM_LENGTH(header); | |
79 | if (!payload_len) { | |
80 | VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload " | |
81 | "length 0", header); | |
82 | return 0; | |
83 | } | |
84 | if (match_len < payload_len + 4) { | |
85 | VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only " | |
86 | "%u bytes left in nx_match", payload_len + 4, match_len); | |
87 | return 0; | |
88 | } | |
89 | ||
90 | return header; | |
91 | } | |
92 | ||
90bf1e07 | 93 | static enum ofperr |
7623f4dd SH |
94 | nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict, |
95 | uint16_t priority, struct cls_rule *rule, | |
96 | ovs_be64 *cookie, ovs_be64 *cookie_mask) | |
09246b99 BP |
97 | { |
98 | uint32_t header; | |
09246b99 | 99 | |
e729e793 JP |
100 | assert((cookie != NULL) == (cookie_mask != NULL)); |
101 | ||
a3a0c29e BP |
102 | cls_rule_init_catchall(rule, priority); |
103 | if (cookie) { | |
104 | *cookie = *cookie_mask = htonll(0); | |
105 | } | |
106 | if (!match_len) { | |
107 | return 0; | |
108 | } | |
109 | ||
102ce766 EJ |
110 | for (; |
111 | (header = nx_entry_ok(p, match_len)) != 0; | |
112 | p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) { | |
28da1f8f | 113 | const struct mf_field *mf; |
90bf1e07 | 114 | enum ofperr error; |
09246b99 | 115 | |
28da1f8f BP |
116 | mf = mf_from_nxm_header(header); |
117 | if (!mf) { | |
102ce766 | 118 | if (strict) { |
2e0525bc | 119 | error = OFPERR_OFPBMC_BAD_FIELD; |
102ce766 EJ |
120 | } else { |
121 | continue; | |
122 | } | |
28da1f8f | 123 | } else if (!mf_are_prereqs_ok(mf, &rule->flow)) { |
2e0525bc | 124 | error = OFPERR_OFPBMC_BAD_PREREQ; |
28da1f8f | 125 | } else if (!mf_is_all_wild(mf, &rule->wc)) { |
2e0525bc | 126 | error = OFPERR_OFPBMC_DUP_FIELD; |
b5ae8913 | 127 | } else if (header != OXM_OF_IN_PORT) { |
28da1f8f | 128 | unsigned int width = mf->n_bytes; |
6a885fd0 BP |
129 | union mf_value value; |
130 | ||
131 | memcpy(&value, p + 4, width); | |
28da1f8f | 132 | if (!mf_is_value_valid(mf, &value)) { |
2e0525bc | 133 | error = OFPERR_OFPBMC_BAD_VALUE; |
6a885fd0 BP |
134 | } else if (!NXM_HASMASK(header)) { |
135 | error = 0; | |
28da1f8f | 136 | mf_set_value(mf, &value, rule); |
6a885fd0 BP |
137 | } else { |
138 | union mf_value mask; | |
139 | ||
140 | memcpy(&mask, p + 4 + width, width); | |
28da1f8f | 141 | if (!mf_is_mask_valid(mf, &mask)) { |
2e0525bc | 142 | error = OFPERR_OFPBMC_BAD_MASK; |
6a885fd0 BP |
143 | } else { |
144 | error = 0; | |
28da1f8f | 145 | mf_set(mf, &value, &mask, rule); |
6a885fd0 BP |
146 | } |
147 | } | |
b5ae8913 SH |
148 | } else { |
149 | /* Special case for 32bit ports when using OXM, | |
150 | * ports are 16 bits wide otherwise. */ | |
151 | ovs_be32 port_of11; | |
152 | uint16_t port; | |
153 | ||
154 | memcpy(&port_of11, p + 4, sizeof port_of11); | |
155 | error = ofputil_port_from_ofp11(port_of11, &port); | |
156 | if (!error) { | |
157 | cls_rule_set_in_port(rule, port); | |
158 | } | |
09246b99 | 159 | } |
6a885fd0 | 160 | |
e729e793 JP |
161 | /* Check if the match is for a cookie rather than a classifier rule. */ |
162 | if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) { | |
163 | if (*cookie_mask) { | |
2e0525bc | 164 | error = OFPERR_OFPBMC_DUP_FIELD; |
e729e793 JP |
165 | } else { |
166 | unsigned int width = sizeof *cookie; | |
167 | ||
168 | memcpy(cookie, p + 4, width); | |
169 | if (NXM_HASMASK(header)) { | |
170 | memcpy(cookie_mask, p + 4 + width, width); | |
171 | } else { | |
172 | *cookie_mask = htonll(UINT64_MAX); | |
173 | } | |
174 | error = 0; | |
175 | } | |
176 | } | |
177 | ||
09246b99 | 178 | if (error) { |
28da1f8f BP |
179 | VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", " |
180 | "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), " | |
181 | "(%s)", header, | |
09246b99 | 182 | NXM_VENDOR(header), NXM_FIELD(header), |
28da1f8f | 183 | NXM_HASMASK(header), NXM_LENGTH(header), |
90bf1e07 | 184 | ofperr_to_string(error)); |
09246b99 BP |
185 | return error; |
186 | } | |
09246b99 BP |
187 | } |
188 | ||
2e0525bc | 189 | return match_len ? OFPERR_OFPBMC_BAD_LEN : 0; |
09246b99 | 190 | } |
102ce766 | 191 | |
7623f4dd SH |
192 | static enum ofperr |
193 | nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict, | |
194 | uint16_t priority, struct cls_rule *rule, | |
195 | ovs_be64 *cookie, ovs_be64 *cookie_mask) | |
196 | { | |
197 | uint8_t *p = NULL; | |
198 | ||
199 | if (match_len) { | |
200 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
201 | if (!p) { | |
202 | VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a " | |
203 | "multiple of 8, is longer than space in message (max " | |
204 | "length %zu)", match_len, b->size); | |
205 | return OFPERR_OFPBMC_BAD_LEN; | |
206 | } | |
207 | } | |
208 | ||
209 | return nx_pull_raw(p, match_len, strict, priority, rule, | |
210 | cookie, cookie_mask); | |
211 | } | |
212 | ||
102ce766 EJ |
213 | /* Parses the nx_match formatted match description in 'b' with length |
214 | * 'match_len'. The results are stored in 'rule', which is initialized with | |
215 | * 'priority'. If 'cookie' and 'cookie_mask' contain valid pointers, then the | |
216 | * cookie and mask will be stored in them if a "NXM_NX_COOKIE*" match is | |
217 | * defined. Otherwise, 0 is stored in both. | |
218 | * | |
219 | * Fails with an error when encountering unknown NXM headers. | |
220 | * | |
221 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 222 | enum ofperr |
102ce766 EJ |
223 | nx_pull_match(struct ofpbuf *b, unsigned int match_len, |
224 | uint16_t priority, struct cls_rule *rule, | |
225 | ovs_be64 *cookie, ovs_be64 *cookie_mask) | |
226 | { | |
227 | return nx_pull_match__(b, match_len, true, priority, rule, cookie, | |
228 | cookie_mask); | |
229 | } | |
230 | ||
231 | /* Behaves the same as nx_pull_match() with one exception. Skips over unknown | |
232 | * NXM headers instead of failing with an error when they are encountered. */ | |
90bf1e07 | 233 | enum ofperr |
102ce766 EJ |
234 | nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len, |
235 | uint16_t priority, struct cls_rule *rule, | |
236 | ovs_be64 *cookie, ovs_be64 *cookie_mask) | |
237 | { | |
238 | return nx_pull_match__(b, match_len, false, priority, rule, cookie, | |
239 | cookie_mask); | |
240 | } | |
7623f4dd SH |
241 | |
242 | static enum ofperr | |
243 | oxm_pull_match__(struct ofpbuf *b, bool strict, | |
244 | uint16_t priority, struct cls_rule *rule) | |
245 | { | |
246 | struct ofp11_match_header *omh = b->data; | |
247 | uint8_t *p; | |
248 | uint16_t match_len; | |
249 | ||
250 | if (b->size < sizeof *omh) { | |
251 | return OFPERR_OFPBMC_BAD_LEN; | |
252 | } | |
253 | ||
254 | match_len = ntohs(omh->length); | |
255 | if (match_len < sizeof *omh) { | |
256 | return OFPERR_OFPBMC_BAD_LEN; | |
257 | } | |
258 | ||
259 | if (omh->type != htons(OFPMT_OXM)) { | |
260 | return OFPERR_OFPBMC_BAD_TYPE; | |
261 | } | |
262 | ||
263 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
264 | if (!p) { | |
265 | VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a " | |
266 | "multiple of 8, is longer than space in message (max " | |
267 | "length %zu)", match_len, b->size); | |
268 | return OFPERR_OFPBMC_BAD_LEN; | |
269 | } | |
270 | ||
271 | return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh, | |
272 | strict, priority, rule, NULL, NULL); | |
273 | } | |
274 | ||
275 | /* Parses the oxm formatted match description preceeded by a struct | |
276 | * ofp11_match in 'b' with length 'match_len'. The results are stored in | |
277 | * 'rule', which is initialized with 'priority'. | |
278 | * | |
279 | * Fails with an error when encountering unknown OXM headers. | |
280 | * | |
281 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
282 | enum ofperr | |
283 | oxm_pull_match(struct ofpbuf *b, uint16_t priority, struct cls_rule *rule) | |
284 | { | |
285 | return oxm_pull_match__(b, true, priority, rule); | |
286 | } | |
287 | ||
288 | /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown | |
289 | * PXM headers instead of failing with an error when they are encountered. */ | |
290 | enum ofperr | |
291 | oxm_pull_match_loose(struct ofpbuf *b, uint16_t priority, | |
292 | struct cls_rule *rule) | |
293 | { | |
294 | return oxm_pull_match__(b, false, priority, rule); | |
295 | } | |
09246b99 BP |
296 | \f |
297 | /* nx_put_match() and helpers. | |
298 | * | |
299 | * 'put' functions whose names end in 'w' add a wildcarded field. | |
300 | * 'put' functions whose names end in 'm' add a field that might be wildcarded. | |
301 | * Other 'put' functions add exact-match fields. | |
302 | */ | |
303 | ||
304 | static void | |
305 | nxm_put_header(struct ofpbuf *b, uint32_t header) | |
306 | { | |
307 | ovs_be32 n_header = htonl(header); | |
308 | ofpbuf_put(b, &n_header, sizeof n_header); | |
309 | } | |
310 | ||
311 | static void | |
312 | nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value) | |
313 | { | |
314 | nxm_put_header(b, header); | |
315 | ofpbuf_put(b, &value, sizeof value); | |
316 | } | |
317 | ||
7257b535 BP |
318 | static void |
319 | nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask) | |
320 | { | |
321 | switch (mask) { | |
322 | case 0: | |
323 | break; | |
324 | ||
325 | case UINT8_MAX: | |
326 | nxm_put_8(b, header, value); | |
327 | break; | |
328 | ||
329 | default: | |
330 | nxm_put_header(b, NXM_MAKE_WILD_HEADER(header)); | |
331 | ofpbuf_put(b, &value, sizeof value); | |
332 | ofpbuf_put(b, &mask, sizeof mask); | |
333 | } | |
334 | } | |
335 | ||
09246b99 BP |
336 | static void |
337 | nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value) | |
338 | { | |
339 | nxm_put_header(b, header); | |
340 | ofpbuf_put(b, &value, sizeof value); | |
341 | } | |
342 | ||
343 | static void | |
344 | nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask) | |
345 | { | |
346 | nxm_put_header(b, header); | |
347 | ofpbuf_put(b, &value, sizeof value); | |
348 | ofpbuf_put(b, &mask, sizeof mask); | |
349 | } | |
350 | ||
66642cb4 BP |
351 | static void |
352 | nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask) | |
353 | { | |
354 | switch (mask) { | |
355 | case 0: | |
356 | break; | |
357 | ||
358 | case CONSTANT_HTONS(UINT16_MAX): | |
359 | nxm_put_16(b, header, value); | |
360 | break; | |
361 | ||
362 | default: | |
363 | nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask); | |
364 | break; | |
365 | } | |
366 | } | |
367 | ||
09246b99 BP |
368 | static void |
369 | nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value) | |
370 | { | |
371 | nxm_put_header(b, header); | |
372 | ofpbuf_put(b, &value, sizeof value); | |
373 | } | |
374 | ||
375 | static void | |
376 | nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask) | |
377 | { | |
378 | nxm_put_header(b, header); | |
379 | ofpbuf_put(b, &value, sizeof value); | |
380 | ofpbuf_put(b, &mask, sizeof mask); | |
381 | } | |
382 | ||
383 | static void | |
384 | nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask) | |
385 | { | |
386 | switch (mask) { | |
387 | case 0: | |
388 | break; | |
389 | ||
66642cb4 | 390 | case CONSTANT_HTONL(UINT32_MAX): |
09246b99 BP |
391 | nxm_put_32(b, header, value); |
392 | break; | |
393 | ||
394 | default: | |
395 | nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask); | |
396 | break; | |
397 | } | |
398 | } | |
399 | ||
400 | static void | |
401 | nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value) | |
402 | { | |
403 | nxm_put_header(b, header); | |
404 | ofpbuf_put(b, &value, sizeof value); | |
405 | } | |
406 | ||
8368c090 BP |
407 | static void |
408 | nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask) | |
409 | { | |
410 | nxm_put_header(b, header); | |
411 | ofpbuf_put(b, &value, sizeof value); | |
412 | ofpbuf_put(b, &mask, sizeof mask); | |
413 | } | |
414 | ||
415 | static void | |
416 | nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask) | |
417 | { | |
418 | switch (mask) { | |
419 | case 0: | |
420 | break; | |
421 | ||
422 | case CONSTANT_HTONLL(UINT64_MAX): | |
423 | nxm_put_64(b, header, value); | |
424 | break; | |
425 | ||
426 | default: | |
427 | nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask); | |
428 | break; | |
429 | } | |
430 | } | |
431 | ||
09246b99 BP |
432 | static void |
433 | nxm_put_eth(struct ofpbuf *b, uint32_t header, | |
434 | const uint8_t value[ETH_ADDR_LEN]) | |
435 | { | |
436 | nxm_put_header(b, header); | |
437 | ofpbuf_put(b, value, ETH_ADDR_LEN); | |
438 | } | |
439 | ||
1e37a2d7 | 440 | static void |
73c0ce34 JS |
441 | nxm_put_eth_masked(struct ofpbuf *b, uint32_t header, |
442 | const uint8_t value[ETH_ADDR_LEN], | |
443 | const uint8_t mask[ETH_ADDR_LEN]) | |
1e37a2d7 | 444 | { |
73c0ce34 JS |
445 | if (!eth_addr_is_zero(mask)) { |
446 | if (eth_mask_is_exact(mask)) { | |
447 | nxm_put_eth(b, header, value); | |
448 | } else { | |
449 | nxm_put_header(b, NXM_MAKE_WILD_HEADER(header)); | |
450 | ofpbuf_put(b, value, ETH_ADDR_LEN); | |
451 | ofpbuf_put(b, mask, ETH_ADDR_LEN); | |
452 | } | |
1e37a2d7 BP |
453 | } |
454 | } | |
455 | ||
d31f1109 JP |
456 | static void |
457 | nxm_put_ipv6(struct ofpbuf *b, uint32_t header, | |
458 | const struct in6_addr *value, const struct in6_addr *mask) | |
459 | { | |
460 | if (ipv6_mask_is_any(mask)) { | |
461 | return; | |
462 | } else if (ipv6_mask_is_exact(mask)) { | |
463 | nxm_put_header(b, header); | |
464 | ofpbuf_put(b, value, sizeof *value); | |
465 | } else { | |
466 | nxm_put_header(b, NXM_MAKE_WILD_HEADER(header)); | |
467 | ofpbuf_put(b, value, sizeof *value); | |
468 | ofpbuf_put(b, mask, sizeof *mask); | |
469 | } | |
470 | } | |
471 | ||
7257b535 | 472 | static void |
9e44d715 | 473 | nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr) |
7257b535 | 474 | { |
eadef313 JP |
475 | uint8_t nw_frag = cr->flow.nw_frag; |
476 | uint8_t nw_frag_mask = cr->wc.nw_frag_mask; | |
7257b535 | 477 | |
eadef313 | 478 | switch (nw_frag_mask) { |
7257b535 BP |
479 | case 0: |
480 | break; | |
481 | ||
eadef313 JP |
482 | case FLOW_NW_FRAG_MASK: |
483 | nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag); | |
7257b535 BP |
484 | break; |
485 | ||
486 | default: | |
eadef313 JP |
487 | nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag, |
488 | nw_frag_mask & FLOW_NW_FRAG_MASK); | |
7257b535 BP |
489 | break; |
490 | } | |
491 | } | |
492 | ||
8e7082b0 BP |
493 | static void |
494 | nxm_put_ip(struct ofpbuf *b, const struct cls_rule *cr, | |
b5ae8913 SH |
495 | uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code, |
496 | bool oxm) | |
8e7082b0 | 497 | { |
8e7082b0 BP |
498 | const struct flow *flow = &cr->flow; |
499 | ||
500 | nxm_put_frag(b, cr); | |
501 | ||
5d9499c4 | 502 | if (cr->wc.nw_tos_mask & IP_DSCP_MASK) { |
b5ae8913 SH |
503 | nxm_put_8(b, oxm ? OXM_OF_IP_DSCP : NXM_OF_IP_TOS, |
504 | flow->nw_tos & IP_DSCP_MASK); | |
8e7082b0 BP |
505 | } |
506 | ||
5d9499c4 | 507 | if (cr->wc.nw_tos_mask & IP_ECN_MASK) { |
b5ae8913 SH |
508 | nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN, |
509 | flow->nw_tos & IP_ECN_MASK); | |
8e7082b0 BP |
510 | } |
511 | ||
3840c406 | 512 | if (!oxm && cr->wc.nw_ttl_mask) { |
8e7082b0 BP |
513 | nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl); |
514 | } | |
515 | ||
851d3105 | 516 | if (cr->wc.nw_proto_mask) { |
b5ae8913 | 517 | nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto); |
8e7082b0 BP |
518 | |
519 | if (flow->nw_proto == IPPROTO_TCP) { | |
b5ae8913 SH |
520 | nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC, |
521 | flow->tp_src, cr->wc.tp_src_mask); | |
522 | nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST, | |
523 | flow->tp_dst, cr->wc.tp_dst_mask); | |
8e7082b0 | 524 | } else if (flow->nw_proto == IPPROTO_UDP) { |
b5ae8913 SH |
525 | nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC, |
526 | flow->tp_src, cr->wc.tp_src_mask); | |
527 | nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST, | |
528 | flow->tp_dst, cr->wc.tp_dst_mask); | |
8e7082b0 | 529 | } else if (flow->nw_proto == icmp_proto) { |
73f33563 | 530 | if (cr->wc.tp_src_mask) { |
8e7082b0 BP |
531 | nxm_put_8(b, icmp_type, ntohs(flow->tp_src)); |
532 | } | |
73f33563 | 533 | if (cr->wc.tp_dst_mask) { |
8e7082b0 BP |
534 | nxm_put_8(b, icmp_code, ntohs(flow->tp_dst)); |
535 | } | |
536 | } | |
537 | } | |
538 | } | |
539 | ||
4d0ed519 | 540 | /* Appends to 'b' the nx_match format that expresses 'cr' (except for |
7623f4dd SH |
541 | * 'cr->priority', because priority is not part of nx_match). For Flow Mod and |
542 | * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. | |
543 | * Otherwise, 'cookie_mask' should be zero. | |
4d0ed519 BP |
544 | * |
545 | * This function can cause 'b''s data to be reallocated. | |
546 | * | |
547 | * Returns the number of bytes appended to 'b', excluding padding. | |
548 | * | |
549 | * If 'cr' is a catch-all rule that matches every packet, then this function | |
550 | * appends nothing to 'b' and returns 0. */ | |
7623f4dd SH |
551 | static int |
552 | nx_put_raw(struct ofpbuf *b, bool oxm, const struct cls_rule *cr, | |
553 | ovs_be64 cookie, ovs_be64 cookie_mask) | |
09246b99 | 554 | { |
d8ae4d67 | 555 | const flow_wildcards_t wc = cr->wc.wildcards; |
09246b99 BP |
556 | const struct flow *flow = &cr->flow; |
557 | const size_t start_len = b->size; | |
09246b99 | 558 | int match_len; |
b6c9e612 | 559 | int i; |
09246b99 | 560 | |
851d3105 | 561 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 16); |
a877206f | 562 | |
09246b99 | 563 | /* Metadata. */ |
d8ae4d67 | 564 | if (!(wc & FWW_IN_PORT)) { |
09246b99 | 565 | uint16_t in_port = flow->in_port; |
b5ae8913 SH |
566 | if (oxm) { |
567 | nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port)); | |
568 | } else { | |
569 | nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port)); | |
570 | } | |
09246b99 BP |
571 | } |
572 | ||
573 | /* Ethernet. */ | |
b5ae8913 SH |
574 | nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC, |
575 | flow->dl_src, cr->wc.dl_src_mask); | |
576 | nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST, | |
577 | flow->dl_dst, cr->wc.dl_dst_mask); | |
d8ae4d67 | 578 | if (!(wc & FWW_DL_TYPE)) { |
b5ae8913 | 579 | nxm_put_16(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE, |
36956a7d | 580 | ofputil_dl_type_to_openflow(flow->dl_type)); |
09246b99 BP |
581 | } |
582 | ||
95f61ba8 SH |
583 | /* 802.1Q. */ |
584 | if (oxm) { | |
585 | ovs_be16 vid = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI); | |
586 | ovs_be16 mask = cr->wc.vlan_tci_mask & htons(VLAN_VID_MASK | VLAN_CFI); | |
587 | ||
588 | if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) { | |
589 | nxm_put_16(b, OXM_OF_VLAN_VID, vid); | |
590 | } else if (mask) { | |
591 | nxm_put_16m(b, OXM_OF_VLAN_VID, vid, mask); | |
592 | } | |
593 | ||
594 | if (vid && vlan_tci_to_pcp(cr->wc.vlan_tci_mask)) { | |
595 | nxm_put_8(b, OXM_OF_VLAN_PCP, vlan_tci_to_pcp(flow->vlan_tci)); | |
596 | } | |
597 | ||
598 | } else { | |
599 | nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask); | |
600 | } | |
09246b99 | 601 | |
66642cb4 | 602 | /* L3. */ |
d8ae4d67 | 603 | if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) { |
09246b99 | 604 | /* IP. */ |
b5ae8913 SH |
605 | nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC, |
606 | flow->nw_src, cr->wc.nw_src_mask); | |
607 | nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST, | |
608 | flow->nw_dst, cr->wc.nw_dst_mask); | |
609 | nxm_put_ip(b, cr, IPPROTO_ICMP, | |
610 | oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE, | |
611 | oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm); | |
d31f1109 JP |
612 | } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) { |
613 | /* IPv6. */ | |
b5ae8913 SH |
614 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC, |
615 | &flow->ipv6_src, &cr->wc.ipv6_src_mask); | |
616 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST, | |
617 | &flow->ipv6_dst, &cr->wc.ipv6_dst_mask); | |
618 | nxm_put_ip(b, cr, IPPROTO_ICMPV6, | |
619 | oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE, | |
620 | oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm); | |
9e44d715 | 621 | |
32455024 SH |
622 | nxm_put_32m(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL, |
623 | flow->ipv6_label, cr->wc.ipv6_label_mask); | |
d31f1109 | 624 | |
8e7082b0 BP |
625 | if (flow->nw_proto == IPPROTO_ICMPV6 |
626 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
627 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) { | |
b5ae8913 SH |
628 | nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET, |
629 | &flow->nd_target, &cr->wc.nd_target_mask); | |
e878338b SH |
630 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) { |
631 | nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL, | |
632 | flow->arp_sha, cr->wc.arp_sha_mask); | |
8e7082b0 | 633 | } |
e878338b SH |
634 | if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { |
635 | nxm_put_eth_masked(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL, | |
636 | flow->arp_tha, cr->wc.arp_tha_mask); | |
d31f1109 JP |
637 | } |
638 | } | |
d8ae4d67 | 639 | } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) { |
09246b99 | 640 | /* ARP. */ |
851d3105 | 641 | if (cr->wc.nw_proto_mask) { |
b5ae8913 SH |
642 | nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP, |
643 | htons(flow->nw_proto)); | |
09246b99 | 644 | } |
b5ae8913 SH |
645 | nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA, |
646 | flow->nw_src, cr->wc.nw_src_mask); | |
647 | nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA, | |
648 | flow->nw_dst, cr->wc.nw_dst_mask); | |
e878338b SH |
649 | nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA, |
650 | flow->arp_sha, cr->wc.arp_sha_mask); | |
651 | nxm_put_eth_masked(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA, | |
652 | flow->arp_tha, cr->wc.arp_tha_mask); | |
09246b99 BP |
653 | } |
654 | ||
655 | /* Tunnel ID. */ | |
8368c090 | 656 | nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask); |
09246b99 | 657 | |
b6c9e612 BP |
658 | /* Registers. */ |
659 | for (i = 0; i < FLOW_N_REGS; i++) { | |
660 | nxm_put_32m(b, NXM_NX_REG(i), | |
661 | htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i])); | |
662 | } | |
663 | ||
969fc56c JS |
664 | /* OpenFlow 1.1+ Metadata. */ |
665 | nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, cr->wc.metadata_mask); | |
666 | ||
e729e793 JP |
667 | /* Cookie. */ |
668 | nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask); | |
669 | ||
09246b99 | 670 | match_len = b->size - start_len; |
7623f4dd SH |
671 | return match_len; |
672 | } | |
673 | ||
674 | /* Appends to 'b' the nx_match format that expresses 'cr' (except for | |
675 | * 'cr->priority', because priority is not part of nx_match), plus enough zero | |
676 | * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow | |
677 | * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. | |
678 | * Otherwise, 'cookie_mask' should be zero. | |
679 | * | |
680 | * This function can cause 'b''s data to be reallocated. | |
681 | * | |
682 | * Returns the number of bytes appended to 'b', excluding padding. The return | |
683 | * value can be zero if it appended nothing at all to 'b' (which happens if | |
684 | * 'cr' is a catch-all rule that matches every packet). */ | |
685 | int | |
686 | nx_put_match(struct ofpbuf *b, const struct cls_rule *cr, | |
687 | ovs_be64 cookie, ovs_be64 cookie_mask) | |
688 | { | |
689 | int match_len = nx_put_raw(b, false, cr, cookie, cookie_mask); | |
690 | ||
691 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); | |
692 | return match_len; | |
693 | } | |
694 | ||
695 | ||
696 | /* Appends to 'b' an struct ofp11_match_header followed by the oxm format that | |
697 | * expresses 'cr' (except for 'cr->priority', because priority is not part of | |
698 | * nx_match), plus enough zero bytes to pad the data appended out to a multiple | |
699 | * of 8. | |
700 | * | |
701 | * This function can cause 'b''s data to be reallocated. | |
702 | * | |
703 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
704 | * returns zero. */ | |
705 | int | |
706 | oxm_put_match(struct ofpbuf *b, const struct cls_rule *cr) | |
707 | { | |
708 | int match_len; | |
709 | struct ofp11_match_header *omh; | |
710 | size_t start_len = b->size; | |
711 | ovs_be64 cookie = htonll(0), cookie_mask = htonll(0); | |
712 | ||
713 | ofpbuf_put_uninit(b, sizeof *omh); | |
714 | match_len = nx_put_raw(b, true, cr, cookie, cookie_mask) + sizeof *omh; | |
09246b99 | 715 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); |
7623f4dd SH |
716 | |
717 | omh = (struct ofp11_match_header *)((char *)b->data + start_len); | |
718 | omh->type = htons(OFPMT_OXM); | |
719 | omh->length = htons(match_len); | |
720 | ||
09246b99 BP |
721 | return match_len; |
722 | } | |
723 | \f | |
724 | /* nx_match_to_string() and helpers. */ | |
725 | ||
f393f81e BP |
726 | static void format_nxm_field_name(struct ds *, uint32_t header); |
727 | ||
09246b99 BP |
728 | char * |
729 | nx_match_to_string(const uint8_t *p, unsigned int match_len) | |
730 | { | |
731 | uint32_t header; | |
732 | struct ds s; | |
733 | ||
734 | if (!match_len) { | |
735 | return xstrdup("<any>"); | |
736 | } | |
737 | ||
738 | ds_init(&s); | |
739 | while ((header = nx_entry_ok(p, match_len)) != 0) { | |
740 | unsigned int length = NXM_LENGTH(header); | |
741 | unsigned int value_len = nxm_field_bytes(header); | |
742 | const uint8_t *value = p + 4; | |
743 | const uint8_t *mask = value + value_len; | |
09246b99 BP |
744 | unsigned int i; |
745 | ||
746 | if (s.length) { | |
747 | ds_put_cstr(&s, ", "); | |
748 | } | |
749 | ||
f393f81e | 750 | format_nxm_field_name(&s, header); |
09246b99 BP |
751 | ds_put_char(&s, '('); |
752 | ||
753 | for (i = 0; i < value_len; i++) { | |
754 | ds_put_format(&s, "%02x", value[i]); | |
755 | } | |
756 | if (NXM_HASMASK(header)) { | |
757 | ds_put_char(&s, '/'); | |
758 | for (i = 0; i < value_len; i++) { | |
759 | ds_put_format(&s, "%02x", mask[i]); | |
760 | } | |
761 | } | |
762 | ds_put_char(&s, ')'); | |
763 | ||
764 | p += 4 + length; | |
765 | match_len -= 4 + length; | |
766 | } | |
767 | ||
768 | if (match_len) { | |
769 | if (s.length) { | |
770 | ds_put_cstr(&s, ", "); | |
771 | } | |
772 | ||
773 | ds_put_format(&s, "<%u invalid bytes>", match_len); | |
774 | } | |
775 | ||
776 | return ds_steal_cstr(&s); | |
777 | } | |
778 | ||
7623f4dd SH |
779 | char * |
780 | oxm_match_to_string(const uint8_t *p, unsigned int match_len) | |
781 | { | |
782 | const struct ofp11_match_header *omh = (struct ofp11_match_header *)p; | |
783 | uint16_t match_len_; | |
784 | struct ds s; | |
785 | ||
786 | ds_init(&s); | |
787 | ||
788 | if (match_len < sizeof *omh) { | |
789 | ds_put_format(&s, "<match too short: %u>", match_len); | |
790 | goto err; | |
791 | } | |
792 | ||
793 | if (omh->type != htons(OFPMT_OXM)) { | |
794 | ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type)); | |
795 | goto err; | |
796 | } | |
797 | ||
798 | match_len_ = ntohs(omh->length); | |
799 | if (match_len_ < sizeof *omh) { | |
800 | ds_put_format(&s, "<match length field too short: %u>", match_len_); | |
801 | goto err; | |
802 | } | |
803 | ||
804 | if (match_len_ != match_len) { | |
805 | ds_put_format(&s, "<match length field incorrect: %u != %u>", | |
806 | match_len_, match_len); | |
807 | goto err; | |
808 | } | |
809 | ||
810 | return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh); | |
811 | ||
812 | err: | |
813 | return ds_steal_cstr(&s); | |
814 | } | |
815 | ||
f393f81e BP |
816 | static void |
817 | format_nxm_field_name(struct ds *s, uint32_t header) | |
818 | { | |
28da1f8f BP |
819 | const struct mf_field *mf = mf_from_nxm_header(header); |
820 | if (mf) { | |
b5ae8913 | 821 | ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name); |
28da1f8f BP |
822 | if (NXM_HASMASK(header)) { |
823 | ds_put_cstr(s, "_W"); | |
824 | } | |
e729e793 JP |
825 | } else if (header == NXM_NX_COOKIE) { |
826 | ds_put_cstr(s, "NXM_NX_COOKIE"); | |
827 | } else if (header == NXM_NX_COOKIE_W) { | |
828 | ds_put_cstr(s, "NXM_NX_COOKIE_W"); | |
f393f81e BP |
829 | } else { |
830 | ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header)); | |
831 | } | |
832 | } | |
833 | ||
558d80cb BP |
834 | static uint32_t |
835 | parse_nxm_field_name(const char *name, int name_len) | |
09246b99 | 836 | { |
28da1f8f BP |
837 | bool wild; |
838 | int i; | |
09246b99 | 839 | |
558d80cb | 840 | /* Check whether it's a field name. */ |
28da1f8f BP |
841 | wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2); |
842 | if (wild) { | |
843 | name_len -= 2; | |
844 | } | |
e729e793 | 845 | |
28da1f8f BP |
846 | for (i = 0; i < MFF_N_IDS; i++) { |
847 | const struct mf_field *mf = mf_from_id(i); | |
b5ae8913 SH |
848 | uint32_t header; |
849 | ||
850 | if (mf->nxm_name && | |
851 | !strncmp(mf->nxm_name, name, name_len) && | |
852 | mf->nxm_name[name_len] == '\0') { | |
853 | header = mf->nxm_header; | |
854 | } else if (mf->oxm_name && | |
855 | !strncmp(mf->oxm_name, name, name_len) && | |
856 | mf->oxm_name[name_len] == '\0') { | |
857 | header = mf->oxm_header; | |
858 | } else { | |
859 | continue; | |
860 | } | |
28da1f8f | 861 | |
b5ae8913 SH |
862 | if (!wild) { |
863 | return header; | |
864 | } else if (mf->maskable != MFM_NONE) { | |
865 | return NXM_MAKE_WILD_HEADER(header); | |
09246b99 BP |
866 | } |
867 | } | |
868 | ||
b5ae8913 SH |
869 | if (!strncmp("NXM_NX_COOKIE", name, name_len) && |
870 | (name_len == strlen("NXM_NX_COOKIE"))) { | |
e729e793 JP |
871 | if (!wild) { |
872 | return NXM_NX_COOKIE; | |
873 | } else { | |
874 | return NXM_NX_COOKIE_W; | |
875 | } | |
876 | } | |
877 | ||
558d80cb BP |
878 | /* Check whether it's a 32-bit field header value as hex. |
879 | * (This isn't ordinarily useful except for testing error behavior.) */ | |
880 | if (name_len == 8) { | |
881 | uint32_t header = hexits_value(name, name_len, NULL); | |
882 | if (header != UINT_MAX) { | |
883 | return header; | |
884 | } | |
885 | } | |
886 | ||
887 | return 0; | |
09246b99 | 888 | } |
09246b99 BP |
889 | \f |
890 | /* nx_match_from_string(). */ | |
891 | ||
7623f4dd SH |
892 | static int |
893 | nx_match_from_string_raw(const char *s, struct ofpbuf *b) | |
09246b99 BP |
894 | { |
895 | const char *full_s = s; | |
896 | const size_t start_len = b->size; | |
09246b99 BP |
897 | |
898 | if (!strcmp(s, "<any>")) { | |
899 | /* Ensure that 'b->data' isn't actually null. */ | |
900 | ofpbuf_prealloc_tailroom(b, 1); | |
901 | return 0; | |
902 | } | |
903 | ||
904 | for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) { | |
558d80cb BP |
905 | const char *name; |
906 | uint32_t header; | |
09246b99 | 907 | int name_len; |
78090f63 | 908 | size_t n; |
09246b99 | 909 | |
558d80cb | 910 | name = s; |
09246b99 BP |
911 | name_len = strcspn(s, "("); |
912 | if (s[name_len] != '(') { | |
913 | ovs_fatal(0, "%s: missing ( at end of nx_match", full_s); | |
914 | } | |
915 | ||
558d80cb BP |
916 | header = parse_nxm_field_name(name, name_len); |
917 | if (!header) { | |
09246b99 BP |
918 | ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s); |
919 | } | |
920 | ||
921 | s += name_len + 1; | |
922 | ||
558d80cb | 923 | nxm_put_header(b, header); |
78090f63 BP |
924 | s = ofpbuf_put_hex(b, s, &n); |
925 | if (n != nxm_field_bytes(header)) { | |
926 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
927 | } | |
558d80cb | 928 | if (NXM_HASMASK(header)) { |
09246b99 BP |
929 | s += strspn(s, " "); |
930 | if (*s != '/') { | |
558d80cb BP |
931 | ovs_fatal(0, "%s: missing / in masked field %.*s", |
932 | full_s, name_len, name); | |
09246b99 | 933 | } |
78090f63 BP |
934 | s = ofpbuf_put_hex(b, s + 1, &n); |
935 | if (n != nxm_field_bytes(header)) { | |
936 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
937 | } | |
09246b99 BP |
938 | } |
939 | ||
940 | s += strspn(s, " "); | |
941 | if (*s != ')') { | |
558d80cb BP |
942 | ovs_fatal(0, "%s: missing ) following field %.*s", |
943 | full_s, name_len, name); | |
09246b99 BP |
944 | } |
945 | s++; | |
946 | } | |
947 | ||
7623f4dd SH |
948 | return b->size - start_len; |
949 | } | |
950 | ||
951 | int | |
952 | nx_match_from_string(const char *s, struct ofpbuf *b) | |
953 | { | |
954 | int match_len = nx_match_from_string_raw(s, b); | |
955 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); | |
956 | return match_len; | |
957 | } | |
958 | ||
959 | int | |
960 | oxm_match_from_string(const char *s, struct ofpbuf *b) | |
961 | { | |
962 | int match_len; | |
963 | struct ofp11_match_header *omh; | |
964 | size_t start_len = b->size; | |
965 | ||
966 | ofpbuf_put_uninit(b, sizeof *omh); | |
967 | match_len = nx_match_from_string_raw(s, b) + sizeof *omh; | |
09246b99 | 968 | ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len); |
7623f4dd SH |
969 | |
970 | omh = (struct ofp11_match_header *)((char *)b->data + start_len); | |
971 | omh->type = htons(OFPMT_OXM); | |
972 | omh->length = htons(match_len); | |
973 | ||
09246b99 BP |
974 | return match_len; |
975 | } | |
b6c9e612 | 976 | \f |
f393f81e | 977 | void |
f25d0cf3 | 978 | nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s) |
f393f81e BP |
979 | { |
980 | const char *full_s = s; | |
f393f81e | 981 | |
f25d0cf3 | 982 | s = mf_parse_subfield(&move->src, s); |
f393f81e BP |
983 | if (strncmp(s, "->", 2)) { |
984 | ovs_fatal(0, "%s: missing `->' following source", full_s); | |
985 | } | |
986 | s += 2; | |
f25d0cf3 | 987 | s = mf_parse_subfield(&move->dst, s); |
f393f81e BP |
988 | if (*s != '\0') { |
989 | ovs_fatal(0, "%s: trailing garbage following destination", full_s); | |
990 | } | |
991 | ||
f25d0cf3 | 992 | if (move->src.n_bits != move->dst.n_bits) { |
f393f81e | 993 | ovs_fatal(0, "%s: source field is %d bits wide but destination is " |
f25d0cf3 BP |
994 | "%d bits wide", full_s, |
995 | move->src.n_bits, move->dst.n_bits); | |
f393f81e | 996 | } |
f393f81e BP |
997 | } |
998 | ||
999 | void | |
f25d0cf3 | 1000 | nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s) |
f393f81e BP |
1001 | { |
1002 | const char *full_s = s; | |
f393f81e | 1003 | |
f25d0cf3 | 1004 | load->value = strtoull(s, (char **) &s, 0); |
f393f81e BP |
1005 | if (strncmp(s, "->", 2)) { |
1006 | ovs_fatal(0, "%s: missing `->' following value", full_s); | |
1007 | } | |
1008 | s += 2; | |
f25d0cf3 | 1009 | s = mf_parse_subfield(&load->dst, s); |
f393f81e BP |
1010 | if (*s != '\0') { |
1011 | ovs_fatal(0, "%s: trailing garbage following destination", full_s); | |
1012 | } | |
1013 | ||
f25d0cf3 BP |
1014 | if (load->dst.n_bits < 64 && (load->value >> load->dst.n_bits) != 0) { |
1015 | ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits", | |
1016 | full_s, load->value, load->dst.n_bits); | |
f393f81e | 1017 | } |
f393f81e BP |
1018 | } |
1019 | \f | |
1020 | /* nxm_format_reg_move(), nxm_format_reg_load(). */ | |
1021 | ||
f393f81e | 1022 | void |
f25d0cf3 | 1023 | nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s) |
f393f81e | 1024 | { |
f393f81e | 1025 | ds_put_format(s, "move:"); |
f25d0cf3 | 1026 | mf_format_subfield(&move->src, s); |
f393f81e | 1027 | ds_put_cstr(s, "->"); |
f25d0cf3 | 1028 | mf_format_subfield(&move->dst, s); |
f393f81e BP |
1029 | } |
1030 | ||
1031 | void | |
f25d0cf3 BP |
1032 | nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s) |
1033 | { | |
1034 | ds_put_format(s, "load:%#"PRIx64"->", load->value); | |
1035 | mf_format_subfield(&load->dst, s); | |
1036 | } | |
1037 | \f | |
1038 | enum ofperr | |
1039 | nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm, | |
1040 | struct ofpbuf *ofpacts) | |
f393f81e | 1041 | { |
f25d0cf3 | 1042 | struct ofpact_reg_move *move; |
f393f81e | 1043 | |
f25d0cf3 BP |
1044 | move = ofpact_put_REG_MOVE(ofpacts); |
1045 | move->src.field = mf_from_nxm_header(ntohl(narm->src)); | |
1046 | move->src.ofs = ntohs(narm->src_ofs); | |
1047 | move->src.n_bits = ntohs(narm->n_bits); | |
1048 | move->dst.field = mf_from_nxm_header(ntohl(narm->dst)); | |
1049 | move->dst.ofs = ntohs(narm->dst_ofs); | |
1050 | move->dst.n_bits = ntohs(narm->n_bits); | |
816fd533 | 1051 | |
f25d0cf3 | 1052 | return nxm_reg_move_check(move, NULL); |
f393f81e | 1053 | } |
b6c9e612 | 1054 | |
816fd533 | 1055 | enum ofperr |
f25d0cf3 BP |
1056 | nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl, |
1057 | struct ofpbuf *ofpacts) | |
b6c9e612 | 1058 | { |
f25d0cf3 | 1059 | struct ofpact_reg_load *load; |
b6c9e612 | 1060 | |
f25d0cf3 BP |
1061 | load = ofpact_put_REG_LOAD(ofpacts); |
1062 | load->dst.field = mf_from_nxm_header(ntohl(narl->dst)); | |
1063 | load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits); | |
1064 | load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits); | |
1065 | load->value = ntohll(narl->value); | |
1066 | ||
1067 | /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in | |
1068 | * narl->value. */ | |
1069 | if (load->dst.n_bits < 64 && load->value >> load->dst.n_bits) { | |
1070 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
b6c9e612 BP |
1071 | } |
1072 | ||
f25d0cf3 | 1073 | return nxm_reg_load_check(load, NULL); |
43edca57 | 1074 | } |
f25d0cf3 | 1075 | \f |
90bf1e07 | 1076 | enum ofperr |
f25d0cf3 | 1077 | nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow) |
43edca57 | 1078 | { |
90bf1e07 | 1079 | enum ofperr error; |
43edca57 | 1080 | |
f25d0cf3 | 1081 | error = mf_check_src(&move->src, flow); |
43edca57 EJ |
1082 | if (error) { |
1083 | return error; | |
b6c9e612 BP |
1084 | } |
1085 | ||
f25d0cf3 BP |
1086 | return mf_check_dst(&move->dst, NULL); |
1087 | } | |
b6c9e612 | 1088 | |
f25d0cf3 BP |
1089 | enum ofperr |
1090 | nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow) | |
1091 | { | |
1092 | return mf_check_dst(&load->dst, flow); | |
b6c9e612 BP |
1093 | } |
1094 | \f | |
b6c9e612 | 1095 | void |
f25d0cf3 BP |
1096 | nxm_reg_move_to_nxast(const struct ofpact_reg_move *move, |
1097 | struct ofpbuf *openflow) | |
b6c9e612 | 1098 | { |
f25d0cf3 | 1099 | struct nx_action_reg_move *narm; |
28da1f8f | 1100 | |
f25d0cf3 BP |
1101 | narm = ofputil_put_NXAST_REG_MOVE(openflow); |
1102 | narm->n_bits = htons(move->dst.n_bits); | |
1103 | narm->src_ofs = htons(move->src.ofs); | |
1104 | narm->dst_ofs = htons(move->dst.ofs); | |
1105 | narm->src = htonl(move->src.field->nxm_header); | |
1106 | narm->dst = htonl(move->dst.field->nxm_header); | |
1107 | } | |
816fd533 | 1108 | |
f25d0cf3 BP |
1109 | void |
1110 | nxm_reg_load_to_nxast(const struct ofpact_reg_load *load, | |
1111 | struct ofpbuf *openflow) | |
1112 | { | |
1113 | struct nx_action_reg_load *narl; | |
1114 | ||
1115 | narl = ofputil_put_NXAST_REG_LOAD(openflow); | |
1116 | narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits); | |
1117 | narl->dst = htonl(load->dst.field->nxm_header); | |
1118 | narl->value = htonll(load->value); | |
b6c9e612 | 1119 | } |
f25d0cf3 BP |
1120 | \f |
1121 | /* nxm_execute_reg_move(), nxm_execute_reg_load(). */ | |
b6c9e612 BP |
1122 | |
1123 | void | |
f25d0cf3 | 1124 | nxm_execute_reg_move(const struct ofpact_reg_move *move, |
b6c9e612 BP |
1125 | struct flow *flow) |
1126 | { | |
f25d0cf3 BP |
1127 | union mf_value src_value; |
1128 | union mf_value dst_value; | |
816fd533 | 1129 | |
f25d0cf3 BP |
1130 | mf_get_value(move->dst.field, flow, &dst_value); |
1131 | mf_get_value(move->src.field, flow, &src_value); | |
1132 | bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs, | |
1133 | &dst_value, move->dst.field->n_bytes, move->dst.ofs, | |
1134 | move->src.n_bits); | |
1135 | mf_set_flow_value(move->dst.field, &dst_value, flow); | |
43edca57 | 1136 | } |
b6c9e612 | 1137 | |
43edca57 | 1138 | void |
f25d0cf3 | 1139 | nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow) |
43edca57 | 1140 | { |
f25d0cf3 | 1141 | nxm_reg_load(&load->dst, load->value, flow); |
816fd533 | 1142 | } |
28da1f8f | 1143 | |
816fd533 | 1144 | void |
f25d0cf3 BP |
1145 | nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data, |
1146 | struct flow *flow) | |
816fd533 | 1147 | { |
f25d0cf3 BP |
1148 | union mf_value dst_value; |
1149 | union mf_value src_value; | |
1150 | ||
1151 | mf_get_value(dst->field, flow, &dst_value); | |
1152 | src_value.be64 = htonll(src_data); | |
1153 | bitwise_copy(&src_value, sizeof src_value.be64, 0, | |
1154 | &dst_value, dst->field->n_bytes, dst->ofs, | |
1155 | dst->n_bits); | |
1156 | mf_set_flow_value(dst->field, &dst_value, flow); | |
b6c9e612 | 1157 | } |