]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-util.c
datapath-windows: Fix connection state tracking in Conntrack
[mirror_ovs.git] / lib / ofp-util.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
24 #include <stdlib.h>
25 #include "bitmap.h"
26 #include "bundle.h"
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "learn.h"
30 #include "multipath.h"
31 #include "netdev.h"
32 #include "nx-match.h"
33 #include "id-pool.h"
34 #include "openflow/netronome-ext.h"
35 #include "openvswitch/dynamic-string.h"
36 #include "openvswitch/meta-flow.h"
37 #include "openvswitch/ofp-actions.h"
38 #include "openvswitch/ofp-errors.h"
39 #include "openvswitch/ofp-msgs.h"
40 #include "openvswitch/ofp-print.h"
41 #include "openvswitch/ofp-prop.h"
42 #include "openvswitch/ofp-util.h"
43 #include "openvswitch/ofpbuf.h"
44 #include "openvswitch/type-props.h"
45 #include "openvswitch/vlog.h"
46 #include "openflow/intel-ext.h"
47 #include "packets.h"
48 #include "pktbuf.h"
49 #include "random.h"
50 #include "tun-metadata.h"
51 #include "unaligned.h"
52 #include "util.h"
53 #include "uuid.h"
54
55 VLOG_DEFINE_THIS_MODULE(ofp_util);
56
57 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
58 * in the peer and so there's not much point in showing a lot of them. */
59 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
60
61 static enum ofputil_table_vacancy ofputil_decode_table_vacancy(
62 ovs_be32 config, enum ofp_version);
63 static enum ofputil_table_eviction ofputil_decode_table_eviction(
64 ovs_be32 config, enum ofp_version);
65 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
66 enum ofputil_table_eviction,
67 enum ofputil_table_vacancy,
68 enum ofp_version);
69
70 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
71 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
72 * is wildcarded.
73 *
74 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
75 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
76 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
77 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
78 * wildcarded. */
79 ovs_be32
80 ofputil_wcbits_to_netmask(int wcbits)
81 {
82 wcbits &= 0x3f;
83 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
84 }
85
86 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
87 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
88 * between 0 and 32 inclusive.
89 *
90 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
91 * still be in the valid range but isn't otherwise meaningful. */
92 int
93 ofputil_netmask_to_wcbits(ovs_be32 netmask)
94 {
95 return 32 - ip_count_cidr_bits(netmask);
96 }
97
98 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
99 * flow_wildcards in 'wc' for use in struct match. It is the caller's
100 * responsibility to handle the special case where the flow match's dl_vlan is
101 * set to OFP_VLAN_NONE. */
102 void
103 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
104 {
105 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 36);
106
107 /* Initialize most of wc. */
108 flow_wildcards_init_catchall(wc);
109
110 if (!(ofpfw & OFPFW10_IN_PORT)) {
111 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
112 }
113
114 if (!(ofpfw & OFPFW10_NW_TOS)) {
115 wc->masks.nw_tos |= IP_DSCP_MASK;
116 }
117
118 if (!(ofpfw & OFPFW10_NW_PROTO)) {
119 wc->masks.nw_proto = UINT8_MAX;
120 }
121 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
122 >> OFPFW10_NW_SRC_SHIFT);
123 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
124 >> OFPFW10_NW_DST_SHIFT);
125
126 if (!(ofpfw & OFPFW10_TP_SRC)) {
127 wc->masks.tp_src = OVS_BE16_MAX;
128 }
129 if (!(ofpfw & OFPFW10_TP_DST)) {
130 wc->masks.tp_dst = OVS_BE16_MAX;
131 }
132
133 if (!(ofpfw & OFPFW10_DL_SRC)) {
134 WC_MASK_FIELD(wc, dl_src);
135 }
136 if (!(ofpfw & OFPFW10_DL_DST)) {
137 WC_MASK_FIELD(wc, dl_dst);
138 }
139 if (!(ofpfw & OFPFW10_DL_TYPE)) {
140 wc->masks.dl_type = OVS_BE16_MAX;
141 }
142
143 /* VLAN TCI mask. */
144 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
145 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
146 }
147 if (!(ofpfw & OFPFW10_DL_VLAN)) {
148 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
149 }
150 }
151
152 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
153 void
154 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
155 struct match *match)
156 {
157 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
158
159 /* Initialize match->wc. */
160 memset(&match->flow, 0, sizeof match->flow);
161 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
162
163 /* Initialize most of match->flow. */
164 match->flow.nw_src = ofmatch->nw_src;
165 match->flow.nw_dst = ofmatch->nw_dst;
166 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
167 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
168 match->flow.tp_src = ofmatch->tp_src;
169 match->flow.tp_dst = ofmatch->tp_dst;
170 match->flow.dl_src = ofmatch->dl_src;
171 match->flow.dl_dst = ofmatch->dl_dst;
172 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
173 match->flow.nw_proto = ofmatch->nw_proto;
174
175 /* Translate VLANs. */
176 if (!(ofpfw & OFPFW10_DL_VLAN) &&
177 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
178 /* Match only packets without 802.1Q header.
179 *
180 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
181 *
182 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
183 * because we can't have a specific PCP without an 802.1Q header.
184 * However, older versions of OVS treated this as matching packets
185 * withut an 802.1Q header, so we do here too. */
186 match->flow.vlan_tci = htons(0);
187 match->wc.masks.vlan_tci = htons(0xffff);
188 } else {
189 ovs_be16 vid, pcp, tci;
190 uint16_t hpcp;
191
192 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
193 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
194 pcp = htons(hpcp);
195 tci = vid | pcp | htons(VLAN_CFI);
196 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
197 }
198
199 /* Clean up. */
200 match_zero_wildcarded_fields(match);
201 }
202
203 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
204 void
205 ofputil_match_to_ofp10_match(const struct match *match,
206 struct ofp10_match *ofmatch)
207 {
208 const struct flow_wildcards *wc = &match->wc;
209 uint32_t ofpfw;
210
211 /* Figure out most OpenFlow wildcards. */
212 ofpfw = 0;
213 if (!wc->masks.in_port.ofp_port) {
214 ofpfw |= OFPFW10_IN_PORT;
215 }
216 if (!wc->masks.dl_type) {
217 ofpfw |= OFPFW10_DL_TYPE;
218 }
219 if (!wc->masks.nw_proto) {
220 ofpfw |= OFPFW10_NW_PROTO;
221 }
222 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
223 << OFPFW10_NW_SRC_SHIFT);
224 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
225 << OFPFW10_NW_DST_SHIFT);
226 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
227 ofpfw |= OFPFW10_NW_TOS;
228 }
229 if (!wc->masks.tp_src) {
230 ofpfw |= OFPFW10_TP_SRC;
231 }
232 if (!wc->masks.tp_dst) {
233 ofpfw |= OFPFW10_TP_DST;
234 }
235 if (eth_addr_is_zero(wc->masks.dl_src)) {
236 ofpfw |= OFPFW10_DL_SRC;
237 }
238 if (eth_addr_is_zero(wc->masks.dl_dst)) {
239 ofpfw |= OFPFW10_DL_DST;
240 }
241
242 /* Translate VLANs. */
243 ofmatch->dl_vlan = htons(0);
244 ofmatch->dl_vlan_pcp = 0;
245 if (match->wc.masks.vlan_tci == htons(0)) {
246 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
247 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
248 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
249 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
250 } else {
251 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
252 ofpfw |= OFPFW10_DL_VLAN;
253 } else {
254 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
255 }
256
257 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
258 ofpfw |= OFPFW10_DL_VLAN_PCP;
259 } else {
260 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
261 }
262 }
263
264 /* Compose most of the match structure. */
265 ofmatch->wildcards = htonl(ofpfw);
266 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
267 ofmatch->dl_src = match->flow.dl_src;
268 ofmatch->dl_dst = match->flow.dl_dst;
269 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
270 ofmatch->nw_src = match->flow.nw_src;
271 ofmatch->nw_dst = match->flow.nw_dst;
272 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
273 ofmatch->nw_proto = match->flow.nw_proto;
274 ofmatch->tp_src = match->flow.tp_src;
275 ofmatch->tp_dst = match->flow.tp_dst;
276 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
277 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
278 }
279
280 enum ofperr
281 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
282 uint16_t *padded_match_len)
283 {
284 struct ofp11_match_header *omh = buf->data;
285 uint16_t match_len;
286
287 if (buf->size < sizeof *omh) {
288 return OFPERR_OFPBMC_BAD_LEN;
289 }
290
291 match_len = ntohs(omh->length);
292
293 switch (ntohs(omh->type)) {
294 case OFPMT_STANDARD: {
295 struct ofp11_match *om;
296
297 if (match_len != sizeof *om || buf->size < sizeof *om) {
298 return OFPERR_OFPBMC_BAD_LEN;
299 }
300 om = ofpbuf_pull(buf, sizeof *om);
301 if (padded_match_len) {
302 *padded_match_len = match_len;
303 }
304 return ofputil_match_from_ofp11_match(om, match);
305 }
306
307 case OFPMT_OXM:
308 if (padded_match_len) {
309 *padded_match_len = ROUND_UP(match_len, 8);
310 }
311 return oxm_pull_match(buf, match);
312
313 default:
314 return OFPERR_OFPBMC_BAD_TYPE;
315 }
316 }
317
318 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
319 * Returns 0 if successful, otherwise an OFPERR_* value. */
320 enum ofperr
321 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
322 struct match *match)
323 {
324 uint16_t wc = ntohl(ofmatch->wildcards);
325 bool ipv4, arp, rarp;
326
327 match_init_catchall(match);
328
329 if (!(wc & OFPFW11_IN_PORT)) {
330 ofp_port_t ofp_port;
331 enum ofperr error;
332
333 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
334 if (error) {
335 return OFPERR_OFPBMC_BAD_VALUE;
336 }
337 match_set_in_port(match, ofp_port);
338 }
339
340 match_set_dl_src_masked(match, ofmatch->dl_src,
341 eth_addr_invert(ofmatch->dl_src_mask));
342 match_set_dl_dst_masked(match, ofmatch->dl_dst,
343 eth_addr_invert(ofmatch->dl_dst_mask));
344
345 if (!(wc & OFPFW11_DL_VLAN)) {
346 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
347 /* Match only packets without a VLAN tag. */
348 match->flow.vlan_tci = htons(0);
349 match->wc.masks.vlan_tci = OVS_BE16_MAX;
350 } else {
351 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
352 /* Match any packet with a VLAN tag regardless of VID. */
353 match->flow.vlan_tci = htons(VLAN_CFI);
354 match->wc.masks.vlan_tci = htons(VLAN_CFI);
355 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
356 /* Match only packets with the specified VLAN VID. */
357 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
358 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
359 } else {
360 /* Invalid VID. */
361 return OFPERR_OFPBMC_BAD_VALUE;
362 }
363
364 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
365 if (ofmatch->dl_vlan_pcp <= 7) {
366 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
367 << VLAN_PCP_SHIFT);
368 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
369 } else {
370 /* Invalid PCP. */
371 return OFPERR_OFPBMC_BAD_VALUE;
372 }
373 }
374 }
375 }
376
377 if (!(wc & OFPFW11_DL_TYPE)) {
378 match_set_dl_type(match,
379 ofputil_dl_type_from_openflow(ofmatch->dl_type));
380 }
381
382 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
383 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
384 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
385
386 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
387 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
388 /* Invalid TOS. */
389 return OFPERR_OFPBMC_BAD_VALUE;
390 }
391
392 match_set_nw_dscp(match, ofmatch->nw_tos);
393 }
394
395 if (ipv4 || arp || rarp) {
396 if (!(wc & OFPFW11_NW_PROTO)) {
397 match_set_nw_proto(match, ofmatch->nw_proto);
398 }
399 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
400 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
401 }
402
403 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
404 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
405 switch (match->flow.nw_proto) {
406 case IPPROTO_ICMP:
407 /* "A.2.3 Flow Match Structures" in OF1.1 says:
408 *
409 * The tp_src and tp_dst fields will be ignored unless the
410 * network protocol specified is as TCP, UDP or SCTP.
411 *
412 * but I'm pretty sure we should support ICMP too, otherwise
413 * that's a regression from OF1.0. */
414 if (!(wc & OFPFW11_TP_SRC)) {
415 uint16_t icmp_type = ntohs(ofmatch->tp_src);
416 if (icmp_type < 0x100) {
417 match_set_icmp_type(match, icmp_type);
418 } else {
419 return OFPERR_OFPBMC_BAD_FIELD;
420 }
421 }
422 if (!(wc & OFPFW11_TP_DST)) {
423 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
424 if (icmp_code < 0x100) {
425 match_set_icmp_code(match, icmp_code);
426 } else {
427 return OFPERR_OFPBMC_BAD_FIELD;
428 }
429 }
430 break;
431
432 case IPPROTO_TCP:
433 case IPPROTO_UDP:
434 case IPPROTO_SCTP:
435 if (!(wc & (OFPFW11_TP_SRC))) {
436 match_set_tp_src(match, ofmatch->tp_src);
437 }
438 if (!(wc & (OFPFW11_TP_DST))) {
439 match_set_tp_dst(match, ofmatch->tp_dst);
440 }
441 break;
442
443 default:
444 /* OF1.1 says explicitly to ignore this. */
445 break;
446 }
447 }
448
449 if (eth_type_mpls(match->flow.dl_type)) {
450 if (!(wc & OFPFW11_MPLS_LABEL)) {
451 match_set_mpls_label(match, 0, ofmatch->mpls_label);
452 }
453 if (!(wc & OFPFW11_MPLS_TC)) {
454 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
455 }
456 }
457
458 match_set_metadata_masked(match, ofmatch->metadata,
459 ~ofmatch->metadata_mask);
460
461 return 0;
462 }
463
464 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
465 void
466 ofputil_match_to_ofp11_match(const struct match *match,
467 struct ofp11_match *ofmatch)
468 {
469 uint32_t wc = 0;
470
471 memset(ofmatch, 0, sizeof *ofmatch);
472 ofmatch->omh.type = htons(OFPMT_STANDARD);
473 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
474
475 if (!match->wc.masks.in_port.ofp_port) {
476 wc |= OFPFW11_IN_PORT;
477 } else {
478 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
479 }
480
481 ofmatch->dl_src = match->flow.dl_src;
482 ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
483 ofmatch->dl_dst = match->flow.dl_dst;
484 ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
485
486 if (match->wc.masks.vlan_tci == htons(0)) {
487 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
488 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
489 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
490 ofmatch->dl_vlan = htons(OFPVID11_NONE);
491 wc |= OFPFW11_DL_VLAN_PCP;
492 } else {
493 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
494 ofmatch->dl_vlan = htons(OFPVID11_ANY);
495 } else {
496 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
497 }
498
499 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
500 wc |= OFPFW11_DL_VLAN_PCP;
501 } else {
502 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
503 }
504 }
505
506 if (!match->wc.masks.dl_type) {
507 wc |= OFPFW11_DL_TYPE;
508 } else {
509 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
510 }
511
512 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
513 wc |= OFPFW11_NW_TOS;
514 } else {
515 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
516 }
517
518 if (!match->wc.masks.nw_proto) {
519 wc |= OFPFW11_NW_PROTO;
520 } else {
521 ofmatch->nw_proto = match->flow.nw_proto;
522 }
523
524 ofmatch->nw_src = match->flow.nw_src;
525 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
526 ofmatch->nw_dst = match->flow.nw_dst;
527 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
528
529 if (!match->wc.masks.tp_src) {
530 wc |= OFPFW11_TP_SRC;
531 } else {
532 ofmatch->tp_src = match->flow.tp_src;
533 }
534
535 if (!match->wc.masks.tp_dst) {
536 wc |= OFPFW11_TP_DST;
537 } else {
538 ofmatch->tp_dst = match->flow.tp_dst;
539 }
540
541 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
542 wc |= OFPFW11_MPLS_LABEL;
543 } else {
544 ofmatch->mpls_label = htonl(mpls_lse_to_label(
545 match->flow.mpls_lse[0]));
546 }
547
548 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
549 wc |= OFPFW11_MPLS_TC;
550 } else {
551 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
552 }
553
554 ofmatch->metadata = match->flow.metadata;
555 ofmatch->metadata_mask = ~match->wc.masks.metadata;
556
557 ofmatch->wildcards = htonl(wc);
558 }
559
560 /* Returns the "typical" length of a match for 'protocol', for use in
561 * estimating space to preallocate. */
562 int
563 ofputil_match_typical_len(enum ofputil_protocol protocol)
564 {
565 switch (protocol) {
566 case OFPUTIL_P_OF10_STD:
567 case OFPUTIL_P_OF10_STD_TID:
568 return sizeof(struct ofp10_match);
569
570 case OFPUTIL_P_OF10_NXM:
571 case OFPUTIL_P_OF10_NXM_TID:
572 return NXM_TYPICAL_LEN;
573
574 case OFPUTIL_P_OF11_STD:
575 return sizeof(struct ofp11_match);
576
577 case OFPUTIL_P_OF12_OXM:
578 case OFPUTIL_P_OF13_OXM:
579 case OFPUTIL_P_OF14_OXM:
580 case OFPUTIL_P_OF15_OXM:
581 case OFPUTIL_P_OF16_OXM:
582 return NXM_TYPICAL_LEN;
583
584 default:
585 OVS_NOT_REACHED();
586 }
587 }
588
589 /* Appends to 'b' an struct ofp11_match_header followed by a match that
590 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
591 * data appended out to a multiple of 8. 'protocol' must be one that is usable
592 * in OpenFlow 1.1 or later.
593 *
594 * This function can cause 'b''s data to be reallocated.
595 *
596 * Returns the number of bytes appended to 'b', excluding the padding. Never
597 * returns zero. */
598 int
599 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
600 enum ofputil_protocol protocol)
601 {
602 switch (protocol) {
603 case OFPUTIL_P_OF10_STD:
604 case OFPUTIL_P_OF10_STD_TID:
605 case OFPUTIL_P_OF10_NXM:
606 case OFPUTIL_P_OF10_NXM_TID:
607 OVS_NOT_REACHED();
608
609 case OFPUTIL_P_OF11_STD: {
610 struct ofp11_match *om;
611
612 /* Make sure that no padding is needed. */
613 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
614
615 om = ofpbuf_put_uninit(b, sizeof *om);
616 ofputil_match_to_ofp11_match(match, om);
617 return sizeof *om;
618 }
619
620 case OFPUTIL_P_OF12_OXM:
621 case OFPUTIL_P_OF13_OXM:
622 case OFPUTIL_P_OF14_OXM:
623 case OFPUTIL_P_OF15_OXM:
624 case OFPUTIL_P_OF16_OXM:
625 return oxm_put_match(b, match,
626 ofputil_protocol_to_ofp_version(protocol));
627 }
628
629 OVS_NOT_REACHED();
630 }
631
632 /* Given a 'dl_type' value in the format used in struct flow, returns the
633 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
634 * structure. */
635 ovs_be16
636 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
637 {
638 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
639 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
640 : flow_dl_type);
641 }
642
643 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
644 * structure, returns the corresponding 'dl_type' value for use in struct
645 * flow. */
646 ovs_be16
647 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
648 {
649 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
650 ? htons(FLOW_DL_TYPE_NONE)
651 : ofp_dl_type);
652 }
653 \f
654 /* Protocols. */
655
656 struct proto_abbrev {
657 enum ofputil_protocol protocol;
658 const char *name;
659 };
660
661 /* Most users really don't care about some of the differences between
662 * protocols. These abbreviations help with that. */
663 static const struct proto_abbrev proto_abbrevs[] = {
664 { OFPUTIL_P_ANY, "any" },
665 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
666 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
667 { OFPUTIL_P_ANY_OXM, "OXM" },
668 };
669 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
670
671 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
672 OFPUTIL_P_OF16_OXM,
673 OFPUTIL_P_OF15_OXM,
674 OFPUTIL_P_OF14_OXM,
675 OFPUTIL_P_OF13_OXM,
676 OFPUTIL_P_OF12_OXM,
677 OFPUTIL_P_OF11_STD,
678 OFPUTIL_P_OF10_NXM,
679 OFPUTIL_P_OF10_STD,
680 };
681 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
682
683 /* Returns the set of ofputil_protocols that are supported with the given
684 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
685 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
686 * if 'version' is not supported or outside the valid range. */
687 enum ofputil_protocol
688 ofputil_protocols_from_ofp_version(enum ofp_version version)
689 {
690 switch (version) {
691 case OFP10_VERSION:
692 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
693 case OFP11_VERSION:
694 return OFPUTIL_P_OF11_STD;
695 case OFP12_VERSION:
696 return OFPUTIL_P_OF12_OXM;
697 case OFP13_VERSION:
698 return OFPUTIL_P_OF13_OXM;
699 case OFP14_VERSION:
700 return OFPUTIL_P_OF14_OXM;
701 case OFP15_VERSION:
702 return OFPUTIL_P_OF15_OXM;
703 case OFP16_VERSION:
704 return OFPUTIL_P_OF16_OXM;
705 default:
706 return 0;
707 }
708 }
709
710 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
711 * connection that has negotiated the given 'version'. 'version' should
712 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
713 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
714 * outside the valid range. */
715 enum ofputil_protocol
716 ofputil_protocol_from_ofp_version(enum ofp_version version)
717 {
718 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
719 }
720
721 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
722 * etc.) that corresponds to 'protocol'. */
723 enum ofp_version
724 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
725 {
726 switch (protocol) {
727 case OFPUTIL_P_OF10_STD:
728 case OFPUTIL_P_OF10_STD_TID:
729 case OFPUTIL_P_OF10_NXM:
730 case OFPUTIL_P_OF10_NXM_TID:
731 return OFP10_VERSION;
732 case OFPUTIL_P_OF11_STD:
733 return OFP11_VERSION;
734 case OFPUTIL_P_OF12_OXM:
735 return OFP12_VERSION;
736 case OFPUTIL_P_OF13_OXM:
737 return OFP13_VERSION;
738 case OFPUTIL_P_OF14_OXM:
739 return OFP14_VERSION;
740 case OFPUTIL_P_OF15_OXM:
741 return OFP15_VERSION;
742 case OFPUTIL_P_OF16_OXM:
743 return OFP16_VERSION;
744 }
745
746 OVS_NOT_REACHED();
747 }
748
749 /* Returns a bitmap of OpenFlow versions that are supported by at
750 * least one of the 'protocols'. */
751 uint32_t
752 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
753 {
754 uint32_t bitmap = 0;
755
756 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
757 enum ofputil_protocol protocol = rightmost_1bit(protocols);
758
759 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
760 }
761
762 return bitmap;
763 }
764
765 /* Returns the set of protocols that are supported on top of the
766 * OpenFlow versions included in 'bitmap'. */
767 enum ofputil_protocol
768 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
769 {
770 enum ofputil_protocol protocols = 0;
771
772 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
773 enum ofp_version version = rightmost_1bit_idx(bitmap);
774
775 protocols |= ofputil_protocols_from_ofp_version(version);
776 }
777
778 return protocols;
779 }
780
781 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
782 * otherwise. */
783 bool
784 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
785 {
786 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
787 }
788
789 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
790 * extension turned on or off if 'enable' is true or false, respectively.
791 *
792 * This extension is only useful for protocols whose "standard" version does
793 * not allow specific tables to be modified. In particular, this is true of
794 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
795 * specifies a table ID and so there is no need for such an extension. When
796 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
797 * extension, this function just returns its 'protocol' argument unchanged
798 * regardless of the value of 'enable'. */
799 enum ofputil_protocol
800 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
801 {
802 switch (protocol) {
803 case OFPUTIL_P_OF10_STD:
804 case OFPUTIL_P_OF10_STD_TID:
805 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
806
807 case OFPUTIL_P_OF10_NXM:
808 case OFPUTIL_P_OF10_NXM_TID:
809 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
810
811 case OFPUTIL_P_OF11_STD:
812 return OFPUTIL_P_OF11_STD;
813
814 case OFPUTIL_P_OF12_OXM:
815 return OFPUTIL_P_OF12_OXM;
816
817 case OFPUTIL_P_OF13_OXM:
818 return OFPUTIL_P_OF13_OXM;
819
820 case OFPUTIL_P_OF14_OXM:
821 return OFPUTIL_P_OF14_OXM;
822
823 case OFPUTIL_P_OF15_OXM:
824 return OFPUTIL_P_OF15_OXM;
825
826 case OFPUTIL_P_OF16_OXM:
827 return OFPUTIL_P_OF16_OXM;
828
829 default:
830 OVS_NOT_REACHED();
831 }
832 }
833
834 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
835 * some extension to a standard protocol version, the return value is the
836 * standard version of that protocol without any extension. If 'protocol' is a
837 * standard protocol version, returns 'protocol' unchanged. */
838 enum ofputil_protocol
839 ofputil_protocol_to_base(enum ofputil_protocol protocol)
840 {
841 return ofputil_protocol_set_tid(protocol, false);
842 }
843
844 /* Returns 'new_base' with any extensions taken from 'cur'. */
845 enum ofputil_protocol
846 ofputil_protocol_set_base(enum ofputil_protocol cur,
847 enum ofputil_protocol new_base)
848 {
849 bool tid = (cur & OFPUTIL_P_TID) != 0;
850
851 switch (new_base) {
852 case OFPUTIL_P_OF10_STD:
853 case OFPUTIL_P_OF10_STD_TID:
854 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
855
856 case OFPUTIL_P_OF10_NXM:
857 case OFPUTIL_P_OF10_NXM_TID:
858 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
859
860 case OFPUTIL_P_OF11_STD:
861 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
862
863 case OFPUTIL_P_OF12_OXM:
864 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
865
866 case OFPUTIL_P_OF13_OXM:
867 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
868
869 case OFPUTIL_P_OF14_OXM:
870 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
871
872 case OFPUTIL_P_OF15_OXM:
873 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
874
875 case OFPUTIL_P_OF16_OXM:
876 return ofputil_protocol_set_tid(OFPUTIL_P_OF16_OXM, tid);
877
878 default:
879 OVS_NOT_REACHED();
880 }
881 }
882
883 /* Returns a string form of 'protocol', if a simple form exists (that is, if
884 * 'protocol' is either a single protocol or it is a combination of protocols
885 * that have a single abbreviation). Otherwise, returns NULL. */
886 const char *
887 ofputil_protocol_to_string(enum ofputil_protocol protocol)
888 {
889 const struct proto_abbrev *p;
890
891 /* Use a "switch" statement for single-bit names so that we get a compiler
892 * warning if we forget any. */
893 switch (protocol) {
894 case OFPUTIL_P_OF10_NXM:
895 return "NXM-table_id";
896
897 case OFPUTIL_P_OF10_NXM_TID:
898 return "NXM+table_id";
899
900 case OFPUTIL_P_OF10_STD:
901 return "OpenFlow10-table_id";
902
903 case OFPUTIL_P_OF10_STD_TID:
904 return "OpenFlow10+table_id";
905
906 case OFPUTIL_P_OF11_STD:
907 return "OpenFlow11";
908
909 case OFPUTIL_P_OF12_OXM:
910 return "OXM-OpenFlow12";
911
912 case OFPUTIL_P_OF13_OXM:
913 return "OXM-OpenFlow13";
914
915 case OFPUTIL_P_OF14_OXM:
916 return "OXM-OpenFlow14";
917
918 case OFPUTIL_P_OF15_OXM:
919 return "OXM-OpenFlow15";
920
921 case OFPUTIL_P_OF16_OXM:
922 return "OXM-OpenFlow16";
923 }
924
925 /* Check abbreviations. */
926 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
927 if (protocol == p->protocol) {
928 return p->name;
929 }
930 }
931
932 return NULL;
933 }
934
935 /* Returns a string that represents 'protocols'. The return value might be a
936 * comma-separated list if 'protocols' doesn't have a simple name. The return
937 * value is "none" if 'protocols' is 0.
938 *
939 * The caller must free the returned string (with free()). */
940 char *
941 ofputil_protocols_to_string(enum ofputil_protocol protocols)
942 {
943 struct ds s;
944
945 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
946 if (protocols == 0) {
947 return xstrdup("none");
948 }
949
950 ds_init(&s);
951 while (protocols) {
952 const struct proto_abbrev *p;
953 int i;
954
955 if (s.length) {
956 ds_put_char(&s, ',');
957 }
958
959 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
960 if ((protocols & p->protocol) == p->protocol) {
961 ds_put_cstr(&s, p->name);
962 protocols &= ~p->protocol;
963 goto match;
964 }
965 }
966
967 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
968 enum ofputil_protocol bit = 1u << i;
969
970 if (protocols & bit) {
971 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
972 protocols &= ~bit;
973 goto match;
974 }
975 }
976 OVS_NOT_REACHED();
977
978 match: ;
979 }
980 return ds_steal_cstr(&s);
981 }
982
983 static enum ofputil_protocol
984 ofputil_protocol_from_string__(const char *s, size_t n)
985 {
986 const struct proto_abbrev *p;
987 int i;
988
989 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
990 enum ofputil_protocol bit = 1u << i;
991 const char *name = ofputil_protocol_to_string(bit);
992
993 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
994 return bit;
995 }
996 }
997
998 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
999 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1000 return p->protocol;
1001 }
1002 }
1003
1004 return 0;
1005 }
1006
1007 /* Returns the nonempty set of protocols represented by 's', which can be a
1008 * single protocol name or abbreviation or a comma-separated list of them.
1009 *
1010 * Aborts the program with an error message if 's' is invalid. */
1011 enum ofputil_protocol
1012 ofputil_protocols_from_string(const char *s)
1013 {
1014 const char *orig_s = s;
1015 enum ofputil_protocol protocols;
1016
1017 protocols = 0;
1018 while (*s) {
1019 enum ofputil_protocol p;
1020 size_t n;
1021
1022 n = strcspn(s, ",");
1023 if (n == 0) {
1024 s++;
1025 continue;
1026 }
1027
1028 p = ofputil_protocol_from_string__(s, n);
1029 if (!p) {
1030 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1031 }
1032 protocols |= p;
1033
1034 s += n;
1035 }
1036
1037 if (!protocols) {
1038 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1039 }
1040 return protocols;
1041 }
1042
1043 enum ofp_version
1044 ofputil_version_from_string(const char *s)
1045 {
1046 if (!strcasecmp(s, "OpenFlow10")) {
1047 return OFP10_VERSION;
1048 }
1049 if (!strcasecmp(s, "OpenFlow11")) {
1050 return OFP11_VERSION;
1051 }
1052 if (!strcasecmp(s, "OpenFlow12")) {
1053 return OFP12_VERSION;
1054 }
1055 if (!strcasecmp(s, "OpenFlow13")) {
1056 return OFP13_VERSION;
1057 }
1058 if (!strcasecmp(s, "OpenFlow14")) {
1059 return OFP14_VERSION;
1060 }
1061 if (!strcasecmp(s, "OpenFlow15")) {
1062 return OFP15_VERSION;
1063 }
1064 if (!strcasecmp(s, "OpenFlow16")) {
1065 return OFP16_VERSION;
1066 }
1067 return 0;
1068 }
1069
1070 static bool
1071 is_delimiter(unsigned char c)
1072 {
1073 return isspace(c) || c == ',';
1074 }
1075
1076 uint32_t
1077 ofputil_versions_from_string(const char *s)
1078 {
1079 size_t i = 0;
1080 uint32_t bitmap = 0;
1081
1082 while (s[i]) {
1083 size_t j;
1084 int version;
1085 char *key;
1086
1087 if (is_delimiter(s[i])) {
1088 i++;
1089 continue;
1090 }
1091 j = 0;
1092 while (s[i + j] && !is_delimiter(s[i + j])) {
1093 j++;
1094 }
1095 key = xmemdup0(s + i, j);
1096 version = ofputil_version_from_string(key);
1097 if (!version) {
1098 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1099 }
1100 free(key);
1101 bitmap |= 1u << version;
1102 i += j;
1103 }
1104
1105 return bitmap;
1106 }
1107
1108 uint32_t
1109 ofputil_versions_from_strings(char ** const s, size_t count)
1110 {
1111 uint32_t bitmap = 0;
1112
1113 while (count--) {
1114 int version = ofputil_version_from_string(s[count]);
1115 if (!version) {
1116 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1117 } else {
1118 bitmap |= 1u << version;
1119 }
1120 }
1121
1122 return bitmap;
1123 }
1124
1125 const char *
1126 ofputil_version_to_string(enum ofp_version ofp_version)
1127 {
1128 switch (ofp_version) {
1129 case OFP10_VERSION:
1130 return "OpenFlow10";
1131 case OFP11_VERSION:
1132 return "OpenFlow11";
1133 case OFP12_VERSION:
1134 return "OpenFlow12";
1135 case OFP13_VERSION:
1136 return "OpenFlow13";
1137 case OFP14_VERSION:
1138 return "OpenFlow14";
1139 case OFP15_VERSION:
1140 return "OpenFlow15";
1141 case OFP16_VERSION:
1142 return "OpenFlow16";
1143 default:
1144 OVS_NOT_REACHED();
1145 }
1146 }
1147
1148 bool
1149 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1150 {
1151 switch (packet_in_format) {
1152 case NXPIF_STANDARD:
1153 case NXPIF_NXT_PACKET_IN:
1154 case NXPIF_NXT_PACKET_IN2:
1155 return true;
1156 }
1157
1158 return false;
1159 }
1160
1161 const char *
1162 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1163 {
1164 switch (packet_in_format) {
1165 case NXPIF_STANDARD:
1166 return "standard";
1167 case NXPIF_NXT_PACKET_IN:
1168 return "nxt_packet_in";
1169 case NXPIF_NXT_PACKET_IN2:
1170 return "nxt_packet_in2";
1171 default:
1172 OVS_NOT_REACHED();
1173 }
1174 }
1175
1176 int
1177 ofputil_packet_in_format_from_string(const char *s)
1178 {
1179 return (!strcmp(s, "standard") || !strcmp(s, "openflow10")
1180 ? NXPIF_STANDARD
1181 : !strcmp(s, "nxt_packet_in") || !strcmp(s, "nxm")
1182 ? NXPIF_NXT_PACKET_IN
1183 : !strcmp(s, "nxt_packet_in2")
1184 ? NXPIF_NXT_PACKET_IN2
1185 : -1);
1186 }
1187
1188 void
1189 ofputil_format_version(struct ds *msg, enum ofp_version version)
1190 {
1191 ds_put_format(msg, "0x%02x", version);
1192 }
1193
1194 void
1195 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1196 {
1197 ds_put_cstr(msg, ofputil_version_to_string(version));
1198 }
1199
1200 static void
1201 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1202 void (*format_version)(struct ds *msg,
1203 enum ofp_version))
1204 {
1205 while (bitmap) {
1206 format_version(msg, raw_ctz(bitmap));
1207 bitmap = zero_rightmost_1bit(bitmap);
1208 if (bitmap) {
1209 ds_put_cstr(msg, ", ");
1210 }
1211 }
1212 }
1213
1214 void
1215 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1216 {
1217 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1218 }
1219
1220 void
1221 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1222 {
1223 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1224 }
1225
1226 static bool
1227 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1228 uint32_t *allowed_versionsp)
1229 {
1230 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1231 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1232 uint32_t allowed_versions;
1233
1234 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1235 return false;
1236 }
1237
1238 /* Only use the first 32-bit element of the bitmap as that is all the
1239 * current implementation supports. Subsequent elements are ignored which
1240 * should have no effect on session negotiation until Open vSwitch supports
1241 * wire-protocol versions greater than 31.
1242 */
1243 allowed_versions = ntohl(bitmap[0]);
1244
1245 if (allowed_versions & 1) {
1246 /* There's no OpenFlow version 0. */
1247 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1248 "version 0x00");
1249 allowed_versions &= ~1u;
1250 }
1251
1252 if (!allowed_versions) {
1253 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1254 "version (between 0x01 and 0x1f)");
1255 return false;
1256 }
1257
1258 *allowed_versionsp = allowed_versions;
1259 return true;
1260 }
1261
1262 static uint32_t
1263 version_bitmap_from_version(uint8_t ofp_version)
1264 {
1265 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1266 }
1267
1268 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1269 * the set of OpenFlow versions for which 'oh' announces support.
1270 *
1271 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1272 * successful, and thus '*allowed_versions' is always initialized. However, it
1273 * returns false if 'oh' contains some data that could not be fully understood,
1274 * true if 'oh' was completely parsed. */
1275 bool
1276 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1277 {
1278 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
1279 ofpbuf_pull(&msg, sizeof *oh);
1280
1281 *allowed_versions = version_bitmap_from_version(oh->version);
1282
1283 bool ok = true;
1284 while (msg.size) {
1285 const struct ofp_hello_elem_header *oheh;
1286 unsigned int len;
1287
1288 if (msg.size < sizeof *oheh) {
1289 return false;
1290 }
1291
1292 oheh = msg.data;
1293 len = ntohs(oheh->length);
1294 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1295 return false;
1296 }
1297
1298 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1299 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1300 ok = false;
1301 }
1302 }
1303
1304 return ok;
1305 }
1306
1307 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1308 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1309 static bool
1310 should_send_version_bitmap(uint32_t allowed_versions)
1311 {
1312 return !is_pow2((allowed_versions >> 1) + 1);
1313 }
1314
1315 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1316 * versions in the 'allowed_versions' bitmaps and returns the message. */
1317 struct ofpbuf *
1318 ofputil_encode_hello(uint32_t allowed_versions)
1319 {
1320 enum ofp_version ofp_version;
1321 struct ofpbuf *msg;
1322
1323 ofp_version = leftmost_1bit_idx(allowed_versions);
1324 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1325
1326 if (should_send_version_bitmap(allowed_versions)) {
1327 struct ofp_hello_elem_header *oheh;
1328 uint16_t map_len;
1329
1330 map_len = sizeof allowed_versions;
1331 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1332 oheh->type = htons(OFPHET_VERSIONBITMAP);
1333 oheh->length = htons(map_len + sizeof *oheh);
1334 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1335
1336 ofpmsg_update_length(msg);
1337 }
1338
1339 return msg;
1340 }
1341
1342 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1343 * protocol is 'current', at least partly transitions the protocol to 'want'.
1344 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1345 * connection if the switch processes the returned message correctly. (If
1346 * '*next != want' then the caller will have to iterate.)
1347 *
1348 * If 'current == want', or if it is not possible to transition from 'current'
1349 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1350 * protocol versions), returns NULL and stores 'current' in '*next'. */
1351 struct ofpbuf *
1352 ofputil_encode_set_protocol(enum ofputil_protocol current,
1353 enum ofputil_protocol want,
1354 enum ofputil_protocol *next)
1355 {
1356 enum ofp_version cur_version, want_version;
1357 enum ofputil_protocol cur_base, want_base;
1358 bool cur_tid, want_tid;
1359
1360 cur_version = ofputil_protocol_to_ofp_version(current);
1361 want_version = ofputil_protocol_to_ofp_version(want);
1362 if (cur_version != want_version) {
1363 *next = current;
1364 return NULL;
1365 }
1366
1367 cur_base = ofputil_protocol_to_base(current);
1368 want_base = ofputil_protocol_to_base(want);
1369 if (cur_base != want_base) {
1370 *next = ofputil_protocol_set_base(current, want_base);
1371
1372 switch (want_base) {
1373 case OFPUTIL_P_OF10_NXM:
1374 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1375
1376 case OFPUTIL_P_OF10_STD:
1377 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1378
1379 case OFPUTIL_P_OF11_STD:
1380 case OFPUTIL_P_OF12_OXM:
1381 case OFPUTIL_P_OF13_OXM:
1382 case OFPUTIL_P_OF14_OXM:
1383 case OFPUTIL_P_OF15_OXM:
1384 case OFPUTIL_P_OF16_OXM:
1385 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1386 * verified above that we're not trying to change versions. */
1387 OVS_NOT_REACHED();
1388
1389 case OFPUTIL_P_OF10_STD_TID:
1390 case OFPUTIL_P_OF10_NXM_TID:
1391 OVS_NOT_REACHED();
1392 }
1393 }
1394
1395 cur_tid = (current & OFPUTIL_P_TID) != 0;
1396 want_tid = (want & OFPUTIL_P_TID) != 0;
1397 if (cur_tid != want_tid) {
1398 *next = ofputil_protocol_set_tid(current, want_tid);
1399 return ofputil_make_flow_mod_table_id(want_tid);
1400 }
1401
1402 ovs_assert(current == want);
1403
1404 *next = current;
1405 return NULL;
1406 }
1407
1408 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1409 * format to 'nxff'. */
1410 struct ofpbuf *
1411 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1412 {
1413 struct nx_set_flow_format *sff;
1414 struct ofpbuf *msg;
1415
1416 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1417
1418 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1419 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1420 sff->format = htonl(nxff);
1421
1422 return msg;
1423 }
1424
1425 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1426 * otherwise. */
1427 enum ofputil_protocol
1428 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1429 {
1430 switch (flow_format) {
1431 case NXFF_OPENFLOW10:
1432 return OFPUTIL_P_OF10_STD;
1433
1434 case NXFF_NXM:
1435 return OFPUTIL_P_OF10_NXM;
1436
1437 default:
1438 return 0;
1439 }
1440 }
1441
1442 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1443 bool
1444 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1445 {
1446 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1447 }
1448
1449 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1450 * value. */
1451 const char *
1452 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1453 {
1454 switch (flow_format) {
1455 case NXFF_OPENFLOW10:
1456 return "openflow10";
1457 case NXFF_NXM:
1458 return "nxm";
1459 default:
1460 OVS_NOT_REACHED();
1461 }
1462 }
1463
1464 struct ofpbuf *
1465 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1466 enum nx_packet_in_format packet_in_format)
1467 {
1468 struct nx_set_packet_in_format *spif;
1469 struct ofpbuf *msg;
1470
1471 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1472 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1473 spif->format = htonl(packet_in_format);
1474
1475 return msg;
1476 }
1477
1478 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1479 * extension on or off (according to 'flow_mod_table_id'). */
1480 struct ofpbuf *
1481 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1482 {
1483 struct nx_flow_mod_table_id *nfmti;
1484 struct ofpbuf *msg;
1485
1486 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1487 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1488 nfmti->set = flow_mod_table_id;
1489 return msg;
1490 }
1491
1492 struct ofputil_flow_mod_flag {
1493 uint16_t raw_flag;
1494 enum ofp_version min_version, max_version;
1495 enum ofputil_flow_mod_flags flag;
1496 };
1497
1498 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1499 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1500 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1501 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1502 OFPUTIL_FF_EMERG },
1503 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1504 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1505 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1506 { 0, 0, 0, 0 },
1507 };
1508
1509 static enum ofperr
1510 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1511 enum ofp_flow_mod_command command,
1512 enum ofp_version version,
1513 enum ofputil_flow_mod_flags *flagsp)
1514 {
1515 uint16_t raw_flags = ntohs(raw_flags_);
1516 const struct ofputil_flow_mod_flag *f;
1517
1518 *flagsp = 0;
1519 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1520 if (raw_flags & f->raw_flag
1521 && version >= f->min_version
1522 && (!f->max_version || version <= f->max_version)) {
1523 raw_flags &= ~f->raw_flag;
1524 *flagsp |= f->flag;
1525 }
1526 }
1527
1528 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1529 * never do.
1530 *
1531 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1532 * resets counters. */
1533 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1534 && command == OFPFC_ADD) {
1535 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1536 }
1537
1538 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1539 }
1540
1541 static ovs_be16
1542 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1543 enum ofp_version version)
1544 {
1545 const struct ofputil_flow_mod_flag *f;
1546 uint16_t raw_flags;
1547
1548 raw_flags = 0;
1549 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1550 if (f->flag & flags
1551 && version >= f->min_version
1552 && (!f->max_version || version <= f->max_version)) {
1553 raw_flags |= f->raw_flag;
1554 }
1555 }
1556
1557 return htons(raw_flags);
1558 }
1559
1560 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1561 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1562 * code.
1563 *
1564 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1565 * The caller must initialize 'ofpacts' and retains ownership of it.
1566 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1567 *
1568 * Does not validate the flow_mod actions. The caller should do that, with
1569 * ofpacts_check(). */
1570 enum ofperr
1571 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1572 const struct ofp_header *oh,
1573 enum ofputil_protocol protocol,
1574 struct ofpbuf *ofpacts,
1575 ofp_port_t max_port, uint8_t max_table)
1576 {
1577 ovs_be16 raw_flags;
1578 enum ofperr error;
1579 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1580 enum ofpraw raw = ofpraw_pull_assert(&b);
1581 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1582 /* Standard OpenFlow 1.1+ flow_mod. */
1583 const struct ofp11_flow_mod *ofm;
1584
1585 ofm = ofpbuf_pull(&b, sizeof *ofm);
1586
1587 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1588 if (error) {
1589 return error;
1590 }
1591
1592 /* Translate the message. */
1593 fm->priority = ntohs(ofm->priority);
1594 if (ofm->command == OFPFC_ADD
1595 || (oh->version == OFP11_VERSION
1596 && (ofm->command == OFPFC_MODIFY ||
1597 ofm->command == OFPFC_MODIFY_STRICT)
1598 && ofm->cookie_mask == htonll(0))) {
1599 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1600 * not match on the cookie is treated as an "add" if there is no
1601 * match. */
1602 fm->cookie = htonll(0);
1603 fm->cookie_mask = htonll(0);
1604 fm->new_cookie = ofm->cookie;
1605 } else {
1606 fm->cookie = ofm->cookie;
1607 fm->cookie_mask = ofm->cookie_mask;
1608 fm->new_cookie = OVS_BE64_MAX;
1609 }
1610 fm->modify_cookie = false;
1611 fm->command = ofm->command;
1612
1613 /* Get table ID.
1614 *
1615 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1616 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1617 fm->table_id = ofm->table_id;
1618 if (fm->table_id == OFPTT_ALL
1619 && (oh->version == OFP11_VERSION
1620 || (ofm->command != OFPFC_DELETE &&
1621 ofm->command != OFPFC_DELETE_STRICT))) {
1622 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1623 }
1624
1625 fm->idle_timeout = ntohs(ofm->idle_timeout);
1626 fm->hard_timeout = ntohs(ofm->hard_timeout);
1627 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1628 fm->importance = ntohs(ofm->importance);
1629 } else {
1630 fm->importance = 0;
1631 }
1632 fm->buffer_id = ntohl(ofm->buffer_id);
1633 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1634 if (error) {
1635 return error;
1636 }
1637
1638 fm->out_group = (ofm->command == OFPFC_DELETE ||
1639 ofm->command == OFPFC_DELETE_STRICT
1640 ? ntohl(ofm->out_group)
1641 : OFPG_ANY);
1642 raw_flags = ofm->flags;
1643 } else {
1644 uint16_t command;
1645
1646 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1647 /* Standard OpenFlow 1.0 flow_mod. */
1648 const struct ofp10_flow_mod *ofm;
1649
1650 /* Get the ofp10_flow_mod. */
1651 ofm = ofpbuf_pull(&b, sizeof *ofm);
1652
1653 /* Translate the rule. */
1654 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1655 ofputil_normalize_match(&fm->match);
1656
1657 /* OpenFlow 1.0 says that exact-match rules have to have the
1658 * highest possible priority. */
1659 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1660 ? ntohs(ofm->priority)
1661 : UINT16_MAX);
1662
1663 /* Translate the message. */
1664 command = ntohs(ofm->command);
1665 fm->cookie = htonll(0);
1666 fm->cookie_mask = htonll(0);
1667 fm->new_cookie = ofm->cookie;
1668 fm->idle_timeout = ntohs(ofm->idle_timeout);
1669 fm->hard_timeout = ntohs(ofm->hard_timeout);
1670 fm->importance = 0;
1671 fm->buffer_id = ntohl(ofm->buffer_id);
1672 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1673 fm->out_group = OFPG_ANY;
1674 raw_flags = ofm->flags;
1675 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1676 /* Nicira extended flow_mod. */
1677 const struct nx_flow_mod *nfm;
1678
1679 /* Dissect the message. */
1680 nfm = ofpbuf_pull(&b, sizeof *nfm);
1681 error = nx_pull_match(&b, ntohs(nfm->match_len),
1682 &fm->match, &fm->cookie, &fm->cookie_mask);
1683 if (error) {
1684 return error;
1685 }
1686
1687 /* Translate the message. */
1688 command = ntohs(nfm->command);
1689 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1690 /* Flow additions may only set a new cookie, not match an
1691 * existing cookie. */
1692 return OFPERR_NXBRC_NXM_INVALID;
1693 }
1694 fm->priority = ntohs(nfm->priority);
1695 fm->new_cookie = nfm->cookie;
1696 fm->idle_timeout = ntohs(nfm->idle_timeout);
1697 fm->hard_timeout = ntohs(nfm->hard_timeout);
1698 fm->importance = 0;
1699 fm->buffer_id = ntohl(nfm->buffer_id);
1700 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1701 fm->out_group = OFPG_ANY;
1702 raw_flags = nfm->flags;
1703 } else {
1704 OVS_NOT_REACHED();
1705 }
1706
1707 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1708 if (protocol & OFPUTIL_P_TID) {
1709 fm->command = command & 0xff;
1710 fm->table_id = command >> 8;
1711 } else {
1712 if (command > 0xff) {
1713 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1714 "but flow_mod_table_id extension is not enabled");
1715 }
1716 fm->command = command;
1717 fm->table_id = 0xff;
1718 }
1719 }
1720
1721 if (fm->command > OFPFC_DELETE_STRICT) {
1722 return OFPERR_OFPFMFC_BAD_COMMAND;
1723 }
1724
1725 error = ofpacts_pull_openflow_instructions(&b, b.size,
1726 oh->version, ofpacts);
1727 if (error) {
1728 return error;
1729 }
1730 fm->ofpacts = ofpacts->data;
1731 fm->ofpacts_len = ofpacts->size;
1732
1733 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1734 oh->version, &fm->flags);
1735 if (error) {
1736 return error;
1737 }
1738
1739 if (fm->flags & OFPUTIL_FF_EMERG) {
1740 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1741 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1742 *
1743 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1744 * or hard_timeout is nonzero. Otherwise, there is no good error
1745 * code, so just state that the flow table is full. */
1746 return (fm->hard_timeout || fm->idle_timeout
1747 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1748 : OFPERR_OFPFMFC_TABLE_FULL);
1749 }
1750
1751 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1752 &fm->match.flow, max_port,
1753 fm->table_id, max_table, protocol);
1754 }
1755
1756 static enum ofperr
1757 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1758 struct ofpbuf *bands)
1759 {
1760 const struct ofp13_meter_band_header *ombh;
1761 struct ofputil_meter_band *mb;
1762 uint16_t n = 0;
1763
1764 ombh = ofpbuf_try_pull(msg, len);
1765 if (!ombh) {
1766 return OFPERR_OFPBRC_BAD_LEN;
1767 }
1768
1769 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1770 size_t ombh_len = ntohs(ombh->len);
1771 /* All supported band types have the same length. */
1772 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1773 return OFPERR_OFPBRC_BAD_LEN;
1774 }
1775 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1776 mb->type = ntohs(ombh->type);
1777 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1778 return OFPERR_OFPMMFC_BAD_BAND;
1779 }
1780 mb->rate = ntohl(ombh->rate);
1781 mb->burst_size = ntohl(ombh->burst_size);
1782 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1783 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1784 n++;
1785 len -= ombh_len;
1786 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1787 (char *) ombh + ombh_len);
1788 }
1789 if (len) {
1790 return OFPERR_OFPBRC_BAD_LEN;
1791 }
1792 *n_bands = n;
1793 return 0;
1794 }
1795
1796 enum ofperr
1797 ofputil_decode_meter_mod(const struct ofp_header *oh,
1798 struct ofputil_meter_mod *mm,
1799 struct ofpbuf *bands)
1800 {
1801 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1802 ofpraw_pull_assert(&b);
1803 const struct ofp13_meter_mod *omm = ofpbuf_pull(&b, sizeof *omm);
1804
1805 /* Translate the message. */
1806 mm->command = ntohs(omm->command);
1807 if (mm->command != OFPMC13_ADD &&
1808 mm->command != OFPMC13_MODIFY &&
1809 mm->command != OFPMC13_DELETE) {
1810 return OFPERR_OFPMMFC_BAD_COMMAND;
1811 }
1812 mm->meter.meter_id = ntohl(omm->meter_id);
1813
1814 if (mm->command == OFPMC13_DELETE) {
1815 mm->meter.flags = 0;
1816 mm->meter.n_bands = 0;
1817 mm->meter.bands = NULL;
1818 } else {
1819 enum ofperr error;
1820
1821 mm->meter.flags = ntohs(omm->flags);
1822 if (mm->meter.flags & OFPMF13_KBPS &&
1823 mm->meter.flags & OFPMF13_PKTPS) {
1824 return OFPERR_OFPMMFC_BAD_FLAGS;
1825 }
1826 mm->meter.bands = bands->data;
1827
1828 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1829 if (error) {
1830 return error;
1831 }
1832 }
1833 return 0;
1834 }
1835
1836 void
1837 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1838 {
1839 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1840 *meter_id = ntohl(omr->meter_id);
1841 }
1842
1843 struct ofpbuf *
1844 ofputil_encode_meter_request(enum ofp_version ofp_version,
1845 enum ofputil_meter_request_type type,
1846 uint32_t meter_id)
1847 {
1848 struct ofpbuf *msg;
1849
1850 enum ofpraw raw;
1851
1852 switch (type) {
1853 case OFPUTIL_METER_CONFIG:
1854 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1855 break;
1856 case OFPUTIL_METER_STATS:
1857 raw = OFPRAW_OFPST13_METER_REQUEST;
1858 break;
1859 default:
1860 case OFPUTIL_METER_FEATURES:
1861 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1862 break;
1863 }
1864
1865 msg = ofpraw_alloc(raw, ofp_version, 0);
1866
1867 if (type != OFPUTIL_METER_FEATURES) {
1868 struct ofp13_meter_multipart_request *omr;
1869 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1870 omr->meter_id = htonl(meter_id);
1871 }
1872 return msg;
1873 }
1874
1875 static void
1876 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1877 struct ofpbuf *msg)
1878 {
1879 uint16_t n = 0;
1880
1881 for (n = 0; n < n_bands; ++n) {
1882 /* Currently all band types have same size. */
1883 struct ofp13_meter_band_dscp_remark *ombh;
1884 size_t ombh_len = sizeof *ombh;
1885
1886 ombh = ofpbuf_put_zeros(msg, ombh_len);
1887
1888 ombh->type = htons(mb->type);
1889 ombh->len = htons(ombh_len);
1890 ombh->rate = htonl(mb->rate);
1891 ombh->burst_size = htonl(mb->burst_size);
1892 ombh->prec_level = mb->prec_level;
1893
1894 mb++;
1895 }
1896 }
1897
1898 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1899 void
1900 ofputil_append_meter_config(struct ovs_list *replies,
1901 const struct ofputil_meter_config *mc)
1902 {
1903 struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
1904 size_t start_ofs = msg->size;
1905 struct ofp13_meter_config *reply;
1906
1907 ofpbuf_put_uninit(msg, sizeof *reply);
1908 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1909
1910 reply = ofpbuf_at_assert(msg, start_ofs, sizeof *reply);
1911 reply->flags = htons(mc->flags);
1912 reply->meter_id = htonl(mc->meter_id);
1913 reply->length = htons(msg->size - start_ofs);
1914
1915 ofpmp_postappend(replies, start_ofs);
1916 }
1917
1918 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1919 void
1920 ofputil_append_meter_stats(struct ovs_list *replies,
1921 const struct ofputil_meter_stats *ms)
1922 {
1923 struct ofp13_meter_stats *reply;
1924 uint16_t n = 0;
1925 uint16_t len;
1926
1927 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1928 reply = ofpmp_append(replies, len);
1929
1930 reply->meter_id = htonl(ms->meter_id);
1931 reply->len = htons(len);
1932 memset(reply->pad, 0, sizeof reply->pad);
1933 reply->flow_count = htonl(ms->flow_count);
1934 reply->packet_in_count = htonll(ms->packet_in_count);
1935 reply->byte_in_count = htonll(ms->byte_in_count);
1936 reply->duration_sec = htonl(ms->duration_sec);
1937 reply->duration_nsec = htonl(ms->duration_nsec);
1938
1939 for (n = 0; n < ms->n_bands; ++n) {
1940 const struct ofputil_meter_band_stats *src = &ms->bands[n];
1941 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1942
1943 dst->packet_band_count = htonll(src->packet_count);
1944 dst->byte_band_count = htonll(src->byte_count);
1945 }
1946 }
1947
1948 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1949 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1950 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1951 * it across the call.
1952 *
1953 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1954 * message. Calling this function multiple times for a single 'msg' iterates
1955 * through the replies. 'bands' is cleared for each reply.
1956 *
1957 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1958 * otherwise a positive errno value. */
1959 int
1960 ofputil_decode_meter_config(struct ofpbuf *msg,
1961 struct ofputil_meter_config *mc,
1962 struct ofpbuf *bands)
1963 {
1964 const struct ofp13_meter_config *omc;
1965 enum ofperr err;
1966
1967 /* Pull OpenFlow headers for the first call. */
1968 if (!msg->header) {
1969 ofpraw_pull_assert(msg);
1970 }
1971
1972 if (!msg->size) {
1973 return EOF;
1974 }
1975
1976 omc = ofpbuf_try_pull(msg, sizeof *omc);
1977 if (!omc) {
1978 VLOG_WARN_RL(&bad_ofmsg_rl,
1979 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
1980 msg->size);
1981 return OFPERR_OFPBRC_BAD_LEN;
1982 }
1983
1984 ofpbuf_clear(bands);
1985 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
1986 &mc->n_bands, bands);
1987 if (err) {
1988 return err;
1989 }
1990 mc->meter_id = ntohl(omc->meter_id);
1991 mc->flags = ntohs(omc->flags);
1992 mc->bands = bands->data;
1993
1994 return 0;
1995 }
1996
1997 static enum ofperr
1998 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1999 struct ofpbuf *bands)
2000 {
2001 const struct ofp13_meter_band_stats *ombs;
2002 struct ofputil_meter_band_stats *mbs;
2003 uint16_t n, i;
2004
2005 ombs = ofpbuf_try_pull(msg, len);
2006 if (!ombs) {
2007 return OFPERR_OFPBRC_BAD_LEN;
2008 }
2009
2010 n = len / sizeof *ombs;
2011 if (len != n * sizeof *ombs) {
2012 return OFPERR_OFPBRC_BAD_LEN;
2013 }
2014
2015 mbs = ofpbuf_put_uninit(bands, len);
2016
2017 for (i = 0; i < n; ++i) {
2018 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2019 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2020 }
2021 *n_bands = n;
2022 return 0;
2023 }
2024
2025 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2026 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2027 * decoded into 'bands'.
2028 *
2029 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2030 * message. Calling this function multiple times for a single 'msg' iterates
2031 * through the replies. 'bands' is cleared for each reply.
2032 *
2033 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2034 * otherwise a positive errno value. */
2035 int
2036 ofputil_decode_meter_stats(struct ofpbuf *msg,
2037 struct ofputil_meter_stats *ms,
2038 struct ofpbuf *bands)
2039 {
2040 const struct ofp13_meter_stats *oms;
2041 enum ofperr err;
2042
2043 /* Pull OpenFlow headers for the first call. */
2044 if (!msg->header) {
2045 ofpraw_pull_assert(msg);
2046 }
2047
2048 if (!msg->size) {
2049 return EOF;
2050 }
2051
2052 oms = ofpbuf_try_pull(msg, sizeof *oms);
2053 if (!oms) {
2054 VLOG_WARN_RL(&bad_ofmsg_rl,
2055 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2056 msg->size);
2057 return OFPERR_OFPBRC_BAD_LEN;
2058 }
2059
2060 ofpbuf_clear(bands);
2061 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2062 &ms->n_bands, bands);
2063 if (err) {
2064 return err;
2065 }
2066 ms->meter_id = ntohl(oms->meter_id);
2067 ms->flow_count = ntohl(oms->flow_count);
2068 ms->packet_in_count = ntohll(oms->packet_in_count);
2069 ms->byte_in_count = ntohll(oms->byte_in_count);
2070 ms->duration_sec = ntohl(oms->duration_sec);
2071 ms->duration_nsec = ntohl(oms->duration_nsec);
2072 ms->bands = bands->data;
2073
2074 return 0;
2075 }
2076
2077 void
2078 ofputil_decode_meter_features(const struct ofp_header *oh,
2079 struct ofputil_meter_features *mf)
2080 {
2081 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2082
2083 mf->max_meters = ntohl(omf->max_meter);
2084 mf->band_types = ntohl(omf->band_types);
2085 mf->capabilities = ntohl(omf->capabilities);
2086 mf->max_bands = omf->max_bands;
2087 mf->max_color = omf->max_color;
2088 }
2089
2090 struct ofpbuf *
2091 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2092 const struct ofp_header *request)
2093 {
2094 struct ofpbuf *reply;
2095 struct ofp13_meter_features *omf;
2096
2097 reply = ofpraw_alloc_stats_reply(request, 0);
2098 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2099
2100 omf->max_meter = htonl(mf->max_meters);
2101 omf->band_types = htonl(mf->band_types);
2102 omf->capabilities = htonl(mf->capabilities);
2103 omf->max_bands = mf->max_bands;
2104 omf->max_color = mf->max_color;
2105
2106 return reply;
2107 }
2108
2109 struct ofpbuf *
2110 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2111 const struct ofputil_meter_mod *mm)
2112 {
2113 struct ofpbuf *msg;
2114
2115 struct ofp13_meter_mod *omm;
2116
2117 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2118 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2119 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2120 omm->command = htons(mm->command);
2121 if (mm->command != OFPMC13_DELETE) {
2122 omm->flags = htons(mm->meter.flags);
2123 }
2124 omm->meter_id = htonl(mm->meter.meter_id);
2125
2126 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2127
2128 ofpmsg_update_length(msg);
2129 return msg;
2130 }
2131
2132 static ovs_be16
2133 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2134 enum ofputil_protocol protocol)
2135 {
2136 return htons(protocol & OFPUTIL_P_TID
2137 ? (fm->command & 0xff) | (fm->table_id << 8)
2138 : fm->command);
2139 }
2140
2141 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2142 * 'protocol' and returns the message. */
2143 struct ofpbuf *
2144 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2145 enum ofputil_protocol protocol)
2146 {
2147 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2148 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2149 struct ofpbuf *msg;
2150
2151 switch (protocol) {
2152 case OFPUTIL_P_OF11_STD:
2153 case OFPUTIL_P_OF12_OXM:
2154 case OFPUTIL_P_OF13_OXM:
2155 case OFPUTIL_P_OF14_OXM:
2156 case OFPUTIL_P_OF15_OXM:
2157 case OFPUTIL_P_OF16_OXM: {
2158 struct ofp11_flow_mod *ofm;
2159 int tailroom;
2160
2161 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2162 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2163 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2164 if ((protocol == OFPUTIL_P_OF11_STD
2165 && (fm->command == OFPFC_MODIFY ||
2166 fm->command == OFPFC_MODIFY_STRICT)
2167 && fm->cookie_mask == htonll(0))
2168 || fm->command == OFPFC_ADD) {
2169 ofm->cookie = fm->new_cookie;
2170 } else {
2171 ofm->cookie = fm->cookie & fm->cookie_mask;
2172 }
2173 ofm->cookie_mask = fm->cookie_mask;
2174 if (fm->table_id != OFPTT_ALL
2175 || (protocol != OFPUTIL_P_OF11_STD
2176 && (fm->command == OFPFC_DELETE ||
2177 fm->command == OFPFC_DELETE_STRICT))) {
2178 ofm->table_id = fm->table_id;
2179 } else {
2180 ofm->table_id = 0;
2181 }
2182 ofm->command = fm->command;
2183 ofm->idle_timeout = htons(fm->idle_timeout);
2184 ofm->hard_timeout = htons(fm->hard_timeout);
2185 ofm->priority = htons(fm->priority);
2186 ofm->buffer_id = htonl(fm->buffer_id);
2187 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2188 ofm->out_group = htonl(fm->out_group);
2189 ofm->flags = raw_flags;
2190 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2191 ofm->importance = htons(fm->importance);
2192 } else {
2193 ofm->importance = 0;
2194 }
2195 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2196 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2197 version);
2198 break;
2199 }
2200
2201 case OFPUTIL_P_OF10_STD:
2202 case OFPUTIL_P_OF10_STD_TID: {
2203 struct ofp10_flow_mod *ofm;
2204
2205 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2206 fm->ofpacts_len);
2207 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2208 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2209 ofm->cookie = fm->new_cookie;
2210 ofm->command = ofputil_tid_command(fm, protocol);
2211 ofm->idle_timeout = htons(fm->idle_timeout);
2212 ofm->hard_timeout = htons(fm->hard_timeout);
2213 ofm->priority = htons(fm->priority);
2214 ofm->buffer_id = htonl(fm->buffer_id);
2215 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2216 ofm->flags = raw_flags;
2217 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2218 version);
2219 break;
2220 }
2221
2222 case OFPUTIL_P_OF10_NXM:
2223 case OFPUTIL_P_OF10_NXM_TID: {
2224 struct nx_flow_mod *nfm;
2225 int match_len;
2226
2227 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2228 NXM_TYPICAL_LEN + fm->ofpacts_len);
2229 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2230 nfm->command = ofputil_tid_command(fm, protocol);
2231 nfm->cookie = fm->new_cookie;
2232 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2233 nfm = msg->msg;
2234 nfm->idle_timeout = htons(fm->idle_timeout);
2235 nfm->hard_timeout = htons(fm->hard_timeout);
2236 nfm->priority = htons(fm->priority);
2237 nfm->buffer_id = htonl(fm->buffer_id);
2238 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2239 nfm->flags = raw_flags;
2240 nfm->match_len = htons(match_len);
2241 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2242 version);
2243 break;
2244 }
2245
2246 default:
2247 OVS_NOT_REACHED();
2248 }
2249
2250 ofpmsg_update_length(msg);
2251 return msg;
2252 }
2253
2254 static enum ofperr
2255 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2256 const struct ofp10_flow_stats_request *ofsr,
2257 bool aggregate)
2258 {
2259 fsr->aggregate = aggregate;
2260 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2261 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2262 fsr->out_group = OFPG_ANY;
2263 fsr->table_id = ofsr->table_id;
2264 fsr->cookie = fsr->cookie_mask = htonll(0);
2265
2266 return 0;
2267 }
2268
2269 static enum ofperr
2270 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2271 struct ofpbuf *b, bool aggregate)
2272 {
2273 const struct ofp11_flow_stats_request *ofsr;
2274 enum ofperr error;
2275
2276 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2277 fsr->aggregate = aggregate;
2278 fsr->table_id = ofsr->table_id;
2279 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2280 if (error) {
2281 return error;
2282 }
2283 fsr->out_group = ntohl(ofsr->out_group);
2284 fsr->cookie = ofsr->cookie;
2285 fsr->cookie_mask = ofsr->cookie_mask;
2286 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2287 if (error) {
2288 return error;
2289 }
2290
2291 return 0;
2292 }
2293
2294 static enum ofperr
2295 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2296 struct ofpbuf *b, bool aggregate)
2297 {
2298 const struct nx_flow_stats_request *nfsr;
2299 enum ofperr error;
2300
2301 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2302 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2303 &fsr->cookie, &fsr->cookie_mask);
2304 if (error) {
2305 return error;
2306 }
2307 if (b->size) {
2308 return OFPERR_OFPBRC_BAD_LEN;
2309 }
2310
2311 fsr->aggregate = aggregate;
2312 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2313 fsr->out_group = OFPG_ANY;
2314 fsr->table_id = nfsr->table_id;
2315
2316 return 0;
2317 }
2318
2319 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2320 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2321 *
2322 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2323 * request all queues. */
2324 struct ofpbuf *
2325 ofputil_encode_queue_get_config_request(enum ofp_version version,
2326 ofp_port_t port,
2327 uint32_t queue)
2328 {
2329 struct ofpbuf *request;
2330
2331 if (version == OFP10_VERSION) {
2332 struct ofp10_queue_get_config_request *qgcr10;
2333
2334 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2335 version, 0);
2336 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2337 qgcr10->port = htons(ofp_to_u16(port));
2338 } else if (version < OFP14_VERSION) {
2339 struct ofp11_queue_get_config_request *qgcr11;
2340
2341 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2342 version, 0);
2343 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2344 qgcr11->port = ofputil_port_to_ofp11(port);
2345 } else {
2346 struct ofp14_queue_desc_request *qdr14;
2347
2348 request = ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST,
2349 version, 0);
2350 qdr14 = ofpbuf_put_zeros(request, sizeof *qdr14);
2351 qdr14->port = ofputil_port_to_ofp11(port);
2352 qdr14->queue = htonl(queue);
2353 }
2354
2355 return request;
2356 }
2357
2358 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2359 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2360 * code. */
2361 enum ofperr
2362 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2363 ofp_port_t *port, uint32_t *queue)
2364 {
2365 const struct ofp10_queue_get_config_request *qgcr10;
2366 const struct ofp11_queue_get_config_request *qgcr11;
2367 const struct ofp14_queue_desc_request *qdr14;
2368 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2369 enum ofpraw raw = ofpraw_pull_assert(&b);
2370
2371 switch ((int) raw) {
2372 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2373 qgcr10 = b.data;
2374 *port = u16_to_ofp(ntohs(qgcr10->port));
2375 *queue = OFPQ_ALL;
2376 break;
2377
2378 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2379 qgcr11 = b.data;
2380 *queue = OFPQ_ALL;
2381 enum ofperr error = ofputil_port_from_ofp11(qgcr11->port, port);
2382 if (error || *port == OFPP_ANY) {
2383 return error;
2384 }
2385 break;
2386
2387 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2388 qdr14 = b.data;
2389 *queue = ntohl(qdr14->queue);
2390 return ofputil_port_from_ofp11(qdr14->port, port);
2391
2392 default:
2393 OVS_NOT_REACHED();
2394 }
2395
2396 return (ofp_to_u16(*port) < ofp_to_u16(OFPP_MAX)
2397 ? 0
2398 : OFPERR_OFPQOFC_BAD_PORT);
2399 }
2400
2401 /* Constructs and returns the beginning of a reply to
2402 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2403 * may append information about individual queues with
2404 * ofputil_append_queue_get_config_reply(). */
2405 void
2406 ofputil_start_queue_get_config_reply(const struct ofp_header *request,
2407 struct ovs_list *replies)
2408 {
2409 struct ofpbuf *reply;
2410 enum ofperr error;
2411 ofp_port_t port;
2412 uint32_t queue;
2413
2414 error = ofputil_decode_queue_get_config_request(request, &port, &queue);
2415 ovs_assert(!error);
2416
2417 enum ofpraw raw = ofpraw_decode_assert(request);
2418 switch ((int) raw) {
2419 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2420 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2421 request, 0);
2422 struct ofp10_queue_get_config_reply *qgcr10
2423 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2424 qgcr10->port = htons(ofp_to_u16(port));
2425 break;
2426
2427 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2428 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2429 request, 0);
2430 struct ofp11_queue_get_config_reply *qgcr11
2431 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2432 qgcr11->port = ofputil_port_to_ofp11(port);
2433 break;
2434
2435 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2436 reply = ofpraw_alloc_stats_reply(request, 0);
2437 break;
2438
2439 default:
2440 OVS_NOT_REACHED();
2441 }
2442
2443 ovs_list_init(replies);
2444 ovs_list_push_back(replies, &reply->list_node);
2445 }
2446
2447 static void
2448 put_ofp10_queue_rate(struct ofpbuf *reply,
2449 enum ofp10_queue_properties property, uint16_t rate)
2450 {
2451 if (rate != UINT16_MAX) {
2452 struct ofp10_queue_prop_rate *oqpr;
2453
2454 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2455 oqpr->prop_header.property = htons(property);
2456 oqpr->prop_header.len = htons(sizeof *oqpr);
2457 oqpr->rate = htons(rate);
2458 }
2459 }
2460
2461 static void
2462 put_ofp14_queue_rate(struct ofpbuf *reply,
2463 enum ofp14_queue_desc_prop_type type, uint16_t rate)
2464 {
2465 if (rate != UINT16_MAX) {
2466 ofpprop_put_u16(reply, type, rate);
2467 }
2468 }
2469
2470 void
2471 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config *qc,
2472 struct ovs_list *replies)
2473 {
2474 enum ofp_version ofp_version = ofpmp_version(replies);
2475 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
2476 size_t start_ofs = reply->size;
2477 size_t len_ofs;
2478 ovs_be16 *len;
2479
2480 if (ofp_version < OFP14_VERSION) {
2481 if (ofp_version < OFP12_VERSION) {
2482 struct ofp10_packet_queue *opq10;
2483
2484 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2485 opq10->queue_id = htonl(qc->queue);
2486 len_ofs = (char *) &opq10->len - (char *) reply->data;
2487 } else {
2488 struct ofp12_packet_queue *opq12;
2489
2490 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2491 opq12->port = ofputil_port_to_ofp11(qc->port);
2492 opq12->queue_id = htonl(qc->queue);
2493 len_ofs = (char *) &opq12->len - (char *) reply->data;
2494 }
2495
2496 put_ofp10_queue_rate(reply, OFPQT10_MIN_RATE, qc->min_rate);
2497 put_ofp10_queue_rate(reply, OFPQT11_MAX_RATE, qc->max_rate);
2498 } else {
2499 struct ofp14_queue_desc *oqd = ofpbuf_put_zeros(reply, sizeof *oqd);
2500 oqd->port_no = ofputil_port_to_ofp11(qc->port);
2501 oqd->queue_id = htonl(qc->queue);
2502 len_ofs = (char *) &oqd->len - (char *) reply->data;
2503 put_ofp14_queue_rate(reply, OFPQDPT14_MIN_RATE, qc->min_rate);
2504 put_ofp14_queue_rate(reply, OFPQDPT14_MAX_RATE, qc->max_rate);
2505 }
2506
2507 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2508 *len = htons(reply->size - start_ofs);
2509
2510 if (ofp_version >= OFP14_VERSION) {
2511 ofpmp_postappend(replies, start_ofs);
2512 }
2513 }
2514
2515 static enum ofperr
2516 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header *hdr,
2517 uint16_t *rate)
2518 {
2519 const struct ofp10_queue_prop_rate *oqpr;
2520
2521 if (hdr->len == htons(sizeof *oqpr)) {
2522 oqpr = (const struct ofp10_queue_prop_rate *) hdr;
2523 *rate = ntohs(oqpr->rate);
2524 return 0;
2525 } else {
2526 return OFPERR_OFPBRC_BAD_LEN;
2527 }
2528 }
2529
2530 static int
2531 ofputil_pull_queue_get_config_reply10(struct ofpbuf *msg,
2532 struct ofputil_queue_config *queue)
2533 {
2534 const struct ofp_header *oh = msg->header;
2535 unsigned int opq_len; /* Length of protocol-specific queue header. */
2536 unsigned int len; /* Total length of queue + properties. */
2537
2538 /* Obtain the port number from the message header. */
2539 if (oh->version == OFP10_VERSION) {
2540 const struct ofp10_queue_get_config_reply *oqgcr10 = msg->msg;
2541 queue->port = u16_to_ofp(ntohs(oqgcr10->port));
2542 } else {
2543 const struct ofp11_queue_get_config_reply *oqgcr11 = msg->msg;
2544 enum ofperr error = ofputil_port_from_ofp11(oqgcr11->port,
2545 &queue->port);
2546 if (error) {
2547 return error;
2548 }
2549 }
2550
2551 /* Pull off the queue header and get the queue number and length. */
2552 if (oh->version < OFP12_VERSION) {
2553 const struct ofp10_packet_queue *opq10;
2554 opq10 = ofpbuf_try_pull(msg, sizeof *opq10);
2555 if (!opq10) {
2556 return OFPERR_OFPBRC_BAD_LEN;
2557 }
2558 queue->queue = ntohl(opq10->queue_id);
2559 len = ntohs(opq10->len);
2560 opq_len = sizeof *opq10;
2561 } else {
2562 const struct ofp12_packet_queue *opq12;
2563 opq12 = ofpbuf_try_pull(msg, sizeof *opq12);
2564 if (!opq12) {
2565 return OFPERR_OFPBRC_BAD_LEN;
2566 }
2567 queue->queue = ntohl(opq12->queue_id);
2568 len = ntohs(opq12->len);
2569 opq_len = sizeof *opq12;
2570 }
2571
2572 /* Length check. */
2573 if (len < opq_len || len > msg->size + opq_len || len % 8) {
2574 return OFPERR_OFPBRC_BAD_LEN;
2575 }
2576 len -= opq_len;
2577
2578 /* Pull properties. The format of these properties differs from used in
2579 * OF1.4+ so we can't use the common property functions. */
2580 while (len > 0) {
2581 const struct ofp10_queue_prop_header *hdr;
2582 unsigned int property;
2583 unsigned int prop_len;
2584 enum ofperr error = 0;
2585
2586 hdr = ofpbuf_at_assert(msg, 0, sizeof *hdr);
2587 prop_len = ntohs(hdr->len);
2588 if (prop_len < sizeof *hdr || prop_len > msg->size || prop_len % 8) {
2589 return OFPERR_OFPBRC_BAD_LEN;
2590 }
2591
2592 property = ntohs(hdr->property);
2593 switch (property) {
2594 case OFPQT10_MIN_RATE:
2595 error = parse_ofp10_queue_rate(hdr, &queue->min_rate);
2596 break;
2597
2598 case OFPQT11_MAX_RATE:
2599 error = parse_ofp10_queue_rate(hdr, &queue->max_rate);
2600 break;
2601
2602 default:
2603 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2604 break;
2605 }
2606 if (error) {
2607 return error;
2608 }
2609
2610 ofpbuf_pull(msg, prop_len);
2611 len -= prop_len;
2612 }
2613 return 0;
2614 }
2615
2616 static int
2617 ofputil_pull_queue_get_config_reply14(struct ofpbuf *msg,
2618 struct ofputil_queue_config *queue)
2619 {
2620 struct ofp14_queue_desc *oqd14 = ofpbuf_try_pull(msg, sizeof *oqd14);
2621 if (!oqd14) {
2622 return OFPERR_OFPBRC_BAD_LEN;
2623 }
2624 enum ofperr error = ofputil_port_from_ofp11(oqd14->port_no, &queue->port);
2625 if (error) {
2626 return error;
2627 }
2628 queue->queue = ntohl(oqd14->queue_id);
2629
2630 /* Length check. */
2631 unsigned int len = ntohs(oqd14->len);
2632 if (len < sizeof *oqd14 || len > msg->size + sizeof *oqd14 || len % 8) {
2633 return OFPERR_OFPBRC_BAD_LEN;
2634 }
2635 len -= sizeof *oqd14;
2636
2637 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
2638 len);
2639 while (properties.size > 0) {
2640 struct ofpbuf payload;
2641 uint64_t type;
2642
2643 error = ofpprop_pull(&properties, &payload, &type);
2644 if (error) {
2645 return error;
2646 }
2647
2648 switch (type) {
2649 case OFPQDPT14_MIN_RATE:
2650 error = ofpprop_parse_u16(&payload, &queue->min_rate);
2651 break;
2652
2653 case OFPQDPT14_MAX_RATE:
2654 error = ofpprop_parse_u16(&payload, &queue->max_rate);
2655 break;
2656
2657 default:
2658 error = OFPPROP_UNKNOWN(true, "queue desc", type);
2659 break;
2660 }
2661
2662 if (error) {
2663 return error;
2664 }
2665 }
2666
2667 return 0;
2668 }
2669
2670 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2671 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2672 * must already have pulled off the main header.
2673 *
2674 * This function returns EOF if the last queue has already been decoded, 0 if a
2675 * queue was successfully decoded into '*queue', or an ofperr if there was a
2676 * problem decoding 'reply'. */
2677 int
2678 ofputil_pull_queue_get_config_reply(struct ofpbuf *msg,
2679 struct ofputil_queue_config *queue)
2680 {
2681 enum ofpraw raw;
2682 if (!msg->header) {
2683 /* Pull OpenFlow header. */
2684 raw = ofpraw_pull_assert(msg);
2685
2686 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2687 * doesn't have one at all). */
2688 if (raw == OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY) {
2689 ofpbuf_pull(msg, sizeof(struct ofp10_queue_get_config_reply));
2690 } else if (raw == OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY) {
2691 ofpbuf_pull(msg, sizeof(struct ofp11_queue_get_config_reply));
2692 } else {
2693 ovs_assert(raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY);
2694 }
2695 } else {
2696 raw = ofpraw_decode_assert(msg->header);
2697 }
2698
2699 queue->min_rate = UINT16_MAX;
2700 queue->max_rate = UINT16_MAX;
2701
2702 if (!msg->size) {
2703 return EOF;
2704 } else if (raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY) {
2705 return ofputil_pull_queue_get_config_reply14(msg, queue);
2706 } else {
2707 return ofputil_pull_queue_get_config_reply10(msg, queue);
2708 }
2709 }
2710
2711 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2712 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2713 * successful, otherwise an OpenFlow error code. */
2714 enum ofperr
2715 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2716 const struct ofp_header *oh)
2717 {
2718 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2719 enum ofpraw raw = ofpraw_pull_assert(&b);
2720 switch ((int) raw) {
2721 case OFPRAW_OFPST10_FLOW_REQUEST:
2722 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2723
2724 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2725 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2726
2727 case OFPRAW_OFPST11_FLOW_REQUEST:
2728 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2729
2730 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2731 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2732
2733 case OFPRAW_NXST_FLOW_REQUEST:
2734 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2735
2736 case OFPRAW_NXST_AGGREGATE_REQUEST:
2737 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2738
2739 default:
2740 /* Hey, the caller lied. */
2741 OVS_NOT_REACHED();
2742 }
2743 }
2744
2745 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2746 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2747 * 'protocol', and returns the message. */
2748 struct ofpbuf *
2749 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2750 enum ofputil_protocol protocol)
2751 {
2752 struct ofpbuf *msg;
2753 enum ofpraw raw;
2754
2755 switch (protocol) {
2756 case OFPUTIL_P_OF11_STD:
2757 case OFPUTIL_P_OF12_OXM:
2758 case OFPUTIL_P_OF13_OXM:
2759 case OFPUTIL_P_OF14_OXM:
2760 case OFPUTIL_P_OF15_OXM:
2761 case OFPUTIL_P_OF16_OXM: {
2762 struct ofp11_flow_stats_request *ofsr;
2763
2764 raw = (fsr->aggregate
2765 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2766 : OFPRAW_OFPST11_FLOW_REQUEST);
2767 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2768 ofputil_match_typical_len(protocol));
2769 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2770 ofsr->table_id = fsr->table_id;
2771 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2772 ofsr->out_group = htonl(fsr->out_group);
2773 ofsr->cookie = fsr->cookie;
2774 ofsr->cookie_mask = fsr->cookie_mask;
2775 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2776 break;
2777 }
2778
2779 case OFPUTIL_P_OF10_STD:
2780 case OFPUTIL_P_OF10_STD_TID: {
2781 struct ofp10_flow_stats_request *ofsr;
2782
2783 raw = (fsr->aggregate
2784 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2785 : OFPRAW_OFPST10_FLOW_REQUEST);
2786 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2787 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2788 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2789 ofsr->table_id = fsr->table_id;
2790 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2791 break;
2792 }
2793
2794 case OFPUTIL_P_OF10_NXM:
2795 case OFPUTIL_P_OF10_NXM_TID: {
2796 struct nx_flow_stats_request *nfsr;
2797 int match_len;
2798
2799 raw = (fsr->aggregate
2800 ? OFPRAW_NXST_AGGREGATE_REQUEST
2801 : OFPRAW_NXST_FLOW_REQUEST);
2802 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2803 ofpbuf_put_zeros(msg, sizeof *nfsr);
2804 match_len = nx_put_match(msg, &fsr->match,
2805 fsr->cookie, fsr->cookie_mask);
2806
2807 nfsr = msg->msg;
2808 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2809 nfsr->match_len = htons(match_len);
2810 nfsr->table_id = fsr->table_id;
2811 break;
2812 }
2813
2814 default:
2815 OVS_NOT_REACHED();
2816 }
2817
2818 return msg;
2819 }
2820
2821 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2822 * ofputil_flow_stats in 'fs'.
2823 *
2824 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2825 * OpenFlow message. Calling this function multiple times for a single 'msg'
2826 * iterates through the replies. The caller must initially leave 'msg''s layer
2827 * pointers null and not modify them between calls.
2828 *
2829 * Most switches don't send the values needed to populate fs->idle_age and
2830 * fs->hard_age, so those members will usually be set to 0. If the switch from
2831 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2832 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2833 * 'idle_age' and 'hard_age' members in 'fs'.
2834 *
2835 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2836 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2837 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2838 *
2839 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2840 * otherwise a positive errno value. */
2841 int
2842 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2843 struct ofpbuf *msg,
2844 bool flow_age_extension,
2845 struct ofpbuf *ofpacts)
2846 {
2847 const struct ofp_header *oh;
2848 size_t instructions_len;
2849 enum ofperr error;
2850 enum ofpraw raw;
2851
2852 error = (msg->header ? ofpraw_decode(&raw, msg->header)
2853 : ofpraw_pull(&raw, msg));
2854 if (error) {
2855 return error;
2856 }
2857 oh = msg->header;
2858
2859 if (!msg->size) {
2860 return EOF;
2861 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2862 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2863 const struct ofp11_flow_stats *ofs;
2864 size_t length;
2865 uint16_t padded_match_len;
2866
2867 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2868 if (!ofs) {
2869 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2870 "bytes at end", msg->size);
2871 return EINVAL;
2872 }
2873
2874 length = ntohs(ofs->length);
2875 if (length < sizeof *ofs) {
2876 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2877 "length %"PRIuSIZE, length);
2878 return EINVAL;
2879 }
2880
2881 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2882 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2883 return EINVAL;
2884 }
2885 instructions_len = length - sizeof *ofs - padded_match_len;
2886
2887 fs->priority = ntohs(ofs->priority);
2888 fs->table_id = ofs->table_id;
2889 fs->duration_sec = ntohl(ofs->duration_sec);
2890 fs->duration_nsec = ntohl(ofs->duration_nsec);
2891 fs->idle_timeout = ntohs(ofs->idle_timeout);
2892 fs->hard_timeout = ntohs(ofs->hard_timeout);
2893 if (oh->version >= OFP14_VERSION) {
2894 fs->importance = ntohs(ofs->importance);
2895 } else {
2896 fs->importance = 0;
2897 }
2898 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2899 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2900 &fs->flags);
2901 if (error) {
2902 return error;
2903 }
2904 } else {
2905 fs->flags = 0;
2906 }
2907 fs->idle_age = -1;
2908 fs->hard_age = -1;
2909 fs->cookie = ofs->cookie;
2910 fs->packet_count = ntohll(ofs->packet_count);
2911 fs->byte_count = ntohll(ofs->byte_count);
2912 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2913 const struct ofp10_flow_stats *ofs;
2914 size_t length;
2915
2916 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2917 if (!ofs) {
2918 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2919 "bytes at end", msg->size);
2920 return EINVAL;
2921 }
2922
2923 length = ntohs(ofs->length);
2924 if (length < sizeof *ofs) {
2925 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2926 "length %"PRIuSIZE, length);
2927 return EINVAL;
2928 }
2929 instructions_len = length - sizeof *ofs;
2930
2931 fs->cookie = get_32aligned_be64(&ofs->cookie);
2932 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2933 fs->priority = ntohs(ofs->priority);
2934 fs->table_id = ofs->table_id;
2935 fs->duration_sec = ntohl(ofs->duration_sec);
2936 fs->duration_nsec = ntohl(ofs->duration_nsec);
2937 fs->idle_timeout = ntohs(ofs->idle_timeout);
2938 fs->hard_timeout = ntohs(ofs->hard_timeout);
2939 fs->importance = 0;
2940 fs->idle_age = -1;
2941 fs->hard_age = -1;
2942 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2943 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2944 fs->flags = 0;
2945 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2946 const struct nx_flow_stats *nfs;
2947 size_t match_len, length;
2948
2949 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2950 if (!nfs) {
2951 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2952 "bytes at end", msg->size);
2953 return EINVAL;
2954 }
2955
2956 length = ntohs(nfs->length);
2957 match_len = ntohs(nfs->match_len);
2958 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2959 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2960 "claims invalid length %"PRIuSIZE, match_len, length);
2961 return EINVAL;
2962 }
2963 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2964 return EINVAL;
2965 }
2966 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2967
2968 fs->cookie = nfs->cookie;
2969 fs->table_id = nfs->table_id;
2970 fs->duration_sec = ntohl(nfs->duration_sec);
2971 fs->duration_nsec = ntohl(nfs->duration_nsec);
2972 fs->priority = ntohs(nfs->priority);
2973 fs->idle_timeout = ntohs(nfs->idle_timeout);
2974 fs->hard_timeout = ntohs(nfs->hard_timeout);
2975 fs->importance = 0;
2976 fs->idle_age = -1;
2977 fs->hard_age = -1;
2978 if (flow_age_extension) {
2979 if (nfs->idle_age) {
2980 fs->idle_age = ntohs(nfs->idle_age) - 1;
2981 }
2982 if (nfs->hard_age) {
2983 fs->hard_age = ntohs(nfs->hard_age) - 1;
2984 }
2985 }
2986 fs->packet_count = ntohll(nfs->packet_count);
2987 fs->byte_count = ntohll(nfs->byte_count);
2988 fs->flags = 0;
2989 } else {
2990 OVS_NOT_REACHED();
2991 }
2992
2993 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2994 ofpacts)) {
2995 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2996 return EINVAL;
2997 }
2998 fs->ofpacts = ofpacts->data;
2999 fs->ofpacts_len = ofpacts->size;
3000
3001 return 0;
3002 }
3003
3004 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3005 *
3006 * We use this in situations where OVS internally uses UINT64_MAX to mean
3007 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3008 static uint64_t
3009 unknown_to_zero(uint64_t count)
3010 {
3011 return count != UINT64_MAX ? count : 0;
3012 }
3013
3014 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3015 * those already present in the list of ofpbufs in 'replies'. 'replies' should
3016 * have been initialized with ofpmp_init(). */
3017 void
3018 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
3019 struct ovs_list *replies)
3020 {
3021 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
3022 size_t start_ofs = reply->size;
3023 enum ofp_version version = ofpmp_version(replies);
3024 enum ofpraw raw = ofpmp_decode_raw(replies);
3025
3026 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
3027 struct ofp11_flow_stats *ofs;
3028
3029 ofpbuf_put_uninit(reply, sizeof *ofs);
3030 oxm_put_match(reply, &fs->match, version);
3031 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
3032 version);
3033
3034 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3035 ofs->length = htons(reply->size - start_ofs);
3036 ofs->table_id = fs->table_id;
3037 ofs->pad = 0;
3038 ofs->duration_sec = htonl(fs->duration_sec);
3039 ofs->duration_nsec = htonl(fs->duration_nsec);
3040 ofs->priority = htons(fs->priority);
3041 ofs->idle_timeout = htons(fs->idle_timeout);
3042 ofs->hard_timeout = htons(fs->hard_timeout);
3043 if (version >= OFP14_VERSION) {
3044 ofs->importance = htons(fs->importance);
3045 } else {
3046 ofs->importance = 0;
3047 }
3048 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3049 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3050 } else {
3051 ofs->flags = 0;
3052 }
3053 memset(ofs->pad2, 0, sizeof ofs->pad2);
3054 ofs->cookie = fs->cookie;
3055 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3056 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3057 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3058 struct ofp10_flow_stats *ofs;
3059
3060 ofpbuf_put_uninit(reply, sizeof *ofs);
3061 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3062 version);
3063 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3064 ofs->length = htons(reply->size - start_ofs);
3065 ofs->table_id = fs->table_id;
3066 ofs->pad = 0;
3067 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3068 ofs->duration_sec = htonl(fs->duration_sec);
3069 ofs->duration_nsec = htonl(fs->duration_nsec);
3070 ofs->priority = htons(fs->priority);
3071 ofs->idle_timeout = htons(fs->idle_timeout);
3072 ofs->hard_timeout = htons(fs->hard_timeout);
3073 memset(ofs->pad2, 0, sizeof ofs->pad2);
3074 put_32aligned_be64(&ofs->cookie, fs->cookie);
3075 put_32aligned_be64(&ofs->packet_count,
3076 htonll(unknown_to_zero(fs->packet_count)));
3077 put_32aligned_be64(&ofs->byte_count,
3078 htonll(unknown_to_zero(fs->byte_count)));
3079 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3080 struct nx_flow_stats *nfs;
3081 int match_len;
3082
3083 ofpbuf_put_uninit(reply, sizeof *nfs);
3084 match_len = nx_put_match(reply, &fs->match, 0, 0);
3085 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3086 version);
3087 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3088 nfs->length = htons(reply->size - start_ofs);
3089 nfs->table_id = fs->table_id;
3090 nfs->pad = 0;
3091 nfs->duration_sec = htonl(fs->duration_sec);
3092 nfs->duration_nsec = htonl(fs->duration_nsec);
3093 nfs->priority = htons(fs->priority);
3094 nfs->idle_timeout = htons(fs->idle_timeout);
3095 nfs->hard_timeout = htons(fs->hard_timeout);
3096 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3097 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3098 : UINT16_MAX);
3099 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3100 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3101 : UINT16_MAX);
3102 nfs->match_len = htons(match_len);
3103 nfs->cookie = fs->cookie;
3104 nfs->packet_count = htonll(fs->packet_count);
3105 nfs->byte_count = htonll(fs->byte_count);
3106 } else {
3107 OVS_NOT_REACHED();
3108 }
3109
3110 ofpmp_postappend(replies, start_ofs);
3111 }
3112
3113 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3114 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3115 struct ofpbuf *
3116 ofputil_encode_aggregate_stats_reply(
3117 const struct ofputil_aggregate_stats *stats,
3118 const struct ofp_header *request)
3119 {
3120 struct ofp_aggregate_stats_reply *asr;
3121 uint64_t packet_count;
3122 uint64_t byte_count;
3123 struct ofpbuf *msg;
3124 enum ofpraw raw;
3125
3126 ofpraw_decode(&raw, request);
3127 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3128 packet_count = unknown_to_zero(stats->packet_count);
3129 byte_count = unknown_to_zero(stats->byte_count);
3130 } else {
3131 packet_count = stats->packet_count;
3132 byte_count = stats->byte_count;
3133 }
3134
3135 msg = ofpraw_alloc_stats_reply(request, 0);
3136 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3137 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3138 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3139 asr->flow_count = htonl(stats->flow_count);
3140
3141 return msg;
3142 }
3143
3144 enum ofperr
3145 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3146 const struct ofp_header *reply)
3147 {
3148 struct ofpbuf msg = ofpbuf_const_initializer(reply, ntohs(reply->length));
3149 ofpraw_pull_assert(&msg);
3150
3151 struct ofp_aggregate_stats_reply *asr = msg.msg;
3152 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3153 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3154 stats->flow_count = ntohl(asr->flow_count);
3155
3156 return 0;
3157 }
3158
3159 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3160 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3161 * an OpenFlow error code. */
3162 enum ofperr
3163 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3164 const struct ofp_header *oh)
3165 {
3166 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3167 enum ofpraw raw = ofpraw_pull_assert(&b);
3168 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3169 const struct ofp12_flow_removed *ofr;
3170 enum ofperr error;
3171
3172 ofr = ofpbuf_pull(&b, sizeof *ofr);
3173
3174 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3175 if (error) {
3176 return error;
3177 }
3178
3179 fr->priority = ntohs(ofr->priority);
3180 fr->cookie = ofr->cookie;
3181 fr->reason = ofr->reason;
3182 fr->table_id = ofr->table_id;
3183 fr->duration_sec = ntohl(ofr->duration_sec);
3184 fr->duration_nsec = ntohl(ofr->duration_nsec);
3185 fr->idle_timeout = ntohs(ofr->idle_timeout);
3186 fr->hard_timeout = ntohs(ofr->hard_timeout);
3187 fr->packet_count = ntohll(ofr->packet_count);
3188 fr->byte_count = ntohll(ofr->byte_count);
3189 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3190 const struct ofp10_flow_removed *ofr;
3191
3192 ofr = ofpbuf_pull(&b, sizeof *ofr);
3193
3194 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3195 fr->priority = ntohs(ofr->priority);
3196 fr->cookie = ofr->cookie;
3197 fr->reason = ofr->reason;
3198 fr->table_id = 255;
3199 fr->duration_sec = ntohl(ofr->duration_sec);
3200 fr->duration_nsec = ntohl(ofr->duration_nsec);
3201 fr->idle_timeout = ntohs(ofr->idle_timeout);
3202 fr->hard_timeout = 0;
3203 fr->packet_count = ntohll(ofr->packet_count);
3204 fr->byte_count = ntohll(ofr->byte_count);
3205 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3206 struct nx_flow_removed *nfr;
3207 enum ofperr error;
3208
3209 nfr = ofpbuf_pull(&b, sizeof *nfr);
3210 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3211 NULL, NULL);
3212 if (error) {
3213 return error;
3214 }
3215 if (b.size) {
3216 return OFPERR_OFPBRC_BAD_LEN;
3217 }
3218
3219 fr->priority = ntohs(nfr->priority);
3220 fr->cookie = nfr->cookie;
3221 fr->reason = nfr->reason;
3222 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3223 fr->duration_sec = ntohl(nfr->duration_sec);
3224 fr->duration_nsec = ntohl(nfr->duration_nsec);
3225 fr->idle_timeout = ntohs(nfr->idle_timeout);
3226 fr->hard_timeout = 0;
3227 fr->packet_count = ntohll(nfr->packet_count);
3228 fr->byte_count = ntohll(nfr->byte_count);
3229 } else {
3230 OVS_NOT_REACHED();
3231 }
3232
3233 return 0;
3234 }
3235
3236 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3237 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3238 * message. */
3239 struct ofpbuf *
3240 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3241 enum ofputil_protocol protocol)
3242 {
3243 struct ofpbuf *msg;
3244 enum ofp_flow_removed_reason reason = fr->reason;
3245
3246 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3247 reason = OFPRR_DELETE;
3248 }
3249
3250 switch (protocol) {
3251 case OFPUTIL_P_OF11_STD:
3252 case OFPUTIL_P_OF12_OXM:
3253 case OFPUTIL_P_OF13_OXM:
3254 case OFPUTIL_P_OF14_OXM:
3255 case OFPUTIL_P_OF15_OXM:
3256 case OFPUTIL_P_OF16_OXM: {
3257 struct ofp12_flow_removed *ofr;
3258
3259 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3260 ofputil_protocol_to_ofp_version(protocol),
3261 htonl(0),
3262 ofputil_match_typical_len(protocol));
3263 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3264 ofr->cookie = fr->cookie;
3265 ofr->priority = htons(fr->priority);
3266 ofr->reason = reason;
3267 ofr->table_id = fr->table_id;
3268 ofr->duration_sec = htonl(fr->duration_sec);
3269 ofr->duration_nsec = htonl(fr->duration_nsec);
3270 ofr->idle_timeout = htons(fr->idle_timeout);
3271 ofr->hard_timeout = htons(fr->hard_timeout);
3272 ofr->packet_count = htonll(fr->packet_count);
3273 ofr->byte_count = htonll(fr->byte_count);
3274 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3275 break;
3276 }
3277
3278 case OFPUTIL_P_OF10_STD:
3279 case OFPUTIL_P_OF10_STD_TID: {
3280 struct ofp10_flow_removed *ofr;
3281
3282 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3283 htonl(0), 0);
3284 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3285 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3286 ofr->cookie = fr->cookie;
3287 ofr->priority = htons(fr->priority);
3288 ofr->reason = reason;
3289 ofr->duration_sec = htonl(fr->duration_sec);
3290 ofr->duration_nsec = htonl(fr->duration_nsec);
3291 ofr->idle_timeout = htons(fr->idle_timeout);
3292 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3293 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3294 break;
3295 }
3296
3297 case OFPUTIL_P_OF10_NXM:
3298 case OFPUTIL_P_OF10_NXM_TID: {
3299 struct nx_flow_removed *nfr;
3300 int match_len;
3301
3302 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3303 htonl(0), NXM_TYPICAL_LEN);
3304 ofpbuf_put_zeros(msg, sizeof *nfr);
3305 match_len = nx_put_match(msg, &fr->match, 0, 0);
3306
3307 nfr = msg->msg;
3308 nfr->cookie = fr->cookie;
3309 nfr->priority = htons(fr->priority);
3310 nfr->reason = reason;
3311 nfr->table_id = fr->table_id + 1;
3312 nfr->duration_sec = htonl(fr->duration_sec);
3313 nfr->duration_nsec = htonl(fr->duration_nsec);
3314 nfr->idle_timeout = htons(fr->idle_timeout);
3315 nfr->match_len = htons(match_len);
3316 nfr->packet_count = htonll(fr->packet_count);
3317 nfr->byte_count = htonll(fr->byte_count);
3318 break;
3319 }
3320
3321 default:
3322 OVS_NOT_REACHED();
3323 }
3324
3325 return msg;
3326 }
3327
3328 /* The caller has done basic initialization of '*pin'; the other output
3329 * arguments needs to be initialized. */
3330 static enum ofperr
3331 decode_nx_packet_in2(const struct ofp_header *oh, bool loose,
3332 struct ofputil_packet_in *pin,
3333 size_t *total_len, uint32_t *buffer_id,
3334 struct ofpbuf *continuation)
3335 {
3336 *total_len = 0;
3337 *buffer_id = UINT32_MAX;
3338
3339 struct ofpbuf properties;
3340 ofpbuf_use_const(&properties, oh, ntohs(oh->length));
3341 ofpraw_pull_assert(&properties);
3342
3343 while (properties.size > 0) {
3344 struct ofpbuf payload;
3345 uint64_t type;
3346
3347 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
3348 if (error) {
3349 return error;
3350 }
3351
3352 switch (type) {
3353 case NXPINT_PACKET:
3354 pin->packet = payload.msg;
3355 pin->packet_len = ofpbuf_msgsize(&payload);
3356 break;
3357
3358 case NXPINT_FULL_LEN: {
3359 uint32_t u32;
3360 error = ofpprop_parse_u32(&payload, &u32);
3361 *total_len = u32;
3362 break;
3363 }
3364
3365 case NXPINT_BUFFER_ID:
3366 error = ofpprop_parse_u32(&payload, buffer_id);
3367 break;
3368
3369 case NXPINT_TABLE_ID:
3370 error = ofpprop_parse_u8(&payload, &pin->table_id);
3371 break;
3372
3373 case NXPINT_COOKIE:
3374 error = ofpprop_parse_be64(&payload, &pin->cookie);
3375 break;
3376
3377 case NXPINT_REASON: {
3378 uint8_t reason;
3379 error = ofpprop_parse_u8(&payload, &reason);
3380 pin->reason = reason;
3381 break;
3382 }
3383
3384 case NXPINT_METADATA:
3385 error = oxm_decode_match(payload.msg, ofpbuf_msgsize(&payload),
3386 &pin->flow_metadata);
3387 break;
3388
3389 case NXPINT_USERDATA:
3390 pin->userdata = payload.msg;
3391 pin->userdata_len = ofpbuf_msgsize(&payload);
3392 break;
3393
3394 case NXPINT_CONTINUATION:
3395 if (continuation) {
3396 error = ofpprop_parse_nested(&payload, continuation);
3397 }
3398 break;
3399
3400 default:
3401 error = OFPPROP_UNKNOWN(loose, "NX_PACKET_IN2", type);
3402 break;
3403 }
3404 if (error) {
3405 return error;
3406 }
3407 }
3408
3409 if (!pin->packet_len) {
3410 VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 lacks packet");
3411 return OFPERR_OFPBRC_BAD_LEN;
3412 } else if (!*total_len) {
3413 *total_len = pin->packet_len;
3414 } else if (*total_len < pin->packet_len) {
3415 VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 claimed full_len < len");
3416 return OFPERR_OFPBRC_BAD_LEN;
3417 }
3418
3419 return 0;
3420 }
3421
3422 /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3423 * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3424 * included in the message. If 'total_lenp' is nonnull, populates
3425 * '*total_lenp' with the original length of the packet (which is larger than
3426 * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3427 * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
3428 * was not buffered).
3429 *
3430 * Populates 'continuation', if nonnull, with the continuation data from the
3431 * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3432 * format of this data is supposed to be opaque to anything other than
3433 * ovs-vswitchd, so that in any other process the only reasonable use of this
3434 * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3435 *
3436 * This function points 'pin->packet' into 'oh', so the caller should not free
3437 * it separately from the original OpenFlow message. This is also true for
3438 * 'pin->userdata' (which could also end up NULL if there is no userdata).
3439 *
3440 * Returns 0 if successful, otherwise an OpenFlow error code. */
3441 enum ofperr
3442 ofputil_decode_packet_in(const struct ofp_header *oh, bool loose,
3443 struct ofputil_packet_in *pin,
3444 size_t *total_lenp, uint32_t *buffer_idp,
3445 struct ofpbuf *continuation)
3446 {
3447 uint32_t buffer_id;
3448 size_t total_len;
3449
3450 memset(pin, 0, sizeof *pin);
3451 pin->cookie = OVS_BE64_MAX;
3452 if (continuation) {
3453 ofpbuf_use_const(continuation, NULL, 0);
3454 }
3455
3456 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3457 enum ofpraw raw = ofpraw_pull_assert(&b);
3458 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3459 const struct ofp12_packet_in *opi = ofpbuf_pull(&b, sizeof *opi);
3460 const ovs_be64 *cookie = (raw == OFPRAW_OFPT13_PACKET_IN
3461 ? ofpbuf_pull(&b, sizeof *cookie)
3462 : NULL);
3463 enum ofperr error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3464 if (error) {
3465 return error;
3466 }
3467
3468 if (!ofpbuf_try_pull(&b, 2)) {
3469 return OFPERR_OFPBRC_BAD_LEN;
3470 }
3471
3472 pin->reason = opi->reason;
3473 pin->table_id = opi->table_id;
3474 buffer_id = ntohl(opi->buffer_id);
3475 total_len = ntohs(opi->total_len);
3476 if (cookie) {
3477 pin->cookie = *cookie;
3478 }
3479
3480 pin->packet = b.data;
3481 pin->packet_len = b.size;
3482 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3483 const struct ofp10_packet_in *opi;
3484
3485 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3486
3487 pin->packet = CONST_CAST(uint8_t *, opi->data);
3488 pin->packet_len = b.size;
3489
3490 match_init_catchall(&pin->flow_metadata);
3491 match_set_in_port(&pin->flow_metadata,
3492 u16_to_ofp(ntohs(opi->in_port)));
3493 pin->reason = opi->reason;
3494 buffer_id = ntohl(opi->buffer_id);
3495 total_len = ntohs(opi->total_len);
3496 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3497 const struct ofp11_packet_in *opi;
3498 ofp_port_t in_port;
3499 enum ofperr error;
3500
3501 opi = ofpbuf_pull(&b, sizeof *opi);
3502
3503 pin->packet = b.data;
3504 pin->packet_len = b.size;
3505
3506 buffer_id = ntohl(opi->buffer_id);
3507 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3508 if (error) {
3509 return error;
3510 }
3511 match_init_catchall(&pin->flow_metadata);
3512 match_set_in_port(&pin->flow_metadata, in_port);
3513 total_len = ntohs(opi->total_len);
3514 pin->reason = opi->reason;
3515 pin->table_id = opi->table_id;
3516 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3517 const struct nx_packet_in *npi;
3518 int error;
3519
3520 npi = ofpbuf_pull(&b, sizeof *npi);
3521 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3522 &pin->flow_metadata, NULL, NULL);
3523 if (error) {
3524 return error;
3525 }
3526
3527 if (!ofpbuf_try_pull(&b, 2)) {
3528 return OFPERR_OFPBRC_BAD_LEN;
3529 }
3530
3531 pin->reason = npi->reason;
3532 pin->table_id = npi->table_id;
3533 pin->cookie = npi->cookie;
3534
3535 buffer_id = ntohl(npi->buffer_id);
3536 total_len = ntohs(npi->total_len);
3537
3538 pin->packet = b.data;
3539 pin->packet_len = b.size;
3540 } else if (raw == OFPRAW_NXT_PACKET_IN2 || raw == OFPRAW_NXT_RESUME) {
3541 enum ofperr error = decode_nx_packet_in2(oh, loose, pin, &total_len,
3542 &buffer_id, continuation);
3543 if (error) {
3544 return error;
3545 }
3546 } else {
3547 OVS_NOT_REACHED();
3548 }
3549
3550 if (total_lenp) {
3551 *total_lenp = total_len;
3552 }
3553 if (buffer_idp) {
3554 *buffer_idp = buffer_id;
3555 }
3556
3557 return 0;
3558 }
3559
3560 static int
3561 encode_packet_in_reason(enum ofp_packet_in_reason reason,
3562 enum ofp_version version)
3563 {
3564 switch (reason) {
3565 case OFPR_NO_MATCH:
3566 case OFPR_ACTION:
3567 case OFPR_INVALID_TTL:
3568 return reason;
3569
3570 case OFPR_ACTION_SET:
3571 case OFPR_GROUP:
3572 case OFPR_PACKET_OUT:
3573 return version < OFP14_VERSION ? OFPR_ACTION : reason;
3574
3575 case OFPR_EXPLICIT_MISS:
3576 return version < OFP13_VERSION ? OFPR_ACTION : OFPR_NO_MATCH;
3577
3578 case OFPR_IMPLICIT_MISS:
3579 return OFPR_NO_MATCH;
3580
3581 case OFPR_N_REASONS:
3582 default:
3583 OVS_NOT_REACHED();
3584 }
3585 }
3586
3587 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3588 * function omits it. The caller can add it itself if desired. */
3589 static void
3590 ofputil_put_packet_in(const struct ofputil_packet_in *pin,
3591 enum ofp_version version, uint32_t buffer_id,
3592 size_t include_bytes, struct ofpbuf *msg)
3593 {
3594 /* Add packet properties. */
3595 ofpprop_put(msg, NXPINT_PACKET, pin->packet, include_bytes);
3596 if (include_bytes != pin->packet_len) {
3597 ofpprop_put_u32(msg, NXPINT_FULL_LEN, pin->packet_len);
3598 }
3599 if (buffer_id != UINT32_MAX) {
3600 ofpprop_put_u32(msg, NXPINT_BUFFER_ID, buffer_id);
3601 }
3602
3603 /* Add flow properties. */
3604 ofpprop_put_u8(msg, NXPINT_TABLE_ID, pin->table_id);
3605 if (pin->cookie != OVS_BE64_MAX) {
3606 ofpprop_put_be64(msg, NXPINT_COOKIE, pin->cookie);
3607 }
3608
3609 /* Add other properties. */
3610 ofpprop_put_u8(msg, NXPINT_REASON,
3611 encode_packet_in_reason(pin->reason, version));
3612
3613 size_t start = ofpprop_start(msg, NXPINT_METADATA);
3614 oxm_put_raw(msg, &pin->flow_metadata, version);
3615 ofpprop_end(msg, start);
3616 }
3617
3618 static void
3619 put_actions_property(struct ofpbuf *msg, uint64_t prop_type,
3620 enum ofp_version version,
3621 const struct ofpact *actions, size_t actions_len)
3622 {
3623 if (actions_len) {
3624 size_t start = ofpprop_start_nested(msg, prop_type);
3625 ofpacts_put_openflow_actions(actions, actions_len, msg, version);
3626 ofpprop_end(msg, start);
3627 }
3628 }
3629
3630 enum nx_continuation_prop_type {
3631 NXCPT_BRIDGE = 0x8000,
3632 NXCPT_STACK,
3633 NXCPT_MIRRORS,
3634 NXCPT_CONNTRACKED,
3635 NXCPT_TABLE_ID,
3636 NXCPT_COOKIE,
3637 NXCPT_ACTIONS,
3638 NXCPT_ACTION_SET,
3639 };
3640
3641 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3642 * function omits it. The caller can add it itself if desired. */
3643 static void
3644 ofputil_put_packet_in_private(const struct ofputil_packet_in_private *pin,
3645 enum ofp_version version, uint32_t buffer_id,
3646 size_t include_bytes, struct ofpbuf *msg)
3647 {
3648 ofputil_put_packet_in(&pin->public, version, buffer_id,
3649 include_bytes, msg);
3650
3651 size_t continuation_ofs = ofpprop_start_nested(msg, NXPINT_CONTINUATION);
3652 size_t inner_ofs = msg->size;
3653
3654 if (!uuid_is_zero(&pin->bridge)) {
3655 ofpprop_put_uuid(msg, NXCPT_BRIDGE, &pin->bridge);
3656 }
3657
3658 for (size_t i = 0; i < pin->n_stack; i++) {
3659 const union mf_subvalue *s = &pin->stack[i];
3660 size_t ofs;
3661 for (ofs = 0; ofs < sizeof *s; ofs++) {
3662 if (s->u8[ofs]) {
3663 break;
3664 }
3665 }
3666
3667 ofpprop_put(msg, NXCPT_STACK, &s->u8[ofs], sizeof *s - ofs);
3668 }
3669
3670 if (pin->mirrors) {
3671 ofpprop_put_u32(msg, NXCPT_MIRRORS, pin->mirrors);
3672 }
3673
3674 if (pin->conntracked) {
3675 ofpprop_put_flag(msg, NXCPT_CONNTRACKED);
3676 }
3677
3678 if (pin->actions_len) {
3679 /* Divide 'pin->actions' into groups that begins with an
3680 * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3681 * NXCPT_COOKIE property (if either has changed; each is initially
3682 * assumed 0), then a NXCPT_ACTIONS property with the grouped
3683 * actions.
3684 *
3685 * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3686 * always do that later, since this is a private property. */
3687 const struct ofpact *const end = ofpact_end(pin->actions,
3688 pin->actions_len);
3689 const struct ofpact_unroll_xlate *unroll = NULL;
3690 uint8_t table_id = 0;
3691 ovs_be64 cookie = 0;
3692
3693 const struct ofpact *a;
3694 for (a = pin->actions; ; a = ofpact_next(a)) {
3695 if (a == end || a->type == OFPACT_UNROLL_XLATE) {
3696 if (unroll) {
3697 if (table_id != unroll->rule_table_id) {
3698 ofpprop_put_u8(msg, NXCPT_TABLE_ID,
3699 unroll->rule_table_id);
3700 table_id = unroll->rule_table_id;
3701 }
3702 if (cookie != unroll->rule_cookie) {
3703 ofpprop_put_be64(msg, NXCPT_COOKIE,
3704 unroll->rule_cookie);
3705 cookie = unroll->rule_cookie;
3706 }
3707 }
3708
3709 const struct ofpact *start
3710 = unroll ? ofpact_next(&unroll->ofpact) : pin->actions;
3711 put_actions_property(msg, NXCPT_ACTIONS, version,
3712 start, (a - start) * sizeof *a);
3713
3714 if (a == end) {
3715 break;
3716 }
3717 unroll = ofpact_get_UNROLL_XLATE(a);
3718 }
3719 }
3720 }
3721
3722 if (pin->action_set_len) {
3723 size_t start = ofpprop_start_nested(msg, NXCPT_ACTION_SET);
3724 ofpacts_put_openflow_actions(pin->action_set,
3725 pin->action_set_len, msg, version);
3726 ofpprop_end(msg, start);
3727 }
3728
3729 if (msg->size > inner_ofs) {
3730 ofpprop_end(msg, continuation_ofs);
3731 } else {
3732 msg->size = continuation_ofs;
3733 }
3734 }
3735
3736 static struct ofpbuf *
3737 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin,
3738 uint32_t buffer_id)
3739 {
3740 struct ofp10_packet_in *opi;
3741 struct ofpbuf *msg;
3742
3743 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3744 htonl(0), pin->packet_len);
3745 opi = ofpbuf_put_zeros(msg, offsetof(struct ofp10_packet_in, data));
3746 opi->total_len = htons(pin->packet_len);
3747 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3748 opi->reason = encode_packet_in_reason(pin->reason, OFP10_VERSION);
3749 opi->buffer_id = htonl(buffer_id);
3750
3751 return msg;
3752 }
3753
3754 static struct ofpbuf *
3755 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin,
3756 enum ofp_version version, uint32_t buffer_id)
3757 {
3758 struct nx_packet_in *npi;
3759 struct ofpbuf *msg;
3760 size_t match_len;
3761
3762 /* The final argument is just an estimate of the space required. */
3763 msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, version,
3764 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3765 ofpbuf_put_zeros(msg, sizeof *npi);
3766 match_len = nx_put_match(msg, &pin->flow_metadata, 0, 0);
3767 ofpbuf_put_zeros(msg, 2);
3768
3769 npi = msg->msg;
3770 npi->buffer_id = htonl(buffer_id);
3771 npi->total_len = htons(pin->packet_len);
3772 npi->reason = encode_packet_in_reason(pin->reason, version);
3773 npi->table_id = pin->table_id;
3774 npi->cookie = pin->cookie;
3775 npi->match_len = htons(match_len);
3776
3777 return msg;
3778 }
3779
3780 static struct ofpbuf *
3781 ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private *pin,
3782 enum ofp_version version, uint32_t buffer_id,
3783 size_t include_bytes)
3784 {
3785 /* 'extra' is just an estimate of the space required. */
3786 size_t extra = (pin->public.packet_len
3787 + NXM_TYPICAL_LEN /* flow_metadata */
3788 + pin->n_stack * 16
3789 + pin->actions_len
3790 + pin->action_set_len
3791 + 256); /* fudge factor */
3792 struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2, version,
3793 htonl(0), extra);
3794
3795 ofputil_put_packet_in_private(pin, version, buffer_id, include_bytes, msg);
3796 if (pin->public.userdata_len) {
3797 ofpprop_put(msg, NXPINT_USERDATA, pin->public.userdata,
3798 pin->public.userdata_len);
3799 }
3800
3801 ofpmsg_update_length(msg);
3802 return msg;
3803 }
3804
3805 static struct ofpbuf *
3806 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin,
3807 uint32_t buffer_id)
3808 {
3809 struct ofp11_packet_in *opi;
3810 struct ofpbuf *msg;
3811
3812 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3813 htonl(0), pin->packet_len);
3814 opi = ofpbuf_put_zeros(msg, sizeof *opi);
3815 opi->buffer_id = htonl(buffer_id);
3816 opi->in_port = ofputil_port_to_ofp11(
3817 pin->flow_metadata.flow.in_port.ofp_port);
3818 opi->in_phy_port = opi->in_port;
3819 opi->total_len = htons(pin->packet_len);
3820 opi->reason = encode_packet_in_reason(pin->reason, OFP11_VERSION);
3821 opi->table_id = pin->table_id;
3822
3823 return msg;
3824 }
3825
3826 static struct ofpbuf *
3827 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3828 enum ofp_version version,
3829 uint32_t buffer_id)
3830 {
3831 enum ofpraw raw = (version >= OFP13_VERSION
3832 ? OFPRAW_OFPT13_PACKET_IN
3833 : OFPRAW_OFPT12_PACKET_IN);
3834 struct ofpbuf *msg;
3835
3836 /* The final argument is just an estimate of the space required. */
3837 msg = ofpraw_alloc_xid(raw, version,
3838 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3839
3840 struct ofp12_packet_in *opi = ofpbuf_put_zeros(msg, sizeof *opi);
3841 opi->buffer_id = htonl(buffer_id);
3842 opi->total_len = htons(pin->packet_len);
3843 opi->reason = encode_packet_in_reason(pin->reason, version);
3844 opi->table_id = pin->table_id;
3845
3846 if (version >= OFP13_VERSION) {
3847 ovs_be64 cookie = pin->cookie;
3848 ofpbuf_put(msg, &cookie, sizeof cookie);
3849 }
3850
3851 oxm_put_match(msg, &pin->flow_metadata, version);
3852 ofpbuf_put_zeros(msg, 2);
3853
3854 return msg;
3855 }
3856
3857 /* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3858 * for 'protocol', using the packet-in format specified by 'packet_in_format'.
3859 *
3860 * If 'pkt_buf' is nonnull and 'max_len' allows the packet to be buffered, this
3861 * function will attempt to obtain a buffer ID from 'pktbuf' and truncate the
3862 * packet to 'max_len' bytes. Otherwise, or if 'pktbuf' doesn't have a free
3863 * buffer, it will send the whole packet without buffering.
3864 *
3865 * This function is really meant only for use by ovs-vswitchd. To any other
3866 * code, the "continuation" data, i.e. the data that is in struct
3867 * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3868 * to be opaque (and it might change from one OVS version to another). Thus,
3869 * if any other code wants to encode a packet-in, it should use a non-"private"
3870 * version of this function. (Such a version doesn't currently exist because
3871 * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3872 * write it...) */
3873 struct ofpbuf *
3874 ofputil_encode_packet_in_private(const struct ofputil_packet_in_private *pin,
3875 enum ofputil_protocol protocol,
3876 enum nx_packet_in_format packet_in_format,
3877 uint16_t max_len, struct pktbuf *pktbuf)
3878 {
3879 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
3880
3881 /* Get buffer ID. */
3882 ofp_port_t in_port = pin->public.flow_metadata.flow.in_port.ofp_port;
3883 uint32_t buffer_id = (max_len != OFPCML12_NO_BUFFER && pktbuf
3884 ? pktbuf_save(pktbuf, pin->public.packet,
3885 pin->public.packet_len, in_port)
3886 : UINT32_MAX);
3887
3888 /* Calculate the number of bytes of the packet to include in the
3889 * packet-in:
3890 *
3891 * - If not buffered, the whole thing.
3892 *
3893 * - Otherwise, no more than 'max_len' bytes. */
3894 size_t include_bytes = (buffer_id == UINT32_MAX
3895 ? pin->public.packet_len
3896 : MIN(max_len, pin->public.packet_len));
3897
3898 struct ofpbuf *msg;
3899 switch (packet_in_format) {
3900 case NXPIF_STANDARD:
3901 switch (protocol) {
3902 case OFPUTIL_P_OF10_STD:
3903 case OFPUTIL_P_OF10_STD_TID:
3904 case OFPUTIL_P_OF10_NXM:
3905 case OFPUTIL_P_OF10_NXM_TID:
3906 msg = ofputil_encode_ofp10_packet_in(&pin->public, buffer_id);
3907 break;
3908
3909 case OFPUTIL_P_OF11_STD:
3910 msg = ofputil_encode_ofp11_packet_in(&pin->public, buffer_id);
3911 break;
3912
3913 case OFPUTIL_P_OF12_OXM:
3914 case OFPUTIL_P_OF13_OXM:
3915 case OFPUTIL_P_OF14_OXM:
3916 case OFPUTIL_P_OF15_OXM:
3917 case OFPUTIL_P_OF16_OXM:
3918 msg = ofputil_encode_ofp12_packet_in(&pin->public, version, buffer_id);
3919 break;
3920
3921 default:
3922 OVS_NOT_REACHED();
3923 }
3924 break;
3925
3926 case NXPIF_NXT_PACKET_IN:
3927 msg = ofputil_encode_nx_packet_in(&pin->public, version, buffer_id);
3928 break;
3929
3930 case NXPIF_NXT_PACKET_IN2:
3931 return ofputil_encode_nx_packet_in2(pin, version, buffer_id,
3932 include_bytes);
3933
3934 default:
3935 OVS_NOT_REACHED();
3936 }
3937
3938 ofpbuf_put(msg, pin->public.packet, include_bytes);
3939 ofpmsg_update_length(msg);
3940 return msg;
3941 }
3942
3943 /* Returns a string form of 'reason'. The return value is either a statically
3944 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3945 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3946 const char *
3947 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3948 char *reasonbuf, size_t bufsize)
3949 {
3950 switch (reason) {
3951 case OFPR_NO_MATCH:
3952 return "no_match";
3953 case OFPR_ACTION:
3954 return "action";
3955 case OFPR_INVALID_TTL:
3956 return "invalid_ttl";
3957 case OFPR_ACTION_SET:
3958 return "action_set";
3959 case OFPR_GROUP:
3960 return "group";
3961 case OFPR_PACKET_OUT:
3962 return "packet_out";
3963 case OFPR_EXPLICIT_MISS:
3964 case OFPR_IMPLICIT_MISS:
3965 return "";
3966
3967 case OFPR_N_REASONS:
3968 default:
3969 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3970 return reasonbuf;
3971 }
3972 }
3973
3974 bool
3975 ofputil_packet_in_reason_from_string(const char *s,
3976 enum ofp_packet_in_reason *reason)
3977 {
3978 int i;
3979
3980 for (i = 0; i < OFPR_N_REASONS; i++) {
3981 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3982 const char *reason_s;
3983
3984 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3985 sizeof reasonbuf);
3986 if (!strcasecmp(s, reason_s)) {
3987 *reason = i;
3988 return true;
3989 }
3990 }
3991 return false;
3992 }
3993
3994 /* Returns a newly allocated NXT_RESUME message for 'pin', with the given
3995 * 'continuation', for 'protocol'. This message is suitable for resuming the
3996 * pipeline traveral of the packet represented by 'pin', if sent to the switch
3997 * from which 'pin' was received. */
3998 struct ofpbuf *
3999 ofputil_encode_resume(const struct ofputil_packet_in *pin,
4000 const struct ofpbuf *continuation,
4001 enum ofputil_protocol protocol)
4002 {
4003 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
4004 size_t extra = pin->packet_len + NXM_TYPICAL_LEN + continuation->size;
4005 struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_RESUME, version,
4006 0, extra);
4007 ofputil_put_packet_in(pin, version, UINT32_MAX, pin->packet_len, msg);
4008 ofpprop_put_nested(msg, NXPINT_CONTINUATION, continuation);
4009 ofpmsg_update_length(msg);
4010 return msg;
4011 }
4012
4013 static enum ofperr
4014 parse_subvalue_prop(const struct ofpbuf *property, union mf_subvalue *sv)
4015 {
4016 unsigned int len = ofpbuf_msgsize(property);
4017 if (len > sizeof *sv) {
4018 VLOG_WARN_RL(&bad_ofmsg_rl, "NXCPT_STACK property has bad length %u",
4019 len);
4020 return OFPERR_OFPBPC_BAD_LEN;
4021 }
4022 memset(sv, 0, sizeof *sv);
4023 memcpy(&sv->u8[sizeof *sv - len], property->msg, len);
4024 return 0;
4025 }
4026
4027 static enum ofperr
4028 parse_actions_property(struct ofpbuf *property, enum ofp_version version,
4029 struct ofpbuf *ofpacts)
4030 {
4031 if (!ofpbuf_try_pull(property, ROUND_UP(ofpbuf_headersize(property), 8))) {
4032 VLOG_WARN_RL(&bad_ofmsg_rl, "actions property has bad length %"PRIu32,
4033 property->size);
4034 return OFPERR_OFPBPC_BAD_LEN;
4035 }
4036
4037 return ofpacts_pull_openflow_actions(property, property->size,
4038 version, ofpacts);
4039 }
4040
4041 /* This is like ofputil_decode_packet_in(), except that it decodes the
4042 * continuation data into 'pin'. The format of this data is supposed to be
4043 * opaque to any process other than ovs-vswitchd, so this function should not
4044 * be used outside ovs-vswitchd.
4045 *
4046 * When successful, 'pin' contains some dynamically allocated data. Call
4047 * ofputil_packet_in_private_destroy() to free this data. */
4048 enum ofperr
4049 ofputil_decode_packet_in_private(const struct ofp_header *oh, bool loose,
4050 struct ofputil_packet_in_private *pin,
4051 size_t *total_len, uint32_t *buffer_id)
4052 {
4053 memset(pin, 0, sizeof *pin);
4054
4055 struct ofpbuf continuation;
4056 enum ofperr error;
4057 error = ofputil_decode_packet_in(oh, loose, &pin->public, total_len,
4058 buffer_id, &continuation);
4059 if (error) {
4060 return error;
4061 }
4062
4063 struct ofpbuf actions, action_set;
4064 ofpbuf_init(&actions, 0);
4065 ofpbuf_init(&action_set, 0);
4066
4067 uint8_t table_id = 0;
4068 ovs_be64 cookie = 0;
4069
4070 size_t allocated_stack = 0;
4071
4072 while (continuation.size > 0) {
4073 struct ofpbuf payload;
4074 uint64_t type;
4075
4076 error = ofpprop_pull(&continuation, &payload, &type);
4077 ovs_assert(!error);
4078
4079 switch (type) {
4080 case NXCPT_BRIDGE:
4081 error = ofpprop_parse_uuid(&payload, &pin->bridge);
4082 break;
4083
4084 case NXCPT_STACK:
4085 if (pin->n_stack >= allocated_stack) {
4086 pin->stack = x2nrealloc(pin->stack, &allocated_stack,
4087 sizeof *pin->stack);
4088 }
4089 error = parse_subvalue_prop(&payload,
4090 &pin->stack[pin->n_stack++]);
4091 break;
4092
4093 case NXCPT_MIRRORS:
4094 error = ofpprop_parse_u32(&payload, &pin->mirrors);
4095 break;
4096
4097 case NXCPT_CONNTRACKED:
4098 pin->conntracked = true;
4099 break;
4100
4101 case NXCPT_TABLE_ID:
4102 error = ofpprop_parse_u8(&payload, &table_id);
4103 break;
4104
4105 case NXCPT_COOKIE:
4106 error = ofpprop_parse_be64(&payload, &cookie);
4107 break;
4108
4109 case NXCPT_ACTIONS: {
4110 struct ofpact_unroll_xlate *unroll
4111 = ofpact_put_UNROLL_XLATE(&actions);
4112 unroll->rule_table_id = table_id;
4113 unroll->rule_cookie = cookie;
4114 error = parse_actions_property(&payload, oh->version, &actions);
4115 break;
4116 }
4117
4118 case NXCPT_ACTION_SET:
4119 error = parse_actions_property(&payload, oh->version, &action_set);
4120 break;
4121
4122 default:
4123 error = OFPPROP_UNKNOWN(loose, "continuation", type);
4124 break;
4125 }
4126 if (error) {
4127 break;
4128 }
4129 }
4130
4131 pin->actions_len = actions.size;
4132 pin->actions = ofpbuf_steal_data(&actions);
4133 pin->action_set_len = action_set.size;
4134 pin->action_set = ofpbuf_steal_data(&action_set);
4135
4136 if (error) {
4137 ofputil_packet_in_private_destroy(pin);
4138 }
4139
4140 return 0;
4141 }
4142
4143 /* Frees data in 'pin' that is dynamically allocated by
4144 * ofputil_decode_packet_in_private().
4145 *
4146 * 'pin->public' contains some pointer members that
4147 * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4148 * data, so this function doesn't free those. */
4149 void
4150 ofputil_packet_in_private_destroy(struct ofputil_packet_in_private *pin)
4151 {
4152 if (pin) {
4153 free(pin->stack);
4154 free(pin->actions);
4155 free(pin->action_set);
4156 }
4157 }
4158
4159 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4160 * 'po'.
4161 *
4162 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4163 * message's actions. The caller must initialize 'ofpacts' and retains
4164 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4165 *
4166 * Returns 0 if successful, otherwise an OFPERR_* value. */
4167 enum ofperr
4168 ofputil_decode_packet_out(struct ofputil_packet_out *po,
4169 const struct ofp_header *oh,
4170 struct ofpbuf *ofpacts)
4171 {
4172 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4173 enum ofpraw raw = ofpraw_pull_assert(&b);
4174
4175 ofpbuf_clear(ofpacts);
4176 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
4177 enum ofperr error;
4178 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4179
4180 po->buffer_id = ntohl(opo->buffer_id);
4181 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
4182 if (error) {
4183 return error;
4184 }
4185
4186 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
4187 oh->version, ofpacts);
4188 if (error) {
4189 return error;
4190 }
4191 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
4192 enum ofperr error;
4193 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4194
4195 po->buffer_id = ntohl(opo->buffer_id);
4196 po->in_port = u16_to_ofp(ntohs(opo->in_port));
4197
4198 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
4199 oh->version, ofpacts);
4200 if (error) {
4201 return error;
4202 }
4203 } else {
4204 OVS_NOT_REACHED();
4205 }
4206
4207 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
4208 && po->in_port != OFPP_LOCAL
4209 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
4210 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
4211 po->in_port);
4212 return OFPERR_OFPBRC_BAD_PORT;
4213 }
4214
4215 po->ofpacts = ofpacts->data;
4216 po->ofpacts_len = ofpacts->size;
4217
4218 if (po->buffer_id == UINT32_MAX) {
4219 po->packet = b.data;
4220 po->packet_len = b.size;
4221 } else {
4222 po->packet = NULL;
4223 po->packet_len = 0;
4224 }
4225
4226 return 0;
4227 }
4228 \f
4229 /* ofputil_phy_port */
4230
4231 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4232 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4233 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4234 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4235 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4236 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4237 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4238 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4239
4240 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
4241 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
4242 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
4243 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
4244 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
4245 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
4246
4247 static enum netdev_features
4248 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
4249 {
4250 uint32_t ofp10 = ntohl(ofp10_);
4251 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
4252 }
4253
4254 static ovs_be32
4255 netdev_port_features_to_ofp10(enum netdev_features features)
4256 {
4257 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
4258 }
4259
4260 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4261 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4262 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4263 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4264 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4265 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4266 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4267 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
4268 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
4269 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
4270 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
4271 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
4272 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
4273 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
4274 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
4275 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
4276
4277 static enum netdev_features
4278 netdev_port_features_from_ofp11(ovs_be32 ofp11)
4279 {
4280 return ntohl(ofp11) & 0xffff;
4281 }
4282
4283 static ovs_be32
4284 netdev_port_features_to_ofp11(enum netdev_features features)
4285 {
4286 return htonl(features & 0xffff);
4287 }
4288
4289 static enum ofperr
4290 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
4291 const struct ofp10_phy_port *opp)
4292 {
4293 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
4294 pp->hw_addr = opp->hw_addr;
4295 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
4296
4297 pp->config = ntohl(opp->config) & OFPPC10_ALL;
4298 pp->state = ntohl(opp->state) & OFPPS10_ALL;
4299
4300 pp->curr = netdev_port_features_from_ofp10(opp->curr);
4301 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
4302 pp->supported = netdev_port_features_from_ofp10(opp->supported);
4303 pp->peer = netdev_port_features_from_ofp10(opp->peer);
4304
4305 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
4306 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
4307
4308 return 0;
4309 }
4310
4311 static enum ofperr
4312 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
4313 const struct ofp11_port *op)
4314 {
4315 enum ofperr error;
4316
4317 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4318 if (error) {
4319 return error;
4320 }
4321 pp->hw_addr = op->hw_addr;
4322 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
4323
4324 pp->config = ntohl(op->config) & OFPPC11_ALL;
4325 pp->state = ntohl(op->state) & OFPPS11_ALL;
4326
4327 pp->curr = netdev_port_features_from_ofp11(op->curr);
4328 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
4329 pp->supported = netdev_port_features_from_ofp11(op->supported);
4330 pp->peer = netdev_port_features_from_ofp11(op->peer);
4331
4332 pp->curr_speed = ntohl(op->curr_speed);
4333 pp->max_speed = ntohl(op->max_speed);
4334
4335 return 0;
4336 }
4337
4338 static enum ofperr
4339 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
4340 struct ofputil_phy_port *pp)
4341 {
4342 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
4343
4344 if (payload->size != sizeof *eth) {
4345 return OFPERR_OFPBPC_BAD_LEN;
4346 }
4347
4348 pp->curr = netdev_port_features_from_ofp11(eth->curr);
4349 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
4350 pp->supported = netdev_port_features_from_ofp11(eth->supported);
4351 pp->peer = netdev_port_features_from_ofp11(eth->peer);
4352
4353 pp->curr_speed = ntohl(eth->curr_speed);
4354 pp->max_speed = ntohl(eth->max_speed);
4355
4356 return 0;
4357 }
4358
4359 static enum ofperr
4360 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
4361 {
4362 struct ofp14_port *op = ofpbuf_try_pull(msg, sizeof *op);
4363 if (!op) {
4364 return OFPERR_OFPBRC_BAD_LEN;
4365 }
4366
4367 size_t len = ntohs(op->length);
4368 if (len < sizeof *op || len - sizeof *op > msg->size) {
4369 return OFPERR_OFPBRC_BAD_LEN;
4370 }
4371 len -= sizeof *op;
4372
4373 enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4374 if (error) {
4375 return error;
4376 }
4377 pp->hw_addr = op->hw_addr;
4378 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
4379
4380 pp->config = ntohl(op->config) & OFPPC11_ALL;
4381 pp->state = ntohl(op->state) & OFPPS11_ALL;
4382
4383 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
4384 len);
4385 while (properties.size > 0) {
4386 struct ofpbuf payload;
4387 enum ofperr error;
4388 uint64_t type;
4389
4390 error = ofpprop_pull(&properties, &payload, &type);
4391 if (error) {
4392 return error;
4393 }
4394
4395 switch (type) {
4396 case OFPPDPT14_ETHERNET:
4397 error = parse_ofp14_port_ethernet_property(&payload, pp);
4398 break;
4399
4400 default:
4401 error = OFPPROP_UNKNOWN(true, "port", type);
4402 break;
4403 }
4404
4405 if (error) {
4406 return error;
4407 }
4408 }
4409
4410 return 0;
4411 }
4412
4413 static void
4414 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
4415 struct ofp10_phy_port *opp)
4416 {
4417 memset(opp, 0, sizeof *opp);
4418
4419 opp->port_no = htons(ofp_to_u16(pp->port_no));
4420 opp->hw_addr = pp->hw_addr;
4421 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
4422
4423 opp->config = htonl(pp->config & OFPPC10_ALL);
4424 opp->state = htonl(pp->state & OFPPS10_ALL);
4425
4426 opp->curr = netdev_port_features_to_ofp10(pp->curr);
4427 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
4428 opp->supported = netdev_port_features_to_ofp10(pp->supported);
4429 opp->peer = netdev_port_features_to_ofp10(pp->peer);
4430 }
4431
4432 static void
4433 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
4434 struct ofp11_port *op)
4435 {
4436 memset(op, 0, sizeof *op);
4437
4438 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4439 op->hw_addr = pp->hw_addr;
4440 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
4441
4442 op->config = htonl(pp->config & OFPPC11_ALL);
4443 op->state = htonl(pp->state & OFPPS11_ALL);
4444
4445 op->curr = netdev_port_features_to_ofp11(pp->curr);
4446 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
4447 op->supported = netdev_port_features_to_ofp11(pp->supported);
4448 op->peer = netdev_port_features_to_ofp11(pp->peer);
4449
4450 op->curr_speed = htonl(pp->curr_speed);
4451 op->max_speed = htonl(pp->max_speed);
4452 }
4453
4454 static void
4455 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
4456 struct ofpbuf *b)
4457 {
4458 struct ofp14_port *op;
4459 struct ofp14_port_desc_prop_ethernet *eth;
4460
4461 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
4462
4463 op = ofpbuf_put_zeros(b, sizeof *op);
4464 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4465 op->length = htons(sizeof *op + sizeof *eth);
4466 op->hw_addr = pp->hw_addr;
4467 ovs_strlcpy(op->name, pp->name, sizeof op->name);
4468 op->config = htonl(pp->config & OFPPC11_ALL);
4469 op->state = htonl(pp->state & OFPPS11_ALL);
4470
4471 eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
4472 eth->curr = netdev_port_features_to_ofp11(pp->curr);
4473 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
4474 eth->supported = netdev_port_features_to_ofp11(pp->supported);
4475 eth->peer = netdev_port_features_to_ofp11(pp->peer);
4476 eth->curr_speed = htonl(pp->curr_speed);
4477 eth->max_speed = htonl(pp->max_speed);
4478 }
4479
4480 static void
4481 ofputil_put_phy_port(enum ofp_version ofp_version,
4482 const struct ofputil_phy_port *pp, struct ofpbuf *b)
4483 {
4484 switch (ofp_version) {
4485 case OFP10_VERSION: {
4486 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
4487 ofputil_encode_ofp10_phy_port(pp, opp);
4488 break;
4489 }
4490
4491 case OFP11_VERSION:
4492 case OFP12_VERSION:
4493 case OFP13_VERSION: {
4494 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
4495 ofputil_encode_ofp11_port(pp, op);
4496 break;
4497 }
4498
4499 case OFP14_VERSION:
4500 case OFP15_VERSION:
4501 case OFP16_VERSION:
4502 ofputil_put_ofp14_port(pp, b);
4503 break;
4504
4505 default:
4506 OVS_NOT_REACHED();
4507 }
4508 }
4509
4510 enum ofperr
4511 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4512 ofp_port_t *port)
4513 {
4514 struct ofpbuf b = ofpbuf_const_initializer(request,
4515 ntohs(request->length));
4516 enum ofpraw raw = ofpraw_pull_assert(&b);
4517 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4518 *port = OFPP_ANY;
4519 return 0;
4520 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4521 ovs_be32 *ofp11_port;
4522
4523 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4524 return ofputil_port_from_ofp11(*ofp11_port, port);
4525 } else {
4526 OVS_NOT_REACHED();
4527 }
4528 }
4529
4530 struct ofpbuf *
4531 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4532 ofp_port_t port)
4533 {
4534 struct ofpbuf *request;
4535
4536 switch (ofp_version) {
4537 case OFP10_VERSION:
4538 case OFP11_VERSION:
4539 case OFP12_VERSION:
4540 case OFP13_VERSION:
4541 case OFP14_VERSION:
4542 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4543 ofp_version, 0);
4544 break;
4545 case OFP15_VERSION:
4546 case OFP16_VERSION:{
4547 struct ofp15_port_desc_request *req;
4548 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4549 ofp_version, 0);
4550 req = ofpbuf_put_zeros(request, sizeof *req);
4551 req->port_no = ofputil_port_to_ofp11(port);
4552 break;
4553 }
4554 default:
4555 OVS_NOT_REACHED();
4556 }
4557
4558 return request;
4559 }
4560
4561 void
4562 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4563 struct ovs_list *replies)
4564 {
4565 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
4566 size_t start_ofs = reply->size;
4567
4568 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4569 ofpmp_postappend(replies, start_ofs);
4570 }
4571 \f
4572 /* ofputil_switch_config */
4573
4574 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4575 * message, into 'config'. Returns false if 'oh' contained any flags that
4576 * aren't specified in its version of OpenFlow, true otherwise. */
4577 static bool
4578 ofputil_decode_switch_config(const struct ofp_header *oh,
4579 struct ofputil_switch_config *config)
4580 {
4581 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4582 ofpraw_pull_assert(&b);
4583
4584 const struct ofp_switch_config *osc = ofpbuf_pull(&b, sizeof *osc);
4585 config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
4586 config->miss_send_len = ntohs(osc->miss_send_len);
4587
4588 ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
4589 if (oh->version < OFP13_VERSION) {
4590 const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4591 valid_mask |= ttl_bit;
4592 config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4593 } else {
4594 config->invalid_ttl_to_controller = -1;
4595 }
4596
4597 return !(osc->flags & ~valid_mask);
4598 }
4599
4600 void
4601 ofputil_decode_get_config_reply(const struct ofp_header *oh,
4602 struct ofputil_switch_config *config)
4603 {
4604 ofputil_decode_switch_config(oh, config);
4605 }
4606
4607 enum ofperr
4608 ofputil_decode_set_config(const struct ofp_header *oh,
4609 struct ofputil_switch_config *config)
4610 {
4611 return (ofputil_decode_switch_config(oh, config)
4612 ? 0
4613 : OFPERR_OFPSCFC_BAD_FLAGS);
4614 }
4615
4616 static struct ofpbuf *
4617 ofputil_put_switch_config(const struct ofputil_switch_config *config,
4618 struct ofpbuf *b)
4619 {
4620 const struct ofp_header *oh = b->data;
4621 struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4622 osc->flags = htons(config->frag);
4623 if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4624 osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4625 }
4626 osc->miss_send_len = htons(config->miss_send_len);
4627 return b;
4628 }
4629
4630 struct ofpbuf *
4631 ofputil_encode_get_config_reply(const struct ofp_header *request,
4632 const struct ofputil_switch_config *config)
4633 {
4634 struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4635 request, 0);
4636 return ofputil_put_switch_config(config, b);
4637 }
4638
4639 struct ofpbuf *
4640 ofputil_encode_set_config(const struct ofputil_switch_config *config,
4641 enum ofp_version version)
4642 {
4643 struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4644 return ofputil_put_switch_config(config, b);
4645 }
4646 \f
4647 /* ofputil_switch_features */
4648
4649 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4650 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4651 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4652 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4653 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4654 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4655 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4656 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4657
4658 static uint32_t
4659 ofputil_capabilities_mask(enum ofp_version ofp_version)
4660 {
4661 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4662 switch (ofp_version) {
4663 case OFP10_VERSION:
4664 case OFP11_VERSION:
4665 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4666 case OFP12_VERSION:
4667 case OFP13_VERSION:
4668 case OFP14_VERSION:
4669 case OFP15_VERSION:
4670 case OFP16_VERSION:
4671 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4672 default:
4673 /* Caller needs to check osf->header.version itself */
4674 return 0;
4675 }
4676 }
4677
4678 /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4679 * an abstract representation in '*features', readying 'b' to iterate over the
4680 * OpenFlow port structures following 'osf' with later calls to
4681 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4682 * value. */
4683 enum ofperr
4684 ofputil_pull_switch_features(struct ofpbuf *b,
4685 struct ofputil_switch_features *features)
4686 {
4687 const struct ofp_header *oh = b->data;
4688 enum ofpraw raw = ofpraw_pull_assert(b);
4689 const struct ofp_switch_features *osf = ofpbuf_pull(b, sizeof *osf);
4690 features->datapath_id = ntohll(osf->datapath_id);
4691 features->n_buffers = ntohl(osf->n_buffers);
4692 features->n_tables = osf->n_tables;
4693 features->auxiliary_id = 0;
4694
4695 features->capabilities = ntohl(osf->capabilities) &
4696 ofputil_capabilities_mask(oh->version);
4697
4698 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4699 if (osf->capabilities & htonl(OFPC10_STP)) {
4700 features->capabilities |= OFPUTIL_C_STP;
4701 }
4702 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4703 OFP10_VERSION);
4704 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4705 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4706 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4707 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4708 }
4709 features->ofpacts = 0;
4710 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4711 features->auxiliary_id = osf->auxiliary_id;
4712 }
4713 } else {
4714 return OFPERR_OFPBRC_BAD_VERSION;
4715 }
4716
4717 return 0;
4718 }
4719
4720 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4721 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4722 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4723 *
4724 * Given a buffer 'b' that contains a Features Reply message, this message
4725 * checks if it contains a complete list of the switch's ports. Returns true,
4726 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4727 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4728 * message.
4729 *
4730 * When this function returns false, the caller should send an OFPST_PORT_DESC
4731 * stats request to get the ports. */
4732 bool
4733 ofputil_switch_features_has_ports(struct ofpbuf *b)
4734 {
4735 struct ofp_header *oh = b->data;
4736 size_t phy_port_size;
4737
4738 if (oh->version >= OFP13_VERSION) {
4739 /* OpenFlow 1.3+ never has ports in the feature reply. */
4740 return false;
4741 }
4742
4743 phy_port_size = (oh->version == OFP10_VERSION
4744 ? sizeof(struct ofp10_phy_port)
4745 : sizeof(struct ofp11_port));
4746 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4747 /* There's room for additional ports in the feature reply.
4748 * Assume that the list is complete. */
4749 return true;
4750 }
4751
4752 /* The feature reply has no room for more ports. Probably the list is
4753 * truncated. Drop the ports and tell the caller to retrieve them with
4754 * OFPST_PORT_DESC. */
4755 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4756 ofpmsg_update_length(b);
4757 return false;
4758 }
4759
4760 /* Returns a buffer owned by the caller that encodes 'features' in the format
4761 * required by 'protocol' with the given 'xid'. The caller should append port
4762 * information to the buffer with subsequent calls to
4763 * ofputil_put_switch_features_port(). */
4764 struct ofpbuf *
4765 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4766 enum ofputil_protocol protocol, ovs_be32 xid)
4767 {
4768 struct ofp_switch_features *osf;
4769 struct ofpbuf *b;
4770 enum ofp_version version;
4771 enum ofpraw raw;
4772
4773 version = ofputil_protocol_to_ofp_version(protocol);
4774 switch (version) {
4775 case OFP10_VERSION:
4776 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4777 break;
4778 case OFP11_VERSION:
4779 case OFP12_VERSION:
4780 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4781 break;
4782 case OFP13_VERSION:
4783 case OFP14_VERSION:
4784 case OFP15_VERSION:
4785 case OFP16_VERSION:
4786 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4787 break;
4788 default:
4789 OVS_NOT_REACHED();
4790 }
4791 b = ofpraw_alloc_xid(raw, version, xid, 0);
4792 osf = ofpbuf_put_zeros(b, sizeof *osf);
4793 osf->datapath_id = htonll(features->datapath_id);
4794 osf->n_buffers = htonl(features->n_buffers);
4795 osf->n_tables = features->n_tables;
4796
4797 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4798 osf->capabilities = htonl(features->capabilities &
4799 ofputil_capabilities_mask(version));
4800 switch (version) {
4801 case OFP10_VERSION:
4802 if (features->capabilities & OFPUTIL_C_STP) {
4803 osf->capabilities |= htonl(OFPC10_STP);
4804 }
4805 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4806 OFP10_VERSION);
4807 break;
4808 case OFP13_VERSION:
4809 case OFP14_VERSION:
4810 case OFP15_VERSION:
4811 case OFP16_VERSION:
4812 osf->auxiliary_id = features->auxiliary_id;
4813 /* fall through */
4814 case OFP11_VERSION:
4815 case OFP12_VERSION:
4816 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4817 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4818 }
4819 break;
4820 default:
4821 OVS_NOT_REACHED();
4822 }
4823
4824 return b;
4825 }
4826
4827 /* Encodes 'pp' into the format required by the switch_features message already
4828 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4829 * and appends the encoded version to 'b'. */
4830 void
4831 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4832 struct ofpbuf *b)
4833 {
4834 const struct ofp_header *oh = b->data;
4835
4836 if (oh->version < OFP13_VERSION) {
4837 /* Try adding a port description to the message, but drop it again if
4838 * the buffer overflows. (This possibility for overflow is why
4839 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4840 size_t start_ofs = b->size;
4841 ofputil_put_phy_port(oh->version, pp, b);
4842 if (b->size > UINT16_MAX) {
4843 b->size = start_ofs;
4844 }
4845 }
4846 }
4847 \f
4848 /* ofputil_port_status */
4849
4850 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4851 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4852 enum ofperr
4853 ofputil_decode_port_status(const struct ofp_header *oh,
4854 struct ofputil_port_status *ps)
4855 {
4856 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4857 ofpraw_pull_assert(&b);
4858
4859 const struct ofp_port_status *ops = ofpbuf_pull(&b, sizeof *ops);
4860 if (ops->reason != OFPPR_ADD &&
4861 ops->reason != OFPPR_DELETE &&
4862 ops->reason != OFPPR_MODIFY) {
4863 return OFPERR_NXBRC_BAD_REASON;
4864 }
4865 ps->reason = ops->reason;
4866
4867 int retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4868 ovs_assert(retval != EOF);
4869 return retval;
4870 }
4871
4872 /* Converts the abstract form of a "port status" message in '*ps' into an
4873 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4874 * a buffer owned by the caller. */
4875 struct ofpbuf *
4876 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4877 enum ofputil_protocol protocol)
4878 {
4879 struct ofp_port_status *ops;
4880 struct ofpbuf *b;
4881 enum ofp_version version;
4882 enum ofpraw raw;
4883
4884 version = ofputil_protocol_to_ofp_version(protocol);
4885 switch (version) {
4886 case OFP10_VERSION:
4887 raw = OFPRAW_OFPT10_PORT_STATUS;
4888 break;
4889
4890 case OFP11_VERSION:
4891 case OFP12_VERSION:
4892 case OFP13_VERSION:
4893 raw = OFPRAW_OFPT11_PORT_STATUS;
4894 break;
4895
4896 case OFP14_VERSION:
4897 case OFP15_VERSION:
4898 case OFP16_VERSION:
4899 raw = OFPRAW_OFPT14_PORT_STATUS;
4900 break;
4901
4902 default:
4903 OVS_NOT_REACHED();
4904 }
4905
4906 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4907 ops = ofpbuf_put_zeros(b, sizeof *ops);
4908 ops->reason = ps->reason;
4909 ofputil_put_phy_port(version, &ps->desc, b);
4910 ofpmsg_update_length(b);
4911 return b;
4912 }
4913
4914 /* ofputil_port_mod */
4915
4916 static enum ofperr
4917 parse_port_mod_ethernet_property(struct ofpbuf *property,
4918 struct ofputil_port_mod *pm)
4919 {
4920 ovs_be32 advertise;
4921 enum ofperr error;
4922
4923 error = ofpprop_parse_be32(property, &advertise);
4924 if (!error) {
4925 pm->advertise = netdev_port_features_from_ofp11(advertise);
4926 }
4927 return error;
4928 }
4929
4930 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4931 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4932 enum ofperr
4933 ofputil_decode_port_mod(const struct ofp_header *oh,
4934 struct ofputil_port_mod *pm, bool loose)
4935 {
4936 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4937 enum ofpraw raw = ofpraw_pull_assert(&b);
4938 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4939 const struct ofp10_port_mod *opm = b.data;
4940
4941 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4942 pm->hw_addr = opm->hw_addr;
4943 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4944 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4945 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4946 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4947 const struct ofp11_port_mod *opm = b.data;
4948 enum ofperr error;
4949
4950 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4951 if (error) {
4952 return error;
4953 }
4954
4955 pm->hw_addr = opm->hw_addr;
4956 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4957 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4958 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4959 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4960 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4961 enum ofperr error;
4962
4963 memset(pm, 0, sizeof *pm);
4964
4965 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4966 if (error) {
4967 return error;
4968 }
4969
4970 pm->hw_addr = opm->hw_addr;
4971 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4972 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4973
4974 while (b.size > 0) {
4975 struct ofpbuf property;
4976 enum ofperr error;
4977 uint64_t type;
4978
4979 error = ofpprop_pull(&b, &property, &type);
4980 if (error) {
4981 return error;
4982 }
4983
4984 switch (type) {
4985 case OFPPMPT14_ETHERNET:
4986 error = parse_port_mod_ethernet_property(&property, pm);
4987 break;
4988
4989 default:
4990 error = OFPPROP_UNKNOWN(loose, "port_mod", type);
4991 break;
4992 }
4993
4994 if (error) {
4995 return error;
4996 }
4997 }
4998 } else {
4999 return OFPERR_OFPBRC_BAD_TYPE;
5000 }
5001
5002 pm->config &= pm->mask;
5003 return 0;
5004 }
5005
5006 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
5007 * message suitable for 'protocol', and returns that encoded form in a buffer
5008 * owned by the caller. */
5009 struct ofpbuf *
5010 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
5011 enum ofputil_protocol protocol)
5012 {
5013 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5014 struct ofpbuf *b;
5015
5016 switch (ofp_version) {
5017 case OFP10_VERSION: {
5018 struct ofp10_port_mod *opm;
5019
5020 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
5021 opm = ofpbuf_put_zeros(b, sizeof *opm);
5022 opm->port_no = htons(ofp_to_u16(pm->port_no));
5023 opm->hw_addr = pm->hw_addr;
5024 opm->config = htonl(pm->config & OFPPC10_ALL);
5025 opm->mask = htonl(pm->mask & OFPPC10_ALL);
5026 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
5027 break;
5028 }
5029
5030 case OFP11_VERSION:
5031 case OFP12_VERSION:
5032 case OFP13_VERSION: {
5033 struct ofp11_port_mod *opm;
5034
5035 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
5036 opm = ofpbuf_put_zeros(b, sizeof *opm);
5037 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
5038 opm->hw_addr = pm->hw_addr;
5039 opm->config = htonl(pm->config & OFPPC11_ALL);
5040 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5041 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
5042 break;
5043 }
5044 case OFP14_VERSION:
5045 case OFP15_VERSION:
5046 case OFP16_VERSION: {
5047 struct ofp14_port_mod *opm;
5048
5049 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, 0);
5050 opm = ofpbuf_put_zeros(b, sizeof *opm);
5051 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
5052 opm->hw_addr = pm->hw_addr;
5053 opm->config = htonl(pm->config & OFPPC11_ALL);
5054 opm->mask = htonl(pm->mask & OFPPC11_ALL);
5055
5056 if (pm->advertise) {
5057 ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
5058 netdev_port_features_to_ofp11(pm->advertise));
5059 }
5060 break;
5061 }
5062 default:
5063 OVS_NOT_REACHED();
5064 }
5065
5066 return b;
5067 }
5068 \f
5069 /* Table features. */
5070
5071 static enum ofperr
5072 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
5073 uint64_t *typep)
5074 {
5075 enum ofperr error;
5076
5077 error = ofpprop_pull(msg, payload, typep);
5078 if (payload && !error) {
5079 ofpbuf_pull(payload, (char *)payload->msg - (char *)payload->header);
5080 }
5081 return error;
5082 }
5083
5084 static enum ofperr
5085 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
5086 uint64_t *ofpacts)
5087 {
5088 uint32_t types = 0;
5089
5090 while (payload->size > 0) {
5091 enum ofperr error;
5092 uint64_t type;
5093
5094 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &type);
5095 if (error) {
5096 return error;
5097 }
5098 if (type < CHAR_BIT * sizeof types) {
5099 types |= 1u << type;
5100 }
5101 }
5102
5103 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
5104 return 0;
5105 }
5106
5107 static enum ofperr
5108 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
5109 {
5110 *insts = 0;
5111 while (payload->size > 0) {
5112 enum ovs_instruction_type inst;
5113 enum ofperr error;
5114 uint64_t ofpit;
5115
5116 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5117 * It seems clear that they aren't padded to 8 bytes, though, because
5118 * both standards say that "non-experimenter instructions are 4 bytes"
5119 * and do not mention any padding before the first instruction ID.
5120 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5121 * aligned on an odd 4-byte boundary.)
5122 *
5123 * Anyway, we just assume they're all glommed together on byte
5124 * boundaries. */
5125 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &ofpit);
5126 if (error) {
5127 return error;
5128 }
5129
5130 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
5131 if (!error) {
5132 *insts |= 1u << inst;
5133 } else if (!loose) {
5134 return error;
5135 }
5136 }
5137 return 0;
5138 }
5139
5140 static enum ofperr
5141 parse_table_features_next_table(struct ofpbuf *payload,
5142 unsigned long int *next_tables)
5143 {
5144 size_t i;
5145
5146 memset(next_tables, 0, bitmap_n_bytes(255));
5147 for (i = 0; i < payload->size; i++) {
5148 uint8_t id = ((const uint8_t *) payload->data)[i];
5149 if (id >= 255) {
5150 return OFPERR_OFPBPC_BAD_VALUE;
5151 }
5152 bitmap_set1(next_tables, id);
5153 }
5154 return 0;
5155 }
5156
5157 static enum ofperr
5158 parse_oxms(struct ofpbuf *payload, bool loose,
5159 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
5160 {
5161 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
5162 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
5163
5164 while (payload->size > 0) {
5165 const struct mf_field *field;
5166 enum ofperr error;
5167 bool hasmask;
5168
5169 error = nx_pull_header(payload, &field, &hasmask);
5170 if (!error) {
5171 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
5172 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
5173 return error;
5174 }
5175 }
5176 if (exactp) {
5177 *exactp = exact;
5178 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
5179 return OFPERR_OFPBMC_BAD_MASK;
5180 }
5181 if (maskedp) {
5182 *maskedp = masked;
5183 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
5184 return OFPERR_OFPBMC_BAD_MASK;
5185 }
5186 return 0;
5187 }
5188
5189 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5190 * ofputil_table_features in 'tf'.
5191 *
5192 * If 'loose' is true, this function ignores properties and values that it does
5193 * not understand, as a controller would want to do when interpreting
5194 * capabilities provided by a switch. If 'loose' is false, this function
5195 * treats unknown properties and values as an error, as a switch would want to
5196 * do when interpreting a configuration request made by a controller.
5197 *
5198 * A single OpenFlow message can specify features for multiple tables. Calling
5199 * this function multiple times for a single 'msg' iterates through the tables
5200 * in the message. The caller must initially leave 'msg''s layer pointers null
5201 * and not modify them between calls.
5202 *
5203 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5204 * a positive "enum ofperr" value. */
5205 int
5206 ofputil_decode_table_features(struct ofpbuf *msg,
5207 struct ofputil_table_features *tf, bool loose)
5208 {
5209 memset(tf, 0, sizeof *tf);
5210
5211 if (!msg->header) {
5212 ofpraw_pull_assert(msg);
5213 }
5214
5215 if (!msg->size) {
5216 return EOF;
5217 }
5218
5219 const struct ofp_header *oh = msg->header;
5220 struct ofp13_table_features *otf = msg->data;
5221 if (msg->size < sizeof *otf) {
5222 return OFPERR_OFPBPC_BAD_LEN;
5223 }
5224
5225 unsigned int len = ntohs(otf->length);
5226 if (len < sizeof *otf || len % 8 || len > msg->size) {
5227 return OFPERR_OFPBPC_BAD_LEN;
5228 }
5229
5230 tf->table_id = otf->table_id;
5231 if (tf->table_id == OFPTT_ALL) {
5232 return OFPERR_OFPTFFC_BAD_TABLE;
5233 }
5234
5235 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
5236 tf->metadata_match = otf->metadata_match;
5237 tf->metadata_write = otf->metadata_write;
5238 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
5239 if (oh->version >= OFP14_VERSION) {
5240 uint32_t caps = ntohl(otf->capabilities);
5241 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
5242 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
5243 } else {
5244 tf->supports_eviction = -1;
5245 tf->supports_vacancy_events = -1;
5246 }
5247 tf->max_entries = ntohl(otf->max_entries);
5248
5249 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
5250 len);
5251 ofpbuf_pull(&properties, sizeof *otf);
5252 while (properties.size > 0) {
5253 struct ofpbuf payload;
5254 enum ofperr error;
5255 uint64_t type;
5256
5257 error = pull_table_feature_property(&properties, &payload, &type);
5258 if (error) {
5259 return error;
5260 }
5261
5262 switch ((enum ofp13_table_feature_prop_type) type) {
5263 case OFPTFPT13_INSTRUCTIONS:
5264 error = parse_instruction_ids(&payload, loose,
5265 &tf->nonmiss.instructions);
5266 break;
5267 case OFPTFPT13_INSTRUCTIONS_MISS:
5268 error = parse_instruction_ids(&payload, loose,
5269 &tf->miss.instructions);
5270 break;
5271
5272 case OFPTFPT13_NEXT_TABLES:
5273 error = parse_table_features_next_table(&payload,
5274 tf->nonmiss.next);
5275 break;
5276 case OFPTFPT13_NEXT_TABLES_MISS:
5277 error = parse_table_features_next_table(&payload, tf->miss.next);
5278 break;
5279
5280 case OFPTFPT13_WRITE_ACTIONS:
5281 error = parse_action_bitmap(&payload, oh->version,
5282 &tf->nonmiss.write.ofpacts);
5283 break;
5284 case OFPTFPT13_WRITE_ACTIONS_MISS:
5285 error = parse_action_bitmap(&payload, oh->version,
5286 &tf->miss.write.ofpacts);
5287 break;
5288
5289 case OFPTFPT13_APPLY_ACTIONS:
5290 error = parse_action_bitmap(&payload, oh->version,
5291 &tf->nonmiss.apply.ofpacts);
5292 break;
5293 case OFPTFPT13_APPLY_ACTIONS_MISS:
5294 error = parse_action_bitmap(&payload, oh->version,
5295 &tf->miss.apply.ofpacts);
5296 break;
5297
5298 case OFPTFPT13_MATCH:
5299 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
5300 break;
5301 case OFPTFPT13_WILDCARDS:
5302 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
5303 break;
5304
5305 case OFPTFPT13_WRITE_SETFIELD:
5306 error = parse_oxms(&payload, loose,
5307 &tf->nonmiss.write.set_fields, NULL);
5308 break;
5309 case OFPTFPT13_WRITE_SETFIELD_MISS:
5310 error = parse_oxms(&payload, loose,
5311 &tf->miss.write.set_fields, NULL);
5312 break;
5313 case OFPTFPT13_APPLY_SETFIELD:
5314 error = parse_oxms(&payload, loose,
5315 &tf->nonmiss.apply.set_fields, NULL);
5316 break;
5317 case OFPTFPT13_APPLY_SETFIELD_MISS:
5318 error = parse_oxms(&payload, loose,
5319 &tf->miss.apply.set_fields, NULL);
5320 break;
5321
5322 case OFPTFPT13_EXPERIMENTER:
5323 case OFPTFPT13_EXPERIMENTER_MISS:
5324 default:
5325 error = OFPPROP_UNKNOWN(loose, "table features", type);
5326 break;
5327 }
5328 if (error) {
5329 return error;
5330 }
5331 }
5332
5333 /* Fix inconsistencies:
5334 *
5335 * - Turn on 'match' bits that are set in 'mask', because maskable
5336 * fields are matchable.
5337 *
5338 * - Turn on 'wildcard' bits that are set in 'mask', because a field
5339 * that is arbitrarily maskable can be wildcarded entirely.
5340 *
5341 * - Turn off 'wildcard' bits that are not in 'match', because a field
5342 * must be matchable for it to be meaningfully wildcarded. */
5343 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
5344 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
5345 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
5346
5347 return 0;
5348 }
5349
5350 /* Encodes and returns a request to obtain the table features of a switch.
5351 * The message is encoded for OpenFlow version 'ofp_version'. */
5352 struct ofpbuf *
5353 ofputil_encode_table_features_request(enum ofp_version ofp_version)
5354 {
5355 struct ofpbuf *request = NULL;
5356
5357 switch (ofp_version) {
5358 case OFP10_VERSION:
5359 case OFP11_VERSION:
5360 case OFP12_VERSION:
5361 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5362 "(\'-O OpenFlow13\')");
5363 case OFP13_VERSION:
5364 case OFP14_VERSION:
5365 case OFP15_VERSION:
5366 case OFP16_VERSION:
5367 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
5368 ofp_version, 0);
5369 break;
5370 default:
5371 OVS_NOT_REACHED();
5372 }
5373
5374 return request;
5375 }
5376
5377 static void
5378 put_fields_property(struct ofpbuf *reply,
5379 const struct mf_bitmap *fields,
5380 const struct mf_bitmap *masks,
5381 enum ofp13_table_feature_prop_type property,
5382 enum ofp_version version)
5383 {
5384 size_t start_ofs;
5385 int field;
5386
5387 start_ofs = ofpprop_start(reply, property);
5388 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
5389 nx_put_header(reply, field, version,
5390 masks && bitmap_is_set(masks->bm, field));
5391 }
5392 ofpprop_end(reply, start_ofs);
5393 }
5394
5395 static void
5396 put_table_action_features(struct ofpbuf *reply,
5397 const struct ofputil_table_action_features *taf,
5398 enum ofp13_table_feature_prop_type actions_type,
5399 enum ofp13_table_feature_prop_type set_fields_type,
5400 int miss_offset, enum ofp_version version)
5401 {
5402 ofpprop_put_bitmap(reply, actions_type + miss_offset,
5403 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
5404 version)));
5405 put_fields_property(reply, &taf->set_fields, NULL,
5406 set_fields_type + miss_offset, version);
5407 }
5408
5409 static void
5410 put_table_instruction_features(
5411 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
5412 int miss_offset, enum ofp_version version)
5413 {
5414 size_t start_ofs;
5415 uint8_t table_id;
5416
5417 ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
5418 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
5419 version)));
5420
5421 start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
5422 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
5423 ofpbuf_put(reply, &table_id, 1);
5424 }
5425 ofpprop_end(reply, start_ofs);
5426
5427 put_table_action_features(reply, &tif->write,
5428 OFPTFPT13_WRITE_ACTIONS,
5429 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
5430 put_table_action_features(reply, &tif->apply,
5431 OFPTFPT13_APPLY_ACTIONS,
5432 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
5433 }
5434
5435 void
5436 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
5437 struct ovs_list *replies)
5438 {
5439 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
5440 enum ofp_version version = ofpmp_version(replies);
5441 size_t start_ofs = reply->size;
5442 struct ofp13_table_features *otf;
5443
5444 otf = ofpbuf_put_zeros(reply, sizeof *otf);
5445 otf->table_id = tf->table_id;
5446 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
5447 otf->metadata_match = tf->metadata_match;
5448 otf->metadata_write = tf->metadata_write;
5449 if (version >= OFP14_VERSION) {
5450 if (tf->supports_eviction) {
5451 otf->capabilities |= htonl(OFPTC14_EVICTION);
5452 }
5453 if (tf->supports_vacancy_events) {
5454 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
5455 }
5456 }
5457 otf->max_entries = htonl(tf->max_entries);
5458
5459 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
5460 put_table_instruction_features(reply, &tf->miss, 1, version);
5461
5462 put_fields_property(reply, &tf->match, &tf->mask,
5463 OFPTFPT13_MATCH, version);
5464 put_fields_property(reply, &tf->wildcard, NULL,
5465 OFPTFPT13_WILDCARDS, version);
5466
5467 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
5468 otf->length = htons(reply->size - start_ofs);
5469 ofpmp_postappend(replies, start_ofs);
5470 }
5471
5472 static enum ofperr
5473 parse_table_desc_vacancy_property(struct ofpbuf *property,
5474 struct ofputil_table_desc *td)
5475 {
5476 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5477
5478 if (property->size != sizeof *otv) {
5479 return OFPERR_OFPBPC_BAD_LEN;
5480 }
5481
5482 td->table_vacancy.vacancy_down = otv->vacancy_down;
5483 td->table_vacancy.vacancy_up = otv->vacancy_up;
5484 td->table_vacancy.vacancy = otv->vacancy;
5485 return 0;
5486 }
5487
5488 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5489 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5490 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5491 * value. */
5492 int
5493 ofputil_decode_table_desc(struct ofpbuf *msg,
5494 struct ofputil_table_desc *td,
5495 enum ofp_version version)
5496 {
5497 memset(td, 0, sizeof *td);
5498
5499 if (!msg->header) {
5500 ofpraw_pull_assert(msg);
5501 }
5502
5503 if (!msg->size) {
5504 return EOF;
5505 }
5506
5507 struct ofp14_table_desc *otd = ofpbuf_try_pull(msg, sizeof *otd);
5508 if (!otd) {
5509 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
5510 "leftover bytes at end", msg->size);
5511 return OFPERR_OFPBRC_BAD_LEN;
5512 }
5513
5514 td->table_id = otd->table_id;
5515 size_t length = ntohs(otd->length);
5516 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
5517 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
5518 "length %"PRIuSIZE, length);
5519 return OFPERR_OFPBRC_BAD_LEN;
5520 }
5521 length -= sizeof *otd;
5522
5523 td->eviction = ofputil_decode_table_eviction(otd->config, version);
5524 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
5525 td->eviction_flags = UINT32_MAX;
5526
5527 struct ofpbuf properties = ofpbuf_const_initializer(
5528 ofpbuf_pull(msg, length), length);
5529 while (properties.size > 0) {
5530 struct ofpbuf payload;
5531 enum ofperr error;
5532 uint64_t type;
5533
5534 error = ofpprop_pull(&properties, &payload, &type);
5535 if (error) {
5536 return error;
5537 }
5538
5539 switch (type) {
5540 case OFPTMPT14_EVICTION:
5541 error = ofpprop_parse_u32(&payload, &td->eviction_flags);
5542 break;
5543
5544 case OFPTMPT14_VACANCY:
5545 error = parse_table_desc_vacancy_property(&payload, td);
5546 break;
5547
5548 default:
5549 error = OFPPROP_UNKNOWN(true, "table_desc", type);
5550 break;
5551 }
5552
5553 if (error) {
5554 return error;
5555 }
5556 }
5557
5558 return 0;
5559 }
5560
5561 /* Encodes and returns a request to obtain description of tables of a switch.
5562 * The message is encoded for OpenFlow version 'ofp_version'. */
5563 struct ofpbuf *
5564 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5565 {
5566 struct ofpbuf *request = NULL;
5567
5568 if (ofp_version >= OFP14_VERSION) {
5569 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5570 ofp_version, 0);
5571 } else {
5572 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5573 "(\'-O OpenFlow14\')");
5574 }
5575
5576 return request;
5577 }
5578
5579 /* Function to append Table desc information in a reply list. */
5580 void
5581 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5582 struct ovs_list *replies,
5583 enum ofp_version version)
5584 {
5585 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
5586 size_t start_otd;
5587 struct ofp14_table_desc *otd;
5588
5589 start_otd = reply->size;
5590 ofpbuf_put_zeros(reply, sizeof *otd);
5591 if (td->eviction_flags != UINT32_MAX) {
5592 ofpprop_put_u32(reply, OFPTMPT14_EVICTION, td->eviction_flags);
5593 }
5594 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5595 struct ofp14_table_mod_prop_vacancy *otv;
5596
5597 otv = ofpprop_put_zeros(reply, OFPTMPT14_VACANCY, sizeof *otv);
5598 otv->vacancy_down = td->table_vacancy.vacancy_down;
5599 otv->vacancy_up = td->table_vacancy.vacancy_up;
5600 otv->vacancy = td->table_vacancy.vacancy;
5601 }
5602
5603 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5604 otd->length = htons(reply->size - start_otd);
5605 otd->table_id = td->table_id;
5606 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5607 td->eviction, td->vacancy,
5608 version);
5609 ofpmp_postappend(replies, start_otd);
5610 }
5611
5612 /* This function parses Vacancy property, and decodes the
5613 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5614 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5615 * greater than vacancy_up and also when current vacancy has non-zero
5616 * value. Returns 0 on success. */
5617 static enum ofperr
5618 parse_table_mod_vacancy_property(struct ofpbuf *property,
5619 struct ofputil_table_mod *tm)
5620 {
5621 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5622
5623 if (property->size != sizeof *otv) {
5624 return OFPERR_OFPBPC_BAD_LEN;
5625 }
5626 tm->table_vacancy.vacancy_down = otv->vacancy_down;
5627 tm->table_vacancy.vacancy_up = otv->vacancy_up;
5628 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
5629 OFPPROP_LOG(&bad_ofmsg_rl, false,
5630 "Value of vacancy_down is greater than vacancy_up");
5631 return OFPERR_OFPBPC_BAD_VALUE;
5632 }
5633 if (tm->table_vacancy.vacancy_down > 100 ||
5634 tm->table_vacancy.vacancy_up > 100) {
5635 OFPPROP_LOG(&bad_ofmsg_rl, false, "Vacancy threshold percentage "
5636 "should not be greater than 100");
5637 return OFPERR_OFPBPC_BAD_VALUE;
5638 }
5639 tm->table_vacancy.vacancy = otv->vacancy;
5640 if (tm->table_vacancy.vacancy) {
5641 OFPPROP_LOG(&bad_ofmsg_rl, false,
5642 "Vacancy value should be zero for table-mod messages");
5643 return OFPERR_OFPBPC_BAD_VALUE;
5644 }
5645 return 0;
5646 }
5647
5648 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5649 * table configuration (a table mod, table stats, or table features message),
5650 * returns the table vacancy configuration that it specifies.
5651 *
5652 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5653 * so for other 'version' this function always returns
5654 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5655 static enum ofputil_table_vacancy
5656 ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5657 {
5658 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5659 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5660 : OFPUTIL_TABLE_VACANCY_OFF);
5661 }
5662
5663 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5664 * table configuration (a table mod, table stats, or table features message),
5665 * returns the table eviction configuration that it specifies.
5666 *
5667 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5668 * so for other 'version' values this function always returns
5669 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5670 static enum ofputil_table_eviction
5671 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5672 {
5673 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5674 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5675 : OFPUTIL_TABLE_EVICTION_OFF);
5676 }
5677
5678 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5679 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5680 * 'eviction' values. */
5681 static ovs_be32
5682 ofputil_encode_table_config(enum ofputil_table_miss miss,
5683 enum ofputil_table_eviction eviction,
5684 enum ofputil_table_vacancy vacancy,
5685 enum ofp_version version)
5686 {
5687 uint32_t config = 0;
5688 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5689 * information on the crazy evolution of this field. */
5690 switch (version) {
5691 case OFP10_VERSION:
5692 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5693 return htonl(0);
5694
5695 case OFP11_VERSION:
5696 case OFP12_VERSION:
5697 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5698 switch (miss) {
5699 case OFPUTIL_TABLE_MISS_DEFAULT:
5700 /* Really this shouldn't be used for encoding (the caller should
5701 * provide a specific value) but I can't imagine that defaulting to
5702 * the fall-through case here will hurt. */
5703 case OFPUTIL_TABLE_MISS_CONTROLLER:
5704 default:
5705 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5706 case OFPUTIL_TABLE_MISS_CONTINUE:
5707 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5708 case OFPUTIL_TABLE_MISS_DROP:
5709 return htonl(OFPTC11_TABLE_MISS_DROP);
5710 }
5711 OVS_NOT_REACHED();
5712
5713 case OFP13_VERSION:
5714 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5715 * flags, so this is correct. */
5716 return htonl(0);
5717
5718 case OFP14_VERSION:
5719 case OFP15_VERSION:
5720 case OFP16_VERSION:
5721 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5722 * OFPTC14_VACANCY_EVENTS. */
5723 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5724 config |= OFPTC14_EVICTION;
5725 }
5726 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5727 config |= OFPTC14_VACANCY_EVENTS;
5728 }
5729 return htonl(config);
5730 }
5731
5732 OVS_NOT_REACHED();
5733 }
5734
5735 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5736 * table configuration (a table mod, table stats, or table features message),
5737 * returns the table miss configuration that it specifies.
5738 *
5739 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5740 * other 'version' values this function always returns
5741 * OFPUTIL_TABLE_MISS_DEFAULT. */
5742 static enum ofputil_table_miss
5743 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5744 {
5745 uint32_t config = ntohl(config_);
5746
5747 if (version == OFP11_VERSION || version == OFP12_VERSION) {
5748 switch (config & OFPTC11_TABLE_MISS_MASK) {
5749 case OFPTC11_TABLE_MISS_CONTROLLER:
5750 return OFPUTIL_TABLE_MISS_CONTROLLER;
5751
5752 case OFPTC11_TABLE_MISS_CONTINUE:
5753 return OFPUTIL_TABLE_MISS_CONTINUE;
5754
5755 case OFPTC11_TABLE_MISS_DROP:
5756 return OFPUTIL_TABLE_MISS_DROP;
5757
5758 default:
5759 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5760 return OFPUTIL_TABLE_MISS_CONTROLLER;
5761 }
5762 } else {
5763 return OFPUTIL_TABLE_MISS_DEFAULT;
5764 }
5765 }
5766
5767 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5768 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5769 enum ofperr
5770 ofputil_decode_table_mod(const struct ofp_header *oh,
5771 struct ofputil_table_mod *pm)
5772 {
5773 memset(pm, 0, sizeof *pm);
5774 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5775 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5776 pm->eviction_flags = UINT32_MAX;
5777 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
5778
5779 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5780 enum ofpraw raw = ofpraw_pull_assert(&b);
5781 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5782 const struct ofp11_table_mod *otm = b.data;
5783
5784 pm->table_id = otm->table_id;
5785 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5786 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5787 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5788
5789 pm->table_id = otm->table_id;
5790 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5791 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5792 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
5793 while (b.size > 0) {
5794 struct ofpbuf property;
5795 enum ofperr error;
5796 uint64_t type;
5797
5798 error = ofpprop_pull(&b, &property, &type);
5799 if (error) {
5800 return error;
5801 }
5802
5803 switch (type) {
5804 case OFPTMPT14_EVICTION:
5805 error = ofpprop_parse_u32(&property, &pm->eviction);
5806 break;
5807
5808 case OFPTMPT14_VACANCY:
5809 error = parse_table_mod_vacancy_property(&property, pm);
5810 break;
5811
5812 default:
5813 error = OFPERR_OFPBRC_BAD_TYPE;
5814 break;
5815 }
5816
5817 if (error) {
5818 return error;
5819 }
5820 }
5821 } else {
5822 return OFPERR_OFPBRC_BAD_TYPE;
5823 }
5824
5825 return 0;
5826 }
5827
5828 /* Converts the abstract form of a "table mod" message in '*tm' into an
5829 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5830 * buffer owned by the caller. */
5831 struct ofpbuf *
5832 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5833 enum ofputil_protocol protocol)
5834 {
5835 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5836 struct ofpbuf *b;
5837
5838 switch (ofp_version) {
5839 case OFP10_VERSION: {
5840 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5841 "(\'-O OpenFlow11\')");
5842 break;
5843 }
5844 case OFP11_VERSION:
5845 case OFP12_VERSION:
5846 case OFP13_VERSION: {
5847 struct ofp11_table_mod *otm;
5848
5849 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5850 otm = ofpbuf_put_zeros(b, sizeof *otm);
5851 otm->table_id = tm->table_id;
5852 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5853 tm->vacancy, ofp_version);
5854 break;
5855 }
5856 case OFP14_VERSION:
5857 case OFP15_VERSION:
5858 case OFP16_VERSION: {
5859 struct ofp14_table_mod *otm;
5860
5861 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5862 otm = ofpbuf_put_zeros(b, sizeof *otm);
5863 otm->table_id = tm->table_id;
5864 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5865 tm->vacancy, ofp_version);
5866
5867 if (tm->eviction_flags != UINT32_MAX) {
5868 ofpprop_put_u32(b, OFPTMPT14_EVICTION, tm->eviction_flags);
5869 }
5870 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5871 struct ofp14_table_mod_prop_vacancy *otv;
5872
5873 otv = ofpprop_put_zeros(b, OFPTMPT14_VACANCY, sizeof *otv);
5874 otv->vacancy_down = tm->table_vacancy.vacancy_down;
5875 otv->vacancy_up = tm->table_vacancy.vacancy_up;
5876 }
5877 break;
5878 }
5879 default:
5880 OVS_NOT_REACHED();
5881 }
5882
5883 return b;
5884 }
5885 \f
5886 /* ofputil_role_request */
5887
5888 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5889 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5890 * OFPERR_* value. */
5891 enum ofperr
5892 ofputil_decode_role_message(const struct ofp_header *oh,
5893 struct ofputil_role_request *rr)
5894 {
5895 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5896 enum ofpraw raw = ofpraw_pull_assert(&b);
5897 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5898 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5899 const struct ofp12_role_request *orr = b.msg;
5900
5901 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5902 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5903 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5904 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5905 return OFPERR_OFPRRFC_BAD_ROLE;
5906 }
5907
5908 rr->role = ntohl(orr->role);
5909 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5910 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5911 : orr->generation_id == OVS_BE64_MAX) {
5912 rr->have_generation_id = false;
5913 rr->generation_id = 0;
5914 } else {
5915 rr->have_generation_id = true;
5916 rr->generation_id = ntohll(orr->generation_id);
5917 }
5918 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5919 raw == OFPRAW_NXT_ROLE_REPLY) {
5920 const struct nx_role_request *nrr = b.msg;
5921
5922 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5923 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5924 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5925
5926 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5927 nrr->role != htonl(NX_ROLE_MASTER) &&
5928 nrr->role != htonl(NX_ROLE_SLAVE)) {
5929 return OFPERR_OFPRRFC_BAD_ROLE;
5930 }
5931
5932 rr->role = ntohl(nrr->role) + 1;
5933 rr->have_generation_id = false;
5934 rr->generation_id = 0;
5935 } else {
5936 OVS_NOT_REACHED();
5937 }
5938
5939 return 0;
5940 }
5941
5942 /* Returns an encoded form of a role reply suitable for the "request" in a
5943 * buffer owned by the caller. */
5944 struct ofpbuf *
5945 ofputil_encode_role_reply(const struct ofp_header *request,
5946 const struct ofputil_role_request *rr)
5947 {
5948 struct ofpbuf *buf;
5949 enum ofpraw raw;
5950
5951 raw = ofpraw_decode_assert(request);
5952 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5953 struct ofp12_role_request *orr;
5954
5955 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5956 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5957
5958 orr->role = htonl(rr->role);
5959 orr->generation_id = htonll(rr->have_generation_id
5960 ? rr->generation_id
5961 : UINT64_MAX);
5962 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5963 struct nx_role_request *nrr;
5964
5965 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5966 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5967 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5968
5969 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5970 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5971 nrr->role = htonl(rr->role - 1);
5972 } else {
5973 OVS_NOT_REACHED();
5974 }
5975
5976 return buf;
5977 }
5978 \f
5979 /* Encodes "role status" message 'status' for sending in the given
5980 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5981 * otherwise a null pointer. */
5982 struct ofpbuf *
5983 ofputil_encode_role_status(const struct ofputil_role_status *status,
5984 enum ofputil_protocol protocol)
5985 {
5986 enum ofp_version version;
5987
5988 version = ofputil_protocol_to_ofp_version(protocol);
5989 if (version >= OFP14_VERSION) {
5990 struct ofp14_role_status *rstatus;
5991 struct ofpbuf *buf;
5992
5993 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5994 0);
5995 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5996 rstatus->role = htonl(status->role);
5997 rstatus->reason = status->reason;
5998 rstatus->generation_id = htonll(status->generation_id);
5999
6000 return buf;
6001 } else {
6002 return NULL;
6003 }
6004 }
6005
6006 enum ofperr
6007 ofputil_decode_role_status(const struct ofp_header *oh,
6008 struct ofputil_role_status *rs)
6009 {
6010 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
6011 enum ofpraw raw = ofpraw_pull_assert(&b);
6012 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
6013
6014 const struct ofp14_role_status *r = b.msg;
6015 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
6016 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
6017 r->role != htonl(OFPCR12_ROLE_MASTER) &&
6018 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
6019 return OFPERR_OFPRRFC_BAD_ROLE;
6020 }
6021
6022 rs->role = ntohl(r->role);
6023 rs->generation_id = ntohll(r->generation_id);
6024 rs->reason = r->reason;
6025
6026 return 0;
6027 }
6028
6029 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
6030 * 'protocol' must be for OpenFlow 1.4 or later. */
6031 struct ofpbuf *
6032 ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
6033 enum ofputil_protocol protocol)
6034 {
6035 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6036 struct ofpbuf *inner;
6037
6038 switch (rf->reason) {
6039 case OFPRFR_GROUP_MOD:
6040 inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
6041 break;
6042
6043 case OFPRFR_METER_MOD:
6044 inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
6045 break;
6046
6047 case OFPRFR_N_REASONS:
6048 default:
6049 OVS_NOT_REACHED();
6050 }
6051
6052 struct ofp_header *inner_oh = inner->data;
6053 inner_oh->xid = rf->xid;
6054 inner_oh->length = htons(inner->size);
6055
6056 struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
6057 ofp_version, htonl(0),
6058 inner->size);
6059 ofpbuf_put(outer, inner->data, inner->size);
6060 ofpbuf_delete(inner);
6061
6062 return outer;
6063 }
6064
6065 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6066 * form into '*rf' and returns 0, and the caller is later responsible for
6067 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6068 * failure, returns an ofperr and '*rf' is indeterminate. */
6069 enum ofperr
6070 ofputil_decode_requestforward(const struct ofp_header *outer,
6071 struct ofputil_requestforward *rf)
6072 {
6073 struct ofpbuf b = ofpbuf_const_initializer(outer, ntohs(outer->length));
6074
6075 /* Skip past outer message. */
6076 enum ofpraw outer_raw = ofpraw_pull_assert(&b);
6077 ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
6078
6079 /* Validate inner message. */
6080 if (b.size < sizeof(struct ofp_header)) {
6081 return OFPERR_OFPBFC_MSG_BAD_LEN;
6082 }
6083 const struct ofp_header *inner = b.data;
6084 unsigned int inner_len = ntohs(inner->length);
6085 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
6086 return OFPERR_OFPBFC_MSG_BAD_LEN;
6087 }
6088 if (inner->version != outer->version) {
6089 return OFPERR_OFPBRC_BAD_VERSION;
6090 }
6091
6092 /* Parse inner message. */
6093 enum ofptype type;
6094 enum ofperr error = ofptype_decode(&type, inner);
6095 if (error) {
6096 return error;
6097 }
6098
6099 rf->xid = inner->xid;
6100 if (type == OFPTYPE_GROUP_MOD) {
6101 rf->reason = OFPRFR_GROUP_MOD;
6102 rf->group_mod = xmalloc(sizeof *rf->group_mod);
6103 error = ofputil_decode_group_mod(inner, rf->group_mod);
6104 if (error) {
6105 free(rf->group_mod);
6106 return error;
6107 }
6108 } else if (type == OFPTYPE_METER_MOD) {
6109 rf->reason = OFPRFR_METER_MOD;
6110 rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
6111 ofpbuf_init(&rf->bands, 64);
6112 error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
6113 if (error) {
6114 free(rf->meter_mod);
6115 ofpbuf_uninit(&rf->bands);
6116 return error;
6117 }
6118 } else {
6119 return OFPERR_OFPBFC_MSG_UNSUP;
6120 }
6121
6122 return 0;
6123 }
6124
6125 /* Frees the content of 'rf', which should have been initialized through a
6126 * successful call to ofputil_decode_requestforward(). */
6127 void
6128 ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
6129 {
6130 if (!rf) {
6131 return;
6132 }
6133
6134 switch (rf->reason) {
6135 case OFPRFR_GROUP_MOD:
6136 ofputil_uninit_group_mod(rf->group_mod);
6137 free(rf->group_mod);
6138 break;
6139
6140 case OFPRFR_METER_MOD:
6141 ofpbuf_uninit(&rf->bands);
6142 free(rf->meter_mod);
6143 break;
6144
6145 case OFPRFR_N_REASONS:
6146 OVS_NOT_REACHED();
6147 }
6148 }
6149
6150 /* Table stats. */
6151
6152 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6153 * matched and a field that must be wildcarded. This function returns a bitmap
6154 * that contains both kinds of fields. */
6155 static struct mf_bitmap
6156 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
6157 {
6158 struct mf_bitmap wc = features->match;
6159 bitmap_not(wc.bm, MFF_N_IDS);
6160 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
6161 return wc;
6162 }
6163
6164 struct ofp10_wc_map {
6165 enum ofp10_flow_wildcards wc10;
6166 enum mf_field_id mf;
6167 };
6168
6169 static const struct ofp10_wc_map ofp10_wc_map[] = {
6170 { OFPFW10_IN_PORT, MFF_IN_PORT },
6171 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
6172 { OFPFW10_DL_SRC, MFF_ETH_SRC },
6173 { OFPFW10_DL_DST, MFF_ETH_DST},
6174 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
6175 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
6176 { OFPFW10_TP_SRC, MFF_TCP_SRC },
6177 { OFPFW10_TP_DST, MFF_TCP_DST },
6178 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
6179 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
6180 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
6181 { OFPFW10_NW_TOS, MFF_IP_DSCP },
6182 };
6183
6184 static ovs_be32
6185 mf_bitmap_to_of10(const struct mf_bitmap *fields)
6186 {
6187 const struct ofp10_wc_map *p;
6188 uint32_t wc10 = 0;
6189
6190 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6191 if (bitmap_is_set(fields->bm, p->mf)) {
6192 wc10 |= p->wc10;
6193 }
6194 }
6195 return htonl(wc10);
6196 }
6197
6198 static struct mf_bitmap
6199 mf_bitmap_from_of10(ovs_be32 wc10_)
6200 {
6201 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6202 const struct ofp10_wc_map *p;
6203 uint32_t wc10 = ntohl(wc10_);
6204
6205 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6206 if (wc10 & p->wc10) {
6207 bitmap_set1(fields.bm, p->mf);
6208 }
6209 }
6210 return fields;
6211 }
6212
6213 static void
6214 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
6215 const struct ofputil_table_features *features,
6216 struct ofpbuf *buf)
6217 {
6218 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
6219 struct ofp10_table_stats *out;
6220
6221 out = ofpbuf_put_zeros(buf, sizeof *out);
6222 out->table_id = features->table_id;
6223 ovs_strlcpy(out->name, features->name, sizeof out->name);
6224 out->wildcards = mf_bitmap_to_of10(&wc);
6225 out->max_entries = htonl(features->max_entries);
6226 out->active_count = htonl(stats->active_count);
6227 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
6228 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
6229 }
6230
6231 struct ofp11_wc_map {
6232 enum ofp11_flow_match_fields wc11;
6233 enum mf_field_id mf;
6234 };
6235
6236 static const struct ofp11_wc_map ofp11_wc_map[] = {
6237 { OFPFMF11_IN_PORT, MFF_IN_PORT },
6238 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
6239 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
6240 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
6241 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
6242 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
6243 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
6244 { OFPFMF11_TP_DST, MFF_TCP_DST },
6245 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
6246 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
6247 /* I don't know what OFPFMF11_TYPE means. */
6248 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
6249 { OFPFMF11_DL_DST, MFF_ETH_DST },
6250 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
6251 { OFPFMF11_NW_DST, MFF_IPV4_DST },
6252 { OFPFMF11_METADATA, MFF_METADATA },
6253 };
6254
6255 static ovs_be32
6256 mf_bitmap_to_of11(const struct mf_bitmap *fields)
6257 {
6258 const struct ofp11_wc_map *p;
6259 uint32_t wc11 = 0;
6260
6261 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6262 if (bitmap_is_set(fields->bm, p->mf)) {
6263 wc11 |= p->wc11;
6264 }
6265 }
6266 return htonl(wc11);
6267 }
6268
6269 static struct mf_bitmap
6270 mf_bitmap_from_of11(ovs_be32 wc11_)
6271 {
6272 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6273 const struct ofp11_wc_map *p;
6274 uint32_t wc11 = ntohl(wc11_);
6275
6276 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6277 if (wc11 & p->wc11) {
6278 bitmap_set1(fields.bm, p->mf);
6279 }
6280 }
6281 return fields;
6282 }
6283
6284 static void
6285 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
6286 const struct ofputil_table_features *features,
6287 struct ofpbuf *buf)
6288 {
6289 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
6290 struct ofp11_table_stats *out;
6291
6292 out = ofpbuf_put_zeros(buf, sizeof *out);
6293 out->table_id = features->table_id;
6294 ovs_strlcpy(out->name, features->name, sizeof out->name);
6295 out->wildcards = mf_bitmap_to_of11(&wc);
6296 out->match = mf_bitmap_to_of11(&features->match);
6297 out->instructions = ovsinst_bitmap_to_openflow(
6298 features->nonmiss.instructions, OFP11_VERSION);
6299 out->write_actions = ofpact_bitmap_to_openflow(
6300 features->nonmiss.write.ofpacts, OFP11_VERSION);
6301 out->apply_actions = ofpact_bitmap_to_openflow(
6302 features->nonmiss.apply.ofpacts, OFP11_VERSION);
6303 out->config = htonl(features->miss_config);
6304 out->max_entries = htonl(features->max_entries);
6305 out->active_count = htonl(stats->active_count);
6306 out->lookup_count = htonll(stats->lookup_count);
6307 out->matched_count = htonll(stats->matched_count);
6308 }
6309
6310 static void
6311 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
6312 const struct ofputil_table_features *features,
6313 struct ofpbuf *buf)
6314 {
6315 struct ofp12_table_stats *out;
6316
6317 out = ofpbuf_put_zeros(buf, sizeof *out);
6318 out->table_id = features->table_id;
6319 ovs_strlcpy(out->name, features->name, sizeof out->name);
6320 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
6321 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
6322 OFP12_VERSION);
6323 out->write_actions = ofpact_bitmap_to_openflow(
6324 features->nonmiss.write.ofpacts, OFP12_VERSION);
6325 out->apply_actions = ofpact_bitmap_to_openflow(
6326 features->nonmiss.apply.ofpacts, OFP12_VERSION);
6327 out->write_setfields = oxm_bitmap_from_mf_bitmap(
6328 &features->nonmiss.write.set_fields, OFP12_VERSION);
6329 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
6330 &features->nonmiss.apply.set_fields, OFP12_VERSION);
6331 out->metadata_match = features->metadata_match;
6332 out->metadata_write = features->metadata_write;
6333 out->instructions = ovsinst_bitmap_to_openflow(
6334 features->nonmiss.instructions, OFP12_VERSION);
6335 out->config = ofputil_encode_table_config(features->miss_config,
6336 OFPUTIL_TABLE_EVICTION_DEFAULT,
6337 OFPUTIL_TABLE_VACANCY_DEFAULT,
6338 OFP12_VERSION);
6339 out->max_entries = htonl(features->max_entries);
6340 out->active_count = htonl(stats->active_count);
6341 out->lookup_count = htonll(stats->lookup_count);
6342 out->matched_count = htonll(stats->matched_count);
6343 }
6344
6345 static void
6346 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
6347 struct ofpbuf *buf)
6348 {
6349 struct ofp13_table_stats *out;
6350
6351 out = ofpbuf_put_zeros(buf, sizeof *out);
6352 out->table_id = stats->table_id;
6353 out->active_count = htonl(stats->active_count);
6354 out->lookup_count = htonll(stats->lookup_count);
6355 out->matched_count = htonll(stats->matched_count);
6356 }
6357
6358 struct ofpbuf *
6359 ofputil_encode_table_stats_reply(const struct ofp_header *request)
6360 {
6361 return ofpraw_alloc_stats_reply(request, 0);
6362 }
6363
6364 void
6365 ofputil_append_table_stats_reply(struct ofpbuf *reply,
6366 const struct ofputil_table_stats *stats,
6367 const struct ofputil_table_features *features)
6368 {
6369 struct ofp_header *oh = reply->header;
6370
6371 ovs_assert(stats->table_id == features->table_id);
6372
6373 switch ((enum ofp_version) oh->version) {
6374 case OFP10_VERSION:
6375 ofputil_put_ofp10_table_stats(stats, features, reply);
6376 break;
6377
6378 case OFP11_VERSION:
6379 ofputil_put_ofp11_table_stats(stats, features, reply);
6380 break;
6381
6382 case OFP12_VERSION:
6383 ofputil_put_ofp12_table_stats(stats, features, reply);
6384 break;
6385
6386 case OFP13_VERSION:
6387 case OFP14_VERSION:
6388 case OFP15_VERSION:
6389 case OFP16_VERSION:
6390 ofputil_put_ofp13_table_stats(stats, reply);
6391 break;
6392
6393 default:
6394 OVS_NOT_REACHED();
6395 }
6396 }
6397
6398 static int
6399 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
6400 struct ofputil_table_stats *stats,
6401 struct ofputil_table_features *features)
6402 {
6403 struct ofp10_table_stats *ots;
6404
6405 ots = ofpbuf_try_pull(msg, sizeof *ots);
6406 if (!ots) {
6407 return OFPERR_OFPBRC_BAD_LEN;
6408 }
6409
6410 features->table_id = ots->table_id;
6411 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6412 features->max_entries = ntohl(ots->max_entries);
6413 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
6414
6415 stats->table_id = ots->table_id;
6416 stats->active_count = ntohl(ots->active_count);
6417 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
6418 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
6419
6420 return 0;
6421 }
6422
6423 static int
6424 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
6425 struct ofputil_table_stats *stats,
6426 struct ofputil_table_features *features)
6427 {
6428 struct ofp11_table_stats *ots;
6429
6430 ots = ofpbuf_try_pull(msg, sizeof *ots);
6431 if (!ots) {
6432 return OFPERR_OFPBRC_BAD_LEN;
6433 }
6434
6435 features->table_id = ots->table_id;
6436 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6437 features->max_entries = ntohl(ots->max_entries);
6438 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6439 ots->instructions, OFP11_VERSION);
6440 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6441 ots->write_actions, OFP11_VERSION);
6442 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6443 ots->write_actions, OFP11_VERSION);
6444 features->miss = features->nonmiss;
6445 features->miss_config = ofputil_decode_table_miss(ots->config,
6446 OFP11_VERSION);
6447 features->match = mf_bitmap_from_of11(ots->match);
6448 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
6449 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6450
6451 stats->table_id = ots->table_id;
6452 stats->active_count = ntohl(ots->active_count);
6453 stats->lookup_count = ntohll(ots->lookup_count);
6454 stats->matched_count = ntohll(ots->matched_count);
6455
6456 return 0;
6457 }
6458
6459 static int
6460 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
6461 struct ofputil_table_stats *stats,
6462 struct ofputil_table_features *features)
6463 {
6464 struct ofp12_table_stats *ots;
6465
6466 ots = ofpbuf_try_pull(msg, sizeof *ots);
6467 if (!ots) {
6468 return OFPERR_OFPBRC_BAD_LEN;
6469 }
6470
6471 features->table_id = ots->table_id;
6472 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6473 features->metadata_match = ots->metadata_match;
6474 features->metadata_write = ots->metadata_write;
6475 features->miss_config = ofputil_decode_table_miss(ots->config,
6476 OFP12_VERSION);
6477 features->max_entries = ntohl(ots->max_entries);
6478
6479 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6480 ots->instructions, OFP12_VERSION);
6481 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6482 ots->write_actions, OFP12_VERSION);
6483 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6484 ots->apply_actions, OFP12_VERSION);
6485 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
6486 ots->write_setfields, OFP12_VERSION);
6487 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
6488 ots->apply_setfields, OFP12_VERSION);
6489 features->miss = features->nonmiss;
6490
6491 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
6492 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
6493 OFP12_VERSION);
6494 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6495
6496 stats->table_id = ots->table_id;
6497 stats->active_count = ntohl(ots->active_count);
6498 stats->lookup_count = ntohll(ots->lookup_count);
6499 stats->matched_count = ntohll(ots->matched_count);
6500
6501 return 0;
6502 }
6503
6504 static int
6505 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
6506 struct ofputil_table_stats *stats,
6507 struct ofputil_table_features *features)
6508 {
6509 struct ofp13_table_stats *ots;
6510
6511 ots = ofpbuf_try_pull(msg, sizeof *ots);
6512 if (!ots) {
6513 return OFPERR_OFPBRC_BAD_LEN;
6514 }
6515
6516 features->table_id = ots->table_id;
6517
6518 stats->table_id = ots->table_id;
6519 stats->active_count = ntohl(ots->active_count);
6520 stats->lookup_count = ntohll(ots->lookup_count);
6521 stats->matched_count = ntohll(ots->matched_count);
6522
6523 return 0;
6524 }
6525
6526 int
6527 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6528 struct ofputil_table_stats *stats,
6529 struct ofputil_table_features *features)
6530 {
6531 const struct ofp_header *oh;
6532
6533 if (!msg->header) {
6534 ofpraw_pull_assert(msg);
6535 }
6536 oh = msg->header;
6537
6538 if (!msg->size) {
6539 return EOF;
6540 }
6541
6542 memset(stats, 0, sizeof *stats);
6543 memset(features, 0, sizeof *features);
6544 features->supports_eviction = -1;
6545 features->supports_vacancy_events = -1;
6546
6547 switch ((enum ofp_version) oh->version) {
6548 case OFP10_VERSION:
6549 return ofputil_decode_ofp10_table_stats(msg, stats, features);
6550
6551 case OFP11_VERSION:
6552 return ofputil_decode_ofp11_table_stats(msg, stats, features);
6553
6554 case OFP12_VERSION:
6555 return ofputil_decode_ofp12_table_stats(msg, stats, features);
6556
6557 case OFP13_VERSION:
6558 case OFP14_VERSION:
6559 case OFP15_VERSION:
6560 case OFP16_VERSION:
6561 return ofputil_decode_ofp13_table_stats(msg, stats, features);
6562
6563 default:
6564 OVS_NOT_REACHED();
6565 }
6566 }
6567 \f
6568 /* ofputil_flow_monitor_request */
6569
6570 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6571 * ofputil_flow_monitor_request in 'rq'.
6572 *
6573 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6574 * message. Calling this function multiple times for a single 'msg' iterates
6575 * through the requests. The caller must initially leave 'msg''s layer
6576 * pointers null and not modify them between calls.
6577 *
6578 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6579 * otherwise an OFPERR_* value. */
6580 int
6581 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6582 struct ofpbuf *msg)
6583 {
6584 struct nx_flow_monitor_request *nfmr;
6585 uint16_t flags;
6586
6587 if (!msg->header) {
6588 ofpraw_pull_assert(msg);
6589 }
6590
6591 if (!msg->size) {
6592 return EOF;
6593 }
6594
6595 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6596 if (!nfmr) {
6597 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6598 "leftover bytes at end", msg->size);
6599 return OFPERR_OFPBRC_BAD_LEN;
6600 }
6601
6602 flags = ntohs(nfmr->flags);
6603 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6604 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6605 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6606 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6607 flags);
6608 return OFPERR_OFPMOFC_BAD_FLAGS;
6609 }
6610
6611 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6612 return OFPERR_NXBRC_MUST_BE_ZERO;
6613 }
6614
6615 rq->id = ntohl(nfmr->id);
6616 rq->flags = flags;
6617 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
6618 rq->table_id = nfmr->table_id;
6619
6620 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
6621 }
6622
6623 void
6624 ofputil_append_flow_monitor_request(
6625 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6626 {
6627 struct nx_flow_monitor_request *nfmr;
6628 size_t start_ofs;
6629 int match_len;
6630
6631 if (!msg->size) {
6632 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
6633 }
6634
6635 start_ofs = msg->size;
6636 ofpbuf_put_zeros(msg, sizeof *nfmr);
6637 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
6638
6639 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6640 nfmr->id = htonl(rq->id);
6641 nfmr->flags = htons(rq->flags);
6642 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
6643 nfmr->match_len = htons(match_len);
6644 nfmr->table_id = rq->table_id;
6645 }
6646
6647 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6648 * into an abstract ofputil_flow_update in 'update'. The caller must have
6649 * initialized update->match to point to space allocated for a match.
6650 *
6651 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6652 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6653 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6654 * will point into the 'ofpacts' buffer.
6655 *
6656 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6657 * this function multiple times for a single 'msg' iterates through the
6658 * updates. The caller must initially leave 'msg''s layer pointers null and
6659 * not modify them between calls.
6660 *
6661 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6662 * otherwise an OFPERR_* value. */
6663 int
6664 ofputil_decode_flow_update(struct ofputil_flow_update *update,
6665 struct ofpbuf *msg, struct ofpbuf *ofpacts)
6666 {
6667 struct nx_flow_update_header *nfuh;
6668 unsigned int length;
6669 struct ofp_header *oh;
6670
6671 if (!msg->header) {
6672 ofpraw_pull_assert(msg);
6673 }
6674
6675 ofpbuf_clear(ofpacts);
6676 if (!msg->size) {
6677 return EOF;
6678 }
6679
6680 if (msg->size < sizeof(struct nx_flow_update_header)) {
6681 goto bad_len;
6682 }
6683
6684 oh = msg->header;
6685
6686 nfuh = msg->data;
6687 update->event = ntohs(nfuh->event);
6688 length = ntohs(nfuh->length);
6689 if (length > msg->size || length % 8) {
6690 goto bad_len;
6691 }
6692
6693 if (update->event == NXFME_ABBREV) {
6694 struct nx_flow_update_abbrev *nfua;
6695
6696 if (length != sizeof *nfua) {
6697 goto bad_len;
6698 }
6699
6700 nfua = ofpbuf_pull(msg, sizeof *nfua);
6701 update->xid = nfua->xid;
6702 return 0;
6703 } else if (update->event == NXFME_ADDED
6704 || update->event == NXFME_DELETED
6705 || update->event == NXFME_MODIFIED) {
6706 struct nx_flow_update_full *nfuf;
6707 unsigned int actions_len;
6708 unsigned int match_len;
6709 enum ofperr error;
6710
6711 if (length < sizeof *nfuf) {
6712 goto bad_len;
6713 }
6714
6715 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6716 match_len = ntohs(nfuf->match_len);
6717 if (sizeof *nfuf + match_len > length) {
6718 goto bad_len;
6719 }
6720
6721 update->reason = ntohs(nfuf->reason);
6722 update->idle_timeout = ntohs(nfuf->idle_timeout);
6723 update->hard_timeout = ntohs(nfuf->hard_timeout);
6724 update->table_id = nfuf->table_id;
6725 update->cookie = nfuf->cookie;
6726 update->priority = ntohs(nfuf->priority);
6727
6728 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
6729 if (error) {
6730 return error;
6731 }
6732
6733 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
6734 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
6735 ofpacts);
6736 if (error) {
6737 return error;
6738 }
6739
6740 update->ofpacts = ofpacts->data;
6741 update->ofpacts_len = ofpacts->size;
6742 return 0;
6743 } else {
6744 VLOG_WARN_RL(&bad_ofmsg_rl,
6745 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6746 ntohs(nfuh->event));
6747 return OFPERR_NXBRC_FM_BAD_EVENT;
6748 }
6749
6750 bad_len:
6751 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6752 "leftover bytes at end", msg->size);
6753 return OFPERR_OFPBRC_BAD_LEN;
6754 }
6755
6756 uint32_t
6757 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6758 {
6759 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6760
6761 return ntohl(cancel->id);
6762 }
6763
6764 struct ofpbuf *
6765 ofputil_encode_flow_monitor_cancel(uint32_t id)
6766 {
6767 struct nx_flow_monitor_cancel *nfmc;
6768 struct ofpbuf *msg;
6769
6770 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6771 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
6772 nfmc->id = htonl(id);
6773 return msg;
6774 }
6775
6776 void
6777 ofputil_start_flow_update(struct ovs_list *replies)
6778 {
6779 struct ofpbuf *msg;
6780
6781 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6782 htonl(0), 1024);
6783
6784 ovs_list_init(replies);
6785 ovs_list_push_back(replies, &msg->list_node);
6786 }
6787
6788 void
6789 ofputil_append_flow_update(const struct ofputil_flow_update *update,
6790 struct ovs_list *replies)
6791 {
6792 enum ofp_version version = ofpmp_version(replies);
6793 struct nx_flow_update_header *nfuh;
6794 struct ofpbuf *msg;
6795 size_t start_ofs;
6796
6797 msg = ofpbuf_from_list(ovs_list_back(replies));
6798 start_ofs = msg->size;
6799
6800 if (update->event == NXFME_ABBREV) {
6801 struct nx_flow_update_abbrev *nfua;
6802
6803 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6804 nfua->xid = update->xid;
6805 } else {
6806 struct nx_flow_update_full *nfuf;
6807 int match_len;
6808
6809 ofpbuf_put_zeros(msg, sizeof *nfuf);
6810 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6811 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6812 version);
6813 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6814 nfuf->reason = htons(update->reason);
6815 nfuf->priority = htons(update->priority);
6816 nfuf->idle_timeout = htons(update->idle_timeout);
6817 nfuf->hard_timeout = htons(update->hard_timeout);
6818 nfuf->match_len = htons(match_len);
6819 nfuf->table_id = update->table_id;
6820 nfuf->cookie = update->cookie;
6821 }
6822
6823 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6824 nfuh->length = htons(msg->size - start_ofs);
6825 nfuh->event = htons(update->event);
6826
6827 ofpmp_postappend(replies, start_ofs);
6828 }
6829 \f
6830 struct ofpbuf *
6831 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6832 enum ofputil_protocol protocol)
6833 {
6834 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6835 struct ofpbuf *msg;
6836 size_t size;
6837
6838 size = po->ofpacts_len;
6839 if (po->buffer_id == UINT32_MAX) {
6840 size += po->packet_len;
6841 }
6842
6843 switch (ofp_version) {
6844 case OFP10_VERSION: {
6845 struct ofp10_packet_out *opo;
6846 size_t actions_ofs;
6847
6848 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6849 ofpbuf_put_zeros(msg, sizeof *opo);
6850 actions_ofs = msg->size;
6851 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6852 ofp_version);
6853
6854 opo = msg->msg;
6855 opo->buffer_id = htonl(po->buffer_id);
6856 opo->in_port = htons(ofp_to_u16(po->in_port));
6857 opo->actions_len = htons(msg->size - actions_ofs);
6858 break;
6859 }
6860
6861 case OFP11_VERSION:
6862 case OFP12_VERSION:
6863 case OFP13_VERSION:
6864 case OFP14_VERSION:
6865 case OFP15_VERSION:
6866 case OFP16_VERSION: {
6867 struct ofp11_packet_out *opo;
6868 size_t len;
6869
6870 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6871 ofpbuf_put_zeros(msg, sizeof *opo);
6872 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6873 ofp_version);
6874 opo = msg->msg;
6875 opo->buffer_id = htonl(po->buffer_id);
6876 opo->in_port = ofputil_port_to_ofp11(po->in_port);
6877 opo->actions_len = htons(len);
6878 break;
6879 }
6880
6881 default:
6882 OVS_NOT_REACHED();
6883 }
6884
6885 if (po->buffer_id == UINT32_MAX) {
6886 ofpbuf_put(msg, po->packet, po->packet_len);
6887 }
6888
6889 ofpmsg_update_length(msg);
6890
6891 return msg;
6892 }
6893 \f
6894 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6895 struct ofpbuf *
6896 make_echo_request(enum ofp_version ofp_version)
6897 {
6898 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6899 htonl(0), 0);
6900 }
6901
6902 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6903 * OFPT_ECHO_REQUEST message in 'rq'. */
6904 struct ofpbuf *
6905 make_echo_reply(const struct ofp_header *rq)
6906 {
6907 struct ofpbuf rq_buf = ofpbuf_const_initializer(rq, ntohs(rq->length));
6908 ofpraw_pull_assert(&rq_buf);
6909
6910 struct ofpbuf *reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY,
6911 rq, rq_buf.size);
6912 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6913 return reply;
6914 }
6915
6916 struct ofpbuf *
6917 ofputil_encode_barrier_request(enum ofp_version ofp_version)
6918 {
6919 enum ofpraw type;
6920
6921 switch (ofp_version) {
6922 case OFP16_VERSION:
6923 case OFP15_VERSION:
6924 case OFP14_VERSION:
6925 case OFP13_VERSION:
6926 case OFP12_VERSION:
6927 case OFP11_VERSION:
6928 type = OFPRAW_OFPT11_BARRIER_REQUEST;
6929 break;
6930
6931 case OFP10_VERSION:
6932 type = OFPRAW_OFPT10_BARRIER_REQUEST;
6933 break;
6934
6935 default:
6936 OVS_NOT_REACHED();
6937 }
6938
6939 return ofpraw_alloc(type, ofp_version, 0);
6940 }
6941
6942 const char *
6943 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
6944 {
6945 switch (frag) {
6946 case OFPUTIL_FRAG_NORMAL: return "normal";
6947 case OFPUTIL_FRAG_DROP: return "drop";
6948 case OFPUTIL_FRAG_REASM: return "reassemble";
6949 case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
6950 }
6951
6952 OVS_NOT_REACHED();
6953 }
6954
6955 bool
6956 ofputil_frag_handling_from_string(const char *s,
6957 enum ofputil_frag_handling *frag)
6958 {
6959 if (!strcasecmp(s, "normal")) {
6960 *frag = OFPUTIL_FRAG_NORMAL;
6961 } else if (!strcasecmp(s, "drop")) {
6962 *frag = OFPUTIL_FRAG_DROP;
6963 } else if (!strcasecmp(s, "reassemble")) {
6964 *frag = OFPUTIL_FRAG_REASM;
6965 } else if (!strcasecmp(s, "nx-match")) {
6966 *frag = OFPUTIL_FRAG_NX_MATCH;
6967 } else {
6968 return false;
6969 }
6970 return true;
6971 }
6972
6973 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6974 * port number and stores the latter in '*ofp10_port', for the purpose of
6975 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6976 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6977 *
6978 * See the definition of OFP11_MAX for an explanation of the mapping. */
6979 enum ofperr
6980 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6981 {
6982 uint32_t ofp11_port_h = ntohl(ofp11_port);
6983
6984 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6985 *ofp10_port = u16_to_ofp(ofp11_port_h);
6986 return 0;
6987 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6988 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6989 return 0;
6990 } else {
6991 *ofp10_port = OFPP_NONE;
6992 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6993 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6994 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6995 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6996 return OFPERR_OFPBAC_BAD_OUT_PORT;
6997 }
6998 }
6999
7000 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
7001 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
7002 *
7003 * See the definition of OFP11_MAX for an explanation of the mapping. */
7004 ovs_be32
7005 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
7006 {
7007 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
7008 ? ofp_to_u16(ofp10_port)
7009 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
7010 }
7011
7012 #define OFPUTIL_NAMED_PORTS \
7013 OFPUTIL_NAMED_PORT(IN_PORT) \
7014 OFPUTIL_NAMED_PORT(TABLE) \
7015 OFPUTIL_NAMED_PORT(NORMAL) \
7016 OFPUTIL_NAMED_PORT(FLOOD) \
7017 OFPUTIL_NAMED_PORT(ALL) \
7018 OFPUTIL_NAMED_PORT(CONTROLLER) \
7019 OFPUTIL_NAMED_PORT(LOCAL) \
7020 OFPUTIL_NAMED_PORT(ANY) \
7021 OFPUTIL_NAMED_PORT(UNSET)
7022
7023 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
7024 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
7025 OFPUTIL_NAMED_PORTS \
7026 OFPUTIL_NAMED_PORT(NONE)
7027
7028 /* Stores the port number represented by 's' into '*portp'. 's' may be an
7029 * integer or, for reserved ports, the standard OpenFlow name for the port
7030 * (e.g. "LOCAL").
7031 *
7032 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7033 * or name. The caller should issue an error message in this case, because
7034 * this function usually does not. (This gives the caller an opportunity to
7035 * look up the port name another way, e.g. by contacting the switch and listing
7036 * the names of all its ports).
7037 *
7038 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7039 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7040 * range as described in include/openflow/openflow-1.1.h. */
7041 bool
7042 ofputil_port_from_string(const char *s, ofp_port_t *portp)
7043 {
7044 unsigned int port32; /* int is at least 32 bits wide. */
7045
7046 if (*s == '-') {
7047 VLOG_WARN("Negative value %s is not a valid port number.", s);
7048 return false;
7049 }
7050 *portp = 0;
7051 if (str_to_uint(s, 10, &port32)) {
7052 if (port32 < ofp_to_u16(OFPP_MAX)) {
7053 /* Pass. */
7054 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
7055 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7056 "be translated to %u when talking to an OF1.1 or "
7057 "later controller", port32, port32 + OFPP11_OFFSET);
7058 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
7059 char name[OFP_MAX_PORT_NAME_LEN];
7060
7061 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
7062 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
7063 "for compatibility with OpenFlow 1.1 and later",
7064 name, port32);
7065 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
7066 VLOG_WARN("port %u is outside the supported range 0 through "
7067 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
7068 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
7069 return false;
7070 } else {
7071 port32 -= OFPP11_OFFSET;
7072 }
7073
7074 *portp = u16_to_ofp(port32);
7075 return true;
7076 } else {
7077 struct pair {
7078 const char *name;
7079 ofp_port_t value;
7080 };
7081 static const struct pair pairs[] = {
7082 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7083 OFPUTIL_NAMED_PORTS_WITH_NONE
7084 #undef OFPUTIL_NAMED_PORT
7085 };
7086 const struct pair *p;
7087
7088 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
7089 if (!strcasecmp(s, p->name)) {
7090 *portp = p->value;
7091 return true;
7092 }
7093 }
7094 return false;
7095 }
7096 }
7097
7098 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
7099 * Most ports' string representation is just the port number, but for special
7100 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7101 void
7102 ofputil_format_port(ofp_port_t port, struct ds *s)
7103 {
7104 char name[OFP_MAX_PORT_NAME_LEN];
7105
7106 ofputil_port_to_string(port, name, sizeof name);
7107 ds_put_cstr(s, name);
7108 }
7109
7110 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7111 * representation of OpenFlow port number 'port'. Most ports are represented
7112 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7113 * by name, e.g. "LOCAL". */
7114 void
7115 ofputil_port_to_string(ofp_port_t port,
7116 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
7117 {
7118 switch (port) {
7119 #define OFPUTIL_NAMED_PORT(NAME) \
7120 case OFPP_##NAME: \
7121 ovs_strlcpy(namebuf, #NAME, bufsize); \
7122 break;
7123 OFPUTIL_NAMED_PORTS
7124 #undef OFPUTIL_NAMED_PORT
7125
7126 default:
7127 snprintf(namebuf, bufsize, "%"PRIu16, port);
7128 break;
7129 }
7130 }
7131
7132 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7133 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7134 * (either "ANY" or "ALL").
7135 *
7136 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7137 * name. */
7138 bool
7139 ofputil_group_from_string(const char *s, uint32_t *group_idp)
7140 {
7141 if (!strcasecmp(s, "any")) {
7142 *group_idp = OFPG_ANY;
7143 } else if (!strcasecmp(s, "all")) {
7144 *group_idp = OFPG_ALL;
7145 } else if (!str_to_uint(s, 10, group_idp)) {
7146 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7147 "32-bit nonnegative integers or the keywords ANY or "
7148 "ALL.)", s);
7149 return false;
7150 }
7151
7152 return true;
7153 }
7154
7155 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7156 * Most groups' string representation is just the number, but for special
7157 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7158 void
7159 ofputil_format_group(uint32_t group_id, struct ds *s)
7160 {
7161 char name[MAX_GROUP_NAME_LEN];
7162
7163 ofputil_group_to_string(group_id, name, sizeof name);
7164 ds_put_cstr(s, name);
7165 }
7166
7167
7168 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7169 * representation of OpenFlow group ID 'group_id'. Most group are represented
7170 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7171 * by name, e.g. "ALL". */
7172 void
7173 ofputil_group_to_string(uint32_t group_id,
7174 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
7175 {
7176 switch (group_id) {
7177 case OFPG_ALL:
7178 ovs_strlcpy(namebuf, "ALL", bufsize);
7179 break;
7180
7181 case OFPG_ANY:
7182 ovs_strlcpy(namebuf, "ANY", bufsize);
7183 break;
7184
7185 default:
7186 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
7187 break;
7188 }
7189 }
7190
7191 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
7192 * 'ofp_version', tries to pull the first element from the array. If
7193 * successful, initializes '*pp' with an abstract representation of the
7194 * port and returns 0. If no ports remain to be decoded, returns EOF.
7195 * On an error, returns a positive OFPERR_* value. */
7196 int
7197 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
7198 struct ofputil_phy_port *pp)
7199 {
7200 memset(pp, 0, sizeof *pp);
7201
7202 switch (ofp_version) {
7203 case OFP10_VERSION: {
7204 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
7205 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
7206 }
7207 case OFP11_VERSION:
7208 case OFP12_VERSION:
7209 case OFP13_VERSION: {
7210 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
7211 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
7212 }
7213 case OFP14_VERSION:
7214 case OFP15_VERSION:
7215 case OFP16_VERSION:
7216 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
7217 default:
7218 OVS_NOT_REACHED();
7219 }
7220 }
7221
7222 static void
7223 ofputil_normalize_match__(struct match *match, bool may_log)
7224 {
7225 enum {
7226 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
7227 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
7228 MAY_NW_PROTO = 1 << 2, /* nw_proto */
7229 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
7230 MAY_ARP_SHA = 1 << 4, /* arp_sha */
7231 MAY_ARP_THA = 1 << 5, /* arp_tha */
7232 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
7233 MAY_ND_TARGET = 1 << 7, /* nd_target */
7234 MAY_MPLS = 1 << 8, /* mpls label and tc */
7235 } may_match;
7236
7237 struct flow_wildcards wc;
7238
7239 /* Figure out what fields may be matched. */
7240 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
7241 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
7242 if (match->flow.nw_proto == IPPROTO_TCP ||
7243 match->flow.nw_proto == IPPROTO_UDP ||
7244 match->flow.nw_proto == IPPROTO_SCTP ||
7245 match->flow.nw_proto == IPPROTO_ICMP) {
7246 may_match |= MAY_TP_ADDR;
7247 }
7248 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
7249 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
7250 if (match->flow.nw_proto == IPPROTO_TCP ||
7251 match->flow.nw_proto == IPPROTO_UDP ||
7252 match->flow.nw_proto == IPPROTO_SCTP) {
7253 may_match |= MAY_TP_ADDR;
7254 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
7255 may_match |= MAY_TP_ADDR;
7256 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
7257 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
7258 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
7259 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
7260 }
7261 }
7262 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
7263 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
7264 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
7265 } else if (eth_type_mpls(match->flow.dl_type)) {
7266 may_match = MAY_MPLS;
7267 } else {
7268 may_match = 0;
7269 }
7270
7271 /* Clear the fields that may not be matched. */
7272 wc = match->wc;
7273 if (!(may_match & MAY_NW_ADDR)) {
7274 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
7275 }
7276 if (!(may_match & MAY_TP_ADDR)) {
7277 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
7278 }
7279 if (!(may_match & MAY_NW_PROTO)) {
7280 wc.masks.nw_proto = 0;
7281 }
7282 if (!(may_match & MAY_IPVx)) {
7283 wc.masks.nw_tos = 0;
7284 wc.masks.nw_ttl = 0;
7285 }
7286 if (!(may_match & MAY_ARP_SHA)) {
7287 WC_UNMASK_FIELD(&wc, arp_sha);
7288 }
7289 if (!(may_match & MAY_ARP_THA)) {
7290 WC_UNMASK_FIELD(&wc, arp_tha);
7291 }
7292 if (!(may_match & MAY_IPV6)) {
7293 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
7294 wc.masks.ipv6_label = htonl(0);
7295 }
7296 if (!(may_match & MAY_ND_TARGET)) {
7297 wc.masks.nd_target = in6addr_any;
7298 }
7299 if (!(may_match & MAY_MPLS)) {
7300 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
7301 }
7302
7303 /* Log any changes. */
7304 if (!flow_wildcards_equal(&wc, &match->wc)) {
7305 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
7306 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
7307
7308 match->wc = wc;
7309 match_zero_wildcarded_fields(match);
7310
7311 if (log) {
7312 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
7313 VLOG_INFO("normalization changed ofp_match, details:");
7314 VLOG_INFO(" pre: %s", pre);
7315 VLOG_INFO("post: %s", post);
7316 free(pre);
7317 free(post);
7318 }
7319 }
7320 }
7321
7322 /* "Normalizes" the wildcards in 'match'. That means:
7323 *
7324 * 1. If the type of level N is known, then only the valid fields for that
7325 * level may be specified. For example, ARP does not have a TOS field,
7326 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
7327 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
7328 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
7329 * IPv4 flow.
7330 *
7331 * 2. If the type of level N is not known (or not understood by Open
7332 * vSwitch), then no fields at all for that level may be specified. For
7333 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
7334 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
7335 * SCTP flow.
7336 *
7337 * If this function changes 'match', it logs a rate-limited informational
7338 * message. */
7339 void
7340 ofputil_normalize_match(struct match *match)
7341 {
7342 ofputil_normalize_match__(match, true);
7343 }
7344
7345 /* Same as ofputil_normalize_match() without the logging. Thus, this function
7346 * is suitable for a program's internal use, whereas ofputil_normalize_match()
7347 * sense for use on flows received from elsewhere (so that a bug in the program
7348 * that sent them can be reported and corrected). */
7349 void
7350 ofputil_normalize_match_quiet(struct match *match)
7351 {
7352 ofputil_normalize_match__(match, false);
7353 }
7354
7355 static size_t
7356 parse_value(const char *s, const char *delimiters)
7357 {
7358 size_t n = 0;
7359
7360 /* Iterate until we reach a delimiter.
7361 *
7362 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7363 * terminator at the end of 's'. */
7364 while (!strchr(delimiters, s[n])) {
7365 if (s[n] == '(') {
7366 int level = 0;
7367 do {
7368 switch (s[n]) {
7369 case '\0':
7370 return n;
7371 case '(':
7372 level++;
7373 break;
7374 case ')':
7375 level--;
7376 break;
7377 }
7378 n++;
7379 } while (level > 0);
7380 } else {
7381 n++;
7382 }
7383 }
7384 return n;
7385 }
7386
7387 /* Parses a key or a key-value pair from '*stringp'.
7388 *
7389 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7390 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7391 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7392 * are substrings of '*stringp' created by replacing some of its bytes by null
7393 * terminators. Returns true.
7394 *
7395 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7396 * NULL and returns false. */
7397 bool
7398 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
7399 {
7400 /* Skip white space and delimiters. If that brings us to the end of the
7401 * input string, we are done and there are no more key-value pairs. */
7402 *stringp += strspn(*stringp, ", \t\r\n");
7403 if (**stringp == '\0') {
7404 *keyp = *valuep = NULL;
7405 return false;
7406 }
7407
7408 /* Extract the key and the delimiter that ends the key-value pair or begins
7409 * the value. Advance the input position past the key and delimiter. */
7410 char *key = *stringp;
7411 size_t key_len = strcspn(key, ":=(, \t\r\n");
7412 char key_delim = key[key_len];
7413 key[key_len] = '\0';
7414 *stringp += key_len + (key_delim != '\0');
7415
7416 /* Figure out what delimiter ends the value:
7417 *
7418 * - If key_delim is ":" or "=", the value extends until white space
7419 * or a comma.
7420 *
7421 * - If key_delim is "(", the value extends until ")".
7422 *
7423 * If there is no value, we are done. */
7424 const char *value_delims;
7425 if (key_delim == ':' || key_delim == '=') {
7426 value_delims = ", \t\r\n";
7427 } else if (key_delim == '(') {
7428 value_delims = ")";
7429 } else {
7430 *keyp = key;
7431 *valuep = key + key_len; /* Empty string. */
7432 return true;
7433 }
7434
7435 /* Extract the value. Advance the input position past the value and
7436 * delimiter. */
7437 char *value = *stringp;
7438 size_t value_len = parse_value(value, value_delims);
7439 char value_delim = value[value_len];
7440 value[value_len] = '\0';
7441 *stringp += value_len + (value_delim != '\0');
7442
7443 *keyp = key;
7444 *valuep = value;
7445 return true;
7446 }
7447
7448 /* Encode a dump ports request for 'port', the encoded message
7449 * will be for OpenFlow version 'ofp_version'. Returns message
7450 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7451 struct ofpbuf *
7452 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
7453 {
7454 struct ofpbuf *request;
7455
7456 switch (ofp_version) {
7457 case OFP10_VERSION: {
7458 struct ofp10_port_stats_request *req;
7459 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
7460 req = ofpbuf_put_zeros(request, sizeof *req);
7461 req->port_no = htons(ofp_to_u16(port));
7462 break;
7463 }
7464 case OFP11_VERSION:
7465 case OFP12_VERSION:
7466 case OFP13_VERSION:
7467 case OFP14_VERSION:
7468 case OFP15_VERSION:
7469 case OFP16_VERSION: {
7470 struct ofp11_port_stats_request *req;
7471 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
7472 req = ofpbuf_put_zeros(request, sizeof *req);
7473 req->port_no = ofputil_port_to_ofp11(port);
7474 break;
7475 }
7476 default:
7477 OVS_NOT_REACHED();
7478 }
7479
7480 return request;
7481 }
7482
7483 static void
7484 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
7485 struct ofp10_port_stats *ps10)
7486 {
7487 ps10->port_no = htons(ofp_to_u16(ops->port_no));
7488 memset(ps10->pad, 0, sizeof ps10->pad);
7489 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
7490 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
7491 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
7492 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
7493 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
7494 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
7495 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
7496 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
7497 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
7498 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
7499 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
7500 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
7501 }
7502
7503 static void
7504 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
7505 struct ofp11_port_stats *ps11)
7506 {
7507 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
7508 memset(ps11->pad, 0, sizeof ps11->pad);
7509 ps11->rx_packets = htonll(ops->stats.rx_packets);
7510 ps11->tx_packets = htonll(ops->stats.tx_packets);
7511 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
7512 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
7513 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
7514 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
7515 ps11->rx_errors = htonll(ops->stats.rx_errors);
7516 ps11->tx_errors = htonll(ops->stats.tx_errors);
7517 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7518 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
7519 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7520 ps11->collisions = htonll(ops->stats.collisions);
7521 }
7522
7523 static void
7524 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
7525 struct ofp13_port_stats *ps13)
7526 {
7527 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
7528 ps13->duration_sec = htonl(ops->duration_sec);
7529 ps13->duration_nsec = htonl(ops->duration_nsec);
7530 }
7531
7532 static void
7533 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
7534 struct ovs_list *replies)
7535 {
7536 struct ofp14_port_stats_prop_ethernet *eth;
7537 struct intel_port_stats_rfc2819 *stats_rfc2819;
7538 struct ofp14_port_stats *ps14;
7539 struct ofpbuf *reply;
7540
7541 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth +
7542 sizeof *stats_rfc2819);
7543
7544 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
7545 ps14->length = htons(sizeof *ps14 + sizeof *eth +
7546 sizeof *stats_rfc2819);
7547 memset(ps14->pad, 0, sizeof ps14->pad);
7548 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7549 ps14->duration_sec = htonl(ops->duration_sec);
7550 ps14->duration_nsec = htonl(ops->duration_nsec);
7551 ps14->rx_packets = htonll(ops->stats.rx_packets);
7552 ps14->tx_packets = htonll(ops->stats.tx_packets);
7553 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7554 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7555 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7556 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7557 ps14->rx_errors = htonll(ops->stats.rx_errors);
7558 ps14->tx_errors = htonll(ops->stats.tx_errors);
7559
7560 eth = ofpprop_put_zeros(reply, OFPPSPT14_ETHERNET, sizeof *eth);
7561 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7562 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7563 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7564 eth->collisions = htonll(ops->stats.collisions);
7565
7566 uint64_t prop_type = OFPPROP_EXP(INTEL_VENDOR_ID,
7567 INTEL_PORT_STATS_RFC2819);
7568
7569 stats_rfc2819 = ofpprop_put_zeros(reply, prop_type,
7570 sizeof *stats_rfc2819);
7571
7572 memset(stats_rfc2819->pad, 0, sizeof stats_rfc2819->pad);
7573 stats_rfc2819->rx_1_to_64_packets = htonll(ops->stats.rx_1_to_64_packets);
7574 stats_rfc2819->rx_65_to_127_packets =
7575 htonll(ops->stats.rx_65_to_127_packets);
7576 stats_rfc2819->rx_128_to_255_packets =
7577 htonll(ops->stats.rx_128_to_255_packets);
7578 stats_rfc2819->rx_256_to_511_packets =
7579 htonll(ops->stats.rx_256_to_511_packets);
7580 stats_rfc2819->rx_512_to_1023_packets =
7581 htonll(ops->stats.rx_512_to_1023_packets);
7582 stats_rfc2819->rx_1024_to_1522_packets =
7583 htonll(ops->stats.rx_1024_to_1522_packets);
7584 stats_rfc2819->rx_1523_to_max_packets =
7585 htonll(ops->stats.rx_1523_to_max_packets);
7586
7587 stats_rfc2819->tx_1_to_64_packets = htonll(ops->stats.tx_1_to_64_packets);
7588 stats_rfc2819->tx_65_to_127_packets =
7589 htonll(ops->stats.tx_65_to_127_packets);
7590 stats_rfc2819->tx_128_to_255_packets =
7591 htonll(ops->stats.tx_128_to_255_packets);
7592 stats_rfc2819->tx_256_to_511_packets =
7593 htonll(ops->stats.tx_256_to_511_packets);
7594 stats_rfc2819->tx_512_to_1023_packets =
7595 htonll(ops->stats.tx_512_to_1023_packets);
7596 stats_rfc2819->tx_1024_to_1522_packets =
7597 htonll(ops->stats.tx_1024_to_1522_packets);
7598 stats_rfc2819->tx_1523_to_max_packets =
7599 htonll(ops->stats.tx_1523_to_max_packets);
7600
7601 stats_rfc2819->tx_multicast_packets =
7602 htonll(ops->stats.tx_multicast_packets);
7603 stats_rfc2819->rx_broadcast_packets =
7604 htonll(ops->stats.rx_broadcast_packets);
7605 stats_rfc2819->tx_broadcast_packets =
7606 htonll(ops->stats.tx_broadcast_packets);
7607 stats_rfc2819->rx_undersized_errors =
7608 htonll(ops->stats.rx_undersized_errors);
7609 stats_rfc2819->rx_oversize_errors =
7610 htonll(ops->stats.rx_oversize_errors);
7611 stats_rfc2819->rx_fragmented_errors =
7612 htonll(ops->stats.rx_fragmented_errors);
7613 stats_rfc2819->rx_jabber_errors =
7614 htonll(ops->stats.rx_jabber_errors);
7615 }
7616
7617 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7618 void
7619 ofputil_append_port_stat(struct ovs_list *replies,
7620 const struct ofputil_port_stats *ops)
7621 {
7622 switch (ofpmp_version(replies)) {
7623 case OFP13_VERSION: {
7624 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7625 ofputil_port_stats_to_ofp13(ops, reply);
7626 break;
7627 }
7628 case OFP12_VERSION:
7629 case OFP11_VERSION: {
7630 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7631 ofputil_port_stats_to_ofp11(ops, reply);
7632 break;
7633 }
7634
7635 case OFP10_VERSION: {
7636 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7637 ofputil_port_stats_to_ofp10(ops, reply);
7638 break;
7639 }
7640
7641 case OFP14_VERSION:
7642 case OFP15_VERSION:
7643 case OFP16_VERSION:
7644 ofputil_append_ofp14_port_stats(ops, replies);
7645 break;
7646
7647 default:
7648 OVS_NOT_REACHED();
7649 }
7650 }
7651
7652 static enum ofperr
7653 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7654 const struct ofp10_port_stats *ps10)
7655 {
7656
7657 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
7658 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7659 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7660 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7661 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7662 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7663 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7664 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7665 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7666 ops->stats.rx_frame_errors =
7667 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7668 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7669 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7670 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
7671 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7672
7673 return 0;
7674 }
7675
7676 static enum ofperr
7677 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7678 const struct ofp11_port_stats *ps11)
7679 {
7680 enum ofperr error;
7681
7682 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7683 if (error) {
7684 return error;
7685 }
7686
7687 ops->stats.rx_packets = ntohll(ps11->rx_packets);
7688 ops->stats.tx_packets = ntohll(ps11->tx_packets);
7689 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7690 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7691 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7692 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7693 ops->stats.rx_errors = ntohll(ps11->rx_errors);
7694 ops->stats.tx_errors = ntohll(ps11->tx_errors);
7695 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7696 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7697 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7698 ops->stats.collisions = ntohll(ps11->collisions);
7699 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7700
7701 return 0;
7702 }
7703
7704 static enum ofperr
7705 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7706 const struct ofp13_port_stats *ps13)
7707 {
7708 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
7709 if (!error) {
7710 ops->duration_sec = ntohl(ps13->duration_sec);
7711 ops->duration_nsec = ntohl(ps13->duration_nsec);
7712 }
7713 return error;
7714 }
7715
7716 static enum ofperr
7717 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7718 struct ofputil_port_stats *ops)
7719 {
7720 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
7721
7722 if (payload->size != sizeof *eth) {
7723 return OFPERR_OFPBPC_BAD_LEN;
7724 }
7725
7726 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7727 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7728 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7729 ops->stats.collisions = ntohll(eth->collisions);
7730
7731 return 0;
7732 }
7733
7734 static enum ofperr
7735 parse_intel_port_stats_rfc2819_property(const struct ofpbuf *payload,
7736 struct ofputil_port_stats *ops)
7737 {
7738 const struct intel_port_stats_rfc2819 *rfc2819 = payload->data;
7739
7740 if (payload->size != sizeof *rfc2819) {
7741 return OFPERR_OFPBPC_BAD_LEN;
7742 }
7743 ops->stats.rx_1_to_64_packets = ntohll(rfc2819->rx_1_to_64_packets);
7744 ops->stats.rx_65_to_127_packets = ntohll(rfc2819->rx_65_to_127_packets);
7745 ops->stats.rx_128_to_255_packets = ntohll(rfc2819->rx_128_to_255_packets);
7746 ops->stats.rx_256_to_511_packets = ntohll(rfc2819->rx_256_to_511_packets);
7747 ops->stats.rx_512_to_1023_packets =
7748 ntohll(rfc2819->rx_512_to_1023_packets);
7749 ops->stats.rx_1024_to_1522_packets =
7750 ntohll(rfc2819->rx_1024_to_1522_packets);
7751 ops->stats.rx_1523_to_max_packets =
7752 ntohll(rfc2819->rx_1523_to_max_packets);
7753
7754 ops->stats.tx_1_to_64_packets = ntohll(rfc2819->tx_1_to_64_packets);
7755 ops->stats.tx_65_to_127_packets = ntohll(rfc2819->tx_65_to_127_packets);
7756 ops->stats.tx_128_to_255_packets = ntohll(rfc2819->tx_128_to_255_packets);
7757 ops->stats.tx_256_to_511_packets = ntohll(rfc2819->tx_256_to_511_packets);
7758 ops->stats.tx_512_to_1023_packets =
7759 ntohll(rfc2819->tx_512_to_1023_packets);
7760 ops->stats.tx_1024_to_1522_packets =
7761 ntohll(rfc2819->tx_1024_to_1522_packets);
7762 ops->stats.tx_1523_to_max_packets =
7763 ntohll(rfc2819->tx_1523_to_max_packets);
7764
7765 ops->stats.tx_multicast_packets = ntohll(rfc2819->tx_multicast_packets);
7766 ops->stats.rx_broadcast_packets = ntohll(rfc2819->rx_broadcast_packets);
7767 ops->stats.tx_broadcast_packets = ntohll(rfc2819->tx_broadcast_packets);
7768 ops->stats.rx_undersized_errors = ntohll(rfc2819->rx_undersized_errors);
7769
7770 ops->stats.rx_oversize_errors = ntohll(rfc2819->rx_oversize_errors);
7771 ops->stats.rx_fragmented_errors = ntohll(rfc2819->rx_fragmented_errors);
7772 ops->stats.rx_jabber_errors = ntohll(rfc2819->rx_jabber_errors);
7773
7774 return 0;
7775 }
7776
7777 static enum ofperr
7778 parse_intel_port_stats_property(const struct ofpbuf *payload,
7779 uint32_t exp_type,
7780 struct ofputil_port_stats *ops)
7781 {
7782 enum ofperr error;
7783
7784 switch (exp_type) {
7785 case INTEL_PORT_STATS_RFC2819:
7786 error = parse_intel_port_stats_rfc2819_property(payload, ops);
7787 break;
7788 default:
7789 error = OFPERR_OFPBPC_BAD_EXP_TYPE;
7790 break;
7791 }
7792
7793 return error;
7794 }
7795
7796 static enum ofperr
7797 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
7798 struct ofpbuf *msg)
7799 {
7800 const struct ofp14_port_stats *ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
7801 if (!ps14) {
7802 return OFPERR_OFPBRC_BAD_LEN;
7803 }
7804
7805 size_t len = ntohs(ps14->length);
7806 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
7807 return OFPERR_OFPBRC_BAD_LEN;
7808 }
7809 len -= sizeof *ps14;
7810
7811 enum ofperr error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
7812 if (error) {
7813 return error;
7814 }
7815
7816 ops->duration_sec = ntohl(ps14->duration_sec);
7817 ops->duration_nsec = ntohl(ps14->duration_nsec);
7818 ops->stats.rx_packets = ntohll(ps14->rx_packets);
7819 ops->stats.tx_packets = ntohll(ps14->tx_packets);
7820 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
7821 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
7822 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
7823 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
7824 ops->stats.rx_errors = ntohll(ps14->rx_errors);
7825 ops->stats.tx_errors = ntohll(ps14->tx_errors);
7826
7827
7828 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
7829 len);
7830 while (properties.size > 0) {
7831 struct ofpbuf payload;
7832 enum ofperr error;
7833 uint64_t type = 0;
7834
7835 error = ofpprop_pull(&properties, &payload, &type);
7836 if (error) {
7837 return error;
7838 }
7839 switch (type) {
7840 case OFPPSPT14_ETHERNET:
7841 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
7842 break;
7843 case OFPPROP_EXP(INTEL_VENDOR_ID, INTEL_PORT_STATS_RFC2819):
7844 error = parse_intel_port_stats_property(&payload,
7845 INTEL_PORT_STATS_RFC2819,
7846 ops);
7847 break;
7848 default:
7849 error = OFPPROP_UNKNOWN(true, "port stats", type);
7850 break;
7851 }
7852
7853 if (error) {
7854 return error;
7855 }
7856 }
7857
7858 return 0;
7859 }
7860
7861 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7862 * message 'oh'. */
7863 size_t
7864 ofputil_count_port_stats(const struct ofp_header *oh)
7865 {
7866 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
7867 ofpraw_pull_assert(&b);
7868
7869 for (size_t n = 0; ; n++) {
7870 struct ofputil_port_stats ps;
7871 if (ofputil_decode_port_stats(&ps, &b)) {
7872 return n;
7873 }
7874 }
7875 }
7876
7877 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7878 * ofputil_port_stats in 'ps'.
7879 *
7880 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7881 * message. Calling this function multiple times for a single 'msg' iterates
7882 * through the replies. The caller must initially leave 'msg''s layer pointers
7883 * null and not modify them between calls.
7884 *
7885 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7886 * otherwise a positive errno value. */
7887 int
7888 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
7889 {
7890 enum ofperr error;
7891 enum ofpraw raw;
7892
7893 memset(&(ps->stats), 0xFF, sizeof (ps->stats));
7894
7895 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7896 : ofpraw_pull(&raw, msg));
7897 if (error) {
7898 return error;
7899 }
7900
7901 if (!msg->size) {
7902 return EOF;
7903 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
7904 return ofputil_pull_ofp14_port_stats(ps, msg);
7905 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
7906 const struct ofp13_port_stats *ps13;
7907 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7908 if (!ps13) {
7909 goto bad_len;
7910 }
7911 return ofputil_port_stats_from_ofp13(ps, ps13);
7912 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7913 const struct ofp11_port_stats *ps11;
7914
7915 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7916 if (!ps11) {
7917 goto bad_len;
7918 }
7919 return ofputil_port_stats_from_ofp11(ps, ps11);
7920 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7921 const struct ofp10_port_stats *ps10;
7922
7923 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7924 if (!ps10) {
7925 goto bad_len;
7926 }
7927 return ofputil_port_stats_from_ofp10(ps, ps10);
7928 } else {
7929 OVS_NOT_REACHED();
7930 }
7931
7932 bad_len:
7933 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7934 "bytes at end", msg->size);
7935 return OFPERR_OFPBRC_BAD_LEN;
7936 }
7937
7938 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7939 * port number and stores the latter in '*ofp10_port'.
7940 * Returns 0 if successful, otherwise an OFPERR_* number. */
7941 enum ofperr
7942 ofputil_decode_port_stats_request(const struct ofp_header *request,
7943 ofp_port_t *ofp10_port)
7944 {
7945 switch ((enum ofp_version)request->version) {
7946 case OFP16_VERSION:
7947 case OFP15_VERSION:
7948 case OFP14_VERSION:
7949 case OFP13_VERSION:
7950 case OFP12_VERSION:
7951 case OFP11_VERSION: {
7952 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7953 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7954 }
7955
7956 case OFP10_VERSION: {
7957 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7958 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7959 return 0;
7960 }
7961
7962 default:
7963 OVS_NOT_REACHED();
7964 }
7965 }
7966
7967 static void
7968 ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats *ois,
7969 struct nx_ipfix_stats_reply *reply)
7970 {
7971 reply->collector_set_id = htonl(ois->collector_set_id);
7972 reply->total_flows = htonll(ois->total_flows);
7973 reply->current_flows = htonll(ois->current_flows);
7974 reply->pkts = htonll(ois->pkts);
7975 reply->ipv4_pkts = htonll(ois->ipv4_pkts);
7976 reply->ipv6_pkts = htonll(ois->ipv6_pkts);
7977 reply->error_pkts = htonll(ois->error_pkts);
7978 reply->ipv4_error_pkts = htonll(ois->ipv4_error_pkts);
7979 reply->ipv6_error_pkts = htonll(ois->ipv6_error_pkts);
7980 reply->tx_pkts = htonll(ois->tx_pkts);
7981 reply->tx_errors = htonll(ois->tx_errors);
7982 memset(reply->pad, 0, sizeof reply->pad);
7983 }
7984
7985 /* Encode a ipfix stat for 'ois' and append it to 'replies'. */
7986 void
7987 ofputil_append_ipfix_stat(struct ovs_list *replies,
7988 const struct ofputil_ipfix_stats *ois)
7989 {
7990 struct nx_ipfix_stats_reply *reply = ofpmp_append(replies, sizeof *reply);
7991 ofputil_ipfix_stats_to_reply(ois, reply);
7992 }
7993
7994 static enum ofperr
7995 ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats *is,
7996 const struct nx_ipfix_stats_reply *reply)
7997 {
7998 is->collector_set_id = ntohl(reply->collector_set_id);
7999 is->total_flows = ntohll(reply->total_flows);
8000 is->current_flows = ntohll(reply->current_flows);
8001 is->pkts = ntohll(reply->pkts);
8002 is->ipv4_pkts = ntohll(reply->ipv4_pkts);
8003 is->ipv6_pkts = ntohll(reply->ipv6_pkts);
8004 is->error_pkts = ntohll(reply->error_pkts);
8005 is->ipv4_error_pkts = ntohll(reply->ipv4_error_pkts);
8006 is->ipv6_error_pkts = ntohll(reply->ipv6_error_pkts);
8007 is->tx_pkts = ntohll(reply->tx_pkts);
8008 is->tx_errors = ntohll(reply->tx_errors);
8009
8010 return 0;
8011 }
8012
8013 int
8014 ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats *is, struct ofpbuf *msg)
8015 {
8016 enum ofperr error;
8017 enum ofpraw raw;
8018
8019 memset(is, 0xFF, sizeof (*is));
8020
8021 error = (msg->header ? ofpraw_decode(&raw, msg->header)
8022 : ofpraw_pull(&raw, msg));
8023 if (error) {
8024 return error;
8025 }
8026
8027 if (!msg->size) {
8028 return EOF;
8029 } else if (raw == OFPRAW_NXST_IPFIX_BRIDGE_REPLY ||
8030 raw == OFPRAW_NXST_IPFIX_FLOW_REPLY) {
8031 struct nx_ipfix_stats_reply *reply;
8032
8033 reply = ofpbuf_try_pull(msg, sizeof *reply);
8034 return ofputil_ipfix_stats_from_nx(is, reply);
8035 } else {
8036 OVS_NOT_REACHED();
8037 }
8038 }
8039
8040
8041 /* Returns the number of ipfix stats elements in
8042 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8043 * message 'oh'. */
8044 size_t
8045 ofputil_count_ipfix_stats(const struct ofp_header *oh)
8046 {
8047 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
8048 ofpraw_pull_assert(&b);
8049
8050 return b.size / sizeof(struct ofputil_ipfix_stats);
8051 }
8052
8053 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8054 void
8055 ofputil_bucket_list_destroy(struct ovs_list *buckets)
8056 {
8057 struct ofputil_bucket *bucket;
8058
8059 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
8060 free(bucket->ofpacts);
8061 free(bucket);
8062 }
8063 }
8064
8065 /* Clones 'bucket' and its ofpacts data */
8066 static struct ofputil_bucket *
8067 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
8068 {
8069 struct ofputil_bucket *new;
8070
8071 new = xmemdup(bucket, sizeof *bucket);
8072 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
8073
8074 return new;
8075 }
8076
8077 /* Clones each of the buckets in the list 'src' appending them
8078 * in turn to 'dest' which should be an initialised list.
8079 * An exception is that if the pointer value of a bucket in 'src'
8080 * matches 'skip' then it is not cloned or appended to 'dest'.
8081 * This allows all of 'src' or 'all of 'src' except 'skip' to
8082 * be cloned and appended to 'dest'. */
8083 void
8084 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
8085 const struct ofputil_bucket *skip)
8086 {
8087 struct ofputil_bucket *bucket;
8088
8089 LIST_FOR_EACH (bucket, list_node, src) {
8090 struct ofputil_bucket *new_bucket;
8091
8092 if (bucket == skip) {
8093 continue;
8094 }
8095
8096 new_bucket = ofputil_bucket_clone_data(bucket);
8097 ovs_list_push_back(dest, &new_bucket->list_node);
8098 }
8099 }
8100
8101 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8102 * Returns the first bucket found or NULL if no buckets are found. */
8103 struct ofputil_bucket *
8104 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
8105 {
8106 struct ofputil_bucket *bucket;
8107
8108 if (bucket_id > OFPG15_BUCKET_MAX) {
8109 return NULL;
8110 }
8111
8112 LIST_FOR_EACH (bucket, list_node, buckets) {
8113 if (bucket->bucket_id == bucket_id) {
8114 return bucket;
8115 }
8116 }
8117
8118 return NULL;
8119 }
8120
8121 /* Returns true if more than one bucket in the list 'buckets'
8122 * have the same bucket id. Returns false otherwise. */
8123 bool
8124 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
8125 {
8126 struct ofputil_bucket *i, *j;
8127
8128 LIST_FOR_EACH (i, list_node, buckets) {
8129 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
8130 if (i == j) {
8131 break;
8132 }
8133 if (i->bucket_id == j->bucket_id) {
8134 return true;
8135 }
8136 }
8137 }
8138
8139 return false;
8140 }
8141
8142 /* Returns the bucket at the front of the list 'buckets'.
8143 * Undefined if 'buckets is empty. */
8144 struct ofputil_bucket *
8145 ofputil_bucket_list_front(const struct ovs_list *buckets)
8146 {
8147 static struct ofputil_bucket *bucket;
8148
8149 ASSIGN_CONTAINER(bucket, ovs_list_front(buckets), list_node);
8150
8151 return bucket;
8152 }
8153
8154 /* Returns the bucket at the back of the list 'buckets'.
8155 * Undefined if 'buckets is empty. */
8156 struct ofputil_bucket *
8157 ofputil_bucket_list_back(const struct ovs_list *buckets)
8158 {
8159 static struct ofputil_bucket *bucket;
8160
8161 ASSIGN_CONTAINER(bucket, ovs_list_back(buckets), list_node);
8162
8163 return bucket;
8164 }
8165
8166 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8167 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8168 * for all groups.)
8169 *
8170 * Group statistics include packet and byte counts for each group. */
8171 struct ofpbuf *
8172 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
8173 uint32_t group_id)
8174 {
8175 struct ofpbuf *request;
8176
8177 switch (ofp_version) {
8178 case OFP10_VERSION:
8179 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8180 "(\'-O OpenFlow11\')");
8181 case OFP11_VERSION:
8182 case OFP12_VERSION:
8183 case OFP13_VERSION:
8184 case OFP14_VERSION:
8185 case OFP15_VERSION:
8186 case OFP16_VERSION: {
8187 struct ofp11_group_stats_request *req;
8188 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
8189 req = ofpbuf_put_zeros(request, sizeof *req);
8190 req->group_id = htonl(group_id);
8191 break;
8192 }
8193 default:
8194 OVS_NOT_REACHED();
8195 }
8196
8197 return request;
8198 }
8199
8200 void
8201 ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
8202 {
8203 ofputil_bucket_list_destroy(&gd->buckets);
8204 ofputil_group_properties_destroy(&gd->props);
8205 }
8206
8207 /* Decodes the OpenFlow group description request in 'oh', returning the group
8208 * whose description is requested, or OFPG_ALL if stats for all groups was
8209 * requested. */
8210 uint32_t
8211 ofputil_decode_group_desc_request(const struct ofp_header *oh)
8212 {
8213 struct ofpbuf request = ofpbuf_const_initializer(oh, ntohs(oh->length));
8214 enum ofpraw raw = ofpraw_pull_assert(&request);
8215 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
8216 return OFPG_ALL;
8217 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
8218 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
8219 return ntohl(*group_id);
8220 } else {
8221 OVS_NOT_REACHED();
8222 }
8223 }
8224
8225 /* Returns an OpenFlow group description request for OpenFlow version
8226 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8227 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8228 * groups).
8229 *
8230 * Group descriptions include the bucket and action configuration for each
8231 * group. */
8232 struct ofpbuf *
8233 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
8234 uint32_t group_id)
8235 {
8236 struct ofpbuf *request;
8237
8238 switch (ofp_version) {
8239 case OFP10_VERSION:
8240 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8241 "(\'-O OpenFlow11\')");
8242 case OFP11_VERSION:
8243 case OFP12_VERSION:
8244 case OFP13_VERSION:
8245 case OFP14_VERSION:
8246 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
8247 ofp_version, 0);
8248 break;
8249 case OFP15_VERSION:
8250 case OFP16_VERSION: {
8251 struct ofp15_group_desc_request *req;
8252 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
8253 ofp_version, 0);
8254 req = ofpbuf_put_zeros(request, sizeof *req);
8255 req->group_id = htonl(group_id);
8256 break;
8257 }
8258 default:
8259 OVS_NOT_REACHED();
8260 }
8261
8262 return request;
8263 }
8264
8265 static void
8266 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
8267 struct ofp11_bucket_counter bucket_cnts[])
8268 {
8269 int i;
8270
8271 for (i = 0; i < gs->n_buckets; i++) {
8272 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
8273 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
8274 }
8275 }
8276
8277 static void
8278 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
8279 struct ofp11_group_stats *gs11, size_t length,
8280 struct ofp11_bucket_counter bucket_cnts[])
8281 {
8282 memset(gs11, 0, sizeof *gs11);
8283 gs11->length = htons(length);
8284 gs11->group_id = htonl(gs->group_id);
8285 gs11->ref_count = htonl(gs->ref_count);
8286 gs11->packet_count = htonll(gs->packet_count);
8287 gs11->byte_count = htonll(gs->byte_count);
8288 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
8289 }
8290
8291 static void
8292 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
8293 struct ofp13_group_stats *gs13, size_t length,
8294 struct ofp11_bucket_counter bucket_cnts[])
8295 {
8296 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
8297 gs13->duration_sec = htonl(gs->duration_sec);
8298 gs13->duration_nsec = htonl(gs->duration_nsec);
8299
8300 }
8301
8302 /* Encodes 'gs' properly for the format of the list of group statistics
8303 * replies already begun in 'replies' and appends it to the list. 'replies'
8304 * must have originally been initialized with ofpmp_init(). */
8305 void
8306 ofputil_append_group_stats(struct ovs_list *replies,
8307 const struct ofputil_group_stats *gs)
8308 {
8309 size_t bucket_counter_size;
8310 struct ofp11_bucket_counter *bucket_counters;
8311 size_t length;
8312
8313 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
8314
8315 switch (ofpmp_version(replies)) {
8316 case OFP11_VERSION:
8317 case OFP12_VERSION:{
8318 struct ofp11_group_stats *gs11;
8319
8320 length = sizeof *gs11 + bucket_counter_size;
8321 gs11 = ofpmp_append(replies, length);
8322 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
8323 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
8324 break;
8325 }
8326
8327 case OFP13_VERSION:
8328 case OFP14_VERSION:
8329 case OFP15_VERSION:
8330 case OFP16_VERSION: {
8331 struct ofp13_group_stats *gs13;
8332
8333 length = sizeof *gs13 + bucket_counter_size;
8334 gs13 = ofpmp_append(replies, length);
8335 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
8336 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
8337 break;
8338 }
8339
8340 case OFP10_VERSION:
8341 default:
8342 OVS_NOT_REACHED();
8343 }
8344 }
8345 /* Returns an OpenFlow group features request for OpenFlow version
8346 * 'ofp_version'. */
8347 struct ofpbuf *
8348 ofputil_encode_group_features_request(enum ofp_version ofp_version)
8349 {
8350 struct ofpbuf *request = NULL;
8351
8352 switch (ofp_version) {
8353 case OFP10_VERSION:
8354 case OFP11_VERSION:
8355 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8356 "(\'-O OpenFlow12\')");
8357 case OFP12_VERSION:
8358 case OFP13_VERSION:
8359 case OFP14_VERSION:
8360 case OFP15_VERSION:
8361 case OFP16_VERSION:
8362 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
8363 ofp_version, 0);
8364 break;
8365 default:
8366 OVS_NOT_REACHED();
8367 }
8368
8369 return request;
8370 }
8371
8372 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
8373 * group features request 'request'. */
8374 struct ofpbuf *
8375 ofputil_encode_group_features_reply(
8376 const struct ofputil_group_features *features,
8377 const struct ofp_header *request)
8378 {
8379 struct ofp12_group_features_stats *ogf;
8380 struct ofpbuf *reply;
8381 int i;
8382
8383 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
8384 request->version, request->xid, 0);
8385 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
8386 ogf->types = htonl(features->types);
8387 ogf->capabilities = htonl(features->capabilities);
8388 for (i = 0; i < OFPGT12_N_TYPES; i++) {
8389 ogf->max_groups[i] = htonl(features->max_groups[i]);
8390 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
8391 request->version);
8392 }
8393
8394 return reply;
8395 }
8396
8397 /* Decodes group features reply 'oh' into 'features'. */
8398 void
8399 ofputil_decode_group_features_reply(const struct ofp_header *oh,
8400 struct ofputil_group_features *features)
8401 {
8402 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
8403 int i;
8404
8405 features->types = ntohl(ogf->types);
8406 features->capabilities = ntohl(ogf->capabilities);
8407 for (i = 0; i < OFPGT12_N_TYPES; i++) {
8408 features->max_groups[i] = ntohl(ogf->max_groups[i]);
8409 features->ofpacts[i] = ofpact_bitmap_from_openflow(
8410 ogf->actions[i], oh->version);
8411 }
8412 }
8413
8414 /* Parse a group status request message into a 32 bit OpenFlow 1.1
8415 * group ID and stores the latter in '*group_id'.
8416 * Returns 0 if successful, otherwise an OFPERR_* number. */
8417 enum ofperr
8418 ofputil_decode_group_stats_request(const struct ofp_header *request,
8419 uint32_t *group_id)
8420 {
8421 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
8422 *group_id = ntohl(gsr11->group_id);
8423 return 0;
8424 }
8425
8426 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
8427 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8428 * caller to eventually free.
8429 *
8430 * Multiple group stats replies can be packed into a single OpenFlow message.
8431 * Calling this function multiple times for a single 'msg' iterates through the
8432 * replies. The caller must initially leave 'msg''s layer pointers null and
8433 * not modify them between calls.
8434 *
8435 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8436 * otherwise a positive errno value. */
8437 int
8438 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
8439 struct ofputil_group_stats *gs)
8440 {
8441 struct ofp11_bucket_counter *obc;
8442 struct ofp11_group_stats *ogs11;
8443 enum ofpraw raw;
8444 enum ofperr error;
8445 size_t base_len;
8446 size_t length;
8447 size_t i;
8448
8449 gs->bucket_stats = NULL;
8450 error = (msg->header ? ofpraw_decode(&raw, msg->header)
8451 : ofpraw_pull(&raw, msg));
8452 if (error) {
8453 return error;
8454 }
8455
8456 if (!msg->size) {
8457 return EOF;
8458 }
8459
8460 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
8461 base_len = sizeof *ogs11;
8462 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
8463 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
8464 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
8465 struct ofp13_group_stats *ogs13;
8466
8467 base_len = sizeof *ogs13;
8468 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
8469 if (ogs13) {
8470 ogs11 = &ogs13->gs;
8471 gs->duration_sec = ntohl(ogs13->duration_sec);
8472 gs->duration_nsec = ntohl(ogs13->duration_nsec);
8473 } else {
8474 ogs11 = NULL;
8475 }
8476 } else {
8477 OVS_NOT_REACHED();
8478 }
8479
8480 if (!ogs11) {
8481 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
8482 ofpraw_get_name(raw), msg->size);
8483 return OFPERR_OFPBRC_BAD_LEN;
8484 }
8485 length = ntohs(ogs11->length);
8486 if (length < sizeof base_len) {
8487 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
8488 ofpraw_get_name(raw), length);
8489 return OFPERR_OFPBRC_BAD_LEN;
8490 }
8491
8492 gs->group_id = ntohl(ogs11->group_id);
8493 gs->ref_count = ntohl(ogs11->ref_count);
8494 gs->packet_count = ntohll(ogs11->packet_count);
8495 gs->byte_count = ntohll(ogs11->byte_count);
8496
8497 gs->n_buckets = (length - base_len) / sizeof *obc;
8498 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
8499 if (!obc) {
8500 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
8501 ofpraw_get_name(raw), msg->size);
8502 return OFPERR_OFPBRC_BAD_LEN;
8503 }
8504
8505 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
8506 for (i = 0; i < gs->n_buckets; i++) {
8507 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
8508 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
8509 }
8510
8511 return 0;
8512 }
8513
8514 static void
8515 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
8516 struct ofpbuf *openflow, enum ofp_version ofp_version)
8517 {
8518 struct ofp11_bucket *ob;
8519 size_t start;
8520
8521 start = openflow->size;
8522 ofpbuf_put_zeros(openflow, sizeof *ob);
8523 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8524 openflow, ofp_version);
8525 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
8526 ob->len = htons(openflow->size - start);
8527 ob->weight = htons(bucket->weight);
8528 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
8529 ob->watch_group = htonl(bucket->watch_group);
8530 }
8531
8532 static void
8533 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
8534 uint32_t bucket_id, enum ofp11_group_type group_type,
8535 struct ofpbuf *openflow, enum ofp_version ofp_version)
8536 {
8537 struct ofp15_bucket *ob;
8538 size_t start, actions_start, actions_len;
8539
8540 start = openflow->size;
8541 ofpbuf_put_zeros(openflow, sizeof *ob);
8542
8543 actions_start = openflow->size;
8544 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8545 openflow, ofp_version);
8546 actions_len = openflow->size - actions_start;
8547
8548 if (group_type == OFPGT11_SELECT) {
8549 ofpprop_put_u16(openflow, OFPGBPT15_WEIGHT, bucket->weight);
8550 }
8551 if (bucket->watch_port != OFPP_ANY) {
8552 ofpprop_put_be32(openflow, OFPGBPT15_WATCH_PORT,
8553 ofputil_port_to_ofp11(bucket->watch_port));
8554 }
8555 if (bucket->watch_group != OFPG_ANY) {
8556 ofpprop_put_u32(openflow, OFPGBPT15_WATCH_GROUP, bucket->watch_group);
8557 }
8558
8559 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
8560 ob->len = htons(openflow->size - start);
8561 ob->action_array_len = htons(actions_len);
8562 ob->bucket_id = htonl(bucket_id);
8563 }
8564
8565 static void
8566 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
8567 const struct ofputil_group_props *gp,
8568 struct ofpbuf *openflow)
8569 {
8570 struct ntr_group_prop_selection_method *prop;
8571 size_t start;
8572
8573 start = openflow->size;
8574 ofpbuf_put_zeros(openflow, sizeof *prop);
8575 oxm_put_field_array(openflow, &gp->fields, ofp_version);
8576 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
8577 prop->type = htons(OFPGPT15_EXPERIMENTER);
8578 prop->experimenter = htonl(NTR_VENDOR_ID);
8579 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
8580 strcpy(prop->selection_method, gp->selection_method);
8581 prop->selection_method_param = htonll(gp->selection_method_param);
8582 ofpprop_end(openflow, start);
8583 }
8584
8585 static void
8586 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
8587 const struct ovs_list *buckets,
8588 struct ovs_list *replies,
8589 enum ofp_version version)
8590 {
8591 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
8592 struct ofp11_group_desc_stats *ogds;
8593 struct ofputil_bucket *bucket;
8594 size_t start_ogds;
8595
8596 start_ogds = reply->size;
8597 ofpbuf_put_zeros(reply, sizeof *ogds);
8598 LIST_FOR_EACH (bucket, list_node, buckets) {
8599 ofputil_put_ofp11_bucket(bucket, reply, version);
8600 }
8601 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8602 ogds->length = htons(reply->size - start_ogds);
8603 ogds->type = gds->type;
8604 ogds->group_id = htonl(gds->group_id);
8605
8606 ofpmp_postappend(replies, start_ogds);
8607 }
8608
8609 static void
8610 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
8611 const struct ovs_list *buckets,
8612 struct ovs_list *replies,
8613 enum ofp_version version)
8614 {
8615 struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
8616 struct ofp15_group_desc_stats *ogds;
8617 struct ofputil_bucket *bucket;
8618 size_t start_ogds, start_buckets;
8619
8620 start_ogds = reply->size;
8621 ofpbuf_put_zeros(reply, sizeof *ogds);
8622 start_buckets = reply->size;
8623 LIST_FOR_EACH (bucket, list_node, buckets) {
8624 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
8625 gds->type, reply, version);
8626 }
8627 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8628 ogds->type = gds->type;
8629 ogds->group_id = htonl(gds->group_id);
8630 ogds->bucket_list_len = htons(reply->size - start_buckets);
8631
8632 /* Add group properties */
8633 if (gds->props.selection_method[0]) {
8634 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
8635 reply);
8636 }
8637 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8638 ogds->length = htons(reply->size - start_ogds);
8639
8640 ofpmp_postappend(replies, start_ogds);
8641 }
8642
8643 /* Appends a group stats reply that contains the data in 'gds' to those already
8644 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8645 * initialized with ofpmp_init(). */
8646 void
8647 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
8648 const struct ovs_list *buckets,
8649 struct ovs_list *replies)
8650 {
8651 enum ofp_version version = ofpmp_version(replies);
8652
8653 switch (version)
8654 {
8655 case OFP11_VERSION:
8656 case OFP12_VERSION:
8657 case OFP13_VERSION:
8658 case OFP14_VERSION:
8659 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
8660 break;
8661
8662 case OFP15_VERSION:
8663 case OFP16_VERSION:
8664 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
8665 break;
8666
8667 case OFP10_VERSION:
8668 default:
8669 OVS_NOT_REACHED();
8670 }
8671 }
8672
8673 static enum ofperr
8674 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
8675 enum ofp_version version, struct ovs_list *buckets)
8676 {
8677 struct ofp11_bucket *ob;
8678 uint32_t bucket_id = 0;
8679
8680 ovs_list_init(buckets);
8681 while (buckets_length > 0) {
8682 struct ofputil_bucket *bucket;
8683 struct ofpbuf ofpacts;
8684 enum ofperr error;
8685 size_t ob_len;
8686
8687 ob = (buckets_length >= sizeof *ob
8688 ? ofpbuf_try_pull(msg, sizeof *ob)
8689 : NULL);
8690 if (!ob) {
8691 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
8692 buckets_length);
8693 return OFPERR_OFPGMFC_BAD_BUCKET;
8694 }
8695
8696 ob_len = ntohs(ob->len);
8697 if (ob_len < sizeof *ob) {
8698 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8699 "%"PRIuSIZE" is not valid", ob_len);
8700 return OFPERR_OFPGMFC_BAD_BUCKET;
8701 } else if (ob_len > buckets_length) {
8702 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8703 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
8704 ob_len, buckets_length);
8705 return OFPERR_OFPGMFC_BAD_BUCKET;
8706 }
8707 buckets_length -= ob_len;
8708
8709 ofpbuf_init(&ofpacts, 0);
8710 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
8711 version, &ofpacts);
8712 if (error) {
8713 ofpbuf_uninit(&ofpacts);
8714 ofputil_bucket_list_destroy(buckets);
8715 return error;
8716 }
8717
8718 bucket = xzalloc(sizeof *bucket);
8719 bucket->weight = ntohs(ob->weight);
8720 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8721 if (error) {
8722 ofpbuf_uninit(&ofpacts);
8723 ofputil_bucket_list_destroy(buckets);
8724 return OFPERR_OFPGMFC_BAD_WATCH;
8725 }
8726 bucket->watch_group = ntohl(ob->watch_group);
8727 bucket->bucket_id = bucket_id++;
8728
8729 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8730 bucket->ofpacts_len = ofpacts.size;
8731 ovs_list_push_back(buckets, &bucket->list_node);
8732 }
8733
8734 return 0;
8735 }
8736
8737 static enum ofperr
8738 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
8739 enum ofp_version version, uint8_t group_type,
8740 struct ovs_list *buckets)
8741 {
8742 struct ofp15_bucket *ob;
8743
8744 ovs_list_init(buckets);
8745 while (buckets_length > 0) {
8746 struct ofputil_bucket *bucket = NULL;
8747 struct ofpbuf ofpacts;
8748 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
8749 size_t ob_len, actions_len, properties_len;
8750 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8751 ovs_be32 watch_group = htonl(OFPG_ANY);
8752 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
8753
8754 ofpbuf_init(&ofpacts, 0);
8755
8756 ob = ofpbuf_try_pull(msg, sizeof *ob);
8757 if (!ob) {
8758 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
8759 " leftover bytes", buckets_length);
8760 goto err;
8761 }
8762
8763 ob_len = ntohs(ob->len);
8764 actions_len = ntohs(ob->action_array_len);
8765
8766 if (ob_len < sizeof *ob) {
8767 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8768 "%"PRIuSIZE" is not valid", ob_len);
8769 goto err;
8770 } else if (ob_len > buckets_length) {
8771 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8772 "%"PRIuSIZE" exceeds remaining buckets data size %"
8773 PRIuSIZE, ob_len, buckets_length);
8774 goto err;
8775 } else if (actions_len > ob_len - sizeof *ob) {
8776 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
8777 "length %"PRIuSIZE" exceeds remaining bucket "
8778 "data size %"PRIuSIZE, actions_len,
8779 ob_len - sizeof *ob);
8780 goto err;
8781 }
8782 buckets_length -= ob_len;
8783
8784 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
8785 &ofpacts);
8786 if (err) {
8787 goto err;
8788 }
8789
8790 properties_len = ob_len - sizeof *ob - actions_len;
8791 struct ofpbuf properties = ofpbuf_const_initializer(
8792 ofpbuf_pull(msg, properties_len), properties_len);
8793 while (properties.size > 0) {
8794 struct ofpbuf payload;
8795 uint64_t type;
8796
8797 err = ofpprop_pull(&properties, &payload, &type);
8798 if (err) {
8799 goto err;
8800 }
8801
8802 switch (type) {
8803 case OFPGBPT15_WEIGHT:
8804 err = ofpprop_parse_be16(&payload, &weight);
8805 break;
8806
8807 case OFPGBPT15_WATCH_PORT:
8808 err = ofpprop_parse_be32(&payload, &watch_port);
8809 break;
8810
8811 case OFPGBPT15_WATCH_GROUP:
8812 err = ofpprop_parse_be32(&payload, &watch_group);
8813 break;
8814
8815 default:
8816 err = OFPPROP_UNKNOWN(false, "group bucket", type);
8817 break;
8818 }
8819
8820 if (err) {
8821 goto err;
8822 }
8823 }
8824
8825 bucket = xzalloc(sizeof *bucket);
8826
8827 bucket->weight = ntohs(weight);
8828 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
8829 if (err) {
8830 err = OFPERR_OFPGMFC_BAD_WATCH;
8831 goto err;
8832 }
8833 bucket->watch_group = ntohl(watch_group);
8834 bucket->bucket_id = ntohl(ob->bucket_id);
8835 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8836 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
8837 bucket->bucket_id);
8838 err = OFPERR_OFPGMFC_BAD_BUCKET;
8839 goto err;
8840 }
8841
8842 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8843 bucket->ofpacts_len = ofpacts.size;
8844 ovs_list_push_back(buckets, &bucket->list_node);
8845
8846 continue;
8847
8848 err:
8849 free(bucket);
8850 ofpbuf_uninit(&ofpacts);
8851 ofputil_bucket_list_destroy(buckets);
8852 return err;
8853 }
8854
8855 if (ofputil_bucket_check_duplicate_id(buckets)) {
8856 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
8857 ofputil_bucket_list_destroy(buckets);
8858 return OFPERR_OFPGMFC_BAD_BUCKET;
8859 }
8860
8861 return 0;
8862 }
8863
8864 static void
8865 ofputil_init_group_properties(struct ofputil_group_props *gp)
8866 {
8867 memset(gp, 0, sizeof *gp);
8868 }
8869
8870 void
8871 ofputil_group_properties_copy(struct ofputil_group_props *to,
8872 const struct ofputil_group_props *from)
8873 {
8874 *to = *from;
8875 to->fields.values = xmemdup(from->fields.values, from->fields.values_size);
8876 }
8877
8878 void
8879 ofputil_group_properties_destroy(struct ofputil_group_props *gp)
8880 {
8881 free(gp->fields.values);
8882 }
8883
8884 static enum ofperr
8885 parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
8886 enum ofp11_group_type group_type,
8887 enum ofp15_group_mod_command group_cmd,
8888 struct ofputil_group_props *gp)
8889 {
8890 struct ntr_group_prop_selection_method *prop = payload->data;
8891 size_t fields_len, method_len;
8892 enum ofperr error;
8893
8894 switch (group_type) {
8895 case OFPGT11_SELECT:
8896 break;
8897 case OFPGT11_ALL:
8898 case OFPGT11_INDIRECT:
8899 case OFPGT11_FF:
8900 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8901 "only allowed for select groups");
8902 return OFPERR_OFPBPC_BAD_VALUE;
8903 default:
8904 OVS_NOT_REACHED();
8905 }
8906
8907 switch (group_cmd) {
8908 case OFPGC15_ADD:
8909 case OFPGC15_MODIFY:
8910 case OFPGC15_ADD_OR_MOD:
8911 break;
8912 case OFPGC15_DELETE:
8913 case OFPGC15_INSERT_BUCKET:
8914 case OFPGC15_REMOVE_BUCKET:
8915 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8916 "only allowed for add and delete group modifications");
8917 return OFPERR_OFPBPC_BAD_VALUE;
8918 default:
8919 OVS_NOT_REACHED();
8920 }
8921
8922 if (payload->size < sizeof *prop) {
8923 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
8924 "length %u is not valid", payload->size);
8925 return OFPERR_OFPBPC_BAD_LEN;
8926 }
8927
8928 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
8929
8930 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
8931 OFPPROP_LOG(&bad_ofmsg_rl, false,
8932 "ntr selection method is not null terminated");
8933 return OFPERR_OFPBPC_BAD_VALUE;
8934 }
8935
8936 if (strcmp("hash", prop->selection_method)) {
8937 OFPPROP_LOG(&bad_ofmsg_rl, false,
8938 "ntr selection method '%s' is not supported",
8939 prop->selection_method);
8940 return OFPERR_OFPBPC_BAD_VALUE;
8941 }
8942
8943 strcpy(gp->selection_method, prop->selection_method);
8944 gp->selection_method_param = ntohll(prop->selection_method_param);
8945
8946 if (!method_len && gp->selection_method_param) {
8947 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8948 "non-zero but selection method is empty");
8949 return OFPERR_OFPBPC_BAD_VALUE;
8950 }
8951
8952 ofpbuf_pull(payload, sizeof *prop);
8953
8954 fields_len = ntohs(prop->length) - sizeof *prop;
8955 if (!method_len && fields_len) {
8956 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8957 "zero but fields are provided");
8958 return OFPERR_OFPBPC_BAD_VALUE;
8959 }
8960
8961 error = oxm_pull_field_array(payload->data, fields_len,
8962 &gp->fields);
8963 if (error) {
8964 OFPPROP_LOG(&bad_ofmsg_rl, false,
8965 "ntr selection method fields are invalid");
8966 return error;
8967 }
8968
8969 return 0;
8970 }
8971
8972 static enum ofperr
8973 parse_ofp15_group_properties(struct ofpbuf *msg,
8974 enum ofp11_group_type group_type,
8975 enum ofp15_group_mod_command group_cmd,
8976 struct ofputil_group_props *gp,
8977 size_t properties_len)
8978 {
8979 struct ofpbuf properties = ofpbuf_const_initializer(
8980 ofpbuf_pull(msg, properties_len), properties_len);
8981 while (properties.size > 0) {
8982 struct ofpbuf payload;
8983 enum ofperr error;
8984 uint64_t type;
8985
8986 error = ofpprop_pull(&properties, &payload, &type);
8987 if (error) {
8988 return error;
8989 }
8990
8991 switch (type) {
8992 case OFPPROP_EXP(NTR_VENDOR_ID, NTRT_SELECTION_METHOD):
8993 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID, NTRT_SELECTION_METHOD):
8994 error = parse_group_prop_ntr_selection_method(&payload, group_type,
8995 group_cmd, gp);
8996 break;
8997
8998 default:
8999 error = OFPPROP_UNKNOWN(false, "group", type);
9000 break;
9001 }
9002
9003 if (error) {
9004 return error;
9005 }
9006 }
9007
9008 return 0;
9009 }
9010
9011 static int
9012 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
9013 struct ofpbuf *msg,
9014 enum ofp_version version)
9015 {
9016 struct ofp11_group_desc_stats *ogds;
9017 size_t length;
9018
9019 if (!msg->header) {
9020 ofpraw_pull_assert(msg);
9021 }
9022
9023 if (!msg->size) {
9024 return EOF;
9025 }
9026
9027 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9028 if (!ogds) {
9029 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
9030 "leftover bytes at end", msg->size);
9031 return OFPERR_OFPBRC_BAD_LEN;
9032 }
9033 gd->type = ogds->type;
9034 gd->group_id = ntohl(ogds->group_id);
9035
9036 length = ntohs(ogds->length);
9037 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
9038 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9039 "length %"PRIuSIZE, length);
9040 return OFPERR_OFPBRC_BAD_LEN;
9041 }
9042
9043 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
9044 &gd->buckets);
9045 }
9046
9047 static int
9048 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
9049 struct ofpbuf *msg,
9050 enum ofp_version version)
9051 {
9052 struct ofp15_group_desc_stats *ogds;
9053 uint16_t length, bucket_list_len;
9054 int error;
9055
9056 if (!msg->header) {
9057 ofpraw_pull_assert(msg);
9058 }
9059
9060 if (!msg->size) {
9061 return EOF;
9062 }
9063
9064 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9065 if (!ogds) {
9066 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
9067 "leftover bytes at end", msg->size);
9068 return OFPERR_OFPBRC_BAD_LEN;
9069 }
9070 gd->type = ogds->type;
9071 gd->group_id = ntohl(ogds->group_id);
9072
9073 length = ntohs(ogds->length);
9074 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
9075 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9076 "length %u", length);
9077 return OFPERR_OFPBRC_BAD_LEN;
9078 }
9079
9080 bucket_list_len = ntohs(ogds->bucket_list_len);
9081 if (length < bucket_list_len + sizeof *ogds) {
9082 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9083 "bucket list length %u", bucket_list_len);
9084 return OFPERR_OFPBRC_BAD_LEN;
9085 }
9086 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
9087 &gd->buckets);
9088 if (error) {
9089 return error;
9090 }
9091
9092 /* By definition group desc messages don't have a group mod command.
9093 * However, parse_group_prop_ntr_selection_method() checks to make sure
9094 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9095 * against group mod messages with other commands supplying
9096 * a NTR selection method group experimenter property.
9097 * Such properties are valid for group desc replies so
9098 * claim that the group mod command is OFPGC15_ADD to
9099 * satisfy the check in parse_group_prop_ntr_selection_method() */
9100 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
9101 length - sizeof *ogds - bucket_list_len);
9102 }
9103
9104 /* Converts a group description reply in 'msg' into an abstract
9105 * ofputil_group_desc in 'gd'.
9106 *
9107 * Multiple group description replies can be packed into a single OpenFlow
9108 * message. Calling this function multiple times for a single 'msg' iterates
9109 * through the replies. The caller must initially leave 'msg''s layer pointers
9110 * null and not modify them between calls.
9111 *
9112 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9113 * otherwise a positive errno value. */
9114 int
9115 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
9116 struct ofpbuf *msg, enum ofp_version version)
9117 {
9118 ofputil_init_group_properties(&gd->props);
9119
9120 switch (version)
9121 {
9122 case OFP11_VERSION:
9123 case OFP12_VERSION:
9124 case OFP13_VERSION:
9125 case OFP14_VERSION:
9126 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
9127
9128 case OFP15_VERSION:
9129 case OFP16_VERSION:
9130 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
9131
9132 case OFP10_VERSION:
9133 default:
9134 OVS_NOT_REACHED();
9135 }
9136 }
9137
9138 void
9139 ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
9140 {
9141 ofputil_bucket_list_destroy(&gm->buckets);
9142 ofputil_group_properties_destroy(&gm->props);
9143 }
9144
9145 static struct ofpbuf *
9146 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
9147 const struct ofputil_group_mod *gm)
9148 {
9149 struct ofpbuf *b;
9150 struct ofp11_group_mod *ogm;
9151 size_t start_ogm;
9152 struct ofputil_bucket *bucket;
9153
9154 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
9155 start_ogm = b->size;
9156 ofpbuf_put_zeros(b, sizeof *ogm);
9157
9158 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9159 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
9160 }
9161 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9162 ogm->command = htons(gm->command);
9163 ogm->type = gm->type;
9164 ogm->group_id = htonl(gm->group_id);
9165
9166 return b;
9167 }
9168
9169 static struct ofpbuf *
9170 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
9171 const struct ofputil_group_mod *gm)
9172 {
9173 struct ofpbuf *b;
9174 struct ofp15_group_mod *ogm;
9175 size_t start_ogm;
9176 struct ofputil_bucket *bucket;
9177 struct id_pool *bucket_ids = NULL;
9178
9179 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
9180 start_ogm = b->size;
9181 ofpbuf_put_zeros(b, sizeof *ogm);
9182
9183 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9184 uint32_t bucket_id;
9185
9186 /* Generate a bucket id if none was supplied */
9187 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
9188 if (!bucket_ids) {
9189 const struct ofputil_bucket *bkt;
9190
9191 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
9192
9193 /* Mark all bucket_ids that are present in gm
9194 * as used in the pool. */
9195 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
9196 if (bkt == bucket) {
9197 break;
9198 }
9199 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
9200 id_pool_add(bucket_ids, bkt->bucket_id);
9201 }
9202 }
9203 }
9204
9205 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
9206 OVS_NOT_REACHED();
9207 }
9208 } else {
9209 bucket_id = bucket->bucket_id;
9210 }
9211
9212 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
9213 }
9214 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9215 ogm->command = htons(gm->command);
9216 ogm->type = gm->type;
9217 ogm->group_id = htonl(gm->group_id);
9218 ogm->command_bucket_id = htonl(gm->command_bucket_id);
9219 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
9220
9221 /* Add group properties */
9222 if (gm->props.selection_method[0]) {
9223 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
9224 }
9225
9226 id_pool_destroy(bucket_ids);
9227 return b;
9228 }
9229
9230 static void
9231 bad_group_cmd(enum ofp15_group_mod_command cmd)
9232 {
9233 const char *opt_version;
9234 const char *version;
9235 const char *cmd_str;
9236
9237 switch (cmd) {
9238 case OFPGC15_ADD:
9239 case OFPGC15_MODIFY:
9240 case OFPGC15_ADD_OR_MOD:
9241 case OFPGC15_DELETE:
9242 version = "1.1";
9243 opt_version = "11";
9244 break;
9245
9246 case OFPGC15_INSERT_BUCKET:
9247 case OFPGC15_REMOVE_BUCKET:
9248 version = "1.5";
9249 opt_version = "15";
9250 break;
9251
9252 default:
9253 OVS_NOT_REACHED();
9254 }
9255
9256 switch (cmd) {
9257 case OFPGC15_ADD:
9258 cmd_str = "add-group";
9259 break;
9260
9261 case OFPGC15_MODIFY:
9262 case OFPGC15_ADD_OR_MOD:
9263 cmd_str = "mod-group";
9264 break;
9265
9266 case OFPGC15_DELETE:
9267 cmd_str = "del-group";
9268 break;
9269
9270 case OFPGC15_INSERT_BUCKET:
9271 cmd_str = "insert-bucket";
9272 break;
9273
9274 case OFPGC15_REMOVE_BUCKET:
9275 cmd_str = "remove-bucket";
9276 break;
9277
9278 default:
9279 OVS_NOT_REACHED();
9280 }
9281
9282 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9283 cmd_str, version, opt_version);
9284
9285 }
9286
9287 /* Converts abstract group mod 'gm' into a message for OpenFlow version
9288 * 'ofp_version' and returns the message. */
9289 struct ofpbuf *
9290 ofputil_encode_group_mod(enum ofp_version ofp_version,
9291 const struct ofputil_group_mod *gm)
9292 {
9293
9294 switch (ofp_version) {
9295 case OFP10_VERSION:
9296 bad_group_cmd(gm->command);
9297
9298 case OFP11_VERSION:
9299 case OFP12_VERSION:
9300 case OFP13_VERSION:
9301 case OFP14_VERSION:
9302 if (gm->command > OFPGC11_DELETE && gm->command != OFPGC11_ADD_OR_MOD) {
9303 bad_group_cmd(gm->command);
9304 }
9305 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
9306
9307 case OFP15_VERSION:
9308 case OFP16_VERSION:
9309 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
9310
9311 default:
9312 OVS_NOT_REACHED();
9313 }
9314 }
9315
9316 static enum ofperr
9317 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9318 struct ofputil_group_mod *gm)
9319 {
9320 const struct ofp11_group_mod *ogm;
9321 enum ofperr error;
9322
9323 ogm = ofpbuf_pull(msg, sizeof *ogm);
9324 gm->command = ntohs(ogm->command);
9325 gm->type = ogm->type;
9326 gm->group_id = ntohl(ogm->group_id);
9327 gm->command_bucket_id = OFPG15_BUCKET_ALL;
9328
9329 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
9330 &gm->buckets);
9331
9332 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9333 if (!error
9334 && ofp_version >= OFP13_VERSION
9335 && gm->command == OFPGC11_DELETE
9336 && !ovs_list_is_empty(&gm->buckets)) {
9337 error = OFPERR_OFPGMFC_INVALID_GROUP;
9338 }
9339
9340 return error;
9341 }
9342
9343 static enum ofperr
9344 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9345 struct ofputil_group_mod *gm)
9346 {
9347 const struct ofp15_group_mod *ogm;
9348 uint16_t bucket_list_len;
9349 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
9350
9351 ogm = ofpbuf_pull(msg, sizeof *ogm);
9352 gm->command = ntohs(ogm->command);
9353 gm->type = ogm->type;
9354 gm->group_id = ntohl(ogm->group_id);
9355
9356 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
9357 switch (gm->command) {
9358 case OFPGC15_REMOVE_BUCKET:
9359 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9360 error = 0;
9361 }
9362 /* Fall through */
9363 case OFPGC15_INSERT_BUCKET:
9364 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
9365 gm->command_bucket_id == OFPG15_BUCKET_FIRST
9366 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
9367 error = 0;
9368 }
9369 break;
9370
9371 case OFPGC11_ADD:
9372 case OFPGC11_MODIFY:
9373 case OFPGC11_ADD_OR_MOD:
9374 case OFPGC11_DELETE:
9375 default:
9376 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9377 error = 0;
9378 }
9379 break;
9380 }
9381 if (error) {
9382 VLOG_WARN_RL(&bad_ofmsg_rl,
9383 "group command bucket id (%u) is out of range",
9384 gm->command_bucket_id);
9385 return OFPERR_OFPGMFC_BAD_BUCKET;
9386 }
9387
9388 bucket_list_len = ntohs(ogm->bucket_array_len);
9389 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
9390 gm->type, &gm->buckets);
9391 if (error) {
9392 return error;
9393 }
9394
9395 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
9396 msg->size);
9397 }
9398
9399 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9400 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9401 enum ofperr
9402 ofputil_decode_group_mod(const struct ofp_header *oh,
9403 struct ofputil_group_mod *gm)
9404 {
9405 ofputil_init_group_properties(&gm->props);
9406
9407 enum ofp_version ofp_version = oh->version;
9408 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
9409 ofpraw_pull_assert(&msg);
9410
9411 enum ofperr err;
9412 switch (ofp_version)
9413 {
9414 case OFP11_VERSION:
9415 case OFP12_VERSION:
9416 case OFP13_VERSION:
9417 case OFP14_VERSION:
9418 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
9419 break;
9420
9421 case OFP15_VERSION:
9422 case OFP16_VERSION:
9423 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
9424 break;
9425
9426 case OFP10_VERSION:
9427 default:
9428 OVS_NOT_REACHED();
9429 }
9430 if (err) {
9431 return err;
9432 }
9433
9434 switch (gm->type) {
9435 case OFPGT11_INDIRECT:
9436 if (gm->command != OFPGC11_DELETE
9437 && !ovs_list_is_singleton(&gm->buckets) ) {
9438 return OFPERR_OFPGMFC_INVALID_GROUP;
9439 }
9440 break;
9441 case OFPGT11_ALL:
9442 case OFPGT11_SELECT:
9443 case OFPGT11_FF:
9444 break;
9445 default:
9446 return OFPERR_OFPGMFC_BAD_TYPE;
9447 }
9448
9449 switch (gm->command) {
9450 case OFPGC11_ADD:
9451 case OFPGC11_MODIFY:
9452 case OFPGC11_ADD_OR_MOD:
9453 case OFPGC11_DELETE:
9454 case OFPGC15_INSERT_BUCKET:
9455 break;
9456 case OFPGC15_REMOVE_BUCKET:
9457 if (!ovs_list_is_empty(&gm->buckets)) {
9458 return OFPERR_OFPGMFC_BAD_BUCKET;
9459 }
9460 break;
9461 default:
9462 return OFPERR_OFPGMFC_BAD_COMMAND;
9463 }
9464
9465 struct ofputil_bucket *bucket;
9466 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9467 if (bucket->weight && gm->type != OFPGT11_SELECT) {
9468 return OFPERR_OFPGMFC_INVALID_GROUP;
9469 }
9470
9471 switch (gm->type) {
9472 case OFPGT11_ALL:
9473 case OFPGT11_INDIRECT:
9474 if (ofputil_bucket_has_liveness(bucket)) {
9475 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
9476 }
9477 break;
9478 case OFPGT11_SELECT:
9479 break;
9480 case OFPGT11_FF:
9481 if (!ofputil_bucket_has_liveness(bucket)) {
9482 return OFPERR_OFPGMFC_INVALID_GROUP;
9483 }
9484 break;
9485 default:
9486 /* Returning BAD TYPE to be consistent
9487 * though gm->type has been checked already. */
9488 return OFPERR_OFPGMFC_BAD_TYPE;
9489 }
9490 }
9491
9492 return 0;
9493 }
9494
9495 /* Destroys 'bms'. */
9496 void
9497 ofputil_encode_bundle_msgs(struct ofputil_bundle_msg *bms, size_t n_bms,
9498 struct ovs_list *requests,
9499 enum ofputil_protocol protocol)
9500 {
9501 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
9502
9503 for (size_t i = 0; i < n_bms; i++) {
9504 struct ofpbuf *request = NULL;
9505
9506 switch ((int)bms[i].type) {
9507 case OFPTYPE_FLOW_MOD:
9508 request = ofputil_encode_flow_mod(&bms[i].fm, protocol);
9509 free(CONST_CAST(struct ofpact *, bms[i].fm.ofpacts));
9510 break;
9511 case OFPTYPE_GROUP_MOD:
9512 request = ofputil_encode_group_mod(version, &bms[i].gm);
9513 ofputil_uninit_group_mod(&bms[i].gm);
9514 break;
9515 default:
9516 break;
9517 }
9518 if (request) {
9519 ovs_list_push_back(requests, &request->list_node);
9520 }
9521 }
9522 free(bms);
9523 }
9524
9525 /* Parse a queue status request message into 'oqsr'.
9526 * Returns 0 if successful, otherwise an OFPERR_* number. */
9527 enum ofperr
9528 ofputil_decode_queue_stats_request(const struct ofp_header *request,
9529 struct ofputil_queue_stats_request *oqsr)
9530 {
9531 switch ((enum ofp_version)request->version) {
9532 case OFP16_VERSION:
9533 case OFP15_VERSION:
9534 case OFP14_VERSION:
9535 case OFP13_VERSION:
9536 case OFP12_VERSION:
9537 case OFP11_VERSION: {
9538 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
9539 oqsr->queue_id = ntohl(qsr11->queue_id);
9540 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
9541 }
9542
9543 case OFP10_VERSION: {
9544 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
9545 oqsr->queue_id = ntohl(qsr10->queue_id);
9546 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
9547 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9548 if (oqsr->port_no == OFPP_ALL) {
9549 oqsr->port_no = OFPP_ANY;
9550 }
9551 return 0;
9552 }
9553
9554 default:
9555 OVS_NOT_REACHED();
9556 }
9557 }
9558
9559 /* Encode a queue stats request for 'oqsr', the encoded message
9560 * will be for OpenFlow version 'ofp_version'. Returns message
9561 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
9562 struct ofpbuf *
9563 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
9564 const struct ofputil_queue_stats_request *oqsr)
9565 {
9566 struct ofpbuf *request;
9567
9568 switch (ofp_version) {
9569 case OFP11_VERSION:
9570 case OFP12_VERSION:
9571 case OFP13_VERSION:
9572 case OFP14_VERSION:
9573 case OFP15_VERSION:
9574 case OFP16_VERSION: {
9575 struct ofp11_queue_stats_request *req;
9576 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
9577 req = ofpbuf_put_zeros(request, sizeof *req);
9578 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
9579 req->queue_id = htonl(oqsr->queue_id);
9580 break;
9581 }
9582 case OFP10_VERSION: {
9583 struct ofp10_queue_stats_request *req;
9584 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
9585 req = ofpbuf_put_zeros(request, sizeof *req);
9586 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
9587 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
9588 ? OFPP_ALL : oqsr->port_no));
9589 req->queue_id = htonl(oqsr->queue_id);
9590 break;
9591 }
9592 default:
9593 OVS_NOT_REACHED();
9594 }
9595
9596 return request;
9597 }
9598
9599 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9600 * message 'oh'. */
9601 size_t
9602 ofputil_count_queue_stats(const struct ofp_header *oh)
9603 {
9604 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9605 ofpraw_pull_assert(&b);
9606
9607 for (size_t n = 0; ; n++) {
9608 struct ofputil_queue_stats qs;
9609 if (ofputil_decode_queue_stats(&qs, &b)) {
9610 return n;
9611 }
9612 }
9613 }
9614
9615 static enum ofperr
9616 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
9617 const struct ofp10_queue_stats *qs10)
9618 {
9619 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
9620 oqs->queue_id = ntohl(qs10->queue_id);
9621 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
9622 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
9623 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
9624 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9625
9626 return 0;
9627 }
9628
9629 static enum ofperr
9630 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
9631 const struct ofp11_queue_stats *qs11)
9632 {
9633 enum ofperr error;
9634
9635 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
9636 if (error) {
9637 return error;
9638 }
9639
9640 oqs->queue_id = ntohl(qs11->queue_id);
9641 oqs->tx_bytes = ntohll(qs11->tx_bytes);
9642 oqs->tx_packets = ntohll(qs11->tx_packets);
9643 oqs->tx_errors = ntohll(qs11->tx_errors);
9644 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9645
9646 return 0;
9647 }
9648
9649 static enum ofperr
9650 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
9651 const struct ofp13_queue_stats *qs13)
9652 {
9653 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
9654 if (!error) {
9655 oqs->duration_sec = ntohl(qs13->duration_sec);
9656 oqs->duration_nsec = ntohl(qs13->duration_nsec);
9657 }
9658
9659 return error;
9660 }
9661
9662 static enum ofperr
9663 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
9664 struct ofpbuf *msg)
9665 {
9666 const struct ofp14_queue_stats *qs14;
9667 size_t len;
9668
9669 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
9670 if (!qs14) {
9671 return OFPERR_OFPBRC_BAD_LEN;
9672 }
9673
9674 len = ntohs(qs14->length);
9675 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
9676 return OFPERR_OFPBRC_BAD_LEN;
9677 }
9678 ofpbuf_pull(msg, len - sizeof *qs14);
9679
9680 /* No properties yet defined, so ignore them for now. */
9681
9682 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9683 }
9684
9685 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9686 * ofputil_queue_stats in 'qs'.
9687 *
9688 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9689 * message. Calling this function multiple times for a single 'msg' iterates
9690 * through the replies. The caller must initially leave 'msg''s layer pointers
9691 * null and not modify them between calls.
9692 *
9693 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9694 * otherwise a positive errno value. */
9695 int
9696 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9697 {
9698 enum ofperr error;
9699 enum ofpraw raw;
9700
9701 error = (msg->header ? ofpraw_decode(&raw, msg->header)
9702 : ofpraw_pull(&raw, msg));
9703 if (error) {
9704 return error;
9705 }
9706
9707 if (!msg->size) {
9708 return EOF;
9709 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9710 return ofputil_pull_ofp14_queue_stats(qs, msg);
9711 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9712 const struct ofp13_queue_stats *qs13;
9713
9714 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9715 if (!qs13) {
9716 goto bad_len;
9717 }
9718 return ofputil_queue_stats_from_ofp13(qs, qs13);
9719 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9720 const struct ofp11_queue_stats *qs11;
9721
9722 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9723 if (!qs11) {
9724 goto bad_len;
9725 }
9726 return ofputil_queue_stats_from_ofp11(qs, qs11);
9727 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9728 const struct ofp10_queue_stats *qs10;
9729
9730 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9731 if (!qs10) {
9732 goto bad_len;
9733 }
9734 return ofputil_queue_stats_from_ofp10(qs, qs10);
9735 } else {
9736 OVS_NOT_REACHED();
9737 }
9738
9739 bad_len:
9740 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
9741 "bytes at end", msg->size);
9742 return OFPERR_OFPBRC_BAD_LEN;
9743 }
9744
9745 static void
9746 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
9747 struct ofp10_queue_stats *qs10)
9748 {
9749 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
9750 memset(qs10->pad, 0, sizeof qs10->pad);
9751 qs10->queue_id = htonl(oqs->queue_id);
9752 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
9753 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
9754 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
9755 }
9756
9757 static void
9758 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
9759 struct ofp11_queue_stats *qs11)
9760 {
9761 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
9762 qs11->queue_id = htonl(oqs->queue_id);
9763 qs11->tx_bytes = htonll(oqs->tx_bytes);
9764 qs11->tx_packets = htonll(oqs->tx_packets);
9765 qs11->tx_errors = htonll(oqs->tx_errors);
9766 }
9767
9768 static void
9769 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
9770 struct ofp13_queue_stats *qs13)
9771 {
9772 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
9773 if (oqs->duration_sec != UINT32_MAX) {
9774 qs13->duration_sec = htonl(oqs->duration_sec);
9775 qs13->duration_nsec = htonl(oqs->duration_nsec);
9776 } else {
9777 qs13->duration_sec = OVS_BE32_MAX;
9778 qs13->duration_nsec = OVS_BE32_MAX;
9779 }
9780 }
9781
9782 static void
9783 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
9784 struct ofp14_queue_stats *qs14)
9785 {
9786 qs14->length = htons(sizeof *qs14);
9787 memset(qs14->pad, 0, sizeof qs14->pad);
9788 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
9789 }
9790
9791
9792 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9793 void
9794 ofputil_append_queue_stat(struct ovs_list *replies,
9795 const struct ofputil_queue_stats *oqs)
9796 {
9797 switch (ofpmp_version(replies)) {
9798 case OFP13_VERSION: {
9799 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9800 ofputil_queue_stats_to_ofp13(oqs, reply);
9801 break;
9802 }
9803
9804 case OFP12_VERSION:
9805 case OFP11_VERSION: {
9806 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9807 ofputil_queue_stats_to_ofp11(oqs, reply);
9808 break;
9809 }
9810
9811 case OFP10_VERSION: {
9812 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9813 ofputil_queue_stats_to_ofp10(oqs, reply);
9814 break;
9815 }
9816
9817 case OFP14_VERSION:
9818 case OFP15_VERSION:
9819 case OFP16_VERSION: {
9820 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9821 ofputil_queue_stats_to_ofp14(oqs, reply);
9822 break;
9823 }
9824
9825 default:
9826 OVS_NOT_REACHED();
9827 }
9828 }
9829
9830 enum ofperr
9831 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
9832 struct ofputil_bundle_ctrl_msg *msg)
9833 {
9834 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9835 enum ofpraw raw = ofpraw_pull_assert(&b);
9836 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL
9837 || raw == OFPRAW_ONFT13_BUNDLE_CONTROL);
9838
9839 const struct ofp14_bundle_ctrl_msg *m = b.msg;
9840 msg->bundle_id = ntohl(m->bundle_id);
9841 msg->type = ntohs(m->type);
9842 msg->flags = ntohs(m->flags);
9843
9844 return 0;
9845 }
9846
9847 struct ofpbuf *
9848 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
9849 struct ofputil_bundle_ctrl_msg *bc)
9850 {
9851 struct ofpbuf *request;
9852 struct ofp14_bundle_ctrl_msg *m;
9853
9854 switch (ofp_version) {
9855 case OFP10_VERSION:
9856 case OFP11_VERSION:
9857 case OFP12_VERSION:
9858 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
9859 "(\'-O OpenFlow14\')");
9860 case OFP13_VERSION:
9861 case OFP14_VERSION:
9862 case OFP15_VERSION:
9863 case OFP16_VERSION:
9864 request = ofpraw_alloc(ofp_version == OFP13_VERSION
9865 ? OFPRAW_ONFT13_BUNDLE_CONTROL
9866 : OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
9867 m = ofpbuf_put_zeros(request, sizeof *m);
9868
9869 m->bundle_id = htonl(bc->bundle_id);
9870 m->type = htons(bc->type);
9871 m->flags = htons(bc->flags);
9872 break;
9873 default:
9874 OVS_NOT_REACHED();
9875 }
9876
9877 return request;
9878 }
9879
9880 struct ofpbuf *
9881 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
9882 struct ofputil_bundle_ctrl_msg *msg)
9883 {
9884 struct ofpbuf *buf;
9885 struct ofp14_bundle_ctrl_msg *m;
9886
9887 buf = ofpraw_alloc_reply(oh->version == OFP13_VERSION
9888 ? OFPRAW_ONFT13_BUNDLE_CONTROL
9889 : OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
9890 m = ofpbuf_put_zeros(buf, sizeof *m);
9891
9892 m->bundle_id = htonl(msg->bundle_id);
9893 m->type = htons(msg->type);
9894 m->flags = htons(msg->flags);
9895
9896 return buf;
9897 }
9898
9899 /* Return true for bundlable state change requests, false for other messages.
9900 */
9901 static bool
9902 ofputil_is_bundlable(enum ofptype type)
9903 {
9904 switch (type) {
9905 /* Minimum required by OpenFlow 1.4. */
9906 case OFPTYPE_PORT_MOD:
9907 case OFPTYPE_FLOW_MOD:
9908 /* Other supported types. */
9909 case OFPTYPE_GROUP_MOD:
9910 return true;
9911
9912 /* Nice to have later. */
9913 case OFPTYPE_FLOW_MOD_TABLE_ID:
9914 case OFPTYPE_TABLE_MOD:
9915 case OFPTYPE_METER_MOD:
9916 case OFPTYPE_PACKET_OUT:
9917 case OFPTYPE_NXT_TLV_TABLE_MOD:
9918
9919 /* Not to be bundlable. */
9920 case OFPTYPE_ECHO_REQUEST:
9921 case OFPTYPE_FEATURES_REQUEST:
9922 case OFPTYPE_GET_CONFIG_REQUEST:
9923 case OFPTYPE_SET_CONFIG:
9924 case OFPTYPE_BARRIER_REQUEST:
9925 case OFPTYPE_ROLE_REQUEST:
9926 case OFPTYPE_ECHO_REPLY:
9927 case OFPTYPE_SET_FLOW_FORMAT:
9928 case OFPTYPE_SET_PACKET_IN_FORMAT:
9929 case OFPTYPE_SET_CONTROLLER_ID:
9930 case OFPTYPE_FLOW_AGE:
9931 case OFPTYPE_FLOW_MONITOR_CANCEL:
9932 case OFPTYPE_SET_ASYNC_CONFIG:
9933 case OFPTYPE_GET_ASYNC_REQUEST:
9934 case OFPTYPE_DESC_STATS_REQUEST:
9935 case OFPTYPE_FLOW_STATS_REQUEST:
9936 case OFPTYPE_AGGREGATE_STATS_REQUEST:
9937 case OFPTYPE_TABLE_STATS_REQUEST:
9938 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9939 case OFPTYPE_TABLE_DESC_REQUEST:
9940 case OFPTYPE_PORT_STATS_REQUEST:
9941 case OFPTYPE_QUEUE_STATS_REQUEST:
9942 case OFPTYPE_PORT_DESC_STATS_REQUEST:
9943 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9944 case OFPTYPE_METER_STATS_REQUEST:
9945 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9946 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9947 case OFPTYPE_GROUP_STATS_REQUEST:
9948 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9949 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9950 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9951 case OFPTYPE_BUNDLE_CONTROL:
9952 case OFPTYPE_BUNDLE_ADD_MESSAGE:
9953 case OFPTYPE_HELLO:
9954 case OFPTYPE_ERROR:
9955 case OFPTYPE_FEATURES_REPLY:
9956 case OFPTYPE_GET_CONFIG_REPLY:
9957 case OFPTYPE_PACKET_IN:
9958 case OFPTYPE_FLOW_REMOVED:
9959 case OFPTYPE_PORT_STATUS:
9960 case OFPTYPE_BARRIER_REPLY:
9961 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9962 case OFPTYPE_DESC_STATS_REPLY:
9963 case OFPTYPE_FLOW_STATS_REPLY:
9964 case OFPTYPE_QUEUE_STATS_REPLY:
9965 case OFPTYPE_PORT_STATS_REPLY:
9966 case OFPTYPE_TABLE_STATS_REPLY:
9967 case OFPTYPE_AGGREGATE_STATS_REPLY:
9968 case OFPTYPE_PORT_DESC_STATS_REPLY:
9969 case OFPTYPE_ROLE_REPLY:
9970 case OFPTYPE_FLOW_MONITOR_PAUSED:
9971 case OFPTYPE_FLOW_MONITOR_RESUMED:
9972 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9973 case OFPTYPE_GET_ASYNC_REPLY:
9974 case OFPTYPE_GROUP_STATS_REPLY:
9975 case OFPTYPE_GROUP_DESC_STATS_REPLY:
9976 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9977 case OFPTYPE_METER_STATS_REPLY:
9978 case OFPTYPE_METER_CONFIG_STATS_REPLY:
9979 case OFPTYPE_METER_FEATURES_STATS_REPLY:
9980 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9981 case OFPTYPE_TABLE_DESC_REPLY:
9982 case OFPTYPE_ROLE_STATUS:
9983 case OFPTYPE_REQUESTFORWARD:
9984 case OFPTYPE_TABLE_STATUS:
9985 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
9986 case OFPTYPE_NXT_TLV_TABLE_REPLY:
9987 case OFPTYPE_NXT_RESUME:
9988 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
9989 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
9990 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
9991 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
9992 break;
9993 }
9994
9995 return false;
9996 }
9997
9998 enum ofperr
9999 ofputil_decode_bundle_add(const struct ofp_header *oh,
10000 struct ofputil_bundle_add_msg *msg,
10001 enum ofptype *typep)
10002 {
10003 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10004 enum ofpraw raw = ofpraw_pull_assert(&b);
10005 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
10006 || raw == OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE);
10007
10008 const struct ofp14_bundle_ctrl_msg *m = ofpbuf_pull(&b, sizeof *m);
10009 msg->bundle_id = ntohl(m->bundle_id);
10010 msg->flags = ntohs(m->flags);
10011
10012 msg->msg = b.data;
10013 if (msg->msg->version != oh->version) {
10014 return OFPERR_OFPBFC_BAD_VERSION;
10015 }
10016 size_t inner_len = ntohs(msg->msg->length);
10017 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
10018 return OFPERR_OFPBFC_MSG_BAD_LEN;
10019 }
10020 if (msg->msg->xid != oh->xid) {
10021 return OFPERR_OFPBFC_MSG_BAD_XID;
10022 }
10023
10024 /* Reject unbundlable messages. */
10025 enum ofptype type;
10026 enum ofperr error = ofptype_decode(&type, msg->msg);
10027 if (error) {
10028 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10029 "message is unparsable (%s)", ofperr_get_name(error));
10030 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
10031 }
10032
10033 if (!ofputil_is_bundlable(type)) {
10034 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
10035 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type));
10036 return OFPERR_OFPBFC_MSG_UNSUP;
10037 }
10038 if (typep) {
10039 *typep = type;
10040 }
10041
10042 return 0;
10043 }
10044
10045 struct ofpbuf *
10046 ofputil_encode_bundle_add(enum ofp_version ofp_version,
10047 struct ofputil_bundle_add_msg *msg)
10048 {
10049 struct ofpbuf *request;
10050 struct ofp14_bundle_ctrl_msg *m;
10051
10052 /* Must use the same xid as the embedded message. */
10053 request = ofpraw_alloc_xid(ofp_version == OFP13_VERSION
10054 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10055 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
10056 msg->msg->xid, ntohs(msg->msg->length));
10057 m = ofpbuf_put_zeros(request, sizeof *m);
10058
10059 m->bundle_id = htonl(msg->bundle_id);
10060 m->flags = htons(msg->flags);
10061 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
10062
10063 ofpmsg_update_length(request);
10064 return request;
10065 }
10066
10067 static void
10068 encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
10069 {
10070 struct ofputil_tlv_map *map;
10071
10072 LIST_FOR_EACH (map, list_node, mappings) {
10073 struct nx_tlv_map *nx_map;
10074
10075 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
10076 nx_map->option_class = htons(map->option_class);
10077 nx_map->option_type = map->option_type;
10078 nx_map->option_len = map->option_len;
10079 nx_map->index = htons(map->index);
10080 }
10081 }
10082
10083 struct ofpbuf *
10084 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
10085 struct ofputil_tlv_table_mod *ttm)
10086 {
10087 struct ofpbuf *b;
10088 struct nx_tlv_table_mod *nx_ttm;
10089
10090 b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
10091 nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
10092 nx_ttm->command = htons(ttm->command);
10093 encode_tlv_table_mappings(b, &ttm->mappings);
10094
10095 return b;
10096 }
10097
10098 static enum ofperr
10099 decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
10100 struct ovs_list *mappings)
10101 {
10102 ovs_list_init(mappings);
10103
10104 while (msg->size) {
10105 struct nx_tlv_map *nx_map;
10106 struct ofputil_tlv_map *map;
10107
10108 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
10109 map = xmalloc(sizeof *map);
10110 ovs_list_push_back(mappings, &map->list_node);
10111
10112 map->option_class = ntohs(nx_map->option_class);
10113 map->option_type = nx_map->option_type;
10114
10115 map->option_len = nx_map->option_len;
10116 if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
10117 VLOG_WARN_RL(&bad_ofmsg_rl,
10118 "tlv table option length (%u) is not a valid option size",
10119 map->option_len);
10120 ofputil_uninit_tlv_table(mappings);
10121 return OFPERR_NXTTMFC_BAD_OPT_LEN;
10122 }
10123
10124 map->index = ntohs(nx_map->index);
10125 if (map->index >= max_fields) {
10126 VLOG_WARN_RL(&bad_ofmsg_rl,
10127 "tlv table field index (%u) is too large (max %u)",
10128 map->index, max_fields - 1);
10129 ofputil_uninit_tlv_table(mappings);
10130 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
10131 }
10132 }
10133
10134 return 0;
10135 }
10136
10137 enum ofperr
10138 ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
10139 struct ofputil_tlv_table_mod *ttm)
10140 {
10141 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
10142 ofpraw_pull_assert(&msg);
10143
10144 struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
10145 ttm->command = ntohs(nx_ttm->command);
10146 if (ttm->command > NXTTMC_CLEAR) {
10147 VLOG_WARN_RL(&bad_ofmsg_rl,
10148 "tlv table mod command (%u) is out of range",
10149 ttm->command);
10150 return OFPERR_NXTTMFC_BAD_COMMAND;
10151 }
10152
10153 return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
10154 &ttm->mappings);
10155 }
10156
10157 struct ofpbuf *
10158 ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
10159 struct ofputil_tlv_table_reply *ttr)
10160 {
10161 struct ofpbuf *b;
10162 struct nx_tlv_table_reply *nx_ttr;
10163
10164 b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
10165 nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
10166 nx_ttr->max_option_space = htonl(ttr->max_option_space);
10167 nx_ttr->max_fields = htons(ttr->max_fields);
10168
10169 encode_tlv_table_mappings(b, &ttr->mappings);
10170
10171 return b;
10172 }
10173
10174 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
10175 * if successful, otherwise an ofperr.
10176 *
10177 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10178 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
10179 * less than TUN_METADATA_NUM_OPTS. */
10180 enum ofperr
10181 ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
10182 struct ofputil_tlv_table_reply *ttr)
10183 {
10184 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
10185 ofpraw_pull_assert(&msg);
10186
10187 struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
10188 ttr->max_option_space = ntohl(nx_ttr->max_option_space);
10189 ttr->max_fields = ntohs(nx_ttr->max_fields);
10190
10191 return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
10192 }
10193
10194 void
10195 ofputil_uninit_tlv_table(struct ovs_list *mappings)
10196 {
10197 struct ofputil_tlv_map *map;
10198
10199 LIST_FOR_EACH_POP (map, list_node, mappings) {
10200 free(map);
10201 }
10202 }
10203
10204 const char *
10205 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type)
10206 {
10207 switch (type) {
10208 case OAM_PACKET_IN: return "PACKET_IN";
10209 case OAM_PORT_STATUS: return "PORT_STATUS";
10210 case OAM_FLOW_REMOVED: return "FLOW_REMOVED";
10211 case OAM_ROLE_STATUS: return "ROLE_STATUS";
10212 case OAM_TABLE_STATUS: return "TABLE_STATUS";
10213 case OAM_REQUESTFORWARD: return "REQUESTFORWARD";
10214
10215 case OAM_N_TYPES:
10216 default:
10217 OVS_NOT_REACHED();
10218 }
10219 }
10220
10221 struct ofp14_async_prop {
10222 uint64_t prop_type;
10223 enum ofputil_async_msg_type oam;
10224 bool master;
10225 uint32_t allowed10, allowed14;
10226 };
10227
10228 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10229 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10230 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10231
10232 static const struct ofp14_async_prop async_props[] = {
10233 AP_PAIR( 0, OAM_PACKET_IN, OFPR10_BITS, OFPR14_BITS),
10234 AP_PAIR( 2, OAM_PORT_STATUS, (1 << OFPPR_N_REASONS) - 1, 0),
10235 AP_PAIR( 4, OAM_FLOW_REMOVED, (1 << OVS_OFPRR_NONE) - 1, 0),
10236 AP_PAIR( 6, OAM_ROLE_STATUS, (1 << OFPCRR_N_REASONS) - 1, 0),
10237 AP_PAIR( 8, OAM_TABLE_STATUS, OFPTR_BITS, 0),
10238 AP_PAIR(10, OAM_REQUESTFORWARD, (1 << OFPRFR_N_REASONS) - 1, 0),
10239 };
10240
10241 #define FOR_EACH_ASYNC_PROP(VAR) \
10242 for (const struct ofp14_async_prop *VAR = async_props; \
10243 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10244
10245 static const struct ofp14_async_prop *
10246 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type)
10247 {
10248 FOR_EACH_ASYNC_PROP (ap) {
10249 if (prop_type == ap->prop_type) {
10250 return ap;
10251 }
10252 }
10253 return NULL;
10254 }
10255
10256 static const struct ofp14_async_prop *
10257 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam,
10258 bool master)
10259 {
10260 FOR_EACH_ASYNC_PROP (ap) {
10261 if (ap->oam == oam && ap->master == master) {
10262 return ap;
10263 }
10264 }
10265 return NULL;
10266 }
10267
10268 static uint32_t
10269 ofp14_async_prop_allowed(const struct ofp14_async_prop *prop,
10270 enum ofp_version version)
10271 {
10272 return version >= OFP14_VERSION ? prop->allowed14 : prop->allowed10;
10273 }
10274
10275 static ovs_be32
10276 encode_async_mask(const struct ofputil_async_cfg *src,
10277 const struct ofp14_async_prop *ap,
10278 enum ofp_version version)
10279 {
10280 uint32_t mask = ap->master ? src->master[ap->oam] : src->slave[ap->oam];
10281 return htonl(mask & ofp14_async_prop_allowed(ap, version));
10282 }
10283
10284 static enum ofperr
10285 decode_async_mask(ovs_be32 src,
10286 const struct ofp14_async_prop *ap, enum ofp_version version,
10287 bool loose, struct ofputil_async_cfg *dst)
10288 {
10289 uint32_t mask = ntohl(src);
10290 uint32_t allowed = ofp14_async_prop_allowed(ap, version);
10291 if (mask & ~allowed) {
10292 OFPPROP_LOG(&bad_ofmsg_rl, loose,
10293 "bad value %#x for %s (allowed mask %#x)",
10294 mask, ofputil_async_msg_type_to_string(ap->oam),
10295 allowed);
10296 mask &= allowed;
10297 if (!loose) {
10298 return OFPERR_OFPACFC_INVALID;
10299 }
10300 }
10301
10302 if (ap->oam == OAM_PACKET_IN) {
10303 if (mask & (1u << OFPR_NO_MATCH)) {
10304 mask |= 1u << OFPR_EXPLICIT_MISS;
10305 if (version < OFP13_VERSION) {
10306 mask |= 1u << OFPR_IMPLICIT_MISS;
10307 }
10308 }
10309 }
10310
10311 uint32_t *array = ap->master ? dst->master : dst->slave;
10312 array[ap->oam] = mask;
10313 return 0;
10314 }
10315
10316 static enum ofperr
10317 parse_async_tlv(const struct ofpbuf *property,
10318 const struct ofp14_async_prop *ap,
10319 struct ofputil_async_cfg *ac,
10320 enum ofp_version version, bool loose)
10321 {
10322 enum ofperr error;
10323 ovs_be32 mask;
10324
10325 error = ofpprop_parse_be32(property, &mask);
10326 if (error) {
10327 return error;
10328 }
10329
10330 if (ofpprop_is_experimenter(ap->prop_type)) {
10331 /* For experimenter properties, whether a property is for the master or
10332 * slave role is indicated by both 'type' and 'exp_type' in struct
10333 * ofp_prop_experimenter. Check that these are consistent. */
10334 const struct ofp_prop_experimenter *ope = property->data;
10335 bool should_be_master = ope->type == htons(0xffff);
10336 if (should_be_master != ap->master) {
10337 VLOG_WARN_RL(&bad_ofmsg_rl, "async property type %#"PRIx16" "
10338 "indicates %s role but exp_type %"PRIu32" indicates "
10339 "%s role",
10340 ntohs(ope->type),
10341 should_be_master ? "master" : "slave",
10342 ntohl(ope->exp_type),
10343 ap->master ? "master" : "slave");
10344 return OFPERR_OFPBPC_BAD_EXP_TYPE;
10345 }
10346 }
10347
10348 return decode_async_mask(mask, ap, version, loose, ac);
10349 }
10350
10351 static void
10352 decode_legacy_async_masks(const ovs_be32 masks[2],
10353 enum ofputil_async_msg_type oam,
10354 enum ofp_version version,
10355 struct ofputil_async_cfg *dst)
10356 {
10357 for (int i = 0; i < 2; i++) {
10358 bool master = i == 0;
10359 const struct ofp14_async_prop *ap
10360 = get_ofp14_async_config_prop_by_oam(oam, master);
10361 decode_async_mask(masks[i], ap, version, true, dst);
10362 }
10363 }
10364
10365 /* Decodes the OpenFlow "set async config" request and "get async config
10366 * reply" message in '*oh' into an abstract form in 'ac'.
10367 *
10368 * Some versions of the "set async config" request change only some of the
10369 * settings and leave the others alone. This function uses 'basis' as the
10370 * initial state for decoding these. Other versions of the request change all
10371 * the settings; this function ignores 'basis' when decoding these.
10372 *
10373 * If 'loose' is true, this function ignores properties and values that it does
10374 * not understand, as a controller would want to do when interpreting
10375 * capabilities provided by a switch. If 'loose' is false, this function
10376 * treats unknown properties and values as an error, as a switch would want to
10377 * do when interpreting a configuration request made by a controller.
10378 *
10379 * Returns 0 if successful, otherwise an OFPERR_* value.
10380 *
10381 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10382 * the valid range of mask.
10383 *
10384 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10385 * supported.*/
10386 enum ofperr
10387 ofputil_decode_set_async_config(const struct ofp_header *oh, bool loose,
10388 const struct ofputil_async_cfg *basis,
10389 struct ofputil_async_cfg *ac)
10390 {
10391 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10392 enum ofpraw raw = ofpraw_pull_assert(&b);
10393
10394 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
10395 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
10396 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
10397 const struct nx_async_config *msg = ofpmsg_body(oh);
10398
10399 *ac = OFPUTIL_ASYNC_CFG_INIT;
10400 decode_legacy_async_masks(msg->packet_in_mask, OAM_PACKET_IN,
10401 oh->version, ac);
10402 decode_legacy_async_masks(msg->port_status_mask, OAM_PORT_STATUS,
10403 oh->version, ac);
10404 decode_legacy_async_masks(msg->flow_removed_mask, OAM_FLOW_REMOVED,
10405 oh->version, ac);
10406 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
10407 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY ||
10408 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2) {
10409 *ac = *basis;
10410 while (b.size > 0) {
10411 struct ofpbuf property;
10412 enum ofperr error;
10413 uint64_t type;
10414
10415 error = ofpprop_pull__(&b, &property, 8, 0xfffe, &type);
10416 if (error) {
10417 return error;
10418 }
10419
10420 const struct ofp14_async_prop *ap
10421 = get_ofp14_async_config_prop_by_prop_type(type);
10422 error = (ap
10423 ? parse_async_tlv(&property, ap, ac, oh->version, loose)
10424 : OFPPROP_UNKNOWN(loose, "async config", type));
10425 if (error) {
10426 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10427 * knows why, it's OpenFlow. */
10428 if (error == OFPERR_OFPBPC_BAD_TYPE) {
10429 error = OFPERR_OFPACFC_UNSUPPORTED;
10430 }
10431 return error;
10432 }
10433 }
10434 } else {
10435 return OFPERR_OFPBRC_BAD_VERSION;
10436 }
10437 return 0;
10438 }
10439
10440 static void
10441 encode_legacy_async_masks(const struct ofputil_async_cfg *ac,
10442 enum ofputil_async_msg_type oam,
10443 enum ofp_version version,
10444 ovs_be32 masks[2])
10445 {
10446 for (int i = 0; i < 2; i++) {
10447 bool master = i == 0;
10448 const struct ofp14_async_prop *ap
10449 = get_ofp14_async_config_prop_by_oam(oam, master);
10450 masks[i] = encode_async_mask(ac, ap, version);
10451 }
10452 }
10453
10454 static void
10455 ofputil_put_async_config__(const struct ofputil_async_cfg *ac,
10456 struct ofpbuf *buf, bool tlv,
10457 enum ofp_version version, uint32_t oams)
10458 {
10459 if (!tlv) {
10460 struct nx_async_config *msg = ofpbuf_put_zeros(buf, sizeof *msg);
10461 encode_legacy_async_masks(ac, OAM_PACKET_IN, version,
10462 msg->packet_in_mask);
10463 encode_legacy_async_masks(ac, OAM_PORT_STATUS, version,
10464 msg->port_status_mask);
10465 encode_legacy_async_masks(ac, OAM_FLOW_REMOVED, version,
10466 msg->flow_removed_mask);
10467 } else {
10468 FOR_EACH_ASYNC_PROP (ap) {
10469 if (oams & (1u << ap->oam)) {
10470 size_t ofs = buf->size;
10471 ofpprop_put_be32(buf, ap->prop_type,
10472 encode_async_mask(ac, ap, version));
10473
10474 /* For experimenter properties, we need to use type 0xfffe for
10475 * master and 0xffff for slaves. */
10476 if (ofpprop_is_experimenter(ap->prop_type)) {
10477 struct ofp_prop_experimenter *ope
10478 = ofpbuf_at_assert(buf, ofs, sizeof *ope);
10479 ope->type = ap->master ? htons(0xffff) : htons(0xfffe);
10480 }
10481 }
10482 }
10483 }
10484 }
10485
10486 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10487 * states that the asynchronous message configuration is 'ac'. */
10488 struct ofpbuf *
10489 ofputil_encode_get_async_reply(const struct ofp_header *oh,
10490 const struct ofputil_async_cfg *ac)
10491 {
10492 struct ofpbuf *buf;
10493
10494 enum ofpraw raw = (oh->version < OFP14_VERSION
10495 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10496 : OFPRAW_OFPT14_GET_ASYNC_REPLY);
10497 struct ofpbuf *reply = ofpraw_alloc_reply(raw, oh, 0);
10498 ofputil_put_async_config__(ac, reply,
10499 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY,
10500 oh->version, UINT32_MAX);
10501 return reply;
10502
10503 return buf;
10504 }
10505
10506 /* Encodes and returns a message, in a format appropriate for OpenFlow version
10507 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10508 *
10509 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10510 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10511 * messages, so more messages than requested may be configured. OF1.0 through
10512 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10513 * any extensions then this function encodes an Open vSwitch extension message
10514 * that does support configuring OVS extension OAM_*. */
10515 struct ofpbuf *
10516 ofputil_encode_set_async_config(const struct ofputil_async_cfg *ac,
10517 uint32_t oams, enum ofp_version ofp_version)
10518 {
10519 enum ofpraw raw = (ofp_version >= OFP14_VERSION ? OFPRAW_OFPT14_SET_ASYNC
10520 : oams & OAM_EXTENSIONS ? OFPRAW_NXT_SET_ASYNC_CONFIG2
10521 : ofp_version >= OFP13_VERSION ? OFPRAW_OFPT13_SET_ASYNC
10522 : OFPRAW_NXT_SET_ASYNC_CONFIG);
10523 struct ofpbuf *request = ofpraw_alloc(raw, ofp_version, 0);
10524 ofputil_put_async_config__(ac, request,
10525 (raw == OFPRAW_OFPT14_SET_ASYNC ||
10526 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2),
10527 ofp_version, oams);
10528 return request;
10529 }
10530
10531 struct ofputil_async_cfg
10532 ofputil_async_cfg_default(enum ofp_version version)
10533 {
10534 /* We enable all of the OF1.4 reasons regardless of 'version' because the
10535 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10536 * introduced in OF1.0, breaking it into more specific categories. When we
10537 * encode these for earlier OpenFlow versions, we translate them into
10538 * OFPR_ACTION. */
10539 uint32_t pin = OFPR14_BITS & ~(1u << OFPR_INVALID_TTL);
10540 pin |= 1u << OFPR_EXPLICIT_MISS;
10541 if (version <= OFP12_VERSION) {
10542 pin |= 1u << OFPR_IMPLICIT_MISS;
10543 }
10544
10545 return (struct ofputil_async_cfg) {
10546 .master[OAM_PACKET_IN] = pin,
10547
10548 .master[OAM_FLOW_REMOVED]
10549 = (version >= OFP14_VERSION ? OFPRR14_BITS : OFPRR10_BITS),
10550
10551 .master[OAM_PORT_STATUS] = OFPPR_BITS,
10552 .slave[OAM_PORT_STATUS] = OFPPR_BITS,
10553 };
10554 }
10555
10556 static void
10557 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc *td,
10558 struct ofpbuf *b, enum ofp_version version)
10559 {
10560 struct ofp14_table_desc *otd;
10561 struct ofp14_table_mod_prop_vacancy *otv;
10562 size_t start_otd;
10563
10564 start_otd = b->size;
10565 ofpbuf_put_zeros(b, sizeof *otd);
10566
10567 ofpprop_put_u32(b, OFPTMPT14_EVICTION, td->eviction_flags);
10568
10569 otv = ofpbuf_put_zeros(b, sizeof *otv);
10570 otv->type = htons(OFPTMPT14_VACANCY);
10571 otv->length = htons(sizeof *otv);
10572 otv->vacancy_down = td->table_vacancy.vacancy_down;
10573 otv->vacancy_up = td->table_vacancy.vacancy_up;
10574 otv->vacancy = td->table_vacancy.vacancy;
10575
10576 otd = ofpbuf_at_assert(b, start_otd, sizeof *otd);
10577 otd->length = htons(b->size - start_otd);
10578 otd->table_id = td->table_id;
10579 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
10580 td->eviction, td->vacancy,
10581 version);
10582 }
10583
10584 /* Converts the abstract form of a "table status" message in '*ts' into an
10585 * OpenFlow message suitable for 'protocol', and returns that encoded form in
10586 * a buffer owned by the caller. */
10587 struct ofpbuf *
10588 ofputil_encode_table_status(const struct ofputil_table_status *ts,
10589 enum ofputil_protocol protocol)
10590 {
10591 enum ofp_version version;
10592 struct ofpbuf *b;
10593
10594 version = ofputil_protocol_to_ofp_version(protocol);
10595 if (version >= OFP14_VERSION) {
10596 enum ofpraw raw;
10597 struct ofp14_table_status *ots;
10598
10599 raw = OFPRAW_OFPT14_TABLE_STATUS;
10600 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
10601 ots = ofpbuf_put_zeros(b, sizeof *ots);
10602 ots->reason = ts->reason;
10603 ofputil_put_ofp14_table_desc(&ts->desc, b, version);
10604 ofpmsg_update_length(b);
10605 return b;
10606 } else {
10607 return NULL;
10608 }
10609 }
10610
10611 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10612 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10613 enum ofperr
10614 ofputil_decode_table_status(const struct ofp_header *oh,
10615 struct ofputil_table_status *ts)
10616 {
10617 const struct ofp14_table_status *ots;
10618 struct ofpbuf b;
10619 enum ofperr error;
10620 enum ofpraw raw;
10621
10622 ofpbuf_use_const(&b, oh, ntohs(oh->length));
10623 raw = ofpraw_pull_assert(&b);
10624 ots = ofpbuf_pull(&b, sizeof *ots);
10625
10626 if (raw == OFPRAW_OFPT14_TABLE_STATUS) {
10627 if (ots->reason != OFPTR_VACANCY_DOWN
10628 && ots->reason != OFPTR_VACANCY_UP) {
10629 return OFPERR_OFPBPC_BAD_VALUE;
10630 }
10631 ts->reason = ots->reason;
10632
10633 error = ofputil_decode_table_desc(&b, &ts->desc, oh->version);
10634 return error;
10635 } else {
10636 return OFPERR_OFPBRC_BAD_VERSION;
10637 }
10638
10639 return 0;
10640 }