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