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