]> git.proxmox.com Git - ovs.git/blame_incremental - lib/ofp-actions.c
ofp-actions: Pass ofp_version to decode functions.
[ovs.git] / lib / ofp-actions.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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#include "ofp-actions.h"
19#include "bundle.h"
20#include "byte-order.h"
21#include "compiler.h"
22#include "dummy.h"
23#include "dynamic-string.h"
24#include "hmap.h"
25#include "learn.h"
26#include "meta-flow.h"
27#include "multipath.h"
28#include "nx-match.h"
29#include "ofp-parse.h"
30#include "ofp-util.h"
31#include "ofpbuf.h"
32#include "unaligned.h"
33#include "util.h"
34#include "openvswitch/vlog.h"
35
36VLOG_DEFINE_THIS_MODULE(ofp_actions);
37
38static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39
40struct ofp_action_header;
41
42/* Raw identifiers for OpenFlow actions.
43 *
44 * Decoding and encoding OpenFlow actions across multiple versions is difficult
45 * to do in a clean, consistent way. This enumeration lays out all of the
46 * forms of actions that Open vSwitch supports.
47 *
48 * The comments here must follow a stylized form because the
49 * "extract-ofp-actions" program parses them at build time to generate data
50 * tables.
51 *
52 * - The first part of each comment specifies the vendor, OpenFlow versions,
53 * and type for each protocol that supports the action:
54 *
55 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
56 * extension actions. (Support for other vendors can be added, but
57 * it can't be done just based on a vendor ID definition alone
58 * because OpenFlow doesn't define a standard way to specify a
59 * subtype for vendor actions, so other vendors might do it different
60 * from Nicira.)
61 *
62 * # The version can specify a specific OpenFlow version, a version
63 * range delimited by "-", or an open-ended range with "+".
64 *
65 * # The type, in parentheses, is the action type number (for standard
66 * OpenFlow actions) or subtype (for vendor extension actions).
67 *
68 * # Optionally one may add "is deprecated" followed by a
69 * human-readable reason in parentheses (which will be used in log
70 * messages), if a particular action should no longer be used.
71 *
72 * Multiple such specifications may be separated by commas.
73 *
74 * - The second part describes the action's wire format. It may be:
75 *
76 * # "struct <name>": The struct fully specifies the wire format. The
77 * action is exactly the size of the struct. (Thus, the struct must
78 * be an exact multiple of 8 bytes in size.)
79 *
80 * # "struct <name>, ...": The struct specifies the beginning of the
81 * wire format. An instance of the action is either the struct's
82 * exact size, or a multiple of 8 bytes longer.
83 *
84 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
85 * vendor extension) header, followed by 0 or more pad bytes to align
86 * to a multiple of <N> bits, followed by an argument of the given
87 * type, followed by 0 or more pad bytes to bring the total action up
88 * to a multiple of 8 bytes.
89 *
90 * # "void": The action is just a (standard or vendor extension)
91 * header.
92 *
93 * - Optional additional text enclosed in square brackets is commentary for
94 * the human reader.
95 */
96enum ofp_raw_action_type {
97/* ## ----------------- ## */
98/* ## Standard actions. ## */
99/* ## ----------------- ## */
100
101 /* OF1.0(0): struct ofp10_action_output. */
102 OFPAT_RAW10_OUTPUT,
103 /* OF1.1+(0): struct ofp11_action_output. */
104 OFPAT_RAW11_OUTPUT,
105
106 /* OF1.0(1): uint16_t. */
107 OFPAT_RAW10_SET_VLAN_VID,
108 /* OF1.0(2): uint8_t. */
109 OFPAT_RAW10_SET_VLAN_PCP,
110
111 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
112 *
113 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
114 * modification actions: the 1.0 versions push a VLAN header if none is
115 * present, but the 1.1 versions do not. That is the only reason that we
116 * distinguish their raw action types.] */
117 OFPAT_RAW11_SET_VLAN_VID,
118 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
119 OFPAT_RAW11_SET_VLAN_PCP,
120
121 /* OF1.1+(17): ovs_be16.
122 *
123 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
124 * TCI.] */
125 OFPAT_RAW11_PUSH_VLAN,
126
127 /* OF1.0(3): void. */
128 OFPAT_RAW10_STRIP_VLAN,
129 /* OF1.1+(18): void. */
130 OFPAT_RAW11_POP_VLAN,
131
132 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
133 * ofp_action_dl_addr. */
134 OFPAT_RAW_SET_DL_SRC,
135
136 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_DST,
139
140 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
141 * ovs_be32. */
142 OFPAT_RAW_SET_NW_SRC,
143
144 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
145 * ovs_be32. */
146 OFPAT_RAW_SET_NW_DST,
147
148 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
149 OFPAT_RAW_SET_NW_TOS,
150
151 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW11_SET_NW_ECN,
153
154 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
155 * ovs_be16. */
156 OFPAT_RAW_SET_TP_SRC,
157
158 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
159 * ovs_be16. */
160 OFPAT_RAW_SET_TP_DST,
161
162 /* OF1.0(11): struct ofp10_action_enqueue. */
163 OFPAT_RAW10_ENQUEUE,
164
165 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
166 * ovs_be32. */
167 OFPAT_RAW_SET_MPLS_LABEL,
168
169 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
170 * uint8_t. */
171 OFPAT_RAW_SET_MPLS_TC,
172
173 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
174 * uint8_t. */
175 OFPAT_RAW_SET_MPLS_TTL,
176
177 /* NX1.0(26), OF1.1+(16): void. */
178 OFPAT_RAW_DEC_MPLS_TTL,
179
180 /* NX1.0(23), OF1.1+(19): ovs_be16.
181 *
182 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
183 OFPAT_RAW_PUSH_MPLS,
184
185 /* NX1.0(24), OF1.1+(20): ovs_be16.
186 *
187 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
188 * ETH_TYPE_MPLS otherwise, not the label.] */
189 OFPAT_RAW_POP_MPLS,
190
191 /* NX1.0(4), OF1.1+(21): uint32_t. */
192 OFPAT_RAW_SET_QUEUE,
193
194 /* OF1.1+(22): uint32_t. */
195 OFPAT_RAW11_GROUP,
196
197 /* OF1.1+(23): uint8_t. */
198 OFPAT_RAW11_SET_NW_TTL,
199
200 /* NX1.0(18), OF1.1+(24): void. */
201 OFPAT_RAW_DEC_NW_TTL,
202 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
203 NXAST_RAW_DEC_TTL_CNT_IDS,
204
205 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
206 OFPAT_RAW12_SET_FIELD,
207 /* OF1.5+(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW15_SET_FIELD,
209 /* NX1.0-1.4(7): struct nx_action_reg_load.
210 *
211 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
212 * we drop reg_load.] */
213 NXAST_RAW_REG_LOAD,
214 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
215 *
216 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
217 * we drop reg_load2.] */
218 NXAST_RAW_REG_LOAD2,
219
220 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
221 OFPAT_RAW15_COPY_FIELD,
222 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
223 ONFACT_RAW13_COPY_FIELD,
224 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
225 NXAST_RAW_REG_MOVE,
226
227/* ## ------------------------- ## */
228/* ## Nicira extension actions. ## */
229/* ## ------------------------- ## */
230
231/* Actions similar to standard actions are listed with the standard actions. */
232
233 /* NX1.0+(1): uint16_t. */
234 NXAST_RAW_RESUBMIT,
235 /* NX1.0+(14): struct nx_action_resubmit. */
236 NXAST_RAW_RESUBMIT_TABLE,
237
238 /* NX1.0+(2): uint32_t. */
239 NXAST_RAW_SET_TUNNEL,
240 /* NX1.0+(9): uint64_t. */
241 NXAST_RAW_SET_TUNNEL64,
242
243 /* NX1.0+(5): void. */
244 NXAST_RAW_POP_QUEUE,
245
246 /* NX1.0+(8): struct nx_action_note, ... */
247 NXAST_RAW_NOTE,
248
249 /* NX1.0+(10): struct nx_action_multipath. */
250 NXAST_RAW_MULTIPATH,
251
252 /* NX1.0+(12): struct nx_action_bundle, ... */
253 NXAST_RAW_BUNDLE,
254 /* NX1.0+(13): struct nx_action_bundle, ... */
255 NXAST_RAW_BUNDLE_LOAD,
256
257 /* NX1.0+(15): struct nx_action_output_reg. */
258 NXAST_RAW_OUTPUT_REG,
259 /* NX1.0+(32): struct nx_action_output_reg2. */
260 NXAST_RAW_OUTPUT_REG2,
261
262 /* NX1.0+(16): struct nx_action_learn, ... */
263 NXAST_RAW_LEARN,
264
265 /* NX1.0+(17): void. */
266 NXAST_RAW_EXIT,
267
268 /* NX1.0+(19): struct nx_action_fin_timeout. */
269 NXAST_RAW_FIN_TIMEOUT,
270
271 /* NX1.0+(20): struct nx_action_controller. */
272 NXAST_RAW_CONTROLLER,
273
274 /* NX1.0+(22): struct nx_action_write_metadata. */
275 NXAST_RAW_WRITE_METADATA,
276
277 /* NX1.0+(27): struct nx_action_stack. */
278 NXAST_RAW_STACK_PUSH,
279
280 /* NX1.0+(28): struct nx_action_stack. */
281 NXAST_RAW_STACK_POP,
282
283 /* NX1.0+(29): struct nx_action_sample. */
284 NXAST_RAW_SAMPLE,
285
286 /* NX1.0+(34): struct nx_action_conjunction. */
287 NXAST_RAW_CONJUNCTION,
288
289/* ## ------------------ ## */
290/* ## Debugging actions. ## */
291/* ## ------------------ ## */
292
293/* These are intentionally undocumented, subject to change, and ovs-vswitchd */
294/* accepts them only if started with --enable-dummy. */
295
296 /* NX1.0+(255): void. */
297 NXAST_RAW_DEBUG_RECIRC,
298};
299
300/* OpenFlow actions are always a multiple of 8 bytes in length. */
301#define OFP_ACTION_ALIGN 8
302
303/* Define a few functions for working with instructions. */
304#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
305 static inline const struct STRUCT * OVS_UNUSED \
306 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
307 { \
308 ovs_assert(inst->type == htons(ENUM)); \
309 return ALIGNED_CAST(struct STRUCT *, inst); \
310 } \
311 \
312 static inline void OVS_UNUSED \
313 instruction_init_##ENUM(struct STRUCT *s) \
314 { \
315 memset(s, 0, sizeof *s); \
316 s->type = htons(ENUM); \
317 s->len = htons(sizeof *s); \
318 } \
319 \
320 static inline struct STRUCT * OVS_UNUSED \
321 instruction_put_##ENUM(struct ofpbuf *buf) \
322 { \
323 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
324 instruction_init_##ENUM(s); \
325 return s; \
326 }
327OVS_INSTRUCTIONS
328#undef DEFINE_INST
329
330static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
331 size_t ofs);
332static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
333
334static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
335 uint32_t allowed_ovsinsts,
336 enum ofpact_type outer_action);
337
338static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
339 enum mf_field_id, uint64_t value);
340
341static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
342 enum ofp_raw_action_type *, uint64_t *arg);
343static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
344 enum ofp_raw_action_type, uint64_t arg);
345
346static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
347 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
348 bool allow_instructions, enum ofpact_type outer_action);
349
350/* Pull off existing actions or instructions. Used by nesting actions to keep
351 * ofpacts_parse() oblivious of actions nesting.
352 *
353 * Push the actions back on after nested parsing, e.g.:
354 *
355 * size_t ofs = ofpacts_pull(ofpacts);
356 * ...nested parsing...
357 * ofpbuf_push_uninit(ofpacts, ofs);
358 */
359static size_t
360ofpacts_pull(struct ofpbuf *ofpacts)
361{
362 size_t ofs;
363
364 ofpact_pad(ofpacts);
365 ofs = ofpacts->size;
366 ofpbuf_pull(ofpacts, ofs);
367
368 return ofs;
369}
370
371#include "ofp-actions.inc1"
372\f
373/* Output actions. */
374
375/* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
376 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
377 * number of bytes to send. A 'max_len' of zero means no bytes of the
378 * packet should be sent. */
379struct ofp10_action_output {
380 ovs_be16 type; /* OFPAT10_OUTPUT. */
381 ovs_be16 len; /* Length is 8. */
382 ovs_be16 port; /* Output port. */
383 ovs_be16 max_len; /* Max length to send to controller. */
384};
385OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
386
387/* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
388 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
389 * number of bytes to send. A 'max_len' of zero means no bytes of the
390 * packet should be sent.*/
391struct ofp11_action_output {
392 ovs_be16 type; /* OFPAT11_OUTPUT. */
393 ovs_be16 len; /* Length is 16. */
394 ovs_be32 port; /* Output port. */
395 ovs_be16 max_len; /* Max length to send to controller. */
396 uint8_t pad[6]; /* Pad to 64 bits. */
397};
398OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
399
400static enum ofperr
401decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
402 enum ofp_version ofp_version OVS_UNUSED,
403 struct ofpbuf *out)
404{
405 struct ofpact_output *output;
406
407 output = ofpact_put_OUTPUT(out);
408 output->port = u16_to_ofp(ntohs(oao->port));
409 output->max_len = ntohs(oao->max_len);
410
411 return ofpact_check_output_port(output->port, OFPP_MAX);
412}
413
414static enum ofperr
415decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
416 enum ofp_version ofp_version OVS_UNUSED,
417 struct ofpbuf *out)
418{
419 struct ofpact_output *output;
420 enum ofperr error;
421
422 output = ofpact_put_OUTPUT(out);
423 output->max_len = ntohs(oao->max_len);
424
425 error = ofputil_port_from_ofp11(oao->port, &output->port);
426 if (error) {
427 return error;
428 }
429
430 return ofpact_check_output_port(output->port, OFPP_MAX);
431}
432
433static void
434encode_OUTPUT(const struct ofpact_output *output,
435 enum ofp_version ofp_version, struct ofpbuf *out)
436{
437 if (ofp_version == OFP10_VERSION) {
438 struct ofp10_action_output *oao;
439
440 oao = put_OFPAT10_OUTPUT(out);
441 oao->port = htons(ofp_to_u16(output->port));
442 oao->max_len = htons(output->max_len);
443 } else {
444 struct ofp11_action_output *oao;
445
446 oao = put_OFPAT11_OUTPUT(out);
447 oao->port = ofputil_port_to_ofp11(output->port);
448 oao->max_len = htons(output->max_len);
449 }
450}
451
452static char * OVS_WARN_UNUSED_RESULT
453parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
454 enum ofputil_protocol *usable_protocols OVS_UNUSED)
455{
456 if (strchr(arg, '[')) {
457 struct ofpact_output_reg *output_reg;
458
459 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
460 output_reg->max_len = UINT16_MAX;
461 return mf_parse_subfield(&output_reg->src, arg);
462 } else {
463 struct ofpact_output *output;
464
465 output = ofpact_put_OUTPUT(ofpacts);
466 if (!ofputil_port_from_string(arg, &output->port)) {
467 return xasprintf("%s: output to unknown port", arg);
468 }
469 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
470 return NULL;
471 }
472}
473
474static void
475format_OUTPUT(const struct ofpact_output *a, struct ds *s)
476{
477 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
478 ds_put_format(s, "output:%"PRIu16, a->port);
479 } else {
480 ofputil_format_port(a->port, s);
481 if (a->port == OFPP_CONTROLLER) {
482 ds_put_format(s, ":%"PRIu16, a->max_len);
483 }
484 }
485}
486\f
487/* Group actions. */
488
489static enum ofperr
490decode_OFPAT_RAW11_GROUP(uint32_t group_id,
491 enum ofp_version ofp_version OVS_UNUSED,
492 struct ofpbuf *out)
493{
494 ofpact_put_GROUP(out)->group_id = group_id;
495 return 0;
496}
497
498static void
499encode_GROUP(const struct ofpact_group *group,
500 enum ofp_version ofp_version, struct ofpbuf *out)
501{
502 if (ofp_version == OFP10_VERSION) {
503 /* XXX */
504 } else {
505 put_OFPAT11_GROUP(out, group->group_id);
506 }
507}
508
509static char * OVS_WARN_UNUSED_RESULT
510parse_GROUP(char *arg, struct ofpbuf *ofpacts,
511 enum ofputil_protocol *usable_protocols OVS_UNUSED)
512{
513 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
514}
515
516static void
517format_GROUP(const struct ofpact_group *a, struct ds *s)
518{
519 ds_put_format(s, "group:%"PRIu32, a->group_id);
520}
521\f
522/* Action structure for NXAST_CONTROLLER.
523 *
524 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
525 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
526 * specifying:
527 *
528 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
529 *
530 * - 'controller_id': The ID of the controller connection to which the
531 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
532 * sent only to controllers that have the specified controller connection
533 * ID. See "struct nx_controller_id" for more information. */
534struct nx_action_controller {
535 ovs_be16 type; /* OFPAT_VENDOR. */
536 ovs_be16 len; /* Length is 16. */
537 ovs_be32 vendor; /* NX_VENDOR_ID. */
538 ovs_be16 subtype; /* NXAST_CONTROLLER. */
539 ovs_be16 max_len; /* Maximum length to send to controller. */
540 ovs_be16 controller_id; /* Controller ID to send packet-in. */
541 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
542 uint8_t zero; /* Must be zero. */
543};
544OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
545
546static enum ofperr
547decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
548 enum ofp_version ofp_version OVS_UNUSED,
549 struct ofpbuf *out)
550{
551 struct ofpact_controller *oc;
552
553 oc = ofpact_put_CONTROLLER(out);
554 oc->max_len = ntohs(nac->max_len);
555 oc->controller_id = ntohs(nac->controller_id);
556 oc->reason = nac->reason;
557 return 0;
558}
559
560static void
561encode_CONTROLLER(const struct ofpact_controller *controller,
562 enum ofp_version ofp_version OVS_UNUSED,
563 struct ofpbuf *out)
564{
565 struct nx_action_controller *nac;
566
567 nac = put_NXAST_CONTROLLER(out);
568 nac->max_len = htons(controller->max_len);
569 nac->controller_id = htons(controller->controller_id);
570 nac->reason = controller->reason;
571}
572
573static char * OVS_WARN_UNUSED_RESULT
574parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
575 enum ofputil_protocol *usable_protocols OVS_UNUSED)
576{
577 enum ofp_packet_in_reason reason = OFPR_ACTION;
578 uint16_t controller_id = 0;
579 uint16_t max_len = UINT16_MAX;
580
581 if (!arg[0]) {
582 /* Use defaults. */
583 } else if (strspn(arg, "0123456789") == strlen(arg)) {
584 char *error = str_to_u16(arg, "max_len", &max_len);
585 if (error) {
586 return error;
587 }
588 } else {
589 char *name, *value;
590
591 while (ofputil_parse_key_value(&arg, &name, &value)) {
592 if (!strcmp(name, "reason")) {
593 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
594 return xasprintf("unknown reason \"%s\"", value);
595 }
596 } else if (!strcmp(name, "max_len")) {
597 char *error = str_to_u16(value, "max_len", &max_len);
598 if (error) {
599 return error;
600 }
601 } else if (!strcmp(name, "id")) {
602 char *error = str_to_u16(value, "id", &controller_id);
603 if (error) {
604 return error;
605 }
606 } else {
607 return xasprintf("unknown key \"%s\" parsing controller "
608 "action", name);
609 }
610 }
611 }
612
613 if (reason == OFPR_ACTION && controller_id == 0) {
614 struct ofpact_output *output;
615
616 output = ofpact_put_OUTPUT(ofpacts);
617 output->port = OFPP_CONTROLLER;
618 output->max_len = max_len;
619 } else {
620 struct ofpact_controller *controller;
621
622 controller = ofpact_put_CONTROLLER(ofpacts);
623 controller->max_len = max_len;
624 controller->reason = reason;
625 controller->controller_id = controller_id;
626 }
627
628 return NULL;
629}
630
631static void
632format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
633{
634 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
635 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
636 } else {
637 enum ofp_packet_in_reason reason = a->reason;
638
639 ds_put_cstr(s, "controller(");
640 if (reason != OFPR_ACTION) {
641 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
642
643 ds_put_format(s, "reason=%s,",
644 ofputil_packet_in_reason_to_string(
645 reason, reasonbuf, sizeof reasonbuf));
646 }
647 if (a->max_len != UINT16_MAX) {
648 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
649 }
650 if (a->controller_id != 0) {
651 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
652 }
653 ds_chomp(s, ',');
654 ds_put_char(s, ')');
655 }
656}
657\f
658/* Enqueue action. */
659struct ofp10_action_enqueue {
660 ovs_be16 type; /* OFPAT10_ENQUEUE. */
661 ovs_be16 len; /* Len is 16. */
662 ovs_be16 port; /* Port that queue belongs. Should
663 refer to a valid physical port
664 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
665 uint8_t pad[6]; /* Pad for 64-bit alignment. */
666 ovs_be32 queue_id; /* Where to enqueue the packets. */
667};
668OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
669
670static enum ofperr
671decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
672 enum ofp_version ofp_version OVS_UNUSED,
673 struct ofpbuf *out)
674{
675 struct ofpact_enqueue *enqueue;
676
677 enqueue = ofpact_put_ENQUEUE(out);
678 enqueue->port = u16_to_ofp(ntohs(oae->port));
679 enqueue->queue = ntohl(oae->queue_id);
680 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
681 && enqueue->port != OFPP_IN_PORT
682 && enqueue->port != OFPP_LOCAL) {
683 return OFPERR_OFPBAC_BAD_OUT_PORT;
684 }
685 return 0;
686}
687
688static void
689encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
690 enum ofp_version ofp_version, struct ofpbuf *out)
691{
692 if (ofp_version == OFP10_VERSION) {
693 struct ofp10_action_enqueue *oae;
694
695 oae = put_OFPAT10_ENQUEUE(out);
696 oae->port = htons(ofp_to_u16(enqueue->port));
697 oae->queue_id = htonl(enqueue->queue);
698 } else {
699 /* XXX */
700 }
701}
702
703static char * OVS_WARN_UNUSED_RESULT
704parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
705 enum ofputil_protocol *usable_protocols OVS_UNUSED)
706{
707 char *sp = NULL;
708 char *port = strtok_r(arg, ":q,", &sp);
709 char *queue = strtok_r(NULL, "", &sp);
710 struct ofpact_enqueue *enqueue;
711
712 if (port == NULL || queue == NULL) {
713 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
714 "\"enqueue(PORT,QUEUE)\"");
715 }
716
717 enqueue = ofpact_put_ENQUEUE(ofpacts);
718 if (!ofputil_port_from_string(port, &enqueue->port)) {
719 return xasprintf("%s: enqueue to unknown port", port);
720 }
721 return str_to_u32(queue, &enqueue->queue);
722}
723
724static void
725format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
726{
727 ds_put_format(s, "enqueue:");
728 ofputil_format_port(a->port, s);
729 ds_put_format(s, ":%"PRIu32, a->queue);
730}
731\f
732/* Action structure for NXAST_OUTPUT_REG.
733 *
734 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
735 *
736 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
737 * the NXAST_REG_LOAD action.
738 *
739 * The acceptable nxm_header values for 'src' are the same as the acceptable
740 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
741 *
742 * The 'max_len' field indicates the number of bytes to send when the chosen
743 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
744 * field of OFPAT_OUTPUT.
745 *
746 * The 'zero' field is required to be zeroed for forward compatibility. */
747struct nx_action_output_reg {
748 ovs_be16 type; /* OFPAT_VENDOR. */
749 ovs_be16 len; /* 24. */
750 ovs_be32 vendor; /* NX_VENDOR_ID. */
751 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
752
753 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
754 ovs_be32 src; /* Source. */
755
756 ovs_be16 max_len; /* Max length to send to controller. */
757
758 uint8_t zero[6]; /* Reserved, must be zero. */
759};
760OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
761
762/* Action structure for NXAST_OUTPUT_REG2.
763 *
764 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
765 * experimenter OXM as 'src'.
766 */
767struct nx_action_output_reg2 {
768 ovs_be16 type; /* OFPAT_VENDOR. */
769 ovs_be16 len; /* 24. */
770 ovs_be32 vendor; /* NX_VENDOR_ID. */
771 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
772
773 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
774 ovs_be16 max_len; /* Max length to send to controller. */
775
776 /* Followed by:
777 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
778 * - Enough 0-bytes to pad the action out to 24 bytes. */
779 uint8_t pad[10];
780};
781OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
782
783static enum ofperr
784decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
785 enum ofp_version ofp_version OVS_UNUSED,
786 struct ofpbuf *out)
787{
788 struct ofpact_output_reg *output_reg;
789
790 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
791 return OFPERR_OFPBAC_BAD_ARGUMENT;
792 }
793
794 output_reg = ofpact_put_OUTPUT_REG(out);
795 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
796 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
797 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
798 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
799 output_reg->max_len = ntohs(naor->max_len);
800
801 return mf_check_src(&output_reg->src, NULL);
802}
803
804static enum ofperr
805decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
806 enum ofp_version ofp_version OVS_UNUSED,
807 struct ofpbuf *out)
808{
809 struct ofpact_output_reg *output_reg;
810 enum ofperr error;
811 struct ofpbuf b;
812
813 output_reg = ofpact_put_OUTPUT_REG(out);
814 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
815 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
816 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
817 output_reg->max_len = ntohs(naor->max_len);
818
819 ofpbuf_use_const(&b, naor, ntohs(naor->len));
820 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
821 error = nx_pull_header(&b, &output_reg->src.field, NULL);
822 if (error) {
823 return error;
824 }
825 if (!is_all_zeros(b.data, b.size)) {
826 return OFPERR_NXBRC_MUST_BE_ZERO;
827 }
828
829 return mf_check_src(&output_reg->src, NULL);
830}
831
832static void
833encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
834 enum ofp_version ofp_version OVS_UNUSED,
835 struct ofpbuf *out)
836{
837 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
838 * cannot be encoded in the older form, encode it as
839 * NXAST_RAW_OUTPUT_REG2. */
840 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
841 || !mf_nxm_header(output_reg->src.field->id)) {
842 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
843 size_t size = out->size;
844
845 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
846 output_reg->src.n_bits);
847 naor->max_len = htons(output_reg->max_len);
848
849 out->size = size - sizeof naor->pad;
850 nx_put_header(out, output_reg->src.field->id, 0, false);
851 out->size = size;
852 } else {
853 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
854
855 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
856 output_reg->src.n_bits);
857 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
858 naor->max_len = htons(output_reg->max_len);
859 }
860}
861
862static char * OVS_WARN_UNUSED_RESULT
863parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
864 enum ofputil_protocol *usable_protocols OVS_UNUSED)
865{
866 return parse_OUTPUT(arg, ofpacts, usable_protocols);
867}
868
869static void
870format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
871{
872 ds_put_cstr(s, "output:");
873 mf_format_subfield(&a->src, s);
874}
875\f
876/* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
877 *
878 * The bundle actions choose a slave from a supplied list of options.
879 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
880 * selection to a register.
881 *
882 * The list of possible slaves follows the nx_action_bundle structure. The size
883 * of each slave is governed by its type as indicated by the 'slave_type'
884 * parameter. The list of slaves should be padded at its end with zeros to make
885 * the total length of the action a multiple of 8.
886 *
887 * Switches infer from the 'slave_type' parameter the size of each slave. All
888 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
889 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
890 * 2 byte width. Switches should reject actions which indicate unknown or
891 * unsupported slave types.
892 *
893 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
894 * slave. If the switch does not support the specified 'algorithm' parameter,
895 * it should reject the action.
896 *
897 * Several algorithms take into account liveness when selecting slaves. The
898 * liveness of a slave is implementation defined (with one exception), but will
899 * generally take into account things like its carrier status and the results
900 * of any link monitoring protocols which happen to be running on it. In order
901 * to give controllers a place-holder value, the OFPP_NONE port is always
902 * considered live.
903 *
904 * Some slave selection strategies require the use of a hash function, in which
905 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
906 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
907 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
908 * 'basis' parameter is used as a universal hash parameter. Different values
909 * of 'basis' yield different hash results.
910 *
911 * The 'zero' parameter at the end of the action structure is reserved for
912 * future use. Switches are required to reject actions which have nonzero
913 * bytes in the 'zero' field.
914 *
915 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
916 * should reject actions which have nonzero bytes in either of these fields.
917 *
918 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
919 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
920 * similar to those for the NXAST_REG_LOAD action. */
921struct nx_action_bundle {
922 ovs_be16 type; /* OFPAT_VENDOR. */
923 ovs_be16 len; /* Length including slaves. */
924 ovs_be32 vendor; /* NX_VENDOR_ID. */
925 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
926
927 /* Slave choice algorithm to apply to hash value. */
928 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
929
930 /* What fields to hash and how. */
931 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
932 ovs_be16 basis; /* Universal hash parameter. */
933
934 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
935 ovs_be16 n_slaves; /* Number of slaves. */
936
937 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
938 ovs_be32 dst; /* Destination. */
939
940 uint8_t zero[4]; /* Reserved. Must be zero. */
941};
942OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
943
944static enum ofperr
945decode_bundle(bool load, const struct nx_action_bundle *nab,
946 struct ofpbuf *ofpacts)
947{
948 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
949 struct ofpact_bundle *bundle;
950 uint32_t slave_type;
951 size_t slaves_size, i;
952 enum ofperr error;
953
954 bundle = ofpact_put_BUNDLE(ofpacts);
955
956 bundle->n_slaves = ntohs(nab->n_slaves);
957 bundle->basis = ntohs(nab->basis);
958 bundle->fields = ntohs(nab->fields);
959 bundle->algorithm = ntohs(nab->algorithm);
960 slave_type = ntohl(nab->slave_type);
961 slaves_size = ntohs(nab->len) - sizeof *nab;
962
963 error = OFPERR_OFPBAC_BAD_ARGUMENT;
964 if (!flow_hash_fields_valid(bundle->fields)) {
965 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
966 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
967 VLOG_WARN_RL(&rl, "too many slaves");
968 } else if (bundle->algorithm != NX_BD_ALG_HRW
969 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
970 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
971 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
972 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
973 } else {
974 error = 0;
975 }
976
977 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
978 VLOG_WARN_RL(&rl, "reserved field is nonzero");
979 error = OFPERR_OFPBAC_BAD_ARGUMENT;
980 }
981
982 if (load) {
983 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
984 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
985 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
986
987 if (bundle->dst.n_bits < 16) {
988 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
989 "destination.");
990 error = OFPERR_OFPBAC_BAD_ARGUMENT;
991 }
992 } else {
993 if (nab->ofs_nbits || nab->dst) {
994 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
995 error = OFPERR_OFPBAC_BAD_ARGUMENT;
996 }
997 }
998
999 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1000 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1001 "allocated for slaves. %"PRIuSIZE" bytes are required "
1002 "for %"PRIu16" slaves.",
1003 load ? "bundle_load" : "bundle", slaves_size,
1004 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1005 error = OFPERR_OFPBAC_BAD_LEN;
1006 }
1007
1008 for (i = 0; i < bundle->n_slaves; i++) {
1009 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1010 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1011 }
1012
1013 bundle = ofpacts->header;
1014 ofpact_update_len(ofpacts, &bundle->ofpact);
1015
1016 if (!error) {
1017 error = bundle_check(bundle, OFPP_MAX, NULL);
1018 }
1019 return error;
1020}
1021
1022static enum ofperr
1023decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1024 enum ofp_version ofp_version OVS_UNUSED,
1025 struct ofpbuf *out)
1026{
1027 return decode_bundle(false, nab, out);
1028}
1029
1030static enum ofperr
1031decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1032 enum ofp_version ofp_version OVS_UNUSED,
1033 struct ofpbuf *out)
1034{
1035 return decode_bundle(true, nab, out);
1036}
1037
1038static void
1039encode_BUNDLE(const struct ofpact_bundle *bundle,
1040 enum ofp_version ofp_version OVS_UNUSED,
1041 struct ofpbuf *out)
1042{
1043 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1044 struct nx_action_bundle *nab;
1045 ovs_be16 *slaves;
1046 size_t i;
1047
1048 nab = (bundle->dst.field
1049 ? put_NXAST_BUNDLE_LOAD(out)
1050 : put_NXAST_BUNDLE(out));
1051 nab->len = htons(ntohs(nab->len) + slaves_len);
1052 nab->algorithm = htons(bundle->algorithm);
1053 nab->fields = htons(bundle->fields);
1054 nab->basis = htons(bundle->basis);
1055 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1056 nab->n_slaves = htons(bundle->n_slaves);
1057 if (bundle->dst.field) {
1058 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1059 bundle->dst.n_bits);
1060 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1061 }
1062
1063 slaves = ofpbuf_put_zeros(out, slaves_len);
1064 for (i = 0; i < bundle->n_slaves; i++) {
1065 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1066 }
1067}
1068
1069static char * OVS_WARN_UNUSED_RESULT
1070parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1071 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1072{
1073 return bundle_parse(arg, ofpacts);
1074}
1075
1076static char * OVS_WARN_UNUSED_RESULT
1077parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1078{
1079 return bundle_parse_load(arg, ofpacts);
1080}
1081
1082static void
1083format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1084{
1085 bundle_format(a, s);
1086}
1087\f
1088/* Set VLAN actions. */
1089
1090static enum ofperr
1091decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1092{
1093 if (vid & ~0xfff) {
1094 return OFPERR_OFPBAC_BAD_ARGUMENT;
1095 } else {
1096 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1097 vlan_vid->vlan_vid = vid;
1098 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1099 return 0;
1100 }
1101}
1102
1103static enum ofperr
1104decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1105 enum ofp_version ofp_version OVS_UNUSED,
1106 struct ofpbuf *out)
1107{
1108 return decode_set_vlan_vid(vid, true, out);
1109}
1110
1111static enum ofperr
1112decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1113 enum ofp_version ofp_version OVS_UNUSED,
1114 struct ofpbuf *out)
1115{
1116 return decode_set_vlan_vid(vid, false, out);
1117}
1118
1119static void
1120encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1121 enum ofp_version ofp_version, struct ofpbuf *out)
1122{
1123 uint16_t vid = vlan_vid->vlan_vid;
1124
1125 /* Push a VLAN tag, if none is present and this form of the action calls
1126 * for such a feature. */
1127 if (ofp_version > OFP10_VERSION
1128 && vlan_vid->push_vlan_if_needed
1129 && !vlan_vid->flow_has_vlan) {
1130 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1131 }
1132
1133 if (ofp_version == OFP10_VERSION) {
1134 put_OFPAT10_SET_VLAN_VID(out, vid);
1135 } else if (ofp_version == OFP11_VERSION) {
1136 put_OFPAT11_SET_VLAN_VID(out, vid);
1137 } else {
1138 ofpact_put_set_field(out, ofp_version,
1139 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1140 }
1141}
1142
1143static char * OVS_WARN_UNUSED_RESULT
1144parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1145{
1146 struct ofpact_vlan_vid *vlan_vid;
1147 uint16_t vid;
1148 char *error;
1149
1150 error = str_to_u16(arg, "VLAN VID", &vid);
1151 if (error) {
1152 return error;
1153 }
1154
1155 if (vid & ~VLAN_VID_MASK) {
1156 return xasprintf("%s: not a valid VLAN VID", arg);
1157 }
1158 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1159 vlan_vid->vlan_vid = vid;
1160 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1161 return NULL;
1162}
1163
1164static char * OVS_WARN_UNUSED_RESULT
1165parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1166 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1167{
1168 return parse_set_vlan_vid(arg, ofpacts, false);
1169}
1170
1171static void
1172format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1173{
1174 ds_put_format(s, "%s:%"PRIu16,
1175 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1176 a->vlan_vid);
1177}
1178\f
1179/* Set PCP actions. */
1180
1181static enum ofperr
1182decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1183{
1184 if (pcp & ~7) {
1185 return OFPERR_OFPBAC_BAD_ARGUMENT;
1186 } else {
1187 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1188 vlan_pcp->vlan_pcp = pcp;
1189 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1190 return 0;
1191 }
1192}
1193
1194static enum ofperr
1195decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1196 enum ofp_version ofp_version OVS_UNUSED,
1197 struct ofpbuf *out)
1198{
1199 return decode_set_vlan_pcp(pcp, true, out);
1200}
1201
1202static enum ofperr
1203decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1204 enum ofp_version ofp_version OVS_UNUSED,
1205 struct ofpbuf *out)
1206{
1207 return decode_set_vlan_pcp(pcp, false, out);
1208}
1209
1210static void
1211encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1212 enum ofp_version ofp_version, struct ofpbuf *out)
1213{
1214 uint8_t pcp = vlan_pcp->vlan_pcp;
1215
1216 /* Push a VLAN tag, if none is present and this form of the action calls
1217 * for such a feature. */
1218 if (ofp_version > OFP10_VERSION
1219 && vlan_pcp->push_vlan_if_needed
1220 && !vlan_pcp->flow_has_vlan) {
1221 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1222 }
1223
1224 if (ofp_version == OFP10_VERSION) {
1225 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1226 } else if (ofp_version == OFP11_VERSION) {
1227 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1228 } else {
1229 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1230 }
1231}
1232
1233static char * OVS_WARN_UNUSED_RESULT
1234parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1235{
1236 struct ofpact_vlan_pcp *vlan_pcp;
1237 uint8_t pcp;
1238 char *error;
1239
1240 error = str_to_u8(arg, "VLAN PCP", &pcp);
1241 if (error) {
1242 return error;
1243 }
1244
1245 if (pcp & ~7) {
1246 return xasprintf("%s: not a valid VLAN PCP", arg);
1247 }
1248 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1249 vlan_pcp->vlan_pcp = pcp;
1250 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1251 return NULL;
1252}
1253
1254static char * OVS_WARN_UNUSED_RESULT
1255parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1256 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1257{
1258 return parse_set_vlan_pcp(arg, ofpacts, false);
1259}
1260
1261static void
1262format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1263{
1264 ds_put_format(s, "%s:%"PRIu8,
1265 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1266 a->vlan_pcp);
1267}
1268\f
1269/* Strip VLAN actions. */
1270
1271static enum ofperr
1272decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1273{
1274 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1275 return 0;
1276}
1277
1278static enum ofperr
1279decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1280{
1281 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1282 return 0;
1283}
1284
1285static void
1286encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1287 enum ofp_version ofp_version, struct ofpbuf *out)
1288{
1289 if (ofp_version == OFP10_VERSION) {
1290 put_OFPAT10_STRIP_VLAN(out);
1291 } else {
1292 put_OFPAT11_POP_VLAN(out);
1293 }
1294}
1295
1296static char * OVS_WARN_UNUSED_RESULT
1297parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1298 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1299{
1300 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1301 return NULL;
1302}
1303
1304static char * OVS_WARN_UNUSED_RESULT
1305parse_pop_vlan(struct ofpbuf *ofpacts)
1306{
1307 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1308 return NULL;
1309}
1310
1311static void
1312format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1313{
1314 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1315 ? "pop_vlan"
1316 : "strip_vlan"));
1317}
1318\f
1319/* Push VLAN action. */
1320
1321static enum ofperr
1322decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1323 enum ofp_version ofp_version OVS_UNUSED,
1324 struct ofpbuf *out)
1325{
1326 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1327 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1328 return OFPERR_OFPBAC_BAD_ARGUMENT;
1329 }
1330 ofpact_put_PUSH_VLAN(out);
1331 return 0;
1332}
1333
1334static void
1335encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1336 enum ofp_version ofp_version, struct ofpbuf *out)
1337{
1338 if (ofp_version == OFP10_VERSION) {
1339 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1340 * follow this action. */
1341 } else {
1342 /* XXX ETH_TYPE_VLAN_8021AD case */
1343 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1344 }
1345}
1346
1347static char * OVS_WARN_UNUSED_RESULT
1348parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1349 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1350{
1351 uint16_t ethertype;
1352 char *error;
1353
1354 *usable_protocols &= OFPUTIL_P_OF11_UP;
1355 error = str_to_u16(arg, "ethertype", &ethertype);
1356 if (error) {
1357 return error;
1358 }
1359
1360 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1361 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1362 return xasprintf("%s: not a valid VLAN ethertype", arg);
1363 }
1364
1365 ofpact_put_PUSH_VLAN(ofpacts);
1366 return NULL;
1367}
1368
1369static void
1370format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1371{
1372 /* XXX 802.1AD case*/
1373 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1374}
1375\f
1376/* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1377struct ofp_action_dl_addr {
1378 ovs_be16 type; /* Type. */
1379 ovs_be16 len; /* Length is 16. */
1380 struct eth_addr dl_addr; /* Ethernet address. */
1381 uint8_t pad[6];
1382};
1383OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1384
1385static enum ofperr
1386decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1387 enum ofp_version ofp_version OVS_UNUSED,
1388 struct ofpbuf *out)
1389{
1390 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1391 return 0;
1392}
1393
1394static enum ofperr
1395decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1396 enum ofp_version ofp_version OVS_UNUSED,
1397 struct ofpbuf *out)
1398{
1399 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1400 return 0;
1401}
1402
1403static void
1404encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1405 enum ofp_raw_action_type raw, enum mf_field_id field,
1406 struct ofpbuf *out)
1407{
1408 if (ofp_version < OFP12_VERSION) {
1409 struct ofp_action_dl_addr *oada;
1410
1411 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1412 oada->dl_addr = mac->mac;
1413 } else {
1414 ofpact_put_set_field(out, ofp_version, field,
1415 eth_addr_to_uint64(mac->mac));
1416 }
1417}
1418
1419static void
1420encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1421 struct ofpbuf *out)
1422{
1423 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1424 out);
1425
1426}
1427
1428static void
1429encode_SET_ETH_DST(const struct ofpact_mac *mac,
1430 enum ofp_version ofp_version,
1431 struct ofpbuf *out)
1432{
1433 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1434 out);
1435
1436}
1437
1438static char * OVS_WARN_UNUSED_RESULT
1439parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1440 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1441{
1442 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1443}
1444
1445static char * OVS_WARN_UNUSED_RESULT
1446parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1447 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1448{
1449 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1450}
1451
1452static void
1453format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1454{
1455 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1456}
1457
1458static void
1459format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1460{
1461 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1462}
1463\f
1464/* Set IPv4 address actions. */
1465
1466static enum ofperr
1467decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1468 enum ofp_version ofp_version OVS_UNUSED,
1469 struct ofpbuf *out)
1470{
1471 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1472 return 0;
1473}
1474
1475static enum ofperr
1476decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1477 enum ofp_version ofp_version OVS_UNUSED,
1478 struct ofpbuf *out)
1479{
1480 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1481 return 0;
1482}
1483
1484static void
1485encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1486 enum ofp_version ofp_version,
1487 enum ofp_raw_action_type raw, enum mf_field_id field,
1488 struct ofpbuf *out)
1489{
1490 ovs_be32 addr = ipv4->ipv4;
1491 if (ofp_version < OFP12_VERSION) {
1492 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1493 } else {
1494 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1495 }
1496}
1497
1498static void
1499encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1500 enum ofp_version ofp_version, struct ofpbuf *out)
1501{
1502 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1503 out);
1504}
1505
1506static void
1507encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1508 enum ofp_version ofp_version, struct ofpbuf *out)
1509{
1510 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1511 out);
1512}
1513
1514static char * OVS_WARN_UNUSED_RESULT
1515parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1516 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1517{
1518 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1519}
1520
1521static char * OVS_WARN_UNUSED_RESULT
1522parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1523 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1524{
1525 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1526}
1527
1528static void
1529format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1530{
1531 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1532}
1533
1534static void
1535format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1536{
1537 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1538}
1539\f
1540/* Set IPv4/v6 TOS actions. */
1541
1542static enum ofperr
1543decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1544 enum ofp_version ofp_version OVS_UNUSED,
1545 struct ofpbuf *out)
1546{
1547 if (dscp & ~IP_DSCP_MASK) {
1548 return OFPERR_OFPBAC_BAD_ARGUMENT;
1549 } else {
1550 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1551 return 0;
1552 }
1553}
1554
1555static void
1556encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1557 enum ofp_version ofp_version, struct ofpbuf *out)
1558{
1559 if (ofp_version < OFP12_VERSION) {
1560 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1561 } else {
1562 ofpact_put_set_field(out, ofp_version,
1563 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1564 }
1565}
1566
1567static char * OVS_WARN_UNUSED_RESULT
1568parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1569 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1570{
1571 uint8_t tos;
1572 char *error;
1573
1574 error = str_to_u8(arg, "TOS", &tos);
1575 if (error) {
1576 return error;
1577 }
1578
1579 if (tos & ~IP_DSCP_MASK) {
1580 return xasprintf("%s: not a valid TOS", arg);
1581 }
1582 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1583 return NULL;
1584}
1585
1586static void
1587format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1588{
1589 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1590}
1591\f
1592/* Set IPv4/v6 ECN actions. */
1593
1594static enum ofperr
1595decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1596 enum ofp_version ofp_version OVS_UNUSED,
1597 struct ofpbuf *out)
1598{
1599 if (ecn & ~IP_ECN_MASK) {
1600 return OFPERR_OFPBAC_BAD_ARGUMENT;
1601 } else {
1602 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1603 return 0;
1604 }
1605}
1606
1607static void
1608encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1609 enum ofp_version ofp_version, struct ofpbuf *out)
1610{
1611 uint8_t ecn = ip_ecn->ecn;
1612 if (ofp_version == OFP10_VERSION) {
1613 /* XXX */
1614 } else if (ofp_version == OFP11_VERSION) {
1615 put_OFPAT11_SET_NW_ECN(out, ecn);
1616 } else {
1617 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1618 }
1619}
1620
1621static char * OVS_WARN_UNUSED_RESULT
1622parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1623 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1624{
1625 uint8_t ecn;
1626 char *error;
1627
1628 error = str_to_u8(arg, "ECN", &ecn);
1629 if (error) {
1630 return error;
1631 }
1632
1633 if (ecn & ~IP_ECN_MASK) {
1634 return xasprintf("%s: not a valid ECN", arg);
1635 }
1636 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1637 return NULL;
1638}
1639
1640static void
1641format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1642{
1643 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1644}
1645\f
1646/* Set IPv4/v6 TTL actions. */
1647
1648static enum ofperr
1649decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1650 enum ofp_version ofp_version OVS_UNUSED,
1651 struct ofpbuf *out)
1652{
1653 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1654 return 0;
1655}
1656
1657static void
1658encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1659 enum ofp_version ofp_version, struct ofpbuf *out)
1660{
1661 if (ofp_version >= OFP11_VERSION) {
1662 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1663 } else {
1664 /* XXX */
1665 }
1666}
1667
1668static char * OVS_WARN_UNUSED_RESULT
1669parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1670 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1671{
1672 uint8_t ttl;
1673 char *error;
1674
1675 error = str_to_u8(arg, "TTL", &ttl);
1676 if (error) {
1677 return error;
1678 }
1679
1680 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1681 return NULL;
1682}
1683
1684static void
1685format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1686{
1687 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1688}
1689\f
1690/* Set TCP/UDP/SCTP port actions. */
1691
1692static enum ofperr
1693decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1694 enum ofp_version ofp_version OVS_UNUSED,
1695 struct ofpbuf *out)
1696{
1697 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1698 return 0;
1699}
1700
1701static enum ofperr
1702decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1703 enum ofp_version ofp_version OVS_UNUSED,
1704 struct ofpbuf *out)
1705{
1706 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1707 return 0;
1708}
1709
1710static void
1711encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1712 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1713 enum mf_field_id field, struct ofpbuf *out)
1714{
1715 uint16_t port = l4_port->port;
1716
1717 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1718 ofpact_put_set_field(out, ofp_version, field, port);
1719 } else {
1720 ofpact_put_raw(out, ofp_version, raw, port);
1721 }
1722}
1723
1724static void
1725encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1726 enum ofp_version ofp_version, struct ofpbuf *out)
1727{
1728 uint8_t proto = l4_port->flow_ip_proto;
1729 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1730 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1731 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1732 : MFF_N_IDS);
1733
1734 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1735}
1736
1737static void
1738encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1739 enum ofp_version ofp_version,
1740 struct ofpbuf *out)
1741{
1742 uint8_t proto = l4_port->flow_ip_proto;
1743 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1744 : proto == IPPROTO_UDP ? MFF_UDP_DST
1745 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1746 : MFF_N_IDS);
1747
1748 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1749}
1750
1751static char * OVS_WARN_UNUSED_RESULT
1752parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1753 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1754{
1755 return str_to_u16(arg, "source port",
1756 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1757}
1758
1759static char * OVS_WARN_UNUSED_RESULT
1760parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1761 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1762{
1763 return str_to_u16(arg, "destination port",
1764 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1765}
1766
1767static void
1768format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1769{
1770 ds_put_format(s, "mod_tp_src:%d", a->port);
1771}
1772
1773static void
1774format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1775{
1776 ds_put_format(s, "mod_tp_dst:%d", a->port);
1777}
1778\f
1779/* Action structure for OFPAT_COPY_FIELD. */
1780struct ofp15_action_copy_field {
1781 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1782 ovs_be16 len; /* Length is padded to 64 bits. */
1783 ovs_be16 n_bits; /* Number of bits to copy. */
1784 ovs_be16 src_offset; /* Starting bit offset in source. */
1785 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1786 uint8_t pad[2];
1787 /* Followed by:
1788 * - OXM header for source field.
1789 * - OXM header for destination field.
1790 * - Padding with 0-bytes to a multiple of 8 bytes.
1791 * The "pad2" member is the beginning of the above. */
1792 uint8_t pad2[4];
1793};
1794OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1795
1796/* Action structure for OpenFlow 1.3 extension copy-field action.. */
1797struct onf_action_copy_field {
1798 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1799 ovs_be16 len; /* Length is padded to 64 bits. */
1800 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1801 ovs_be16 exp_type; /* 3200. */
1802 uint8_t pad[2]; /* Not used. */
1803 ovs_be16 n_bits; /* Number of bits to copy. */
1804 ovs_be16 src_offset; /* Starting bit offset in source. */
1805 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1806 uint8_t pad2[2]; /* Not used. */
1807 /* Followed by:
1808 * - OXM header for source field.
1809 * - OXM header for destination field.
1810 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1811 * The "pad3" member is the beginning of the above. */
1812 uint8_t pad3[4]; /* Not used. */
1813};
1814OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1815
1816/* Action structure for NXAST_REG_MOVE.
1817 *
1818 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1819 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1820 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1821 * the next most significant bit, and so on.
1822 *
1823 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1824 * sense to use nxm_hasmask=1 because the action does not do any kind of
1825 * matching; it uses the actual value of a field.)
1826 *
1827 * The following nxm_header values are potentially acceptable as 'src':
1828 *
1829 * - NXM_OF_IN_PORT
1830 * - NXM_OF_ETH_DST
1831 * - NXM_OF_ETH_SRC
1832 * - NXM_OF_ETH_TYPE
1833 * - NXM_OF_VLAN_TCI
1834 * - NXM_OF_IP_TOS
1835 * - NXM_OF_IP_PROTO
1836 * - NXM_OF_IP_SRC
1837 * - NXM_OF_IP_DST
1838 * - NXM_OF_TCP_SRC
1839 * - NXM_OF_TCP_DST
1840 * - NXM_OF_UDP_SRC
1841 * - NXM_OF_UDP_DST
1842 * - NXM_OF_ICMP_TYPE
1843 * - NXM_OF_ICMP_CODE
1844 * - NXM_OF_ARP_OP
1845 * - NXM_OF_ARP_SPA
1846 * - NXM_OF_ARP_TPA
1847 * - NXM_NX_TUN_ID
1848 * - NXM_NX_ARP_SHA
1849 * - NXM_NX_ARP_THA
1850 * - NXM_NX_ICMPV6_TYPE
1851 * - NXM_NX_ICMPV6_CODE
1852 * - NXM_NX_ND_SLL
1853 * - NXM_NX_ND_TLL
1854 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1855 * - NXM_NX_PKT_MARK
1856 * - NXM_NX_TUN_IPV4_SRC
1857 * - NXM_NX_TUN_IPV4_DST
1858 *
1859 * The following nxm_header values are potentially acceptable as 'dst':
1860 *
1861 * - NXM_OF_ETH_DST
1862 * - NXM_OF_ETH_SRC
1863 * - NXM_OF_IP_TOS
1864 * - NXM_OF_IP_SRC
1865 * - NXM_OF_IP_DST
1866 * - NXM_OF_TCP_SRC
1867 * - NXM_OF_TCP_DST
1868 * - NXM_OF_UDP_SRC
1869 * - NXM_OF_UDP_DST
1870 * - NXM_NX_ARP_SHA
1871 * - NXM_NX_ARP_THA
1872 * - NXM_OF_ARP_OP
1873 * - NXM_OF_ARP_SPA
1874 * - NXM_OF_ARP_TPA
1875 * Modifying any of the above fields changes the corresponding packet
1876 * header.
1877 *
1878 * - NXM_OF_IN_PORT
1879 *
1880 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1881 *
1882 * - NXM_NX_PKT_MARK
1883 *
1884 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1885 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1886 * header (if any), ignoring the other bits. Setting a value with CFI=1
1887 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1888 * to the field's new value (with the CFI bit masked out).
1889 *
1890 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1891 * any of these values modifies the corresponding tunnel header field used
1892 * for the packet's next tunnel encapsulation, if allowed by the
1893 * configuration of the output tunnel port.
1894 *
1895 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1896 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1897 * used only if the flow's nx_match includes an nxm_entry that specifies
1898 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1899 *
1900 * The switch will reject actions for which src_ofs+n_bits is greater than the
1901 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1902 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1903 *
1904 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1905 * behaves as if 'src' were copied out to a temporary buffer, then the
1906 * temporary buffer copied to 'dst'.
1907 */
1908struct nx_action_reg_move {
1909 ovs_be16 type; /* OFPAT_VENDOR. */
1910 ovs_be16 len; /* Length is 24. */
1911 ovs_be32 vendor; /* NX_VENDOR_ID. */
1912 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1913 ovs_be16 n_bits; /* Number of bits. */
1914 ovs_be16 src_ofs; /* Starting bit offset in source. */
1915 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1916 /* Followed by:
1917 * - OXM/NXM header for source field (4 or 8 bytes).
1918 * - OXM/NXM header for destination field (4 or 8 bytes).
1919 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1920};
1921OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1922
1923static enum ofperr
1924decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1925 const void *action, ovs_be16 action_len, size_t oxm_offset,
1926 struct ofpbuf *ofpacts)
1927{
1928 struct ofpact_reg_move *move;
1929 enum ofperr error;
1930 struct ofpbuf b;
1931
1932 move = ofpact_put_REG_MOVE(ofpacts);
1933 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1934 move->src.ofs = ntohs(src_offset);
1935 move->src.n_bits = ntohs(n_bits);
1936 move->dst.ofs = ntohs(dst_offset);
1937 move->dst.n_bits = ntohs(n_bits);
1938
1939 ofpbuf_use_const(&b, action, ntohs(action_len));
1940 ofpbuf_pull(&b, oxm_offset);
1941 error = nx_pull_header(&b, &move->src.field, NULL);
1942 if (error) {
1943 return error;
1944 }
1945 error = nx_pull_header(&b, &move->dst.field, NULL);
1946 if (error) {
1947 return error;
1948 }
1949
1950 if (!is_all_zeros(b.data, b.size)) {
1951 return OFPERR_NXBRC_MUST_BE_ZERO;
1952 }
1953
1954 return nxm_reg_move_check(move, NULL);
1955}
1956
1957static enum ofperr
1958decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1959 enum ofp_version ofp_version OVS_UNUSED,
1960 struct ofpbuf *ofpacts)
1961{
1962 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1963 oacf->n_bits, oacf, oacf->len,
1964 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1965}
1966
1967static enum ofperr
1968decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1969 enum ofp_version ofp_version OVS_UNUSED,
1970 struct ofpbuf *ofpacts)
1971{
1972 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1973 oacf->n_bits, oacf, oacf->len,
1974 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1975}
1976
1977static enum ofperr
1978decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1979 enum ofp_version ofp_version OVS_UNUSED,
1980 struct ofpbuf *ofpacts)
1981{
1982 struct ofpact_reg_move *move;
1983 enum ofperr error;
1984 struct ofpbuf b;
1985
1986 move = ofpact_put_REG_MOVE(ofpacts);
1987 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1988 move->src.ofs = ntohs(narm->src_ofs);
1989 move->src.n_bits = ntohs(narm->n_bits);
1990 move->dst.ofs = ntohs(narm->dst_ofs);
1991 move->dst.n_bits = ntohs(narm->n_bits);
1992
1993 ofpbuf_use_const(&b, narm, ntohs(narm->len));
1994 ofpbuf_pull(&b, sizeof *narm);
1995 error = nx_pull_header(&b, &move->src.field, NULL);
1996 if (error) {
1997 return error;
1998 }
1999 error = nx_pull_header(&b, &move->dst.field, NULL);
2000 if (error) {
2001 return error;
2002 }
2003 if (!is_all_zeros(b.data, b.size)) {
2004 return OFPERR_NXBRC_MUST_BE_ZERO;
2005 }
2006
2007 return nxm_reg_move_check(move, NULL);
2008}
2009
2010static void
2011encode_REG_MOVE(const struct ofpact_reg_move *move,
2012 enum ofp_version ofp_version, struct ofpbuf *out)
2013{
2014 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2015 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2016 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2017 * probe for support. Until we have that ability, we currently prefer
2018 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2019 * versions. */
2020 size_t start_ofs = out->size;
2021 if (ofp_version >= OFP15_VERSION) {
2022 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2023 copy->n_bits = htons(move->dst.n_bits);
2024 copy->src_offset = htons(move->src.ofs);
2025 copy->dst_offset = htons(move->dst.ofs);
2026 out->size = out->size - sizeof copy->pad2;
2027 nx_put_header(out, move->src.field->id, ofp_version, false);
2028 nx_put_header(out, move->dst.field->id, ofp_version, false);
2029 } else if (ofp_version == OFP13_VERSION
2030 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2031 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2032 copy->n_bits = htons(move->dst.n_bits);
2033 copy->src_offset = htons(move->src.ofs);
2034 copy->dst_offset = htons(move->dst.ofs);
2035 out->size = out->size - sizeof copy->pad3;
2036 nx_put_header(out, move->src.field->id, ofp_version, false);
2037 nx_put_header(out, move->dst.field->id, ofp_version, false);
2038 } else {
2039 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2040 narm->n_bits = htons(move->dst.n_bits);
2041 narm->src_ofs = htons(move->src.ofs);
2042 narm->dst_ofs = htons(move->dst.ofs);
2043 nx_put_header(out, move->src.field->id, 0, false);
2044 nx_put_header(out, move->dst.field->id, 0, false);
2045 }
2046 pad_ofpat(out, start_ofs);
2047}
2048
2049static char * OVS_WARN_UNUSED_RESULT
2050parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2051 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2052{
2053 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2054 const char *full_arg = arg;
2055 char *error;
2056
2057 error = mf_parse_subfield__(&move->src, &arg);
2058 if (error) {
2059 return error;
2060 }
2061 if (strncmp(arg, "->", 2)) {
2062 return xasprintf("%s: missing `->' following source", full_arg);
2063 }
2064 arg += 2;
2065 error = mf_parse_subfield(&move->dst, arg);
2066 if (error) {
2067 return error;
2068 }
2069
2070 if (move->src.n_bits != move->dst.n_bits) {
2071 return xasprintf("%s: source field is %d bits wide but destination is "
2072 "%d bits wide", full_arg,
2073 move->src.n_bits, move->dst.n_bits);
2074 }
2075 return NULL;
2076}
2077
2078static void
2079format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2080{
2081 nxm_format_reg_move(a, s);
2082}
2083\f
2084/* Action structure for OFPAT12_SET_FIELD. */
2085struct ofp12_action_set_field {
2086 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2087 ovs_be16 len; /* Length is padded to 64 bits. */
2088
2089 /* Followed by:
2090 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2091 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2092 *
2093 * The "pad" member is the beginning of the above. */
2094 uint8_t pad[4];
2095};
2096OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2097
2098/* Action structure for NXAST_REG_LOAD.
2099 *
2100 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2101 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2102 * starts at 0 for the least-significant bit, 1 for the next most significant
2103 * bit, and so on.
2104 *
2105 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2106 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2107 * loading them.
2108 *
2109 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2110 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2111 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2112 * also stored as one less than its true value:
2113 *
2114 * 15 6 5 0
2115 * +------------------------------+------------------+
2116 * | ofs | n_bits - 1 |
2117 * +------------------------------+------------------+
2118 *
2119 * The switch will reject actions for which ofs+n_bits is greater than the
2120 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2121 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2122 * OFPBAC_BAD_ARGUMENT.
2123 */
2124struct nx_action_reg_load {
2125 ovs_be16 type; /* OFPAT_VENDOR. */
2126 ovs_be16 len; /* Length is 24. */
2127 ovs_be32 vendor; /* NX_VENDOR_ID. */
2128 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2129 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2130 ovs_be32 dst; /* Destination register. */
2131 ovs_be64 value; /* Immediate value. */
2132};
2133OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2134
2135/* Action structure for NXAST_REG_LOAD2.
2136 *
2137 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2138 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2139 * OXM experimenter fields. */
2140struct nx_action_reg_load2 {
2141 ovs_be16 type; /* OFPAT_VENDOR. */
2142 ovs_be16 len; /* At least 16. */
2143 ovs_be32 vendor; /* NX_VENDOR_ID. */
2144 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2145
2146 /* Followed by:
2147 * - An NXM/OXM header, value, and optionally a mask.
2148 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2149 *
2150 * The "pad" member is the beginning of the above. */
2151 uint8_t pad[6];
2152};
2153OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2154
2155static enum ofperr
2156decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2157 bool may_mask, struct ofpbuf *ofpacts)
2158{
2159 struct ofpact_set_field *sf;
2160 enum ofperr error;
2161 struct ofpbuf b;
2162
2163 sf = ofpact_put_SET_FIELD(ofpacts);
2164
2165 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2166 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2167 error = nx_pull_entry(&b, &sf->field, &sf->value,
2168 may_mask ? &sf->mask : NULL);
2169 if (error) {
2170 return (error == OFPERR_OFPBMC_BAD_MASK
2171 ? OFPERR_OFPBAC_BAD_SET_MASK
2172 : error);
2173 }
2174 if (!may_mask) {
2175 memset(&sf->mask, 0xff, sf->field->n_bytes);
2176 }
2177
2178 if (!is_all_zeros(b.data, b.size)) {
2179 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2180 }
2181
2182 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2183 * Set-Field. */
2184 if (sf->field->id == MFF_IN_PORT_OXM) {
2185 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2186 }
2187
2188 /* oxm_length is now validated to be compatible with mf_value. */
2189 if (!sf->field->writable) {
2190 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2191 sf->field->name);
2192 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2193 }
2194
2195 /* The value must be valid for match. OpenFlow 1.5 also says,
2196 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2197 * a 1-bit in oxm_value and in oxm_mask." */
2198 if (!mf_is_value_valid(sf->field, &sf->value)
2199 || (sf->field->id == MFF_VLAN_VID
2200 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2201 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2202 struct ds ds = DS_EMPTY_INITIALIZER;
2203 mf_format(sf->field, &sf->value, NULL, &ds);
2204 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2205 sf->field->name, ds_cstr(&ds));
2206 ds_destroy(&ds);
2207
2208 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2209 }
2210 return 0;
2211}
2212
2213static enum ofperr
2214decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2215 enum ofp_version ofp_version OVS_UNUSED,
2216 struct ofpbuf *ofpacts)
2217{
2218 return decode_ofpat_set_field(oasf, false, ofpacts);
2219}
2220
2221static enum ofperr
2222decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2223 enum ofp_version ofp_version OVS_UNUSED,
2224 struct ofpbuf *ofpacts)
2225{
2226 return decode_ofpat_set_field(oasf, true, ofpacts);
2227}
2228
2229static enum ofperr
2230decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2231 enum ofp_version ofp_version OVS_UNUSED,
2232 struct ofpbuf *out)
2233{
2234 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2235 struct mf_subfield dst;
2236 enum ofperr error;
2237
2238 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2239
2240 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2241 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2242 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2243 error = mf_check_dst(&dst, NULL);
2244 if (error) {
2245 return error;
2246 }
2247
2248 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2249 * narl->value. */
2250 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2251 return OFPERR_OFPBAC_BAD_ARGUMENT;
2252 }
2253
2254 sf->field = dst.field;
2255 bitwise_put(ntohll(narl->value),
2256 &sf->value, dst.field->n_bytes, dst.ofs,
2257 dst.n_bits);
2258 bitwise_put(UINT64_MAX,
2259 &sf->mask, dst.field->n_bytes, dst.ofs,
2260 dst.n_bits);
2261
2262 return 0;
2263}
2264
2265static enum ofperr
2266decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2267 enum ofp_version ofp_version OVS_UNUSED,
2268 struct ofpbuf *out)
2269{
2270 struct ofpact_set_field *sf;
2271 enum ofperr error;
2272 struct ofpbuf b;
2273
2274 sf = ofpact_put_SET_FIELD(out);
2275 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2276
2277 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2278 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2279 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2280 if (error) {
2281 return error;
2282 }
2283 if (!is_all_zeros(b.data, b.size)) {
2284 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2285 }
2286
2287 if (!sf->field->writable) {
2288 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2289 sf->field->name);
2290 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2291 }
2292 return 0;
2293}
2294
2295static void
2296ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2297 enum mf_field_id field, uint64_t value_)
2298{
2299 struct ofp12_action_set_field *oasf OVS_UNUSED;
2300 int n_bytes = mf_from_id(field)->n_bytes;
2301 size_t start_ofs = openflow->size;
2302 union mf_value value;
2303
2304 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2305
2306 oasf = put_OFPAT12_SET_FIELD(openflow);
2307 openflow->size = openflow->size - sizeof oasf->pad;
2308 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2309 pad_ofpat(openflow, start_ofs);
2310}
2311
2312static bool
2313next_load_segment(const struct ofpact_set_field *sf,
2314 struct mf_subfield *dst, uint64_t *value)
2315{
2316 int n_bits = sf->field->n_bits;
2317 int n_bytes = sf->field->n_bytes;
2318 int start = dst->ofs + dst->n_bits;
2319
2320 if (start < n_bits) {
2321 dst->field = sf->field;
2322 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2323 if (dst->ofs < n_bits) {
2324 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2325 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2326 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2327 return true;
2328 }
2329 }
2330 return false;
2331}
2332
2333/* Convert 'sf' to a series of REG_LOADs. */
2334static void
2335set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2336{
2337 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2338 * experimenter OXM or is variable length (or if it came in as
2339 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2340 * NXAST_REG_LOAD, which is backward compatible. */
2341 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2342 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2343 struct nx_action_reg_load2 *narl OVS_UNUSED;
2344 size_t start_ofs = openflow->size;
2345
2346 narl = put_NXAST_REG_LOAD2(openflow);
2347 openflow->size = openflow->size - sizeof narl->pad;
2348 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2349 pad_ofpat(openflow, start_ofs);
2350 } else {
2351 struct mf_subfield dst;
2352 uint64_t value;
2353
2354 dst.ofs = dst.n_bits = 0;
2355 while (next_load_segment(sf, &dst, &value)) {
2356 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2357 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2358 narl->dst = htonl(mf_nxm_header(dst.field->id));
2359 narl->value = htonll(value);
2360 }
2361 }
2362}
2363
2364/* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2365 * actions, if we can, falling back to Nicira extensions if we must.
2366 *
2367 * We check only meta-flow types that can appear within set field actions and
2368 * that have a mapping to compatible action types. These struct mf_field
2369 * definitions have a defined OXM or NXM header value and specify the field as
2370 * writable. */
2371static void
2372set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2373 enum ofp_version ofp_version,
2374 struct ofpbuf *out)
2375{
2376 switch ((int) sf->field->id) {
2377 case MFF_VLAN_TCI: {
2378 ovs_be16 tci = sf->value.be16;
2379 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2380 uint16_t vid = vlan_tci_to_vid(tci);
2381 uint8_t pcp = vlan_tci_to_pcp(tci);
2382
2383 if (ofp_version < OFP11_VERSION) {
2384 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2385 *
2386 * If CFI=1, Add or modify VLAN VID & PCP.
2387 * If CFI=0, strip VLAN header, if any.
2388 */
2389 if (cfi) {
2390 put_OFPAT10_SET_VLAN_VID(out, vid);
2391 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2392 } else {
2393 put_OFPAT10_STRIP_VLAN(out);
2394 }
2395 } else {
2396 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2397 *
2398 * If CFI=1, Add or modify VLAN VID & PCP.
2399 * OpenFlow 1.1 set actions only apply if the packet
2400 * already has VLAN tags. To be sure that is the case
2401 * we have to push a VLAN header. As we do not support
2402 * multiple layers of VLANs, this is a no-op, if a VLAN
2403 * header already exists. This may backfire, however,
2404 * when we start supporting multiple layers of VLANs.
2405 * If CFI=0, strip VLAN header, if any.
2406 */
2407 if (cfi) {
2408 /* Push a VLAN tag, if one was not seen at action validation
2409 * time. */
2410 if (!sf->flow_has_vlan) {
2411 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2412 }
2413 put_OFPAT11_SET_VLAN_VID(out, vid);
2414 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2415 } else {
2416 /* If the flow did not match on vlan, we have no way of
2417 * knowing if the vlan tag exists, so we must POP just to be
2418 * sure. */
2419 put_OFPAT11_POP_VLAN(out);
2420 }
2421 }
2422 break;
2423 }
2424
2425 case MFF_VLAN_VID: {
2426 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2427 if (ofp_version == OFP10_VERSION) {
2428 put_OFPAT10_SET_VLAN_VID(out, vid);
2429 } else {
2430 put_OFPAT11_SET_VLAN_VID(out, vid);
2431 }
2432 break;
2433 }
2434
2435 case MFF_VLAN_PCP:
2436 if (ofp_version == OFP10_VERSION) {
2437 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2438 } else {
2439 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2440 }
2441 break;
2442
2443 case MFF_ETH_SRC:
2444 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2445 break;
2446
2447 case MFF_ETH_DST:
2448 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2449 break;
2450
2451 case MFF_IPV4_SRC:
2452 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2453 break;
2454
2455 case MFF_IPV4_DST:
2456 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2457 break;
2458
2459 case MFF_IP_DSCP:
2460 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2461 break;
2462
2463 case MFF_IP_DSCP_SHIFTED:
2464 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2465 break;
2466
2467 case MFF_TCP_SRC:
2468 case MFF_UDP_SRC:
2469 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2470 break;
2471
2472 case MFF_TCP_DST:
2473 case MFF_UDP_DST:
2474 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2475 break;
2476
2477 default:
2478 set_field_to_nxast(sf, out);
2479 break;
2480 }
2481}
2482
2483static void
2484set_field_to_set_field(const struct ofpact_set_field *sf,
2485 enum ofp_version ofp_version, struct ofpbuf *out)
2486{
2487 struct ofp12_action_set_field *oasf OVS_UNUSED;
2488 size_t start_ofs = out->size;
2489
2490 oasf = put_OFPAT12_SET_FIELD(out);
2491 out->size = out->size - sizeof oasf->pad;
2492 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2493 pad_ofpat(out, start_ofs);
2494}
2495
2496static void
2497encode_SET_FIELD(const struct ofpact_set_field *sf,
2498 enum ofp_version ofp_version, struct ofpbuf *out)
2499{
2500 if (ofp_version >= OFP15_VERSION) {
2501 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2502 * entirely). */
2503 set_field_to_set_field(sf, ofp_version, out);
2504 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2505 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2506 /* It came in as reg_load, send it out the same way. */
2507 set_field_to_nxast(sf, out);
2508 } else if (ofp_version < OFP12_VERSION) {
2509 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2510 set_field_to_legacy_openflow(sf, ofp_version, out);
2511 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2512 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2513 * entire field, so encode it as OFPAT_SET_FIELD. */
2514 set_field_to_set_field(sf, ofp_version, out);
2515 } else {
2516 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2517 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2518 * so encode it as reg_load. */
2519 set_field_to_nxast(sf, out);
2520 }
2521}
2522
2523/* Parses the input argument 'arg' into the key, value, and delimiter
2524 * components that are common across the reg_load and set_field action format.
2525 *
2526 * With an argument like "1->metadata", sets the following pointers to
2527 * point within 'arg':
2528 * key: "metadata"
2529 * value: "1"
2530 * delim: "->"
2531 *
2532 * Returns NULL if successful, otherwise a malloc()'d string describing the
2533 * error. The caller is responsible for freeing the returned string. */
2534static char * OVS_WARN_UNUSED_RESULT
2535set_field_split_str(char *arg, char **key, char **value, char **delim)
2536{
2537 char *value_end;
2538
2539 *value = arg;
2540 value_end = strstr(arg, "->");
2541 *key = value_end + strlen("->");
2542 if (delim) {
2543 *delim = value_end;
2544 }
2545
2546 if (!value_end) {
2547 return xasprintf("%s: missing `->'", arg);
2548 }
2549 if (strlen(value_end) <= strlen("->")) {
2550 return xasprintf("%s: missing field name following `->'", arg);
2551 }
2552
2553 return NULL;
2554}
2555
2556/* Parses a "set_field" action with argument 'arg', appending the parsed
2557 * action to 'ofpacts'.
2558 *
2559 * Returns NULL if successful, otherwise a malloc()'d string describing the
2560 * error. The caller is responsible for freeing the returned string. */
2561static char * OVS_WARN_UNUSED_RESULT
2562set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2563 enum ofputil_protocol *usable_protocols)
2564{
2565 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2566 char *value;
2567 char *delim;
2568 char *key;
2569 const struct mf_field *mf;
2570 char *error;
2571
2572 error = set_field_split_str(arg, &key, &value, &delim);
2573 if (error) {
2574 return error;
2575 }
2576
2577 mf = mf_from_name(key);
2578 if (!mf) {
2579 return xasprintf("%s is not a valid OXM field name", key);
2580 }
2581 if (!mf->writable) {
2582 return xasprintf("%s is read-only", key);
2583 }
2584 sf->field = mf;
2585 delim[0] = '\0';
2586 error = mf_parse(mf, value, &sf->value, &sf->mask);
2587 if (error) {
2588 return error;
2589 }
2590
2591 if (!mf_is_value_valid(mf, &sf->value)) {
2592 return xasprintf("%s is not a valid value for field %s", value, key);
2593 }
2594
2595 *usable_protocols &= mf->usable_protocols_exact;
2596 return NULL;
2597}
2598
2599/* Parses 'arg' as the argument to a "set_field" action, and appends such an
2600 * action to 'ofpacts'.
2601 *
2602 * Returns NULL if successful, otherwise a malloc()'d string describing the
2603 * error. The caller is responsible for freeing the returned string. */
2604static char * OVS_WARN_UNUSED_RESULT
2605parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2606 enum ofputil_protocol *usable_protocols)
2607{
2608 char *copy = xstrdup(arg);
2609 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2610 free(copy);
2611 return error;
2612}
2613
2614static char * OVS_WARN_UNUSED_RESULT
2615parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2616{
2617 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2618 struct mf_subfield dst;
2619 char *key, *value_str;
2620 union mf_value value;
2621 char *error;
2622
2623 error = set_field_split_str(arg, &key, &value_str, NULL);
2624 if (error) {
2625 return error;
2626 }
2627
2628 error = mf_parse_subfield(&dst, key);
2629 if (error) {
2630 return error;
2631 }
2632
2633 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2634 &key)) {
2635 return xasprintf("%s: cannot parse integer value", arg);
2636 }
2637
2638 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2639 dst.field->n_bytes * 8 - dst.n_bits)) {
2640 struct ds ds;
2641
2642 ds_init(&ds);
2643 mf_format(dst.field, &value, NULL, &ds);
2644 error = xasprintf("%s: value %s does not fit into %d bits",
2645 arg, ds_cstr(&ds), dst.n_bits);
2646 ds_destroy(&ds);
2647 return error;
2648 }
2649
2650 sf->field = dst.field;
2651 memset(&sf->value, 0, sizeof sf->value);
2652 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2653 dst.field->n_bytes, dst.ofs, dst.n_bits);
2654 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2655
2656 return NULL;
2657}
2658
2659static void
2660format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2661{
2662 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2663 struct mf_subfield dst;
2664 uint64_t value;
2665
2666 dst.ofs = dst.n_bits = 0;
2667 while (next_load_segment(a, &dst, &value)) {
2668 ds_put_format(s, "load:%#"PRIx64"->", value);
2669 mf_format_subfield(&dst, s);
2670 ds_put_char(s, ',');
2671 }
2672 ds_chomp(s, ',');
2673 } else {
2674 ds_put_cstr(s, "set_field:");
2675 mf_format(a->field, &a->value, &a->mask, s);
2676 ds_put_format(s, "->%s", a->field->name);
2677 }
2678}
2679
2680/* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2681 * is marked such that, if possible, it will be translated to OpenFlow as
2682 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2683 * that was the way that the action was expressed when it came into OVS or for
2684 * backward compatibility. */
2685struct ofpact_set_field *
2686ofpact_put_reg_load(struct ofpbuf *ofpacts)
2687{
2688 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2689 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2690 return sf;
2691}
2692\f
2693/* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2694 *
2695 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2696 * top of the stack.
2697 */
2698struct nx_action_stack {
2699 ovs_be16 type; /* OFPAT_VENDOR. */
2700 ovs_be16 len; /* Length is 16. */
2701 ovs_be32 vendor; /* NX_VENDOR_ID. */
2702 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2703 ovs_be16 offset; /* Bit offset into the field. */
2704 /* Followed by:
2705 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2706 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2707 * - Enough 0-bytes to pad out the action to 24 bytes. */
2708 uint8_t pad[12]; /* See above. */
2709};
2710OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2711
2712static enum ofperr
2713decode_stack_action(const struct nx_action_stack *nasp,
2714 struct ofpact_stack *stack_action)
2715{
2716 enum ofperr error;
2717 struct ofpbuf b;
2718
2719 stack_action->subfield.ofs = ntohs(nasp->offset);
2720
2721 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2722 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2723 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2724 if (error) {
2725 return error;
2726 }
2727 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2728 ofpbuf_pull(&b, 2);
2729 if (!is_all_zeros(b.data, b.size)) {
2730 return OFPERR_NXBRC_MUST_BE_ZERO;
2731 }
2732
2733 return 0;
2734}
2735
2736static enum ofperr
2737decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2738 enum ofp_version ofp_version OVS_UNUSED,
2739 struct ofpbuf *ofpacts)
2740{
2741 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2742 enum ofperr error = decode_stack_action(nasp, push);
2743 return error ? error : nxm_stack_push_check(push, NULL);
2744}
2745
2746static enum ofperr
2747decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2748 enum ofp_version ofp_version OVS_UNUSED,
2749 struct ofpbuf *ofpacts)
2750{
2751 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2752 enum ofperr error = decode_stack_action(nasp, pop);
2753 return error ? error : nxm_stack_pop_check(pop, NULL);
2754}
2755
2756static void
2757encode_STACK_op(const struct ofpact_stack *stack_action,
2758 struct nx_action_stack *nasp)
2759{
2760 struct ofpbuf b;
2761 ovs_be16 n_bits;
2762
2763 nasp->offset = htons(stack_action->subfield.ofs);
2764
2765 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2766 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2767 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2768 n_bits = htons(stack_action->subfield.n_bits);
2769 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2770}
2771
2772static void
2773encode_STACK_PUSH(const struct ofpact_stack *stack,
2774 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2775{
2776 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2777}
2778
2779static void
2780encode_STACK_POP(const struct ofpact_stack *stack,
2781 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2782{
2783 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2784}
2785
2786static char * OVS_WARN_UNUSED_RESULT
2787parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2788 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2789{
2790 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2791}
2792
2793static char * OVS_WARN_UNUSED_RESULT
2794parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2795 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2796{
2797 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2798}
2799
2800static void
2801format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2802{
2803 nxm_format_stack_push(a, s);
2804}
2805
2806static void
2807format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2808{
2809 nxm_format_stack_pop(a, s);
2810}
2811\f
2812/* Action structure for NXAST_DEC_TTL_CNT_IDS.
2813 *
2814 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2815 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2816 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2817 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2818 *
2819 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2820 * sent only to controllers with id 0.)
2821 */
2822struct nx_action_cnt_ids {
2823 ovs_be16 type; /* OFPAT_VENDOR. */
2824 ovs_be16 len; /* Length including slaves. */
2825 ovs_be32 vendor; /* NX_VENDOR_ID. */
2826 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2827
2828 ovs_be16 n_controllers; /* Number of controllers. */
2829 uint8_t zeros[4]; /* Must be zero. */
2830
2831 /* Followed by 1 or more controller ids.
2832 *
2833 * uint16_t cnt_ids[]; // Controller ids.
2834 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2835 */
2836};
2837OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2838
2839static enum ofperr
2840decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2841{
2842 uint16_t id = 0;
2843 struct ofpact_cnt_ids *ids;
2844 enum ofperr error = 0;
2845
2846 ids = ofpact_put_DEC_TTL(out);
2847 ids->n_controllers = 1;
2848 ofpbuf_put(out, &id, sizeof id);
2849 ids = out->header;
2850 ofpact_update_len(out, &ids->ofpact);
2851 return error;
2852}
2853
2854static enum ofperr
2855decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2856 enum ofp_version ofp_version OVS_UNUSED,
2857 struct ofpbuf *out)
2858{
2859 struct ofpact_cnt_ids *ids;
2860 size_t ids_size;
2861 int i;
2862
2863 ids = ofpact_put_DEC_TTL(out);
2864 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2865 ids->n_controllers = ntohs(nac_ids->n_controllers);
2866 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2867
2868 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2869 return OFPERR_NXBRC_MUST_BE_ZERO;
2870 }
2871
2872 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2873 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2874 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2875 "are required for %"PRIu16" controllers.",
2876 ids_size, ids->n_controllers * sizeof(ovs_be16),
2877 ids->n_controllers);
2878 return OFPERR_OFPBAC_BAD_LEN;
2879 }
2880
2881 for (i = 0; i < ids->n_controllers; i++) {
2882 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2883 ofpbuf_put(out, &id, sizeof id);
2884 ids = out->header;
2885 }
2886
2887 ofpact_update_len(out, &ids->ofpact);
2888
2889 return 0;
2890}
2891
2892static void
2893encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2894 enum ofp_version ofp_version, struct ofpbuf *out)
2895{
2896 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2897 || dec_ttl->n_controllers != 1
2898 || dec_ttl->cnt_ids[0] != 0) {
2899 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2900 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2901 ovs_be16 *ids;
2902 size_t i;
2903
2904 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2905 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2906
2907 ids = ofpbuf_put_zeros(out, ids_len);
2908 for (i = 0; i < dec_ttl->n_controllers; i++) {
2909 ids[i] = htons(dec_ttl->cnt_ids[i]);
2910 }
2911 } else {
2912 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2913 }
2914}
2915
2916static void
2917parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2918{
2919 struct ofpact_cnt_ids *ids;
2920 uint16_t id = 0;
2921
2922 ofpact_put_DEC_TTL(ofpacts);
2923 ofpbuf_put(ofpacts, &id, sizeof id);
2924 ids = ofpacts->header;
2925 ids->n_controllers++;
2926 ofpact_update_len(ofpacts, &ids->ofpact);
2927}
2928
2929static char * OVS_WARN_UNUSED_RESULT
2930parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2931 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2932{
2933 if (*arg == '\0') {
2934 parse_noargs_dec_ttl(ofpacts);
2935 } else {
2936 struct ofpact_cnt_ids *ids;
2937 char *cntr;
2938
2939 ids = ofpact_put_DEC_TTL(ofpacts);
2940 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2941 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2942 cntr = strtok_r(NULL, ", ", &arg)) {
2943 uint16_t id = atoi(cntr);
2944
2945 ofpbuf_put(ofpacts, &id, sizeof id);
2946 ids = ofpacts->header;
2947 ids->n_controllers++;
2948 }
2949 if (!ids->n_controllers) {
2950 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2951 "id.");
2952 }
2953 ofpact_update_len(ofpacts, &ids->ofpact);
2954 }
2955 return NULL;
2956}
2957
2958static void
2959format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2960{
2961 size_t i;
2962
2963 ds_put_cstr(s, "dec_ttl");
2964 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2965 ds_put_cstr(s, "(");
2966 for (i = 0; i < a->n_controllers; i++) {
2967 if (i) {
2968 ds_put_cstr(s, ",");
2969 }
2970 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2971 }
2972 ds_put_cstr(s, ")");
2973 }
2974}
2975\f
2976/* Set MPLS label actions. */
2977
2978static enum ofperr
2979decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
2980 enum ofp_version ofp_version OVS_UNUSED,
2981 struct ofpbuf *out)
2982{
2983 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2984 return 0;
2985}
2986
2987static void
2988encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2989 enum ofp_version ofp_version,
2990 struct ofpbuf *out)
2991{
2992 if (ofp_version < OFP12_VERSION) {
2993 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2994 } else {
2995 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2996 ntohl(label->label));
2997 }
2998}
2999
3000static char * OVS_WARN_UNUSED_RESULT
3001parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3002 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3003{
3004 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3005 if (*arg == '\0') {
3006 return xstrdup("set_mpls_label: expected label.");
3007 }
3008
3009 mpls_label->label = htonl(atoi(arg));
3010 return NULL;
3011}
3012
3013static void
3014format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3015{
3016 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3017}
3018\f
3019/* Set MPLS TC actions. */
3020
3021static enum ofperr
3022decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3023 enum ofp_version ofp_version OVS_UNUSED,
3024 struct ofpbuf *out)
3025{
3026 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3027 return 0;
3028}
3029
3030static void
3031encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3032 enum ofp_version ofp_version, struct ofpbuf *out)
3033{
3034 if (ofp_version < OFP12_VERSION) {
3035 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3036 } else {
3037 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3038 }
3039}
3040
3041static char * OVS_WARN_UNUSED_RESULT
3042parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3043 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3044{
3045 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3046
3047 if (*arg == '\0') {
3048 return xstrdup("set_mpls_tc: expected tc.");
3049 }
3050
3051 mpls_tc->tc = atoi(arg);
3052 return NULL;
3053}
3054
3055static void
3056format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3057{
3058 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3059}
3060\f
3061/* Set MPLS TTL actions. */
3062
3063static enum ofperr
3064decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3065 enum ofp_version ofp_version OVS_UNUSED,
3066 struct ofpbuf *out)
3067{
3068 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3069 return 0;
3070}
3071
3072static void
3073encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3074 enum ofp_version ofp_version, struct ofpbuf *out)
3075{
3076 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3077}
3078
3079/* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3080 * action to 'ofpacts'.
3081 *
3082 * Returns NULL if successful, otherwise a malloc()'d string describing the
3083 * error. The caller is responsible for freeing the returned string. */
3084static char * OVS_WARN_UNUSED_RESULT
3085parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3086 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3087{
3088 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3089
3090 if (*arg == '\0') {
3091 return xstrdup("set_mpls_ttl: expected ttl.");
3092 }
3093
3094 mpls_ttl->ttl = atoi(arg);
3095 return NULL;
3096}
3097
3098static void
3099format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3100{
3101 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3102}
3103\f
3104/* Decrement MPLS TTL actions. */
3105
3106static enum ofperr
3107decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3108{
3109 ofpact_put_DEC_MPLS_TTL(out);
3110 return 0;
3111}
3112
3113static void
3114encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3115 enum ofp_version ofp_version, struct ofpbuf *out)
3116{
3117 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3118}
3119
3120static char * OVS_WARN_UNUSED_RESULT
3121parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3122 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3123{
3124 ofpact_put_DEC_MPLS_TTL(ofpacts);
3125 return NULL;
3126}
3127
3128static void
3129format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3130{
3131 ds_put_cstr(s, "dec_mpls_ttl");
3132}
3133\f
3134/* Push MPLS label action. */
3135
3136static enum ofperr
3137decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3138 enum ofp_version ofp_version OVS_UNUSED,
3139 struct ofpbuf *out)
3140{
3141 struct ofpact_push_mpls *oam;
3142
3143 if (!eth_type_mpls(ethertype)) {
3144 return OFPERR_OFPBAC_BAD_ARGUMENT;
3145 }
3146 oam = ofpact_put_PUSH_MPLS(out);
3147 oam->ethertype = ethertype;
3148
3149 return 0;
3150}
3151
3152static void
3153encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3154 enum ofp_version ofp_version, struct ofpbuf *out)
3155{
3156 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3157}
3158
3159static char * OVS_WARN_UNUSED_RESULT
3160parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3161 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3162{
3163 uint16_t ethertype;
3164 char *error;
3165
3166 error = str_to_u16(arg, "push_mpls", &ethertype);
3167 if (!error) {
3168 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3169 }
3170 return error;
3171}
3172
3173static void
3174format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3175{
3176 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3177}
3178\f
3179/* Pop MPLS label action. */
3180
3181static enum ofperr
3182decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3183 enum ofp_version ofp_version OVS_UNUSED,
3184 struct ofpbuf *out)
3185{
3186 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3187 return 0;
3188}
3189
3190static void
3191encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3192 enum ofp_version ofp_version, struct ofpbuf *out)
3193{
3194 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3195}
3196
3197static char * OVS_WARN_UNUSED_RESULT
3198parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3199 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3200{
3201 uint16_t ethertype;
3202 char *error;
3203
3204 error = str_to_u16(arg, "pop_mpls", &ethertype);
3205 if (!error) {
3206 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3207 }
3208 return error;
3209}
3210
3211static void
3212format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3213{
3214 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3215}
3216\f
3217/* Set tunnel ID actions. */
3218
3219static enum ofperr
3220decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3221 enum ofp_version ofp_version OVS_UNUSED,
3222 struct ofpbuf *out)
3223{
3224 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3225 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3226 tunnel->tun_id = tun_id;
3227 return 0;
3228}
3229
3230static enum ofperr
3231decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3232 enum ofp_version ofp_version OVS_UNUSED,
3233 struct ofpbuf *out)
3234{
3235 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3236 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3237 tunnel->tun_id = tun_id;
3238 return 0;
3239}
3240
3241static void
3242encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3243 enum ofp_version ofp_version, struct ofpbuf *out)
3244{
3245 uint64_t tun_id = tunnel->tun_id;
3246
3247 if (ofp_version < OFP12_VERSION) {
3248 if (tun_id <= UINT32_MAX
3249 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3250 put_NXAST_SET_TUNNEL(out, tun_id);
3251 } else {
3252 put_NXAST_SET_TUNNEL64(out, tun_id);
3253 }
3254 } else {
3255 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3256 }
3257}
3258
3259static char * OVS_WARN_UNUSED_RESULT
3260parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3261 enum ofp_raw_action_type raw)
3262{
3263 struct ofpact_tunnel *tunnel;
3264
3265 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3266 tunnel->ofpact.raw = raw;
3267 return str_to_u64(arg, &tunnel->tun_id);
3268}
3269
3270static char * OVS_WARN_UNUSED_RESULT
3271parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3272 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3273{
3274 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3275}
3276
3277static void
3278format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3279{
3280 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3281 (a->tun_id > UINT32_MAX
3282 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3283 a->tun_id);
3284}
3285\f
3286/* Set queue action. */
3287
3288static enum ofperr
3289decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3290 enum ofp_version ofp_version OVS_UNUSED,
3291 struct ofpbuf *out)
3292{
3293 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3294 return 0;
3295}
3296
3297static void
3298encode_SET_QUEUE(const struct ofpact_queue *queue,
3299 enum ofp_version ofp_version, struct ofpbuf *out)
3300{
3301 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3302}
3303
3304static char * OVS_WARN_UNUSED_RESULT
3305parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3306 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3307{
3308 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3309}
3310
3311static void
3312format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3313{
3314 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3315}
3316\f
3317/* Pop queue action. */
3318
3319static enum ofperr
3320decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3321{
3322 ofpact_put_POP_QUEUE(out);
3323 return 0;
3324}
3325
3326static void
3327encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3328 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3329{
3330 put_NXAST_POP_QUEUE(out);
3331}
3332
3333static char * OVS_WARN_UNUSED_RESULT
3334parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3335 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3336{
3337 ofpact_put_POP_QUEUE(ofpacts);
3338 return NULL;
3339}
3340
3341static void
3342format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3343{
3344 ds_put_cstr(s, "pop_queue");
3345}
3346\f
3347/* Action structure for NXAST_FIN_TIMEOUT.
3348 *
3349 * This action changes the idle timeout or hard timeout, or both, of this
3350 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3351 * When such a packet is observed, the action reduces the rule's idle timeout
3352 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3353 * action has no effect on an existing timeout that is already shorter than the
3354 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3355 * of zero has no effect on the respective timeout.
3356 *
3357 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3358 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3359 * receipt of the FIN or RST.
3360 *
3361 * This is useful for quickly discarding learned TCP flows that otherwise will
3362 * take a long time to expire.
3363 *
3364 * This action is intended for use with an OpenFlow rule that matches only a
3365 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3366 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3367 * RST in any of those flows will cause the entire OpenFlow rule to expire
3368 * early, which is not normally desirable.
3369 */
3370struct nx_action_fin_timeout {
3371 ovs_be16 type; /* OFPAT_VENDOR. */
3372 ovs_be16 len; /* 16. */
3373 ovs_be32 vendor; /* NX_VENDOR_ID. */
3374 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3375 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3376 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3377 ovs_be16 pad; /* Must be zero. */
3378};
3379OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3380
3381static enum ofperr
3382decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3383 enum ofp_version ofp_version OVS_UNUSED,
3384 struct ofpbuf *out)
3385{
3386 struct ofpact_fin_timeout *oft;
3387
3388 oft = ofpact_put_FIN_TIMEOUT(out);
3389 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3390 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3391 return 0;
3392}
3393
3394static void
3395encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3396 enum ofp_version ofp_version OVS_UNUSED,
3397 struct ofpbuf *out)
3398{
3399 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3400 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3401 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3402}
3403
3404static char * OVS_WARN_UNUSED_RESULT
3405parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3406 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3407{
3408 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3409 char *key, *value;
3410
3411 while (ofputil_parse_key_value(&arg, &key, &value)) {
3412 char *error;
3413
3414 if (!strcmp(key, "idle_timeout")) {
3415 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3416 } else if (!strcmp(key, "hard_timeout")) {
3417 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3418 } else {
3419 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3420 key);
3421 }
3422
3423 if (error) {
3424 return error;
3425 }
3426 }
3427 return NULL;
3428}
3429
3430static void
3431format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3432{
3433 ds_put_cstr(s, "fin_timeout(");
3434 if (a->fin_idle_timeout) {
3435 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3436 }
3437 if (a->fin_hard_timeout) {
3438 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3439 }
3440 ds_chomp(s, ',');
3441 ds_put_char(s, ')');
3442}
3443\f
3444/* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3445 *
3446 * These actions search one of the switch's flow tables:
3447 *
3448 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3449 * it specifies the table to search.
3450 *
3451 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3452 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3453 * table, that is, the OpenFlow flow table that contains the flow from
3454 * which this action was obtained. If this action did not come from a
3455 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3456 * is the current table.
3457 *
3458 * The flow table lookup uses a flow that may be slightly modified from the
3459 * original lookup:
3460 *
3461 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3462 * is used as the flow's in_port.
3463 *
3464 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3465 * then its value is used as the flow's in_port. Otherwise, the original
3466 * in_port is used.
3467 *
3468 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3469 * resubmit action, then the flow is updated with the new values.
3470 *
3471 * Following the lookup, the original in_port is restored.
3472 *
3473 * If the modified flow matched in the flow table, then the corresponding
3474 * actions are executed. Afterward, actions following the resubmit in the
3475 * original set of actions, if any, are executed; any changes made to the
3476 * packet (e.g. changes to VLAN) by secondary actions persist when those
3477 * actions are executed, although the original in_port is restored.
3478 *
3479 * Resubmit actions may be used any number of times within a set of actions.
3480 *
3481 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3482 * implementation-defined depth, further resubmit actions are simply ignored.
3483 *
3484 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3485 * 'pad' to be all-bits-zero.
3486 *
3487 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3488 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3489 */
3490struct nx_action_resubmit {
3491 ovs_be16 type; /* OFPAT_VENDOR. */
3492 ovs_be16 len; /* Length is 16. */
3493 ovs_be32 vendor; /* NX_VENDOR_ID. */
3494 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3495 ovs_be16 in_port; /* New in_port for checking flow table. */
3496 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3497 uint8_t pad[3];
3498};
3499OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3500
3501static enum ofperr
3502decode_NXAST_RAW_RESUBMIT(uint16_t port,
3503 enum ofp_version ofp_version OVS_UNUSED,
3504 struct ofpbuf *out)
3505{
3506 struct ofpact_resubmit *resubmit;
3507
3508 resubmit = ofpact_put_RESUBMIT(out);
3509 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3510 resubmit->in_port = u16_to_ofp(port);
3511 resubmit->table_id = 0xff;
3512 return 0;
3513}
3514
3515static enum ofperr
3516decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3517 enum ofp_version ofp_version OVS_UNUSED,
3518 struct ofpbuf *out)
3519{
3520 struct ofpact_resubmit *resubmit;
3521
3522 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3523 return OFPERR_OFPBAC_BAD_ARGUMENT;
3524 }
3525
3526 resubmit = ofpact_put_RESUBMIT(out);
3527 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3528 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3529 resubmit->table_id = nar->table;
3530 return 0;
3531}
3532
3533static void
3534encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3535 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3536{
3537 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3538
3539 if (resubmit->table_id == 0xff
3540 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3541 put_NXAST_RESUBMIT(out, in_port);
3542 } else {
3543 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3544 nar->table = resubmit->table_id;
3545 nar->in_port = htons(in_port);
3546 }
3547}
3548
3549static char * OVS_WARN_UNUSED_RESULT
3550parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3551 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3552{
3553 struct ofpact_resubmit *resubmit;
3554 char *in_port_s, *table_s;
3555
3556 resubmit = ofpact_put_RESUBMIT(ofpacts);
3557
3558 in_port_s = strsep(&arg, ",");
3559 if (in_port_s && in_port_s[0]) {
3560 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3561 return xasprintf("%s: resubmit to unknown port", in_port_s);
3562 }
3563 } else {
3564 resubmit->in_port = OFPP_IN_PORT;
3565 }
3566
3567 table_s = strsep(&arg, ",");
3568 if (table_s && table_s[0]) {
3569 uint32_t table_id = 0;
3570 char *error;
3571
3572 error = str_to_u32(table_s, &table_id);
3573 if (error) {
3574 return error;
3575 }
3576 resubmit->table_id = table_id;
3577 } else {
3578 resubmit->table_id = 255;
3579 }
3580
3581 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3582 return xstrdup("at least one \"in_port\" or \"table\" must be "
3583 "specified on resubmit");
3584 }
3585 return NULL;
3586}
3587
3588static void
3589format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3590{
3591 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3592 ds_put_cstr(s, "resubmit:");
3593 ofputil_format_port(a->in_port, s);
3594 } else {
3595 ds_put_format(s, "resubmit(");
3596 if (a->in_port != OFPP_IN_PORT) {
3597 ofputil_format_port(a->in_port, s);
3598 }
3599 ds_put_char(s, ',');
3600 if (a->table_id != 255) {
3601 ds_put_format(s, "%"PRIu8, a->table_id);
3602 }
3603 ds_put_char(s, ')');
3604 }
3605}
3606\f
3607/* Action structure for NXAST_LEARN.
3608 *
3609 * This action adds or modifies a flow in an OpenFlow table, similar to
3610 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3611 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3612 * flow's match criteria and actions are built by applying each of the series
3613 * of flow_mod_spec elements included as part of the action.
3614 *
3615 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3616 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3617 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3618 * bits from a source to a destination. In this case, the header contains
3619 * multiple fields:
3620 *
3621 * 15 14 13 12 11 10 0
3622 * +------+---+------+---------------------------------+
3623 * | 0 |src| dst | n_bits |
3624 * +------+---+------+---------------------------------+
3625 *
3626 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3627 * following table summarizes the meaning of each possible combination.
3628 * Details follow the table:
3629 *
3630 * src dst meaning
3631 * --- --- ----------------------------------------------------------
3632 * 0 0 Add match criteria based on value in a field.
3633 * 1 0 Add match criteria based on an immediate value.
3634 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3635 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3636 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3637 * All other combinations are undefined and not allowed.
3638 *
3639 * The flow_mod_spec header is followed by a source specification and a
3640 * destination specification. The format and meaning of the source
3641 * specification depends on 'src':
3642 *
3643 * - If 'src' is 0, the source bits are taken from a field in the flow to
3644 * which this action is attached. (This should be a wildcarded field. If
3645 * its value is fully specified then the source bits being copied have
3646 * constant values.)
3647 *
3648 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3649 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3650 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3651 * 'field' and 'ofs' are subject to the same restrictions as the source
3652 * field in NXAST_REG_MOVE.
3653 *
3654 * - If 'src' is 1, the source bits are a constant value. The source
3655 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3656 * number in network order, the source bits are the 'n_bits'
3657 * least-significant bits. The switch will report an error if other bits
3658 * in the constant are nonzero.
3659 *
3660 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3661 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3662 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3663 * meaning of the flow_mod_spec depends on 'dst':
3664 *
3665 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3666 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3667 * packet equal the source bits. 'field' may be any nxm_header with
3668 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3669 *
3670 * Order is significant. Earlier flow_mod_specs must satisfy any
3671 * prerequisites for matching fields specified later, by copying constant
3672 * values into prerequisite fields.
3673 *
3674 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3675 * restrictions.
3676 *
3677 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3678 * the new flow. The new flow copies the source bits into
3679 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3680 * flow_mod_specs.
3681 *
3682 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3683 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3684 *
3685 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3686 * empty. It has the following meaning:
3687 *
3688 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3689 * The new flow outputs to the OpenFlow port specified by the source field.
3690 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3691 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3692 * may not be used.
3693 *
3694 * Resource Management
3695 * -------------------
3696 *
3697 * A switch has a finite amount of flow table space available for learning.
3698 * When this space is exhausted, no new learning table entries will be learned
3699 * until some existing flow table entries expire. The controller should be
3700 * prepared to handle this by flooding (which can be implemented as a
3701 * low-priority flow).
3702 *
3703 * If a learned flow matches a single TCP stream with a relatively long
3704 * timeout, one may make the best of resource constraints by setting
3705 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3706 * both, to shorter timeouts. When either of these is specified as a nonzero
3707 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3708 * the learned flow.
3709 *
3710 * Examples
3711 * --------
3712 *
3713 * The following examples give a prose description of the flow_mod_specs along
3714 * with informal notation for how those would be represented and a hex dump of
3715 * the bytes that would be required.
3716 *
3717 * These examples could work with various nx_action_learn parameters. Typical
3718 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3719 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3720 *
3721 * 1. Learn input port based on the source MAC, with lookup into
3722 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3723 *
3724 * Match on in_port=99:
3725 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3726 * ovs_be16(99), 00 63
3727 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3728 *
3729 * Match Ethernet destination on Ethernet source from packet:
3730 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3731 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3732 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3733 *
3734 * Set NXM_NX_REG1[16:31] to the packet's input port:
3735 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3736 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3737 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3738 *
3739 * Given a packet that arrived on port A with Ethernet source address B,
3740 * this would set up the flow "in_port=99, dl_dst=B,
3741 * actions=load:A->NXM_NX_REG1[16..31]".
3742 *
3743 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3744 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3745 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3746 *
3747 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3748 * into NXM_NX_REG1[16:31]:
3749 *
3750 * Match on same VLAN ID as packet:
3751 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3752 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3753 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3754 *
3755 * Match Ethernet destination on Ethernet source from packet:
3756 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3757 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3758 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3759 *
3760 * Output to the packet's input port:
3761 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3762 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3763 *
3764 * Given a packet that arrived on port A with Ethernet source address B in
3765 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3766 * actions=output:A".
3767 *
3768 * In syntax accepted by ovs-ofctl, this action is:
3769 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3770 * output:NXM_OF_IN_PORT[])
3771 *
3772 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3773 * 10-second MAC expiration time to make it easier to see what's going on
3774 *
3775 * ovs-vsctl del-controller br0
3776 * ovs-ofctl del-flows br0
3777 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3778 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3779 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3780 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3781 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3782 *
3783 * You can then dump the MAC learning table with:
3784 *
3785 * ovs-ofctl dump-flows br0 table=1
3786 *
3787 * Usage Advice
3788 * ------------
3789 *
3790 * For best performance, segregate learned flows into a table that is not used
3791 * for any other flows except possibly for a lowest-priority "catch-all" flow
3792 * (a flow with no match criteria). If different learning actions specify
3793 * different match criteria, use different tables for the learned flows.
3794 *
3795 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3796 * These timeouts apply to the flow that is added, which means that a flow with
3797 * an idle timeout will expire when no traffic has been sent *to* the learned
3798 * address. This is not usually the intent in MAC learning; instead, we want
3799 * the MAC learn entry to expire when no traffic has been sent *from* the
3800 * learned address. Use a hard timeout for that.
3801 *
3802 *
3803 * Visibility of Changes
3804 * ---------------------
3805 *
3806 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3807 * flow translation are visible to flow table lookups made later in the flow
3808 * translation. This means that, in the example above, a MAC learned by the
3809 * learn action in table 0 would be found in table 1 (if the packet being
3810 * processed had the same source and destination MAC address).
3811 *
3812 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3813 * modify a flow) by a "learn" action are visible only for later flow
3814 * translations, not for later lookups within the same flow translation. In
3815 * the MAC learning example, a MAC learned by the learn action in table 0 would
3816 * not be found in table 1 if the flow translation would resubmit to table 1
3817 * after the processing of the learn action, meaning that if this MAC had not
3818 * been learned before then the packet would be flooded. */
3819struct nx_action_learn {
3820 ovs_be16 type; /* OFPAT_VENDOR. */
3821 ovs_be16 len; /* At least 24. */
3822 ovs_be32 vendor; /* NX_VENDOR_ID. */
3823 ovs_be16 subtype; /* NXAST_LEARN. */
3824 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3825 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3826 ovs_be16 priority; /* Priority level of flow entry. */
3827 ovs_be64 cookie; /* Cookie for new flow. */
3828 ovs_be16 flags; /* NX_LEARN_F_*. */
3829 uint8_t table_id; /* Table to insert flow entry. */
3830 uint8_t pad; /* Must be zero. */
3831 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3832 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3833 /* Followed by a sequence of flow_mod_spec elements, as described above,
3834 * until the end of the action is reached. */
3835};
3836OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3837
3838static ovs_be16
3839get_be16(const void **pp)
3840{
3841 const ovs_be16 *p = *pp;
3842 ovs_be16 value = *p;
3843 *pp = p + 1;
3844 return value;
3845}
3846
3847static ovs_be32
3848get_be32(const void **pp)
3849{
3850 const ovs_be32 *p = *pp;
3851 ovs_be32 value = get_unaligned_be32(p);
3852 *pp = p + 1;
3853 return value;
3854}
3855
3856static void
3857get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3858{
3859 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3860 sf->ofs = ntohs(get_be16(p));
3861 sf->n_bits = n_bits;
3862}
3863
3864static unsigned int
3865learn_min_len(uint16_t header)
3866{
3867 int n_bits = header & NX_LEARN_N_BITS_MASK;
3868 int src_type = header & NX_LEARN_SRC_MASK;
3869 int dst_type = header & NX_LEARN_DST_MASK;
3870 unsigned int min_len;
3871
3872 min_len = 0;
3873 if (src_type == NX_LEARN_SRC_FIELD) {
3874 min_len += sizeof(ovs_be32); /* src_field */
3875 min_len += sizeof(ovs_be16); /* src_ofs */
3876 } else {
3877 min_len += DIV_ROUND_UP(n_bits, 16);
3878 }
3879 if (dst_type == NX_LEARN_DST_MATCH ||
3880 dst_type == NX_LEARN_DST_LOAD) {
3881 min_len += sizeof(ovs_be32); /* dst_field */
3882 min_len += sizeof(ovs_be16); /* dst_ofs */
3883 }
3884 return min_len;
3885}
3886
3887/* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3888 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3889static enum ofperr
3890decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3891 enum ofp_version ofp_version OVS_UNUSED,
3892 struct ofpbuf *ofpacts)
3893{
3894 struct ofpact_learn *learn;
3895 const void *p, *end;
3896
3897 if (nal->pad) {
3898 return OFPERR_OFPBAC_BAD_ARGUMENT;
3899 }
3900
3901 learn = ofpact_put_LEARN(ofpacts);
3902
3903 learn->idle_timeout = ntohs(nal->idle_timeout);
3904 learn->hard_timeout = ntohs(nal->hard_timeout);
3905 learn->priority = ntohs(nal->priority);
3906 learn->cookie = nal->cookie;
3907 learn->table_id = nal->table_id;
3908 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3909 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3910
3911 learn->flags = ntohs(nal->flags);
3912 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3913 NX_LEARN_F_DELETE_LEARNED)) {
3914 return OFPERR_OFPBAC_BAD_ARGUMENT;
3915 }
3916
3917 if (learn->table_id == 0xff) {
3918 return OFPERR_OFPBAC_BAD_ARGUMENT;
3919 }
3920
3921 end = (char *) nal + ntohs(nal->len);
3922 for (p = nal + 1; p != end; ) {
3923 struct ofpact_learn_spec *spec;
3924 uint16_t header = ntohs(get_be16(&p));
3925
3926 if (!header) {
3927 break;
3928 }
3929
3930 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3931 learn = ofpacts->header;
3932 learn->n_specs++;
3933
3934 spec->src_type = header & NX_LEARN_SRC_MASK;
3935 spec->dst_type = header & NX_LEARN_DST_MASK;
3936 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3937
3938 /* Check for valid src and dst type combination. */
3939 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3940 spec->dst_type == NX_LEARN_DST_LOAD ||
3941 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3942 spec->src_type == NX_LEARN_SRC_FIELD)) {
3943 /* OK. */
3944 } else {
3945 return OFPERR_OFPBAC_BAD_ARGUMENT;
3946 }
3947
3948 /* Check that the arguments don't overrun the end of the action. */
3949 if ((char *) end - (char *) p < learn_min_len(header)) {
3950 return OFPERR_OFPBAC_BAD_LEN;
3951 }
3952
3953 /* Get the source. */
3954 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3955 get_subfield(spec->n_bits, &p, &spec->src);
3956 } else {
3957 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3958
3959 bitwise_copy(p, p_bytes, 0,
3960 &spec->src_imm, sizeof spec->src_imm, 0,
3961 spec->n_bits);
3962 p = (const uint8_t *) p + p_bytes;
3963 }
3964
3965 /* Get the destination. */
3966 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3967 spec->dst_type == NX_LEARN_DST_LOAD) {
3968 get_subfield(spec->n_bits, &p, &spec->dst);
3969 }
3970 }
3971 ofpact_update_len(ofpacts, &learn->ofpact);
3972
3973 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3974 return OFPERR_OFPBAC_BAD_ARGUMENT;
3975 }
3976
3977 return 0;
3978}
3979
3980static void
3981put_be16(struct ofpbuf *b, ovs_be16 x)
3982{
3983 ofpbuf_put(b, &x, sizeof x);
3984}
3985
3986static void
3987put_be32(struct ofpbuf *b, ovs_be32 x)
3988{
3989 ofpbuf_put(b, &x, sizeof x);
3990}
3991
3992static void
3993put_u16(struct ofpbuf *b, uint16_t x)
3994{
3995 put_be16(b, htons(x));
3996}
3997
3998static void
3999put_u32(struct ofpbuf *b, uint32_t x)
4000{
4001 put_be32(b, htonl(x));
4002}
4003
4004static void
4005encode_LEARN(const struct ofpact_learn *learn,
4006 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4007{
4008 const struct ofpact_learn_spec *spec;
4009 struct nx_action_learn *nal;
4010 size_t start_ofs;
4011
4012 start_ofs = out->size;
4013 nal = put_NXAST_LEARN(out);
4014 nal->idle_timeout = htons(learn->idle_timeout);
4015 nal->hard_timeout = htons(learn->hard_timeout);
4016 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4017 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4018 nal->priority = htons(learn->priority);
4019 nal->cookie = learn->cookie;
4020 nal->flags = htons(learn->flags);
4021 nal->table_id = learn->table_id;
4022
4023 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4024 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4025
4026 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4027 put_u32(out, mf_nxm_header(spec->src.field->id));
4028 put_u16(out, spec->src.ofs);
4029 } else {
4030 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4031 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4032 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4033 bits, n_dst_bytes, 0,
4034 spec->n_bits);
4035 }
4036
4037 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4038 spec->dst_type == NX_LEARN_DST_LOAD) {
4039 put_u32(out, mf_nxm_header(spec->dst.field->id));
4040 put_u16(out, spec->dst.ofs);
4041 }
4042 }
4043
4044 pad_ofpat(out, start_ofs);
4045}
4046
4047static char * OVS_WARN_UNUSED_RESULT
4048parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4049 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4050{
4051 return learn_parse(arg, ofpacts);
4052}
4053
4054static void
4055format_LEARN(const struct ofpact_learn *a, struct ds *s)
4056{
4057 learn_format(a, s);
4058}
4059\f
4060/* Action structure for NXAST_CONJUNCTION. */
4061struct nx_action_conjunction {
4062 ovs_be16 type; /* OFPAT_VENDOR. */
4063 ovs_be16 len; /* At least 16. */
4064 ovs_be32 vendor; /* NX_VENDOR_ID. */
4065 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4066 uint8_t clause;
4067 uint8_t n_clauses;
4068 ovs_be32 id;
4069};
4070OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4071
4072static void
4073add_conjunction(struct ofpbuf *out,
4074 uint32_t id, uint8_t clause, uint8_t n_clauses)
4075{
4076 struct ofpact_conjunction *oc;
4077
4078 oc = ofpact_put_CONJUNCTION(out);
4079 oc->id = id;
4080 oc->clause = clause;
4081 oc->n_clauses = n_clauses;
4082}
4083
4084static enum ofperr
4085decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4086 enum ofp_version ofp_version OVS_UNUSED,
4087 struct ofpbuf *out)
4088{
4089 if (nac->n_clauses < 2 || nac->n_clauses > 64
4090 || nac->clause >= nac->n_clauses) {
4091 return OFPERR_NXBAC_BAD_CONJUNCTION;
4092 } else {
4093 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4094 return 0;
4095 }
4096}
4097
4098static void
4099encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4100 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4101{
4102 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4103 nac->clause = oc->clause;
4104 nac->n_clauses = oc->n_clauses;
4105 nac->id = htonl(oc->id);
4106}
4107
4108static void
4109format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4110{
4111 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4112 oc->id, oc->clause + 1, oc->n_clauses);
4113}
4114
4115static char * OVS_WARN_UNUSED_RESULT
4116parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4117 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4118{
4119 uint8_t n_clauses;
4120 uint8_t clause;
4121 uint32_t id;
4122 int n;
4123
4124 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4125 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4126 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4127 }
4128
4129 if (n_clauses < 2) {
4130 return xstrdup("conjunction must have at least 2 clauses");
4131 } else if (n_clauses > 64) {
4132 return xstrdup("conjunction must have at most 64 clauses");
4133 } else if (clause < 1) {
4134 return xstrdup("clause index must be positive");
4135 } else if (clause > n_clauses) {
4136 return xstrdup("clause index must be less than or equal to "
4137 "number of clauses");
4138 }
4139
4140 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4141 return NULL;
4142}
4143\f
4144/* Action structure for NXAST_MULTIPATH.
4145 *
4146 * This action performs the following steps in sequence:
4147 *
4148 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4149 * Refer to the definition of "enum nx_mp_fields" for details.
4150 *
4151 * The 'basis' value is used as a universal hash parameter, that is,
4152 * different values of 'basis' yield different hash functions. The
4153 * particular universal hash function used is implementation-defined.
4154 *
4155 * The hashed fields' values are drawn from the current state of the
4156 * flow, including all modifications that have been made by actions up to
4157 * this point.
4158 *
4159 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4160 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4161 * for details.
4162 *
4163 * The output of the algorithm is 'link', an unsigned integer less than
4164 * or equal to 'max_link'.
4165 *
4166 * Some algorithms use 'arg' as an additional argument.
4167 *
4168 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4169 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4170 * action.
4171 *
4172 * The switch will reject actions that have an unknown 'fields', or an unknown
4173 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4174 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4175 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4176 */
4177struct nx_action_multipath {
4178 ovs_be16 type; /* OFPAT_VENDOR. */
4179 ovs_be16 len; /* Length is 32. */
4180 ovs_be32 vendor; /* NX_VENDOR_ID. */
4181 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4182
4183 /* What fields to hash and how. */
4184 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4185 ovs_be16 basis; /* Universal hash parameter. */
4186 ovs_be16 pad0;
4187
4188 /* Multipath link choice algorithm to apply to hash value. */
4189 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4190 ovs_be16 max_link; /* Number of output links, minus 1. */
4191 ovs_be32 arg; /* Algorithm-specific argument. */
4192 ovs_be16 pad1;
4193
4194 /* Where to store the result. */
4195 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4196 ovs_be32 dst; /* Destination. */
4197};
4198OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4199
4200static enum ofperr
4201decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4202 enum ofp_version ofp_version OVS_UNUSED,
4203 struct ofpbuf *out)
4204{
4205 uint32_t n_links = ntohs(nam->max_link) + 1;
4206 size_t min_n_bits = log_2_ceil(n_links);
4207 struct ofpact_multipath *mp;
4208
4209 mp = ofpact_put_MULTIPATH(out);
4210 mp->fields = ntohs(nam->fields);
4211 mp->basis = ntohs(nam->basis);
4212 mp->algorithm = ntohs(nam->algorithm);
4213 mp->max_link = ntohs(nam->max_link);
4214 mp->arg = ntohl(nam->arg);
4215 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4216 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4217 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4218
4219 if (!flow_hash_fields_valid(mp->fields)) {
4220 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4221 return OFPERR_OFPBAC_BAD_ARGUMENT;
4222 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4223 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4224 && mp->algorithm != NX_MP_ALG_HRW
4225 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4226 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4227 return OFPERR_OFPBAC_BAD_ARGUMENT;
4228 } else if (mp->dst.n_bits < min_n_bits) {
4229 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4230 "%"PRIu32" links", min_n_bits, n_links);
4231 return OFPERR_OFPBAC_BAD_ARGUMENT;
4232 }
4233
4234 return multipath_check(mp, NULL);
4235}
4236
4237static void
4238encode_MULTIPATH(const struct ofpact_multipath *mp,
4239 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4240{
4241 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4242
4243 nam->fields = htons(mp->fields);
4244 nam->basis = htons(mp->basis);
4245 nam->algorithm = htons(mp->algorithm);
4246 nam->max_link = htons(mp->max_link);
4247 nam->arg = htonl(mp->arg);
4248 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4249 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4250}
4251
4252static char * OVS_WARN_UNUSED_RESULT
4253parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4254 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4255{
4256 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4257}
4258
4259static void
4260format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4261{
4262 multipath_format(a, s);
4263}
4264\f
4265/* Action structure for NXAST_NOTE.
4266 *
4267 * This action has no effect. It is variable length. The switch does not
4268 * attempt to interpret the user-defined 'note' data in any way. A controller
4269 * can use this action to attach arbitrary metadata to a flow.
4270 *
4271 * This action might go away in the future.
4272 */
4273struct nx_action_note {
4274 ovs_be16 type; /* OFPAT_VENDOR. */
4275 ovs_be16 len; /* A multiple of 8, but at least 16. */
4276 ovs_be32 vendor; /* NX_VENDOR_ID. */
4277 ovs_be16 subtype; /* NXAST_NOTE. */
4278 uint8_t note[6]; /* Start of user-defined data. */
4279 /* Possibly followed by additional user-defined data. */
4280};
4281OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4282
4283static enum ofperr
4284decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4285 enum ofp_version ofp_version OVS_UNUSED,
4286 struct ofpbuf *out)
4287{
4288 struct ofpact_note *note;
4289 unsigned int length;
4290
4291 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4292 note = ofpact_put(out, OFPACT_NOTE,
4293 offsetof(struct ofpact_note, data) + length);
4294 note->length = length;
4295 memcpy(note->data, nan->note, length);
4296
4297 return 0;
4298}
4299
4300static void
4301encode_NOTE(const struct ofpact_note *note,
4302 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4303{
4304 size_t start_ofs = out->size;
4305 struct nx_action_note *nan;
4306 unsigned int remainder;
4307 unsigned int len;
4308
4309 put_NXAST_NOTE(out);
4310 out->size = out->size - sizeof nan->note;
4311
4312 ofpbuf_put(out, note->data, note->length);
4313
4314 len = out->size - start_ofs;
4315 remainder = len % OFP_ACTION_ALIGN;
4316 if (remainder) {
4317 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4318 }
4319 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4320 nan->len = htons(out->size - start_ofs);
4321}
4322
4323static char * OVS_WARN_UNUSED_RESULT
4324parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4325 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4326{
4327 struct ofpact_note *note;
4328
4329 note = ofpact_put_NOTE(ofpacts);
4330 while (*arg != '\0') {
4331 uint8_t byte;
4332 bool ok;
4333
4334 if (*arg == '.') {
4335 arg++;
4336 }
4337 if (*arg == '\0') {
4338 break;
4339 }
4340
4341 byte = hexits_value(arg, 2, &ok);
4342 if (!ok) {
4343 return xstrdup("bad hex digit in `note' argument");
4344 }
4345 ofpbuf_put(ofpacts, &byte, 1);
4346
4347 note = ofpacts->header;
4348 note->length++;
4349
4350 arg += 2;
4351 }
4352 ofpact_update_len(ofpacts, &note->ofpact);
4353 return NULL;
4354}
4355
4356static void
4357format_NOTE(const struct ofpact_note *a, struct ds *s)
4358{
4359 size_t i;
4360
4361 ds_put_cstr(s, "note:");
4362 for (i = 0; i < a->length; i++) {
4363 if (i) {
4364 ds_put_char(s, '.');
4365 }
4366 ds_put_format(s, "%02"PRIx8, a->data[i]);
4367 }
4368}
4369\f
4370/* Exit action. */
4371
4372static enum ofperr
4373decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4374{
4375 ofpact_put_EXIT(out);
4376 return 0;
4377}
4378
4379static void
4380encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4381 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4382{
4383 put_NXAST_EXIT(out);
4384}
4385
4386static char * OVS_WARN_UNUSED_RESULT
4387parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4388 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4389{
4390 ofpact_put_EXIT(ofpacts);
4391 return NULL;
4392}
4393
4394static void
4395format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4396{
4397 ds_put_cstr(s, "exit");
4398}
4399\f
4400/* Unroll xlate action. */
4401
4402static void
4403encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4404 enum ofp_version ofp_version OVS_UNUSED,
4405 struct ofpbuf *out OVS_UNUSED)
4406{
4407 OVS_NOT_REACHED();
4408}
4409
4410static char * OVS_WARN_UNUSED_RESULT
4411parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4412 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4413{
4414 OVS_NOT_REACHED();
4415 return NULL;
4416}
4417
4418static void
4419format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4420 struct ds *s)
4421{
4422 ds_put_cstr(s, "unroll_xlate");
4423}
4424\f
4425/* Action structure for NXAST_SAMPLE.
4426 *
4427 * Samples matching packets with the given probability and sends them
4428 * each to the set of collectors identified with the given ID. The
4429 * probability is expressed as a number of packets to be sampled out
4430 * of USHRT_MAX packets, and must be >0.
4431 *
4432 * When sending packet samples to IPFIX collectors, the IPFIX flow
4433 * record sent for each sampled packet is associated with the given
4434 * observation domain ID and observation point ID. Each IPFIX flow
4435 * record contain the sampled packet's headers when executing this
4436 * rule. If a sampled packet's headers are modified by previous
4437 * actions in the flow, those modified headers are sent. */
4438struct nx_action_sample {
4439 ovs_be16 type; /* OFPAT_VENDOR. */
4440 ovs_be16 len; /* Length is 24. */
4441 ovs_be32 vendor; /* NX_VENDOR_ID. */
4442 ovs_be16 subtype; /* NXAST_SAMPLE. */
4443 ovs_be16 probability; /* Fraction of packets to sample. */
4444 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4445 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4446 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4447};
4448OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4449
4450static enum ofperr
4451decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4452 enum ofp_version ofp_version OVS_UNUSED,
4453 struct ofpbuf *out)
4454{
4455 struct ofpact_sample *sample;
4456
4457 sample = ofpact_put_SAMPLE(out);
4458 sample->probability = ntohs(nas->probability);
4459 sample->collector_set_id = ntohl(nas->collector_set_id);
4460 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4461 sample->obs_point_id = ntohl(nas->obs_point_id);
4462
4463 if (sample->probability == 0) {
4464 return OFPERR_OFPBAC_BAD_ARGUMENT;
4465 }
4466
4467 return 0;
4468}
4469
4470static void
4471encode_SAMPLE(const struct ofpact_sample *sample,
4472 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4473{
4474 struct nx_action_sample *nas;
4475
4476 nas = put_NXAST_SAMPLE(out);
4477 nas->probability = htons(sample->probability);
4478 nas->collector_set_id = htonl(sample->collector_set_id);
4479 nas->obs_domain_id = htonl(sample->obs_domain_id);
4480 nas->obs_point_id = htonl(sample->obs_point_id);
4481}
4482
4483/* Parses 'arg' as the argument to a "sample" action, and appends such an
4484 * action to 'ofpacts'.
4485 *
4486 * Returns NULL if successful, otherwise a malloc()'d string describing the
4487 * error. The caller is responsible for freeing the returned string. */
4488static char * OVS_WARN_UNUSED_RESULT
4489parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4490 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4491{
4492 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4493 char *key, *value;
4494
4495 while (ofputil_parse_key_value(&arg, &key, &value)) {
4496 char *error = NULL;
4497
4498 if (!strcmp(key, "probability")) {
4499 error = str_to_u16(value, "probability", &os->probability);
4500 if (!error && os->probability == 0) {
4501 error = xasprintf("invalid probability value \"%s\"", value);
4502 }
4503 } else if (!strcmp(key, "collector_set_id")) {
4504 error = str_to_u32(value, &os->collector_set_id);
4505 } else if (!strcmp(key, "obs_domain_id")) {
4506 error = str_to_u32(value, &os->obs_domain_id);
4507 } else if (!strcmp(key, "obs_point_id")) {
4508 error = str_to_u32(value, &os->obs_point_id);
4509 } else {
4510 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4511 key);
4512 }
4513 if (error) {
4514 return error;
4515 }
4516 }
4517 if (os->probability == 0) {
4518 return xstrdup("non-zero \"probability\" must be specified on sample");
4519 }
4520 return NULL;
4521}
4522
4523static void
4524format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4525{
4526 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4527 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4528 a->probability, a->collector_set_id,
4529 a->obs_domain_id, a->obs_point_id);
4530}
4531\f
4532/* debug_recirc instruction. */
4533
4534static bool enable_debug;
4535
4536void
4537ofpact_dummy_enable(void)
4538{
4539 enable_debug = true;
4540}
4541
4542static enum ofperr
4543decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4544{
4545 if (!enable_debug) {
4546 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4547 }
4548
4549 ofpact_put_DEBUG_RECIRC(out);
4550 return 0;
4551}
4552
4553static void
4554encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4555 enum ofp_version ofp_version OVS_UNUSED,
4556 struct ofpbuf *out)
4557{
4558 put_NXAST_DEBUG_RECIRC(out);
4559}
4560
4561static char * OVS_WARN_UNUSED_RESULT
4562parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4563 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4564{
4565 ofpact_put_DEBUG_RECIRC(ofpacts);
4566 return NULL;
4567}
4568
4569static void
4570format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4571{
4572 ds_put_cstr(s, "debug_recirc");
4573}
4574\f
4575/* Meter instruction. */
4576
4577static void
4578encode_METER(const struct ofpact_meter *meter,
4579 enum ofp_version ofp_version, struct ofpbuf *out)
4580{
4581 if (ofp_version >= OFP13_VERSION) {
4582 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4583 }
4584}
4585
4586static char * OVS_WARN_UNUSED_RESULT
4587parse_METER(char *arg, struct ofpbuf *ofpacts,
4588 enum ofputil_protocol *usable_protocols)
4589{
4590 *usable_protocols &= OFPUTIL_P_OF13_UP;
4591 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4592}
4593
4594static void
4595format_METER(const struct ofpact_meter *a, struct ds *s)
4596{
4597 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4598}
4599\f
4600/* Clear-Actions instruction. */
4601
4602static void
4603encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4604 enum ofp_version ofp_version OVS_UNUSED,
4605 struct ofpbuf *out OVS_UNUSED)
4606{
4607 if (ofp_version > OFP10_VERSION) {
4608 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4609 }
4610}
4611
4612static char * OVS_WARN_UNUSED_RESULT
4613parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4614 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4615{
4616 ofpact_put_CLEAR_ACTIONS(ofpacts);
4617 return NULL;
4618}
4619
4620static void
4621format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4622{
4623 ds_put_cstr(s, "clear_actions");
4624}
4625\f
4626/* Write-Actions instruction. */
4627
4628static void
4629encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4630 enum ofp_version ofp_version, struct ofpbuf *out)
4631{
4632 if (ofp_version > OFP10_VERSION) {
4633 const size_t ofs = out->size;
4634
4635 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4636 ofpacts_put_openflow_actions(actions->actions,
4637 ofpact_nest_get_action_len(actions),
4638 out, ofp_version);
4639 ofpacts_update_instruction_actions(out, ofs);
4640 }
4641}
4642
4643static char * OVS_WARN_UNUSED_RESULT
4644parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4645 enum ofputil_protocol *usable_protocols)
4646{
4647 size_t ofs = ofpacts_pull(ofpacts);
4648 struct ofpact_nest *on;
4649 char *error;
4650
4651 /* Add a Write-Actions instruction and then pull it off. */
4652 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4653 ofpbuf_pull(ofpacts, sizeof *on);
4654
4655 /* Parse nested actions.
4656 *
4657 * We pulled off "write-actions" and the previous actions because the
4658 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4659 * that it doesn't actually include the nested actions. That means that
4660 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4661 * follows a Write-Actions, which is an invalid order. */
4662 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4663 OFPACT_WRITE_ACTIONS);
4664
4665 /* Put the Write-Actions back on and update its length. */
4666 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4667 on->ofpact.len = ofpacts->size;
4668
4669 /* Put any previous actions or instructions back on. */
4670 ofpbuf_push_uninit(ofpacts, ofs);
4671
4672 return error;
4673}
4674
4675static void
4676format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4677{
4678 ds_put_cstr(s, "write_actions(");
4679 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4680 ds_put_char(s, ')');
4681}
4682\f
4683/* Action structure for NXAST_WRITE_METADATA.
4684 *
4685 * Modifies the 'mask' bits of the metadata value. */
4686struct nx_action_write_metadata {
4687 ovs_be16 type; /* OFPAT_VENDOR. */
4688 ovs_be16 len; /* Length is 32. */
4689 ovs_be32 vendor; /* NX_VENDOR_ID. */
4690 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4691 uint8_t zeros[6]; /* Must be zero. */
4692 ovs_be64 metadata; /* Metadata register. */
4693 ovs_be64 mask; /* Metadata mask. */
4694};
4695OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4696
4697static enum ofperr
4698decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4699 enum ofp_version ofp_version OVS_UNUSED,
4700 struct ofpbuf *out)
4701{
4702 struct ofpact_metadata *om;
4703
4704 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4705 return OFPERR_NXBRC_MUST_BE_ZERO;
4706 }
4707
4708 om = ofpact_put_WRITE_METADATA(out);
4709 om->metadata = nawm->metadata;
4710 om->mask = nawm->mask;
4711
4712 return 0;
4713}
4714
4715static void
4716encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4717 enum ofp_version ofp_version, struct ofpbuf *out)
4718{
4719 if (ofp_version == OFP10_VERSION) {
4720 struct nx_action_write_metadata *nawm;
4721
4722 nawm = put_NXAST_WRITE_METADATA(out);
4723 nawm->metadata = metadata->metadata;
4724 nawm->mask = metadata->mask;
4725 } else {
4726 struct ofp11_instruction_write_metadata *oiwm;
4727
4728 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4729 oiwm->metadata = metadata->metadata;
4730 oiwm->metadata_mask = metadata->mask;
4731 }
4732}
4733
4734static char * OVS_WARN_UNUSED_RESULT
4735parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4736 enum ofputil_protocol *usable_protocols)
4737{
4738 struct ofpact_metadata *om;
4739 char *mask = strchr(arg, '/');
4740
4741 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4742
4743 om = ofpact_put_WRITE_METADATA(ofpacts);
4744 if (mask) {
4745 char *error;
4746
4747 *mask = '\0';
4748 error = str_to_be64(mask + 1, &om->mask);
4749 if (error) {
4750 return error;
4751 }
4752 } else {
4753 om->mask = OVS_BE64_MAX;
4754 }
4755
4756 return str_to_be64(arg, &om->metadata);
4757}
4758
4759static void
4760format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4761{
4762 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4763 if (a->mask != OVS_BE64_MAX) {
4764 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4765 }
4766}
4767\f
4768/* Goto-Table instruction. */
4769
4770static void
4771encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4772 enum ofp_version ofp_version, struct ofpbuf *out)
4773{
4774 if (ofp_version == OFP10_VERSION) {
4775 struct nx_action_resubmit *nar;
4776
4777 nar = put_NXAST_RESUBMIT_TABLE(out);
4778 nar->table = goto_table->table_id;
4779 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4780 } else {
4781 struct ofp11_instruction_goto_table *oigt;
4782
4783 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4784 oigt->table_id = goto_table->table_id;
4785 memset(oigt->pad, 0, sizeof oigt->pad);
4786 }
4787}
4788
4789static char * OVS_WARN_UNUSED_RESULT
4790parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4791 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4792{
4793 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4794 char *table_s = strsep(&arg, ",");
4795 if (!table_s || !table_s[0]) {
4796 return xstrdup("instruction goto-table needs table id");
4797 }
4798 return str_to_u8(table_s, "table", &ogt->table_id);
4799}
4800
4801static void
4802format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4803{
4804 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4805}
4806\f
4807static void
4808log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4809 const struct ofp_action_header *bad_action, enum ofperr error)
4810{
4811 if (!VLOG_DROP_WARN(&rl)) {
4812 struct ds s;
4813
4814 ds_init(&s);
4815 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4816 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4817 (char *)bad_action - (char *)actions,
4818 ofperr_get_name(error), ds_cstr(&s));
4819 ds_destroy(&s);
4820 }
4821}
4822
4823static enum ofperr
4824ofpacts_decode(const void *actions, size_t actions_len,
4825 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4826{
4827 struct ofpbuf openflow;
4828
4829 ofpbuf_use_const(&openflow, actions, actions_len);
4830 while (openflow.size) {
4831 const struct ofp_action_header *action = openflow.data;
4832 enum ofp_raw_action_type raw;
4833 enum ofperr error;
4834 uint64_t arg;
4835
4836 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4837 if (!error) {
4838 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
4839 }
4840
4841 if (error) {
4842 log_bad_action(actions, actions_len, action, error);
4843 return error;
4844 }
4845 }
4846
4847 ofpact_pad(ofpacts);
4848 return 0;
4849}
4850
4851static enum ofperr
4852ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4853 unsigned int actions_len,
4854 enum ofp_version version,
4855 uint32_t allowed_ovsinsts,
4856 struct ofpbuf *ofpacts,
4857 enum ofpact_type outer_action)
4858{
4859 const struct ofp_action_header *actions;
4860 enum ofperr error;
4861
4862 if (!outer_action) {
4863 ofpbuf_clear(ofpacts);
4864 }
4865
4866 if (actions_len % OFP_ACTION_ALIGN != 0) {
4867 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4868 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4869 return OFPERR_OFPBRC_BAD_LEN;
4870 }
4871
4872 actions = ofpbuf_try_pull(openflow, actions_len);
4873 if (actions == NULL) {
4874 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4875 "remaining message length (%"PRIu32")",
4876 actions_len, openflow->size);
4877 return OFPERR_OFPBRC_BAD_LEN;
4878 }
4879
4880 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4881 if (error) {
4882 ofpbuf_clear(ofpacts);
4883 return error;
4884 }
4885
4886 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
4887 outer_action);
4888 if (error) {
4889 ofpbuf_clear(ofpacts);
4890 }
4891 return error;
4892}
4893
4894/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4895 * front of 'openflow' into ofpacts. On success, replaces any existing content
4896 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4897 * Returns 0 if successful, otherwise an OpenFlow error.
4898 *
4899 * Actions are processed according to their OpenFlow version which
4900 * is provided in the 'version' parameter.
4901 *
4902 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4903 * you should call ofpacts_pull_openflow_instructions() instead of this
4904 * function.
4905 *
4906 * The parsed actions are valid generically, but they may not be valid in a
4907 * specific context. For example, port numbers up to OFPP_MAX are valid
4908 * generically, but specific datapaths may only support port numbers in a
4909 * smaller range. Use ofpacts_check() to additional check whether actions are
4910 * valid in a specific context. */
4911enum ofperr
4912ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4913 unsigned int actions_len,
4914 enum ofp_version version,
4915 struct ofpbuf *ofpacts)
4916{
4917 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4918 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4919 ofpacts, 0);
4920}
4921\f
4922/* OpenFlow 1.1 actions. */
4923
4924
4925/* True if an action sets the value of a field
4926 * in a way that is compatibile with the action set.
4927 * The field can be set via either a set or a move action.
4928 * False otherwise. */
4929static bool
4930ofpact_is_set_or_move_action(const struct ofpact *a)
4931{
4932 switch (a->type) {
4933 case OFPACT_SET_FIELD:
4934 case OFPACT_REG_MOVE:
4935 case OFPACT_SET_ETH_DST:
4936 case OFPACT_SET_ETH_SRC:
4937 case OFPACT_SET_IP_DSCP:
4938 case OFPACT_SET_IP_ECN:
4939 case OFPACT_SET_IP_TTL:
4940 case OFPACT_SET_IPV4_DST:
4941 case OFPACT_SET_IPV4_SRC:
4942 case OFPACT_SET_L4_DST_PORT:
4943 case OFPACT_SET_L4_SRC_PORT:
4944 case OFPACT_SET_MPLS_LABEL:
4945 case OFPACT_SET_MPLS_TC:
4946 case OFPACT_SET_MPLS_TTL:
4947 case OFPACT_SET_QUEUE:
4948 case OFPACT_SET_TUNNEL:
4949 case OFPACT_SET_VLAN_PCP:
4950 case OFPACT_SET_VLAN_VID:
4951 return true;
4952 case OFPACT_BUNDLE:
4953 case OFPACT_CLEAR_ACTIONS:
4954 case OFPACT_CONTROLLER:
4955 case OFPACT_DEC_MPLS_TTL:
4956 case OFPACT_DEC_TTL:
4957 case OFPACT_ENQUEUE:
4958 case OFPACT_EXIT:
4959 case OFPACT_UNROLL_XLATE:
4960 case OFPACT_FIN_TIMEOUT:
4961 case OFPACT_GOTO_TABLE:
4962 case OFPACT_GROUP:
4963 case OFPACT_LEARN:
4964 case OFPACT_CONJUNCTION:
4965 case OFPACT_METER:
4966 case OFPACT_MULTIPATH:
4967 case OFPACT_NOTE:
4968 case OFPACT_OUTPUT:
4969 case OFPACT_OUTPUT_REG:
4970 case OFPACT_POP_MPLS:
4971 case OFPACT_POP_QUEUE:
4972 case OFPACT_PUSH_MPLS:
4973 case OFPACT_PUSH_VLAN:
4974 case OFPACT_RESUBMIT:
4975 case OFPACT_SAMPLE:
4976 case OFPACT_STACK_POP:
4977 case OFPACT_STACK_PUSH:
4978 case OFPACT_STRIP_VLAN:
4979 case OFPACT_WRITE_ACTIONS:
4980 case OFPACT_WRITE_METADATA:
4981 case OFPACT_DEBUG_RECIRC:
4982 return false;
4983 default:
4984 OVS_NOT_REACHED();
4985 }
4986}
4987
4988/* True if an action is allowed in the action set.
4989 * False otherwise. */
4990static bool
4991ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4992{
4993 switch (a->type) {
4994 case OFPACT_DEC_MPLS_TTL:
4995 case OFPACT_DEC_TTL:
4996 case OFPACT_GROUP:
4997 case OFPACT_OUTPUT:
4998 case OFPACT_POP_MPLS:
4999 case OFPACT_PUSH_MPLS:
5000 case OFPACT_PUSH_VLAN:
5001 case OFPACT_REG_MOVE:
5002 case OFPACT_SET_FIELD:
5003 case OFPACT_SET_ETH_DST:
5004 case OFPACT_SET_ETH_SRC:
5005 case OFPACT_SET_IP_DSCP:
5006 case OFPACT_SET_IP_ECN:
5007 case OFPACT_SET_IP_TTL:
5008 case OFPACT_SET_IPV4_DST:
5009 case OFPACT_SET_IPV4_SRC:
5010 case OFPACT_SET_L4_DST_PORT:
5011 case OFPACT_SET_L4_SRC_PORT:
5012 case OFPACT_SET_MPLS_LABEL:
5013 case OFPACT_SET_MPLS_TC:
5014 case OFPACT_SET_MPLS_TTL:
5015 case OFPACT_SET_QUEUE:
5016 case OFPACT_SET_TUNNEL:
5017 case OFPACT_SET_VLAN_PCP:
5018 case OFPACT_SET_VLAN_VID:
5019 case OFPACT_STRIP_VLAN:
5020 return true;
5021
5022 /* In general these actions are excluded because they are not part of
5023 * the OpenFlow specification nor map to actions that are defined in
5024 * the specification. Thus the order in which they should be applied
5025 * in the action set is undefined. */
5026 case OFPACT_BUNDLE:
5027 case OFPACT_CONTROLLER:
5028 case OFPACT_ENQUEUE:
5029 case OFPACT_EXIT:
5030 case OFPACT_UNROLL_XLATE:
5031 case OFPACT_FIN_TIMEOUT:
5032 case OFPACT_LEARN:
5033 case OFPACT_CONJUNCTION:
5034 case OFPACT_MULTIPATH:
5035 case OFPACT_NOTE:
5036 case OFPACT_OUTPUT_REG:
5037 case OFPACT_POP_QUEUE:
5038 case OFPACT_RESUBMIT:
5039 case OFPACT_SAMPLE:
5040 case OFPACT_STACK_POP:
5041 case OFPACT_STACK_PUSH:
5042 case OFPACT_DEBUG_RECIRC:
5043
5044 /* The action set may only include actions and thus
5045 * may not include any instructions */
5046 case OFPACT_CLEAR_ACTIONS:
5047 case OFPACT_GOTO_TABLE:
5048 case OFPACT_METER:
5049 case OFPACT_WRITE_ACTIONS:
5050 case OFPACT_WRITE_METADATA:
5051 return false;
5052 default:
5053 OVS_NOT_REACHED();
5054 }
5055}
5056
5057/* Append ofpact 'a' onto the tail of 'out' */
5058static void
5059ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5060{
5061 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5062}
5063
5064/* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5065static bool
5066ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5067 enum ofpact_type filter)
5068{
5069 const struct ofpact *target;
5070 const struct ofpact *a;
5071
5072 target = NULL;
5073 OFPACT_FOR_EACH (a, in->data, in->size) {
5074 if (a->type == filter) {
5075 target = a;
5076 }
5077 }
5078 if (target) {
5079 ofpact_copy(out, target);
5080 }
5081 return target != NULL;
5082}
5083
5084/* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5085 * The order of appended ofpacts is preserved between 'in' and 'out' */
5086static void
5087ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5088 bool (*filter)(const struct ofpact *))
5089{
5090 const struct ofpact *a;
5091
5092 OFPACT_FOR_EACH (a, in->data, in->size) {
5093 if (filter(a)) {
5094 ofpact_copy(out, a);
5095 }
5096 }
5097}
5098
5099/* Reads 'action_set', which contains ofpacts accumulated by
5100 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5101 * executed directly into 'action_list'. (These names correspond to the
5102 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5103 *
5104 * In general this involves appending the last instance of each action that is
5105 * admissible in the action set in the order described in the OpenFlow
5106 * specification.
5107 *
5108 * Exceptions:
5109 * + output action is only appended if no group action was present in 'in'.
5110 * + As a simplification all set actions are copied in the order the are
5111 * provided in 'in' as many set actions applied to a field has the same
5112 * affect as only applying the last action that sets a field and
5113 * duplicates are removed by do_xlate_actions().
5114 * This has an unwanted side-effect of compsoting multiple
5115 * LOAD_REG actions that touch different regions of the same field. */
5116void
5117ofpacts_execute_action_set(struct ofpbuf *action_list,
5118 const struct ofpbuf *action_set)
5119{
5120 /* The OpenFlow spec "Action Set" section specifies this order. */
5121 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5122 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5123 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5124 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5125 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5126 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5127 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5128 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5129
5130 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5131 * we should execute only OFPACT_GROUP.
5132 *
5133 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5134 * all the actions because there's no point in modifying a packet that will
5135 * not be sent anywhere. */
5136 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5137 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5138 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5139 ofpbuf_clear(action_list);
5140 }
5141}
5142
5143
5144static enum ofperr
5145ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5146 size_t n_in, enum ofp_version version,
5147 struct ofpbuf *out)
5148{
5149 enum ofperr error;
5150 struct ofpact *a;
5151 size_t start = out->size;
5152
5153 error = ofpacts_decode(in, n_in, version, out);
5154
5155 if (error) {
5156 return error;
5157 }
5158
5159 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5160 if (!ofpact_is_allowed_in_actions_set(a)) {
5161 VLOG_WARN_RL(&rl, "disallowed action in action set");
5162 return OFPERR_OFPBAC_BAD_TYPE;
5163 }
5164 }
5165
5166 return 0;
5167}
5168\f
5169/* OpenFlow 1.1 instructions. */
5170
5171struct instruction_type_info {
5172 enum ovs_instruction_type type;
5173 const char *name;
5174};
5175
5176static const struct instruction_type_info inst_info[] = {
5177#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5178OVS_INSTRUCTIONS
5179#undef DEFINE_INST
5180};
5181
5182const char *
5183ovs_instruction_name_from_type(enum ovs_instruction_type type)
5184{
5185 return inst_info[type].name;
5186}
5187
5188int
5189ovs_instruction_type_from_name(const char *name)
5190{
5191 const struct instruction_type_info *p;
5192 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5193 if (!strcasecmp(name, p->name)) {
5194 return p->type;
5195 }
5196 }
5197 return -1;
5198}
5199
5200enum ovs_instruction_type
5201ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5202{
5203 switch (type) {
5204 case OFPACT_METER:
5205 return OVSINST_OFPIT13_METER;
5206 case OFPACT_CLEAR_ACTIONS:
5207 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5208 case OFPACT_WRITE_ACTIONS:
5209 return OVSINST_OFPIT11_WRITE_ACTIONS;
5210 case OFPACT_WRITE_METADATA:
5211 return OVSINST_OFPIT11_WRITE_METADATA;
5212 case OFPACT_GOTO_TABLE:
5213 return OVSINST_OFPIT11_GOTO_TABLE;
5214 case OFPACT_OUTPUT:
5215 case OFPACT_GROUP:
5216 case OFPACT_CONTROLLER:
5217 case OFPACT_ENQUEUE:
5218 case OFPACT_OUTPUT_REG:
5219 case OFPACT_BUNDLE:
5220 case OFPACT_SET_VLAN_VID:
5221 case OFPACT_SET_VLAN_PCP:
5222 case OFPACT_STRIP_VLAN:
5223 case OFPACT_PUSH_VLAN:
5224 case OFPACT_SET_ETH_SRC:
5225 case OFPACT_SET_ETH_DST:
5226 case OFPACT_SET_IPV4_SRC:
5227 case OFPACT_SET_IPV4_DST:
5228 case OFPACT_SET_IP_DSCP:
5229 case OFPACT_SET_IP_ECN:
5230 case OFPACT_SET_IP_TTL:
5231 case OFPACT_SET_L4_SRC_PORT:
5232 case OFPACT_SET_L4_DST_PORT:
5233 case OFPACT_REG_MOVE:
5234 case OFPACT_SET_FIELD:
5235 case OFPACT_STACK_PUSH:
5236 case OFPACT_STACK_POP:
5237 case OFPACT_DEC_TTL:
5238 case OFPACT_SET_MPLS_LABEL:
5239 case OFPACT_SET_MPLS_TC:
5240 case OFPACT_SET_MPLS_TTL:
5241 case OFPACT_DEC_MPLS_TTL:
5242 case OFPACT_PUSH_MPLS:
5243 case OFPACT_POP_MPLS:
5244 case OFPACT_SET_TUNNEL:
5245 case OFPACT_SET_QUEUE:
5246 case OFPACT_POP_QUEUE:
5247 case OFPACT_FIN_TIMEOUT:
5248 case OFPACT_RESUBMIT:
5249 case OFPACT_LEARN:
5250 case OFPACT_CONJUNCTION:
5251 case OFPACT_MULTIPATH:
5252 case OFPACT_NOTE:
5253 case OFPACT_EXIT:
5254 case OFPACT_UNROLL_XLATE:
5255 case OFPACT_SAMPLE:
5256 case OFPACT_DEBUG_RECIRC:
5257 default:
5258 return OVSINST_OFPIT11_APPLY_ACTIONS;
5259 }
5260}
5261
5262enum ofperr
5263ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5264 const uint16_t inst_type)
5265{
5266 switch (inst_type) {
5267
5268#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5269 case ENUM: \
5270 *instruction_type = OVSINST_##ENUM; \
5271 return 0;
5272OVS_INSTRUCTIONS
5273#undef DEFINE_INST
5274
5275 default:
5276 return OFPERR_OFPBIC_UNKNOWN_INST;
5277 }
5278}
5279
5280/* Two-way translation between OVS's internal "OVSINST_*" representation of
5281 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5282struct ovsinst_map {
5283 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5284 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5285};
5286
5287static const struct ovsinst_map *
5288get_ovsinst_map(enum ofp_version version)
5289{
5290 /* OpenFlow 1.1 and 1.2 instructions. */
5291 static const struct ovsinst_map of11[] = {
5292 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5293 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5294 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5295 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5296 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5297 { 0, -1 },
5298 };
5299
5300 /* OpenFlow 1.3+ instructions. */
5301 static const struct ovsinst_map of13[] = {
5302 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5303 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5304 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5305 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5306 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5307 { OVSINST_OFPIT13_METER, 6 },
5308 { 0, -1 },
5309 };
5310
5311 return version < OFP13_VERSION ? of11 : of13;
5312}
5313
5314/* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5315 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5316 * (OFP11_VERSION or later), and returns the result. */
5317ovs_be32
5318ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5319{
5320 uint32_t ofpit_bitmap = 0;
5321 const struct ovsinst_map *x;
5322
5323 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5324 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5325 ofpit_bitmap |= 1u << x->ofpit;
5326 }
5327 }
5328 return htonl(ofpit_bitmap);
5329}
5330
5331/* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5332 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5333 * correspond to OVSINST_* values, and returns the result. */
5334uint32_t
5335ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5336{
5337 uint32_t ovsinst_bitmap = 0;
5338 const struct ovsinst_map *x;
5339
5340 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5341 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5342 ovsinst_bitmap |= 1u << x->ovsinst;
5343 }
5344 }
5345 return ovsinst_bitmap;
5346}
5347
5348static inline struct ofp11_instruction *
5349instruction_next(const struct ofp11_instruction *inst)
5350{
5351 return ((struct ofp11_instruction *) (void *)
5352 ((uint8_t *) inst + ntohs(inst->len)));
5353}
5354
5355static inline bool
5356instruction_is_valid(const struct ofp11_instruction *inst,
5357 size_t n_instructions)
5358{
5359 uint16_t len = ntohs(inst->len);
5360 return (!(len % OFP11_INSTRUCTION_ALIGN)
5361 && len >= sizeof *inst
5362 && len / sizeof *inst <= n_instructions);
5363}
5364
5365/* This macro is careful to check for instructions with bad lengths. */
5366#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5367 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5368 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5369 ((LEFT) -= (ntohs((ITER)->len) \
5370 / sizeof(struct ofp11_instruction)), \
5371 (ITER) = instruction_next(ITER)))
5372
5373static enum ofperr
5374decode_openflow11_instruction(const struct ofp11_instruction *inst,
5375 enum ovs_instruction_type *type)
5376{
5377 uint16_t len = ntohs(inst->len);
5378
5379 switch (inst->type) {
5380 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5381 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5382
5383#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5384 case CONSTANT_HTONS(ENUM): \
5385 if (EXTENSIBLE \
5386 ? len >= sizeof(struct STRUCT) \
5387 : len == sizeof(struct STRUCT)) { \
5388 *type = OVSINST_##ENUM; \
5389 return 0; \
5390 } else { \
5391 return OFPERR_OFPBIC_BAD_LEN; \
5392 }
5393OVS_INSTRUCTIONS
5394#undef DEFINE_INST
5395
5396 default:
5397 return OFPERR_OFPBIC_UNKNOWN_INST;
5398 }
5399}
5400
5401static enum ofperr
5402decode_openflow11_instructions(const struct ofp11_instruction insts[],
5403 size_t n_insts,
5404 const struct ofp11_instruction *out[])
5405{
5406 const struct ofp11_instruction *inst;
5407 size_t left;
5408
5409 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5410 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5411 enum ovs_instruction_type type;
5412 enum ofperr error;
5413
5414 error = decode_openflow11_instruction(inst, &type);
5415 if (error) {
5416 return error;
5417 }
5418
5419 if (out[type]) {
5420 return OFPERR_OFPBIC_DUP_INST;
5421 }
5422 out[type] = inst;
5423 }
5424
5425 if (left) {
5426 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5427 (n_insts - left) * sizeof *inst);
5428 return OFPERR_OFPBIC_BAD_LEN;
5429 }
5430 return 0;
5431}
5432
5433static void
5434get_actions_from_instruction(const struct ofp11_instruction *inst,
5435 const struct ofp_action_header **actions,
5436 size_t *actions_len)
5437{
5438 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5439 *actions_len = ntohs(inst->len) - sizeof *inst;
5440}
5441
5442enum ofperr
5443ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5444 unsigned int instructions_len,
5445 enum ofp_version version,
5446 struct ofpbuf *ofpacts)
5447{
5448 const struct ofp11_instruction *instructions;
5449 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5450 enum ofperr error;
5451
5452 if (version == OFP10_VERSION) {
5453 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5454 version,
5455 (1u << N_OVS_INSTRUCTIONS) - 1,
5456 ofpacts, 0);
5457 }
5458
5459 ofpbuf_clear(ofpacts);
5460
5461 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5462 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5463 "multiple of %d",
5464 instructions_len, OFP11_INSTRUCTION_ALIGN);
5465 error = OFPERR_OFPBIC_BAD_LEN;
5466 goto exit;
5467 }
5468
5469 instructions = ofpbuf_try_pull(openflow, instructions_len);
5470 if (instructions == NULL) {
5471 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5472 "remaining message length (%"PRIu32")",
5473 instructions_len, openflow->size);
5474 error = OFPERR_OFPBIC_BAD_LEN;
5475 goto exit;
5476 }
5477
5478 error = decode_openflow11_instructions(
5479 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5480 insts);
5481 if (error) {
5482 goto exit;
5483 }
5484
5485 if (insts[OVSINST_OFPIT13_METER]) {
5486 const struct ofp13_instruction_meter *oim;
5487 struct ofpact_meter *om;
5488
5489 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5490 insts[OVSINST_OFPIT13_METER]);
5491
5492 om = ofpact_put_METER(ofpacts);
5493 om->meter_id = ntohl(oim->meter_id);
5494 }
5495 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5496 const struct ofp_action_header *actions;
5497 size_t actions_len;
5498
5499 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5500 &actions, &actions_len);
5501 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5502 if (error) {
5503 goto exit;
5504 }
5505 }
5506 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5507 instruction_get_OFPIT11_CLEAR_ACTIONS(
5508 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5509 ofpact_put_CLEAR_ACTIONS(ofpacts);
5510 }
5511 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5512 struct ofpact_nest *on;
5513 const struct ofp_action_header *actions;
5514 size_t actions_len;
5515 size_t start;
5516
5517 ofpact_pad(ofpacts);
5518 start = ofpacts->size;
5519 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5520 offsetof(struct ofpact_nest, actions));
5521 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5522 &actions, &actions_len);
5523 error = ofpacts_decode_for_action_set(actions, actions_len,
5524 version, ofpacts);
5525 if (error) {
5526 goto exit;
5527 }
5528 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5529 on->ofpact.len = ofpacts->size - start;
5530 }
5531 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5532 const struct ofp11_instruction_write_metadata *oiwm;
5533 struct ofpact_metadata *om;
5534
5535 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5536 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5537
5538 om = ofpact_put_WRITE_METADATA(ofpacts);
5539 om->metadata = oiwm->metadata;
5540 om->mask = oiwm->metadata_mask;
5541 }
5542 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5543 const struct ofp11_instruction_goto_table *oigt;
5544 struct ofpact_goto_table *ogt;
5545
5546 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5547 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5548 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5549 ogt->table_id = oigt->table_id;
5550 }
5551
5552 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5553 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5554exit:
5555 if (error) {
5556 ofpbuf_clear(ofpacts);
5557 }
5558 return error;
5559}
5560
5561/* Update the length of the instruction that begins at offset 'ofs' within
5562 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5563 * If the instruction contains no nested actions, deletes it entirely. */
5564static void
5565ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5566{
5567 struct ofp11_instruction_actions *oia;
5568
5569 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5570 if (openflow->size > ofs + sizeof *oia) {
5571 oia->len = htons(openflow->size - ofs);
5572 } else {
5573 openflow->size = ofs;
5574 }
5575}
5576\f
5577/* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5578 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5579 * valid, otherwise an OpenFlow error code. */
5580enum ofperr
5581ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5582{
5583 switch (port) {
5584 case OFPP_IN_PORT:
5585 case OFPP_TABLE:
5586 case OFPP_NORMAL:
5587 case OFPP_FLOOD:
5588 case OFPP_ALL:
5589 case OFPP_CONTROLLER:
5590 case OFPP_LOCAL:
5591 return 0;
5592
5593 case OFPP_NONE:
5594 return OFPERR_OFPBAC_BAD_OUT_PORT;
5595
5596 default:
5597 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5598 return 0;
5599 }
5600 return OFPERR_OFPBAC_BAD_OUT_PORT;
5601 }
5602}
5603
5604/* Removes the protocols that require consistency between match and actions
5605 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5606 *
5607 * (An example of an inconsistency between match and actions is a flow that
5608 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5609 * label.) */
5610static void
5611inconsistent_match(enum ofputil_protocol *usable_protocols)
5612{
5613 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5614}
5615
5616/* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5617 * caller must restore them.
5618 *
5619 * Modifies some actions, filling in fields that could not be properly set
5620 * without context. */
5621static enum ofperr
5622ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5623 struct flow *flow, ofp_port_t max_ports,
5624 uint8_t table_id, uint8_t n_tables)
5625{
5626 const struct ofpact_enqueue *enqueue;
5627 const struct mf_field *mf;
5628
5629 switch (a->type) {
5630 case OFPACT_OUTPUT:
5631 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5632 max_ports);
5633
5634 case OFPACT_CONTROLLER:
5635 return 0;
5636
5637 case OFPACT_ENQUEUE:
5638 enqueue = ofpact_get_ENQUEUE(a);
5639 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5640 && enqueue->port != OFPP_IN_PORT
5641 && enqueue->port != OFPP_LOCAL) {
5642 return OFPERR_OFPBAC_BAD_OUT_PORT;
5643 }
5644 return 0;
5645
5646 case OFPACT_OUTPUT_REG:
5647 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5648
5649 case OFPACT_BUNDLE:
5650 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5651
5652 case OFPACT_SET_VLAN_VID:
5653 /* Remember if we saw a vlan tag in the flow to aid translating to
5654 * OpenFlow 1.1+ if need be. */
5655 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5656 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5657 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5658 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5659 inconsistent_match(usable_protocols);
5660 }
5661 /* Temporary mark that we have a vlan tag. */
5662 flow->vlan_tci |= htons(VLAN_CFI);
5663 return 0;
5664
5665 case OFPACT_SET_VLAN_PCP:
5666 /* Remember if we saw a vlan tag in the flow to aid translating to
5667 * OpenFlow 1.1+ if need be. */
5668 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5669 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5670 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5671 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5672 inconsistent_match(usable_protocols);
5673 }
5674 /* Temporary mark that we have a vlan tag. */
5675 flow->vlan_tci |= htons(VLAN_CFI);
5676 return 0;
5677
5678 case OFPACT_STRIP_VLAN:
5679 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5680 inconsistent_match(usable_protocols);
5681 }
5682 /* Temporary mark that we have no vlan tag. */
5683 flow->vlan_tci = htons(0);
5684 return 0;
5685
5686 case OFPACT_PUSH_VLAN:
5687 if (flow->vlan_tci & htons(VLAN_CFI)) {
5688 /* Multiple VLAN headers not supported. */
5689 return OFPERR_OFPBAC_BAD_TAG;
5690 }
5691 /* Temporary mark that we have a vlan tag. */
5692 flow->vlan_tci |= htons(VLAN_CFI);
5693 return 0;
5694
5695 case OFPACT_SET_ETH_SRC:
5696 case OFPACT_SET_ETH_DST:
5697 return 0;
5698
5699 case OFPACT_SET_IPV4_SRC:
5700 case OFPACT_SET_IPV4_DST:
5701 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5702 inconsistent_match(usable_protocols);
5703 }
5704 return 0;
5705
5706 case OFPACT_SET_IP_DSCP:
5707 case OFPACT_SET_IP_ECN:
5708 case OFPACT_SET_IP_TTL:
5709 case OFPACT_DEC_TTL:
5710 if (!is_ip_any(flow)) {
5711 inconsistent_match(usable_protocols);
5712 }
5713 return 0;
5714
5715 case OFPACT_SET_L4_SRC_PORT:
5716 case OFPACT_SET_L4_DST_PORT:
5717 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5718 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5719 && flow->nw_proto != IPPROTO_SCTP)) {
5720 inconsistent_match(usable_protocols);
5721 }
5722 /* Note on which transport protocol the port numbers are set.
5723 * This allows this set action to be converted to an OF1.2 set field
5724 * action. */
5725 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5726 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5727 } else {
5728 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5729 }
5730 return 0;
5731
5732 case OFPACT_REG_MOVE:
5733 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5734
5735 case OFPACT_SET_FIELD:
5736 mf = ofpact_get_SET_FIELD(a)->field;
5737 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5738 if (!mf_are_prereqs_ok(mf, flow) ||
5739 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5740 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5741 mf->name);
5742 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5743 }
5744 /* Remember if we saw a vlan tag in the flow to aid translating to
5745 * OpenFlow 1.1 if need be. */
5746 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5747 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5748 if (mf->id == MFF_VLAN_TCI) {
5749 /* The set field may add or remove the vlan tag,
5750 * Mark the status temporarily. */
5751 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5752 }
5753 return 0;
5754
5755 case OFPACT_STACK_PUSH:
5756 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5757
5758 case OFPACT_STACK_POP:
5759 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5760
5761 case OFPACT_SET_MPLS_LABEL:
5762 case OFPACT_SET_MPLS_TC:
5763 case OFPACT_SET_MPLS_TTL:
5764 case OFPACT_DEC_MPLS_TTL:
5765 if (!eth_type_mpls(flow->dl_type)) {
5766 inconsistent_match(usable_protocols);
5767 }
5768 return 0;
5769
5770 case OFPACT_SET_TUNNEL:
5771 case OFPACT_SET_QUEUE:
5772 case OFPACT_POP_QUEUE:
5773 case OFPACT_RESUBMIT:
5774 return 0;
5775
5776 case OFPACT_FIN_TIMEOUT:
5777 if (flow->nw_proto != IPPROTO_TCP) {
5778 inconsistent_match(usable_protocols);
5779 }
5780 return 0;
5781
5782 case OFPACT_LEARN:
5783 return learn_check(ofpact_get_LEARN(a), flow);
5784
5785 case OFPACT_CONJUNCTION:
5786 return 0;
5787
5788 case OFPACT_MULTIPATH:
5789 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5790
5791 case OFPACT_NOTE:
5792 case OFPACT_EXIT:
5793 return 0;
5794
5795 case OFPACT_PUSH_MPLS:
5796 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5797 /* The packet is now MPLS and the MPLS payload is opaque.
5798 * Thus nothing can be assumed about the network protocol.
5799 * Temporarily mark that we have no nw_proto. */
5800 flow->nw_proto = 0;
5801 return 0;
5802
5803 case OFPACT_POP_MPLS:
5804 if (!eth_type_mpls(flow->dl_type)) {
5805 inconsistent_match(usable_protocols);
5806 }
5807 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5808 return 0;
5809
5810 case OFPACT_SAMPLE:
5811 return 0;
5812
5813 case OFPACT_CLEAR_ACTIONS:
5814 return 0;
5815
5816 case OFPACT_WRITE_ACTIONS: {
5817 /* Use a temporary copy of 'usable_protocols' because we can't check
5818 * consistency of an action set. */
5819 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5820 enum ofputil_protocol p = *usable_protocols;
5821 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5822 flow, max_ports, table_id, n_tables, &p);
5823 }
5824
5825 case OFPACT_WRITE_METADATA:
5826 return 0;
5827
5828 case OFPACT_METER: {
5829 uint32_t mid = ofpact_get_METER(a)->meter_id;
5830 if (mid == 0 || mid > OFPM13_MAX) {
5831 return OFPERR_OFPMMFC_INVALID_METER;
5832 }
5833 return 0;
5834 }
5835
5836 case OFPACT_GOTO_TABLE: {
5837 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5838 if ((table_id != 255 && goto_table <= table_id)
5839 || (n_tables != 255 && goto_table >= n_tables)) {
5840 return OFPERR_OFPBIC_BAD_TABLE_ID;
5841 }
5842 return 0;
5843 }
5844
5845 case OFPACT_GROUP:
5846 return 0;
5847
5848 case OFPACT_UNROLL_XLATE:
5849 /* UNROLL is an internal action that should never be seen via
5850 * OpenFlow. */
5851 return OFPERR_OFPBAC_BAD_TYPE;
5852
5853 case OFPACT_DEBUG_RECIRC:
5854 return 0;
5855
5856 default:
5857 OVS_NOT_REACHED();
5858 }
5859}
5860
5861/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5862 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5863 * switch with no more than 'max_ports' ports.
5864 *
5865 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5866 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5867 * example of an inconsistency between match and actions is a flow that does
5868 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5869 *
5870 * May annotate ofpacts with information gathered from the 'flow'.
5871 *
5872 * May temporarily modify 'flow', but restores the changes before returning. */
5873enum ofperr
5874ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5875 struct flow *flow, ofp_port_t max_ports,
5876 uint8_t table_id, uint8_t n_tables,
5877 enum ofputil_protocol *usable_protocols)
5878{
5879 struct ofpact *a;
5880 ovs_be16 dl_type = flow->dl_type;
5881 ovs_be16 vlan_tci = flow->vlan_tci;
5882 uint8_t nw_proto = flow->nw_proto;
5883 enum ofperr error = 0;
5884
5885 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5886 error = ofpact_check__(usable_protocols, a, flow,
5887 max_ports, table_id, n_tables);
5888 if (error) {
5889 break;
5890 }
5891 }
5892 /* Restore fields that may have been modified. */
5893 flow->dl_type = dl_type;
5894 flow->vlan_tci = vlan_tci;
5895 flow->nw_proto = nw_proto;
5896 return error;
5897}
5898
5899/* Like ofpacts_check(), but reports inconsistencies as
5900 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5901enum ofperr
5902ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5903 struct flow *flow, ofp_port_t max_ports,
5904 uint8_t table_id, uint8_t n_tables,
5905 enum ofputil_protocol usable_protocols)
5906{
5907 enum ofputil_protocol p = usable_protocols;
5908 enum ofperr error;
5909
5910 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5911 table_id, n_tables, &p);
5912 return (error ? error
5913 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5914 : 0);
5915}
5916
5917static enum ofperr
5918ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
5919{
5920 if (outer_action != OFPACT_WRITE_ACTIONS) {
5921 VLOG_WARN("\"%s\" action doesn't support nested action \"%s\"",
5922 ofpact_name(outer_action), ofpact_name(a->type));
5923 return OFPERR_OFPBAC_BAD_ARGUMENT;
5924 }
5925
5926 return 0;
5927}
5928
5929/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
5930 * appropriate order as defined by the OpenFlow spec and as required by Open
5931 * vSwitch.
5932 *
5933 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
5934 * instructions that are allowed within 'ofpacts[]'.
5935 *
5936 * If 'outer_action' is not zero, it specifies that the actions are nested
5937 * within another action of type 'outer_action'. */
5938static enum ofperr
5939ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5940 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
5941{
5942 const struct ofpact *a;
5943 enum ovs_instruction_type inst;
5944
5945 inst = OVSINST_OFPIT13_METER;
5946 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5947 enum ovs_instruction_type next;
5948
5949 if (a->type == OFPACT_CONJUNCTION) {
5950 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5951 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
5952 VLOG_WARN("\"conjunction\" actions may be used along with "
5953 "\"note\" but not any other kind of action "
5954 "(such as the \"%s\" action used here)",
5955 ofpact_name(a->type));
5956 return OFPERR_NXBAC_BAD_CONJUNCTION;
5957 }
5958 }
5959 return 0;
5960 }
5961
5962 if (outer_action) {
5963 enum ofperr error = ofpacts_verify_nested(a, outer_action);
5964
5965 if (error) {
5966 return error;
5967 }
5968 }
5969
5970 next = ovs_instruction_type_from_ofpact_type(a->type);
5971 if (a > ofpacts
5972 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5973 ? next < inst
5974 : next <= inst)) {
5975 const char *name = ovs_instruction_name_from_type(inst);
5976 const char *next_name = ovs_instruction_name_from_type(next);
5977
5978 if (next == inst) {
5979 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5980 "1.1+ compatibility", name);
5981 } else {
5982 VLOG_WARN("invalid instruction ordering: %s must appear "
5983 "before %s, for OpenFlow 1.1+ compatibility",
5984 next_name, name);
5985 }
5986 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5987 }
5988 if (!((1u << next) & allowed_ovsinsts)) {
5989 const char *name = ovs_instruction_name_from_type(next);
5990
5991 VLOG_WARN("%s instruction not allowed here", name);
5992 return OFPERR_OFPBIC_UNSUP_INST;
5993 }
5994
5995 inst = next;
5996 }
5997
5998 return 0;
5999}
6000\f
6001/* Converting ofpacts to OpenFlow. */
6002
6003static void
6004encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6005 struct ofpbuf *out)
6006{
6007 switch (a->type) {
6008#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6009 case OFPACT_##ENUM: \
6010 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6011 return;
6012 OFPACTS
6013#undef OFPACT
6014 default:
6015 OVS_NOT_REACHED();
6016 }
6017}
6018
6019/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6020 * actions in 'openflow', appending the actions to any existing data in
6021 * 'openflow'. */
6022size_t
6023ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6024 struct ofpbuf *openflow,
6025 enum ofp_version ofp_version)
6026{
6027 const struct ofpact *a;
6028 size_t start_size = openflow->size;
6029
6030 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6031 encode_ofpact(a, ofp_version, openflow);
6032 }
6033 return openflow->size - start_size;
6034}
6035
6036static enum ovs_instruction_type
6037ofpact_is_apply_actions(const struct ofpact *a)
6038{
6039 return (ovs_instruction_type_from_ofpact_type(a->type)
6040 == OVSINST_OFPIT11_APPLY_ACTIONS);
6041}
6042
6043void
6044ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6045 size_t ofpacts_len,
6046 struct ofpbuf *openflow,
6047 enum ofp_version ofp_version)
6048{
6049 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6050 const struct ofpact *a;
6051
6052 if (ofp_version == OFP10_VERSION) {
6053 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6054 ofp_version);
6055 return;
6056 }
6057
6058 a = ofpacts;
6059 while (a < end) {
6060 if (ofpact_is_apply_actions(a)) {
6061 size_t ofs = openflow->size;
6062
6063 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6064 do {
6065 encode_ofpact(a, ofp_version, openflow);
6066 a = ofpact_next(a);
6067 } while (a < end && ofpact_is_apply_actions(a));
6068 ofpacts_update_instruction_actions(openflow, ofs);
6069 } else {
6070 encode_ofpact(a, ofp_version, openflow);
6071 a = ofpact_next(a);
6072 }
6073 }
6074}
6075\f
6076/* Sets of supported actions. */
6077
6078/* Two-way translation between OVS's internal "OFPACT_*" representation of
6079 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6080 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6081 * instance is specific to one OpenFlow version.) */
6082struct ofpact_map {
6083 enum ofpact_type ofpact; /* Internal name for action type. */
6084 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6085};
6086
6087static const struct ofpact_map *
6088get_ofpact_map(enum ofp_version version)
6089{
6090 /* OpenFlow 1.0 actions. */
6091 static const struct ofpact_map of10[] = {
6092 { OFPACT_OUTPUT, 0 },
6093 { OFPACT_SET_VLAN_VID, 1 },
6094 { OFPACT_SET_VLAN_PCP, 2 },
6095 { OFPACT_STRIP_VLAN, 3 },
6096 { OFPACT_SET_ETH_SRC, 4 },
6097 { OFPACT_SET_ETH_DST, 5 },
6098 { OFPACT_SET_IPV4_SRC, 6 },
6099 { OFPACT_SET_IPV4_DST, 7 },
6100 { OFPACT_SET_IP_DSCP, 8 },
6101 { OFPACT_SET_L4_SRC_PORT, 9 },
6102 { OFPACT_SET_L4_DST_PORT, 10 },
6103 { OFPACT_ENQUEUE, 11 },
6104 { 0, -1 },
6105 };
6106
6107 /* OpenFlow 1.1 actions. */
6108 static const struct ofpact_map of11[] = {
6109 { OFPACT_OUTPUT, 0 },
6110 { OFPACT_SET_VLAN_VID, 1 },
6111 { OFPACT_SET_VLAN_PCP, 2 },
6112 { OFPACT_SET_ETH_SRC, 3 },
6113 { OFPACT_SET_ETH_DST, 4 },
6114 { OFPACT_SET_IPV4_SRC, 5 },
6115 { OFPACT_SET_IPV4_DST, 6 },
6116 { OFPACT_SET_IP_DSCP, 7 },
6117 { OFPACT_SET_IP_ECN, 8 },
6118 { OFPACT_SET_L4_SRC_PORT, 9 },
6119 { OFPACT_SET_L4_DST_PORT, 10 },
6120 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6121 /* OFPAT_COPY_TTL_IN (12) not supported. */
6122 { OFPACT_SET_MPLS_LABEL, 13 },
6123 { OFPACT_SET_MPLS_TC, 14 },
6124 { OFPACT_SET_MPLS_TTL, 15 },
6125 { OFPACT_DEC_MPLS_TTL, 16 },
6126 { OFPACT_PUSH_VLAN, 17 },
6127 { OFPACT_STRIP_VLAN, 18 },
6128 { OFPACT_PUSH_MPLS, 19 },
6129 { OFPACT_POP_MPLS, 20 },
6130 { OFPACT_SET_QUEUE, 21 },
6131 { OFPACT_GROUP, 22 },
6132 { OFPACT_SET_IP_TTL, 23 },
6133 { OFPACT_DEC_TTL, 24 },
6134 { 0, -1 },
6135 };
6136
6137 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6138 static const struct ofpact_map of12[] = {
6139 { OFPACT_OUTPUT, 0 },
6140 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6141 /* OFPAT_COPY_TTL_IN (12) not supported. */
6142 { OFPACT_SET_MPLS_TTL, 15 },
6143 { OFPACT_DEC_MPLS_TTL, 16 },
6144 { OFPACT_PUSH_VLAN, 17 },
6145 { OFPACT_STRIP_VLAN, 18 },
6146 { OFPACT_PUSH_MPLS, 19 },
6147 { OFPACT_POP_MPLS, 20 },
6148 { OFPACT_SET_QUEUE, 21 },
6149 { OFPACT_GROUP, 22 },
6150 { OFPACT_SET_IP_TTL, 23 },
6151 { OFPACT_DEC_TTL, 24 },
6152 { OFPACT_SET_FIELD, 25 },
6153 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6154 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6155 { 0, -1 },
6156 };
6157
6158 switch (version) {
6159 case OFP10_VERSION:
6160 return of10;
6161
6162 case OFP11_VERSION:
6163 return of11;
6164
6165 case OFP12_VERSION:
6166 case OFP13_VERSION:
6167 case OFP14_VERSION:
6168 case OFP15_VERSION:
6169 default:
6170 return of12;
6171 }
6172}
6173
6174/* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6175 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6176 * returns the result. */
6177ovs_be32
6178ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6179{
6180 uint32_t openflow_bitmap = 0;
6181 const struct ofpact_map *x;
6182
6183 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6184 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6185 openflow_bitmap |= 1u << x->ofpat;
6186 }
6187 }
6188 return htonl(openflow_bitmap);
6189}
6190
6191/* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6192 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6193 * and returns the result. */
6194uint64_t
6195ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6196{
6197 uint64_t ofpact_bitmap = 0;
6198 const struct ofpact_map *x;
6199
6200 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6201 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6202 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6203 }
6204 }
6205 return ofpact_bitmap;
6206}
6207
6208/* Appends to 's' a string representation of the set of OFPACT_* represented
6209 * by 'ofpacts_bitmap'. */
6210void
6211ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6212{
6213 if (!ofpacts_bitmap) {
6214 ds_put_cstr(s, "<none>");
6215 } else {
6216 while (ofpacts_bitmap) {
6217 ds_put_format(s, "%s ",
6218 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6219 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6220 }
6221 ds_chomp(s, ' ');
6222 }
6223}
6224\f
6225/* Returns true if 'action' outputs to 'port', false otherwise. */
6226static bool
6227ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6228{
6229 switch (ofpact->type) {
6230 case OFPACT_OUTPUT:
6231 return ofpact_get_OUTPUT(ofpact)->port == port;
6232 case OFPACT_ENQUEUE:
6233 return ofpact_get_ENQUEUE(ofpact)->port == port;
6234 case OFPACT_CONTROLLER:
6235 return port == OFPP_CONTROLLER;
6236
6237 case OFPACT_OUTPUT_REG:
6238 case OFPACT_BUNDLE:
6239 case OFPACT_SET_VLAN_VID:
6240 case OFPACT_SET_VLAN_PCP:
6241 case OFPACT_STRIP_VLAN:
6242 case OFPACT_PUSH_VLAN:
6243 case OFPACT_SET_ETH_SRC:
6244 case OFPACT_SET_ETH_DST:
6245 case OFPACT_SET_IPV4_SRC:
6246 case OFPACT_SET_IPV4_DST:
6247 case OFPACT_SET_IP_DSCP:
6248 case OFPACT_SET_IP_ECN:
6249 case OFPACT_SET_IP_TTL:
6250 case OFPACT_SET_L4_SRC_PORT:
6251 case OFPACT_SET_L4_DST_PORT:
6252 case OFPACT_REG_MOVE:
6253 case OFPACT_SET_FIELD:
6254 case OFPACT_STACK_PUSH:
6255 case OFPACT_STACK_POP:
6256 case OFPACT_DEC_TTL:
6257 case OFPACT_SET_MPLS_LABEL:
6258 case OFPACT_SET_MPLS_TC:
6259 case OFPACT_SET_MPLS_TTL:
6260 case OFPACT_DEC_MPLS_TTL:
6261 case OFPACT_SET_TUNNEL:
6262 case OFPACT_WRITE_METADATA:
6263 case OFPACT_SET_QUEUE:
6264 case OFPACT_POP_QUEUE:
6265 case OFPACT_FIN_TIMEOUT:
6266 case OFPACT_RESUBMIT:
6267 case OFPACT_LEARN:
6268 case OFPACT_CONJUNCTION:
6269 case OFPACT_MULTIPATH:
6270 case OFPACT_NOTE:
6271 case OFPACT_EXIT:
6272 case OFPACT_UNROLL_XLATE:
6273 case OFPACT_PUSH_MPLS:
6274 case OFPACT_POP_MPLS:
6275 case OFPACT_SAMPLE:
6276 case OFPACT_CLEAR_ACTIONS:
6277 case OFPACT_WRITE_ACTIONS:
6278 case OFPACT_GOTO_TABLE:
6279 case OFPACT_METER:
6280 case OFPACT_GROUP:
6281 case OFPACT_DEBUG_RECIRC:
6282 default:
6283 return false;
6284 }
6285}
6286
6287/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6288 * to 'port', false otherwise. */
6289bool
6290ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6291 ofp_port_t port)
6292{
6293 const struct ofpact *a;
6294
6295 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6296 if (ofpact_outputs_to_port(a, port)) {
6297 return true;
6298 }
6299 }
6300
6301 return false;
6302}
6303
6304/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6305 * to 'group', false otherwise. */
6306bool
6307ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6308 uint32_t group_id)
6309{
6310 const struct ofpact *a;
6311
6312 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6313 if (a->type == OFPACT_GROUP
6314 && ofpact_get_GROUP(a)->group_id == group_id) {
6315 return true;
6316 }
6317 }
6318
6319 return false;
6320}
6321
6322bool
6323ofpacts_equal(const struct ofpact *a, size_t a_len,
6324 const struct ofpact *b, size_t b_len)
6325{
6326 return a_len == b_len && !memcmp(a, b, a_len);
6327}
6328
6329/* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6330 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6331 *
6332 * This function relies on the order of 'ofpacts' being correct (as checked by
6333 * ofpacts_verify()). */
6334uint32_t
6335ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6336{
6337 const struct ofpact *a;
6338
6339 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6340 enum ovs_instruction_type inst;
6341
6342 inst = ovs_instruction_type_from_ofpact_type(a->type);
6343 if (a->type == OFPACT_METER) {
6344 return ofpact_get_METER(a)->meter_id;
6345 } else if (inst > OVSINST_OFPIT13_METER) {
6346 break;
6347 }
6348 }
6349
6350 return 0;
6351}
6352\f
6353/* Formatting ofpacts. */
6354
6355static void
6356ofpact_format(const struct ofpact *a, struct ds *s)
6357{
6358 switch (a->type) {
6359#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6360 case OFPACT_##ENUM: \
6361 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6362 break;
6363 OFPACTS
6364#undef OFPACT
6365 default:
6366 OVS_NOT_REACHED();
6367 }
6368}
6369
6370/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6371 * 'ofpacts' to 'string'. */
6372void
6373ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6374 struct ds *string)
6375{
6376 if (!ofpacts_len) {
6377 ds_put_cstr(string, "drop");
6378 } else {
6379 const struct ofpact *a;
6380
6381 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6382 if (a != ofpacts) {
6383 ds_put_cstr(string, ",");
6384 }
6385
6386 /* XXX write-actions */
6387 ofpact_format(a, string);
6388 }
6389 }
6390}
6391\f
6392/* Internal use by helpers. */
6393
6394void *
6395ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6396{
6397 struct ofpact *ofpact;
6398
6399 ofpact_pad(ofpacts);
6400 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6401 ofpact = ofpacts->header;
6402 ofpact_init(ofpact, type, len);
6403 return ofpact;
6404}
6405
6406void
6407ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6408{
6409 memset(ofpact, 0, len);
6410 ofpact->type = type;
6411 ofpact->raw = -1;
6412 ofpact->len = len;
6413}
6414\f
6415/* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6416 * starting at 'ofpact'.
6417 *
6418 * This is the correct way to update a variable-length ofpact's length after
6419 * adding the variable-length part of the payload. (See the large comment
6420 * near the end of ofp-actions.h for more information.) */
6421void
6422ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6423{
6424 ovs_assert(ofpact == ofpacts->header);
6425 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6426}
6427
6428/* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6429 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6430 * client must call this itself after adding the final ofpact to an array of
6431 * them.
6432 *
6433 * (The consequences of failing to call this function are probably not dire.
6434 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6435 * not dereference it. That's undefined behavior, technically, but it will not
6436 * cause a real problem on common systems. Still, it seems better to call
6437 * it.) */
6438void
6439ofpact_pad(struct ofpbuf *ofpacts)
6440{
6441 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6442 if (pad) {
6443 ofpbuf_put_zeros(ofpacts, pad);
6444 }
6445}
6446\f
6447
6448
6449
6450static char * OVS_WARN_UNUSED_RESULT
6451ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6452 enum ofputil_protocol *usable_protocols)
6453{
6454 switch (type) {
6455#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6456 case OFPACT_##ENUM: \
6457 return parse_##ENUM(value, ofpacts, usable_protocols);
6458 OFPACTS
6459#undef OFPACT
6460 default:
6461 OVS_NOT_REACHED();
6462 }
6463}
6464
6465static bool
6466ofpact_type_from_name(const char *name, enum ofpact_type *type)
6467{
6468#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6469 if (!strcasecmp(name, NAME)) { \
6470 *type = OFPACT_##ENUM; \
6471 return true; \
6472 }
6473 OFPACTS
6474#undef OFPACT
6475
6476 return false;
6477}
6478
6479/* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6480 *
6481 * Returns NULL if successful, otherwise a malloc()'d string describing the
6482 * error. The caller is responsible for freeing the returned string.
6483 *
6484 * If 'outer_action' is specified, indicates that the actions being parsed
6485 * are nested within another action of the type specified in 'outer_action'. */
6486static char * OVS_WARN_UNUSED_RESULT
6487ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6488 enum ofputil_protocol *usable_protocols,
6489 bool allow_instructions, enum ofpact_type outer_action)
6490{
6491 int prev_inst = -1;
6492 enum ofperr retval;
6493 char *key, *value;
6494 bool drop = false;
6495 char *pos;
6496
6497 pos = str;
6498 while (ofputil_parse_key_value(&pos, &key, &value)) {
6499 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6500 enum ofpact_type type;
6501 char *error = NULL;
6502 ofp_port_t port;
6503
6504 if (ofpact_type_from_name(key, &type)) {
6505 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6506 inst = ovs_instruction_type_from_ofpact_type(type);
6507 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6508 error = parse_set_vlan_vid(value, ofpacts, true);
6509 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6510 error = parse_set_vlan_pcp(value, ofpacts, true);
6511 } else if (!strcasecmp(key, "set_nw_ttl")) {
6512 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6513 } else if (!strcasecmp(key, "pop_vlan")) {
6514 error = parse_pop_vlan(ofpacts);
6515 } else if (!strcasecmp(key, "set_tunnel64")) {
6516 error = parse_set_tunnel(value, ofpacts,
6517 NXAST_RAW_SET_TUNNEL64);
6518 } else if (!strcasecmp(key, "load")) {
6519 error = parse_reg_load(value, ofpacts);
6520 } else if (!strcasecmp(key, "bundle_load")) {
6521 error = parse_bundle_load(value, ofpacts);
6522 } else if (!strcasecmp(key, "drop")) {
6523 drop = true;
6524 } else if (!strcasecmp(key, "apply_actions")) {
6525 return xstrdup("apply_actions is the default instruction");
6526 } else if (ofputil_port_from_string(key, &port)) {
6527 ofpact_put_OUTPUT(ofpacts)->port = port;
6528 } else {
6529 return xasprintf("unknown action %s", key);
6530 }
6531 if (error) {
6532 return error;
6533 }
6534
6535 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6536 if (!allow_instructions) {
6537 return xasprintf("only actions are allowed here (not "
6538 "instruction %s)",
6539 ovs_instruction_name_from_type(inst));
6540 }
6541 if (inst == prev_inst) {
6542 return xasprintf("instruction %s may be specified only once",
6543 ovs_instruction_name_from_type(inst));
6544 }
6545 }
6546 if (prev_inst != -1 && inst < prev_inst) {
6547 return xasprintf("instruction %s must be specified before %s",
6548 ovs_instruction_name_from_type(inst),
6549 ovs_instruction_name_from_type(prev_inst));
6550 }
6551 prev_inst = inst;
6552 }
6553 ofpact_pad(ofpacts);
6554
6555 if (drop && ofpacts->size) {
6556 return xstrdup("\"drop\" must not be accompanied by any other action "
6557 "or instruction");
6558 }
6559
6560 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6561 (allow_instructions
6562 ? (1u << N_OVS_INSTRUCTIONS) - 1
6563 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6564 outer_action);
6565 if (retval) {
6566 return xstrdup("Incorrect instruction ordering");
6567 }
6568
6569 return NULL;
6570}
6571
6572static char * OVS_WARN_UNUSED_RESULT
6573ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6574 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6575 enum ofpact_type outer_action)
6576{
6577 uint32_t orig_size = ofpacts->size;
6578 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6579 allow_instructions, outer_action);
6580 if (error) {
6581 ofpacts->size = orig_size;
6582 }
6583 return error;
6584}
6585
6586static char * OVS_WARN_UNUSED_RESULT
6587ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6588 enum ofputil_protocol *usable_protocols,
6589 bool allow_instructions, enum ofpact_type outer_action)
6590{
6591 char *error, *s;
6592
6593 *usable_protocols = OFPUTIL_P_ANY;
6594
6595 s = xstrdup(s_);
6596 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6597 outer_action);
6598 free(s);
6599
6600 return error;
6601}
6602
6603/* Parses 's' as a set of OpenFlow actions and appends the actions to
6604 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6605 * that are nested within the action of type 'outer_action'.
6606 *
6607 * Returns NULL if successful, otherwise a malloc()'d string describing the
6608 * error. The caller is responsible for freeing the returned string. */
6609char * OVS_WARN_UNUSED_RESULT
6610ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6611 enum ofputil_protocol *usable_protocols)
6612{
6613 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6614}
6615
6616/* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6617 * 'ofpacts'.
6618 *
6619 * Returns NULL if successful, otherwise a malloc()'d string describing the
6620 * error. The caller is responsible for freeing the returned string. */
6621char * OVS_WARN_UNUSED_RESULT
6622ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6623 enum ofputil_protocol *usable_protocols)
6624{
6625 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6626}
6627
6628const char *
6629ofpact_name(enum ofpact_type type)
6630{
6631 switch (type) {
6632#define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6633 OFPACTS
6634#undef OFPACT
6635 }
6636 return "<unknown>";
6637}
6638\f
6639/* Low-level action decoding and encoding functions. */
6640
6641/* Everything needed to identify a particular OpenFlow action. */
6642struct ofpact_hdrs {
6643 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6644 uint16_t type; /* Type if standard, otherwise subtype. */
6645 uint8_t ofp_version; /* From ofp_header. */
6646};
6647
6648/* Information about a particular OpenFlow action. */
6649struct ofpact_raw_instance {
6650 /* The action's identity. */
6651 struct ofpact_hdrs hdrs;
6652 enum ofp_raw_action_type raw;
6653
6654 /* Looking up the action. */
6655 struct hmap_node decode_node; /* Based on 'hdrs'. */
6656 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6657
6658 /* The action's encoded size.
6659 *
6660 * If this action is fixed-length, 'min_length' == 'max_length'.
6661 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6662 * OFP_ACTION_ALIGN) == 65528. */
6663 unsigned short int min_length;
6664 unsigned short int max_length;
6665
6666 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6667 * offset of that argument from the beginning of the action and 'arg_len'
6668 * its length, both in bytes.
6669 *
6670 * For actions that take other forms, these are both zero. */
6671 unsigned short int arg_ofs;
6672 unsigned short int arg_len;
6673
6674 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6675 const char *name;
6676
6677 /* If this action is deprecated, a human-readable string with a brief
6678 * explanation. */
6679 const char *deprecation;
6680};
6681
6682/* Action header. */
6683struct ofp_action_header {
6684 /* The meaning of other values of 'type' generally depends on the OpenFlow
6685 * version (see enum ofp_raw_action_type).
6686 *
6687 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6688 * designates an OpenFlow vendor ID and that the remainder of the action
6689 * structure has a vendor-defined meaning.
6690 */
6691#define OFPAT_VENDOR 0xffff
6692 ovs_be16 type;
6693
6694 /* Always a multiple of 8. */
6695 ovs_be16 len;
6696
6697 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6698 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6699 ovs_be32 vendor;
6700};
6701OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6702
6703/* Header for Nicira-defined actions and for ONF vendor extensions.
6704 *
6705 * This cannot be used as an entirely generic vendor extension action header,
6706 * because OpenFlow does not specify the location or size of the action
6707 * subtype; it just happens that ONF extensions and Nicira extensions share
6708 * this format. */
6709struct ext_action_header {
6710 ovs_be16 type; /* OFPAT_VENDOR. */
6711 ovs_be16 len; /* At least 16. */
6712 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6713 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6714 uint8_t pad[6];
6715};
6716OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6717
6718static bool
6719ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6720 const struct ofpact_hdrs *b)
6721{
6722 return (a->vendor == b->vendor
6723 && a->type == b->type
6724 && a->ofp_version == b->ofp_version);
6725}
6726
6727static uint32_t
6728ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6729{
6730 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6731}
6732
6733#include "ofp-actions.inc2"
6734
6735static struct hmap *
6736ofpact_decode_hmap(void)
6737{
6738 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6739 static struct hmap hmap;
6740
6741 if (ovsthread_once_start(&once)) {
6742 struct ofpact_raw_instance *inst;
6743
6744 hmap_init(&hmap);
6745 for (inst = all_raw_instances;
6746 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6747 inst++) {
6748 hmap_insert(&hmap, &inst->decode_node,
6749 ofpact_hdrs_hash(&inst->hdrs));
6750 }
6751 ovsthread_once_done(&once);
6752 }
6753 return &hmap;
6754}
6755
6756static struct hmap *
6757ofpact_encode_hmap(void)
6758{
6759 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6760 static struct hmap hmap;
6761
6762 if (ovsthread_once_start(&once)) {
6763 struct ofpact_raw_instance *inst;
6764
6765 hmap_init(&hmap);
6766 for (inst = all_raw_instances;
6767 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6768 inst++) {
6769 hmap_insert(&hmap, &inst->encode_node,
6770 hash_2words(inst->raw, inst->hdrs.ofp_version));
6771 }
6772 ovsthread_once_done(&once);
6773 }
6774 return &hmap;
6775}
6776
6777static enum ofperr
6778ofpact_decode_raw(enum ofp_version ofp_version,
6779 const struct ofp_action_header *oah, size_t length,
6780 const struct ofpact_raw_instance **instp)
6781{
6782 const struct ofpact_raw_instance *inst;
6783 struct ofpact_hdrs hdrs;
6784
6785 *instp = NULL;
6786 if (length < sizeof *oah) {
6787 return OFPERR_OFPBAC_BAD_LEN;
6788 }
6789
6790 /* Get base action type. */
6791 if (oah->type == htons(OFPAT_VENDOR)) {
6792 /* Get vendor. */
6793 hdrs.vendor = ntohl(oah->vendor);
6794 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
6795 /* Get extension subtype. */
6796 const struct ext_action_header *nah;
6797
6798 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
6799 if (length < sizeof *nah) {
6800 return OFPERR_OFPBAC_BAD_LEN;
6801 }
6802 hdrs.type = ntohs(nah->subtype);
6803 } else {
6804 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6805 hdrs.vendor);
6806 return OFPERR_OFPBAC_BAD_VENDOR;
6807 }
6808 } else {
6809 hdrs.vendor = 0;
6810 hdrs.type = ntohs(oah->type);
6811 }
6812
6813 hdrs.ofp_version = ofp_version;
6814 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6815 ofpact_decode_hmap()) {
6816 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6817 *instp = inst;
6818 return 0;
6819 }
6820 }
6821
6822 return (hdrs.vendor
6823 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6824 : OFPERR_OFPBAC_BAD_TYPE);
6825}
6826
6827static enum ofperr
6828ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6829 enum ofp_raw_action_type *raw, uint64_t *arg)
6830{
6831 const struct ofp_action_header *oah = buf->data;
6832 const struct ofpact_raw_instance *action;
6833 unsigned int length;
6834 enum ofperr error;
6835
6836 *raw = *arg = 0;
6837 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
6838 if (error) {
6839 return error;
6840 }
6841
6842 if (action->deprecation) {
6843 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6844 action->name, ofputil_version_to_string(ofp_version),
6845 action->deprecation);
6846 }
6847
6848 length = ntohs(oah->len);
6849 if (length > buf->size) {
6850 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6851 "length %"PRIu32, action->name, length, buf->size);
6852 return OFPERR_OFPBAC_BAD_LEN;
6853 }
6854 if (length < action->min_length || length > action->max_length) {
6855 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6856 "[%hu,%hu]", action->name, length,
6857 action->min_length, action->max_length);
6858 return OFPERR_OFPBAC_BAD_LEN;
6859 }
6860 if (length % 8) {
6861 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6862 "of 8", action->name, length);
6863 return OFPERR_OFPBAC_BAD_LEN;
6864 }
6865
6866 *raw = action->raw;
6867 *arg = 0;
6868 if (action->arg_len) {
6869 const uint8_t *p;
6870 int i;
6871
6872 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6873 for (i = 0; i < action->arg_len; i++) {
6874 *arg = (*arg << 8) | p[i];
6875 }
6876 }
6877
6878 ofpbuf_pull(buf, length);
6879
6880 return 0;
6881}
6882
6883static const struct ofpact_raw_instance *
6884ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6885{
6886 const struct ofpact_raw_instance *inst;
6887
6888 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6889 ofpact_encode_hmap()) {
6890 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6891 return inst;
6892 }
6893 }
6894 OVS_NOT_REACHED();
6895}
6896
6897static void *
6898ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6899 enum ofp_raw_action_type raw, uint64_t arg)
6900{
6901 const struct ofpact_raw_instance *inst;
6902 struct ofp_action_header *oah;
6903 const struct ofpact_hdrs *hdrs;
6904
6905 inst = ofpact_raw_lookup(ofp_version, raw);
6906 hdrs = &inst->hdrs;
6907
6908 oah = ofpbuf_put_zeros(buf, inst->min_length);
6909 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6910 oah->len = htons(inst->min_length);
6911 oah->vendor = htonl(hdrs->vendor);
6912
6913 switch (hdrs->vendor) {
6914 case 0:
6915 break;
6916
6917 case NX_VENDOR_ID:
6918 case ONF_VENDOR_ID: {
6919 struct ext_action_header *nah = (struct ext_action_header *) oah;
6920 nah->subtype = htons(hdrs->type);
6921 break;
6922 }
6923
6924 default:
6925 OVS_NOT_REACHED();
6926 }
6927
6928 if (inst->arg_len) {
6929 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6930 int i;
6931
6932 for (i = 0; i < inst->arg_len; i++) {
6933 *--p = arg;
6934 arg >>= 8;
6935 }
6936 } else {
6937 ovs_assert(!arg);
6938 }
6939
6940 return oah;
6941}
6942
6943static void
6944pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6945{
6946 struct ofp_action_header *oah;
6947
6948 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
6949
6950 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6951 oah->len = htons(openflow->size - start_ofs);
6952}
6953