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