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