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