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