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