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