]> git.proxmox.com Git - ovs.git/blame - lib/ofp-util.c
ofp-util: Drop unneeded null pointer tests.
[ovs.git] / lib / ofp-util.c
CommitLineData
fa37b408 1/*
e0edde6f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
fa37b408
BP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18#include "ofp-print.h"
dc4762ed 19#include <errno.h>
fa37b408 20#include <inttypes.h>
6ca00f6f
ETN
21#include <sys/types.h>
22#include <netinet/in.h>
b459a924 23#include <netinet/icmp6.h>
fa37b408 24#include <stdlib.h>
3b6a2571 25#include "autopath.h"
daff3353 26#include "bundle.h"
10a24935 27#include "byte-order.h"
d8ae4d67 28#include "classifier.h"
dc4762ed 29#include "dynamic-string.h"
75a75043 30#include "learn.h"
816fd533 31#include "meta-flow.h"
9e1fd49b 32#include "multipath.h"
6c038611 33#include "netdev.h"
b6c9e612 34#include "nx-match.h"
f25d0cf3 35#include "ofp-actions.h"
dc4762ed 36#include "ofp-errors.h"
982697a4 37#include "ofp-msgs.h"
fa37b408
BP
38#include "ofp-util.h"
39#include "ofpbuf.h"
40#include "packets.h"
41#include "random.h"
4ffd1b43 42#include "unaligned.h"
e41a9130 43#include "type-props.h"
5136ce49 44#include "vlog.h"
fa37b408 45
d98e6007 46VLOG_DEFINE_THIS_MODULE(ofp_util);
fa37b408
BP
47
48/* Rate limit for OpenFlow message parse errors. These always indicate a bug
49 * in the peer and so there's not much point in showing a lot of them. */
50static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
51
0596e897
BP
52/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
53 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
54 * is wildcarded.
55 *
56 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
57 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
58 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
59 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
60 * wildcarded. */
61ovs_be32
62ofputil_wcbits_to_netmask(int wcbits)
63{
64 wcbits &= 0x3f;
65 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
66}
67
68/* Given the IP netmask 'netmask', returns the number of bits of the IP address
c08201d6
BP
69 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
70 * between 0 and 32 inclusive.
71 *
72 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
73 * still be in the valid range but isn't otherwise meaningful. */
0596e897
BP
74int
75ofputil_netmask_to_wcbits(ovs_be32 netmask)
76{
aad29cd1 77 return 32 - ip_count_cidr_bits(netmask);
0596e897
BP
78}
79
eec25dc1 80/* A list of the FWW_* and OFPFW10_ bits that have the same value, meaning, and
d8ae4d67
BP
81 * name. */
82#define WC_INVARIANT_LIST \
83 WC_INVARIANT_BIT(IN_PORT) \
d8ae4d67 84 WC_INVARIANT_BIT(DL_TYPE) \
73f33563 85 WC_INVARIANT_BIT(NW_PROTO)
d8ae4d67
BP
86
87/* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
88 * actually have the same names and values. */
eec25dc1 89#define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW10_##NAME);
d8ae4d67
BP
90 WC_INVARIANT_LIST
91#undef WC_INVARIANT_BIT
92
93/* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
94 * OR'd together. */
eeba8e4f 95static const flow_wildcards_t WC_INVARIANTS = 0
d8ae4d67
BP
96#define WC_INVARIANT_BIT(NAME) | FWW_##NAME
97 WC_INVARIANT_LIST
98#undef WC_INVARIANT_BIT
eeba8e4f 99;
d8ae4d67 100
eec25dc1
BP
101/* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
102 * flow_wildcards in 'wc' for use in struct cls_rule. It is the caller's
103 * responsibility to handle the special case where the flow match's dl_vlan is
104 * set to OFP_VLAN_NONE. */
7286b1e1 105void
eec25dc1 106ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
d8ae4d67 107{
e878338b 108 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
a877206f 109
d8ae4d67 110 /* Initialize most of rule->wc. */
f9ba8dad 111 flow_wildcards_init_catchall(wc);
eeba8e4f 112 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
bad68a99 113
70d19663 114 /* Wildcard fields that aren't defined by ofp10_match. */
e878338b 115 wc->wildcards |= FWW_NW_ECN | FWW_NW_TTL;
bad68a99 116
eec25dc1 117 if (ofpfw & OFPFW10_NW_TOS) {
2486e66a
JP
118 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
119 * the enum than we can use. */
120 wc->wildcards |= FWW_NW_DSCP;
d8ae4d67 121 }
7257b535 122
eec25dc1
BP
123 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_SRC_SHIFT);
124 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_DST_SHIFT);
d8ae4d67 125
eec25dc1 126 if (!(ofpfw & OFPFW10_TP_SRC)) {
73f33563
BP
127 wc->tp_src_mask = htons(UINT16_MAX);
128 }
eec25dc1 129 if (!(ofpfw & OFPFW10_TP_DST)) {
73f33563
BP
130 wc->tp_dst_mask = htons(UINT16_MAX);
131 }
132
eec25dc1 133 if (!(ofpfw & OFPFW10_DL_SRC)) {
73c0ce34
JS
134 memset(wc->dl_src_mask, 0xff, ETH_ADDR_LEN);
135 }
eec25dc1 136 if (!(ofpfw & OFPFW10_DL_DST)) {
73c0ce34 137 memset(wc->dl_dst_mask, 0xff, ETH_ADDR_LEN);
d8ae4d67
BP
138 }
139
eb6f28db 140 /* VLAN TCI mask. */
eec25dc1 141 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
eb6f28db
BP
142 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
143 }
eec25dc1 144 if (!(ofpfw & OFPFW10_DL_VLAN)) {
eb6f28db
BP
145 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
146 }
147}
148
eec25dc1
BP
149/* Converts the ofp10_match in 'match' into a cls_rule in 'rule', with the
150 * given 'priority'. */
eb6f28db 151void
eec25dc1
BP
152ofputil_cls_rule_from_ofp10_match(const struct ofp10_match *match,
153 unsigned int priority, struct cls_rule *rule)
eb6f28db 154{
eec25dc1 155 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW10_ALL;
eb6f28db
BP
156
157 /* Initialize rule->priority, rule->wc. */
158 rule->priority = !ofpfw ? UINT16_MAX : priority;
eec25dc1 159 ofputil_wildcard_from_ofpfw10(ofpfw, &rule->wc);
eb6f28db 160
66642cb4 161 /* Initialize most of rule->flow. */
d8ae4d67
BP
162 rule->flow.nw_src = match->nw_src;
163 rule->flow.nw_dst = match->nw_dst;
abe529af 164 rule->flow.in_port = ntohs(match->in_port);
36956a7d 165 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
d8ae4d67
BP
166 rule->flow.tp_src = match->tp_src;
167 rule->flow.tp_dst = match->tp_dst;
168 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
169 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
eadef313 170 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
d8ae4d67
BP
171 rule->flow.nw_proto = match->nw_proto;
172
66642cb4 173 /* Translate VLANs. */
0c436519
SH
174 if (!(ofpfw & OFPFW10_DL_VLAN) &&
175 match->dl_vlan == htons(OFP10_VLAN_NONE)) {
47271d0d
BP
176 /* Match only packets without 802.1Q header.
177 *
eec25dc1 178 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
47271d0d 179 *
eec25dc1 180 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
47271d0d
BP
181 * because we can't have a specific PCP without an 802.1Q header.
182 * However, older versions of OVS treated this as matching packets
183 * withut an 802.1Q header, so we do here too. */
66642cb4 184 rule->flow.vlan_tci = htons(0);
eb6f28db 185 rule->wc.vlan_tci_mask = htons(0xffff);
47271d0d
BP
186 } else {
187 ovs_be16 vid, pcp, tci;
188
47271d0d
BP
189 vid = match->dl_vlan & htons(VLAN_VID_MASK);
190 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
191 tci = vid | pcp | htons(VLAN_CFI);
eb6f28db 192 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
66642cb4
BP
193 }
194
d8ae4d67
BP
195 /* Clean up. */
196 cls_rule_zero_wildcarded_fields(rule);
197}
198
eec25dc1 199/* Convert 'rule' into the OpenFlow 1.0 match structure 'match'. */
d8ae4d67 200void
eec25dc1
BP
201ofputil_cls_rule_to_ofp10_match(const struct cls_rule *rule,
202 struct ofp10_match *match)
d8ae4d67
BP
203{
204 const struct flow_wildcards *wc = &rule->wc;
eeba8e4f 205 uint32_t ofpfw;
d8ae4d67 206
66642cb4 207 /* Figure out most OpenFlow wildcards. */
eeba8e4f 208 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
eec25dc1
BP
209 ofpfw |= (ofputil_netmask_to_wcbits(wc->nw_src_mask)
210 << OFPFW10_NW_SRC_SHIFT);
211 ofpfw |= (ofputil_netmask_to_wcbits(wc->nw_dst_mask)
212 << OFPFW10_NW_DST_SHIFT);
2486e66a 213 if (wc->wildcards & FWW_NW_DSCP) {
eec25dc1 214 ofpfw |= OFPFW10_NW_TOS;
d8ae4d67 215 }
73f33563 216 if (!wc->tp_src_mask) {
eec25dc1 217 ofpfw |= OFPFW10_TP_SRC;
73f33563
BP
218 }
219 if (!wc->tp_dst_mask) {
eec25dc1 220 ofpfw |= OFPFW10_TP_DST;
73f33563 221 }
73c0ce34 222 if (eth_addr_is_zero(wc->dl_src_mask)) {
eec25dc1 223 ofpfw |= OFPFW10_DL_SRC;
73c0ce34
JS
224 }
225 if (eth_addr_is_zero(wc->dl_dst_mask)) {
eec25dc1 226 ofpfw |= OFPFW10_DL_DST;
73c0ce34 227 }
ff9d3826 228
66642cb4
BP
229 /* Translate VLANs. */
230 match->dl_vlan = htons(0);
231 match->dl_vlan_pcp = 0;
232 if (rule->wc.vlan_tci_mask == htons(0)) {
eec25dc1 233 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
66642cb4
BP
234 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
235 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
0c436519 236 match->dl_vlan = htons(OFP10_VLAN_NONE);
41ca9a1e 237 ofpfw |= OFPFW10_DL_VLAN_PCP;
66642cb4
BP
238 } else {
239 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
eec25dc1 240 ofpfw |= OFPFW10_DL_VLAN;
66642cb4
BP
241 } else {
242 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
243 }
244
245 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
eec25dc1 246 ofpfw |= OFPFW10_DL_VLAN_PCP;
66642cb4
BP
247 } else {
248 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
249 }
250 }
251
252 /* Compose most of the match structure. */
d8ae4d67 253 match->wildcards = htonl(ofpfw);
abe529af 254 match->in_port = htons(rule->flow.in_port);
d8ae4d67
BP
255 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
256 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
36956a7d 257 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
d8ae4d67
BP
258 match->nw_src = rule->flow.nw_src;
259 match->nw_dst = rule->flow.nw_dst;
eadef313 260 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
d8ae4d67
BP
261 match->nw_proto = rule->flow.nw_proto;
262 match->tp_src = rule->flow.tp_src;
263 match->tp_dst = rule->flow.tp_dst;
264 memset(match->pad1, '\0', sizeof match->pad1);
265 memset(match->pad2, '\0', sizeof match->pad2);
266}
267
aa319503
BP
268enum ofperr
269ofputil_pull_ofp11_match(struct ofpbuf *buf, unsigned int priority,
36a16881 270 struct cls_rule *rule, uint16_t *padded_match_len)
aa319503 271{
36a16881
SH
272 struct ofp11_match_header *omh = buf->data;
273 uint16_t match_len;
aa319503 274
36a16881 275 if (buf->size < sizeof *omh) {
aa319503
BP
276 return OFPERR_OFPBMC_BAD_LEN;
277 }
278
36a16881
SH
279 match_len = ntohs(omh->length);
280
aa319503 281 switch (ntohs(omh->type)) {
36a16881
SH
282 case OFPMT_STANDARD: {
283 struct ofp11_match *om;
284
285 if (match_len != sizeof *om || buf->size < sizeof *om) {
aa319503
BP
286 return OFPERR_OFPBMC_BAD_LEN;
287 }
288 om = ofpbuf_pull(buf, sizeof *om);
36a16881
SH
289 if (padded_match_len) {
290 *padded_match_len = match_len;
291 }
aa319503 292 return ofputil_cls_rule_from_ofp11_match(om, priority, rule);
36a16881
SH
293 }
294
295 case OFPMT_OXM:
296 if (padded_match_len) {
297 *padded_match_len = ROUND_UP(match_len, 8);
298 }
299 return oxm_pull_match(buf, priority, rule);
aa319503
BP
300
301 default:
302 return OFPERR_OFPBMC_BAD_TYPE;
303 }
304}
305
410698cf
BP
306/* Converts the ofp11_match in 'match' into a cls_rule in 'rule', with the
307 * given 'priority'. Returns 0 if successful, otherwise an OFPERR_* value. */
308enum ofperr
309ofputil_cls_rule_from_ofp11_match(const struct ofp11_match *match,
310 unsigned int priority,
311 struct cls_rule *rule)
312{
313 uint16_t wc = ntohl(match->wildcards);
314 uint8_t dl_src_mask[ETH_ADDR_LEN];
315 uint8_t dl_dst_mask[ETH_ADDR_LEN];
316 bool ipv4, arp;
317 int i;
318
319 cls_rule_init_catchall(rule, priority);
320
321 if (!(wc & OFPFW11_IN_PORT)) {
322 uint16_t ofp_port;
323 enum ofperr error;
324
325 error = ofputil_port_from_ofp11(match->in_port, &ofp_port);
326 if (error) {
327 return OFPERR_OFPBMC_BAD_VALUE;
328 }
329 cls_rule_set_in_port(rule, ofp_port);
330 }
331
332 for (i = 0; i < ETH_ADDR_LEN; i++) {
333 dl_src_mask[i] = ~match->dl_src_mask[i];
334 }
335 cls_rule_set_dl_src_masked(rule, match->dl_src, dl_src_mask);
336
337 for (i = 0; i < ETH_ADDR_LEN; i++) {
338 dl_dst_mask[i] = ~match->dl_dst_mask[i];
339 }
340 cls_rule_set_dl_dst_masked(rule, match->dl_dst, dl_dst_mask);
341
342 if (!(wc & OFPFW11_DL_VLAN)) {
343 if (match->dl_vlan == htons(OFPVID11_NONE)) {
344 /* Match only packets without a VLAN tag. */
345 rule->flow.vlan_tci = htons(0);
346 rule->wc.vlan_tci_mask = htons(UINT16_MAX);
347 } else {
348 if (match->dl_vlan == htons(OFPVID11_ANY)) {
349 /* Match any packet with a VLAN tag regardless of VID. */
350 rule->flow.vlan_tci = htons(VLAN_CFI);
351 rule->wc.vlan_tci_mask = htons(VLAN_CFI);
352 } else if (ntohs(match->dl_vlan) < 4096) {
353 /* Match only packets with the specified VLAN VID. */
354 rule->flow.vlan_tci = htons(VLAN_CFI) | match->dl_vlan;
355 rule->wc.vlan_tci_mask = htons(VLAN_CFI | VLAN_VID_MASK);
356 } else {
357 /* Invalid VID. */
358 return OFPERR_OFPBMC_BAD_VALUE;
359 }
360
361 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
362 if (match->dl_vlan_pcp <= 7) {
363 rule->flow.vlan_tci |= htons(match->dl_vlan_pcp
364 << VLAN_PCP_SHIFT);
365 rule->wc.vlan_tci_mask |= htons(VLAN_PCP_MASK);
366 } else {
367 /* Invalid PCP. */
368 return OFPERR_OFPBMC_BAD_VALUE;
369 }
370 }
371 }
372 }
373
374 if (!(wc & OFPFW11_DL_TYPE)) {
375 cls_rule_set_dl_type(rule,
376 ofputil_dl_type_from_openflow(match->dl_type));
377 }
378
379 ipv4 = rule->flow.dl_type == htons(ETH_TYPE_IP);
380 arp = rule->flow.dl_type == htons(ETH_TYPE_ARP);
381
382 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
383 if (match->nw_tos & ~IP_DSCP_MASK) {
384 /* Invalid TOS. */
385 return OFPERR_OFPBMC_BAD_VALUE;
386 }
387
388 cls_rule_set_nw_dscp(rule, match->nw_tos);
389 }
390
391 if (ipv4 || arp) {
392 if (!(wc & OFPFW11_NW_PROTO)) {
393 cls_rule_set_nw_proto(rule, match->nw_proto);
394 }
410698cf
BP
395 cls_rule_set_nw_src_masked(rule, match->nw_src, ~match->nw_src_mask);
396 cls_rule_set_nw_dst_masked(rule, match->nw_dst, ~match->nw_dst_mask);
397 }
398
399#define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
400 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
401 switch (rule->flow.nw_proto) {
402 case IPPROTO_ICMP:
403 /* "A.2.3 Flow Match Structures" in OF1.1 says:
404 *
405 * The tp_src and tp_dst fields will be ignored unless the
406 * network protocol specified is as TCP, UDP or SCTP.
407 *
408 * but I'm pretty sure we should support ICMP too, otherwise
409 * that's a regression from OF1.0. */
410 if (!(wc & OFPFW11_TP_SRC)) {
411 uint16_t icmp_type = ntohs(match->tp_src);
412 if (icmp_type < 0x100) {
413 cls_rule_set_icmp_type(rule, icmp_type);
414 } else {
415 return OFPERR_OFPBMC_BAD_FIELD;
416 }
417 }
418 if (!(wc & OFPFW11_TP_DST)) {
419 uint16_t icmp_code = ntohs(match->tp_dst);
420 if (icmp_code < 0x100) {
421 cls_rule_set_icmp_code(rule, icmp_code);
422 } else {
423 return OFPERR_OFPBMC_BAD_FIELD;
424 }
425 }
426 break;
427
428 case IPPROTO_TCP:
429 case IPPROTO_UDP:
430 if (!(wc & (OFPFW11_TP_SRC))) {
431 cls_rule_set_tp_src(rule, match->tp_src);
432 }
433 if (!(wc & (OFPFW11_TP_DST))) {
434 cls_rule_set_tp_dst(rule, match->tp_dst);
435 }
436 break;
437
438 case IPPROTO_SCTP:
439 /* We don't support SCTP and it seems that we should tell the
440 * controller, since OF1.1 implementations are supposed to. */
441 return OFPERR_OFPBMC_BAD_FIELD;
442
443 default:
444 /* OF1.1 says explicitly to ignore this. */
445 break;
446 }
447 }
448
449 if (rule->flow.dl_type == htons(ETH_TYPE_MPLS) ||
450 rule->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
451 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
452
453 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
454 /* MPLS not supported. */
455 return OFPERR_OFPBMC_BAD_TAG;
456 }
457 }
458
459 if (match->metadata_mask != htonll(UINT64_MAX)) {
969fc56c
JS
460 cls_rule_set_metadata_masked(rule, match->metadata,
461 ~match->metadata_mask);
410698cf
BP
462 }
463
464 return 0;
465}
466
467/* Convert 'rule' into the OpenFlow 1.1 match structure 'match'. */
468void
469ofputil_cls_rule_to_ofp11_match(const struct cls_rule *rule,
470 struct ofp11_match *match)
471{
472 uint32_t wc = 0;
473 int i;
474
475 memset(match, 0, sizeof *match);
476 match->omh.type = htons(OFPMT_STANDARD);
477 match->omh.length = htons(OFPMT11_STANDARD_LENGTH);
478
479 if (rule->wc.wildcards & FWW_IN_PORT) {
480 wc |= OFPFW11_IN_PORT;
481 } else {
482 match->in_port = ofputil_port_to_ofp11(rule->flow.in_port);
483 }
484
410698cf
BP
485 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
486 for (i = 0; i < ETH_ADDR_LEN; i++) {
487 match->dl_src_mask[i] = ~rule->wc.dl_src_mask[i];
488 }
489
490 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
491 for (i = 0; i < ETH_ADDR_LEN; i++) {
492 match->dl_dst_mask[i] = ~rule->wc.dl_dst_mask[i];
493 }
494
495 if (rule->wc.vlan_tci_mask == htons(0)) {
496 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
497 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
498 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
499 match->dl_vlan = htons(OFPVID11_NONE);
500 wc |= OFPFW11_DL_VLAN_PCP;
501 } else {
502 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
503 match->dl_vlan = htons(OFPVID11_ANY);
504 } else {
505 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
506 }
507
508 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
509 wc |= OFPFW11_DL_VLAN_PCP;
510 } else {
511 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
512 }
513 }
514
515 if (rule->wc.wildcards & FWW_DL_TYPE) {
516 wc |= OFPFW11_DL_TYPE;
517 } else {
518 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
519 }
520
521 if (rule->wc.wildcards & FWW_NW_DSCP) {
522 wc |= OFPFW11_NW_TOS;
523 } else {
524 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
525 }
526
527 if (rule->wc.wildcards & FWW_NW_PROTO) {
528 wc |= OFPFW11_NW_PROTO;
529 } else {
530 match->nw_proto = rule->flow.nw_proto;
531 }
532
533 match->nw_src = rule->flow.nw_src;
534 match->nw_src_mask = ~rule->wc.nw_src_mask;
535 match->nw_dst = rule->flow.nw_dst;
536 match->nw_dst_mask = ~rule->wc.nw_dst_mask;
537
538 if (!rule->wc.tp_src_mask) {
539 wc |= OFPFW11_TP_SRC;
540 } else {
541 match->tp_src = rule->flow.tp_src;
542 }
543
544 if (!rule->wc.tp_dst_mask) {
545 wc |= OFPFW11_TP_DST;
546 } else {
547 match->tp_dst = rule->flow.tp_dst;
548 }
549
550 /* MPLS not supported. */
551 wc |= OFPFW11_MPLS_LABEL;
552 wc |= OFPFW11_MPLS_TC;
553
969fc56c
JS
554 match->metadata = rule->flow.metadata;
555 match->metadata_mask = ~rule->wc.metadata_mask;
410698cf
BP
556
557 match->wildcards = htonl(wc);
558}
559
36956a7d 560/* Given a 'dl_type' value in the format used in struct flow, returns the
eec25dc1
BP
561 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
562 * structure. */
36956a7d
BP
563ovs_be16
564ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
565{
566 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
567 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
568 : flow_dl_type);
569}
570
eec25dc1 571/* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
36956a7d
BP
572 * structure, returns the corresponding 'dl_type' value for use in struct
573 * flow. */
574ovs_be16
575ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
576{
577 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
578 ? htons(FLOW_DL_TYPE_NONE)
579 : ofp_dl_type);
580}
2e4f5fcf 581\f
27527aa0 582/* Protocols. */
7fa91113 583
27527aa0
BP
584struct proto_abbrev {
585 enum ofputil_protocol protocol;
586 const char *name;
587};
588
589/* Most users really don't care about some of the differences between
590 * protocols. These abbreviations help with that. */
591static const struct proto_abbrev proto_abbrevs[] = {
592 { OFPUTIL_P_ANY, "any" },
593 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
594 { OFPUTIL_P_NXM_ANY, "NXM" },
595};
596#define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
597
598enum ofputil_protocol ofputil_flow_dump_protocols[] = {
599 OFPUTIL_P_NXM,
600 OFPUTIL_P_OF10,
601};
602size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
603
604/* Returns the ofputil_protocol that is initially in effect on an OpenFlow
605 * connection that has negotiated the given 'version'. 'version' should
606 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
607 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
608 * outside the valid range. */
609enum ofputil_protocol
2e3fa633 610ofputil_protocol_from_ofp_version(enum ofp_version version)
27527aa0
BP
611{
612 switch (version) {
2e3fa633
SH
613 case OFP10_VERSION:
614 return OFPUTIL_P_OF10;
615 case OFP12_VERSION:
616 return OFPUTIL_P_OF12;
617 case OFP11_VERSION:
618 default:
619 return 0;
27527aa0
BP
620 }
621}
622
44d3732d
SH
623/* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
624 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
2e3fa633 625enum ofp_version
9e1fd49b
BP
626ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
627{
628 switch (protocol) {
629 case OFPUTIL_P_OF10:
630 case OFPUTIL_P_OF10_TID:
631 case OFPUTIL_P_NXM:
632 case OFPUTIL_P_NXM_TID:
633 return OFP10_VERSION;
44d3732d
SH
634 case OFPUTIL_P_OF12:
635 return OFP12_VERSION;
9e1fd49b
BP
636 }
637
638 NOT_REACHED();
639}
640
27527aa0
BP
641/* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
642 * otherwise. */
7fa91113 643bool
27527aa0 644ofputil_protocol_is_valid(enum ofputil_protocol protocol)
7fa91113 645{
27527aa0
BP
646 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
647}
648
649/* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
650 * extension turned on or off if 'enable' is true or false, respectively.
651 *
652 * This extension is only useful for protocols whose "standard" version does
653 * not allow specific tables to be modified. In particular, this is true of
654 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
655 * specifies a table ID and so there is no need for such an extension. When
656 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
657 * extension, this function just returns its 'protocol' argument unchanged
658 * regardless of the value of 'enable'. */
659enum ofputil_protocol
660ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
661{
662 switch (protocol) {
663 case OFPUTIL_P_OF10:
664 case OFPUTIL_P_OF10_TID:
665 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
666
667 case OFPUTIL_P_NXM:
668 case OFPUTIL_P_NXM_TID:
669 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
670
44d3732d
SH
671 case OFPUTIL_P_OF12:
672 return OFPUTIL_P_OF12;
673
27527aa0
BP
674 default:
675 NOT_REACHED();
7fa91113 676 }
27527aa0 677}
7fa91113 678
27527aa0
BP
679/* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
680 * some extension to a standard protocol version, the return value is the
681 * standard version of that protocol without any extension. If 'protocol' is a
682 * standard protocol version, returns 'protocol' unchanged. */
683enum ofputil_protocol
684ofputil_protocol_to_base(enum ofputil_protocol protocol)
685{
686 return ofputil_protocol_set_tid(protocol, false);
7fa91113
BP
687}
688
27527aa0
BP
689/* Returns 'new_base' with any extensions taken from 'cur'. */
690enum ofputil_protocol
691ofputil_protocol_set_base(enum ofputil_protocol cur,
692 enum ofputil_protocol new_base)
7fa91113 693{
27527aa0
BP
694 bool tid = (cur & OFPUTIL_P_TID) != 0;
695
696 switch (new_base) {
697 case OFPUTIL_P_OF10:
698 case OFPUTIL_P_OF10_TID:
699 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
700
701 case OFPUTIL_P_NXM:
702 case OFPUTIL_P_NXM_TID:
703 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
704
44d3732d
SH
705 case OFPUTIL_P_OF12:
706 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
707
7fa91113
BP
708 default:
709 NOT_REACHED();
710 }
711}
712
27527aa0
BP
713/* Returns a string form of 'protocol', if a simple form exists (that is, if
714 * 'protocol' is either a single protocol or it is a combination of protocols
715 * that have a single abbreviation). Otherwise, returns NULL. */
716const char *
717ofputil_protocol_to_string(enum ofputil_protocol protocol)
88ca35ee 718{
27527aa0
BP
719 const struct proto_abbrev *p;
720
721 /* Use a "switch" statement for single-bit names so that we get a compiler
722 * warning if we forget any. */
723 switch (protocol) {
724 case OFPUTIL_P_NXM:
725 return "NXM-table_id";
726
727 case OFPUTIL_P_NXM_TID:
728 return "NXM+table_id";
729
730 case OFPUTIL_P_OF10:
731 return "OpenFlow10-table_id";
732
733 case OFPUTIL_P_OF10_TID:
734 return "OpenFlow10+table_id";
44d3732d
SH
735
736 case OFPUTIL_P_OF12:
737 return NULL;
27527aa0
BP
738 }
739
740 /* Check abbreviations. */
741 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
742 if (protocol == p->protocol) {
743 return p->name;
744 }
745 }
746
747 return NULL;
748}
749
750/* Returns a string that represents 'protocols'. The return value might be a
751 * comma-separated list if 'protocols' doesn't have a simple name. The return
752 * value is "none" if 'protocols' is 0.
753 *
754 * The caller must free the returned string (with free()). */
755char *
756ofputil_protocols_to_string(enum ofputil_protocol protocols)
757{
758 struct ds s;
759
760 assert(!(protocols & ~OFPUTIL_P_ANY));
761 if (protocols == 0) {
762 return xstrdup("none");
763 }
764
765 ds_init(&s);
766 while (protocols) {
767 const struct proto_abbrev *p;
768 int i;
769
770 if (s.length) {
771 ds_put_char(&s, ',');
772 }
773
774 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
775 if ((protocols & p->protocol) == p->protocol) {
776 ds_put_cstr(&s, p->name);
777 protocols &= ~p->protocol;
778 goto match;
779 }
780 }
781
782 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
783 enum ofputil_protocol bit = 1u << i;
784
785 if (protocols & bit) {
786 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
787 protocols &= ~bit;
788 goto match;
789 }
790 }
791 NOT_REACHED();
792
793 match: ;
794 }
795 return ds_steal_cstr(&s);
796}
797
798static enum ofputil_protocol
799ofputil_protocol_from_string__(const char *s, size_t n)
800{
801 const struct proto_abbrev *p;
802 int i;
803
804 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
805 enum ofputil_protocol bit = 1u << i;
806 const char *name = ofputil_protocol_to_string(bit);
807
808 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
809 return bit;
810 }
811 }
812
813 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
814 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
815 return p->protocol;
816 }
817 }
818
819 return 0;
820}
821
822/* Returns the nonempty set of protocols represented by 's', which can be a
823 * single protocol name or abbreviation or a comma-separated list of them.
824 *
825 * Aborts the program with an error message if 's' is invalid. */
826enum ofputil_protocol
827ofputil_protocols_from_string(const char *s)
828{
829 const char *orig_s = s;
830 enum ofputil_protocol protocols;
831
832 protocols = 0;
833 while (*s) {
834 enum ofputil_protocol p;
835 size_t n;
836
837 n = strcspn(s, ",");
838 if (n == 0) {
839 s++;
840 continue;
841 }
842
843 p = ofputil_protocol_from_string__(s, n);
844 if (!p) {
845 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
846 }
847 protocols |= p;
848
849 s += n;
850 }
851
852 if (!protocols) {
853 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
854 }
855 return protocols;
88ca35ee
BP
856}
857
54834960
EJ
858bool
859ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
860{
861 switch (packet_in_format) {
862 case NXPIF_OPENFLOW10:
863 case NXPIF_NXM:
864 return true;
865 }
866
867 return false;
868}
869
870const char *
871ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
872{
873 switch (packet_in_format) {
874 case NXPIF_OPENFLOW10:
875 return "openflow10";
876 case NXPIF_NXM:
877 return "nxm";
878 default:
879 NOT_REACHED();
880 }
881}
882
883int
884ofputil_packet_in_format_from_string(const char *s)
885{
886 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
887 : !strcmp(s, "nxm") ? NXPIF_NXM
888 : -1);
889}
890
88ca35ee
BP
891static bool
892regs_fully_wildcarded(const struct flow_wildcards *wc)
893{
894 int i;
895
896 for (i = 0; i < FLOW_N_REGS; i++) {
897 if (wc->reg_masks[i] != 0) {
898 return false;
899 }
900 }
901 return true;
902}
903
27527aa0
BP
904/* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
905 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
906 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
907 * use OpenFlow 1.0 protocol for backward compatibility. */
908enum ofputil_protocol
909ofputil_usable_protocols(const struct cls_rule *rule)
8368c090
BP
910{
911 const struct flow_wildcards *wc = &rule->wc;
8368c090 912
e878338b 913 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
a877206f 914
73c0ce34
JS
915 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
916 if (!eth_mask_is_exact(wc->dl_src_mask)
917 && !eth_addr_is_zero(wc->dl_src_mask)) {
918 return OFPUTIL_P_NXM_ANY;
919 }
920 if (!eth_mask_is_exact(wc->dl_dst_mask)
921 && !eth_addr_is_zero(wc->dl_dst_mask)) {
27527aa0 922 return OFPUTIL_P_NXM_ANY;
8368c090
BP
923 }
924
969fc56c
JS
925 /* NXM and OF1.1+ support matching metadata. */
926 if (wc->metadata_mask != htonll(0)) {
927 return OFPUTIL_P_NXM_ANY;
928 }
929
bad68a99 930 /* Only NXM supports matching ARP hardware addresses. */
e878338b
SH
931 if (!eth_addr_is_zero(wc->arp_sha_mask) ||
932 !eth_addr_is_zero(wc->arp_tha_mask)) {
27527aa0 933 return OFPUTIL_P_NXM_ANY;
bad68a99
JP
934 }
935
d31f1109
JP
936 /* Only NXM supports matching IPv6 traffic. */
937 if (!(wc->wildcards & FWW_DL_TYPE)
938 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
27527aa0 939 return OFPUTIL_P_NXM_ANY;
d31f1109
JP
940 }
941
8368c090
BP
942 /* Only NXM supports matching registers. */
943 if (!regs_fully_wildcarded(wc)) {
27527aa0 944 return OFPUTIL_P_NXM_ANY;
8368c090
BP
945 }
946
b78f6b77
BP
947 /* Only NXM supports matching tun_id. */
948 if (wc->tun_id_mask != htonll(0)) {
27527aa0 949 return OFPUTIL_P_NXM_ANY;
8368c090
BP
950 }
951
7257b535 952 /* Only NXM supports matching fragments. */
eadef313 953 if (wc->nw_frag_mask) {
27527aa0 954 return OFPUTIL_P_NXM_ANY;
7257b535
BP
955 }
956
fa8223b7 957 /* Only NXM supports matching IPv6 flow label. */
32455024 958 if (wc->ipv6_label_mask) {
27527aa0 959 return OFPUTIL_P_NXM_ANY;
fa8223b7
JP
960 }
961
530180fd 962 /* Only NXM supports matching IP ECN bits. */
2486e66a 963 if (!(wc->wildcards & FWW_NW_ECN)) {
27527aa0 964 return OFPUTIL_P_NXM_ANY;
530180fd
JP
965 }
966
a61680c6
JP
967 /* Only NXM supports matching IP TTL/hop limit. */
968 if (!(wc->wildcards & FWW_NW_TTL)) {
27527aa0 969 return OFPUTIL_P_NXM_ANY;
a61680c6
JP
970 }
971
c08201d6
BP
972 /* Only NXM supports non-CIDR IPv4 address masks. */
973 if (!ip_is_cidr(wc->nw_src_mask) || !ip_is_cidr(wc->nw_dst_mask)) {
974 return OFPUTIL_P_NXM_ANY;
975 }
976
73f33563
BP
977 /* Only NXM supports bitwise matching on transport port. */
978 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
979 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
27527aa0 980 return OFPUTIL_P_NXM_ANY;
73f33563
BP
981 }
982
8368c090 983 /* Other formats can express this rule. */
27527aa0
BP
984 return OFPUTIL_P_ANY;
985}
986
987/* Returns an OpenFlow message that, sent on an OpenFlow connection whose
988 * protocol is 'current', at least partly transitions the protocol to 'want'.
989 * Stores in '*next' the protocol that will be in effect on the OpenFlow
990 * connection if the switch processes the returned message correctly. (If
991 * '*next != want' then the caller will have to iterate.)
992 *
993 * If 'current == want', returns NULL and stores 'current' in '*next'. */
994struct ofpbuf *
995ofputil_encode_set_protocol(enum ofputil_protocol current,
996 enum ofputil_protocol want,
997 enum ofputil_protocol *next)
998{
999 enum ofputil_protocol cur_base, want_base;
1000 bool cur_tid, want_tid;
1001
1002 cur_base = ofputil_protocol_to_base(current);
1003 want_base = ofputil_protocol_to_base(want);
1004 if (cur_base != want_base) {
1005 *next = ofputil_protocol_set_base(current, want_base);
1006
1007 switch (want_base) {
1008 case OFPUTIL_P_NXM:
1009 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1010
1011 case OFPUTIL_P_OF10:
1012 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1013
44d3732d
SH
1014 case OFPUTIL_P_OF12:
1015 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1016
27527aa0
BP
1017 case OFPUTIL_P_OF10_TID:
1018 case OFPUTIL_P_NXM_TID:
1019 NOT_REACHED();
1020 }
1021 }
1022
1023 cur_tid = (current & OFPUTIL_P_TID) != 0;
1024 want_tid = (want & OFPUTIL_P_TID) != 0;
1025 if (cur_tid != want_tid) {
1026 *next = ofputil_protocol_set_tid(current, want_tid);
1027 return ofputil_make_flow_mod_table_id(want_tid);
1028 }
1029
1030 assert(current == want);
1031
1032 *next = current;
1033 return NULL;
88ca35ee
BP
1034}
1035
27527aa0
BP
1036/* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1037 * format to 'nxff'. */
88ca35ee 1038struct ofpbuf *
27527aa0 1039ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
88ca35ee 1040{
73dbf4ab 1041 struct nx_set_flow_format *sff;
88ca35ee
BP
1042 struct ofpbuf *msg;
1043
27527aa0
BP
1044 assert(ofputil_nx_flow_format_is_valid(nxff));
1045
982697a4
BP
1046 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1047 sff = ofpbuf_put_zeros(msg, sizeof *sff);
27527aa0 1048 sff->format = htonl(nxff);
88ca35ee
BP
1049
1050 return msg;
1051}
1052
27527aa0
BP
1053/* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1054 * otherwise. */
1055enum ofputil_protocol
1056ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1057{
1058 switch (flow_format) {
1059 case NXFF_OPENFLOW10:
1060 return OFPUTIL_P_OF10;
1061
1062 case NXFF_NXM:
1063 return OFPUTIL_P_NXM;
1064
44d3732d
SH
1065 case NXFF_OPENFLOW12:
1066 return OFPUTIL_P_OF12;
1067
27527aa0
BP
1068 default:
1069 return 0;
1070 }
1071}
1072
1073/* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1074bool
1075ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1076{
1077 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1078}
1079
1080/* Returns a string version of 'flow_format', which must be a valid NXFF_*
1081 * value. */
1082const char *
1083ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1084{
1085 switch (flow_format) {
1086 case NXFF_OPENFLOW10:
1087 return "openflow10";
1088 case NXFF_NXM:
1089 return "nxm";
44d3732d
SH
1090 case NXFF_OPENFLOW12:
1091 return "openflow12";
27527aa0
BP
1092 default:
1093 NOT_REACHED();
1094 }
1095}
1096
54834960
EJ
1097struct ofpbuf *
1098ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1099{
73dbf4ab 1100 struct nx_set_packet_in_format *spif;
54834960
EJ
1101 struct ofpbuf *msg;
1102
982697a4
BP
1103 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION, 0);
1104 spif = ofpbuf_put_zeros(msg, sizeof *spif);
54834960
EJ
1105 spif->format = htonl(packet_in_format);
1106
1107 return msg;
1108}
1109
6c1491fb
BP
1110/* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1111 * extension on or off (according to 'flow_mod_table_id'). */
1112struct ofpbuf *
1113ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1114{
73dbf4ab 1115 struct nx_flow_mod_table_id *nfmti;
6c1491fb
BP
1116 struct ofpbuf *msg;
1117
982697a4
BP
1118 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1119 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
6c1491fb
BP
1120 nfmti->set = flow_mod_table_id;
1121 return msg;
1122}
1123
7fa91113
BP
1124/* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1125 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1126 * code.
1127 *
f25d0cf3
BP
1128 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1129 * The caller must initialize 'ofpacts' and retains ownership of it.
1130 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1131 *
1132 * Does not validate the flow_mod actions. The caller should do that, with
1133 * ofpacts_check(). */
90bf1e07 1134enum ofperr
a9a2da38 1135ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
27527aa0 1136 const struct ofp_header *oh,
f25d0cf3
BP
1137 enum ofputil_protocol protocol,
1138 struct ofpbuf *ofpacts)
2e4f5fcf 1139{
6c1491fb 1140 uint16_t command;
2e4f5fcf 1141 struct ofpbuf b;
982697a4 1142 enum ofpraw raw;
2e4f5fcf 1143
2013493b 1144 ofpbuf_use_const(&b, oh, ntohs(oh->length));
982697a4 1145 raw = ofpraw_pull_assert(&b);
aa319503 1146 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
982697a4 1147 /* Standard OpenFlow 1.1 flow_mod. */
aa319503 1148 const struct ofp11_flow_mod *ofm;
90bf1e07 1149 enum ofperr error;
2e4f5fcf 1150
bbc32a88 1151 ofm = ofpbuf_pull(&b, sizeof *ofm);
2e4f5fcf 1152
36a16881
SH
1153 error = ofputil_pull_ofp11_match(&b, ntohs(ofm->priority), &fm->cr,
1154 NULL);
aa319503
BP
1155 if (error) {
1156 return error;
1c0b7503
BP
1157 }
1158
aa319503 1159 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
f25d0cf3
BP
1160 if (error) {
1161 return error;
1162 }
1163
2e4f5fcf 1164 /* Translate the message. */
aa319503
BP
1165 if (ofm->command == OFPFC_ADD) {
1166 fm->cookie = htonll(0);
1167 fm->cookie_mask = htonll(0);
1168 fm->new_cookie = ofm->cookie;
1169 } else {
1170 /* XXX */
1171 fm->cookie = ofm->cookie;
1172 fm->cookie_mask = ofm->cookie_mask;
1173 fm->new_cookie = htonll(UINT64_MAX);
1174 }
1175 fm->command = ofm->command;
1176 fm->table_id = ofm->table_id;
2e4f5fcf
BP
1177 fm->idle_timeout = ntohs(ofm->idle_timeout);
1178 fm->hard_timeout = ntohs(ofm->hard_timeout);
1179 fm->buffer_id = ntohl(ofm->buffer_id);
aa319503 1180 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
2e4f5fcf
BP
1181 if (error) {
1182 return error;
1183 }
aa319503
BP
1184 if (ofm->out_group != htonl(OFPG_ANY)) {
1185 return OFPERR_NXFMFC_GROUPS_NOT_SUPPORTED;
2e4f5fcf 1186 }
aa319503
BP
1187 fm->flags = ntohs(ofm->flags);
1188 } else {
1189 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1190 /* Standard OpenFlow 1.0 flow_mod. */
1191 const struct ofp10_flow_mod *ofm;
1192 uint16_t priority;
1193 enum ofperr error;
1194
1195 /* Get the ofp10_flow_mod. */
1196 ofm = ofpbuf_pull(&b, sizeof *ofm);
1197
1198 /* Set priority based on original wildcards. Normally we'd allow
1199 * ofputil_cls_rule_from_match() to do this for us, but
1200 * ofputil_normalize_rule() can put wildcards where the original
1201 * flow didn't have them. */
1202 priority = ntohs(ofm->priority);
1203 if (!(ofm->match.wildcards & htonl(OFPFW10_ALL))) {
1204 priority = UINT16_MAX;
1205 }
2e4f5fcf 1206
aa319503
BP
1207 /* Translate the rule. */
1208 ofputil_cls_rule_from_ofp10_match(&ofm->match, priority, &fm->cr);
1209 ofputil_normalize_rule(&fm->cr);
1210
1211 /* Now get the actions. */
1212 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1213 if (error) {
1214 return error;
1215 }
1216
1217 /* Translate the message. */
1218 command = ntohs(ofm->command);
1219 fm->cookie = htonll(0);
1220 fm->cookie_mask = htonll(0);
1221 fm->new_cookie = ofm->cookie;
1222 fm->idle_timeout = ntohs(ofm->idle_timeout);
1223 fm->hard_timeout = ntohs(ofm->hard_timeout);
1224 fm->buffer_id = ntohl(ofm->buffer_id);
1225 fm->out_port = ntohs(ofm->out_port);
1226 fm->flags = ntohs(ofm->flags);
1227 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1228 /* Nicira extended flow_mod. */
1229 const struct nx_flow_mod *nfm;
1230 enum ofperr error;
1231
1232 /* Dissect the message. */
1233 nfm = ofpbuf_pull(&b, sizeof *nfm);
1234 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1235 &fm->cr, &fm->cookie, &fm->cookie_mask);
1236 if (error) {
1237 return error;
1238 }
1239 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1240 if (error) {
1241 return error;
1242 }
1243
1244 /* Translate the message. */
1245 command = ntohs(nfm->command);
1246 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1247 /* Flow additions may only set a new cookie, not match an
1248 * existing cookie. */
1249 return OFPERR_NXBRC_NXM_INVALID;
1250 }
1251 fm->new_cookie = nfm->cookie;
1252 fm->idle_timeout = ntohs(nfm->idle_timeout);
1253 fm->hard_timeout = ntohs(nfm->hard_timeout);
1254 fm->buffer_id = ntohl(nfm->buffer_id);
1255 fm->out_port = ntohs(nfm->out_port);
1256 fm->flags = ntohs(nfm->flags);
1257 } else {
1258 NOT_REACHED();
1259 }
1260
1261 if (protocol & OFPUTIL_P_TID) {
1262 fm->command = command & 0xff;
1263 fm->table_id = command >> 8;
1264 } else {
1265 fm->command = command;
1266 fm->table_id = 0xff;
e729e793 1267 }
2e4f5fcf
BP
1268 }
1269
f25d0cf3
BP
1270 fm->ofpacts = ofpacts->data;
1271 fm->ofpacts_len = ofpacts->size;
6c1491fb 1272
2e4f5fcf
BP
1273 return 0;
1274}
1275
aa6305ea
SH
1276static ovs_be16
1277ofputil_tid_command(const struct ofputil_flow_mod *fm,
1278 enum ofputil_protocol protocol)
1279{
1280 return htons(protocol & OFPUTIL_P_TID
1281 ? (fm->command & 0xff) | (fm->table_id << 8)
1282 : fm->command);
1283}
1284
2e4f5fcf 1285/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
6cbe2c0f 1286 * 'protocol' and returns the message. */
2e4f5fcf 1287struct ofpbuf *
a9a2da38 1288ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
27527aa0 1289 enum ofputil_protocol protocol)
2e4f5fcf 1290{
2e4f5fcf
BP
1291 struct ofpbuf *msg;
1292
27527aa0 1293 switch (protocol) {
aa6305ea
SH
1294 case OFPUTIL_P_OF12: {
1295 struct ofp11_flow_mod *ofm;
1296
1297 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION,
1298 NXM_TYPICAL_LEN + fm->ofpacts_len);
1299 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1300 ofm->cookie = fm->new_cookie;
1301 ofm->cookie_mask = fm->cookie_mask;
1302 ofm->table_id = fm->table_id;
1303 ofm->command = fm->command;
1304 ofm->idle_timeout = htons(fm->idle_timeout);
1305 ofm->hard_timeout = htons(fm->hard_timeout);
1306 ofm->priority = htons(fm->cr.priority);
1307 ofm->buffer_id = htonl(fm->buffer_id);
1308 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1309 ofm->out_group = htonl(OFPG11_ANY);
1310 ofm->flags = htons(fm->flags);
1311 oxm_put_match(msg, &fm->cr);
5b289eaf 1312 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
aa6305ea
SH
1313 break;
1314 }
1315
27527aa0 1316 case OFPUTIL_P_OF10:
3f192f23
SH
1317 case OFPUTIL_P_OF10_TID: {
1318 struct ofp10_flow_mod *ofm;
1319
982697a4
BP
1320 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1321 fm->ofpacts_len);
1322 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
eec25dc1 1323 ofputil_cls_rule_to_ofp10_match(&fm->cr, &ofm->match);
623e1caf 1324 ofm->cookie = fm->new_cookie;
aa6305ea 1325 ofm->command = ofputil_tid_command(fm, protocol);
2e4f5fcf
BP
1326 ofm->idle_timeout = htons(fm->idle_timeout);
1327 ofm->hard_timeout = htons(fm->hard_timeout);
1328 ofm->priority = htons(fm->cr.priority);
1329 ofm->buffer_id = htonl(fm->buffer_id);
1330 ofm->out_port = htons(fm->out_port);
1331 ofm->flags = htons(fm->flags);
5b289eaf 1332 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
27527aa0 1333 break;
3f192f23 1334 }
2e4f5fcf 1335
27527aa0 1336 case OFPUTIL_P_NXM:
3f192f23
SH
1337 case OFPUTIL_P_NXM_TID: {
1338 struct nx_flow_mod *nfm;
1339 int match_len;
1340
982697a4
BP
1341 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1342 NXM_TYPICAL_LEN + fm->ofpacts_len);
1343 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
aa6305ea 1344 nfm->command = ofputil_tid_command(fm, protocol);
623e1caf 1345 nfm->cookie = fm->new_cookie;
7623f4dd 1346 match_len = nx_put_match(msg, &fm->cr, fm->cookie, fm->cookie_mask);
982697a4 1347 nfm = msg->l3;
2e4f5fcf
BP
1348 nfm->idle_timeout = htons(fm->idle_timeout);
1349 nfm->hard_timeout = htons(fm->hard_timeout);
1350 nfm->priority = htons(fm->cr.priority);
1351 nfm->buffer_id = htonl(fm->buffer_id);
1352 nfm->out_port = htons(fm->out_port);
1353 nfm->flags = htons(fm->flags);
1354 nfm->match_len = htons(match_len);
5b289eaf 1355 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
27527aa0 1356 break;
3f192f23 1357 }
27527aa0
BP
1358
1359 default:
2e4f5fcf
BP
1360 NOT_REACHED();
1361 }
1362
982697a4 1363 ofpmsg_update_length(msg);
2e4f5fcf
BP
1364 return msg;
1365}
1366
27527aa0
BP
1367/* Returns a bitmask with a 1-bit for each protocol that could be used to
1368 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1369 * 0-bit for each protocol that is inadequate.
1370 *
1371 * (The return value will have at least one 1-bit.) */
1372enum ofputil_protocol
1373ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1374 size_t n_fms)
1375{
1376 enum ofputil_protocol usable_protocols;
1377 size_t i;
1378
1379 usable_protocols = OFPUTIL_P_ANY;
1380 for (i = 0; i < n_fms; i++) {
1381 const struct ofputil_flow_mod *fm = &fms[i];
1382
1383 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1384 if (fm->table_id != 0xff) {
1385 usable_protocols &= OFPUTIL_P_TID;
1386 }
623e1caf
JP
1387
1388 /* Matching of the cookie is only supported through NXM. */
1389 if (fm->cookie_mask != htonll(0)) {
27527aa0
BP
1390 usable_protocols &= OFPUTIL_P_NXM_ANY;
1391 }
1392 }
1393 assert(usable_protocols);
1394
1395 return usable_protocols;
1396}
1397
90bf1e07 1398static enum ofperr
81d1ea94 1399ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
e2b9ac44 1400 const struct ofp10_flow_stats_request *ofsr,
2e4f5fcf
BP
1401 bool aggregate)
1402{
2e4f5fcf 1403 fsr->aggregate = aggregate;
eec25dc1 1404 ofputil_cls_rule_from_ofp10_match(&ofsr->match, 0, &fsr->match);
2e4f5fcf
BP
1405 fsr->out_port = ntohs(ofsr->out_port);
1406 fsr->table_id = ofsr->table_id;
e729e793 1407 fsr->cookie = fsr->cookie_mask = htonll(0);
2e4f5fcf
BP
1408
1409 return 0;
1410}
1411
90bf1e07 1412static enum ofperr
81d1ea94 1413ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
982697a4 1414 struct ofpbuf *b, bool aggregate)
2e4f5fcf
BP
1415{
1416 const struct nx_flow_stats_request *nfsr;
90bf1e07 1417 enum ofperr error;
2e4f5fcf 1418
982697a4
BP
1419 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1420 error = nx_pull_match(b, ntohs(nfsr->match_len), 0, &fsr->match,
e729e793 1421 &fsr->cookie, &fsr->cookie_mask);
2e4f5fcf
BP
1422 if (error) {
1423 return error;
1424 }
982697a4 1425 if (b->size) {
90bf1e07 1426 return OFPERR_OFPBRC_BAD_LEN;
2e4f5fcf
BP
1427 }
1428
1429 fsr->aggregate = aggregate;
1430 fsr->out_port = ntohs(nfsr->out_port);
1431 fsr->table_id = nfsr->table_id;
1432
1433 return 0;
1434}
1435
1436/* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
b78f6b77
BP
1437 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1438 * successful, otherwise an OpenFlow error code. */
90bf1e07 1439enum ofperr
81d1ea94 1440ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
b78f6b77 1441 const struct ofp_header *oh)
2e4f5fcf 1442{
982697a4 1443 enum ofpraw raw;
2e4f5fcf 1444 struct ofpbuf b;
2e4f5fcf 1445
2013493b 1446 ofpbuf_use_const(&b, oh, ntohs(oh->length));
982697a4
BP
1447 raw = ofpraw_pull_assert(&b);
1448 switch ((int) raw) {
cfc23141 1449 case OFPRAW_OFPST10_FLOW_REQUEST:
982697a4 1450 return ofputil_decode_ofpst_flow_request(fsr, b.data, false);
2e4f5fcf 1451
982697a4
BP
1452 case OFPRAW_OFPST_AGGREGATE_REQUEST:
1453 return ofputil_decode_ofpst_flow_request(fsr, b.data, true);
2e4f5fcf 1454
982697a4
BP
1455 case OFPRAW_NXST_FLOW_REQUEST:
1456 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2e4f5fcf 1457
982697a4
BP
1458 case OFPRAW_NXST_AGGREGATE_REQUEST:
1459 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2e4f5fcf
BP
1460
1461 default:
1462 /* Hey, the caller lied. */
1463 NOT_REACHED();
1464 }
1465}
1466
1467/* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
4ffd1b43 1468 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
27527aa0 1469 * 'protocol', and returns the message. */
2e4f5fcf 1470struct ofpbuf *
81d1ea94 1471ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
27527aa0 1472 enum ofputil_protocol protocol)
2e4f5fcf
BP
1473{
1474 struct ofpbuf *msg;
982697a4 1475 enum ofpraw raw;
2e4f5fcf 1476
27527aa0 1477 switch (protocol) {
06516c65
SH
1478 case OFPUTIL_P_OF12: {
1479 struct ofp11_flow_stats_request *ofsr;
1480
1481 raw = (fsr->aggregate
1482 ? OFPRAW_OFPST_AGGREGATE_REQUEST
cfc23141 1483 : OFPRAW_OFPST11_FLOW_REQUEST);
06516c65
SH
1484 msg = ofpraw_alloc(raw, OFP12_VERSION, NXM_TYPICAL_LEN);
1485 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1486 ofsr->table_id = fsr->table_id;
1487 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1488 ofsr->out_group = htonl(OFPG11_ANY);
1489 ofsr->cookie = fsr->cookie;
1490 ofsr->cookie_mask = fsr->cookie_mask;
1491 oxm_put_match(msg, &fsr->match);
1492 break;
1493 }
1494
27527aa0
BP
1495 case OFPUTIL_P_OF10:
1496 case OFPUTIL_P_OF10_TID: {
e2b9ac44 1497 struct ofp10_flow_stats_request *ofsr;
2e4f5fcf 1498
982697a4
BP
1499 raw = (fsr->aggregate
1500 ? OFPRAW_OFPST_AGGREGATE_REQUEST
cfc23141 1501 : OFPRAW_OFPST10_FLOW_REQUEST);
982697a4
BP
1502 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1503 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
eec25dc1 1504 ofputil_cls_rule_to_ofp10_match(&fsr->match, &ofsr->match);
2e4f5fcf
BP
1505 ofsr->table_id = fsr->table_id;
1506 ofsr->out_port = htons(fsr->out_port);
27527aa0
BP
1507 break;
1508 }
1509
1510 case OFPUTIL_P_NXM:
1511 case OFPUTIL_P_NXM_TID: {
2e4f5fcf
BP
1512 struct nx_flow_stats_request *nfsr;
1513 int match_len;
1514
982697a4
BP
1515 raw = (fsr->aggregate
1516 ? OFPRAW_NXST_AGGREGATE_REQUEST
1517 : OFPRAW_NXST_FLOW_REQUEST);
06516c65 1518 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
982697a4 1519 ofpbuf_put_zeros(msg, sizeof *nfsr);
7623f4dd 1520 match_len = nx_put_match(msg, &fsr->match,
e729e793 1521 fsr->cookie, fsr->cookie_mask);
2e4f5fcf 1522
982697a4 1523 nfsr = msg->l3;
2e4f5fcf
BP
1524 nfsr->out_port = htons(fsr->out_port);
1525 nfsr->match_len = htons(match_len);
1526 nfsr->table_id = fsr->table_id;
27527aa0
BP
1527 break;
1528 }
1529
1530 default:
2e4f5fcf
BP
1531 NOT_REACHED();
1532 }
1533
1534 return msg;
1535}
d1e2cf21 1536
27527aa0
BP
1537/* Returns a bitmask with a 1-bit for each protocol that could be used to
1538 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1539 *
1540 * (The return value will have at least one 1-bit.) */
1541enum ofputil_protocol
1542ofputil_flow_stats_request_usable_protocols(
1543 const struct ofputil_flow_stats_request *fsr)
1544{
1545 enum ofputil_protocol usable_protocols;
1546
1547 usable_protocols = ofputil_usable_protocols(&fsr->match);
1548 if (fsr->cookie_mask != htonll(0)) {
1549 usable_protocols &= OFPUTIL_P_NXM_ANY;
1550 }
1551 return usable_protocols;
1552}
1553
4ffd1b43 1554/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
b78f6b77 1555 * ofputil_flow_stats in 'fs'.
4ffd1b43
BP
1556 *
1557 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1558 * OpenFlow message. Calling this function multiple times for a single 'msg'
1559 * iterates through the replies. The caller must initially leave 'msg''s layer
1560 * pointers null and not modify them between calls.
1561 *
f27f2134
BP
1562 * Most switches don't send the values needed to populate fs->idle_age and
1563 * fs->hard_age, so those members will usually be set to 0. If the switch from
1564 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1565 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1566 * 'idle_age' and 'hard_age' members in 'fs'.
1567 *
f25d0cf3
BP
1568 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1569 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1570 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1571 *
4ffd1b43
BP
1572 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1573 * otherwise a positive errno value. */
1574int
1575ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
f27f2134 1576 struct ofpbuf *msg,
f25d0cf3
BP
1577 bool flow_age_extension,
1578 struct ofpbuf *ofpacts)
4ffd1b43 1579{
982697a4
BP
1580 enum ofperr error;
1581 enum ofpraw raw;
4ffd1b43 1582
982697a4
BP
1583 error = (msg->l2
1584 ? ofpraw_decode(&raw, msg->l2)
1585 : ofpraw_pull(&raw, msg));
1586 if (error) {
1587 return error;
4ffd1b43
BP
1588 }
1589
1590 if (!msg->size) {
1591 return EOF;
6ec5f0c5
SH
1592 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1593 const struct ofp11_flow_stats *ofs;
1594 size_t length;
1595 uint16_t padded_match_len;
1596
1597 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1598 if (!ofs) {
1599 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1600 "bytes at end", msg->size);
1601 return EINVAL;
1602 }
1603
1604 length = ntohs(ofs->length);
1605 if (length < sizeof *ofs) {
1606 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1607 "length %zu", length);
1608 return EINVAL;
1609 }
1610
1611 if (ofputil_pull_ofp11_match(msg, ntohs(ofs->priority), &fs->rule,
1612 &padded_match_len)) {
1613 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1614 return EINVAL;
1615 }
1616
1617 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1618 padded_match_len, ofpacts)) {
1619 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1620 return EINVAL;
1621 }
1622
1623 fs->table_id = ofs->table_id;
1624 fs->duration_sec = ntohl(ofs->duration_sec);
1625 fs->duration_nsec = ntohl(ofs->duration_nsec);
1626 fs->idle_timeout = ntohs(ofs->idle_timeout);
1627 fs->hard_timeout = ntohs(ofs->hard_timeout);
1628 fs->idle_age = -1;
1629 fs->hard_age = -1;
1630 fs->cookie = ofs->cookie;
1631 fs->packet_count = ntohll(ofs->packet_count);
1632 fs->byte_count = ntohll(ofs->byte_count);
cfc23141 1633 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
e2b9ac44 1634 const struct ofp10_flow_stats *ofs;
4ffd1b43
BP
1635 size_t length;
1636
1637 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1638 if (!ofs) {
1639 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1640 "bytes at end", msg->size);
1641 return EINVAL;
1642 }
1643
1644 length = ntohs(ofs->length);
1645 if (length < sizeof *ofs) {
1646 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1647 "length %zu", length);
1648 return EINVAL;
1649 }
1650
d01c980f 1651 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
4ffd1b43
BP
1652 return EINVAL;
1653 }
1654
1655 fs->cookie = get_32aligned_be64(&ofs->cookie);
eec25dc1
BP
1656 ofputil_cls_rule_from_ofp10_match(&ofs->match, ntohs(ofs->priority),
1657 &fs->rule);
4ffd1b43
BP
1658 fs->table_id = ofs->table_id;
1659 fs->duration_sec = ntohl(ofs->duration_sec);
1660 fs->duration_nsec = ntohl(ofs->duration_nsec);
1661 fs->idle_timeout = ntohs(ofs->idle_timeout);
1662 fs->hard_timeout = ntohs(ofs->hard_timeout);
f27f2134
BP
1663 fs->idle_age = -1;
1664 fs->hard_age = -1;
4ffd1b43
BP
1665 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1666 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
982697a4 1667 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
4ffd1b43 1668 const struct nx_flow_stats *nfs;
f25d0cf3 1669 size_t match_len, actions_len, length;
4ffd1b43
BP
1670
1671 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1672 if (!nfs) {
1673 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1674 "bytes at end", msg->size);
1675 return EINVAL;
1676 }
1677
1678 length = ntohs(nfs->length);
1679 match_len = ntohs(nfs->match_len);
1680 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1681 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1682 "claims invalid length %zu", match_len, length);
1683 return EINVAL;
1684 }
e729e793
JP
1685 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1686 NULL, NULL)) {
4ffd1b43
BP
1687 return EINVAL;
1688 }
1689
f25d0cf3 1690 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
d01c980f 1691 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
4ffd1b43
BP
1692 return EINVAL;
1693 }
1694
1695 fs->cookie = nfs->cookie;
1696 fs->table_id = nfs->table_id;
1697 fs->duration_sec = ntohl(nfs->duration_sec);
1698 fs->duration_nsec = ntohl(nfs->duration_nsec);
1699 fs->idle_timeout = ntohs(nfs->idle_timeout);
1700 fs->hard_timeout = ntohs(nfs->hard_timeout);
f27f2134
BP
1701 fs->idle_age = -1;
1702 fs->hard_age = -1;
1703 if (flow_age_extension) {
1704 if (nfs->idle_age) {
1705 fs->idle_age = ntohs(nfs->idle_age) - 1;
1706 }
1707 if (nfs->hard_age) {
1708 fs->hard_age = ntohs(nfs->hard_age) - 1;
1709 }
1710 }
4ffd1b43
BP
1711 fs->packet_count = ntohll(nfs->packet_count);
1712 fs->byte_count = ntohll(nfs->byte_count);
1713 } else {
1714 NOT_REACHED();
1715 }
1716
f25d0cf3
BP
1717 fs->ofpacts = ofpacts->data;
1718 fs->ofpacts_len = ofpacts->size;
1719
4ffd1b43
BP
1720 return 0;
1721}
1722
5e9d0469
BP
1723/* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1724 *
1725 * We use this in situations where OVS internally uses UINT64_MAX to mean
1726 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1727static uint64_t
1728unknown_to_zero(uint64_t count)
1729{
1730 return count != UINT64_MAX ? count : 0;
1731}
1732
349adfb2
BP
1733/* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1734 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1735 * have been initialized with ofputil_start_stats_reply(). */
1736void
1737ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1738 struct list *replies)
1739{
f25d0cf3 1740 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
f25d0cf3 1741 size_t start_ofs = reply->size;
982697a4 1742 enum ofpraw raw;
349adfb2 1743
982697a4 1744 ofpraw_decode_partial(&raw, reply->data, reply->size);
22a86f18
SH
1745 if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1746 struct ofp11_flow_stats *ofs;
1747
1748 ofpbuf_put_uninit(reply, sizeof *ofs);
1749 oxm_put_match(reply, &fs->rule);
1750 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
1751 reply);
1752
1753 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1754 ofs->length = htons(reply->size - start_ofs);
1755 ofs->table_id = fs->table_id;
1756 ofs->pad = 0;
1757 ofs->duration_sec = htonl(fs->duration_sec);
1758 ofs->duration_nsec = htonl(fs->duration_nsec);
1759 ofs->priority = htons(fs->rule.priority);
1760 ofs->idle_timeout = htons(fs->idle_timeout);
1761 ofs->hard_timeout = htons(fs->hard_timeout);
1762 memset(ofs->pad2, 0, sizeof ofs->pad2);
1763 ofs->cookie = fs->cookie;
1764 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
1765 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
1766 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
e2b9ac44 1767 struct ofp10_flow_stats *ofs;
349adfb2 1768
1a59dc2c
BP
1769 ofpbuf_put_uninit(reply, sizeof *ofs);
1770 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1771
1772 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1773 ofs->length = htons(reply->size - start_ofs);
349adfb2
BP
1774 ofs->table_id = fs->table_id;
1775 ofs->pad = 0;
eec25dc1 1776 ofputil_cls_rule_to_ofp10_match(&fs->rule, &ofs->match);
349adfb2
BP
1777 ofs->duration_sec = htonl(fs->duration_sec);
1778 ofs->duration_nsec = htonl(fs->duration_nsec);
1779 ofs->priority = htons(fs->rule.priority);
1780 ofs->idle_timeout = htons(fs->idle_timeout);
1781 ofs->hard_timeout = htons(fs->hard_timeout);
1782 memset(ofs->pad2, 0, sizeof ofs->pad2);
1783 put_32aligned_be64(&ofs->cookie, fs->cookie);
5e9d0469
BP
1784 put_32aligned_be64(&ofs->packet_count,
1785 htonll(unknown_to_zero(fs->packet_count)));
1786 put_32aligned_be64(&ofs->byte_count,
1787 htonll(unknown_to_zero(fs->byte_count)));
982697a4 1788 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
349adfb2 1789 struct nx_flow_stats *nfs;
1a59dc2c 1790 int match_len;
349adfb2 1791
1a59dc2c 1792 ofpbuf_put_uninit(reply, sizeof *nfs);
7623f4dd 1793 match_len = nx_put_match(reply, &fs->rule, 0, 0);
1a59dc2c
BP
1794 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1795
1796 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
1797 nfs->length = htons(reply->size - start_ofs);
349adfb2
BP
1798 nfs->table_id = fs->table_id;
1799 nfs->pad = 0;
1800 nfs->duration_sec = htonl(fs->duration_sec);
1801 nfs->duration_nsec = htonl(fs->duration_nsec);
1802 nfs->priority = htons(fs->rule.priority);
1803 nfs->idle_timeout = htons(fs->idle_timeout);
1804 nfs->hard_timeout = htons(fs->hard_timeout);
f27f2134
BP
1805 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1806 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1807 : UINT16_MAX);
1808 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1809 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1810 : UINT16_MAX);
1a59dc2c 1811 nfs->match_len = htons(match_len);
349adfb2
BP
1812 nfs->cookie = fs->cookie;
1813 nfs->packet_count = htonll(fs->packet_count);
1814 nfs->byte_count = htonll(fs->byte_count);
349adfb2
BP
1815 } else {
1816 NOT_REACHED();
1817 }
f25d0cf3 1818
982697a4 1819 ofpmp_postappend(replies, start_ofs);
349adfb2
BP
1820}
1821
76c93b22 1822/* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
a814ba0f 1823 * NXST_AGGREGATE reply matching 'request', and returns the message. */
76c93b22
BP
1824struct ofpbuf *
1825ofputil_encode_aggregate_stats_reply(
1826 const struct ofputil_aggregate_stats *stats,
982697a4 1827 const struct ofp_header *request)
76c93b22 1828{
a814ba0f
BP
1829 struct ofp_aggregate_stats_reply *asr;
1830 uint64_t packet_count;
1831 uint64_t byte_count;
76c93b22 1832 struct ofpbuf *msg;
982697a4 1833 enum ofpraw raw;
76c93b22 1834
982697a4
BP
1835 ofpraw_decode(&raw, request);
1836 if (raw == OFPRAW_OFPST_AGGREGATE_REQUEST) {
a814ba0f
BP
1837 packet_count = unknown_to_zero(stats->packet_count);
1838 byte_count = unknown_to_zero(stats->byte_count);
76c93b22 1839 } else {
a814ba0f
BP
1840 packet_count = stats->packet_count;
1841 byte_count = stats->byte_count;
76c93b22
BP
1842 }
1843
a814ba0f
BP
1844 msg = ofpraw_alloc_stats_reply(request, 0);
1845 asr = ofpbuf_put_zeros(msg, sizeof *asr);
1846 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
1847 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
1848 asr->flow_count = htonl(stats->flow_count);
1849
76c93b22
BP
1850 return msg;
1851}
1852
982697a4
BP
1853enum ofperr
1854ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
1855 const struct ofp_header *reply)
1856{
a814ba0f 1857 struct ofp_aggregate_stats_reply *asr;
982697a4 1858 struct ofpbuf msg;
982697a4
BP
1859
1860 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
a814ba0f
BP
1861 ofpraw_pull_assert(&msg);
1862
1863 asr = msg.l3;
1864 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
1865 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
1866 stats->flow_count = ntohl(asr->flow_count);
982697a4
BP
1867
1868 return 0;
1869}
1870
b78f6b77
BP
1871/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1872 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1873 * an OpenFlow error code. */
90bf1e07 1874enum ofperr
9b045a0c 1875ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
b78f6b77 1876 const struct ofp_header *oh)
9b045a0c 1877{
982697a4
BP
1878 enum ofpraw raw;
1879 struct ofpbuf b;
9b045a0c 1880
982697a4
BP
1881 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1882 raw = ofpraw_pull_assert(&b);
eefbf181
SH
1883 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
1884 const struct ofp12_flow_removed *ofr;
1885 enum ofperr error;
1886
1887 ofr = ofpbuf_pull(&b, sizeof *ofr);
1888
1889 error = ofputil_pull_ofp11_match(&b, ntohs(ofr->priority),
1890 &fr->rule, NULL);
1891 if (error) {
1892 return error;
1893 }
1894
1895 fr->cookie = ofr->cookie;
1896 fr->reason = ofr->reason;
1897 /* XXX: ofr->table_id is ignored */
1898 fr->duration_sec = ntohl(ofr->duration_sec);
1899 fr->duration_nsec = ntohl(ofr->duration_nsec);
1900 fr->idle_timeout = ntohs(ofr->idle_timeout);
1901 fr->hard_timeout = ntohs(ofr->hard_timeout);
1902 fr->packet_count = ntohll(ofr->packet_count);
1903 fr->byte_count = ntohll(ofr->byte_count);
1904 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
9b045a0c
BP
1905 const struct ofp_flow_removed *ofr;
1906
982697a4
BP
1907 ofr = ofpbuf_pull(&b, sizeof *ofr);
1908
eec25dc1
BP
1909 ofputil_cls_rule_from_ofp10_match(&ofr->match, ntohs(ofr->priority),
1910 &fr->rule);
9b045a0c
BP
1911 fr->cookie = ofr->cookie;
1912 fr->reason = ofr->reason;
1913 fr->duration_sec = ntohl(ofr->duration_sec);
1914 fr->duration_nsec = ntohl(ofr->duration_nsec);
1915 fr->idle_timeout = ntohs(ofr->idle_timeout);
fa2bad0f 1916 fr->hard_timeout = 0;
9b045a0c
BP
1917 fr->packet_count = ntohll(ofr->packet_count);
1918 fr->byte_count = ntohll(ofr->byte_count);
982697a4 1919 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
9b045a0c 1920 struct nx_flow_removed *nfr;
9b045a0c
BP
1921 int error;
1922
9b045a0c
BP
1923 nfr = ofpbuf_pull(&b, sizeof *nfr);
1924 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
e729e793 1925 &fr->rule, NULL, NULL);
9b045a0c
BP
1926 if (error) {
1927 return error;
1928 }
1929 if (b.size) {
90bf1e07 1930 return OFPERR_OFPBRC_BAD_LEN;
9b045a0c
BP
1931 }
1932
1933 fr->cookie = nfr->cookie;
1934 fr->reason = nfr->reason;
1935 fr->duration_sec = ntohl(nfr->duration_sec);
1936 fr->duration_nsec = ntohl(nfr->duration_nsec);
1937 fr->idle_timeout = ntohs(nfr->idle_timeout);
fa2bad0f 1938 fr->hard_timeout = 0;
9b045a0c
BP
1939 fr->packet_count = ntohll(nfr->packet_count);
1940 fr->byte_count = ntohll(nfr->byte_count);
1941 } else {
1942 NOT_REACHED();
1943 }
1944
1945 return 0;
1946}
1947
588cd7b5 1948/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
27527aa0 1949 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
588cd7b5
BP
1950 * message. */
1951struct ofpbuf *
1952ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
27527aa0 1953 enum ofputil_protocol protocol)
588cd7b5
BP
1954{
1955 struct ofpbuf *msg;
1956
27527aa0 1957 switch (protocol) {
83974732
SH
1958 case OFPUTIL_P_OF12: {
1959 struct ofp12_flow_removed *ofr;
1960
1961 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
1962 ofputil_protocol_to_ofp_version(protocol),
1963 htonl(0), NXM_TYPICAL_LEN);
1964 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
1965 ofr->cookie = fr->cookie;
1966 ofr->priority = htons(fr->rule.priority);
1967 ofr->reason = fr->reason;
1968 ofr->table_id = 0;
1969 ofr->duration_sec = htonl(fr->duration_sec);
1970 ofr->duration_nsec = htonl(fr->duration_nsec);
1971 ofr->idle_timeout = htons(fr->idle_timeout);
fa2bad0f 1972 ofr->hard_timeout = htons(fr->hard_timeout);
83974732
SH
1973 ofr->packet_count = htonll(fr->packet_count);
1974 ofr->byte_count = htonll(fr->byte_count);
1975 oxm_put_match(msg, &fr->rule);
1976 break;
1977 }
1978
27527aa0
BP
1979 case OFPUTIL_P_OF10:
1980 case OFPUTIL_P_OF10_TID: {
588cd7b5
BP
1981 struct ofp_flow_removed *ofr;
1982
982697a4
BP
1983 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
1984 htonl(0), 0);
1985 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
eec25dc1 1986 ofputil_cls_rule_to_ofp10_match(&fr->rule, &ofr->match);
7fb563b9 1987 ofr->cookie = fr->cookie;
588cd7b5
BP
1988 ofr->priority = htons(fr->rule.priority);
1989 ofr->reason = fr->reason;
1990 ofr->duration_sec = htonl(fr->duration_sec);
1991 ofr->duration_nsec = htonl(fr->duration_nsec);
1992 ofr->idle_timeout = htons(fr->idle_timeout);
5e9d0469
BP
1993 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1994 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
27527aa0
BP
1995 break;
1996 }
1997
1998 case OFPUTIL_P_NXM:
1999 case OFPUTIL_P_NXM_TID: {
588cd7b5
BP
2000 struct nx_flow_removed *nfr;
2001 int match_len;
2002
982697a4
BP
2003 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2004 htonl(0), NXM_TYPICAL_LEN);
2005 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
7623f4dd 2006 match_len = nx_put_match(msg, &fr->rule, 0, 0);
588cd7b5 2007
982697a4 2008 nfr = msg->l3;
588cd7b5
BP
2009 nfr->cookie = fr->cookie;
2010 nfr->priority = htons(fr->rule.priority);
2011 nfr->reason = fr->reason;
2012 nfr->duration_sec = htonl(fr->duration_sec);
2013 nfr->duration_nsec = htonl(fr->duration_nsec);
2014 nfr->idle_timeout = htons(fr->idle_timeout);
2015 nfr->match_len = htons(match_len);
2016 nfr->packet_count = htonll(fr->packet_count);
2017 nfr->byte_count = htonll(fr->byte_count);
27527aa0
BP
2018 break;
2019 }
2020
2021 default:
588cd7b5
BP
2022 NOT_REACHED();
2023 }
2024
2025 return msg;
2026}
2027
7cfb9651
SH
2028static void
2029ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2030 struct cls_rule *rule,
2031 struct ofpbuf *b)
2032{
2033 pin->packet = b->data;
2034 pin->packet_len = b->size;
2035
2036 pin->fmd.in_port = rule->flow.in_port;
7cfb9651 2037 pin->fmd.tun_id = rule->flow.tun_id;
7cfb9651 2038 pin->fmd.metadata = rule->flow.metadata;
7cfb9651 2039 memcpy(pin->fmd.regs, rule->flow.regs, sizeof pin->fmd.regs);
7cfb9651
SH
2040}
2041
f7cc6bd8 2042enum ofperr
65120a8a
EJ
2043ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2044 const struct ofp_header *oh)
2045{
982697a4
BP
2046 enum ofpraw raw;
2047 struct ofpbuf b;
65120a8a 2048
65120a8a
EJ
2049 memset(pin, 0, sizeof *pin);
2050
982697a4
BP
2051 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2052 raw = ofpraw_pull_assert(&b);
7cfb9651
SH
2053 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2054 const struct ofp12_packet_in *opi;
2055 struct cls_rule rule;
2056 int error;
2057
2058 opi = ofpbuf_pull(&b, sizeof *opi);
2059 error = oxm_pull_match_loose(&b, 0, &rule);
2060 if (error) {
2061 return error;
2062 }
2063
2064 if (!ofpbuf_try_pull(&b, 2)) {
2065 return OFPERR_OFPBRC_BAD_LEN;
2066 }
2067
2068 pin->reason = opi->reason;
2069 pin->table_id = opi->table_id;
2070
2071 pin->buffer_id = ntohl(opi->buffer_id);
2072 pin->total_len = ntohs(opi->total_len);
2073
2074 ofputil_decode_packet_in_finish(pin, &rule, &b);
2075 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
982697a4
BP
2076 const struct ofp_packet_in *opi;
2077
2078 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
65120a8a
EJ
2079
2080 pin->packet = opi->data;
982697a4 2081 pin->packet_len = b.size;
65120a8a 2082
5d6c3af0 2083 pin->fmd.in_port = ntohs(opi->in_port);
65120a8a
EJ
2084 pin->reason = opi->reason;
2085 pin->buffer_id = ntohl(opi->buffer_id);
2086 pin->total_len = ntohs(opi->total_len);
982697a4 2087 } else if (raw == OFPRAW_NXT_PACKET_IN) {
73dbf4ab 2088 const struct nx_packet_in *npi;
54834960 2089 struct cls_rule rule;
54834960
EJ
2090 int error;
2091
54834960
EJ
2092 npi = ofpbuf_pull(&b, sizeof *npi);
2093 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
b5ae8913 2094 NULL);
54834960
EJ
2095 if (error) {
2096 return error;
2097 }
2098
2099 if (!ofpbuf_try_pull(&b, 2)) {
90bf1e07 2100 return OFPERR_OFPBRC_BAD_LEN;
54834960
EJ
2101 }
2102
54834960
EJ
2103 pin->reason = npi->reason;
2104 pin->table_id = npi->table_id;
2105 pin->cookie = npi->cookie;
2106
54834960
EJ
2107 pin->buffer_id = ntohl(npi->buffer_id);
2108 pin->total_len = ntohs(npi->total_len);
7cfb9651
SH
2109
2110 ofputil_decode_packet_in_finish(pin, &rule, &b);
65120a8a
EJ
2111 } else {
2112 NOT_REACHED();
2113 }
2114
2115 return 0;
2116}
2117
d94240ec
SH
2118static void
2119ofputil_packet_in_to_rule(const struct ofputil_packet_in *pin,
2120 struct cls_rule *rule)
2121{
2122 int i;
2123
2124 cls_rule_init_catchall(rule, 0);
42edbe39
BP
2125 if (pin->fmd.tun_id != htonll(0)) {
2126 cls_rule_set_tun_id(rule, pin->fmd.tun_id);
2127 }
2128 if (pin->fmd.metadata != htonll(0)) {
2129 cls_rule_set_metadata(rule, pin->fmd.metadata);
2130 }
d94240ec
SH
2131
2132 for (i = 0; i < FLOW_N_REGS; i++) {
42edbe39
BP
2133 if (pin->fmd.regs[i]) {
2134 cls_rule_set_reg(rule, i, pin->fmd.regs[i]);
2135 }
d94240ec
SH
2136 }
2137
2138 cls_rule_set_in_port(rule, pin->fmd.in_port);
2139}
2140
54834960
EJ
2141/* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2142 * in the format specified by 'packet_in_format'. */
ebb57021 2143struct ofpbuf *
54834960 2144ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
d94240ec 2145 enum ofputil_protocol protocol,
54834960 2146 enum nx_packet_in_format packet_in_format)
ebb57021 2147{
54834960
EJ
2148 size_t send_len = MIN(pin->send_len, pin->packet_len);
2149 struct ofpbuf *packet;
ebb57021
BP
2150
2151 /* Add OFPT_PACKET_IN. */
d94240ec
SH
2152 if (protocol == OFPUTIL_P_OF12) {
2153 struct ofp12_packet_in *opi;
2154 struct cls_rule rule;
2155
2156 ofputil_packet_in_to_rule(pin, &rule);
2157
2158 /* The final argument is just an estimate of the space required. */
2159 packet = ofpraw_alloc_xid(OFPRAW_OFPT12_PACKET_IN, OFP12_VERSION,
2160 htonl(0), (sizeof(struct flow_metadata) * 2
2161 + 2 + send_len));
2162 ofpbuf_put_zeros(packet, sizeof *opi);
2163 oxm_put_match(packet, &rule);
2164 ofpbuf_put_zeros(packet, 2);
2165 ofpbuf_put(packet, pin->packet, send_len);
2166
2167 opi = packet->l3;
2168 opi->buffer_id = htonl(pin->buffer_id);
2169 opi->total_len = htons(pin->total_len);
2170 opi->reason = pin->reason;
2171 opi->table_id = pin->table_id;
2172 } else if (packet_in_format == NXPIF_OPENFLOW10) {
54834960
EJ
2173 struct ofp_packet_in *opi;
2174
982697a4
BP
2175 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2176 htonl(0), send_len);
2177 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
54834960
EJ
2178 opi->total_len = htons(pin->total_len);
2179 opi->in_port = htons(pin->fmd.in_port);
2180 opi->reason = pin->reason;
2181 opi->buffer_id = htonl(pin->buffer_id);
2182
2183 ofpbuf_put(packet, pin->packet, send_len);
2184 } else if (packet_in_format == NXPIF_NXM) {
73dbf4ab 2185 struct nx_packet_in *npi;
54834960
EJ
2186 struct cls_rule rule;
2187 size_t match_len;
54834960 2188
d94240ec 2189 ofputil_packet_in_to_rule(pin, &rule);
54834960 2190
982697a4
BP
2191 /* The final argument is just an estimate of the space required. */
2192 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2193 htonl(0), (sizeof(struct flow_metadata) * 2
2194 + 2 + send_len));
54834960 2195 ofpbuf_put_zeros(packet, sizeof *npi);
7623f4dd 2196 match_len = nx_put_match(packet, &rule, 0, 0);
54834960
EJ
2197 ofpbuf_put_zeros(packet, 2);
2198 ofpbuf_put(packet, pin->packet, send_len);
2199
982697a4 2200 npi = packet->l3;
54834960
EJ
2201 npi->buffer_id = htonl(pin->buffer_id);
2202 npi->total_len = htons(pin->total_len);
2203 npi->reason = pin->reason;
2204 npi->table_id = pin->table_id;
2205 npi->cookie = pin->cookie;
2206 npi->match_len = htons(match_len);
2207 } else {
2208 NOT_REACHED();
2209 }
982697a4 2210 ofpmsg_update_length(packet);
54834960
EJ
2211
2212 return packet;
ebb57021
BP
2213}
2214
7c1a76a4
BP
2215const char *
2216ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2217{
2218 static char s[INT_STRLEN(int) + 1];
2219
2220 switch (reason) {
2221 case OFPR_NO_MATCH:
2222 return "no_match";
2223 case OFPR_ACTION:
2224 return "action";
2225 case OFPR_INVALID_TTL:
2226 return "invalid_ttl";
2227
2228 case OFPR_N_REASONS:
2229 default:
2230 sprintf(s, "%d", (int) reason);
2231 return s;
2232 }
2233}
2234
2235bool
2236ofputil_packet_in_reason_from_string(const char *s,
2237 enum ofp_packet_in_reason *reason)
2238{
2239 int i;
2240
2241 for (i = 0; i < OFPR_N_REASONS; i++) {
2242 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2243 *reason = i;
2244 return true;
2245 }
2246 }
2247 return false;
2248}
2249
f25d0cf3
BP
2250/* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2251 * 'po'.
2252 *
2253 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2254 * message's actions. The caller must initialize 'ofpacts' and retains
2255 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2256 *
2257 * Returns 0 if successful, otherwise an OFPERR_* value. */
c6a93eb7
BP
2258enum ofperr
2259ofputil_decode_packet_out(struct ofputil_packet_out *po,
982697a4 2260 const struct ofp_header *oh,
f25d0cf3 2261 struct ofpbuf *ofpacts)
c6a93eb7 2262{
eb5ee596 2263 enum ofperr bad_in_port_err;
982697a4 2264 enum ofpraw raw;
c6a93eb7
BP
2265 struct ofpbuf b;
2266
982697a4
BP
2267 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2268 raw = ofpraw_pull_assert(&b);
982697a4 2269
eb5ee596
SH
2270 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2271 enum ofperr error;
2272 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2273
2274 po->buffer_id = ntohl(opo->buffer_id);
2275 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2276 if (error) {
2277 return error;
2278 }
2279
2280 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2281 ofpacts);
2282 if (error) {
2283 return error;
2284 }
2285
2286 bad_in_port_err = OFPERR_OFPBMC_BAD_VALUE;
2287 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
8a6bc7cd
SH
2288 enum ofperr error;
2289 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2290
2291 po->buffer_id = ntohl(opo->buffer_id);
2292 po->in_port = ntohs(opo->in_port);
2293
2294 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2295 if (error) {
2296 return error;
2297 }
eb5ee596
SH
2298
2299 bad_in_port_err = OFPERR_NXBRC_BAD_IN_PORT;
8a6bc7cd
SH
2300 } else {
2301 NOT_REACHED();
2302 }
2303
c6a93eb7 2304 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
751c7785 2305 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
c6a93eb7
BP
2306 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2307 po->in_port);
eb5ee596 2308 return bad_in_port_err;
c6a93eb7
BP
2309 }
2310
f25d0cf3
BP
2311 po->ofpacts = ofpacts->data;
2312 po->ofpacts_len = ofpacts->size;
c6a93eb7
BP
2313
2314 if (po->buffer_id == UINT32_MAX) {
2315 po->packet = b.data;
2316 po->packet_len = b.size;
2317 } else {
2318 po->packet = NULL;
2319 po->packet_len = 0;
2320 }
2321
2322 return 0;
2323}
6c038611
BP
2324\f
2325/* ofputil_phy_port */
2326
2327/* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2328BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2329BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2330BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2331BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2332BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2333BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2334BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2335
2336/* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
9e1fd49b
BP
2337BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2338BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2339BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2340BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2341BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
6c038611 2342
9e1fd49b
BP
2343static enum netdev_features
2344netdev_port_features_from_ofp10(ovs_be32 ofp10_)
6c038611
BP
2345{
2346 uint32_t ofp10 = ntohl(ofp10_);
2347 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2348}
2349
9e1fd49b
BP
2350static ovs_be32
2351netdev_port_features_to_ofp10(enum netdev_features features)
6c038611
BP
2352{
2353 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2354}
c6a93eb7 2355
9e1fd49b
BP
2356BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2357BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2358BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2359BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2360BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2361BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2362BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2363BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2364BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2365BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2366BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2367BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2368BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2369BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2370BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2371BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2372
2373static enum netdev_features
2374netdev_port_features_from_ofp11(ovs_be32 ofp11)
2375{
2376 return ntohl(ofp11) & 0xffff;
2377}
2378
2379static ovs_be32
2380netdev_port_features_to_ofp11(enum netdev_features features)
2381{
2382 return htonl(features & 0xffff);
2383}
2384
2385static enum ofperr
2386ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2387 const struct ofp10_phy_port *opp)
2388{
2389 memset(pp, 0, sizeof *pp);
2390
2391 pp->port_no = ntohs(opp->port_no);
2392 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2393 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2394
2395 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2396 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2397
2398 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2399 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2400 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2401 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2402
2403 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2404 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2405
2406 return 0;
2407}
2408
2409static enum ofperr
2410ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2411 const struct ofp11_port *op)
2412{
2413 enum ofperr error;
2414
2415 memset(pp, 0, sizeof *pp);
2416
2417 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2418 if (error) {
2419 return error;
2420 }
2421 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2422 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2423
2424 pp->config = ntohl(op->config) & OFPPC11_ALL;
2425 pp->state = ntohl(op->state) & OFPPC11_ALL;
2426
2427 pp->curr = netdev_port_features_from_ofp11(op->curr);
2428 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2429 pp->supported = netdev_port_features_from_ofp11(op->supported);
2430 pp->peer = netdev_port_features_from_ofp11(op->peer);
2431
2432 pp->curr_speed = ntohl(op->curr_speed);
2433 pp->max_speed = ntohl(op->max_speed);
2434
2435 return 0;
2436}
2437
5b5a3a67 2438static size_t
2e3fa633
SH
2439ofputil_get_phy_port_size(enum ofp_version ofp_version)
2440{
2441 switch (ofp_version) {
2442 case OFP10_VERSION:
2443 return sizeof(struct ofp10_phy_port);
2444 case OFP11_VERSION:
2445 case OFP12_VERSION:
2446 return sizeof(struct ofp11_port);
2447 default:
2448 NOT_REACHED();
2449 }
5b5a3a67
JP
2450}
2451
9e1fd49b
BP
2452static void
2453ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2454 struct ofp10_phy_port *opp)
2455{
2456 memset(opp, 0, sizeof *opp);
2457
2458 opp->port_no = htons(pp->port_no);
2459 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2460 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2461
2462 opp->config = htonl(pp->config & OFPPC10_ALL);
2463 opp->state = htonl(pp->state & OFPPS10_ALL);
2464
2465 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2466 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2467 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2468 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2469}
2470
2471static void
2472ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2473 struct ofp11_port *op)
2474{
2475 memset(op, 0, sizeof *op);
2476
2477 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2478 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2479 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2480
2481 op->config = htonl(pp->config & OFPPC11_ALL);
2482 op->state = htonl(pp->state & OFPPS11_ALL);
2483
2484 op->curr = netdev_port_features_to_ofp11(pp->curr);
2485 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2486 op->supported = netdev_port_features_to_ofp11(pp->supported);
2487 op->peer = netdev_port_features_to_ofp11(pp->peer);
2488
2489 op->curr_speed = htonl(pp->curr_speed);
2490 op->max_speed = htonl(pp->max_speed);
2491}
2492
2493static void
2e3fa633
SH
2494ofputil_put_phy_port(enum ofp_version ofp_version,
2495 const struct ofputil_phy_port *pp, struct ofpbuf *b)
9e1fd49b 2496{
2e3fa633
SH
2497 switch (ofp_version) {
2498 case OFP10_VERSION: {
9e1fd49b
BP
2499 struct ofp10_phy_port *opp;
2500 if (b->size + sizeof *opp <= UINT16_MAX) {
2501 opp = ofpbuf_put_uninit(b, sizeof *opp);
2502 ofputil_encode_ofp10_phy_port(pp, opp);
2503 }
2e3fa633
SH
2504 break;
2505 }
2506
2507 case OFP11_VERSION:
2508 case OFP12_VERSION: {
9e1fd49b
BP
2509 struct ofp11_port *op;
2510 if (b->size + sizeof *op <= UINT16_MAX) {
2511 op = ofpbuf_put_uninit(b, sizeof *op);
2512 ofputil_encode_ofp11_port(pp, op);
2513 }
2e3fa633
SH
2514 break;
2515 }
2516
2517 default:
2518 NOT_REACHED();
9e1fd49b
BP
2519 }
2520}
2be393ed
JP
2521
2522void
2e3fa633 2523ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2be393ed
JP
2524 const struct ofputil_phy_port *pp,
2525 struct list *replies)
2526{
2e3fa633
SH
2527 switch (ofp_version) {
2528 case OFP10_VERSION: {
2be393ed
JP
2529 struct ofp10_phy_port *opp;
2530
982697a4 2531 opp = ofpmp_append(replies, sizeof *opp);
2be393ed 2532 ofputil_encode_ofp10_phy_port(pp, opp);
2e3fa633
SH
2533 break;
2534 }
2535
2536 case OFP11_VERSION:
2537 case OFP12_VERSION: {
2be393ed
JP
2538 struct ofp11_port *op;
2539
982697a4 2540 op = ofpmp_append(replies, sizeof *op);
2be393ed 2541 ofputil_encode_ofp11_port(pp, op);
2e3fa633
SH
2542 break;
2543 }
2544
2545 default:
2546 NOT_REACHED();
2be393ed
JP
2547 }
2548}
9e1fd49b
BP
2549\f
2550/* ofputil_switch_features */
2551
2552#define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
60202987 2553 OFPC_IP_REASM | OFPC_QUEUE_STATS)
9e1fd49b
BP
2554BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2555BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2556BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2557BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2558BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2559BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2560
2561struct ofputil_action_bit_translation {
2562 enum ofputil_action_bitmap ofputil_bit;
2563 int of_bit;
2564};
2565
2566static const struct ofputil_action_bit_translation of10_action_bits[] = {
2567 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2568 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2569 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2570 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2571 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2572 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2573 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2574 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2575 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2576 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2577 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2578 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2579 { 0, 0 },
2580};
2581
9e1fd49b
BP
2582static enum ofputil_action_bitmap
2583decode_action_bits(ovs_be32 of_actions,
2584 const struct ofputil_action_bit_translation *x)
2585{
2586 enum ofputil_action_bitmap ofputil_actions;
2587
2588 ofputil_actions = 0;
2589 for (; x->ofputil_bit; x++) {
2590 if (of_actions & htonl(1u << x->of_bit)) {
2591 ofputil_actions |= x->ofputil_bit;
2592 }
2593 }
2594 return ofputil_actions;
2595}
2596
60202987
SH
2597static uint32_t
2598ofputil_capabilities_mask(enum ofp_version ofp_version)
2599{
2600 /* Handle capabilities whose bit is unique for all Open Flow versions */
2601 switch (ofp_version) {
2602 case OFP10_VERSION:
2603 case OFP11_VERSION:
2604 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2605 case OFP12_VERSION:
2606 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2607 default:
2608 /* Caller needs to check osf->header.version itself */
2609 return 0;
2610 }
2611}
2612
9e1fd49b
BP
2613/* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2614 * abstract representation in '*features'. Initializes '*b' to iterate over
2615 * the OpenFlow port structures following 'osf' with later calls to
2be393ed 2616 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
9e1fd49b
BP
2617 * OFPERR_* value. */
2618enum ofperr
982697a4 2619ofputil_decode_switch_features(const struct ofp_header *oh,
9e1fd49b
BP
2620 struct ofputil_switch_features *features,
2621 struct ofpbuf *b)
2622{
982697a4
BP
2623 const struct ofp_switch_features *osf;
2624 enum ofpraw raw;
2625
2626 ofpbuf_use_const(b, oh, ntohs(oh->length));
2627 raw = ofpraw_pull_assert(b);
9e1fd49b 2628
982697a4 2629 osf = ofpbuf_pull(b, sizeof *osf);
9e1fd49b
BP
2630 features->datapath_id = ntohll(osf->datapath_id);
2631 features->n_buffers = ntohl(osf->n_buffers);
2632 features->n_tables = osf->n_tables;
2633
60202987
SH
2634 features->capabilities = ntohl(osf->capabilities) &
2635 ofputil_capabilities_mask(oh->version);
9e1fd49b 2636
982697a4 2637 if (b->size % ofputil_get_phy_port_size(oh->version)) {
5b5a3a67
JP
2638 return OFPERR_OFPBRC_BAD_LEN;
2639 }
2640
982697a4 2641 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
9e1fd49b
BP
2642 if (osf->capabilities & htonl(OFPC10_STP)) {
2643 features->capabilities |= OFPUTIL_C_STP;
2644 }
2645 features->actions = decode_action_bits(osf->actions, of10_action_bits);
982697a4 2646 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
9e1fd49b
BP
2647 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2648 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2649 }
34b28fc7 2650 features->actions = 0;
9e1fd49b
BP
2651 } else {
2652 return OFPERR_OFPBRC_BAD_VERSION;
2653 }
2654
2655 return 0;
2656}
2657
982697a4 2658/* Returns true if the maximum number of ports are in 'oh'. */
347b7ac4 2659static bool
982697a4 2660max_ports_in_features(const struct ofp_header *oh)
347b7ac4 2661{
982697a4
BP
2662 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2663 return ntohs(oh->length) + pp_size > UINT16_MAX;
347b7ac4
JP
2664}
2665
2666/* Given a buffer 'b' that contains a Features Reply message, checks if
2667 * it contains the maximum number of ports that will fit. If so, it
2668 * returns true and removes the ports from the message. The caller
2669 * should then send an OFPST_PORT_DESC stats request to get the ports,
2670 * since the switch may have more ports than could be represented in the
2671 * Features Reply. Otherwise, returns false.
2672 */
2673bool
2674ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2675{
982697a4 2676 struct ofp_header *oh = b->data;
347b7ac4 2677
982697a4 2678 if (max_ports_in_features(oh)) {
347b7ac4 2679 /* Remove all the ports. */
982697a4
BP
2680 b->size = (sizeof(struct ofp_header)
2681 + sizeof(struct ofp_switch_features));
2682 ofpmsg_update_length(b);
347b7ac4
JP
2683
2684 return true;
2685 }
2686
2687 return false;
2688}
2689
9e1fd49b
BP
2690static ovs_be32
2691encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2692 const struct ofputil_action_bit_translation *x)
2693{
2694 uint32_t of_actions;
2695
2696 of_actions = 0;
2697 for (; x->ofputil_bit; x++) {
2698 if (ofputil_actions & x->ofputil_bit) {
2699 of_actions |= 1 << x->of_bit;
2700 }
2701 }
2702 return htonl(of_actions);
2703}
2704
2705/* Returns a buffer owned by the caller that encodes 'features' in the format
2706 * required by 'protocol' with the given 'xid'. The caller should append port
2707 * information to the buffer with subsequent calls to
2708 * ofputil_put_switch_features_port(). */
2709struct ofpbuf *
2710ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2711 enum ofputil_protocol protocol, ovs_be32 xid)
2712{
2713 struct ofp_switch_features *osf;
2714 struct ofpbuf *b;
2e3fa633
SH
2715 enum ofp_version version;
2716 enum ofpraw raw;
982697a4
BP
2717
2718 version = ofputil_protocol_to_ofp_version(protocol);
2e3fa633
SH
2719 switch (version) {
2720 case OFP10_VERSION:
2721 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2722 break;
2723 case OFP11_VERSION:
2724 case OFP12_VERSION:
2725 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2726 break;
2727 default:
2728 NOT_REACHED();
2729 }
2730 b = ofpraw_alloc_xid(raw, version, xid, 0);
982697a4 2731 osf = ofpbuf_put_zeros(b, sizeof *osf);
9e1fd49b
BP
2732 osf->datapath_id = htonll(features->datapath_id);
2733 osf->n_buffers = htonl(features->n_buffers);
2734 osf->n_tables = features->n_tables;
2735
2736 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
60202987
SH
2737 osf->capabilities = htonl(features->capabilities &
2738 ofputil_capabilities_mask(version));
2e3fa633
SH
2739 switch (version) {
2740 case OFP10_VERSION:
9e1fd49b
BP
2741 if (features->capabilities & OFPUTIL_C_STP) {
2742 osf->capabilities |= htonl(OFPC10_STP);
2743 }
2744 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2e3fa633
SH
2745 break;
2746 case OFP11_VERSION:
2747 case OFP12_VERSION:
9e1fd49b
BP
2748 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2749 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2750 }
2e3fa633
SH
2751 break;
2752 default:
2753 NOT_REACHED();
9e1fd49b
BP
2754 }
2755
2756 return b;
2757}
2758
2759/* Encodes 'pp' into the format required by the switch_features message already
2760 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2761 * and appends the encoded version to 'b'. */
2762void
2763ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2764 struct ofpbuf *b)
2765{
982697a4 2766 const struct ofp_header *oh = b->data;
9e1fd49b 2767
982697a4 2768 ofputil_put_phy_port(oh->version, pp, b);
9e1fd49b
BP
2769}
2770\f
2771/* ofputil_port_status */
2772
2773/* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2774 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2775enum ofperr
982697a4 2776ofputil_decode_port_status(const struct ofp_header *oh,
9e1fd49b
BP
2777 struct ofputil_port_status *ps)
2778{
982697a4 2779 const struct ofp_port_status *ops;
9e1fd49b
BP
2780 struct ofpbuf b;
2781 int retval;
2782
982697a4
BP
2783 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2784 ofpraw_pull_assert(&b);
2785 ops = ofpbuf_pull(&b, sizeof *ops);
2786
9e1fd49b
BP
2787 if (ops->reason != OFPPR_ADD &&
2788 ops->reason != OFPPR_DELETE &&
2789 ops->reason != OFPPR_MODIFY) {
2790 return OFPERR_NXBRC_BAD_REASON;
2791 }
2792 ps->reason = ops->reason;
2793
982697a4 2794 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
9e1fd49b
BP
2795 assert(retval != EOF);
2796 return retval;
2797}
2798
2799/* Converts the abstract form of a "port status" message in '*ps' into an
2800 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2801 * a buffer owned by the caller. */
2802struct ofpbuf *
2803ofputil_encode_port_status(const struct ofputil_port_status *ps,
2804 enum ofputil_protocol protocol)
2805{
2806 struct ofp_port_status *ops;
2807 struct ofpbuf *b;
2e3fa633
SH
2808 enum ofp_version version;
2809 enum ofpraw raw;
982697a4
BP
2810
2811 version = ofputil_protocol_to_ofp_version(protocol);
2e3fa633
SH
2812 switch (version) {
2813 case OFP10_VERSION:
2814 raw = OFPRAW_OFPT10_PORT_STATUS;
2815 break;
2816
2817 case OFP11_VERSION:
2818 case OFP12_VERSION:
2819 raw = OFPRAW_OFPT11_PORT_STATUS;
2820 break;
2821
2822 default:
2823 NOT_REACHED();
2824 }
2825
2826 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
982697a4 2827 ops = ofpbuf_put_zeros(b, sizeof *ops);
9e1fd49b 2828 ops->reason = ps->reason;
982697a4
BP
2829 ofputil_put_phy_port(version, &ps->desc, b);
2830 ofpmsg_update_length(b);
9e1fd49b
BP
2831 return b;
2832}
2833\f
2834/* ofputil_port_mod */
2835
2836/* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2837 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2838enum ofperr
2839ofputil_decode_port_mod(const struct ofp_header *oh,
2840 struct ofputil_port_mod *pm)
2841{
982697a4
BP
2842 enum ofpraw raw;
2843 struct ofpbuf b;
9e1fd49b 2844
982697a4
BP
2845 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2846 raw = ofpraw_pull_assert(&b);
2847
2848 if (raw == OFPRAW_OFPT10_PORT_MOD) {
2849 const struct ofp10_port_mod *opm = b.data;
9e1fd49b
BP
2850
2851 pm->port_no = ntohs(opm->port_no);
2852 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2853 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2854 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2855 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
982697a4
BP
2856 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
2857 const struct ofp11_port_mod *opm = b.data;
9e1fd49b
BP
2858 enum ofperr error;
2859
9e1fd49b
BP
2860 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2861 if (error) {
2862 return error;
2863 }
2864
2865 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2866 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2867 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2868 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2869 } else {
982697a4 2870 return OFPERR_OFPBRC_BAD_TYPE;
9e1fd49b
BP
2871 }
2872
2873 pm->config &= pm->mask;
2874 return 0;
2875}
2876
2877/* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2878 * message suitable for 'protocol', and returns that encoded form in a buffer
2879 * owned by the caller. */
2880struct ofpbuf *
2881ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2882 enum ofputil_protocol protocol)
2883{
2e3fa633 2884 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
9e1fd49b
BP
2885 struct ofpbuf *b;
2886
2e3fa633
SH
2887 switch (ofp_version) {
2888 case OFP10_VERSION: {
9e1fd49b
BP
2889 struct ofp10_port_mod *opm;
2890
982697a4
BP
2891 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
2892 opm = ofpbuf_put_zeros(b, sizeof *opm);
9e1fd49b
BP
2893 opm->port_no = htons(pm->port_no);
2894 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2895 opm->config = htonl(pm->config & OFPPC10_ALL);
2896 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2897 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2e3fa633
SH
2898 break;
2899 }
2900
5fe7c919
SH
2901 case OFP11_VERSION:
2902 case OFP12_VERSION: {
9e1fd49b
BP
2903 struct ofp11_port_mod *opm;
2904
982697a4
BP
2905 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
2906 opm = ofpbuf_put_zeros(b, sizeof *opm);
026a5179 2907 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
9e1fd49b
BP
2908 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2909 opm->config = htonl(pm->config & OFPPC11_ALL);
2910 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2911 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2e3fa633
SH
2912 break;
2913 }
2914
2e3fa633 2915 default:
9e1fd49b
BP
2916 NOT_REACHED();
2917 }
2918
2919 return b;
2920}
2b07c8b1
BP
2921\f
2922/* ofputil_flow_monitor_request */
2923
2924/* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
2925 * ofputil_flow_monitor_request in 'rq'.
2926 *
2927 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
2928 * message. Calling this function multiple times for a single 'msg' iterates
2929 * through the requests. The caller must initially leave 'msg''s layer
2930 * pointers null and not modify them between calls.
2931 *
2932 * Returns 0 if successful, EOF if no requests were left in this 'msg',
2933 * otherwise an OFPERR_* value. */
2934int
2935ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
2936 struct ofpbuf *msg)
2937{
2938 struct nx_flow_monitor_request *nfmr;
2939 uint16_t flags;
2940
2941 if (!msg->l2) {
2942 msg->l2 = msg->data;
982697a4 2943 ofpraw_pull_assert(msg);
2b07c8b1
BP
2944 }
2945
2946 if (!msg->size) {
2947 return EOF;
2948 }
2949
2950 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
2951 if (!nfmr) {
2952 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
2953 "leftover bytes at end", msg->size);
2954 return OFPERR_OFPBRC_BAD_LEN;
2955 }
2956
2957 flags = ntohs(nfmr->flags);
2958 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
2959 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
2960 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
2961 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
2962 flags);
2963 return OFPERR_NXBRC_FM_BAD_FLAGS;
2964 }
2965
2966 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
2967 return OFPERR_NXBRC_MUST_BE_ZERO;
2968 }
2969
2970 rq->id = ntohl(nfmr->id);
2971 rq->flags = flags;
2972 rq->out_port = ntohs(nfmr->out_port);
2973 rq->table_id = nfmr->table_id;
2974
2975 return nx_pull_match(msg, ntohs(nfmr->match_len), OFP_DEFAULT_PRIORITY,
2976 &rq->match, NULL, NULL);
2977}
2978
2979void
2980ofputil_append_flow_monitor_request(
2981 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
2982{
2983 struct nx_flow_monitor_request *nfmr;
2984 size_t start_ofs;
2985 int match_len;
2986
2987 if (!msg->size) {
982697a4 2988 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
2b07c8b1
BP
2989 }
2990
2991 start_ofs = msg->size;
2992 ofpbuf_put_zeros(msg, sizeof *nfmr);
7623f4dd 2993 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
2b07c8b1
BP
2994
2995 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
2996 nfmr->id = htonl(rq->id);
2997 nfmr->flags = htons(rq->flags);
2998 nfmr->out_port = htons(rq->out_port);
2999 nfmr->match_len = htons(match_len);
3000 nfmr->table_id = rq->table_id;
3001}
3002
3003/* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3004 * into an abstract ofputil_flow_update in 'update'. The caller must have
3005 * initialized update->match to point to space allocated for a cls_rule.
3006 *
3007 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3008 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3009 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3010 * will point into the 'ofpacts' buffer.
3011 *
3012 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3013 * this function multiple times for a single 'msg' iterates through the
3014 * updates. The caller must initially leave 'msg''s layer pointers null and
3015 * not modify them between calls.
3016 *
3017 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3018 * otherwise an OFPERR_* value. */
3019int
3020ofputil_decode_flow_update(struct ofputil_flow_update *update,
3021 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3022{
3023 struct nx_flow_update_header *nfuh;
3024 unsigned int length;
3025
3026 if (!msg->l2) {
3027 msg->l2 = msg->data;
982697a4 3028 ofpraw_pull_assert(msg);
2b07c8b1
BP
3029 }
3030
3031 if (!msg->size) {
3032 return EOF;
3033 }
3034
3035 if (msg->size < sizeof(struct nx_flow_update_header)) {
3036 goto bad_len;
3037 }
3038
3039 nfuh = msg->data;
3040 update->event = ntohs(nfuh->event);
3041 length = ntohs(nfuh->length);
3042 if (length > msg->size || length % 8) {
3043 goto bad_len;
3044 }
3045
3046 if (update->event == NXFME_ABBREV) {
3047 struct nx_flow_update_abbrev *nfua;
3048
3049 if (length != sizeof *nfua) {
3050 goto bad_len;
3051 }
3052
3053 nfua = ofpbuf_pull(msg, sizeof *nfua);
3054 update->xid = nfua->xid;
3055 return 0;
3056 } else if (update->event == NXFME_ADDED
3057 || update->event == NXFME_DELETED
3058 || update->event == NXFME_MODIFIED) {
3059 struct nx_flow_update_full *nfuf;
3060 unsigned int actions_len;
3061 unsigned int match_len;
3062 enum ofperr error;
3063
3064 if (length < sizeof *nfuf) {
3065 goto bad_len;
3066 }
3067
3068 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3069 match_len = ntohs(nfuf->match_len);
3070 if (sizeof *nfuf + match_len > length) {
3071 goto bad_len;
3072 }
3073
3074 update->reason = ntohs(nfuf->reason);
3075 update->idle_timeout = ntohs(nfuf->idle_timeout);
3076 update->hard_timeout = ntohs(nfuf->hard_timeout);
3077 update->table_id = nfuf->table_id;
3078 update->cookie = nfuf->cookie;
3079
3080 error = nx_pull_match(msg, match_len, ntohs(nfuf->priority),
3081 update->match, NULL, NULL);
3082 if (error) {
3083 return error;
3084 }
3085
3086 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3087 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3088 if (error) {
3089 return error;
3090 }
3091
3092 update->ofpacts = ofpacts->data;
3093 update->ofpacts_len = ofpacts->size;
3094 return 0;
3095 } else {
3096 VLOG_WARN_RL(&bad_ofmsg_rl,
3097 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3098 ntohs(nfuh->event));
3099 return OFPERR_OFPET_BAD_REQUEST;
3100 }
3101
3102bad_len:
3103 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3104 "leftover bytes at end", msg->size);
3105 return OFPERR_OFPBRC_BAD_LEN;
3106}
3107
3108uint32_t
3109ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3110{
982697a4
BP
3111 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3112
3113 return ntohl(cancel->id);
2b07c8b1 3114}
9e1fd49b 3115
2b07c8b1
BP
3116struct ofpbuf *
3117ofputil_encode_flow_monitor_cancel(uint32_t id)
3118{
3119 struct nx_flow_monitor_cancel *nfmc;
3120 struct ofpbuf *msg;
3121
982697a4
BP
3122 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3123 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
2b07c8b1
BP
3124 nfmc->id = htonl(id);
3125 return msg;
3126}
3127
3128void
3129ofputil_start_flow_update(struct list *replies)
3130{
3131 struct ofpbuf *msg;
3132
982697a4
BP
3133 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3134 htonl(0), 1024);
2b07c8b1
BP
3135
3136 list_init(replies);
3137 list_push_back(replies, &msg->list_node);
3138}
3139
3140void
3141ofputil_append_flow_update(const struct ofputil_flow_update *update,
3142 struct list *replies)
3143{
3144 struct nx_flow_update_header *nfuh;
3145 struct ofpbuf *msg;
3146 size_t start_ofs;
3147
3148 msg = ofpbuf_from_list(list_back(replies));
3149 start_ofs = msg->size;
3150
3151 if (update->event == NXFME_ABBREV) {
3152 struct nx_flow_update_abbrev *nfua;
3153
3154 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3155 nfua->xid = update->xid;
3156 } else {
3157 struct nx_flow_update_full *nfuf;
3158 int match_len;
3159
3160 ofpbuf_put_zeros(msg, sizeof *nfuf);
7623f4dd 3161 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
2b07c8b1
BP
3162 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3163
3164 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3165 nfuf->reason = htons(update->reason);
3166 nfuf->priority = htons(update->match->priority);
3167 nfuf->idle_timeout = htons(update->idle_timeout);
3168 nfuf->hard_timeout = htons(update->hard_timeout);
3169 nfuf->match_len = htons(match_len);
3170 nfuf->table_id = update->table_id;
3171 nfuf->cookie = update->cookie;
3172 }
3173
3174 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3175 nfuh->length = htons(msg->size - start_ofs);
3176 nfuh->event = htons(update->event);
3177
982697a4 3178 ofpmp_postappend(replies, start_ofs);
2b07c8b1
BP
3179}
3180\f
c6a93eb7 3181struct ofpbuf *
de0f3156
SH
3182ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3183 enum ofputil_protocol protocol)
c6a93eb7 3184{
de0f3156 3185 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
c6a93eb7
BP
3186 struct ofpbuf *msg;
3187 size_t size;
3188
982697a4 3189 size = po->ofpacts_len;
c6a93eb7
BP
3190 if (po->buffer_id == UINT32_MAX) {
3191 size += po->packet_len;
3192 }
3193
de0f3156
SH
3194 switch (ofp_version) {
3195 case OFP10_VERSION: {
3196 struct ofp_packet_out *opo;
3197 size_t actions_ofs;
3198
3199 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3200 ofpbuf_put_zeros(msg, sizeof *opo);
3201 actions_ofs = msg->size;
3202 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3203
3204 opo = msg->l3;
3205 opo->buffer_id = htonl(po->buffer_id);
3206 opo->in_port = htons(po->in_port);
3207 opo->actions_len = htons(msg->size - actions_ofs);
3208 break;
3209 }
f25d0cf3 3210
de0f3156 3211 case OFP11_VERSION:
7c1b1a0d
SH
3212 case OFP12_VERSION: {
3213 struct ofp11_packet_out *opo;
3214 size_t len;
3215
3216 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3217 ofpbuf_put_zeros(msg, sizeof *opo);
3218 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3219
3220 opo = msg->l3;
3221 opo->buffer_id = htonl(po->buffer_id);
3222 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3223 opo->actions_len = htons(len);
3224 break;
3225 }
3226
de0f3156
SH
3227 default:
3228 NOT_REACHED();
3229 }
f25d0cf3 3230
c6a93eb7
BP
3231 if (po->buffer_id == UINT32_MAX) {
3232 ofpbuf_put(msg, po->packet, po->packet_len);
3233 }
f25d0cf3 3234
982697a4 3235 ofpmsg_update_length(msg);
c6a93eb7
BP
3236
3237 return msg;
3238}
d1e2cf21 3239\f
fa37b408
BP
3240/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3241struct ofpbuf *
1a126c0c 3242make_echo_request(enum ofp_version ofp_version)
fa37b408 3243{
1a126c0c 3244 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
982697a4 3245 htonl(0), 0);
fa37b408
BP
3246}
3247
3248/* Creates and returns an OFPT_ECHO_REPLY message matching the
3249 * OFPT_ECHO_REQUEST message in 'rq'. */
3250struct ofpbuf *
3251make_echo_reply(const struct ofp_header *rq)
3252{
982697a4
BP
3253 struct ofpbuf rq_buf;
3254 struct ofpbuf *reply;
3255
3256 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3257 ofpraw_pull_assert(&rq_buf);
3258
3259 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3260 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3261 return reply;
fa37b408
BP
3262}
3263
efb80167 3264struct ofpbuf *
a0ae0b6e 3265ofputil_encode_barrier_request(enum ofp_version ofp_version)
efb80167 3266{
a0ae0b6e
SH
3267 enum ofpraw type;
3268
3269 switch (ofp_version) {
3270 case OFP12_VERSION:
3271 case OFP11_VERSION:
3272 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3273 break;
3274
3275 case OFP10_VERSION:
3276 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3277 break;
3278
3279 default:
3280 NOT_REACHED();
3281 }
3282
3283 return ofpraw_alloc(type, ofp_version, 0);
efb80167
BP
3284}
3285
7257b535
BP
3286const char *
3287ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3288{
3289 switch (flags & OFPC_FRAG_MASK) {
3290 case OFPC_FRAG_NORMAL: return "normal";
3291 case OFPC_FRAG_DROP: return "drop";
3292 case OFPC_FRAG_REASM: return "reassemble";
3293 case OFPC_FRAG_NX_MATCH: return "nx-match";
3294 }
3295
3296 NOT_REACHED();
3297}
3298
3299bool
3300ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3301{
3302 if (!strcasecmp(s, "normal")) {
3303 *flags = OFPC_FRAG_NORMAL;
3304 } else if (!strcasecmp(s, "drop")) {
3305 *flags = OFPC_FRAG_DROP;
3306 } else if (!strcasecmp(s, "reassemble")) {
3307 *flags = OFPC_FRAG_REASM;
3308 } else if (!strcasecmp(s, "nx-match")) {
3309 *flags = OFPC_FRAG_NX_MATCH;
3310 } else {
3311 return false;
3312 }
3313 return true;
3314}
3315
7b7503ea
BP
3316/* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3317 * port number and stores the latter in '*ofp10_port', for the purpose of
3318 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3319 * otherwise an OFPERR_* number.
3320 *
3321 * See the definition of OFP11_MAX for an explanation of the mapping. */
3322enum ofperr
3323ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3324{
3325 uint32_t ofp11_port_h = ntohl(ofp11_port);
3326
3327 if (ofp11_port_h < OFPP_MAX) {
3328 *ofp10_port = ofp11_port_h;
3329 return 0;
3330 } else if (ofp11_port_h >= OFPP11_MAX) {
3331 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3332 return 0;
3333 } else {
3334 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3335 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3336 ofp11_port_h, OFPP_MAX - 1,
3337 (uint32_t) OFPP11_MAX, UINT32_MAX);
3338 return OFPERR_OFPBAC_BAD_OUT_PORT;
3339 }
3340}
3341
3342/* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3343 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3344 *
3345 * See the definition of OFP11_MAX for an explanation of the mapping. */
3346ovs_be32
3347ofputil_port_to_ofp11(uint16_t ofp10_port)
3348{
3349 return htonl(ofp10_port < OFPP_MAX
3350 ? ofp10_port
3351 : ofp10_port + OFPP11_OFFSET);
3352}
3353
08f94c0e 3354/* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
c1c9c9c4 3355 * that the switch will never have more than 'max_ports' ports. Returns 0 if
90bf1e07
BP
3356 * 'port' is valid, otherwise an OpenFlow return code. */
3357enum ofperr
77410139 3358ofputil_check_output_port(uint16_t port, int max_ports)
fa37b408
BP
3359{
3360 switch (port) {
3361 case OFPP_IN_PORT:
3362 case OFPP_TABLE:
3363 case OFPP_NORMAL:
3364 case OFPP_FLOOD:
3365 case OFPP_ALL:
3366 case OFPP_CONTROLLER:
0d193212 3367 case OFPP_NONE:
fa37b408
BP
3368 case OFPP_LOCAL:
3369 return 0;
3370
3371 default:
c1c9c9c4 3372 if (port < max_ports) {
fa37b408
BP
3373 return 0;
3374 }
90bf1e07 3375 return OFPERR_OFPBAC_BAD_OUT_PORT;
fa37b408
BP
3376 }
3377}
3378
39dc9082
BP
3379#define OFPUTIL_NAMED_PORTS \
3380 OFPUTIL_NAMED_PORT(IN_PORT) \
3381 OFPUTIL_NAMED_PORT(TABLE) \
3382 OFPUTIL_NAMED_PORT(NORMAL) \
3383 OFPUTIL_NAMED_PORT(FLOOD) \
3384 OFPUTIL_NAMED_PORT(ALL) \
3385 OFPUTIL_NAMED_PORT(CONTROLLER) \
3386 OFPUTIL_NAMED_PORT(LOCAL) \
3387 OFPUTIL_NAMED_PORT(NONE)
3388
3389/* Checks whether 's' is the string representation of an OpenFlow port number,
3390 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
3391 * number in '*port' and returns true. Otherwise, returns false. */
3392bool
3393ofputil_port_from_string(const char *name, uint16_t *port)
3394{
3395 struct pair {
3396 const char *name;
3397 uint16_t value;
3398 };
3399 static const struct pair pairs[] = {
3400#define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3401 OFPUTIL_NAMED_PORTS
3402#undef OFPUTIL_NAMED_PORT
3403 };
3404 static const int n_pairs = ARRAY_SIZE(pairs);
3405 int i;
3406
3407 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
3408 *port = i;
3409 return true;
3410 }
3411
3412 for (i = 0; i < n_pairs; i++) {
3413 if (!strcasecmp(name, pairs[i].name)) {
3414 *port = pairs[i].value;
3415 return true;
3416 }
3417 }
3418 return false;
3419}
3420
3421/* Appends to 's' a string representation of the OpenFlow port number 'port'.
3422 * Most ports' string representation is just the port number, but for special
3423 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3424void
3425ofputil_format_port(uint16_t port, struct ds *s)
3426{
3427 const char *name;
3428
3429 switch (port) {
3430#define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3431 OFPUTIL_NAMED_PORTS
3432#undef OFPUTIL_NAMED_PORT
3433
3434 default:
3435 ds_put_format(s, "%"PRIu16, port);
3436 return;
3437 }
3438 ds_put_cstr(s, name);
3439}
3440
2be393ed
JP
3441/* Given a buffer 'b' that contains an array of OpenFlow ports of type
3442 * 'ofp_version', tries to pull the first element from the array. If
3443 * successful, initializes '*pp' with an abstract representation of the
3444 * port and returns 0. If no ports remain to be decoded, returns EOF.
3445 * On an error, returns a positive OFPERR_* value. */
3446int
2e3fa633 3447ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
2be393ed
JP
3448 struct ofputil_phy_port *pp)
3449{
2e3fa633
SH
3450 switch (ofp_version) {
3451 case OFP10_VERSION: {
2be393ed
JP
3452 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3453 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
2e3fa633
SH
3454 }
3455 case OFP11_VERSION:
3456 case OFP12_VERSION: {
2be393ed
JP
3457 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3458 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3459 }
2e3fa633
SH
3460 default:
3461 NOT_REACHED();
3462 }
2be393ed
JP
3463}
3464
3465/* Given a buffer 'b' that contains an array of OpenFlow ports of type
3466 * 'ofp_version', returns the number of elements. */
3467size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3468{
5b5a3a67 3469 return b->size / ofputil_get_phy_port_size(ofp_version);
2be393ed
JP
3470}
3471
e23ae585 3472/* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
08f94c0e 3473 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
e23ae585
BP
3474 * 'name' is not the name of any action.
3475 *
3476 * ofp-util.def lists the mapping from names to action. */
3477int
3478ofputil_action_code_from_name(const char *name)
3479{
3480 static const char *names[OFPUTIL_N_ACTIONS] = {
690a61c5 3481 NULL,
d01c980f
BP
3482#define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3483#define OFPAT11_ACTION(ENUM, STRUCT, NAME) NAME,
e23ae585
BP
3484#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3485#include "ofp-util.def"
3486 };
3487
3488 const char **p;
3489
3490 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3491 if (*p && !strcasecmp(name, *p)) {
3492 return p - names;
3493 }
3494 }
3495 return -1;
3496}
3497
93996add
BP
3498/* Appends an action of the type specified by 'code' to 'buf' and returns the
3499 * action. Initializes the parts of 'action' that identify it as having type
3500 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3501 * have variable length, the length used and cleared is that of struct
3502 * <STRUCT>. */
3503void *
3504ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3505{
3506 switch (code) {
690a61c5
BP
3507 case OFPUTIL_ACTION_INVALID:
3508 NOT_REACHED();
3509
08f94c0e 3510#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
93996add 3511 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
d01c980f 3512#define OFPAT11_ACTION OFPAT10_ACTION
93996add
BP
3513#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3514 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3515#include "ofp-util.def"
3516 }
3517 NOT_REACHED();
3518}
3519
08f94c0e 3520#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
93996add
BP
3521 void \
3522 ofputil_init_##ENUM(struct STRUCT *s) \
3523 { \
3524 memset(s, 0, sizeof *s); \
3525 s->type = htons(ENUM); \
3526 s->len = htons(sizeof *s); \
3527 } \
3528 \
3529 struct STRUCT * \
3530 ofputil_put_##ENUM(struct ofpbuf *buf) \
3531 { \
3532 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3533 ofputil_init_##ENUM(s); \
3534 return s; \
3535 }
d01c980f 3536#define OFPAT11_ACTION OFPAT10_ACTION
93996add
BP
3537#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3538 void \
3539 ofputil_init_##ENUM(struct STRUCT *s) \
3540 { \
3541 memset(s, 0, sizeof *s); \
08f94c0e 3542 s->type = htons(OFPAT10_VENDOR); \
93996add
BP
3543 s->len = htons(sizeof *s); \
3544 s->vendor = htonl(NX_VENDOR_ID); \
3545 s->subtype = htons(ENUM); \
3546 } \
3547 \
3548 struct STRUCT * \
3549 ofputil_put_##ENUM(struct ofpbuf *buf) \
3550 { \
3551 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3552 ofputil_init_##ENUM(s); \
3553 return s; \
3554 }
3555#include "ofp-util.def"
3556
3cbd9931
BP
3557static void
3558ofputil_normalize_rule__(struct cls_rule *rule, bool may_log)
b459a924
BP
3559{
3560 enum {
3561 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3562 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3563 MAY_NW_PROTO = 1 << 2, /* nw_proto */
a61680c6 3564 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
b459a924
BP
3565 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3566 MAY_ARP_THA = 1 << 5, /* arp_tha */
d78477ec 3567 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
b459a924
BP
3568 MAY_ND_TARGET = 1 << 7 /* nd_target */
3569 } may_match;
3570
3571 struct flow_wildcards wc;
3572
3573 /* Figure out what fields may be matched. */
3574 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
9e44d715 3575 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
b459a924
BP
3576 if (rule->flow.nw_proto == IPPROTO_TCP ||
3577 rule->flow.nw_proto == IPPROTO_UDP ||
3578 rule->flow.nw_proto == IPPROTO_ICMP) {
3579 may_match |= MAY_TP_ADDR;
3580 }
27527aa0 3581 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
d78477ec 3582 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
b459a924
BP
3583 if (rule->flow.nw_proto == IPPROTO_TCP ||
3584 rule->flow.nw_proto == IPPROTO_UDP) {
3585 may_match |= MAY_TP_ADDR;
3586 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3587 may_match |= MAY_TP_ADDR;
3588 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3589 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3590 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3591 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3592 }
3593 }
3594 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
27527aa0 3595 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
1c0b7503 3596 } else {
b459a924
BP
3597 may_match = 0;
3598 }
3599
3600 /* Clear the fields that may not be matched. */
3601 wc = rule->wc;
3602 if (!(may_match & MAY_NW_ADDR)) {
3603 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3604 }
3605 if (!(may_match & MAY_TP_ADDR)) {
73f33563 3606 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
b459a924
BP
3607 }
3608 if (!(may_match & MAY_NW_PROTO)) {
3609 wc.wildcards |= FWW_NW_PROTO;
3610 }
9e44d715 3611 if (!(may_match & MAY_IPVx)) {
2486e66a
JP
3612 wc.wildcards |= FWW_NW_DSCP;
3613 wc.wildcards |= FWW_NW_ECN;
a61680c6 3614 wc.wildcards |= FWW_NW_TTL;
b459a924
BP
3615 }
3616 if (!(may_match & MAY_ARP_SHA)) {
e878338b 3617 memset(wc.arp_sha_mask, 0, ETH_ADDR_LEN);
b459a924
BP
3618 }
3619 if (!(may_match & MAY_ARP_THA)) {
e878338b 3620 memset(wc.arp_tha_mask, 0, ETH_ADDR_LEN);
b459a924 3621 }
d78477ec 3622 if (!(may_match & MAY_IPV6)) {
b459a924 3623 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
32455024 3624 wc.ipv6_label_mask = htonl(0);
b459a924
BP
3625 }
3626 if (!(may_match & MAY_ND_TARGET)) {
47284b1f 3627 wc.nd_target_mask = in6addr_any;
b459a924
BP
3628 }
3629
3630 /* Log any changes. */
3631 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3cbd9931 3632 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
b459a924
BP
3633 char *pre = log ? cls_rule_to_string(rule) : NULL;
3634
3635 rule->wc = wc;
3636 cls_rule_zero_wildcarded_fields(rule);
3637
3638 if (log) {
3639 char *post = cls_rule_to_string(rule);
3640 VLOG_INFO("normalization changed ofp_match, details:");
3641 VLOG_INFO(" pre: %s", pre);
3642 VLOG_INFO("post: %s", post);
3643 free(pre);
3644 free(post);
3645 }
fa37b408 3646 }
3f09c339 3647}
26c112c2 3648
3cbd9931
BP
3649/* "Normalizes" the wildcards in 'rule'. That means:
3650 *
3651 * 1. If the type of level N is known, then only the valid fields for that
3652 * level may be specified. For example, ARP does not have a TOS field,
3653 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3654 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3655 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3656 * IPv4 flow.
3657 *
3658 * 2. If the type of level N is not known (or not understood by Open
3659 * vSwitch), then no fields at all for that level may be specified. For
3660 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3661 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3662 * SCTP flow.
3663 *
3664 * If this function changes 'rule', it logs a rate-limited informational
3665 * message. */
3666void
3667ofputil_normalize_rule(struct cls_rule *rule)
3668{
3669 ofputil_normalize_rule__(rule, true);
3670}
3671
3672/* Same as ofputil_normalize_rule() without the logging. Thus, this function
3673 * is suitable for a program's internal use, whereas ofputil_normalize_rule()
3674 * sense for use on flows received from elsewhere (so that a bug in the program
3675 * that sent them can be reported and corrected). */
3676void
3677ofputil_normalize_rule_quiet(struct cls_rule *rule)
3678{
3679 ofputil_normalize_rule__(rule, false);
3680}
3681
0ff22822
BP
3682/* Parses a key or a key-value pair from '*stringp'.
3683 *
3684 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3685 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3686 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3687 * are substrings of '*stringp' created by replacing some of its bytes by null
3688 * terminators. Returns true.
3689 *
3690 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3691 * NULL and returns false. */
3692bool
3693ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3694{
3695 char *pos, *key, *value;
3696 size_t key_len;
3697
3698 pos = *stringp;
3699 pos += strspn(pos, ", \t\r\n");
3700 if (*pos == '\0') {
3701 *keyp = *valuep = NULL;
3702 return false;
3703 }
3704
3705 key = pos;
3706 key_len = strcspn(pos, ":=(, \t\r\n");
3707 if (key[key_len] == ':' || key[key_len] == '=') {
3708 /* The value can be separated by a colon. */
3709 size_t value_len;
3710
3711 value = key + key_len + 1;
3712 value_len = strcspn(value, ", \t\r\n");
3713 pos = value + value_len + (value[value_len] != '\0');
3714 value[value_len] = '\0';
3715 } else if (key[key_len] == '(') {
3716 /* The value can be surrounded by balanced parentheses. The outermost
3717 * set of parentheses is removed. */
3718 int level = 1;
3719 size_t value_len;
3720
3721 value = key + key_len + 1;
3722 for (value_len = 0; level > 0; value_len++) {
3723 switch (value[value_len]) {
3724 case '\0':
33cadc50
BP
3725 level = 0;
3726 break;
0ff22822
BP
3727
3728 case '(':
3729 level++;
3730 break;
3731
3732 case ')':
3733 level--;
3734 break;
3735 }
3736 }
3737 value[value_len - 1] = '\0';
3738 pos = value + value_len;
3739 } else {
3740 /* There might be no value at all. */
3741 value = key + key_len; /* Will become the empty string below. */
3742 pos = key + key_len + (key[key_len] != '\0');
3743 }
3744 key[key_len] = '\0';
3745
3746 *stringp = pos;
3747 *keyp = key;
3748 *valuep = value;
3749 return true;
3750}