]> git.proxmox.com Git - ovs.git/blob - lib/ofp-util.c
ofproto: Use OFPRR_GROUP_DELETE
[ovs.git] / lib / ofp-util.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 "ofp-actions.h"
36 #include "ofp-errors.h"
37 #include "ofp-msgs.h"
38 #include "ofp-util.h"
39 #include "ofpbuf.h"
40 #include "packets.h"
41 #include "random.h"
42 #include "unaligned.h"
43 #include "type-props.h"
44 #include "vlog.h"
45 #include "bitmap.h"
46
47 VLOG_DEFINE_THIS_MODULE(ofp_util);
48
49 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
50 * in the peer and so there's not much point in showing a lot of them. */
51 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
52
53 struct ofp_prop_header {
54 ovs_be16 type;
55 ovs_be16 len;
56 };
57
58 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
59 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
60 * nonnull, the entire property, including the header, in '*property'. Returns
61 * 0 if successful, otherwise an error code. */
62 static enum ofperr
63 ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
64 uint16_t *typep)
65 {
66 struct ofp_prop_header *oph;
67 unsigned int len;
68
69 if (ofpbuf_size(msg) < sizeof *oph) {
70 return OFPERR_OFPBPC_BAD_LEN;
71 }
72
73 oph = ofpbuf_data(msg);
74 len = ntohs(oph->len);
75 if (len < sizeof *oph || ROUND_UP(len, 8) > ofpbuf_size(msg)) {
76 return OFPERR_OFPBPC_BAD_LEN;
77 }
78
79 *typep = ntohs(oph->type);
80 if (property) {
81 ofpbuf_use_const(property, ofpbuf_data(msg), len);
82 }
83 ofpbuf_pull(msg, ROUND_UP(len, 8));
84 return 0;
85 }
86
87 static void PRINTF_FORMAT(2, 3)
88 log_property(bool loose, const char *message, ...)
89 {
90 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
91 if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
92 va_list args;
93
94 va_start(args, message);
95 vlog_valist(THIS_MODULE, level, message, args);
96 va_end(args);
97 }
98 }
99
100 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
101 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
102 * is wildcarded.
103 *
104 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
105 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
106 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
107 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
108 * wildcarded. */
109 ovs_be32
110 ofputil_wcbits_to_netmask(int wcbits)
111 {
112 wcbits &= 0x3f;
113 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
114 }
115
116 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
117 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
118 * between 0 and 32 inclusive.
119 *
120 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
121 * still be in the valid range but isn't otherwise meaningful. */
122 int
123 ofputil_netmask_to_wcbits(ovs_be32 netmask)
124 {
125 return 32 - ip_count_cidr_bits(netmask);
126 }
127
128 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
129 * flow_wildcards in 'wc' for use in struct match. It is the caller's
130 * responsibility to handle the special case where the flow match's dl_vlan is
131 * set to OFP_VLAN_NONE. */
132 void
133 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
134 {
135 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
136
137 /* Initialize most of wc. */
138 flow_wildcards_init_catchall(wc);
139
140 if (!(ofpfw & OFPFW10_IN_PORT)) {
141 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
142 }
143
144 if (!(ofpfw & OFPFW10_NW_TOS)) {
145 wc->masks.nw_tos |= IP_DSCP_MASK;
146 }
147
148 if (!(ofpfw & OFPFW10_NW_PROTO)) {
149 wc->masks.nw_proto = UINT8_MAX;
150 }
151 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
152 >> OFPFW10_NW_SRC_SHIFT);
153 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
154 >> OFPFW10_NW_DST_SHIFT);
155
156 if (!(ofpfw & OFPFW10_TP_SRC)) {
157 wc->masks.tp_src = OVS_BE16_MAX;
158 }
159 if (!(ofpfw & OFPFW10_TP_DST)) {
160 wc->masks.tp_dst = OVS_BE16_MAX;
161 }
162
163 if (!(ofpfw & OFPFW10_DL_SRC)) {
164 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
165 }
166 if (!(ofpfw & OFPFW10_DL_DST)) {
167 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
168 }
169 if (!(ofpfw & OFPFW10_DL_TYPE)) {
170 wc->masks.dl_type = OVS_BE16_MAX;
171 }
172
173 /* VLAN TCI mask. */
174 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
175 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
176 }
177 if (!(ofpfw & OFPFW10_DL_VLAN)) {
178 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
179 }
180 }
181
182 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
183 void
184 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
185 struct match *match)
186 {
187 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
188
189 /* Initialize match->wc. */
190 memset(&match->flow, 0, sizeof match->flow);
191 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
192
193 /* Initialize most of match->flow. */
194 match->flow.nw_src = ofmatch->nw_src;
195 match->flow.nw_dst = ofmatch->nw_dst;
196 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
197 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
198 match->flow.tp_src = ofmatch->tp_src;
199 match->flow.tp_dst = ofmatch->tp_dst;
200 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
201 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
202 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
203 match->flow.nw_proto = ofmatch->nw_proto;
204
205 /* Translate VLANs. */
206 if (!(ofpfw & OFPFW10_DL_VLAN) &&
207 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
208 /* Match only packets without 802.1Q header.
209 *
210 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
211 *
212 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
213 * because we can't have a specific PCP without an 802.1Q header.
214 * However, older versions of OVS treated this as matching packets
215 * withut an 802.1Q header, so we do here too. */
216 match->flow.vlan_tci = htons(0);
217 match->wc.masks.vlan_tci = htons(0xffff);
218 } else {
219 ovs_be16 vid, pcp, tci;
220 uint16_t hpcp;
221
222 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
223 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
224 pcp = htons(hpcp);
225 tci = vid | pcp | htons(VLAN_CFI);
226 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
227 }
228
229 /* Clean up. */
230 match_zero_wildcarded_fields(match);
231 }
232
233 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
234 void
235 ofputil_match_to_ofp10_match(const struct match *match,
236 struct ofp10_match *ofmatch)
237 {
238 const struct flow_wildcards *wc = &match->wc;
239 uint32_t ofpfw;
240
241 /* Figure out most OpenFlow wildcards. */
242 ofpfw = 0;
243 if (!wc->masks.in_port.ofp_port) {
244 ofpfw |= OFPFW10_IN_PORT;
245 }
246 if (!wc->masks.dl_type) {
247 ofpfw |= OFPFW10_DL_TYPE;
248 }
249 if (!wc->masks.nw_proto) {
250 ofpfw |= OFPFW10_NW_PROTO;
251 }
252 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
253 << OFPFW10_NW_SRC_SHIFT);
254 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
255 << OFPFW10_NW_DST_SHIFT);
256 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
257 ofpfw |= OFPFW10_NW_TOS;
258 }
259 if (!wc->masks.tp_src) {
260 ofpfw |= OFPFW10_TP_SRC;
261 }
262 if (!wc->masks.tp_dst) {
263 ofpfw |= OFPFW10_TP_DST;
264 }
265 if (eth_addr_is_zero(wc->masks.dl_src)) {
266 ofpfw |= OFPFW10_DL_SRC;
267 }
268 if (eth_addr_is_zero(wc->masks.dl_dst)) {
269 ofpfw |= OFPFW10_DL_DST;
270 }
271
272 /* Translate VLANs. */
273 ofmatch->dl_vlan = htons(0);
274 ofmatch->dl_vlan_pcp = 0;
275 if (match->wc.masks.vlan_tci == htons(0)) {
276 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
277 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
278 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
279 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
280 ofpfw |= OFPFW10_DL_VLAN_PCP;
281 } else {
282 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
283 ofpfw |= OFPFW10_DL_VLAN;
284 } else {
285 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
286 }
287
288 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
289 ofpfw |= OFPFW10_DL_VLAN_PCP;
290 } else {
291 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
292 }
293 }
294
295 /* Compose most of the match structure. */
296 ofmatch->wildcards = htonl(ofpfw);
297 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
298 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
299 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
300 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
301 ofmatch->nw_src = match->flow.nw_src;
302 ofmatch->nw_dst = match->flow.nw_dst;
303 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
304 ofmatch->nw_proto = match->flow.nw_proto;
305 ofmatch->tp_src = match->flow.tp_src;
306 ofmatch->tp_dst = match->flow.tp_dst;
307 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
308 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
309 }
310
311 enum ofperr
312 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
313 uint16_t *padded_match_len)
314 {
315 struct ofp11_match_header *omh = ofpbuf_data(buf);
316 uint16_t match_len;
317
318 if (ofpbuf_size(buf) < sizeof *omh) {
319 return OFPERR_OFPBMC_BAD_LEN;
320 }
321
322 match_len = ntohs(omh->length);
323
324 switch (ntohs(omh->type)) {
325 case OFPMT_STANDARD: {
326 struct ofp11_match *om;
327
328 if (match_len != sizeof *om || ofpbuf_size(buf) < sizeof *om) {
329 return OFPERR_OFPBMC_BAD_LEN;
330 }
331 om = ofpbuf_pull(buf, sizeof *om);
332 if (padded_match_len) {
333 *padded_match_len = match_len;
334 }
335 return ofputil_match_from_ofp11_match(om, match);
336 }
337
338 case OFPMT_OXM:
339 if (padded_match_len) {
340 *padded_match_len = ROUND_UP(match_len, 8);
341 }
342 return oxm_pull_match(buf, match);
343
344 default:
345 return OFPERR_OFPBMC_BAD_TYPE;
346 }
347 }
348
349 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
350 * Returns 0 if successful, otherwise an OFPERR_* value. */
351 enum ofperr
352 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
353 struct match *match)
354 {
355 uint16_t wc = ntohl(ofmatch->wildcards);
356 uint8_t dl_src_mask[ETH_ADDR_LEN];
357 uint8_t dl_dst_mask[ETH_ADDR_LEN];
358 bool ipv4, arp, rarp;
359 int i;
360
361 match_init_catchall(match);
362
363 if (!(wc & OFPFW11_IN_PORT)) {
364 ofp_port_t ofp_port;
365 enum ofperr error;
366
367 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
368 if (error) {
369 return OFPERR_OFPBMC_BAD_VALUE;
370 }
371 match_set_in_port(match, ofp_port);
372 }
373
374 for (i = 0; i < ETH_ADDR_LEN; i++) {
375 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
376 }
377 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
378
379 for (i = 0; i < ETH_ADDR_LEN; i++) {
380 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
381 }
382 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
383
384 if (!(wc & OFPFW11_DL_VLAN)) {
385 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
386 /* Match only packets without a VLAN tag. */
387 match->flow.vlan_tci = htons(0);
388 match->wc.masks.vlan_tci = OVS_BE16_MAX;
389 } else {
390 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
391 /* Match any packet with a VLAN tag regardless of VID. */
392 match->flow.vlan_tci = htons(VLAN_CFI);
393 match->wc.masks.vlan_tci = htons(VLAN_CFI);
394 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
395 /* Match only packets with the specified VLAN VID. */
396 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
397 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
398 } else {
399 /* Invalid VID. */
400 return OFPERR_OFPBMC_BAD_VALUE;
401 }
402
403 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
404 if (ofmatch->dl_vlan_pcp <= 7) {
405 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
406 << VLAN_PCP_SHIFT);
407 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
408 } else {
409 /* Invalid PCP. */
410 return OFPERR_OFPBMC_BAD_VALUE;
411 }
412 }
413 }
414 }
415
416 if (!(wc & OFPFW11_DL_TYPE)) {
417 match_set_dl_type(match,
418 ofputil_dl_type_from_openflow(ofmatch->dl_type));
419 }
420
421 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
422 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
423 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
424
425 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
426 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
427 /* Invalid TOS. */
428 return OFPERR_OFPBMC_BAD_VALUE;
429 }
430
431 match_set_nw_dscp(match, ofmatch->nw_tos);
432 }
433
434 if (ipv4 || arp || rarp) {
435 if (!(wc & OFPFW11_NW_PROTO)) {
436 match_set_nw_proto(match, ofmatch->nw_proto);
437 }
438 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
439 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
440 }
441
442 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
443 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
444 switch (match->flow.nw_proto) {
445 case IPPROTO_ICMP:
446 /* "A.2.3 Flow Match Structures" in OF1.1 says:
447 *
448 * The tp_src and tp_dst fields will be ignored unless the
449 * network protocol specified is as TCP, UDP or SCTP.
450 *
451 * but I'm pretty sure we should support ICMP too, otherwise
452 * that's a regression from OF1.0. */
453 if (!(wc & OFPFW11_TP_SRC)) {
454 uint16_t icmp_type = ntohs(ofmatch->tp_src);
455 if (icmp_type < 0x100) {
456 match_set_icmp_type(match, icmp_type);
457 } else {
458 return OFPERR_OFPBMC_BAD_FIELD;
459 }
460 }
461 if (!(wc & OFPFW11_TP_DST)) {
462 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
463 if (icmp_code < 0x100) {
464 match_set_icmp_code(match, icmp_code);
465 } else {
466 return OFPERR_OFPBMC_BAD_FIELD;
467 }
468 }
469 break;
470
471 case IPPROTO_TCP:
472 case IPPROTO_UDP:
473 case IPPROTO_SCTP:
474 if (!(wc & (OFPFW11_TP_SRC))) {
475 match_set_tp_src(match, ofmatch->tp_src);
476 }
477 if (!(wc & (OFPFW11_TP_DST))) {
478 match_set_tp_dst(match, ofmatch->tp_dst);
479 }
480 break;
481
482 default:
483 /* OF1.1 says explicitly to ignore this. */
484 break;
485 }
486 }
487
488 if (eth_type_mpls(match->flow.dl_type)) {
489 if (!(wc & OFPFW11_MPLS_LABEL)) {
490 match_set_mpls_label(match, 0, ofmatch->mpls_label);
491 }
492 if (!(wc & OFPFW11_MPLS_TC)) {
493 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
494 }
495 }
496
497 match_set_metadata_masked(match, ofmatch->metadata,
498 ~ofmatch->metadata_mask);
499
500 return 0;
501 }
502
503 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
504 void
505 ofputil_match_to_ofp11_match(const struct match *match,
506 struct ofp11_match *ofmatch)
507 {
508 uint32_t wc = 0;
509 int i;
510
511 memset(ofmatch, 0, sizeof *ofmatch);
512 ofmatch->omh.type = htons(OFPMT_STANDARD);
513 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
514
515 if (!match->wc.masks.in_port.ofp_port) {
516 wc |= OFPFW11_IN_PORT;
517 } else {
518 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
519 }
520
521 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
522 for (i = 0; i < ETH_ADDR_LEN; i++) {
523 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
524 }
525
526 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
527 for (i = 0; i < ETH_ADDR_LEN; i++) {
528 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
529 }
530
531 if (match->wc.masks.vlan_tci == htons(0)) {
532 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
533 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
534 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
535 ofmatch->dl_vlan = htons(OFPVID11_NONE);
536 wc |= OFPFW11_DL_VLAN_PCP;
537 } else {
538 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
539 ofmatch->dl_vlan = htons(OFPVID11_ANY);
540 } else {
541 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
542 }
543
544 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
545 wc |= OFPFW11_DL_VLAN_PCP;
546 } else {
547 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
548 }
549 }
550
551 if (!match->wc.masks.dl_type) {
552 wc |= OFPFW11_DL_TYPE;
553 } else {
554 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
555 }
556
557 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
558 wc |= OFPFW11_NW_TOS;
559 } else {
560 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
561 }
562
563 if (!match->wc.masks.nw_proto) {
564 wc |= OFPFW11_NW_PROTO;
565 } else {
566 ofmatch->nw_proto = match->flow.nw_proto;
567 }
568
569 ofmatch->nw_src = match->flow.nw_src;
570 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
571 ofmatch->nw_dst = match->flow.nw_dst;
572 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
573
574 if (!match->wc.masks.tp_src) {
575 wc |= OFPFW11_TP_SRC;
576 } else {
577 ofmatch->tp_src = match->flow.tp_src;
578 }
579
580 if (!match->wc.masks.tp_dst) {
581 wc |= OFPFW11_TP_DST;
582 } else {
583 ofmatch->tp_dst = match->flow.tp_dst;
584 }
585
586 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
587 wc |= OFPFW11_MPLS_LABEL;
588 } else {
589 ofmatch->mpls_label = htonl(mpls_lse_to_label(
590 match->flow.mpls_lse[0]));
591 }
592
593 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
594 wc |= OFPFW11_MPLS_TC;
595 } else {
596 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
597 }
598
599 ofmatch->metadata = match->flow.metadata;
600 ofmatch->metadata_mask = ~match->wc.masks.metadata;
601
602 ofmatch->wildcards = htonl(wc);
603 }
604
605 /* Returns the "typical" length of a match for 'protocol', for use in
606 * estimating space to preallocate. */
607 int
608 ofputil_match_typical_len(enum ofputil_protocol protocol)
609 {
610 switch (protocol) {
611 case OFPUTIL_P_OF10_STD:
612 case OFPUTIL_P_OF10_STD_TID:
613 return sizeof(struct ofp10_match);
614
615 case OFPUTIL_P_OF10_NXM:
616 case OFPUTIL_P_OF10_NXM_TID:
617 return NXM_TYPICAL_LEN;
618
619 case OFPUTIL_P_OF11_STD:
620 return sizeof(struct ofp11_match);
621
622 case OFPUTIL_P_OF12_OXM:
623 case OFPUTIL_P_OF13_OXM:
624 case OFPUTIL_P_OF14_OXM:
625 case OFPUTIL_P_OF15_OXM:
626 return NXM_TYPICAL_LEN;
627
628 default:
629 OVS_NOT_REACHED();
630 }
631 }
632
633 /* Appends to 'b' an struct ofp11_match_header followed by a match that
634 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
635 * data appended out to a multiple of 8. 'protocol' must be one that is usable
636 * in OpenFlow 1.1 or later.
637 *
638 * This function can cause 'b''s data to be reallocated.
639 *
640 * Returns the number of bytes appended to 'b', excluding the padding. Never
641 * returns zero. */
642 int
643 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
644 enum ofputil_protocol protocol)
645 {
646 switch (protocol) {
647 case OFPUTIL_P_OF10_STD:
648 case OFPUTIL_P_OF10_STD_TID:
649 case OFPUTIL_P_OF10_NXM:
650 case OFPUTIL_P_OF10_NXM_TID:
651 OVS_NOT_REACHED();
652
653 case OFPUTIL_P_OF11_STD: {
654 struct ofp11_match *om;
655
656 /* Make sure that no padding is needed. */
657 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
658
659 om = ofpbuf_put_uninit(b, sizeof *om);
660 ofputil_match_to_ofp11_match(match, om);
661 return sizeof *om;
662 }
663
664 case OFPUTIL_P_OF12_OXM:
665 case OFPUTIL_P_OF13_OXM:
666 case OFPUTIL_P_OF14_OXM:
667 case OFPUTIL_P_OF15_OXM:
668 return oxm_put_match(b, match,
669 ofputil_protocol_to_ofp_version(protocol));
670 }
671
672 OVS_NOT_REACHED();
673 }
674
675 /* Given a 'dl_type' value in the format used in struct flow, returns the
676 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
677 * structure. */
678 ovs_be16
679 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
680 {
681 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
682 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
683 : flow_dl_type);
684 }
685
686 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
687 * structure, returns the corresponding 'dl_type' value for use in struct
688 * flow. */
689 ovs_be16
690 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
691 {
692 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
693 ? htons(FLOW_DL_TYPE_NONE)
694 : ofp_dl_type);
695 }
696 \f
697 /* Protocols. */
698
699 struct proto_abbrev {
700 enum ofputil_protocol protocol;
701 const char *name;
702 };
703
704 /* Most users really don't care about some of the differences between
705 * protocols. These abbreviations help with that. */
706 static const struct proto_abbrev proto_abbrevs[] = {
707 { OFPUTIL_P_ANY, "any" },
708 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
709 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
710 { OFPUTIL_P_ANY_OXM, "OXM" },
711 };
712 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
713
714 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
715 OFPUTIL_P_OF15_OXM,
716 OFPUTIL_P_OF14_OXM,
717 OFPUTIL_P_OF13_OXM,
718 OFPUTIL_P_OF12_OXM,
719 OFPUTIL_P_OF11_STD,
720 OFPUTIL_P_OF10_NXM,
721 OFPUTIL_P_OF10_STD,
722 };
723 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
724
725 /* Returns the set of ofputil_protocols that are supported with the given
726 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
727 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
728 * if 'version' is not supported or outside the valid range. */
729 enum ofputil_protocol
730 ofputil_protocols_from_ofp_version(enum ofp_version version)
731 {
732 switch (version) {
733 case OFP10_VERSION:
734 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
735 case OFP11_VERSION:
736 return OFPUTIL_P_OF11_STD;
737 case OFP12_VERSION:
738 return OFPUTIL_P_OF12_OXM;
739 case OFP13_VERSION:
740 return OFPUTIL_P_OF13_OXM;
741 case OFP14_VERSION:
742 return OFPUTIL_P_OF14_OXM;
743 case OFP15_VERSION:
744 return OFPUTIL_P_OF15_OXM;
745 default:
746 return 0;
747 }
748 }
749
750 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
751 * connection that has negotiated the given 'version'. 'version' should
752 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
753 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
754 * outside the valid range. */
755 enum ofputil_protocol
756 ofputil_protocol_from_ofp_version(enum ofp_version version)
757 {
758 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
759 }
760
761 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
762 * etc.) that corresponds to 'protocol'. */
763 enum ofp_version
764 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
765 {
766 switch (protocol) {
767 case OFPUTIL_P_OF10_STD:
768 case OFPUTIL_P_OF10_STD_TID:
769 case OFPUTIL_P_OF10_NXM:
770 case OFPUTIL_P_OF10_NXM_TID:
771 return OFP10_VERSION;
772 case OFPUTIL_P_OF11_STD:
773 return OFP11_VERSION;
774 case OFPUTIL_P_OF12_OXM:
775 return OFP12_VERSION;
776 case OFPUTIL_P_OF13_OXM:
777 return OFP13_VERSION;
778 case OFPUTIL_P_OF14_OXM:
779 return OFP14_VERSION;
780 case OFPUTIL_P_OF15_OXM:
781 return OFP15_VERSION;
782 }
783
784 OVS_NOT_REACHED();
785 }
786
787 /* Returns a bitmap of OpenFlow versions that are supported by at
788 * least one of the 'protocols'. */
789 uint32_t
790 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
791 {
792 uint32_t bitmap = 0;
793
794 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
795 enum ofputil_protocol protocol = rightmost_1bit(protocols);
796
797 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
798 }
799
800 return bitmap;
801 }
802
803 /* Returns the set of protocols that are supported on top of the
804 * OpenFlow versions included in 'bitmap'. */
805 enum ofputil_protocol
806 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
807 {
808 enum ofputil_protocol protocols = 0;
809
810 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
811 enum ofp_version version = rightmost_1bit_idx(bitmap);
812
813 protocols |= ofputil_protocols_from_ofp_version(version);
814 }
815
816 return protocols;
817 }
818
819 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
820 * otherwise. */
821 bool
822 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
823 {
824 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
825 }
826
827 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
828 * extension turned on or off if 'enable' is true or false, respectively.
829 *
830 * This extension is only useful for protocols whose "standard" version does
831 * not allow specific tables to be modified. In particular, this is true of
832 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
833 * specifies a table ID and so there is no need for such an extension. When
834 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
835 * extension, this function just returns its 'protocol' argument unchanged
836 * regardless of the value of 'enable'. */
837 enum ofputil_protocol
838 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
839 {
840 switch (protocol) {
841 case OFPUTIL_P_OF10_STD:
842 case OFPUTIL_P_OF10_STD_TID:
843 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
844
845 case OFPUTIL_P_OF10_NXM:
846 case OFPUTIL_P_OF10_NXM_TID:
847 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
848
849 case OFPUTIL_P_OF11_STD:
850 return OFPUTIL_P_OF11_STD;
851
852 case OFPUTIL_P_OF12_OXM:
853 return OFPUTIL_P_OF12_OXM;
854
855 case OFPUTIL_P_OF13_OXM:
856 return OFPUTIL_P_OF13_OXM;
857
858 case OFPUTIL_P_OF14_OXM:
859 return OFPUTIL_P_OF14_OXM;
860
861 case OFPUTIL_P_OF15_OXM:
862 return OFPUTIL_P_OF15_OXM;
863
864 default:
865 OVS_NOT_REACHED();
866 }
867 }
868
869 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
870 * some extension to a standard protocol version, the return value is the
871 * standard version of that protocol without any extension. If 'protocol' is a
872 * standard protocol version, returns 'protocol' unchanged. */
873 enum ofputil_protocol
874 ofputil_protocol_to_base(enum ofputil_protocol protocol)
875 {
876 return ofputil_protocol_set_tid(protocol, false);
877 }
878
879 /* Returns 'new_base' with any extensions taken from 'cur'. */
880 enum ofputil_protocol
881 ofputil_protocol_set_base(enum ofputil_protocol cur,
882 enum ofputil_protocol new_base)
883 {
884 bool tid = (cur & OFPUTIL_P_TID) != 0;
885
886 switch (new_base) {
887 case OFPUTIL_P_OF10_STD:
888 case OFPUTIL_P_OF10_STD_TID:
889 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
890
891 case OFPUTIL_P_OF10_NXM:
892 case OFPUTIL_P_OF10_NXM_TID:
893 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
894
895 case OFPUTIL_P_OF11_STD:
896 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
897
898 case OFPUTIL_P_OF12_OXM:
899 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
900
901 case OFPUTIL_P_OF13_OXM:
902 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
903
904 case OFPUTIL_P_OF14_OXM:
905 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
906
907 case OFPUTIL_P_OF15_OXM:
908 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
909
910 default:
911 OVS_NOT_REACHED();
912 }
913 }
914
915 /* Returns a string form of 'protocol', if a simple form exists (that is, if
916 * 'protocol' is either a single protocol or it is a combination of protocols
917 * that have a single abbreviation). Otherwise, returns NULL. */
918 const char *
919 ofputil_protocol_to_string(enum ofputil_protocol protocol)
920 {
921 const struct proto_abbrev *p;
922
923 /* Use a "switch" statement for single-bit names so that we get a compiler
924 * warning if we forget any. */
925 switch (protocol) {
926 case OFPUTIL_P_OF10_NXM:
927 return "NXM-table_id";
928
929 case OFPUTIL_P_OF10_NXM_TID:
930 return "NXM+table_id";
931
932 case OFPUTIL_P_OF10_STD:
933 return "OpenFlow10-table_id";
934
935 case OFPUTIL_P_OF10_STD_TID:
936 return "OpenFlow10+table_id";
937
938 case OFPUTIL_P_OF11_STD:
939 return "OpenFlow11";
940
941 case OFPUTIL_P_OF12_OXM:
942 return "OXM-OpenFlow12";
943
944 case OFPUTIL_P_OF13_OXM:
945 return "OXM-OpenFlow13";
946
947 case OFPUTIL_P_OF14_OXM:
948 return "OXM-OpenFlow14";
949
950 case OFPUTIL_P_OF15_OXM:
951 return "OXM-OpenFlow15";
952 }
953
954 /* Check abbreviations. */
955 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
956 if (protocol == p->protocol) {
957 return p->name;
958 }
959 }
960
961 return NULL;
962 }
963
964 /* Returns a string that represents 'protocols'. The return value might be a
965 * comma-separated list if 'protocols' doesn't have a simple name. The return
966 * value is "none" if 'protocols' is 0.
967 *
968 * The caller must free the returned string (with free()). */
969 char *
970 ofputil_protocols_to_string(enum ofputil_protocol protocols)
971 {
972 struct ds s;
973
974 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
975 if (protocols == 0) {
976 return xstrdup("none");
977 }
978
979 ds_init(&s);
980 while (protocols) {
981 const struct proto_abbrev *p;
982 int i;
983
984 if (s.length) {
985 ds_put_char(&s, ',');
986 }
987
988 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
989 if ((protocols & p->protocol) == p->protocol) {
990 ds_put_cstr(&s, p->name);
991 protocols &= ~p->protocol;
992 goto match;
993 }
994 }
995
996 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
997 enum ofputil_protocol bit = 1u << i;
998
999 if (protocols & bit) {
1000 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1001 protocols &= ~bit;
1002 goto match;
1003 }
1004 }
1005 OVS_NOT_REACHED();
1006
1007 match: ;
1008 }
1009 return ds_steal_cstr(&s);
1010 }
1011
1012 static enum ofputil_protocol
1013 ofputil_protocol_from_string__(const char *s, size_t n)
1014 {
1015 const struct proto_abbrev *p;
1016 int i;
1017
1018 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1019 enum ofputil_protocol bit = 1u << i;
1020 const char *name = ofputil_protocol_to_string(bit);
1021
1022 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1023 return bit;
1024 }
1025 }
1026
1027 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1028 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1029 return p->protocol;
1030 }
1031 }
1032
1033 return 0;
1034 }
1035
1036 /* Returns the nonempty set of protocols represented by 's', which can be a
1037 * single protocol name or abbreviation or a comma-separated list of them.
1038 *
1039 * Aborts the program with an error message if 's' is invalid. */
1040 enum ofputil_protocol
1041 ofputil_protocols_from_string(const char *s)
1042 {
1043 const char *orig_s = s;
1044 enum ofputil_protocol protocols;
1045
1046 protocols = 0;
1047 while (*s) {
1048 enum ofputil_protocol p;
1049 size_t n;
1050
1051 n = strcspn(s, ",");
1052 if (n == 0) {
1053 s++;
1054 continue;
1055 }
1056
1057 p = ofputil_protocol_from_string__(s, n);
1058 if (!p) {
1059 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1060 }
1061 protocols |= p;
1062
1063 s += n;
1064 }
1065
1066 if (!protocols) {
1067 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1068 }
1069 return protocols;
1070 }
1071
1072 enum ofp_version
1073 ofputil_version_from_string(const char *s)
1074 {
1075 if (!strcasecmp(s, "OpenFlow10")) {
1076 return OFP10_VERSION;
1077 }
1078 if (!strcasecmp(s, "OpenFlow11")) {
1079 return OFP11_VERSION;
1080 }
1081 if (!strcasecmp(s, "OpenFlow12")) {
1082 return OFP12_VERSION;
1083 }
1084 if (!strcasecmp(s, "OpenFlow13")) {
1085 return OFP13_VERSION;
1086 }
1087 if (!strcasecmp(s, "OpenFlow14")) {
1088 return OFP14_VERSION;
1089 }
1090 if (!strcasecmp(s, "OpenFlow15")) {
1091 return OFP15_VERSION;
1092 }
1093 return 0;
1094 }
1095
1096 static bool
1097 is_delimiter(unsigned char c)
1098 {
1099 return isspace(c) || c == ',';
1100 }
1101
1102 uint32_t
1103 ofputil_versions_from_string(const char *s)
1104 {
1105 size_t i = 0;
1106 uint32_t bitmap = 0;
1107
1108 while (s[i]) {
1109 size_t j;
1110 int version;
1111 char *key;
1112
1113 if (is_delimiter(s[i])) {
1114 i++;
1115 continue;
1116 }
1117 j = 0;
1118 while (s[i + j] && !is_delimiter(s[i + j])) {
1119 j++;
1120 }
1121 key = xmemdup0(s + i, j);
1122 version = ofputil_version_from_string(key);
1123 if (!version) {
1124 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1125 }
1126 free(key);
1127 bitmap |= 1u << version;
1128 i += j;
1129 }
1130
1131 return bitmap;
1132 }
1133
1134 uint32_t
1135 ofputil_versions_from_strings(char ** const s, size_t count)
1136 {
1137 uint32_t bitmap = 0;
1138
1139 while (count--) {
1140 int version = ofputil_version_from_string(s[count]);
1141 if (!version) {
1142 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1143 } else {
1144 bitmap |= 1u << version;
1145 }
1146 }
1147
1148 return bitmap;
1149 }
1150
1151 const char *
1152 ofputil_version_to_string(enum ofp_version ofp_version)
1153 {
1154 switch (ofp_version) {
1155 case OFP10_VERSION:
1156 return "OpenFlow10";
1157 case OFP11_VERSION:
1158 return "OpenFlow11";
1159 case OFP12_VERSION:
1160 return "OpenFlow12";
1161 case OFP13_VERSION:
1162 return "OpenFlow13";
1163 case OFP14_VERSION:
1164 return "OpenFlow14";
1165 case OFP15_VERSION:
1166 return "OpenFlow15";
1167 default:
1168 OVS_NOT_REACHED();
1169 }
1170 }
1171
1172 bool
1173 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1174 {
1175 switch (packet_in_format) {
1176 case NXPIF_OPENFLOW10:
1177 case NXPIF_NXM:
1178 return true;
1179 }
1180
1181 return false;
1182 }
1183
1184 const char *
1185 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1186 {
1187 switch (packet_in_format) {
1188 case NXPIF_OPENFLOW10:
1189 return "openflow10";
1190 case NXPIF_NXM:
1191 return "nxm";
1192 default:
1193 OVS_NOT_REACHED();
1194 }
1195 }
1196
1197 int
1198 ofputil_packet_in_format_from_string(const char *s)
1199 {
1200 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1201 : !strcmp(s, "nxm") ? NXPIF_NXM
1202 : -1);
1203 }
1204
1205 void
1206 ofputil_format_version(struct ds *msg, enum ofp_version version)
1207 {
1208 ds_put_format(msg, "0x%02x", version);
1209 }
1210
1211 void
1212 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1213 {
1214 ds_put_cstr(msg, ofputil_version_to_string(version));
1215 }
1216
1217 static void
1218 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1219 void (*format_version)(struct ds *msg,
1220 enum ofp_version))
1221 {
1222 while (bitmap) {
1223 format_version(msg, raw_ctz(bitmap));
1224 bitmap = zero_rightmost_1bit(bitmap);
1225 if (bitmap) {
1226 ds_put_cstr(msg, ", ");
1227 }
1228 }
1229 }
1230
1231 void
1232 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1233 {
1234 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1235 }
1236
1237 void
1238 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1239 {
1240 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1241 }
1242
1243 static bool
1244 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1245 uint32_t *allowed_versionsp)
1246 {
1247 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1248 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1249 uint32_t allowed_versions;
1250
1251 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1252 return false;
1253 }
1254
1255 /* Only use the first 32-bit element of the bitmap as that is all the
1256 * current implementation supports. Subsequent elements are ignored which
1257 * should have no effect on session negotiation until Open vSwtich supports
1258 * wire-protocol versions greater than 31.
1259 */
1260 allowed_versions = ntohl(bitmap[0]);
1261
1262 if (allowed_versions & 1) {
1263 /* There's no OpenFlow version 0. */
1264 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1265 "version 0x00");
1266 allowed_versions &= ~1u;
1267 }
1268
1269 if (!allowed_versions) {
1270 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1271 "version (between 0x01 and 0x1f)");
1272 return false;
1273 }
1274
1275 *allowed_versionsp = allowed_versions;
1276 return true;
1277 }
1278
1279 static uint32_t
1280 version_bitmap_from_version(uint8_t ofp_version)
1281 {
1282 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1283 }
1284
1285 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1286 * the set of OpenFlow versions for which 'oh' announces support.
1287 *
1288 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1289 * successful, and thus '*allowed_versions' is always initialized. However, it
1290 * returns false if 'oh' contains some data that could not be fully understood,
1291 * true if 'oh' was completely parsed. */
1292 bool
1293 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1294 {
1295 struct ofpbuf msg;
1296 bool ok = true;
1297
1298 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1299 ofpbuf_pull(&msg, sizeof *oh);
1300
1301 *allowed_versions = version_bitmap_from_version(oh->version);
1302 while (ofpbuf_size(&msg)) {
1303 const struct ofp_hello_elem_header *oheh;
1304 unsigned int len;
1305
1306 if (ofpbuf_size(&msg) < sizeof *oheh) {
1307 return false;
1308 }
1309
1310 oheh = ofpbuf_data(&msg);
1311 len = ntohs(oheh->length);
1312 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1313 return false;
1314 }
1315
1316 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1317 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1318 ok = false;
1319 }
1320 }
1321
1322 return ok;
1323 }
1324
1325 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1326 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1327 static bool
1328 should_send_version_bitmap(uint32_t allowed_versions)
1329 {
1330 return !is_pow2((allowed_versions >> 1) + 1);
1331 }
1332
1333 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1334 * versions in the 'allowed_versions' bitmaps and returns the message. */
1335 struct ofpbuf *
1336 ofputil_encode_hello(uint32_t allowed_versions)
1337 {
1338 enum ofp_version ofp_version;
1339 struct ofpbuf *msg;
1340
1341 ofp_version = leftmost_1bit_idx(allowed_versions);
1342 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1343
1344 if (should_send_version_bitmap(allowed_versions)) {
1345 struct ofp_hello_elem_header *oheh;
1346 uint16_t map_len;
1347
1348 map_len = sizeof allowed_versions;
1349 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1350 oheh->type = htons(OFPHET_VERSIONBITMAP);
1351 oheh->length = htons(map_len + sizeof *oheh);
1352 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1353
1354 ofpmsg_update_length(msg);
1355 }
1356
1357 return msg;
1358 }
1359
1360 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1361 * protocol is 'current', at least partly transitions the protocol to 'want'.
1362 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1363 * connection if the switch processes the returned message correctly. (If
1364 * '*next != want' then the caller will have to iterate.)
1365 *
1366 * If 'current == want', or if it is not possible to transition from 'current'
1367 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1368 * protocol versions), returns NULL and stores 'current' in '*next'. */
1369 struct ofpbuf *
1370 ofputil_encode_set_protocol(enum ofputil_protocol current,
1371 enum ofputil_protocol want,
1372 enum ofputil_protocol *next)
1373 {
1374 enum ofp_version cur_version, want_version;
1375 enum ofputil_protocol cur_base, want_base;
1376 bool cur_tid, want_tid;
1377
1378 cur_version = ofputil_protocol_to_ofp_version(current);
1379 want_version = ofputil_protocol_to_ofp_version(want);
1380 if (cur_version != want_version) {
1381 *next = current;
1382 return NULL;
1383 }
1384
1385 cur_base = ofputil_protocol_to_base(current);
1386 want_base = ofputil_protocol_to_base(want);
1387 if (cur_base != want_base) {
1388 *next = ofputil_protocol_set_base(current, want_base);
1389
1390 switch (want_base) {
1391 case OFPUTIL_P_OF10_NXM:
1392 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1393
1394 case OFPUTIL_P_OF10_STD:
1395 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1396
1397 case OFPUTIL_P_OF11_STD:
1398 case OFPUTIL_P_OF12_OXM:
1399 case OFPUTIL_P_OF13_OXM:
1400 case OFPUTIL_P_OF14_OXM:
1401 case OFPUTIL_P_OF15_OXM:
1402 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1403 * verified above that we're not trying to change versions. */
1404 OVS_NOT_REACHED();
1405
1406 case OFPUTIL_P_OF10_STD_TID:
1407 case OFPUTIL_P_OF10_NXM_TID:
1408 OVS_NOT_REACHED();
1409 }
1410 }
1411
1412 cur_tid = (current & OFPUTIL_P_TID) != 0;
1413 want_tid = (want & OFPUTIL_P_TID) != 0;
1414 if (cur_tid != want_tid) {
1415 *next = ofputil_protocol_set_tid(current, want_tid);
1416 return ofputil_make_flow_mod_table_id(want_tid);
1417 }
1418
1419 ovs_assert(current == want);
1420
1421 *next = current;
1422 return NULL;
1423 }
1424
1425 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1426 * format to 'nxff'. */
1427 struct ofpbuf *
1428 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1429 {
1430 struct nx_set_flow_format *sff;
1431 struct ofpbuf *msg;
1432
1433 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1434
1435 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1436 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1437 sff->format = htonl(nxff);
1438
1439 return msg;
1440 }
1441
1442 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1443 * otherwise. */
1444 enum ofputil_protocol
1445 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1446 {
1447 switch (flow_format) {
1448 case NXFF_OPENFLOW10:
1449 return OFPUTIL_P_OF10_STD;
1450
1451 case NXFF_NXM:
1452 return OFPUTIL_P_OF10_NXM;
1453
1454 default:
1455 return 0;
1456 }
1457 }
1458
1459 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1460 bool
1461 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1462 {
1463 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1464 }
1465
1466 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1467 * value. */
1468 const char *
1469 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1470 {
1471 switch (flow_format) {
1472 case NXFF_OPENFLOW10:
1473 return "openflow10";
1474 case NXFF_NXM:
1475 return "nxm";
1476 default:
1477 OVS_NOT_REACHED();
1478 }
1479 }
1480
1481 struct ofpbuf *
1482 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1483 enum nx_packet_in_format packet_in_format)
1484 {
1485 struct nx_set_packet_in_format *spif;
1486 struct ofpbuf *msg;
1487
1488 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1489 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1490 spif->format = htonl(packet_in_format);
1491
1492 return msg;
1493 }
1494
1495 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1496 * extension on or off (according to 'flow_mod_table_id'). */
1497 struct ofpbuf *
1498 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1499 {
1500 struct nx_flow_mod_table_id *nfmti;
1501 struct ofpbuf *msg;
1502
1503 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1504 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1505 nfmti->set = flow_mod_table_id;
1506 return msg;
1507 }
1508
1509 struct ofputil_flow_mod_flag {
1510 uint16_t raw_flag;
1511 enum ofp_version min_version, max_version;
1512 enum ofputil_flow_mod_flags flag;
1513 };
1514
1515 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1516 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1517 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1518 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1519 OFPUTIL_FF_EMERG },
1520 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1521 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1522 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1523 { 0, 0, 0, 0 },
1524 };
1525
1526 static enum ofperr
1527 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1528 enum ofp_flow_mod_command command,
1529 enum ofp_version version,
1530 enum ofputil_flow_mod_flags *flagsp)
1531 {
1532 uint16_t raw_flags = ntohs(raw_flags_);
1533 const struct ofputil_flow_mod_flag *f;
1534
1535 *flagsp = 0;
1536 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1537 if (raw_flags & f->raw_flag
1538 && version >= f->min_version
1539 && (!f->max_version || version <= f->max_version)) {
1540 raw_flags &= ~f->raw_flag;
1541 *flagsp |= f->flag;
1542 }
1543 }
1544
1545 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1546 * never do.
1547 *
1548 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1549 * resets counters. */
1550 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1551 && command == OFPFC_ADD) {
1552 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1553 }
1554
1555 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1556 }
1557
1558 static ovs_be16
1559 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1560 enum ofp_version version)
1561 {
1562 const struct ofputil_flow_mod_flag *f;
1563 uint16_t raw_flags;
1564
1565 raw_flags = 0;
1566 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1567 if (f->flag & flags
1568 && version >= f->min_version
1569 && (!f->max_version || version <= f->max_version)) {
1570 raw_flags |= f->raw_flag;
1571 }
1572 }
1573
1574 return htons(raw_flags);
1575 }
1576
1577 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1578 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1579 * code.
1580 *
1581 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1582 * The caller must initialize 'ofpacts' and retains ownership of it.
1583 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1584 *
1585 * Does not validate the flow_mod actions. The caller should do that, with
1586 * ofpacts_check(). */
1587 enum ofperr
1588 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1589 const struct ofp_header *oh,
1590 enum ofputil_protocol protocol,
1591 struct ofpbuf *ofpacts,
1592 ofp_port_t max_port, uint8_t max_table)
1593 {
1594 ovs_be16 raw_flags;
1595 enum ofperr error;
1596 struct ofpbuf b;
1597 enum ofpraw raw;
1598
1599 /* Ignored for non-delete actions */
1600 fm->delete_reason = OFPRR_DELETE;
1601
1602 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1603 raw = ofpraw_pull_assert(&b);
1604 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1605 /* Standard OpenFlow 1.1+ flow_mod. */
1606 const struct ofp11_flow_mod *ofm;
1607
1608 ofm = ofpbuf_pull(&b, sizeof *ofm);
1609
1610 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1611 if (error) {
1612 return error;
1613 }
1614
1615 error = ofpacts_pull_openflow_instructions(&b, ofpbuf_size(&b), oh->version,
1616 ofpacts);
1617 if (error) {
1618 return error;
1619 }
1620
1621 /* Translate the message. */
1622 fm->priority = ntohs(ofm->priority);
1623 if (ofm->command == OFPFC_ADD
1624 || (oh->version == OFP11_VERSION
1625 && (ofm->command == OFPFC_MODIFY ||
1626 ofm->command == OFPFC_MODIFY_STRICT)
1627 && ofm->cookie_mask == htonll(0))) {
1628 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1629 * not match on the cookie is treated as an "add" if there is no
1630 * match. */
1631 fm->cookie = htonll(0);
1632 fm->cookie_mask = htonll(0);
1633 fm->new_cookie = ofm->cookie;
1634 } else {
1635 fm->cookie = ofm->cookie;
1636 fm->cookie_mask = ofm->cookie_mask;
1637 fm->new_cookie = OVS_BE64_MAX;
1638 }
1639 fm->modify_cookie = false;
1640 fm->command = ofm->command;
1641
1642 /* Get table ID.
1643 *
1644 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1645 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1646 fm->table_id = ofm->table_id;
1647 if (fm->table_id == OFPTT_ALL
1648 && (oh->version == OFP11_VERSION
1649 || (ofm->command != OFPFC_DELETE &&
1650 ofm->command != OFPFC_DELETE_STRICT))) {
1651 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1652 }
1653
1654 fm->idle_timeout = ntohs(ofm->idle_timeout);
1655 fm->hard_timeout = ntohs(ofm->hard_timeout);
1656 fm->buffer_id = ntohl(ofm->buffer_id);
1657 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1658 if (error) {
1659 return error;
1660 }
1661
1662 fm->out_group = (ofm->command == OFPFC_DELETE ||
1663 ofm->command == OFPFC_DELETE_STRICT
1664 ? ntohl(ofm->out_group)
1665 : OFPG11_ANY);
1666 raw_flags = ofm->flags;
1667 } else {
1668 uint16_t command;
1669
1670 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1671 /* Standard OpenFlow 1.0 flow_mod. */
1672 const struct ofp10_flow_mod *ofm;
1673
1674 /* Get the ofp10_flow_mod. */
1675 ofm = ofpbuf_pull(&b, sizeof *ofm);
1676
1677 /* Translate the rule. */
1678 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1679 ofputil_normalize_match(&fm->match);
1680
1681 /* Now get the actions. */
1682 error = ofpacts_pull_openflow_actions(&b, ofpbuf_size(&b), oh->version,
1683 ofpacts);
1684 if (error) {
1685 return error;
1686 }
1687
1688 /* OpenFlow 1.0 says that exact-match rules have to have the
1689 * highest possible priority. */
1690 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1691 ? ntohs(ofm->priority)
1692 : UINT16_MAX);
1693
1694 /* Translate the message. */
1695 command = ntohs(ofm->command);
1696 fm->cookie = htonll(0);
1697 fm->cookie_mask = htonll(0);
1698 fm->new_cookie = ofm->cookie;
1699 fm->idle_timeout = ntohs(ofm->idle_timeout);
1700 fm->hard_timeout = ntohs(ofm->hard_timeout);
1701 fm->buffer_id = ntohl(ofm->buffer_id);
1702 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1703 fm->out_group = OFPG11_ANY;
1704 raw_flags = ofm->flags;
1705 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1706 /* Nicira extended flow_mod. */
1707 const struct nx_flow_mod *nfm;
1708
1709 /* Dissect the message. */
1710 nfm = ofpbuf_pull(&b, sizeof *nfm);
1711 error = nx_pull_match(&b, ntohs(nfm->match_len),
1712 &fm->match, &fm->cookie, &fm->cookie_mask);
1713 if (error) {
1714 return error;
1715 }
1716 error = ofpacts_pull_openflow_actions(&b, ofpbuf_size(&b), oh->version,
1717 ofpacts);
1718 if (error) {
1719 return error;
1720 }
1721
1722 /* Translate the message. */
1723 command = ntohs(nfm->command);
1724 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1725 /* Flow additions may only set a new cookie, not match an
1726 * existing cookie. */
1727 return OFPERR_NXBRC_NXM_INVALID;
1728 }
1729 fm->priority = ntohs(nfm->priority);
1730 fm->new_cookie = nfm->cookie;
1731 fm->idle_timeout = ntohs(nfm->idle_timeout);
1732 fm->hard_timeout = ntohs(nfm->hard_timeout);
1733 fm->buffer_id = ntohl(nfm->buffer_id);
1734 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1735 fm->out_group = OFPG11_ANY;
1736 raw_flags = nfm->flags;
1737 } else {
1738 OVS_NOT_REACHED();
1739 }
1740
1741 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1742 if (protocol & OFPUTIL_P_TID) {
1743 fm->command = command & 0xff;
1744 fm->table_id = command >> 8;
1745 } else {
1746 fm->command = command;
1747 fm->table_id = 0xff;
1748 }
1749 }
1750
1751 fm->ofpacts = ofpbuf_data(ofpacts);
1752 fm->ofpacts_len = ofpbuf_size(ofpacts);
1753
1754 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1755 oh->version, &fm->flags);
1756 if (error) {
1757 return error;
1758 }
1759
1760 if (fm->flags & OFPUTIL_FF_EMERG) {
1761 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1762 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1763 *
1764 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1765 * or hard_timeout is nonzero. Otherwise, there is no good error
1766 * code, so just state that the flow table is full. */
1767 return (fm->hard_timeout || fm->idle_timeout
1768 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1769 : OFPERR_OFPFMFC_TABLE_FULL);
1770 }
1771
1772 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1773 &fm->match.flow, max_port,
1774 fm->table_id, max_table, protocol);
1775 }
1776
1777 static enum ofperr
1778 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1779 struct ofpbuf *bands)
1780 {
1781 const struct ofp13_meter_band_header *ombh;
1782 struct ofputil_meter_band *mb;
1783 uint16_t n = 0;
1784
1785 ombh = ofpbuf_try_pull(msg, len);
1786 if (!ombh) {
1787 return OFPERR_OFPBRC_BAD_LEN;
1788 }
1789
1790 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1791 size_t ombh_len = ntohs(ombh->len);
1792 /* All supported band types have the same length. */
1793 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1794 return OFPERR_OFPBRC_BAD_LEN;
1795 }
1796 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1797 mb->type = ntohs(ombh->type);
1798 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1799 return OFPERR_OFPMMFC_BAD_BAND;
1800 }
1801 mb->rate = ntohl(ombh->rate);
1802 mb->burst_size = ntohl(ombh->burst_size);
1803 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1804 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1805 n++;
1806 len -= ombh_len;
1807 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1808 (char *) ombh + ombh_len);
1809 }
1810 if (len) {
1811 return OFPERR_OFPBRC_BAD_LEN;
1812 }
1813 *n_bands = n;
1814 return 0;
1815 }
1816
1817 enum ofperr
1818 ofputil_decode_meter_mod(const struct ofp_header *oh,
1819 struct ofputil_meter_mod *mm,
1820 struct ofpbuf *bands)
1821 {
1822 const struct ofp13_meter_mod *omm;
1823 struct ofpbuf b;
1824
1825 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1826 ofpraw_pull_assert(&b);
1827 omm = ofpbuf_pull(&b, sizeof *omm);
1828
1829 /* Translate the message. */
1830 mm->command = ntohs(omm->command);
1831 if (mm->command != OFPMC13_ADD &&
1832 mm->command != OFPMC13_MODIFY &&
1833 mm->command != OFPMC13_DELETE) {
1834 return OFPERR_OFPMMFC_BAD_COMMAND;
1835 }
1836 mm->meter.meter_id = ntohl(omm->meter_id);
1837
1838 if (mm->command == OFPMC13_DELETE) {
1839 mm->meter.flags = 0;
1840 mm->meter.n_bands = 0;
1841 mm->meter.bands = NULL;
1842 } else {
1843 enum ofperr error;
1844
1845 mm->meter.flags = ntohs(omm->flags);
1846 if (mm->meter.flags & OFPMF13_KBPS &&
1847 mm->meter.flags & OFPMF13_PKTPS) {
1848 return OFPERR_OFPMMFC_BAD_FLAGS;
1849 }
1850 mm->meter.bands = ofpbuf_data(bands);
1851
1852 error = ofputil_pull_bands(&b, ofpbuf_size(&b), &mm->meter.n_bands, bands);
1853 if (error) {
1854 return error;
1855 }
1856 }
1857 return 0;
1858 }
1859
1860 void
1861 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1862 {
1863 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1864 *meter_id = ntohl(omr->meter_id);
1865 }
1866
1867 struct ofpbuf *
1868 ofputil_encode_meter_request(enum ofp_version ofp_version,
1869 enum ofputil_meter_request_type type,
1870 uint32_t meter_id)
1871 {
1872 struct ofpbuf *msg;
1873
1874 enum ofpraw raw;
1875
1876 switch (type) {
1877 case OFPUTIL_METER_CONFIG:
1878 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1879 break;
1880 case OFPUTIL_METER_STATS:
1881 raw = OFPRAW_OFPST13_METER_REQUEST;
1882 break;
1883 default:
1884 case OFPUTIL_METER_FEATURES:
1885 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1886 break;
1887 }
1888
1889 msg = ofpraw_alloc(raw, ofp_version, 0);
1890
1891 if (type != OFPUTIL_METER_FEATURES) {
1892 struct ofp13_meter_multipart_request *omr;
1893 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1894 omr->meter_id = htonl(meter_id);
1895 }
1896 return msg;
1897 }
1898
1899 static void
1900 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1901 struct ofpbuf *msg)
1902 {
1903 uint16_t n = 0;
1904
1905 for (n = 0; n < n_bands; ++n) {
1906 /* Currently all band types have same size. */
1907 struct ofp13_meter_band_dscp_remark *ombh;
1908 size_t ombh_len = sizeof *ombh;
1909
1910 ombh = ofpbuf_put_zeros(msg, ombh_len);
1911
1912 ombh->type = htons(mb->type);
1913 ombh->len = htons(ombh_len);
1914 ombh->rate = htonl(mb->rate);
1915 ombh->burst_size = htonl(mb->burst_size);
1916 ombh->prec_level = mb->prec_level;
1917
1918 mb++;
1919 }
1920 }
1921
1922 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1923 void
1924 ofputil_append_meter_config(struct list *replies,
1925 const struct ofputil_meter_config *mc)
1926 {
1927 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1928 size_t start_ofs = ofpbuf_size(msg);
1929 struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply);
1930 reply->flags = htons(mc->flags);
1931 reply->meter_id = htonl(mc->meter_id);
1932
1933 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1934
1935 reply->length = htons(ofpbuf_size(msg) - start_ofs);
1936
1937 ofpmp_postappend(replies, start_ofs);
1938 }
1939
1940 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1941 void
1942 ofputil_append_meter_stats(struct list *replies,
1943 const struct ofputil_meter_stats *ms)
1944 {
1945 struct ofp13_meter_stats *reply;
1946 uint16_t n = 0;
1947 uint16_t len;
1948
1949 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1950 reply = ofpmp_append(replies, len);
1951
1952 reply->meter_id = htonl(ms->meter_id);
1953 reply->len = htons(len);
1954 memset(reply->pad, 0, sizeof reply->pad);
1955 reply->flow_count = htonl(ms->flow_count);
1956 reply->packet_in_count = htonll(ms->packet_in_count);
1957 reply->byte_in_count = htonll(ms->byte_in_count);
1958 reply->duration_sec = htonl(ms->duration_sec);
1959 reply->duration_nsec = htonl(ms->duration_nsec);
1960
1961 for (n = 0; n < ms->n_bands; ++n) {
1962 const struct ofputil_meter_band_stats *src = &ms->bands[n];
1963 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1964
1965 dst->packet_band_count = htonll(src->packet_count);
1966 dst->byte_band_count = htonll(src->byte_count);
1967 }
1968 }
1969
1970 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1971 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1972 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1973 * it across the call.
1974 *
1975 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1976 * message. Calling this function multiple times for a single 'msg' iterates
1977 * through the replies. 'bands' is cleared for each reply.
1978 *
1979 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1980 * otherwise a positive errno value. */
1981 int
1982 ofputil_decode_meter_config(struct ofpbuf *msg,
1983 struct ofputil_meter_config *mc,
1984 struct ofpbuf *bands)
1985 {
1986 const struct ofp13_meter_config *omc;
1987 enum ofperr err;
1988
1989 /* Pull OpenFlow headers for the first call. */
1990 if (!msg->frame) {
1991 ofpraw_pull_assert(msg);
1992 }
1993
1994 if (!ofpbuf_size(msg)) {
1995 return EOF;
1996 }
1997
1998 omc = ofpbuf_try_pull(msg, sizeof *omc);
1999 if (!omc) {
2000 VLOG_WARN_RL(&bad_ofmsg_rl,
2001 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
2002 ofpbuf_size(msg));
2003 return OFPERR_OFPBRC_BAD_LEN;
2004 }
2005
2006 ofpbuf_clear(bands);
2007 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2008 &mc->n_bands, bands);
2009 if (err) {
2010 return err;
2011 }
2012 mc->meter_id = ntohl(omc->meter_id);
2013 mc->flags = ntohs(omc->flags);
2014 mc->bands = ofpbuf_data(bands);
2015
2016 return 0;
2017 }
2018
2019 static enum ofperr
2020 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2021 struct ofpbuf *bands)
2022 {
2023 const struct ofp13_meter_band_stats *ombs;
2024 struct ofputil_meter_band_stats *mbs;
2025 uint16_t n, i;
2026
2027 ombs = ofpbuf_try_pull(msg, len);
2028 if (!ombs) {
2029 return OFPERR_OFPBRC_BAD_LEN;
2030 }
2031
2032 n = len / sizeof *ombs;
2033 if (len != n * sizeof *ombs) {
2034 return OFPERR_OFPBRC_BAD_LEN;
2035 }
2036
2037 mbs = ofpbuf_put_uninit(bands, len);
2038
2039 for (i = 0; i < n; ++i) {
2040 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2041 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2042 }
2043 *n_bands = n;
2044 return 0;
2045 }
2046
2047 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2048 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2049 * decoded into 'bands'.
2050 *
2051 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2052 * message. Calling this function multiple times for a single 'msg' iterates
2053 * through the replies. 'bands' is cleared for each reply.
2054 *
2055 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2056 * otherwise a positive errno value. */
2057 int
2058 ofputil_decode_meter_stats(struct ofpbuf *msg,
2059 struct ofputil_meter_stats *ms,
2060 struct ofpbuf *bands)
2061 {
2062 const struct ofp13_meter_stats *oms;
2063 enum ofperr err;
2064
2065 /* Pull OpenFlow headers for the first call. */
2066 if (!msg->frame) {
2067 ofpraw_pull_assert(msg);
2068 }
2069
2070 if (!ofpbuf_size(msg)) {
2071 return EOF;
2072 }
2073
2074 oms = ofpbuf_try_pull(msg, sizeof *oms);
2075 if (!oms) {
2076 VLOG_WARN_RL(&bad_ofmsg_rl,
2077 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2078 ofpbuf_size(msg));
2079 return OFPERR_OFPBRC_BAD_LEN;
2080 }
2081
2082 ofpbuf_clear(bands);
2083 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2084 &ms->n_bands, bands);
2085 if (err) {
2086 return err;
2087 }
2088 ms->meter_id = ntohl(oms->meter_id);
2089 ms->flow_count = ntohl(oms->flow_count);
2090 ms->packet_in_count = ntohll(oms->packet_in_count);
2091 ms->byte_in_count = ntohll(oms->byte_in_count);
2092 ms->duration_sec = ntohl(oms->duration_sec);
2093 ms->duration_nsec = ntohl(oms->duration_nsec);
2094 ms->bands = ofpbuf_data(bands);
2095
2096 return 0;
2097 }
2098
2099 void
2100 ofputil_decode_meter_features(const struct ofp_header *oh,
2101 struct ofputil_meter_features *mf)
2102 {
2103 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2104
2105 mf->max_meters = ntohl(omf->max_meter);
2106 mf->band_types = ntohl(omf->band_types);
2107 mf->capabilities = ntohl(omf->capabilities);
2108 mf->max_bands = omf->max_bands;
2109 mf->max_color = omf->max_color;
2110 }
2111
2112 struct ofpbuf *
2113 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2114 const struct ofp_header *request)
2115 {
2116 struct ofpbuf *reply;
2117 struct ofp13_meter_features *omf;
2118
2119 reply = ofpraw_alloc_stats_reply(request, 0);
2120 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2121
2122 omf->max_meter = htonl(mf->max_meters);
2123 omf->band_types = htonl(mf->band_types);
2124 omf->capabilities = htonl(mf->capabilities);
2125 omf->max_bands = mf->max_bands;
2126 omf->max_color = mf->max_color;
2127
2128 return reply;
2129 }
2130
2131 struct ofpbuf *
2132 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2133 const struct ofputil_meter_mod *mm)
2134 {
2135 struct ofpbuf *msg;
2136
2137 struct ofp13_meter_mod *omm;
2138
2139 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2140 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2141 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2142 omm->command = htons(mm->command);
2143 if (mm->command != OFPMC13_DELETE) {
2144 omm->flags = htons(mm->meter.flags);
2145 }
2146 omm->meter_id = htonl(mm->meter.meter_id);
2147
2148 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2149
2150 ofpmsg_update_length(msg);
2151 return msg;
2152 }
2153
2154 static ovs_be16
2155 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2156 enum ofputil_protocol protocol)
2157 {
2158 return htons(protocol & OFPUTIL_P_TID
2159 ? (fm->command & 0xff) | (fm->table_id << 8)
2160 : fm->command);
2161 }
2162
2163 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2164 * 'protocol' and returns the message. */
2165 struct ofpbuf *
2166 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2167 enum ofputil_protocol protocol)
2168 {
2169 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2170 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2171 struct ofpbuf *msg;
2172
2173 switch (protocol) {
2174 case OFPUTIL_P_OF11_STD:
2175 case OFPUTIL_P_OF12_OXM:
2176 case OFPUTIL_P_OF13_OXM:
2177 case OFPUTIL_P_OF14_OXM:
2178 case OFPUTIL_P_OF15_OXM: {
2179 struct ofp11_flow_mod *ofm;
2180 int tailroom;
2181
2182 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2183 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2184 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2185 if ((protocol == OFPUTIL_P_OF11_STD
2186 && (fm->command == OFPFC_MODIFY ||
2187 fm->command == OFPFC_MODIFY_STRICT)
2188 && fm->cookie_mask == htonll(0))
2189 || fm->command == OFPFC_ADD) {
2190 ofm->cookie = fm->new_cookie;
2191 } else {
2192 ofm->cookie = fm->cookie;
2193 }
2194 ofm->cookie_mask = fm->cookie_mask;
2195 if (fm->table_id != OFPTT_ALL
2196 || (protocol != OFPUTIL_P_OF11_STD
2197 && (fm->command == OFPFC_DELETE ||
2198 fm->command == OFPFC_DELETE_STRICT))) {
2199 ofm->table_id = fm->table_id;
2200 } else {
2201 ofm->table_id = 0;
2202 }
2203 ofm->command = fm->command;
2204 ofm->idle_timeout = htons(fm->idle_timeout);
2205 ofm->hard_timeout = htons(fm->hard_timeout);
2206 ofm->priority = htons(fm->priority);
2207 ofm->buffer_id = htonl(fm->buffer_id);
2208 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2209 ofm->out_group = htonl(fm->out_group);
2210 ofm->flags = raw_flags;
2211 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2212 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2213 version);
2214 break;
2215 }
2216
2217 case OFPUTIL_P_OF10_STD:
2218 case OFPUTIL_P_OF10_STD_TID: {
2219 struct ofp10_flow_mod *ofm;
2220
2221 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2222 fm->ofpacts_len);
2223 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2224 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2225 ofm->cookie = fm->new_cookie;
2226 ofm->command = ofputil_tid_command(fm, protocol);
2227 ofm->idle_timeout = htons(fm->idle_timeout);
2228 ofm->hard_timeout = htons(fm->hard_timeout);
2229 ofm->priority = htons(fm->priority);
2230 ofm->buffer_id = htonl(fm->buffer_id);
2231 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2232 ofm->flags = raw_flags;
2233 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2234 version);
2235 break;
2236 }
2237
2238 case OFPUTIL_P_OF10_NXM:
2239 case OFPUTIL_P_OF10_NXM_TID: {
2240 struct nx_flow_mod *nfm;
2241 int match_len;
2242
2243 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2244 NXM_TYPICAL_LEN + fm->ofpacts_len);
2245 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2246 nfm->command = ofputil_tid_command(fm, protocol);
2247 nfm->cookie = fm->new_cookie;
2248 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2249 nfm = ofpbuf_l3(msg);
2250 nfm->idle_timeout = htons(fm->idle_timeout);
2251 nfm->hard_timeout = htons(fm->hard_timeout);
2252 nfm->priority = htons(fm->priority);
2253 nfm->buffer_id = htonl(fm->buffer_id);
2254 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2255 nfm->flags = raw_flags;
2256 nfm->match_len = htons(match_len);
2257 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2258 version);
2259 break;
2260 }
2261
2262 default:
2263 OVS_NOT_REACHED();
2264 }
2265
2266 ofpmsg_update_length(msg);
2267 return msg;
2268 }
2269
2270 static enum ofperr
2271 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2272 const struct ofp10_flow_stats_request *ofsr,
2273 bool aggregate)
2274 {
2275 fsr->aggregate = aggregate;
2276 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2277 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2278 fsr->out_group = OFPG11_ANY;
2279 fsr->table_id = ofsr->table_id;
2280 fsr->cookie = fsr->cookie_mask = htonll(0);
2281
2282 return 0;
2283 }
2284
2285 static enum ofperr
2286 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2287 struct ofpbuf *b, bool aggregate)
2288 {
2289 const struct ofp11_flow_stats_request *ofsr;
2290 enum ofperr error;
2291
2292 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2293 fsr->aggregate = aggregate;
2294 fsr->table_id = ofsr->table_id;
2295 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2296 if (error) {
2297 return error;
2298 }
2299 fsr->out_group = ntohl(ofsr->out_group);
2300 fsr->cookie = ofsr->cookie;
2301 fsr->cookie_mask = ofsr->cookie_mask;
2302 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2303 if (error) {
2304 return error;
2305 }
2306
2307 return 0;
2308 }
2309
2310 static enum ofperr
2311 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2312 struct ofpbuf *b, bool aggregate)
2313 {
2314 const struct nx_flow_stats_request *nfsr;
2315 enum ofperr error;
2316
2317 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2318 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2319 &fsr->cookie, &fsr->cookie_mask);
2320 if (error) {
2321 return error;
2322 }
2323 if (ofpbuf_size(b)) {
2324 return OFPERR_OFPBRC_BAD_LEN;
2325 }
2326
2327 fsr->aggregate = aggregate;
2328 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2329 fsr->out_group = OFPG11_ANY;
2330 fsr->table_id = nfsr->table_id;
2331
2332 return 0;
2333 }
2334
2335 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2336 * 'port', suitable for OpenFlow version 'version'. */
2337 struct ofpbuf *
2338 ofputil_encode_queue_get_config_request(enum ofp_version version,
2339 ofp_port_t port)
2340 {
2341 struct ofpbuf *request;
2342
2343 if (version == OFP10_VERSION) {
2344 struct ofp10_queue_get_config_request *qgcr10;
2345
2346 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2347 version, 0);
2348 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2349 qgcr10->port = htons(ofp_to_u16(port));
2350 } else {
2351 struct ofp11_queue_get_config_request *qgcr11;
2352
2353 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2354 version, 0);
2355 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2356 qgcr11->port = ofputil_port_to_ofp11(port);
2357 }
2358
2359 return request;
2360 }
2361
2362 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2363 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2364 * code. */
2365 enum ofperr
2366 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2367 ofp_port_t *port)
2368 {
2369 const struct ofp10_queue_get_config_request *qgcr10;
2370 const struct ofp11_queue_get_config_request *qgcr11;
2371 enum ofpraw raw;
2372 struct ofpbuf b;
2373
2374 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2375 raw = ofpraw_pull_assert(&b);
2376
2377 switch ((int) raw) {
2378 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2379 qgcr10 = ofpbuf_data(&b);
2380 *port = u16_to_ofp(ntohs(qgcr10->port));
2381 return 0;
2382
2383 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2384 qgcr11 = ofpbuf_data(&b);
2385 return ofputil_port_from_ofp11(qgcr11->port, port);
2386 }
2387
2388 OVS_NOT_REACHED();
2389 }
2390
2391 /* Constructs and returns the beginning of a reply to
2392 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2393 * individual queues with ofputil_append_queue_get_config_reply(). */
2394 struct ofpbuf *
2395 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2396 {
2397 struct ofp10_queue_get_config_reply *qgcr10;
2398 struct ofp11_queue_get_config_reply *qgcr11;
2399 struct ofpbuf *reply;
2400 enum ofperr error;
2401 struct ofpbuf b;
2402 enum ofpraw raw;
2403 ofp_port_t port;
2404
2405 error = ofputil_decode_queue_get_config_request(oh, &port);
2406 ovs_assert(!error);
2407
2408 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2409 raw = ofpraw_pull_assert(&b);
2410
2411 switch ((int) raw) {
2412 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2413 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2414 oh, 0);
2415 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2416 qgcr10->port = htons(ofp_to_u16(port));
2417 break;
2418
2419 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2420 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2421 oh, 0);
2422 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2423 qgcr11->port = ofputil_port_to_ofp11(port);
2424 break;
2425
2426 default:
2427 OVS_NOT_REACHED();
2428 }
2429
2430 return reply;
2431 }
2432
2433 static void
2434 put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property,
2435 uint16_t rate)
2436 {
2437 if (rate != UINT16_MAX) {
2438 struct ofp_queue_prop_rate *oqpr;
2439
2440 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2441 oqpr->prop_header.property = htons(property);
2442 oqpr->prop_header.len = htons(sizeof *oqpr);
2443 oqpr->rate = htons(rate);
2444 }
2445 }
2446
2447 /* Appends a queue description for 'queue_id' to the
2448 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2449 void
2450 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2451 const struct ofputil_queue_config *oqc)
2452 {
2453 const struct ofp_header *oh = ofpbuf_data(reply);
2454 size_t start_ofs, len_ofs;
2455 ovs_be16 *len;
2456
2457 start_ofs = ofpbuf_size(reply);
2458 if (oh->version < OFP12_VERSION) {
2459 struct ofp10_packet_queue *opq10;
2460
2461 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2462 opq10->queue_id = htonl(oqc->queue_id);
2463 len_ofs = (char *) &opq10->len - (char *) ofpbuf_data(reply);
2464 } else {
2465 struct ofp11_queue_get_config_reply *qgcr11;
2466 struct ofp12_packet_queue *opq12;
2467 ovs_be32 port;
2468
2469 qgcr11 = ofpbuf_l3(reply);
2470 port = qgcr11->port;
2471
2472 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2473 opq12->port = port;
2474 opq12->queue_id = htonl(oqc->queue_id);
2475 len_ofs = (char *) &opq12->len - (char *) ofpbuf_data(reply);
2476 }
2477
2478 put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate);
2479 put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate);
2480
2481 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2482 *len = htons(ofpbuf_size(reply) - start_ofs);
2483 }
2484
2485 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2486 * stores in '*port' the port that the reply is about. The caller may call
2487 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2488 * queues included in the reply. Returns 0 if successful, otherwise an
2489 * ofperr.*/
2490 enum ofperr
2491 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2492 {
2493 const struct ofp10_queue_get_config_reply *qgcr10;
2494 const struct ofp11_queue_get_config_reply *qgcr11;
2495 enum ofpraw raw;
2496
2497 raw = ofpraw_pull_assert(reply);
2498 switch ((int) raw) {
2499 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2500 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2501 *port = u16_to_ofp(ntohs(qgcr10->port));
2502 return 0;
2503
2504 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2505 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2506 return ofputil_port_from_ofp11(qgcr11->port, port);
2507 }
2508
2509 OVS_NOT_REACHED();
2510 }
2511
2512 static enum ofperr
2513 parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate)
2514 {
2515 const struct ofp_queue_prop_rate *oqpr;
2516
2517 if (hdr->len == htons(sizeof *oqpr)) {
2518 oqpr = (const struct ofp_queue_prop_rate *) hdr;
2519 *rate = ntohs(oqpr->rate);
2520 return 0;
2521 } else {
2522 return OFPERR_OFPBRC_BAD_LEN;
2523 }
2524 }
2525
2526 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2527 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2528 * must already have pulled off the main header.
2529 *
2530 * This function returns EOF if the last queue has already been decoded, 0 if a
2531 * queue was successfully decoded into '*queue', or an ofperr if there was a
2532 * problem decoding 'reply'. */
2533 int
2534 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2535 struct ofputil_queue_config *queue)
2536 {
2537 const struct ofp_header *oh;
2538 unsigned int opq_len;
2539 unsigned int len;
2540
2541 if (!ofpbuf_size(reply)) {
2542 return EOF;
2543 }
2544
2545 queue->min_rate = UINT16_MAX;
2546 queue->max_rate = UINT16_MAX;
2547
2548 oh = reply->frame;
2549 if (oh->version < OFP12_VERSION) {
2550 const struct ofp10_packet_queue *opq10;
2551
2552 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2553 if (!opq10) {
2554 return OFPERR_OFPBRC_BAD_LEN;
2555 }
2556 queue->queue_id = ntohl(opq10->queue_id);
2557 len = ntohs(opq10->len);
2558 opq_len = sizeof *opq10;
2559 } else {
2560 const struct ofp12_packet_queue *opq12;
2561
2562 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2563 if (!opq12) {
2564 return OFPERR_OFPBRC_BAD_LEN;
2565 }
2566 queue->queue_id = ntohl(opq12->queue_id);
2567 len = ntohs(opq12->len);
2568 opq_len = sizeof *opq12;
2569 }
2570
2571 if (len < opq_len || len > ofpbuf_size(reply) + opq_len || len % 8) {
2572 return OFPERR_OFPBRC_BAD_LEN;
2573 }
2574 len -= opq_len;
2575
2576 while (len > 0) {
2577 const struct ofp_queue_prop_header *hdr;
2578 unsigned int property;
2579 unsigned int prop_len;
2580 enum ofperr error = 0;
2581
2582 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2583 prop_len = ntohs(hdr->len);
2584 if (prop_len < sizeof *hdr || prop_len > ofpbuf_size(reply) || prop_len % 8) {
2585 return OFPERR_OFPBRC_BAD_LEN;
2586 }
2587
2588 property = ntohs(hdr->property);
2589 switch (property) {
2590 case OFPQT_MIN_RATE:
2591 error = parse_queue_rate(hdr, &queue->min_rate);
2592 break;
2593
2594 case OFPQT_MAX_RATE:
2595 error = parse_queue_rate(hdr, &queue->max_rate);
2596 break;
2597
2598 default:
2599 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2600 break;
2601 }
2602 if (error) {
2603 return error;
2604 }
2605
2606 ofpbuf_pull(reply, prop_len);
2607 len -= prop_len;
2608 }
2609 return 0;
2610 }
2611
2612 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2613 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2614 * successful, otherwise an OpenFlow error code. */
2615 enum ofperr
2616 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2617 const struct ofp_header *oh)
2618 {
2619 enum ofpraw raw;
2620 struct ofpbuf b;
2621
2622 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2623 raw = ofpraw_pull_assert(&b);
2624 switch ((int) raw) {
2625 case OFPRAW_OFPST10_FLOW_REQUEST:
2626 return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), false);
2627
2628 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2629 return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), true);
2630
2631 case OFPRAW_OFPST11_FLOW_REQUEST:
2632 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2633
2634 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2635 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2636
2637 case OFPRAW_NXST_FLOW_REQUEST:
2638 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2639
2640 case OFPRAW_NXST_AGGREGATE_REQUEST:
2641 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2642
2643 default:
2644 /* Hey, the caller lied. */
2645 OVS_NOT_REACHED();
2646 }
2647 }
2648
2649 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2650 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2651 * 'protocol', and returns the message. */
2652 struct ofpbuf *
2653 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2654 enum ofputil_protocol protocol)
2655 {
2656 struct ofpbuf *msg;
2657 enum ofpraw raw;
2658
2659 switch (protocol) {
2660 case OFPUTIL_P_OF11_STD:
2661 case OFPUTIL_P_OF12_OXM:
2662 case OFPUTIL_P_OF13_OXM:
2663 case OFPUTIL_P_OF14_OXM:
2664 case OFPUTIL_P_OF15_OXM: {
2665 struct ofp11_flow_stats_request *ofsr;
2666
2667 raw = (fsr->aggregate
2668 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2669 : OFPRAW_OFPST11_FLOW_REQUEST);
2670 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2671 ofputil_match_typical_len(protocol));
2672 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2673 ofsr->table_id = fsr->table_id;
2674 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2675 ofsr->out_group = htonl(fsr->out_group);
2676 ofsr->cookie = fsr->cookie;
2677 ofsr->cookie_mask = fsr->cookie_mask;
2678 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2679 break;
2680 }
2681
2682 case OFPUTIL_P_OF10_STD:
2683 case OFPUTIL_P_OF10_STD_TID: {
2684 struct ofp10_flow_stats_request *ofsr;
2685
2686 raw = (fsr->aggregate
2687 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2688 : OFPRAW_OFPST10_FLOW_REQUEST);
2689 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2690 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2691 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2692 ofsr->table_id = fsr->table_id;
2693 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2694 break;
2695 }
2696
2697 case OFPUTIL_P_OF10_NXM:
2698 case OFPUTIL_P_OF10_NXM_TID: {
2699 struct nx_flow_stats_request *nfsr;
2700 int match_len;
2701
2702 raw = (fsr->aggregate
2703 ? OFPRAW_NXST_AGGREGATE_REQUEST
2704 : OFPRAW_NXST_FLOW_REQUEST);
2705 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2706 ofpbuf_put_zeros(msg, sizeof *nfsr);
2707 match_len = nx_put_match(msg, &fsr->match,
2708 fsr->cookie, fsr->cookie_mask);
2709
2710 nfsr = ofpbuf_l3(msg);
2711 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2712 nfsr->match_len = htons(match_len);
2713 nfsr->table_id = fsr->table_id;
2714 break;
2715 }
2716
2717 default:
2718 OVS_NOT_REACHED();
2719 }
2720
2721 return msg;
2722 }
2723
2724 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2725 * ofputil_flow_stats in 'fs'.
2726 *
2727 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2728 * OpenFlow message. Calling this function multiple times for a single 'msg'
2729 * iterates through the replies. The caller must initially leave 'msg''s layer
2730 * pointers null and not modify them between calls.
2731 *
2732 * Most switches don't send the values needed to populate fs->idle_age and
2733 * fs->hard_age, so those members will usually be set to 0. If the switch from
2734 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2735 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2736 * 'idle_age' and 'hard_age' members in 'fs'.
2737 *
2738 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2739 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2740 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2741 *
2742 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2743 * otherwise a positive errno value. */
2744 int
2745 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2746 struct ofpbuf *msg,
2747 bool flow_age_extension,
2748 struct ofpbuf *ofpacts)
2749 {
2750 const struct ofp_header *oh;
2751 enum ofperr error;
2752 enum ofpraw raw;
2753
2754 error = (msg->frame
2755 ? ofpraw_decode(&raw, msg->frame)
2756 : ofpraw_pull(&raw, msg));
2757 if (error) {
2758 return error;
2759 }
2760 oh = msg->frame;
2761
2762 if (!ofpbuf_size(msg)) {
2763 return EOF;
2764 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2765 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2766 const struct ofp11_flow_stats *ofs;
2767 size_t length;
2768 uint16_t padded_match_len;
2769
2770 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2771 if (!ofs) {
2772 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2773 "bytes at end", ofpbuf_size(msg));
2774 return EINVAL;
2775 }
2776
2777 length = ntohs(ofs->length);
2778 if (length < sizeof *ofs) {
2779 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2780 "length %"PRIuSIZE, length);
2781 return EINVAL;
2782 }
2783
2784 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2785 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2786 return EINVAL;
2787 }
2788
2789 if (ofpacts_pull_openflow_instructions(msg, length - sizeof *ofs -
2790 padded_match_len, oh->version,
2791 ofpacts)) {
2792 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2793 return EINVAL;
2794 }
2795
2796 fs->priority = ntohs(ofs->priority);
2797 fs->table_id = ofs->table_id;
2798 fs->duration_sec = ntohl(ofs->duration_sec);
2799 fs->duration_nsec = ntohl(ofs->duration_nsec);
2800 fs->idle_timeout = ntohs(ofs->idle_timeout);
2801 fs->hard_timeout = ntohs(ofs->hard_timeout);
2802 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2803 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2804 &fs->flags);
2805 if (error) {
2806 return error;
2807 }
2808 } else {
2809 fs->flags = 0;
2810 }
2811 fs->idle_age = -1;
2812 fs->hard_age = -1;
2813 fs->cookie = ofs->cookie;
2814 fs->packet_count = ntohll(ofs->packet_count);
2815 fs->byte_count = ntohll(ofs->byte_count);
2816 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2817 const struct ofp10_flow_stats *ofs;
2818 size_t length;
2819
2820 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2821 if (!ofs) {
2822 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2823 "bytes at end", ofpbuf_size(msg));
2824 return EINVAL;
2825 }
2826
2827 length = ntohs(ofs->length);
2828 if (length < sizeof *ofs) {
2829 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2830 "length %"PRIuSIZE, length);
2831 return EINVAL;
2832 }
2833
2834 if (ofpacts_pull_openflow_actions(msg, length - sizeof *ofs,
2835 oh->version, ofpacts)) {
2836 return EINVAL;
2837 }
2838
2839 fs->cookie = get_32aligned_be64(&ofs->cookie);
2840 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2841 fs->priority = ntohs(ofs->priority);
2842 fs->table_id = ofs->table_id;
2843 fs->duration_sec = ntohl(ofs->duration_sec);
2844 fs->duration_nsec = ntohl(ofs->duration_nsec);
2845 fs->idle_timeout = ntohs(ofs->idle_timeout);
2846 fs->hard_timeout = ntohs(ofs->hard_timeout);
2847 fs->idle_age = -1;
2848 fs->hard_age = -1;
2849 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2850 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2851 fs->flags = 0;
2852 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2853 const struct nx_flow_stats *nfs;
2854 size_t match_len, actions_len, length;
2855
2856 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2857 if (!nfs) {
2858 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2859 "bytes at end", ofpbuf_size(msg));
2860 return EINVAL;
2861 }
2862
2863 length = ntohs(nfs->length);
2864 match_len = ntohs(nfs->match_len);
2865 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2866 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2867 "claims invalid length %"PRIuSIZE, match_len, length);
2868 return EINVAL;
2869 }
2870 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2871 return EINVAL;
2872 }
2873
2874 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2875 if (ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
2876 ofpacts)) {
2877 return EINVAL;
2878 }
2879
2880 fs->cookie = nfs->cookie;
2881 fs->table_id = nfs->table_id;
2882 fs->duration_sec = ntohl(nfs->duration_sec);
2883 fs->duration_nsec = ntohl(nfs->duration_nsec);
2884 fs->priority = ntohs(nfs->priority);
2885 fs->idle_timeout = ntohs(nfs->idle_timeout);
2886 fs->hard_timeout = ntohs(nfs->hard_timeout);
2887 fs->idle_age = -1;
2888 fs->hard_age = -1;
2889 if (flow_age_extension) {
2890 if (nfs->idle_age) {
2891 fs->idle_age = ntohs(nfs->idle_age) - 1;
2892 }
2893 if (nfs->hard_age) {
2894 fs->hard_age = ntohs(nfs->hard_age) - 1;
2895 }
2896 }
2897 fs->packet_count = ntohll(nfs->packet_count);
2898 fs->byte_count = ntohll(nfs->byte_count);
2899 fs->flags = 0;
2900 } else {
2901 OVS_NOT_REACHED();
2902 }
2903
2904 fs->ofpacts = ofpbuf_data(ofpacts);
2905 fs->ofpacts_len = ofpbuf_size(ofpacts);
2906
2907 return 0;
2908 }
2909
2910 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2911 *
2912 * We use this in situations where OVS internally uses UINT64_MAX to mean
2913 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2914 static uint64_t
2915 unknown_to_zero(uint64_t count)
2916 {
2917 return count != UINT64_MAX ? count : 0;
2918 }
2919
2920 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2921 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2922 * have been initialized with ofpmp_init(). */
2923 void
2924 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2925 struct list *replies)
2926 {
2927 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2928 size_t start_ofs = ofpbuf_size(reply);
2929 enum ofp_version version = ofpmp_version(replies);
2930 enum ofpraw raw = ofpmp_decode_raw(replies);
2931
2932 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2933 struct ofp11_flow_stats *ofs;
2934
2935 ofpbuf_put_uninit(reply, sizeof *ofs);
2936 oxm_put_match(reply, &fs->match, version);
2937 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
2938 version);
2939
2940 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2941 ofs->length = htons(ofpbuf_size(reply) - start_ofs);
2942 ofs->table_id = fs->table_id;
2943 ofs->pad = 0;
2944 ofs->duration_sec = htonl(fs->duration_sec);
2945 ofs->duration_nsec = htonl(fs->duration_nsec);
2946 ofs->priority = htons(fs->priority);
2947 ofs->idle_timeout = htons(fs->idle_timeout);
2948 ofs->hard_timeout = htons(fs->hard_timeout);
2949 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2950 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
2951 } else {
2952 ofs->flags = 0;
2953 }
2954 memset(ofs->pad2, 0, sizeof ofs->pad2);
2955 ofs->cookie = fs->cookie;
2956 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2957 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2958 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2959 struct ofp10_flow_stats *ofs;
2960
2961 ofpbuf_put_uninit(reply, sizeof *ofs);
2962 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
2963 version);
2964 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2965 ofs->length = htons(ofpbuf_size(reply) - start_ofs);
2966 ofs->table_id = fs->table_id;
2967 ofs->pad = 0;
2968 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2969 ofs->duration_sec = htonl(fs->duration_sec);
2970 ofs->duration_nsec = htonl(fs->duration_nsec);
2971 ofs->priority = htons(fs->priority);
2972 ofs->idle_timeout = htons(fs->idle_timeout);
2973 ofs->hard_timeout = htons(fs->hard_timeout);
2974 memset(ofs->pad2, 0, sizeof ofs->pad2);
2975 put_32aligned_be64(&ofs->cookie, fs->cookie);
2976 put_32aligned_be64(&ofs->packet_count,
2977 htonll(unknown_to_zero(fs->packet_count)));
2978 put_32aligned_be64(&ofs->byte_count,
2979 htonll(unknown_to_zero(fs->byte_count)));
2980 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2981 struct nx_flow_stats *nfs;
2982 int match_len;
2983
2984 ofpbuf_put_uninit(reply, sizeof *nfs);
2985 match_len = nx_put_match(reply, &fs->match, 0, 0);
2986 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
2987 version);
2988 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
2989 nfs->length = htons(ofpbuf_size(reply) - start_ofs);
2990 nfs->table_id = fs->table_id;
2991 nfs->pad = 0;
2992 nfs->duration_sec = htonl(fs->duration_sec);
2993 nfs->duration_nsec = htonl(fs->duration_nsec);
2994 nfs->priority = htons(fs->priority);
2995 nfs->idle_timeout = htons(fs->idle_timeout);
2996 nfs->hard_timeout = htons(fs->hard_timeout);
2997 nfs->idle_age = htons(fs->idle_age < 0 ? 0
2998 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
2999 : UINT16_MAX);
3000 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3001 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3002 : UINT16_MAX);
3003 nfs->match_len = htons(match_len);
3004 nfs->cookie = fs->cookie;
3005 nfs->packet_count = htonll(fs->packet_count);
3006 nfs->byte_count = htonll(fs->byte_count);
3007 } else {
3008 OVS_NOT_REACHED();
3009 }
3010
3011 ofpmp_postappend(replies, start_ofs);
3012 }
3013
3014 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3015 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3016 struct ofpbuf *
3017 ofputil_encode_aggregate_stats_reply(
3018 const struct ofputil_aggregate_stats *stats,
3019 const struct ofp_header *request)
3020 {
3021 struct ofp_aggregate_stats_reply *asr;
3022 uint64_t packet_count;
3023 uint64_t byte_count;
3024 struct ofpbuf *msg;
3025 enum ofpraw raw;
3026
3027 ofpraw_decode(&raw, request);
3028 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3029 packet_count = unknown_to_zero(stats->packet_count);
3030 byte_count = unknown_to_zero(stats->byte_count);
3031 } else {
3032 packet_count = stats->packet_count;
3033 byte_count = stats->byte_count;
3034 }
3035
3036 msg = ofpraw_alloc_stats_reply(request, 0);
3037 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3038 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3039 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3040 asr->flow_count = htonl(stats->flow_count);
3041
3042 return msg;
3043 }
3044
3045 enum ofperr
3046 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3047 const struct ofp_header *reply)
3048 {
3049 struct ofp_aggregate_stats_reply *asr;
3050 struct ofpbuf msg;
3051
3052 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3053 ofpraw_pull_assert(&msg);
3054
3055 asr = ofpbuf_l3(&msg);
3056 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3057 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3058 stats->flow_count = ntohl(asr->flow_count);
3059
3060 return 0;
3061 }
3062
3063 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3064 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3065 * an OpenFlow error code. */
3066 enum ofperr
3067 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3068 const struct ofp_header *oh)
3069 {
3070 enum ofpraw raw;
3071 struct ofpbuf b;
3072
3073 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3074 raw = ofpraw_pull_assert(&b);
3075 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3076 const struct ofp12_flow_removed *ofr;
3077 enum ofperr error;
3078
3079 ofr = ofpbuf_pull(&b, sizeof *ofr);
3080
3081 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3082 if (error) {
3083 return error;
3084 }
3085
3086 fr->priority = ntohs(ofr->priority);
3087 fr->cookie = ofr->cookie;
3088 fr->reason = ofr->reason;
3089 fr->table_id = ofr->table_id;
3090 fr->duration_sec = ntohl(ofr->duration_sec);
3091 fr->duration_nsec = ntohl(ofr->duration_nsec);
3092 fr->idle_timeout = ntohs(ofr->idle_timeout);
3093 fr->hard_timeout = ntohs(ofr->hard_timeout);
3094 fr->packet_count = ntohll(ofr->packet_count);
3095 fr->byte_count = ntohll(ofr->byte_count);
3096 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3097 const struct ofp10_flow_removed *ofr;
3098
3099 ofr = ofpbuf_pull(&b, sizeof *ofr);
3100
3101 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3102 fr->priority = ntohs(ofr->priority);
3103 fr->cookie = ofr->cookie;
3104 fr->reason = ofr->reason;
3105 fr->table_id = 255;
3106 fr->duration_sec = ntohl(ofr->duration_sec);
3107 fr->duration_nsec = ntohl(ofr->duration_nsec);
3108 fr->idle_timeout = ntohs(ofr->idle_timeout);
3109 fr->hard_timeout = 0;
3110 fr->packet_count = ntohll(ofr->packet_count);
3111 fr->byte_count = ntohll(ofr->byte_count);
3112 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3113 struct nx_flow_removed *nfr;
3114 enum ofperr error;
3115
3116 nfr = ofpbuf_pull(&b, sizeof *nfr);
3117 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3118 NULL, NULL);
3119 if (error) {
3120 return error;
3121 }
3122 if (ofpbuf_size(&b)) {
3123 return OFPERR_OFPBRC_BAD_LEN;
3124 }
3125
3126 fr->priority = ntohs(nfr->priority);
3127 fr->cookie = nfr->cookie;
3128 fr->reason = nfr->reason;
3129 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3130 fr->duration_sec = ntohl(nfr->duration_sec);
3131 fr->duration_nsec = ntohl(nfr->duration_nsec);
3132 fr->idle_timeout = ntohs(nfr->idle_timeout);
3133 fr->hard_timeout = 0;
3134 fr->packet_count = ntohll(nfr->packet_count);
3135 fr->byte_count = ntohll(nfr->byte_count);
3136 } else {
3137 OVS_NOT_REACHED();
3138 }
3139
3140 return 0;
3141 }
3142
3143 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3144 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3145 * message. */
3146 struct ofpbuf *
3147 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3148 enum ofputil_protocol protocol)
3149 {
3150 struct ofpbuf *msg;
3151
3152 switch (protocol) {
3153 case OFPUTIL_P_OF11_STD:
3154 case OFPUTIL_P_OF12_OXM:
3155 case OFPUTIL_P_OF13_OXM:
3156 case OFPUTIL_P_OF14_OXM:
3157 case OFPUTIL_P_OF15_OXM: {
3158 struct ofp12_flow_removed *ofr;
3159
3160 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3161 ofputil_protocol_to_ofp_version(protocol),
3162 htonl(0),
3163 ofputil_match_typical_len(protocol));
3164 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3165 ofr->cookie = fr->cookie;
3166 ofr->priority = htons(fr->priority);
3167 ofr->reason = fr->reason;
3168 ofr->table_id = fr->table_id;
3169 ofr->duration_sec = htonl(fr->duration_sec);
3170 ofr->duration_nsec = htonl(fr->duration_nsec);
3171 ofr->idle_timeout = htons(fr->idle_timeout);
3172 ofr->hard_timeout = htons(fr->hard_timeout);
3173 ofr->packet_count = htonll(fr->packet_count);
3174 ofr->byte_count = htonll(fr->byte_count);
3175 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3176 break;
3177 }
3178
3179 case OFPUTIL_P_OF10_STD:
3180 case OFPUTIL_P_OF10_STD_TID: {
3181 struct ofp10_flow_removed *ofr;
3182
3183 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3184 htonl(0), 0);
3185 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3186 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3187 ofr->cookie = fr->cookie;
3188 ofr->priority = htons(fr->priority);
3189 ofr->reason = fr->reason;
3190 ofr->duration_sec = htonl(fr->duration_sec);
3191 ofr->duration_nsec = htonl(fr->duration_nsec);
3192 ofr->idle_timeout = htons(fr->idle_timeout);
3193 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3194 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3195 break;
3196 }
3197
3198 case OFPUTIL_P_OF10_NXM:
3199 case OFPUTIL_P_OF10_NXM_TID: {
3200 struct nx_flow_removed *nfr;
3201 int match_len;
3202
3203 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3204 htonl(0), NXM_TYPICAL_LEN);
3205 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
3206 match_len = nx_put_match(msg, &fr->match, 0, 0);
3207
3208 nfr = ofpbuf_l3(msg);
3209 nfr->cookie = fr->cookie;
3210 nfr->priority = htons(fr->priority);
3211 nfr->reason = fr->reason;
3212 nfr->table_id = fr->table_id + 1;
3213 nfr->duration_sec = htonl(fr->duration_sec);
3214 nfr->duration_nsec = htonl(fr->duration_nsec);
3215 nfr->idle_timeout = htons(fr->idle_timeout);
3216 nfr->match_len = htons(match_len);
3217 nfr->packet_count = htonll(fr->packet_count);
3218 nfr->byte_count = htonll(fr->byte_count);
3219 break;
3220 }
3221
3222 default:
3223 OVS_NOT_REACHED();
3224 }
3225
3226 return msg;
3227 }
3228
3229 static void
3230 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
3231 struct match *match, struct ofpbuf *b)
3232 {
3233 pin->packet = ofpbuf_data(b);
3234 pin->packet_len = ofpbuf_size(b);
3235
3236 pin->fmd.in_port = match->flow.in_port.ofp_port;
3237 pin->fmd.tun_id = match->flow.tunnel.tun_id;
3238 pin->fmd.tun_src = match->flow.tunnel.ip_src;
3239 pin->fmd.tun_dst = match->flow.tunnel.ip_dst;
3240 pin->fmd.metadata = match->flow.metadata;
3241 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
3242 pin->fmd.pkt_mark = match->flow.pkt_mark;
3243 }
3244
3245 enum ofperr
3246 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3247 const struct ofp_header *oh)
3248 {
3249 enum ofpraw raw;
3250 struct ofpbuf b;
3251
3252 memset(pin, 0, sizeof *pin);
3253 pin->cookie = OVS_BE64_MAX;
3254
3255 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3256 raw = ofpraw_pull_assert(&b);
3257 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3258 const struct ofp13_packet_in *opi;
3259 struct match match;
3260 int error;
3261 size_t packet_in_size;
3262
3263 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3264 packet_in_size = sizeof (struct ofp12_packet_in);
3265 } else {
3266 packet_in_size = sizeof (struct ofp13_packet_in);
3267 }
3268
3269 opi = ofpbuf_pull(&b, packet_in_size);
3270 error = oxm_pull_match_loose(&b, &match);
3271 if (error) {
3272 return error;
3273 }
3274
3275 if (!ofpbuf_try_pull(&b, 2)) {
3276 return OFPERR_OFPBRC_BAD_LEN;
3277 }
3278
3279 pin->reason = opi->pi.reason;
3280 pin->table_id = opi->pi.table_id;
3281 pin->buffer_id = ntohl(opi->pi.buffer_id);
3282 pin->total_len = ntohs(opi->pi.total_len);
3283
3284 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3285 pin->cookie = opi->cookie;
3286 }
3287
3288 ofputil_decode_packet_in_finish(pin, &match, &b);
3289 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3290 const struct ofp10_packet_in *opi;
3291
3292 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3293
3294 pin->packet = opi->data;
3295 pin->packet_len = ofpbuf_size(&b);
3296
3297 pin->fmd.in_port = u16_to_ofp(ntohs(opi->in_port));
3298 pin->reason = opi->reason;
3299 pin->buffer_id = ntohl(opi->buffer_id);
3300 pin->total_len = ntohs(opi->total_len);
3301 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3302 const struct ofp11_packet_in *opi;
3303 enum ofperr error;
3304
3305 opi = ofpbuf_pull(&b, sizeof *opi);
3306
3307 pin->packet = ofpbuf_data(&b);
3308 pin->packet_len = ofpbuf_size(&b);
3309
3310 pin->buffer_id = ntohl(opi->buffer_id);
3311 error = ofputil_port_from_ofp11(opi->in_port, &pin->fmd.in_port);
3312 if (error) {
3313 return error;
3314 }
3315 pin->total_len = ntohs(opi->total_len);
3316 pin->reason = opi->reason;
3317 pin->table_id = opi->table_id;
3318 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3319 const struct nx_packet_in *npi;
3320 struct match match;
3321 int error;
3322
3323 npi = ofpbuf_pull(&b, sizeof *npi);
3324 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
3325 NULL);
3326 if (error) {
3327 return error;
3328 }
3329
3330 if (!ofpbuf_try_pull(&b, 2)) {
3331 return OFPERR_OFPBRC_BAD_LEN;
3332 }
3333
3334 pin->reason = npi->reason;
3335 pin->table_id = npi->table_id;
3336 pin->cookie = npi->cookie;
3337
3338 pin->buffer_id = ntohl(npi->buffer_id);
3339 pin->total_len = ntohs(npi->total_len);
3340
3341 ofputil_decode_packet_in_finish(pin, &match, &b);
3342 } else {
3343 OVS_NOT_REACHED();
3344 }
3345
3346 return 0;
3347 }
3348
3349 static void
3350 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
3351 struct match *match)
3352 {
3353 int i;
3354
3355 match_init_catchall(match);
3356 if (pin->fmd.tun_id != htonll(0)) {
3357 match_set_tun_id(match, pin->fmd.tun_id);
3358 }
3359 if (pin->fmd.tun_src != htonl(0)) {
3360 match_set_tun_src(match, pin->fmd.tun_src);
3361 }
3362 if (pin->fmd.tun_dst != htonl(0)) {
3363 match_set_tun_dst(match, pin->fmd.tun_dst);
3364 }
3365 if (pin->fmd.metadata != htonll(0)) {
3366 match_set_metadata(match, pin->fmd.metadata);
3367 }
3368
3369 for (i = 0; i < FLOW_N_REGS; i++) {
3370 if (pin->fmd.regs[i]) {
3371 match_set_reg(match, i, pin->fmd.regs[i]);
3372 }
3373 }
3374
3375 if (pin->fmd.pkt_mark != 0) {
3376 match_set_pkt_mark(match, pin->fmd.pkt_mark);
3377 }
3378
3379 match_set_in_port(match, pin->fmd.in_port);
3380 }
3381
3382 static struct ofpbuf *
3383 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3384 {
3385 struct ofp10_packet_in *opi;
3386 struct ofpbuf *packet;
3387
3388 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3389 htonl(0), pin->packet_len);
3390 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3391 opi->total_len = htons(pin->total_len);
3392 opi->in_port = htons(ofp_to_u16(pin->fmd.in_port));
3393 opi->reason = pin->reason;
3394 opi->buffer_id = htonl(pin->buffer_id);
3395
3396 ofpbuf_put(packet, pin->packet, pin->packet_len);
3397
3398 return packet;
3399 }
3400
3401 static struct ofpbuf *
3402 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3403 {
3404 struct nx_packet_in *npi;
3405 struct ofpbuf *packet;
3406 struct match match;
3407 size_t match_len;
3408
3409 ofputil_packet_in_to_match(pin, &match);
3410
3411 /* The final argument is just an estimate of the space required. */
3412 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3413 htonl(0), (sizeof(struct flow_metadata) * 2
3414 + 2 + pin->packet_len));
3415 ofpbuf_put_zeros(packet, sizeof *npi);
3416 match_len = nx_put_match(packet, &match, 0, 0);
3417 ofpbuf_put_zeros(packet, 2);
3418 ofpbuf_put(packet, pin->packet, pin->packet_len);
3419
3420 npi = ofpbuf_l3(packet);
3421 npi->buffer_id = htonl(pin->buffer_id);
3422 npi->total_len = htons(pin->total_len);
3423 npi->reason = pin->reason;
3424 npi->table_id = pin->table_id;
3425 npi->cookie = pin->cookie;
3426 npi->match_len = htons(match_len);
3427
3428 return packet;
3429 }
3430
3431 static struct ofpbuf *
3432 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3433 {
3434 struct ofp11_packet_in *opi;
3435 struct ofpbuf *packet;
3436
3437 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3438 htonl(0), pin->packet_len);
3439 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3440 opi->buffer_id = htonl(pin->buffer_id);
3441 opi->in_port = ofputil_port_to_ofp11(pin->fmd.in_port);
3442 opi->in_phy_port = opi->in_port;
3443 opi->total_len = htons(pin->total_len);
3444 opi->reason = pin->reason;
3445 opi->table_id = pin->table_id;
3446
3447 ofpbuf_put(packet, pin->packet, pin->packet_len);
3448
3449 return packet;
3450 }
3451
3452 static struct ofpbuf *
3453 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3454 enum ofputil_protocol protocol)
3455 {
3456 struct ofp13_packet_in *opi;
3457 struct match match;
3458 enum ofpraw packet_in_raw;
3459 enum ofp_version packet_in_version;
3460 size_t packet_in_size;
3461 struct ofpbuf *packet;
3462
3463 if (protocol == OFPUTIL_P_OF12_OXM) {
3464 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3465 packet_in_version = OFP12_VERSION;
3466 packet_in_size = sizeof (struct ofp12_packet_in);
3467 } else {
3468 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3469 packet_in_version = OFP13_VERSION;
3470 packet_in_size = sizeof (struct ofp13_packet_in);
3471 }
3472
3473 ofputil_packet_in_to_match(pin, &match);
3474
3475 /* The final argument is just an estimate of the space required. */
3476 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3477 htonl(0), (sizeof(struct flow_metadata) * 2
3478 + 2 + pin->packet_len));
3479 ofpbuf_put_zeros(packet, packet_in_size);
3480 oxm_put_match(packet, &match, ofputil_protocol_to_ofp_version(protocol));
3481 ofpbuf_put_zeros(packet, 2);
3482 ofpbuf_put(packet, pin->packet, pin->packet_len);
3483
3484 opi = ofpbuf_l3(packet);
3485 opi->pi.buffer_id = htonl(pin->buffer_id);
3486 opi->pi.total_len = htons(pin->total_len);
3487 opi->pi.reason = pin->reason;
3488 opi->pi.table_id = pin->table_id;
3489 if (protocol == OFPUTIL_P_OF13_OXM) {
3490 opi->cookie = pin->cookie;
3491 }
3492
3493 return packet;
3494 }
3495
3496 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3497 * in the format specified by 'packet_in_format'. */
3498 struct ofpbuf *
3499 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3500 enum ofputil_protocol protocol,
3501 enum nx_packet_in_format packet_in_format)
3502 {
3503 struct ofpbuf *packet;
3504
3505 switch (protocol) {
3506 case OFPUTIL_P_OF10_STD:
3507 case OFPUTIL_P_OF10_STD_TID:
3508 case OFPUTIL_P_OF10_NXM:
3509 case OFPUTIL_P_OF10_NXM_TID:
3510 packet = (packet_in_format == NXPIF_NXM
3511 ? ofputil_encode_nx_packet_in(pin)
3512 : ofputil_encode_ofp10_packet_in(pin));
3513 break;
3514
3515 case OFPUTIL_P_OF11_STD:
3516 packet = ofputil_encode_ofp11_packet_in(pin);
3517 break;
3518
3519 case OFPUTIL_P_OF12_OXM:
3520 case OFPUTIL_P_OF13_OXM:
3521 case OFPUTIL_P_OF14_OXM:
3522 case OFPUTIL_P_OF15_OXM:
3523 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3524 break;
3525
3526 default:
3527 OVS_NOT_REACHED();
3528 }
3529
3530 ofpmsg_update_length(packet);
3531 return packet;
3532 }
3533
3534 /* Returns a string form of 'reason'. The return value is either a statically
3535 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3536 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3537 const char *
3538 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3539 char *reasonbuf, size_t bufsize)
3540 {
3541 switch (reason) {
3542 case OFPR_NO_MATCH:
3543 return "no_match";
3544 case OFPR_ACTION:
3545 return "action";
3546 case OFPR_INVALID_TTL:
3547 return "invalid_ttl";
3548
3549 case OFPR_N_REASONS:
3550 default:
3551 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3552 return reasonbuf;
3553 }
3554 }
3555
3556 bool
3557 ofputil_packet_in_reason_from_string(const char *s,
3558 enum ofp_packet_in_reason *reason)
3559 {
3560 int i;
3561
3562 for (i = 0; i < OFPR_N_REASONS; i++) {
3563 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3564 const char *reason_s;
3565
3566 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3567 sizeof reasonbuf);
3568 if (!strcasecmp(s, reason_s)) {
3569 *reason = i;
3570 return true;
3571 }
3572 }
3573 return false;
3574 }
3575
3576 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3577 * 'po'.
3578 *
3579 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3580 * message's actions. The caller must initialize 'ofpacts' and retains
3581 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3582 *
3583 * Returns 0 if successful, otherwise an OFPERR_* value. */
3584 enum ofperr
3585 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3586 const struct ofp_header *oh,
3587 struct ofpbuf *ofpacts)
3588 {
3589 enum ofpraw raw;
3590 struct ofpbuf b;
3591
3592 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3593 raw = ofpraw_pull_assert(&b);
3594
3595 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3596 enum ofperr error;
3597 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3598
3599 po->buffer_id = ntohl(opo->buffer_id);
3600 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3601 if (error) {
3602 return error;
3603 }
3604
3605 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3606 oh->version, ofpacts);
3607 if (error) {
3608 return error;
3609 }
3610 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3611 enum ofperr error;
3612 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3613
3614 po->buffer_id = ntohl(opo->buffer_id);
3615 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3616
3617 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3618 oh->version, ofpacts);
3619 if (error) {
3620 return error;
3621 }
3622 } else {
3623 OVS_NOT_REACHED();
3624 }
3625
3626 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3627 && po->in_port != OFPP_LOCAL
3628 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3629 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3630 po->in_port);
3631 return OFPERR_OFPBRC_BAD_PORT;
3632 }
3633
3634 po->ofpacts = ofpbuf_data(ofpacts);
3635 po->ofpacts_len = ofpbuf_size(ofpacts);
3636
3637 if (po->buffer_id == UINT32_MAX) {
3638 po->packet = ofpbuf_data(&b);
3639 po->packet_len = ofpbuf_size(&b);
3640 } else {
3641 po->packet = NULL;
3642 po->packet_len = 0;
3643 }
3644
3645 return 0;
3646 }
3647 \f
3648 /* ofputil_phy_port */
3649
3650 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3651 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3652 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3653 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3654 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3655 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3656 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3657 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3658
3659 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3660 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3661 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3662 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3663 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3664 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3665
3666 static enum netdev_features
3667 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3668 {
3669 uint32_t ofp10 = ntohl(ofp10_);
3670 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3671 }
3672
3673 static ovs_be32
3674 netdev_port_features_to_ofp10(enum netdev_features features)
3675 {
3676 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3677 }
3678
3679 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3680 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3681 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3682 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3683 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3684 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3685 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3686 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3687 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3688 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3689 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3690 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3691 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3692 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3693 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3694 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3695
3696 static enum netdev_features
3697 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3698 {
3699 return ntohl(ofp11) & 0xffff;
3700 }
3701
3702 static ovs_be32
3703 netdev_port_features_to_ofp11(enum netdev_features features)
3704 {
3705 return htonl(features & 0xffff);
3706 }
3707
3708 static enum ofperr
3709 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3710 const struct ofp10_phy_port *opp)
3711 {
3712 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3713 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
3714 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3715
3716 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3717 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3718
3719 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3720 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3721 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3722 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3723
3724 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3725 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3726
3727 return 0;
3728 }
3729
3730 static enum ofperr
3731 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3732 const struct ofp11_port *op)
3733 {
3734 enum ofperr error;
3735
3736 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3737 if (error) {
3738 return error;
3739 }
3740 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3741 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3742
3743 pp->config = ntohl(op->config) & OFPPC11_ALL;
3744 pp->state = ntohl(op->state) & OFPPS11_ALL;
3745
3746 pp->curr = netdev_port_features_from_ofp11(op->curr);
3747 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3748 pp->supported = netdev_port_features_from_ofp11(op->supported);
3749 pp->peer = netdev_port_features_from_ofp11(op->peer);
3750
3751 pp->curr_speed = ntohl(op->curr_speed);
3752 pp->max_speed = ntohl(op->max_speed);
3753
3754 return 0;
3755 }
3756
3757 static enum ofperr
3758 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3759 struct ofputil_phy_port *pp)
3760 {
3761 struct ofp14_port_desc_prop_ethernet *eth = ofpbuf_data(payload);
3762
3763 if (ofpbuf_size(payload) != sizeof *eth) {
3764 return OFPERR_OFPBPC_BAD_LEN;
3765 }
3766
3767 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3768 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3769 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3770 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3771
3772 pp->curr_speed = ntohl(eth->curr_speed);
3773 pp->max_speed = ntohl(eth->max_speed);
3774
3775 return 0;
3776 }
3777
3778 static enum ofperr
3779 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3780 {
3781 struct ofpbuf properties;
3782 struct ofp14_port *op;
3783 enum ofperr error;
3784 size_t len;
3785
3786 op = ofpbuf_try_pull(msg, sizeof *op);
3787 if (!op) {
3788 return OFPERR_OFPBRC_BAD_LEN;
3789 }
3790
3791 len = ntohs(op->length);
3792 if (len < sizeof *op || len - sizeof *op > ofpbuf_size(msg)) {
3793 return OFPERR_OFPBRC_BAD_LEN;
3794 }
3795 len -= sizeof *op;
3796 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
3797
3798 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3799 if (error) {
3800 return error;
3801 }
3802 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3803 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3804
3805 pp->config = ntohl(op->config) & OFPPC11_ALL;
3806 pp->state = ntohl(op->state) & OFPPS11_ALL;
3807
3808 while (ofpbuf_size(&properties) > 0) {
3809 struct ofpbuf payload;
3810 enum ofperr error;
3811 uint16_t type;
3812
3813 error = ofputil_pull_property(&properties, &payload, &type);
3814 if (error) {
3815 return error;
3816 }
3817
3818 switch (type) {
3819 case OFPPDPT14_ETHERNET:
3820 error = parse_ofp14_port_ethernet_property(&payload, pp);
3821 break;
3822
3823 default:
3824 log_property(true, "unknown port property %"PRIu16, type);
3825 error = 0;
3826 break;
3827 }
3828
3829 if (error) {
3830 return error;
3831 }
3832 }
3833
3834 return 0;
3835 }
3836
3837 static void
3838 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3839 struct ofp10_phy_port *opp)
3840 {
3841 memset(opp, 0, sizeof *opp);
3842
3843 opp->port_no = htons(ofp_to_u16(pp->port_no));
3844 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3845 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3846
3847 opp->config = htonl(pp->config & OFPPC10_ALL);
3848 opp->state = htonl(pp->state & OFPPS10_ALL);
3849
3850 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3851 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3852 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3853 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3854 }
3855
3856 static void
3857 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3858 struct ofp11_port *op)
3859 {
3860 memset(op, 0, sizeof *op);
3861
3862 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3863 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3864 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3865
3866 op->config = htonl(pp->config & OFPPC11_ALL);
3867 op->state = htonl(pp->state & OFPPS11_ALL);
3868
3869 op->curr = netdev_port_features_to_ofp11(pp->curr);
3870 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3871 op->supported = netdev_port_features_to_ofp11(pp->supported);
3872 op->peer = netdev_port_features_to_ofp11(pp->peer);
3873
3874 op->curr_speed = htonl(pp->curr_speed);
3875 op->max_speed = htonl(pp->max_speed);
3876 }
3877
3878 static void
3879 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
3880 struct ofpbuf *b)
3881 {
3882 struct ofp14_port *op;
3883 struct ofp14_port_desc_prop_ethernet *eth;
3884
3885 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
3886
3887 op = ofpbuf_put_zeros(b, sizeof *op);
3888 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3889 op->length = htons(sizeof *op + sizeof *eth);
3890 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3891 ovs_strlcpy(op->name, pp->name, sizeof op->name);
3892 op->config = htonl(pp->config & OFPPC11_ALL);
3893 op->state = htonl(pp->state & OFPPS11_ALL);
3894
3895 eth = ofpbuf_put_zeros(b, sizeof *eth);
3896 eth->type = htons(OFPPDPT14_ETHERNET);
3897 eth->length = htons(sizeof *eth);
3898 eth->curr = netdev_port_features_to_ofp11(pp->curr);
3899 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
3900 eth->supported = netdev_port_features_to_ofp11(pp->supported);
3901 eth->peer = netdev_port_features_to_ofp11(pp->peer);
3902 eth->curr_speed = htonl(pp->curr_speed);
3903 eth->max_speed = htonl(pp->max_speed);
3904 }
3905
3906 static void
3907 ofputil_put_phy_port(enum ofp_version ofp_version,
3908 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3909 {
3910 switch (ofp_version) {
3911 case OFP10_VERSION: {
3912 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
3913 ofputil_encode_ofp10_phy_port(pp, opp);
3914 break;
3915 }
3916
3917 case OFP11_VERSION:
3918 case OFP12_VERSION:
3919 case OFP13_VERSION: {
3920 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
3921 ofputil_encode_ofp11_port(pp, op);
3922 break;
3923 }
3924
3925 case OFP14_VERSION:
3926 case OFP15_VERSION:
3927 ofputil_put_ofp14_port(pp, b);
3928 break;
3929
3930 default:
3931 OVS_NOT_REACHED();
3932 }
3933 }
3934
3935 enum ofperr
3936 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
3937 ofp_port_t *port)
3938 {
3939 struct ofpbuf b;
3940 enum ofpraw raw;
3941
3942 ofpbuf_use_const(&b, request, ntohs(request->length));
3943 raw = ofpraw_pull_assert(&b);
3944 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
3945 *port = OFPP_ANY;
3946 return 0;
3947 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
3948 ovs_be32 *ofp11_port;
3949
3950 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
3951 return ofputil_port_from_ofp11(*ofp11_port, port);
3952 } else {
3953 OVS_NOT_REACHED();
3954 }
3955 }
3956
3957 struct ofpbuf *
3958 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
3959 ofp_port_t port)
3960 {
3961 struct ofpbuf *request;
3962 ovs_be32 ofp11_port;
3963
3964 switch (ofp_version) {
3965 case OFP10_VERSION:
3966 case OFP11_VERSION:
3967 case OFP12_VERSION:
3968 case OFP13_VERSION:
3969 case OFP14_VERSION:
3970 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
3971 ofp_version, 0);
3972 break;
3973
3974 case OFP15_VERSION:
3975 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
3976 ofp_version, 0);
3977 ofp11_port = ofputil_port_to_ofp11(port);
3978 ofpbuf_put(request, &ofp11_port, sizeof ofp11_port);
3979 break;
3980
3981 default:
3982 OVS_NOT_REACHED();
3983 }
3984
3985 return request;
3986 }
3987
3988 void
3989 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
3990 struct list *replies)
3991 {
3992 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
3993 size_t start_ofs = ofpbuf_size(reply);
3994
3995 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
3996 ofpmp_postappend(replies, start_ofs);
3997 }
3998 \f
3999 /* ofputil_switch_features */
4000
4001 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4002 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4003 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4004 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4005 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4006 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4007 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4008 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4009
4010 struct ofputil_action_bit_translation {
4011 enum ofputil_action_bitmap ofputil_bit;
4012 int of_bit;
4013 };
4014
4015 static const struct ofputil_action_bit_translation of10_action_bits[] = {
4016 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
4017 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
4018 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
4019 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
4020 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
4021 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
4022 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
4023 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
4024 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
4025 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
4026 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
4027 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
4028 { 0, 0 },
4029 };
4030
4031 static enum ofputil_action_bitmap
4032 decode_action_bits(ovs_be32 of_actions,
4033 const struct ofputil_action_bit_translation *x)
4034 {
4035 enum ofputil_action_bitmap ofputil_actions;
4036
4037 ofputil_actions = 0;
4038 for (; x->ofputil_bit; x++) {
4039 if (of_actions & htonl(1u << x->of_bit)) {
4040 ofputil_actions |= x->ofputil_bit;
4041 }
4042 }
4043 return ofputil_actions;
4044 }
4045
4046 static uint32_t
4047 ofputil_capabilities_mask(enum ofp_version ofp_version)
4048 {
4049 /* Handle capabilities whose bit is unique for all Open Flow versions */
4050 switch (ofp_version) {
4051 case OFP10_VERSION:
4052 case OFP11_VERSION:
4053 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4054 case OFP12_VERSION:
4055 case OFP13_VERSION:
4056 case OFP14_VERSION:
4057 case OFP15_VERSION:
4058 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4059 default:
4060 /* Caller needs to check osf->header.version itself */
4061 return 0;
4062 }
4063 }
4064
4065 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4066 * abstract representation in '*features'. Initializes '*b' to iterate over
4067 * the OpenFlow port structures following 'osf' with later calls to
4068 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4069 * OFPERR_* value. */
4070 enum ofperr
4071 ofputil_decode_switch_features(const struct ofp_header *oh,
4072 struct ofputil_switch_features *features,
4073 struct ofpbuf *b)
4074 {
4075 const struct ofp_switch_features *osf;
4076 enum ofpraw raw;
4077
4078 ofpbuf_use_const(b, oh, ntohs(oh->length));
4079 raw = ofpraw_pull_assert(b);
4080
4081 osf = ofpbuf_pull(b, sizeof *osf);
4082 features->datapath_id = ntohll(osf->datapath_id);
4083 features->n_buffers = ntohl(osf->n_buffers);
4084 features->n_tables = osf->n_tables;
4085 features->auxiliary_id = 0;
4086
4087 features->capabilities = ntohl(osf->capabilities) &
4088 ofputil_capabilities_mask(oh->version);
4089
4090 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4091 if (osf->capabilities & htonl(OFPC10_STP)) {
4092 features->capabilities |= OFPUTIL_C_STP;
4093 }
4094 features->actions = decode_action_bits(osf->actions, of10_action_bits);
4095 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4096 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4097 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4098 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4099 }
4100 features->actions = 0;
4101 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4102 features->auxiliary_id = osf->auxiliary_id;
4103 }
4104 } else {
4105 return OFPERR_OFPBRC_BAD_VERSION;
4106 }
4107
4108 return 0;
4109 }
4110
4111 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4112 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4113 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4114 *
4115 * Given a buffer 'b' that contains a Features Reply message, this message
4116 * checks if it contains a complete list of the switch's ports. Returns true,
4117 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4118 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4119 * message.
4120 *
4121 * When this function returns false, the caller should send an OFPST_PORT_DESC
4122 * stats request to get the ports. */
4123 bool
4124 ofputil_switch_features_has_ports(struct ofpbuf *b)
4125 {
4126 struct ofp_header *oh = ofpbuf_data(b);
4127 size_t phy_port_size;
4128
4129 if (oh->version >= OFP13_VERSION) {
4130 /* OpenFlow 1.3+ never has ports in the feature reply. */
4131 return false;
4132 }
4133
4134 phy_port_size = (oh->version == OFP10_VERSION
4135 ? sizeof(struct ofp10_phy_port)
4136 : sizeof(struct ofp11_port));
4137 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4138 /* There's room for additional ports in the feature reply.
4139 * Assume that the list is complete. */
4140 return true;
4141 }
4142
4143 /* The feature reply has no room for more ports. Probably the list is
4144 * truncated. Drop the ports and tell the caller to retrieve them with
4145 * OFPST_PORT_DESC. */
4146 ofpbuf_set_size(b, sizeof *oh + sizeof(struct ofp_switch_features));
4147 ofpmsg_update_length(b);
4148 return false;
4149 }
4150
4151 static ovs_be32
4152 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
4153 const struct ofputil_action_bit_translation *x)
4154 {
4155 uint32_t of_actions;
4156
4157 of_actions = 0;
4158 for (; x->ofputil_bit; x++) {
4159 if (ofputil_actions & x->ofputil_bit) {
4160 of_actions |= 1 << x->of_bit;
4161 }
4162 }
4163 return htonl(of_actions);
4164 }
4165
4166 /* Returns a buffer owned by the caller that encodes 'features' in the format
4167 * required by 'protocol' with the given 'xid'. The caller should append port
4168 * information to the buffer with subsequent calls to
4169 * ofputil_put_switch_features_port(). */
4170 struct ofpbuf *
4171 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4172 enum ofputil_protocol protocol, ovs_be32 xid)
4173 {
4174 struct ofp_switch_features *osf;
4175 struct ofpbuf *b;
4176 enum ofp_version version;
4177 enum ofpraw raw;
4178
4179 version = ofputil_protocol_to_ofp_version(protocol);
4180 switch (version) {
4181 case OFP10_VERSION:
4182 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4183 break;
4184 case OFP11_VERSION:
4185 case OFP12_VERSION:
4186 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4187 break;
4188 case OFP13_VERSION:
4189 case OFP14_VERSION:
4190 case OFP15_VERSION:
4191 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4192 break;
4193 default:
4194 OVS_NOT_REACHED();
4195 }
4196 b = ofpraw_alloc_xid(raw, version, xid, 0);
4197 osf = ofpbuf_put_zeros(b, sizeof *osf);
4198 osf->datapath_id = htonll(features->datapath_id);
4199 osf->n_buffers = htonl(features->n_buffers);
4200 osf->n_tables = features->n_tables;
4201
4202 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4203 osf->capabilities = htonl(features->capabilities &
4204 ofputil_capabilities_mask(version));
4205 switch (version) {
4206 case OFP10_VERSION:
4207 if (features->capabilities & OFPUTIL_C_STP) {
4208 osf->capabilities |= htonl(OFPC10_STP);
4209 }
4210 osf->actions = encode_action_bits(features->actions, of10_action_bits);
4211 break;
4212 case OFP13_VERSION:
4213 case OFP14_VERSION:
4214 case OFP15_VERSION:
4215 osf->auxiliary_id = features->auxiliary_id;
4216 /* fall through */
4217 case OFP11_VERSION:
4218 case OFP12_VERSION:
4219 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4220 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4221 }
4222 break;
4223 default:
4224 OVS_NOT_REACHED();
4225 }
4226
4227 return b;
4228 }
4229
4230 /* Encodes 'pp' into the format required by the switch_features message already
4231 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4232 * and appends the encoded version to 'b'. */
4233 void
4234 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4235 struct ofpbuf *b)
4236 {
4237 const struct ofp_header *oh = ofpbuf_data(b);
4238
4239 if (oh->version < OFP13_VERSION) {
4240 /* Try adding a port description to the message, but drop it again if
4241 * the buffer overflows. (This possibility for overflow is why
4242 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4243 size_t start_ofs = ofpbuf_size(b);
4244 ofputil_put_phy_port(oh->version, pp, b);
4245 if (ofpbuf_size(b) > UINT16_MAX) {
4246 ofpbuf_set_size(b, start_ofs);
4247 }
4248 }
4249 }
4250 \f
4251 /* ofputil_port_status */
4252
4253 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4254 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4255 enum ofperr
4256 ofputil_decode_port_status(const struct ofp_header *oh,
4257 struct ofputil_port_status *ps)
4258 {
4259 const struct ofp_port_status *ops;
4260 struct ofpbuf b;
4261 int retval;
4262
4263 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4264 ofpraw_pull_assert(&b);
4265 ops = ofpbuf_pull(&b, sizeof *ops);
4266
4267 if (ops->reason != OFPPR_ADD &&
4268 ops->reason != OFPPR_DELETE &&
4269 ops->reason != OFPPR_MODIFY) {
4270 return OFPERR_NXBRC_BAD_REASON;
4271 }
4272 ps->reason = ops->reason;
4273
4274 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4275 ovs_assert(retval != EOF);
4276 return retval;
4277 }
4278
4279 /* Converts the abstract form of a "port status" message in '*ps' into an
4280 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4281 * a buffer owned by the caller. */
4282 struct ofpbuf *
4283 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4284 enum ofputil_protocol protocol)
4285 {
4286 struct ofp_port_status *ops;
4287 struct ofpbuf *b;
4288 enum ofp_version version;
4289 enum ofpraw raw;
4290
4291 version = ofputil_protocol_to_ofp_version(protocol);
4292 switch (version) {
4293 case OFP10_VERSION:
4294 raw = OFPRAW_OFPT10_PORT_STATUS;
4295 break;
4296
4297 case OFP11_VERSION:
4298 case OFP12_VERSION:
4299 case OFP13_VERSION:
4300 raw = OFPRAW_OFPT11_PORT_STATUS;
4301 break;
4302
4303 case OFP14_VERSION:
4304 case OFP15_VERSION:
4305 raw = OFPRAW_OFPT14_PORT_STATUS;
4306 break;
4307
4308 default:
4309 OVS_NOT_REACHED();
4310 }
4311
4312 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4313 ops = ofpbuf_put_zeros(b, sizeof *ops);
4314 ops->reason = ps->reason;
4315 ofputil_put_phy_port(version, &ps->desc, b);
4316 ofpmsg_update_length(b);
4317 return b;
4318 }
4319
4320 /* ofputil_port_mod */
4321
4322 static enum ofperr
4323 parse_port_mod_ethernet_property(struct ofpbuf *property,
4324 struct ofputil_port_mod *pm)
4325 {
4326 struct ofp14_port_mod_prop_ethernet *eth = ofpbuf_data(property);
4327
4328 if (ofpbuf_size(property) != sizeof *eth) {
4329 return OFPERR_OFPBRC_BAD_LEN;
4330 }
4331
4332 pm->advertise = netdev_port_features_from_ofp11(eth->advertise);
4333 return 0;
4334 }
4335
4336 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4337 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4338 enum ofperr
4339 ofputil_decode_port_mod(const struct ofp_header *oh,
4340 struct ofputil_port_mod *pm, bool loose)
4341 {
4342 enum ofpraw raw;
4343 struct ofpbuf b;
4344
4345 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4346 raw = ofpraw_pull_assert(&b);
4347
4348 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4349 const struct ofp10_port_mod *opm = ofpbuf_data(&b);
4350
4351 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4352 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4353 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4354 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4355 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4356 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4357 const struct ofp11_port_mod *opm = ofpbuf_data(&b);
4358 enum ofperr error;
4359
4360 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4361 if (error) {
4362 return error;
4363 }
4364
4365 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4366 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4367 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4368 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4369 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4370 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4371 enum ofperr error;
4372
4373 memset(pm, 0, sizeof *pm);
4374
4375 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4376 if (error) {
4377 return error;
4378 }
4379
4380 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4381 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4382 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4383
4384 while (ofpbuf_size(&b) > 0) {
4385 struct ofpbuf property;
4386 enum ofperr error;
4387 uint16_t type;
4388
4389 error = ofputil_pull_property(&b, &property, &type);
4390 if (error) {
4391 return error;
4392 }
4393
4394 switch (type) {
4395 case OFPPMPT14_ETHERNET:
4396 error = parse_port_mod_ethernet_property(&property, pm);
4397 break;
4398
4399 default:
4400 log_property(loose, "unknown port_mod property %"PRIu16, type);
4401 if (loose) {
4402 error = 0;
4403 } else if (type == OFPPMPT14_EXPERIMENTER) {
4404 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
4405 } else {
4406 error = OFPERR_OFPBRC_BAD_TYPE;
4407 }
4408 break;
4409 }
4410
4411 if (error) {
4412 return error;
4413 }
4414 }
4415 } else {
4416 return OFPERR_OFPBRC_BAD_TYPE;
4417 }
4418
4419 pm->config &= pm->mask;
4420 return 0;
4421 }
4422
4423 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4424 * message suitable for 'protocol', and returns that encoded form in a buffer
4425 * owned by the caller. */
4426 struct ofpbuf *
4427 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4428 enum ofputil_protocol protocol)
4429 {
4430 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4431 struct ofpbuf *b;
4432
4433 switch (ofp_version) {
4434 case OFP10_VERSION: {
4435 struct ofp10_port_mod *opm;
4436
4437 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4438 opm = ofpbuf_put_zeros(b, sizeof *opm);
4439 opm->port_no = htons(ofp_to_u16(pm->port_no));
4440 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4441 opm->config = htonl(pm->config & OFPPC10_ALL);
4442 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4443 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4444 break;
4445 }
4446
4447 case OFP11_VERSION:
4448 case OFP12_VERSION:
4449 case OFP13_VERSION: {
4450 struct ofp11_port_mod *opm;
4451
4452 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4453 opm = ofpbuf_put_zeros(b, sizeof *opm);
4454 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4455 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4456 opm->config = htonl(pm->config & OFPPC11_ALL);
4457 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4458 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4459 break;
4460 }
4461 case OFP14_VERSION:
4462 case OFP15_VERSION: {
4463 struct ofp14_port_mod_prop_ethernet *eth;
4464 struct ofp14_port_mod *opm;
4465
4466 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth);
4467 opm = ofpbuf_put_zeros(b, sizeof *opm);
4468 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4469 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4470 opm->config = htonl(pm->config & OFPPC11_ALL);
4471 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4472
4473 if (pm->advertise) {
4474 eth = ofpbuf_put_zeros(b, sizeof *eth);
4475 eth->type = htons(OFPPMPT14_ETHERNET);
4476 eth->length = htons(sizeof *eth);
4477 eth->advertise = netdev_port_features_to_ofp11(pm->advertise);
4478 }
4479 break;
4480 }
4481 default:
4482 OVS_NOT_REACHED();
4483 }
4484
4485 return b;
4486 }
4487
4488 static enum ofperr
4489 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4490 uint16_t *typep)
4491 {
4492 enum ofperr error;
4493
4494 error = ofputil_pull_property(msg, payload, typep);
4495 if (payload && !error) {
4496 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4497 }
4498 return error;
4499 }
4500
4501 static enum ofperr
4502 parse_table_ids(struct ofpbuf *payload, uint32_t *ids)
4503 {
4504 uint16_t type;
4505
4506 *ids = 0;
4507 while (ofpbuf_size(payload) > 0) {
4508 enum ofperr error = pull_table_feature_property(payload, NULL, &type);
4509 if (error) {
4510 return error;
4511 }
4512 if (type < CHAR_BIT * sizeof *ids) {
4513 *ids |= 1u << type;
4514 }
4515 }
4516 return 0;
4517 }
4518
4519 static enum ofperr
4520 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4521 {
4522 *insts = 0;
4523 while (ofpbuf_size(payload) > 0) {
4524 enum ovs_instruction_type inst;
4525 enum ofperr error;
4526 uint16_t ofpit;
4527
4528 error = pull_table_feature_property(payload, NULL, &ofpit);
4529 if (error) {
4530 return error;
4531 }
4532
4533 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4534 if (!error) {
4535 *insts |= 1u << inst;
4536 } else if (!loose) {
4537 return error;
4538 }
4539 }
4540 return 0;
4541 }
4542
4543 static enum ofperr
4544 parse_table_features_next_table(struct ofpbuf *payload,
4545 unsigned long int *next_tables)
4546 {
4547 size_t i;
4548
4549 memset(next_tables, 0, bitmap_n_bytes(255));
4550 for (i = 0; i < ofpbuf_size(payload); i++) {
4551 uint8_t id = ((const uint8_t *) ofpbuf_data(payload))[i];
4552 if (id >= 255) {
4553 return OFPERR_OFPBPC_BAD_VALUE;
4554 }
4555 bitmap_set1(next_tables, id);
4556 }
4557 return 0;
4558 }
4559
4560 static enum ofperr
4561 parse_oxm(struct ofpbuf *b, bool loose,
4562 const struct mf_field **fieldp, bool *hasmask)
4563 {
4564 ovs_be32 *oxmp;
4565 uint32_t oxm;
4566
4567 oxmp = ofpbuf_try_pull(b, sizeof *oxmp);
4568 if (!oxmp) {
4569 return OFPERR_OFPBPC_BAD_LEN;
4570 }
4571 oxm = ntohl(*oxmp);
4572
4573 /* Determine '*hasmask'. If 'oxm' is masked, convert it to the equivalent
4574 * unmasked version, because the table of OXM fields we support only has
4575 * masked versions of fields that we support with masks, but we should be
4576 * able to parse the masked versions of those here. */
4577 *hasmask = NXM_HASMASK(oxm);
4578 if (*hasmask) {
4579 if (NXM_LENGTH(oxm) & 1) {
4580 return OFPERR_OFPBPC_BAD_VALUE;
4581 }
4582 oxm = NXM_HEADER(NXM_VENDOR(oxm), NXM_FIELD(oxm), NXM_LENGTH(oxm) / 2);
4583 }
4584
4585 *fieldp = mf_from_nxm_header(oxm);
4586 if (!*fieldp) {
4587 log_property(loose, "unknown OXM field %#"PRIx32, ntohl(*oxmp));
4588 }
4589 return *fieldp ? 0 : OFPERR_OFPBMC_BAD_FIELD;
4590 }
4591
4592 static enum ofperr
4593 parse_oxms(struct ofpbuf *payload, bool loose,
4594 uint64_t *exactp, uint64_t *maskedp)
4595 {
4596 uint64_t exact, masked;
4597
4598 exact = masked = 0;
4599 while (ofpbuf_size(payload) > 0) {
4600 const struct mf_field *field;
4601 enum ofperr error;
4602 bool hasmask;
4603
4604 error = parse_oxm(payload, loose, &field, &hasmask);
4605 if (!error) {
4606 if (hasmask) {
4607 masked |= UINT64_C(1) << field->id;
4608 } else {
4609 exact |= UINT64_C(1) << field->id;
4610 }
4611 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4612 return error;
4613 }
4614 }
4615 if (exactp) {
4616 *exactp = exact;
4617 } else if (exact) {
4618 return OFPERR_OFPBMC_BAD_MASK;
4619 }
4620 if (maskedp) {
4621 *maskedp = masked;
4622 } else if (masked) {
4623 return OFPERR_OFPBMC_BAD_MASK;
4624 }
4625 return 0;
4626 }
4627
4628 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4629 * ofputil_table_features in 'tf'.
4630 *
4631 * If 'loose' is true, this function ignores properties and values that it does
4632 * not understand, as a controller would want to do when interpreting
4633 * capabilities provided by a switch. If 'loose' is false, this function
4634 * treats unknown properties and values as an error, as a switch would want to
4635 * do when interpreting a configuration request made by a controller.
4636 *
4637 * A single OpenFlow message can specify features for multiple tables. Calling
4638 * this function multiple times for a single 'msg' iterates through the tables
4639 * in the message. The caller must initially leave 'msg''s layer pointers null
4640 * and not modify them between calls.
4641 *
4642 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4643 * a positive "enum ofperr" value. */
4644 int
4645 ofputil_decode_table_features(struct ofpbuf *msg,
4646 struct ofputil_table_features *tf, bool loose)
4647 {
4648 struct ofp13_table_features *otf;
4649 unsigned int len;
4650
4651 if (!msg->frame) {
4652 ofpraw_pull_assert(msg);
4653 }
4654
4655 if (!ofpbuf_size(msg)) {
4656 return EOF;
4657 }
4658
4659 if (ofpbuf_size(msg) < sizeof *otf) {
4660 return OFPERR_OFPBPC_BAD_LEN;
4661 }
4662
4663 otf = ofpbuf_data(msg);
4664 len = ntohs(otf->length);
4665 if (len < sizeof *otf || len % 8 || len > ofpbuf_size(msg)) {
4666 return OFPERR_OFPBPC_BAD_LEN;
4667 }
4668 ofpbuf_pull(msg, sizeof *otf);
4669
4670 tf->table_id = otf->table_id;
4671 if (tf->table_id == OFPTT_ALL) {
4672 return OFPERR_OFPTFFC_BAD_TABLE;
4673 }
4674
4675 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4676 tf->metadata_match = otf->metadata_match;
4677 tf->metadata_write = otf->metadata_write;
4678 tf->config = ntohl(otf->config);
4679 tf->max_entries = ntohl(otf->max_entries);
4680
4681 while (ofpbuf_size(msg) > 0) {
4682 struct ofpbuf payload;
4683 enum ofperr error;
4684 uint16_t type;
4685
4686 error = pull_table_feature_property(msg, &payload, &type);
4687 if (error) {
4688 return error;
4689 }
4690
4691 switch ((enum ofp13_table_feature_prop_type) type) {
4692 case OFPTFPT13_INSTRUCTIONS:
4693 error = parse_instruction_ids(&payload, loose,
4694 &tf->nonmiss.instructions);
4695 break;
4696 case OFPTFPT13_INSTRUCTIONS_MISS:
4697 error = parse_instruction_ids(&payload, loose,
4698 &tf->miss.instructions);
4699 break;
4700
4701 case OFPTFPT13_NEXT_TABLES:
4702 error = parse_table_features_next_table(&payload,
4703 tf->nonmiss.next);
4704 break;
4705 case OFPTFPT13_NEXT_TABLES_MISS:
4706 error = parse_table_features_next_table(&payload, tf->miss.next);
4707 break;
4708
4709 case OFPTFPT13_WRITE_ACTIONS:
4710 error = parse_table_ids(&payload, &tf->nonmiss.write.actions);
4711 break;
4712 case OFPTFPT13_WRITE_ACTIONS_MISS:
4713 error = parse_table_ids(&payload, &tf->miss.write.actions);
4714 break;
4715
4716 case OFPTFPT13_APPLY_ACTIONS:
4717 error = parse_table_ids(&payload, &tf->nonmiss.apply.actions);
4718 break;
4719 case OFPTFPT13_APPLY_ACTIONS_MISS:
4720 error = parse_table_ids(&payload, &tf->miss.apply.actions);
4721 break;
4722
4723 case OFPTFPT13_MATCH:
4724 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4725 break;
4726 case OFPTFPT13_WILDCARDS:
4727 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4728 break;
4729
4730 case OFPTFPT13_WRITE_SETFIELD:
4731 error = parse_oxms(&payload, loose,
4732 &tf->nonmiss.write.set_fields, NULL);
4733 break;
4734 case OFPTFPT13_WRITE_SETFIELD_MISS:
4735 error = parse_oxms(&payload, loose,
4736 &tf->miss.write.set_fields, NULL);
4737 break;
4738 case OFPTFPT13_APPLY_SETFIELD:
4739 error = parse_oxms(&payload, loose,
4740 &tf->nonmiss.apply.set_fields, NULL);
4741 break;
4742 case OFPTFPT13_APPLY_SETFIELD_MISS:
4743 error = parse_oxms(&payload, loose,
4744 &tf->miss.apply.set_fields, NULL);
4745 break;
4746
4747 case OFPTFPT13_EXPERIMENTER:
4748 case OFPTFPT13_EXPERIMENTER_MISS:
4749 default:
4750 log_property(loose, "unknown table features property %"PRIu16,
4751 type);
4752 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4753 break;
4754 }
4755 if (error) {
4756 return error;
4757 }
4758 }
4759
4760 /* Fix inconsistencies:
4761 *
4762 * - Turn off 'mask' and 'wildcard' bits that are not in 'match',
4763 * because a field must be matchable to be masked or wildcarded.
4764 *
4765 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4766 * that is arbitrarily maskable can be wildcarded entirely. */
4767 tf->mask &= tf->match;
4768 tf->wildcard &= tf->match;
4769
4770 tf->wildcard |= tf->mask;
4771
4772 return 0;
4773 }
4774
4775 /* Encodes and returns a request to obtain the table features of a switch.
4776 * The message is encoded for OpenFlow version 'ofp_version'. */
4777 struct ofpbuf *
4778 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4779 {
4780 struct ofpbuf *request = NULL;
4781
4782 switch (ofp_version) {
4783 case OFP10_VERSION:
4784 case OFP11_VERSION:
4785 case OFP12_VERSION:
4786 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4787 "(\'-O OpenFlow13\')");
4788 case OFP13_VERSION:
4789 case OFP14_VERSION:
4790 case OFP15_VERSION:
4791 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4792 ofp_version, 0);
4793 break;
4794 default:
4795 OVS_NOT_REACHED();
4796 }
4797
4798 return request;
4799 }
4800
4801 /* ofputil_table_mod */
4802
4803 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
4804 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4805 enum ofperr
4806 ofputil_decode_table_mod(const struct ofp_header *oh,
4807 struct ofputil_table_mod *pm)
4808 {
4809 enum ofpraw raw;
4810 struct ofpbuf b;
4811
4812 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4813 raw = ofpraw_pull_assert(&b);
4814
4815 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
4816 const struct ofp11_table_mod *otm = ofpbuf_data(&b);
4817
4818 pm->table_id = otm->table_id;
4819 pm->config = ntohl(otm->config);
4820 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
4821 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
4822
4823 pm->table_id = otm->table_id;
4824 pm->config = ntohl(otm->config);
4825 /* We do not understand any properties yet, so we do not bother
4826 * parsing them. */
4827 } else {
4828 return OFPERR_OFPBRC_BAD_TYPE;
4829 }
4830
4831 return 0;
4832 }
4833
4834 /* Converts the abstract form of a "table mod" message in '*pm' into an OpenFlow
4835 * message suitable for 'protocol', and returns that encoded form in a buffer
4836 * owned by the caller. */
4837 struct ofpbuf *
4838 ofputil_encode_table_mod(const struct ofputil_table_mod *pm,
4839 enum ofputil_protocol protocol)
4840 {
4841 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4842 struct ofpbuf *b;
4843
4844 switch (ofp_version) {
4845 case OFP10_VERSION: {
4846 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
4847 "(\'-O OpenFlow11\')");
4848 break;
4849 }
4850 case OFP11_VERSION:
4851 case OFP12_VERSION:
4852 case OFP13_VERSION: {
4853 struct ofp11_table_mod *otm;
4854
4855 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
4856 otm = ofpbuf_put_zeros(b, sizeof *otm);
4857 otm->table_id = pm->table_id;
4858 otm->config = htonl(pm->config);
4859 break;
4860 }
4861 case OFP14_VERSION:
4862 case OFP15_VERSION: {
4863 struct ofp14_table_mod *otm;
4864
4865 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
4866 otm = ofpbuf_put_zeros(b, sizeof *otm);
4867 otm->table_id = pm->table_id;
4868 otm->config = htonl(pm->config);
4869 break;
4870 }
4871 default:
4872 OVS_NOT_REACHED();
4873 }
4874
4875 return b;
4876 }
4877 \f
4878 /* ofputil_role_request */
4879
4880 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
4881 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
4882 * OFPERR_* value. */
4883 enum ofperr
4884 ofputil_decode_role_message(const struct ofp_header *oh,
4885 struct ofputil_role_request *rr)
4886 {
4887 struct ofpbuf b;
4888 enum ofpraw raw;
4889
4890 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4891 raw = ofpraw_pull_assert(&b);
4892
4893 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
4894 raw == OFPRAW_OFPT12_ROLE_REPLY) {
4895 const struct ofp12_role_request *orr = ofpbuf_l3(&b);
4896
4897 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
4898 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
4899 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
4900 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
4901 return OFPERR_OFPRRFC_BAD_ROLE;
4902 }
4903
4904 rr->role = ntohl(orr->role);
4905 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
4906 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
4907 : orr->generation_id == OVS_BE64_MAX) {
4908 rr->have_generation_id = false;
4909 rr->generation_id = 0;
4910 } else {
4911 rr->have_generation_id = true;
4912 rr->generation_id = ntohll(orr->generation_id);
4913 }
4914 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
4915 raw == OFPRAW_NXT_ROLE_REPLY) {
4916 const struct nx_role_request *nrr = ofpbuf_l3(&b);
4917
4918 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
4919 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
4920 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
4921
4922 if (nrr->role != htonl(NX_ROLE_OTHER) &&
4923 nrr->role != htonl(NX_ROLE_MASTER) &&
4924 nrr->role != htonl(NX_ROLE_SLAVE)) {
4925 return OFPERR_OFPRRFC_BAD_ROLE;
4926 }
4927
4928 rr->role = ntohl(nrr->role) + 1;
4929 rr->have_generation_id = false;
4930 rr->generation_id = 0;
4931 } else {
4932 OVS_NOT_REACHED();
4933 }
4934
4935 return 0;
4936 }
4937
4938 /* Returns an encoded form of a role reply suitable for the "request" in a
4939 * buffer owned by the caller. */
4940 struct ofpbuf *
4941 ofputil_encode_role_reply(const struct ofp_header *request,
4942 const struct ofputil_role_request *rr)
4943 {
4944 struct ofpbuf *buf;
4945 enum ofpraw raw;
4946
4947 raw = ofpraw_decode_assert(request);
4948 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
4949 struct ofp12_role_request *orr;
4950
4951 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
4952 orr = ofpbuf_put_zeros(buf, sizeof *orr);
4953
4954 orr->role = htonl(rr->role);
4955 orr->generation_id = htonll(rr->have_generation_id
4956 ? rr->generation_id
4957 : UINT64_MAX);
4958 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
4959 struct nx_role_request *nrr;
4960
4961 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
4962 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
4963 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
4964
4965 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
4966 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
4967 nrr->role = htonl(rr->role - 1);
4968 } else {
4969 OVS_NOT_REACHED();
4970 }
4971
4972 return buf;
4973 }
4974 \f
4975 struct ofpbuf *
4976 ofputil_encode_role_status(const struct ofputil_role_status *status,
4977 enum ofputil_protocol protocol)
4978 {
4979 struct ofpbuf *buf;
4980 enum ofp_version version;
4981 struct ofp14_role_status *rstatus;
4982
4983 version = ofputil_protocol_to_ofp_version(protocol);
4984 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0), 0);
4985 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
4986 rstatus->role = htonl(status->role);
4987 rstatus->reason = status->reason;
4988 rstatus->generation_id = htonll(status->generation_id);
4989
4990 return buf;
4991 }
4992
4993 enum ofperr
4994 ofputil_decode_role_status(const struct ofp_header *oh,
4995 struct ofputil_role_status *rs)
4996 {
4997 struct ofpbuf b;
4998 enum ofpraw raw;
4999 const struct ofp14_role_status *r;
5000
5001 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5002 raw = ofpraw_pull_assert(&b);
5003 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5004
5005 r = ofpbuf_l3(&b);
5006 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5007 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5008 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5009 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5010 return OFPERR_OFPRRFC_BAD_ROLE;
5011 }
5012
5013 rs->role = ntohl(r->role);
5014 rs->generation_id = ntohll(r->generation_id);
5015 rs->reason = r->reason;
5016
5017 return 0;
5018 }
5019
5020 /* Table stats. */
5021
5022 static void
5023 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
5024 struct ofpbuf *buf)
5025 {
5026 struct wc_map {
5027 enum ofp10_flow_wildcards wc10;
5028 enum oxm12_ofb_match_fields mf12;
5029 };
5030
5031 static const struct wc_map wc_map[] = {
5032 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
5033 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
5034 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
5035 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
5036 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
5037 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
5038 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
5039 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
5040 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
5041 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
5042 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
5043 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
5044 };
5045
5046 struct ofp10_table_stats *out;
5047 const struct wc_map *p;
5048
5049 out = ofpbuf_put_zeros(buf, sizeof *out);
5050 out->table_id = in->table_id;
5051 ovs_strlcpy(out->name, in->name, sizeof out->name);
5052 out->wildcards = 0;
5053 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
5054 if (in->wildcards & htonll(1ULL << p->mf12)) {
5055 out->wildcards |= htonl(p->wc10);
5056 }
5057 }
5058 out->max_entries = in->max_entries;
5059 out->active_count = in->active_count;
5060 put_32aligned_be64(&out->lookup_count, in->lookup_count);
5061 put_32aligned_be64(&out->matched_count, in->matched_count);
5062 }
5063
5064 static ovs_be32
5065 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
5066 {
5067 struct map {
5068 enum ofp11_flow_match_fields fmf11;
5069 enum oxm12_ofb_match_fields mf12;
5070 };
5071
5072 static const struct map map[] = {
5073 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
5074 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
5075 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
5076 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
5077 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
5078 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
5079 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
5080 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
5081 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
5082 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
5083 /* I don't know what OFPFMF11_TYPE means. */
5084 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
5085 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
5086 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
5087 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
5088 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
5089 };
5090
5091 const struct map *p;
5092 uint32_t fmf11;
5093
5094 fmf11 = 0;
5095 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
5096 if (oxm12 & htonll(1ULL << p->mf12)) {
5097 fmf11 |= p->fmf11;
5098 }
5099 }
5100 return htonl(fmf11);
5101 }
5102
5103 static void
5104 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
5105 struct ofpbuf *buf)
5106 {
5107 struct ofp11_table_stats *out;
5108
5109 out = ofpbuf_put_zeros(buf, sizeof *out);
5110 out->table_id = in->table_id;
5111 ovs_strlcpy(out->name, in->name, sizeof out->name);
5112 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
5113 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
5114 out->instructions = in->instructions;
5115 out->write_actions = in->write_actions;
5116 out->apply_actions = in->apply_actions;
5117 out->config = in->config;
5118 out->max_entries = in->max_entries;
5119 out->active_count = in->active_count;
5120 out->lookup_count = in->lookup_count;
5121 out->matched_count = in->matched_count;
5122 }
5123
5124 static void
5125 ofputil_put_ofp12_table_stats(const struct ofp12_table_stats *in,
5126 struct ofpbuf *buf)
5127 {
5128 struct ofp12_table_stats *out = ofpbuf_put(buf, in, sizeof *in);
5129
5130 /* Trim off OF1.3-only capabilities. */
5131 out->match &= htonll(OFPXMT12_MASK);
5132 out->wildcards &= htonll(OFPXMT12_MASK);
5133 out->write_setfields &= htonll(OFPXMT12_MASK);
5134 out->apply_setfields &= htonll(OFPXMT12_MASK);
5135 }
5136
5137 static void
5138 ofputil_put_ofp13_table_stats(const struct ofp12_table_stats *in,
5139 struct ofpbuf *buf)
5140 {
5141 struct ofp13_table_stats *out;
5142
5143 /* OF 1.3 splits table features off the ofp_table_stats,
5144 * so there is not much here. */
5145
5146 out = ofpbuf_put_uninit(buf, sizeof *out);
5147 out->table_id = in->table_id;
5148 out->active_count = in->active_count;
5149 out->lookup_count = in->lookup_count;
5150 out->matched_count = in->matched_count;
5151 }
5152
5153 struct ofpbuf *
5154 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
5155 const struct ofp_header *request)
5156 {
5157 struct ofpbuf *reply;
5158 int i;
5159
5160 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
5161
5162 for (i = 0; i < n; i++) {
5163 switch ((enum ofp_version) request->version) {
5164 case OFP10_VERSION:
5165 ofputil_put_ofp10_table_stats(&stats[i], reply);
5166 break;
5167
5168 case OFP11_VERSION:
5169 ofputil_put_ofp11_table_stats(&stats[i], reply);
5170 break;
5171
5172 case OFP12_VERSION:
5173 ofputil_put_ofp12_table_stats(&stats[i], reply);
5174 break;
5175
5176 case OFP13_VERSION:
5177 case OFP14_VERSION:
5178 case OFP15_VERSION:
5179 ofputil_put_ofp13_table_stats(&stats[i], reply);
5180 break;
5181
5182 default:
5183 OVS_NOT_REACHED();
5184 }
5185 }
5186
5187 return reply;
5188 }
5189 \f
5190 /* ofputil_flow_monitor_request */
5191
5192 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
5193 * ofputil_flow_monitor_request in 'rq'.
5194 *
5195 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
5196 * message. Calling this function multiple times for a single 'msg' iterates
5197 * through the requests. The caller must initially leave 'msg''s layer
5198 * pointers null and not modify them between calls.
5199 *
5200 * Returns 0 if successful, EOF if no requests were left in this 'msg',
5201 * otherwise an OFPERR_* value. */
5202 int
5203 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
5204 struct ofpbuf *msg)
5205 {
5206 struct nx_flow_monitor_request *nfmr;
5207 uint16_t flags;
5208
5209 if (!msg->frame) {
5210 ofpraw_pull_assert(msg);
5211 }
5212
5213 if (!ofpbuf_size(msg)) {
5214 return EOF;
5215 }
5216
5217 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
5218 if (!nfmr) {
5219 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
5220 "leftover bytes at end", ofpbuf_size(msg));
5221 return OFPERR_OFPBRC_BAD_LEN;
5222 }
5223
5224 flags = ntohs(nfmr->flags);
5225 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
5226 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
5227 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
5228 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
5229 flags);
5230 return OFPERR_NXBRC_FM_BAD_FLAGS;
5231 }
5232
5233 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
5234 return OFPERR_NXBRC_MUST_BE_ZERO;
5235 }
5236
5237 rq->id = ntohl(nfmr->id);
5238 rq->flags = flags;
5239 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
5240 rq->table_id = nfmr->table_id;
5241
5242 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
5243 }
5244
5245 void
5246 ofputil_append_flow_monitor_request(
5247 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
5248 {
5249 struct nx_flow_monitor_request *nfmr;
5250 size_t start_ofs;
5251 int match_len;
5252
5253 if (!ofpbuf_size(msg)) {
5254 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
5255 }
5256
5257 start_ofs = ofpbuf_size(msg);
5258 ofpbuf_put_zeros(msg, sizeof *nfmr);
5259 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
5260
5261 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
5262 nfmr->id = htonl(rq->id);
5263 nfmr->flags = htons(rq->flags);
5264 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
5265 nfmr->match_len = htons(match_len);
5266 nfmr->table_id = rq->table_id;
5267 }
5268
5269 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
5270 * into an abstract ofputil_flow_update in 'update'. The caller must have
5271 * initialized update->match to point to space allocated for a match.
5272 *
5273 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
5274 * actions (except for NXFME_ABBREV, which never includes actions). The caller
5275 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
5276 * will point into the 'ofpacts' buffer.
5277 *
5278 * Multiple flow updates can be packed into a single OpenFlow message. Calling
5279 * this function multiple times for a single 'msg' iterates through the
5280 * updates. The caller must initially leave 'msg''s layer pointers null and
5281 * not modify them between calls.
5282 *
5283 * Returns 0 if successful, EOF if no updates were left in this 'msg',
5284 * otherwise an OFPERR_* value. */
5285 int
5286 ofputil_decode_flow_update(struct ofputil_flow_update *update,
5287 struct ofpbuf *msg, struct ofpbuf *ofpacts)
5288 {
5289 struct nx_flow_update_header *nfuh;
5290 unsigned int length;
5291 struct ofp_header *oh;
5292
5293 if (!msg->frame) {
5294 ofpraw_pull_assert(msg);
5295 }
5296
5297 if (!ofpbuf_size(msg)) {
5298 return EOF;
5299 }
5300
5301 if (ofpbuf_size(msg) < sizeof(struct nx_flow_update_header)) {
5302 goto bad_len;
5303 }
5304
5305 oh = msg->frame;
5306
5307 nfuh = ofpbuf_data(msg);
5308 update->event = ntohs(nfuh->event);
5309 length = ntohs(nfuh->length);
5310 if (length > ofpbuf_size(msg) || length % 8) {
5311 goto bad_len;
5312 }
5313
5314 if (update->event == NXFME_ABBREV) {
5315 struct nx_flow_update_abbrev *nfua;
5316
5317 if (length != sizeof *nfua) {
5318 goto bad_len;
5319 }
5320
5321 nfua = ofpbuf_pull(msg, sizeof *nfua);
5322 update->xid = nfua->xid;
5323 return 0;
5324 } else if (update->event == NXFME_ADDED
5325 || update->event == NXFME_DELETED
5326 || update->event == NXFME_MODIFIED) {
5327 struct nx_flow_update_full *nfuf;
5328 unsigned int actions_len;
5329 unsigned int match_len;
5330 enum ofperr error;
5331
5332 if (length < sizeof *nfuf) {
5333 goto bad_len;
5334 }
5335
5336 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
5337 match_len = ntohs(nfuf->match_len);
5338 if (sizeof *nfuf + match_len > length) {
5339 goto bad_len;
5340 }
5341
5342 update->reason = ntohs(nfuf->reason);
5343 update->idle_timeout = ntohs(nfuf->idle_timeout);
5344 update->hard_timeout = ntohs(nfuf->hard_timeout);
5345 update->table_id = nfuf->table_id;
5346 update->cookie = nfuf->cookie;
5347 update->priority = ntohs(nfuf->priority);
5348
5349 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
5350 if (error) {
5351 return error;
5352 }
5353
5354 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
5355 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5356 ofpacts);
5357 if (error) {
5358 return error;
5359 }
5360
5361 update->ofpacts = ofpbuf_data(ofpacts);
5362 update->ofpacts_len = ofpbuf_size(ofpacts);
5363 return 0;
5364 } else {
5365 VLOG_WARN_RL(&bad_ofmsg_rl,
5366 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
5367 ntohs(nfuh->event));
5368 return OFPERR_NXBRC_FM_BAD_EVENT;
5369 }
5370
5371 bad_len:
5372 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
5373 "leftover bytes at end", ofpbuf_size(msg));
5374 return OFPERR_OFPBRC_BAD_LEN;
5375 }
5376
5377 uint32_t
5378 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
5379 {
5380 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
5381
5382 return ntohl(cancel->id);
5383 }
5384
5385 struct ofpbuf *
5386 ofputil_encode_flow_monitor_cancel(uint32_t id)
5387 {
5388 struct nx_flow_monitor_cancel *nfmc;
5389 struct ofpbuf *msg;
5390
5391 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
5392 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
5393 nfmc->id = htonl(id);
5394 return msg;
5395 }
5396
5397 void
5398 ofputil_start_flow_update(struct list *replies)
5399 {
5400 struct ofpbuf *msg;
5401
5402 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
5403 htonl(0), 1024);
5404
5405 list_init(replies);
5406 list_push_back(replies, &msg->list_node);
5407 }
5408
5409 void
5410 ofputil_append_flow_update(const struct ofputil_flow_update *update,
5411 struct list *replies)
5412 {
5413 enum ofp_version version = ofpmp_version(replies);
5414 struct nx_flow_update_header *nfuh;
5415 struct ofpbuf *msg;
5416 size_t start_ofs;
5417
5418 msg = ofpbuf_from_list(list_back(replies));
5419 start_ofs = ofpbuf_size(msg);
5420
5421 if (update->event == NXFME_ABBREV) {
5422 struct nx_flow_update_abbrev *nfua;
5423
5424 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
5425 nfua->xid = update->xid;
5426 } else {
5427 struct nx_flow_update_full *nfuf;
5428 int match_len;
5429
5430 ofpbuf_put_zeros(msg, sizeof *nfuf);
5431 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
5432 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
5433 version);
5434 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
5435 nfuf->reason = htons(update->reason);
5436 nfuf->priority = htons(update->priority);
5437 nfuf->idle_timeout = htons(update->idle_timeout);
5438 nfuf->hard_timeout = htons(update->hard_timeout);
5439 nfuf->match_len = htons(match_len);
5440 nfuf->table_id = update->table_id;
5441 nfuf->cookie = update->cookie;
5442 }
5443
5444 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
5445 nfuh->length = htons(ofpbuf_size(msg) - start_ofs);
5446 nfuh->event = htons(update->event);
5447
5448 ofpmp_postappend(replies, start_ofs);
5449 }
5450 \f
5451 struct ofpbuf *
5452 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
5453 enum ofputil_protocol protocol)
5454 {
5455 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5456 struct ofpbuf *msg;
5457 size_t size;
5458
5459 size = po->ofpacts_len;
5460 if (po->buffer_id == UINT32_MAX) {
5461 size += po->packet_len;
5462 }
5463
5464 switch (ofp_version) {
5465 case OFP10_VERSION: {
5466 struct ofp10_packet_out *opo;
5467 size_t actions_ofs;
5468
5469 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
5470 ofpbuf_put_zeros(msg, sizeof *opo);
5471 actions_ofs = ofpbuf_size(msg);
5472 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
5473 ofp_version);
5474
5475 opo = ofpbuf_l3(msg);
5476 opo->buffer_id = htonl(po->buffer_id);
5477 opo->in_port = htons(ofp_to_u16(po->in_port));
5478 opo->actions_len = htons(ofpbuf_size(msg) - actions_ofs);
5479 break;
5480 }
5481
5482 case OFP11_VERSION:
5483 case OFP12_VERSION:
5484 case OFP13_VERSION:
5485 case OFP14_VERSION:
5486 case OFP15_VERSION: {
5487 struct ofp11_packet_out *opo;
5488 size_t len;
5489
5490 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
5491 ofpbuf_put_zeros(msg, sizeof *opo);
5492 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
5493 ofp_version);
5494 opo = ofpbuf_l3(msg);
5495 opo->buffer_id = htonl(po->buffer_id);
5496 opo->in_port = ofputil_port_to_ofp11(po->in_port);
5497 opo->actions_len = htons(len);
5498 break;
5499 }
5500
5501 default:
5502 OVS_NOT_REACHED();
5503 }
5504
5505 if (po->buffer_id == UINT32_MAX) {
5506 ofpbuf_put(msg, po->packet, po->packet_len);
5507 }
5508
5509 ofpmsg_update_length(msg);
5510
5511 return msg;
5512 }
5513 \f
5514 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
5515 struct ofpbuf *
5516 make_echo_request(enum ofp_version ofp_version)
5517 {
5518 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
5519 htonl(0), 0);
5520 }
5521
5522 /* Creates and returns an OFPT_ECHO_REPLY message matching the
5523 * OFPT_ECHO_REQUEST message in 'rq'. */
5524 struct ofpbuf *
5525 make_echo_reply(const struct ofp_header *rq)
5526 {
5527 struct ofpbuf rq_buf;
5528 struct ofpbuf *reply;
5529
5530 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
5531 ofpraw_pull_assert(&rq_buf);
5532
5533 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, ofpbuf_size(&rq_buf));
5534 ofpbuf_put(reply, ofpbuf_data(&rq_buf), ofpbuf_size(&rq_buf));
5535 return reply;
5536 }
5537
5538 struct ofpbuf *
5539 ofputil_encode_barrier_request(enum ofp_version ofp_version)
5540 {
5541 enum ofpraw type;
5542
5543 switch (ofp_version) {
5544 case OFP15_VERSION:
5545 case OFP14_VERSION:
5546 case OFP13_VERSION:
5547 case OFP12_VERSION:
5548 case OFP11_VERSION:
5549 type = OFPRAW_OFPT11_BARRIER_REQUEST;
5550 break;
5551
5552 case OFP10_VERSION:
5553 type = OFPRAW_OFPT10_BARRIER_REQUEST;
5554 break;
5555
5556 default:
5557 OVS_NOT_REACHED();
5558 }
5559
5560 return ofpraw_alloc(type, ofp_version, 0);
5561 }
5562
5563 const char *
5564 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
5565 {
5566 switch (flags & OFPC_FRAG_MASK) {
5567 case OFPC_FRAG_NORMAL: return "normal";
5568 case OFPC_FRAG_DROP: return "drop";
5569 case OFPC_FRAG_REASM: return "reassemble";
5570 case OFPC_FRAG_NX_MATCH: return "nx-match";
5571 }
5572
5573 OVS_NOT_REACHED();
5574 }
5575
5576 bool
5577 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
5578 {
5579 if (!strcasecmp(s, "normal")) {
5580 *flags = OFPC_FRAG_NORMAL;
5581 } else if (!strcasecmp(s, "drop")) {
5582 *flags = OFPC_FRAG_DROP;
5583 } else if (!strcasecmp(s, "reassemble")) {
5584 *flags = OFPC_FRAG_REASM;
5585 } else if (!strcasecmp(s, "nx-match")) {
5586 *flags = OFPC_FRAG_NX_MATCH;
5587 } else {
5588 return false;
5589 }
5590 return true;
5591 }
5592
5593 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
5594 * port number and stores the latter in '*ofp10_port', for the purpose of
5595 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
5596 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
5597 *
5598 * See the definition of OFP11_MAX for an explanation of the mapping. */
5599 enum ofperr
5600 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
5601 {
5602 uint32_t ofp11_port_h = ntohl(ofp11_port);
5603
5604 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
5605 *ofp10_port = u16_to_ofp(ofp11_port_h);
5606 return 0;
5607 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
5608 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
5609 return 0;
5610 } else {
5611 *ofp10_port = OFPP_NONE;
5612 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
5613 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
5614 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
5615 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
5616 return OFPERR_OFPBAC_BAD_OUT_PORT;
5617 }
5618 }
5619
5620 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
5621 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
5622 *
5623 * See the definition of OFP11_MAX for an explanation of the mapping. */
5624 ovs_be32
5625 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
5626 {
5627 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
5628 ? ofp_to_u16(ofp10_port)
5629 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
5630 }
5631
5632 #define OFPUTIL_NAMED_PORTS \
5633 OFPUTIL_NAMED_PORT(IN_PORT) \
5634 OFPUTIL_NAMED_PORT(TABLE) \
5635 OFPUTIL_NAMED_PORT(NORMAL) \
5636 OFPUTIL_NAMED_PORT(FLOOD) \
5637 OFPUTIL_NAMED_PORT(ALL) \
5638 OFPUTIL_NAMED_PORT(CONTROLLER) \
5639 OFPUTIL_NAMED_PORT(LOCAL) \
5640 OFPUTIL_NAMED_PORT(ANY)
5641
5642 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
5643 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
5644 OFPUTIL_NAMED_PORTS \
5645 OFPUTIL_NAMED_PORT(NONE)
5646
5647 /* Stores the port number represented by 's' into '*portp'. 's' may be an
5648 * integer or, for reserved ports, the standard OpenFlow name for the port
5649 * (e.g. "LOCAL").
5650 *
5651 * Returns true if successful, false if 's' is not a valid OpenFlow port number
5652 * or name. The caller should issue an error message in this case, because
5653 * this function usually does not. (This gives the caller an opportunity to
5654 * look up the port name another way, e.g. by contacting the switch and listing
5655 * the names of all its ports).
5656 *
5657 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
5658 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
5659 * range as described in include/openflow/openflow-1.1.h. */
5660 bool
5661 ofputil_port_from_string(const char *s, ofp_port_t *portp)
5662 {
5663 unsigned int port32; /* int is at least 32 bits wide. */
5664
5665 if (*s == '-') {
5666 VLOG_WARN("Negative value %s is not a valid port number.", s);
5667 return false;
5668 }
5669 *portp = 0;
5670 if (str_to_uint(s, 10, &port32)) {
5671 if (port32 < ofp_to_u16(OFPP_MAX)) {
5672 /* Pass. */
5673 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
5674 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
5675 "be translated to %u when talking to an OF1.1 or "
5676 "later controller", port32, port32 + OFPP11_OFFSET);
5677 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
5678 char name[OFP_MAX_PORT_NAME_LEN];
5679
5680 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
5681 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
5682 "for compatibility with OpenFlow 1.1 and later",
5683 name, port32);
5684 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
5685 VLOG_WARN("port %u is outside the supported range 0 through "
5686 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
5687 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
5688 return false;
5689 } else {
5690 port32 -= OFPP11_OFFSET;
5691 }
5692
5693 *portp = u16_to_ofp(port32);
5694 return true;
5695 } else {
5696 struct pair {
5697 const char *name;
5698 ofp_port_t value;
5699 };
5700 static const struct pair pairs[] = {
5701 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
5702 OFPUTIL_NAMED_PORTS_WITH_NONE
5703 #undef OFPUTIL_NAMED_PORT
5704 };
5705 const struct pair *p;
5706
5707 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
5708 if (!strcasecmp(s, p->name)) {
5709 *portp = p->value;
5710 return true;
5711 }
5712 }
5713 return false;
5714 }
5715 }
5716
5717 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
5718 * Most ports' string representation is just the port number, but for special
5719 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
5720 void
5721 ofputil_format_port(ofp_port_t port, struct ds *s)
5722 {
5723 char name[OFP_MAX_PORT_NAME_LEN];
5724
5725 ofputil_port_to_string(port, name, sizeof name);
5726 ds_put_cstr(s, name);
5727 }
5728
5729 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
5730 * representation of OpenFlow port number 'port'. Most ports are represented
5731 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
5732 * by name, e.g. "LOCAL". */
5733 void
5734 ofputil_port_to_string(ofp_port_t port,
5735 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
5736 {
5737 switch (port) {
5738 #define OFPUTIL_NAMED_PORT(NAME) \
5739 case OFPP_##NAME: \
5740 ovs_strlcpy(namebuf, #NAME, bufsize); \
5741 break;
5742 OFPUTIL_NAMED_PORTS
5743 #undef OFPUTIL_NAMED_PORT
5744
5745 default:
5746 snprintf(namebuf, bufsize, "%"PRIu16, port);
5747 break;
5748 }
5749 }
5750
5751 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
5752 * integer or, for reserved group IDs, the standard OpenFlow name for the group
5753 * (either "ANY" or "ALL").
5754 *
5755 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
5756 * name. */
5757 bool
5758 ofputil_group_from_string(const char *s, uint32_t *group_idp)
5759 {
5760 if (!strcasecmp(s, "any")) {
5761 *group_idp = OFPG11_ANY;
5762 } else if (!strcasecmp(s, "all")) {
5763 *group_idp = OFPG11_ALL;
5764 } else if (!str_to_uint(s, 10, group_idp)) {
5765 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
5766 "32-bit nonnegative integers or the keywords ANY or "
5767 "ALL.)", s);
5768 return false;
5769 }
5770
5771 return true;
5772 }
5773
5774 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
5775 * Most groups' string representation is just the number, but for special
5776 * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */
5777 void
5778 ofputil_format_group(uint32_t group_id, struct ds *s)
5779 {
5780 char name[MAX_GROUP_NAME_LEN];
5781
5782 ofputil_group_to_string(group_id, name, sizeof name);
5783 ds_put_cstr(s, name);
5784 }
5785
5786
5787 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
5788 * representation of OpenFlow group ID 'group_id'. Most group are represented
5789 * as just their number, but special groups, e.g. OFPG11_ALL, are represented
5790 * by name, e.g. "ALL". */
5791 void
5792 ofputil_group_to_string(uint32_t group_id,
5793 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
5794 {
5795 switch (group_id) {
5796 case OFPG11_ALL:
5797 ovs_strlcpy(namebuf, "ALL", bufsize);
5798 break;
5799
5800 case OFPG11_ANY:
5801 ovs_strlcpy(namebuf, "ANY", bufsize);
5802 break;
5803
5804 default:
5805 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
5806 break;
5807 }
5808 }
5809
5810 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
5811 * 'ofp_version', tries to pull the first element from the array. If
5812 * successful, initializes '*pp' with an abstract representation of the
5813 * port and returns 0. If no ports remain to be decoded, returns EOF.
5814 * On an error, returns a positive OFPERR_* value. */
5815 int
5816 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
5817 struct ofputil_phy_port *pp)
5818 {
5819 memset(pp, 0, sizeof *pp);
5820
5821 switch (ofp_version) {
5822 case OFP10_VERSION: {
5823 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
5824 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
5825 }
5826 case OFP11_VERSION:
5827 case OFP12_VERSION:
5828 case OFP13_VERSION: {
5829 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
5830 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
5831 }
5832 case OFP14_VERSION:
5833 case OFP15_VERSION:
5834 return ofpbuf_size(b) ? ofputil_pull_ofp14_port(pp, b) : EOF;
5835 default:
5836 OVS_NOT_REACHED();
5837 }
5838 }
5839
5840 /* ofp-util.def lists the mapping from names to action. */
5841 static const char *const names[OFPUTIL_N_ACTIONS] = {
5842 NULL,
5843 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
5844 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
5845 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
5846 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
5847 #include "ofp-util.def"
5848 };
5849
5850 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
5851 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1
5852 * if 'name' is not the name of any action. */
5853 int
5854 ofputil_action_code_from_name(const char *name)
5855 {
5856 const char *const *p;
5857
5858 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
5859 if (*p && !strcasecmp(name, *p)) {
5860 return p - names;
5861 }
5862 }
5863 return -1;
5864 }
5865
5866 /* Returns name corresponding to the 'enum ofputil_action_code',
5867 * or "Unkonwn action", if the name is not available. */
5868 const char *
5869 ofputil_action_name_from_code(enum ofputil_action_code code)
5870 {
5871 return code < (int)OFPUTIL_N_ACTIONS && names[code] ? names[code]
5872 : "Unknown action";
5873 }
5874
5875 enum ofputil_action_code
5876 ofputil_action_code_from_ofp13_action(enum ofp13_action_type type)
5877 {
5878 switch (type) {
5879
5880 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5881 case ENUM: \
5882 return OFPUTIL_##ENUM;
5883 #include "ofp-util.def"
5884
5885 default:
5886 return OFPUTIL_ACTION_INVALID;
5887 }
5888 }
5889
5890 /* Appends an action of the type specified by 'code' to 'buf' and returns the
5891 * action. Initializes the parts of 'action' that identify it as having type
5892 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
5893 * have variable length, the length used and cleared is that of struct
5894 * <STRUCT>. */
5895 void *
5896 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
5897 {
5898 switch (code) {
5899 case OFPUTIL_ACTION_INVALID:
5900 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
5901 #include "ofp-util.def"
5902 OVS_NOT_REACHED();
5903
5904 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
5905 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
5906 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5907 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
5908 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5909 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
5910 #include "ofp-util.def"
5911 }
5912 OVS_NOT_REACHED();
5913 }
5914
5915 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
5916 void \
5917 ofputil_init_##ENUM(struct STRUCT *s) \
5918 { \
5919 memset(s, 0, sizeof *s); \
5920 s->type = htons(ENUM); \
5921 s->len = htons(sizeof *s); \
5922 } \
5923 \
5924 struct STRUCT * \
5925 ofputil_put_##ENUM(struct ofpbuf *buf) \
5926 { \
5927 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
5928 ofputil_init_##ENUM(s); \
5929 return s; \
5930 }
5931 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5932 OFPAT10_ACTION(ENUM, STRUCT, NAME)
5933 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5934 OFPAT10_ACTION(ENUM, STRUCT, NAME)
5935 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5936 void \
5937 ofputil_init_##ENUM(struct STRUCT *s) \
5938 { \
5939 memset(s, 0, sizeof *s); \
5940 s->type = htons(OFPAT10_VENDOR); \
5941 s->len = htons(sizeof *s); \
5942 s->vendor = htonl(NX_VENDOR_ID); \
5943 s->subtype = htons(ENUM); \
5944 } \
5945 \
5946 struct STRUCT * \
5947 ofputil_put_##ENUM(struct ofpbuf *buf) \
5948 { \
5949 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
5950 ofputil_init_##ENUM(s); \
5951 return s; \
5952 }
5953 #include "ofp-util.def"
5954
5955 static void
5956 ofputil_normalize_match__(struct match *match, bool may_log)
5957 {
5958 enum {
5959 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
5960 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
5961 MAY_NW_PROTO = 1 << 2, /* nw_proto */
5962 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
5963 MAY_ARP_SHA = 1 << 4, /* arp_sha */
5964 MAY_ARP_THA = 1 << 5, /* arp_tha */
5965 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
5966 MAY_ND_TARGET = 1 << 7, /* nd_target */
5967 MAY_MPLS = 1 << 8, /* mpls label and tc */
5968 } may_match;
5969
5970 struct flow_wildcards wc;
5971
5972 /* Figure out what fields may be matched. */
5973 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
5974 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
5975 if (match->flow.nw_proto == IPPROTO_TCP ||
5976 match->flow.nw_proto == IPPROTO_UDP ||
5977 match->flow.nw_proto == IPPROTO_SCTP ||
5978 match->flow.nw_proto == IPPROTO_ICMP) {
5979 may_match |= MAY_TP_ADDR;
5980 }
5981 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
5982 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
5983 if (match->flow.nw_proto == IPPROTO_TCP ||
5984 match->flow.nw_proto == IPPROTO_UDP ||
5985 match->flow.nw_proto == IPPROTO_SCTP) {
5986 may_match |= MAY_TP_ADDR;
5987 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
5988 may_match |= MAY_TP_ADDR;
5989 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
5990 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
5991 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
5992 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
5993 }
5994 }
5995 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
5996 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
5997 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
5998 } else if (eth_type_mpls(match->flow.dl_type)) {
5999 may_match = MAY_MPLS;
6000 } else {
6001 may_match = 0;
6002 }
6003
6004 /* Clear the fields that may not be matched. */
6005 wc = match->wc;
6006 if (!(may_match & MAY_NW_ADDR)) {
6007 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6008 }
6009 if (!(may_match & MAY_TP_ADDR)) {
6010 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6011 }
6012 if (!(may_match & MAY_NW_PROTO)) {
6013 wc.masks.nw_proto = 0;
6014 }
6015 if (!(may_match & MAY_IPVx)) {
6016 wc.masks.nw_tos = 0;
6017 wc.masks.nw_ttl = 0;
6018 }
6019 if (!(may_match & MAY_ARP_SHA)) {
6020 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6021 }
6022 if (!(may_match & MAY_ARP_THA)) {
6023 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6024 }
6025 if (!(may_match & MAY_IPV6)) {
6026 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6027 wc.masks.ipv6_label = htonl(0);
6028 }
6029 if (!(may_match & MAY_ND_TARGET)) {
6030 wc.masks.nd_target = in6addr_any;
6031 }
6032 if (!(may_match & MAY_MPLS)) {
6033 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6034 }
6035
6036 /* Log any changes. */
6037 if (!flow_wildcards_equal(&wc, &match->wc)) {
6038 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6039 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6040
6041 match->wc = wc;
6042 match_zero_wildcarded_fields(match);
6043
6044 if (log) {
6045 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6046 VLOG_INFO("normalization changed ofp_match, details:");
6047 VLOG_INFO(" pre: %s", pre);
6048 VLOG_INFO("post: %s", post);
6049 free(pre);
6050 free(post);
6051 }
6052 }
6053 }
6054
6055 /* "Normalizes" the wildcards in 'match'. That means:
6056 *
6057 * 1. If the type of level N is known, then only the valid fields for that
6058 * level may be specified. For example, ARP does not have a TOS field,
6059 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6060 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6061 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6062 * IPv4 flow.
6063 *
6064 * 2. If the type of level N is not known (or not understood by Open
6065 * vSwitch), then no fields at all for that level may be specified. For
6066 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6067 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6068 * SCTP flow.
6069 *
6070 * If this function changes 'match', it logs a rate-limited informational
6071 * message. */
6072 void
6073 ofputil_normalize_match(struct match *match)
6074 {
6075 ofputil_normalize_match__(match, true);
6076 }
6077
6078 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6079 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6080 * sense for use on flows received from elsewhere (so that a bug in the program
6081 * that sent them can be reported and corrected). */
6082 void
6083 ofputil_normalize_match_quiet(struct match *match)
6084 {
6085 ofputil_normalize_match__(match, false);
6086 }
6087
6088 /* Parses a key or a key-value pair from '*stringp'.
6089 *
6090 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6091 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6092 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6093 * are substrings of '*stringp' created by replacing some of its bytes by null
6094 * terminators. Returns true.
6095 *
6096 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6097 * NULL and returns false. */
6098 bool
6099 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6100 {
6101 char *pos, *key, *value;
6102 size_t key_len;
6103
6104 pos = *stringp;
6105 pos += strspn(pos, ", \t\r\n");
6106 if (*pos == '\0') {
6107 *keyp = *valuep = NULL;
6108 return false;
6109 }
6110
6111 key = pos;
6112 key_len = strcspn(pos, ":=(, \t\r\n");
6113 if (key[key_len] == ':' || key[key_len] == '=') {
6114 /* The value can be separated by a colon. */
6115 size_t value_len;
6116
6117 value = key + key_len + 1;
6118 value_len = strcspn(value, ", \t\r\n");
6119 pos = value + value_len + (value[value_len] != '\0');
6120 value[value_len] = '\0';
6121 } else if (key[key_len] == '(') {
6122 /* The value can be surrounded by balanced parentheses. The outermost
6123 * set of parentheses is removed. */
6124 int level = 1;
6125 size_t value_len;
6126
6127 value = key + key_len + 1;
6128 for (value_len = 0; level > 0; value_len++) {
6129 switch (value[value_len]) {
6130 case '\0':
6131 level = 0;
6132 break;
6133
6134 case '(':
6135 level++;
6136 break;
6137
6138 case ')':
6139 level--;
6140 break;
6141 }
6142 }
6143 value[value_len - 1] = '\0';
6144 pos = value + value_len;
6145 } else {
6146 /* There might be no value at all. */
6147 value = key + key_len; /* Will become the empty string below. */
6148 pos = key + key_len + (key[key_len] != '\0');
6149 }
6150 key[key_len] = '\0';
6151
6152 *stringp = pos;
6153 *keyp = key;
6154 *valuep = value;
6155 return true;
6156 }
6157
6158 /* Encode a dump ports request for 'port', the encoded message
6159 * will be for Open Flow version 'ofp_version'. Returns message
6160 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6161 struct ofpbuf *
6162 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
6163 {
6164 struct ofpbuf *request;
6165
6166 switch (ofp_version) {
6167 case OFP10_VERSION: {
6168 struct ofp10_port_stats_request *req;
6169 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
6170 req = ofpbuf_put_zeros(request, sizeof *req);
6171 req->port_no = htons(ofp_to_u16(port));
6172 break;
6173 }
6174 case OFP11_VERSION:
6175 case OFP12_VERSION:
6176 case OFP13_VERSION:
6177 case OFP14_VERSION:
6178 case OFP15_VERSION: {
6179 struct ofp11_port_stats_request *req;
6180 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
6181 req = ofpbuf_put_zeros(request, sizeof *req);
6182 req->port_no = ofputil_port_to_ofp11(port);
6183 break;
6184 }
6185 default:
6186 OVS_NOT_REACHED();
6187 }
6188
6189 return request;
6190 }
6191
6192 static void
6193 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
6194 struct ofp10_port_stats *ps10)
6195 {
6196 ps10->port_no = htons(ofp_to_u16(ops->port_no));
6197 memset(ps10->pad, 0, sizeof ps10->pad);
6198 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
6199 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
6200 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
6201 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
6202 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
6203 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
6204 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
6205 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
6206 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
6207 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
6208 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
6209 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
6210 }
6211
6212 static void
6213 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
6214 struct ofp11_port_stats *ps11)
6215 {
6216 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
6217 memset(ps11->pad, 0, sizeof ps11->pad);
6218 ps11->rx_packets = htonll(ops->stats.rx_packets);
6219 ps11->tx_packets = htonll(ops->stats.tx_packets);
6220 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
6221 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
6222 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
6223 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
6224 ps11->rx_errors = htonll(ops->stats.rx_errors);
6225 ps11->tx_errors = htonll(ops->stats.tx_errors);
6226 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6227 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
6228 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6229 ps11->collisions = htonll(ops->stats.collisions);
6230 }
6231
6232 static void
6233 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
6234 struct ofp13_port_stats *ps13)
6235 {
6236 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
6237 ps13->duration_sec = htonl(ops->duration_sec);
6238 ps13->duration_nsec = htonl(ops->duration_nsec);
6239 }
6240
6241 static void
6242 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
6243 struct list *replies)
6244 {
6245 struct ofp14_port_stats_prop_ethernet *eth;
6246 struct ofp14_port_stats *ps14;
6247 struct ofpbuf *reply;
6248
6249 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
6250
6251 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
6252 ps14->length = htons(sizeof *ps14 + sizeof *eth);
6253 memset(ps14->pad, 0, sizeof ps14->pad);
6254 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
6255 ps14->duration_sec = htonl(ops->duration_sec);
6256 ps14->duration_nsec = htonl(ops->duration_nsec);
6257 ps14->rx_packets = htonll(ops->stats.rx_packets);
6258 ps14->tx_packets = htonll(ops->stats.tx_packets);
6259 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
6260 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
6261 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
6262 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
6263 ps14->rx_errors = htonll(ops->stats.rx_errors);
6264 ps14->tx_errors = htonll(ops->stats.tx_errors);
6265
6266 eth = ofpbuf_put_uninit(reply, sizeof *eth);
6267 eth->type = htons(OFPPSPT14_ETHERNET);
6268 eth->length = htons(sizeof *eth);
6269 memset(eth->pad, 0, sizeof eth->pad);
6270 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6271 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
6272 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6273 eth->collisions = htonll(ops->stats.collisions);
6274 }
6275
6276 /* Encode a ports stat for 'ops' and append it to 'replies'. */
6277 void
6278 ofputil_append_port_stat(struct list *replies,
6279 const struct ofputil_port_stats *ops)
6280 {
6281 switch (ofpmp_version(replies)) {
6282 case OFP13_VERSION: {
6283 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6284 ofputil_port_stats_to_ofp13(ops, reply);
6285 break;
6286 }
6287 case OFP12_VERSION:
6288 case OFP11_VERSION: {
6289 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6290 ofputil_port_stats_to_ofp11(ops, reply);
6291 break;
6292 }
6293
6294 case OFP10_VERSION: {
6295 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6296 ofputil_port_stats_to_ofp10(ops, reply);
6297 break;
6298 }
6299
6300 case OFP14_VERSION:
6301 case OFP15_VERSION:
6302 ofputil_append_ofp14_port_stats(ops, replies);
6303 break;
6304
6305 default:
6306 OVS_NOT_REACHED();
6307 }
6308 }
6309
6310 static enum ofperr
6311 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
6312 const struct ofp10_port_stats *ps10)
6313 {
6314 memset(ops, 0, sizeof *ops);
6315
6316 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
6317 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
6318 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
6319 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
6320 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
6321 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
6322 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
6323 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
6324 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
6325 ops->stats.rx_frame_errors =
6326 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
6327 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
6328 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
6329 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
6330 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6331
6332 return 0;
6333 }
6334
6335 static enum ofperr
6336 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
6337 const struct ofp11_port_stats *ps11)
6338 {
6339 enum ofperr error;
6340
6341 memset(ops, 0, sizeof *ops);
6342 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
6343 if (error) {
6344 return error;
6345 }
6346
6347 ops->stats.rx_packets = ntohll(ps11->rx_packets);
6348 ops->stats.tx_packets = ntohll(ps11->tx_packets);
6349 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
6350 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
6351 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
6352 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
6353 ops->stats.rx_errors = ntohll(ps11->rx_errors);
6354 ops->stats.tx_errors = ntohll(ps11->tx_errors);
6355 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
6356 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
6357 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
6358 ops->stats.collisions = ntohll(ps11->collisions);
6359 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6360
6361 return 0;
6362 }
6363
6364 static enum ofperr
6365 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
6366 const struct ofp13_port_stats *ps13)
6367 {
6368 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
6369 if (!error) {
6370 ops->duration_sec = ntohl(ps13->duration_sec);
6371 ops->duration_nsec = ntohl(ps13->duration_nsec);
6372 }
6373 return error;
6374 }
6375
6376 static enum ofperr
6377 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
6378 struct ofputil_port_stats *ops)
6379 {
6380 const struct ofp14_port_stats_prop_ethernet *eth = ofpbuf_data(payload);
6381
6382 if (ofpbuf_size(payload) != sizeof *eth) {
6383 return OFPERR_OFPBPC_BAD_LEN;
6384 }
6385
6386 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
6387 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
6388 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
6389 ops->stats.collisions = ntohll(eth->collisions);
6390
6391 return 0;
6392 }
6393
6394 static enum ofperr
6395 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
6396 struct ofpbuf *msg)
6397 {
6398 const struct ofp14_port_stats *ps14;
6399 struct ofpbuf properties;
6400 enum ofperr error;
6401 size_t len;
6402
6403 ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
6404 if (!ps14) {
6405 return OFPERR_OFPBRC_BAD_LEN;
6406 }
6407
6408 len = ntohs(ps14->length);
6409 if (len < sizeof *ps14 || len - sizeof *ps14 > ofpbuf_size(msg)) {
6410 return OFPERR_OFPBRC_BAD_LEN;
6411 }
6412 len -= sizeof *ps14;
6413 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
6414
6415 error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
6416 if (error) {
6417 return error;
6418 }
6419
6420 ops->duration_sec = ntohl(ps14->duration_sec);
6421 ops->duration_nsec = ntohl(ps14->duration_nsec);
6422 ops->stats.rx_packets = ntohll(ps14->rx_packets);
6423 ops->stats.tx_packets = ntohll(ps14->tx_packets);
6424 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
6425 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
6426 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
6427 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
6428 ops->stats.rx_errors = ntohll(ps14->rx_errors);
6429 ops->stats.tx_errors = ntohll(ps14->tx_errors);
6430 ops->stats.rx_frame_errors = UINT64_MAX;
6431 ops->stats.rx_over_errors = UINT64_MAX;
6432 ops->stats.rx_crc_errors = UINT64_MAX;
6433 ops->stats.collisions = UINT64_MAX;
6434
6435 while (ofpbuf_size(&properties) > 0) {
6436 struct ofpbuf payload;
6437 enum ofperr error;
6438 uint16_t type;
6439
6440 error = ofputil_pull_property(&properties, &payload, &type);
6441 if (error) {
6442 return error;
6443 }
6444
6445 switch (type) {
6446 case OFPPSPT14_ETHERNET:
6447 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
6448 break;
6449
6450 default:
6451 log_property(true, "unknown port stats property %"PRIu16, type);
6452 error = 0;
6453 break;
6454 }
6455
6456 if (error) {
6457 return error;
6458 }
6459 }
6460
6461 return 0;
6462 }
6463
6464 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
6465 * message 'oh'. */
6466 size_t
6467 ofputil_count_port_stats(const struct ofp_header *oh)
6468 {
6469 struct ofputil_port_stats ps;
6470 struct ofpbuf b;
6471 size_t n = 0;
6472
6473 ofpbuf_use_const(&b, oh, ntohs(oh->length));
6474 ofpraw_pull_assert(&b);
6475 while (!ofputil_decode_port_stats(&ps, &b)) {
6476 n++;
6477 }
6478 return n;
6479 }
6480
6481 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
6482 * ofputil_port_stats in 'ps'.
6483 *
6484 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
6485 * message. Calling this function multiple times for a single 'msg' iterates
6486 * through the replies. The caller must initially leave 'msg''s layer pointers
6487 * null and not modify them between calls.
6488 *
6489 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6490 * otherwise a positive errno value. */
6491 int
6492 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
6493 {
6494 enum ofperr error;
6495 enum ofpraw raw;
6496
6497 error = (msg->frame
6498 ? ofpraw_decode(&raw, msg->frame)
6499 : ofpraw_pull(&raw, msg));
6500 if (error) {
6501 return error;
6502 }
6503
6504 if (!ofpbuf_size(msg)) {
6505 return EOF;
6506 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
6507 return ofputil_pull_ofp14_port_stats(ps, msg);
6508 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
6509 const struct ofp13_port_stats *ps13;
6510
6511 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
6512 if (!ps13) {
6513 goto bad_len;
6514 }
6515 return ofputil_port_stats_from_ofp13(ps, ps13);
6516 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
6517 const struct ofp11_port_stats *ps11;
6518
6519 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
6520 if (!ps11) {
6521 goto bad_len;
6522 }
6523 return ofputil_port_stats_from_ofp11(ps, ps11);
6524 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
6525 const struct ofp10_port_stats *ps10;
6526
6527 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
6528 if (!ps10) {
6529 goto bad_len;
6530 }
6531 return ofputil_port_stats_from_ofp10(ps, ps10);
6532 } else {
6533 OVS_NOT_REACHED();
6534 }
6535
6536 bad_len:
6537 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
6538 "bytes at end", ofpbuf_size(msg));
6539 return OFPERR_OFPBRC_BAD_LEN;
6540 }
6541
6542 /* Parse a port status request message into a 16 bit OpenFlow 1.0
6543 * port number and stores the latter in '*ofp10_port'.
6544 * Returns 0 if successful, otherwise an OFPERR_* number. */
6545 enum ofperr
6546 ofputil_decode_port_stats_request(const struct ofp_header *request,
6547 ofp_port_t *ofp10_port)
6548 {
6549 switch ((enum ofp_version)request->version) {
6550 case OFP15_VERSION:
6551 case OFP14_VERSION:
6552 case OFP13_VERSION:
6553 case OFP12_VERSION:
6554 case OFP11_VERSION: {
6555 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
6556 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
6557 }
6558
6559 case OFP10_VERSION: {
6560 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
6561 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
6562 return 0;
6563 }
6564
6565 default:
6566 OVS_NOT_REACHED();
6567 }
6568 }
6569
6570 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
6571 void
6572 ofputil_bucket_list_destroy(struct list *buckets)
6573 {
6574 struct ofputil_bucket *bucket, *next_bucket;
6575
6576 LIST_FOR_EACH_SAFE (bucket, next_bucket, list_node, buckets) {
6577 list_remove(&bucket->list_node);
6578 free(bucket->ofpacts);
6579 free(bucket);
6580 }
6581 }
6582
6583 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
6584 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
6585 * for all groups.)
6586 *
6587 * Group statistics include packet and byte counts for each group. */
6588 struct ofpbuf *
6589 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
6590 uint32_t group_id)
6591 {
6592 struct ofpbuf *request;
6593
6594 switch (ofp_version) {
6595 case OFP10_VERSION:
6596 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
6597 "(\'-O OpenFlow11\')");
6598 case OFP11_VERSION:
6599 case OFP12_VERSION:
6600 case OFP13_VERSION:
6601 case OFP14_VERSION:
6602 case OFP15_VERSION: {
6603 struct ofp11_group_stats_request *req;
6604 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
6605 req = ofpbuf_put_zeros(request, sizeof *req);
6606 req->group_id = htonl(group_id);
6607 break;
6608 }
6609 default:
6610 OVS_NOT_REACHED();
6611 }
6612
6613 return request;
6614 }
6615
6616 /* Decodes the OpenFlow group description request in 'oh', returning the group
6617 * whose description is requested, or OFPG_ALL if stats for all groups was
6618 * requested. */
6619 uint32_t
6620 ofputil_decode_group_desc_request(const struct ofp_header *oh)
6621 {
6622 struct ofpbuf request;
6623 enum ofpraw raw;
6624
6625 ofpbuf_use_const(&request, oh, ntohs(oh->length));
6626 raw = ofpraw_pull_assert(&request);
6627 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
6628 return OFPG_ALL;
6629 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
6630 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
6631 return ntohl(*group_id);
6632 } else {
6633 OVS_NOT_REACHED();
6634 }
6635 }
6636
6637 /* Returns an OpenFlow group description request for OpenFlow version
6638 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
6639 * request stats for all groups (OpenFlow 1.4 and earlier always request all
6640 * groups).
6641 *
6642 * Group descriptions include the bucket and action configuration for each
6643 * group. */
6644 struct ofpbuf *
6645 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
6646 uint32_t group_id)
6647 {
6648 struct ofpbuf *request;
6649 ovs_be32 gid;
6650
6651 switch (ofp_version) {
6652 case OFP10_VERSION:
6653 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
6654 "(\'-O OpenFlow11\')");
6655 case OFP11_VERSION:
6656 case OFP12_VERSION:
6657 case OFP13_VERSION:
6658 case OFP14_VERSION:
6659 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
6660 ofp_version, 0);
6661 break;
6662 case OFP15_VERSION:
6663 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
6664 ofp_version, 0);
6665 gid = htonl(group_id);
6666 ofpbuf_put(request, &gid, sizeof gid);
6667 break;
6668 default:
6669 OVS_NOT_REACHED();
6670 }
6671
6672 return request;
6673 }
6674
6675 static void
6676 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
6677 struct ofp11_bucket_counter bucket_cnts[])
6678 {
6679 int i;
6680
6681 for (i = 0; i < gs->n_buckets; i++) {
6682 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
6683 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
6684 }
6685 }
6686
6687 static void
6688 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
6689 struct ofp11_group_stats *gs11, size_t length,
6690 struct ofp11_bucket_counter bucket_cnts[])
6691 {
6692 memset(gs11, 0, sizeof *gs11);
6693 gs11->length = htons(length);
6694 gs11->group_id = htonl(gs->group_id);
6695 gs11->ref_count = htonl(gs->ref_count);
6696 gs11->packet_count = htonll(gs->packet_count);
6697 gs11->byte_count = htonll(gs->byte_count);
6698 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
6699 }
6700
6701 static void
6702 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
6703 struct ofp13_group_stats *gs13, size_t length,
6704 struct ofp11_bucket_counter bucket_cnts[])
6705 {
6706 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
6707 gs13->duration_sec = htonl(gs->duration_sec);
6708 gs13->duration_nsec = htonl(gs->duration_nsec);
6709
6710 }
6711
6712 /* Encodes 'gs' properly for the format of the list of group statistics
6713 * replies already begun in 'replies' and appends it to the list. 'replies'
6714 * must have originally been initialized with ofpmp_init(). */
6715 void
6716 ofputil_append_group_stats(struct list *replies,
6717 const struct ofputil_group_stats *gs)
6718 {
6719 size_t bucket_counter_size;
6720 struct ofp11_bucket_counter *bucket_counters;
6721 size_t length;
6722
6723 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
6724
6725 switch (ofpmp_version(replies)) {
6726 case OFP11_VERSION:
6727 case OFP12_VERSION:{
6728 struct ofp11_group_stats *gs11;
6729
6730 length = sizeof *gs11 + bucket_counter_size;
6731 gs11 = ofpmp_append(replies, length);
6732 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
6733 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
6734 break;
6735 }
6736
6737 case OFP13_VERSION:
6738 case OFP14_VERSION:
6739 case OFP15_VERSION: {
6740 struct ofp13_group_stats *gs13;
6741
6742 length = sizeof *gs13 + bucket_counter_size;
6743 gs13 = ofpmp_append(replies, length);
6744 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
6745 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
6746 break;
6747 }
6748
6749 case OFP10_VERSION:
6750 default:
6751 OVS_NOT_REACHED();
6752 }
6753 }
6754 /* Returns an OpenFlow group features request for OpenFlow version
6755 * 'ofp_version'. */
6756 struct ofpbuf *
6757 ofputil_encode_group_features_request(enum ofp_version ofp_version)
6758 {
6759 struct ofpbuf *request = NULL;
6760
6761 switch (ofp_version) {
6762 case OFP10_VERSION:
6763 case OFP11_VERSION:
6764 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
6765 "(\'-O OpenFlow12\')");
6766 case OFP12_VERSION:
6767 case OFP13_VERSION:
6768 case OFP14_VERSION:
6769 case OFP15_VERSION:
6770 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
6771 ofp_version, 0);
6772 break;
6773 default:
6774 OVS_NOT_REACHED();
6775 }
6776
6777 return request;
6778 }
6779
6780 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
6781 * group features request 'request'. */
6782 struct ofpbuf *
6783 ofputil_encode_group_features_reply(
6784 const struct ofputil_group_features *features,
6785 const struct ofp_header *request)
6786 {
6787 struct ofp12_group_features_stats *ogf;
6788 struct ofpbuf *reply;
6789
6790 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
6791 request->version, request->xid, 0);
6792 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
6793 ogf->types = htonl(features->types);
6794 ogf->capabilities = htonl(features->capabilities);
6795 ogf->max_groups[0] = htonl(features->max_groups[0]);
6796 ogf->max_groups[1] = htonl(features->max_groups[1]);
6797 ogf->max_groups[2] = htonl(features->max_groups[2]);
6798 ogf->max_groups[3] = htonl(features->max_groups[3]);
6799 ogf->actions[0] = htonl(features->actions[0]);
6800 ogf->actions[1] = htonl(features->actions[1]);
6801 ogf->actions[2] = htonl(features->actions[2]);
6802 ogf->actions[3] = htonl(features->actions[3]);
6803
6804 return reply;
6805 }
6806
6807 /* Decodes group features reply 'oh' into 'features'. */
6808 void
6809 ofputil_decode_group_features_reply(const struct ofp_header *oh,
6810 struct ofputil_group_features *features)
6811 {
6812 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
6813
6814 features->types = ntohl(ogf->types);
6815 features->capabilities = ntohl(ogf->capabilities);
6816 features->max_groups[0] = ntohl(ogf->max_groups[0]);
6817 features->max_groups[1] = ntohl(ogf->max_groups[1]);
6818 features->max_groups[2] = ntohl(ogf->max_groups[2]);
6819 features->max_groups[3] = ntohl(ogf->max_groups[3]);
6820 features->actions[0] = ntohl(ogf->actions[0]);
6821 features->actions[1] = ntohl(ogf->actions[1]);
6822 features->actions[2] = ntohl(ogf->actions[2]);
6823 features->actions[3] = ntohl(ogf->actions[3]);
6824 }
6825
6826 /* Parse a group status request message into a 32 bit OpenFlow 1.1
6827 * group ID and stores the latter in '*group_id'.
6828 * Returns 0 if successful, otherwise an OFPERR_* number. */
6829 enum ofperr
6830 ofputil_decode_group_stats_request(const struct ofp_header *request,
6831 uint32_t *group_id)
6832 {
6833 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
6834 *group_id = ntohl(gsr11->group_id);
6835 return 0;
6836 }
6837
6838 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
6839 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
6840 * caller to eventually free.
6841 *
6842 * Multiple group stats replies can be packed into a single OpenFlow message.
6843 * Calling this function multiple times for a single 'msg' iterates through the
6844 * replies. The caller must initially leave 'msg''s layer pointers null and
6845 * not modify them between calls.
6846 *
6847 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6848 * otherwise a positive errno value. */
6849 int
6850 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
6851 struct ofputil_group_stats *gs)
6852 {
6853 struct ofp11_bucket_counter *obc;
6854 struct ofp11_group_stats *ogs11;
6855 enum ofpraw raw;
6856 enum ofperr error;
6857 size_t base_len;
6858 size_t length;
6859 size_t i;
6860
6861 gs->bucket_stats = NULL;
6862 error = (msg->frame
6863 ? ofpraw_decode(&raw, msg->frame)
6864 : ofpraw_pull(&raw, msg));
6865 if (error) {
6866 return error;
6867 }
6868
6869 if (!ofpbuf_size(msg)) {
6870 return EOF;
6871 }
6872
6873 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
6874 base_len = sizeof *ogs11;
6875 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
6876 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
6877 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
6878 struct ofp13_group_stats *ogs13;
6879
6880 base_len = sizeof *ogs13;
6881 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
6882 if (ogs13) {
6883 ogs11 = &ogs13->gs;
6884 gs->duration_sec = ntohl(ogs13->duration_sec);
6885 gs->duration_nsec = ntohl(ogs13->duration_nsec);
6886 } else {
6887 ogs11 = NULL;
6888 }
6889 } else {
6890 OVS_NOT_REACHED();
6891 }
6892
6893 if (!ogs11) {
6894 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6895 ofpraw_get_name(raw), ofpbuf_size(msg));
6896 return OFPERR_OFPBRC_BAD_LEN;
6897 }
6898 length = ntohs(ogs11->length);
6899 if (length < sizeof base_len) {
6900 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
6901 ofpraw_get_name(raw), length);
6902 return OFPERR_OFPBRC_BAD_LEN;
6903 }
6904
6905 gs->group_id = ntohl(ogs11->group_id);
6906 gs->ref_count = ntohl(ogs11->ref_count);
6907 gs->packet_count = ntohll(ogs11->packet_count);
6908 gs->byte_count = ntohll(ogs11->byte_count);
6909
6910 gs->n_buckets = (length - base_len) / sizeof *obc;
6911 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
6912 if (!obc) {
6913 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6914 ofpraw_get_name(raw), ofpbuf_size(msg));
6915 return OFPERR_OFPBRC_BAD_LEN;
6916 }
6917
6918 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
6919 for (i = 0; i < gs->n_buckets; i++) {
6920 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
6921 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
6922 }
6923
6924 return 0;
6925 }
6926
6927 /* Appends a group stats reply that contains the data in 'gds' to those already
6928 * present in the list of ofpbufs in 'replies'. 'replies' should have been
6929 * initialized with ofpmp_init(). */
6930 void
6931 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
6932 struct list *buckets,
6933 struct list *replies)
6934 {
6935 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
6936 enum ofp_version version = ofpmp_version(replies);
6937 struct ofp11_group_desc_stats *ogds;
6938 struct ofputil_bucket *bucket;
6939 size_t start_ogds;
6940
6941 start_ogds = ofpbuf_size(reply);
6942 ofpbuf_put_zeros(reply, sizeof *ogds);
6943 LIST_FOR_EACH (bucket, list_node, buckets) {
6944 struct ofp11_bucket *ob;
6945 size_t start_ob;
6946
6947 start_ob = ofpbuf_size(reply);
6948 ofpbuf_put_zeros(reply, sizeof *ob);
6949 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
6950 reply, version);
6951 ob = ofpbuf_at_assert(reply, start_ob, sizeof *ob);
6952 ob->len = htons(ofpbuf_size(reply) - start_ob);
6953 ob->weight = htons(bucket->weight);
6954 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
6955 ob->watch_group = htonl(bucket->watch_group);
6956 }
6957 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
6958 ogds->length = htons(ofpbuf_size(reply) - start_ogds);
6959 ogds->type = gds->type;
6960 ogds->group_id = htonl(gds->group_id);
6961
6962 ofpmp_postappend(replies, start_ogds);
6963 }
6964
6965 static enum ofperr
6966 ofputil_pull_buckets(struct ofpbuf *msg, size_t buckets_length,
6967 enum ofp_version version, struct list *buckets)
6968 {
6969 struct ofp11_bucket *ob;
6970
6971 list_init(buckets);
6972 while (buckets_length > 0) {
6973 struct ofputil_bucket *bucket;
6974 struct ofpbuf ofpacts;
6975 enum ofperr error;
6976 size_t ob_len;
6977
6978 ob = (buckets_length >= sizeof *ob
6979 ? ofpbuf_try_pull(msg, sizeof *ob)
6980 : NULL);
6981 if (!ob) {
6982 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
6983 buckets_length);
6984 }
6985
6986 ob_len = ntohs(ob->len);
6987 if (ob_len < sizeof *ob) {
6988 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
6989 "%"PRIuSIZE" is not valid", ob_len);
6990 return OFPERR_OFPGMFC_BAD_BUCKET;
6991 } else if (ob_len > buckets_length) {
6992 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
6993 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
6994 ob_len, buckets_length);
6995 return OFPERR_OFPGMFC_BAD_BUCKET;
6996 }
6997 buckets_length -= ob_len;
6998
6999 ofpbuf_init(&ofpacts, 0);
7000 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
7001 version, &ofpacts);
7002 if (error) {
7003 ofpbuf_uninit(&ofpacts);
7004 ofputil_bucket_list_destroy(buckets);
7005 return error;
7006 }
7007
7008 bucket = xzalloc(sizeof *bucket);
7009 bucket->weight = ntohs(ob->weight);
7010 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
7011 if (error) {
7012 ofpbuf_uninit(&ofpacts);
7013 ofputil_bucket_list_destroy(buckets);
7014 return OFPERR_OFPGMFC_BAD_WATCH;
7015 }
7016 bucket->watch_group = ntohl(ob->watch_group);
7017 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7018 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
7019 list_push_back(buckets, &bucket->list_node);
7020 }
7021
7022 return 0;
7023 }
7024
7025 /* Converts a group description reply in 'msg' into an abstract
7026 * ofputil_group_desc in 'gd'.
7027 *
7028 * Multiple group description replies can be packed into a single OpenFlow
7029 * message. Calling this function multiple times for a single 'msg' iterates
7030 * through the replies. The caller must initially leave 'msg''s layer pointers
7031 * null and not modify them between calls.
7032 *
7033 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7034 * otherwise a positive errno value. */
7035 int
7036 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
7037 struct ofpbuf *msg, enum ofp_version version)
7038 {
7039 struct ofp11_group_desc_stats *ogds;
7040 size_t length;
7041
7042 if (!msg->frame) {
7043 ofpraw_pull_assert(msg);
7044 }
7045
7046 if (!ofpbuf_size(msg)) {
7047 return EOF;
7048 }
7049
7050 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
7051 if (!ogds) {
7052 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
7053 "leftover bytes at end", ofpbuf_size(msg));
7054 return OFPERR_OFPBRC_BAD_LEN;
7055 }
7056 gd->type = ogds->type;
7057 gd->group_id = ntohl(ogds->group_id);
7058
7059 length = ntohs(ogds->length);
7060 if (length < sizeof *ogds || length - sizeof *ogds > ofpbuf_size(msg)) {
7061 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
7062 "length %"PRIuSIZE, length);
7063 return OFPERR_OFPBRC_BAD_LEN;
7064 }
7065
7066 return ofputil_pull_buckets(msg, length - sizeof *ogds, version,
7067 &gd->buckets);
7068 }
7069
7070 /* Converts abstract group mod 'gm' into a message for OpenFlow version
7071 * 'ofp_version' and returns the message. */
7072 struct ofpbuf *
7073 ofputil_encode_group_mod(enum ofp_version ofp_version,
7074 const struct ofputil_group_mod *gm)
7075 {
7076 struct ofpbuf *b;
7077 struct ofp11_group_mod *ogm;
7078 size_t start_ogm;
7079 size_t start_bucket;
7080 struct ofputil_bucket *bucket;
7081 struct ofp11_bucket *ob;
7082
7083 switch (ofp_version) {
7084 case OFP10_VERSION: {
7085 if (gm->command == OFPGC11_ADD) {
7086 ovs_fatal(0, "add-group needs OpenFlow 1.1 or later "
7087 "(\'-O OpenFlow11\')");
7088 } else if (gm->command == OFPGC11_MODIFY) {
7089 ovs_fatal(0, "mod-group needs OpenFlow 1.1 or later "
7090 "(\'-O OpenFlow11\')");
7091 } else {
7092 ovs_fatal(0, "del-groups needs OpenFlow 1.1 or later "
7093 "(\'-O OpenFlow11\')");
7094 }
7095 }
7096
7097 case OFP11_VERSION:
7098 case OFP12_VERSION:
7099 case OFP13_VERSION:
7100 case OFP14_VERSION:
7101 case OFP15_VERSION:
7102 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
7103 start_ogm = ofpbuf_size(b);
7104 ofpbuf_put_zeros(b, sizeof *ogm);
7105
7106 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
7107 start_bucket = ofpbuf_size(b);
7108 ofpbuf_put_zeros(b, sizeof *ob);
7109 if (bucket->ofpacts && bucket->ofpacts_len) {
7110 ofpacts_put_openflow_actions(bucket->ofpacts,
7111 bucket->ofpacts_len, b,
7112 ofp_version);
7113 }
7114 ob = ofpbuf_at_assert(b, start_bucket, sizeof *ob);
7115 ob->len = htons(ofpbuf_size(b) - start_bucket);;
7116 ob->weight = htons(bucket->weight);
7117 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7118 ob->watch_group = htonl(bucket->watch_group);
7119 }
7120 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
7121 ogm->command = htons(gm->command);
7122 ogm->type = gm->type;
7123 ogm->group_id = htonl(gm->group_id);
7124
7125 break;
7126
7127 default:
7128 OVS_NOT_REACHED();
7129 }
7130
7131 return b;
7132 }
7133
7134 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
7135 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
7136 enum ofperr
7137 ofputil_decode_group_mod(const struct ofp_header *oh,
7138 struct ofputil_group_mod *gm)
7139 {
7140 const struct ofp11_group_mod *ogm;
7141 struct ofpbuf msg;
7142 struct ofputil_bucket *bucket;
7143 enum ofperr err;
7144
7145 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
7146 ofpraw_pull_assert(&msg);
7147
7148 ogm = ofpbuf_pull(&msg, sizeof *ogm);
7149 gm->command = ntohs(ogm->command);
7150 gm->type = ogm->type;
7151 gm->group_id = ntohl(ogm->group_id);
7152
7153 err = ofputil_pull_buckets(&msg, ofpbuf_size(&msg), oh->version, &gm->buckets);
7154 if (err) {
7155 return err;
7156 }
7157
7158 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
7159 switch (gm->type) {
7160 case OFPGT11_ALL:
7161 case OFPGT11_INDIRECT:
7162 if (ofputil_bucket_has_liveness(bucket)) {
7163 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
7164 }
7165 break;
7166 case OFPGT11_SELECT:
7167 break;
7168 case OFPGT11_FF:
7169 if (!ofputil_bucket_has_liveness(bucket)) {
7170 return OFPERR_OFPGMFC_INVALID_GROUP;
7171 }
7172 break;
7173 default:
7174 OVS_NOT_REACHED();
7175 }
7176 }
7177
7178 return 0;
7179 }
7180
7181 /* Parse a queue status request message into 'oqsr'.
7182 * Returns 0 if successful, otherwise an OFPERR_* number. */
7183 enum ofperr
7184 ofputil_decode_queue_stats_request(const struct ofp_header *request,
7185 struct ofputil_queue_stats_request *oqsr)
7186 {
7187 switch ((enum ofp_version)request->version) {
7188 case OFP15_VERSION:
7189 case OFP14_VERSION:
7190 case OFP13_VERSION:
7191 case OFP12_VERSION:
7192 case OFP11_VERSION: {
7193 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
7194 oqsr->queue_id = ntohl(qsr11->queue_id);
7195 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
7196 }
7197
7198 case OFP10_VERSION: {
7199 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
7200 oqsr->queue_id = ntohl(qsr10->queue_id);
7201 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
7202 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
7203 if (oqsr->port_no == OFPP_ALL) {
7204 oqsr->port_no = OFPP_ANY;
7205 }
7206 return 0;
7207 }
7208
7209 default:
7210 OVS_NOT_REACHED();
7211 }
7212 }
7213
7214 /* Encode a queue statsrequest for 'oqsr', the encoded message
7215 * will be fore Open Flow version 'ofp_version'. Returns message
7216 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7217 struct ofpbuf *
7218 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
7219 const struct ofputil_queue_stats_request *oqsr)
7220 {
7221 struct ofpbuf *request;
7222
7223 switch (ofp_version) {
7224 case OFP11_VERSION:
7225 case OFP12_VERSION:
7226 case OFP13_VERSION:
7227 case OFP14_VERSION:
7228 case OFP15_VERSION: {
7229 struct ofp11_queue_stats_request *req;
7230 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
7231 req = ofpbuf_put_zeros(request, sizeof *req);
7232 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
7233 req->queue_id = htonl(oqsr->queue_id);
7234 break;
7235 }
7236 case OFP10_VERSION: {
7237 struct ofp10_queue_stats_request *req;
7238 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
7239 req = ofpbuf_put_zeros(request, sizeof *req);
7240 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
7241 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
7242 ? OFPP_ALL : oqsr->port_no));
7243 req->queue_id = htonl(oqsr->queue_id);
7244 break;
7245 }
7246 default:
7247 OVS_NOT_REACHED();
7248 }
7249
7250 return request;
7251 }
7252
7253 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
7254 * message 'oh'. */
7255 size_t
7256 ofputil_count_queue_stats(const struct ofp_header *oh)
7257 {
7258 struct ofputil_queue_stats qs;
7259 struct ofpbuf b;
7260 size_t n = 0;
7261
7262 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7263 ofpraw_pull_assert(&b);
7264 while (!ofputil_decode_queue_stats(&qs, &b)) {
7265 n++;
7266 }
7267 return n;
7268 }
7269
7270 static enum ofperr
7271 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
7272 const struct ofp10_queue_stats *qs10)
7273 {
7274 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
7275 oqs->queue_id = ntohl(qs10->queue_id);
7276 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
7277 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
7278 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
7279 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
7280
7281 return 0;
7282 }
7283
7284 static enum ofperr
7285 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
7286 const struct ofp11_queue_stats *qs11)
7287 {
7288 enum ofperr error;
7289
7290 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
7291 if (error) {
7292 return error;
7293 }
7294
7295 oqs->queue_id = ntohl(qs11->queue_id);
7296 oqs->tx_bytes = ntohll(qs11->tx_bytes);
7297 oqs->tx_packets = ntohll(qs11->tx_packets);
7298 oqs->tx_errors = ntohll(qs11->tx_errors);
7299 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
7300
7301 return 0;
7302 }
7303
7304 static enum ofperr
7305 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
7306 const struct ofp13_queue_stats *qs13)
7307 {
7308 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
7309 if (!error) {
7310 oqs->duration_sec = ntohl(qs13->duration_sec);
7311 oqs->duration_nsec = ntohl(qs13->duration_nsec);
7312 }
7313
7314 return error;
7315 }
7316
7317 static enum ofperr
7318 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
7319 struct ofpbuf *msg)
7320 {
7321 const struct ofp14_queue_stats *qs14;
7322 size_t len;
7323
7324 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
7325 if (!qs14) {
7326 return OFPERR_OFPBRC_BAD_LEN;
7327 }
7328
7329 len = ntohs(qs14->length);
7330 if (len < sizeof *qs14 || len - sizeof *qs14 > ofpbuf_size(msg)) {
7331 return OFPERR_OFPBRC_BAD_LEN;
7332 }
7333 ofpbuf_pull(msg, len - sizeof *qs14);
7334
7335 /* No properties yet defined, so ignore them for now. */
7336
7337 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
7338 }
7339
7340 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
7341 * ofputil_queue_stats in 'qs'.
7342 *
7343 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
7344 * message. Calling this function multiple times for a single 'msg' iterates
7345 * through the replies. The caller must initially leave 'msg''s layer pointers
7346 * null and not modify them between calls.
7347 *
7348 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7349 * otherwise a positive errno value. */
7350 int
7351 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
7352 {
7353 enum ofperr error;
7354 enum ofpraw raw;
7355
7356 error = (msg->frame
7357 ? ofpraw_decode(&raw, msg->frame)
7358 : ofpraw_pull(&raw, msg));
7359 if (error) {
7360 return error;
7361 }
7362
7363 if (!ofpbuf_size(msg)) {
7364 return EOF;
7365 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
7366 return ofputil_pull_ofp14_queue_stats(qs, msg);
7367 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
7368 const struct ofp13_queue_stats *qs13;
7369
7370 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
7371 if (!qs13) {
7372 goto bad_len;
7373 }
7374 return ofputil_queue_stats_from_ofp13(qs, qs13);
7375 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
7376 const struct ofp11_queue_stats *qs11;
7377
7378 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
7379 if (!qs11) {
7380 goto bad_len;
7381 }
7382 return ofputil_queue_stats_from_ofp11(qs, qs11);
7383 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
7384 const struct ofp10_queue_stats *qs10;
7385
7386 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
7387 if (!qs10) {
7388 goto bad_len;
7389 }
7390 return ofputil_queue_stats_from_ofp10(qs, qs10);
7391 } else {
7392 OVS_NOT_REACHED();
7393 }
7394
7395 bad_len:
7396 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
7397 "bytes at end", ofpbuf_size(msg));
7398 return OFPERR_OFPBRC_BAD_LEN;
7399 }
7400
7401 static void
7402 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
7403 struct ofp10_queue_stats *qs10)
7404 {
7405 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
7406 memset(qs10->pad, 0, sizeof qs10->pad);
7407 qs10->queue_id = htonl(oqs->queue_id);
7408 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
7409 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
7410 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
7411 }
7412
7413 static void
7414 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
7415 struct ofp11_queue_stats *qs11)
7416 {
7417 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
7418 qs11->queue_id = htonl(oqs->queue_id);
7419 qs11->tx_bytes = htonll(oqs->tx_bytes);
7420 qs11->tx_packets = htonll(oqs->tx_packets);
7421 qs11->tx_errors = htonll(oqs->tx_errors);
7422 }
7423
7424 static void
7425 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
7426 struct ofp13_queue_stats *qs13)
7427 {
7428 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
7429 if (oqs->duration_sec != UINT32_MAX) {
7430 qs13->duration_sec = htonl(oqs->duration_sec);
7431 qs13->duration_nsec = htonl(oqs->duration_nsec);
7432 } else {
7433 qs13->duration_sec = OVS_BE32_MAX;
7434 qs13->duration_nsec = OVS_BE32_MAX;
7435 }
7436 }
7437
7438 static void
7439 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
7440 struct ofp14_queue_stats *qs14)
7441 {
7442 qs14->length = htons(sizeof *qs14);
7443 memset(qs14->pad, 0, sizeof qs14->pad);
7444 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
7445 }
7446
7447
7448 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
7449 void
7450 ofputil_append_queue_stat(struct list *replies,
7451 const struct ofputil_queue_stats *oqs)
7452 {
7453 switch (ofpmp_version(replies)) {
7454 case OFP13_VERSION: {
7455 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7456 ofputil_queue_stats_to_ofp13(oqs, reply);
7457 break;
7458 }
7459
7460 case OFP12_VERSION:
7461 case OFP11_VERSION: {
7462 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7463 ofputil_queue_stats_to_ofp11(oqs, reply);
7464 break;
7465 }
7466
7467 case OFP10_VERSION: {
7468 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7469 ofputil_queue_stats_to_ofp10(oqs, reply);
7470 break;
7471 }
7472
7473 case OFP14_VERSION:
7474 case OFP15_VERSION: {
7475 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7476 ofputil_queue_stats_to_ofp14(oqs, reply);
7477 break;
7478 }
7479
7480 default:
7481 OVS_NOT_REACHED();
7482 }
7483 }
7484
7485 enum ofperr
7486 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
7487 struct ofputil_bundle_ctrl_msg *msg)
7488 {
7489 struct ofpbuf b;
7490 enum ofpraw raw;
7491 const struct ofp14_bundle_ctrl_msg *m;
7492
7493 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7494 raw = ofpraw_pull_assert(&b);
7495 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
7496
7497 m = ofpbuf_l3(&b);
7498 msg->bundle_id = ntohl(m->bundle_id);
7499 msg->type = ntohs(m->type);
7500 msg->flags = ntohs(m->flags);
7501
7502 return 0;
7503 }
7504
7505 struct ofpbuf *
7506 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
7507 struct ofputil_bundle_ctrl_msg *msg)
7508 {
7509 struct ofpbuf *buf;
7510 struct ofp14_bundle_ctrl_msg *m;
7511
7512 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
7513 m = ofpbuf_put_zeros(buf, sizeof *m);
7514
7515 m->bundle_id = htonl(msg->bundle_id);
7516 m->type = htons(msg->type);
7517 m->flags = htons(msg->flags);
7518
7519 return buf;
7520 }
7521
7522 enum ofperr
7523 ofputil_decode_bundle_add(const struct ofp_header *oh,
7524 struct ofputil_bundle_add_msg *msg)
7525 {
7526 const struct ofp14_bundle_ctrl_msg *m;
7527 struct ofpbuf b;
7528 enum ofpraw raw;
7529 size_t inner_len;
7530
7531 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7532 raw = ofpraw_pull_assert(&b);
7533 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
7534
7535 m = ofpbuf_pull(&b, sizeof *m);
7536 msg->bundle_id = ntohl(m->bundle_id);
7537 msg->flags = ntohs(m->flags);
7538
7539 msg->msg = ofpbuf_data(&b);
7540 inner_len = ntohs(msg->msg->length);
7541 if (inner_len < sizeof(struct ofp_header) || inner_len > ofpbuf_size(&b)) {
7542 return OFPERR_OFPBFC_MSG_BAD_LEN;
7543 }
7544
7545 return 0;
7546 }
7547
7548 struct ofpbuf *
7549 ofputil_encode_bundle_add(enum ofp_version ofp_version,
7550 struct ofputil_bundle_add_msg *msg)
7551 {
7552 struct ofpbuf *request;
7553 struct ofp14_bundle_ctrl_msg *m;
7554
7555 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version, 0);
7556 m = ofpbuf_put_zeros(request, sizeof *m);
7557
7558 m->bundle_id = htonl(msg->bundle_id);
7559 m->flags = htons(msg->flags);
7560 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
7561
7562 return request;
7563 }