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