Coverage Report

Created: 2026-05-30 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/ofp-actions.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008-2017, 2019-2020 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
19
#include <sys/types.h>
20
#include <netinet/in.h>
21
22
#include "bundle.h"
23
#include "byte-order.h"
24
#include "colors.h"
25
#include "compiler.h"
26
#include "dummy.h"
27
#include "openvswitch/hmap.h"
28
#include "learn.h"
29
#include "multipath.h"
30
#include "nx-match.h"
31
#include "odp-netlink.h"
32
#include "openvswitch/dynamic-string.h"
33
#include "openvswitch/meta-flow.h"
34
#include "openvswitch/ofp-actions.h"
35
#include "openvswitch/ofp-packet.h"
36
#include "openvswitch/ofp-parse.h"
37
#include "openvswitch/ofp-port.h"
38
#include "openvswitch/ofp-prop.h"
39
#include "openvswitch/ofp-table.h"
40
#include "openvswitch/ofpbuf.h"
41
#include "openvswitch/vlog.h"
42
#include "unaligned.h"
43
#include "util.h"
44
#include "vl-mff-map.h"
45
46
VLOG_DEFINE_THIS_MODULE(ofp_actions);
47
48
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
49
50
struct ofp_action_header;
51
52
/* Header for Open vSwitch and ONF vendor extension actions.
53
 *
54
 * This is the entire header for a few Open vSwitch vendor extension actions,
55
 * the ones that either have no arguments or for which variable-length
56
 * arguments follow the header.
57
 *
58
 * This cannot be used as an entirely generic vendor extension action header,
59
 * because OpenFlow does not specify the location or size of the action
60
 * subtype; it just happens that ONF extensions and Nicira extensions share
61
 * this format. */
62
struct ext_action_header {
63
    ovs_be16 type;                  /* OFPAT_VENDOR. */
64
    ovs_be16 len;                   /* At least 16. */
65
    ovs_be32 vendor;                /* NX_VENDOR_ID or ONF_VENDOR_ID. */
66
    ovs_be16 subtype;               /* See enum ofp_raw_action_type. */
67
    uint8_t pad[6];
68
};
69
OFP_ASSERT(sizeof(struct ext_action_header) == 16);
70
71
/* Raw identifiers for OpenFlow actions.
72
 *
73
 * Decoding and encoding OpenFlow actions across multiple versions is difficult
74
 * to do in a clean, consistent way.  This enumeration lays out all of the
75
 * forms of actions that Open vSwitch supports.
76
 *
77
 * The comments here must follow a stylized form because the
78
 * "extract-ofp-actions" program parses them at build time to generate data
79
 * tables.
80
 *
81
 *   - The first part of each comment specifies the vendor, OpenFlow versions,
82
 *     and type for each protocol that supports the action:
83
 *
84
 *         # The vendor is OF for standard OpenFlow actions, NX for Nicira
85
 *           extension actions.  (Support for other vendors can be added, but
86
 *           it can't be done just based on a vendor ID definition alone
87
 *           because OpenFlow doesn't define a standard way to specify a
88
 *           subtype for vendor actions, so other vendors might do it different
89
 *           from Nicira.)
90
 *
91
 *         # The version can specify a specific OpenFlow version, a version
92
 *           range delimited by "-", or an open-ended range with "+".
93
 *
94
 *         # The type, in parentheses, is the action type number (for standard
95
 *           OpenFlow actions) or subtype (for vendor extension actions).
96
 *
97
 *         # Optionally one may add "is deprecated" followed by a
98
 *           human-readable reason in parentheses (which will be used in log
99
 *           messages), if a particular action should no longer be used.
100
 *
101
 *     Multiple such specifications may be separated by commas.
102
 *
103
 *   - The second part describes the action's wire format.  It may be:
104
 *
105
 *         # "struct <name>": The struct fully specifies the wire format.  The
106
 *           action is exactly the size of the struct.  (Thus, the struct must
107
 *           be an exact multiple of 8 bytes in size.)
108
 *
109
 *         # "struct <name>, ...": The struct specifies the beginning of the
110
 *           wire format.  An instance of the action is either the struct's
111
 *           exact size, or a multiple of 8 bytes longer.
112
 *
113
 *         # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
114
 *           vendor extension) header, followed by 0 or more pad bytes to align
115
 *           to a multiple of <N> bits, followed by an argument of the given
116
 *           type, followed by 0 or more pad bytes to bring the total action up
117
 *           to a multiple of 8 bytes.
118
 *
119
 *         # "void": The action is just a (standard or vendor extension)
120
 *           header.
121
 *
122
 *         # Optionally, one may add "VLMFF" in the end of the second part if
123
 *           the Openflow action may use a variable length meta-flow field
124
 *           (i.e. tun_metadata). Adding "VLMFF" will pass the per-switch based
125
 *           variable length meta-flow field mapping map (struct vl_mff_map) to
126
 *           the corresponding action decoding function.
127
 *
128
 *   - Optional additional text enclosed in square brackets is commentary for
129
 *     the human reader.
130
 */
131
enum ofp_raw_action_type {
132
/* ## ----------------- ## */
133
/* ## Standard actions. ## */
134
/* ## ----------------- ## */
135
136
    /* OF1.0(0): struct ofp10_action_output. */
137
    OFPAT_RAW10_OUTPUT,
138
    /* OF1.1+(0): struct ofp11_action_output. */
139
    OFPAT_RAW11_OUTPUT,
140
141
    /* OF1.0(1): uint16_t. */
142
    OFPAT_RAW10_SET_VLAN_VID,
143
    /* OF1.0(2): uint8_t. */
144
    OFPAT_RAW10_SET_VLAN_PCP,
145
146
    /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
147
     *
148
     * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
149
     * modification actions: the 1.0 versions push a VLAN header if none is
150
     * present, but the 1.1 versions do not.  That is the only reason that we
151
     * distinguish their raw action types.] */
152
    OFPAT_RAW11_SET_VLAN_VID,
153
    /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
154
    OFPAT_RAW11_SET_VLAN_PCP,
155
156
    /* OF1.1+(17): ovs_be16.
157
     *
158
     * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
159
     * TCI.] */
160
    OFPAT_RAW11_PUSH_VLAN,
161
162
    /* OF1.0(3): void. */
163
    OFPAT_RAW10_STRIP_VLAN,
164
    /* OF1.1+(18): void. */
165
    OFPAT_RAW11_POP_VLAN,
166
167
    /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
168
     * ofp_action_dl_addr. */
169
    OFPAT_RAW_SET_DL_SRC,
170
171
    /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
172
     * ofp_action_dl_addr. */
173
    OFPAT_RAW_SET_DL_DST,
174
175
    /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
176
     * ovs_be32. */
177
    OFPAT_RAW_SET_NW_SRC,
178
179
    /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
180
     * ovs_be32. */
181
    OFPAT_RAW_SET_NW_DST,
182
183
    /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
184
    OFPAT_RAW_SET_NW_TOS,
185
186
    /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
187
    OFPAT_RAW11_SET_NW_ECN,
188
189
    /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
190
     * ovs_be16. */
191
    OFPAT_RAW_SET_TP_SRC,
192
193
    /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
194
     * ovs_be16. */
195
    OFPAT_RAW_SET_TP_DST,
196
197
    /* OF1.0(11): struct ofp10_action_enqueue. */
198
    OFPAT_RAW10_ENQUEUE,
199
200
    /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
201
     * ovs_be32. */
202
    OFPAT_RAW_SET_MPLS_LABEL,
203
204
    /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
205
     * uint8_t. */
206
    OFPAT_RAW_SET_MPLS_TC,
207
208
    /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
209
     * uint8_t. */
210
    OFPAT_RAW_SET_MPLS_TTL,
211
212
    /* NX1.0(26), OF1.1+(16): void. */
213
    OFPAT_RAW_DEC_MPLS_TTL,
214
215
    /* NX1.0(23), OF1.1+(19): ovs_be16.
216
     *
217
     * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
218
    OFPAT_RAW_PUSH_MPLS,
219
220
    /* NX1.0(24), OF1.1+(20): ovs_be16.
221
     *
222
     * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
223
     * ETH_TYPE_MPLS otherwise, not the label.] */
224
    OFPAT_RAW_POP_MPLS,
225
226
    /* NX1.0(4), OF1.1+(21): uint32_t. */
227
    OFPAT_RAW_SET_QUEUE,
228
229
    /* NX1.0(40), OF1.1+(22): uint32_t. */
230
    OFPAT_RAW_GROUP,
231
232
    /* OF1.1+(23): uint8_t. */
233
    OFPAT_RAW11_SET_NW_TTL,
234
235
    /* NX1.0(18), OF1.1+(24): void. */
236
    OFPAT_RAW_DEC_NW_TTL,
237
    /* NX1.0+(21): struct nx_action_cnt_ids, ... */
238
    NXAST_RAW_DEC_TTL_CNT_IDS,
239
240
    /* OF1.2-1.4(25): struct ofp12_action_set_field, ... VLMFF */
241
    OFPAT_RAW12_SET_FIELD,
242
    /* OF1.5+(25): struct ofp12_action_set_field, ... VLMFF */
243
    OFPAT_RAW15_SET_FIELD,
244
    /* NX1.0-1.4(7): struct nx_action_reg_load. VLMFF
245
     *
246
     * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
247
     * we drop reg_load.] */
248
    NXAST_RAW_REG_LOAD,
249
    /* NX1.0-1.4(33): struct ext_action_header, ... VLMFF
250
     *
251
     * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
252
     * we drop reg_load2.] */
253
    NXAST_RAW_REG_LOAD2,
254
255
    /* OF1.5+(28): struct ofp15_action_copy_field, ... VLMFF */
256
    OFPAT_RAW15_COPY_FIELD,
257
    /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... VLMFF */
258
    ONFACT_RAW13_COPY_FIELD,
259
    /* NX1.0-1.4(6): struct nx_action_reg_move, ... VLMFF */
260
    NXAST_RAW_REG_MOVE,
261
262
    /* OF1.5+(29): uint32_t. */
263
    OFPAT_RAW15_METER,
264
265
/* ## ------------------------- ## */
266
/* ## Nicira extension actions. ## */
267
/* ## ------------------------- ## */
268
269
/* Actions similar to standard actions are listed with the standard actions. */
270
271
    /* NX1.0+(1): uint16_t. */
272
    NXAST_RAW_RESUBMIT,
273
    /* NX1.0+(14): struct nx_action_resubmit. */
274
    NXAST_RAW_RESUBMIT_TABLE,
275
    /* NX1.0+(44): struct nx_action_resubmit. */
276
    NXAST_RAW_RESUBMIT_TABLE_CT,
277
278
    /* NX1.0+(2): uint32_t. */
279
    NXAST_RAW_SET_TUNNEL,
280
    /* NX1.0+(9): uint64_t. */
281
    NXAST_RAW_SET_TUNNEL64,
282
283
    /* NX1.0+(5): void. */
284
    NXAST_RAW_POP_QUEUE,
285
286
    /* NX1.0+(8): struct nx_action_note, ... */
287
    NXAST_RAW_NOTE,
288
289
    /* NX1.0+(10): struct nx_action_multipath. VLMFF */
290
    NXAST_RAW_MULTIPATH,
291
292
    /* NX1.0+(12): struct nx_action_bundle, ... */
293
    NXAST_RAW_BUNDLE,
294
    /* NX1.0+(13): struct nx_action_bundle, ... VLMFF */
295
    NXAST_RAW_BUNDLE_LOAD,
296
297
    /* NX1.0+(15): struct nx_action_output_reg. VLMFF */
298
    NXAST_RAW_OUTPUT_REG,
299
    /* NX1.0+(32): struct nx_action_output_reg2. VLMFF */
300
    NXAST_RAW_OUTPUT_REG2,
301
302
    /* NX1.0+(16): struct nx_action_learn, ... VLMFF */
303
    NXAST_RAW_LEARN,
304
    /* NX1.0+(45): struct nx_action_learn2, ... VLMFF */
305
    NXAST_RAW_LEARN2,
306
307
    /* NX1.0+(17): void. */
308
    NXAST_RAW_EXIT,
309
310
    /* NX1.0+(19): struct nx_action_fin_timeout. */
311
    NXAST_RAW_FIN_TIMEOUT,
312
313
    /* NX1.0+(20): struct nx_action_controller. */
314
    NXAST_RAW_CONTROLLER,
315
    /* NX1.0+(37): struct ext_action_header, ... */
316
    NXAST_RAW_CONTROLLER2,
317
318
    /* NX1.0+(22): struct nx_action_write_metadata. */
319
    NXAST_RAW_WRITE_METADATA,
320
321
    /* NX1.0+(27): struct nx_action_stack. VLMFF */
322
    NXAST_RAW_STACK_PUSH,
323
324
    /* NX1.0+(28): struct nx_action_stack. VLMFF */
325
    NXAST_RAW_STACK_POP,
326
327
    /* NX1.0+(29): struct nx_action_sample. */
328
    NXAST_RAW_SAMPLE,
329
    /* NX1.0+(38): struct nx_action_sample2. */
330
    NXAST_RAW_SAMPLE2,
331
    /* NX1.0+(41): struct nx_action_sample2. */
332
    NXAST_RAW_SAMPLE3,
333
    /* NX1.0+(51): struct nx_action_sample4. VLMFF */
334
    NXAST_RAW_SAMPLE4,
335
336
    /* NX1.0+(34): struct nx_action_conjunction. */
337
    NXAST_RAW_CONJUNCTION,
338
339
    /* NX1.0+(35): struct nx_action_conntrack, ... VLMFF */
340
    NXAST_RAW_CT,
341
342
    /* NX1.0+(36): struct nx_action_nat, ... */
343
    NXAST_RAW_NAT,
344
345
    /* NX1.0+(39): struct nx_action_output_trunc. */
346
    NXAST_RAW_OUTPUT_TRUNC,
347
348
    /* NX1.0+(42): struct ext_action_header, ... VLMFF */
349
    NXAST_RAW_CLONE,
350
351
    /* NX1.0+(43): void. */
352
    NXAST_RAW_CT_CLEAR,
353
354
    /* NX1.3+(46): struct nx_action_encap, ... */
355
    NXAST_RAW_ENCAP,
356
357
    /* NX1.3+(47): struct nx_action_decap, ... */
358
    NXAST_RAW_DECAP,
359
360
    /* NX1.3+(48): void. */
361
    NXAST_RAW_DEC_NSH_TTL,
362
363
    /* NX1.0+(49): struct nx_action_check_pkt_larger, ... VLMFF */
364
    NXAST_RAW_CHECK_PKT_LARGER,
365
366
    /* NX1.0+(50): struct nx_action_delete_field. VLMFF */
367
    NXAST_RAW_DELETE_FIELD,
368
369
/* ## ------------------ ## */
370
/* ## Debugging actions. ## */
371
/* ## ------------------ ## */
372
373
/* These are intentionally undocumented, subject to change, and ovs-vswitchd */
374
/* accepts them only if started with --enable-dummy. */
375
376
    /* NX1.0+(254): void. */
377
    NXAST_RAW_DEBUG_SLOW,
378
379
    /* NX1.0+(255): void. */
380
    NXAST_RAW_DEBUG_RECIRC,
381
};
382
383
/* OpenFlow actions are always a multiple of 8 bytes in length. */
384
172k
#define OFP_ACTION_ALIGN 8
385
386
/* Define a few functions for working with instructions. */
387
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
388
    static inline const struct STRUCT * OVS_UNUSED              \
389
    instruction_get_##ENUM(const struct ofp11_instruction *inst)\
390
370
    {                                                           \
391
370
        ovs_assert(inst->type == htons(ENUM));                  \
392
370
        return ALIGNED_CAST(struct STRUCT *, inst);             \
393
370
    }                                                           \
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT13_METER
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT11_APPLY_ACTIONS
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT11_WRITE_ACTIONS
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT11_WRITE_METADATA
394
                                                                \
395
    static inline void OVS_UNUSED                               \
396
    instruction_init_##ENUM(struct STRUCT *s)                   \
397
682
    {                                                           \
398
682
        memset(s, 0, sizeof *s);                                \
399
682
        s->type = htons(ENUM);                                  \
400
682
        s->len = htons(sizeof *s);                              \
401
682
    }                                                           \
402
                                                                \
403
    static inline struct STRUCT * OVS_UNUSED                    \
404
    instruction_put_##ENUM(struct ofpbuf *buf)                  \
405
682
    {                                                           \
406
682
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
682
        instruction_init_##ENUM(s);                             \
408
682
        return s;                                               \
409
682
    }
ofp-actions.c:instruction_put_OFPIT13_METER
Line
Count
Source
405
104
    {                                                           \
406
104
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
104
        instruction_init_##ENUM(s);                             \
408
104
        return s;                                               \
409
104
    }
ofp-actions.c:instruction_put_OFPIT11_CLEAR_ACTIONS
Line
Count
Source
405
6
    {                                                           \
406
6
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
6
        instruction_init_##ENUM(s);                             \
408
6
        return s;                                               \
409
6
    }
ofp-actions.c:instruction_put_OFPIT11_WRITE_ACTIONS
Line
Count
Source
405
45
    {                                                           \
406
45
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
45
        instruction_init_##ENUM(s);                             \
408
45
        return s;                                               \
409
45
    }
ofp-actions.c:instruction_put_OFPIT11_WRITE_METADATA
Line
Count
Source
405
5
    {                                                           \
406
5
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
5
        instruction_init_##ENUM(s);                             \
408
5
        return s;                                               \
409
5
    }
ofp-actions.c:instruction_put_OFPIT11_GOTO_TABLE
Line
Count
Source
405
2
    {                                                           \
406
2
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
2
        instruction_init_##ENUM(s);                             \
408
2
        return s;                                               \
409
2
    }
ofp-actions.c:instruction_put_OFPIT11_APPLY_ACTIONS
Line
Count
Source
405
520
    {                                                           \
406
520
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
520
        instruction_init_##ENUM(s);                             \
408
520
        return s;                                               \
409
520
    }
410
1.73k
OVS_INSTRUCTIONS
ofp-actions.c:instruction_get_OFPIT11_CLEAR_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_get_OFPIT11_GOTO_TABLE
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT13_METER
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_CLEAR_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_WRITE_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_WRITE_METADATA
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_GOTO_TABLE
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_APPLY_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
411
1.73k
#undef DEFINE_INST
412
1.73k
413
1.73k
static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
414
1.73k
                                               size_t ofs);
415
1.73k
static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
416
1.73k
417
1.73k
static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
418
1.73k
                                  enum ofp_version, uint32_t allowed_ovsinsts,
419
1.73k
                                  enum ofpact_type outer_action,
420
1.73k
                                  char **errorp);
421
1.73k
422
1.73k
static void put_set_field(struct ofpbuf *openflow, enum ofp_version,
423
1.73k
                          enum mf_field_id, uint64_t value);
424
1.73k
425
1.73k
static void put_reg_load(struct ofpbuf *openflow,
426
1.73k
                         const struct mf_subfield *, uint64_t value);
427
1.73k
428
1.73k
static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
429
1.73k
                                   enum ofp_raw_action_type *, uint64_t *arg,
430
1.73k
                                   size_t *raw_len);
431
1.73k
static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
432
1.73k
                            enum ofp_raw_action_type, uint64_t arg);
433
1.73k
434
1.73k
static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
435
1.73k
    char *str, const struct ofpact_parse_params *pp,
436
1.73k
    bool allow_instructions, enum ofpact_type outer_action);
437
1.73k
static enum ofperr ofpacts_pull_openflow_actions__(
438
1.73k
    struct ofpbuf *openflow, unsigned int actions_len,
439
1.73k
    enum ofp_version version, uint32_t allowed_ovsinsts,
440
1.73k
    struct ofpbuf *ofpacts, enum ofpact_type outer_action,
441
1.73k
    const struct vl_mff_map *vl_mff_map, uint64_t *ofpacts_tlv_bitmap);
442
1.73k
static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
443
1.73k
    const char *s_, const struct ofpact_parse_params *pp,
444
1.73k
    bool allow_instructions, enum ofpact_type outer_action);
445
1.73k
446
1.73k
static void inconsistent_match(enum ofputil_protocol *usable_protocols);
447
1.73k
448
1.73k
/* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
449
1.73k
 * nested ofpacts it returns the first one. */
450
1.73k
struct ofpact *
451
1.73k
ofpact_next_flattened(const struct ofpact *ofpact)
452
1.73k
{
453
0
    switch (ofpact->type) {
454
0
    case OFPACT_OUTPUT:
455
0
    case OFPACT_GROUP:
456
0
    case OFPACT_CONTROLLER:
457
0
    case OFPACT_ENQUEUE:
458
0
    case OFPACT_OUTPUT_REG:
459
0
    case OFPACT_OUTPUT_TRUNC:
460
0
    case OFPACT_BUNDLE:
461
0
    case OFPACT_SET_FIELD:
462
0
    case OFPACT_SET_VLAN_VID:
463
0
    case OFPACT_SET_VLAN_PCP:
464
0
    case OFPACT_STRIP_VLAN:
465
0
    case OFPACT_PUSH_VLAN:
466
0
    case OFPACT_SET_ETH_SRC:
467
0
    case OFPACT_SET_ETH_DST:
468
0
    case OFPACT_SET_IPV4_SRC:
469
0
    case OFPACT_SET_IPV4_DST:
470
0
    case OFPACT_SET_IP_DSCP:
471
0
    case OFPACT_SET_IP_ECN:
472
0
    case OFPACT_SET_IP_TTL:
473
0
    case OFPACT_SET_L4_SRC_PORT:
474
0
    case OFPACT_SET_L4_DST_PORT:
475
0
    case OFPACT_REG_MOVE:
476
0
    case OFPACT_STACK_PUSH:
477
0
    case OFPACT_STACK_POP:
478
0
    case OFPACT_DEC_TTL:
479
0
    case OFPACT_SET_MPLS_LABEL:
480
0
    case OFPACT_SET_MPLS_TC:
481
0
    case OFPACT_SET_MPLS_TTL:
482
0
    case OFPACT_DEC_MPLS_TTL:
483
0
    case OFPACT_PUSH_MPLS:
484
0
    case OFPACT_POP_MPLS:
485
0
    case OFPACT_SET_TUNNEL:
486
0
    case OFPACT_SET_QUEUE:
487
0
    case OFPACT_POP_QUEUE:
488
0
    case OFPACT_FIN_TIMEOUT:
489
0
    case OFPACT_RESUBMIT:
490
0
    case OFPACT_LEARN:
491
0
    case OFPACT_CONJUNCTION:
492
0
    case OFPACT_MULTIPATH:
493
0
    case OFPACT_NOTE:
494
0
    case OFPACT_EXIT:
495
0
    case OFPACT_SAMPLE:
496
0
    case OFPACT_UNROLL_XLATE:
497
0
    case OFPACT_CT_CLEAR:
498
0
    case OFPACT_DEBUG_RECIRC:
499
0
    case OFPACT_DEBUG_SLOW:
500
0
    case OFPACT_METER:
501
0
    case OFPACT_CLEAR_ACTIONS:
502
0
    case OFPACT_WRITE_METADATA:
503
0
    case OFPACT_GOTO_TABLE:
504
0
    case OFPACT_NAT:
505
0
    case OFPACT_ENCAP:
506
0
    case OFPACT_DECAP:
507
0
    case OFPACT_DEC_NSH_TTL:
508
0
    case OFPACT_CHECK_PKT_LARGER:
509
0
    case OFPACT_DELETE_FIELD:
510
0
        return ofpact_next(ofpact);
511
512
0
    case OFPACT_CLONE:
513
0
        return ofpact_get_CLONE(ofpact)->actions;
514
515
0
    case OFPACT_CT:
516
0
        return ofpact_get_CT(ofpact)->actions;
517
518
0
    case OFPACT_WRITE_ACTIONS:
519
0
        return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
520
0
    }
521
522
0
    OVS_NOT_REACHED();
523
0
}
524
525
/* Pull off existing actions or instructions. Used by nesting actions to keep
526
 * ofpacts_parse() oblivious of actions nesting.
527
 *
528
 * Push the actions back on after nested parsing, e.g.:
529
 *
530
 *     size_t ofs = ofpacts_pull(ofpacts);
531
 *     ...nested parsing...
532
 *     ofpbuf_push_uninit(ofpacts, ofs);
533
 */
534
static size_t
535
ofpacts_pull(struct ofpbuf *ofpacts)
536
116k
{
537
116k
    size_t ofs;
538
539
116k
    ofs = ofpacts->size;
540
116k
    ofpbuf_pull(ofpacts, ofs);
541
542
116k
    return ofs;
543
116k
}
544
545
#include "ofp-actions.inc1"
546

547
/* Output actions. */
548
549
/* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
550
 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
551
 * number of bytes to send.  A 'max_len' of zero means no bytes of the
552
 * packet should be sent. */
553
struct ofp10_action_output {
554
    ovs_be16 type;                  /* OFPAT10_OUTPUT. */
555
    ovs_be16 len;                   /* Length is 8. */
556
    ovs_be16 port;                  /* Output port. */
557
    ovs_be16 max_len;               /* Max length to send to controller. */
558
};
559
OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
560
561
/* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
562
   * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
563
   * number of bytes to send. A 'max_len' of zero means no bytes of the
564
   * packet should be sent.*/
565
struct ofp11_action_output {
566
    ovs_be16 type;                    /* OFPAT11_OUTPUT. */
567
    ovs_be16 len;                     /* Length is 16. */
568
    ovs_be32 port;                    /* Output port. */
569
    ovs_be16 max_len;                 /* Max length to send to controller. */
570
    uint8_t pad[6];                   /* Pad to 64 bits. */
571
};
572
OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
573
574
static enum ofperr
575
decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
576
                          enum ofp_version ofp_version OVS_UNUSED,
577
                          struct ofpbuf *out)
578
9.50k
{
579
9.50k
    struct ofpact_output *output;
580
581
9.50k
    output = ofpact_put_OUTPUT(out);
582
9.50k
    output->port = u16_to_ofp(ntohs(oao->port));
583
9.50k
    output->max_len = ntohs(oao->max_len);
584
585
9.50k
    return ofpact_check_output_port(output->port, OFPP_MAX);
586
9.50k
}
587
588
static enum ofperr
589
decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
590
                          enum ofp_version ofp_version OVS_UNUSED,
591
                          struct ofpbuf *out)
592
1.32k
{
593
1.32k
    struct ofpact_output *output;
594
1.32k
    enum ofperr error;
595
596
1.32k
    output = ofpact_put_OUTPUT(out);
597
1.32k
    output->max_len = ntohs(oao->max_len);
598
599
1.32k
    error = ofputil_port_from_ofp11(oao->port, &output->port);
600
1.32k
    if (error) {
601
256
        return error;
602
256
    }
603
604
1.07k
    return ofpact_check_output_port(output->port, OFPP_MAX);
605
1.32k
}
606
607
static void
608
encode_OUTPUT(const struct ofpact_output *output,
609
              enum ofp_version ofp_version, struct ofpbuf *out)
610
88.2k
{
611
88.2k
    if (ofp_version == OFP10_VERSION) {
612
61.1k
        struct ofp10_action_output *oao;
613
614
61.1k
        oao = put_OFPAT10_OUTPUT(out);
615
61.1k
        oao->port = htons(ofp_to_u16(output->port));
616
61.1k
        oao->max_len = htons(output->max_len);
617
61.1k
    } else {
618
27.1k
        struct ofp11_action_output *oao;
619
620
27.1k
        oao = put_OFPAT11_OUTPUT(out);
621
27.1k
        oao->port = ofputil_port_to_ofp11(output->port);
622
27.1k
        oao->max_len = htons(output->max_len);
623
27.1k
    }
624
88.2k
}
625
626
static char * OVS_WARN_UNUSED_RESULT
627
parse_truncate_subfield(const char *arg_,
628
                        const struct ofpact_parse_params *pp,
629
                        struct ofpact_output_trunc *output_trunc)
630
999
{
631
999
    char *key, *value;
632
999
    char *arg = CONST_CAST(char *, arg_);
633
634
3.77k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
635
2.89k
        if (!strcmp(key, "port")) {
636
1.59k
            if (!ofputil_port_from_string(value, pp->port_map,
637
1.59k
                                          &output_trunc->port)) {
638
6
                return xasprintf("output to unknown truncate port: %s",
639
6
                                  value);
640
6
            }
641
1.58k
            if (ofp_to_u16(output_trunc->port) > ofp_to_u16(OFPP_MAX)) {
642
680
                if (output_trunc->port != OFPP_LOCAL &&
643
600
                    output_trunc->port != OFPP_IN_PORT)
644
7
                return xasprintf("output to unsupported truncate port: %s",
645
7
                                 value);
646
680
            }
647
1.58k
        } else if (!strcmp(key, "max_len")) {
648
1.21k
            char *err;
649
650
1.21k
            err = str_to_u32(value, &output_trunc->max_len);
651
1.21k
            if (err) {
652
14
                return err;
653
14
            }
654
655
1.19k
            if (output_trunc->max_len < ETH_HEADER_LEN) {
656
4
                return xasprintf("max_len %"PRIu32" is less than the minimum "
657
4
                                 "value %d",
658
4
                                 output_trunc->max_len, ETH_HEADER_LEN);
659
4
            }
660
1.19k
        } else {
661
89
            return xasprintf("invalid key '%s' in output_trunc argument",
662
89
                                key);
663
89
        }
664
2.89k
    }
665
879
    return NULL;
666
999
}
667
668
static char * OVS_WARN_UNUSED_RESULT
669
parse_OUTPUT(const char *arg, const struct ofpact_parse_params *pp)
670
2.41k
{
671
2.41k
    if (strstr(arg, "port") && strstr(arg, "max_len")) {
672
999
        struct ofpact_output_trunc *output_trunc;
673
674
999
        output_trunc = ofpact_put_OUTPUT_TRUNC(pp->ofpacts);
675
999
        return parse_truncate_subfield(arg, pp, output_trunc);
676
999
    }
677
678
1.41k
    ofp_port_t port;
679
1.41k
    if (ofputil_port_from_string(arg, pp->port_map, &port)) {
680
640
        struct ofpact_output *output = ofpact_put_OUTPUT(pp->ofpacts);
681
640
        output->port = port;
682
640
        output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
683
640
        return NULL;
684
640
    }
685
686
777
    struct mf_subfield src;
687
777
    char *error = mf_parse_subfield(&src, arg);
688
777
    if (!error) {
689
763
        struct ofpact_output_reg *output_reg;
690
691
763
        output_reg = ofpact_put_OUTPUT_REG(pp->ofpacts);
692
763
        output_reg->max_len = UINT16_MAX;
693
763
        output_reg->src = src;
694
763
        return NULL;
695
763
    }
696
14
    free(error);
697
698
14
    return xasprintf("%s: output to unknown port", arg);
699
777
}
700
701
static void
702
format_OUTPUT(const struct ofpact_output *a,
703
              const struct ofpact_format_params *fp)
704
7.77k
{
705
7.77k
    if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
706
4.08k
        ds_put_format(fp->s, "%soutput:%s", colors.special, colors.end);
707
4.08k
    }
708
7.77k
    ofputil_format_port(a->port, fp->port_map, fp->s);
709
7.77k
    if (a->port == OFPP_CONTROLLER) {
710
976
        ds_put_format(fp->s, ":%"PRIu16, a->max_len);
711
976
    }
712
7.77k
}
713
714
static enum ofperr
715
check_OUTPUT(const struct ofpact_output *a,
716
             const struct ofpact_check_params *cp)
717
98.1k
{
718
98.1k
    return ofpact_check_output_port(a->port, cp->max_ports);
719
98.1k
}
720

721
/* Group actions. */
722
723
static enum ofperr
724
decode_OFPAT_RAW_GROUP(uint32_t group_id,
725
                         enum ofp_version ofp_version OVS_UNUSED,
726
                         struct ofpbuf *out)
727
748
{
728
748
    ofpact_put_GROUP(out)->group_id = group_id;
729
748
    return 0;
730
748
}
731
732
static void
733
encode_GROUP(const struct ofpact_group *group,
734
             enum ofp_version ofp_version, struct ofpbuf *out)
735
205
{
736
205
    put_OFPAT_GROUP(out, ofp_version, group->group_id);
737
205
}
738
739
static char * OVS_WARN_UNUSED_RESULT
740
parse_GROUP(char *arg, const struct ofpact_parse_params *pp)
741
220
{
742
220
    return str_to_u32(arg, &ofpact_put_GROUP(pp->ofpacts)->group_id);
743
220
}
744
745
static void
746
format_GROUP(const struct ofpact_group *a,
747
             const struct ofpact_format_params *fp)
748
380
{
749
380
    ds_put_format(fp->s, "%sgroup:%s%"PRIu32,
750
380
                  colors.special, colors.end, a->group_id);
751
380
}
752
753
static enum ofperr
754
check_GROUP(const struct ofpact_group *a OVS_UNUSED,
755
            const struct ofpact_check_params *cp OVS_UNUSED)
756
876
{
757
876
    return 0;
758
876
}
759

760
/* Action structure for NXAST_CONTROLLER.
761
 *
762
 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER.  In
763
 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
764
 * specifying:
765
 *
766
 *    - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
767
 *
768
 *    - 'controller_id': The ID of the controller connection to which the
769
 *      ofp_packet_in should be sent.  The ofp_packet_in or nx_packet_in is
770
 *      sent only to controllers that have the specified controller connection
771
 *      ID.  See "struct nx_controller_id" for more information. */
772
struct nx_action_controller {
773
    ovs_be16 type;                  /* OFPAT_VENDOR. */
774
    ovs_be16 len;                   /* Length is 16. */
775
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
776
    ovs_be16 subtype;               /* NXAST_CONTROLLER. */
777
    ovs_be16 max_len;               /* Maximum length to send to controller. */
778
    ovs_be16 controller_id;         /* Controller ID to send packet-in. */
779
    uint8_t reason;                 /* enum ofp_packet_in_reason (OFPR_*). */
780
    uint8_t zero;                   /* Must be zero. */
781
};
782
OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
783
784
/* Properties for NXAST_CONTROLLER2.
785
 *
786
 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
787
 * on NXT_PACKET_IN2 in nicira-ext.h */
788
enum nx_action_controller2_prop_type {
789
    NXAC2PT_MAX_LEN,            /* ovs_be16 max bytes to send (default all). */
790
    NXAC2PT_CONTROLLER_ID,      /* ovs_be16 dest controller ID (default 0). */
791
    NXAC2PT_REASON,             /* uint8_t reason (OFPR_*), default 0. */
792
    NXAC2PT_USERDATA,           /* Data to copy into NXPINT_USERDATA. */
793
    NXAC2PT_PAUSE,              /* Flag to pause pipeline to resume later. */
794
    NXAC2PT_METER_ID,           /* ovs_b32 meter (default NX_CTLR_NO_METER). */
795
};
796
797
/* The action structure for NXAST_CONTROLLER2 is "struct ext_action_header",
798
 * followed by NXAC2PT_* properties. */
799
800
static enum ofperr
801
decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
802
                            enum ofp_version ofp_version OVS_UNUSED,
803
                            struct ofpbuf *out)
804
3.07k
{
805
3.07k
    struct ofpact_controller *oc;
806
807
3.07k
    oc = ofpact_put_CONTROLLER(out);
808
3.07k
    oc->ofpact.raw = NXAST_RAW_CONTROLLER;
809
3.07k
    oc->max_len = ntohs(nac->max_len);
810
3.07k
    oc->controller_id = ntohs(nac->controller_id);
811
3.07k
    oc->reason = nac->reason;
812
3.07k
    oc->meter_id = NX_CTLR_NO_METER;
813
3.07k
    ofpact_finish_CONTROLLER(out, &oc);
814
815
3.07k
    return 0;
816
3.07k
}
817
818
static enum ofperr
819
decode_NXAST_RAW_CONTROLLER2(const struct ext_action_header *eah,
820
                             enum ofp_version ofp_version OVS_UNUSED,
821
                             struct ofpbuf *out)
822
1.87k
{
823
1.87k
    if (!is_all_zeros(eah->pad, sizeof eah->pad)) {
824
197
        return OFPERR_NXBRC_MUST_BE_ZERO;
825
197
    }
826
827
1.67k
    size_t start_ofs = out->size;
828
1.67k
    struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
829
1.67k
    oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
830
1.67k
    oc->max_len = UINT16_MAX;
831
1.67k
    oc->reason = OFPR_ACTION;
832
1.67k
    oc->meter_id = NX_CTLR_NO_METER;
833
834
1.67k
    struct ofpbuf properties;
835
1.67k
    ofpbuf_use_const(&properties, eah, ntohs(eah->len));
836
1.67k
    ofpbuf_pull(&properties, sizeof *eah);
837
838
3.28k
    while (properties.size > 0) {
839
2.19k
        struct ofpbuf payload;
840
2.19k
        uint64_t type;
841
842
2.19k
        enum ofperr error = ofpprop_pull(&properties, &payload, &type);
843
2.19k
        if (error) {
844
74
            return error;
845
74
        }
846
847
2.11k
        switch (type) {
848
467
        case NXAC2PT_MAX_LEN:
849
467
            error = ofpprop_parse_u16(&payload, &oc->max_len);
850
467
            break;
851
852
119
        case NXAC2PT_CONTROLLER_ID:
853
119
            error = ofpprop_parse_u16(&payload, &oc->controller_id);
854
119
            break;
855
856
698
        case NXAC2PT_REASON: {
857
698
            uint8_t u8;
858
698
            error = ofpprop_parse_u8(&payload, &u8);
859
698
            if (!error) {
860
276
                oc->reason = u8;
861
276
            }
862
698
            break;
863
0
        }
864
865
81
        case NXAC2PT_USERDATA:
866
81
            out->size = start_ofs + sizeof(struct ofpact_controller);
867
81
            ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
868
81
            oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
869
81
            oc->userdata_len = ofpbuf_msgsize(&payload);
870
81
            break;
871
872
498
        case NXAC2PT_PAUSE:
873
498
            oc->pause = true;
874
498
            break;
875
876
231
        case NXAC2PT_METER_ID:
877
231
            error = ofpprop_parse_u32(&payload, &oc->meter_id);
878
231
            break;
879
880
24
        default:
881
24
            error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
882
24
            break;
883
2.11k
        }
884
2.11k
        if (error) {
885
510
            return error;
886
510
        }
887
2.11k
    }
888
889
1.08k
    ofpact_finish_CONTROLLER(out, &oc);
890
891
1.08k
    return 0;
892
1.67k
}
893
894
static void
895
encode_CONTROLLER(const struct ofpact_controller *controller,
896
                  enum ofp_version ofp_version OVS_UNUSED,
897
                  struct ofpbuf *out)
898
6.55k
{
899
6.55k
    if (controller->userdata_len
900
5.07k
        || controller->pause
901
1.28k
        || controller->meter_id != NX_CTLR_NO_METER
902
5.89k
        || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
903
5.89k
        size_t start_ofs = out->size;
904
5.89k
        put_NXAST_CONTROLLER2(out);
905
5.89k
        if (controller->max_len != UINT16_MAX) {
906
216
            ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
907
216
        }
908
5.89k
        if (controller->controller_id != 0) {
909
248
            ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
910
248
                            controller->controller_id);
911
248
        }
912
5.89k
        if (controller->reason != OFPR_ACTION) {
913
317
            ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
914
317
        }
915
5.89k
        if (controller->userdata_len != 0) {
916
1.47k
            ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
917
1.47k
                        controller->userdata_len);
918
1.47k
        }
919
5.89k
        if (controller->pause) {
920
3.79k
            ofpprop_put_flag(out, NXAC2PT_PAUSE);
921
3.79k
        }
922
5.89k
        if (controller->meter_id != NX_CTLR_NO_METER) {
923
619
            ofpprop_put_u32(out, NXAC2PT_METER_ID, controller->meter_id);
924
619
        }
925
5.89k
        pad_ofpat(out, start_ofs);
926
5.89k
    } else {
927
667
        struct nx_action_controller *nac;
928
929
667
        nac = put_NXAST_CONTROLLER(out);
930
667
        nac->max_len = htons(controller->max_len);
931
667
        nac->controller_id = htons(controller->controller_id);
932
667
        nac->reason = controller->reason;
933
667
    }
934
6.55k
}
935
936
static char * OVS_WARN_UNUSED_RESULT
937
parse_CONTROLLER(char *arg, const struct ofpact_parse_params *pp)
938
10.9k
{
939
10.9k
    enum ofp_packet_in_reason reason = OFPR_ACTION;
940
10.9k
    uint16_t controller_id = 0;
941
10.9k
    uint16_t max_len = UINT16_MAX;
942
10.9k
    uint32_t meter_id = NX_CTLR_NO_METER;
943
10.9k
    const char *userdata = NULL;
944
10.9k
    bool pause = false;
945
946
10.9k
    if (!arg[0]) {
947
        /* Use defaults. */
948
10.6k
    } else if (strspn(arg, "0123456789") == strlen(arg)) {
949
640
        char *error = str_to_u16(arg, "max_len", &max_len);
950
640
        if (error) {
951
2
            return error;
952
2
        }
953
10.0k
    } else {
954
10.0k
        char *name, *value;
955
956
23.0k
        while (ofputil_parse_key_value(&arg, &name, &value)) {
957
13.1k
            if (!strcmp(name, "reason")) {
958
1.04k
                if (!ofputil_packet_in_reason_from_string(value, &reason)) {
959
80
                    return xasprintf("unknown reason \"%s\"", value);
960
80
                }
961
12.1k
            } else if (!strcmp(name, "max_len")) {
962
485
                char *error = str_to_u16(value, "max_len", &max_len);
963
485
                if (error) {
964
1
                    return error;
965
1
                }
966
11.6k
            } else if (!strcmp(name, "id")) {
967
1.82k
                char *error = str_to_u16(value, "id", &controller_id);
968
1.82k
                if (error) {
969
4
                    return error;
970
4
                }
971
9.83k
            } else if (!strcmp(name, "userdata")) {
972
2.46k
                userdata = value;
973
7.37k
            } else if (!strcmp(name, "pause")) {
974
6.51k
                pause = true;
975
6.51k
            } else if (!strcmp(name, "meter_id")) {
976
823
                char *error = str_to_u32(value, &meter_id);
977
823
                if (error) {
978
1
                    return error;
979
1
                }
980
823
            } else {
981
34
                return xasprintf("unknown key \"%s\" parsing controller "
982
34
                                 "action", name);
983
34
            }
984
13.1k
        }
985
10.0k
    }
986
987
10.7k
    if (reason == OFPR_ACTION && controller_id == 0 && !userdata && !pause
988
1.91k
        && meter_id == NX_CTLR_NO_METER) {
989
1.24k
        struct ofpact_output *output;
990
991
1.24k
        output = ofpact_put_OUTPUT(pp->ofpacts);
992
1.24k
        output->port = OFPP_CONTROLLER;
993
1.24k
        output->max_len = max_len;
994
9.55k
    } else {
995
9.55k
        struct ofpact_controller *controller;
996
997
9.55k
        controller = ofpact_put_CONTROLLER(pp->ofpacts);
998
9.55k
        controller->max_len = max_len;
999
9.55k
        controller->reason = reason;
1000
9.55k
        controller->controller_id = controller_id;
1001
9.55k
        controller->pause = pause;
1002
9.55k
        controller->meter_id = meter_id;
1003
1004
9.55k
        if (userdata) {
1005
1.81k
            size_t start_ofs = pp->ofpacts->size;
1006
1.81k
            const char *end = ofpbuf_put_hex(pp->ofpacts, userdata, NULL);
1007
1.81k
            if (*end) {
1008
4
                return xstrdup("bad hex digit in `controller' "
1009
4
                               "action `userdata'");
1010
4
            }
1011
1.80k
            size_t userdata_len = pp->ofpacts->size - start_ofs;
1012
1.80k
            controller = pp->ofpacts->header;
1013
1.80k
            controller->userdata_len = userdata_len;
1014
1.80k
        }
1015
1016
9.54k
        if (ofpbuf_oversized(pp->ofpacts)) {
1017
1
            return xasprintf("input too big");
1018
1
        }
1019
1020
9.54k
        ofpact_finish_CONTROLLER(pp->ofpacts, &controller);
1021
9.54k
    }
1022
1023
10.7k
    return NULL;
1024
10.7k
}
1025
1026
static void
1027
format_CONTROLLER(const struct ofpact_controller *a,
1028
                  const struct ofpact_format_params *fp)
1029
3.81k
{
1030
3.81k
    if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len
1031
893
        && !a->pause && a->meter_id == NX_CTLR_NO_METER) {
1032
307
        ds_put_format(fp->s, "%sCONTROLLER:%s%"PRIu16,
1033
307
                      colors.special, colors.end, a->max_len);
1034
3.50k
    } else {
1035
3.50k
        enum ofp_packet_in_reason reason = a->reason;
1036
1037
3.50k
        ds_put_format(fp->s, "%scontroller(%s", colors.paren, colors.end);
1038
3.50k
        if (reason != OFPR_ACTION) {
1039
2.57k
            char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1040
1041
2.57k
            ds_put_format(fp->s, "%sreason=%s%s,", colors.param, colors.end,
1042
2.57k
                          ofputil_packet_in_reason_to_string(
1043
2.57k
                              reason, reasonbuf, sizeof reasonbuf));
1044
2.57k
        }
1045
3.50k
        if (a->max_len != UINT16_MAX) {
1046
3.01k
            ds_put_format(fp->s, "%smax_len=%s%"PRIu16",",
1047
3.01k
                          colors.param, colors.end, a->max_len);
1048
3.01k
        }
1049
3.50k
        if (a->controller_id != 0) {
1050
2.22k
            ds_put_format(fp->s, "%sid=%s%"PRIu16",",
1051
2.22k
                          colors.param, colors.end, a->controller_id);
1052
2.22k
        }
1053
3.50k
        if (a->userdata_len) {
1054
80
            ds_put_format(fp->s, "%suserdata=%s", colors.param, colors.end);
1055
80
            ds_put_hex_with_delimiter(fp->s, a->userdata, a->userdata_len,
1056
80
                                      ".");
1057
80
            ds_put_char(fp->s, ',');
1058
80
        }
1059
3.50k
        if (a->pause) {
1060
356
            ds_put_format(fp->s, "%spause%s,", colors.value, colors.end);
1061
356
        }
1062
3.50k
        if (a->meter_id != NX_CTLR_NO_METER) {
1063
230
            ds_put_format(fp->s, "%smeter_id=%s%"PRIu32",",
1064
230
                          colors.param, colors.end, a->meter_id);
1065
230
        }
1066
3.50k
        ds_chomp(fp->s, ',');
1067
3.50k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
1068
3.50k
    }
1069
3.81k
}
1070
1071
static enum ofperr
1072
check_CONTROLLER(const struct ofpact_controller *a OVS_UNUSED,
1073
                 const struct ofpact_check_params *cp OVS_UNUSED)
1074
6.67k
{
1075
6.67k
    return 0;
1076
6.67k
}
1077

1078
/* Enqueue action. */
1079
struct ofp10_action_enqueue {
1080
    ovs_be16 type;            /* OFPAT10_ENQUEUE. */
1081
    ovs_be16 len;             /* Len is 16. */
1082
    ovs_be16 port;            /* Port that queue belongs. Should
1083
                                 refer to a valid physical port
1084
                                 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
1085
    uint8_t pad[6];           /* Pad for 64-bit alignment. */
1086
    ovs_be32 queue_id;        /* Where to enqueue the packets. */
1087
};
1088
OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
1089
1090
static enum ofperr
1091
decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
1092
                           enum ofp_version ofp_version OVS_UNUSED,
1093
                           struct ofpbuf *out)
1094
380
{
1095
380
    struct ofpact_enqueue *enqueue;
1096
1097
380
    enqueue = ofpact_put_ENQUEUE(out);
1098
380
    enqueue->port = u16_to_ofp(ntohs(oae->port));
1099
380
    enqueue->queue = ntohl(oae->queue_id);
1100
380
    if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
1101
380
        && enqueue->port != OFPP_IN_PORT
1102
229
        && enqueue->port != OFPP_LOCAL) {
1103
14
        return OFPERR_OFPBAC_BAD_OUT_PORT;
1104
14
    }
1105
366
    return 0;
1106
380
}
1107
1108
static void
1109
encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
1110
               enum ofp_version ofp_version, struct ofpbuf *out)
1111
813
{
1112
813
    if (ofp_version == OFP10_VERSION) {
1113
391
        struct ofp10_action_enqueue *oae;
1114
1115
391
        oae = put_OFPAT10_ENQUEUE(out);
1116
391
        oae->port = htons(ofp_to_u16(enqueue->port));
1117
391
        oae->queue_id = htonl(enqueue->queue);
1118
422
    } else {
1119
422
        put_OFPAT_SET_QUEUE(out, ofp_version, enqueue->queue);
1120
1121
422
        struct ofp11_action_output *oao = put_OFPAT11_OUTPUT(out);
1122
422
        oao->port = ofputil_port_to_ofp11(enqueue->port);
1123
422
        oao->max_len = OVS_BE16_MAX;
1124
1125
422
        put_NXAST_POP_QUEUE(out);
1126
422
    }
1127
813
}
1128
1129
static char * OVS_WARN_UNUSED_RESULT
1130
parse_ENQUEUE(char *arg, const struct ofpact_parse_params *pp)
1131
844
{
1132
844
    char *sp = NULL;
1133
844
    char *port = strtok_r(arg, ":q,", &sp);
1134
844
    char *queue = strtok_r(NULL, "", &sp);
1135
844
    struct ofpact_enqueue *enqueue;
1136
1137
844
    if (port == NULL || queue == NULL) {
1138
3
        return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1139
3
                       "\"enqueue(PORT,QUEUE)\"");
1140
3
    }
1141
1142
841
    enqueue = ofpact_put_ENQUEUE(pp->ofpacts);
1143
841
    if (!ofputil_port_from_string(port, pp->port_map, &enqueue->port)) {
1144
1
        return xasprintf("%s: enqueue to unknown port", port);
1145
1
    }
1146
840
    return str_to_u32(queue, &enqueue->queue);
1147
841
}
1148
1149
static void
1150
format_ENQUEUE(const struct ofpact_enqueue *a,
1151
               const struct ofpact_format_params *fp)
1152
366
{
1153
366
    ds_put_format(fp->s, "%senqueue:%s", colors.param, colors.end);
1154
366
    ofputil_format_port(a->port, fp->port_map, fp->s);
1155
366
    ds_put_format(fp->s, ":%"PRIu32, a->queue);
1156
366
}
1157
1158
static enum ofperr
1159
check_ENQUEUE(const struct ofpact_enqueue *a,
1160
              const struct ofpact_check_params *cp)
1161
933
{
1162
933
    if (ofp_to_u16(a->port) >= ofp_to_u16(cp->max_ports)
1163
933
        && a->port != OFPP_IN_PORT
1164
244
        && a->port != OFPP_LOCAL) {
1165
8
        return OFPERR_OFPBAC_BAD_OUT_PORT;
1166
8
    }
1167
925
    return 0;
1168
933
}
1169

1170
/* Action structure for NXAST_OUTPUT_REG.
1171
 *
1172
 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1173
 *
1174
 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1175
 * the NXAST_REG_LOAD action.
1176
 *
1177
 * The acceptable nxm_header values for 'src' are the same as the acceptable
1178
 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1179
 *
1180
 * The 'max_len' field indicates the number of bytes to send when the chosen
1181
 * port is OFPP_CONTROLLER.  Its semantics are equivalent to the 'max_len'
1182
 * field of OFPAT_OUTPUT.
1183
 *
1184
 * The 'zero' field is required to be zeroed for forward compatibility. */
1185
struct nx_action_output_reg {
1186
    ovs_be16 type;              /* OFPAT_VENDOR. */
1187
    ovs_be16 len;               /* 24. */
1188
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
1189
    ovs_be16 subtype;           /* NXAST_OUTPUT_REG. */
1190
1191
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1192
    ovs_be32 src;               /* Source. */
1193
1194
    ovs_be16 max_len;           /* Max length to send to controller. */
1195
1196
    uint8_t zero[6];            /* Reserved, must be zero. */
1197
};
1198
OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
1199
1200
/* Action structure for NXAST_OUTPUT_REG2.
1201
 *
1202
 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1203
 * experimenter OXM as 'src'.
1204
 */
1205
struct nx_action_output_reg2 {
1206
    ovs_be16 type;              /* OFPAT_VENDOR. */
1207
    ovs_be16 len;               /* 24. */
1208
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
1209
    ovs_be16 subtype;           /* NXAST_OUTPUT_REG2. */
1210
1211
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1212
    ovs_be16 max_len;           /* Max length to send to controller. */
1213
1214
    /* Followed by:
1215
     * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1216
     * - Enough 0-bytes to pad the action out to 24 bytes. */
1217
    uint8_t pad[10];
1218
};
1219
OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1220
1221
static enum ofperr
1222
decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
1223
                            enum ofp_version ofp_version OVS_UNUSED,
1224
                            const struct vl_mff_map *vl_mff_map,
1225
                            uint64_t *tlv_bitmap, struct ofpbuf *out)
1226
749
{
1227
749
    struct ofpact_output_reg *output_reg;
1228
749
    enum ofperr error;
1229
1230
749
    if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
1231
298
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1232
298
    }
1233
1234
451
    output_reg = ofpact_put_OUTPUT_REG(out);
1235
451
    output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
1236
451
    output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1237
451
    output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1238
451
    output_reg->max_len = ntohs(naor->max_len);
1239
451
    error = mf_vl_mff_mf_from_nxm_header(ntohl(naor->src), vl_mff_map,
1240
451
                                         &output_reg->src.field, tlv_bitmap);
1241
451
    if (error) {
1242
80
        return error;
1243
80
    }
1244
1245
371
    return mf_check_src(&output_reg->src, NULL);
1246
451
}
1247
1248
static enum ofperr
1249
decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
1250
                             enum ofp_version ofp_version OVS_UNUSED,
1251
                             const struct vl_mff_map *vl_mff_map,
1252
                             uint64_t *tlv_bitmap, struct ofpbuf *out)
1253
240
{
1254
240
    struct ofpact_output_reg *output_reg;
1255
240
    enum ofperr error;
1256
1257
240
    output_reg = ofpact_put_OUTPUT_REG(out);
1258
240
    output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1259
240
    output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1260
240
    output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1261
240
    output_reg->max_len = ntohs(naor->max_len);
1262
1263
240
    struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
1264
240
    ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
1265
1266
240
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &output_reg->src.field,
1267
240
                                     NULL, tlv_bitmap);
1268
240
    if (error) {
1269
80
        return error;
1270
80
    }
1271
1272
160
    if (!is_all_zeros(b.data, b.size)) {
1273
18
        return OFPERR_NXBRC_MUST_BE_ZERO;
1274
18
    }
1275
1276
142
    return mf_check_src(&output_reg->src, NULL);
1277
160
}
1278
1279
static void
1280
encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1281
                  enum ofp_version ofp_version OVS_UNUSED,
1282
                  struct ofpbuf *out)
1283
567
{
1284
    /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1285
     * cannot be encoded in the older form, encode it as
1286
     * NXAST_RAW_OUTPUT_REG2. */
1287
567
    if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1288
567
        || !mf_nxm_header(output_reg->src.field->id)) {
1289
343
        struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
1290
343
        size_t size = out->size;
1291
1292
343
        naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1293
343
                                               output_reg->src.n_bits);
1294
343
        naor->max_len = htons(output_reg->max_len);
1295
1296
343
        out->size = size - sizeof naor->pad;
1297
343
        nx_put_mff_header(out, output_reg->src.field, 0, false);
1298
343
        out->size = size;
1299
343
    } else {
1300
224
        struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
1301
1302
224
        naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1303
224
                                               output_reg->src.n_bits);
1304
224
        naor->src = htonl(nxm_header_from_mff(output_reg->src.field));
1305
224
        naor->max_len = htons(output_reg->max_len);
1306
224
    }
1307
567
}
1308
1309
static char * OVS_WARN_UNUSED_RESULT
1310
parse_OUTPUT_REG(const char *arg, const struct ofpact_parse_params *pp)
1311
378
{
1312
378
    return parse_OUTPUT(arg, pp);
1313
378
}
1314
1315
static void
1316
format_OUTPUT_REG(const struct ofpact_output_reg *a,
1317
                  const struct ofpact_format_params *fp)
1318
387
{
1319
387
    ds_put_format(fp->s, "%soutput:%s", colors.special, colors.end);
1320
387
    mf_format_subfield(&a->src, fp->s);
1321
387
}
1322
1323
static enum ofperr
1324
check_OUTPUT_REG(const struct ofpact_output_reg *a,
1325
                 const struct ofpact_check_params *cp)
1326
710
{
1327
710
    return mf_check_src(&a->src, cp->match);
1328
710
}
1329

1330
/* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1331
 *
1332
 * The bundle actions choose a member from a supplied list of options.
1333
 * NXAST_BUNDLE outputs to its selection.  NXAST_BUNDLE_LOAD writes its
1334
 * selection to a register.
1335
 *
1336
 * The list of possible members follows the nx_action_bundle structure. The
1337
 * size of each member is governed by its type as indicated by the
1338
 * 'member_type' parameter. The list of members should be padded at its end
1339
 * with zeros to make the total length of the action a multiple of 8.
1340
 *
1341
 * Switches infer from the 'member_type' parameter the size of each member.
1342
 * All implementations must support the NXM_OF_IN_PORT 'member_type' which
1343
 * indicates that the members are OpenFlow port numbers with
1344
 * NXM_LENGTH(NXM_OF_IN_PORT) == 2 byte width.  Switches should reject actions
1345
 * which indicate unknown or unsupported member types.
1346
 *
1347
 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1348
 * member.  If the switch does not support the specified 'algorithm' parameter,
1349
 * it should reject the action.
1350
 *
1351
 * Several algorithms take into account liveness when selecting members.  The
1352
 * liveness of a member is implementation defined (with one exception), but
1353
 * will generally take into account things like its carrier status and the
1354
 * results of any link monitoring protocols which happen to be running on it.
1355
 * In order to give controllers a place-holder value, the OFPP_NONE port is
1356
 * always considered live, that is, NXAST_BUNDLE_LOAD stores OFPP_NONE in the
1357
 * output register if no member is live.
1358
 *
1359
 * Some member selection strategies require the use of a hash function, in
1360
 * which case the 'fields' and 'basis' parameters should be populated.  The
1361
 * 'fields' parameter (one of NX_HASH_FIELDS_*) designates which parts of the
1362
 * flow to hash.  Refer to the definition of "enum nx_hash_fields" for details.
1363
 * The 'basis' parameter is used as a universal hash parameter.  Different
1364
 * values of 'basis' yield different hash results.
1365
 *
1366
 * The 'zero' parameter at the end of the action structure is reserved for
1367
 * future use.  Switches are required to reject actions which have nonzero
1368
 * bytes in the 'zero' field.
1369
 *
1370
 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed.  Switches
1371
 * should reject actions which have nonzero bytes in either of these fields.
1372
 *
1373
 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected member in
1374
 * dst[ofs:ofs+n_bits].  The format and semantics of 'dst' and 'ofs_nbits' are
1375
 * similar to those for the NXAST_REG_LOAD action. */
1376
struct nx_action_bundle {
1377
    ovs_be16 type;              /* OFPAT_VENDOR. */
1378
    ovs_be16 len;               /* Length including members. */
1379
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
1380
    ovs_be16 subtype;           /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1381
1382
    /* Member choice algorithm to apply to hash value. */
1383
    ovs_be16 algorithm;         /* One of NX_BD_ALG_*. */
1384
1385
    /* What fields to hash and how. */
1386
    ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
1387
    ovs_be16 basis;             /* Universal hash parameter. */
1388
1389
    ovs_be32 member_type;       /* NXM_OF_IN_PORT. */
1390
    ovs_be16 n_members;         /* Number of members. */
1391
1392
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1393
    ovs_be32 dst;               /* Destination. */
1394
1395
    uint8_t zero[4];            /* Reserved. Must be zero. */
1396
};
1397
OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1398
1399
static enum ofperr
1400
decode_bundle(bool load, const struct nx_action_bundle *nab,
1401
              const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
1402
              struct ofpbuf *ofpacts)
1403
4.77k
{
1404
4.77k
    static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 5);
1405
4.77k
    struct ofpact_bundle *bundle;
1406
4.77k
    uint32_t member_type;
1407
4.77k
    size_t members_size, i;
1408
4.77k
    enum ofperr error;
1409
1410
4.77k
    bundle = ofpact_put_BUNDLE(ofpacts);
1411
1412
4.77k
    bundle->n_members = ntohs(nab->n_members);
1413
4.77k
    bundle->basis = ntohs(nab->basis);
1414
4.77k
    bundle->fields = ntohs(nab->fields);
1415
4.77k
    bundle->algorithm = ntohs(nab->algorithm);
1416
4.77k
    member_type = ntohl(nab->member_type);
1417
4.77k
    members_size = ntohs(nab->len) - sizeof *nab;
1418
1419
4.77k
    error = OFPERR_OFPBAC_BAD_ARGUMENT;
1420
4.77k
    if (!flow_hash_fields_valid(bundle->fields)) {
1421
1.81k
        VLOG_WARN_RL(&rll, "unsupported fields %d", (int) bundle->fields);
1422
2.96k
    } else if (bundle->n_members > BUNDLE_MAX_MEMBERS) {
1423
761
        VLOG_WARN_RL(&rll, "too many members");
1424
2.20k
    } else if (bundle->algorithm != NX_BD_ALG_HRW
1425
892
               && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1426
259
        VLOG_WARN_RL(&rll, "unsupported algorithm %d", (int) bundle->algorithm);
1427
1.94k
    } else if (member_type != mf_nxm_header(MFF_IN_PORT)) {
1428
1.38k
        VLOG_WARN_RL(&rll, "unsupported member type %"PRIu32, member_type);
1429
1.38k
    } else {
1430
552
        error = 0;
1431
552
    }
1432
1433
4.77k
    if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1434
3.72k
        VLOG_WARN_RL(&rll, "reserved field is nonzero");
1435
3.72k
        error = OFPERR_OFPBAC_BAD_ARGUMENT;
1436
3.72k
    }
1437
1438
4.77k
    if (load) {
1439
1.86k
        bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1440
1.86k
        bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1441
1.86k
        error = mf_vl_mff_mf_from_nxm_header(ntohl(nab->dst), vl_mff_map,
1442
1.86k
                                             &bundle->dst.field, tlv_bitmap);
1443
1.86k
        if (error) {
1444
175
            return error;
1445
175
        }
1446
1447
1.68k
        if (bundle->dst.n_bits < 16) {
1448
80
            VLOG_WARN_RL(&rll, "bundle_load action requires at least 16 bit "
1449
80
                         "destination.");
1450
80
            error = OFPERR_OFPBAC_BAD_ARGUMENT;
1451
80
        }
1452
2.91k
    } else {
1453
2.91k
        if (nab->ofs_nbits || nab->dst) {
1454
2.32k
            VLOG_WARN_RL(&rll, "bundle action has nonzero reserved fields");
1455
2.32k
            error = OFPERR_OFPBAC_BAD_ARGUMENT;
1456
2.32k
        }
1457
2.91k
    }
1458
1459
4.60k
    if (members_size < bundle->n_members * sizeof(ovs_be16)) {
1460
2.30k
        VLOG_WARN_RL(&rll, "Nicira action %s only has %"PRIuSIZE" bytes "
1461
2.30k
                     "allocated for members.  %"PRIuSIZE" bytes are "
1462
2.30k
                     "required for %u members.",
1463
2.30k
                     load ? "bundle_load" : "bundle", members_size,
1464
2.30k
                     bundle->n_members * sizeof(ovs_be16), bundle->n_members);
1465
2.30k
        error = OFPERR_OFPBAC_BAD_LEN;
1466
2.30k
    } else {
1467
13.6k
        for (i = 0; i < bundle->n_members; i++) {
1468
11.3k
            ofp_port_t ofp_port
1469
11.3k
                = u16_to_ofp(ntohs(((ovs_be16 *)(nab + 1))[i]));
1470
11.3k
            ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1471
11.3k
            bundle = ofpacts->header;
1472
11.3k
        }
1473
2.29k
    }
1474
1475
4.60k
    ofpact_finish_BUNDLE(ofpacts, &bundle);
1476
4.60k
    if (!error) {
1477
2.12k
        error = bundle_check(bundle, OFPP_MAX, NULL);
1478
2.12k
    }
1479
4.60k
    return error;
1480
4.77k
}
1481
1482
static enum ofperr
1483
decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1484
                        enum ofp_version ofp_version OVS_UNUSED,
1485
                        struct ofpbuf *out)
1486
2.91k
{
1487
2.91k
    return decode_bundle(false, nab, NULL, NULL, out);
1488
2.91k
}
1489
1490
static enum ofperr
1491
decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1492
                             enum ofp_version ofp_version OVS_UNUSED,
1493
                             const struct vl_mff_map *vl_mff_map,
1494
                             uint64_t *tlv_bitmap, struct ofpbuf *out)
1495
1.86k
{
1496
1.86k
    return decode_bundle(true, nab, vl_mff_map, tlv_bitmap, out);
1497
1.86k
}
1498
1499
static void
1500
encode_BUNDLE(const struct ofpact_bundle *bundle,
1501
              enum ofp_version ofp_version OVS_UNUSED,
1502
              struct ofpbuf *out)
1503
1.58k
{
1504
1.58k
    int members_len = ROUND_UP(2 * bundle->n_members, OFP_ACTION_ALIGN);
1505
1.58k
    struct nx_action_bundle *nab;
1506
1.58k
    ovs_be16 *members;
1507
1.58k
    size_t i;
1508
1509
1.58k
    nab = (bundle->dst.field
1510
1.58k
           ? put_NXAST_BUNDLE_LOAD(out)
1511
1.58k
           : put_NXAST_BUNDLE(out));
1512
1.58k
    nab->len = htons(ntohs(nab->len) + members_len);
1513
1.58k
    nab->algorithm = htons(bundle->algorithm);
1514
1.58k
    nab->fields = htons(bundle->fields);
1515
1.58k
    nab->basis = htons(bundle->basis);
1516
1.58k
    nab->member_type = htonl(mf_nxm_header(MFF_IN_PORT));
1517
1.58k
    nab->n_members = htons(bundle->n_members);
1518
1.58k
    if (bundle->dst.field) {
1519
3
        nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1520
3
                                              bundle->dst.n_bits);
1521
3
        nab->dst = htonl(nxm_header_from_mff(bundle->dst.field));
1522
3
    }
1523
1524
1.58k
    members = ofpbuf_put_zeros(out, members_len);
1525
52.7k
    for (i = 0; i < bundle->n_members; i++) {
1526
51.1k
        members[i] = htons(ofp_to_u16(bundle->members[i]));
1527
51.1k
    }
1528
1.58k
}
1529
1530
static char * OVS_WARN_UNUSED_RESULT
1531
parse_BUNDLE(const char *arg, const struct ofpact_parse_params *pp)
1532
3.09k
{
1533
3.09k
    return bundle_parse(arg, pp->port_map, pp->ofpacts);
1534
3.09k
}
1535
1536
static char * OVS_WARN_UNUSED_RESULT
1537
parse_bundle_load(const char *arg, const struct ofpact_parse_params *pp)
1538
10
{
1539
10
    return bundle_parse_load(arg, pp->port_map, pp->ofpacts);
1540
10
}
1541
1542
static void
1543
format_BUNDLE(const struct ofpact_bundle *a,
1544
              const struct ofpact_format_params *fp)
1545
997
{
1546
997
    bundle_format(a, fp->port_map, fp->s);
1547
997
}
1548
1549
static enum ofperr
1550
check_BUNDLE(const struct ofpact_bundle *a,
1551
             const struct ofpact_check_params *cp)
1552
2.29k
{
1553
2.29k
    return bundle_check(a, cp->max_ports, cp->match);
1554
2.29k
}
1555

1556
/* Set VLAN actions. */
1557
1558
static enum ofperr
1559
decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1560
8.55k
{
1561
8.55k
    if (vid & ~0xfff) {
1562
190
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1563
8.36k
    } else {
1564
8.36k
        struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1565
8.36k
        vlan_vid->vlan_vid = vid;
1566
8.36k
        vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1567
8.36k
        return 0;
1568
8.36k
    }
1569
8.55k
}
1570
1571
static enum ofperr
1572
decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1573
                                enum ofp_version ofp_version OVS_UNUSED,
1574
                                struct ofpbuf *out)
1575
7.59k
{
1576
7.59k
    return decode_set_vlan_vid(vid, true, out);
1577
7.59k
}
1578
1579
static enum ofperr
1580
decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1581
                                enum ofp_version ofp_version OVS_UNUSED,
1582
                                struct ofpbuf *out)
1583
956
{
1584
956
    return decode_set_vlan_vid(vid, false, out);
1585
956
}
1586
1587
static void
1588
encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1589
                    enum ofp_version ofp_version, struct ofpbuf *out)
1590
1.45k
{
1591
1.45k
    uint16_t vid = vlan_vid->vlan_vid;
1592
1593
    /* Push a VLAN tag, if none is present and this form of the action calls
1594
     * for such a feature. */
1595
1.45k
    if (ofp_version > OFP10_VERSION
1596
1.01k
        && vlan_vid->push_vlan_if_needed
1597
616
        && !vlan_vid->flow_has_vlan) {
1598
206
        put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1599
206
    }
1600
1601
1.45k
    if (ofp_version == OFP10_VERSION) {
1602
446
        put_OFPAT10_SET_VLAN_VID(out, vid);
1603
1.01k
    } else if (ofp_version == OFP11_VERSION) {
1604
303
        put_OFPAT11_SET_VLAN_VID(out, vid);
1605
710
    } else {
1606
710
        put_set_field(out, ofp_version, MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1607
710
    }
1608
1.45k
}
1609
1610
static char * OVS_WARN_UNUSED_RESULT
1611
parse_set_vlan_vid(char *arg, bool push_vlan_if_needed,
1612
                   const struct ofpact_parse_params *pp)
1613
2.09k
{
1614
2.09k
    struct ofpact_vlan_vid *vlan_vid;
1615
2.09k
    uint16_t vid;
1616
2.09k
    char *error;
1617
1618
2.09k
    error = str_to_u16(arg, "VLAN VID", &vid);
1619
2.09k
    if (error) {
1620
4
        return error;
1621
4
    }
1622
1623
2.09k
    if (vid & ~VLAN_VID_MASK) {
1624
9
        return xasprintf("%s: not a valid VLAN VID", arg);
1625
9
    }
1626
2.08k
    vlan_vid = ofpact_put_SET_VLAN_VID(pp->ofpacts);
1627
2.08k
    vlan_vid->vlan_vid = vid;
1628
2.08k
    vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1629
2.08k
    return NULL;
1630
2.09k
}
1631
1632
static char * OVS_WARN_UNUSED_RESULT
1633
parse_SET_VLAN_VID(char *arg, const struct ofpact_parse_params *pp)
1634
1.08k
{
1635
1.08k
    return parse_set_vlan_vid(arg, false, pp);
1636
1.08k
}
1637
1638
static void
1639
format_SET_VLAN_VID(const struct ofpact_vlan_vid *a,
1640
                    const struct ofpact_format_params *fp)
1641
5.10k
{
1642
5.10k
    ds_put_format(fp->s, "%s%s:%s%"PRIu16, colors.param,
1643
5.10k
                  a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1644
5.10k
                  colors.end, a->vlan_vid);
1645
5.10k
}
1646
1647
static enum ofperr
1648
check_SET_VLAN_VID(struct ofpact_vlan_vid *a, struct ofpact_check_params *cp)
1649
6.77k
{
1650
    /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1651
     * 1.1+ if need be. */
1652
6.77k
    ovs_be16 *tci = &cp->match->flow.vlans[0].tci;
1653
6.77k
    a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
1654
6.77k
    if (!a->flow_has_vlan && !a->push_vlan_if_needed) {
1655
334
        inconsistent_match(&cp->usable_protocols);
1656
334
    }
1657
1658
    /* Temporarily mark that we have a vlan tag. */
1659
6.77k
    *tci |= htons(VLAN_CFI);
1660
1661
6.77k
    return 0;
1662
6.77k
}
1663

1664
/* Set PCP actions. */
1665
1666
static enum ofperr
1667
decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1668
5.06k
{
1669
5.06k
    if (pcp & ~7) {
1670
175
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1671
4.89k
    } else {
1672
4.89k
        struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1673
4.89k
        vlan_pcp->vlan_pcp = pcp;
1674
4.89k
        vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1675
4.89k
        return 0;
1676
4.89k
    }
1677
5.06k
}
1678
1679
static enum ofperr
1680
decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1681
                                enum ofp_version ofp_version OVS_UNUSED,
1682
                                struct ofpbuf *out)
1683
3.45k
{
1684
3.45k
    return decode_set_vlan_pcp(pcp, true, out);
1685
3.45k
}
1686
1687
static enum ofperr
1688
decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1689
                                enum ofp_version ofp_version OVS_UNUSED,
1690
                                struct ofpbuf *out)
1691
1.61k
{
1692
1.61k
    return decode_set_vlan_pcp(pcp, false, out);
1693
1.61k
}
1694
1695
static void
1696
encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1697
                    enum ofp_version ofp_version, struct ofpbuf *out)
1698
1.66k
{
1699
1.66k
    uint8_t pcp = vlan_pcp->vlan_pcp;
1700
1701
    /* Push a VLAN tag, if none is present and this form of the action calls
1702
     * for such a feature. */
1703
1.66k
    if (ofp_version > OFP10_VERSION
1704
1.28k
        && vlan_pcp->push_vlan_if_needed
1705
760
        && !vlan_pcp->flow_has_vlan) {
1706
202
        put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1707
202
    }
1708
1709
1.66k
    if (ofp_version == OFP10_VERSION) {
1710
385
        put_OFPAT10_SET_VLAN_PCP(out, pcp);
1711
1.28k
    } else if (ofp_version == OFP11_VERSION) {
1712
761
        put_OFPAT11_SET_VLAN_PCP(out, pcp);
1713
761
    } else {
1714
519
        put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1715
519
    }
1716
1.66k
}
1717
1718
static char * OVS_WARN_UNUSED_RESULT
1719
parse_set_vlan_pcp(char *arg, bool push_vlan_if_needed,
1720
                   const struct ofpact_parse_params *pp)
1721
1.74k
{
1722
1.74k
    struct ofpact_vlan_pcp *vlan_pcp;
1723
1.74k
    uint8_t pcp;
1724
1.74k
    char *error;
1725
1726
1.74k
    error = str_to_u8(arg, "VLAN PCP", &pcp);
1727
1.74k
    if (error) {
1728
4
        return error;
1729
4
    }
1730
1731
1.74k
    if (pcp & ~7) {
1732
5
        return xasprintf("%s: not a valid VLAN PCP", arg);
1733
5
    }
1734
1.73k
    vlan_pcp = ofpact_put_SET_VLAN_PCP(pp->ofpacts);
1735
1.73k
    vlan_pcp->vlan_pcp = pcp;
1736
1.73k
    vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1737
1.73k
    return NULL;
1738
1.74k
}
1739
1740
static char * OVS_WARN_UNUSED_RESULT
1741
parse_SET_VLAN_PCP(char *arg, const struct ofpact_parse_params *pp)
1742
770
{
1743
770
    return parse_set_vlan_pcp(arg, false, pp);
1744
770
}
1745
1746
static void
1747
format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a,
1748
                    const struct ofpact_format_params *fp)
1749
2.93k
{
1750
2.93k
    ds_put_format(fp->s, "%s%s:%s%"PRIu8, colors.param,
1751
2.93k
                  a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1752
2.93k
                  colors.end, a->vlan_pcp);
1753
2.93k
}
1754
1755
static enum ofperr
1756
check_SET_VLAN_PCP(struct ofpact_vlan_pcp *a, struct ofpact_check_params *cp)
1757
4.88k
{
1758
    /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1759
     * 1.1+ if need be. */
1760
4.88k
    ovs_be16 *tci = &cp->match->flow.vlans[0].tci;
1761
4.88k
    a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
1762
4.88k
    if (!a->flow_has_vlan && !a->push_vlan_if_needed) {
1763
1.09k
        inconsistent_match(&cp->usable_protocols);
1764
1.09k
    }
1765
1766
    /* Temporarily mark that we have a vlan tag. */
1767
4.88k
    *tci |= htons(VLAN_CFI);
1768
1769
4.88k
    return 0;
1770
4.88k
}
1771

1772
/* Strip VLAN actions. */
1773
1774
static enum ofperr
1775
decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1776
5.07k
{
1777
5.07k
    ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1778
5.07k
    return 0;
1779
5.07k
}
1780
1781
static enum ofperr
1782
decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1783
3.94k
{
1784
3.94k
    ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1785
3.94k
    return 0;
1786
3.94k
}
1787
1788
static void
1789
encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1790
                  enum ofp_version ofp_version, struct ofpbuf *out)
1791
1.64k
{
1792
1.64k
    if (ofp_version == OFP10_VERSION) {
1793
878
        put_OFPAT10_STRIP_VLAN(out);
1794
878
    } else {
1795
770
        put_OFPAT11_POP_VLAN(out);
1796
770
    }
1797
1.64k
}
1798
1799
static char * OVS_WARN_UNUSED_RESULT
1800
parse_STRIP_VLAN(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
1801
713
{
1802
713
    ofpact_put_STRIP_VLAN(pp->ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1803
713
    return NULL;
1804
713
}
1805
1806
static char * OVS_WARN_UNUSED_RESULT
1807
parse_pop_vlan(const struct ofpact_parse_params *pp)
1808
2.04k
{
1809
2.04k
    ofpact_put_STRIP_VLAN(pp->ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1810
2.04k
    return NULL;
1811
2.04k
}
1812
1813
static void
1814
format_STRIP_VLAN(const struct ofpact_null *a,
1815
                  const struct ofpact_format_params *fp)
1816
5.96k
{
1817
5.96k
    ds_put_format(fp->s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1818
5.96k
                    ? "%spop_vlan%s"
1819
5.96k
                    : "%sstrip_vlan%s"),
1820
5.96k
                  colors.value, colors.end);
1821
5.96k
}
1822
1823
static enum ofperr
1824
check_STRIP_VLAN(const struct ofpact_null *a OVS_UNUSED,
1825
                 struct ofpact_check_params *cp)
1826
8.39k
{
1827
8.39k
    if (!(cp->match->flow.vlans[0].tci & htons(VLAN_CFI))) {
1828
3.37k
        inconsistent_match(&cp->usable_protocols);
1829
3.37k
    }
1830
8.39k
    flow_pop_vlan(&cp->match->flow, NULL);
1831
8.39k
    return 0;
1832
8.39k
}
1833

1834
/* Push VLAN action. */
1835
1836
static enum ofperr
1837
decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1838
                             enum ofp_version ofp_version OVS_UNUSED,
1839
                             struct ofpbuf *out)
1840
2.62k
{
1841
2.62k
    struct ofpact_push_vlan *push_vlan;
1842
2.62k
    if (!eth_type_vlan(eth_type)) {
1843
441
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1844
441
    }
1845
2.18k
    push_vlan = ofpact_put_PUSH_VLAN(out);
1846
2.18k
    push_vlan->ethertype = eth_type;
1847
2.18k
    return 0;
1848
2.62k
}
1849
1850
static void
1851
encode_PUSH_VLAN(const struct ofpact_push_vlan *push_vlan,
1852
                 enum ofp_version ofp_version, struct ofpbuf *out)
1853
722
{
1854
722
    if (ofp_version == OFP10_VERSION) {
1855
        /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1856
         * follow this action. */
1857
522
    } else {
1858
522
        put_OFPAT11_PUSH_VLAN(out, push_vlan->ethertype);
1859
522
    }
1860
722
}
1861
1862
static char * OVS_WARN_UNUSED_RESULT
1863
parse_PUSH_VLAN(char *arg, const struct ofpact_parse_params *pp)
1864
1.25k
{
1865
1.25k
    struct ofpact_push_vlan *push_vlan;
1866
1.25k
    uint16_t ethertype;
1867
1.25k
    char *error;
1868
1869
1.25k
    *pp->usable_protocols &= OFPUTIL_P_OF11_UP;
1870
1.25k
    error = str_to_u16(arg, "ethertype", &ethertype);
1871
1.25k
    if (error) {
1872
3
        return error;
1873
3
    }
1874
1875
1.24k
    if (!eth_type_vlan(htons(ethertype))) {
1876
41
        return xasprintf("%s: not a valid VLAN ethertype", arg);
1877
41
    }
1878
1.20k
    push_vlan = ofpact_put_PUSH_VLAN(pp->ofpacts);
1879
1.20k
    push_vlan->ethertype = htons(ethertype);
1880
1.20k
    return NULL;
1881
1.24k
}
1882
1883
static void
1884
format_PUSH_VLAN(const struct ofpact_push_vlan *push_vlan,
1885
                 const struct ofpact_format_params *fp)
1886
1.12k
{
1887
1.12k
    ds_put_format(fp->s, "%spush_vlan:%s%#"PRIx16,
1888
1.12k
                  colors.param, colors.end, ntohs(push_vlan->ethertype));
1889
1.12k
}
1890
1891
static enum ofperr
1892
check_PUSH_VLAN(const struct ofpact_push_vlan *a OVS_UNUSED,
1893
                struct ofpact_check_params *cp)
1894
3.00k
{
1895
3.00k
    struct flow *flow = &cp->match->flow;
1896
3.00k
    if (flow->vlans[FLOW_MAX_VLAN_HEADERS - 1].tci & htons(VLAN_CFI)) {
1897
        /* Support maximum (FLOW_MAX_VLAN_HEADERS) VLAN headers. */
1898
299
        return OFPERR_OFPBAC_BAD_TAG;
1899
299
    }
1900
    /* Temporary mark that we have a vlan tag. */
1901
2.70k
    flow_push_vlan_uninit(flow, NULL);
1902
2.70k
    flow->vlans[0].tci |= htons(VLAN_CFI);
1903
2.70k
    return 0;
1904
3.00k
}
1905

1906
/* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1907
struct ofp_action_dl_addr {
1908
    ovs_be16 type;                  /* Type. */
1909
    ovs_be16 len;                   /* Length is 16. */
1910
    struct eth_addr dl_addr;        /* Ethernet address. */
1911
    uint8_t pad[6];
1912
};
1913
OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1914
1915
static enum ofperr
1916
decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1917
                            enum ofp_version ofp_version OVS_UNUSED,
1918
                            struct ofpbuf *out)
1919
92
{
1920
92
    ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1921
92
    return 0;
1922
92
}
1923
1924
static enum ofperr
1925
decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1926
                            enum ofp_version ofp_version OVS_UNUSED,
1927
                            struct ofpbuf *out)
1928
113
{
1929
113
    ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1930
113
    return 0;
1931
113
}
1932
1933
static void
1934
encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1935
                    enum ofp_raw_action_type raw, enum mf_field_id field,
1936
                    struct ofpbuf *out)
1937
784
{
1938
784
    if (ofp_version < OFP12_VERSION) {
1939
396
        struct ofp_action_dl_addr *oada;
1940
1941
396
        oada = ofpact_put_raw(out, ofp_version, raw, 0);
1942
396
        oada->dl_addr = mac->mac;
1943
396
    } else {
1944
388
        put_set_field(out, ofp_version, field, eth_addr_to_uint64(mac->mac));
1945
388
    }
1946
784
}
1947
1948
static void
1949
encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1950
                   struct ofpbuf *out)
1951
392
{
1952
392
    encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1953
392
                        out);
1954
1955
392
}
1956
1957
static void
1958
encode_SET_ETH_DST(const struct ofpact_mac *mac,
1959
                               enum ofp_version ofp_version,
1960
                               struct ofpbuf *out)
1961
392
{
1962
392
    encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1963
392
                        out);
1964
1965
392
}
1966
1967
static char * OVS_WARN_UNUSED_RESULT
1968
parse_SET_ETH_SRC(char *arg, const struct ofpact_parse_params *pp)
1969
403
{
1970
403
    return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(pp->ofpacts)->mac);
1971
403
}
1972
1973
static char * OVS_WARN_UNUSED_RESULT
1974
parse_SET_ETH_DST(char *arg, const struct ofpact_parse_params *pp)
1975
402
{
1976
402
    return str_to_mac(arg, &ofpact_put_SET_ETH_DST(pp->ofpacts)->mac);
1977
402
}
1978
1979
static void
1980
format_SET_ETH_SRC(const struct ofpact_mac *a,
1981
                   const struct ofpact_format_params *fp)
1982
91
{
1983
91
    ds_put_format(fp->s, "%smod_dl_src:%s"ETH_ADDR_FMT,
1984
91
                  colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1985
91
}
1986
1987
static void
1988
format_SET_ETH_DST(const struct ofpact_mac *a,
1989
                   const struct ofpact_format_params *fp)
1990
68
{
1991
68
    ds_put_format(fp->s, "%smod_dl_dst:%s"ETH_ADDR_FMT,
1992
68
                  colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1993
68
}
1994
1995
static enum ofperr
1996
check_SET_ETH_SRC(const struct ofpact_mac *a OVS_UNUSED,
1997
                  const struct ofpact_check_params *cp OVS_UNUSED)
1998
477
{
1999
477
    return 0;
2000
477
}
2001
2002
static enum ofperr
2003
check_SET_ETH_DST(const struct ofpact_mac *a OVS_UNUSED,
2004
                  const struct ofpact_check_params *cp OVS_UNUSED)
2005
439
{
2006
439
    return 0;
2007
439
}
2008

2009
/* Set IPv4 address actions. */
2010
2011
static enum ofperr
2012
decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
2013
                            enum ofp_version ofp_version OVS_UNUSED,
2014
                            struct ofpbuf *out)
2015
2.77k
{
2016
2.77k
    ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
2017
2.77k
    return 0;
2018
2.77k
}
2019
2020
static enum ofperr
2021
decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
2022
                            enum ofp_version ofp_version OVS_UNUSED,
2023
                            struct ofpbuf *out)
2024
6.49k
{
2025
6.49k
    ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
2026
6.49k
    return 0;
2027
6.49k
}
2028
2029
static void
2030
encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
2031
                     enum ofp_version ofp_version,
2032
                     enum ofp_raw_action_type raw, enum mf_field_id field,
2033
                     struct ofpbuf *out)
2034
1.92k
{
2035
1.92k
    ovs_be32 addr = ipv4->ipv4;
2036
1.92k
    if (ofp_version < OFP12_VERSION) {
2037
1.49k
        ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
2038
1.49k
    } else {
2039
438
        put_set_field(out, ofp_version, field, ntohl(addr));
2040
438
    }
2041
1.92k
}
2042
2043
static void
2044
encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
2045
                    enum ofp_version ofp_version, struct ofpbuf *out)
2046
1.02k
{
2047
1.02k
    encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
2048
1.02k
                         out);
2049
1.02k
}
2050
2051
static void
2052
encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
2053
                    enum ofp_version ofp_version, struct ofpbuf *out)
2054
902
{
2055
902
    encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
2056
902
                         out);
2057
902
}
2058
2059
static char * OVS_WARN_UNUSED_RESULT
2060
parse_SET_IPV4_SRC(char *arg, const struct ofpact_parse_params *pp)
2061
1.03k
{
2062
1.03k
    return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(pp->ofpacts)->ipv4);
2063
1.03k
}
2064
2065
static char * OVS_WARN_UNUSED_RESULT
2066
parse_SET_IPV4_DST(char *arg, const struct ofpact_parse_params *pp)
2067
914
{
2068
914
    return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(pp->ofpacts)->ipv4);
2069
914
}
2070
2071
static void
2072
format_SET_IPV4_SRC(const struct ofpact_ipv4 *a,
2073
                    const struct ofpact_format_params *fp)
2074
1.29k
{
2075
1.29k
    ds_put_format(fp->s, "%smod_nw_src:%s"IP_FMT,
2076
1.29k
                  colors.param, colors.end, IP_ARGS(a->ipv4));
2077
1.29k
}
2078
2079
static void
2080
format_SET_IPV4_DST(const struct ofpact_ipv4 *a,
2081
                    const struct ofpact_format_params *fp)
2082
3.52k
{
2083
3.52k
    ds_put_format(fp->s, "%smod_nw_dst:%s"IP_FMT,
2084
3.52k
                  colors.param, colors.end, IP_ARGS(a->ipv4));
2085
3.52k
}
2086
2087
static enum ofperr
2088
check_set_ipv4(struct ofpact_check_params *cp)
2089
8.11k
{
2090
8.11k
    ovs_be16 dl_type = get_dl_type(&cp->match->flow);
2091
8.11k
    if (dl_type != htons(ETH_TYPE_IP)) {
2092
3.25k
        inconsistent_match(&cp->usable_protocols);
2093
3.25k
    }
2094
8.11k
    return 0;
2095
8.11k
}
2096
2097
static enum ofperr
2098
check_SET_IPV4_SRC(const struct ofpact_ipv4 *a OVS_UNUSED,
2099
                   struct ofpact_check_params *cp)
2100
2.97k
{
2101
2.97k
    return check_set_ipv4(cp);
2102
2.97k
}
2103
2104
static enum ofperr
2105
check_SET_IPV4_DST(const struct ofpact_ipv4 *a OVS_UNUSED,
2106
                   struct ofpact_check_params *cp)
2107
5.14k
{
2108
5.14k
    return check_set_ipv4(cp);
2109
5.14k
}
2110

2111
/* Set IPv4/v6 TOS actions. */
2112
2113
static enum ofperr
2114
decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
2115
                            enum ofp_version ofp_version OVS_UNUSED,
2116
                            struct ofpbuf *out)
2117
7.37k
{
2118
7.37k
    if (dscp & ~IP_DSCP_MASK) {
2119
135
        return OFPERR_OFPBAC_BAD_ARGUMENT;
2120
7.23k
    } else {
2121
7.23k
        ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
2122
7.23k
        return 0;
2123
7.23k
    }
2124
7.37k
}
2125
2126
static void
2127
encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
2128
                   enum ofp_version ofp_version, struct ofpbuf *out)
2129
1.29k
{
2130
1.29k
    if (ofp_version < OFP12_VERSION) {
2131
962
        put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
2132
962
    } else {
2133
328
        put_set_field(out, ofp_version, MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
2134
328
    }
2135
1.29k
}
2136
2137
static char * OVS_WARN_UNUSED_RESULT
2138
parse_SET_IP_DSCP(char *arg, const struct ofpact_parse_params *pp)
2139
2140
1.58k
{
2141
1.58k
    uint8_t tos;
2142
1.58k
    char *error;
2143
2144
1.58k
    error = str_to_u8(arg, "TOS", &tos);
2145
1.58k
    if (error) {
2146
6
        return error;
2147
6
    }
2148
2149
1.57k
    if (tos & ~IP_DSCP_MASK) {
2150
1
        return xasprintf("%s: not a valid TOS", arg);
2151
1
    }
2152
1.57k
    ofpact_put_SET_IP_DSCP(pp->ofpacts)->dscp = tos;
2153
1.57k
    return NULL;
2154
1.57k
}
2155
2156
static void
2157
format_SET_IP_DSCP(const struct ofpact_dscp *a,
2158
                   const struct ofpact_format_params *fp)
2159
5.09k
{
2160
5.09k
    ds_put_format(fp->s, "%smod_nw_tos:%s%d",
2161
5.09k
                  colors.param, colors.end, a->dscp);
2162
5.09k
}
2163
2164
static enum ofperr
2165
check_set_ip(struct ofpact_check_params *cp)
2166
15.8k
{
2167
15.8k
    if (!is_ip_any(&cp->match->flow)) {
2168
8.60k
        inconsistent_match(&cp->usable_protocols);
2169
8.60k
    }
2170
15.8k
    return 0;
2171
15.8k
}
2172
2173
static enum ofperr
2174
check_SET_IP_DSCP(const struct ofpact_dscp *a OVS_UNUSED,
2175
                  struct ofpact_check_params *cp)
2176
6.57k
{
2177
6.57k
    return check_set_ip(cp);
2178
6.57k
}
2179

2180
/* Set IPv4/v6 ECN actions. */
2181
2182
static enum ofperr
2183
decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
2184
                              enum ofp_version ofp_version OVS_UNUSED,
2185
                              struct ofpbuf *out)
2186
2.29k
{
2187
2.29k
    if (ecn & ~IP_ECN_MASK) {
2188
811
        return OFPERR_OFPBAC_BAD_ARGUMENT;
2189
1.48k
    } else {
2190
1.48k
        ofpact_put_SET_IP_ECN(out)->ecn = ecn;
2191
1.48k
        return 0;
2192
1.48k
    }
2193
2.29k
}
2194
2195
static void
2196
encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
2197
                  enum ofp_version ofp_version, struct ofpbuf *out)
2198
1.17k
{
2199
1.17k
    uint8_t ecn = ip_ecn->ecn;
2200
1.17k
    if (ofp_version == OFP10_VERSION) {
2201
757
        struct mf_subfield dst = { .field = mf_from_id(MFF_IP_ECN),
2202
757
                                   .ofs = 0, .n_bits = 2 };
2203
757
        put_reg_load(out, &dst, ecn);
2204
757
    } else if (ofp_version == OFP11_VERSION) {
2205
194
        put_OFPAT11_SET_NW_ECN(out, ecn);
2206
228
    } else {
2207
228
        put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
2208
228
    }
2209
1.17k
}
2210
2211
static char * OVS_WARN_UNUSED_RESULT
2212
parse_SET_IP_ECN(char *arg, const struct ofpact_parse_params *pp)
2213
3.18k
{
2214
3.18k
    uint8_t ecn;
2215
3.18k
    char *error;
2216
2217
3.18k
    error = str_to_u8(arg, "ECN", &ecn);
2218
3.18k
    if (error) {
2219
18
        return error;
2220
18
    }
2221
2222
3.16k
    if (ecn & ~IP_ECN_MASK) {
2223
1
        return xasprintf("%s: not a valid ECN", arg);
2224
1
    }
2225
3.16k
    ofpact_put_SET_IP_ECN(pp->ofpacts)->ecn = ecn;
2226
3.16k
    return NULL;
2227
3.16k
}
2228
2229
static void
2230
format_SET_IP_ECN(const struct ofpact_ecn *a,
2231
                  const struct ofpact_format_params *fp)
2232
1.16k
{
2233
1.16k
    ds_put_format(fp->s, "%smod_nw_ecn:%s%d",
2234
1.16k
                  colors.param, colors.end, a->ecn);
2235
1.16k
}
2236
2237
static enum ofperr
2238
check_SET_IP_ECN(const struct ofpact_ecn *a OVS_UNUSED,
2239
                 struct ofpact_check_params *cp)
2240
2.00k
{
2241
2.00k
    return check_set_ip(cp);
2242
2.00k
}
2243

2244
/* Set IPv4/v6 TTL actions. */
2245
2246
static enum ofperr
2247
decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
2248
                              enum ofp_version ofp_version OVS_UNUSED,
2249
                              struct ofpbuf *out)
2250
1.22k
{
2251
1.22k
    ofpact_put_SET_IP_TTL(out)->ttl = ttl;
2252
1.22k
    return 0;
2253
1.22k
}
2254
2255
static void
2256
encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
2257
                  enum ofp_version ofp_version, struct ofpbuf *out)
2258
989
{
2259
989
    if (ofp_version >= OFP11_VERSION) {
2260
228
        put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
2261
761
    } else {
2262
761
        struct mf_subfield dst = { .field = mf_from_id(MFF_IP_TTL),
2263
761
                                   .ofs = 0, .n_bits = 8 };
2264
761
        put_reg_load(out, &dst, ttl->ttl);
2265
761
    }
2266
989
}
2267
2268
static char * OVS_WARN_UNUSED_RESULT
2269
parse_SET_IP_TTL(char *arg, const struct ofpact_parse_params *pp)
2270
2271
1.94k
{
2272
1.94k
    uint8_t ttl;
2273
1.94k
    char *error;
2274
2275
1.94k
    error = str_to_u8(arg, "TTL", &ttl);
2276
1.94k
    if (error) {
2277
25
        return error;
2278
25
    }
2279
2280
1.92k
    ofpact_put_SET_IP_TTL(pp->ofpacts)->ttl = ttl;
2281
1.92k
    return NULL;
2282
1.94k
}
2283
2284
static void
2285
format_SET_IP_TTL(const struct ofpact_ip_ttl *a,
2286
                  const struct ofpact_format_params *fp)
2287
1.21k
{
2288
1.21k
    ds_put_format(fp->s, "%smod_nw_ttl:%s%d",
2289
1.21k
                  colors.param, colors.end, a->ttl);
2290
1.21k
}
2291
2292
static enum ofperr
2293
check_SET_IP_TTL(const struct ofpact_ip_ttl *a OVS_UNUSED,
2294
                 struct ofpact_check_params *cp)
2295
2.93k
{
2296
2.93k
    return check_set_ip(cp);
2297
2.93k
}
2298

2299
/* Set TCP/UDP/SCTP port actions. */
2300
2301
static enum ofperr
2302
decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
2303
                            enum ofp_version ofp_version OVS_UNUSED,
2304
                            struct ofpbuf *out)
2305
6.36k
{
2306
6.36k
    ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
2307
6.36k
    return 0;
2308
6.36k
}
2309
2310
static enum ofperr
2311
decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
2312
                            enum ofp_version ofp_version OVS_UNUSED,
2313
                            struct ofpbuf *out)
2314
8.11k
{
2315
8.11k
    ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
2316
8.11k
    return 0;
2317
8.11k
}
2318
2319
static void
2320
encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
2321
                   enum ofp_version ofp_version, enum ofp_raw_action_type raw,
2322
                   enum mf_field_id field, struct ofpbuf *out)
2323
3.64k
{
2324
3.64k
    uint16_t port = l4_port->port;
2325
2326
3.64k
    if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
2327
519
        put_set_field(out, ofp_version, field, port);
2328
3.12k
    } else {
2329
3.12k
        ofpact_put_raw(out, ofp_version, raw, port);
2330
3.12k
    }
2331
3.64k
}
2332
2333
static void
2334
encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
2335
                       enum ofp_version ofp_version, struct ofpbuf *out)
2336
2.19k
{
2337
2.19k
    uint8_t proto = l4_port->flow_ip_proto;
2338
2.19k
    enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
2339
2.19k
                              : proto == IPPROTO_UDP ? MFF_UDP_SRC
2340
1.87k
                              : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2341
1.67k
                              : MFF_N_IDS);
2342
2343
2.19k
    encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
2344
2.19k
}
2345
2346
static void
2347
encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
2348
                       enum ofp_version ofp_version,
2349
                       struct ofpbuf *out)
2350
1.45k
{
2351
1.45k
    uint8_t proto = l4_port->flow_ip_proto;
2352
1.45k
    enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
2353
1.45k
                              : proto == IPPROTO_UDP ? MFF_UDP_DST
2354
1.23k
                              : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2355
1.00k
                              : MFF_N_IDS);
2356
2357
1.45k
    encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
2358
1.45k
}
2359
2360
static char * OVS_WARN_UNUSED_RESULT
2361
parse_SET_L4_SRC_PORT(char *arg, const struct ofpact_parse_params *pp)
2362
2.48k
{
2363
2.48k
    return str_to_u16(arg, "source port",
2364
2.48k
                      &ofpact_put_SET_L4_SRC_PORT(pp->ofpacts)->port);
2365
2.48k
}
2366
2367
static char * OVS_WARN_UNUSED_RESULT
2368
parse_SET_L4_DST_PORT(char *arg, const struct ofpact_parse_params *pp)
2369
1.47k
{
2370
1.47k
    return str_to_u16(arg, "destination port",
2371
1.47k
                      &ofpact_put_SET_L4_DST_PORT(pp->ofpacts)->port);
2372
1.47k
}
2373
2374
static void
2375
format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a,
2376
                       const struct ofpact_format_params *fp)
2377
4.47k
{
2378
4.47k
    ds_put_format(fp->s, "%smod_tp_src:%s%d",
2379
4.47k
                  colors.param, colors.end, a->port);
2380
4.47k
}
2381
2382
static void
2383
format_SET_L4_DST_PORT(const struct ofpact_l4_port *a,
2384
                       const struct ofpact_format_params *fp)
2385
5.75k
{
2386
5.75k
    ds_put_format(fp->s, "%smod_tp_dst:%s%d",
2387
5.75k
                  colors.param, colors.end, a->port);
2388
5.75k
}
2389
2390
static enum ofperr
2391
check_set_l4_port(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2392
12.7k
{
2393
12.7k
    const struct flow *flow = &cp->match->flow;
2394
12.7k
    if (!is_ip_any(flow)
2395
7.16k
        || flow->nw_frag & FLOW_NW_FRAG_LATER
2396
6.63k
        || (flow->nw_proto != IPPROTO_TCP &&
2397
5.11k
            flow->nw_proto != IPPROTO_UDP &&
2398
7.88k
            flow->nw_proto != IPPROTO_SCTP)) {
2399
7.88k
        inconsistent_match(&cp->usable_protocols);
2400
7.88k
    }
2401
2402
    /* Note the transport protocol in use, to allow this action to be converted
2403
     * to an OF1.2 set_field action later if necessary. */
2404
12.7k
    a->flow_ip_proto = flow->nw_proto;
2405
2406
12.7k
    return 0;
2407
12.7k
}
2408
2409
static enum ofperr
2410
check_SET_L4_SRC_PORT(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2411
5.59k
{
2412
5.59k
    return check_set_l4_port(a, cp);
2413
5.59k
}
2414
2415
static enum ofperr
2416
check_SET_L4_DST_PORT(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2417
7.19k
{
2418
7.19k
    return check_set_l4_port(a, cp);
2419
7.19k
}
2420

2421
/* Action structure for OFPAT_COPY_FIELD. */
2422
struct ofp15_action_copy_field {
2423
    ovs_be16 type;              /* OFPAT_COPY_FIELD. */
2424
    ovs_be16 len;               /* Length is padded to 64 bits. */
2425
    ovs_be16 n_bits;            /* Number of bits to copy. */
2426
    ovs_be16 src_offset;        /* Starting bit offset in source. */
2427
    ovs_be16 dst_offset;        /* Starting bit offset in destination. */
2428
    uint8_t pad[2];
2429
    /* Followed by:
2430
     * - OXM header for source field.
2431
     * - OXM header for destination field.
2432
     * - Padding with 0-bytes to a multiple of 8 bytes.
2433
     * The "pad2" member is the beginning of the above. */
2434
    uint8_t pad2[4];
2435
};
2436
OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
2437
2438
/* Action structure for OpenFlow 1.3 extension copy-field action.. */
2439
struct onf_action_copy_field {
2440
    ovs_be16 type;              /* OFPAT_EXPERIMENTER. */
2441
    ovs_be16 len;               /* Length is padded to 64 bits. */
2442
    ovs_be32 experimenter;      /* ONF_VENDOR_ID. */
2443
    ovs_be16 exp_type;          /* 3200. */
2444
    uint8_t pad[2];             /* Not used. */
2445
    ovs_be16 n_bits;            /* Number of bits to copy. */
2446
    ovs_be16 src_offset;        /* Starting bit offset in source. */
2447
    ovs_be16 dst_offset;        /* Starting bit offset in destination. */
2448
    uint8_t pad2[2];            /* Not used. */
2449
    /* Followed by:
2450
     * - OXM header for source field.
2451
     * - OXM header for destination field.
2452
     * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2453
     * The "pad3" member is the beginning of the above. */
2454
    uint8_t pad3[4];            /* Not used. */
2455
};
2456
OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2457
2458
/* Action structure for NXAST_REG_MOVE.
2459
 *
2460
 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2461
 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2462
 * bit 'c').  Bit numbering starts at 0 for the least-significant bit, 1 for
2463
 * the next most significant bit, and so on.
2464
 *
2465
 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0.  (It doesn't make
2466
 * sense to use nxm_hasmask=1 because the action does not do any kind of
2467
 * matching; it uses the actual value of a field.)
2468
 *
2469
 * The following nxm_header values are potentially acceptable as 'src':
2470
 *
2471
 *   - NXM_OF_IN_PORT
2472
 *   - NXM_OF_ETH_DST
2473
 *   - NXM_OF_ETH_SRC
2474
 *   - NXM_OF_ETH_TYPE
2475
 *   - NXM_OF_VLAN_TCI
2476
 *   - NXM_OF_IP_TOS
2477
 *   - NXM_OF_IP_PROTO
2478
 *   - NXM_OF_IP_SRC
2479
 *   - NXM_OF_IP_DST
2480
 *   - NXM_OF_TCP_SRC
2481
 *   - NXM_OF_TCP_DST
2482
 *   - NXM_OF_UDP_SRC
2483
 *   - NXM_OF_UDP_DST
2484
 *   - NXM_OF_ICMP_TYPE
2485
 *   - NXM_OF_ICMP_CODE
2486
 *   - NXM_OF_ARP_OP
2487
 *   - NXM_OF_ARP_SPA
2488
 *   - NXM_OF_ARP_TPA
2489
 *   - NXM_NX_TUN_ID
2490
 *   - NXM_NX_ARP_SHA
2491
 *   - NXM_NX_ARP_THA
2492
 *   - NXM_NX_ICMPV6_TYPE
2493
 *   - NXM_NX_ICMPV6_CODE
2494
 *   - NXM_NX_ND_SLL
2495
 *   - NXM_NX_ND_TLL
2496
 *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
2497
 *   - NXM_NX_PKT_MARK
2498
 *   - NXM_NX_TUN_IPV4_SRC
2499
 *   - NXM_NX_TUN_IPV4_DST
2500
 *
2501
 * The following nxm_header values are potentially acceptable as 'dst':
2502
 *
2503
 *   - NXM_OF_ETH_DST
2504
 *   - NXM_OF_ETH_SRC
2505
 *   - NXM_OF_IP_TOS
2506
 *   - NXM_OF_IP_SRC
2507
 *   - NXM_OF_IP_DST
2508
 *   - NXM_OF_TCP_SRC
2509
 *   - NXM_OF_TCP_DST
2510
 *   - NXM_OF_UDP_SRC
2511
 *   - NXM_OF_UDP_DST
2512
 *   - NXM_OF_ICMP_TYPE
2513
 *   - NXM_OF_ICMP_CODE
2514
 *   - NXM_NX_ICMPV6_TYPE
2515
 *   - NXM_NX_ICMPV6_CODE
2516
 *   - NXM_NX_ARP_SHA
2517
 *   - NXM_NX_ARP_THA
2518
 *   - NXM_OF_ARP_OP
2519
 *   - NXM_OF_ARP_SPA
2520
 *   - NXM_OF_ARP_TPA
2521
 *     Modifying any of the above fields changes the corresponding packet
2522
 *     header.
2523
 *
2524
 *   - NXM_OF_IN_PORT
2525
 *
2526
 *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
2527
 *
2528
 *   - NXM_NX_PKT_MARK
2529
 *
2530
 *   - NXM_OF_VLAN_TCI.  Modifying this field's value has side effects on the
2531
 *     packet's 802.1Q header.  Setting a value with CFI=0 removes the 802.1Q
2532
 *     header (if any), ignoring the other bits.  Setting a value with CFI=1
2533
 *     adds or modifies the 802.1Q header appropriately, setting the TCI field
2534
 *     to the field's new value (with the CFI bit masked out).
2535
 *
2536
 *   - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST.  Modifying
2537
 *     any of these values modifies the corresponding tunnel header field used
2538
 *     for the packet's next tunnel encapsulation, if allowed by the
2539
 *     configuration of the output tunnel port.
2540
 *
2541
 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2542
 * nx_match satisfies its prerequisites.  For example, NXM_OF_IP_TOS may be
2543
 * used only if the flow's nx_match includes an nxm_entry that specifies
2544
 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2545
 *
2546
 * The switch will reject actions for which src_ofs+n_bits is greater than the
2547
 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2548
 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2549
 *
2550
 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2551
 * behaves as if 'src' were copied out to a temporary buffer, then the
2552
 * temporary buffer copied to 'dst'.
2553
 */
2554
struct nx_action_reg_move {
2555
    ovs_be16 type;                  /* OFPAT_VENDOR. */
2556
    ovs_be16 len;                   /* Length is 24. */
2557
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
2558
    ovs_be16 subtype;               /* NXAST_REG_MOVE. */
2559
    ovs_be16 n_bits;                /* Number of bits. */
2560
    ovs_be16 src_ofs;               /* Starting bit offset in source. */
2561
    ovs_be16 dst_ofs;               /* Starting bit offset in destination. */
2562
    /* Followed by:
2563
     * - OXM/NXM header for source field (4 or 8 bytes).
2564
     * - OXM/NXM header for destination field (4 or 8 bytes).
2565
     * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2566
};
2567
OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2568
2569
static enum ofperr
2570
decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2571
                    const void *action, ovs_be16 action_len, size_t oxm_offset,
2572
                    const struct vl_mff_map *vl_mff_map,
2573
                    uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2574
3.78k
{
2575
3.78k
    struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2576
3.78k
    enum ofperr error;
2577
2578
3.78k
    move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2579
3.78k
    move->src.ofs = ntohs(src_offset);
2580
3.78k
    move->src.n_bits = ntohs(n_bits);
2581
3.78k
    move->dst.ofs = ntohs(dst_offset);
2582
3.78k
    move->dst.n_bits = ntohs(n_bits);
2583
2584
3.78k
    struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
2585
3.78k
    ofpbuf_pull(&b, oxm_offset);
2586
2587
3.78k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->src.field, NULL,
2588
3.78k
                                     tlv_bitmap);
2589
3.78k
    if (error) {
2590
715
        return error;
2591
715
    }
2592
3.07k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->dst.field, NULL,
2593
3.07k
                                     tlv_bitmap);
2594
3.07k
    if (error) {
2595
358
        return error;
2596
358
    }
2597
2598
2.71k
    if (!is_all_zeros(b.data, b.size)) {
2599
692
        return OFPERR_NXBRC_MUST_BE_ZERO;
2600
692
    }
2601
2602
2.02k
    return nxm_reg_move_check(move, NULL);
2603
2.71k
}
2604
2605
static enum ofperr
2606
decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2607
                              enum ofp_version ofp_version OVS_UNUSED,
2608
                              const struct vl_mff_map *vl_mff_map,
2609
                              uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2610
3.75k
{
2611
3.75k
    return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2612
3.75k
                               oacf->n_bits, oacf, oacf->len,
2613
3.75k
                               OBJECT_OFFSETOF(oacf, pad2), vl_mff_map,
2614
3.75k
                               tlv_bitmap, ofpacts);
2615
3.75k
}
2616
2617
static enum ofperr
2618
decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2619
                               enum ofp_version ofp_version OVS_UNUSED,
2620
                               const struct vl_mff_map *vl_mff_map,
2621
                               uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2622
34
{
2623
34
    return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2624
34
                               oacf->n_bits, oacf, oacf->len,
2625
34
                               OBJECT_OFFSETOF(oacf, pad3), vl_mff_map,
2626
34
                               tlv_bitmap, ofpacts);
2627
34
}
2628
2629
static enum ofperr
2630
decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2631
                          enum ofp_version ofp_version OVS_UNUSED,
2632
                          const struct vl_mff_map *vl_mff_map,
2633
                          uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2634
3.69k
{
2635
3.69k
    struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2636
3.69k
    enum ofperr error;
2637
2638
3.69k
    move->ofpact.raw = NXAST_RAW_REG_MOVE;
2639
3.69k
    move->src.ofs = ntohs(narm->src_ofs);
2640
3.69k
    move->src.n_bits = ntohs(narm->n_bits);
2641
3.69k
    move->dst.ofs = ntohs(narm->dst_ofs);
2642
3.69k
    move->dst.n_bits = ntohs(narm->n_bits);
2643
2644
3.69k
    struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
2645
3.69k
    ofpbuf_pull(&b, sizeof *narm);
2646
2647
3.69k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->src.field, NULL,
2648
3.69k
                                     tlv_bitmap);
2649
3.69k
    if (error) {
2650
303
        return error;
2651
303
    }
2652
2653
3.38k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->dst.field, NULL,
2654
3.38k
                                     tlv_bitmap);
2655
3.38k
    if (error) {
2656
145
        return error;
2657
145
    }
2658
2659
3.24k
    if (!is_all_zeros(b.data, b.size)) {
2660
35
        return OFPERR_NXBRC_MUST_BE_ZERO;
2661
35
    }
2662
2663
3.20k
    return nxm_reg_move_check(move, NULL);
2664
3.24k
}
2665
2666
static void
2667
encode_REG_MOVE(const struct ofpact_reg_move *move,
2668
                enum ofp_version ofp_version, struct ofpbuf *out)
2669
459
{
2670
    /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2671
     * NXAST_RAW_REG_MOVE is somewhat difficult.  Neither one is guaranteed to
2672
     * be supported by every OpenFlow 1.3 implementation.  It would be ideal to
2673
     * probe for support.  Until we have that ability, we currently prefer
2674
     * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2675
     * versions.  */
2676
459
    size_t start_ofs = out->size;
2677
459
    if (ofp_version >= OFP15_VERSION) {
2678
0
        struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2679
0
        copy->n_bits = htons(move->dst.n_bits);
2680
0
        copy->src_offset = htons(move->src.ofs);
2681
0
        copy->dst_offset = htons(move->dst.ofs);
2682
0
        out->size = out->size - sizeof copy->pad2;
2683
0
        nx_put_mff_header(out, move->src.field, ofp_version, false);
2684
0
        nx_put_mff_header(out, move->dst.field, ofp_version, false);
2685
459
    } else if (ofp_version == OFP13_VERSION
2686
194
               && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2687
0
        struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2688
0
        copy->n_bits = htons(move->dst.n_bits);
2689
0
        copy->src_offset = htons(move->src.ofs);
2690
0
        copy->dst_offset = htons(move->dst.ofs);
2691
0
        out->size = out->size - sizeof copy->pad3;
2692
0
        nx_put_mff_header(out, move->src.field, ofp_version, false);
2693
0
        nx_put_mff_header(out, move->dst.field, ofp_version, false);
2694
459
    } else {
2695
459
        struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2696
459
        narm->n_bits = htons(move->dst.n_bits);
2697
459
        narm->src_ofs = htons(move->src.ofs);
2698
459
        narm->dst_ofs = htons(move->dst.ofs);
2699
459
        nx_put_mff_header(out, move->src.field, 0, false);
2700
459
        nx_put_mff_header(out, move->dst.field, 0, false);
2701
459
    }
2702
459
    pad_ofpat(out, start_ofs);
2703
459
}
2704
2705
static char * OVS_WARN_UNUSED_RESULT
2706
parse_REG_MOVE(const char *arg, const struct ofpact_parse_params *pp)
2707
481
{
2708
481
    struct ofpact_reg_move *move = ofpact_put_REG_MOVE(pp->ofpacts);
2709
481
    return nxm_parse_reg_move(move, arg);
2710
481
}
2711
2712
static void
2713
format_REG_MOVE(const struct ofpact_reg_move *a,
2714
                const struct ofpact_format_params *fp)
2715
334
{
2716
334
    nxm_format_reg_move(a, fp->s);
2717
334
}
2718
2719
static enum ofperr
2720
check_REG_MOVE(const struct ofpact_reg_move *a,
2721
               const struct ofpact_check_params *cp)
2722
704
{
2723
704
    return nxm_reg_move_check(a, cp->match);
2724
704
}
2725

2726
/* Action structure for OFPAT12_SET_FIELD. */
2727
struct ofp12_action_set_field {
2728
    ovs_be16 type;                  /* OFPAT12_SET_FIELD. */
2729
    ovs_be16 len;                   /* Length is padded to 64 bits. */
2730
2731
    /* Followed by:
2732
     * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2733
     * - Enough 0-bytes to pad out to a multiple of 64 bits.
2734
     *
2735
     * The "pad" member is the beginning of the above. */
2736
    uint8_t pad[4];
2737
};
2738
OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2739
2740
/* Action structure for NXAST_REG_LOAD.
2741
 *
2742
 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2743
 * within 'a' numbered 'b' through 'c' (not including bit 'c').  Bit numbering
2744
 * starts at 0 for the least-significant bit, 1 for the next most significant
2745
 * bit, and so on.
2746
 *
2747
 * 'dst' is an nxm_header with nxm_hasmask=0.  See the documentation for
2748
 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2749
 * loading them.
2750
 *
2751
 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2752
 * to avoid enlarging the structure by another 8 bytes.  To allow 'n_bits' to
2753
 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2754
 * also stored as one less than its true value:
2755
 *
2756
 *  15                           6 5                0
2757
 * +------------------------------+------------------+
2758
 * |              ofs             |    n_bits - 1    |
2759
 * +------------------------------+------------------+
2760
 *
2761
 * The switch will reject actions for which ofs+n_bits is greater than the
2762
 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2763
 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2764
 * OFPBAC_BAD_ARGUMENT.
2765
 */
2766
struct nx_action_reg_load {
2767
    ovs_be16 type;                  /* OFPAT_VENDOR. */
2768
    ovs_be16 len;                   /* Length is 24. */
2769
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
2770
    ovs_be16 subtype;               /* NXAST_REG_LOAD. */
2771
    ovs_be16 ofs_nbits;             /* (ofs << 6) | (n_bits - 1). */
2772
    ovs_be32 dst;                   /* Destination register. */
2773
    ovs_be64 value;                 /* Immediate value. */
2774
};
2775
OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2776
2777
/* The NXAST_REG_LOAD2 action structure is "struct ext_action_header",
2778
 * followed by:
2779
 *
2780
 * - An NXM/OXM header, value, and optionally a mask.
2781
 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2782
 *
2783
 * The "pad" member is the beginning of the above. */
2784
2785
static enum ofperr
2786
decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2787
                       bool may_mask, const struct vl_mff_map *vl_mff_map,
2788
                       uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2789
9.53k
{
2790
9.53k
    struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
2791
9.53k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2792
2793
9.53k
    union mf_value value, mask;
2794
9.53k
    const struct mf_field *field;
2795
9.53k
    enum ofperr error;
2796
9.53k
    error  = mf_vl_mff_nx_pull_entry(&b, vl_mff_map, &field, &value,
2797
9.53k
                                     may_mask ? &mask : NULL, tlv_bitmap);
2798
9.53k
    if (error) {
2799
886
        return (error == OFPERR_OFPBMC_BAD_MASK
2800
886
                ? OFPERR_OFPBAC_BAD_SET_MASK
2801
886
                : error);
2802
886
    }
2803
2804
8.65k
    if (!may_mask) {
2805
3.05k
        memset(&mask, 0xff, field->n_bytes);
2806
3.05k
    }
2807
2808
8.65k
    if (!is_all_zeros(b.data, b.size)) {
2809
570
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2810
570
    }
2811
2812
    /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2813
     * Set-Field. */
2814
8.08k
    if (field->id == MFF_IN_PORT_OXM) {
2815
79
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2816
79
    }
2817
2818
    /* oxm_length is now validated to be compatible with mf_value. */
2819
8.00k
    if (!field->writable) {
2820
203
        VLOG_WARN_RL(&rl, "destination field %s is not writable",
2821
203
                     field->name);
2822
203
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2823
203
    }
2824
2825
    /* The value must be valid for match.  OpenFlow 1.5 also says,
2826
     * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2827
     * a 1-bit in oxm_value and in oxm_mask." */
2828
7.79k
    if (!mf_is_value_valid(field, &value)
2829
7.72k
        || (field->id == MFF_VLAN_VID
2830
819
            && (!(mask.be16 & htons(OFPVID12_PRESENT))
2831
855
                || !(value.be16 & htons(OFPVID12_PRESENT))))) {
2832
855
        struct ds ds = DS_EMPTY_INITIALIZER;
2833
855
        mf_format(field, &value, NULL, NULL, &ds);
2834
855
        VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2835
855
                     field->name, ds_cstr(&ds));
2836
855
        ds_destroy(&ds);
2837
2838
855
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2839
855
    }
2840
2841
6.94k
    ofpact_put_set_field(ofpacts, field, &value, &mask);
2842
6.94k
    return 0;
2843
7.79k
}
2844
2845
static enum ofperr
2846
decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2847
                             enum ofp_version ofp_version OVS_UNUSED,
2848
                             const struct vl_mff_map *vl_mff_map,
2849
                             uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2850
3.65k
{
2851
3.65k
    return decode_ofpat_set_field(oasf, false, vl_mff_map, tlv_bitmap,
2852
3.65k
                                  ofpacts);
2853
3.65k
}
2854
2855
static enum ofperr
2856
decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2857
                             enum ofp_version ofp_version OVS_UNUSED,
2858
                             const struct vl_mff_map *vl_mff_map,
2859
                             uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2860
5.87k
{
2861
5.87k
    return decode_ofpat_set_field(oasf, true, vl_mff_map, tlv_bitmap, ofpacts);
2862
5.87k
}
2863
2864
static enum ofperr
2865
decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2866
                          enum ofp_version ofp_version OVS_UNUSED,
2867
                          const struct vl_mff_map *vl_mff_map,
2868
                          uint64_t *tlv_bitmap, struct ofpbuf *out)
2869
3.40k
{
2870
3.40k
    struct mf_subfield dst;
2871
3.40k
    enum ofperr error;
2872
2873
3.40k
    dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2874
3.40k
    dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2875
3.40k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(narl->dst), vl_mff_map,
2876
3.40k
                                         &dst.field, tlv_bitmap);
2877
3.40k
    if (error) {
2878
94
        return error;
2879
94
    }
2880
2881
3.30k
    error = mf_check_dst(&dst, NULL);
2882
3.30k
    if (error) {
2883
138
        return error;
2884
138
    }
2885
2886
    /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2887
     * narl->value. */
2888
3.16k
    if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2889
779
        return OFPERR_OFPBAC_BAD_ARGUMENT;
2890
779
    }
2891
2892
2.39k
    struct ofpact_set_field *sf = ofpact_put_reg_load(out, dst.field, NULL,
2893
2.39k
                                                      NULL);
2894
2.39k
    bitwise_put(ntohll(narl->value),
2895
2.39k
                sf->value, dst.field->n_bytes, dst.ofs,
2896
2.39k
                dst.n_bits);
2897
2.39k
    bitwise_put(UINT64_MAX,
2898
2.39k
                ofpact_set_field_mask(sf), dst.field->n_bytes, dst.ofs,
2899
2.39k
                dst.n_bits);
2900
2.39k
    return 0;
2901
3.16k
}
2902
2903
static enum ofperr
2904
decode_NXAST_RAW_REG_LOAD2(const struct ext_action_header *eah,
2905
                           enum ofp_version ofp_version OVS_UNUSED,
2906
                           const struct vl_mff_map *vl_mff_map,
2907
                           uint64_t *tlv_bitmap, struct ofpbuf *out)
2908
2.68k
{
2909
2.68k
    struct ofpbuf b = ofpbuf_const_initializer(eah, ntohs(eah->len));
2910
2.68k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(eah, pad));
2911
2912
2.68k
    union mf_value value, mask;
2913
2.68k
    const struct mf_field *field;
2914
2.68k
    enum ofperr error;
2915
2.68k
    error = mf_vl_mff_nx_pull_entry(&b, vl_mff_map, &field, &value, &mask,
2916
2.68k
                                    tlv_bitmap);
2917
2.68k
    if (error) {
2918
108
        return error;
2919
108
    }
2920
2921
2.57k
    if (!is_all_zeros(b.data, b.size)) {
2922
18
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2923
18
    }
2924
2925
2.55k
    if (!field->writable) {
2926
664
        VLOG_WARN_RL(&rl, "destination field %s is not writable", field->name);
2927
664
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2928
664
    }
2929
2930
    /* Put value and mask. */
2931
1.89k
    ofpact_put_reg_load2(out, field, &value, &mask);
2932
1.89k
    return 0;
2933
2.55k
}
2934
2935
static void
2936
put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2937
              enum mf_field_id field, uint64_t value_)
2938
5.43k
{
2939
5.43k
    struct ofp12_action_set_field *oasf OVS_UNUSED;
2940
5.43k
    int n_bytes = mf_from_id(field)->n_bytes;
2941
5.43k
    size_t start_ofs = openflow->size;
2942
5.43k
    union mf_value value;
2943
2944
5.43k
    value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2945
2946
5.43k
    oasf = put_OFPAT12_SET_FIELD(openflow);
2947
5.43k
    openflow->size = openflow->size - sizeof oasf->pad;
2948
5.43k
    nx_put_entry(openflow, mf_from_id(field), ofp_version, &value, NULL);
2949
5.43k
    pad_ofpat(openflow, start_ofs);
2950
5.43k
}
2951
2952
static void
2953
put_reg_load(struct ofpbuf *openflow,
2954
             const struct mf_subfield *dst, uint64_t value)
2955
70.5k
{
2956
70.5k
    ovs_assert(dst->n_bits <= 64);
2957
2958
70.5k
    struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2959
70.5k
    narl->ofs_nbits = nxm_encode_ofs_nbits(dst->ofs, dst->n_bits);
2960
70.5k
    narl->dst = htonl(nxm_header_from_mff(dst->field));
2961
70.5k
    narl->value = htonll(value);
2962
70.5k
}
2963
2964
static bool
2965
next_load_segment(const struct ofpact_set_field *sf,
2966
                  struct mf_subfield *dst, uint64_t *value)
2967
83.2k
{
2968
83.2k
    int n_bits = sf->field->n_bits;
2969
83.2k
    int n_bytes = sf->field->n_bytes;
2970
83.2k
    int start = dst->ofs + dst->n_bits;
2971
2972
83.2k
    if (start < n_bits) {
2973
78.0k
        dst->field = sf->field;
2974
78.0k
        dst->ofs = bitwise_scan(ofpact_set_field_mask(sf), n_bytes, 1, start,
2975
78.0k
                                n_bits);
2976
78.0k
        if (dst->ofs < n_bits) {
2977
70.9k
            dst->n_bits = bitwise_scan(ofpact_set_field_mask(sf), n_bytes, 0,
2978
70.9k
                                       dst->ofs + 1,
2979
70.9k
                                       MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2980
70.9k
            *value = bitwise_get(sf->value, n_bytes, dst->ofs, dst->n_bits);
2981
70.9k
            return true;
2982
70.9k
        }
2983
78.0k
    }
2984
12.2k
    return false;
2985
83.2k
}
2986
2987
/* Convert 'sf' to a series of REG_LOADs. */
2988
static void
2989
set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2990
20.9k
{
2991
    /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2992
     * experimenter OXM or is variable length (or if it came in as
2993
     * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2.  Otherwise use
2994
     * NXAST_REG_LOAD, which is backward compatible. */
2995
20.9k
    if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2996
20.9k
        || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2997
10.5k
        struct ext_action_header *eah OVS_UNUSED;
2998
10.5k
        size_t start_ofs = openflow->size;
2999
3000
10.5k
        eah = put_NXAST_REG_LOAD2(openflow);
3001
10.5k
        openflow->size = openflow->size - sizeof eah->pad;
3002
10.5k
        nx_put_entry(openflow, sf->field, 0, sf->value,
3003
10.5k
                     ofpact_set_field_mask(sf));
3004
10.5k
        pad_ofpat(openflow, start_ofs);
3005
10.5k
    } else {
3006
10.3k
        struct mf_subfield dst;
3007
10.3k
        uint64_t value;
3008
3009
10.3k
        dst.ofs = dst.n_bits = 0;
3010
79.3k
        while (next_load_segment(sf, &dst, &value)) {
3011
69.0k
            put_reg_load(openflow, &dst, value);
3012
69.0k
        }
3013
10.3k
    }
3014
20.9k
}
3015
3016
/* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
3017
 * actions, if we can, falling back to Nicira extensions if we must.
3018
 *
3019
 * We check only meta-flow types that can appear within set field actions and
3020
 * that have a mapping to compatible action types.  These struct mf_field
3021
 * definitions have a defined OXM or NXM header value and specify the field as
3022
 * writable. */
3023
static void
3024
set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
3025
                             enum ofp_version ofp_version,
3026
                             struct ofpbuf *out)
3027
21.0k
{
3028
21.0k
    switch ((int) sf->field->id) {
3029
1.32k
    case MFF_VLAN_TCI: {
3030
1.32k
        ovs_be16 tci = sf->value->be16;
3031
1.32k
        bool cfi = (tci & htons(VLAN_CFI)) != 0;
3032
1.32k
        uint16_t vid = vlan_tci_to_vid(tci);
3033
1.32k
        uint8_t pcp = vlan_tci_to_pcp(tci);
3034
3035
1.32k
        if (ofp_version < OFP11_VERSION) {
3036
            /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
3037
             *
3038
             * If CFI=1, Add or modify VLAN VID & PCP.
3039
             * If CFI=0, strip VLAN header, if any.
3040
             */
3041
421
            if (cfi) {
3042
155
                put_OFPAT10_SET_VLAN_VID(out, vid);
3043
155
                put_OFPAT10_SET_VLAN_PCP(out, pcp);
3044
266
            } else {
3045
266
                put_OFPAT10_STRIP_VLAN(out);
3046
266
            }
3047
905
        } else {
3048
            /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
3049
             *
3050
             * If CFI=1, Add or modify VLAN VID & PCP.
3051
             *    OpenFlow 1.1 set actions only apply if the packet
3052
             *    already has VLAN tags.  To be sure that is the case
3053
             *    we have to push a VLAN header.  As we do not support
3054
             *    multiple layers of VLANs, this is a no-op, if a VLAN
3055
             *    header already exists.  This may backfire, however,
3056
             *    when we start supporting multiple layers of VLANs.
3057
             * If CFI=0, strip VLAN header, if any.
3058
             */
3059
905
            if (cfi) {
3060
                /* Push a VLAN tag, if one was not seen at action validation
3061
                 * time. */
3062
496
                if (!sf->flow_has_vlan) {
3063
269
                    put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
3064
269
                }
3065
496
                put_OFPAT11_SET_VLAN_VID(out, vid);
3066
496
                put_OFPAT11_SET_VLAN_PCP(out, pcp);
3067
496
            } else {
3068
                /* If the flow did not match on vlan, we have no way of
3069
                 * knowing if the vlan tag exists, so we must POP just to be
3070
                 * sure. */
3071
409
                put_OFPAT11_POP_VLAN(out);
3072
409
            }
3073
905
        }
3074
1.32k
        break;
3075
0
    }
3076
3077
357
    case MFF_VLAN_VID: {
3078
357
        uint16_t vid = ntohs(sf->value->be16) & VLAN_VID_MASK;
3079
357
        if (ofp_version == OFP10_VERSION) {
3080
163
            put_OFPAT10_SET_VLAN_VID(out, vid);
3081
194
        } else {
3082
194
            put_OFPAT11_SET_VLAN_VID(out, vid);
3083
194
        }
3084
357
        break;
3085
0
    }
3086
3087
389
    case MFF_VLAN_PCP:
3088
389
        if (ofp_version == OFP10_VERSION) {
3089
195
            put_OFPAT10_SET_VLAN_PCP(out, sf->value->u8);
3090
195
        } else {
3091
194
            put_OFPAT11_SET_VLAN_PCP(out, sf->value->u8);
3092
194
        }
3093
389
        break;
3094
3095
195
    case MFF_ETH_SRC:
3096
195
        put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value->mac;
3097
195
        break;
3098
3099
66
    case MFF_ETH_DST:
3100
66
        put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value->mac;
3101
66
        break;
3102
3103
196
    case MFF_IPV4_SRC:
3104
196
        put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value->be32);
3105
196
        break;
3106
3107
194
    case MFF_IPV4_DST:
3108
194
        put_OFPAT_SET_NW_DST(out, ofp_version, sf->value->be32);
3109
194
        break;
3110
3111
226
    case MFF_IP_DSCP:
3112
226
        put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value->u8);
3113
226
        break;
3114
3115
196
    case MFF_IP_DSCP_SHIFTED:
3116
196
        put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value->u8 << 2);
3117
196
        break;
3118
3119
69
    case MFF_IP_ECN: {
3120
69
        struct ofpact_ecn ip_ecn = { .ecn = sf->value->u8 };
3121
69
        encode_SET_IP_ECN(&ip_ecn, ofp_version, out);
3122
69
        break;
3123
0
    }
3124
3125
194
    case MFF_TCP_SRC:
3126
467
    case MFF_UDP_SRC:
3127
467
        put_OFPAT_SET_TP_SRC(out, sf->value->be16);
3128
467
        break;
3129
3130
302
    case MFF_TCP_DST:
3131
376
    case MFF_UDP_DST:
3132
376
        put_OFPAT_SET_TP_DST(out, sf->value->be16);
3133
376
        break;
3134
3135
16.9k
    default:
3136
16.9k
        set_field_to_nxast(sf, out);
3137
16.9k
        break;
3138
21.0k
    }
3139
21.0k
}
3140
3141
static void
3142
set_field_to_set_field(const struct ofpact_set_field *sf,
3143
                       enum ofp_version ofp_version, struct ofpbuf *out)
3144
499
{
3145
499
    struct ofp12_action_set_field *oasf OVS_UNUSED;
3146
499
    size_t start_ofs = out->size;
3147
3148
499
    oasf = put_OFPAT12_SET_FIELD(out);
3149
499
    out->size = out->size - sizeof oasf->pad;
3150
499
    nx_put_entry(out, sf->field, ofp_version, sf->value,
3151
499
                 ofpact_set_field_mask(sf));
3152
499
    pad_ofpat(out, start_ofs);
3153
499
}
3154
3155
static void
3156
encode_SET_FIELD(const struct ofpact_set_field *sf,
3157
                 enum ofp_version ofp_version, struct ofpbuf *out)
3158
25.4k
{
3159
25.4k
    if (ofp_version >= OFP15_VERSION) {
3160
        /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
3161
         * entirely). */
3162
0
        set_field_to_set_field(sf, ofp_version, out);
3163
25.4k
    } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
3164
24.9k
               sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
3165
        /* It came in as reg_load, send it out the same way. */
3166
542
        set_field_to_nxast(sf, out);
3167
24.9k
    } else if (ofp_version < OFP12_VERSION) {
3168
        /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
3169
21.0k
        set_field_to_legacy_openflow(sf, ofp_version, out);
3170
21.0k
    } else if (is_all_ones(ofpact_set_field_mask(sf), sf->field->n_bytes)) {
3171
        /* We're encoding to OpenFlow 1.2, 1.3, or 1.4.  The action sets an
3172
         * entire field, so encode it as OFPAT_SET_FIELD. */
3173
499
        set_field_to_set_field(sf, ofp_version, out);
3174
3.40k
    } else {
3175
        /* We're encoding to OpenFlow 1.2, 1.3, or 1.4.  The action cannot be
3176
         * encoded as OFPAT_SET_FIELD because it does not set an entire field,
3177
         * so encode it as reg_load. */
3178
3.40k
        set_field_to_nxast(sf, out);
3179
3.40k
    }
3180
25.4k
}
3181
3182
/* Parses the input argument 'arg' into the key, value, and delimiter
3183
 * components that are common across the reg_load and set_field action format.
3184
 *
3185
 * With an argument like "1->metadata", sets the following pointers to
3186
 * point within 'arg':
3187
 * key: "metadata"
3188
 * value: "1"
3189
 * delim: "->"
3190
 *
3191
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3192
 * error.  The caller is responsible for freeing the returned string. */
3193
static char * OVS_WARN_UNUSED_RESULT
3194
set_field_split_str(char *arg, char **key, char **value, char **delim)
3195
49.2k
{
3196
49.2k
    char *value_end;
3197
3198
49.2k
    *key = NULL;
3199
49.2k
    *value = arg;
3200
49.2k
    if (delim) {
3201
48.5k
        *delim = NULL;
3202
48.5k
    }
3203
3204
49.2k
    value_end = strstr(arg, "->");
3205
49.2k
    if (!value_end) {
3206
17
        return xasprintf("%s: missing `->'", arg);
3207
17
    }
3208
3209
49.2k
    *key = value_end + strlen("->");
3210
49.2k
    if (delim) {
3211
48.5k
        *delim = value_end;
3212
48.5k
    }
3213
49.2k
    if (strlen(value_end) <= strlen("->")) {
3214
3
        return xasprintf("%s: missing field name following `->'", arg);
3215
3
    }
3216
3217
49.2k
    return NULL;
3218
49.2k
}
3219
3220
/* Parses a "set_field" action with argument 'arg', appending the parsed
3221
 * action to 'pp->ofpacts'.
3222
 *
3223
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3224
 * error.  The caller is responsible for freeing the returned string. */
3225
static char * OVS_WARN_UNUSED_RESULT
3226
set_field_parse__(char *arg, const struct ofpact_parse_params *pp)
3227
48.1k
{
3228
48.1k
    char *value;
3229
48.1k
    char *delim;
3230
48.1k
    char *key;
3231
48.1k
    const struct mf_field *mf;
3232
48.1k
    union mf_value sf_value, sf_mask;
3233
48.1k
    char *error;
3234
3235
48.1k
    error = set_field_split_str(arg, &key, &value, &delim);
3236
48.1k
    if (error) {
3237
13
        return error;
3238
13
    }
3239
3240
48.1k
    mf = mf_from_name(key);
3241
48.1k
    if (!mf) {
3242
58
        return xasprintf("%s is not a valid OXM field name", key);
3243
58
    }
3244
48.0k
    if (!mf->writable) {
3245
1
        return xasprintf("%s is read-only", key);
3246
1
    }
3247
3248
48.0k
    delim[0] = '\0';
3249
48.0k
    error = mf_parse(mf, value, pp->port_map, &sf_value, &sf_mask);
3250
48.0k
    if (error) {
3251
7
        return error;
3252
7
    }
3253
3254
48.0k
    if (!mf_is_value_valid(mf, &sf_value)) {
3255
6
        return xasprintf("%s is not a valid value for field %s", value, key);
3256
6
    }
3257
3258
48.0k
    *pp->usable_protocols &= mf->usable_protocols_exact;
3259
3260
48.0k
    ofpact_put_set_field(pp->ofpacts, mf, &sf_value, &sf_mask);
3261
48.0k
    return NULL;
3262
48.0k
}
3263
3264
/* Parses 'arg' as the argument to a "set_field" action, and appends such an
3265
 * action to 'pp->ofpacts'.
3266
 *
3267
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3268
 * error.  The caller is responsible for freeing the returned string. */
3269
static char * OVS_WARN_UNUSED_RESULT
3270
parse_SET_FIELD(const char *arg, const struct ofpact_parse_params *pp)
3271
48.1k
{
3272
48.1k
    char *copy = xstrdup(arg);
3273
48.1k
    char *error = set_field_parse__(copy, pp);
3274
48.1k
    free(copy);
3275
48.1k
    return error;
3276
48.1k
}
3277
3278
static char * OVS_WARN_UNUSED_RESULT
3279
parse_reg_load(char *arg, const struct ofpact_parse_params *pp)
3280
660
{
3281
660
    struct mf_subfield dst;
3282
660
    char *key, *value_str;
3283
660
    union mf_value value;
3284
660
    char *error;
3285
3286
660
    error = set_field_split_str(arg, &key, &value_str, NULL);
3287
660
    if (error) {
3288
3
        return error;
3289
3
    }
3290
3291
657
    error = mf_parse_subfield(&dst, key);
3292
657
    if (error) {
3293
8
        return error;
3294
8
    }
3295
3296
649
    if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
3297
649
                         &key)) {
3298
1
        return xasprintf("%s: cannot parse integer value", arg);
3299
1
    }
3300
3301
648
    if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
3302
648
                              dst.field->n_bytes * 8 - dst.n_bits)) {
3303
1
        struct ds ds;
3304
3305
1
        ds_init(&ds);
3306
1
        mf_format(dst.field, &value, NULL, NULL, &ds);
3307
1
        error = xasprintf("%s: value %s does not fit into %d bits",
3308
1
                          arg, ds_cstr(&ds), dst.n_bits);
3309
1
        ds_destroy(&ds);
3310
1
        return error;
3311
1
    }
3312
3313
647
    struct ofpact_set_field *sf = ofpact_put_reg_load(pp->ofpacts, dst.field,
3314
647
                                                      NULL, NULL);
3315
3316
647
    bitwise_copy(&value, dst.field->n_bytes, 0, sf->value,
3317
647
                 dst.field->n_bytes, dst.ofs, dst.n_bits);
3318
647
    bitwise_one(ofpact_set_field_mask(sf), dst.field->n_bytes, dst.ofs,
3319
647
                dst.n_bits);
3320
647
    return NULL;
3321
648
}
3322
3323
static void
3324
format_SET_FIELD(const struct ofpact_set_field *a,
3325
                 const struct ofpact_format_params *fp)
3326
6.38k
{
3327
6.38k
    if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
3328
1.94k
        struct mf_subfield dst;
3329
1.94k
        uint64_t value;
3330
3331
1.94k
        dst.ofs = dst.n_bits = 0;
3332
3.89k
        while (next_load_segment(a, &dst, &value)) {
3333
1.94k
            ds_put_format(fp->s, "%sload:%s%#"PRIx64"%s->%s",
3334
1.94k
                          colors.special, colors.end, value,
3335
1.94k
                          colors.special, colors.end);
3336
1.94k
            mf_format_subfield(&dst, fp->s);
3337
1.94k
            ds_put_char(fp->s, ',');
3338
1.94k
        }
3339
1.94k
        ds_chomp(fp->s, ',');
3340
4.43k
    } else {
3341
4.43k
        ds_put_format(fp->s, "%sset_field:%s", colors.special, colors.end);
3342
4.43k
        mf_format(a->field, a->value, ofpact_set_field_mask(a),
3343
4.43k
                  fp->port_map, fp->s);
3344
4.43k
        ds_put_format(fp->s, "%s->%s%s",
3345
4.43k
                      colors.special, colors.end, a->field->name);
3346
4.43k
    }
3347
6.38k
}
3348
3349
static enum ofperr
3350
check_SET_FIELD(struct ofpact_set_field *a,
3351
                const struct ofpact_check_params *cp)
3352
27.3k
{
3353
27.3k
    const struct mf_field *mf = a->field;
3354
27.3k
    struct flow *flow = &cp->match->flow;
3355
27.3k
    ovs_be16 *tci = &flow->vlans[0].tci;
3356
3357
    /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
3358
27.3k
    if (!mf_are_prereqs_ok(mf, flow, NULL)
3359
26.8k
        || (mf->id == MFF_VLAN_VID && !(*tci & htons(VLAN_CFI)))) {
3360
528
        VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisites",
3361
528
                     mf->name);
3362
528
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
3363
528
    }
3364
3365
    /* Remember if we saw a VLAN tag in the flow, to aid translating to
3366
     * OpenFlow 1.1 if need be. */
3367
26.7k
    a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
3368
26.7k
    if (mf->id == MFF_VLAN_TCI) {
3369
        /* The set field may add or remove the VLAN tag,
3370
         * Mark the status temporarily. */
3371
1.36k
        *tci = a->value->be16;
3372
1.36k
    }
3373
3374
26.7k
    return 0;
3375
27.3k
}
3376
3377
/* Appends an OFPACT_SET_FIELD ofpact with enough space for the value and a
3378
 * properly aligned mask for the 'field' to 'ofpacts' and returns it.  Fills
3379
 * in the value from 'value', if non-NULL, and mask from 'mask' if non-NULL.
3380
 * If 'value' is non-NULL and 'mask' is NULL, an all-ones mask will be
3381
 * filled in. */
3382
struct ofpact_set_field *
3383
ofpact_put_set_field(struct ofpbuf *ofpacts, const struct mf_field *field,
3384
                     const void *value, const void *mask)
3385
59.9k
{
3386
    /* Ensure there's enough space for:
3387
     * - value (8-byte aligned)
3388
     * - mask  (8-byte aligned)
3389
     * - padding (to make the whole ofpact_set_field 8-byte aligned)
3390
     */
3391
59.9k
    size_t total_size = 2 * ROUND_UP(field->n_bytes, OFPACT_ALIGNTO);
3392
59.9k
    struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
3393
59.9k
    sf->field = field;
3394
3395
    /* Fill with all zeroes to make sure the padding is initialized. */
3396
59.9k
    ofpbuf_put_zeros(ofpacts, total_size);
3397
59.9k
    sf = ofpacts->header;
3398
3399
    /* Fill in the value and mask if given, otherwise keep the zeroes
3400
     * so that the caller may fill in the value and mask itself. */
3401
59.9k
    if (value) {
3402
56.8k
        memcpy(sf->value, value, field->n_bytes);
3403
56.8k
        if (mask) {
3404
56.8k
            memcpy(ofpact_set_field_mask(sf), mask, field->n_bytes);
3405
56.8k
        } else {
3406
0
            memset(ofpact_set_field_mask(sf), 0xff, field->n_bytes);
3407
0
        }
3408
56.8k
    }
3409
3410
    /* Update length. */
3411
59.9k
    ofpact_finish_SET_FIELD(ofpacts, &sf);
3412
3413
59.9k
    return sf;
3414
59.9k
}
3415
3416
/* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it.  The ofpact
3417
 * is marked such that, if possible, it will be translated to OpenFlow as
3418
 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
3419
 * that was the way that the action was expressed when it came into OVS or for
3420
 * backward compatibility. */
3421
struct ofpact_set_field *
3422
ofpact_put_reg_load(struct ofpbuf *ofpacts, const struct mf_field *field,
3423
                    const void *value, const void *mask)
3424
3.03k
{
3425
3.03k
    struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts, field, value,
3426
3.03k
                                                       mask);
3427
3.03k
    sf->ofpact.raw = NXAST_RAW_REG_LOAD;
3428
3429
3.03k
    return sf;
3430
3.03k
}
3431
3432
struct ofpact_set_field *
3433
ofpact_put_reg_load2(struct ofpbuf *ofpacts, const struct mf_field *field,
3434
                     const void *value, const void *mask)
3435
1.89k
{
3436
1.89k
    struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts, field, value,
3437
1.89k
                                                       mask);
3438
1.89k
    sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
3439
3440
1.89k
    return sf;
3441
1.89k
}
3442
3443

3444
/* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
3445
 *
3446
 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
3447
 * top of the stack.
3448
 */
3449
struct nx_action_stack {
3450
    ovs_be16 type;                  /* OFPAT_VENDOR. */
3451
    ovs_be16 len;                   /* Length is 16. */
3452
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
3453
    ovs_be16 subtype;               /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
3454
    ovs_be16 offset;                /* Bit offset into the field. */
3455
    /* Followed by:
3456
     * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3457
     * - ovs_be16 'n_bits', the number of bits to extract from the field.
3458
     * - Enough 0-bytes to pad out the action to 24 bytes. */
3459
    uint8_t pad[12];                /* See above. */
3460
};
3461
OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
3462
3463
static enum ofperr
3464
decode_stack_action(const struct nx_action_stack *nasp,
3465
                    const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
3466
                    struct ofpact_stack *stack_action)
3467
1.17k
{
3468
1.17k
    enum ofperr error;
3469
1.17k
    stack_action->subfield.ofs = ntohs(nasp->offset);
3470
3471
1.17k
    struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
3472
1.17k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
3473
1.17k
    error  = mf_vl_mff_nx_pull_header(&b, vl_mff_map,
3474
1.17k
                                      &stack_action->subfield.field, NULL,
3475
1.17k
                                      tlv_bitmap);
3476
1.17k
    if (error) {
3477
210
        return error;
3478
210
    }
3479
3480
963
    stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
3481
963
    ofpbuf_pull(&b, 2);
3482
963
    if (!is_all_zeros(b.data, b.size)) {
3483
154
        return OFPERR_NXBRC_MUST_BE_ZERO;
3484
154
    }
3485
3486
809
    return 0;
3487
963
}
3488
3489
static enum ofperr
3490
decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
3491
                            enum ofp_version ofp_version OVS_UNUSED,
3492
                            const struct vl_mff_map *vl_mff_map,
3493
                            uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
3494
462
{
3495
462
    struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
3496
462
    enum ofperr error = decode_stack_action(nasp, vl_mff_map, tlv_bitmap,
3497
462
                                            push);
3498
462
    return error ? error : nxm_stack_push_check(push, NULL);
3499
462
}
3500
3501
static enum ofperr
3502
decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
3503
                           enum ofp_version ofp_version OVS_UNUSED,
3504
                           const struct vl_mff_map *vl_mff_map,
3505
                           uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
3506
711
{
3507
711
    struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
3508
711
    enum ofperr error = decode_stack_action(nasp, vl_mff_map, tlv_bitmap,
3509
711
                                            pop);
3510
711
    return error ? error : nxm_stack_pop_check(pop, NULL);
3511
711
}
3512
3513
static void
3514
encode_STACK_op(const struct ofpact_stack *stack_action,
3515
                struct nx_action_stack *nasp)
3516
2.34k
{
3517
2.34k
    struct ofpbuf b;
3518
2.34k
    ovs_be16 n_bits;
3519
3520
2.34k
    nasp->offset = htons(stack_action->subfield.ofs);
3521
3522
2.34k
    ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
3523
2.34k
    ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
3524
2.34k
    nx_put_mff_header(&b, stack_action->subfield.field, 0, false);
3525
2.34k
    n_bits = htons(stack_action->subfield.n_bits);
3526
2.34k
    ofpbuf_put(&b, &n_bits, sizeof n_bits);
3527
2.34k
}
3528
3529
static void
3530
encode_STACK_PUSH(const struct ofpact_stack *stack,
3531
                  enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3532
474
{
3533
474
    encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
3534
474
}
3535
3536
static void
3537
encode_STACK_POP(const struct ofpact_stack *stack,
3538
                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3539
1.87k
{
3540
1.87k
    encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3541
1.87k
}
3542
3543
static char * OVS_WARN_UNUSED_RESULT
3544
parse_STACK_PUSH(char *arg, const struct ofpact_parse_params *pp)
3545
496
{
3546
496
    return nxm_parse_stack_action(ofpact_put_STACK_PUSH(pp->ofpacts), arg);
3547
496
}
3548
3549
static char * OVS_WARN_UNUSED_RESULT
3550
parse_STACK_POP(char *arg, const struct ofpact_parse_params *pp)
3551
2.12k
{
3552
2.12k
    return nxm_parse_stack_action(ofpact_put_STACK_POP(pp->ofpacts), arg);
3553
2.12k
}
3554
3555
static void
3556
format_STACK_PUSH(const struct ofpact_stack *a,
3557
                  const struct ofpact_format_params *fp)
3558
6
{
3559
6
    nxm_format_stack_push(a, fp->s);
3560
6
}
3561
3562
static void
3563
format_STACK_POP(const struct ofpact_stack *a,
3564
                 const struct ofpact_format_params *fp)
3565
261
{
3566
261
    nxm_format_stack_pop(a, fp->s);
3567
261
}
3568
3569
static enum ofperr
3570
check_STACK_PUSH(const struct ofpact_stack *a,
3571
                 const struct ofpact_check_params *cp)
3572
555
{
3573
555
    return nxm_stack_push_check(a, cp->match);
3574
555
}
3575
3576
static enum ofperr
3577
check_STACK_POP(const struct ofpact_stack *a,
3578
                const struct ofpact_check_params *cp)
3579
2.41k
{
3580
2.41k
    return nxm_stack_pop_check(a, cp->match);
3581
2.41k
}
3582

3583
/* Action structure for NXAST_DEC_TTL_CNT_IDS.
3584
 *
3585
 * If the packet is not IPv4 or IPv6, does nothing.  For IPv4 or IPv6, if the
3586
 * TTL or hop limit is at least 2, decrements it by 1.  Otherwise, if TTL or
3587
 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3588
 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3589
 *
3590
 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3591
 * sent only to controllers with id 0.)
3592
 */
3593
struct nx_action_cnt_ids {
3594
    ovs_be16 type;              /* OFPAT_VENDOR. */
3595
    ovs_be16 len;               /* Length including cnt_ids. */
3596
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
3597
    ovs_be16 subtype;           /* NXAST_DEC_TTL_CNT_IDS. */
3598
3599
    ovs_be16 n_controllers;     /* Number of controllers. */
3600
    uint8_t zeros[4];           /* Must be zero. */
3601
3602
    /* Followed by 1 or more controller ids:
3603
     *
3604
     * uint16_t cnt_ids[];      -- Controller ids.
3605
     * uint8_t pad[];           -- Must be 0 to 8-byte align cnt_ids[].
3606
     */
3607
};
3608
OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3609
3610
static enum ofperr
3611
decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3612
2.80k
{
3613
2.80k
    uint16_t id = 0;
3614
2.80k
    struct ofpact_cnt_ids *ids;
3615
2.80k
    enum ofperr error = 0;
3616
3617
2.80k
    ids = ofpact_put_DEC_TTL(out);
3618
2.80k
    ids->n_controllers = 1;
3619
2.80k
    ofpbuf_put(out, &id, sizeof id);
3620
2.80k
    ids = out->header;
3621
2.80k
    ofpact_finish_DEC_TTL(out, &ids);
3622
2.80k
    return error;
3623
2.80k
}
3624
3625
static enum ofperr
3626
decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
3627
                                 enum ofp_version ofp_version OVS_UNUSED,
3628
                                 struct ofpbuf *out)
3629
2.96k
{
3630
2.96k
    struct ofpact_cnt_ids *ids;
3631
2.96k
    size_t ids_size;
3632
2.96k
    int i;
3633
3634
2.96k
    ids = ofpact_put_DEC_TTL(out);
3635
2.96k
    ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3636
2.96k
    ids->n_controllers = ntohs(nac_ids->n_controllers);
3637
2.96k
    ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3638
3639
2.96k
    if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3640
343
        return OFPERR_NXBRC_MUST_BE_ZERO;
3641
343
    }
3642
3643
2.62k
    if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3644
249
        VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3645
249
                     "bytes allocated for controller ids.  %"PRIuSIZE" bytes "
3646
249
                     "are required for %u controllers.",
3647
249
                     ids_size, ids->n_controllers * sizeof(ovs_be16),
3648
249
                     ids->n_controllers);
3649
249
        return OFPERR_OFPBAC_BAD_LEN;
3650
249
    }
3651
3652
7.89k
    for (i = 0; i < ids->n_controllers; i++) {
3653
5.52k
        uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3654
5.52k
        ofpbuf_put(out, &id, sizeof id);
3655
5.52k
        ids = out->header;
3656
5.52k
    }
3657
3658
2.37k
    ofpact_finish_DEC_TTL(out, &ids);
3659
3660
2.37k
    return 0;
3661
2.62k
}
3662
3663
static void
3664
encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3665
               enum ofp_version ofp_version, struct ofpbuf *out)
3666
2.13k
{
3667
2.13k
    if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3668
665
        || dec_ttl->n_controllers != 1
3669
1.46k
        || dec_ttl->cnt_ids[0] != 0) {
3670
1.46k
        struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3671
1.46k
        int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3672
1.46k
        ovs_be16 *ids;
3673
1.46k
        size_t i;
3674
3675
1.46k
        nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3676
1.46k
        nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3677
3678
1.46k
        ids = ofpbuf_put_zeros(out, ids_len);
3679
535k
        for (i = 0; i < dec_ttl->n_controllers; i++) {
3680
534k
            ids[i] = htons(dec_ttl->cnt_ids[i]);
3681
534k
        }
3682
1.46k
    } else {
3683
665
        put_OFPAT_DEC_NW_TTL(out, ofp_version);
3684
665
    }
3685
2.13k
}
3686
3687
static void
3688
parse_noargs_dec_ttl(const struct ofpact_parse_params *pp)
3689
670
{
3690
670
    struct ofpact_cnt_ids *ids;
3691
670
    uint16_t id = 0;
3692
3693
670
    ofpact_put_DEC_TTL(pp->ofpacts);
3694
670
    ofpbuf_put(pp->ofpacts, &id, sizeof id);
3695
670
    ids = pp->ofpacts->header;
3696
670
    ids->n_controllers++;
3697
670
    ofpact_finish_DEC_TTL(pp->ofpacts, &ids);
3698
670
}
3699
3700
static char * OVS_WARN_UNUSED_RESULT
3701
parse_DEC_TTL(char *arg, const struct ofpact_parse_params *pp)
3702
2.64k
{
3703
2.64k
    if (*arg == '\0') {
3704
670
        parse_noargs_dec_ttl(pp);
3705
1.97k
    } else {
3706
1.97k
        struct ofpact_cnt_ids *ids;
3707
1.97k
        char *cntr;
3708
3709
1.97k
        ids = ofpact_put_DEC_TTL(pp->ofpacts);
3710
1.97k
        ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3711
951k
        for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3712
949k
             cntr = strtok_r(NULL, ", ", &arg)) {
3713
949k
            uint16_t id = atoi(cntr);
3714
3715
949k
            ofpbuf_put(pp->ofpacts, &id, sizeof id);
3716
949k
            ids = pp->ofpacts->header;
3717
949k
            ids->n_controllers++;
3718
949k
        }
3719
1.97k
        if (!ids->n_controllers) {
3720
1
            return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3721
1
                           "id.");
3722
1
        }
3723
3724
1.97k
        if (ofpbuf_oversized(pp->ofpacts)) {
3725
4
            return xasprintf("input too big");
3726
4
        }
3727
3728
1.96k
        ofpact_finish_DEC_TTL(pp->ofpacts, &ids);
3729
1.96k
    }
3730
2.63k
    return NULL;
3731
2.64k
}
3732
3733
static void
3734
format_DEC_TTL(const struct ofpact_cnt_ids *a,
3735
               const struct ofpact_format_params *fp)
3736
3.12k
{
3737
3.12k
    size_t i;
3738
3739
3.12k
    ds_put_format(fp->s, "%sdec_ttl%s", colors.paren, colors.end);
3740
3.12k
    if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3741
986
        ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
3742
3.75k
        for (i = 0; i < a->n_controllers; i++) {
3743
2.77k
            if (i) {
3744
1.85k
                ds_put_cstr(fp->s, ",");
3745
1.85k
            }
3746
2.77k
            ds_put_format(fp->s, "%"PRIu16, a->cnt_ids[i]);
3747
2.77k
        }
3748
986
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
3749
986
    }
3750
3.12k
}
3751
3752
static enum ofperr
3753
check_DEC_TTL(const struct ofpact_cnt_ids *a OVS_UNUSED,
3754
              struct ofpact_check_params *cp)
3755
4.34k
{
3756
4.34k
    return check_set_ip(cp);
3757
4.34k
}
3758

3759
/* Set MPLS label actions. */
3760
3761
static enum ofperr
3762
decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3763
                                enum ofp_version ofp_version OVS_UNUSED,
3764
                                struct ofpbuf *out)
3765
2.17k
{
3766
2.17k
    ofpact_put_SET_MPLS_LABEL(out)->label = label;
3767
2.17k
    return 0;
3768
2.17k
}
3769
3770
static void
3771
encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3772
                      enum ofp_version ofp_version,
3773
                                  struct ofpbuf *out)
3774
2.68k
{
3775
2.68k
    if (ofp_version < OFP12_VERSION) {
3776
880
        put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3777
1.80k
    } else {
3778
1.80k
        put_set_field(out, ofp_version, MFF_MPLS_LABEL, ntohl(label->label));
3779
1.80k
    }
3780
2.68k
}
3781
3782
static char * OVS_WARN_UNUSED_RESULT
3783
parse_SET_MPLS_LABEL(char *arg, const struct ofpact_parse_params *pp)
3784
2.97k
{
3785
2.97k
    struct ofpact_mpls_label *mpls_label
3786
2.97k
        = ofpact_put_SET_MPLS_LABEL(pp->ofpacts);
3787
2.97k
    uint32_t label;
3788
2.97k
    char *error;
3789
3790
2.97k
    if (*arg == '\0') {
3791
3
        return xstrdup("set_mpls_label: expected label.");
3792
3
    }
3793
3794
2.97k
    error = str_to_u32(arg, &label);
3795
2.97k
    if (error) {
3796
1
        return error;
3797
1
    }
3798
3799
2.97k
    if (label & ~0xfffff) {
3800
13
        return xasprintf("%s: not a valid MPLS label", arg);
3801
13
    }
3802
2.96k
    mpls_label->label = htonl(label);
3803
2.96k
    return NULL;
3804
2.97k
}
3805
3806
static void
3807
format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a,
3808
                      const struct ofpact_format_params *fp)
3809
418
{
3810
418
    ds_put_format(fp->s, "%sset_mpls_label(%s%"PRIu32"%s)%s",
3811
418
                  colors.paren, colors.end, ntohl(a->label),
3812
418
                  colors.paren, colors.end);
3813
418
}
3814
3815
static enum ofperr
3816
check_set_mpls(struct ofpact_check_params *cp)
3817
14.8k
{
3818
14.8k
    ovs_be16 dl_type = get_dl_type(&cp->match->flow);
3819
14.8k
    if (!eth_type_mpls(dl_type)) {
3820
7.69k
        inconsistent_match(&cp->usable_protocols);
3821
7.69k
    }
3822
14.8k
    return 0;
3823
14.8k
}
3824
3825
static enum ofperr
3826
check_SET_MPLS_LABEL(const struct ofpact_mpls_label *a OVS_UNUSED,
3827
                     struct ofpact_check_params *cp)
3828
5.03k
{
3829
5.03k
    return check_set_mpls(cp);
3830
5.03k
}
3831

3832
/* Set MPLS TC actions. */
3833
3834
static enum ofperr
3835
decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3836
                             enum ofp_version ofp_version OVS_UNUSED,
3837
                             struct ofpbuf *out)
3838
572
{
3839
572
    ofpact_put_SET_MPLS_TC(out)->tc = tc;
3840
572
    return 0;
3841
572
}
3842
3843
static void
3844
encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3845
                   enum ofp_version ofp_version, struct ofpbuf *out)
3846
999
{
3847
999
    if (ofp_version < OFP12_VERSION) {
3848
753
        put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3849
753
    } else {
3850
246
        put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3851
246
    }
3852
999
}
3853
3854
static char * OVS_WARN_UNUSED_RESULT
3855
parse_SET_MPLS_TC(char *arg, const struct ofpact_parse_params *pp)
3856
2.90k
{
3857
2.90k
    struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(pp->ofpacts);
3858
2.90k
    uint8_t tc;
3859
2.90k
    char *error;
3860
3861
2.90k
    if (*arg == '\0') {
3862
3
        return xstrdup("set_mpls_tc: expected tc.");
3863
3
    }
3864
3865
2.90k
    error = str_to_u8(arg, "MPLS TC", &tc);
3866
2.90k
    if (error) {
3867
2
        return error;
3868
2
    }
3869
3870
2.89k
    if (tc & ~7) {
3871
3
        return xasprintf("%s: not a valid MPLS TC", arg);
3872
3
    }
3873
2.89k
    mpls_tc->tc = tc;
3874
2.89k
    return NULL;
3875
2.89k
}
3876
3877
static void
3878
format_SET_MPLS_TC(const struct ofpact_mpls_tc *a,
3879
                   const struct ofpact_format_params *fp)
3880
544
{
3881
544
    ds_put_format(fp->s, "%sset_mpls_tc(%s%"PRIu8"%s)%s",
3882
544
                  colors.paren, colors.end, a->tc,
3883
544
                  colors.paren, colors.end);
3884
544
}
3885
3886
static enum ofperr
3887
check_SET_MPLS_TC(const struct ofpact_mpls_tc *a OVS_UNUSED,
3888
                  struct ofpact_check_params *cp)
3889
1.66k
{
3890
1.66k
    return check_set_mpls(cp);
3891
1.66k
}
3892

3893
/* Set MPLS TTL actions. */
3894
3895
static enum ofperr
3896
decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3897
                              enum ofp_version ofp_version OVS_UNUSED,
3898
                              struct ofpbuf *out)
3899
1.94k
{
3900
1.94k
    ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3901
1.94k
    return 0;
3902
1.94k
}
3903
3904
static void
3905
encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3906
                    enum ofp_version ofp_version, struct ofpbuf *out)
3907
1.06k
{
3908
1.06k
    put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3909
1.06k
}
3910
3911
/* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3912
 * action to 'pp->ofpacts'.
3913
 *
3914
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3915
 * error.  The caller is responsible for freeing the returned string. */
3916
static char * OVS_WARN_UNUSED_RESULT
3917
parse_SET_MPLS_TTL(char *arg, const struct ofpact_parse_params *pp)
3918
1.31k
{
3919
1.31k
    struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(pp->ofpacts);
3920
1.31k
    uint8_t ttl;
3921
1.31k
    char *error;
3922
3923
1.31k
    if (*arg == '\0') {
3924
1
        return xstrdup("set_mpls_ttl: expected ttl.");
3925
1
    }
3926
3927
1.30k
    error = str_to_u8(arg, "MPLS TTL", &ttl);
3928
1.30k
    if (error) {
3929
6
        return error;
3930
6
    }
3931
1.30k
    mpls_ttl->ttl = ttl;
3932
1.30k
    return NULL;
3933
1.30k
}
3934
3935
static void
3936
format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a,
3937
                    const struct ofpact_format_params *fp)
3938
1.67k
{
3939
1.67k
    ds_put_format(fp->s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3940
1.67k
                  colors.paren, colors.end, a->ttl,
3941
1.67k
                  colors.paren, colors.end);
3942
1.67k
}
3943
3944
static enum ofperr
3945
check_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a OVS_UNUSED,
3946
                   struct ofpact_check_params *cp)
3947
2.39k
{
3948
2.39k
    return check_set_mpls(cp);
3949
2.39k
}
3950

3951
/* Decrement MPLS TTL actions. */
3952
3953
static enum ofperr
3954
decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3955
4.46k
{
3956
4.46k
    ofpact_put_DEC_MPLS_TTL(out);
3957
4.46k
    return 0;
3958
4.46k
}
3959
3960
static void
3961
encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3962
                    enum ofp_version ofp_version, struct ofpbuf *out)
3963
1.71k
{
3964
1.71k
    put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3965
1.71k
}
3966
3967
static char * OVS_WARN_UNUSED_RESULT
3968
parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
3969
1.74k
{
3970
1.74k
    ofpact_put_DEC_MPLS_TTL(pp->ofpacts);
3971
1.74k
    return NULL;
3972
1.74k
}
3973
3974
static void
3975
format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED,
3976
                    const struct ofpact_format_params *fp)
3977
3.99k
{
3978
3.99k
    ds_put_format(fp->s, "%sdec_mpls_ttl%s", colors.value, colors.end);
3979
3.99k
}
3980
3981
static enum ofperr
3982
check_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED,
3983
                   struct ofpact_check_params *cp)
3984
5.70k
{
3985
5.70k
    return check_set_mpls(cp);
3986
5.70k
}
3987

3988
/* Push MPLS label action. */
3989
3990
static enum ofperr
3991
decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3992
                           enum ofp_version ofp_version OVS_UNUSED,
3993
                           struct ofpbuf *out)
3994
6.98k
{
3995
6.98k
    struct ofpact_push_mpls *oam;
3996
3997
6.98k
    if (!eth_type_mpls(ethertype)) {
3998
192
        return OFPERR_OFPBAC_BAD_ARGUMENT;
3999
192
    }
4000
6.79k
    oam = ofpact_put_PUSH_MPLS(out);
4001
6.79k
    oam->ethertype = ethertype;
4002
4003
6.79k
    return 0;
4004
6.98k
}
4005
4006
static void
4007
encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
4008
                 enum ofp_version ofp_version, struct ofpbuf *out)
4009
328
{
4010
328
    put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
4011
328
}
4012
4013
static char * OVS_WARN_UNUSED_RESULT
4014
parse_PUSH_MPLS(char *arg, const struct ofpact_parse_params *pp)
4015
565
{
4016
565
    uint16_t ethertype;
4017
565
    char *error;
4018
4019
565
    error = str_to_u16(arg, "push_mpls", &ethertype);
4020
565
    if (!error) {
4021
562
        ofpact_put_PUSH_MPLS(pp->ofpacts)->ethertype = htons(ethertype);
4022
562
    }
4023
565
    return error;
4024
565
}
4025
4026
static void
4027
format_PUSH_MPLS(const struct ofpact_push_mpls *a,
4028
                 const struct ofpact_format_params *fp)
4029
6.51k
{
4030
6.51k
    ds_put_format(fp->s, "%spush_mpls:%s0x%04"PRIx16,
4031
6.51k
                  colors.param, colors.end, ntohs(a->ethertype));
4032
6.51k
}
4033
4034
static enum ofperr
4035
check_PUSH_MPLS(const struct ofpact_push_mpls *a,
4036
                struct ofpact_check_params *cp)
4037
7.06k
{
4038
7.06k
    struct flow *flow = &cp->match->flow;
4039
4040
7.06k
    if (flow->packet_type != htonl(PT_ETH)) {
4041
279
        inconsistent_match(&cp->usable_protocols);
4042
279
    }
4043
7.06k
    flow->dl_type = a->ethertype;
4044
4045
    /* The packet is now MPLS and the MPLS payload is opaque.
4046
     * Thus nothing can be assumed about the network protocol.
4047
     * Temporarily mark that we have no nw_proto. */
4048
7.06k
    flow->nw_proto = 0;
4049
4050
7.06k
    return 0;
4051
7.06k
}
4052

4053
/* Pop MPLS label action. */
4054
4055
static enum ofperr
4056
decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
4057
                          enum ofp_version ofp_version OVS_UNUSED,
4058
                          struct ofpbuf *out)
4059
4.70k
{
4060
4.70k
    ofpact_put_POP_MPLS(out)->ethertype = ethertype;
4061
4.70k
    return 0;
4062
4.70k
}
4063
4064
static void
4065
encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
4066
                enum ofp_version ofp_version, struct ofpbuf *out)
4067
1.17k
{
4068
1.17k
    put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
4069
1.17k
}
4070
4071
static char * OVS_WARN_UNUSED_RESULT
4072
parse_POP_MPLS(char *arg, const struct ofpact_parse_params *pp)
4073
5.66k
{
4074
5.66k
    uint16_t ethertype;
4075
5.66k
    char *error;
4076
4077
5.66k
    error = str_to_u16(arg, "pop_mpls", &ethertype);
4078
5.66k
    if (!error) {
4079
5.66k
        ofpact_put_POP_MPLS(pp->ofpacts)->ethertype = htons(ethertype);
4080
5.66k
    }
4081
5.66k
    return error;
4082
5.66k
}
4083
4084
static void
4085
format_POP_MPLS(const struct ofpact_pop_mpls *a,
4086
                const struct ofpact_format_params *fp)
4087
3.90k
{
4088
3.90k
    ds_put_format(fp->s, "%spop_mpls:%s0x%04"PRIx16,
4089
3.90k
                  colors.param, colors.end, ntohs(a->ethertype));
4090
3.90k
}
4091
4092
static enum ofperr
4093
check_POP_MPLS(const struct ofpact_pop_mpls *a, struct ofpact_check_params *cp)
4094
5.16k
{
4095
5.16k
    struct flow *flow = &cp->match->flow;
4096
5.16k
    ovs_be16 dl_type = get_dl_type(flow);
4097
4098
5.16k
    if (flow->packet_type != htonl(PT_ETH) || !eth_type_mpls(dl_type)) {
4099
4.01k
        inconsistent_match(&cp->usable_protocols);
4100
4.01k
    }
4101
5.16k
    flow->dl_type = a->ethertype;
4102
5.16k
    return 0;
4103
5.16k
}
4104

4105
/* Set tunnel ID actions. */
4106
4107
static enum ofperr
4108
decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
4109
                            enum ofp_version ofp_version OVS_UNUSED,
4110
                            struct ofpbuf *out)
4111
921
{
4112
921
    struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
4113
921
    tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
4114
921
    tunnel->tun_id = tun_id;
4115
921
    return 0;
4116
921
}
4117
4118
static enum ofperr
4119
decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
4120
                              enum ofp_version ofp_version OVS_UNUSED,
4121
                              struct ofpbuf *out)
4122
77
{
4123
77
    struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
4124
77
    tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
4125
77
    tunnel->tun_id = tun_id;
4126
77
    return 0;
4127
77
}
4128
4129
static void
4130
encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
4131
                  enum ofp_version ofp_version, struct ofpbuf *out)
4132
1.09k
{
4133
1.09k
    uint64_t tun_id = tunnel->tun_id;
4134
4135
1.09k
    if (ofp_version < OFP12_VERSION) {
4136
830
        if (tun_id <= UINT32_MAX
4137
560
            && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
4138
345
            put_NXAST_SET_TUNNEL(out, tun_id);
4139
485
        } else {
4140
485
            put_NXAST_SET_TUNNEL64(out, tun_id);
4141
485
        }
4142
830
    } else {
4143
262
        put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
4144
262
    }
4145
1.09k
}
4146
4147
static char * OVS_WARN_UNUSED_RESULT
4148
parse_set_tunnel(char *arg, enum ofp_raw_action_type raw,
4149
                 const struct ofpact_parse_params *pp)
4150
1.19k
{
4151
1.19k
    struct ofpact_tunnel *tunnel;
4152
4153
1.19k
    tunnel = ofpact_put_SET_TUNNEL(pp->ofpacts);
4154
1.19k
    tunnel->ofpact.raw = raw;
4155
1.19k
    return str_to_u64(arg, &tunnel->tun_id);
4156
1.19k
}
4157
4158
static char * OVS_WARN_UNUSED_RESULT
4159
parse_SET_TUNNEL(char *arg, const struct ofpact_parse_params *pp)
4160
740
{
4161
740
    return parse_set_tunnel(arg, NXAST_RAW_SET_TUNNEL, pp);
4162
740
}
4163
4164
static void
4165
format_SET_TUNNEL(const struct ofpact_tunnel *a,
4166
                  const struct ofpact_format_params *fp)
4167
168
{
4168
168
    ds_put_format(fp->s, "%sset_tunnel%s:%s%#"PRIx64, colors.param,
4169
168
                  (a->tun_id > UINT32_MAX
4170
107
                   || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
4171
168
                  colors.end, a->tun_id);
4172
168
}
4173
4174
static enum ofperr
4175
check_SET_TUNNEL(const struct ofpact_tunnel *a OVS_UNUSED,
4176
                 const struct ofpact_check_params *cp OVS_UNUSED)
4177
1.13k
{
4178
1.13k
    return 0;
4179
1.13k
}
4180

4181
/* Delete field action. */
4182
4183
/* Action structure for DELETE_FIELD */
4184
struct nx_action_delete_field {
4185
    ovs_be16 type;          /* OFPAT_VENDOR */
4186
    ovs_be16 len;           /* Length is 24. */
4187
    ovs_be32 vendor;        /* NX_VENDOR_ID. */
4188
    ovs_be16 subtype;       /* NXAST_DELETE_FIELD. */
4189
    /* Followed by:
4190
     * - OXM/NXM header for field to delete (4 or 8 bytes).
4191
     * - Enough 0-bytes to pad out the action to 24 bytes. */
4192
    uint8_t pad[14];
4193
};
4194
OFP_ASSERT(sizeof(struct nx_action_delete_field ) == 24);
4195
4196
static enum ofperr
4197
decode_NXAST_RAW_DELETE_FIELD(const struct nx_action_delete_field *nadf,
4198
                              enum ofp_version ofp_version OVS_UNUSED,
4199
                              const struct vl_mff_map *vl_mff_map,
4200
                              uint64_t *tlv_bitmap, struct ofpbuf *out)
4201
522
{
4202
522
    struct ofpact_delete_field *delete_field;
4203
522
    enum ofperr err;
4204
4205
522
    delete_field = ofpact_put_DELETE_FIELD(out);
4206
522
    delete_field->ofpact.raw = NXAST_RAW_DELETE_FIELD;
4207
4208
522
    struct ofpbuf b = ofpbuf_const_initializer(nadf, ntohs(nadf->len));
4209
522
    ofpbuf_pull(&b, OBJECT_OFFSETOF(nadf, pad));
4210
4211
522
    err = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &delete_field->field,
4212
522
                                   NULL, tlv_bitmap);
4213
522
    if (err) {
4214
465
        return err;
4215
465
    }
4216
4217
57
    return 0;
4218
522
}
4219
4220
static void
4221
encode_DELETE_FIELD(const struct ofpact_delete_field *delete_field,
4222
                    enum ofp_version ofp_version OVS_UNUSED,
4223
                    struct ofpbuf *out)
4224
195
{
4225
195
    size_t size;
4226
4227
195
    put_NXAST_DELETE_FIELD(out);
4228
195
    size = out->size;
4229
4230
195
    out->size = size - MEMBER_SIZEOF(struct nx_action_delete_field, pad);
4231
195
    nx_put_mff_header(out, delete_field->field, 0, false);
4232
195
    out->size = size;
4233
195
}
4234
4235
static char * OVS_WARN_UNUSED_RESULT
4236
parse_DELETE_FIELD(char *arg, const struct ofpact_parse_params *pp)
4237
253
{
4238
253
    struct ofpact_delete_field *delete_field;
4239
4240
253
    delete_field = ofpact_put_DELETE_FIELD(pp->ofpacts);
4241
253
    return mf_parse_field(&delete_field->field, arg);
4242
253
}
4243
4244
static void
4245
format_DELETE_FIELD(const struct ofpact_delete_field *odf,
4246
                          const struct ofpact_format_params *fp)
4247
8
{
4248
8
    ds_put_format(fp->s, "%sdelete_field:%s", colors.param,
4249
8
                  colors.end);
4250
8
    ds_put_format(fp->s, "%s", odf->field->name);
4251
8
}
4252
4253
static enum ofperr
4254
check_DELETE_FIELD(const struct ofpact_delete_field *odf,
4255
                         struct ofpact_check_params *cp OVS_UNUSED)
4256
259
{
4257
259
    if (odf->field->id < MFF_TUN_METADATA0 ||
4258
217
        odf->field->id > MFF_TUN_METADATA63) {
4259
56
        return OFPERR_OFPBAC_BAD_ARGUMENT;
4260
56
    }
4261
203
    return 0;
4262
259
}
4263

4264
/* Set queue action. */
4265
4266
static enum ofperr
4267
decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
4268
                           enum ofp_version ofp_version OVS_UNUSED,
4269
                           struct ofpbuf *out)
4270
692
{
4271
692
    ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
4272
692
    return 0;
4273
692
}
4274
4275
static void
4276
encode_SET_QUEUE(const struct ofpact_queue *queue,
4277
                 enum ofp_version ofp_version, struct ofpbuf *out)
4278
194
{
4279
194
    put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
4280
194
}
4281
4282
static char * OVS_WARN_UNUSED_RESULT
4283
parse_SET_QUEUE(char *arg, const struct ofpact_parse_params *pp)
4284
205
{
4285
205
    return str_to_u32(arg, &ofpact_put_SET_QUEUE(pp->ofpacts)->queue_id);
4286
205
}
4287
4288
static void
4289
format_SET_QUEUE(const struct ofpact_queue *a,
4290
                 const struct ofpact_format_params *fp)
4291
682
{
4292
682
    ds_put_format(fp->s, "%sset_queue:%s%"PRIu32,
4293
682
                  colors.param, colors.end, a->queue_id);
4294
682
}
4295
4296
static enum ofperr
4297
check_SET_QUEUE(const struct ofpact_queue *a OVS_UNUSED,
4298
                const struct ofpact_check_params *cp OVS_UNUSED)
4299
303
{
4300
303
    return 0;
4301
303
}
4302

4303
/* Pop queue action. */
4304
4305
static enum ofperr
4306
decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
4307
1.18k
{
4308
1.18k
    ofpact_put_POP_QUEUE(out);
4309
1.18k
    return 0;
4310
1.18k
}
4311
4312
static void
4313
encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
4314
                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4315
419
{
4316
419
    put_NXAST_POP_QUEUE(out);
4317
419
}
4318
4319
static char * OVS_WARN_UNUSED_RESULT
4320
parse_POP_QUEUE(const char *arg OVS_UNUSED,
4321
                const struct ofpact_parse_params *pp)
4322
677
{
4323
677
    ofpact_put_POP_QUEUE(pp->ofpacts);
4324
677
    return NULL;
4325
677
}
4326
4327
static void
4328
format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED,
4329
                 const struct ofpact_format_params *fp)
4330
248
{
4331
248
    ds_put_format(fp->s, "%spop_queue%s", colors.value, colors.end);
4332
248
}
4333
4334
static enum ofperr
4335
check_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED,
4336
                const struct ofpact_check_params *cp OVS_UNUSED)
4337
445
{
4338
445
    return 0;
4339
445
}
4340

4341
/* Action structure for NXAST_FIN_TIMEOUT.
4342
 *
4343
 * This action changes the idle timeout or hard timeout, or both, of this
4344
 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
4345
 * When such a packet is observed, the action reduces the rule's idle timeout
4346
 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'.  This
4347
 * action has no effect on an existing timeout that is already shorter than the
4348
 * one that the action specifies.  A 'fin_idle_timeout' or 'fin_hard_timeout'
4349
 * of zero has no effect on the respective timeout.
4350
 *
4351
 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
4352
 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
4353
 * receipt of the FIN or RST.
4354
 *
4355
 * This is useful for quickly discarding learned TCP flows that otherwise will
4356
 * take a long time to expire.
4357
 *
4358
 * This action is intended for use with an OpenFlow rule that matches only a
4359
 * single TCP flow.  If the rule matches multiple TCP flows (e.g. it wildcards
4360
 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
4361
 * RST in any of those flows will cause the entire OpenFlow rule to expire
4362
 * early, which is not normally desirable.
4363
 */
4364
struct nx_action_fin_timeout {
4365
    ovs_be16 type;              /* OFPAT_VENDOR. */
4366
    ovs_be16 len;               /* 16. */
4367
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
4368
    ovs_be16 subtype;           /* NXAST_FIN_TIMEOUT. */
4369
    ovs_be16 fin_idle_timeout;  /* New idle timeout, if nonzero. */
4370
    ovs_be16 fin_hard_timeout;  /* New hard timeout, if nonzero. */
4371
    ovs_be16 pad;               /* Must be zero. */
4372
};
4373
OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
4374
4375
static enum ofperr
4376
decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
4377
                             enum ofp_version ofp_version OVS_UNUSED,
4378
                             struct ofpbuf *out)
4379
794
{
4380
794
    struct ofpact_fin_timeout *oft;
4381
4382
794
    oft = ofpact_put_FIN_TIMEOUT(out);
4383
794
    oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
4384
794
    oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
4385
794
    return 0;
4386
794
}
4387
4388
static void
4389
encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
4390
                   enum ofp_version ofp_version OVS_UNUSED,
4391
                   struct ofpbuf *out)
4392
433
{
4393
433
    struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
4394
433
    naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
4395
433
    naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
4396
433
}
4397
4398
static char * OVS_WARN_UNUSED_RESULT
4399
parse_FIN_TIMEOUT(char *arg, const struct ofpact_parse_params *pp)
4400
1.62k
{
4401
1.62k
    struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(pp->ofpacts);
4402
1.62k
    char *key, *value;
4403
4404
2.74k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
4405
1.28k
        char *error;
4406
4407
1.28k
        if (!strcmp(key, "idle_timeout")) {
4408
305
            error =  str_to_u16(value, key, &oft->fin_idle_timeout);
4409
979
        } else if (!strcmp(key, "hard_timeout")) {
4410
821
            error = str_to_u16(value, key, &oft->fin_hard_timeout);
4411
821
        } else {
4412
158
            error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
4413
158
                              key);
4414
158
        }
4415
4416
1.28k
        if (error) {
4417
167
            return error;
4418
167
        }
4419
1.28k
    }
4420
1.45k
    return NULL;
4421
1.62k
}
4422
4423
static void
4424
format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a,
4425
                   const struct ofpact_format_params *fp)
4426
715
{
4427
715
    ds_put_format(fp->s, "%sfin_timeout(%s", colors.paren, colors.end);
4428
715
    if (a->fin_idle_timeout) {
4429
549
        ds_put_format(fp->s, "%sidle_timeout=%s%"PRIu16",",
4430
549
                      colors.param, colors.end, a->fin_idle_timeout);
4431
549
    }
4432
715
    if (a->fin_hard_timeout) {
4433
699
        ds_put_format(fp->s, "%shard_timeout=%s%"PRIu16",",
4434
699
                      colors.param, colors.end, a->fin_hard_timeout);
4435
699
    }
4436
715
    ds_chomp(fp->s, ',');
4437
715
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4438
715
}
4439
4440
4441
static enum ofperr
4442
check_FIN_TIMEOUT(const struct ofpact_fin_timeout *a OVS_UNUSED,
4443
                  struct ofpact_check_params *cp)
4444
605
{
4445
605
    if (cp->match->flow.nw_proto != IPPROTO_TCP) {
4446
273
        inconsistent_match(&cp->usable_protocols);
4447
273
    }
4448
605
    return 0;
4449
605
}
4450

4451
/* Action structure for NXAST_ENCAP */
4452
struct nx_action_encap {
4453
    ovs_be16 type;         /* OFPAT_VENDOR. */
4454
    ovs_be16 len;          /* Total size including any property TLVs. */
4455
    ovs_be32 vendor;       /* NX_VENDOR_ID. */
4456
    ovs_be16 subtype;      /* NXAST_ENCAP. */
4457
    ovs_be16 hdr_size;     /* Header size in bytes, 0 = 'not specified'.*/
4458
    ovs_be32 new_pkt_type; /* Header type to add and PACKET_TYPE of result. */
4459
    struct ofp_ed_prop_header props[];  /* Encap TLV properties. */
4460
};
4461
OFP_ASSERT(sizeof(struct nx_action_encap) == 16);
4462
4463
static enum ofperr
4464
decode_NXAST_RAW_ENCAP(const struct nx_action_encap *nae,
4465
                       enum ofp_version ofp_version OVS_UNUSED,
4466
                       struct ofpbuf *out)
4467
6.89k
{
4468
6.89k
    struct ofpact_encap *encap;
4469
6.89k
    const struct ofp_ed_prop_header *ofp_prop;
4470
6.89k
    const size_t encap_ofs = out->size;
4471
6.89k
    size_t props_len;
4472
6.89k
    uint16_t n_props = 0;
4473
6.89k
    int err;
4474
4475
6.89k
    encap = ofpact_put_ENCAP(out);
4476
6.89k
    encap->ofpact.raw = NXAST_RAW_ENCAP;
4477
6.89k
    switch (ntohl(nae->new_pkt_type)) {
4478
4.77k
    case PT_ETH:
4479
5.35k
    case PT_NSH:
4480
5.59k
    case PT_MPLS:
4481
6.26k
    case PT_MPLS_MC:
4482
        /* Add supported encap header types here. */
4483
6.26k
        break;
4484
633
    default:
4485
633
        return OFPERR_NXBAC_BAD_HEADER_TYPE;
4486
6.89k
    }
4487
6.26k
    encap->new_pkt_type = nae->new_pkt_type;
4488
6.26k
    encap->hdr_size = ntohs(nae->hdr_size);
4489
4490
6.26k
    ofp_prop = nae->props;
4491
6.26k
    props_len = ntohs(nae->len) - offsetof(struct nx_action_encap, props);
4492
6.26k
    n_props = 0;
4493
14.2k
    while (props_len > 0) {
4494
12.0k
        err = decode_ed_prop(&ofp_prop, out, &props_len);
4495
12.0k
        if (err) {
4496
4.14k
            return err;
4497
4.14k
        }
4498
7.93k
        n_props++;
4499
7.93k
    }
4500
2.12k
    encap = ofpbuf_at_assert(out, encap_ofs, sizeof *encap);
4501
2.12k
    encap->n_props = n_props;
4502
2.12k
    out->header = &encap->ofpact;
4503
2.12k
    ofpact_finish_ENCAP(out, &encap);
4504
4505
2.12k
    return 0;
4506
6.26k
}
4507
4508
static void
4509
encode_ENCAP(const struct ofpact_encap *encap,
4510
             enum ofp_version ofp_version OVS_UNUSED,
4511
             struct ofpbuf *out)
4512
2.68k
{
4513
2.68k
    size_t start_ofs = out->size;
4514
2.68k
    struct nx_action_encap *nae = put_NXAST_ENCAP(out);
4515
2.68k
    int i;
4516
4517
2.68k
    nae->new_pkt_type = encap->new_pkt_type;
4518
2.68k
    nae->hdr_size = htons(encap->hdr_size);
4519
2.68k
    const struct ofpact_ed_prop *prop = encap->props;
4520
3.28k
    for (i = 0; i < encap->n_props; i++) {
4521
606
        encode_ed_prop(&prop, out);
4522
606
    }
4523
2.68k
    pad_ofpat(out, start_ofs);
4524
2.68k
}
4525
4526
static bool
4527
parse_encap_header(const char *hdr, ovs_be32 *packet_type)
4528
3.98k
{
4529
3.98k
    if (strcmp(hdr, "ethernet") == 0) {
4530
360
        *packet_type = htonl(PT_ETH);
4531
3.62k
    } else if (strcmp(hdr, "nsh") == 0) {
4532
2.56k
        *packet_type = htonl(PT_NSH);
4533
2.56k
    } else if (strcmp(hdr, "mpls") == 0) {
4534
321
        *packet_type = htonl(PT_MPLS);
4535
747
    } else if (strcmp(hdr, "mpls_mc") == 0) {
4536
614
        *packet_type = htonl(PT_MPLS_MC);
4537
614
    } else {
4538
133
        return false;
4539
133
    }
4540
3.85k
    return true;
4541
3.98k
}
4542
4543
static char *
4544
parse_ed_props(const uint16_t prop_class, char **arg, int *n_props, struct ofpbuf *out)
4545
3.85k
{
4546
3.85k
    char *key, *value, *err;
4547
3.85k
    uint8_t prop_type;
4548
4549
5.04k
    while (ofputil_parse_key_value(arg, &key, &value)) {
4550
1.27k
        if (!parse_ed_prop_type(prop_class, key, &prop_type)) {
4551
71
            return xasprintf("Invalid property: %s", key);
4552
71
        }
4553
1.20k
        if (value == NULL) {
4554
0
            return xasprintf("Missing the value for property: %s", key);
4555
0
        }
4556
1.20k
        err = parse_ed_prop_value(prop_class, prop_type, value, out);
4557
1.20k
        if (err != NULL) {
4558
13
            return err;
4559
13
        }
4560
1.19k
        (*n_props)++;
4561
1.19k
    }
4562
3.77k
    return NULL;
4563
3.85k
}
4564
4565
/* The string representation of the encap action is
4566
 * encap(header_type(prop=<value>,tlv(<class>,<type>,<value>),...))
4567
 */
4568
4569
static char * OVS_WARN_UNUSED_RESULT
4570
parse_ENCAP(char *arg, const struct ofpact_parse_params *pp)
4571
3.99k
{
4572
3.99k
    *pp->usable_protocols &= OFPUTIL_P_OF13_UP;
4573
4574
3.99k
    struct ofpact_encap *encap;
4575
3.99k
    char *key, *value, *str;
4576
3.99k
    char *error = NULL;
4577
3.99k
    uint16_t prop_class;
4578
3.99k
    int n_props = 0;
4579
4580
3.99k
    encap = ofpact_put_ENCAP(pp->ofpacts);
4581
3.99k
    encap->hdr_size = 0;
4582
    /* Parse encap header type. */
4583
3.99k
    str = arg;
4584
3.99k
    if (!ofputil_parse_key_value(&arg, &key, &value)) {
4585
4
        return xasprintf("Missing encap hdr: %s", str);
4586
4
    }
4587
3.98k
    if (!parse_encap_header(key, &encap->new_pkt_type)) {
4588
133
        return xasprintf("Encap hdr not supported: %s", value);
4589
133
    }
4590
3.85k
    if (!parse_ed_prop_class(key, &prop_class)) {
4591
0
        return xasprintf("Invalid encap prop class: %s", key);
4592
0
    }
4593
    /* Parse encap properties. */
4594
3.85k
    error = parse_ed_props(prop_class, &value, &n_props, pp->ofpacts);
4595
3.85k
    if (error != NULL) {
4596
84
        return error;
4597
84
    }
4598
    /* ofpbuf may have been re-allocated. */
4599
3.77k
    encap = pp->ofpacts->header;
4600
3.77k
    encap->n_props = n_props;
4601
4602
3.77k
    if (ofpbuf_oversized(pp->ofpacts)) {
4603
0
        return xasprintf("input too big");
4604
0
    }
4605
4606
3.77k
    ofpact_finish_ENCAP(pp->ofpacts, &encap);
4607
3.77k
    return NULL;
4608
3.77k
}
4609
4610
static char *
4611
format_encap_pkt_type(const ovs_be32 pkt_type)
4612
1.54k
{
4613
1.54k
    switch (ntohl(pkt_type)) {
4614
479
    case PT_ETH:
4615
479
        return "ethernet";
4616
570
    case PT_NSH:
4617
570
        return "nsh";
4618
212
    case PT_MPLS:
4619
212
        return "mpls";
4620
279
    case PT_MPLS_MC:
4621
279
        return "mpls_mc";
4622
0
    default:
4623
0
        return "UNKNOWN";
4624
1.54k
    }
4625
1.54k
}
4626
4627
static void
4628
format_ed_props(struct ds *s, uint16_t n_props,
4629
                const struct ofpact_ed_prop *prop)
4630
302
{
4631
302
    const uint8_t *p = (uint8_t *) prop;
4632
302
    int i;
4633
4634
302
    if (n_props == 0) {
4635
0
        return;
4636
0
    }
4637
713
    for (i = 0; i < n_props; i++) {
4638
411
        format_ed_prop(s, prop);
4639
411
        ds_put_char(s, ',');
4640
411
        p += ROUND_UP(prop->len, 8);
4641
411
        prop = ALIGNED_CAST(const struct ofpact_ed_prop *, p);
4642
411
    }
4643
302
    if (n_props > 0) {
4644
302
        ds_chomp(s, ',');
4645
302
    }
4646
302
}
4647
4648
static void
4649
format_ENCAP(const struct ofpact_encap *a,
4650
             const struct ofpact_format_params *fp)
4651
1.54k
{
4652
1.54k
    ds_put_format(fp->s, "%sencap(%s", colors.paren, colors.end);
4653
1.54k
    ds_put_format(fp->s, "%s", format_encap_pkt_type(a->new_pkt_type));
4654
1.54k
    if (a->n_props > 0) {
4655
302
        ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
4656
302
        format_ed_props(fp->s, a->n_props, a->props);
4657
302
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4658
302
    }
4659
1.54k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4660
1.54k
}
4661
4662
static enum ofperr
4663
check_ENCAP(const struct ofpact_encap *a, struct ofpact_check_params *cp)
4664
4.21k
{
4665
4.21k
    struct flow *flow = &cp->match->flow;
4666
4.21k
    flow->packet_type = a->new_pkt_type;
4667
4.21k
    if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE) {
4668
3.69k
        flow->dl_type = htons(pt_ns_type(flow->packet_type));
4669
3.69k
    }
4670
4.21k
    if (!is_ip_any(flow)) {
4671
3.90k
        flow->nw_proto = 0;
4672
3.90k
    }
4673
4.21k
    return 0;
4674
4.21k
}
4675

4676
/* Action structure for NXAST_DECAP */
4677
struct nx_action_decap {
4678
    ovs_be16 type;         /* OFPAT_VENDOR. */
4679
    ovs_be16 len;          /* Total size including any property TLVs. */
4680
    ovs_be32 vendor;       /* NX_VENDOR_ID. */
4681
    ovs_be16 subtype;      /* NXAST_DECAP. */
4682
    uint8_t pad[2];        /* 2 bytes padding */
4683
4684
    /* Packet type or result.
4685
     *
4686
     * The special value (0,0xFFFE) "Use next proto"
4687
     * is used to request OVS to automatically set the new packet type based
4688
     * on the decap'ed header's next protocol.
4689
     */
4690
    ovs_be32 new_pkt_type;
4691
};
4692
OFP_ASSERT(sizeof(struct nx_action_decap) == 16);
4693
4694
static enum ofperr
4695
decode_NXAST_RAW_DECAP(const struct nx_action_decap *nad,
4696
                       enum ofp_version ofp_version OVS_UNUSED,
4697
                       struct ofpbuf *ofpacts)
4698
10.6k
{
4699
10.6k
    struct ofpact_decap * decap;
4700
4701
10.6k
    if (ntohs(nad->len) > sizeof *nad) {
4702
        /* No properties supported yet. */
4703
61
        return OFPERR_NXBAC_UNKNOWN_ED_PROP;
4704
61
    }
4705
4706
10.5k
    decap = ofpact_put_DECAP(ofpacts);
4707
10.5k
    decap->ofpact.raw = NXAST_RAW_DECAP;
4708
10.5k
    decap->new_pkt_type = nad->new_pkt_type;
4709
10.5k
    return 0;
4710
10.6k
}
4711
4712
static void
4713
encode_DECAP(const struct ofpact_decap *decap,
4714
                enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4715
1.44k
{
4716
1.44k
    struct nx_action_decap *nad = put_NXAST_DECAP(out);
4717
4718
1.44k
    nad->len = htons(sizeof(struct nx_action_decap));
4719
1.44k
    nad->new_pkt_type = decap->new_pkt_type;
4720
1.44k
}
4721
4722
static char * OVS_WARN_UNUSED_RESULT
4723
parse_DECAP(char *arg, const struct ofpact_parse_params *pp)
4724
2.00k
{
4725
2.00k
    struct ofpact_decap *decap;
4726
2.00k
    char *key, *value, *pos;
4727
2.00k
    char *error = NULL;
4728
2.00k
    uint16_t ns, type;
4729
4730
2.00k
    decap = ofpact_put_DECAP(pp->ofpacts);
4731
    /* Default next packet_type is PT_USE_NEXT_PROTO. */
4732
2.00k
    decap->new_pkt_type = htonl(PT_USE_NEXT_PROTO);
4733
4734
    /* Parse decap packet_type if given. */
4735
2.00k
    if (ofputil_parse_key_value(&arg, &key, &value)) {
4736
114
        if (strcmp(key, "packet_type") == 0) {
4737
24
            pos = value;
4738
24
            if (!ofputil_parse_key_value(&pos, &key, &value)
4739
23
                || strcmp(key, "ns") != 0) {
4740
2
                return xstrdup("Missing packet_type attribute ns");
4741
2
            }
4742
22
            error = str_to_u16(value, "ns", &ns);
4743
22
            if (error) {
4744
1
                return error;
4745
1
            }
4746
21
            if (ns >= OFPHTN_N_TYPES) {
4747
2
                return xasprintf("Unsupported ns value: %"PRIu16, ns);
4748
2
            }
4749
19
            if (!ofputil_parse_key_value(&pos, &key, &value)
4750
18
                || strcmp(key, "type") != 0) {
4751
17
                return xstrdup("Missing packet_type attribute type");
4752
17
            }
4753
2
            error = str_to_u16(value, "type", &type);
4754
2
            if (error) {
4755
1
                return error;
4756
1
            }
4757
1
            decap->new_pkt_type = htonl(PACKET_TYPE(ns, type));
4758
90
        } else {
4759
90
            return xasprintf("Invalid decap argument: %s", key);
4760
90
        }
4761
114
    }
4762
1.88k
    return NULL;
4763
2.00k
}
4764
4765
static void
4766
format_DECAP(const struct ofpact_decap *a,
4767
             const struct ofpact_format_params *fp)
4768
4.79k
{
4769
4.79k
    ds_put_format(fp->s, "%sdecap(%s", colors.paren, colors.end);
4770
4.79k
    if (a->new_pkt_type != htonl(PT_USE_NEXT_PROTO)) {
4771
4.51k
        ds_put_format(fp->s, "packet_type(ns=%"PRIu16",type=%#"PRIx16")",
4772
4.51k
                      pt_ns(a->new_pkt_type),
4773
4.51k
                      pt_ns_type(a->new_pkt_type));
4774
4.51k
    }
4775
4.79k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4776
4.79k
}
4777
4778
static enum ofperr
4779
check_DECAP(const struct ofpact_decap *a OVS_UNUSED,
4780
            struct ofpact_check_params *cp)
4781
9.91k
{
4782
9.91k
    struct flow *flow = &cp->match->flow;
4783
9.91k
    if (flow->packet_type == htonl(PT_ETH)) {
4784
        /* Adjust the packet_type to allow subsequent actions. */
4785
5.94k
        flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
4786
5.94k
                                           ntohs(flow->dl_type));
4787
5.94k
    } else {
4788
        /* The actual packet_type is only known after decapsulation.
4789
         * Do not allow subsequent actions that depend on packet headers. */
4790
3.97k
        flow->packet_type = htonl(PT_UNKNOWN);
4791
3.97k
        flow->dl_type = OVS_BE16_MAX;
4792
3.97k
    }
4793
9.91k
    return 0;
4794
9.91k
}
4795
4796
/* Action dec_nsh_ttl */
4797
4798
static enum ofperr
4799
decode_NXAST_RAW_DEC_NSH_TTL(struct ofpbuf *out)
4800
684
{
4801
684
    ofpact_put_DEC_NSH_TTL(out);
4802
684
    return 0;
4803
684
}
4804
4805
static void
4806
encode_DEC_NSH_TTL(const struct ofpact_null *null OVS_UNUSED,
4807
            enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4808
841
{
4809
841
    put_NXAST_DEC_NSH_TTL(out);
4810
841
}
4811
4812
static char * OVS_WARN_UNUSED_RESULT
4813
parse_DEC_NSH_TTL(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
4814
1.37k
{
4815
1.37k
    ofpact_put_DEC_NSH_TTL(pp->ofpacts);
4816
1.37k
    return NULL;
4817
1.37k
}
4818
4819
static void
4820
format_DEC_NSH_TTL(const struct ofpact_null *a OVS_UNUSED,
4821
                   const struct ofpact_format_params *fp)
4822
678
{
4823
678
    ds_put_format(fp->s, "%sdec_nsh_ttl%s", colors.special, colors.end);
4824
678
}
4825
4826
static enum ofperr
4827
check_DEC_NSH_TTL(const struct ofpact_null *a OVS_UNUSED,
4828
                  struct ofpact_check_params *cp)
4829
1.65k
{
4830
1.65k
    struct flow *flow = &cp->match->flow;
4831
1.65k
    if (flow->packet_type != htonl(PT_NSH) &&
4832
742
        flow->dl_type != htons(ETH_TYPE_NSH)) {
4833
423
        inconsistent_match(&cp->usable_protocols);
4834
423
    }
4835
1.65k
    return 0;
4836
1.65k
}
4837

4838
/* Action structures for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, and
4839
 * NXAST_RESUBMIT_TABLE_CT.
4840
 *
4841
 * These actions search one of the switch's flow tables:
4842
 *
4843
 *    - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the
4844
 *      'table' member is not 255, then it specifies the table to search.
4845
 *
4846
 *    - Otherwise (for NXAST_RESUBMIT_TABLE or NXAST_RESUBMIT_TABLE_CT with a
4847
 *      'table' of 255, or for NXAST_RESUBMIT regardless of 'table'), it
4848
 *      searches the current flow table, that is, the OpenFlow flow table that
4849
 *      contains the flow from which this action was obtained.  If this action
4850
 *      did not come from a flow table (e.g. it came from an OFPT_PACKET_OUT
4851
 *      message), then table 0 is the current table.
4852
 *
4853
 * The flow table lookup uses a flow that may be slightly modified from the
4854
 * original lookup:
4855
 *
4856
 *    - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
4857
 *      is used as the flow's in_port.
4858
 *
4859
 *    - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the 'in_port'
4860
 *      member is not OFPP_IN_PORT, then its value is used as the flow's
4861
 *      in_port.  Otherwise, the original in_port is used.
4862
 *
4863
 *    - For NXAST_RESUBMIT_TABLE_CT the Conntrack 5-tuple fields are used as
4864
 *      the packets IP header fields during the lookup.
4865
 *
4866
 *    - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
4867
 *      resubmit action, then the flow is updated with the new values.
4868
 *
4869
 * Following the lookup, the original in_port is restored.
4870
 *
4871
 * If the modified flow matched in the flow table, then the corresponding
4872
 * actions are executed.  Afterward, actions following the resubmit in the
4873
 * original set of actions, if any, are executed; any changes made to the
4874
 * packet (e.g. changes to VLAN) by secondary actions persist when those
4875
 * actions are executed, although the original in_port is restored.
4876
 *
4877
 * Resubmit actions may be used any number of times within a set of actions.
4878
 *
4879
 * Resubmit actions may nest.  To prevent infinite loops and excessive resource
4880
 * use, the implementation may limit nesting depth and the total number of
4881
 * resubmits:
4882
 *
4883
 *    - Open vSwitch 1.0.1 and earlier did not support recursion.
4884
 *
4885
 *    - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
4886
 *
4887
 *    - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
4888
 *
4889
 *    - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
4890
 *
4891
 *    - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
4892
 *
4893
 *    - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
4894
 *      a total limit of 4,096 resubmits per flow translation (earlier versions
4895
 *      did not impose any total limit).
4896
 *
4897
 * NXAST_RESUBMIT ignores 'table' and 'pad'.  NXAST_RESUBMIT_TABLE and
4898
 * NXAST_RESUBMIT_TABLE_CT require 'pad' to be all-bits-zero.
4899
 *
4900
 * Open vSwitch 1.0.1 and earlier did not support recursion.  Open vSwitch
4901
 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.  Open vSwitch before
4902
 * 2.8.0 did not support NXAST_RESUBMIT_TABLE_CT.
4903
 */
4904
struct nx_action_resubmit {
4905
    ovs_be16 type;                  /* OFPAT_VENDOR. */
4906
    ovs_be16 len;                   /* Length is 16. */
4907
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
4908
    ovs_be16 subtype;               /* NXAST_RESUBMIT. */
4909
    ovs_be16 in_port;               /* New in_port for checking flow table. */
4910
    uint8_t table;                  /* NXAST_RESUBMIT_TABLE: table to use. */
4911
    uint8_t pad[3];
4912
};
4913
OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
4914
4915
static enum ofperr
4916
decode_NXAST_RAW_RESUBMIT(uint16_t port,
4917
                          enum ofp_version ofp_version OVS_UNUSED,
4918
                          struct ofpbuf *out)
4919
437
{
4920
437
    struct ofpact_resubmit *resubmit;
4921
4922
437
    resubmit = ofpact_put_RESUBMIT(out);
4923
437
    resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
4924
437
    resubmit->in_port = u16_to_ofp(port);
4925
437
    resubmit->table_id = 0xff;
4926
437
    return 0;
4927
437
}
4928
4929
static enum ofperr
4930
decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
4931
                                enum ofp_version ofp_version OVS_UNUSED,
4932
                                struct ofpbuf *out)
4933
3.50k
{
4934
3.50k
    struct ofpact_resubmit *resubmit;
4935
4936
3.50k
    if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
4937
1.38k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
4938
1.38k
    }
4939
4940
2.11k
    resubmit = ofpact_put_RESUBMIT(out);
4941
2.11k
    resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
4942
2.11k
    resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
4943
2.11k
    resubmit->table_id = nar->table;
4944
2.11k
    return 0;
4945
3.50k
}
4946
4947
static enum ofperr
4948
decode_NXAST_RAW_RESUBMIT_TABLE_CT(const struct nx_action_resubmit *nar,
4949
                                   enum ofp_version ofp_version OVS_UNUSED,
4950
                                   struct ofpbuf *out)
4951
3.09k
{
4952
3.09k
    enum ofperr error = decode_NXAST_RAW_RESUBMIT_TABLE(nar, ofp_version, out);
4953
3.09k
    if (error) {
4954
1.05k
        return error;
4955
1.05k
    }
4956
2.04k
    struct ofpact_resubmit *resubmit = out->header;
4957
2.04k
    resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE_CT;
4958
2.04k
    resubmit->with_ct_orig = true;
4959
2.04k
    return 0;
4960
3.09k
}
4961
4962
static void
4963
encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
4964
                enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4965
1.32k
{
4966
1.32k
    uint16_t in_port = ofp_to_u16(resubmit->in_port);
4967
4968
1.32k
    if (resubmit->table_id == 0xff
4969
846
        && resubmit->ofpact.raw == NXAST_RAW_RESUBMIT) {
4970
0
        put_NXAST_RESUBMIT(out, in_port);
4971
1.32k
    } else {
4972
1.32k
        struct nx_action_resubmit *nar;
4973
1.32k
        nar = resubmit->with_ct_orig
4974
1.32k
            ? put_NXAST_RESUBMIT_TABLE_CT(out) : put_NXAST_RESUBMIT_TABLE(out);
4975
1.32k
        nar->table = resubmit->table_id;
4976
1.32k
        nar->in_port = htons(in_port);
4977
1.32k
    }
4978
1.32k
}
4979
4980
static char * OVS_WARN_UNUSED_RESULT
4981
parse_RESUBMIT(char *arg, const struct ofpact_parse_params *pp)
4982
2.08k
{
4983
2.08k
    struct ofpact_resubmit *resubmit;
4984
2.08k
    char *in_port_s, *table_s, *ct_s;
4985
4986
2.08k
    resubmit = ofpact_put_RESUBMIT(pp->ofpacts);
4987
4988
2.08k
    in_port_s = strsep(&arg, ",");
4989
2.08k
    if (in_port_s && in_port_s[0]) {
4990
1.49k
        if (!ofputil_port_from_string(in_port_s, pp->port_map,
4991
1.49k
                                      &resubmit->in_port)) {
4992
3
            return xasprintf("%s: resubmit to unknown port", in_port_s);
4993
3
        }
4994
1.49k
    } else {
4995
589
        resubmit->in_port = OFPP_IN_PORT;
4996
589
    }
4997
4998
2.08k
    table_s = strsep(&arg, ",");
4999
2.08k
    if (table_s && table_s[0]) {
5000
637
        if (!ofputil_table_from_string(table_s, pp->table_map,
5001
637
                                       &resubmit->table_id)) {
5002
4
            return xasprintf("%s: resubmit to unknown table", table_s);
5003
4
        }
5004
1.44k
    } else {
5005
1.44k
        resubmit->table_id = 255;
5006
1.44k
    }
5007
5008
2.08k
    ct_s = strsep(&arg, ",");
5009
2.08k
    if (ct_s && ct_s[0]) {
5010
406
        if (strcmp(ct_s, "ct")) {
5011
6
            return xasprintf("%s: unknown parameter", ct_s);
5012
6
        }
5013
400
        resubmit->with_ct_orig = true;
5014
1.67k
    } else {
5015
1.67k
        resubmit->with_ct_orig = false;
5016
1.67k
    }
5017
5018
2.07k
    if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
5019
15
        return xstrdup("at least one \"in_port\" or \"table\" must be "
5020
15
                       "specified on resubmit");
5021
15
    }
5022
2.05k
    return NULL;
5023
2.07k
}
5024
5025
static void
5026
format_RESUBMIT(const struct ofpact_resubmit *a,
5027
                const struct ofpact_format_params *fp)
5028
2.05k
{
5029
2.05k
    if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
5030
132
        ds_put_format(fp->s, "%sresubmit:%s", colors.special, colors.end);
5031
132
        ofputil_format_port(a->in_port, fp->port_map, fp->s);
5032
1.91k
    } else {
5033
1.91k
        ds_put_format(fp->s, "%sresubmit(%s", colors.paren, colors.end);
5034
1.91k
        if (a->in_port != OFPP_IN_PORT) {
5035
1.83k
            ofputil_format_port(a->in_port, fp->port_map, fp->s);
5036
1.83k
        }
5037
1.91k
        ds_put_char(fp->s, ',');
5038
1.91k
        if (a->table_id != 255) {
5039
1.89k
            ofputil_format_table(a->table_id, fp->table_map, fp->s);
5040
1.89k
        }
5041
1.91k
        if (a->with_ct_orig) {
5042
1.82k
            ds_put_cstr(fp->s, ",ct");
5043
1.82k
        }
5044
1.91k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
5045
1.91k
    }
5046
2.05k
}
5047
5048
static enum ofperr
5049
check_RESUBMIT(const struct ofpact_resubmit *a,
5050
               const struct ofpact_check_params *cp)
5051
1.45k
{
5052
1.45k
    if (a->with_ct_orig && !is_ct_valid(&cp->match->flow, &cp->match->wc,
5053
406
                                        NULL)) {
5054
14
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5055
14
    }
5056
1.44k
    return 0;
5057
1.45k
}
5058

5059
/* Action structure for NXAST_LEARN and NXAST_LEARN2.
5060
 *
5061
 * This action adds or modifies a flow in an OpenFlow table, similar to
5062
 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'.  The new flow has the
5063
 * specified idle timeout, hard timeout, priority, cookie, and flags.  The new
5064
 * flow's match criteria and actions are built by applying each of the series
5065
 * of flow_mod_spec elements included as part of the action.
5066
 *
5067
 * A flow_mod_spec starts with a 16-bit header.  A header that is all-bits-0 is
5068
 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
5069
 * length.  Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
5070
 * bits from a source to a destination.  In this case, the header contains
5071
 * multiple fields:
5072
 *
5073
 *  15  14  13 12  11 10                              0
5074
 * +------+---+------+---------------------------------+
5075
 * |   0  |src|  dst |             n_bits              |
5076
 * +------+---+------+---------------------------------+
5077
 *
5078
 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'.  The
5079
 * following table summarizes the meaning of each possible combination.
5080
 * Details follow the table:
5081
 *
5082
 *   src dst  meaning
5083
 *   --- ---  ----------------------------------------------------------
5084
 *    0   0   Add match criteria based on value in a field.
5085
 *    1   0   Add match criteria based on an immediate value.
5086
 *    0   1   Add NXAST_REG_LOAD action to copy field into a different field.
5087
 *    1   1   Add NXAST_REG_LOAD action to load immediate value into a field.
5088
 *    0   2   Add OFPAT_OUTPUT action to output to port from specified field.
5089
 *   All other combinations are undefined and not allowed.
5090
 *
5091
 * The flow_mod_spec header is followed by a source specification and a
5092
 * destination specification.  The format and meaning of the source
5093
 * specification depends on 'src':
5094
 *
5095
 *   - If 'src' is 0, the source bits are taken from a field in the flow to
5096
 *     which this action is attached.  (This should be a wildcarded field.  If
5097
 *     its value is fully specified then the source bits being copied have
5098
 *     constant values.)
5099
 *
5100
 *     The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
5101
 *     'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
5102
 *     offset within that field.  The source bits are field[ofs:ofs+n_bits-1].
5103
 *     'field' and 'ofs' are subject to the same restrictions as the source
5104
 *     field in NXAST_REG_MOVE.
5105
 *
5106
 *   - If 'src' is 1, the source bits are a constant value.  The source
5107
 *     specification is (n_bits+15)/16*2 bytes long.  Taking those bytes as a
5108
 *     number in network order, the source bits are the 'n_bits'
5109
 *     least-significant bits.  The switch will report an error if other bits
5110
 *     in the constant are nonzero.
5111
 *
5112
 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
5113
 * ovs_be32 'field' and an ovs_be16 'ofs'.  'field' is an nxm_header with
5114
 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field.  The
5115
 * meaning of the flow_mod_spec depends on 'dst':
5116
 *
5117
 *   - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
5118
 *     flow.  The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
5119
 *     packet equal the source bits.  'field' may be any nxm_header with
5120
 *     nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
5121
 *
5122
 *     Order is significant.  Earlier flow_mod_specs must satisfy any
5123
 *     prerequisites for matching fields specified later, by copying constant
5124
 *     values into prerequisite fields.
5125
 *
5126
 *     The switch will reject flow_mod_specs that do not satisfy NXM masking
5127
 *     restrictions.
5128
 *
5129
 *   - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
5130
 *     the new flow.  The new flow copies the source bits into
5131
 *     field[ofs:ofs+n_bits-1].  Actions are executed in the same order as the
5132
 *     flow_mod_specs.
5133
 *
5134
 *     A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
5135
 *     greater than 64 yields multiple NXAST_REG_LOAD actions.
5136
 *
5137
 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
5138
 * empty.  It has the following meaning:
5139
 *
5140
 *   - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
5141
 *     The new flow outputs to the OpenFlow port specified by the source field.
5142
 *     Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
5143
 *     OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported.  Other special ports
5144
 *     may not be used.
5145
 *
5146
 * Resource Management
5147
 * -------------------
5148
 *
5149
 * A switch has a finite amount of flow table space available for learning.
5150
 * When this space is exhausted, no new learning table entries will be learned
5151
 * until some existing flow table entries expire.  The controller should be
5152
 * prepared to handle this by flooding (which can be implemented as a
5153
 * low-priority flow).
5154
 *
5155
 * If a learned flow matches a single TCP stream with a relatively long
5156
 * timeout, one may make the best of resource constraints by setting
5157
 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
5158
 * both, to shorter timeouts.  When either of these is specified as a nonzero
5159
 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
5160
 * the learned flow.
5161
 *
5162
 * Examples
5163
 * --------
5164
 *
5165
 * The following examples give a prose description of the flow_mod_specs along
5166
 * with informal notation for how those would be represented and a hex dump of
5167
 * the bytes that would be required.
5168
 *
5169
 * These examples could work with various nx_action_learn parameters.  Typical
5170
 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
5171
 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
5172
 *
5173
 * 1. Learn input port based on the source MAC, with lookup into
5174
 *    NXM_NX_REG1[16:31] by resubmit to in_port=99:
5175
 *
5176
 *    Match on in_port=99:
5177
 *       ovs_be16(src=1, dst=0, n_bits=16),               20 10
5178
 *       ovs_be16(99),                                    00 63
5179
 *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
5180
 *
5181
 *    Match Ethernet destination on Ethernet source from packet:
5182
 *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
5183
 *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
5184
 *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
5185
 *
5186
 *    Set NXM_NX_REG1[16:31] to the packet's input port:
5187
 *       ovs_be16(src=0, dst=1, n_bits=16),               08 10
5188
 *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
5189
 *       ovs_be32(NXM_NX_REG1), ovs_be16(16)              00 01 02 04 00 10
5190
 *
5191
 *    Given a packet that arrived on port A with Ethernet source address B,
5192
 *    this would set up the flow "in_port=99, dl_dst=B,
5193
 *    actions=load:A->NXM_NX_REG1[16..31]".
5194
 *
5195
 *    In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
5196
 *    NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5197
 *    load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
5198
 *
5199
 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
5200
 *    into NXM_NX_REG1[16:31]:
5201
 *
5202
 *    Match on same VLAN ID as packet:
5203
 *       ovs_be16(src=0, dst=0, n_bits=12),               00 0c
5204
 *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
5205
 *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
5206
 *
5207
 *    Match Ethernet destination on Ethernet source from packet:
5208
 *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
5209
 *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
5210
 *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
5211
 *
5212
 *    Output to the packet's input port:
5213
 *       ovs_be16(src=0, dst=2, n_bits=16),               10 10
5214
 *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
5215
 *
5216
 *    Given a packet that arrived on port A with Ethernet source address B in
5217
 *    VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
5218
 *    actions=output:A".
5219
 *
5220
 *    In syntax accepted by ovs-ofctl, this action is:
5221
 *    learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5222
 *    output:NXM_OF_IN_PORT[])
5223
 *
5224
 * 3. Here's a recipe for a very simple-minded MAC learning switch.  It uses a
5225
 *    10-second MAC expiration time to make it easier to see what's going on
5226
 *
5227
 *      ovs-vsctl del-controller br0
5228
 *      ovs-ofctl del-flows br0
5229
 *      ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
5230
          hard_timeout=10, NXM_OF_VLAN_TCI[0..11],             \
5231
          NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],                   \
5232
          output:NXM_OF_IN_PORT[]), resubmit(,1)"
5233
 *      ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
5234
 *
5235
 *    You can then dump the MAC learning table with:
5236
 *
5237
 *      ovs-ofctl dump-flows br0 table=1
5238
 *
5239
 * Usage Advice
5240
 * ------------
5241
 *
5242
 * For best performance, segregate learned flows into a table that is not used
5243
 * for any other flows except possibly for a lowest-priority "catch-all" flow
5244
 * (a flow with no match criteria).  If different learning actions specify
5245
 * different match criteria, use different tables for the learned flows.
5246
 *
5247
 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
5248
 * These timeouts apply to the flow that is added, which means that a flow with
5249
 * an idle timeout will expire when no traffic has been sent *to* the learned
5250
 * address.  This is not usually the intent in MAC learning; instead, we want
5251
 * the MAC learn entry to expire when no traffic has been sent *from* the
5252
 * learned address.  Use a hard timeout for that.
5253
 *
5254
 *
5255
 * Visibility of Changes
5256
 * ---------------------
5257
 *
5258
 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
5259
 * flow translation are visible to flow table lookups made later in the flow
5260
 * translation.  This means that, in the example above, a MAC learned by the
5261
 * learn action in table 0 would be found in table 1 (if the packet being
5262
 * processed had the same source and destination MAC address).
5263
 *
5264
 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
5265
 * modify a flow) by a "learn" action are visible only for later flow
5266
 * translations, not for later lookups within the same flow translation.  In
5267
 * the MAC learning example, a MAC learned by the learn action in table 0 would
5268
 * not be found in table 1 if the flow translation would resubmit to table 1
5269
 * after the processing of the learn action, meaning that if this MAC had not
5270
 * been learned before then the packet would be flooded. */
5271
struct nx_action_learn {
5272
    ovs_be16 type;              /* OFPAT_VENDOR. */
5273
    ovs_be16 len;               /* At least 24. */
5274
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
5275
    ovs_be16 subtype;           /* NXAST_LEARN. */
5276
    ovs_be16 idle_timeout;      /* Idle time before discarding (seconds). */
5277
    ovs_be16 hard_timeout;      /* Max time before discarding (seconds). */
5278
    ovs_be16 priority;          /* Priority level of flow entry. */
5279
    ovs_be64 cookie;            /* Cookie for new flow. */
5280
    ovs_be16 flags;             /* NX_LEARN_F_*. */
5281
    uint8_t table_id;           /* Table to insert flow entry. */
5282
    uint8_t pad;                /* Must be zero. */
5283
    ovs_be16 fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
5284
    ovs_be16 fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
5285
    /* Followed by a sequence of flow_mod_spec elements, as described above,
5286
     * until the end of the action is reached. */
5287
};
5288
OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
5289
5290
struct nx_action_learn2 {
5291
    struct nx_action_learn up;  /* The wire format includes nx_action_learn. */
5292
    ovs_be32 limit;             /* Maximum number of learned flows.
5293
                                 * 0 indicates unlimited. */
5294
5295
    /* Where to store the result. */
5296
    ovs_be16 result_dst_ofs;    /* Starting bit offset in destination. */
5297
5298
    ovs_be16 pad2;              /* Must be zero. */
5299
    /* Followed by:
5300
     * - OXM/NXM header for destination field (4 or 8 bytes),
5301
     *   if NX_LEARN_F_WRITE_RESULT is set in 'flags'
5302
     * - a sequence of flow_mod_spec elements, as described above,
5303
     *   until the end of the action is reached. */
5304
};
5305
OFP_ASSERT(sizeof(struct nx_action_learn2) == 40);
5306
5307
static ovs_be16
5308
get_be16(const void **pp)
5309
88.9k
{
5310
88.9k
    const ovs_be16 *p = *pp;
5311
88.9k
    ovs_be16 value = *p;
5312
88.9k
    *pp = p + 1;
5313
88.9k
    return value;
5314
88.9k
}
5315
5316
static ovs_be32
5317
get_be32(const void **pp)
5318
39.2k
{
5319
39.2k
    const ovs_be32 *p = *pp;
5320
39.2k
    ovs_be32 value = get_unaligned_be32(p);
5321
39.2k
    *pp = p + 1;
5322
39.2k
    return value;
5323
39.2k
}
5324
5325
static enum ofperr
5326
get_subfield(int n_bits, const void **p, struct mf_subfield *sf,
5327
             const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap)
5328
39.2k
{
5329
39.2k
    enum ofperr error;
5330
5331
39.2k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(get_be32(p)), vl_mff_map,
5332
39.2k
                                         &sf->field, tlv_bitmap);
5333
39.2k
    sf->ofs = ntohs(get_be16(p));
5334
39.2k
    sf->n_bits = n_bits;
5335
39.2k
    return error;
5336
39.2k
}
5337
5338
static unsigned int
5339
learn_min_len(uint16_t header)
5340
39.8k
{
5341
39.8k
    int n_bits = header & NX_LEARN_N_BITS_MASK;
5342
39.8k
    int src_type = header & NX_LEARN_SRC_MASK;
5343
39.8k
    int dst_type = header & NX_LEARN_DST_MASK;
5344
39.8k
    unsigned int min_len;
5345
5346
39.8k
    min_len = 0;
5347
39.8k
    if (src_type == NX_LEARN_SRC_FIELD) {
5348
1.05k
        min_len += sizeof(ovs_be32); /* src_field */
5349
1.05k
        min_len += sizeof(ovs_be16); /* src_ofs */
5350
38.8k
    } else {
5351
38.8k
        min_len += 2 * DIV_ROUND_UP(n_bits, 16);
5352
38.8k
    }
5353
39.8k
    if (dst_type == NX_LEARN_DST_MATCH ||
5354
39.5k
        dst_type == NX_LEARN_DST_LOAD) {
5355
39.5k
        min_len += sizeof(ovs_be32); /* dst_field */
5356
39.5k
        min_len += sizeof(ovs_be16); /* dst_ofs */
5357
39.5k
    }
5358
39.8k
    return min_len;
5359
39.8k
}
5360
5361
static enum ofperr
5362
decode_LEARN_common(const struct nx_action_learn *nal,
5363
                    enum ofp_raw_action_type raw,
5364
                    struct ofpact_learn *learn)
5365
24.7k
{
5366
24.7k
    if (nal->pad) {
5367
2.66k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5368
2.66k
    }
5369
5370
22.0k
    learn->ofpact.raw = raw;
5371
22.0k
    learn->idle_timeout = ntohs(nal->idle_timeout);
5372
22.0k
    learn->hard_timeout = ntohs(nal->hard_timeout);
5373
22.0k
    learn->priority = ntohs(nal->priority);
5374
22.0k
    learn->cookie = nal->cookie;
5375
22.0k
    learn->table_id = nal->table_id;
5376
22.0k
    learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
5377
22.0k
    learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
5378
22.0k
    learn->flags = ntohs(nal->flags);
5379
5380
22.0k
    if (learn->table_id == 0xff) {
5381
251
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5382
251
    }
5383
5384
21.8k
    return 0;
5385
22.0k
}
5386
5387
static enum ofperr
5388
decode_LEARN_specs(const void *p, const void *end,
5389
                   const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
5390
                   struct ofpbuf *ofpacts)
5391
21.1k
{
5392
21.1k
    struct ofpact_learn *learn = ofpacts->header;
5393
5394
58.8k
    while (p != end) {
5395
49.6k
        struct ofpact_learn_spec *spec;
5396
49.6k
        uint16_t header = ntohs(get_be16(&p));
5397
5398
49.6k
        if (!header) {
5399
9.39k
            break;
5400
9.39k
        }
5401
5402
40.2k
        spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
5403
40.2k
        learn = ofpacts->header;
5404
5405
40.2k
        spec->src_type = header & NX_LEARN_SRC_MASK;
5406
40.2k
        spec->dst_type = header & NX_LEARN_DST_MASK;
5407
40.2k
        spec->n_bits = header & NX_LEARN_N_BITS_MASK;
5408
5409
        /* Check for valid src and dst type combination. */
5410
40.2k
        if (spec->dst_type == NX_LEARN_DST_MATCH ||
5411
2.22k
            spec->dst_type == NX_LEARN_DST_LOAD ||
5412
712
            (spec->dst_type == NX_LEARN_DST_OUTPUT &&
5413
39.8k
             spec->src_type == NX_LEARN_SRC_FIELD)) {
5414
            /* OK. */
5415
39.8k
        } else {
5416
359
            return OFPERR_OFPBAC_BAD_ARGUMENT;
5417
359
        }
5418
5419
        /* Check that the arguments don't overrun the end of the action. */
5420
39.8k
        if ((char *) end - (char *) p < learn_min_len(header)) {
5421
789
            return OFPERR_OFPBAC_BAD_LEN;
5422
789
        }
5423
5424
        /* Get the source. */
5425
39.1k
        const uint8_t *imm = NULL;
5426
39.1k
        unsigned int imm_bytes = 0;
5427
39.1k
        enum ofperr error;
5428
39.1k
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
5429
644
            error = get_subfield(spec->n_bits, &p, &spec->src, vl_mff_map,
5430
644
                                 tlv_bitmap);
5431
644
            if (error) {
5432
226
                return error;
5433
226
            }
5434
38.4k
        } else {
5435
38.4k
            int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
5436
38.4k
            p = (const uint8_t *) p + p_bytes;
5437
5438
38.4k
            imm_bytes = DIV_ROUND_UP(spec->n_bits, 8);
5439
38.4k
            imm = (const uint8_t *) p - imm_bytes;
5440
38.4k
        }
5441
5442
        /* Get the destination. */
5443
38.8k
        if (spec->dst_type == NX_LEARN_DST_MATCH ||
5444
38.6k
            spec->dst_type == NX_LEARN_DST_LOAD) {
5445
38.6k
            error = get_subfield(spec->n_bits, &p, &spec->dst, vl_mff_map,
5446
38.6k
                                 tlv_bitmap);
5447
38.6k
            if (error) {
5448
1.18k
                return error;
5449
1.18k
            }
5450
38.6k
        }
5451
5452
37.6k
        if (imm) {
5453
37.2k
            uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
5454
37.2k
                                                OFPACT_ALIGN(imm_bytes));
5455
37.2k
            memcpy(src_imm, imm, imm_bytes);
5456
5457
37.2k
            learn = ofpacts->header;
5458
37.2k
        }
5459
37.6k
    }
5460
18.6k
    ofpact_finish_LEARN(ofpacts, &learn);
5461
5462
18.6k
    if (!is_all_zeros(p, (char *) end - (char *) p)) {
5463
207
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5464
207
    }
5465
5466
18.4k
    return 0;
5467
18.6k
}
5468
5469
/* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5470
 * 'ofpacts'.  Returns 0 if successful, otherwise an OFPERR_*. */
5471
static enum ofperr
5472
decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
5473
                       enum ofp_version ofp_version OVS_UNUSED,
5474
                       const struct vl_mff_map *vl_mff_map,
5475
                       uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
5476
22.7k
{
5477
22.7k
    struct ofpact_learn *learn;
5478
22.7k
    enum ofperr error;
5479
5480
22.7k
    learn = ofpact_put_LEARN(ofpacts);
5481
5482
22.7k
    error = decode_LEARN_common(nal, NXAST_RAW_LEARN, learn);
5483
22.7k
    if (error) {
5484
1.27k
        return error;
5485
1.27k
    }
5486
5487
21.5k
    if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
5488
21.5k
                         NX_LEARN_F_DELETE_LEARNED)) {
5489
478
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5490
478
    }
5491
5492
21.0k
    return decode_LEARN_specs(nal + 1, (char *) nal + ntohs(nal->len),
5493
21.0k
                              vl_mff_map, tlv_bitmap, ofpacts);
5494
21.5k
}
5495
5496
/* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5497
 * 'ofpacts'.  Returns 0 if successful, otherwise an OFPERR_*. */
5498
static enum ofperr
5499
decode_NXAST_RAW_LEARN2(const struct nx_action_learn2 *nal,
5500
                        enum ofp_version ofp_version OVS_UNUSED,
5501
                        const struct vl_mff_map *vl_mff_map,
5502
                        uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
5503
2.65k
{
5504
2.65k
    struct ofpbuf b = ofpbuf_const_initializer(nal, ntohs(nal->up.len));
5505
2.65k
    struct ofpact_learn *learn;
5506
2.65k
    enum ofperr error;
5507
5508
2.65k
    if (nal->pad2) {
5509
706
        return OFPERR_NXBAC_MUST_BE_ZERO;
5510
706
    }
5511
5512
1.94k
    learn = ofpact_put_LEARN(ofpacts);
5513
1.94k
    error = decode_LEARN_common(&nal->up, NXAST_RAW_LEARN2, learn);
5514
1.94k
    if (error) {
5515
1.64k
        return error;
5516
1.64k
    }
5517
5518
304
    learn->limit = ntohl(nal->limit);
5519
5520
304
    if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
5521
304
                         NX_LEARN_F_DELETE_LEARNED |
5522
304
                         NX_LEARN_F_WRITE_RESULT)) {
5523
19
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5524
19
    }
5525
5526
285
    ofpbuf_pull(&b, sizeof *nal);
5527
5528
285
    if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
5529
190
        error = nx_pull_header(&b, vl_mff_map, &learn->result_dst.field, NULL);
5530
190
        if (error) {
5531
99
            return error;
5532
99
        }
5533
91
        if (!learn->result_dst.field->writable) {
5534
14
            return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
5535
14
        }
5536
77
        learn->result_dst.ofs = ntohs(nal->result_dst_ofs);
5537
77
        learn->result_dst.n_bits = 1;
5538
95
    } else if (nal->result_dst_ofs) {
5539
19
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5540
19
    }
5541
5542
153
    return decode_LEARN_specs(b.data, (char *) nal + ntohs(nal->up.len),
5543
153
                              vl_mff_map, tlv_bitmap, ofpacts);
5544
285
}
5545
5546
static void
5547
put_be16(struct ofpbuf *b, ovs_be16 x)
5548
98.4k
{
5549
98.4k
    ofpbuf_put(b, &x, sizeof x);
5550
98.4k
}
5551
5552
static void
5553
put_be32(struct ofpbuf *b, ovs_be32 x)
5554
50.4k
{
5555
50.4k
    ofpbuf_put(b, &x, sizeof x);
5556
50.4k
}
5557
5558
static void
5559
put_u16(struct ofpbuf *b, uint16_t x)
5560
98.4k
{
5561
98.4k
    put_be16(b, htons(x));
5562
98.4k
}
5563
5564
static void
5565
put_u32(struct ofpbuf *b, uint32_t x)
5566
50.4k
{
5567
50.4k
    put_be32(b, htonl(x));
5568
50.4k
}
5569
5570
static void
5571
encode_LEARN(const struct ofpact_learn *learn,
5572
             enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5573
30.9k
{
5574
30.9k
    const struct ofpact_learn_spec *spec;
5575
30.9k
    struct nx_action_learn *nal;
5576
30.9k
    size_t start_ofs;
5577
5578
30.9k
    start_ofs = out->size;
5579
5580
30.9k
    if (learn->ofpact.raw == NXAST_RAW_LEARN2
5581
30.9k
        || learn->limit != 0
5582
30.5k
        || learn->flags & NX_LEARN_F_WRITE_RESULT) {
5583
644
        struct nx_action_learn2 *nal2;
5584
5585
644
        nal2 = put_NXAST_LEARN2(out);
5586
644
        nal2->limit = htonl(learn->limit);
5587
644
        nal2->result_dst_ofs = htons(learn->result_dst.ofs);
5588
644
        nal = &nal2->up;
5589
30.2k
    } else {
5590
30.2k
        nal = put_NXAST_LEARN(out);
5591
30.2k
    }
5592
30.9k
    nal->idle_timeout = htons(learn->idle_timeout);
5593
30.9k
    nal->hard_timeout = htons(learn->hard_timeout);
5594
30.9k
    nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
5595
30.9k
    nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
5596
30.9k
    nal->priority = htons(learn->priority);
5597
30.9k
    nal->cookie = learn->cookie;
5598
30.9k
    nal->flags = htons(learn->flags);
5599
30.9k
    nal->table_id = learn->table_id;
5600
5601
30.9k
    if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
5602
264
        nx_put_header(out, learn->result_dst.field->id, 0, false);
5603
264
    }
5604
5605
48.0k
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
5606
48.0k
        put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
5607
5608
48.0k
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
5609
2.81k
            put_u32(out, nxm_header_from_mff(spec->src.field));
5610
2.81k
            put_u16(out, spec->src.ofs);
5611
45.1k
        } else {
5612
45.1k
            size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
5613
45.1k
            uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
5614
45.1k
            unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
5615
5616
45.1k
            memcpy(bits + n_dst_bytes - n_bytes, ofpact_learn_spec_imm(spec),
5617
45.1k
                   n_bytes);
5618
45.1k
        }
5619
5620
48.0k
        if (spec->dst_type == NX_LEARN_DST_MATCH ||
5621
47.6k
            spec->dst_type == NX_LEARN_DST_LOAD) {
5622
47.6k
            put_u32(out, nxm_header_from_mff(spec->dst.field));
5623
47.6k
            put_u16(out, spec->dst.ofs);
5624
47.6k
        }
5625
48.0k
    }
5626
5627
30.9k
    pad_ofpat(out, start_ofs);
5628
30.9k
}
5629
5630
static char * OVS_WARN_UNUSED_RESULT
5631
parse_LEARN(char *arg, const struct ofpact_parse_params *pp)
5632
32.6k
{
5633
32.6k
    return learn_parse(arg, pp->port_map, pp->table_map, pp->ofpacts);
5634
32.6k
}
5635
5636
static void
5637
format_LEARN(const struct ofpact_learn *a,
5638
             const struct ofpact_format_params *fp)
5639
17.4k
{
5640
17.4k
    learn_format(a, fp->port_map, fp->table_map, fp->s);
5641
17.4k
}
5642
5643
static enum ofperr
5644
check_LEARN(const struct ofpact_learn *a,
5645
            const struct ofpact_check_params *cp)
5646
48.8k
{
5647
48.8k
    return learn_check(a, cp->match);
5648
48.8k
}
5649

5650
/* Action structure for NXAST_CONJUNCTION. */
5651
struct nx_action_conjunction {
5652
    ovs_be16 type;                  /* OFPAT_VENDOR. */
5653
    ovs_be16 len;                   /* At least 16. */
5654
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
5655
    ovs_be16 subtype;               /* See enum ofp_raw_action_type. */
5656
    uint8_t clause;
5657
    uint8_t n_clauses;
5658
    ovs_be32 id;
5659
};
5660
OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
5661
5662
static void
5663
add_conjunction(struct ofpbuf *out,
5664
                uint32_t id, uint8_t clause, uint8_t n_clauses)
5665
3.48k
{
5666
3.48k
    struct ofpact_conjunction *oc;
5667
5668
3.48k
    oc = ofpact_put_CONJUNCTION(out);
5669
3.48k
    oc->id = id;
5670
3.48k
    oc->clause = clause;
5671
3.48k
    oc->n_clauses = n_clauses;
5672
3.48k
}
5673
5674
static enum ofperr
5675
decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
5676
                             enum ofp_version ofp_version OVS_UNUSED,
5677
                             struct ofpbuf *out)
5678
3.96k
{
5679
3.96k
    if (nac->n_clauses < 2 || nac->n_clauses > 64
5680
3.10k
        || nac->clause >= nac->n_clauses) {
5681
1.13k
        return OFPERR_NXBAC_BAD_CONJUNCTION;
5682
2.82k
    } else {
5683
2.82k
        add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
5684
2.82k
        return 0;
5685
2.82k
    }
5686
3.96k
}
5687
5688
static void
5689
encode_CONJUNCTION(const struct ofpact_conjunction *oc,
5690
                   enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5691
351
{
5692
351
    struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
5693
351
    nac->clause = oc->clause;
5694
351
    nac->n_clauses = oc->n_clauses;
5695
351
    nac->id = htonl(oc->id);
5696
351
}
5697
5698
static void
5699
format_CONJUNCTION(const struct ofpact_conjunction *oc,
5700
                   const struct ofpact_format_params *fp)
5701
1.22k
{
5702
1.22k
    ds_put_format(fp->s, "%sconjunction(%s%"PRIu32",%d/%"PRIu8"%s)%s",
5703
1.22k
                  colors.paren, colors.end,
5704
1.22k
                  oc->id, oc->clause + 1, oc->n_clauses,
5705
1.22k
                  colors.paren, colors.end);
5706
1.22k
}
5707
5708
static char * OVS_WARN_UNUSED_RESULT
5709
parse_CONJUNCTION(const char *arg, const struct ofpact_parse_params *pp)
5710
684
{
5711
684
    uint8_t n_clauses;
5712
684
    uint8_t clause;
5713
684
    uint32_t id;
5714
684
    int n;
5715
5716
684
    if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
5717
684
                  &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
5718
20
        return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
5719
20
    }
5720
5721
664
    if (n_clauses < 2) {
5722
1
        return xstrdup("conjunction must have at least 2 clauses");
5723
663
    } else if (n_clauses > 64) {
5724
1
        return xstrdup("conjunction must have at most 64 clauses");
5725
662
    } else if (clause < 1) {
5726
2
        return xstrdup("clause index must be positive");
5727
660
    } else if (clause > n_clauses) {
5728
3
        return xstrdup("clause index must be less than or equal to "
5729
3
                       "number of clauses");
5730
3
    }
5731
5732
657
    add_conjunction(pp->ofpacts, id, clause - 1, n_clauses);
5733
657
    return NULL;
5734
664
}
5735
5736
static enum ofperr
5737
check_CONJUNCTION(const struct ofpact_conjunction *a OVS_UNUSED,
5738
                  const struct ofpact_check_params *cp OVS_UNUSED)
5739
488
{
5740
488
    return 0;
5741
488
}
5742

5743
/* Action structure for NXAST_MULTIPATH.
5744
 *
5745
 * This action performs the following steps in sequence:
5746
 *
5747
 *    1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
5748
 *       Refer to the definition of "enum nx_mp_fields" for details.
5749
 *
5750
 *       The 'basis' value is used as a universal hash parameter, that is,
5751
 *       different values of 'basis' yield different hash functions.  The
5752
 *       particular universal hash function used is implementation-defined.
5753
 *
5754
 *       The hashed fields' values are drawn from the current state of the
5755
 *       flow, including all modifications that have been made by actions up to
5756
 *       this point.
5757
 *
5758
 *    2. Applies the multipath link choice algorithm specified by 'algorithm',
5759
 *       one of NX_MP_ALG_*.  Refer to the definition of "enum nx_mp_algorithm"
5760
 *       for details.
5761
 *
5762
 *       The output of the algorithm is 'link', an unsigned integer less than
5763
 *       or equal to 'max_link'.
5764
 *
5765
 *       Some algorithms use 'arg' as an additional argument.
5766
 *
5767
 *    3. Stores 'link' in dst[ofs:ofs+n_bits].  The format and semantics of
5768
 *       'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
5769
 *       action.
5770
 *
5771
 * The switch will reject actions that have an unknown 'fields', or an unknown
5772
 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
5773
 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
5774
 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
5775
 */
5776
struct nx_action_multipath {
5777
    ovs_be16 type;              /* OFPAT_VENDOR. */
5778
    ovs_be16 len;               /* Length is 32. */
5779
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
5780
    ovs_be16 subtype;           /* NXAST_MULTIPATH. */
5781
5782
    /* What fields to hash and how. */
5783
    ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
5784
    ovs_be16 basis;             /* Universal hash parameter. */
5785
    ovs_be16 pad0;
5786
5787
    /* Multipath link choice algorithm to apply to hash value. */
5788
    ovs_be16 algorithm;         /* One of NX_MP_ALG_*. */
5789
    ovs_be16 max_link;          /* Number of output links, minus 1. */
5790
    ovs_be32 arg;               /* Algorithm-specific argument. */
5791
    ovs_be16 pad1;
5792
5793
    /* Where to store the result. */
5794
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
5795
    ovs_be32 dst;               /* Destination. */
5796
};
5797
OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
5798
5799
static enum ofperr
5800
decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
5801
                           enum ofp_version ofp_version OVS_UNUSED,
5802
                           const struct vl_mff_map *vl_mff_map,
5803
                           uint64_t *tlv_bitmap, struct ofpbuf *out)
5804
5.43k
{
5805
5.43k
    uint32_t n_links = ntohs(nam->max_link) + 1;
5806
5.43k
    size_t min_n_bits = log_2_ceil(n_links);
5807
5.43k
    struct ofpact_multipath *mp;
5808
5.43k
    enum ofperr error;
5809
5810
5.43k
    mp = ofpact_put_MULTIPATH(out);
5811
5.43k
    mp->fields = ntohs(nam->fields);
5812
5.43k
    mp->basis = ntohs(nam->basis);
5813
5.43k
    mp->algorithm = ntohs(nam->algorithm);
5814
5.43k
    mp->max_link = ntohs(nam->max_link);
5815
5.43k
    mp->arg = ntohl(nam->arg);
5816
5.43k
    mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
5817
5.43k
    mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
5818
5.43k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(nam->dst), vl_mff_map,
5819
5.43k
                                         &mp->dst.field, tlv_bitmap);
5820
5.43k
    if (error) {
5821
343
        return error;
5822
343
    }
5823
5824
5.09k
    if (!flow_hash_fields_valid(mp->fields)) {
5825
359
        VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
5826
359
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5827
4.73k
    } else if (mp->algorithm != NX_MP_ALG_MODULO_N
5828
4.52k
               && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
5829
4.48k
               && mp->algorithm != NX_MP_ALG_HRW
5830
3.96k
               && mp->algorithm != NX_MP_ALG_ITER_HASH) {
5831
1.28k
        VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
5832
1.28k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5833
3.44k
    } else if (mp->dst.n_bits < min_n_bits) {
5834
22
        VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
5835
22
                     "%"PRIu32" links", min_n_bits, n_links);
5836
22
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5837
22
    }
5838
5839
3.42k
    return multipath_check(mp, NULL);
5840
5.09k
}
5841
5842
static void
5843
encode_MULTIPATH(const struct ofpact_multipath *mp,
5844
                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5845
445
{
5846
445
    struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
5847
5848
445
    nam->fields = htons(mp->fields);
5849
445
    nam->basis = htons(mp->basis);
5850
445
    nam->algorithm = htons(mp->algorithm);
5851
445
    nam->max_link = htons(mp->max_link);
5852
445
    nam->arg = htonl(mp->arg);
5853
445
    nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
5854
445
    nam->dst = htonl(nxm_header_from_mff(mp->dst.field));
5855
445
}
5856
5857
static char * OVS_WARN_UNUSED_RESULT
5858
parse_MULTIPATH(const char *arg, const struct ofpact_parse_params *pp)
5859
2.45k
{
5860
2.45k
    return multipath_parse(ofpact_put_MULTIPATH(pp->ofpacts), arg);
5861
2.45k
}
5862
5863
static void
5864
format_MULTIPATH(const struct ofpact_multipath *a,
5865
                 const struct ofpact_format_params *fp)
5866
2.58k
{
5867
2.58k
    multipath_format(a, fp->s);
5868
2.58k
}
5869
5870
static enum ofperr
5871
check_MULTIPATH(const struct ofpact_multipath *a,
5872
                const struct ofpact_check_params *cp)
5873
919
{
5874
919
    return multipath_check(a, cp->match);
5875
919
}
5876

5877
/* Action structure for NXAST_NOTE.
5878
 *
5879
 * This action has no effect.  It is variable length.  The switch does not
5880
 * attempt to interpret the user-defined 'note' data in any way.  A controller
5881
 * can use this action to attach arbitrary metadata to a flow.
5882
 *
5883
 * This action might go away in the future.
5884
 */
5885
struct nx_action_note {
5886
    ovs_be16 type;                  /* OFPAT_VENDOR. */
5887
    ovs_be16 len;                   /* A multiple of 8, but at least 16. */
5888
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
5889
    ovs_be16 subtype;               /* NXAST_NOTE. */
5890
    uint8_t note[6];                /* Start of user-defined data. */
5891
    /* Possibly followed by additional user-defined data. */
5892
};
5893
OFP_ASSERT(sizeof(struct nx_action_note) == 16);
5894
5895
static enum ofperr
5896
decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
5897
                      enum ofp_version ofp_version OVS_UNUSED,
5898
                      struct ofpbuf *out)
5899
8.56k
{
5900
8.56k
    struct ofpact_note *note;
5901
8.56k
    unsigned int length;
5902
5903
8.56k
    length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
5904
8.56k
    note = ofpact_put_NOTE(out);
5905
8.56k
    note->length = length;
5906
8.56k
    ofpbuf_put(out, nan->note, length);
5907
8.56k
    note = out->header;
5908
8.56k
    ofpact_finish_NOTE(out, &note);
5909
5910
8.56k
    return 0;
5911
8.56k
}
5912
5913
static void
5914
encode_NOTE(const struct ofpact_note *note,
5915
            enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5916
1.11k
{
5917
1.11k
    size_t start_ofs = out->size;
5918
1.11k
    struct nx_action_note *nan;
5919
5920
1.11k
    put_NXAST_NOTE(out);
5921
1.11k
    out->size = out->size - sizeof nan->note;
5922
5923
1.11k
    ofpbuf_put(out, note->data, note->length);
5924
1.11k
    pad_ofpat(out, start_ofs);
5925
1.11k
}
5926
5927
static char * OVS_WARN_UNUSED_RESULT
5928
parse_NOTE(const char *arg, const struct ofpact_parse_params *pp)
5929
1.50k
{
5930
1.50k
    size_t start_ofs = pp->ofpacts->size;
5931
1.50k
    ofpact_put_NOTE(pp->ofpacts);
5932
1.50k
    arg = ofpbuf_put_hex(pp->ofpacts, arg, NULL);
5933
1.50k
    if (arg[0]) {
5934
13
        return xstrdup("bad hex digit in `note' argument");
5935
13
    }
5936
1.49k
    struct ofpact_note *note = ofpbuf_at_assert(pp->ofpacts, start_ofs,
5937
1.49k
                                                sizeof *note);
5938
1.49k
    note->length = pp->ofpacts->size - (start_ofs + sizeof *note);
5939
5940
1.49k
    if (ofpbuf_oversized(pp->ofpacts)) {
5941
1
        return xasprintf("input too big");
5942
1
    }
5943
5944
1.49k
    ofpact_finish_NOTE(pp->ofpacts, &note);
5945
1.49k
    return NULL;
5946
1.49k
}
5947
5948
static void
5949
format_NOTE(const struct ofpact_note *a,
5950
            const struct ofpact_format_params *fp)
5951
2.64k
{
5952
2.64k
    ds_put_format(fp->s, "%snote:%s", colors.param, colors.end);
5953
2.64k
    ds_put_hex_with_delimiter(fp->s, a->data, a->length, ".");
5954
2.64k
}
5955
5956
static enum ofperr
5957
check_NOTE(const struct ofpact_note *a OVS_UNUSED,
5958
           const struct ofpact_check_params *cp OVS_UNUSED)
5959
1.25k
{
5960
1.25k
    return 0;
5961
1.25k
}
5962

5963
/* Exit action. */
5964
5965
static enum ofperr
5966
decode_NXAST_RAW_EXIT(struct ofpbuf *out)
5967
2.14k
{
5968
2.14k
    ofpact_put_EXIT(out);
5969
2.14k
    return 0;
5970
2.14k
}
5971
5972
static void
5973
encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
5974
            enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5975
2.13k
{
5976
2.13k
    put_NXAST_EXIT(out);
5977
2.13k
}
5978
5979
static char * OVS_WARN_UNUSED_RESULT
5980
parse_EXIT(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
5981
2.77k
{
5982
2.77k
    ofpact_put_EXIT(pp->ofpacts);
5983
2.77k
    return NULL;
5984
2.77k
}
5985
5986
static void
5987
format_EXIT(const struct ofpact_null *a OVS_UNUSED,
5988
            const struct ofpact_format_params *fp)
5989
1.97k
{
5990
1.97k
    ds_put_format(fp->s, "%sexit%s", colors.special, colors.end);
5991
1.97k
}
5992
5993
static enum ofperr
5994
check_EXIT(const struct ofpact_null *a OVS_UNUSED,
5995
           const struct ofpact_check_params *cp OVS_UNUSED)
5996
2.31k
{
5997
2.31k
    return 0;
5998
2.31k
}
5999

6000
/* Unroll xlate action. */
6001
6002
static void
6003
encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
6004
                    enum ofp_version ofp_version OVS_UNUSED,
6005
                    struct ofpbuf *out OVS_UNUSED)
6006
0
{
6007
0
    OVS_NOT_REACHED();
6008
0
}
6009
6010
static char * OVS_WARN_UNUSED_RESULT
6011
parse_UNROLL_XLATE(char *arg OVS_UNUSED,
6012
                   const struct ofpact_parse_params *pp OVS_UNUSED)
6013
1
{
6014
1
    return xasprintf("UNROLL is an internal action "
6015
1
                     "that shouldn't be used via OpenFlow");
6016
1
}
6017
6018
static void
6019
format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a,
6020
                    const struct ofpact_format_params *fp)
6021
3.08k
{
6022
3.08k
    ds_put_format(fp->s, "%sunroll_xlate(%s%stable=%s",
6023
3.08k
                  colors.paren,   colors.end,
6024
3.08k
                  colors.special, colors.end);
6025
3.08k
    ofputil_format_table(a->rule_table_id, fp->table_map, fp->s);
6026
3.08k
    ds_put_format(fp->s, ", %scookie=%s%"PRIu64"%s)%s",
6027
3.08k
                  colors.param,   colors.end, ntohll(a->rule_cookie),
6028
3.08k
                  colors.paren,   colors.end);
6029
3.08k
}
6030
6031
static enum ofperr
6032
check_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
6033
                   const struct ofpact_check_params *cp OVS_UNUSED)
6034
0
{
6035
    /* UNROLL is an internal action that should never be seen via OpenFlow. */
6036
0
    return OFPERR_OFPBAC_BAD_TYPE;
6037
0
}
6038

6039
/* The NXAST_CLONE action is "struct ext_action_header", followed by zero or
6040
 * more embedded OpenFlow actions. */
6041
6042
static enum ofperr
6043
decode_NXAST_RAW_CLONE(const struct ext_action_header *eah,
6044
                       enum ofp_version ofp_version,
6045
                       const struct vl_mff_map *vl_mff_map,
6046
                       uint64_t *tlv_bitmap, struct ofpbuf *out)
6047
606
{
6048
606
    int error;
6049
606
    struct ofpbuf openflow;
6050
606
    const size_t clone_offset = ofpacts_pull(out);
6051
606
    struct ofpact_nest *clone = ofpact_put_CLONE(out);
6052
6053
    /* decode action list */
6054
606
    ofpbuf_pull(out, sizeof(*clone));
6055
606
    openflow = ofpbuf_const_initializer(
6056
606
                    eah + 1, ntohs(eah->len) - sizeof *eah);
6057
606
    error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
6058
606
                                            ofp_version,
6059
606
                                            1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6060
606
                                            out, 0, vl_mff_map, tlv_bitmap);
6061
606
    if (error) {
6062
37
        return error;
6063
37
    }
6064
569
    clone = ofpbuf_push_uninit(out, sizeof *clone);
6065
569
    out->header = &clone->ofpact;
6066
569
    ofpact_finish_CLONE(out, &clone);
6067
569
    ofpbuf_push_uninit(out, clone_offset);
6068
569
    return error;
6069
606
}
6070
6071
static void
6072
encode_CLONE(const struct ofpact_nest *clone,
6073
              enum ofp_version ofp_version, struct ofpbuf *out)
6074
4.38k
{
6075
4.38k
    size_t len;
6076
4.38k
    const size_t ofs = out->size;
6077
4.38k
    struct ext_action_header *eah;
6078
6079
4.38k
    put_NXAST_CLONE(out);
6080
4.38k
    len = ofpacts_put_openflow_actions(clone->actions,
6081
4.38k
                                       ofpact_nest_get_action_len(clone),
6082
4.38k
                                       out, ofp_version);
6083
4.38k
    len += sizeof *eah;
6084
4.38k
    eah = ofpbuf_at(out, ofs, sizeof *eah);
6085
4.38k
    eah->len = htons(len);
6086
4.38k
}
6087
6088
static char * OVS_WARN_UNUSED_RESULT
6089
parse_CLONE(char *arg, const struct ofpact_parse_params *pp)
6090
11.9k
{
6091
11.9k
    const size_t clone_offset = ofpacts_pull(pp->ofpacts);
6092
11.9k
    struct ofpact_nest *clone = ofpact_put_CLONE(pp->ofpacts);
6093
11.9k
    char *error;
6094
6095
11.9k
    ofpbuf_pull(pp->ofpacts, sizeof *clone);
6096
11.9k
    error = ofpacts_parse_copy(arg, pp, false, OFPACT_CLONE);
6097
    /* header points to the action list */
6098
11.9k
    pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, sizeof *clone);
6099
11.9k
    clone = pp->ofpacts->header;
6100
6101
11.9k
    if (ofpbuf_oversized(pp->ofpacts)) {
6102
3
        free(error);
6103
3
        return xasprintf("input too big");
6104
3
    }
6105
6106
11.9k
    ofpact_finish_CLONE(pp->ofpacts, &clone);
6107
11.9k
    ofpbuf_push_uninit(pp->ofpacts, clone_offset);
6108
11.9k
    return error;
6109
11.9k
}
6110
6111
static void
6112
format_CLONE(const struct ofpact_nest *a,
6113
             const struct ofpact_format_params *fp)
6114
497
{
6115
497
    ds_put_format(fp->s, "%sclone(%s", colors.paren, colors.end);
6116
497
    ofpacts_format(a->actions, ofpact_nest_get_action_len(a), fp);
6117
497
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6118
497
}
6119
6120
static enum ofperr
6121
check_subactions(struct ofpact *ofpacts, size_t ofpacts_len,
6122
                 struct ofpact_check_params *cp)
6123
15.2k
{
6124
15.2k
    struct ofpact_check_params sub = *cp;
6125
15.2k
    enum ofperr error = ofpacts_check(ofpacts, ofpacts_len, &sub);
6126
15.2k
    cp->usable_protocols &= sub.usable_protocols;
6127
15.2k
    return error;
6128
15.2k
}
6129
6130
static enum ofperr
6131
check_CLONE(struct ofpact_nest *a, struct ofpact_check_params *cp)
6132
4.49k
{
6133
4.49k
    return check_subactions(a->actions, ofpact_nest_get_action_len(a), cp);
6134
4.49k
}
6135

6136
/* Action structure for NXAST_SAMPLE.
6137
 *
6138
 * Samples matching packets with the given probability and sends them
6139
 * each to the set of collectors identified with the given ID.  The
6140
 * probability is expressed as a number of packets to be sampled out
6141
 * of USHRT_MAX packets, and must be >0.
6142
 *
6143
 * When sending packet samples to IPFIX collectors, the IPFIX flow
6144
 * record sent for each sampled packet is associated with the given
6145
 * observation domain ID and observation point ID.  Each IPFIX flow
6146
 * record contain the sampled packet's headers when executing this
6147
 * rule.  If a sampled packet's headers are modified by previous
6148
 * actions in the flow, those modified headers are sent. */
6149
struct nx_action_sample {
6150
    ovs_be16 type;                  /* OFPAT_VENDOR. */
6151
    ovs_be16 len;                   /* Length is 24. */
6152
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
6153
    ovs_be16 subtype;               /* NXAST_SAMPLE. */
6154
    ovs_be16 probability;           /* Fraction of packets to sample. */
6155
    ovs_be32 collector_set_id;      /* ID of collector set in OVSDB. */
6156
    ovs_be32 obs_domain_id;         /* ID of sampling observation domain. */
6157
    ovs_be32 obs_point_id;          /* ID of sampling observation point. */
6158
};
6159
OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
6160
6161
/* Action structure for NXAST_SAMPLE2 and NXAST_SAMPLE3.
6162
 *
6163
 * NXAST_SAMPLE2 was added in Open vSwitch 2.5.90.  Compared to NXAST_SAMPLE,
6164
 * it adds support for exporting egress tunnel information.
6165
 *
6166
 * NXAST_SAMPLE3 was added in Open vSwitch 2.6.90.  Compared to NXAST_SAMPLE2,
6167
 * it adds support for the 'direction' field. */
6168
struct nx_action_sample2 {
6169
    ovs_be16 type;                  /* OFPAT_VENDOR. */
6170
    ovs_be16 len;                   /* Length is 32. */
6171
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
6172
    ovs_be16 subtype;               /* NXAST_SAMPLE. */
6173
    ovs_be16 probability;           /* Fraction of packets to sample. */
6174
    ovs_be32 collector_set_id;      /* ID of collector set in OVSDB. */
6175
    ovs_be32 obs_domain_id;         /* ID of sampling observation domain. */
6176
    ovs_be32 obs_point_id;          /* ID of sampling observation point. */
6177
    ovs_be16 sampling_port;         /* Sampling port. */
6178
    uint8_t  direction;             /* NXAST_SAMPLE3 only. */
6179
    uint8_t  zeros[5];              /* Pad to a multiple of 8 bytes */
6180
 };
6181
 OFP_ASSERT(sizeof(struct nx_action_sample2) == 32);
6182
6183
/* Action structure for NXAST_SAMPLE4
6184
 *
6185
 * NXAST_SAMPLE4 was added in Open vSwitch 3.4.0.  Compared to NXAST_SAMPLE3,
6186
 * it adds support for using field specifiers for observation_domain_id and
6187
 * observation_point_id. */
6188
struct nx_action_sample4 {
6189
    ovs_be16 type;                      /* OFPAT_VENDOR. */
6190
    ovs_be16 len;                       /* Length is 40. */
6191
    ovs_be32 vendor;                    /* NX_VENDOR_ID. */
6192
    ovs_be16 subtype;                   /* NXAST_SAMPLE4. */
6193
    ovs_be16 probability;               /* Fraction of packets to sample. */
6194
    ovs_be32 collector_set_id;          /* ID of collector set in OVSDB. */
6195
    ovs_be32 obs_domain_src;            /* The observation_domain_id source. */
6196
    union {
6197
        ovs_be16 obs_domain_ofs_nbits;  /* Range to use from source field. */
6198
        ovs_be32 obs_domain_imm;        /* Immediate value for domain id. */
6199
    };
6200
    ovs_be32 obs_point_src;             /* The observation_point_id source. */
6201
    union {
6202
        ovs_be16 obs_point_ofs_nbits;   /* Range to use from source field. */
6203
        ovs_be32 obs_point_imm;         /* Immediate value for point id. */
6204
    };
6205
    ovs_be16 sampling_port;             /* Sampling port. */
6206
    uint8_t direction;                  /* Sampling direction. */
6207
    uint8_t zeros[5];                   /* Pad to a multiple of 8 bytes */
6208
 };
6209
 OFP_ASSERT(sizeof(struct nx_action_sample4) == 40);
6210
6211
static enum ofperr
6212
decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
6213
                        enum ofp_version ofp_version OVS_UNUSED,
6214
                        struct ofpbuf *out)
6215
117
{
6216
117
    struct ofpact_sample *sample;
6217
6218
117
    sample = ofpact_put_SAMPLE(out);
6219
117
    sample->ofpact.raw = NXAST_RAW_SAMPLE;
6220
117
    sample->probability = ntohs(nas->probability);
6221
117
    sample->collector_set_id = ntohl(nas->collector_set_id);
6222
117
    sample->obs_domain_imm = ntohl(nas->obs_domain_id);
6223
117
    sample->obs_domain_src.field = NULL;
6224
117
    sample->obs_point_imm = ntohl(nas->obs_point_id);
6225
117
    sample->obs_point_src.field = NULL;
6226
117
    sample->sampling_port = OFPP_NONE;
6227
117
    sample->direction = NX_ACTION_SAMPLE_DEFAULT;
6228
117
    sample->obs_domain_src.field = NULL;
6229
117
    sample->obs_point_src.field = NULL;
6230
117
    if (sample->probability == 0) {
6231
11
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6232
11
    }
6233
6234
106
    return 0;
6235
117
}
6236
6237
static enum ofperr
6238
decode_SAMPLE2(const struct nx_action_sample2 *nas,
6239
               enum ofp_raw_action_type raw,
6240
               enum nx_action_sample_direction direction,
6241
               struct ofpact_sample *sample)
6242
1.76k
{
6243
1.76k
    sample->ofpact.raw = raw;
6244
1.76k
    sample->probability = ntohs(nas->probability);
6245
1.76k
    sample->collector_set_id = ntohl(nas->collector_set_id);
6246
1.76k
    sample->obs_domain_imm = ntohl(nas->obs_domain_id);
6247
1.76k
    sample->obs_domain_src.field = NULL;
6248
1.76k
    sample->obs_point_imm = ntohl(nas->obs_point_id);
6249
1.76k
    sample->obs_point_src.field = NULL;
6250
1.76k
    sample->sampling_port = u16_to_ofp(ntohs(nas->sampling_port));
6251
1.76k
    sample->direction = direction;
6252
6253
1.76k
    if (sample->probability == 0) {
6254
469
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6255
469
    }
6256
6257
1.29k
    return 0;
6258
1.76k
}
6259
6260
static enum ofperr
6261
decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2 *nas,
6262
                         enum ofp_version ofp_version OVS_UNUSED,
6263
                         struct ofpbuf *out)
6264
74
{
6265
74
    return decode_SAMPLE2(nas, NXAST_RAW_SAMPLE2, NX_ACTION_SAMPLE_DEFAULT,
6266
74
                          ofpact_put_SAMPLE(out));
6267
74
}
6268
6269
static int
6270
check_sample_direction(enum nx_action_sample_direction direction)
6271
2.79k
{
6272
2.79k
    if (direction != NX_ACTION_SAMPLE_DEFAULT &&
6273
1.01k
        direction != NX_ACTION_SAMPLE_INGRESS &&
6274
828
        direction != NX_ACTION_SAMPLE_EGRESS) {
6275
197
        VLOG_WARN_RL(&rl, "invalid sample direction %"PRIu8, direction);
6276
197
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6277
197
    }
6278
2.59k
    return 0;
6279
2.79k
}
6280
6281
static enum ofperr
6282
decode_NXAST_RAW_SAMPLE3(const struct nx_action_sample2 *nas,
6283
                         enum ofp_version ofp_version OVS_UNUSED,
6284
                         struct ofpbuf *out)
6285
2.22k
{
6286
2.22k
    struct ofpact_sample *sample = ofpact_put_SAMPLE(out);
6287
2.22k
    int err;
6288
6289
2.22k
    if (!is_all_zeros(nas->zeros, sizeof nas->zeros)) {
6290
465
        return OFPERR_NXBRC_MUST_BE_ZERO;
6291
465
    }
6292
1.76k
    err = check_sample_direction(nas->direction);
6293
1.76k
    if (err) {
6294
68
        return err;
6295
68
    }
6296
1.69k
    return decode_SAMPLE2(nas, NXAST_RAW_SAMPLE3, nas->direction, sample);
6297
1.76k
}
6298
6299
static int
6300
decode_sample_obs_id(ovs_be32 src, ovs_be16 ofs_nbits, ovs_be32 imm,
6301
                     const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
6302
                     struct mf_subfield *src_out, uint32_t *imm_out)
6303
1.63k
{
6304
1.63k
    if (src) {
6305
903
        enum ofperr error;
6306
6307
903
        src_out->ofs = nxm_decode_ofs(ofs_nbits);
6308
903
        src_out->n_bits = nxm_decode_n_bits(ofs_nbits);
6309
903
        error = mf_vl_mff_mf_from_nxm_header(ntohl(src),
6310
903
                                             vl_mff_map, &src_out->field,
6311
903
                                             tlv_bitmap);
6312
903
        if (error) {
6313
288
            return error;
6314
288
        }
6315
6316
615
        error = mf_check_src(src_out, NULL);
6317
615
        if (error) {
6318
58
            return error;
6319
58
        }
6320
6321
557
        if (src_out->n_bits > 32) {
6322
55
            VLOG_WARN_RL(&rl, "size of field used in observation_id (%d) "
6323
55
                               "exceeds maximum (32)", src_out->n_bits);
6324
55
            return OFPERR_OFPBAC_BAD_ARGUMENT;
6325
55
        }
6326
730
    } else {
6327
730
        src_out->field = NULL;
6328
730
        *imm_out = ntohl(imm);
6329
730
    }
6330
6331
1.23k
    return 0;
6332
1.63k
}
6333
6334
static enum ofperr
6335
decode_NXAST_RAW_SAMPLE4(const struct nx_action_sample4 *nas,
6336
                         enum ofp_version ofp_version OVS_UNUSED,
6337
                         const struct vl_mff_map *vl_mff_map,
6338
                         uint64_t *tlv_bitmap,
6339
                         struct ofpbuf *out)
6340
1.14k
{
6341
1.14k
    struct ofpact_sample *sample = ofpact_put_SAMPLE(out);
6342
1.14k
    int err;
6343
6344
1.14k
    if (!is_all_zeros(nas->zeros, sizeof nas->zeros)) {
6345
109
        return OFPERR_NXBRC_MUST_BE_ZERO;
6346
109
    }
6347
6348
1.03k
    err = check_sample_direction(nas->direction);
6349
1.03k
    if (err) {
6350
129
        return err;
6351
129
    }
6352
6353
905
    sample->ofpact.raw = NXAST_RAW_SAMPLE4;
6354
905
    sample->probability = ntohs(nas->probability);
6355
905
    sample->collector_set_id = ntohl(nas->collector_set_id);
6356
905
    sample->sampling_port = u16_to_ofp(ntohs(nas->sampling_port));
6357
905
    sample->direction = nas->direction;
6358
6359
905
    if (sample->probability == 0) {
6360
6
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6361
6
    }
6362
6363
899
    err = decode_sample_obs_id(nas->obs_domain_src,
6364
899
                               nas->obs_domain_ofs_nbits,
6365
899
                               nas->obs_domain_imm,
6366
899
                               vl_mff_map, tlv_bitmap,
6367
899
                               &sample->obs_domain_src,
6368
899
                               &sample->obs_domain_imm);
6369
899
    if (err) {
6370
165
        return err;
6371
165
    }
6372
6373
734
    return decode_sample_obs_id(nas->obs_point_src,
6374
734
                                nas->obs_point_ofs_nbits,
6375
734
                                nas->obs_point_imm,
6376
734
                                vl_mff_map, tlv_bitmap,
6377
734
                                &sample->obs_point_src,
6378
734
                                &sample->obs_point_imm);
6379
899
}
6380
6381
static void
6382
encode_SAMPLE2(const struct ofpact_sample *sample,
6383
               struct nx_action_sample2 *nas)
6384
303
{
6385
303
    nas->probability = htons(sample->probability);
6386
303
    nas->collector_set_id = htonl(sample->collector_set_id);
6387
303
    nas->obs_domain_id = htonl(sample->obs_domain_imm);
6388
303
    nas->obs_point_id = htonl(sample->obs_point_imm);
6389
303
    nas->sampling_port = htons(ofp_to_u16(sample->sampling_port));
6390
303
    nas->direction = sample->direction;
6391
303
}
6392
6393
static void
6394
encode_SAMPLE4(const struct ofpact_sample *sample,
6395
               struct nx_action_sample4 *nas)
6396
437
{
6397
437
    nas->probability = htons(sample->probability);
6398
437
    nas->collector_set_id = htonl(sample->collector_set_id);
6399
437
    nas->sampling_port = htons(ofp_to_u16(sample->sampling_port));
6400
437
    nas->direction = sample->direction;
6401
6402
437
    if (sample->obs_domain_src.field) {
6403
243
        nas->obs_domain_src =
6404
243
            htonl(nxm_header_from_mff(sample->obs_domain_src.field));
6405
243
        nas->obs_domain_ofs_nbits =
6406
243
            nxm_encode_ofs_nbits(sample->obs_domain_src.ofs,
6407
243
                                 sample->obs_domain_src.n_bits);
6408
243
    } else {
6409
194
        nas->obs_domain_src = htonl(0);
6410
194
        nas->obs_domain_imm = htonl(sample->obs_domain_imm);
6411
194
    }
6412
437
    if (sample->obs_point_src.field) {
6413
194
        nas->obs_point_src =
6414
194
            htonl(nxm_header_from_mff(sample->obs_point_src.field));
6415
194
        nas->obs_point_ofs_nbits =
6416
194
            nxm_encode_ofs_nbits(sample->obs_point_src.ofs,
6417
194
                                 sample->obs_point_src.n_bits);
6418
243
    } else {
6419
243
        nas->obs_point_src = htonl(0);
6420
243
        nas->obs_point_imm = htonl(sample->obs_point_imm);
6421
243
    }
6422
437
}
6423
6424
static void
6425
encode_SAMPLE(const struct ofpact_sample *sample,
6426
              enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
6427
1.15k
{
6428
1.15k
    if (sample->ofpact.raw == NXAST_RAW_SAMPLE4 ||
6429
1.15k
        sample->obs_domain_src.field ||
6430
915
        sample->obs_point_src.field) {
6431
437
        encode_SAMPLE4(sample, put_NXAST_SAMPLE4(out));
6432
721
    } else if (sample->ofpact.raw == NXAST_RAW_SAMPLE3
6433
721
        || sample->direction != NX_ACTION_SAMPLE_DEFAULT) {
6434
90
        encode_SAMPLE2(sample, put_NXAST_SAMPLE3(out));
6435
631
    } else if (sample->ofpact.raw == NXAST_RAW_SAMPLE2
6436
631
               || sample->sampling_port != OFPP_NONE) {
6437
213
        encode_SAMPLE2(sample, put_NXAST_SAMPLE2(out));
6438
418
    } else {
6439
418
        struct nx_action_sample *nas = put_NXAST_SAMPLE(out);
6440
418
        nas->probability = htons(sample->probability);
6441
418
        nas->collector_set_id = htonl(sample->collector_set_id);
6442
418
        nas->obs_domain_id = htonl(sample->obs_domain_imm);
6443
418
        nas->obs_point_id = htonl(sample->obs_point_imm);
6444
418
    }
6445
1.15k
}
6446
6447
/* Parses 'arg' as the argument to a "sample" action, and appends such an
6448
 * action to 'pp->ofpacts'.
6449
 *
6450
 * Returns NULL if successful, otherwise a malloc()'d string describing the
6451
 * error.  The caller is responsible for freeing the returned string. */
6452
static char * OVS_WARN_UNUSED_RESULT
6453
parse_SAMPLE(char *arg, const struct ofpact_parse_params *pp)
6454
1.43k
{
6455
1.43k
    struct ofpact_sample *os = ofpact_put_SAMPLE(pp->ofpacts);
6456
1.43k
    os->sampling_port = OFPP_NONE;
6457
1.43k
    os->direction = NX_ACTION_SAMPLE_DEFAULT;
6458
6459
1.43k
    char *key, *value;
6460
4.72k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
6461
3.42k
        char *error = NULL;
6462
6463
3.42k
        if (!strcmp(key, "probability")) {
6464
1.28k
            error = str_to_u16(value, "probability", &os->probability);
6465
1.28k
            if (!error && os->probability == 0) {
6466
1
                error = xasprintf("invalid probability value \"%s\"", value);
6467
1
            }
6468
2.13k
        } else if (!strcmp(key, "collector_set_id")) {
6469
68
            error = str_to_u32(value, &os->collector_set_id);
6470
2.06k
        } else if (!strcmp(key, "obs_domain_id")) {
6471
580
            error = str_to_u32(value, &os->obs_domain_imm);
6472
6473
580
            if (error) {
6474
283
                free(error);
6475
283
                error = mf_parse_subfield(&os->obs_domain_src, value);
6476
283
                if (error) {
6477
1
                    return error;
6478
1
                }
6479
282
                if (os->obs_domain_src.n_bits > 32) {
6480
1
                    return xasprintf("size of obs_domain_id field (%d) "
6481
1
                                     "exceeds maximum (32)",
6482
1
                                     os->obs_domain_src.n_bits);
6483
1
                }
6484
282
            }
6485
1.48k
        } else if (!strcmp(key, "obs_point_id")) {
6486
533
            error = str_to_u32(value, &os->obs_point_imm);
6487
6488
533
            if (error) {
6489
244
                free(error);
6490
244
                error = mf_parse_subfield(&os->obs_point_src, value);
6491
244
                if (error) {
6492
4
                    return error;
6493
4
                }
6494
240
                if (os->obs_point_src.n_bits > 32) {
6495
1
                    return xasprintf("size of obs_point_id field (%d) "
6496
1
                                     "exceeds maximum (32)",
6497
1
                                     os->obs_point_src.n_bits);
6498
1
                }
6499
240
            }
6500
956
        } else if (!strcmp(key, "sampling_port")) {
6501
283
            if (!ofputil_port_from_string(value, pp->port_map,
6502
283
                                          &os->sampling_port)) {
6503
1
                error = xasprintf("%s: unknown port", value);
6504
1
            }
6505
673
        } else if (!strcmp(key, "ingress")) {
6506
445
            os->direction = NX_ACTION_SAMPLE_INGRESS;
6507
445
        } else if (!strcmp(key, "egress")) {
6508
112
            os->direction = NX_ACTION_SAMPLE_EGRESS;
6509
116
        } else {
6510
116
            error = xasprintf("invalid key \"%s\" in \"sample\" argument",
6511
116
                              key);
6512
116
        }
6513
3.41k
        if (error) {
6514
126
            return error;
6515
126
        }
6516
3.41k
    }
6517
1.29k
    if (os->probability == 0) {
6518
29
        return xstrdup("non-zero \"probability\" must be specified on sample");
6519
29
    }
6520
6521
1.26k
    return NULL;
6522
1.29k
}
6523
6524
static void
6525
format_SAMPLE(const struct ofpact_sample *a,
6526
              const struct ofpact_format_params *fp)
6527
1.60k
{
6528
1.60k
    ds_put_format(fp->s, "%ssample(%s%sprobability=%s%"PRIu16
6529
1.60k
                  ",%scollector_set_id=%s%"PRIu32,
6530
1.60k
                  colors.paren, colors.end,
6531
1.60k
                  colors.param, colors.end, a->probability,
6532
1.60k
                  colors.param, colors.end, a->collector_set_id);
6533
6534
1.60k
    ds_put_format(fp->s, ",%sobs_domain_id=%s", colors.param, colors.end);
6535
1.60k
    if (a->obs_domain_src.field) {
6536
193
        mf_format_subfield(&a->obs_domain_src, fp->s);
6537
1.41k
    } else {
6538
1.41k
        ds_put_format(fp->s, "%"PRIu32, a->obs_domain_imm);
6539
1.41k
    }
6540
1.60k
    ds_put_format(fp->s, ",%sobs_point_id=%s", colors.param, colors.end);
6541
1.60k
    if (a->obs_point_src.field) {
6542
232
        mf_format_subfield(&a->obs_point_src, fp->s);
6543
1.37k
    } else {
6544
1.37k
        ds_put_format(fp->s, "%"PRIu32, a->obs_point_imm);
6545
1.37k
    }
6546
1.60k
    if (a->sampling_port != OFPP_NONE) {
6547
1.53k
        ds_put_format(fp->s, ",%ssampling_port=%s", colors.param, colors.end);
6548
1.53k
        ofputil_format_port(a->sampling_port, fp->port_map, fp->s);
6549
1.53k
    }
6550
1.60k
    if (a->direction == NX_ACTION_SAMPLE_INGRESS) {
6551
49
        ds_put_format(fp->s, ",%singress%s", colors.param, colors.end);
6552
1.56k
    } else if (a->direction == NX_ACTION_SAMPLE_EGRESS) {
6553
586
        ds_put_format(fp->s, ",%segress%s", colors.param, colors.end);
6554
586
    }
6555
1.60k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6556
1.60k
}
6557
6558
static enum ofperr
6559
check_SAMPLE(const struct ofpact_sample *a OVS_UNUSED,
6560
             const struct ofpact_check_params *cp OVS_UNUSED)
6561
1.17k
{
6562
1.17k
    return 0;
6563
1.17k
}
6564

6565
/* debug instructions. */
6566
6567
static bool enable_debug;
6568
6569
void
6570
ofpact_dummy_enable(void)
6571
0
{
6572
0
    enable_debug = true;
6573
0
}
6574
6575
static enum ofperr
6576
decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
6577
15
{
6578
15
    if (!enable_debug) {
6579
15
        return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
6580
15
    }
6581
6582
0
    ofpact_put_DEBUG_RECIRC(out);
6583
0
    return 0;
6584
15
}
6585
6586
static void
6587
encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
6588
                    enum ofp_version ofp_version OVS_UNUSED,
6589
                    struct ofpbuf *out)
6590
456
{
6591
456
    put_NXAST_DEBUG_RECIRC(out);
6592
456
}
6593
6594
static char * OVS_WARN_UNUSED_RESULT
6595
parse_DEBUG_RECIRC(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6596
744
{
6597
744
    ofpact_put_DEBUG_RECIRC(pp->ofpacts);
6598
744
    return NULL;
6599
744
}
6600
6601
static void
6602
format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED,
6603
                    const struct ofpact_format_params *fp)
6604
0
{
6605
0
    ds_put_format(fp->s, "%sdebug_recirc%s", colors.value, colors.end);
6606
0
}
6607
6608
static enum ofperr
6609
check_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED,
6610
                   const struct ofpact_check_params *cp OVS_UNUSED)
6611
457
{
6612
457
    return 0;
6613
457
}
6614
6615
static enum ofperr
6616
decode_NXAST_RAW_DEBUG_SLOW(struct ofpbuf *out)
6617
352
{
6618
352
    if (!enable_debug) {
6619
352
        return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
6620
352
    }
6621
6622
0
    ofpact_put_DEBUG_SLOW(out);
6623
0
    return 0;
6624
352
}
6625
6626
static void
6627
encode_DEBUG_SLOW(const struct ofpact_null *n OVS_UNUSED,
6628
                  enum ofp_version ofp_version OVS_UNUSED,
6629
                  struct ofpbuf *out)
6630
1.06k
{
6631
1.06k
    put_NXAST_DEBUG_SLOW(out);
6632
1.06k
}
6633
6634
static char * OVS_WARN_UNUSED_RESULT
6635
parse_DEBUG_SLOW(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6636
1.93k
{
6637
1.93k
    ofpact_put_DEBUG_SLOW(pp->ofpacts);
6638
1.93k
    return NULL;
6639
1.93k
}
6640
6641
static void
6642
format_DEBUG_SLOW(const struct ofpact_null *a OVS_UNUSED,
6643
                  const struct ofpact_format_params *fp)
6644
0
{
6645
0
    ds_put_format(fp->s, "%sdebug_slow%s", colors.value, colors.end);
6646
0
}
6647
6648
static enum ofperr
6649
check_DEBUG_SLOW(const struct ofpact_null *a OVS_UNUSED,
6650
                 const struct ofpact_check_params *cp OVS_UNUSED)
6651
1.07k
{
6652
1.07k
    return 0;
6653
1.07k
}
6654
6655
/* Action structure for NXAST_CT.
6656
 *
6657
 * Pass traffic to the connection tracker.
6658
 *
6659
 * There are two important concepts to understanding the connection tracking
6660
 * interface: Packet state and Connection state. Packets may be "Untracked" or
6661
 * "Tracked". Connections may be "Uncommitted" or "Committed".
6662
 *
6663
 *   - Packet State:
6664
 *
6665
 *      Untracked packets have an unknown connection state.  In most
6666
 *      cases, packets entering the OpenFlow pipeline will initially be
6667
 *      in the untracked state. Untracked packets may become tracked by
6668
 *      executing NXAST_CT with a "recirc_table" specified. This makes
6669
 *      various aspects about the connection available, in particular
6670
 *      the connection state.
6671
 *
6672
 *      An NXAST_CT action always puts the packet into an untracked
6673
 *      state for the current processing path.  If "recirc_table" is
6674
 *      set, execution is forked and the packet passes through the
6675
 *      connection tracker.  The specified table's processing path is
6676
 *      able to match on Connection state until the end of the OpenFlow
6677
 *      pipeline or NXAST_CT is called again.
6678
 *
6679
 *   - Connection State:
6680
 *
6681
 *      Multiple packets may be associated with a single connection. Initially,
6682
 *      all connections are uncommitted. The connection state corresponding to
6683
 *      a packet is available in the NXM_NX_CT_STATE field for tracked packets.
6684
 *
6685
 *      Uncommitted connections have no state stored about them. Uncommitted
6686
 *      connections may transition into the committed state by executing
6687
 *      NXAST_CT with the NX_CT_F_COMMIT flag.
6688
 *
6689
 *      Once a connection becomes committed, information may be gathered about
6690
 *      the connection by passing subsequent packets through the connection
6691
 *      tracker, and the state of the connection will be stored beyond the
6692
 *      lifetime of packet processing.
6693
 *
6694
 *      A committed connection always has the directionality of the packet that
6695
 *      caused the connection to be committed in the first place.  This is the
6696
 *      "original direction" of the connection, and the opposite direction is
6697
 *      the "reply direction".  If a connection is already committed, but it is
6698
 *      then decided that the original direction should be the opposite of the
6699
 *      existing connection, NX_CT_F_FORCE flag may be used in addition to
6700
 *      NX_CT_F_COMMIT flag to in effect terminate the existing connection and
6701
 *      start a new one in the current direction.
6702
 *
6703
 *      Connections may transition back into the uncommitted state due to
6704
 *      external timers, or due to the contents of packets that are sent to the
6705
 *      connection tracker. This behaviour is outside of the scope of the
6706
 *      OpenFlow interface.
6707
 *
6708
 * The "zone" specifies a context within which the tracking is done:
6709
 *
6710
 *      The connection tracking zone is a 16-bit number. Each zone is an
6711
 *      independent connection tracking context. The connection state for each
6712
 *      connection is completely separate for each zone, so if a connection
6713
 *      is committed to zone A, then it will remain uncommitted in zone B.
6714
 *      If NXAST_CT is executed with the same zone multiple times, later
6715
 *      executions have no effect.
6716
 *
6717
 *      If 'zone_src' is nonzero, this specifies that the zone should be
6718
 *      sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
6719
 *      of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
6720
 *      NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
6721
 *      are the same as the acceptable nxm_header values for the 'src' field of
6722
 *      NXAST_REG_MOVE.
6723
 *
6724
 *      If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
6725
 *      connection tracking zone.
6726
 *
6727
 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
6728
 *
6729
 *      If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
6730
 *      packet will be logically cloned prior to executing this action. One
6731
 *      copy will be sent to the connection tracker, then will be re-injected
6732
 *      into the OpenFlow pipeline beginning at the OpenFlow table specified in
6733
 *      this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
6734
 *      fields will be populated. The original instance of the packet will
6735
 *      continue the current actions list. This can be thought of as similar to
6736
 *      the effect of the "output" action: One copy is sent out (in this case,
6737
 *      to the connection tracker), but the current copy continues processing.
6738
 *
6739
 *      It is strongly recommended that this table is later than the current
6740
 *      table, to prevent loops.
6741
 *
6742
 * The "alg" attaches protocol-specific behaviour to this action:
6743
 *
6744
 *      The ALG is a 16-bit number which specifies that additional
6745
 *      processing should be applied to this traffic.
6746
 *
6747
 *      Protocol | Value | Meaning
6748
 *      --------------------------------------------------------------------
6749
 *      None     |     0 | No protocol-specific behaviour.
6750
 *      FTP      |    21 | Parse FTP control connections and observe the
6751
 *               |       | negotiation of related data connections.
6752
 *      Other    | Other | Unsupported protocols.
6753
 *
6754
 *      By way of example, if FTP control connections have this action applied
6755
 *      with the ALG set to FTP (21), then the connection tracker will observe
6756
 *      the negotiation of data connections. This allows the connection
6757
 *      tracker to identify subsequent data connections as "related" to this
6758
 *      existing connection. The "related" flag will be populated in the
6759
 *      NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
6760
 *      specified.
6761
 *
6762
 * Zero or more actions may immediately follow this action. These actions will
6763
 * be executed within the context of the connection tracker, and they require
6764
 * NX_CT_F_COMMIT flag be set.
6765
 */
6766
struct nx_action_conntrack {
6767
    ovs_be16 type;              /* OFPAT_VENDOR. */
6768
    ovs_be16 len;               /* At least 24. */
6769
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
6770
    ovs_be16 subtype;           /* NXAST_CT. */
6771
    ovs_be16 flags;             /* Zero or more NX_CT_F_* flags.
6772
                                 * Unspecified flag bits must be zero. */
6773
    ovs_be32 zone_src;          /* Connection tracking context. */
6774
    union {
6775
        ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
6776
        ovs_be16 zone_imm;      /* Immediate value for zone. */
6777
    };
6778
    uint8_t recirc_table;       /* Recirculate to a specific table, or
6779
                                   NX_CT_RECIRC_NONE for no recirculation. */
6780
    uint8_t pad[3];             /* Zeroes */
6781
    ovs_be16 alg;               /* Well-known port number for the protocol.
6782
                                 * 0 indicates no ALG is required. */
6783
    /* Followed by a sequence of zero or more OpenFlow actions. The length of
6784
     * these is included in 'len'. */
6785
};
6786
OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
6787
6788
static enum ofperr
6789
decode_ct_zone(const struct nx_action_conntrack *nac,
6790
               struct ofpact_conntrack *out,
6791
               const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap)
6792
8.29k
{
6793
8.29k
    if (nac->zone_src) {
6794
2.56k
        enum ofperr error;
6795
6796
2.56k
        out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
6797
2.56k
        out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
6798
2.56k
        error = mf_vl_mff_mf_from_nxm_header(ntohl(nac->zone_src),
6799
2.56k
                                             vl_mff_map, &out->zone_src.field,
6800
2.56k
                                             tlv_bitmap);
6801
2.56k
        if (error) {
6802
990
            return error;
6803
990
        }
6804
6805
1.57k
        error = mf_check_src(&out->zone_src, NULL);
6806
1.57k
        if (error) {
6807
395
            return error;
6808
395
        }
6809
6810
1.18k
        if (out->zone_src.n_bits != 16) {
6811
108
            VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
6812
108
                         out->zone_src.n_bits);
6813
108
            return OFPERR_OFPBAC_BAD_SET_LEN;
6814
108
        }
6815
5.72k
    } else {
6816
5.72k
        out->zone_src.field = NULL;
6817
5.72k
        out->zone_imm = ntohs(nac->zone_imm);
6818
5.72k
    }
6819
6820
6.80k
    return 0;
6821
8.29k
}
6822
6823
static enum ofperr
6824
decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
6825
                    enum ofp_version ofp_version,
6826
                    const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
6827
                    struct ofpbuf *out)
6828
8.63k
{
6829
8.63k
    const size_t ct_offset = ofpacts_pull(out);
6830
8.63k
    struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
6831
8.63k
    int error;
6832
6833
8.63k
    conntrack->flags = ntohs(nac->flags);
6834
8.63k
    if (conntrack->flags & NX_CT_F_FORCE &&
6835
2.39k
        !(conntrack->flags & NX_CT_F_COMMIT)) {
6836
343
        error = OFPERR_OFPBAC_BAD_ARGUMENT;
6837
343
        goto out;
6838
343
    }
6839
6840
8.29k
    error = decode_ct_zone(nac, conntrack, vl_mff_map, tlv_bitmap);
6841
8.29k
    if (error) {
6842
1.49k
        goto out;
6843
1.49k
    }
6844
6.80k
    conntrack->recirc_table = nac->recirc_table;
6845
6.80k
    conntrack->alg = ntohs(nac->alg);
6846
6847
6.80k
    ofpbuf_pull(out, sizeof(*conntrack));
6848
6849
6.80k
    struct ofpbuf openflow = ofpbuf_const_initializer(
6850
6.80k
        nac + 1, ntohs(nac->len) - sizeof(*nac));
6851
6.80k
    error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
6852
6.80k
                                            ofp_version,
6853
6.80k
                                            1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6854
6.80k
                                            out, OFPACT_CT, vl_mff_map,
6855
6.80k
                                            tlv_bitmap);
6856
6.80k
    if (error) {
6857
1.04k
        return error;
6858
1.04k
    }
6859
6860
5.75k
    conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
6861
5.75k
    out->header = &conntrack->ofpact;
6862
5.75k
    ofpact_finish_CT(out, &conntrack);
6863
6864
5.75k
    if (conntrack->ofpact.len > sizeof(*conntrack)
6865
3.92k
        && !(conntrack->flags & NX_CT_F_COMMIT)) {
6866
479
        const struct ofpact *a;
6867
479
        size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
6868
6869
479
        OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
6870
479
            if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
6871
335
                || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
6872
335
                VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
6873
335
                             "other than NAT without arguments are specified.");
6874
335
                error = OFPERR_OFPBAC_BAD_ARGUMENT;
6875
335
                goto out;
6876
335
            }
6877
479
        }
6878
479
    }
6879
6880
7.59k
out:
6881
7.59k
    ofpbuf_push_uninit(out, ct_offset);
6882
7.59k
    return error;
6883
5.75k
}
6884
6885
static void
6886
encode_CT(const struct ofpact_conntrack *conntrack,
6887
          enum ofp_version ofp_version, struct ofpbuf *out)
6888
8.88k
{
6889
8.88k
    struct nx_action_conntrack *nac;
6890
8.88k
    const size_t ofs = out->size;
6891
8.88k
    size_t len;
6892
6893
8.88k
    nac = put_NXAST_CT(out);
6894
8.88k
    nac->flags = htons(conntrack->flags);
6895
8.88k
    if (conntrack->zone_src.field) {
6896
199
        nac->zone_src = htonl(nxm_header_from_mff(conntrack->zone_src.field));
6897
199
        nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
6898
199
                                                   conntrack->zone_src.n_bits);
6899
8.68k
    } else {
6900
8.68k
        nac->zone_src = htonl(0);
6901
8.68k
        nac->zone_imm = htons(conntrack->zone_imm);
6902
8.68k
    }
6903
8.88k
    nac->recirc_table = conntrack->recirc_table;
6904
8.88k
    nac->alg = htons(conntrack->alg);
6905
6906
8.88k
    len = ofpacts_put_openflow_actions(conntrack->actions,
6907
8.88k
                                       ofpact_ct_get_action_len(conntrack),
6908
8.88k
                                       out, ofp_version);
6909
8.88k
    len += sizeof(*nac);
6910
8.88k
    nac = ofpbuf_at(out, ofs, sizeof(*nac));
6911
8.88k
    nac->len = htons(len);
6912
8.88k
}
6913
6914
static char *OVS_WARN_UNUSED_RESULT
6915
parse_NAT(char *arg, const struct ofpact_parse_params *pp);
6916
6917
/* Parses 'arg' as the argument to a "ct" action, and appends such an
6918
 * action to 'pp->ofpacts'.
6919
 *
6920
 * Returns NULL if successful, otherwise a malloc()'d string describing the
6921
 * error.  The caller is responsible for freeing the returned string. */
6922
static char * OVS_WARN_UNUSED_RESULT
6923
parse_CT(char *arg, const struct ofpact_parse_params *pp)
6924
16.1k
{
6925
16.1k
    const size_t ct_offset = ofpacts_pull(pp->ofpacts);
6926
16.1k
    struct ofpact_conntrack *oc;
6927
16.1k
    char *error = NULL;
6928
16.1k
    char *key, *value;
6929
6930
16.1k
    oc = ofpact_put_CT(pp->ofpacts);
6931
16.1k
    oc->flags = 0;
6932
16.1k
    oc->recirc_table = NX_CT_RECIRC_NONE;
6933
95.8k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
6934
80.7k
        if (!strcmp(key, "commit")) {
6935
248
            oc->flags |= NX_CT_F_COMMIT;
6936
80.5k
        } else if (!strcmp(key, "force")) {
6937
267
            oc->flags |= NX_CT_F_FORCE;
6938
80.2k
        } else if (!strcmp(key, "table")) {
6939
1.02k
            if (!ofputil_table_from_string(value, pp->table_map,
6940
1.02k
                                           &oc->recirc_table)) {
6941
1
                error = xasprintf("unknown table %s", value);
6942
1.02k
            } else if (oc->recirc_table == NX_CT_RECIRC_NONE) {
6943
1
                error = xasprintf("invalid table %#"PRIx8, oc->recirc_table);
6944
1
            }
6945
79.2k
        } else if (!strcmp(key, "zone")) {
6946
634
            error = str_to_u16(value, "zone", &oc->zone_imm);
6947
6948
634
            if (error) {
6949
430
                free(error);
6950
430
                error = mf_parse_subfield(&oc->zone_src, value);
6951
430
                if (error) {
6952
14
                    return error;
6953
14
                }
6954
430
            }
6955
78.6k
        } else if (!strcmp(key, "alg")) {
6956
371
            error = str_to_connhelper(value, &oc->alg);
6957
78.2k
        } else if (!strcmp(key, "nat")) {
6958
76.2k
            const size_t nat_offset = ofpacts_pull(pp->ofpacts);
6959
6960
76.2k
            error = parse_NAT(value, pp);
6961
            /* Update CT action pointer and length. */
6962
76.2k
            pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, nat_offset);
6963
76.2k
            oc = pp->ofpacts->header;
6964
76.2k
        } else if (!strcmp(key, "exec")) {
6965
            /* Hide existing actions from ofpacts_parse_copy(), so the
6966
             * nesting can be handled transparently. */
6967
1.82k
            enum ofputil_protocol usable_protocols2;
6968
1.82k
            const size_t exec_offset = ofpacts_pull(pp->ofpacts);
6969
6970
            /* Initializes 'usable_protocol2', fold it back to
6971
             * '*usable_protocols' afterwards, so that we do not lose
6972
             * restrictions already in there. */
6973
1.82k
            struct ofpact_parse_params pp2 = *pp;
6974
1.82k
            pp2.usable_protocols = &usable_protocols2;
6975
1.82k
            error = ofpacts_parse_copy(value, &pp2, false, OFPACT_CT);
6976
1.82k
            *pp->usable_protocols &= usable_protocols2;
6977
1.82k
            pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, exec_offset);
6978
1.82k
            oc = pp->ofpacts->header;
6979
1.82k
        } else {
6980
216
            error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
6981
216
        }
6982
80.7k
        if (error) {
6983
1.07k
            break;
6984
1.07k
        }
6985
80.7k
    }
6986
16.1k
    if (!error && oc->flags & NX_CT_F_FORCE && !(oc->flags & NX_CT_F_COMMIT)) {
6987
8
        error = xasprintf("\"force\" flag requires \"commit\" flag.");
6988
8
    }
6989
6990
16.1k
    if (ofpbuf_oversized(pp->ofpacts)) {
6991
3
        free(error);
6992
3
        return xasprintf("input too big");
6993
3
    }
6994
6995
16.1k
    ofpact_finish_CT(pp->ofpacts, &oc);
6996
16.1k
    ofpbuf_push_uninit(pp->ofpacts, ct_offset);
6997
16.1k
    return error;
6998
16.1k
}
6999
7000
static void
7001
format_alg(int port, struct ds *s)
7002
4.88k
{
7003
4.88k
    switch(port) {
7004
971
    case IPPORT_FTP:
7005
971
        ds_put_format(s, "%salg=%sftp,", colors.param, colors.end);
7006
971
        break;
7007
575
    case IPPORT_TFTP:
7008
575
        ds_put_format(s, "%salg=%stftp,", colors.param, colors.end);
7009
575
        break;
7010
580
    case 0:
7011
        /* Don't print. */
7012
580
        break;
7013
2.76k
    default:
7014
2.76k
        ds_put_format(s, "%salg=%s%d,", colors.param, colors.end, port);
7015
2.76k
        break;
7016
4.88k
    }
7017
4.88k
}
7018
7019
static void format_NAT(const struct ofpact_nat *,
7020
                       const struct ofpact_format_params *fp);
7021
7022
static void
7023
format_CT(const struct ofpact_conntrack *a,
7024
          const struct ofpact_format_params *fp)
7025
4.88k
{
7026
4.88k
    ds_put_format(fp->s, "%sct(%s", colors.paren, colors.end);
7027
4.88k
    if (a->flags & NX_CT_F_COMMIT) {
7028
3.56k
        ds_put_format(fp->s, "%scommit%s,", colors.value, colors.end);
7029
3.56k
    }
7030
4.88k
    if (a->flags & NX_CT_F_FORCE) {
7031
1.15k
        ds_put_format(fp->s, "%sforce%s,", colors.value, colors.end);
7032
1.15k
    }
7033
4.88k
    if (a->recirc_table != NX_CT_RECIRC_NONE) {
7034
4.62k
        ds_put_format(fp->s, "%stable=%s", colors.special, colors.end);
7035
4.62k
        ofputil_format_table(a->recirc_table, fp->table_map, fp->s);
7036
4.62k
        ds_put_char(fp->s, ',');
7037
4.62k
    }
7038
4.88k
    if (a->zone_src.field) {
7039
1.02k
        ds_put_format(fp->s, "%szone=%s", colors.param, colors.end);
7040
1.02k
        mf_format_subfield(&a->zone_src, fp->s);
7041
1.02k
        ds_put_char(fp->s, ',');
7042
3.86k
    } else if (a->zone_imm) {
7043
2.57k
        ds_put_format(fp->s, "%szone=%s%"PRIu16",",
7044
2.57k
                      colors.param, colors.end, a->zone_imm);
7045
2.57k
    }
7046
    /* If the first action is a NAT action, format it outside of the 'exec'
7047
     * envelope. */
7048
4.88k
    const struct ofpact *action = a->actions;
7049
4.88k
    size_t actions_len = ofpact_ct_get_action_len(a);
7050
4.88k
    if (actions_len && action->type == OFPACT_NAT) {
7051
2.98k
        format_NAT(ofpact_get_NAT(action), fp);
7052
2.98k
        ds_put_char(fp->s, ',');
7053
2.98k
        actions_len -= OFPACT_ALIGN(action->len);
7054
2.98k
        action = ofpact_next(action);
7055
2.98k
    }
7056
4.88k
    if (actions_len) {
7057
416
        ds_put_format(fp->s, "%sexec(%s", colors.paren, colors.end);
7058
416
        ofpacts_format(action, actions_len, fp);
7059
416
        ds_put_format(fp->s, "%s),%s", colors.paren, colors.end);
7060
416
    }
7061
4.88k
    format_alg(a->alg, fp->s);
7062
4.88k
    ds_chomp(fp->s, ',');
7063
4.88k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7064
4.88k
}
7065
7066
static enum ofperr
7067
check_CT(struct ofpact_conntrack *a, struct ofpact_check_params *cp)
7068
11.8k
{
7069
11.8k
    struct flow *flow = &cp->match->flow;
7070
7071
11.8k
    if (!dl_type_is_ip_any(get_dl_type(flow))
7072
11.4k
        || (flow->ct_state & CS_INVALID && a->flags & NX_CT_F_COMMIT)
7073
11.4k
        || (a->alg == IPPORT_FTP && flow->nw_proto != IPPROTO_TCP)
7074
11.3k
        || (a->alg == IPPORT_TFTP && flow->nw_proto != IPPROTO_UDP)) {
7075
        /* We can't downgrade to OF1.0 and expect inconsistent CT actions
7076
         * be silently discarded.  Instead, datapath flow install fails, so
7077
         * it is better to flag inconsistent CT actions as hard errors. */
7078
490
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
7079
490
    }
7080
7081
11.3k
    if (a->zone_src.field) {
7082
623
        return mf_check_src(&a->zone_src, cp->match);
7083
623
    }
7084
7085
10.7k
    return check_subactions(a->actions, ofpact_ct_get_action_len(a), cp);
7086
11.3k
}
7087

7088
/* ct_clear action. */
7089
7090
static enum ofperr
7091
decode_NXAST_RAW_CT_CLEAR(struct ofpbuf *out)
7092
526
{
7093
526
    ofpact_put_CT_CLEAR(out);
7094
526
    return 0;
7095
526
}
7096
7097
static void
7098
encode_CT_CLEAR(const struct ofpact_null *null OVS_UNUSED,
7099
                enum ofp_version ofp_version OVS_UNUSED,
7100
                struct ofpbuf *out)
7101
528
{
7102
528
    put_NXAST_CT_CLEAR(out);
7103
528
}
7104
7105
static char * OVS_WARN_UNUSED_RESULT
7106
parse_CT_CLEAR(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
7107
916
{
7108
916
    ofpact_put_CT_CLEAR(pp->ofpacts);
7109
916
    return NULL;
7110
916
}
7111
7112
static void
7113
format_CT_CLEAR(const struct ofpact_null *a OVS_UNUSED,
7114
                const struct ofpact_format_params *fp)
7115
104
{
7116
104
    ds_put_format(fp->s, "%sct_clear%s", colors.value, colors.end);
7117
104
}
7118
7119
static enum ofperr
7120
check_CT_CLEAR(const struct ofpact_null *a OVS_UNUSED,
7121
               const struct ofpact_check_params *cp OVS_UNUSED)
7122
623
{
7123
623
    return 0;
7124
623
}
7125

7126
/* NAT action. */
7127
7128
/* Which optional fields are present? */
7129
enum nx_nat_range {
7130
    NX_NAT_RANGE_IPV4_MIN  = 1 << 0, /* ovs_be32 */
7131
    NX_NAT_RANGE_IPV4_MAX  = 1 << 1, /* ovs_be32 */
7132
    NX_NAT_RANGE_IPV6_MIN  = 1 << 2, /* struct in6_addr */
7133
    NX_NAT_RANGE_IPV6_MAX  = 1 << 3, /* struct in6_addr */
7134
    NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
7135
    NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
7136
};
7137
7138
/* Action structure for NXAST_NAT. */
7139
struct nx_action_nat {
7140
    ovs_be16 type;              /* OFPAT_VENDOR. */
7141
    ovs_be16 len;               /* At least 16. */
7142
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
7143
    ovs_be16 subtype;           /* NXAST_NAT. */
7144
    uint8_t  pad[2];            /* Must be zero. */
7145
    ovs_be16 flags;             /* Zero or more NX_NAT_F_* flags.
7146
                                 * Unspecified flag bits must be zero. */
7147
    ovs_be16 range_present;     /* NX_NAT_RANGE_* */
7148
    /* Followed by optional parameters as specified by 'range_present' */
7149
};
7150
OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
7151
7152
static void
7153
encode_NAT(const struct ofpact_nat *nat,
7154
           enum ofp_version ofp_version OVS_UNUSED,
7155
           struct ofpbuf *out)
7156
7.90k
{
7157
7.90k
    struct nx_action_nat *nan;
7158
7.90k
    const size_t ofs = out->size;
7159
7.90k
    uint16_t range_present = 0;
7160
7161
7.90k
    nan = put_NXAST_NAT(out);
7162
7.90k
    nan->flags = htons(nat->flags);
7163
7.90k
    if (nat->range_af == AF_INET) {
7164
1.21k
        if (nat->range.addr.ipv4.min) {
7165
356
            ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
7166
356
            *min = nat->range.addr.ipv4.min;
7167
356
            range_present |= NX_NAT_RANGE_IPV4_MIN;
7168
356
        }
7169
1.21k
        if (nat->range.addr.ipv4.max) {
7170
376
            ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
7171
376
            *max = nat->range.addr.ipv4.max;
7172
376
            range_present |= NX_NAT_RANGE_IPV4_MAX;
7173
376
        }
7174
6.68k
    } else if (nat->range_af == AF_INET6) {
7175
4.05k
        if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
7176
2.94k
            struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
7177
2.94k
            *min = nat->range.addr.ipv6.min;
7178
2.94k
            range_present |= NX_NAT_RANGE_IPV6_MIN;
7179
2.94k
        }
7180
4.05k
        if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
7181
866
            struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
7182
866
            *max = nat->range.addr.ipv6.max;
7183
866
            range_present |= NX_NAT_RANGE_IPV6_MAX;
7184
866
        }
7185
4.05k
    }
7186
7.90k
    if (nat->range_af != AF_UNSPEC) {
7187
5.26k
        if (nat->range.proto.min) {
7188
1.18k
            ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
7189
1.18k
            *min = htons(nat->range.proto.min);
7190
1.18k
            range_present |= NX_NAT_RANGE_PROTO_MIN;
7191
1.18k
        }
7192
5.26k
        if (nat->range.proto.max) {
7193
563
            ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
7194
563
            *max = htons(nat->range.proto.max);
7195
563
            range_present |= NX_NAT_RANGE_PROTO_MAX;
7196
563
        }
7197
5.26k
    }
7198
7.90k
    pad_ofpat(out, ofs);
7199
7.90k
    nan = ofpbuf_at(out, ofs, sizeof *nan);
7200
7.90k
    nan->range_present = htons(range_present);
7201
7.90k
}
7202
7203
static enum ofperr
7204
decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
7205
                     enum ofp_version ofp_version OVS_UNUSED,
7206
                     struct ofpbuf *out)
7207
13.9k
{
7208
13.9k
    struct ofpact_nat *nat;
7209
13.9k
    uint16_t range_present = ntohs(nan->range_present);
7210
13.9k
    const char *opts = (char *)(nan + 1);
7211
13.9k
    uint16_t len = ntohs(nan->len) - sizeof *nan;
7212
7213
13.9k
    nat = ofpact_put_NAT(out);
7214
13.9k
    nat->flags = ntohs(nan->flags);
7215
7216
    /* Check for unknown or mutually exclusive flags. */
7217
13.9k
    if ((nat->flags & ~NX_NAT_F_MASK)
7218
10.8k
        || (nat->flags & NX_NAT_F_SRC && nat->flags & NX_NAT_F_DST)
7219
10.6k
        || (nat->flags & NX_NAT_F_PROTO_HASH
7220
3.27k
            && nat->flags & NX_NAT_F_PROTO_RANDOM)) {
7221
3.27k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7222
3.27k
    }
7223
7224
10.6k
#define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE)                     \
7225
23.3k
    (LEN >= sizeof(TYPE)                                        \
7226
23.3k
     ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE),    \
7227
11.2k
        SRC += sizeof(TYPE))                                    \
7228
23.3k
     : NULL)
7229
7230
10.6k
    nat->range_af = AF_UNSPEC;
7231
10.6k
    if (range_present & NX_NAT_RANGE_IPV4_MIN) {
7232
6.37k
        if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
7233
674
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7234
674
        }
7235
7236
5.69k
        if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
7237
5.68k
            || !nat->range.addr.ipv4.min) {
7238
149
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7239
149
        }
7240
7241
5.54k
        nat->range_af = AF_INET;
7242
7243
5.54k
        if (range_present & NX_NAT_RANGE_IPV4_MAX) {
7244
1.08k
            if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
7245
1.08k
                                ovs_be32)) {
7246
0
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7247
0
            }
7248
1.08k
            if (ntohl(nat->range.addr.ipv4.max)
7249
1.08k
                < ntohl(nat->range.addr.ipv4.min)) {
7250
651
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7251
651
            }
7252
1.08k
        }
7253
5.54k
    } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
7254
149
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7255
4.13k
    } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
7256
2.47k
        if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
7257
2.47k
                            struct in6_addr)
7258
1.47k
            || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
7259
1.47k
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7260
1.47k
        }
7261
7262
995
        nat->range_af = AF_INET6;
7263
7264
995
        if (range_present & NX_NAT_RANGE_IPV6_MAX) {
7265
861
            if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
7266
861
                                struct in6_addr)) {
7267
50
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7268
50
            }
7269
811
            if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
7270
811
                       sizeof(struct in6_addr)) < 0) {
7271
121
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7272
121
            }
7273
811
        }
7274
1.66k
    } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
7275
452
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7276
452
    }
7277
7278
6.93k
    if (range_present & NX_NAT_RANGE_PROTO_MIN) {
7279
2.45k
        ovs_be16 proto;
7280
7281
2.45k
        if (nat->range_af == AF_UNSPEC) {
7282
253
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7283
253
        }
7284
2.20k
        if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
7285
325
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7286
325
        }
7287
1.87k
        nat->range.proto.min = ntohs(proto);
7288
1.87k
        if (range_present & NX_NAT_RANGE_PROTO_MAX) {
7289
685
            if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
7290
0
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7291
0
            }
7292
685
            nat->range.proto.max = ntohs(proto);
7293
685
            if (nat->range.proto.max < nat->range.proto.min) {
7294
343
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7295
343
            }
7296
685
        }
7297
4.48k
    } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
7298
109
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7299
109
    }
7300
7301
5.90k
    return 0;
7302
6.93k
}
7303
7304
static void
7305
format_NAT(const struct ofpact_nat *a, const struct ofpact_format_params *fp)
7306
2.98k
{
7307
2.98k
    ds_put_format(fp->s, "%snat%s", colors.paren, colors.end);
7308
7309
2.98k
    if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
7310
2.64k
        ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
7311
2.64k
        ds_put_format(fp->s, a->flags & NX_NAT_F_SRC ? "%ssrc%s" : "%sdst%s",
7312
2.64k
                      colors.param, colors.end);
7313
7314
2.64k
        if (a->range_af != AF_UNSPEC) {
7315
2.01k
            ds_put_format(fp->s, "%s=%s", colors.param, colors.end);
7316
7317
2.01k
            if (a->range_af == AF_INET) {
7318
2.01k
                ds_put_format(fp->s, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
7319
7320
2.01k
                if (a->range.addr.ipv4.max
7321
189
                    && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
7322
65
                    ds_put_format(fp->s, "-"IP_FMT,
7323
65
                                  IP_ARGS(a->range.addr.ipv4.max));
7324
65
                }
7325
2.01k
            } else if (a->range_af == AF_INET6) {
7326
0
                ipv6_format_addr_bracket(&a->range.addr.ipv6.min, fp->s,
7327
0
                                        a->range.proto.min);
7328
7329
0
                if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
7330
0
                    && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
7331
0
                              sizeof(struct in6_addr)) != 0) {
7332
0
                    ds_put_char(fp->s, '-');
7333
0
                    ipv6_format_addr_bracket(&a->range.addr.ipv6.max, fp->s,
7334
0
                                            a->range.proto.min);
7335
0
                }
7336
0
            }
7337
2.01k
            if (a->range.proto.min) {
7338
1.38k
                ds_put_char(fp->s, ':');
7339
1.38k
                ds_put_format(fp->s, "%"PRIu16, a->range.proto.min);
7340
7341
1.38k
                if (a->range.proto.max
7342
215
                    && a->range.proto.max != a->range.proto.min) {
7343
131
                    ds_put_format(fp->s, "-%"PRIu16, a->range.proto.max);
7344
131
                }
7345
1.38k
            }
7346
2.01k
            ds_put_char(fp->s, ',');
7347
7348
2.01k
            if (a->flags & NX_NAT_F_PERSISTENT) {
7349
1.62k
                ds_put_format(fp->s, "%spersistent%s,",
7350
1.62k
                              colors.value, colors.end);
7351
1.62k
            }
7352
2.01k
            if (a->flags & NX_NAT_F_PROTO_HASH) {
7353
99
                ds_put_format(fp->s, "%shash%s,", colors.value, colors.end);
7354
99
            }
7355
2.01k
            if (a->flags & NX_NAT_F_PROTO_RANDOM) {
7356
1.17k
                ds_put_format(fp->s, "%srandom%s,", colors.value, colors.end);
7357
1.17k
            }
7358
2.01k
        }
7359
2.64k
        ds_chomp(fp->s, ',');
7360
2.64k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7361
2.64k
    }
7362
2.98k
}
7363
7364
static char * OVS_WARN_UNUSED_RESULT
7365
str_to_nat_range(const char *s, struct ofpact_nat *on)
7366
73.8k
{
7367
73.8k
    char ipv6_s[IPV6_SCAN_LEN + 1];
7368
73.8k
    int n = 0;
7369
7370
73.8k
    on->range_af = AF_UNSPEC;
7371
73.8k
    if (ovs_scan_len(s, &n, IP_SCAN_FMT,
7372
73.8k
                     IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
7373
1.28k
        on->range_af = AF_INET;
7374
7375
1.28k
        if (s[n] == '-') {
7376
407
            n++;
7377
407
            if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
7378
407
                              IP_SCAN_ARGS(&on->range.addr.ipv4.max))
7379
406
                || (ntohl(on->range.addr.ipv4.max)
7380
406
                    < ntohl(on->range.addr.ipv4.min))) {
7381
3
                goto error;
7382
3
            }
7383
407
        }
7384
72.6k
    } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
7385
3.23k
                || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
7386
71.6k
               && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
7387
40.6k
        on->range_af = AF_INET6;
7388
7389
40.6k
        if (s[n] == '-') {
7390
1.91k
            n++;
7391
1.91k
            if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
7392
233
                  || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
7393
1.90k
                || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
7394
1.90k
                || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
7395
1.90k
                          sizeof on->range.addr.ipv6.max) < 0) {
7396
30
                goto error;
7397
30
            }
7398
1.91k
        }
7399
40.6k
    }
7400
73.8k
    if (on->range_af != AF_UNSPEC && s[n] == ':') {
7401
1.66k
        n++;
7402
1.66k
        if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
7403
1
            goto error;
7404
1
        }
7405
1.66k
        if (s[n] == '-') {
7406
839
            n++;
7407
839
            if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
7408
837
                || on->range.proto.max < on->range.proto.min) {
7409
5
                goto error;
7410
5
            }
7411
839
        }
7412
1.66k
    }
7413
73.8k
    if (strlen(s) != n) {
7414
72
        return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
7415
72
                         &s[n], s, n);
7416
72
    }
7417
73.7k
    return NULL;
7418
39
error:
7419
39
    return xasprintf("invalid nat range \"%s\"", s);
7420
73.8k
}
7421
7422
7423
/* Parses 'arg' as the argument to a "nat" action, and appends such an
7424
 * action to 'pp->ofpacts'.
7425
 *
7426
 * Returns NULL if successful, otherwise a malloc()'d string describing the
7427
 * error.  The caller is responsible for freeing the returned string. */
7428
static char * OVS_WARN_UNUSED_RESULT
7429
parse_NAT(char *arg, const struct ofpact_parse_params *pp)
7430
84.0k
{
7431
84.0k
    struct ofpact_nat *on = ofpact_put_NAT(pp->ofpacts);
7432
84.0k
    char *key, *value;
7433
7434
84.0k
    on->flags = 0;
7435
84.0k
    on->range_af = AF_UNSPEC;
7436
7437
158k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
7438
74.5k
        char *error = NULL;
7439
7440
74.5k
        if (!strcmp(key, "src")) {
7441
1.69k
            on->flags |= NX_NAT_F_SRC;
7442
1.69k
            error = str_to_nat_range(value, on);
7443
72.8k
        } else if (!strcmp(key, "dst")) {
7444
72.2k
            on->flags |= NX_NAT_F_DST;
7445
72.2k
            error = str_to_nat_range(value, on);
7446
72.2k
        } else if (!strcmp(key, "persistent")) {
7447
214
            on->flags |= NX_NAT_F_PERSISTENT;
7448
449
        } else if (!strcmp(key, "hash")) {
7449
204
            on->flags |= NX_NAT_F_PROTO_HASH;
7450
245
        } else if (!strcmp(key, "random")) {
7451
194
            on->flags |= NX_NAT_F_PROTO_RANDOM;
7452
194
        } else {
7453
51
            error = xasprintf("invalid key \"%s\" in \"nat\" argument",
7454
51
                              key);
7455
51
        }
7456
74.5k
        if (error) {
7457
162
            return error;
7458
162
        }
7459
74.5k
    }
7460
83.8k
    if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
7461
2
        return xasprintf("May only specify one of \"src\" or \"dst\".");
7462
2
    }
7463
83.8k
    if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
7464
12.1k
        if (on->flags) {
7465
15
            return xasprintf("Flags allowed only with \"src\" or \"dst\".");
7466
15
        }
7467
12.1k
        if (on->range_af != AF_UNSPEC) {
7468
0
            return xasprintf("Range allowed only with \"src\" or \"dst\".");
7469
0
        }
7470
12.1k
    }
7471
83.8k
    if (on->flags & NX_NAT_F_PROTO_HASH && on->flags & NX_NAT_F_PROTO_RANDOM) {
7472
0
        return xasprintf("Both \"hash\" and \"random\" are not allowed.");
7473
0
    }
7474
7475
83.8k
    return NULL;
7476
83.8k
}
7477
7478
static enum ofperr
7479
check_NAT(const struct ofpact_nat *a, const struct ofpact_check_params *cp)
7480
9.87k
{
7481
9.87k
    ovs_be16 dl_type = get_dl_type(&cp->match->flow);
7482
9.87k
    if (!dl_type_is_ip_any(dl_type) ||
7483
9.87k
        (a->range_af == AF_INET && dl_type != htons(ETH_TYPE_IP)) ||
7484
9.69k
        (a->range_af == AF_INET6 && dl_type != htons(ETH_TYPE_IPV6))) {
7485
184
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
7486
184
    }
7487
9.69k
    return 0;
7488
9.87k
}
7489

7490
/* Truncate output action. */
7491
struct nx_action_output_trunc {
7492
    ovs_be16 type;              /* OFPAT_VENDOR. */
7493
    ovs_be16 len;               /* At least 16. */
7494
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
7495
    ovs_be16 subtype;           /* NXAST_OUTPUT_TRUNC. */
7496
    ovs_be16 port;              /* Output port */
7497
    ovs_be32 max_len;           /* Truncate packet to size bytes */
7498
};
7499
OFP_ASSERT(sizeof(struct nx_action_output_trunc) == 16);
7500
7501
static enum ofperr
7502
decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc *natrc,
7503
                            enum ofp_version ofp_version OVS_UNUSED,
7504
                            struct ofpbuf *out)
7505
1.76k
{
7506
1.76k
    struct ofpact_output_trunc *output_trunc;
7507
7508
1.76k
    output_trunc = ofpact_put_OUTPUT_TRUNC(out);
7509
1.76k
    output_trunc->max_len = ntohl(natrc->max_len);
7510
1.76k
    output_trunc->port = u16_to_ofp(ntohs(natrc->port));
7511
7512
1.76k
    if (output_trunc->max_len < ETH_HEADER_LEN) {
7513
19
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7514
19
    }
7515
1.74k
    return 0;
7516
1.76k
}
7517
7518
static void
7519
encode_OUTPUT_TRUNC(const struct ofpact_output_trunc *output_trunc,
7520
                  enum ofp_version ofp_version OVS_UNUSED,
7521
                  struct ofpbuf *out)
7522
712
{
7523
712
    struct nx_action_output_trunc *natrc = put_NXAST_OUTPUT_TRUNC(out);
7524
7525
712
    natrc->max_len = htonl(output_trunc->max_len);
7526
712
    natrc->port = htons(ofp_to_u16(output_trunc->port));
7527
712
}
7528
7529
static char * OVS_WARN_UNUSED_RESULT
7530
parse_OUTPUT_TRUNC(const char *arg,
7531
                   const struct ofpact_parse_params *pp OVS_UNUSED)
7532
1
{
7533
    /* Disable output_trunc parsing.  Expose as output(port=N,max_len=M) and
7534
     * reuse parse_OUTPUT to parse output_trunc action. */
7535
1
    return xasprintf("unknown action %s", arg);
7536
1
}
7537
7538
static void
7539
format_OUTPUT_TRUNC(const struct ofpact_output_trunc *a,
7540
                    const struct ofpact_format_params *fp)
7541
1.50k
{
7542
1.50k
    ds_put_format(fp->s, "%soutput%s(port=", colors.special, colors.end);
7543
1.50k
    ofputil_format_port(a->port, fp->port_map, fp->s);
7544
1.50k
    ds_put_format(fp->s, ",max_len=%"PRIu32")", a->max_len);
7545
1.50k
}
7546
7547
static enum ofperr
7548
check_OUTPUT_TRUNC(const struct ofpact_output_trunc *a,
7549
                   const struct ofpact_check_params *cp)
7550
2.20k
{
7551
2.20k
    return ofpact_check_output_port(a->port, cp->max_ports);
7552
2.20k
}
7553

7554
/* Meter.
7555
 *
7556
 * In OpenFlow 1.3 and 1.4, "meter" is an instruction.
7557
 * In OpenFlow 1.5 and later, "meter" is an action.
7558
 *
7559
 * OpenFlow 1.5 */
7560
7561
static enum ofperr
7562
decode_OFPAT_RAW15_METER(uint32_t meter_id,
7563
                         enum ofp_version ofp_version OVS_UNUSED,
7564
                         struct ofpbuf *out)
7565
1.11k
{
7566
1.11k
    struct ofpact_meter *om = ofpact_put_METER(out);
7567
1.11k
    om->meter_id = meter_id;
7568
1.11k
    om->provider_meter_id = UINT32_MAX; /* No provider meter ID. */
7569
1.11k
    return 0;
7570
1.11k
}
7571
7572
static void
7573
encode_METER(const struct ofpact_meter *meter,
7574
             enum ofp_version ofp_version, struct ofpbuf *out)
7575
106
{
7576
106
    if (ofp_version == OFP13_VERSION || ofp_version == OFP14_VERSION) {
7577
104
        instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
7578
104
    } else if (ofp_version >= OFP15_VERSION) {
7579
0
        put_OFPAT15_METER(out, meter->meter_id);
7580
0
    }
7581
106
}
7582
7583
static char * OVS_WARN_UNUSED_RESULT
7584
parse_METER(char *arg, const struct ofpact_parse_params *pp)
7585
565
{
7586
565
    *pp->usable_protocols &= OFPUTIL_P_OF13_UP;
7587
565
    return str_to_u32(arg, &ofpact_put_METER(pp->ofpacts)->meter_id);
7588
565
}
7589
7590
static void
7591
format_METER(const struct ofpact_meter *a,
7592
             const struct ofpact_format_params *fp)
7593
357
{
7594
357
    ds_put_format(fp->s, "%smeter:%s%"PRIu32,
7595
357
                  colors.param, colors.end, a->meter_id);
7596
357
}
7597
7598
static enum ofperr
7599
check_METER(const struct ofpact_meter *a,
7600
            const struct ofpact_check_params *cp OVS_UNUSED)
7601
458
{
7602
458
    uint32_t mid = a->meter_id;
7603
458
    return mid == 0 || mid > OFPM13_MAX ? OFPERR_OFPMMFC_INVALID_METER : 0;
7604
458
}
7605

7606
/* Clear-Actions instruction. */
7607
7608
static void
7609
encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
7610
                     enum ofp_version ofp_version OVS_UNUSED,
7611
                     struct ofpbuf *out OVS_UNUSED)
7612
8
{
7613
8
    if (ofp_version > OFP10_VERSION) {
7614
6
        instruction_put_OFPIT11_CLEAR_ACTIONS(out);
7615
6
    }
7616
8
}
7617
7618
static char * OVS_WARN_UNUSED_RESULT
7619
parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
7620
204
{
7621
204
    ofpact_put_CLEAR_ACTIONS(pp->ofpacts);
7622
204
    return NULL;
7623
204
}
7624
7625
static void
7626
format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED,
7627
                     const struct ofpact_format_params *fp)
7628
294
{
7629
294
    ds_put_format(fp->s, "%sclear_actions%s", colors.value, colors.end);
7630
294
}
7631
7632
static enum ofperr
7633
check_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED,
7634
                    const struct ofpact_check_params *cp OVS_UNUSED)
7635
302
{
7636
302
    return 0;
7637
302
}
7638

7639
/* Write-Actions instruction. */
7640
7641
static void
7642
encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
7643
                     enum ofp_version ofp_version, struct ofpbuf *out)
7644
61
{
7645
61
    if (ofp_version > OFP10_VERSION) {
7646
45
        const size_t ofs = out->size;
7647
7648
45
        instruction_put_OFPIT11_WRITE_ACTIONS(out);
7649
45
        ofpacts_put_openflow_actions(actions->actions,
7650
45
                                     ofpact_nest_get_action_len(actions),
7651
45
                                     out, ofp_version);
7652
45
        ofpacts_update_instruction_actions(out, ofs);
7653
45
    }
7654
61
}
7655
7656
static char * OVS_WARN_UNUSED_RESULT
7657
parse_WRITE_ACTIONS(char *arg, const struct ofpact_parse_params *pp)
7658
1.01k
{
7659
1.01k
    size_t ofs = ofpacts_pull(pp->ofpacts);
7660
1.01k
    struct ofpact_nest *on;
7661
1.01k
    char *error;
7662
7663
    /* Add a Write-Actions instruction and then pull it off. */
7664
1.01k
    ofpact_put(pp->ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
7665
1.01k
    ofpbuf_pull(pp->ofpacts, sizeof *on);
7666
7667
    /* Parse nested actions.
7668
     *
7669
     * We pulled off "write-actions" and the previous actions because the
7670
     * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
7671
     * that it doesn't actually include the nested actions.  That means that
7672
     * ofpacts_parse() would reject them as being part of an Apply-Actions that
7673
     * follows a Write-Actions, which is an invalid order.  */
7674
1.01k
    error = ofpacts_parse(arg, pp, false, OFPACT_WRITE_ACTIONS);
7675
7676
    /* Put the Write-Actions back on and update its length. */
7677
1.01k
    on = ofpbuf_push_uninit(pp->ofpacts, sizeof *on);
7678
1.01k
    on->ofpact.len = pp->ofpacts->size;
7679
7680
    /* Put any previous actions or instructions back on. */
7681
1.01k
    ofpbuf_push_uninit(pp->ofpacts, ofs);
7682
7683
1.01k
    return error;
7684
1.01k
}
7685
7686
static void
7687
format_WRITE_ACTIONS(const struct ofpact_nest *a,
7688
                     const struct ofpact_format_params *fp)
7689
8.68k
{
7690
8.68k
    ds_put_format(fp->s, "%swrite_actions(%s", colors.paren, colors.end);
7691
8.68k
    ofpacts_format(a->actions, ofpact_nest_get_action_len(a), fp);
7692
8.68k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7693
8.68k
}
7694
7695
static enum ofperr
7696
check_WRITE_ACTIONS(struct ofpact_nest *a,
7697
                    const struct ofpact_check_params *cp)
7698
9.60k
{
7699
    /* Use a temporary copy of 'cp' to avoid updating 'cp->usable_protocols',
7700
     * since we can't check consistency of an action set. */
7701
9.60k
    struct ofpact_check_params tmp = *cp;
7702
9.60k
    return ofpacts_check(a->actions, ofpact_nest_get_action_len(a), &tmp);
7703
9.60k
}
7704

7705
/* Action structure for NXAST_WRITE_METADATA.
7706
 *
7707
 * Modifies the 'mask' bits of the metadata value. */
7708
struct nx_action_write_metadata {
7709
    ovs_be16 type;                  /* OFPAT_VENDOR. */
7710
    ovs_be16 len;                   /* Length is 32. */
7711
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
7712
    ovs_be16 subtype;               /* NXAST_WRITE_METADATA. */
7713
    uint8_t zeros[6];               /* Must be zero. */
7714
    ovs_be64 metadata;              /* Metadata register. */
7715
    ovs_be64 mask;                  /* Metadata mask. */
7716
};
7717
OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
7718
7719
static enum ofperr
7720
decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
7721
                                enum ofp_version ofp_version OVS_UNUSED,
7722
                                struct ofpbuf *out)
7723
1.48k
{
7724
1.48k
    struct ofpact_metadata *om;
7725
7726
1.48k
    if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
7727
65
        return OFPERR_NXBRC_MUST_BE_ZERO;
7728
65
    }
7729
7730
1.41k
    om = ofpact_put_WRITE_METADATA(out);
7731
1.41k
    om->metadata = nawm->metadata;
7732
1.41k
    om->mask = nawm->mask;
7733
7734
1.41k
    return 0;
7735
1.48k
}
7736
7737
static void
7738
encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
7739
                      enum ofp_version ofp_version, struct ofpbuf *out)
7740
7
{
7741
7
    if (ofp_version == OFP10_VERSION) {
7742
2
        struct nx_action_write_metadata *nawm;
7743
7744
2
        nawm = put_NXAST_WRITE_METADATA(out);
7745
2
        nawm->metadata = metadata->metadata;
7746
2
        nawm->mask = metadata->mask;
7747
5
    } else {
7748
5
        struct ofp11_instruction_write_metadata *oiwm;
7749
7750
5
        oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
7751
5
        oiwm->metadata = metadata->metadata;
7752
5
        oiwm->metadata_mask = metadata->mask;
7753
5
    }
7754
7
}
7755
7756
static char * OVS_WARN_UNUSED_RESULT
7757
parse_WRITE_METADATA(char *arg, const struct ofpact_parse_params *pp)
7758
591
{
7759
591
    struct ofpact_metadata *om;
7760
591
    char *mask = strchr(arg, '/');
7761
7762
591
    *pp->usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
7763
7764
591
    om = ofpact_put_WRITE_METADATA(pp->ofpacts);
7765
591
    if (mask) {
7766
285
        char *error;
7767
7768
285
        *mask = '\0';
7769
285
        error = str_to_be64(mask + 1, &om->mask);
7770
285
        if (error) {
7771
3
            return error;
7772
3
        }
7773
306
    } else {
7774
306
        om->mask = OVS_BE64_MAX;
7775
306
    }
7776
7777
588
    return str_to_be64(arg, &om->metadata);
7778
591
}
7779
7780
static void
7781
format_WRITE_METADATA(const struct ofpact_metadata *a,
7782
                      const struct ofpact_format_params *fp)
7783
280
{
7784
280
    ds_put_format(fp->s, "%swrite_metadata:%s%#"PRIx64,
7785
280
                  colors.param, colors.end, ntohll(a->metadata));
7786
280
    if (a->mask != OVS_BE64_MAX) {
7787
72
        ds_put_format(fp->s, "/%#"PRIx64, ntohll(a->mask));
7788
72
    }
7789
280
}
7790
7791
static enum ofperr
7792
check_WRITE_METADATA(const struct ofpact_metadata *a OVS_UNUSED,
7793
                     const struct ofpact_check_params *cp OVS_UNUSED)
7794
243
{
7795
243
    return 0;
7796
243
}
7797

7798
/* Check packet length action. */
7799
7800
struct nx_action_check_pkt_larger {
7801
    ovs_be16 type;              /* OFPAT_VENDOR. */
7802
    ovs_be16 len;               /* 24. */
7803
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
7804
    ovs_be16 subtype;           /* NXAST_OUTPUT_REG. */
7805
    ovs_be16 pkt_len;           /* Length of the packet to check. */
7806
    ovs_be16 offset;            /* Result bit offset in destination. */
7807
    /* Followed by:
7808
     * - 'dst', as an OXM/NXM header (either 4 or 8 bytes).
7809
     * - Enough 0-bytes to pad the action out to 24 bytes. */
7810
    uint8_t pad[10];
7811
};
7812
7813
OFP_ASSERT(sizeof(struct nx_action_check_pkt_larger) == 24);
7814
7815
static enum ofperr
7816
decode_NXAST_RAW_CHECK_PKT_LARGER(
7817
    const struct nx_action_check_pkt_larger *ncpl,
7818
    enum ofp_version ofp_version OVS_UNUSED,
7819
    const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
7820
    struct ofpbuf *out)
7821
2.03k
{
7822
2.03k
    struct ofpact_check_pkt_larger *check_pkt_larger;
7823
2.03k
    enum ofperr error;
7824
7825
2.03k
    check_pkt_larger = ofpact_put_CHECK_PKT_LARGER(out);
7826
2.03k
    check_pkt_larger->pkt_len = ntohs(ncpl->pkt_len);
7827
2.03k
    check_pkt_larger->dst.ofs = ntohs(ncpl->offset);
7828
2.03k
    check_pkt_larger->dst.n_bits = 1;
7829
7830
2.03k
    struct ofpbuf b = ofpbuf_const_initializer(ncpl, ntohs(ncpl->len));
7831
2.03k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(ncpl, pad));
7832
7833
2.03k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map,
7834
2.03k
                                     &check_pkt_larger->dst.field,
7835
2.03k
                                     NULL, tlv_bitmap);
7836
2.03k
    if (error) {
7837
526
        return error;
7838
526
    }
7839
7840
1.50k
    if (!is_all_zeros(b.data, b.size)) {
7841
1.00k
        return OFPERR_NXBRC_MUST_BE_ZERO;
7842
1.00k
    }
7843
7844
499
    return mf_check_dst(&check_pkt_larger->dst, NULL);
7845
1.50k
}
7846
7847
static void
7848
encode_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *check_pkt_larger,
7849
                 enum ofp_version ofp_version OVS_UNUSED,
7850
                 struct ofpbuf *out)
7851
216
{
7852
216
    struct nx_action_check_pkt_larger *ncpl = put_NXAST_CHECK_PKT_LARGER(out);
7853
216
    ncpl->pkt_len = htons(check_pkt_larger->pkt_len);
7854
216
    ncpl->offset = htons(check_pkt_larger->dst.ofs);
7855
7856
216
    if (check_pkt_larger->dst.field) {
7857
216
        size_t size = out->size;
7858
216
        out->size = size - sizeof ncpl->pad;
7859
216
        nx_put_mff_header(out, check_pkt_larger->dst.field, 0, false);
7860
216
        out->size = size;
7861
216
    }
7862
216
}
7863
7864
static char * OVS_WARN_UNUSED_RESULT
7865
parse_CHECK_PKT_LARGER(char *arg, const struct ofpact_parse_params *pp)
7866
454
{
7867
454
    char *value;
7868
454
    char *delim;
7869
454
    char *key;
7870
454
    char *error = set_field_split_str(arg, &key, &value, &delim);
7871
454
    if (error) {
7872
4
        return error;
7873
4
    }
7874
7875
450
    delim[0] = '\0';
7876
450
    if (value[strlen(value) - 1] == ')') {
7877
233
        value[strlen(value) - 1] = '\0';
7878
233
    }
7879
450
    struct mf_subfield dst;
7880
450
    error = mf_parse_subfield(&dst, key);
7881
450
    if (error) {
7882
17
        return error;
7883
17
    }
7884
7885
433
    if (dst.n_bits != 1) {
7886
1
        return xstrdup("Only 1-bit destination field is allowed");
7887
1
    }
7888
7889
432
    struct ofpact_check_pkt_larger *check_pkt_larger =
7890
432
        ofpact_put_CHECK_PKT_LARGER(pp->ofpacts);
7891
432
    error = str_to_u16(value, NULL, &check_pkt_larger->pkt_len);
7892
432
    if (error) {
7893
1
        return error;
7894
1
    }
7895
431
    check_pkt_larger->dst = dst;
7896
431
    return NULL;
7897
432
}
7898
7899
static void
7900
format_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *a,
7901
                        const struct ofpact_format_params *fp)
7902
495
{
7903
495
    ds_put_format(fp->s, "%scheck_pkt_larger(%s%"PRIu32")->",
7904
495
                  colors.param, colors.end, a->pkt_len);
7905
495
    mf_format_subfield(&a->dst, fp->s);
7906
495
}
7907
7908
static enum ofperr
7909
check_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *a OVS_UNUSED,
7910
                       const struct ofpact_check_params *cp OVS_UNUSED)
7911
354
{
7912
354
    return 0;
7913
354
}
7914

7915
7916
/* Goto-Table instruction. */
7917
7918
static void
7919
encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
7920
                  enum ofp_version ofp_version, struct ofpbuf *out)
7921
7
{
7922
7
    if (ofp_version == OFP10_VERSION) {
7923
5
        struct nx_action_resubmit *nar;
7924
7925
5
        nar = put_NXAST_RESUBMIT_TABLE(out);
7926
5
        nar->table = goto_table->table_id;
7927
5
        nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
7928
5
    } else {
7929
2
        struct ofp11_instruction_goto_table *oigt;
7930
7931
2
        oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
7932
2
        oigt->table_id = goto_table->table_id;
7933
2
        memset(oigt->pad, 0, sizeof oigt->pad);
7934
2
    }
7935
7
}
7936
7937
static char * OVS_WARN_UNUSED_RESULT
7938
parse_GOTO_TABLE(char *arg, const struct ofpact_parse_params *pp)
7939
244
{
7940
244
    struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(pp->ofpacts);
7941
244
    if (!ofputil_table_from_string(arg, pp->table_map, &ogt->table_id)) {
7942
4
        return xasprintf("unknown table \"%s\"", arg);
7943
4
    }
7944
240
    return NULL;
7945
244
}
7946
7947
static void
7948
format_GOTO_TABLE(const struct ofpact_goto_table *a,
7949
                  const struct ofpact_format_params *fp)
7950
29
{
7951
29
    ds_put_format(fp->s, "%sgoto_table:%s", colors.param, colors.end);
7952
29
    ofputil_format_table(a->table_id, fp->table_map, fp->s);
7953
29
}
7954
7955
static enum ofperr
7956
check_GOTO_TABLE(const struct ofpact_goto_table *a,
7957
                 const struct ofpact_check_params *cp)
7958
72
{
7959
72
    if ((cp->table_id != 255 && a->table_id <= cp->table_id)
7960
36
        || (cp->n_tables != 255 && a->table_id >= cp->n_tables)) {
7961
36
        return OFPERR_OFPBIC_BAD_TABLE_ID;
7962
36
    }
7963
36
    return 0;
7964
72
}
7965

7966
static void
7967
log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
7968
               size_t action_offset, enum ofperr error)
7969
81.2k
{
7970
81.2k
    if (!VLOG_DROP_WARN(&rl)) {
7971
0
        struct ds s;
7972
7973
0
        ds_init(&s);
7974
0
        ds_put_hex_dump(&s, actions, actions_len, 0, false);
7975
0
        VLOG_WARN("bad action at offset %"PRIuSIZE" (%s):\n%s", action_offset,
7976
0
                  ofperr_get_name(error), ds_cstr(&s));
7977
0
        ds_destroy(&s);
7978
0
    }
7979
81.2k
}
7980
7981
static enum ofperr
7982
ofpacts_decode_aligned(struct ofpbuf *openflow, enum ofp_version ofp_version,
7983
                       const struct vl_mff_map *vl_mff_map,
7984
                       uint64_t *ofpacts_tlv_bitmap, struct ofpbuf *ofpacts,
7985
                       size_t *bad_action_offset)
7986
183k
{
7987
183k
    size_t decoded_len = 0;
7988
183k
    enum ofperr error = 0;
7989
7990
183k
    ovs_assert(OFPACT_IS_ALIGNED(openflow->data));
7991
370k
    while (openflow->size) {
7992
        /* Ensure the next action data is properly aligned before decoding it.
7993
         * Some times it's valid to have to decode actions that are not
7994
         * properly aligned (e.g., when processing OF 1.0 statistics reply
7995
         * messages which have a header of 12 bytes - struct ofp10_stats_msg).
7996
         * In other cases the encoder might be buggy.
7997
         */
7998
268k
        if (!OFPACT_IS_ALIGNED(openflow->data)) {
7999
0
            ofpbuf_align(openflow);
8000
0
        }
8001
8002
268k
        const struct ofp_action_header *action = openflow->data;
8003
268k
        enum ofp_raw_action_type raw;
8004
268k
        size_t act_len = 0;
8005
268k
        uint64_t arg;
8006
8007
268k
        error = ofpact_pull_raw(openflow, ofp_version, &raw, &arg, &act_len);
8008
268k
        if (!error) {
8009
239k
            error = ofpact_decode(action, raw, ofp_version, arg, vl_mff_map,
8010
239k
                                  ofpacts_tlv_bitmap, ofpacts);
8011
239k
        }
8012
8013
268k
        if (error) {
8014
81.2k
            *bad_action_offset = decoded_len;
8015
81.2k
            goto done;
8016
81.2k
        }
8017
187k
        decoded_len += act_len;
8018
187k
    }
8019
8020
183k
done:
8021
183k
    return error;
8022
183k
}
8023
8024
static enum ofperr
8025
ofpacts_decode(const void *actions, size_t actions_len,
8026
               enum ofp_version ofp_version,
8027
               const struct vl_mff_map *vl_mff_map,
8028
               uint64_t *ofpacts_tlv_bitmap, struct ofpbuf *ofpacts)
8029
183k
{
8030
183k
    size_t bad_action_offset = 0;
8031
183k
    struct ofpbuf aligned_buf;
8032
8033
183k
    if (!OFPACT_IS_ALIGNED(actions)) {
8034
106k
        ofpbuf_init(&aligned_buf, actions_len);
8035
106k
        ofpbuf_put(&aligned_buf, actions, actions_len);
8036
106k
    } else {
8037
76.8k
        ofpbuf_use_data(&aligned_buf, actions, actions_len);
8038
76.8k
    }
8039
8040
183k
    enum ofperr error
8041
183k
        = ofpacts_decode_aligned(&aligned_buf, ofp_version, vl_mff_map,
8042
183k
                                 ofpacts_tlv_bitmap, ofpacts,
8043
183k
                                 &bad_action_offset);
8044
183k
    if (error) {
8045
81.2k
        log_bad_action(actions, actions_len, bad_action_offset, error);
8046
81.2k
    }
8047
183k
    ofpbuf_uninit(&aligned_buf);
8048
183k
    return error;
8049
183k
}
8050
8051
static enum ofperr
8052
ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
8053
                                unsigned int actions_len,
8054
                                enum ofp_version version,
8055
                                uint32_t allowed_ovsinsts,
8056
                                struct ofpbuf *ofpacts,
8057
                                enum ofpact_type outer_action,
8058
                                const struct vl_mff_map *vl_mff_map,
8059
                                uint64_t *ofpacts_tlv_bitmap)
8060
172k
{
8061
172k
    const struct ofp_action_header *actions;
8062
172k
    enum ofperr error;
8063
8064
172k
    if (actions_len % OFP_ACTION_ALIGN != 0) {
8065
5.99k
        VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
8066
5.99k
                     "multiple of %d", actions_len, OFP_ACTION_ALIGN);
8067
5.99k
        return OFPERR_OFPBRC_BAD_LEN;
8068
5.99k
    }
8069
8070
166k
    actions = ofpbuf_try_pull(openflow, actions_len);
8071
166k
    if (actions == NULL) {
8072
1.55k
        VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
8073
1.55k
                     "remaining message length (%"PRIu32")",
8074
1.55k
                     actions_len, openflow->size);
8075
1.55k
        return OFPERR_OFPBRC_BAD_LEN;
8076
1.55k
    }
8077
8078
165k
    error = ofpacts_decode(actions, actions_len, version, vl_mff_map,
8079
165k
                           ofpacts_tlv_bitmap, ofpacts);
8080
165k
    if (!error) {
8081
88.2k
        error = ofpacts_verify(ofpacts->data, ofpacts->size, version,
8082
88.2k
                               allowed_ovsinsts, outer_action, NULL);
8083
88.2k
    }
8084
165k
    if (error) {
8085
81.8k
        ofpbuf_clear(ofpacts);
8086
81.8k
    }
8087
165k
    return error;
8088
166k
}
8089
8090
/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
8091
 * of 'openflow' into ofpacts.  On success, appends the converted actions to
8092
 * 'ofpacts'; on failure, clears 'ofpacts'.  Returns 0 if successful, otherwise
8093
 * an OpenFlow error.
8094
 *
8095
 * Actions are processed according to their OpenFlow version which
8096
 * is provided in the 'version' parameter.
8097
 *
8098
 * In most places in OpenFlow, actions appear encapsulated in instructions, so
8099
 * you should call ofpacts_pull_openflow_instructions() instead of this
8100
 * function.
8101
 *
8102
 * 'vl_mff_map' and 'ofpacts_tlv_bitmap' are optional. If 'vl_mff_map' is
8103
 * provided, it is used to get variable length mf_fields with configured
8104
 * length in the actions. If an action uses a variable length mf_field,
8105
 * 'ofpacts_tlv_bitmap' is updated accordingly for ref counting. If
8106
 * 'vl_mff_map' is not provided, the default mf_fields with maximum length
8107
 * will be used.
8108
 *
8109
 * The parsed actions are valid generically, but they may not be valid in a
8110
 * specific context.  For example, port numbers up to OFPP_MAX are valid
8111
 * generically, but specific datapaths may only support port numbers in a
8112
 * smaller range.  Use ofpacts_check() to additional check whether actions are
8113
 * valid in a specific context. */
8114
enum ofperr
8115
ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
8116
                              unsigned int actions_len,
8117
                              enum ofp_version version,
8118
                              const struct vl_mff_map *vl_mff_map,
8119
                              uint64_t *ofpacts_tlv_bitmap,
8120
                              struct ofpbuf *ofpacts)
8121
119k
{
8122
119k
    return ofpacts_pull_openflow_actions__(
8123
119k
        openflow, actions_len, version,
8124
119k
        (1u << OVSINST_OFPIT11_APPLY_ACTIONS) | (1u << OVSINST_OFPIT13_METER),
8125
119k
        ofpacts, 0, vl_mff_map, ofpacts_tlv_bitmap);
8126
119k
}
8127

8128
/* OpenFlow 1.1 action sets. */
8129
8130
/* Append ofpact 'a' onto the tail of 'out' */
8131
static void
8132
ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
8133
0
{
8134
0
    ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
8135
0
}
8136
8137
/* The order in which actions in an action set get executed.  This is only for
8138
 * the actions where only the last instance added is used. */
8139
#define ACTION_SET_ORDER                        \
8140
3.35k
    SLOT(OFPACT_STRIP_VLAN)                     \
8141
3.79k
    SLOT(OFPACT_POP_MPLS)                       \
8142
5.31k
    SLOT(OFPACT_DECAP)                          \
8143
5.31k
    SLOT(OFPACT_ENCAP)                          \
8144
6.51k
    SLOT(OFPACT_PUSH_MPLS)                      \
8145
6.51k
    SLOT(OFPACT_PUSH_VLAN)                      \
8146
2.12k
    SLOT(OFPACT_DEC_TTL)                        \
8147
3.98k
    SLOT(OFPACT_DEC_MPLS_TTL)                   \
8148
3.98k
    SLOT(OFPACT_DEC_NSH_TTL)
8149
8150
/* Priority for "final actions" in an action set.  An action set only gets
8151
 * executed at all if at least one of these actions is present.  If more than
8152
 * one is present, then only the one later in this list is executed (and if
8153
 * more than one of a given type, the one later in the action set). */
8154
#define ACTION_SET_FINAL_PRIORITY               \
8155
285
    FINAL(OFPACT_CT)                            \
8156
285
    FINAL(OFPACT_CT_CLEAR)                      \
8157
17
    FINAL(OFPACT_RESUBMIT)                      \
8158
471
    FINAL(OFPACT_OUTPUT)                        \
8159
687
    FINAL(OFPACT_GROUP)
8160
8161
enum action_set_class {
8162
    /* Actions that individually can usefully appear only once in an action
8163
     * set.  If they do appear more than once, then only the last instance is
8164
     * honored. */
8165
#define SLOT(OFPACT) ACTION_SLOT_##OFPACT,
8166
    ACTION_SET_ORDER
8167
#undef SLOT
8168
8169
    /* Final actions. */
8170
#define FINAL(OFPACT) ACTION_SLOT_##OFPACT,
8171
    ACTION_SET_FINAL_PRIORITY
8172
#undef FINAL
8173
8174
    /* Actions that can appear in an action set more than once and are executed
8175
     * in order. */
8176
    ACTION_SLOT_SET_OR_MOVE,
8177
8178
    /* Actions that shouldn't appear in the action set at all. */
8179
    ACTION_SLOT_INVALID
8180
};
8181
8182
/* Count the action set slots. */
8183
#define SLOT(OFPACT) +1
8184
enum { N_ACTION_SLOTS = ACTION_SET_ORDER };
8185
#undef SLOT
8186
8187
static enum action_set_class
8188
action_set_classify(const struct ofpact *a)
8189
39.0k
{
8190
39.0k
    switch (a->type) {
8191
28.9k
#define SLOT(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
8192
0
        ACTION_SET_ORDER
8193
0
#undef SLOT
8194
8195
1.47k
#define FINAL(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
8196
673
        ACTION_SET_FINAL_PRIORITY
8197
0
#undef FINAL
8198
8199
34
    case OFPACT_SET_FIELD:
8200
278
    case OFPACT_REG_MOVE:
8201
317
    case OFPACT_SET_ETH_DST:
8202
394
    case OFPACT_SET_ETH_SRC:
8203
1.02k
    case OFPACT_SET_IP_DSCP:
8204
1.91k
    case OFPACT_SET_IP_ECN:
8205
3.11k
    case OFPACT_SET_IP_TTL:
8206
3.33k
    case OFPACT_SET_IPV4_DST:
8207
3.43k
    case OFPACT_SET_IPV4_SRC:
8208
3.60k
    case OFPACT_SET_L4_DST_PORT:
8209
3.98k
    case OFPACT_SET_L4_SRC_PORT:
8210
4.47k
    case OFPACT_SET_MPLS_LABEL:
8211
5.01k
    case OFPACT_SET_MPLS_TC:
8212
6.13k
    case OFPACT_SET_MPLS_TTL:
8213
6.23k
    case OFPACT_SET_QUEUE:
8214
6.24k
    case OFPACT_SET_TUNNEL:
8215
7.67k
    case OFPACT_SET_VLAN_PCP:
8216
7.71k
    case OFPACT_SET_VLAN_VID:
8217
7.71k
        return ACTION_SLOT_SET_OR_MOVE;
8218
8219
0
    case OFPACT_BUNDLE:
8220
0
    case OFPACT_CLEAR_ACTIONS:
8221
3
    case OFPACT_CLONE:
8222
12
    case OFPACT_NAT:
8223
17
    case OFPACT_CONTROLLER:
8224
17
    case OFPACT_ENQUEUE:
8225
176
    case OFPACT_EXIT:
8226
176
    case OFPACT_UNROLL_XLATE:
8227
240
    case OFPACT_FIN_TIMEOUT:
8228
240
    case OFPACT_GOTO_TABLE:
8229
252
    case OFPACT_LEARN:
8230
252
    case OFPACT_CONJUNCTION:
8231
792
    case OFPACT_METER:
8232
793
    case OFPACT_MULTIPATH:
8233
797
    case OFPACT_NOTE:
8234
797
    case OFPACT_OUTPUT_REG:
8235
955
    case OFPACT_OUTPUT_TRUNC:
8236
956
    case OFPACT_POP_QUEUE:
8237
956
    case OFPACT_SAMPLE:
8238
957
    case OFPACT_STACK_POP:
8239
957
    case OFPACT_STACK_PUSH:
8240
957
    case OFPACT_WRITE_ACTIONS:
8241
957
    case OFPACT_WRITE_METADATA:
8242
957
    case OFPACT_DEBUG_RECIRC:
8243
957
    case OFPACT_DEBUG_SLOW:
8244
957
    case OFPACT_CHECK_PKT_LARGER:
8245
957
    case OFPACT_DELETE_FIELD:
8246
957
        return ACTION_SLOT_INVALID;
8247
8248
0
    default:
8249
0
        OVS_NOT_REACHED();
8250
39.0k
    }
8251
39.0k
}
8252
8253
/* True if an action is allowed in the action set.
8254
 * False otherwise. */
8255
static bool
8256
ofpact_is_allowed_in_actions_set(const struct ofpact *a)
8257
39.0k
{
8258
39.0k
    return action_set_classify(a) != ACTION_SLOT_INVALID;
8259
39.0k
}
8260
8261
/* Reads 'action_set', which contains ofpacts accumulated by
8262
 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
8263
 * executed directly into 'action_list'.  (These names correspond to the
8264
 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
8265
 *
8266
 * In general this involves appending the last instance of each action that is
8267
 * admissible in the action set in the order described in the OpenFlow
8268
 * specification.
8269
 *
8270
 * Exceptions:
8271
 * + output action is only appended if no group action was present in 'in'.
8272
 * + As a simplification all set actions are copied in the order the are
8273
 *   provided in 'in' as many set actions applied to a field has the same
8274
 *   affect as only applying the last action that sets a field and
8275
 *   duplicates are removed by do_xlate_actions().
8276
 *   This has an unwanted side-effect of compsoting multiple
8277
 *   LOAD_REG actions that touch different regions of the same field. */
8278
void
8279
ofpacts_execute_action_set(struct ofpbuf *action_list,
8280
                           const struct ofpbuf *action_set)
8281
0
{
8282
0
    const struct ofpact *slots[N_ACTION_SLOTS] = {NULL, };
8283
8284
0
    struct ofpbuf set_or_move;
8285
0
    ofpbuf_init(&set_or_move, 0);
8286
8287
0
    const struct ofpact *final_action = NULL;
8288
0
    enum action_set_class final_class = 0;
8289
8290
0
    const struct ofpact *cursor;
8291
0
    OFPACT_FOR_EACH (cursor, action_set->data, action_set->size) {
8292
0
        int class = action_set_classify(cursor);
8293
0
        if (class < N_ACTION_SLOTS) {
8294
0
            slots[class] = cursor;
8295
0
        } else if (class < ACTION_SLOT_SET_OR_MOVE) {
8296
0
            if (class >= final_class) {
8297
0
                final_action = cursor;
8298
0
                final_class = class;
8299
0
            }
8300
0
        } else if (class == ACTION_SLOT_SET_OR_MOVE) {
8301
0
            ofpact_copy(&set_or_move, cursor);
8302
0
        } else {
8303
0
            ovs_assert(class == ACTION_SLOT_INVALID);
8304
0
        }
8305
0
    }
8306
8307
0
    if (final_action) {
8308
0
        for (int i = 0; i < N_ACTION_SLOTS; i++) {
8309
0
            if (slots[i]) {
8310
0
                ofpact_copy(action_list, slots[i]);
8311
0
            }
8312
0
        }
8313
0
        ofpbuf_put(action_list, set_or_move.data, set_or_move.size);
8314
0
        ofpact_copy(action_list, final_action);
8315
0
    }
8316
0
    ofpbuf_uninit(&set_or_move);
8317
0
}
8318
8319
8320
static enum ofperr
8321
ofpacts_decode_for_action_set(const struct ofp_action_header *in,
8322
                              size_t n_in, enum ofp_version version,
8323
                              const struct vl_mff_map *vl_mff_map,
8324
                              uint64_t *ofpacts_tlv_bitmap,
8325
                              struct ofpbuf *out)
8326
14.5k
{
8327
14.5k
    enum ofperr error;
8328
14.5k
    struct ofpact *a;
8329
14.5k
    size_t start = out->size;
8330
8331
14.5k
    error = ofpacts_decode(in, n_in, version, vl_mff_map, ofpacts_tlv_bitmap,
8332
14.5k
                           out);
8333
8334
14.5k
    if (error) {
8335
3.87k
        return error;
8336
3.87k
    }
8337
8338
39.0k
    OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
8339
39.0k
        if (!ofpact_is_allowed_in_actions_set(a)) {
8340
957
            VLOG_WARN_RL(&rl, "disallowed action in action set");
8341
957
            return OFPERR_OFPBAC_BAD_TYPE;
8342
957
        }
8343
39.0k
    }
8344
8345
9.74k
    return 0;
8346
10.7k
}
8347

8348
/* OpenFlow 1.1 instructions. */
8349
8350
struct instruction_type_info {
8351
    enum ovs_instruction_type type;
8352
    const char *name;
8353
};
8354
8355
static const struct instruction_type_info inst_info[] = {
8356
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
8357
OVS_INSTRUCTIONS
8358
#undef DEFINE_INST
8359
};
8360
8361
const char *
8362
ovs_instruction_name_from_type(enum ovs_instruction_type type)
8363
23.6k
{
8364
23.6k
    return type < ARRAY_SIZE(inst_info) ? inst_info[type].name : NULL;
8365
23.6k
}
8366
8367
int
8368
ovs_instruction_type_from_name(const char *name)
8369
0
{
8370
0
    const struct instruction_type_info *p;
8371
0
    for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
8372
0
        if (!strcasecmp(name, p->name)) {
8373
0
            return p->type;
8374
0
        }
8375
0
    }
8376
0
    return -1;
8377
0
}
8378
8379
enum ovs_instruction_type
8380
ovs_instruction_type_from_ofpact_type(enum ofpact_type type,
8381
                                      enum ofp_version version)
8382
418k
{
8383
418k
    switch (type) {
8384
628
    case OFPACT_METER:
8385
628
        return (version >= OFP15_VERSION
8386
628
                ? OVSINST_OFPIT11_APPLY_ACTIONS
8387
628
                : OVSINST_OFPIT13_METER);
8388
325
    case OFPACT_CLEAR_ACTIONS:
8389
325
        return OVSINST_OFPIT11_CLEAR_ACTIONS;
8390
9.89k
    case OFPACT_WRITE_ACTIONS:
8391
9.89k
        return OVSINST_OFPIT11_WRITE_ACTIONS;
8392
987
    case OFPACT_WRITE_METADATA:
8393
987
        return OVSINST_OFPIT11_WRITE_METADATA;
8394
98
    case OFPACT_GOTO_TABLE:
8395
98
        return OVSINST_OFPIT11_GOTO_TABLE;
8396
129k
    case OFPACT_OUTPUT:
8397
129k
    case OFPACT_GROUP:
8398
135k
    case OFPACT_CLONE:
8399
147k
    case OFPACT_CONTROLLER:
8400
149k
    case OFPACT_ENQUEUE:
8401
150k
    case OFPACT_OUTPUT_REG:
8402
152k
    case OFPACT_OUTPUT_TRUNC:
8403
156k
    case OFPACT_BUNDLE:
8404
164k
    case OFPACT_SET_VLAN_VID:
8405
169k
    case OFPACT_SET_VLAN_PCP:
8406
176k
    case OFPACT_STRIP_VLAN:
8407
178k
    case OFPACT_PUSH_VLAN:
8408
178k
    case OFPACT_SET_ETH_SRC:
8409
179k
    case OFPACT_SET_ETH_DST:
8410
182k
    case OFPACT_SET_IPV4_SRC:
8411
188k
    case OFPACT_SET_IPV4_DST:
8412
194k
    case OFPACT_SET_IP_DSCP:
8413
196k
    case OFPACT_SET_IP_ECN:
8414
198k
    case OFPACT_SET_IP_TTL:
8415
206k
    case OFPACT_SET_L4_SRC_PORT:
8416
213k
    case OFPACT_SET_L4_DST_PORT:
8417
214k
    case OFPACT_REG_MOVE:
8418
256k
    case OFPACT_SET_FIELD:
8419
257k
    case OFPACT_STACK_PUSH:
8420
259k
    case OFPACT_STACK_POP:
8421
263k
    case OFPACT_DEC_TTL:
8422
269k
    case OFPACT_SET_MPLS_LABEL:
8423
271k
    case OFPACT_SET_MPLS_TC:
8424
273k
    case OFPACT_SET_MPLS_TTL:
8425
275k
    case OFPACT_DEC_MPLS_TTL:
8426
275k
    case OFPACT_PUSH_MPLS:
8427
281k
    case OFPACT_POP_MPLS:
8428
283k
    case OFPACT_SET_TUNNEL:
8429
284k
    case OFPACT_SET_QUEUE:
8430
285k
    case OFPACT_POP_QUEUE:
8431
287k
    case OFPACT_FIN_TIMEOUT:
8432
290k
    case OFPACT_RESUBMIT:
8433
341k
    case OFPACT_LEARN:
8434
341k
    case OFPACT_CONJUNCTION:
8435
344k
    case OFPACT_MULTIPATH:
8436
350k
    case OFPACT_NOTE:
8437
355k
    case OFPACT_EXIT:
8438
359k
    case OFPACT_UNROLL_XLATE:
8439
363k
    case OFPACT_SAMPLE:
8440
363k
    case OFPACT_DEBUG_RECIRC:
8441
364k
    case OFPACT_DEBUG_SLOW:
8442
383k
    case OFPACT_CT:
8443
384k
    case OFPACT_CT_CLEAR:
8444
393k
    case OFPACT_NAT:
8445
399k
    case OFPACT_ENCAP:
8446
404k
    case OFPACT_DECAP:
8447
405k
    case OFPACT_DEC_NSH_TTL:
8448
406k
    case OFPACT_CHECK_PKT_LARGER:
8449
406k
    case OFPACT_DELETE_FIELD:
8450
406k
    default:
8451
406k
        return OVSINST_OFPIT11_APPLY_ACTIONS;
8452
418k
    }
8453
418k
}
8454
8455
enum ofperr
8456
ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
8457
                                    const uint16_t inst_type)
8458
2.23k
{
8459
2.23k
    switch (inst_type) {
8460
8461
0
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
8462
358
    case ENUM:                                      \
8463
358
        *instruction_type = OVSINST_##ENUM;         \
8464
358
        return 0;
8465
0
OVS_INSTRUCTIONS
8466
0
#undef DEFINE_INST
8467
8468
1.87k
    default:
8469
1.87k
        return OFPERR_OFPBIC_UNKNOWN_INST;
8470
2.23k
    }
8471
2.23k
}
8472
8473
/* Two-way translation between OVS's internal "OVSINST_*" representation of
8474
 * instructions and the "OFPIT_*" representation used in OpenFlow. */
8475
struct ovsinst_map {
8476
    enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
8477
    int ofpit;                         /* OFPIT_* number from OpenFlow spec. */
8478
};
8479
8480
static const struct ovsinst_map *
8481
get_ovsinst_map(enum ofp_version version)
8482
19.7k
{
8483
    /* OpenFlow 1.1, 1.2, and 1.5 instructions. */
8484
19.7k
    static const struct ovsinst_map of11[] = {
8485
19.7k
        { OVSINST_OFPIT11_GOTO_TABLE, 1 },
8486
19.7k
        { OVSINST_OFPIT11_WRITE_METADATA, 2 },
8487
19.7k
        { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
8488
19.7k
        { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
8489
19.7k
        { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
8490
19.7k
        { 0, -1 },
8491
19.7k
    };
8492
8493
    /* OpenFlow 1.3 and 1.4 instructions. */
8494
19.7k
    static const struct ovsinst_map of13[] = {
8495
19.7k
        { OVSINST_OFPIT11_GOTO_TABLE, 1 },
8496
19.7k
        { OVSINST_OFPIT11_WRITE_METADATA, 2 },
8497
19.7k
        { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
8498
19.7k
        { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
8499
19.7k
        { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
8500
19.7k
        { OVSINST_OFPIT13_METER, 6 },
8501
19.7k
        { 0, -1 },
8502
19.7k
    };
8503
8504
19.7k
    return version == OFP13_VERSION || version == OFP14_VERSION ? of13 : of11;
8505
19.7k
}
8506
8507
/* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
8508
 * values, into a bitmap of instructions suitable for OpenFlow 'version'
8509
 * (OFP11_VERSION or later), and returns the result. */
8510
ovs_be32
8511
ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
8512
0
{
8513
0
    uint32_t ofpit_bitmap = 0;
8514
0
    const struct ovsinst_map *x;
8515
8516
0
    for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
8517
0
        if (ovsinst_bitmap & (1u << x->ovsinst)) {
8518
0
            ofpit_bitmap |= 1u << x->ofpit;
8519
0
        }
8520
0
    }
8521
0
    return htonl(ofpit_bitmap);
8522
0
}
8523
8524
/* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
8525
 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
8526
 * correspond to OVSINST_* values, and returns the result. */
8527
uint32_t
8528
ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
8529
19.7k
{
8530
19.7k
    uint32_t ovsinst_bitmap = 0;
8531
19.7k
    const struct ovsinst_map *x;
8532
8533
118k
    for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
8534
98.6k
        if (ofpit_bitmap & htonl(1u << x->ofpit)) {
8535
31.6k
            ovsinst_bitmap |= 1u << x->ovsinst;
8536
31.6k
        }
8537
98.6k
    }
8538
19.7k
    return ovsinst_bitmap;
8539
19.7k
}
8540
8541
static inline struct ofp11_instruction *
8542
instruction_next(const struct ofp11_instruction *inst)
8543
19.7k
{
8544
19.7k
    return ((struct ofp11_instruction *) (void *)
8545
19.7k
            ((uint8_t *) inst + ntohs(inst->len)));
8546
19.7k
}
8547
8548
static inline bool
8549
instruction_is_valid(const struct ofp11_instruction *inst,
8550
                     size_t n_instructions)
8551
24.5k
{
8552
24.5k
    uint16_t len = ntohs(inst->len);
8553
24.5k
    return (!(len % OFP11_INSTRUCTION_ALIGN)
8554
22.3k
            && len >= sizeof *inst
8555
22.1k
            && len / sizeof *inst <= n_instructions);
8556
24.5k
}
8557
8558
/* This macro is careful to check for instructions with bad lengths. */
8559
#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
8560
32.7k
    for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
8561
52.4k
         (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
8562
32.7k
         ((LEFT) -= (ntohs((ITER)->len)                                 \
8563
19.7k
                     / sizeof(struct ofp11_instruction)),               \
8564
19.7k
          (ITER) = instruction_next(ITER)))
8565
8566
static enum ofperr
8567
decode_openflow11_instruction(const struct ofp11_instruction *inst,
8568
                              enum ovs_instruction_type *type)
8569
21.9k
{
8570
21.9k
    uint16_t len = ntohs(inst->len);
8571
8572
21.9k
    switch (inst->type) {
8573
116
    case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
8574
116
        return OFPERR_OFPBIC_BAD_EXPERIMENTER;
8575
8576
0
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
8577
20.3k
        case CONSTANT_HTONS(ENUM):                      \
8578
20.3k
            if (EXTENSIBLE                              \
8579
20.3k
                ? len >= sizeof(struct STRUCT)          \
8580
20.3k
                : len == sizeof(struct STRUCT)) {       \
8581
20.1k
                *type = OVSINST_##ENUM;                 \
8582
20.1k
                return 0;                               \
8583
20.1k
            } else {                                    \
8584
179
                return OFPERR_OFPBIC_BAD_LEN;           \
8585
179
            }
8586
116
OVS_INSTRUCTIONS
8587
0
#undef DEFINE_INST
8588
8589
1.43k
    default:
8590
1.43k
        return OFPERR_OFPBIC_UNKNOWN_INST;
8591
21.9k
    }
8592
21.9k
}
8593
8594
static enum ofperr
8595
decode_openflow11_instructions(const struct ofp11_instruction insts[],
8596
                               size_t n_insts, enum ofp_version version,
8597
                               const struct ofp11_instruction *out[])
8598
32.7k
{
8599
32.7k
    const struct ofp11_instruction *inst;
8600
32.7k
    size_t left;
8601
8602
32.7k
    memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
8603
32.7k
    INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
8604
21.9k
        enum ovs_instruction_type type;
8605
21.9k
        enum ofperr error;
8606
8607
21.9k
        error = decode_openflow11_instruction(inst, &type);
8608
21.9k
        if (error) {
8609
1.73k
            return error;
8610
1.73k
        }
8611
8612
20.1k
        if (type == OVSINST_OFPIT13_METER && version >= OFP15_VERSION) {
8613
            /* "meter" is an action, not an instruction, in OpenFlow 1.5. */
8614
304
            return OFPERR_OFPBIC_UNKNOWN_INST;
8615
304
        }
8616
8617
19.8k
        if (out[type]) {
8618
144
            return OFPERR_OFPBIC_DUP_INST;
8619
144
        }
8620
19.7k
        out[type] = inst;
8621
19.7k
    }
8622
8623
30.5k
    if (left) {
8624
2.69k
        VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
8625
2.69k
                     (n_insts - left) * sizeof *inst);
8626
2.69k
        return OFPERR_OFPBIC_BAD_LEN;
8627
2.69k
    }
8628
27.8k
    return 0;
8629
30.5k
}
8630
8631
static void
8632
get_actions_from_instruction(const struct ofp11_instruction *inst,
8633
                             const struct ofp_action_header **actions,
8634
                             size_t *actions_len)
8635
18.3k
{
8636
18.3k
    *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
8637
18.3k
    *actions_len = ntohs(inst->len) - sizeof *inst;
8638
18.3k
}
8639
8640
enum ofperr
8641
ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
8642
                                   unsigned int instructions_len,
8643
                                   enum ofp_version version,
8644
                                   const struct vl_mff_map *vl_mff_map,
8645
                                   uint64_t *ofpacts_tlv_bitmap,
8646
                                   struct ofpbuf *ofpacts)
8647
79.6k
{
8648
79.6k
    const struct ofp11_instruction *instructions;
8649
79.6k
    const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
8650
79.6k
    enum ofperr error;
8651
8652
79.6k
    ofpbuf_clear(ofpacts);
8653
79.6k
    if (version == OFP10_VERSION) {
8654
46.2k
        return ofpacts_pull_openflow_actions__(openflow, instructions_len,
8655
46.2k
                                               version,
8656
46.2k
                                               (1u << N_OVS_INSTRUCTIONS) - 1,
8657
46.2k
                                               ofpacts, 0, vl_mff_map,
8658
46.2k
                                               ofpacts_tlv_bitmap);
8659
46.2k
    }
8660
8661
33.4k
    if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
8662
535
        VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
8663
535
                     "multiple of %d",
8664
535
                     instructions_len, OFP11_INSTRUCTION_ALIGN);
8665
535
        error = OFPERR_OFPBIC_BAD_LEN;
8666
535
        goto exit;
8667
535
    }
8668
8669
32.9k
    instructions = ofpbuf_try_pull(openflow, instructions_len);
8670
32.9k
    if (instructions == NULL) {
8671
153
        VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
8672
153
                     "remaining message length (%"PRIu32")",
8673
153
                     instructions_len, openflow->size);
8674
153
        error = OFPERR_OFPBIC_BAD_LEN;
8675
153
        goto exit;
8676
153
    }
8677
8678
32.7k
    error = decode_openflow11_instructions(
8679
32.7k
        instructions, instructions_len / OFP11_INSTRUCTION_ALIGN, version,
8680
32.7k
        insts);
8681
32.7k
    if (error) {
8682
4.87k
        goto exit;
8683
4.87k
    }
8684
8685
27.8k
    if (insts[OVSINST_OFPIT13_METER]) {
8686
355
        const struct ofp13_instruction_meter *oim;
8687
355
        struct ofpact_meter *om;
8688
8689
355
        oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
8690
355
                           insts[OVSINST_OFPIT13_METER]);
8691
8692
355
        om = ofpact_put_METER(ofpacts);
8693
355
        om->meter_id = ntohl(oim->meter_id);
8694
355
        om->provider_meter_id = UINT32_MAX; /* No provider meter ID. */
8695
355
    }
8696
27.8k
    if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
8697
3.76k
        const struct ofp_action_header *actions;
8698
3.76k
        size_t actions_len;
8699
8700
3.76k
        get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
8701
3.76k
                                     &actions, &actions_len);
8702
3.76k
        error = ofpacts_decode(actions, actions_len, version, vl_mff_map,
8703
3.76k
                               ofpacts_tlv_bitmap, ofpacts);
8704
3.76k
        if (error) {
8705
443
            goto exit;
8706
443
        }
8707
3.76k
    }
8708
27.4k
    if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
8709
298
        instruction_get_OFPIT11_CLEAR_ACTIONS(
8710
298
            insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
8711
298
        ofpact_put_CLEAR_ACTIONS(ofpacts);
8712
298
    }
8713
27.4k
    if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
8714
14.5k
        struct ofpact_nest *on;
8715
14.5k
        const struct ofp_action_header *actions;
8716
14.5k
        size_t actions_len;
8717
14.5k
        size_t start = ofpacts->size;
8718
14.5k
        ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
8719
14.5k
                   offsetof(struct ofpact_nest, actions));
8720
14.5k
        get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
8721
14.5k
                                     &actions, &actions_len);
8722
14.5k
        error = ofpacts_decode_for_action_set(actions, actions_len,
8723
14.5k
                                              version, vl_mff_map,
8724
14.5k
                                              ofpacts_tlv_bitmap, ofpacts);
8725
14.5k
        if (error) {
8726
4.82k
            goto exit;
8727
4.82k
        }
8728
9.74k
        on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
8729
9.74k
        on->ofpact.len = ofpacts->size - start;
8730
9.74k
    }
8731
22.6k
    if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
8732
281
        const struct ofp11_instruction_write_metadata *oiwm;
8733
281
        struct ofpact_metadata *om;
8734
8735
281
        oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
8736
281
                            insts[OVSINST_OFPIT11_WRITE_METADATA]);
8737
8738
281
        om = ofpact_put_WRITE_METADATA(ofpacts);
8739
281
        om->metadata = oiwm->metadata;
8740
281
        om->mask = oiwm->metadata_mask;
8741
281
    }
8742
22.6k
    if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
8743
72
        const struct ofp11_instruction_goto_table *oigt;
8744
72
        struct ofpact_goto_table *ogt;
8745
8746
72
        oigt = instruction_get_OFPIT11_GOTO_TABLE(
8747
72
            insts[OVSINST_OFPIT11_GOTO_TABLE]);
8748
72
        ogt = ofpact_put_GOTO_TABLE(ofpacts);
8749
72
        ogt->table_id = oigt->table_id;
8750
72
    }
8751
8752
22.6k
    error = ofpacts_verify(ofpacts->data, ofpacts->size, version,
8753
22.6k
                           (1u << N_OVS_INSTRUCTIONS) - 1, 0, NULL);
8754
33.4k
exit:
8755
33.4k
    if (error) {
8756
10.8k
        ofpbuf_clear(ofpacts);
8757
10.8k
    }
8758
33.4k
    return error;
8759
22.6k
}
8760
8761
/* Update the length of the instruction that begins at offset 'ofs' within
8762
 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
8763
 * If the instruction contains no nested actions, deletes it entirely. */
8764
static void
8765
ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
8766
565
{
8767
565
    struct ofp11_instruction_actions *oia;
8768
8769
565
    oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
8770
565
    if (openflow->size > ofs + sizeof *oia) {
8771
558
        oia->len = htons(openflow->size - ofs);
8772
558
    } else {
8773
7
        openflow->size = ofs;
8774
7
    }
8775
565
}
8776

8777
/* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
8778
 * switch will never have more than 'max_ports' ports.  Returns 0 if 'port' is
8779
 * valid, otherwise an OpenFlow error code. */
8780
enum ofperr
8781
ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
8782
172k
{
8783
172k
    switch (port) {
8784
3.51k
    case OFPP_IN_PORT:
8785
8.48k
    case OFPP_TABLE:
8786
9.80k
    case OFPP_NORMAL:
8787
12.1k
    case OFPP_FLOOD:
8788
13.9k
    case OFPP_ALL:
8789
16.6k
    case OFPP_CONTROLLER:
8790
17.0k
    case OFPP_LOCAL:
8791
17.0k
        return 0;
8792
8793
28
    case OFPP_NONE:
8794
28
        return OFPERR_OFPBAC_BAD_OUT_PORT;
8795
8796
155k
    default:
8797
155k
        if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
8798
155k
            return 0;
8799
155k
        }
8800
658
        return OFPERR_OFPBAC_BAD_OUT_PORT;
8801
172k
    }
8802
172k
}
8803
8804
/* Removes the protocols that require consistency between match and actions
8805
 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
8806
 *
8807
 * (An example of an inconsistency between match and actions is a flow that
8808
 * does not match on an MPLS Ethertype but has an action that pops an MPLS
8809
 * label.) */
8810
static void
8811
inconsistent_match(enum ofputil_protocol *usable_protocols)
8812
37.2k
{
8813
37.2k
    *usable_protocols &= OFPUTIL_P_OF10_ANY;
8814
37.2k
}
8815
8816
/* May modify flow->packet_type, flow->dl_type, flow->nw_proto and
8817
 * flow->vlan_tci, caller must restore them.
8818
 *
8819
 * Modifies some actions, filling in fields that could not be properly set
8820
 * without context. */
8821
static enum ofperr
8822
ofpact_check__(struct ofpact *a, struct ofpact_check_params *cp)
8823
345k
{
8824
345k
    switch (a->type) {
8825
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                  \
8826
345k
        case OFPACT_##ENUM:                                 \
8827
345k
            return check_##ENUM(ofpact_get_##ENUM(a), cp);
8828
0
        OFPACTS
8829
0
#undef OFPACT
8830
0
    default:
8831
0
        OVS_NOT_REACHED();
8832
345k
    }
8833
345k
}
8834
8835
/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
8836
 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
8837
 * switch with no more than 'max_ports' ports.
8838
 *
8839
 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
8840
 * '*usable_protocols' the protocols that forbid the inconsistency.  (An
8841
 * example of an inconsistency between match and actions is a flow that does
8842
 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
8843
 *
8844
 * May annotate ofpacts with information gathered from the 'match'.
8845
 *
8846
 * May temporarily modify 'match', but restores the changes before
8847
 * returning. */
8848
enum ofperr
8849
ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
8850
              struct ofpact_check_params *cp)
8851
73.7k
{
8852
    /* Save fields that might temporarily be modified. */
8853
73.7k
    struct flow *flow = &cp->match->flow;
8854
73.7k
    ovs_be32 packet_type = flow->packet_type;
8855
73.7k
    ovs_be16 dl_type = flow->dl_type;
8856
73.7k
    uint8_t nw_proto = flow->nw_proto;
8857
73.7k
    union flow_vlan_hdr vlans[FLOW_MAX_VLAN_HEADERS];
8858
73.7k
    memcpy(vlans, flow->vlans, sizeof vlans);
8859
8860
    /* Check all the actions. */
8861
73.7k
    cp->usable_protocols = OFPUTIL_P_ANY;
8862
73.7k
    enum ofperr error = 0;
8863
73.7k
    struct ofpact *a;
8864
345k
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8865
345k
        error = ofpact_check__(a, cp);
8866
345k
        if (error) {
8867
4.37k
            break;
8868
4.37k
        }
8869
345k
    }
8870
8871
    /* Restore fields that may have been modified. */
8872
73.7k
    flow->packet_type = packet_type;
8873
73.7k
    flow->dl_type = dl_type;
8874
73.7k
    memcpy(flow->vlans, vlans, sizeof vlans);
8875
73.7k
    flow->nw_proto = nw_proto;
8876
8877
73.7k
    return error;
8878
73.7k
}
8879
8880
/* Like ofpacts_check(), but reports inconsistencies as
8881
 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
8882
enum ofperr
8883
ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
8884
                          enum ofputil_protocol needed_protocols,
8885
                          struct ofpact_check_params *cp)
8886
42.4k
{
8887
42.4k
    enum ofperr error = ofpacts_check(ofpacts, ofpacts_len, cp);
8888
42.4k
    if (!error && needed_protocols & ~cp->usable_protocols) {
8889
1.67k
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
8890
1.67k
    }
8891
40.7k
    return error;
8892
42.4k
}
8893
8894
/* Returns the destination field that 'ofpact' would write to, or NULL
8895
 * if the action would not write to an mf_field. */
8896
const struct mf_field *
8897
ofpact_get_mf_dst(const struct ofpact *ofpact)
8898
375k
{
8899
375k
    if (ofpact->type == OFPACT_SET_FIELD) {
8900
37.0k
        const struct ofpact_set_field *orl;
8901
8902
37.0k
        orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
8903
37.0k
        return orl->field;
8904
338k
    } else if (ofpact->type == OFPACT_REG_MOVE) {
8905
794
        const struct ofpact_reg_move *orm;
8906
8907
794
        orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
8908
794
        return orm->dst.field;
8909
794
    }
8910
8911
337k
    return NULL;
8912
375k
}
8913
8914
static void OVS_PRINTF_FORMAT(2, 3)
8915
verify_error(char **errorp, const char *format, ...)
8916
5.14k
{
8917
5.14k
    va_list args;
8918
5.14k
    va_start(args, format);
8919
5.14k
    char *error = xvasprintf(format, args);
8920
5.14k
    va_end(args);
8921
8922
5.14k
    if (errorp) {
8923
176
        *errorp = error;
8924
4.96k
    } else {
8925
4.96k
        VLOG_WARN("%s", error);
8926
4.96k
        free(error);
8927
4.96k
    }
8928
5.14k
}
8929
8930
static enum ofperr
8931
unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action,
8932
                    char **errorp)
8933
206
{
8934
206
    verify_error(errorp, "%s action doesn't support nested action %s",
8935
206
                 ofpact_name(outer_action), ofpact_name(action));
8936
206
    return OFPERR_OFPBAC_BAD_ARGUMENT;
8937
206
}
8938
8939
static bool
8940
field_requires_ct(enum mf_field_id field)
8941
39.3k
{
8942
39.3k
    return field == MFF_CT_MARK || field == MFF_CT_LABEL;
8943
39.3k
}
8944
8945
/* Apply nesting constraints for actions */
8946
static enum ofperr
8947
ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action,
8948
                      char **errorp)
8949
375k
{
8950
375k
    const struct mf_field *field = ofpact_get_mf_dst(a);
8951
8952
375k
    if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
8953
96
        verify_error(errorp, "cannot set CT fields outside of ct action");
8954
96
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
8955
96
    }
8956
375k
    if (a->type == OFPACT_NAT) {
8957
11.4k
        if (outer_action != OFPACT_CT) {
8958
2.71k
            verify_error(errorp,
8959
2.71k
                         "Cannot have NAT action outside of \"ct\" action");
8960
2.71k
            return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
8961
2.71k
        }
8962
8.77k
        return 0;
8963
11.4k
    }
8964
8965
364k
    if (outer_action) {
8966
38.1k
        ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
8967
33.2k
                   || outer_action == OFPACT_CT
8968
31.5k
                   || outer_action == OFPACT_CLONE);
8969
8970
38.1k
        if (outer_action == OFPACT_CT) {
8971
1.67k
            if (!field) {
8972
204
                return unsupported_nesting(a->type, outer_action, errorp);
8973
1.47k
            } else if (!field_requires_ct(field->id)) {
8974
69
                verify_error(errorp,
8975
69
                             "%s action doesn't support nested modification "
8976
69
                             "of %s", ofpact_name(outer_action), field->name);
8977
69
                return OFPERR_OFPBAC_BAD_ARGUMENT;
8978
69
            }
8979
1.67k
        }
8980
8981
37.8k
        if (a->type == OFPACT_METER) {
8982
2
            return unsupported_nesting(a->type, outer_action, errorp);
8983
2
        }
8984
37.8k
    }
8985
8986
363k
    return 0;
8987
364k
}
8988
8989
/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
8990
 * appropriate order as defined by the OpenFlow spec and as required by Open
8991
 * vSwitch.
8992
 *
8993
 * The 'version' is relevant only for error reporting: Open vSwitch enforces
8994
 * the same rules for every version of OpenFlow, but different versions require
8995
 * different error codes.
8996
 *
8997
 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
8998
 * instructions that are allowed within 'ofpacts[]'.
8999
 *
9000
 * If 'outer_action' is not zero, it specifies that the actions are nested
9001
 * within another action of type 'outer_action'. */
9002
static enum ofperr
9003
ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
9004
               enum ofp_version version, uint32_t allowed_ovsinsts,
9005
               enum ofpact_type outer_action, char **errorp)
9006
129k
{
9007
129k
    const struct ofpact *a;
9008
129k
    enum ovs_instruction_type inst;
9009
9010
129k
    inst = OVSINST_OFPIT13_METER;
9011
379k
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9012
379k
        enum ovs_instruction_type next;
9013
379k
        enum ofperr error;
9014
9015
379k
        if (a->type == OFPACT_CONJUNCTION) {
9016
7.08k
            OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9017
7.08k
                if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
9018
1.58k
                    verify_error(errorp, "\"conjunction\" actions may be used "
9019
1.58k
                                 "along with \"note\" but not any other kind "
9020
1.58k
                                 "of action (such as the \"%s\" action used "
9021
1.58k
                                 "here)", ofpact_name(a->type));
9022
1.58k
                    return OFPERR_NXBAC_BAD_CONJUNCTION;
9023
1.58k
                }
9024
7.08k
            }
9025
1.72k
            return 0;
9026
3.30k
        }
9027
9028
375k
        error = ofpacts_verify_nested(a, outer_action, errorp);
9029
375k
        if (error) {
9030
3.08k
            return error;
9031
3.08k
        }
9032
9033
372k
        next = ovs_instruction_type_from_ofpact_type(a->type, version);
9034
372k
        if (a > ofpacts
9035
285k
            && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
9036
285k
                ? next < inst
9037
285k
                : next <= inst)) {
9038
449
            const char *name = ovs_instruction_name_from_type(inst);
9039
449
            const char *next_name = ovs_instruction_name_from_type(next);
9040
9041
449
            if (next == inst) {
9042
275
                verify_error(errorp, "duplicate %s instruction not allowed, "
9043
275
                             "for OpenFlow 1.1+ compatibility", name);
9044
275
            } else {
9045
174
                verify_error(errorp, "invalid instruction ordering: "
9046
174
                             "%s must appear before %s, "
9047
174
                             "for OpenFlow 1.1+ compatibility",
9048
174
                             next_name, name);
9049
174
            }
9050
449
            return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
9051
449
        }
9052
372k
        if (!((1u << next) & allowed_ovsinsts)) {
9053
23
            const char *name = ovs_instruction_name_from_type(next);
9054
9055
23
            if (next == OVSINST_OFPIT13_METER && version >= OFP15_VERSION) {
9056
0
                verify_error(errorp, "%s action not allowed here", name);
9057
0
                return OFPERR_OFPBAC_BAD_TYPE;
9058
23
            } else {
9059
23
                verify_error(errorp, "%s instruction not allowed here", name);
9060
23
                return OFPERR_OFPBIC_UNSUP_INST;
9061
23
            }
9062
23
        }
9063
9064
372k
        inst = next;
9065
372k
    }
9066
9067
122k
    return 0;
9068
129k
}
9069

9070
/* Converting ofpacts to OpenFlow. */
9071
9072
static void
9073
encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
9074
              struct ofpbuf *out)
9075
220k
{
9076
220k
    switch (a->type) {
9077
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
9078
220k
        case OFPACT_##ENUM:                                             \
9079
220k
            encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out);      \
9080
220k
            return;
9081
0
        OFPACTS
9082
0
#undef OFPACT
9083
0
    default:
9084
0
        OVS_NOT_REACHED();
9085
220k
    }
9086
220k
}
9087
9088
/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
9089
 * actions in 'openflow', appending the actions to any existing data in
9090
 * 'openflow'. */
9091
size_t
9092
ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
9093
                             struct ofpbuf *openflow,
9094
                             enum ofp_version ofp_version)
9095
22.0k
{
9096
22.0k
    const struct ofpact *a;
9097
22.0k
    size_t start_size = openflow->size;
9098
9099
174k
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9100
174k
        encode_ofpact(a, ofp_version, openflow);
9101
174k
    }
9102
22.0k
    return openflow->size - start_size;
9103
22.0k
}
9104
9105
static enum ovs_instruction_type
9106
ofpact_is_apply_actions(const struct ofpact *a, enum ofp_version version)
9107
46.1k
{
9108
46.1k
    return (ovs_instruction_type_from_ofpact_type(a->type, version)
9109
46.1k
            == OVSINST_OFPIT11_APPLY_ACTIONS);
9110
46.1k
}
9111
9112
void
9113
ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
9114
                                  size_t ofpacts_len,
9115
                                  struct ofpbuf *openflow,
9116
                                  enum ofp_version ofp_version)
9117
666
{
9118
666
    const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
9119
666
    const struct ofpact *a;
9120
9121
666
    if (ofp_version == OFP10_VERSION) {
9122
0
        ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
9123
0
                                     ofp_version);
9124
0
        return;
9125
0
    }
9126
9127
666
    a = ofpacts;
9128
1.34k
    while (a < end) {
9129
683
        if (ofpact_is_apply_actions(a, ofp_version)) {
9130
520
            size_t ofs = openflow->size;
9131
9132
520
            instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
9133
45.9k
            do {
9134
45.9k
                encode_ofpact(a, ofp_version, openflow);
9135
45.9k
                a = ofpact_next(a);
9136
45.9k
            } while (a < end && ofpact_is_apply_actions(a, ofp_version));
9137
520
            ofpacts_update_instruction_actions(openflow, ofs);
9138
520
        } else {
9139
163
            encode_ofpact(a, ofp_version, openflow);
9140
163
            a = ofpact_next(a);
9141
163
        }
9142
683
    }
9143
666
}
9144

9145
/* Sets of supported actions. */
9146
9147
/* Two-way translation between OVS's internal "OFPACT_*" representation of
9148
 * actions and the "OFPAT_*" representation used in some OpenFlow version.
9149
 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
9150
 * instance is specific to one OpenFlow version.) */
9151
struct ofpact_map {
9152
    enum ofpact_type ofpact;    /* Internal name for action type. */
9153
    int ofpat;                  /* OFPAT_* number from OpenFlow spec. */
9154
};
9155
9156
static const struct ofpact_map *
9157
get_ofpact_map(enum ofp_version version)
9158
48.1k
{
9159
    /* OpenFlow 1.0 actions. */
9160
48.1k
    static const struct ofpact_map of10[] = {
9161
48.1k
        { OFPACT_OUTPUT, 0 },
9162
48.1k
        { OFPACT_SET_VLAN_VID, 1 },
9163
48.1k
        { OFPACT_SET_VLAN_PCP, 2 },
9164
48.1k
        { OFPACT_STRIP_VLAN, 3 },
9165
48.1k
        { OFPACT_SET_ETH_SRC, 4 },
9166
48.1k
        { OFPACT_SET_ETH_DST, 5 },
9167
48.1k
        { OFPACT_SET_IPV4_SRC, 6 },
9168
48.1k
        { OFPACT_SET_IPV4_DST, 7 },
9169
48.1k
        { OFPACT_SET_IP_DSCP, 8 },
9170
48.1k
        { OFPACT_SET_L4_SRC_PORT, 9 },
9171
48.1k
        { OFPACT_SET_L4_DST_PORT, 10 },
9172
48.1k
        { OFPACT_ENQUEUE, 11 },
9173
48.1k
        { 0, -1 },
9174
48.1k
    };
9175
9176
    /* OpenFlow 1.1 actions. */
9177
48.1k
    static const struct ofpact_map of11[] = {
9178
48.1k
        { OFPACT_OUTPUT, 0 },
9179
48.1k
        { OFPACT_SET_VLAN_VID, 1 },
9180
48.1k
        { OFPACT_SET_VLAN_PCP, 2 },
9181
48.1k
        { OFPACT_SET_ETH_SRC, 3 },
9182
48.1k
        { OFPACT_SET_ETH_DST, 4 },
9183
48.1k
        { OFPACT_SET_IPV4_SRC, 5 },
9184
48.1k
        { OFPACT_SET_IPV4_DST, 6 },
9185
48.1k
        { OFPACT_SET_IP_DSCP, 7 },
9186
48.1k
        { OFPACT_SET_IP_ECN, 8 },
9187
48.1k
        { OFPACT_SET_L4_SRC_PORT, 9 },
9188
48.1k
        { OFPACT_SET_L4_DST_PORT, 10 },
9189
        /* OFPAT_COPY_TTL_OUT (11) not supported. */
9190
        /* OFPAT_COPY_TTL_IN (12) not supported. */
9191
48.1k
        { OFPACT_SET_MPLS_LABEL, 13 },
9192
48.1k
        { OFPACT_SET_MPLS_TC, 14 },
9193
48.1k
        { OFPACT_SET_MPLS_TTL, 15 },
9194
48.1k
        { OFPACT_DEC_MPLS_TTL, 16 },
9195
48.1k
        { OFPACT_PUSH_VLAN, 17 },
9196
48.1k
        { OFPACT_STRIP_VLAN, 18 },
9197
48.1k
        { OFPACT_PUSH_MPLS, 19 },
9198
48.1k
        { OFPACT_POP_MPLS, 20 },
9199
48.1k
        { OFPACT_SET_QUEUE, 21 },
9200
48.1k
        { OFPACT_GROUP, 22 },
9201
48.1k
        { OFPACT_SET_IP_TTL, 23 },
9202
48.1k
        { OFPACT_DEC_TTL, 24 },
9203
48.1k
        { 0, -1 },
9204
48.1k
    };
9205
9206
    /* OpenFlow 1.2, 1.3, and 1.4 actions. */
9207
48.1k
    static const struct ofpact_map of12[] = {
9208
48.1k
        { OFPACT_OUTPUT, 0 },
9209
        /* OFPAT_COPY_TTL_OUT (11) not supported. */
9210
        /* OFPAT_COPY_TTL_IN (12) not supported. */
9211
48.1k
        { OFPACT_SET_MPLS_TTL, 15 },
9212
48.1k
        { OFPACT_DEC_MPLS_TTL, 16 },
9213
48.1k
        { OFPACT_PUSH_VLAN, 17 },
9214
48.1k
        { OFPACT_STRIP_VLAN, 18 },
9215
48.1k
        { OFPACT_PUSH_MPLS, 19 },
9216
48.1k
        { OFPACT_POP_MPLS, 20 },
9217
48.1k
        { OFPACT_SET_QUEUE, 21 },
9218
48.1k
        { OFPACT_GROUP, 22 },
9219
48.1k
        { OFPACT_SET_IP_TTL, 23 },
9220
48.1k
        { OFPACT_DEC_TTL, 24 },
9221
48.1k
        { OFPACT_SET_FIELD, 25 },
9222
        /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
9223
        /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
9224
48.1k
        { 0, -1 },
9225
48.1k
    };
9226
9227
48.1k
    switch (version) {
9228
2.12k
    case OFP10_VERSION:
9229
2.12k
        return of10;
9230
9231
11.6k
    case OFP11_VERSION:
9232
11.6k
        return of11;
9233
9234
27.8k
    case OFP12_VERSION:
9235
27.8k
    case OFP13_VERSION:
9236
33.0k
    case OFP14_VERSION:
9237
34.4k
    case OFP15_VERSION:
9238
34.4k
    default:
9239
34.4k
        return of12;
9240
48.1k
    }
9241
48.1k
}
9242
9243
/* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
9244
 * values, into a bitmap of actions suitable for OpenFlow 'version', and
9245
 * returns the result. */
9246
ovs_be32
9247
ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
9248
0
{
9249
0
    uint32_t openflow_bitmap = 0;
9250
0
    const struct ofpact_map *x;
9251
9252
0
    for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
9253
0
        if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
9254
0
            openflow_bitmap |= 1u << x->ofpat;
9255
0
        }
9256
0
    }
9257
0
    return htonl(openflow_bitmap);
9258
0
}
9259
9260
/* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
9261
 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
9262
 * and returns the result. */
9263
uint64_t
9264
ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
9265
48.1k
{
9266
48.1k
    uint64_t ofpact_bitmap = 0;
9267
48.1k
    const struct ofpact_map *x;
9268
9269
753k
    for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
9270
705k
        if (ofpat_bitmap & htonl(1u << x->ofpat)) {
9271
217k
            ofpact_bitmap |= UINT64_C(1) << x->ofpact;
9272
217k
        }
9273
705k
    }
9274
48.1k
    return ofpact_bitmap;
9275
48.1k
}
9276
9277
/* Appends to 's' a string representation of the set of OFPACT_* represented
9278
 * by 'ofpacts_bitmap'. */
9279
void
9280
ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
9281
25.0k
{
9282
25.0k
    if (!ofpacts_bitmap) {
9283
805
        ds_put_cstr(s, "<none>");
9284
24.2k
    } else {
9285
143k
        while (ofpacts_bitmap) {
9286
119k
            ds_put_format(s, "%s ",
9287
119k
                          ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
9288
119k
            ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
9289
119k
        }
9290
24.2k
        ds_chomp(s, ' ');
9291
24.2k
    }
9292
25.0k
}
9293

9294
/* Returns true if 'action' outputs to 'port', false otherwise. */
9295
static bool
9296
ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
9297
0
{
9298
0
    switch (ofpact->type) {
9299
0
    case OFPACT_OUTPUT:
9300
0
        return ofpact_get_OUTPUT(ofpact)->port == port;
9301
0
    case OFPACT_ENQUEUE:
9302
0
        return ofpact_get_ENQUEUE(ofpact)->port == port;
9303
0
    case OFPACT_CONTROLLER:
9304
0
        return port == OFPP_CONTROLLER;
9305
9306
0
    case OFPACT_OUTPUT_REG:
9307
0
    case OFPACT_OUTPUT_TRUNC:
9308
0
    case OFPACT_BUNDLE:
9309
0
    case OFPACT_SET_VLAN_VID:
9310
0
    case OFPACT_SET_VLAN_PCP:
9311
0
    case OFPACT_STRIP_VLAN:
9312
0
    case OFPACT_PUSH_VLAN:
9313
0
    case OFPACT_SET_ETH_SRC:
9314
0
    case OFPACT_SET_ETH_DST:
9315
0
    case OFPACT_SET_IPV4_SRC:
9316
0
    case OFPACT_SET_IPV4_DST:
9317
0
    case OFPACT_SET_IP_DSCP:
9318
0
    case OFPACT_SET_IP_ECN:
9319
0
    case OFPACT_SET_IP_TTL:
9320
0
    case OFPACT_SET_L4_SRC_PORT:
9321
0
    case OFPACT_SET_L4_DST_PORT:
9322
0
    case OFPACT_REG_MOVE:
9323
0
    case OFPACT_SET_FIELD:
9324
0
    case OFPACT_STACK_PUSH:
9325
0
    case OFPACT_STACK_POP:
9326
0
    case OFPACT_DEC_TTL:
9327
0
    case OFPACT_SET_MPLS_LABEL:
9328
0
    case OFPACT_SET_MPLS_TC:
9329
0
    case OFPACT_SET_MPLS_TTL:
9330
0
    case OFPACT_DEC_MPLS_TTL:
9331
0
    case OFPACT_SET_TUNNEL:
9332
0
    case OFPACT_WRITE_METADATA:
9333
0
    case OFPACT_SET_QUEUE:
9334
0
    case OFPACT_POP_QUEUE:
9335
0
    case OFPACT_FIN_TIMEOUT:
9336
0
    case OFPACT_RESUBMIT:
9337
0
    case OFPACT_LEARN:
9338
0
    case OFPACT_CONJUNCTION:
9339
0
    case OFPACT_MULTIPATH:
9340
0
    case OFPACT_NOTE:
9341
0
    case OFPACT_EXIT:
9342
0
    case OFPACT_UNROLL_XLATE:
9343
0
    case OFPACT_PUSH_MPLS:
9344
0
    case OFPACT_POP_MPLS:
9345
0
    case OFPACT_SAMPLE:
9346
0
    case OFPACT_CLEAR_ACTIONS:
9347
0
    case OFPACT_CLONE:
9348
0
    case OFPACT_WRITE_ACTIONS:
9349
0
    case OFPACT_GOTO_TABLE:
9350
0
    case OFPACT_METER:
9351
0
    case OFPACT_GROUP:
9352
0
    case OFPACT_DEBUG_RECIRC:
9353
0
    case OFPACT_DEBUG_SLOW:
9354
0
    case OFPACT_CT:
9355
0
    case OFPACT_CT_CLEAR:
9356
0
    case OFPACT_NAT:
9357
0
    case OFPACT_ENCAP:
9358
0
    case OFPACT_DECAP:
9359
0
    case OFPACT_DEC_NSH_TTL:
9360
0
    case OFPACT_CHECK_PKT_LARGER:
9361
0
    case OFPACT_DELETE_FIELD:
9362
0
    default:
9363
0
        return false;
9364
0
    }
9365
0
}
9366
9367
/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9368
 * to 'port', false otherwise. */
9369
bool
9370
ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
9371
                       ofp_port_t port)
9372
0
{
9373
0
    const struct ofpact *a;
9374
9375
0
    OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
9376
0
        if (ofpact_outputs_to_port(a, port)) {
9377
0
            return true;
9378
0
        }
9379
0
    }
9380
9381
0
    return false;
9382
0
}
9383
9384
/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9385
 * to 'group', false otherwise. */
9386
bool
9387
ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
9388
                        uint32_t group_id)
9389
0
{
9390
0
    const struct ofpact *a;
9391
9392
0
    OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
9393
0
        if (a->type == OFPACT_GROUP
9394
0
            && ofpact_get_GROUP(a)->group_id == group_id) {
9395
0
            return true;
9396
0
        }
9397
0
    }
9398
9399
0
    return false;
9400
0
}
9401
9402
/* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9403
 * actions in 'b' are bytewise identical. */
9404
bool
9405
ofpacts_equal(const struct ofpact *a, size_t a_len,
9406
              const struct ofpact *b, size_t b_len)
9407
0
{
9408
0
    return a_len == b_len && (!a_len || !memcmp(a, b, a_len));
9409
0
}
9410
9411
/* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9412
 * actions in 'b' are identical when formatted as strings.  (Converting actions
9413
 * to string form suppresses some rarely meaningful differences, such as the
9414
 * 'compat' member of actions.) */
9415
bool
9416
ofpacts_equal_stringwise(const struct ofpact *a, size_t a_len,
9417
                         const struct ofpact *b, size_t b_len)
9418
0
{
9419
0
    struct ds a_s = DS_EMPTY_INITIALIZER;
9420
0
    struct ofpact_format_params a_fp = { .s = &a_s };
9421
0
    ofpacts_format(a, a_len, &a_fp);
9422
9423
0
    struct ds b_s = DS_EMPTY_INITIALIZER;
9424
0
    struct ofpact_format_params b_fp = { .s = &b_s };
9425
0
    ofpacts_format(b, b_len, &b_fp);
9426
9427
0
    bool equal = !strcmp(ds_cstr(&a_s), ds_cstr(&b_s));
9428
9429
0
    ds_destroy(&a_s);
9430
0
    ds_destroy(&b_s);
9431
9432
0
    return equal;
9433
0
}
9434
9435
/* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
9436
 * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
9437
 *
9438
 * This function relies on the order of 'ofpacts' being correct (as checked by
9439
 * ofpacts_verify()). */
9440
uint32_t
9441
ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
9442
0
{
9443
0
    const struct ofpact *a;
9444
9445
0
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9446
0
        if (a->type == OFPACT_METER) {
9447
0
            return ofpact_get_METER(a)->meter_id;
9448
0
        }
9449
9450
0
        enum ovs_instruction_type inst
9451
0
            = ovs_instruction_type_from_ofpact_type(a->type, 0);
9452
0
        if (inst > OVSINST_OFPIT13_METER) {
9453
0
            break;
9454
0
        }
9455
0
    }
9456
9457
0
    return 0;
9458
0
}
9459

9460
/* Formatting ofpacts. */
9461
9462
static void
9463
ofpact_format(const struct ofpact *a,
9464
              const struct ofpact_format_params *fp)
9465
137k
{
9466
137k
    switch (a->type) {
9467
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
9468
137k
        case OFPACT_##ENUM:                                             \
9469
137k
            format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), fp);  \
9470
137k
            break;
9471
0
        OFPACTS
9472
0
#undef OFPACT
9473
0
    default:
9474
0
        OVS_NOT_REACHED();
9475
137k
    }
9476
137k
}
9477
9478
/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
9479
 * 'ofpacts' to 'fp->s'.  If 'port_map' is nonnull, uses it to translate port
9480
 * numbers to names in output. */
9481
void
9482
ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
9483
               const struct ofpact_format_params *fp)
9484
85.9k
{
9485
85.9k
    if (!ofpacts_len) {
9486
18.4k
        ds_put_format(fp->s, "%sdrop%s", colors.drop, colors.end);
9487
67.5k
    } else {
9488
67.5k
        const struct ofpact *a;
9489
9490
137k
        OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9491
137k
            if (a != ofpacts) {
9492
69.7k
                ds_put_char(fp->s, ',');
9493
69.7k
            }
9494
9495
137k
            ofpact_format(a, fp);
9496
137k
        }
9497
67.5k
    }
9498
85.9k
}
9499

9500
/* Internal use by helpers. */
9501
9502
/* Implementation of ofpact_put_<ENUM>(). */
9503
void *
9504
ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
9505
676k
{
9506
676k
    struct ofpact *ofpact;
9507
9508
676k
    ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
9509
676k
    ofpact = ofpacts->header;
9510
676k
    ofpact_init(ofpact, type, len);
9511
676k
    return ofpact;
9512
676k
}
9513
9514
/* Implementation of ofpact_init_<ENUM>(). */
9515
void
9516
ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
9517
679k
{
9518
679k
    memset(ofpact, 0, len);
9519
679k
    ofpact->type = type;
9520
679k
    ofpact->raw = -1;
9521
679k
    ofpact->len = len;
9522
679k
}
9523
9524
/* Implementation of ofpact_finish_<ENUM>().
9525
 *
9526
 * Finishes composing a variable-length action (begun using
9527
 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
9528
 * bytes and updating its embedded length field.  See the large comment near
9529
 * the end of ofp-actions.h for more information.
9530
 *
9531
 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
9532
 * pointer to the return value of this function. */
9533
void *
9534
ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
9535
189k
{
9536
189k
    ptrdiff_t len;
9537
9538
189k
    ovs_assert(ofpact == ofpacts->header);
9539
189k
    len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
9540
189k
    ovs_assert(len > 0 && len <= UINT16_MAX);
9541
189k
    ofpact->len = len;
9542
189k
    ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
9543
9544
189k
    return ofpacts->header;
9545
189k
}
9546

9547
static char * OVS_WARN_UNUSED_RESULT
9548
ofpact_parse(enum ofpact_type type, char *value,
9549
             const struct ofpact_parse_params *pp)
9550
199k
{
9551
199k
    switch (type) {
9552
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
9553
199k
        case OFPACT_##ENUM:                                             \
9554
199k
            return parse_##ENUM(value, pp);
9555
0
        OFPACTS
9556
0
#undef OFPACT
9557
0
    default:
9558
0
        OVS_NOT_REACHED();
9559
199k
    }
9560
199k
}
9561
9562
static bool
9563
ofpact_type_from_name(const char *name, enum ofpact_type *type)
9564
355k
{
9565
355k
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                            \
9566
14.6M
    if (!strcasecmp(name, NAME)) {                                    \
9567
199k
        *type = OFPACT_##ENUM;                                          \
9568
199k
        return true;                                                    \
9569
199k
    }
9570
355k
    OFPACTS
9571
156k
#undef OFPACT
9572
9573
156k
    return false;
9574
14.2M
}
9575
9576
/* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
9577
 *
9578
 * Returns NULL if successful, otherwise a malloc()'d string describing the
9579
 * error.  The caller is responsible for freeing the returned string.
9580
 *
9581
 * If 'outer_action' is specified, indicates that the actions being parsed
9582
 * are nested within another action of the type specified in 'outer_action'. */
9583
static char * OVS_WARN_UNUSED_RESULT
9584
ofpacts_parse__(char *str, const struct ofpact_parse_params *pp,
9585
                bool allow_instructions, enum ofpact_type outer_action)
9586
28.2k
{
9587
28.2k
    uint32_t orig_size = pp->ofpacts->size;
9588
28.2k
    char *key, *value;
9589
28.2k
    bool drop = false;
9590
28.2k
    char *pos;
9591
9592
28.2k
    pos = str;
9593
374k
    while (ofputil_parse_key_value(&pos, &key, &value)) {
9594
355k
        enum ofpact_type type;
9595
355k
        char *error = NULL;
9596
355k
        ofp_port_t port;
9597
355k
        if (ofpact_type_from_name(key, &type)) {
9598
199k
            error = ofpact_parse(type, value, pp);
9599
9600
199k
            if (type == OFPACT_METER && !allow_instructions) {
9601
                /* Meter is an action in OF1.5 and it's being used in a
9602
                 * context where instructions aren't allowed.  Therefore,
9603
                 * this must be OF1.5+. */
9604
196
                *pp->usable_protocols &= OFPUTIL_P_OF15_UP;
9605
196
            }
9606
199k
        } else if (!strcasecmp(key, "mod_vlan_vid")) {
9607
1.01k
            error = parse_set_vlan_vid(value, true, pp);
9608
155k
        } else if (!strcasecmp(key, "mod_vlan_pcp")) {
9609
977
            error = parse_set_vlan_pcp(value, true, pp);
9610
154k
        } else if (!strcasecmp(key, "set_nw_ttl")) {
9611
1.23k
            error = parse_SET_IP_TTL(value, pp);
9612
152k
        } else if (!strcasecmp(key, "pop_vlan")) {
9613
2.04k
            error = parse_pop_vlan(pp);
9614
150k
        } else if (!strcasecmp(key, "set_tunnel64")) {
9615
453
            error = parse_set_tunnel(value, NXAST_RAW_SET_TUNNEL64, pp);
9616
150k
        } else if (!strcasecmp(key, "load")) {
9617
660
            error = parse_reg_load(value, pp);
9618
149k
        } else if (!strcasecmp(key, "bundle_load")) {
9619
10
            error = parse_bundle_load(value, pp);
9620
149k
        } else if (!strcasecmp(key, "drop")) {
9621
682
            drop = true;
9622
148k
        } else if (!strcasecmp(key, "apply_actions")) {
9623
1
            return xstrdup("apply_actions is the default instruction");
9624
148k
        } else if (ofputil_port_from_string(key, pp->port_map, &port)) {
9625
145k
            ofpact_put_OUTPUT(pp->ofpacts)->port = port;
9626
145k
        } else {
9627
3.23k
            return xasprintf("unknown action %s", key);
9628
3.23k
        }
9629
352k
        if (error) {
9630
6.41k
            return error;
9631
6.41k
        }
9632
345k
        if (pp->ofpacts->size - orig_size > UINT16_MAX) {
9633
11
            return xasprintf("input too big");
9634
11
        }
9635
345k
    }
9636
9637
18.6k
    if (drop && pp->ofpacts->size) {
9638
13
        return xstrdup("\"drop\" must not be accompanied by any other action "
9639
13
                       "or instruction");
9640
13
    }
9641
9642
18.6k
    char *error = NULL;
9643
18.6k
    ofpacts_verify(pp->ofpacts->data, pp->ofpacts->size, OFP11_VERSION,
9644
18.6k
                   (allow_instructions
9645
18.6k
                    ? (1u << N_OVS_INSTRUCTIONS) - 1
9646
18.6k
                    : ((1u << OVSINST_OFPIT11_APPLY_ACTIONS)
9647
12.0k
                       | (1u << OVSINST_OFPIT13_METER))),
9648
18.6k
                   outer_action, &error);
9649
18.6k
    if (error) {
9650
176
        return error;
9651
176
    }
9652
9653
18.4k
    return NULL;
9654
18.6k
}
9655
9656
static char * OVS_WARN_UNUSED_RESULT
9657
ofpacts_parse(char *str, const struct ofpact_parse_params *pp,
9658
              bool allow_instructions, enum ofpact_type outer_action)
9659
28.2k
{
9660
28.2k
    if (pp->depth >= MAX_OFPACT_PARSE_DEPTH) {
9661
3
        return xstrdup("Action nested too deeply");
9662
3
    }
9663
28.2k
    CONST_CAST(struct ofpact_parse_params *, pp)->depth++;
9664
28.2k
    uint32_t orig_size = pp->ofpacts->size;
9665
28.2k
    char *error = ofpacts_parse__(str, pp, allow_instructions, outer_action);
9666
28.2k
    if (error) {
9667
9.85k
        ofpbuf_truncate(pp->ofpacts, orig_size);
9668
9.85k
    }
9669
28.2k
    CONST_CAST(struct ofpact_parse_params *, pp)->depth--;
9670
28.2k
    return error;
9671
28.2k
}
9672
9673
static char * OVS_WARN_UNUSED_RESULT
9674
ofpacts_parse_copy(const char *s_, const struct ofpact_parse_params *pp,
9675
                   bool allow_instructions, enum ofpact_type outer_action)
9676
27.2k
{
9677
27.2k
    char *error, *s;
9678
9679
27.2k
    *pp->usable_protocols = OFPUTIL_P_ANY;
9680
9681
27.2k
    s = xstrdup(s_);
9682
27.2k
    error = ofpacts_parse(s, pp, allow_instructions, outer_action);
9683
27.2k
    free(s);
9684
9685
27.2k
    return error;
9686
27.2k
}
9687
9688
/* Parses 's' as a set of OpenFlow actions and appends the actions to
9689
 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
9690
 * that are nested within the action of type 'outer_action'.
9691
 *
9692
 * Returns NULL if successful, otherwise a malloc()'d string describing the
9693
 * error.  The caller is responsible for freeing the returned string. */
9694
char * OVS_WARN_UNUSED_RESULT
9695
ofpacts_parse_actions(const char *s, const struct ofpact_parse_params *pp)
9696
0
{
9697
0
    return ofpacts_parse_copy(s, pp, false, 0);
9698
0
}
9699
9700
/* Parses 's' as a set of OpenFlow instructions and appends the instructions to
9701
 * 'ofpacts'.
9702
 *
9703
 * Returns NULL if successful, otherwise a malloc()'d string describing the
9704
 * error.  The caller is responsible for freeing the returned string. */
9705
char * OVS_WARN_UNUSED_RESULT
9706
ofpacts_parse_instructions(const char *s, const struct ofpact_parse_params *pp)
9707
13.5k
{
9708
13.5k
    return ofpacts_parse_copy(s, pp, true, 0);
9709
13.5k
}
9710
9711
const char *
9712
ofpact_name(enum ofpact_type type)
9713
121k
{
9714
121k
    switch (type) {
9715
121k
#define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
9716
121k
        OFPACTS
9717
121k
#undef OFPACT
9718
121k
    }
9719
0
    return "<unknown>";
9720
121k
}
9721

9722
/* Low-level action decoding and encoding functions. */
9723
9724
/* Everything needed to identify a particular OpenFlow action. */
9725
struct ofpact_hdrs {
9726
    uint32_t vendor;              /* 0 if standard, otherwise a vendor code. */
9727
    uint16_t type;                /* Type if standard, otherwise subtype. */
9728
    uint8_t ofp_version;          /* From ofp_header. */
9729
};
9730
9731
/* Information about a particular OpenFlow action. */
9732
struct ofpact_raw_instance {
9733
    /* The action's identity. */
9734
    struct ofpact_hdrs hdrs;
9735
    enum ofp_raw_action_type raw;
9736
9737
    /* Looking up the action. */
9738
    struct hmap_node decode_node; /* Based on 'hdrs'. */
9739
    struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
9740
9741
    /* The action's encoded size.
9742
     *
9743
     * If this action is fixed-length, 'min_length' == 'max_length'.
9744
     * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
9745
     * OFP_ACTION_ALIGN) == 65528. */
9746
    unsigned short int min_length;
9747
    unsigned short int max_length;
9748
9749
    /* For actions with a simple integer numeric argument, 'arg_ofs' is the
9750
     * offset of that argument from the beginning of the action and 'arg_len'
9751
     * its length, both in bytes.
9752
     *
9753
     * For actions that take other forms, these are both zero. */
9754
    unsigned short int arg_ofs;
9755
    unsigned short int arg_len;
9756
9757
    /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
9758
    const char *name;
9759
9760
    /* If this action is deprecated, a human-readable string with a brief
9761
     * explanation. */
9762
    const char *deprecation;
9763
};
9764
9765
/* Action header. */
9766
struct ofp_action_header {
9767
    /* The meaning of other values of 'type' generally depends on the OpenFlow
9768
     * version (see enum ofp_raw_action_type).
9769
     *
9770
     * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
9771
     * designates an OpenFlow vendor ID and that the remainder of the action
9772
     * structure has a vendor-defined meaning.
9773
     */
9774
#define OFPAT_VENDOR 0xffff
9775
    ovs_be16 type;
9776
9777
    /* Always a multiple of 8. */
9778
    ovs_be16 len;
9779
9780
    /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
9781
     * ONF_VENDOR_ID.  Other 'type's use this space for some other purpose. */
9782
    ovs_be32 vendor;
9783
};
9784
OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
9785
9786
static bool
9787
ofpact_hdrs_equal(const struct ofpact_hdrs *a,
9788
                  const struct ofpact_hdrs *b)
9789
248k
{
9790
248k
    return (a->vendor == b->vendor
9791
248k
            && a->type == b->type
9792
248k
            && a->ofp_version == b->ofp_version);
9793
248k
}
9794
9795
static uint32_t
9796
ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
9797
259k
{
9798
259k
    return hash_2words(hdrs->vendor,
9799
259k
                       ((uint32_t) hdrs->type << 16) | hdrs->ofp_version);
9800
259k
}
9801
9802
#include "ofp-actions.inc2"
9803
9804
static struct hmap *
9805
ofpact_decode_hmap(void)
9806
259k
{
9807
259k
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
9808
259k
    static struct hmap hmap;
9809
9810
259k
    if (ovsthread_once_start(&once)) {
9811
1
        struct ofpact_raw_instance *inst;
9812
9813
1
        hmap_init(&hmap);
9814
1
        for (inst = all_raw_instances;
9815
385
             inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
9816
384
             inst++) {
9817
384
            hmap_insert(&hmap, &inst->decode_node,
9818
384
                        ofpact_hdrs_hash(&inst->hdrs));
9819
384
        }
9820
1
        ovsthread_once_done(&once);
9821
1
    }
9822
259k
    return &hmap;
9823
259k
}
9824
9825
static struct hmap *
9826
ofpact_encode_hmap(void)
9827
281k
{
9828
281k
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
9829
281k
    static struct hmap hmap;
9830
9831
281k
    if (ovsthread_once_start(&once)) {
9832
1
        struct ofpact_raw_instance *inst;
9833
9834
1
        hmap_init(&hmap);
9835
1
        for (inst = all_raw_instances;
9836
385
             inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
9837
384
             inst++) {
9838
384
            hmap_insert(&hmap, &inst->encode_node,
9839
384
                        hash_2words(inst->raw, inst->hdrs.ofp_version));
9840
384
        }
9841
1
        ovsthread_once_done(&once);
9842
1
    }
9843
281k
    return &hmap;
9844
281k
}
9845
9846
static enum ofperr
9847
ofpact_decode_raw(enum ofp_version ofp_version,
9848
                  const struct ofp_action_header *oah, size_t length,
9849
                  const struct ofpact_raw_instance **instp)
9850
268k
{
9851
268k
    const struct ofpact_raw_instance *inst;
9852
268k
    struct ofpact_hdrs hdrs;
9853
9854
268k
    *instp = NULL;
9855
268k
    if (length < sizeof *oah) {
9856
0
        return OFPERR_OFPBAC_BAD_LEN;
9857
0
    }
9858
9859
    /* Get base action type. */
9860
268k
    if (oah->type == htons(OFPAT_VENDOR)) {
9861
        /* Get vendor. */
9862
143k
        hdrs.vendor = ntohl(oah->vendor);
9863
143k
        if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
9864
            /* Get extension subtype. */
9865
134k
            const struct ext_action_header *nah;
9866
9867
134k
            nah = ALIGNED_CAST(const struct ext_action_header *, oah);
9868
134k
            if (length < sizeof *nah) {
9869
26
                return OFPERR_OFPBAC_BAD_LEN;
9870
26
            }
9871
134k
            hdrs.type = ntohs(nah->subtype);
9872
134k
        } else {
9873
9.10k
            VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
9874
9.10k
                         hdrs.vendor);
9875
9.10k
            return OFPERR_OFPBAC_BAD_VENDOR;
9876
9.10k
        }
9877
143k
    } else {
9878
125k
        hdrs.vendor = 0;
9879
125k
        hdrs.type = ntohs(oah->type);
9880
125k
    }
9881
9882
259k
    hdrs.ofp_version = ofp_version;
9883
259k
    HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
9884
259k
                             ofpact_decode_hmap()) {
9885
248k
        if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
9886
248k
            *instp = inst;
9887
248k
            return 0;
9888
248k
        }
9889
248k
    }
9890
9891
11.3k
    VLOG_WARN_RL(&rl, "unknown %s action for vendor %#"PRIx32" and "
9892
11.3k
                 "type %"PRIu16, ofputil_version_to_string(ofp_version),
9893
11.3k
                 hdrs.vendor, hdrs.type);
9894
11.3k
    return (hdrs.vendor
9895
11.3k
            ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
9896
11.3k
            : OFPERR_OFPBAC_BAD_TYPE);
9897
259k
}
9898
9899
static enum ofperr
9900
ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
9901
                enum ofp_raw_action_type *raw, uint64_t *arg,
9902
                size_t *raw_len)
9903
268k
{
9904
268k
    const struct ofp_action_header *oah = buf->data;
9905
268k
    const struct ofpact_raw_instance *action;
9906
268k
    unsigned int length;
9907
268k
    enum ofperr error;
9908
9909
268k
    *raw = *arg = *raw_len = 0;
9910
268k
    error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
9911
268k
    if (error) {
9912
20.4k
        return error;
9913
20.4k
    }
9914
9915
248k
    if (action->deprecation) {
9916
12.2k
        VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
9917
12.2k
                     action->name, ofputil_version_to_string(ofp_version),
9918
12.2k
                     action->deprecation);
9919
12.2k
    }
9920
9921
248k
    length = ntohs(oah->len);
9922
248k
    if (length > buf->size) {
9923
3.74k
        VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
9924
3.74k
                     "length %"PRIu32, action->name, length, buf->size);
9925
3.74k
        return OFPERR_OFPBAC_BAD_LEN;
9926
3.74k
    }
9927
244k
    if (length < action->min_length || length > action->max_length) {
9928
3.47k
        VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
9929
3.47k
                     "[%hu,%hu]", action->name, length,
9930
3.47k
                     action->min_length, action->max_length);
9931
3.47k
        return OFPERR_OFPBAC_BAD_LEN;
9932
3.47k
    }
9933
240k
    if (length % 8) {
9934
1.31k
        VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
9935
1.31k
                     "of 8", action->name, length);
9936
1.31k
        return OFPERR_OFPBAC_BAD_LEN;
9937
1.31k
    }
9938
9939
239k
    *raw = action->raw;
9940
239k
    *arg = 0;
9941
239k
    if (action->arg_len) {
9942
71.2k
        const uint8_t *p;
9943
71.2k
        int i;
9944
9945
71.2k
        p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
9946
225k
        for (i = 0; i < action->arg_len; i++) {
9947
154k
            *arg = (*arg << 8) | p[i];
9948
154k
        }
9949
71.2k
    }
9950
9951
239k
    ofpbuf_pull(buf, length);
9952
239k
    *raw_len = length;
9953
9954
239k
    return 0;
9955
240k
}
9956
9957
static const struct ofpact_raw_instance *
9958
ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
9959
281k
{
9960
281k
    const struct ofpact_raw_instance *inst;
9961
9962
281k
    HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
9963
281k
                             ofpact_encode_hmap()) {
9964
281k
        if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
9965
281k
            return inst;
9966
281k
        }
9967
281k
    }
9968
281k
    OVS_NOT_REACHED();
9969
281k
}
9970
9971
static void *
9972
ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
9973
               enum ofp_raw_action_type raw, uint64_t arg)
9974
281k
{
9975
281k
    const struct ofpact_raw_instance *inst;
9976
281k
    struct ofp_action_header *oah;
9977
281k
    const struct ofpact_hdrs *hdrs;
9978
9979
281k
    inst = ofpact_raw_lookup(ofp_version, raw);
9980
281k
    hdrs = &inst->hdrs;
9981
9982
281k
    oah = ofpbuf_put_zeros(buf, inst->min_length);
9983
281k
    oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
9984
281k
    oah->len = htons(inst->min_length);
9985
281k
    oah->vendor = htonl(hdrs->vendor);
9986
9987
281k
    switch (hdrs->vendor) {
9988
111k
    case 0:
9989
111k
        break;
9990
9991
169k
    case NX_VENDOR_ID:
9992
169k
    case ONF_VENDOR_ID: {
9993
169k
        struct ext_action_header *nah = (struct ext_action_header *) oah;
9994
169k
        nah->subtype = htons(hdrs->type);
9995
169k
        break;
9996
169k
    }
9997
9998
0
    default:
9999
0
        OVS_NOT_REACHED();
10000
281k
    }
10001
10002
281k
    if (inst->arg_len) {
10003
18.6k
        uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
10004
18.6k
        int i;
10005
10006
60.9k
        for (i = 0; i < inst->arg_len; i++) {
10007
42.2k
            *--p = arg;
10008
42.2k
            arg >>= 8;
10009
42.2k
        }
10010
262k
    } else {
10011
262k
        ovs_assert(!arg);
10012
262k
    }
10013
10014
281k
    return oah;
10015
281k
}
10016
10017
static void
10018
pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
10019
65.4k
{
10020
65.4k
    struct ofp_action_header *oah;
10021
10022
65.4k
    ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
10023
65.4k
                                        OFP_ACTION_ALIGN));
10024
10025
65.4k
    oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
10026
    oah->len = htons(openflow->size - start_ofs);
10027
65.4k
}
10028