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