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