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