2 * Copyright (c) 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "classifier.h"
22 #include "dynamic-string.h"
25 #include "openflow/nicira-ext.h"
27 #include "unaligned.h"
30 VLOG_DEFINE_THIS_MODULE(nx_match
);
32 /* Rate limit for nx_match parse errors. These always indicate a bug in the
33 * peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
37 NXM_INVALID
= OFP_MKERR_NICIRA(OFPET_BAD_REQUEST
, NXBRC_NXM_INVALID
),
38 NXM_BAD_TYPE
= OFP_MKERR_NICIRA(OFPET_BAD_REQUEST
, NXBRC_NXM_BAD_TYPE
),
39 NXM_BAD_VALUE
= OFP_MKERR_NICIRA(OFPET_BAD_REQUEST
, NXBRC_NXM_BAD_VALUE
),
40 NXM_BAD_MASK
= OFP_MKERR_NICIRA(OFPET_BAD_REQUEST
, NXBRC_NXM_BAD_MASK
),
41 NXM_BAD_PREREQ
= OFP_MKERR_NICIRA(OFPET_BAD_REQUEST
, NXBRC_NXM_BAD_PREREQ
),
42 NXM_DUP_TYPE
= OFP_MKERR_NICIRA(OFPET_BAD_REQUEST
, NXBRC_NXM_DUP_TYPE
),
43 BAD_ARGUMENT
= OFP_MKERR(OFPET_BAD_ACTION
, OFPBAC_BAD_ARGUMENT
)
46 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
48 enum nxm_field_index
{
49 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) NFI_NXM_##HEADER,
50 #include "nx-match.def"
55 struct hmap_node hmap_node
;
56 enum nxm_field_index index
; /* NFI_* value. */
57 uint32_t header
; /* NXM_* value. */
58 flow_wildcards_t wildcard
; /* FWW_* bit, if exactly one. */
59 ovs_be16 dl_type
; /* dl_type prerequisite, if nonzero. */
60 uint8_t nw_proto
; /* nw_proto prerequisite, if nonzero. */
61 const char *name
; /* "NXM_*" string. */
64 /* All the known fields. */
65 static struct nxm_field nxm_fields
[N_NXM_FIELDS
] = {
66 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
67 { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
68 CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER },
69 #include "nx-match.def"
72 /* Hash table of 'nxm_fields'. */
73 static struct hmap all_nxm_fields
= HMAP_INITIALIZER(&all_nxm_fields
);
75 /* Possible masks for NXM_OF_ETH_DST_W. */
76 static const uint8_t eth_all_0s
[ETH_ADDR_LEN
]
77 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
78 static const uint8_t eth_all_1s
[ETH_ADDR_LEN
]
79 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
80 static const uint8_t eth_mcast_1
[ETH_ADDR_LEN
]
81 = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
82 static const uint8_t eth_mcast_0
[ETH_ADDR_LEN
]
83 = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
88 if (hmap_is_empty(&all_nxm_fields
)) {
91 for (i
= 0; i
< N_NXM_FIELDS
; i
++) {
92 struct nxm_field
*f
= &nxm_fields
[i
];
93 hmap_insert(&all_nxm_fields
, &f
->hmap_node
,
94 hash_int(f
->header
, 0));
97 /* Verify that the header values are unique (duplicate "case" values
98 * cause a compile error). */
100 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
101 case NXM_##HEADER: break;
102 #include "nx-match.def"
107 static const struct nxm_field
*
108 nxm_field_lookup(uint32_t header
)
114 HMAP_FOR_EACH_WITH_HASH (f
, hmap_node
, hash_int(header
, 0),
116 if (f
->header
== header
) {
124 /* Returns the width of the data for a field with the given 'header', in
127 nxm_field_bytes(uint32_t header
)
129 unsigned int length
= NXM_LENGTH(header
);
130 return NXM_HASMASK(header
) ? length
/ 2 : length
;
133 /* Returns the width of the data for a field with the given 'header', in
136 nxm_field_bits(uint32_t header
)
138 return nxm_field_bytes(header
) * 8;
141 /* nx_pull_match() and helpers. */
144 parse_nx_reg(const struct nxm_field
*f
,
145 struct flow
*flow
, struct flow_wildcards
*wc
,
146 const void *value
, const void *maskp
)
148 int idx
= NXM_NX_REG_IDX(f
->header
);
149 if (wc
->reg_masks
[idx
]) {
152 flow_wildcards_set_reg_mask(wc
, idx
,
153 (NXM_HASMASK(f
->header
)
154 ? ntohl(get_unaligned_be32(maskp
))
156 flow
->regs
[idx
] = ntohl(get_unaligned_be32(value
));
157 flow
->regs
[idx
] &= wc
->reg_masks
[idx
];
163 parse_nxm_entry(struct cls_rule
*rule
, const struct nxm_field
*f
,
164 const void *value
, const void *mask
)
166 struct flow_wildcards
*wc
= &rule
->wc
;
167 struct flow
*flow
= &rule
->flow
;
171 case NFI_NXM_OF_IN_PORT
:
172 flow
->in_port
= ntohs(get_unaligned_be16(value
));
173 if (flow
->in_port
== OFPP_LOCAL
) {
174 flow
->in_port
= ODPP_LOCAL
;
178 /* Ethernet header. */
179 case NFI_NXM_OF_ETH_DST
:
180 if ((wc
->wildcards
& (FWW_DL_DST
| FWW_ETH_MCAST
))
181 != (FWW_DL_DST
| FWW_ETH_MCAST
)) {
184 wc
->wildcards
&= ~(FWW_DL_DST
| FWW_ETH_MCAST
);
185 memcpy(flow
->dl_dst
, value
, ETH_ADDR_LEN
);
188 case NFI_NXM_OF_ETH_DST_W
:
189 if ((wc
->wildcards
& (FWW_DL_DST
| FWW_ETH_MCAST
))
190 != (FWW_DL_DST
| FWW_ETH_MCAST
)) {
192 } else if (eth_addr_equals(mask
, eth_mcast_1
)) {
193 wc
->wildcards
&= ~FWW_ETH_MCAST
;
194 flow
->dl_dst
[0] = *(uint8_t *) value
& 0x01;
195 } else if (eth_addr_equals(mask
, eth_mcast_0
)) {
196 wc
->wildcards
&= ~FWW_DL_DST
;
197 memcpy(flow
->dl_dst
, value
, ETH_ADDR_LEN
);
198 flow
->dl_dst
[0] &= 0xfe;
199 } else if (eth_addr_equals(mask
, eth_all_0s
)) {
201 } else if (eth_addr_equals(mask
, eth_all_1s
)) {
202 wc
->wildcards
&= ~(FWW_DL_DST
| FWW_ETH_MCAST
);
203 memcpy(flow
->dl_dst
, value
, ETH_ADDR_LEN
);
208 case NFI_NXM_OF_ETH_SRC
:
209 memcpy(flow
->dl_src
, value
, ETH_ADDR_LEN
);
211 case NFI_NXM_OF_ETH_TYPE
:
212 flow
->dl_type
= get_unaligned_be16(value
);
216 case NFI_NXM_OF_VLAN_TCI
:
217 if (wc
->vlan_tci_mask
) {
220 cls_rule_set_dl_tci(rule
, get_unaligned_be16(value
));
223 case NFI_NXM_OF_VLAN_TCI_W
:
224 if (wc
->vlan_tci_mask
) {
227 cls_rule_set_dl_tci_masked(rule
, get_unaligned_be16(value
),
228 get_unaligned_be16(mask
));
233 case NFI_NXM_OF_IP_TOS
:
234 if (*(uint8_t *) value
& 0x03) {
235 return NXM_BAD_VALUE
;
237 flow
->nw_tos
= *(uint8_t *) value
;
240 case NFI_NXM_OF_IP_PROTO
:
241 flow
->nw_proto
= *(uint8_t *) value
;
244 /* IP addresses in IP and ARP headers. */
245 case NFI_NXM_OF_IP_SRC
:
246 case NFI_NXM_OF_ARP_SPA
:
247 if (wc
->nw_src_mask
) {
250 cls_rule_set_nw_src(rule
, get_unaligned_be32(value
));
253 case NFI_NXM_OF_IP_SRC_W
:
254 case NFI_NXM_OF_ARP_SPA_W
:
255 if (wc
->nw_src_mask
) {
258 ovs_be32 ip
= get_unaligned_be32(value
);
259 ovs_be32 netmask
= get_unaligned_be32(mask
);
260 if (!cls_rule_set_nw_src_masked(rule
, ip
, netmask
)) {
265 case NFI_NXM_OF_IP_DST
:
266 case NFI_NXM_OF_ARP_TPA
:
267 if (wc
->nw_dst_mask
) {
270 cls_rule_set_nw_dst(rule
, get_unaligned_be32(value
));
273 case NFI_NXM_OF_IP_DST_W
:
274 case NFI_NXM_OF_ARP_TPA_W
:
275 if (wc
->nw_dst_mask
) {
278 ovs_be32 ip
= get_unaligned_be32(value
);
279 ovs_be32 netmask
= get_unaligned_be32(mask
);
280 if (!cls_rule_set_nw_dst_masked(rule
, ip
, netmask
)) {
287 case NFI_NXM_OF_TCP_SRC
:
288 flow
->tp_src
= get_unaligned_be16(value
);
290 case NFI_NXM_OF_TCP_DST
:
291 flow
->tp_dst
= get_unaligned_be16(value
);
295 case NFI_NXM_OF_UDP_SRC
:
296 flow
->tp_src
= get_unaligned_be16(value
);
298 case NFI_NXM_OF_UDP_DST
:
299 flow
->tp_dst
= get_unaligned_be16(value
);
303 case NFI_NXM_OF_ICMP_TYPE
:
304 flow
->tp_src
= htons(*(uint8_t *) value
);
306 case NFI_NXM_OF_ICMP_CODE
:
307 flow
->tp_dst
= htons(*(uint8_t *) value
);
311 case NFI_NXM_OF_ARP_OP
:
312 if (ntohs(get_unaligned_be16(value
)) > 255) {
313 return NXM_BAD_VALUE
;
315 flow
->nw_proto
= ntohs(get_unaligned_be16(value
));
320 case NFI_NXM_NX_TUN_ID
:
321 flow
->tun_id
= htonl(ntohll(get_unaligned_be64(value
)));
325 case NFI_NXM_NX_REG0
:
326 case NFI_NXM_NX_REG0_W
:
328 case NFI_NXM_NX_REG1
:
329 case NFI_NXM_NX_REG1_W
:
332 case NFI_NXM_NX_REG2
:
333 case NFI_NXM_NX_REG2_W
:
336 case NFI_NXM_NX_REG3
:
337 case NFI_NXM_NX_REG3_W
:
342 return parse_nx_reg(f
, flow
, wc
, value
, mask
);
351 nxm_prereqs_ok(const struct nxm_field
*field
, const struct flow
*flow
)
353 return (!field
->dl_type
354 || (field
->dl_type
== flow
->dl_type
355 && (!field
->nw_proto
|| field
->nw_proto
== flow
->nw_proto
)));
359 nx_entry_ok(const void *p
, unsigned int match_len
)
361 unsigned int payload_len
;
367 VLOG_DBG_RL(&rl
, "nx_match ends with partial nxm_header");
371 memcpy(&header_be
, p
, 4);
372 header
= ntohl(header_be
);
374 payload_len
= NXM_LENGTH(header
);
376 VLOG_DBG_RL(&rl
, "nxm_entry %08"PRIx32
" has invalid payload "
380 if (match_len
< payload_len
+ 4) {
381 VLOG_DBG_RL(&rl
, "%"PRIu32
"-byte nxm_entry but only "
382 "%u bytes left in nx_match", payload_len
+ 4, match_len
);
390 nx_pull_match(struct ofpbuf
*b
, unsigned int match_len
, uint16_t priority
,
391 struct cls_rule
*rule
)
396 p
= ofpbuf_try_pull(b
, ROUND_UP(match_len
, 8));
398 VLOG_DBG_RL(&rl
, "nx_match length %zu, rounded up to a "
399 "multiple of 8, is longer than space in message (max "
400 "length %zu)", match_len
, b
->size
);
401 return ofp_mkerr(OFPET_BAD_REQUEST
, OFPBRC_BAD_LEN
);
404 cls_rule_init_catchall(rule
, priority
);
405 while ((header
= nx_entry_ok(p
, match_len
)) != 0) {
406 unsigned length
= NXM_LENGTH(header
);
407 const struct nxm_field
*f
;
410 f
= nxm_field_lookup(header
);
412 error
= NXM_BAD_TYPE
;
413 } else if (!nxm_prereqs_ok(f
, &rule
->flow
)) {
414 error
= NXM_BAD_PREREQ
;
415 } else if (f
->wildcard
&& !(rule
->wc
.wildcards
& f
->wildcard
)) {
416 error
= NXM_DUP_TYPE
;
418 /* 'hasmask' and 'length' are known to be correct at this point
419 * because they are included in 'header' and nxm_field_lookup()
420 * checked them already. */
421 rule
->wc
.wildcards
&= ~f
->wildcard
;
422 error
= parse_nxm_entry(rule
, f
, p
+ 4, p
+ 4 + length
/ 2);
425 VLOG_DBG_RL(&rl
, "bad nxm_entry with vendor=%"PRIu32
", "
426 "field=%"PRIu32
", hasmask=%"PRIu32
", type=%"PRIu32
" "
428 NXM_VENDOR(header
), NXM_FIELD(header
),
429 NXM_HASMASK(header
), NXM_TYPE(header
),
436 match_len
-= 4 + length
;
439 return match_len
? NXM_INVALID
: 0;
442 /* nx_put_match() and helpers.
444 * 'put' functions whose names end in 'w' add a wildcarded field.
445 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
446 * Other 'put' functions add exact-match fields.
450 nxm_put_header(struct ofpbuf
*b
, uint32_t header
)
452 ovs_be32 n_header
= htonl(header
);
453 ofpbuf_put(b
, &n_header
, sizeof n_header
);
457 nxm_put_8(struct ofpbuf
*b
, uint32_t header
, uint8_t value
)
459 nxm_put_header(b
, header
);
460 ofpbuf_put(b
, &value
, sizeof value
);
464 nxm_put_16(struct ofpbuf
*b
, uint32_t header
, ovs_be16 value
)
466 nxm_put_header(b
, header
);
467 ofpbuf_put(b
, &value
, sizeof value
);
471 nxm_put_16w(struct ofpbuf
*b
, uint32_t header
, ovs_be16 value
, ovs_be16 mask
)
473 nxm_put_header(b
, header
);
474 ofpbuf_put(b
, &value
, sizeof value
);
475 ofpbuf_put(b
, &mask
, sizeof mask
);
479 nxm_put_16m(struct ofpbuf
*b
, uint32_t header
, ovs_be16 value
, ovs_be16 mask
)
485 case CONSTANT_HTONS(UINT16_MAX
):
486 nxm_put_16(b
, header
, value
);
490 nxm_put_16w(b
, NXM_MAKE_WILD_HEADER(header
), value
, mask
);
496 nxm_put_32(struct ofpbuf
*b
, uint32_t header
, ovs_be32 value
)
498 nxm_put_header(b
, header
);
499 ofpbuf_put(b
, &value
, sizeof value
);
503 nxm_put_32w(struct ofpbuf
*b
, uint32_t header
, ovs_be32 value
, ovs_be32 mask
)
505 nxm_put_header(b
, header
);
506 ofpbuf_put(b
, &value
, sizeof value
);
507 ofpbuf_put(b
, &mask
, sizeof mask
);
511 nxm_put_32m(struct ofpbuf
*b
, uint32_t header
, ovs_be32 value
, ovs_be32 mask
)
517 case CONSTANT_HTONL(UINT32_MAX
):
518 nxm_put_32(b
, header
, value
);
522 nxm_put_32w(b
, NXM_MAKE_WILD_HEADER(header
), value
, mask
);
528 nxm_put_64(struct ofpbuf
*b
, uint32_t header
, ovs_be64 value
)
530 nxm_put_header(b
, header
);
531 ofpbuf_put(b
, &value
, sizeof value
);
535 nxm_put_eth(struct ofpbuf
*b
, uint32_t header
,
536 const uint8_t value
[ETH_ADDR_LEN
])
538 nxm_put_header(b
, header
);
539 ofpbuf_put(b
, value
, ETH_ADDR_LEN
);
543 nxm_put_eth_dst(struct ofpbuf
*b
,
544 uint32_t wc
, const uint8_t value
[ETH_ADDR_LEN
])
546 switch (wc
& (FWW_DL_DST
| FWW_ETH_MCAST
)) {
547 case FWW_DL_DST
| FWW_ETH_MCAST
:
550 nxm_put_header(b
, NXM_OF_ETH_DST_W
);
551 ofpbuf_put(b
, value
, ETH_ADDR_LEN
);
552 ofpbuf_put(b
, eth_mcast_1
, ETH_ADDR_LEN
);
555 nxm_put_header(b
, NXM_OF_ETH_DST_W
);
556 ofpbuf_put(b
, value
, ETH_ADDR_LEN
);
557 ofpbuf_put(b
, eth_mcast_0
, ETH_ADDR_LEN
);
560 nxm_put_eth(b
, NXM_OF_ETH_DST
, value
);
566 nx_put_match(struct ofpbuf
*b
, const struct cls_rule
*cr
)
568 const flow_wildcards_t wc
= cr
->wc
.wildcards
;
569 const struct flow
*flow
= &cr
->flow
;
570 const size_t start_len
= b
->size
;
575 if (!(wc
& FWW_IN_PORT
)) {
576 uint16_t in_port
= flow
->in_port
;
577 if (in_port
== ODPP_LOCAL
) {
578 in_port
= OFPP_LOCAL
;
580 nxm_put_16(b
, NXM_OF_IN_PORT
, htons(in_port
));
584 nxm_put_eth_dst(b
, wc
, flow
->dl_dst
);
585 if (!(wc
& FWW_DL_SRC
)) {
586 nxm_put_eth(b
, NXM_OF_ETH_SRC
, flow
->dl_src
);
588 if (!(wc
& FWW_DL_TYPE
)) {
589 nxm_put_16(b
, NXM_OF_ETH_TYPE
, flow
->dl_type
);
593 nxm_put_16m(b
, NXM_OF_VLAN_TCI
, flow
->vlan_tci
, cr
->wc
.vlan_tci_mask
);
596 if (!(wc
& FWW_DL_TYPE
) && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
598 if (!(wc
& FWW_NW_TOS
)) {
599 nxm_put_8(b
, NXM_OF_IP_TOS
, flow
->nw_tos
& 0xfc);
601 nxm_put_32m(b
, NXM_OF_IP_SRC
, flow
->nw_src
, cr
->wc
.nw_src_mask
);
602 nxm_put_32m(b
, NXM_OF_IP_DST
, flow
->nw_dst
, cr
->wc
.nw_dst_mask
);
604 if (!(wc
& FWW_NW_PROTO
)) {
605 nxm_put_8(b
, NXM_OF_IP_PROTO
, flow
->nw_proto
);
606 switch (flow
->nw_proto
) {
609 if (!(wc
& FWW_TP_SRC
)) {
610 nxm_put_16(b
, NXM_OF_TCP_SRC
, flow
->tp_src
);
612 if (!(wc
& FWW_TP_DST
)) {
613 nxm_put_16(b
, NXM_OF_TCP_DST
, flow
->tp_dst
);
619 if (!(wc
& FWW_TP_SRC
)) {
620 nxm_put_16(b
, NXM_OF_UDP_SRC
, flow
->tp_src
);
622 if (!(wc
& FWW_TP_DST
)) {
623 nxm_put_16(b
, NXM_OF_UDP_DST
, flow
->tp_dst
);
629 if (!(wc
& FWW_TP_SRC
)) {
630 nxm_put_8(b
, NXM_OF_ICMP_TYPE
, ntohs(flow
->tp_src
));
632 if (!(wc
& FWW_TP_DST
)) {
633 nxm_put_8(b
, NXM_OF_ICMP_CODE
, ntohs(flow
->tp_dst
));
638 } else if (!(wc
& FWW_DL_TYPE
) && flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
640 if (!(wc
& FWW_NW_PROTO
)) {
641 nxm_put_16(b
, NXM_OF_ARP_OP
, htons(flow
->nw_proto
));
643 nxm_put_32m(b
, NXM_OF_ARP_SPA
, flow
->nw_src
, cr
->wc
.nw_src_mask
);
644 nxm_put_32m(b
, NXM_OF_ARP_TPA
, flow
->nw_dst
, cr
->wc
.nw_dst_mask
);
648 if (!(wc
& FWW_TUN_ID
)) {
649 nxm_put_64(b
, NXM_NX_TUN_ID
, htonll(ntohl(flow
->tun_id
)));
653 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
654 nxm_put_32m(b
, NXM_NX_REG(i
),
655 htonl(flow
->regs
[i
]), htonl(cr
->wc
.reg_masks
[i
]));
658 match_len
= b
->size
- start_len
;
659 ofpbuf_put_zeros(b
, ROUND_UP(match_len
, 8) - match_len
);
663 /* nx_match_to_string() and helpers. */
665 static void format_nxm_field_name(struct ds
*, uint32_t header
);
668 nx_match_to_string(const uint8_t *p
, unsigned int match_len
)
674 return xstrdup("<any>");
678 while ((header
= nx_entry_ok(p
, match_len
)) != 0) {
679 unsigned int length
= NXM_LENGTH(header
);
680 unsigned int value_len
= nxm_field_bytes(header
);
681 const uint8_t *value
= p
+ 4;
682 const uint8_t *mask
= value
+ value_len
;
686 ds_put_cstr(&s
, ", ");
689 format_nxm_field_name(&s
, header
);
690 ds_put_char(&s
, '(');
692 for (i
= 0; i
< value_len
; i
++) {
693 ds_put_format(&s
, "%02x", value
[i
]);
695 if (NXM_HASMASK(header
)) {
696 ds_put_char(&s
, '/');
697 for (i
= 0; i
< value_len
; i
++) {
698 ds_put_format(&s
, "%02x", mask
[i
]);
701 ds_put_char(&s
, ')');
704 match_len
-= 4 + length
;
709 ds_put_cstr(&s
, ", ");
712 ds_put_format(&s
, "<%u invalid bytes>", match_len
);
715 return ds_steal_cstr(&s
);
719 format_nxm_field_name(struct ds
*s
, uint32_t header
)
721 const struct nxm_field
*f
= nxm_field_lookup(header
);
723 ds_put_cstr(s
, f
->name
);
725 ds_put_format(s
, "%d:%d", NXM_VENDOR(header
), NXM_FIELD(header
));
730 parse_nxm_field_name(const char *name
, int name_len
)
732 const struct nxm_field
*f
;
734 /* Check whether it's a field name. */
735 for (f
= nxm_fields
; f
< &nxm_fields
[ARRAY_SIZE(nxm_fields
)]; f
++) {
736 if (!strncmp(f
->name
, name
, name_len
) && f
->name
[name_len
] == '\0') {
741 /* Check whether it's a 32-bit field header value as hex.
742 * (This isn't ordinarily useful except for testing error behavior.) */
744 uint32_t header
= hexits_value(name
, name_len
, NULL
);
745 if (header
!= UINT_MAX
) {
753 /* nx_match_from_string(). */
756 nx_match_from_string(const char *s
, struct ofpbuf
*b
)
758 const char *full_s
= s
;
759 const size_t start_len
= b
->size
;
762 if (!strcmp(s
, "<any>")) {
763 /* Ensure that 'b->data' isn't actually null. */
764 ofpbuf_prealloc_tailroom(b
, 1);
768 for (s
+= strspn(s
, ", "); *s
; s
+= strspn(s
, ", ")) {
775 name_len
= strcspn(s
, "(");
776 if (s
[name_len
] != '(') {
777 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s
);
780 header
= parse_nxm_field_name(name
, name_len
);
782 ovs_fatal(0, "%s: unknown field `%.*s'", full_s
, name_len
, s
);
787 nxm_put_header(b
, header
);
788 s
= ofpbuf_put_hex(b
, s
, &n
);
789 if (n
!= nxm_field_bytes(header
)) {
790 ovs_fatal(0, "%.2s: hex digits expected", s
);
792 if (NXM_HASMASK(header
)) {
795 ovs_fatal(0, "%s: missing / in masked field %.*s",
796 full_s
, name_len
, name
);
798 s
= ofpbuf_put_hex(b
, s
+ 1, &n
);
799 if (n
!= nxm_field_bytes(header
)) {
800 ovs_fatal(0, "%.2s: hex digits expected", s
);
806 ovs_fatal(0, "%s: missing ) following field %.*s",
807 full_s
, name_len
, name
);
812 match_len
= b
->size
- start_len
;
813 ofpbuf_put_zeros(b
, ROUND_UP(match_len
, 8) - match_len
);
818 parse_nxm_field_bits(const char *s
, uint32_t *headerp
, int *ofsp
, int *n_bitsp
)
820 const char *full_s
= s
;
828 name_len
= strcspn(s
, "[");
829 if (s
[name_len
] != '[') {
830 ovs_fatal(0, "%s: missing [ looking for field name", full_s
);
833 header
= parse_nxm_field_name(name
, name_len
);
835 ovs_fatal(0, "%s: unknown field `%.*s'", full_s
, name_len
, s
);
837 width
= nxm_field_bits(header
);
840 if (sscanf(s
, "[%d..%d]", &start
, &end
) == 2) {
842 } else if (sscanf(s
, "[%d]", &start
) == 1) {
844 } else if (!strncmp(s
, "[]", 2)) {
848 ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
849 "[<start>..<end>]", full_s
);
851 s
= strchr(s
, ']') + 1;
854 ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
856 } else if (start
>= width
) {
857 ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
858 "%d bits wide", full_s
, start
, width
);
859 } else if (end
>= width
){
860 ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
861 "%d bits wide", full_s
, end
, width
);
866 *n_bitsp
= end
- start
+ 1;
872 nxm_parse_reg_move(struct nx_action_reg_move
*move
, const char *s
)
874 const char *full_s
= s
;
876 int src_ofs
, dst_ofs
;
877 int src_n_bits
, dst_n_bits
;
879 s
= parse_nxm_field_bits(s
, &src
, &src_ofs
, &src_n_bits
);
880 if (strncmp(s
, "->", 2)) {
881 ovs_fatal(0, "%s: missing `->' following source", full_s
);
884 s
= parse_nxm_field_bits(s
, &dst
, &dst_ofs
, &dst_n_bits
);
886 ovs_fatal(0, "%s: trailing garbage following destination", full_s
);
889 if (src_n_bits
!= dst_n_bits
) {
890 ovs_fatal(0, "%s: source field is %d bits wide but destination is "
891 "%d bits wide", full_s
, src_n_bits
, dst_n_bits
);
894 move
->type
= htons(OFPAT_VENDOR
);
895 move
->len
= htons(sizeof *move
);
896 move
->vendor
= htonl(NX_VENDOR_ID
);
897 move
->subtype
= htons(NXAST_REG_MOVE
);
898 move
->n_bits
= htons(src_n_bits
);
899 move
->src_ofs
= htons(src_ofs
);
900 move
->dst_ofs
= htons(dst_ofs
);
901 move
->src
= htonl(src
);
902 move
->dst
= htonl(dst
);
906 nxm_parse_reg_load(struct nx_action_reg_load
*load
, const char *s
)
908 const char *full_s
= s
;
913 value
= strtoull(s
, (char **) &s
, 0);
914 if (strncmp(s
, "->", 2)) {
915 ovs_fatal(0, "%s: missing `->' following value", full_s
);
918 s
= parse_nxm_field_bits(s
, &dst
, &ofs
, &n_bits
);
920 ovs_fatal(0, "%s: trailing garbage following destination", full_s
);
923 if (n_bits
< 64 && (value
>> n_bits
) != 0) {
924 ovs_fatal(0, "%s: value %llu does not fit into %d bits",
925 full_s
, value
, n_bits
);
928 load
->type
= htons(OFPAT_VENDOR
);
929 load
->len
= htons(sizeof *load
);
930 load
->vendor
= htonl(NX_VENDOR_ID
);
931 load
->subtype
= htons(NXAST_REG_LOAD
);
932 load
->ofs_nbits
= htons((ofs
<< 6) | (n_bits
- 1));
933 load
->dst
= htonl(dst
);
934 load
->value
= htonll(value
);
937 /* nxm_format_reg_move(), nxm_format_reg_load(). */
940 format_nxm_field_bits(struct ds
*s
, uint32_t header
, int ofs
, int n_bits
)
942 format_nxm_field_name(s
, header
);
944 ds_put_format(s
, "[%d..%d]", ofs
, ofs
+ n_bits
- 1);
946 ds_put_format(s
, "[%d]", ofs
);
951 nxm_format_reg_move(const struct nx_action_reg_move
*move
, struct ds
*s
)
953 int n_bits
= ntohs(move
->n_bits
);
954 int src_ofs
= ntohs(move
->src_ofs
);
955 int dst_ofs
= ntohs(move
->dst_ofs
);
956 uint32_t src
= ntohl(move
->src
);
957 uint32_t dst
= ntohl(move
->dst
);
959 ds_put_format(s
, "move:");
960 format_nxm_field_bits(s
, src
, src_ofs
, n_bits
);
961 ds_put_cstr(s
, "->");
962 format_nxm_field_bits(s
, dst
, dst_ofs
, n_bits
);
966 nxm_format_reg_load(const struct nx_action_reg_load
*load
, struct ds
*s
)
968 uint16_t ofs_nbits
= ntohs(load
->ofs_nbits
);
969 int ofs
= ofs_nbits
>> 6;
970 int n_bits
= (ofs_nbits
& 0x3f) + 1;
971 uint32_t dst
= ntohl(load
->dst
);
972 uint64_t value
= ntohll(load
->value
);
974 ds_put_format(s
, "load:%"PRIu64
"->", value
);
975 format_nxm_field_bits(s
, dst
, ofs
, n_bits
);
978 /* nxm_check_reg_move(), nxm_check_reg_load(). */
981 field_ok(const struct nxm_field
*f
, const struct flow
*flow
, int size
)
983 return (f
&& !NXM_HASMASK(f
->header
)
984 && nxm_prereqs_ok(f
, flow
) && size
<= nxm_field_bits(f
->header
));
988 nxm_check_reg_move(const struct nx_action_reg_move
*action
,
989 const struct flow
*flow
)
991 const struct nxm_field
*src
;
992 const struct nxm_field
*dst
;
994 if (action
->n_bits
== htons(0)) {
998 src
= nxm_field_lookup(ntohl(action
->src
));
999 if (!field_ok(src
, flow
, ntohs(action
->src_ofs
) + ntohs(action
->n_bits
))) {
1000 return BAD_ARGUMENT
;
1003 dst
= nxm_field_lookup(ntohl(action
->dst
));
1004 if (!field_ok(dst
, flow
, ntohs(action
->dst_ofs
) + ntohs(action
->n_bits
))) {
1005 return BAD_ARGUMENT
;
1008 if (!NXM_IS_NX_REG(dst
->header
)
1009 && dst
->header
!= NXM_OF_VLAN_TCI
1010 && dst
->header
!= NXM_NX_TUN_ID
) {
1011 return BAD_ARGUMENT
;
1018 nxm_check_reg_load(const struct nx_action_reg_load
*action
,
1019 const struct flow
*flow
)
1021 const struct nxm_field
*dst
;
1024 ofs
= ntohs(action
->ofs_nbits
) >> 6;
1025 n_bits
= (ntohs(action
->ofs_nbits
) & 0x3f) + 1;
1026 dst
= nxm_field_lookup(ntohl(action
->dst
));
1027 if (!field_ok(dst
, flow
, ofs
+ n_bits
)) {
1028 return BAD_ARGUMENT
;
1031 /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1033 if (n_bits
< 64 && ntohll(action
->value
) >> n_bits
) {
1034 return BAD_ARGUMENT
;
1037 if (!NXM_IS_NX_REG(dst
->header
)) {
1038 return BAD_ARGUMENT
;
1044 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1047 nxm_read_field(const struct nxm_field
*src
, const struct flow
*flow
)
1049 switch (src
->index
) {
1050 case NFI_NXM_OF_IN_PORT
:
1051 return flow
->in_port
== ODPP_LOCAL
? OFPP_LOCAL
: flow
->in_port
;
1053 case NFI_NXM_OF_ETH_DST
:
1054 return eth_addr_to_uint64(flow
->dl_dst
);
1056 case NFI_NXM_OF_ETH_SRC
:
1057 return eth_addr_to_uint64(flow
->dl_src
);
1059 case NFI_NXM_OF_ETH_TYPE
:
1060 return ntohs(flow
->dl_type
);
1062 case NFI_NXM_OF_VLAN_TCI
:
1063 return ntohs(flow
->vlan_tci
);
1065 case NFI_NXM_OF_IP_TOS
:
1066 return flow
->nw_tos
;
1068 case NFI_NXM_OF_IP_PROTO
:
1069 case NFI_NXM_OF_ARP_OP
:
1070 return flow
->nw_proto
;
1072 case NFI_NXM_OF_IP_SRC
:
1073 case NFI_NXM_OF_ARP_SPA
:
1074 return ntohl(flow
->nw_src
);
1076 case NFI_NXM_OF_IP_DST
:
1077 case NFI_NXM_OF_ARP_TPA
:
1078 return ntohl(flow
->nw_dst
);
1080 case NFI_NXM_OF_TCP_SRC
:
1081 case NFI_NXM_OF_UDP_SRC
:
1082 return ntohs(flow
->tp_src
);
1084 case NFI_NXM_OF_TCP_DST
:
1085 case NFI_NXM_OF_UDP_DST
:
1086 return ntohs(flow
->tp_dst
);
1088 case NFI_NXM_OF_ICMP_TYPE
:
1089 return ntohs(flow
->tp_src
) & 0xff;
1091 case NFI_NXM_OF_ICMP_CODE
:
1092 return ntohs(flow
->tp_dst
) & 0xff;
1094 case NFI_NXM_NX_TUN_ID
:
1095 return ntohl(flow
->tun_id
);
1097 #define NXM_READ_REGISTER(IDX) \
1098 case NFI_NXM_NX_REG##IDX: \
1099 return flow->regs[IDX]; \
1100 case NFI_NXM_NX_REG##IDX##_W: \
1103 NXM_READ_REGISTER(0);
1104 #if FLOW_N_REGS >= 2
1105 NXM_READ_REGISTER(1);
1107 #if FLOW_N_REGS >= 3
1108 NXM_READ_REGISTER(2);
1110 #if FLOW_N_REGS >= 4
1111 NXM_READ_REGISTER(3);
1117 case NFI_NXM_OF_ETH_DST_W
:
1118 case NFI_NXM_OF_VLAN_TCI_W
:
1119 case NFI_NXM_OF_IP_SRC_W
:
1120 case NFI_NXM_OF_IP_DST_W
:
1121 case NFI_NXM_OF_ARP_SPA_W
:
1122 case NFI_NXM_OF_ARP_TPA_W
:
1131 nxm_execute_reg_move(const struct nx_action_reg_move
*action
,
1135 int n_bits
= ntohs(action
->n_bits
);
1136 uint64_t mask
= n_bits
== 64 ? UINT64_MAX
: (UINT64_C(1) << n_bits
) - 1;
1138 /* Get the interesting bits of the source field. */
1139 const struct nxm_field
*src
= nxm_field_lookup(ntohl(action
->src
));
1140 int src_ofs
= ntohs(action
->src_ofs
);
1141 uint64_t src_data
= nxm_read_field(src
, flow
) & (mask
<< src_ofs
);
1143 /* Get the remaining bits of the destination field. */
1144 const struct nxm_field
*dst
= nxm_field_lookup(ntohl(action
->dst
));
1145 int dst_ofs
= ntohs(action
->dst_ofs
);
1146 uint64_t dst_data
= nxm_read_field(dst
, flow
) & ~(mask
<< dst_ofs
);
1148 /* Get the final value. */
1149 uint64_t new_data
= dst_data
| ((src_data
>> src_ofs
) << dst_ofs
);
1151 /* Store the result. */
1152 if (NXM_IS_NX_REG(dst
->header
)) {
1153 flow
->regs
[NXM_NX_REG_IDX(dst
->header
)] = new_data
;
1154 } else if (dst
->header
== NXM_OF_VLAN_TCI
) {
1155 flow
->vlan_tci
= htons(new_data
);
1156 } else if (dst
->header
== NXM_NX_TUN_ID
) {
1157 flow
->tun_id
= htonl(new_data
);
1164 nxm_execute_reg_load(const struct nx_action_reg_load
*action
,
1168 int n_bits
= (ntohs(action
->ofs_nbits
) & 0x3f) + 1;
1169 uint32_t mask
= n_bits
== 32 ? UINT32_MAX
: (UINT32_C(1) << n_bits
) - 1;
1170 uint32_t *reg
= &flow
->regs
[NXM_NX_REG_IDX(ntohl(action
->dst
))];
1172 /* Get source data. */
1173 uint32_t src_data
= ntohll(action
->value
);
1175 /* Get remaining bits of the destination field. */
1176 int dst_ofs
= ntohs(action
->ofs_nbits
) >> 6;
1177 uint32_t dst_data
= *reg
& ~(mask
<< dst_ofs
);
1179 *reg
= dst_data
| (src_data
<< dst_ofs
);