Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at:
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include <config.h>
18 : : #include <ctype.h>
19 : : #include <errno.h>
20 : : #include <inttypes.h>
21 : : #include <sys/types.h>
22 : : #include <netinet/in.h>
23 : : #include <netinet/icmp6.h>
24 : : #include <stdlib.h>
25 : : #include "bitmap.h"
26 : : #include "bundle.h"
27 : : #include "byte-order.h"
28 : : #include "classifier.h"
29 : : #include "learn.h"
30 : : #include "multipath.h"
31 : : #include "netdev.h"
32 : : #include "nx-match.h"
33 : : #include "id-pool.h"
34 : : #include "openflow/netronome-ext.h"
35 : : #include "openvswitch/dynamic-string.h"
36 : : #include "openvswitch/meta-flow.h"
37 : : #include "openvswitch/ofp-actions.h"
38 : : #include "openvswitch/ofp-errors.h"
39 : : #include "openvswitch/ofp-msgs.h"
40 : : #include "openvswitch/ofp-print.h"
41 : : #include "openvswitch/ofp-prop.h"
42 : : #include "openvswitch/ofp-util.h"
43 : : #include "openvswitch/ofpbuf.h"
44 : : #include "openvswitch/type-props.h"
45 : : #include "openvswitch/vlog.h"
46 : : #include "openflow/intel-ext.h"
47 : : #include "packets.h"
48 : : #include "random.h"
49 : : #include "tun-metadata.h"
50 : : #include "unaligned.h"
51 : : #include "util.h"
52 : : #include "uuid.h"
53 : :
54 : 20190 : 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 : 13514 : ofputil_wcbits_to_netmask(int wcbits)
80 : : {
81 : 13514 : wcbits &= 0x3f;
82 [ + + ]: 13514 : 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 : 7428 : ofputil_netmask_to_wcbits(ovs_be32 netmask)
93 : : {
94 : 7428 : 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 : 6757 : ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
103 : : {
104 : : BUILD_ASSERT_DECL(FLOW_WC_SEQ == 36);
105 : :
106 : : /* Initialize most of wc. */
107 : 6757 : flow_wildcards_init_catchall(wc);
108 : :
109 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_IN_PORT)) {
110 : 937 : wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
111 : : }
112 : :
113 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_NW_TOS)) {
114 : 12 : wc->masks.nw_tos |= IP_DSCP_MASK;
115 : : }
116 : :
117 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_NW_PROTO)) {
118 : 128 : wc->masks.nw_proto = UINT8_MAX;
119 : : }
120 : 6757 : wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
121 : 6757 : >> OFPFW10_NW_SRC_SHIFT);
122 : 6757 : wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
123 : 6757 : >> OFPFW10_NW_DST_SHIFT);
124 : :
125 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_TP_SRC)) {
126 : 20 : wc->masks.tp_src = OVS_BE16_MAX;
127 : : }
128 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_TP_DST)) {
129 : 25 : wc->masks.tp_dst = OVS_BE16_MAX;
130 : : }
131 : :
132 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_DL_SRC)) {
133 : 1690 : WC_MASK_FIELD(wc, dl_src);
134 : : }
135 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_DL_DST)) {
136 : 68 : WC_MASK_FIELD(wc, dl_dst);
137 : : }
138 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_DL_TYPE)) {
139 : 236 : wc->masks.dl_type = OVS_BE16_MAX;
140 : : }
141 : :
142 : : /* VLAN TCI mask. */
143 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
144 : 49 : wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
145 : : }
146 [ + + ]: 6757 : if (!(ofpfw & OFPFW10_DL_VLAN)) {
147 : 3176 : wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
148 : : }
149 : 6757 : }
150 : :
151 : : /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
152 : : void
153 : 6757 : ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
154 : : struct match *match)
155 : : {
156 : 6757 : uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
157 : :
158 : : /* Initialize match->wc. */
159 : 6757 : memset(&match->flow, 0, sizeof match->flow);
160 : 6757 : ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
161 : :
162 : : /* Initialize most of match->flow. */
163 : 6757 : match->flow.nw_src = ofmatch->nw_src;
164 : 6757 : match->flow.nw_dst = ofmatch->nw_dst;
165 : 6757 : match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
166 : 6757 : match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
167 : 6757 : match->flow.tp_src = ofmatch->tp_src;
168 : 6757 : match->flow.tp_dst = ofmatch->tp_dst;
169 : 6757 : match->flow.dl_src = ofmatch->dl_src;
170 : 6757 : match->flow.dl_dst = ofmatch->dl_dst;
171 : 6757 : match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
172 : 6757 : match->flow.nw_proto = ofmatch->nw_proto;
173 : :
174 : : /* Translate VLANs. */
175 [ + + + + ]: 9933 : if (!(ofpfw & OFPFW10_DL_VLAN) &&
176 : 3176 : 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 : 15 : match->flow.vlan_tci = htons(0);
186 : 15 : match->wc.masks.vlan_tci = htons(0xffff);
187 : : } else {
188 : : ovs_be16 vid, pcp, tci;
189 : : uint16_t hpcp;
190 : :
191 : 6742 : vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
192 : 6742 : hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
193 : 6742 : pcp = htons(hpcp);
194 : 6742 : tci = vid | pcp | htons(VLAN_CFI);
195 : 6742 : match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
196 : : }
197 : :
198 : : /* Clean up. */
199 : 6757 : match_zero_wildcarded_fields(match);
200 : 6757 : }
201 : :
202 : : /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
203 : : void
204 : 3714 : ofputil_match_to_ofp10_match(const struct match *match,
205 : : struct ofp10_match *ofmatch)
206 : : {
207 : 3714 : const struct flow_wildcards *wc = &match->wc;
208 : : uint32_t ofpfw;
209 : :
210 : : /* Figure out most OpenFlow wildcards. */
211 : 3714 : ofpfw = 0;
212 [ + + ]: 3714 : if (!wc->masks.in_port.ofp_port) {
213 : 2994 : ofpfw |= OFPFW10_IN_PORT;
214 : : }
215 [ + + ]: 3714 : if (!wc->masks.dl_type) {
216 : 3296 : ofpfw |= OFPFW10_DL_TYPE;
217 : : }
218 [ + + ]: 3714 : if (!wc->masks.nw_proto) {
219 : 3387 : ofpfw |= OFPFW10_NW_PROTO;
220 : : }
221 : 3714 : ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
222 : 3714 : << OFPFW10_NW_SRC_SHIFT);
223 : 3714 : ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
224 : 3714 : << OFPFW10_NW_DST_SHIFT);
225 [ + + ]: 3714 : if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
226 : 3467 : ofpfw |= OFPFW10_NW_TOS;
227 : : }
228 [ + + ]: 3714 : if (!wc->masks.tp_src) {
229 : 3603 : ofpfw |= OFPFW10_TP_SRC;
230 : : }
231 [ + + ]: 3714 : if (!wc->masks.tp_dst) {
232 : 3600 : ofpfw |= OFPFW10_TP_DST;
233 : : }
234 [ + + ]: 3714 : if (eth_addr_is_zero(wc->masks.dl_src)) {
235 : 2622 : ofpfw |= OFPFW10_DL_SRC;
236 : : }
237 [ + + ]: 3714 : if (eth_addr_is_zero(wc->masks.dl_dst)) {
238 : 3434 : ofpfw |= OFPFW10_DL_DST;
239 : : }
240 : :
241 : : /* Translate VLANs. */
242 : 3714 : ofmatch->dl_vlan = htons(0);
243 : 3714 : ofmatch->dl_vlan_pcp = 0;
244 [ + + ]: 3714 : if (match->wc.masks.vlan_tci == htons(0)) {
245 : 1860 : ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
246 [ + + ]: 1854 : } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
247 [ + + ]: 1852 : && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
248 : 92 : ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
249 : : } else {
250 [ + + ]: 1762 : if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
251 : 9 : ofpfw |= OFPFW10_DL_VLAN;
252 : : } else {
253 : 1753 : ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
254 : : }
255 : :
256 [ + + ]: 1762 : if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
257 : 1572 : ofpfw |= OFPFW10_DL_VLAN_PCP;
258 : : } else {
259 : 190 : ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
260 : : }
261 : : }
262 : :
263 : : /* Compose most of the match structure. */
264 : 3714 : ofmatch->wildcards = htonl(ofpfw);
265 : 3714 : ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
266 : 3714 : ofmatch->dl_src = match->flow.dl_src;
267 : 3714 : ofmatch->dl_dst = match->flow.dl_dst;
268 : 3714 : ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
269 : 3714 : ofmatch->nw_src = match->flow.nw_src;
270 : 3714 : ofmatch->nw_dst = match->flow.nw_dst;
271 : 3714 : ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
272 : 3714 : ofmatch->nw_proto = match->flow.nw_proto;
273 : 3714 : ofmatch->tp_src = match->flow.tp_src;
274 : 3714 : ofmatch->tp_dst = match->flow.tp_dst;
275 : 3714 : memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
276 : 3714 : memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
277 : 3714 : }
278 : :
279 : : enum ofperr
280 : 50197 : ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
281 : : uint16_t *padded_match_len)
282 : : {
283 : 50197 : struct ofp11_match_header *omh = buf->data;
284 : : uint16_t match_len;
285 : :
286 [ - + ]: 50197 : if (buf->size < sizeof *omh) {
287 : 0 : return OFPERR_OFPBMC_BAD_LEN;
288 : : }
289 : :
290 : 50197 : match_len = ntohs(omh->length);
291 : :
292 [ + + - ]: 50197 : switch (ntohs(omh->type)) {
293 : : case OFPMT_STANDARD: {
294 : : struct ofp11_match *om;
295 : :
296 [ + - ][ - + ]: 148 : if (match_len != sizeof *om || buf->size < sizeof *om) {
297 : 0 : return OFPERR_OFPBMC_BAD_LEN;
298 : : }
299 : 148 : om = ofpbuf_pull(buf, sizeof *om);
300 [ - + ]: 148 : if (padded_match_len) {
301 : 0 : *padded_match_len = match_len;
302 : : }
303 : 148 : return ofputil_match_from_ofp11_match(om, match);
304 : : }
305 : :
306 : : case OFPMT_OXM:
307 [ + + ]: 50049 : if (padded_match_len) {
308 : 4493 : *padded_match_len = ROUND_UP(match_len, 8);
309 : : }
310 : 50049 : return oxm_pull_match(buf, match);
311 : :
312 : : default:
313 : 0 : return OFPERR_OFPBMC_BAD_TYPE;
314 : : }
315 : : }
316 : :
317 : : /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
318 : : * Returns 0 if successful, otherwise an OFPERR_* value. */
319 : : enum ofperr
320 : 203 : ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
321 : : struct match *match)
322 : : {
323 : 203 : uint16_t wc = ntohl(ofmatch->wildcards);
324 : : bool ipv4, arp, rarp;
325 : :
326 : 203 : match_init_catchall(match);
327 : :
328 [ + + ]: 203 : if (!(wc & OFPFW11_IN_PORT)) {
329 : : ofp_port_t ofp_port;
330 : : enum ofperr error;
331 : :
332 : 45 : error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
333 [ + + ]: 45 : if (error) {
334 : 1 : return OFPERR_OFPBMC_BAD_VALUE;
335 : : }
336 : 44 : match_set_in_port(match, ofp_port);
337 : : }
338 : :
339 : 202 : match_set_dl_src_masked(match, ofmatch->dl_src,
340 : : eth_addr_invert(ofmatch->dl_src_mask));
341 : 202 : match_set_dl_dst_masked(match, ofmatch->dl_dst,
342 : : eth_addr_invert(ofmatch->dl_dst_mask));
343 : :
344 [ + + ]: 202 : if (!(wc & OFPFW11_DL_VLAN)) {
345 [ + + ]: 18 : if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
346 : : /* Match only packets without a VLAN tag. */
347 : 4 : match->flow.vlan_tci = htons(0);
348 : 4 : match->wc.masks.vlan_tci = OVS_BE16_MAX;
349 : : } else {
350 [ + + ]: 14 : if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
351 : : /* Match any packet with a VLAN tag regardless of VID. */
352 : 5 : match->flow.vlan_tci = htons(VLAN_CFI);
353 : 5 : match->wc.masks.vlan_tci = htons(VLAN_CFI);
354 [ + + ]: 9 : } else if (ntohs(ofmatch->dl_vlan) < 4096) {
355 : : /* Match only packets with the specified VLAN VID. */
356 : 8 : match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
357 : 8 : match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
358 : : } else {
359 : : /* Invalid VID. */
360 : 1 : return OFPERR_OFPBMC_BAD_VALUE;
361 : : }
362 : :
363 [ + + ]: 13 : if (!(wc & OFPFW11_DL_VLAN_PCP)) {
364 [ + - ]: 7 : if (ofmatch->dl_vlan_pcp <= 7) {
365 : 7 : match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
366 : : << VLAN_PCP_SHIFT);
367 : 7 : match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
368 : : } else {
369 : : /* Invalid PCP. */
370 : 0 : return OFPERR_OFPBMC_BAD_VALUE;
371 : : }
372 : : }
373 : : }
374 : : }
375 : :
376 [ + + ]: 201 : if (!(wc & OFPFW11_DL_TYPE)) {
377 : 50 : match_set_dl_type(match,
378 : 50 : ofputil_dl_type_from_openflow(ofmatch->dl_type));
379 : : }
380 : :
381 : 201 : ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
382 : 201 : arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
383 : 201 : rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
384 : :
385 [ + + ][ + + ]: 201 : if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
386 [ + + ]: 2 : if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
387 : : /* Invalid TOS. */
388 : 1 : return OFPERR_OFPBMC_BAD_VALUE;
389 : : }
390 : :
391 : 1 : match_set_nw_dscp(match, ofmatch->nw_tos);
392 : : }
393 : :
394 [ + + ][ + + ]: 200 : if (ipv4 || arp || rarp) {
[ - + ]
395 [ + + ]: 45 : if (!(wc & OFPFW11_NW_PROTO)) {
396 : 32 : match_set_nw_proto(match, ofmatch->nw_proto);
397 : : }
398 : 45 : match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
399 : 45 : match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
400 : : }
401 : :
402 : : #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
403 [ + + ][ + + ]: 200 : if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
404 [ + + + ]: 14 : switch (match->flow.nw_proto) {
405 : : case IPPROTO_ICMP:
406 : : /* "A.2.3 Flow Match Structures" in OF1.1 says:
407 : : *
408 : : * The tp_src and tp_dst fields will be ignored unless the
409 : : * network protocol specified is as TCP, UDP or SCTP.
410 : : *
411 : : * but I'm pretty sure we should support ICMP too, otherwise
412 : : * that's a regression from OF1.0. */
413 [ + + ]: 2 : if (!(wc & OFPFW11_TP_SRC)) {
414 : 1 : uint16_t icmp_type = ntohs(ofmatch->tp_src);
415 [ + - ]: 1 : if (icmp_type < 0x100) {
416 : 1 : match_set_icmp_type(match, icmp_type);
417 : : } else {
418 : 0 : return OFPERR_OFPBMC_BAD_FIELD;
419 : : }
420 : : }
421 [ + + ]: 2 : if (!(wc & OFPFW11_TP_DST)) {
422 : 1 : uint16_t icmp_code = ntohs(ofmatch->tp_dst);
423 [ + - ]: 1 : if (icmp_code < 0x100) {
424 : 1 : match_set_icmp_code(match, icmp_code);
425 : : } else {
426 : 0 : return OFPERR_OFPBMC_BAD_FIELD;
427 : : }
428 : : }
429 : 2 : break;
430 : :
431 : : case IPPROTO_TCP:
432 : : case IPPROTO_UDP:
433 : : case IPPROTO_SCTP:
434 [ + + ]: 10 : if (!(wc & (OFPFW11_TP_SRC))) {
435 : 5 : match_set_tp_src(match, ofmatch->tp_src);
436 : : }
437 [ + + ]: 10 : if (!(wc & (OFPFW11_TP_DST))) {
438 : 5 : match_set_tp_dst(match, ofmatch->tp_dst);
439 : : }
440 : 10 : break;
441 : :
442 : : default:
443 : : /* OF1.1 says explicitly to ignore this. */
444 : 2 : break;
445 : : }
446 : : }
447 : :
448 [ + + ]: 200 : if (eth_type_mpls(match->flow.dl_type)) {
449 [ + + ]: 2 : if (!(wc & OFPFW11_MPLS_LABEL)) {
450 : 1 : match_set_mpls_label(match, 0, ofmatch->mpls_label);
451 : : }
452 [ + + ]: 2 : if (!(wc & OFPFW11_MPLS_TC)) {
453 : 1 : match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
454 : : }
455 : : }
456 : :
457 : 200 : match_set_metadata_masked(match, ofmatch->metadata,
458 : 200 : ~ofmatch->metadata_mask);
459 : :
460 : 200 : return 0;
461 : : }
462 : :
463 : : /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
464 : : void
465 : 146 : ofputil_match_to_ofp11_match(const struct match *match,
466 : : struct ofp11_match *ofmatch)
467 : : {
468 : 146 : uint32_t wc = 0;
469 : :
470 : 146 : memset(ofmatch, 0, sizeof *ofmatch);
471 : 146 : ofmatch->omh.type = htons(OFPMT_STANDARD);
472 : 146 : ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
473 : :
474 [ + + ]: 146 : if (!match->wc.masks.in_port.ofp_port) {
475 : 116 : wc |= OFPFW11_IN_PORT;
476 : : } else {
477 : 30 : ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
478 : : }
479 : :
480 : 146 : ofmatch->dl_src = match->flow.dl_src;
481 : 146 : ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
482 : 146 : ofmatch->dl_dst = match->flow.dl_dst;
483 : 146 : ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
484 : :
485 [ + + ]: 146 : if (match->wc.masks.vlan_tci == htons(0)) {
486 : 129 : wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
487 [ + + ]: 17 : } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
488 [ + + ]: 15 : && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
489 : 4 : ofmatch->dl_vlan = htons(OFPVID11_NONE);
490 : 4 : wc |= OFPFW11_DL_VLAN_PCP;
491 : : } else {
492 [ + + ]: 13 : if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
493 : 5 : ofmatch->dl_vlan = htons(OFPVID11_ANY);
494 : : } else {
495 : 8 : ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
496 : : }
497 : :
498 [ + + ]: 13 : if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
499 : 6 : wc |= OFPFW11_DL_VLAN_PCP;
500 : : } else {
501 : 7 : ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
502 : : }
503 : : }
504 : :
505 [ + + ]: 146 : if (!match->wc.masks.dl_type) {
506 : 106 : wc |= OFPFW11_DL_TYPE;
507 : : } else {
508 : 40 : ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
509 : : }
510 : :
511 [ + + ]: 146 : if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
512 : 145 : wc |= OFPFW11_NW_TOS;
513 : : } else {
514 : 1 : ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
515 : : }
516 : :
517 [ + + ]: 146 : if (!match->wc.masks.nw_proto) {
518 : 120 : wc |= OFPFW11_NW_PROTO;
519 : : } else {
520 : 26 : ofmatch->nw_proto = match->flow.nw_proto;
521 : : }
522 : :
523 : 146 : ofmatch->nw_src = match->flow.nw_src;
524 : 146 : ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
525 : 146 : ofmatch->nw_dst = match->flow.nw_dst;
526 : 146 : ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
527 : :
528 [ + + ]: 146 : if (!match->wc.masks.tp_src) {
529 : 140 : wc |= OFPFW11_TP_SRC;
530 : : } else {
531 : 6 : ofmatch->tp_src = match->flow.tp_src;
532 : : }
533 : :
534 [ + + ]: 146 : if (!match->wc.masks.tp_dst) {
535 : 140 : wc |= OFPFW11_TP_DST;
536 : : } else {
537 : 6 : ofmatch->tp_dst = match->flow.tp_dst;
538 : : }
539 : :
540 [ + + ]: 146 : if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
541 : 145 : wc |= OFPFW11_MPLS_LABEL;
542 : : } else {
543 : 1 : ofmatch->mpls_label = htonl(mpls_lse_to_label(
544 : : match->flow.mpls_lse[0]));
545 : : }
546 : :
547 [ + + ]: 146 : if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
548 : 145 : wc |= OFPFW11_MPLS_TC;
549 : : } else {
550 : 1 : ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
551 : : }
552 : :
553 : 146 : ofmatch->metadata = match->flow.metadata;
554 : 146 : ofmatch->metadata_mask = ~match->wc.masks.metadata;
555 : :
556 : 146 : ofmatch->wildcards = htonl(wc);
557 : 146 : }
558 : :
559 : : /* Returns the "typical" length of a match for 'protocol', for use in
560 : : * estimating space to preallocate. */
561 : : int
562 : 22932 : ofputil_match_typical_len(enum ofputil_protocol protocol)
563 : : {
564 [ - - + + : 22932 : switch (protocol) {
- ]
565 : : case OFPUTIL_P_OF10_STD:
566 : : case OFPUTIL_P_OF10_STD_TID:
567 : 0 : return sizeof(struct ofp10_match);
568 : :
569 : : case OFPUTIL_P_OF10_NXM:
570 : : case OFPUTIL_P_OF10_NXM_TID:
571 : 0 : return NXM_TYPICAL_LEN;
572 : :
573 : : case OFPUTIL_P_OF11_STD:
574 : 94 : return sizeof(struct ofp11_match);
575 : :
576 : : case OFPUTIL_P_OF12_OXM:
577 : : case OFPUTIL_P_OF13_OXM:
578 : : case OFPUTIL_P_OF14_OXM:
579 : : case OFPUTIL_P_OF15_OXM:
580 : : case OFPUTIL_P_OF16_OXM:
581 : 22838 : return NXM_TYPICAL_LEN;
582 : :
583 : : default:
584 : 0 : OVS_NOT_REACHED();
585 : : }
586 : : }
587 : :
588 : : /* Appends to 'b' an struct ofp11_match_header followed by a match that
589 : : * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
590 : : * data appended out to a multiple of 8. 'protocol' must be one that is usable
591 : : * in OpenFlow 1.1 or later.
592 : : *
593 : : * This function can cause 'b''s data to be reallocated.
594 : : *
595 : : * Returns the number of bytes appended to 'b', excluding the padding. Never
596 : : * returns zero. */
597 : : int
598 : 22932 : ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
599 : : enum ofputil_protocol protocol)
600 : : {
601 [ - + + - ]: 22932 : switch (protocol) {
602 : : case OFPUTIL_P_OF10_STD:
603 : : case OFPUTIL_P_OF10_STD_TID:
604 : : case OFPUTIL_P_OF10_NXM:
605 : : case OFPUTIL_P_OF10_NXM_TID:
606 : 0 : OVS_NOT_REACHED();
607 : :
608 : : case OFPUTIL_P_OF11_STD: {
609 : : struct ofp11_match *om;
610 : :
611 : : /* Make sure that no padding is needed. */
612 : : BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
613 : :
614 : 94 : om = ofpbuf_put_uninit(b, sizeof *om);
615 : 94 : ofputil_match_to_ofp11_match(match, om);
616 : 94 : return sizeof *om;
617 : : }
618 : :
619 : : case OFPUTIL_P_OF12_OXM:
620 : : case OFPUTIL_P_OF13_OXM:
621 : : case OFPUTIL_P_OF14_OXM:
622 : : case OFPUTIL_P_OF15_OXM:
623 : : case OFPUTIL_P_OF16_OXM:
624 : 22838 : return oxm_put_match(b, match,
625 : : ofputil_protocol_to_ofp_version(protocol));
626 : : }
627 : :
628 : 0 : OVS_NOT_REACHED();
629 : : }
630 : :
631 : : /* Given a 'dl_type' value in the format used in struct flow, returns the
632 : : * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
633 : : * structure. */
634 : : ovs_be16
635 : 59952 : ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
636 : : {
637 : 59952 : return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
638 : : ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
639 : : : flow_dl_type);
640 : : }
641 : :
642 : : /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
643 : : * structure, returns the corresponding 'dl_type' value for use in struct
644 : : * flow. */
645 : : ovs_be16
646 : 6807 : ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
647 : : {
648 : 6807 : return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
649 : : ? htons(FLOW_DL_TYPE_NONE)
650 : : : ofp_dl_type);
651 : : }
652 : :
653 : : /* Protocols. */
654 : :
655 : : struct proto_abbrev {
656 : : enum ofputil_protocol protocol;
657 : : const char *name;
658 : : };
659 : :
660 : : /* Most users really don't care about some of the differences between
661 : : * protocols. These abbreviations help with that. */
662 : : static const struct proto_abbrev proto_abbrevs[] = {
663 : : { OFPUTIL_P_ANY, "any" },
664 : : { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
665 : : { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
666 : : { OFPUTIL_P_ANY_OXM, "OXM" },
667 : : };
668 : : #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
669 : :
670 : : enum ofputil_protocol ofputil_flow_dump_protocols[] = {
671 : : OFPUTIL_P_OF16_OXM,
672 : : OFPUTIL_P_OF15_OXM,
673 : : OFPUTIL_P_OF14_OXM,
674 : : OFPUTIL_P_OF13_OXM,
675 : : OFPUTIL_P_OF12_OXM,
676 : : OFPUTIL_P_OF11_STD,
677 : : OFPUTIL_P_OF10_NXM,
678 : : OFPUTIL_P_OF10_STD,
679 : : };
680 : : size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
681 : :
682 : : /* Returns the set of ofputil_protocols that are supported with the given
683 : : * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
684 : : * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
685 : : * if 'version' is not supported or outside the valid range. */
686 : : enum ofputil_protocol
687 : 44291 : ofputil_protocols_from_ofp_version(enum ofp_version version)
688 : : {
689 [ + + + + : 44291 : switch (version) {
+ + - - ]
690 : : case OFP10_VERSION:
691 : 18687 : return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
692 : : case OFP11_VERSION:
693 : 339 : return OFPUTIL_P_OF11_STD;
694 : : case OFP12_VERSION:
695 : 826 : return OFPUTIL_P_OF12_OXM;
696 : : case OFP13_VERSION:
697 : 22700 : return OFPUTIL_P_OF13_OXM;
698 : : case OFP14_VERSION:
699 : 1504 : return OFPUTIL_P_OF14_OXM;
700 : : case OFP15_VERSION:
701 : 235 : return OFPUTIL_P_OF15_OXM;
702 : : case OFP16_VERSION:
703 : 0 : return OFPUTIL_P_OF16_OXM;
704 : : default:
705 : 0 : return 0;
706 : : }
707 : : }
708 : :
709 : : /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
710 : : * connection that has negotiated the given 'version'. 'version' should
711 : : * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
712 : : * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
713 : : * outside the valid range. */
714 : : enum ofputil_protocol
715 : 40380 : ofputil_protocol_from_ofp_version(enum ofp_version version)
716 : : {
717 : 40380 : return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
718 : : }
719 : :
720 : : /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
721 : : * etc.) that corresponds to 'protocol'. */
722 : : enum ofp_version
723 : 76989 : ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
724 : : {
725 [ + + + + : 76989 : switch (protocol) {
+ + + - ]
726 : : case OFPUTIL_P_OF10_STD:
727 : : case OFPUTIL_P_OF10_STD_TID:
728 : : case OFPUTIL_P_OF10_NXM:
729 : : case OFPUTIL_P_OF10_NXM_TID:
730 : 26518 : return OFP10_VERSION;
731 : : case OFPUTIL_P_OF11_STD:
732 : 312 : return OFP11_VERSION;
733 : : case OFPUTIL_P_OF12_OXM:
734 : 915 : return OFP12_VERSION;
735 : : case OFPUTIL_P_OF13_OXM:
736 : 46280 : return OFP13_VERSION;
737 : : case OFPUTIL_P_OF14_OXM:
738 : 2313 : return OFP14_VERSION;
739 : : case OFPUTIL_P_OF15_OXM:
740 : 436 : return OFP15_VERSION;
741 : : case OFPUTIL_P_OF16_OXM:
742 : 215 : return OFP16_VERSION;
743 : : }
744 : :
745 : 0 : OVS_NOT_REACHED();
746 : : }
747 : :
748 : : /* Returns a bitmap of OpenFlow versions that are supported by at
749 : : * least one of the 'protocols'. */
750 : : uint32_t
751 : 4191 : ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
752 : : {
753 : 4191 : uint32_t bitmap = 0;
754 : :
755 [ + + ]: 17704 : for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
756 : 13513 : enum ofputil_protocol protocol = rightmost_1bit(protocols);
757 : :
758 : 13513 : bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
759 : : }
760 : :
761 : 4191 : return bitmap;
762 : : }
763 : :
764 : : /* Returns the set of protocols that are supported on top of the
765 : : * OpenFlow versions included in 'bitmap'. */
766 : : enum ofputil_protocol
767 : 3890 : ofputil_protocols_from_version_bitmap(uint32_t bitmap)
768 : : {
769 : 3890 : enum ofputil_protocol protocols = 0;
770 : :
771 [ + + ]: 7782 : for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
772 : 3892 : enum ofp_version version = rightmost_1bit_idx(bitmap);
773 : :
774 : 3892 : protocols |= ofputil_protocols_from_ofp_version(version);
775 : : }
776 : :
777 : 3890 : return protocols;
778 : : }
779 : :
780 : : /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
781 : : * otherwise. */
782 : : bool
783 : 0 : ofputil_protocol_is_valid(enum ofputil_protocol protocol)
784 : : {
785 [ # # ][ # # ]: 0 : return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
786 : : }
787 : :
788 : : /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
789 : : * extension turned on or off if 'enable' is true or false, respectively.
790 : : *
791 : : * This extension is only useful for protocols whose "standard" version does
792 : : * not allow specific tables to be modified. In particular, this is true of
793 : : * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
794 : : * specifies a table ID and so there is no need for such an extension. When
795 : : * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
796 : : * extension, this function just returns its 'protocol' argument unchanged
797 : : * regardless of the value of 'enable'. */
798 : : enum ofputil_protocol
799 : 36644 : ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
800 : : {
801 [ + + + + : 36644 : switch (protocol) {
+ + + -
- ]
802 : : case OFPUTIL_P_OF10_STD:
803 : : case OFPUTIL_P_OF10_STD_TID:
804 [ + + ]: 11785 : return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
805 : :
806 : : case OFPUTIL_P_OF10_NXM:
807 : : case OFPUTIL_P_OF10_NXM_TID:
808 [ + + ]: 1851 : return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
809 : :
810 : : case OFPUTIL_P_OF11_STD:
811 : 120 : return OFPUTIL_P_OF11_STD;
812 : :
813 : : case OFPUTIL_P_OF12_OXM:
814 : 277 : return OFPUTIL_P_OF12_OXM;
815 : :
816 : : case OFPUTIL_P_OF13_OXM:
817 : 21807 : return OFPUTIL_P_OF13_OXM;
818 : :
819 : : case OFPUTIL_P_OF14_OXM:
820 : 783 : return OFPUTIL_P_OF14_OXM;
821 : :
822 : : case OFPUTIL_P_OF15_OXM:
823 : 21 : return OFPUTIL_P_OF15_OXM;
824 : :
825 : : case OFPUTIL_P_OF16_OXM:
826 : 0 : return OFPUTIL_P_OF16_OXM;
827 : :
828 : : default:
829 : 0 : OVS_NOT_REACHED();
830 : : }
831 : : }
832 : :
833 : : /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
834 : : * some extension to a standard protocol version, the return value is the
835 : : * standard version of that protocol without any extension. If 'protocol' is a
836 : : * standard protocol version, returns 'protocol' unchanged. */
837 : : enum ofputil_protocol
838 : 2012 : ofputil_protocol_to_base(enum ofputil_protocol protocol)
839 : : {
840 : 2012 : return ofputil_protocol_set_tid(protocol, false);
841 : : }
842 : :
843 : : /* Returns 'new_base' with any extensions taken from 'cur'. */
844 : : enum ofputil_protocol
845 : 694 : ofputil_protocol_set_base(enum ofputil_protocol cur,
846 : : enum ofputil_protocol new_base)
847 : : {
848 : 694 : bool tid = (cur & OFPUTIL_P_TID) != 0;
849 : :
850 [ - + - - : 694 : switch (new_base) {
- - - -
- ]
851 : : case OFPUTIL_P_OF10_STD:
852 : : case OFPUTIL_P_OF10_STD_TID:
853 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
854 : :
855 : : case OFPUTIL_P_OF10_NXM:
856 : : case OFPUTIL_P_OF10_NXM_TID:
857 : 694 : return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
858 : :
859 : : case OFPUTIL_P_OF11_STD:
860 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
861 : :
862 : : case OFPUTIL_P_OF12_OXM:
863 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
864 : :
865 : : case OFPUTIL_P_OF13_OXM:
866 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
867 : :
868 : : case OFPUTIL_P_OF14_OXM:
869 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
870 : :
871 : : case OFPUTIL_P_OF15_OXM:
872 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
873 : :
874 : : case OFPUTIL_P_OF16_OXM:
875 : 0 : return ofputil_protocol_set_tid(OFPUTIL_P_OF16_OXM, tid);
876 : :
877 : : default:
878 : 0 : OVS_NOT_REACHED();
879 : : }
880 : : }
881 : :
882 : : /* Returns a string form of 'protocol', if a simple form exists (that is, if
883 : : * 'protocol' is either a single protocol or it is a combination of protocols
884 : : * that have a single abbreviation). Otherwise, returns NULL. */
885 : : const char *
886 : 1859 : ofputil_protocol_to_string(enum ofputil_protocol protocol)
887 : : {
888 : : const struct proto_abbrev *p;
889 : :
890 : : /* Use a "switch" statement for single-bit names so that we get a compiler
891 : : * warning if we forget any. */
892 [ + + + + : 1859 : switch (protocol) {
+ + + + +
+ + ]
893 : : case OFPUTIL_P_OF10_NXM:
894 : 184 : return "NXM-table_id";
895 : :
896 : : case OFPUTIL_P_OF10_NXM_TID:
897 : 64 : return "NXM+table_id";
898 : :
899 : : case OFPUTIL_P_OF10_STD:
900 : 97 : return "OpenFlow10-table_id";
901 : :
902 : : case OFPUTIL_P_OF10_STD_TID:
903 : 63 : return "OpenFlow10+table_id";
904 : :
905 : : case OFPUTIL_P_OF11_STD:
906 : 78 : return "OpenFlow11";
907 : :
908 : : case OFPUTIL_P_OF12_OXM:
909 : 63 : return "OXM-OpenFlow12";
910 : :
911 : : case OFPUTIL_P_OF13_OXM:
912 : 61 : return "OXM-OpenFlow13";
913 : :
914 : : case OFPUTIL_P_OF14_OXM:
915 : 61 : return "OXM-OpenFlow14";
916 : :
917 : : case OFPUTIL_P_OF15_OXM:
918 : 61 : return "OXM-OpenFlow15";
919 : :
920 : : case OFPUTIL_P_OF16_OXM:
921 : 49 : return "OXM-OpenFlow16";
922 : : }
923 : :
924 : : /* Check abbreviations. */
925 [ + + ]: 5390 : for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
926 [ - + ]: 4312 : if (protocol == p->protocol) {
927 : 0 : return p->name;
928 : : }
929 : : }
930 : :
931 : 1078 : return NULL;
932 : : }
933 : :
934 : : /* Returns a string that represents 'protocols'. The return value might be a
935 : : * comma-separated list if 'protocols' doesn't have a simple name. The return
936 : : * value is "none" if 'protocols' is 0.
937 : : *
938 : : * The caller must free the returned string (with free()). */
939 : : char *
940 : 181 : ofputil_protocols_to_string(enum ofputil_protocol protocols)
941 : : {
942 : : struct ds s;
943 : :
944 [ - + ]: 181 : ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
945 [ - + ]: 181 : if (protocols == 0) {
946 : 0 : return xstrdup("none");
947 : : }
948 : :
949 : 181 : ds_init(&s);
950 [ + + ]: 511 : while (protocols) {
951 : : const struct proto_abbrev *p;
952 : : int i;
953 : :
954 [ + + ]: 330 : if (s.length) {
955 : 149 : ds_put_char(&s, ',');
956 : : }
957 : :
958 [ + + ]: 1080 : for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
959 [ + + ]: 1061 : if ((protocols & p->protocol) == p->protocol) {
960 : 311 : ds_put_cstr(&s, p->name);
961 : 311 : protocols &= ~p->protocol;
962 : 311 : goto match;
963 : : }
964 : : }
965 : :
966 [ + - ]: 90 : for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
967 : 90 : enum ofputil_protocol bit = 1u << i;
968 : :
969 [ + + ]: 90 : if (protocols & bit) {
970 : 19 : ds_put_cstr(&s, ofputil_protocol_to_string(bit));
971 : 19 : protocols &= ~bit;
972 : 19 : goto match;
973 : : }
974 : : }
975 : 0 : OVS_NOT_REACHED();
976 : :
977 : : match: ;
978 : : }
979 : 181 : return ds_steal_cstr(&s);
980 : : }
981 : :
982 : : static enum ofputil_protocol
983 : 61 : ofputil_protocol_from_string__(const char *s, size_t n)
984 : : {
985 : : const struct proto_abbrev *p;
986 : : int i;
987 : :
988 [ + + ]: 1725 : for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
989 : 1676 : enum ofputil_protocol bit = 1u << i;
990 : 1676 : const char *name = ofputil_protocol_to_string(bit);
991 : :
992 [ + + ][ + + ]: 1676 : if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
[ + + ]
993 : 12 : return bit;
994 : : }
995 : : }
996 : :
997 [ + - ]: 126 : for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
998 [ + + ][ + + ]: 126 : if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
999 : 49 : return p->protocol;
1000 : : }
1001 : : }
1002 : :
1003 : 0 : return 0;
1004 : : }
1005 : :
1006 : : /* Returns the nonempty set of protocols represented by 's', which can be a
1007 : : * single protocol name or abbreviation or a comma-separated list of them.
1008 : : *
1009 : : * Aborts the program with an error message if 's' is invalid. */
1010 : : enum ofputil_protocol
1011 : 61 : ofputil_protocols_from_string(const char *s)
1012 : : {
1013 : 61 : const char *orig_s = s;
1014 : : enum ofputil_protocol protocols;
1015 : :
1016 : 61 : protocols = 0;
1017 [ + + ]: 122 : while (*s) {
1018 : : enum ofputil_protocol p;
1019 : : size_t n;
1020 : :
1021 : 61 : n = strcspn(s, ",");
1022 [ - + ]: 61 : if (n == 0) {
1023 : 0 : s++;
1024 : 0 : continue;
1025 : : }
1026 : :
1027 : 61 : p = ofputil_protocol_from_string__(s, n);
1028 [ - + ]: 61 : if (!p) {
1029 : 0 : ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1030 : : }
1031 : 61 : protocols |= p;
1032 : :
1033 : 61 : s += n;
1034 : : }
1035 : :
1036 [ - + ]: 61 : if (!protocols) {
1037 : 0 : ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1038 : : }
1039 : 61 : return protocols;
1040 : : }
1041 : :
1042 : : enum ofp_version
1043 : 26874 : ofputil_version_from_string(const char *s)
1044 : : {
1045 [ + + ]: 26874 : if (!strcasecmp(s, "OpenFlow10")) {
1046 : 7374 : return OFP10_VERSION;
1047 : : }
1048 [ + + ]: 19500 : if (!strcasecmp(s, "OpenFlow11")) {
1049 : 3585 : return OFP11_VERSION;
1050 : : }
1051 [ + + ]: 15915 : if (!strcasecmp(s, "OpenFlow12")) {
1052 : 3691 : return OFP12_VERSION;
1053 : : }
1054 [ + + ]: 12224 : if (!strcasecmp(s, "OpenFlow13")) {
1055 : 3700 : return OFP13_VERSION;
1056 : : }
1057 [ + + ]: 8524 : if (!strcasecmp(s, "OpenFlow14")) {
1058 : 3645 : return OFP14_VERSION;
1059 : : }
1060 [ + + ]: 4879 : if (!strcasecmp(s, "OpenFlow15")) {
1061 : 3571 : return OFP15_VERSION;
1062 : : }
1063 [ + - ]: 1308 : if (!strcasecmp(s, "OpenFlow16")) {
1064 : 1308 : return OFP16_VERSION;
1065 : : }
1066 : 0 : return 0;
1067 : : }
1068 : :
1069 : : static bool
1070 : 52694 : is_delimiter(unsigned char c)
1071 : : {
1072 [ + - ][ + + ]: 52694 : return isspace(c) || c == ',';
1073 : : }
1074 : :
1075 : : uint32_t
1076 : 4788 : ofputil_versions_from_string(const char *s)
1077 : : {
1078 : 4788 : size_t i = 0;
1079 : 4788 : uint32_t bitmap = 0;
1080 : :
1081 [ + + ]: 9580 : while (s[i]) {
1082 : : size_t j;
1083 : : int version;
1084 : : char *key;
1085 : :
1086 [ + + ]: 4792 : if (is_delimiter(s[i])) {
1087 : 2 : i++;
1088 : 2 : continue;
1089 : : }
1090 : 4790 : j = 0;
1091 [ + + ][ + + ]: 52690 : while (s[i + j] && !is_delimiter(s[i + j])) {
1092 : 47900 : j++;
1093 : : }
1094 : 4790 : key = xmemdup0(s + i, j);
1095 : 4790 : version = ofputil_version_from_string(key);
1096 [ - + ]: 4790 : if (!version) {
1097 : 0 : VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1098 : : }
1099 : 4790 : free(key);
1100 : 4790 : bitmap |= 1u << version;
1101 : 4790 : i += j;
1102 : : }
1103 : :
1104 : 4788 : return bitmap;
1105 : : }
1106 : :
1107 : : uint32_t
1108 : 3462 : ofputil_versions_from_strings(char ** const s, size_t count)
1109 : : {
1110 : 3462 : uint32_t bitmap = 0;
1111 : :
1112 [ + + ]: 25528 : while (count--) {
1113 : 22066 : int version = ofputil_version_from_string(s[count]);
1114 [ - + ]: 22066 : if (!version) {
1115 [ # # ]: 0 : VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1116 : : } else {
1117 : 22066 : bitmap |= 1u << version;
1118 : : }
1119 : : }
1120 : :
1121 : 3462 : return bitmap;
1122 : : }
1123 : :
1124 : : const char *
1125 : 4 : ofputil_version_to_string(enum ofp_version ofp_version)
1126 : : {
1127 [ + + - - : 4 : switch (ofp_version) {
- - - - ]
1128 : : case OFP10_VERSION:
1129 : 2 : return "OpenFlow10";
1130 : : case OFP11_VERSION:
1131 : 2 : return "OpenFlow11";
1132 : : case OFP12_VERSION:
1133 : 0 : return "OpenFlow12";
1134 : : case OFP13_VERSION:
1135 : 0 : return "OpenFlow13";
1136 : : case OFP14_VERSION:
1137 : 0 : return "OpenFlow14";
1138 : : case OFP15_VERSION:
1139 : 0 : return "OpenFlow15";
1140 : : case OFP16_VERSION:
1141 : 0 : return "OpenFlow16";
1142 : : default:
1143 : 0 : OVS_NOT_REACHED();
1144 : : }
1145 : : }
1146 : :
1147 : : bool
1148 : 413 : ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1149 : : {
1150 [ + - ]: 413 : switch (packet_in_format) {
1151 : : case NXPIF_STANDARD:
1152 : : case NXPIF_NXT_PACKET_IN:
1153 : : case NXPIF_NXT_PACKET_IN2:
1154 : 413 : return true;
1155 : : }
1156 : :
1157 : 0 : return false;
1158 : : }
1159 : :
1160 : : const char *
1161 : 202 : ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1162 : : {
1163 [ + + + - ]: 202 : switch (packet_in_format) {
1164 : : case NXPIF_STANDARD:
1165 : 43 : return "standard";
1166 : : case NXPIF_NXT_PACKET_IN:
1167 : 59 : return "nxt_packet_in";
1168 : : case NXPIF_NXT_PACKET_IN2:
1169 : 100 : return "nxt_packet_in2";
1170 : : default:
1171 : 0 : OVS_NOT_REACHED();
1172 : : }
1173 : : }
1174 : :
1175 : : int
1176 : 113 : ofputil_packet_in_format_from_string(const char *s)
1177 : : {
1178 [ + - ]: 183 : return (!strcmp(s, "standard") || !strcmp(s, "openflow10")
1179 : : ? NXPIF_STANDARD
1180 [ + + ][ + - ]: 184 : : !strcmp(s, "nxt_packet_in") || !strcmp(s, "nxm")
1181 : : ? NXPIF_NXT_PACKET_IN
1182 [ + + ]: 71 : : !strcmp(s, "nxt_packet_in2")
1183 : : ? NXPIF_NXT_PACKET_IN2
1184 [ + - ]: 1 : : -1);
1185 : : }
1186 : :
1187 : : void
1188 : 36817 : ofputil_format_version(struct ds *msg, enum ofp_version version)
1189 : : {
1190 : 36817 : ds_put_format(msg, "0x%02x", version);
1191 : 36817 : }
1192 : :
1193 : : void
1194 : 4 : ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1195 : : {
1196 : 4 : ds_put_cstr(msg, ofputil_version_to_string(version));
1197 : 4 : }
1198 : :
1199 : : static void
1200 : 6943 : ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1201 : : void (*format_version)(struct ds *msg,
1202 : : enum ofp_version))
1203 : : {
1204 [ + + ]: 29966 : while (bitmap) {
1205 : 23023 : format_version(msg, raw_ctz(bitmap));
1206 : 23023 : bitmap = zero_rightmost_1bit(bitmap);
1207 [ + + ]: 23023 : if (bitmap) {
1208 : 16080 : ds_put_cstr(msg, ", ");
1209 : : }
1210 : : }
1211 : 6943 : }
1212 : :
1213 : : void
1214 : 6940 : ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1215 : : {
1216 : 6940 : ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1217 : 6940 : }
1218 : :
1219 : : void
1220 : 3 : ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1221 : : {
1222 : 3 : ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1223 : 3 : }
1224 : :
1225 : : static bool
1226 : 2123 : ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1227 : : uint32_t *allowed_versionsp)
1228 : : {
1229 : 2123 : uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1230 : 2123 : const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1231 : : uint32_t allowed_versions;
1232 : :
1233 [ + - ][ - + ]: 2123 : if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1234 : 0 : return false;
1235 : : }
1236 : :
1237 : : /* Only use the first 32-bit element of the bitmap as that is all the
1238 : : * current implementation supports. Subsequent elements are ignored which
1239 : : * should have no effect on session negotiation until Open vSwitch supports
1240 : : * wire-protocol versions greater than 31.
1241 : : */
1242 : 2123 : allowed_versions = ntohl(bitmap[0]);
1243 : :
1244 [ + + ]: 2123 : if (allowed_versions & 1) {
1245 : : /* There's no OpenFlow version 0. */
1246 [ + - ]: 2 : VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1247 : : "version 0x00");
1248 : 2 : allowed_versions &= ~1u;
1249 : : }
1250 : :
1251 [ + + ]: 2123 : if (!allowed_versions) {
1252 [ + - ]: 3 : VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1253 : : "version (between 0x01 and 0x1f)");
1254 : 3 : return false;
1255 : : }
1256 : :
1257 : 2120 : *allowed_versionsp = allowed_versions;
1258 : 2120 : return true;
1259 : : }
1260 : :
1261 : : static uint32_t
1262 : 13839 : version_bitmap_from_version(uint8_t ofp_version)
1263 : : {
1264 [ + + ]: 13839 : return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1265 : : }
1266 : :
1267 : : /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1268 : : * the set of OpenFlow versions for which 'oh' announces support.
1269 : : *
1270 : : * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1271 : : * successful, and thus '*allowed_versions' is always initialized. However, it
1272 : : * returns false if 'oh' contains some data that could not be fully understood,
1273 : : * true if 'oh' was completely parsed. */
1274 : : bool
1275 : 13839 : ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1276 : : {
1277 : 13839 : struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
1278 : 13839 : ofpbuf_pull(&msg, sizeof *oh);
1279 : :
1280 : 13839 : *allowed_versions = version_bitmap_from_version(oh->version);
1281 : :
1282 : 13839 : bool ok = true;
1283 [ + + ]: 15963 : while (msg.size) {
1284 : : const struct ofp_hello_elem_header *oheh;
1285 : : unsigned int len;
1286 : :
1287 [ + + ]: 2133 : if (msg.size < sizeof *oheh) {
1288 : 2 : return false;
1289 : : }
1290 : :
1291 : 2131 : oheh = msg.data;
1292 : 2131 : len = ntohs(oheh->length);
1293 [ + + ][ + + ]: 2131 : if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1294 : 7 : return false;
1295 : : }
1296 : :
1297 [ + + ]: 2124 : if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1298 [ + + ]: 2123 : || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1299 : 4 : ok = false;
1300 : : }
1301 : : }
1302 : :
1303 : 13839 : return ok;
1304 : : }
1305 : :
1306 : : /* Returns true if 'allowed_versions' needs to be accompanied by a version
1307 : : * bitmap to be correctly expressed in an OFPT_HELLO message. */
1308 : : static bool
1309 : 6924 : should_send_version_bitmap(uint32_t allowed_versions)
1310 : : {
1311 : 6924 : return !is_pow2((allowed_versions >> 1) + 1);
1312 : : }
1313 : :
1314 : : /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1315 : : * versions in the 'allowed_versions' bitmaps and returns the message. */
1316 : : struct ofpbuf *
1317 : 6924 : ofputil_encode_hello(uint32_t allowed_versions)
1318 : : {
1319 : : enum ofp_version ofp_version;
1320 : : struct ofpbuf *msg;
1321 : :
1322 : 6924 : ofp_version = leftmost_1bit_idx(allowed_versions);
1323 : 6924 : msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1324 : :
1325 [ + + ]: 6924 : if (should_send_version_bitmap(allowed_versions)) {
1326 : : struct ofp_hello_elem_header *oheh;
1327 : : uint16_t map_len;
1328 : :
1329 : 1094 : map_len = sizeof allowed_versions;
1330 : 1094 : oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1331 : 1094 : oheh->type = htons(OFPHET_VERSIONBITMAP);
1332 : 1094 : oheh->length = htons(map_len + sizeof *oheh);
1333 : 1094 : *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1334 : :
1335 : 1094 : ofpmsg_update_length(msg);
1336 : : }
1337 : :
1338 : 6924 : return msg;
1339 : : }
1340 : :
1341 : : /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1342 : : * protocol is 'current', at least partly transitions the protocol to 'want'.
1343 : : * Stores in '*next' the protocol that will be in effect on the OpenFlow
1344 : : * connection if the switch processes the returned message correctly. (If
1345 : : * '*next != want' then the caller will have to iterate.)
1346 : : *
1347 : : * If 'current == want', or if it is not possible to transition from 'current'
1348 : : * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1349 : : * protocol versions), returns NULL and stores 'current' in '*next'. */
1350 : : struct ofpbuf *
1351 : 1006 : ofputil_encode_set_protocol(enum ofputil_protocol current,
1352 : : enum ofputil_protocol want,
1353 : : enum ofputil_protocol *next)
1354 : : {
1355 : : enum ofp_version cur_version, want_version;
1356 : : enum ofputil_protocol cur_base, want_base;
1357 : : bool cur_tid, want_tid;
1358 : :
1359 : 1006 : cur_version = ofputil_protocol_to_ofp_version(current);
1360 : 1006 : want_version = ofputil_protocol_to_ofp_version(want);
1361 [ - + ]: 1006 : if (cur_version != want_version) {
1362 : 0 : *next = current;
1363 : 0 : return NULL;
1364 : : }
1365 : :
1366 : 1006 : cur_base = ofputil_protocol_to_base(current);
1367 : 1006 : want_base = ofputil_protocol_to_base(want);
1368 [ + + ]: 1006 : if (cur_base != want_base) {
1369 : 347 : *next = ofputil_protocol_set_base(current, want_base);
1370 : :
1371 [ + - - - : 347 : switch (want_base) {
- ]
1372 : : case OFPUTIL_P_OF10_NXM:
1373 : 347 : return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1374 : :
1375 : : case OFPUTIL_P_OF10_STD:
1376 : 0 : return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1377 : :
1378 : : case OFPUTIL_P_OF11_STD:
1379 : : case OFPUTIL_P_OF12_OXM:
1380 : : case OFPUTIL_P_OF13_OXM:
1381 : : case OFPUTIL_P_OF14_OXM:
1382 : : case OFPUTIL_P_OF15_OXM:
1383 : : case OFPUTIL_P_OF16_OXM:
1384 : : /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1385 : : * verified above that we're not trying to change versions. */
1386 : 0 : OVS_NOT_REACHED();
1387 : :
1388 : : case OFPUTIL_P_OF10_STD_TID:
1389 : : case OFPUTIL_P_OF10_NXM_TID:
1390 : 0 : OVS_NOT_REACHED();
1391 : : }
1392 : : }
1393 : :
1394 : 659 : cur_tid = (current & OFPUTIL_P_TID) != 0;
1395 : 659 : want_tid = (want & OFPUTIL_P_TID) != 0;
1396 [ + + ]: 659 : if (cur_tid != want_tid) {
1397 : 100 : *next = ofputil_protocol_set_tid(current, want_tid);
1398 : 100 : return ofputil_make_flow_mod_table_id(want_tid);
1399 : : }
1400 : :
1401 [ - + ]: 559 : ovs_assert(current == want);
1402 : :
1403 : 559 : *next = current;
1404 : 559 : return NULL;
1405 : : }
1406 : :
1407 : : /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1408 : : * format to 'nxff'. */
1409 : : struct ofpbuf *
1410 : 347 : ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1411 : : {
1412 : : struct nx_set_flow_format *sff;
1413 : : struct ofpbuf *msg;
1414 : :
1415 [ - + ]: 347 : ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1416 : :
1417 : 347 : msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1418 : 347 : sff = ofpbuf_put_zeros(msg, sizeof *sff);
1419 : 347 : sff->format = htonl(nxff);
1420 : :
1421 : 347 : return msg;
1422 : : }
1423 : :
1424 : : /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1425 : : * otherwise. */
1426 : : enum ofputil_protocol
1427 : 1026 : ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1428 : : {
1429 [ - + - ]: 1026 : switch (flow_format) {
1430 : : case NXFF_OPENFLOW10:
1431 : 0 : return OFPUTIL_P_OF10_STD;
1432 : :
1433 : : case NXFF_NXM:
1434 : 1026 : return OFPUTIL_P_OF10_NXM;
1435 : :
1436 : : default:
1437 : 0 : return 0;
1438 : : }
1439 : : }
1440 : :
1441 : : /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1442 : : bool
1443 : 679 : ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1444 : : {
1445 : 679 : return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1446 : : }
1447 : :
1448 : : /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1449 : : * value. */
1450 : : const char *
1451 : 332 : ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1452 : : {
1453 [ - + - ]: 332 : switch (flow_format) {
1454 : : case NXFF_OPENFLOW10:
1455 : 0 : return "openflow10";
1456 : : case NXFF_NXM:
1457 : 332 : return "nxm";
1458 : : default:
1459 : 0 : OVS_NOT_REACHED();
1460 : : }
1461 : : }
1462 : :
1463 : : struct ofpbuf *
1464 : 212 : ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1465 : : enum nx_packet_in_format packet_in_format)
1466 : : {
1467 : : struct nx_set_packet_in_format *spif;
1468 : : struct ofpbuf *msg;
1469 : :
1470 : 212 : msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1471 : 212 : spif = ofpbuf_put_zeros(msg, sizeof *spif);
1472 : 212 : spif->format = htonl(packet_in_format);
1473 : :
1474 : 212 : return msg;
1475 : : }
1476 : :
1477 : : /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1478 : : * extension on or off (according to 'flow_mod_table_id'). */
1479 : : struct ofpbuf *
1480 : 100 : ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1481 : : {
1482 : : struct nx_flow_mod_table_id *nfmti;
1483 : : struct ofpbuf *msg;
1484 : :
1485 : 100 : msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1486 : 100 : nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1487 : 100 : nfmti->set = flow_mod_table_id;
1488 : 100 : return msg;
1489 : : }
1490 : :
1491 : : struct ofputil_flow_mod_flag {
1492 : : uint16_t raw_flag;
1493 : : enum ofp_version min_version, max_version;
1494 : : enum ofputil_flow_mod_flags flag;
1495 : : };
1496 : :
1497 : : static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1498 : : { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1499 : : { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1500 : : { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1501 : : OFPUTIL_FF_EMERG },
1502 : : { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1503 : : { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1504 : : { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1505 : : { 0, 0, 0, 0 },
1506 : : };
1507 : :
1508 : : static enum ofperr
1509 : 71402 : ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1510 : : enum ofp_flow_mod_command command,
1511 : : enum ofp_version version,
1512 : : enum ofputil_flow_mod_flags *flagsp)
1513 : : {
1514 : 71402 : uint16_t raw_flags = ntohs(raw_flags_);
1515 : : const struct ofputil_flow_mod_flag *f;
1516 : :
1517 : 71402 : *flagsp = 0;
1518 [ + + ]: 499814 : for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1519 [ + + ]: 428412 : if (raw_flags & f->raw_flag
1520 [ + - ]: 114 : && version >= f->min_version
1521 [ + + ][ - + ]: 114 : && (!f->max_version || version <= f->max_version)) {
1522 : 103 : raw_flags &= ~f->raw_flag;
1523 : 103 : *flagsp |= f->flag;
1524 : : }
1525 : : }
1526 : :
1527 : : /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1528 : : * never do.
1529 : : *
1530 : : * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1531 : : * resets counters. */
1532 [ + + ][ + + ]: 71402 : if ((version == OFP10_VERSION || version == OFP11_VERSION)
1533 [ + + ]: 21839 : && command == OFPFC_ADD) {
1534 : 20464 : *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1535 : : }
1536 : :
1537 [ - + ]: 71402 : return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1538 : : }
1539 : :
1540 : : static ovs_be16
1541 : 35877 : ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1542 : : enum ofp_version version)
1543 : : {
1544 : : const struct ofputil_flow_mod_flag *f;
1545 : : uint16_t raw_flags;
1546 : :
1547 : 35877 : raw_flags = 0;
1548 [ + + ]: 251139 : for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1549 [ + + ]: 215262 : if (f->flag & flags
1550 [ + - ]: 49 : && version >= f->min_version
1551 [ - + ][ # # ]: 49 : && (!f->max_version || version <= f->max_version)) {
1552 : 49 : raw_flags |= f->raw_flag;
1553 : : }
1554 : : }
1555 : :
1556 : 35877 : return htons(raw_flags);
1557 : : }
1558 : :
1559 : : /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1560 : : * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1561 : : * code.
1562 : : *
1563 : : * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1564 : : * The caller must initialize 'ofpacts' and retains ownership of it.
1565 : : * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1566 : : *
1567 : : * Does not validate the flow_mod actions. The caller should do that, with
1568 : : * ofpacts_check(). */
1569 : : enum ofperr
1570 : 67199 : ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1571 : : const struct ofp_header *oh,
1572 : : enum ofputil_protocol protocol,
1573 : : struct ofpbuf *ofpacts,
1574 : : ofp_port_t max_port, uint8_t max_table)
1575 : : {
1576 : : ovs_be16 raw_flags;
1577 : : enum ofperr error;
1578 : 67199 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1579 : 67199 : enum ofpraw raw = ofpraw_pull_assert(&b);
1580 [ + + ]: 67199 : if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1581 : : /* Standard OpenFlow 1.1+ flow_mod. */
1582 : : const struct ofp11_flow_mod *ofm;
1583 : :
1584 : 45450 : ofm = ofpbuf_pull(&b, sizeof *ofm);
1585 : :
1586 : 45450 : error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1587 [ - + ]: 45450 : if (error) {
1588 : 0 : return error;
1589 : : }
1590 : :
1591 : : /* Translate the message. */
1592 : 45450 : fm->priority = ntohs(ofm->priority);
1593 [ + + ]: 45450 : if (ofm->command == OFPFC_ADD
1594 [ + + ]: 12219 : || (oh->version == OFP11_VERSION
1595 [ + + ][ - + ]: 23 : && (ofm->command == OFPFC_MODIFY ||
1596 : 14 : ofm->command == OFPFC_MODIFY_STRICT)
1597 [ + + ]: 9 : && ofm->cookie_mask == htonll(0))) {
1598 : : /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1599 : : * not match on the cookie is treated as an "add" if there is no
1600 : : * match. */
1601 : 33235 : fm->cookie = htonll(0);
1602 : 33235 : fm->cookie_mask = htonll(0);
1603 : 33235 : fm->new_cookie = ofm->cookie;
1604 : : } else {
1605 : 12215 : fm->cookie = ofm->cookie;
1606 : 12215 : fm->cookie_mask = ofm->cookie_mask;
1607 : 12215 : fm->new_cookie = OVS_BE64_MAX;
1608 : : }
1609 : 45450 : fm->modify_cookie = false;
1610 : 45450 : fm->command = ofm->command;
1611 : :
1612 : : /* Get table ID.
1613 : : *
1614 : : * OF1.1 entirely forbids table_id == OFPTT_ALL.
1615 : : * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1616 : 45450 : fm->table_id = ofm->table_id;
1617 [ + + ]: 45450 : if (fm->table_id == OFPTT_ALL
1618 [ + - ]: 154 : && (oh->version == OFP11_VERSION
1619 [ + + ][ - + ]: 154 : || (ofm->command != OFPFC_DELETE &&
1620 : 42 : ofm->command != OFPFC_DELETE_STRICT))) {
1621 : 0 : return OFPERR_OFPFMFC_BAD_TABLE_ID;
1622 : : }
1623 : :
1624 : 45450 : fm->idle_timeout = ntohs(ofm->idle_timeout);
1625 : 45450 : fm->hard_timeout = ntohs(ofm->hard_timeout);
1626 [ + + ][ + + ]: 45450 : if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1627 : 1395 : fm->importance = ntohs(ofm->importance);
1628 : : } else {
1629 : 44055 : fm->importance = 0;
1630 : : }
1631 : 45450 : fm->buffer_id = ntohl(ofm->buffer_id);
1632 : 45450 : error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1633 [ - + ]: 45450 : if (error) {
1634 : 0 : return error;
1635 : : }
1636 : :
1637 [ + + ]: 45280 : fm->out_group = (ofm->command == OFPFC_DELETE ||
1638 : 45280 : ofm->command == OFPFC_DELETE_STRICT
1639 : 11506 : ? ntohl(ofm->out_group)
1640 [ + + ]: 90730 : : OFPG_ANY);
1641 : 45450 : raw_flags = ofm->flags;
1642 : : } else {
1643 : : uint16_t command;
1644 : :
1645 [ + + ]: 21749 : if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1646 : : /* Standard OpenFlow 1.0 flow_mod. */
1647 : : const struct ofp10_flow_mod *ofm;
1648 : :
1649 : : /* Get the ofp10_flow_mod. */
1650 : 6625 : ofm = ofpbuf_pull(&b, sizeof *ofm);
1651 : :
1652 : : /* Translate the rule. */
1653 : 6625 : ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1654 : 6625 : ofputil_normalize_match(&fm->match);
1655 : :
1656 : : /* OpenFlow 1.0 says that exact-match rules have to have the
1657 : : * highest possible priority. */
1658 : 6625 : fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1659 : 6623 : ? ntohs(ofm->priority)
1660 [ + + ]: 13248 : : UINT16_MAX);
1661 : :
1662 : : /* Translate the message. */
1663 : 6625 : command = ntohs(ofm->command);
1664 : 6625 : fm->cookie = htonll(0);
1665 : 6625 : fm->cookie_mask = htonll(0);
1666 : 6625 : fm->new_cookie = ofm->cookie;
1667 : 6625 : fm->idle_timeout = ntohs(ofm->idle_timeout);
1668 : 6625 : fm->hard_timeout = ntohs(ofm->hard_timeout);
1669 : 6625 : fm->importance = 0;
1670 : 6625 : fm->buffer_id = ntohl(ofm->buffer_id);
1671 : 6625 : fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1672 : 6625 : fm->out_group = OFPG_ANY;
1673 : 6625 : raw_flags = ofm->flags;
1674 [ + - ]: 15124 : } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1675 : : /* Nicira extended flow_mod. */
1676 : : const struct nx_flow_mod *nfm;
1677 : :
1678 : : /* Dissect the message. */
1679 : 15124 : nfm = ofpbuf_pull(&b, sizeof *nfm);
1680 : 15124 : error = nx_pull_match(&b, ntohs(nfm->match_len),
1681 : : &fm->match, &fm->cookie, &fm->cookie_mask);
1682 [ - + ]: 15124 : if (error) {
1683 : 0 : return error;
1684 : : }
1685 : :
1686 : : /* Translate the message. */
1687 : 15124 : command = ntohs(nfm->command);
1688 [ + + ][ - + ]: 15124 : if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1689 : : /* Flow additions may only set a new cookie, not match an
1690 : : * existing cookie. */
1691 : 0 : return OFPERR_NXBRC_NXM_INVALID;
1692 : : }
1693 : 15124 : fm->priority = ntohs(nfm->priority);
1694 : 15124 : fm->new_cookie = nfm->cookie;
1695 : 15124 : fm->idle_timeout = ntohs(nfm->idle_timeout);
1696 : 15124 : fm->hard_timeout = ntohs(nfm->hard_timeout);
1697 : 15124 : fm->importance = 0;
1698 : 15124 : fm->buffer_id = ntohl(nfm->buffer_id);
1699 : 15124 : fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1700 : 15124 : fm->out_group = OFPG_ANY;
1701 : 15124 : raw_flags = nfm->flags;
1702 : : } else {
1703 : 0 : OVS_NOT_REACHED();
1704 : : }
1705 : :
1706 : 21749 : fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1707 [ + + ]: 21749 : if (protocol & OFPUTIL_P_TID) {
1708 : 11488 : fm->command = command & 0xff;
1709 : 11488 : fm->table_id = command >> 8;
1710 : : } else {
1711 [ - + ]: 10261 : if (command > 0xff) {
1712 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1713 : : "but flow_mod_table_id extension is not enabled");
1714 : : }
1715 : 10261 : fm->command = command;
1716 : 10261 : fm->table_id = 0xff;
1717 : : }
1718 : : }
1719 : :
1720 [ - + ]: 67199 : if (fm->command > OFPFC_DELETE_STRICT) {
1721 : 0 : return OFPERR_OFPFMFC_BAD_COMMAND;
1722 : : }
1723 : :
1724 : 67199 : error = ofpacts_pull_openflow_instructions(&b, b.size,
1725 : 67199 : oh->version, ofpacts);
1726 [ - + ]: 67199 : if (error) {
1727 : 0 : return error;
1728 : : }
1729 : 67199 : fm->ofpacts = ofpacts->data;
1730 : 67199 : fm->ofpacts_len = ofpacts->size;
1731 : :
1732 : 67199 : error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1733 : 67199 : oh->version, &fm->flags);
1734 [ - + ]: 67199 : if (error) {
1735 : 0 : return error;
1736 : : }
1737 : :
1738 [ - + ]: 67199 : if (fm->flags & OFPUTIL_FF_EMERG) {
1739 : : /* We do not support the OpenFlow 1.0 emergency flow cache, which
1740 : : * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1741 : : *
1742 : : * OpenFlow 1.0 specifies the error code to use when idle_timeout
1743 : : * or hard_timeout is nonzero. Otherwise, there is no good error
1744 : : * code, so just state that the flow table is full. */
1745 [ # # ][ # # ]: 0 : return (fm->hard_timeout || fm->idle_timeout
1746 : : ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1747 : : : OFPERR_OFPFMFC_TABLE_FULL);
1748 : : }
1749 : :
1750 : 67199 : return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1751 : : &fm->match.flow, max_port,
1752 : 67199 : fm->table_id, max_table, protocol);
1753 : : }
1754 : :
1755 : : static enum ofperr
1756 : 6 : ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1757 : : struct ofpbuf *bands)
1758 : : {
1759 : : const struct ofp13_meter_band_header *ombh;
1760 : : struct ofputil_meter_band *mb;
1761 : 6 : uint16_t n = 0;
1762 : :
1763 : 6 : ombh = ofpbuf_try_pull(msg, len);
1764 [ - + ]: 6 : if (!ombh) {
1765 : 0 : return OFPERR_OFPBRC_BAD_LEN;
1766 : : }
1767 : :
1768 [ + + ]: 10 : while (len >= sizeof (struct ofp13_meter_band_drop)) {
1769 : 5 : size_t ombh_len = ntohs(ombh->len);
1770 : : /* All supported band types have the same length. */
1771 [ - + ]: 5 : if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1772 : 0 : return OFPERR_OFPBRC_BAD_LEN;
1773 : : }
1774 : 5 : mb = ofpbuf_put_uninit(bands, sizeof *mb);
1775 : 5 : mb->type = ntohs(ombh->type);
1776 [ + + ][ + + ]: 5 : if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1777 : 1 : return OFPERR_OFPMMFC_BAD_BAND;
1778 : : }
1779 : 4 : mb->rate = ntohl(ombh->rate);
1780 : 4 : mb->burst_size = ntohl(ombh->burst_size);
1781 [ + + ]: 4 : mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1782 : : ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1783 : 4 : n++;
1784 : 4 : len -= ombh_len;
1785 : 4 : ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1786 : : (char *) ombh + ombh_len);
1787 : : }
1788 [ - + ]: 5 : if (len) {
1789 : 0 : return OFPERR_OFPBRC_BAD_LEN;
1790 : : }
1791 : 5 : *n_bands = n;
1792 : 5 : return 0;
1793 : : }
1794 : :
1795 : : enum ofperr
1796 : 6 : ofputil_decode_meter_mod(const struct ofp_header *oh,
1797 : : struct ofputil_meter_mod *mm,
1798 : : struct ofpbuf *bands)
1799 : : {
1800 : 6 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1801 : 6 : ofpraw_pull_assert(&b);
1802 : 6 : const struct ofp13_meter_mod *omm = ofpbuf_pull(&b, sizeof *omm);
1803 : :
1804 : : /* Translate the message. */
1805 : 6 : mm->command = ntohs(omm->command);
1806 [ + + ][ + + ]: 6 : if (mm->command != OFPMC13_ADD &&
1807 [ + - ]: 1 : mm->command != OFPMC13_MODIFY &&
1808 : 1 : mm->command != OFPMC13_DELETE) {
1809 : 1 : return OFPERR_OFPMMFC_BAD_COMMAND;
1810 : : }
1811 : 5 : mm->meter.meter_id = ntohl(omm->meter_id);
1812 : :
1813 [ - + ]: 5 : if (mm->command == OFPMC13_DELETE) {
1814 : 0 : mm->meter.flags = 0;
1815 : 0 : mm->meter.n_bands = 0;
1816 : 0 : mm->meter.bands = NULL;
1817 : : } else {
1818 : : enum ofperr error;
1819 : :
1820 : 5 : mm->meter.flags = ntohs(omm->flags);
1821 [ + + ][ + + ]: 5 : if (mm->meter.flags & OFPMF13_KBPS &&
1822 : 2 : mm->meter.flags & OFPMF13_PKTPS) {
1823 : 1 : return OFPERR_OFPMMFC_BAD_FLAGS;
1824 : : }
1825 : 4 : mm->meter.bands = bands->data;
1826 : :
1827 : 4 : error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1828 [ + + ]: 4 : if (error) {
1829 : 1 : return error;
1830 : : }
1831 : : }
1832 : 6 : return 0;
1833 : : }
1834 : :
1835 : : void
1836 : 2 : ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1837 : : {
1838 : 2 : const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1839 : 2 : *meter_id = ntohl(omr->meter_id);
1840 : 2 : }
1841 : :
1842 : : struct ofpbuf *
1843 : 0 : ofputil_encode_meter_request(enum ofp_version ofp_version,
1844 : : enum ofputil_meter_request_type type,
1845 : : uint32_t meter_id)
1846 : : {
1847 : : struct ofpbuf *msg;
1848 : :
1849 : : enum ofpraw raw;
1850 : :
1851 [ # # # ]: 0 : switch (type) {
1852 : : case OFPUTIL_METER_CONFIG:
1853 : 0 : raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1854 : 0 : break;
1855 : : case OFPUTIL_METER_STATS:
1856 : 0 : raw = OFPRAW_OFPST13_METER_REQUEST;
1857 : 0 : break;
1858 : : default:
1859 : : case OFPUTIL_METER_FEATURES:
1860 : 0 : raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1861 : 0 : break;
1862 : : }
1863 : :
1864 : 0 : msg = ofpraw_alloc(raw, ofp_version, 0);
1865 : :
1866 [ # # ]: 0 : if (type != OFPUTIL_METER_FEATURES) {
1867 : : struct ofp13_meter_multipart_request *omr;
1868 : 0 : omr = ofpbuf_put_zeros(msg, sizeof *omr);
1869 : 0 : omr->meter_id = htonl(meter_id);
1870 : : }
1871 : 0 : return msg;
1872 : : }
1873 : :
1874 : : static void
1875 : 0 : ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1876 : : struct ofpbuf *msg)
1877 : : {
1878 : 0 : uint16_t n = 0;
1879 : :
1880 [ # # ]: 0 : for (n = 0; n < n_bands; ++n) {
1881 : : /* Currently all band types have same size. */
1882 : : struct ofp13_meter_band_dscp_remark *ombh;
1883 : 0 : size_t ombh_len = sizeof *ombh;
1884 : :
1885 : 0 : ombh = ofpbuf_put_zeros(msg, ombh_len);
1886 : :
1887 : 0 : ombh->type = htons(mb->type);
1888 : 0 : ombh->len = htons(ombh_len);
1889 : 0 : ombh->rate = htonl(mb->rate);
1890 : 0 : ombh->burst_size = htonl(mb->burst_size);
1891 : 0 : ombh->prec_level = mb->prec_level;
1892 : :
1893 : 0 : mb++;
1894 : : }
1895 : 0 : }
1896 : :
1897 : : /* Encode a meter stat for 'mc' and append it to 'replies'. */
1898 : : void
1899 : 0 : ofputil_append_meter_config(struct ovs_list *replies,
1900 : : const struct ofputil_meter_config *mc)
1901 : : {
1902 : 0 : struct ofpbuf *msg = ofpbuf_from_list(ovs_list_back(replies));
1903 : 0 : size_t start_ofs = msg->size;
1904 : : struct ofp13_meter_config *reply;
1905 : :
1906 : 0 : ofpbuf_put_uninit(msg, sizeof *reply);
1907 : 0 : ofputil_put_bands(mc->n_bands, mc->bands, msg);
1908 : :
1909 : 0 : reply = ofpbuf_at_assert(msg, start_ofs, sizeof *reply);
1910 : 0 : reply->flags = htons(mc->flags);
1911 : 0 : reply->meter_id = htonl(mc->meter_id);
1912 : 0 : reply->length = htons(msg->size - start_ofs);
1913 : :
1914 : 0 : ofpmp_postappend(replies, start_ofs);
1915 : 0 : }
1916 : :
1917 : : /* Encode a meter stat for 'ms' and append it to 'replies'. */
1918 : : void
1919 : 0 : ofputil_append_meter_stats(struct ovs_list *replies,
1920 : : const struct ofputil_meter_stats *ms)
1921 : : {
1922 : : struct ofp13_meter_stats *reply;
1923 : 0 : uint16_t n = 0;
1924 : : uint16_t len;
1925 : :
1926 : 0 : len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1927 : 0 : reply = ofpmp_append(replies, len);
1928 : :
1929 : 0 : reply->meter_id = htonl(ms->meter_id);
1930 : 0 : reply->len = htons(len);
1931 : 0 : memset(reply->pad, 0, sizeof reply->pad);
1932 : 0 : reply->flow_count = htonl(ms->flow_count);
1933 : 0 : reply->packet_in_count = htonll(ms->packet_in_count);
1934 : 0 : reply->byte_in_count = htonll(ms->byte_in_count);
1935 : 0 : reply->duration_sec = htonl(ms->duration_sec);
1936 : 0 : reply->duration_nsec = htonl(ms->duration_nsec);
1937 : :
1938 [ # # ]: 0 : for (n = 0; n < ms->n_bands; ++n) {
1939 : 0 : const struct ofputil_meter_band_stats *src = &ms->bands[n];
1940 : 0 : struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1941 : :
1942 : 0 : dst->packet_band_count = htonll(src->packet_count);
1943 : 0 : dst->byte_band_count = htonll(src->byte_count);
1944 : : }
1945 : 0 : }
1946 : :
1947 : : /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1948 : : * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1949 : : * 'bands'. The caller must have initialized 'bands' and retains ownership of
1950 : : * it across the call.
1951 : : *
1952 : : * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1953 : : * message. Calling this function multiple times for a single 'msg' iterates
1954 : : * through the replies. 'bands' is cleared for each reply.
1955 : : *
1956 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
1957 : : * otherwise a positive errno value. */
1958 : : int
1959 : 3 : ofputil_decode_meter_config(struct ofpbuf *msg,
1960 : : struct ofputil_meter_config *mc,
1961 : : struct ofpbuf *bands)
1962 : : {
1963 : : const struct ofp13_meter_config *omc;
1964 : : enum ofperr err;
1965 : :
1966 : : /* Pull OpenFlow headers for the first call. */
1967 [ + + ]: 3 : if (!msg->header) {
1968 : 1 : ofpraw_pull_assert(msg);
1969 : : }
1970 : :
1971 [ + + ]: 3 : if (!msg->size) {
1972 : 1 : return EOF;
1973 : : }
1974 : :
1975 : 2 : omc = ofpbuf_try_pull(msg, sizeof *omc);
1976 [ - + ]: 2 : if (!omc) {
1977 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
1978 : : "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
1979 : : msg->size);
1980 : 0 : return OFPERR_OFPBRC_BAD_LEN;
1981 : : }
1982 : :
1983 : 2 : ofpbuf_clear(bands);
1984 : 2 : err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
1985 : : &mc->n_bands, bands);
1986 [ - + ]: 2 : if (err) {
1987 : 0 : return err;
1988 : : }
1989 : 2 : mc->meter_id = ntohl(omc->meter_id);
1990 : 2 : mc->flags = ntohs(omc->flags);
1991 : 2 : mc->bands = bands->data;
1992 : :
1993 : 2 : return 0;
1994 : : }
1995 : :
1996 : : static enum ofperr
1997 : 2 : ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1998 : : struct ofpbuf *bands)
1999 : : {
2000 : : const struct ofp13_meter_band_stats *ombs;
2001 : : struct ofputil_meter_band_stats *mbs;
2002 : : uint16_t n, i;
2003 : :
2004 : 2 : ombs = ofpbuf_try_pull(msg, len);
2005 [ - + ]: 2 : if (!ombs) {
2006 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2007 : : }
2008 : :
2009 : 2 : n = len / sizeof *ombs;
2010 [ - + ]: 2 : if (len != n * sizeof *ombs) {
2011 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2012 : : }
2013 : :
2014 : 2 : mbs = ofpbuf_put_uninit(bands, len);
2015 : :
2016 [ + + ]: 5 : for (i = 0; i < n; ++i) {
2017 : 3 : mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2018 : 3 : mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2019 : : }
2020 : 2 : *n_bands = n;
2021 : 2 : return 0;
2022 : : }
2023 : :
2024 : : /* Converts an OFPMP_METER reply in 'msg' into an abstract
2025 : : * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2026 : : * decoded into 'bands'.
2027 : : *
2028 : : * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2029 : : * message. Calling this function multiple times for a single 'msg' iterates
2030 : : * through the replies. 'bands' is cleared for each reply.
2031 : : *
2032 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
2033 : : * otherwise a positive errno value. */
2034 : : int
2035 : 3 : ofputil_decode_meter_stats(struct ofpbuf *msg,
2036 : : struct ofputil_meter_stats *ms,
2037 : : struct ofpbuf *bands)
2038 : : {
2039 : : const struct ofp13_meter_stats *oms;
2040 : : enum ofperr err;
2041 : :
2042 : : /* Pull OpenFlow headers for the first call. */
2043 [ + + ]: 3 : if (!msg->header) {
2044 : 1 : ofpraw_pull_assert(msg);
2045 : : }
2046 : :
2047 [ + + ]: 3 : if (!msg->size) {
2048 : 1 : return EOF;
2049 : : }
2050 : :
2051 : 2 : oms = ofpbuf_try_pull(msg, sizeof *oms);
2052 [ - + ]: 2 : if (!oms) {
2053 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
2054 : : "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2055 : : msg->size);
2056 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2057 : : }
2058 : :
2059 : 2 : ofpbuf_clear(bands);
2060 : 2 : err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2061 : : &ms->n_bands, bands);
2062 [ - + ]: 2 : if (err) {
2063 : 0 : return err;
2064 : : }
2065 : 2 : ms->meter_id = ntohl(oms->meter_id);
2066 : 2 : ms->flow_count = ntohl(oms->flow_count);
2067 : 2 : ms->packet_in_count = ntohll(oms->packet_in_count);
2068 : 2 : ms->byte_in_count = ntohll(oms->byte_in_count);
2069 : 2 : ms->duration_sec = ntohl(oms->duration_sec);
2070 : 2 : ms->duration_nsec = ntohl(oms->duration_nsec);
2071 : 2 : ms->bands = bands->data;
2072 : :
2073 : 2 : return 0;
2074 : : }
2075 : :
2076 : : void
2077 : 1 : ofputil_decode_meter_features(const struct ofp_header *oh,
2078 : : struct ofputil_meter_features *mf)
2079 : : {
2080 : 1 : const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2081 : :
2082 : 1 : mf->max_meters = ntohl(omf->max_meter);
2083 : 1 : mf->band_types = ntohl(omf->band_types);
2084 : 1 : mf->capabilities = ntohl(omf->capabilities);
2085 : 1 : mf->max_bands = omf->max_bands;
2086 : 1 : mf->max_color = omf->max_color;
2087 : 1 : }
2088 : :
2089 : : struct ofpbuf *
2090 : 0 : ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2091 : : const struct ofp_header *request)
2092 : : {
2093 : : struct ofpbuf *reply;
2094 : : struct ofp13_meter_features *omf;
2095 : :
2096 : 0 : reply = ofpraw_alloc_stats_reply(request, 0);
2097 : 0 : omf = ofpbuf_put_zeros(reply, sizeof *omf);
2098 : :
2099 : 0 : omf->max_meter = htonl(mf->max_meters);
2100 : 0 : omf->band_types = htonl(mf->band_types);
2101 : 0 : omf->capabilities = htonl(mf->capabilities);
2102 : 0 : omf->max_bands = mf->max_bands;
2103 : 0 : omf->max_color = mf->max_color;
2104 : :
2105 : 0 : return reply;
2106 : : }
2107 : :
2108 : : struct ofpbuf *
2109 : 0 : ofputil_encode_meter_mod(enum ofp_version ofp_version,
2110 : : const struct ofputil_meter_mod *mm)
2111 : : {
2112 : : struct ofpbuf *msg;
2113 : :
2114 : : struct ofp13_meter_mod *omm;
2115 : :
2116 : 0 : msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2117 : 0 : NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2118 : 0 : omm = ofpbuf_put_zeros(msg, sizeof *omm);
2119 : 0 : omm->command = htons(mm->command);
2120 [ # # ]: 0 : if (mm->command != OFPMC13_DELETE) {
2121 : 0 : omm->flags = htons(mm->meter.flags);
2122 : : }
2123 : 0 : omm->meter_id = htonl(mm->meter.meter_id);
2124 : :
2125 : 0 : ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2126 : :
2127 : 0 : ofpmsg_update_length(msg);
2128 : 0 : return msg;
2129 : : }
2130 : :
2131 : : static ovs_be16
2132 : 11057 : ofputil_tid_command(const struct ofputil_flow_mod *fm,
2133 : : enum ofputil_protocol protocol)
2134 : : {
2135 [ + + ]: 11057 : return htons(protocol & OFPUTIL_P_TID
2136 : 1094 : ? (fm->command & 0xff) | (fm->table_id << 8)
2137 : 10510 : : fm->command);
2138 : : }
2139 : :
2140 : : /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2141 : : * 'protocol' and returns the message. */
2142 : : struct ofpbuf *
2143 : 33847 : ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2144 : : enum ofputil_protocol protocol)
2145 : : {
2146 : 33847 : enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2147 : 33847 : ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2148 : : struct ofpbuf *msg;
2149 : :
2150 [ + + + - ]: 33847 : switch (protocol) {
2151 : : case OFPUTIL_P_OF11_STD:
2152 : : case OFPUTIL_P_OF12_OXM:
2153 : : case OFPUTIL_P_OF13_OXM:
2154 : : case OFPUTIL_P_OF14_OXM:
2155 : : case OFPUTIL_P_OF15_OXM:
2156 : : case OFPUTIL_P_OF16_OXM: {
2157 : : struct ofp11_flow_mod *ofm;
2158 : : int tailroom;
2159 : :
2160 : 22790 : tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2161 : 22790 : msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2162 : 22790 : ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2163 [ + + ]: 22790 : if ((protocol == OFPUTIL_P_OF11_STD
2164 [ + + ][ - + ]: 60 : && (fm->command == OFPFC_MODIFY ||
2165 : 56 : fm->command == OFPFC_MODIFY_STRICT)
2166 [ + + ]: 4 : && fm->cookie_mask == htonll(0))
2167 [ + + ]: 22788 : || fm->command == OFPFC_ADD) {
2168 : 16677 : ofm->cookie = fm->new_cookie;
2169 : : } else {
2170 : 6113 : ofm->cookie = fm->cookie & fm->cookie_mask;
2171 : : }
2172 : 22790 : ofm->cookie_mask = fm->cookie_mask;
2173 [ + + ]: 22790 : if (fm->table_id != OFPTT_ALL
2174 [ + + ]: 689 : || (protocol != OFPUTIL_P_OF11_STD
2175 [ + + ][ + + ]: 637 : && (fm->command == OFPFC_DELETE ||
2176 : 578 : fm->command == OFPFC_DELETE_STRICT))) {
2177 : 22181 : ofm->table_id = fm->table_id;
2178 : : } else {
2179 : 609 : ofm->table_id = 0;
2180 : : }
2181 : 22790 : ofm->command = fm->command;
2182 : 22790 : ofm->idle_timeout = htons(fm->idle_timeout);
2183 : 22790 : ofm->hard_timeout = htons(fm->hard_timeout);
2184 : 22790 : ofm->priority = htons(fm->priority);
2185 : 22790 : ofm->buffer_id = htonl(fm->buffer_id);
2186 : 22790 : ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2187 : 22790 : ofm->out_group = htonl(fm->out_group);
2188 : 22790 : ofm->flags = raw_flags;
2189 [ + + ][ + + ]: 22790 : if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2190 : 717 : ofm->importance = htons(fm->importance);
2191 : : } else {
2192 : 22073 : ofm->importance = 0;
2193 : : }
2194 : 22790 : ofputil_put_ofp11_match(msg, &fm->match, protocol);
2195 : 22790 : ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2196 : : version);
2197 : 22790 : break;
2198 : : }
2199 : :
2200 : : case OFPUTIL_P_OF10_STD:
2201 : : case OFPUTIL_P_OF10_STD_TID: {
2202 : : struct ofp10_flow_mod *ofm;
2203 : :
2204 : 3346 : msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2205 : : fm->ofpacts_len);
2206 : 3346 : ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2207 : 3346 : ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2208 : 3346 : ofm->cookie = fm->new_cookie;
2209 : 3346 : ofm->command = ofputil_tid_command(fm, protocol);
2210 : 3346 : ofm->idle_timeout = htons(fm->idle_timeout);
2211 : 3346 : ofm->hard_timeout = htons(fm->hard_timeout);
2212 : 3346 : ofm->priority = htons(fm->priority);
2213 : 3346 : ofm->buffer_id = htonl(fm->buffer_id);
2214 : 3346 : ofm->out_port = htons(ofp_to_u16(fm->out_port));
2215 : 3346 : ofm->flags = raw_flags;
2216 : 3346 : ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2217 : : version);
2218 : 3346 : break;
2219 : : }
2220 : :
2221 : : case OFPUTIL_P_OF10_NXM:
2222 : : case OFPUTIL_P_OF10_NXM_TID: {
2223 : : struct nx_flow_mod *nfm;
2224 : : int match_len;
2225 : :
2226 : 7711 : msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2227 : 7711 : NXM_TYPICAL_LEN + fm->ofpacts_len);
2228 : 7711 : nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2229 : 7711 : nfm->command = ofputil_tid_command(fm, protocol);
2230 : 7711 : nfm->cookie = fm->new_cookie;
2231 : 7711 : match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2232 : 7711 : nfm = msg->msg;
2233 : 7711 : nfm->idle_timeout = htons(fm->idle_timeout);
2234 : 7711 : nfm->hard_timeout = htons(fm->hard_timeout);
2235 : 7711 : nfm->priority = htons(fm->priority);
2236 : 7711 : nfm->buffer_id = htonl(fm->buffer_id);
2237 : 7711 : nfm->out_port = htons(ofp_to_u16(fm->out_port));
2238 : 7711 : nfm->flags = raw_flags;
2239 : 7711 : nfm->match_len = htons(match_len);
2240 : 7711 : ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2241 : : version);
2242 : 7711 : break;
2243 : : }
2244 : :
2245 : : default:
2246 : 0 : OVS_NOT_REACHED();
2247 : : }
2248 : :
2249 : 33847 : ofpmsg_update_length(msg);
2250 : 33847 : return msg;
2251 : : }
2252 : :
2253 : : static enum ofperr
2254 : 28 : ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2255 : : const struct ofp10_flow_stats_request *ofsr,
2256 : : bool aggregate)
2257 : : {
2258 : 28 : fsr->aggregate = aggregate;
2259 : 28 : ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2260 : 28 : fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2261 : 28 : fsr->out_group = OFPG_ANY;
2262 : 28 : fsr->table_id = ofsr->table_id;
2263 : 28 : fsr->cookie = fsr->cookie_mask = htonll(0);
2264 : :
2265 : 28 : return 0;
2266 : : }
2267 : :
2268 : : static enum ofperr
2269 : 224 : ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2270 : : struct ofpbuf *b, bool aggregate)
2271 : : {
2272 : : const struct ofp11_flow_stats_request *ofsr;
2273 : : enum ofperr error;
2274 : :
2275 : 224 : ofsr = ofpbuf_pull(b, sizeof *ofsr);
2276 : 224 : fsr->aggregate = aggregate;
2277 : 224 : fsr->table_id = ofsr->table_id;
2278 : 224 : error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2279 [ - + ]: 224 : if (error) {
2280 : 0 : return error;
2281 : : }
2282 : 224 : fsr->out_group = ntohl(ofsr->out_group);
2283 : 224 : fsr->cookie = ofsr->cookie;
2284 : 224 : fsr->cookie_mask = ofsr->cookie_mask;
2285 : 224 : error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2286 [ - + ]: 224 : if (error) {
2287 : 0 : return error;
2288 : : }
2289 : :
2290 : 224 : return 0;
2291 : : }
2292 : :
2293 : : static enum ofperr
2294 : 530 : ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2295 : : struct ofpbuf *b, bool aggregate)
2296 : : {
2297 : : const struct nx_flow_stats_request *nfsr;
2298 : : enum ofperr error;
2299 : :
2300 : 530 : nfsr = ofpbuf_pull(b, sizeof *nfsr);
2301 : 530 : error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2302 : : &fsr->cookie, &fsr->cookie_mask);
2303 [ - + ]: 530 : if (error) {
2304 : 0 : return error;
2305 : : }
2306 [ - + ]: 530 : if (b->size) {
2307 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2308 : : }
2309 : :
2310 : 530 : fsr->aggregate = aggregate;
2311 : 530 : fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2312 : 530 : fsr->out_group = OFPG_ANY;
2313 : 530 : fsr->table_id = nfsr->table_id;
2314 : :
2315 : 530 : return 0;
2316 : : }
2317 : :
2318 : : /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2319 : : * 'port' and 'queue', suitable for OpenFlow version 'version'.
2320 : : *
2321 : : * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2322 : : * request all queues. */
2323 : : struct ofpbuf *
2324 : 13 : ofputil_encode_queue_get_config_request(enum ofp_version version,
2325 : : ofp_port_t port,
2326 : : uint32_t queue)
2327 : : {
2328 : : struct ofpbuf *request;
2329 : :
2330 [ + + ]: 13 : if (version == OFP10_VERSION) {
2331 : : struct ofp10_queue_get_config_request *qgcr10;
2332 : :
2333 : 4 : request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2334 : : version, 0);
2335 : 4 : qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2336 : 4 : qgcr10->port = htons(ofp_to_u16(port));
2337 [ + + ]: 9 : } else if (version < OFP14_VERSION) {
2338 : : struct ofp11_queue_get_config_request *qgcr11;
2339 : :
2340 : 5 : request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2341 : : version, 0);
2342 : 5 : qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2343 : 5 : qgcr11->port = ofputil_port_to_ofp11(port);
2344 : : } else {
2345 : : struct ofp14_queue_desc_request *qdr14;
2346 : :
2347 : 4 : request = ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST,
2348 : : version, 0);
2349 : 4 : qdr14 = ofpbuf_put_zeros(request, sizeof *qdr14);
2350 : 4 : qdr14->port = ofputil_port_to_ofp11(port);
2351 : 4 : qdr14->queue = htonl(queue);
2352 : : }
2353 : :
2354 : 13 : return request;
2355 : : }
2356 : :
2357 : : /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2358 : : * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2359 : : * code. */
2360 : : enum ofperr
2361 : 52 : ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2362 : : ofp_port_t *port, uint32_t *queue)
2363 : : {
2364 : : const struct ofp10_queue_get_config_request *qgcr10;
2365 : : const struct ofp11_queue_get_config_request *qgcr11;
2366 : : const struct ofp14_queue_desc_request *qdr14;
2367 : 52 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2368 : 52 : enum ofpraw raw = ofpraw_pull_assert(&b);
2369 : :
2370 [ + + + - ]: 52 : switch ((int) raw) {
2371 : : case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2372 : 15 : qgcr10 = b.data;
2373 : 15 : *port = u16_to_ofp(ntohs(qgcr10->port));
2374 : 15 : *queue = OFPQ_ALL;
2375 : 15 : break;
2376 : :
2377 : : case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2378 : 20 : qgcr11 = b.data;
2379 : 20 : *queue = OFPQ_ALL;
2380 : 20 : enum ofperr error = ofputil_port_from_ofp11(qgcr11->port, port);
2381 [ + - ][ + + ]: 20 : if (error || *port == OFPP_ANY) {
2382 : 3 : return error;
2383 : : }
2384 : 17 : break;
2385 : :
2386 : : case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2387 : 17 : qdr14 = b.data;
2388 : 17 : *queue = ntohl(qdr14->queue);
2389 : 17 : return ofputil_port_from_ofp11(qdr14->port, port);
2390 : :
2391 : : default:
2392 : 0 : OVS_NOT_REACHED();
2393 : : }
2394 : :
2395 [ + - ]: 52 : return (ofp_to_u16(*port) < ofp_to_u16(OFPP_MAX)
2396 : : ? 0
2397 : : : OFPERR_OFPQOFC_BAD_PORT);
2398 : : }
2399 : :
2400 : : /* Constructs and returns the beginning of a reply to
2401 : : * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2402 : : * may append information about individual queues with
2403 : : * ofputil_append_queue_get_config_reply(). */
2404 : : void
2405 : 13 : ofputil_start_queue_get_config_reply(const struct ofp_header *request,
2406 : : struct ovs_list *replies)
2407 : : {
2408 : : struct ofpbuf *reply;
2409 : : enum ofperr error;
2410 : : ofp_port_t port;
2411 : : uint32_t queue;
2412 : :
2413 : 13 : error = ofputil_decode_queue_get_config_request(request, &port, &queue);
2414 [ - + ]: 13 : ovs_assert(!error);
2415 : :
2416 : 13 : enum ofpraw raw = ofpraw_decode_assert(request);
2417 [ + + + - ]: 13 : switch ((int) raw) {
2418 : : case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2419 : 4 : reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2420 : : request, 0);
2421 : 4 : struct ofp10_queue_get_config_reply *qgcr10
2422 : : = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2423 : 4 : qgcr10->port = htons(ofp_to_u16(port));
2424 : 4 : break;
2425 : :
2426 : : case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2427 : 5 : reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2428 : : request, 0);
2429 : 5 : struct ofp11_queue_get_config_reply *qgcr11
2430 : : = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2431 : 5 : qgcr11->port = ofputil_port_to_ofp11(port);
2432 : 5 : break;
2433 : :
2434 : : case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2435 : 4 : reply = ofpraw_alloc_stats_reply(request, 0);
2436 : 4 : break;
2437 : :
2438 : : default:
2439 : 0 : OVS_NOT_REACHED();
2440 : : }
2441 : :
2442 : 13 : ovs_list_init(replies);
2443 : 13 : ovs_list_push_back(replies, &reply->list_node);
2444 : 13 : }
2445 : :
2446 : : static void
2447 : 16 : put_ofp10_queue_rate(struct ofpbuf *reply,
2448 : : enum ofp10_queue_properties property, uint16_t rate)
2449 : : {
2450 [ - + ]: 16 : if (rate != UINT16_MAX) {
2451 : : struct ofp10_queue_prop_rate *oqpr;
2452 : :
2453 : 0 : oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2454 : 0 : oqpr->prop_header.property = htons(property);
2455 : 0 : oqpr->prop_header.len = htons(sizeof *oqpr);
2456 : 0 : oqpr->rate = htons(rate);
2457 : : }
2458 : 16 : }
2459 : :
2460 : : static void
2461 : 8 : put_ofp14_queue_rate(struct ofpbuf *reply,
2462 : : enum ofp14_queue_desc_prop_type type, uint16_t rate)
2463 : : {
2464 [ - + ]: 8 : if (rate != UINT16_MAX) {
2465 : 0 : ofpprop_put_u16(reply, type, rate);
2466 : : }
2467 : 8 : }
2468 : :
2469 : : void
2470 : 12 : ofputil_append_queue_get_config_reply(const struct ofputil_queue_config *qc,
2471 : : struct ovs_list *replies)
2472 : : {
2473 : 12 : enum ofp_version ofp_version = ofpmp_version(replies);
2474 : 12 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
2475 : 12 : size_t start_ofs = reply->size;
2476 : : size_t len_ofs;
2477 : : ovs_be16 *len;
2478 : :
2479 [ + + ]: 12 : if (ofp_version < OFP14_VERSION) {
2480 [ + + ]: 8 : if (ofp_version < OFP12_VERSION) {
2481 : : struct ofp10_packet_queue *opq10;
2482 : :
2483 : 4 : opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2484 : 4 : opq10->queue_id = htonl(qc->queue);
2485 : 4 : len_ofs = (char *) &opq10->len - (char *) reply->data;
2486 : : } else {
2487 : : struct ofp12_packet_queue *opq12;
2488 : :
2489 : 4 : opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2490 : 4 : opq12->port = ofputil_port_to_ofp11(qc->port);
2491 : 4 : opq12->queue_id = htonl(qc->queue);
2492 : 4 : len_ofs = (char *) &opq12->len - (char *) reply->data;
2493 : : }
2494 : :
2495 : 8 : put_ofp10_queue_rate(reply, OFPQT10_MIN_RATE, qc->min_rate);
2496 : 8 : put_ofp10_queue_rate(reply, OFPQT11_MAX_RATE, qc->max_rate);
2497 : : } else {
2498 : 4 : struct ofp14_queue_desc *oqd = ofpbuf_put_zeros(reply, sizeof *oqd);
2499 : 4 : oqd->port_no = ofputil_port_to_ofp11(qc->port);
2500 : 4 : oqd->queue_id = htonl(qc->queue);
2501 : 4 : len_ofs = (char *) &oqd->len - (char *) reply->data;
2502 : 4 : put_ofp14_queue_rate(reply, OFPQDPT14_MIN_RATE, qc->min_rate);
2503 : 4 : put_ofp14_queue_rate(reply, OFPQDPT14_MAX_RATE, qc->max_rate);
2504 : : }
2505 : :
2506 : 12 : len = ofpbuf_at(reply, len_ofs, sizeof *len);
2507 : 12 : *len = htons(reply->size - start_ofs);
2508 : :
2509 [ + + ]: 12 : if (ofp_version >= OFP14_VERSION) {
2510 : 4 : ofpmp_postappend(replies, start_ofs);
2511 : : }
2512 : 12 : }
2513 : :
2514 : : static enum ofperr
2515 : 8 : parse_ofp10_queue_rate(const struct ofp10_queue_prop_header *hdr,
2516 : : uint16_t *rate)
2517 : : {
2518 : : const struct ofp10_queue_prop_rate *oqpr;
2519 : :
2520 [ + - ]: 8 : if (hdr->len == htons(sizeof *oqpr)) {
2521 : 8 : oqpr = (const struct ofp10_queue_prop_rate *) hdr;
2522 : 8 : *rate = ntohs(oqpr->rate);
2523 : 8 : return 0;
2524 : : } else {
2525 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2526 : : }
2527 : : }
2528 : :
2529 : : static int
2530 : 24 : ofputil_pull_queue_get_config_reply10(struct ofpbuf *msg,
2531 : : struct ofputil_queue_config *queue)
2532 : : {
2533 : 24 : const struct ofp_header *oh = msg->header;
2534 : : unsigned int opq_len; /* Length of protocol-specific queue header. */
2535 : : unsigned int len; /* Total length of queue + properties. */
2536 : :
2537 : : /* Obtain the port number from the message header. */
2538 [ + + ]: 24 : if (oh->version == OFP10_VERSION) {
2539 : 8 : const struct ofp10_queue_get_config_reply *oqgcr10 = msg->msg;
2540 : 8 : queue->port = u16_to_ofp(ntohs(oqgcr10->port));
2541 : : } else {
2542 : 16 : const struct ofp11_queue_get_config_reply *oqgcr11 = msg->msg;
2543 : 16 : enum ofperr error = ofputil_port_from_ofp11(oqgcr11->port,
2544 : : &queue->port);
2545 [ - + ]: 16 : if (error) {
2546 : 0 : return error;
2547 : : }
2548 : : }
2549 : :
2550 : : /* Pull off the queue header and get the queue number and length. */
2551 [ + + ]: 24 : if (oh->version < OFP12_VERSION) {
2552 : : const struct ofp10_packet_queue *opq10;
2553 : 12 : opq10 = ofpbuf_try_pull(msg, sizeof *opq10);
2554 [ - + ]: 12 : if (!opq10) {
2555 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2556 : : }
2557 : 12 : queue->queue = ntohl(opq10->queue_id);
2558 : 12 : len = ntohs(opq10->len);
2559 : 12 : opq_len = sizeof *opq10;
2560 : : } else {
2561 : : const struct ofp12_packet_queue *opq12;
2562 : 12 : opq12 = ofpbuf_try_pull(msg, sizeof *opq12);
2563 [ - + ]: 12 : if (!opq12) {
2564 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2565 : : }
2566 : 12 : queue->queue = ntohl(opq12->queue_id);
2567 : 12 : len = ntohs(opq12->len);
2568 : 12 : opq_len = sizeof *opq12;
2569 : : }
2570 : :
2571 : : /* Length check. */
2572 [ + - ][ + - ]: 24 : if (len < opq_len || len > msg->size + opq_len || len % 8) {
[ - + ]
2573 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2574 : : }
2575 : 24 : len -= opq_len;
2576 : :
2577 : : /* Pull properties. The format of these properties differs from used in
2578 : : * OF1.4+ so we can't use the common property functions. */
2579 [ + + ]: 32 : while (len > 0) {
2580 : : const struct ofp10_queue_prop_header *hdr;
2581 : : unsigned int property;
2582 : : unsigned int prop_len;
2583 : 8 : enum ofperr error = 0;
2584 : :
2585 : 8 : hdr = ofpbuf_at_assert(msg, 0, sizeof *hdr);
2586 : 8 : prop_len = ntohs(hdr->len);
2587 [ + - ][ + - ]: 8 : if (prop_len < sizeof *hdr || prop_len > msg->size || prop_len % 8) {
[ - + ]
2588 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2589 : : }
2590 : :
2591 : 8 : property = ntohs(hdr->property);
2592 [ + + - ]: 8 : switch (property) {
2593 : : case OFPQT10_MIN_RATE:
2594 : 4 : error = parse_ofp10_queue_rate(hdr, &queue->min_rate);
2595 : 4 : break;
2596 : :
2597 : : case OFPQT11_MAX_RATE:
2598 : 4 : error = parse_ofp10_queue_rate(hdr, &queue->max_rate);
2599 : 4 : break;
2600 : :
2601 : : default:
2602 [ # # ]: 0 : VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2603 : 0 : break;
2604 : : }
2605 [ - + ]: 8 : if (error) {
2606 : 0 : return error;
2607 : : }
2608 : :
2609 : 8 : ofpbuf_pull(msg, prop_len);
2610 : 8 : len -= prop_len;
2611 : : }
2612 : 24 : return 0;
2613 : : }
2614 : :
2615 : : static int
2616 : 10 : ofputil_pull_queue_get_config_reply14(struct ofpbuf *msg,
2617 : : struct ofputil_queue_config *queue)
2618 : : {
2619 : 10 : struct ofp14_queue_desc *oqd14 = ofpbuf_try_pull(msg, sizeof *oqd14);
2620 [ - + ]: 10 : if (!oqd14) {
2621 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2622 : : }
2623 : 10 : enum ofperr error = ofputil_port_from_ofp11(oqd14->port_no, &queue->port);
2624 [ - + ]: 10 : if (error) {
2625 : 0 : return error;
2626 : : }
2627 : 10 : queue->queue = ntohl(oqd14->queue_id);
2628 : :
2629 : : /* Length check. */
2630 : 10 : unsigned int len = ntohs(oqd14->len);
2631 [ + - ][ + - ]: 10 : if (len < sizeof *oqd14 || len > msg->size + sizeof *oqd14 || len % 8) {
[ - + ]
2632 : 0 : return OFPERR_OFPBRC_BAD_LEN;
2633 : : }
2634 : 10 : len -= sizeof *oqd14;
2635 : :
2636 : 10 : struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
2637 : : len);
2638 [ + + ]: 13 : while (properties.size > 0) {
2639 : : struct ofpbuf payload;
2640 : : uint64_t type;
2641 : :
2642 : 3 : error = ofpprop_pull(&properties, &payload, &type);
2643 [ - + ]: 3 : if (error) {
2644 : 0 : return error;
2645 : : }
2646 : :
2647 [ + + - ]: 3 : switch (type) {
2648 : : case OFPQDPT14_MIN_RATE:
2649 : 1 : error = ofpprop_parse_u16(&payload, &queue->min_rate);
2650 : 1 : break;
2651 : :
2652 : : case OFPQDPT14_MAX_RATE:
2653 : 2 : error = ofpprop_parse_u16(&payload, &queue->max_rate);
2654 : 2 : break;
2655 : :
2656 : : default:
2657 : 0 : error = OFPPROP_UNKNOWN(true, "queue desc", type);
2658 : 0 : break;
2659 : : }
2660 : :
2661 [ - + ]: 3 : if (error) {
2662 : 3 : return error;
2663 : : }
2664 : : }
2665 : :
2666 : 10 : return 0;
2667 : : }
2668 : :
2669 : : /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2670 : : * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2671 : : * must already have pulled off the main header.
2672 : : *
2673 : : * This function returns EOF if the last queue has already been decoded, 0 if a
2674 : : * queue was successfully decoded into '*queue', or an ofperr if there was a
2675 : : * problem decoding 'reply'. */
2676 : : int
2677 : 55 : ofputil_pull_queue_get_config_reply(struct ofpbuf *msg,
2678 : : struct ofputil_queue_config *queue)
2679 : : {
2680 : : enum ofpraw raw;
2681 [ + + ]: 55 : if (!msg->header) {
2682 : : /* Pull OpenFlow header. */
2683 : 21 : raw = ofpraw_pull_assert(msg);
2684 : :
2685 : : /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2686 : : * doesn't have one at all). */
2687 [ + + ]: 21 : if (raw == OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY) {
2688 : 7 : ofpbuf_pull(msg, sizeof(struct ofp10_queue_get_config_reply));
2689 [ + + ]: 14 : } else if (raw == OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY) {
2690 : 9 : ofpbuf_pull(msg, sizeof(struct ofp11_queue_get_config_reply));
2691 : : } else {
2692 [ - + ]: 5 : ovs_assert(raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY);
2693 : : }
2694 : : } else {
2695 : 34 : raw = ofpraw_decode_assert(msg->header);
2696 : : }
2697 : :
2698 : 55 : queue->min_rate = UINT16_MAX;
2699 : 55 : queue->max_rate = UINT16_MAX;
2700 : :
2701 [ + + ]: 55 : if (!msg->size) {
2702 : 21 : return EOF;
2703 [ + + ]: 34 : } else if (raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY) {
2704 : 10 : return ofputil_pull_queue_get_config_reply14(msg, queue);
2705 : : } else {
2706 : 24 : return ofputil_pull_queue_get_config_reply10(msg, queue);
2707 : : }
2708 : : }
2709 : :
2710 : : /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2711 : : * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2712 : : * successful, otherwise an OpenFlow error code. */
2713 : : enum ofperr
2714 : 782 : ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2715 : : const struct ofp_header *oh)
2716 : : {
2717 : 782 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2718 : 782 : enum ofpraw raw = ofpraw_pull_assert(&b);
2719 [ + + + + : 782 : switch ((int) raw) {
+ + - ]
2720 : : case OFPRAW_OFPST10_FLOW_REQUEST:
2721 : 25 : return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2722 : :
2723 : : case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2724 : 3 : return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2725 : :
2726 : : case OFPRAW_OFPST11_FLOW_REQUEST:
2727 : 220 : return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2728 : :
2729 : : case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2730 : 4 : return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2731 : :
2732 : : case OFPRAW_NXST_FLOW_REQUEST:
2733 : 523 : return ofputil_decode_nxst_flow_request(fsr, &b, false);
2734 : :
2735 : : case OFPRAW_NXST_AGGREGATE_REQUEST:
2736 : 7 : return ofputil_decode_nxst_flow_request(fsr, &b, true);
2737 : :
2738 : : default:
2739 : : /* Hey, the caller lied. */
2740 : 782 : OVS_NOT_REACHED();
2741 : : }
2742 : : }
2743 : :
2744 : : /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2745 : : * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2746 : : * 'protocol', and returns the message. */
2747 : : struct ofpbuf *
2748 : 407 : ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2749 : : enum ofputil_protocol protocol)
2750 : : {
2751 : : struct ofpbuf *msg;
2752 : : enum ofpraw raw;
2753 : :
2754 [ + + + - ]: 407 : switch (protocol) {
2755 : : case OFPUTIL_P_OF11_STD:
2756 : : case OFPUTIL_P_OF12_OXM:
2757 : : case OFPUTIL_P_OF13_OXM:
2758 : : case OFPUTIL_P_OF14_OXM:
2759 : : case OFPUTIL_P_OF15_OXM:
2760 : : case OFPUTIL_P_OF16_OXM: {
2761 : : struct ofp11_flow_stats_request *ofsr;
2762 : :
2763 [ + + ]: 128 : raw = (fsr->aggregate
2764 : : ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2765 : : : OFPRAW_OFPST11_FLOW_REQUEST);
2766 : 128 : msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2767 : 128 : ofputil_match_typical_len(protocol));
2768 : 128 : ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2769 : 128 : ofsr->table_id = fsr->table_id;
2770 : 128 : ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2771 : 128 : ofsr->out_group = htonl(fsr->out_group);
2772 : 128 : ofsr->cookie = fsr->cookie;
2773 : 128 : ofsr->cookie_mask = fsr->cookie_mask;
2774 : 128 : ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2775 : 128 : break;
2776 : : }
2777 : :
2778 : : case OFPUTIL_P_OF10_STD:
2779 : : case OFPUTIL_P_OF10_STD_TID: {
2780 : : struct ofp10_flow_stats_request *ofsr;
2781 : :
2782 [ + + ]: 13 : raw = (fsr->aggregate
2783 : : ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2784 : : : OFPRAW_OFPST10_FLOW_REQUEST);
2785 : 13 : msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2786 : 13 : ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2787 : 13 : ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2788 : 13 : ofsr->table_id = fsr->table_id;
2789 : 13 : ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2790 : 13 : break;
2791 : : }
2792 : :
2793 : : case OFPUTIL_P_OF10_NXM:
2794 : : case OFPUTIL_P_OF10_NXM_TID: {
2795 : : struct nx_flow_stats_request *nfsr;
2796 : : int match_len;
2797 : :
2798 [ + + ]: 266 : raw = (fsr->aggregate
2799 : : ? OFPRAW_NXST_AGGREGATE_REQUEST
2800 : : : OFPRAW_NXST_FLOW_REQUEST);
2801 : 266 : msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2802 : 266 : ofpbuf_put_zeros(msg, sizeof *nfsr);
2803 : 266 : match_len = nx_put_match(msg, &fsr->match,
2804 : : fsr->cookie, fsr->cookie_mask);
2805 : :
2806 : 266 : nfsr = msg->msg;
2807 : 266 : nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2808 : 266 : nfsr->match_len = htons(match_len);
2809 : 266 : nfsr->table_id = fsr->table_id;
2810 : 266 : break;
2811 : : }
2812 : :
2813 : : default:
2814 : 0 : OVS_NOT_REACHED();
2815 : : }
2816 : :
2817 : 407 : return msg;
2818 : : }
2819 : :
2820 : : /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2821 : : * ofputil_flow_stats in 'fs'.
2822 : : *
2823 : : * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2824 : : * OpenFlow message. Calling this function multiple times for a single 'msg'
2825 : : * iterates through the replies. The caller must initially leave 'msg''s layer
2826 : : * pointers null and not modify them between calls.
2827 : : *
2828 : : * Most switches don't send the values needed to populate fs->idle_age and
2829 : : * fs->hard_age, so those members will usually be set to 0. If the switch from
2830 : : * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2831 : : * 'flow_age_extension' as true so that the contents of 'msg' determine the
2832 : : * 'idle_age' and 'hard_age' members in 'fs'.
2833 : : *
2834 : : * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2835 : : * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2836 : : * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2837 : : *
2838 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
2839 : : * otherwise a positive errno value. */
2840 : : int
2841 : 38237 : ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2842 : : struct ofpbuf *msg,
2843 : : bool flow_age_extension,
2844 : : struct ofpbuf *ofpacts)
2845 : : {
2846 : : const struct ofp_header *oh;
2847 : : size_t instructions_len;
2848 : : enum ofperr error;
2849 : : enum ofpraw raw;
2850 : :
2851 [ + + ]: 38237 : error = (msg->header ? ofpraw_decode(&raw, msg->header)
2852 : : : ofpraw_pull(&raw, msg));
2853 [ - + ]: 38237 : if (error) {
2854 : 0 : return error;
2855 : : }
2856 : 38237 : oh = msg->header;
2857 : :
2858 [ + + ]: 38237 : if (!msg->size) {
2859 : 803 : return EOF;
2860 [ + + ]: 37434 : } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2861 [ + + ]: 41637 : || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2862 : : const struct ofp11_flow_stats *ofs;
2863 : : size_t length;
2864 : : uint16_t padded_match_len;
2865 : :
2866 : 4493 : ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2867 [ - + ]: 4493 : if (!ofs) {
2868 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2869 : : "bytes at end", msg->size);
2870 : 0 : return EINVAL;
2871 : : }
2872 : :
2873 : 4493 : length = ntohs(ofs->length);
2874 [ - + ]: 4493 : if (length < sizeof *ofs) {
2875 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2876 : : "length %"PRIuSIZE, length);
2877 : 0 : return EINVAL;
2878 : : }
2879 : :
2880 [ - + ]: 4493 : if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2881 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2882 : 0 : return EINVAL;
2883 : : }
2884 : 4493 : instructions_len = length - sizeof *ofs - padded_match_len;
2885 : :
2886 : 4493 : fs->priority = ntohs(ofs->priority);
2887 : 4493 : fs->table_id = ofs->table_id;
2888 : 4493 : fs->duration_sec = ntohl(ofs->duration_sec);
2889 : 4493 : fs->duration_nsec = ntohl(ofs->duration_nsec);
2890 : 4493 : fs->idle_timeout = ntohs(ofs->idle_timeout);
2891 : 4493 : fs->hard_timeout = ntohs(ofs->hard_timeout);
2892 [ + + ]: 4493 : if (oh->version >= OFP14_VERSION) {
2893 : 230 : fs->importance = ntohs(ofs->importance);
2894 : : } else {
2895 : 4263 : fs->importance = 0;
2896 : : }
2897 [ + + ]: 4493 : if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2898 : 4203 : error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2899 : : &fs->flags);
2900 [ - + ]: 4203 : if (error) {
2901 : 0 : return error;
2902 : : }
2903 : : } else {
2904 : 290 : fs->flags = 0;
2905 : : }
2906 : 4493 : fs->idle_age = -1;
2907 : 4493 : fs->hard_age = -1;
2908 : 4493 : fs->cookie = ofs->cookie;
2909 : 4493 : fs->packet_count = ntohll(ofs->packet_count);
2910 : 4493 : fs->byte_count = ntohll(ofs->byte_count);
2911 [ + + ]: 32941 : } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2912 : : const struct ofp10_flow_stats *ofs;
2913 : : size_t length;
2914 : :
2915 : 49 : ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2916 [ - + ]: 49 : if (!ofs) {
2917 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2918 : : "bytes at end", msg->size);
2919 : 0 : return EINVAL;
2920 : : }
2921 : :
2922 : 49 : length = ntohs(ofs->length);
2923 [ - + ]: 49 : if (length < sizeof *ofs) {
2924 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2925 : : "length %"PRIuSIZE, length);
2926 : 0 : return EINVAL;
2927 : : }
2928 : 49 : instructions_len = length - sizeof *ofs;
2929 : :
2930 : 49 : fs->cookie = get_32aligned_be64(&ofs->cookie);
2931 : 49 : ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2932 : 49 : fs->priority = ntohs(ofs->priority);
2933 : 49 : fs->table_id = ofs->table_id;
2934 : 49 : fs->duration_sec = ntohl(ofs->duration_sec);
2935 : 49 : fs->duration_nsec = ntohl(ofs->duration_nsec);
2936 : 49 : fs->idle_timeout = ntohs(ofs->idle_timeout);
2937 : 49 : fs->hard_timeout = ntohs(ofs->hard_timeout);
2938 : 49 : fs->importance = 0;
2939 : 49 : fs->idle_age = -1;
2940 : 49 : fs->hard_age = -1;
2941 : 49 : fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2942 : 49 : fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2943 : 49 : fs->flags = 0;
2944 [ + - ]: 32892 : } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2945 : : const struct nx_flow_stats *nfs;
2946 : : size_t match_len, length;
2947 : :
2948 : 32892 : nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2949 [ - + ]: 32892 : if (!nfs) {
2950 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2951 : : "bytes at end", msg->size);
2952 : 0 : return EINVAL;
2953 : : }
2954 : :
2955 : 32892 : length = ntohs(nfs->length);
2956 : 32892 : match_len = ntohs(nfs->match_len);
2957 [ - + ]: 32892 : if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2958 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2959 : : "claims invalid length %"PRIuSIZE, match_len, length);
2960 : 0 : return EINVAL;
2961 : : }
2962 [ - + ]: 32892 : if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2963 : 0 : return EINVAL;
2964 : : }
2965 : 32892 : instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2966 : :
2967 : 32892 : fs->cookie = nfs->cookie;
2968 : 32892 : fs->table_id = nfs->table_id;
2969 : 32892 : fs->duration_sec = ntohl(nfs->duration_sec);
2970 : 32892 : fs->duration_nsec = ntohl(nfs->duration_nsec);
2971 : 32892 : fs->priority = ntohs(nfs->priority);
2972 : 32892 : fs->idle_timeout = ntohs(nfs->idle_timeout);
2973 : 32892 : fs->hard_timeout = ntohs(nfs->hard_timeout);
2974 : 32892 : fs->importance = 0;
2975 : 32892 : fs->idle_age = -1;
2976 : 32892 : fs->hard_age = -1;
2977 [ + + ]: 32892 : if (flow_age_extension) {
2978 [ + + ]: 23601 : if (nfs->idle_age) {
2979 : 23589 : fs->idle_age = ntohs(nfs->idle_age) - 1;
2980 : : }
2981 [ + + ]: 23601 : if (nfs->hard_age) {
2982 : 23586 : fs->hard_age = ntohs(nfs->hard_age) - 1;
2983 : : }
2984 : : }
2985 : 32892 : fs->packet_count = ntohll(nfs->packet_count);
2986 : 32892 : fs->byte_count = ntohll(nfs->byte_count);
2987 : 32892 : fs->flags = 0;
2988 : : } else {
2989 : 0 : OVS_NOT_REACHED();
2990 : : }
2991 : :
2992 [ - + ]: 37434 : if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2993 : : ofpacts)) {
2994 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2995 : 0 : return EINVAL;
2996 : : }
2997 : 37434 : fs->ofpacts = ofpacts->data;
2998 : 37434 : fs->ofpacts_len = ofpacts->size;
2999 : :
3000 : 38237 : return 0;
3001 : : }
3002 : :
3003 : : /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3004 : : *
3005 : : * We use this in situations where OVS internally uses UINT64_MAX to mean
3006 : : * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3007 : : static uint64_t
3008 : 4384 : unknown_to_zero(uint64_t count)
3009 : : {
3010 [ + - ]: 4384 : return count != UINT64_MAX ? count : 0;
3011 : : }
3012 : :
3013 : : /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3014 : : * those already present in the list of ofpbufs in 'replies'. 'replies' should
3015 : : * have been initialized with ofpmp_init(). */
3016 : : void
3017 : 18639 : ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
3018 : : struct ovs_list *replies)
3019 : : {
3020 : 18639 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
3021 : 18639 : size_t start_ofs = reply->size;
3022 : 18639 : enum ofp_version version = ofpmp_version(replies);
3023 : 18639 : enum ofpraw raw = ofpmp_decode_raw(replies);
3024 : :
3025 [ + + ][ + + ]: 20804 : if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
3026 : : struct ofp11_flow_stats *ofs;
3027 : :
3028 : 2165 : ofpbuf_put_uninit(reply, sizeof *ofs);
3029 : 2165 : oxm_put_match(reply, &fs->match, version);
3030 : 2165 : ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
3031 : : version);
3032 : :
3033 : 2165 : ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3034 : 2165 : ofs->length = htons(reply->size - start_ofs);
3035 : 2165 : ofs->table_id = fs->table_id;
3036 : 2165 : ofs->pad = 0;
3037 : 2165 : ofs->duration_sec = htonl(fs->duration_sec);
3038 : 2165 : ofs->duration_nsec = htonl(fs->duration_nsec);
3039 : 2165 : ofs->priority = htons(fs->priority);
3040 : 2165 : ofs->idle_timeout = htons(fs->idle_timeout);
3041 : 2165 : ofs->hard_timeout = htons(fs->hard_timeout);
3042 [ + + ]: 2165 : if (version >= OFP14_VERSION) {
3043 : 101 : ofs->importance = htons(fs->importance);
3044 : : } else {
3045 : 2064 : ofs->importance = 0;
3046 : : }
3047 [ + + ]: 2165 : if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3048 : 2030 : ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3049 : : } else {
3050 : 135 : ofs->flags = 0;
3051 : : }
3052 : 2165 : memset(ofs->pad2, 0, sizeof ofs->pad2);
3053 : 2165 : ofs->cookie = fs->cookie;
3054 : 2165 : ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3055 : 2165 : ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3056 [ + + ]: 16474 : } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3057 : : struct ofp10_flow_stats *ofs;
3058 : :
3059 : 22 : ofpbuf_put_uninit(reply, sizeof *ofs);
3060 : 22 : ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3061 : : version);
3062 : 22 : ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3063 : 22 : ofs->length = htons(reply->size - start_ofs);
3064 : 22 : ofs->table_id = fs->table_id;
3065 : 22 : ofs->pad = 0;
3066 : 22 : ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3067 : 22 : ofs->duration_sec = htonl(fs->duration_sec);
3068 : 22 : ofs->duration_nsec = htonl(fs->duration_nsec);
3069 : 22 : ofs->priority = htons(fs->priority);
3070 : 22 : ofs->idle_timeout = htons(fs->idle_timeout);
3071 : 22 : ofs->hard_timeout = htons(fs->hard_timeout);
3072 : 22 : memset(ofs->pad2, 0, sizeof ofs->pad2);
3073 : 22 : put_32aligned_be64(&ofs->cookie, fs->cookie);
3074 : 22 : put_32aligned_be64(&ofs->packet_count,
3075 : : htonll(unknown_to_zero(fs->packet_count)));
3076 : 22 : put_32aligned_be64(&ofs->byte_count,
3077 : : htonll(unknown_to_zero(fs->byte_count)));
3078 [ + - ]: 16452 : } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3079 : : struct nx_flow_stats *nfs;
3080 : : int match_len;
3081 : :
3082 : 16452 : ofpbuf_put_uninit(reply, sizeof *nfs);
3083 : 16452 : match_len = nx_put_match(reply, &fs->match, 0, 0);
3084 : 16452 : ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3085 : : version);
3086 : 16452 : nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3087 : 16452 : nfs->length = htons(reply->size - start_ofs);
3088 : 16452 : nfs->table_id = fs->table_id;
3089 : 16452 : nfs->pad = 0;
3090 : 16452 : nfs->duration_sec = htonl(fs->duration_sec);
3091 : 16452 : nfs->duration_nsec = htonl(fs->duration_nsec);
3092 : 16452 : nfs->priority = htons(fs->priority);
3093 : 16452 : nfs->idle_timeout = htons(fs->idle_timeout);
3094 : 16452 : nfs->hard_timeout = htons(fs->hard_timeout);
3095 [ + - ][ + - ]: 16452 : nfs->idle_age = htons(fs->idle_age < 0 ? 0
3096 : 32904 : : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3097 : : : UINT16_MAX);
3098 [ + - ][ + - ]: 16452 : nfs->hard_age = htons(fs->hard_age < 0 ? 0
3099 : 32904 : : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3100 : : : UINT16_MAX);
3101 : 16452 : nfs->match_len = htons(match_len);
3102 : 16452 : nfs->cookie = fs->cookie;
3103 : 16452 : nfs->packet_count = htonll(fs->packet_count);
3104 : 16452 : nfs->byte_count = htonll(fs->byte_count);
3105 : : } else {
3106 : 0 : OVS_NOT_REACHED();
3107 : : }
3108 : :
3109 : 18639 : ofpmp_postappend(replies, start_ofs);
3110 : 18639 : }
3111 : :
3112 : : /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3113 : : * NXST_AGGREGATE reply matching 'request', and returns the message. */
3114 : : struct ofpbuf *
3115 : 5 : ofputil_encode_aggregate_stats_reply(
3116 : : const struct ofputil_aggregate_stats *stats,
3117 : : const struct ofp_header *request)
3118 : : {
3119 : : struct ofp_aggregate_stats_reply *asr;
3120 : : uint64_t packet_count;
3121 : : uint64_t byte_count;
3122 : : struct ofpbuf *msg;
3123 : : enum ofpraw raw;
3124 : :
3125 : 5 : ofpraw_decode(&raw, request);
3126 [ + + ]: 5 : if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3127 : 1 : packet_count = unknown_to_zero(stats->packet_count);
3128 : 1 : byte_count = unknown_to_zero(stats->byte_count);
3129 : : } else {
3130 : 4 : packet_count = stats->packet_count;
3131 : 4 : byte_count = stats->byte_count;
3132 : : }
3133 : :
3134 : 5 : msg = ofpraw_alloc_stats_reply(request, 0);
3135 : 5 : asr = ofpbuf_put_zeros(msg, sizeof *asr);
3136 : 5 : put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3137 : 5 : put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3138 : 5 : asr->flow_count = htonl(stats->flow_count);
3139 : :
3140 : 5 : return msg;
3141 : : }
3142 : :
3143 : : enum ofperr
3144 : 14 : ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3145 : : const struct ofp_header *reply)
3146 : : {
3147 : 14 : struct ofpbuf msg = ofpbuf_const_initializer(reply, ntohs(reply->length));
3148 : 14 : ofpraw_pull_assert(&msg);
3149 : :
3150 : 14 : struct ofp_aggregate_stats_reply *asr = msg.msg;
3151 : 14 : stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3152 : 14 : stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3153 : 14 : stats->flow_count = ntohl(asr->flow_count);
3154 : :
3155 : 14 : return 0;
3156 : : }
3157 : :
3158 : : /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3159 : : * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3160 : : * an OpenFlow error code. */
3161 : : enum ofperr
3162 : 40 : ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3163 : : const struct ofp_header *oh)
3164 : : {
3165 : 40 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3166 : 40 : enum ofpraw raw = ofpraw_pull_assert(&b);
3167 [ + + ]: 40 : if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3168 : : const struct ofp12_flow_removed *ofr;
3169 : : enum ofperr error;
3170 : :
3171 : 30 : ofr = ofpbuf_pull(&b, sizeof *ofr);
3172 : :
3173 : 30 : error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3174 [ - + ]: 30 : if (error) {
3175 : 0 : return error;
3176 : : }
3177 : :
3178 : 30 : fr->priority = ntohs(ofr->priority);
3179 : 30 : fr->cookie = ofr->cookie;
3180 : 30 : fr->reason = ofr->reason;
3181 : 30 : fr->table_id = ofr->table_id;
3182 : 30 : fr->duration_sec = ntohl(ofr->duration_sec);
3183 : 30 : fr->duration_nsec = ntohl(ofr->duration_nsec);
3184 : 30 : fr->idle_timeout = ntohs(ofr->idle_timeout);
3185 : 30 : fr->hard_timeout = ntohs(ofr->hard_timeout);
3186 : 30 : fr->packet_count = ntohll(ofr->packet_count);
3187 : 30 : fr->byte_count = ntohll(ofr->byte_count);
3188 [ + + ]: 10 : } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3189 : : const struct ofp10_flow_removed *ofr;
3190 : :
3191 : 9 : ofr = ofpbuf_pull(&b, sizeof *ofr);
3192 : :
3193 : 9 : ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3194 : 9 : fr->priority = ntohs(ofr->priority);
3195 : 9 : fr->cookie = ofr->cookie;
3196 : 9 : fr->reason = ofr->reason;
3197 : 9 : fr->table_id = 255;
3198 : 9 : fr->duration_sec = ntohl(ofr->duration_sec);
3199 : 9 : fr->duration_nsec = ntohl(ofr->duration_nsec);
3200 : 9 : fr->idle_timeout = ntohs(ofr->idle_timeout);
3201 : 9 : fr->hard_timeout = 0;
3202 : 9 : fr->packet_count = ntohll(ofr->packet_count);
3203 : 9 : fr->byte_count = ntohll(ofr->byte_count);
3204 [ + - ]: 1 : } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3205 : : struct nx_flow_removed *nfr;
3206 : : enum ofperr error;
3207 : :
3208 : 1 : nfr = ofpbuf_pull(&b, sizeof *nfr);
3209 : 1 : error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3210 : : NULL, NULL);
3211 [ - + ]: 1 : if (error) {
3212 : 0 : return error;
3213 : : }
3214 [ - + ]: 1 : if (b.size) {
3215 : 0 : return OFPERR_OFPBRC_BAD_LEN;
3216 : : }
3217 : :
3218 : 1 : fr->priority = ntohs(nfr->priority);
3219 : 1 : fr->cookie = nfr->cookie;
3220 : 1 : fr->reason = nfr->reason;
3221 [ + - ]: 1 : fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3222 : 1 : fr->duration_sec = ntohl(nfr->duration_sec);
3223 : 1 : fr->duration_nsec = ntohl(nfr->duration_nsec);
3224 : 1 : fr->idle_timeout = ntohs(nfr->idle_timeout);
3225 : 1 : fr->hard_timeout = 0;
3226 : 1 : fr->packet_count = ntohll(nfr->packet_count);
3227 : 1 : fr->byte_count = ntohll(nfr->byte_count);
3228 : : } else {
3229 : 0 : OVS_NOT_REACHED();
3230 : : }
3231 : :
3232 : 40 : return 0;
3233 : : }
3234 : :
3235 : : /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3236 : : * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3237 : : * message. */
3238 : : struct ofpbuf *
3239 : 18 : ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3240 : : enum ofputil_protocol protocol)
3241 : : {
3242 : : struct ofpbuf *msg;
3243 : 18 : enum ofp_flow_removed_reason reason = fr->reason;
3244 : :
3245 [ - + ][ # # ]: 18 : if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3246 : 0 : reason = OFPRR_DELETE;
3247 : : }
3248 : :
3249 [ + + - - ]: 18 : switch (protocol) {
3250 : : case OFPUTIL_P_OF11_STD:
3251 : : case OFPUTIL_P_OF12_OXM:
3252 : : case OFPUTIL_P_OF13_OXM:
3253 : : case OFPUTIL_P_OF14_OXM:
3254 : : case OFPUTIL_P_OF15_OXM:
3255 : : case OFPUTIL_P_OF16_OXM: {
3256 : : struct ofp12_flow_removed *ofr;
3257 : :
3258 : 28 : msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3259 : 14 : ofputil_protocol_to_ofp_version(protocol),
3260 : : htonl(0),
3261 : 14 : ofputil_match_typical_len(protocol));
3262 : 14 : ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3263 : 14 : ofr->cookie = fr->cookie;
3264 : 14 : ofr->priority = htons(fr->priority);
3265 : 14 : ofr->reason = reason;
3266 : 14 : ofr->table_id = fr->table_id;
3267 : 14 : ofr->duration_sec = htonl(fr->duration_sec);
3268 : 14 : ofr->duration_nsec = htonl(fr->duration_nsec);
3269 : 14 : ofr->idle_timeout = htons(fr->idle_timeout);
3270 : 14 : ofr->hard_timeout = htons(fr->hard_timeout);
3271 : 14 : ofr->packet_count = htonll(fr->packet_count);
3272 : 14 : ofr->byte_count = htonll(fr->byte_count);
3273 : 14 : ofputil_put_ofp11_match(msg, &fr->match, protocol);
3274 : 14 : break;
3275 : : }
3276 : :
3277 : : case OFPUTIL_P_OF10_STD:
3278 : : case OFPUTIL_P_OF10_STD_TID: {
3279 : : struct ofp10_flow_removed *ofr;
3280 : :
3281 : 4 : msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3282 : : htonl(0), 0);
3283 : 4 : ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3284 : 4 : ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3285 : 4 : ofr->cookie = fr->cookie;
3286 : 4 : ofr->priority = htons(fr->priority);
3287 : 4 : ofr->reason = reason;
3288 : 4 : ofr->duration_sec = htonl(fr->duration_sec);
3289 : 4 : ofr->duration_nsec = htonl(fr->duration_nsec);
3290 : 4 : ofr->idle_timeout = htons(fr->idle_timeout);
3291 : 4 : ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3292 : 4 : ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3293 : 4 : break;
3294 : : }
3295 : :
3296 : : case OFPUTIL_P_OF10_NXM:
3297 : : case OFPUTIL_P_OF10_NXM_TID: {
3298 : : struct nx_flow_removed *nfr;
3299 : : int match_len;
3300 : :
3301 : 0 : msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3302 : : htonl(0), NXM_TYPICAL_LEN);
3303 : 0 : ofpbuf_put_zeros(msg, sizeof *nfr);
3304 : 0 : match_len = nx_put_match(msg, &fr->match, 0, 0);
3305 : :
3306 : 0 : nfr = msg->msg;
3307 : 0 : nfr->cookie = fr->cookie;
3308 : 0 : nfr->priority = htons(fr->priority);
3309 : 0 : nfr->reason = reason;
3310 : 0 : nfr->table_id = fr->table_id + 1;
3311 : 0 : nfr->duration_sec = htonl(fr->duration_sec);
3312 : 0 : nfr->duration_nsec = htonl(fr->duration_nsec);
3313 : 0 : nfr->idle_timeout = htons(fr->idle_timeout);
3314 : 0 : nfr->match_len = htons(match_len);
3315 : 0 : nfr->packet_count = htonll(fr->packet_count);
3316 : 0 : nfr->byte_count = htonll(fr->byte_count);
3317 : 0 : break;
3318 : : }
3319 : :
3320 : : default:
3321 : 0 : OVS_NOT_REACHED();
3322 : : }
3323 : :
3324 : 18 : return msg;
3325 : : }
3326 : :
3327 : : /* The caller has done basic initialization of '*pin'; the other output
3328 : : * arguments needs to be initialized. */
3329 : : static enum ofperr
3330 : 2124 : decode_nx_packet_in2(const struct ofp_header *oh, bool loose,
3331 : : struct ofputil_packet_in *pin,
3332 : : size_t *total_len, uint32_t *buffer_id,
3333 : : struct ofpbuf *continuation)
3334 : : {
3335 : 2124 : *total_len = 0;
3336 : 2124 : *buffer_id = UINT32_MAX;
3337 : :
3338 : : struct ofpbuf properties;
3339 : 2124 : ofpbuf_use_const(&properties, oh, ntohs(oh->length));
3340 : 2124 : ofpraw_pull_assert(&properties);
3341 : :
3342 [ + + ]: 14845 : while (properties.size > 0) {
3343 : : struct ofpbuf payload;
3344 : : uint64_t type;
3345 : :
3346 : 12721 : enum ofperr error = ofpprop_pull(&properties, &payload, &type);
3347 [ - + ]: 12721 : if (error) {
3348 : 0 : return error;
3349 : : }
3350 : :
3351 [ + + + + : 12721 : switch (type) {
+ + + + +
- ]
3352 : : case NXPINT_PACKET:
3353 : 2124 : pin->packet = payload.msg;
3354 : 2124 : pin->packet_len = ofpbuf_msgsize(&payload);
3355 : 2124 : break;
3356 : :
3357 : : case NXPINT_FULL_LEN: {
3358 : : uint32_t u32;
3359 : 1 : error = ofpprop_parse_u32(&payload, &u32);
3360 : 1 : *total_len = u32;
3361 : 1 : break;
3362 : : }
3363 : :
3364 : : case NXPINT_BUFFER_ID:
3365 : 1 : error = ofpprop_parse_u32(&payload, buffer_id);
3366 : 1 : break;
3367 : :
3368 : : case NXPINT_TABLE_ID:
3369 : 2123 : error = ofpprop_parse_u8(&payload, &pin->table_id);
3370 : 2123 : break;
3371 : :
3372 : : case NXPINT_COOKIE:
3373 : 2117 : error = ofpprop_parse_be64(&payload, &pin->cookie);
3374 : 2117 : break;
3375 : :
3376 : : case NXPINT_REASON: {
3377 : : uint8_t reason;
3378 : 2123 : error = ofpprop_parse_u8(&payload, &reason);
3379 : 2123 : pin->reason = reason;
3380 : 2123 : break;
3381 : : }
3382 : :
3383 : : case NXPINT_METADATA:
3384 : 2124 : error = oxm_decode_match(payload.msg, ofpbuf_msgsize(&payload),
3385 : : &pin->flow_metadata);
3386 : 2124 : break;
3387 : :
3388 : : case NXPINT_USERDATA:
3389 : 924 : pin->userdata = payload.msg;
3390 : 924 : pin->userdata_len = ofpbuf_msgsize(&payload);
3391 : 924 : break;
3392 : :
3393 : : case NXPINT_CONTINUATION:
3394 [ + - ]: 1184 : if (continuation) {
3395 : 1184 : error = ofpprop_parse_nested(&payload, continuation);
3396 : : }
3397 : 1184 : break;
3398 : :
3399 : : default:
3400 : 0 : error = OFPPROP_UNKNOWN(loose, "NX_PACKET_IN2", type);
3401 : 0 : break;
3402 : : }
3403 [ - + ]: 12721 : if (error) {
3404 : 12721 : return error;
3405 : : }
3406 : : }
3407 : :
3408 [ - + ]: 2124 : if (!pin->packet_len) {
3409 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 lacks packet");
3410 : 0 : return OFPERR_OFPBRC_BAD_LEN;
3411 [ + + ]: 2124 : } else if (!*total_len) {
3412 : 2123 : *total_len = pin->packet_len;
3413 [ - + ]: 1 : } else if (*total_len < pin->packet_len) {
3414 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 claimed full_len < len");
3415 : 0 : return OFPERR_OFPBRC_BAD_LEN;
3416 : : }
3417 : :
3418 : 2124 : return 0;
3419 : : }
3420 : :
3421 : : /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3422 : : * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3423 : : * included in the message. If 'total_lenp' is nonnull, populates
3424 : : * '*total_lenp' with the original length of the packet (which is larger than
3425 : : * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3426 : : * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
3427 : : * was not buffered).
3428 : : *
3429 : : * Populates 'continuation', if nonnull, with the continuation data from the
3430 : : * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3431 : : * format of this data is supposed to be opaque to anything other than
3432 : : * ovs-vswitchd, so that in any other process the only reasonable use of this
3433 : : * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3434 : : *
3435 : : * This function points 'pin->packet' into 'oh', so the caller should not free
3436 : : * it separately from the original OpenFlow message. This is also true for
3437 : : * 'pin->userdata' (which could also end up NULL if there is no userdata).
3438 : : *
3439 : : * Returns 0 if successful, otherwise an OpenFlow error code. */
3440 : : enum ofperr
3441 : 2760 : ofputil_decode_packet_in(const struct ofp_header *oh, bool loose,
3442 : : struct ofputil_packet_in *pin,
3443 : : size_t *total_lenp, uint32_t *buffer_idp,
3444 : : struct ofpbuf *continuation)
3445 : : {
3446 : : uint32_t buffer_id;
3447 : : size_t total_len;
3448 : :
3449 : 2760 : memset(pin, 0, sizeof *pin);
3450 : 2760 : pin->cookie = OVS_BE64_MAX;
3451 [ + - ]: 2760 : if (continuation) {
3452 : 2760 : ofpbuf_use_const(continuation, NULL, 0);
3453 : : }
3454 : :
3455 : 2760 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3456 : 2760 : enum ofpraw raw = ofpraw_pull_assert(&b);
3457 [ + + ][ + + ]: 2948 : if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3458 : 188 : const struct ofp12_packet_in *opi = ofpbuf_pull(&b, sizeof *opi);
3459 : 188 : const ovs_be64 *cookie = (raw == OFPRAW_OFPT13_PACKET_IN
3460 : : ? ofpbuf_pull(&b, sizeof *cookie)
3461 [ + + ]: 188 : : NULL);
3462 : 188 : enum ofperr error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3463 [ - + ]: 188 : if (error) {
3464 : 0 : return error;
3465 : : }
3466 : :
3467 [ - + ]: 188 : if (!ofpbuf_try_pull(&b, 2)) {
3468 : 0 : return OFPERR_OFPBRC_BAD_LEN;
3469 : : }
3470 : :
3471 : 188 : pin->reason = opi->reason;
3472 : 188 : pin->table_id = opi->table_id;
3473 : 188 : buffer_id = ntohl(opi->buffer_id);
3474 : 188 : total_len = ntohs(opi->total_len);
3475 [ + + ]: 188 : if (cookie) {
3476 : 76 : pin->cookie = *cookie;
3477 : : }
3478 : :
3479 : 188 : pin->packet = b.data;
3480 : 188 : pin->packet_len = b.size;
3481 [ + + ]: 2572 : } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3482 : : const struct ofp10_packet_in *opi;
3483 : :
3484 : 48 : opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3485 : :
3486 : 48 : pin->packet = CONST_CAST(uint8_t *, opi->data);
3487 : 48 : pin->packet_len = b.size;
3488 : :
3489 : 48 : match_init_catchall(&pin->flow_metadata);
3490 : 48 : match_set_in_port(&pin->flow_metadata,
3491 : 48 : u16_to_ofp(ntohs(opi->in_port)));
3492 : 48 : pin->reason = opi->reason;
3493 : 48 : buffer_id = ntohl(opi->buffer_id);
3494 : 48 : total_len = ntohs(opi->total_len);
3495 [ + + ]: 2524 : } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3496 : : const struct ofp11_packet_in *opi;
3497 : : ofp_port_t in_port;
3498 : : enum ofperr error;
3499 : :
3500 : 5 : opi = ofpbuf_pull(&b, sizeof *opi);
3501 : :
3502 : 5 : pin->packet = b.data;
3503 : 5 : pin->packet_len = b.size;
3504 : :
3505 : 5 : buffer_id = ntohl(opi->buffer_id);
3506 : 5 : error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3507 [ - + ]: 5 : if (error) {
3508 : 0 : return error;
3509 : : }
3510 : 5 : match_init_catchall(&pin->flow_metadata);
3511 : 5 : match_set_in_port(&pin->flow_metadata, in_port);
3512 : 5 : total_len = ntohs(opi->total_len);
3513 : 5 : pin->reason = opi->reason;
3514 : 5 : pin->table_id = opi->table_id;
3515 [ + + ]: 2519 : } else if (raw == OFPRAW_NXT_PACKET_IN) {
3516 : : const struct nx_packet_in *npi;
3517 : : int error;
3518 : :
3519 : 395 : npi = ofpbuf_pull(&b, sizeof *npi);
3520 : 395 : error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3521 : : &pin->flow_metadata, NULL, NULL);
3522 [ - + ]: 395 : if (error) {
3523 : 0 : return error;
3524 : : }
3525 : :
3526 [ - + ]: 395 : if (!ofpbuf_try_pull(&b, 2)) {
3527 : 0 : return OFPERR_OFPBRC_BAD_LEN;
3528 : : }
3529 : :
3530 : 395 : pin->reason = npi->reason;
3531 : 395 : pin->table_id = npi->table_id;
3532 : 395 : pin->cookie = npi->cookie;
3533 : :
3534 : 395 : buffer_id = ntohl(npi->buffer_id);
3535 : 395 : total_len = ntohs(npi->total_len);
3536 : :
3537 : 395 : pin->packet = b.data;
3538 : 395 : pin->packet_len = b.size;
3539 [ + + ][ + - ]: 4248 : } else if (raw == OFPRAW_NXT_PACKET_IN2 || raw == OFPRAW_NXT_RESUME) {
3540 : 2124 : enum ofperr error = decode_nx_packet_in2(oh, loose, pin, &total_len,
3541 : : &buffer_id, continuation);
3542 [ - + ]: 2124 : if (error) {
3543 : 0 : return error;
3544 : : }
3545 : : } else {
3546 : 0 : OVS_NOT_REACHED();
3547 : : }
3548 : :
3549 [ + + ]: 2760 : if (total_lenp) {
3550 : 1914 : *total_lenp = total_len;
3551 : : }
3552 [ + + ]: 2760 : if (buffer_idp) {
3553 : 1914 : *buffer_idp = buffer_id;
3554 : : }
3555 : :
3556 : 2760 : return 0;
3557 : : }
3558 : :
3559 : : static int
3560 : 1190 : encode_packet_in_reason(enum ofp_packet_in_reason reason,
3561 : : enum ofp_version version)
3562 : : {
3563 [ + + + + : 1190 : switch (reason) {
- ]
3564 : : case OFPR_NO_MATCH:
3565 : : case OFPR_ACTION:
3566 : : case OFPR_INVALID_TTL:
3567 : 1144 : return reason;
3568 : :
3569 : : case OFPR_ACTION_SET:
3570 : : case OFPR_GROUP:
3571 : : case OFPR_PACKET_OUT:
3572 [ + + ]: 15 : return version < OFP14_VERSION ? OFPR_ACTION : reason;
3573 : :
3574 : : case OFPR_EXPLICIT_MISS:
3575 : 12 : return version < OFP13_VERSION ? OFPR_ACTION : OFPR_NO_MATCH;
3576 : :
3577 : : case OFPR_IMPLICIT_MISS:
3578 : 19 : return OFPR_NO_MATCH;
3579 : :
3580 : : case OFPR_N_REASONS:
3581 : : default:
3582 : 0 : OVS_NOT_REACHED();
3583 : : }
3584 : : }
3585 : :
3586 : : /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3587 : : * function omits it. The caller can add it itself if desired. */
3588 : : static void
3589 : 867 : ofputil_put_packet_in(const struct ofputil_packet_in *pin,
3590 : : enum ofp_version version, size_t include_bytes,
3591 : : struct ofpbuf *msg)
3592 : : {
3593 : : /* Add packet properties. */
3594 : 867 : ofpprop_put(msg, NXPINT_PACKET, pin->packet, include_bytes);
3595 [ - + ]: 867 : if (include_bytes != pin->packet_len) {
3596 : 0 : ofpprop_put_u32(msg, NXPINT_FULL_LEN, pin->packet_len);
3597 : : }
3598 : :
3599 : : /* Add flow properties. */
3600 : 867 : ofpprop_put_u8(msg, NXPINT_TABLE_ID, pin->table_id);
3601 [ + + ]: 867 : if (pin->cookie != OVS_BE64_MAX) {
3602 : 864 : ofpprop_put_be64(msg, NXPINT_COOKIE, pin->cookie);
3603 : : }
3604 : :
3605 : : /* Add other properties. */
3606 : 867 : ofpprop_put_u8(msg, NXPINT_REASON,
3607 : 867 : encode_packet_in_reason(pin->reason, version));
3608 : :
3609 : 867 : size_t start = ofpprop_start(msg, NXPINT_METADATA);
3610 : 867 : oxm_put_raw(msg, &pin->flow_metadata, version);
3611 : 867 : ofpprop_end(msg, start);
3612 : 867 : }
3613 : :
3614 : : static void
3615 : 644 : put_actions_property(struct ofpbuf *msg, uint64_t prop_type,
3616 : : enum ofp_version version,
3617 : : const struct ofpact *actions, size_t actions_len)
3618 : : {
3619 [ + + ]: 644 : if (actions_len) {
3620 : 451 : size_t start = ofpprop_start_nested(msg, prop_type);
3621 : 451 : ofpacts_put_openflow_actions(actions, actions_len, msg, version);
3622 : 451 : ofpprop_end(msg, start);
3623 : : }
3624 : 644 : }
3625 : :
3626 : : enum nx_continuation_prop_type {
3627 : : NXCPT_BRIDGE = 0x8000,
3628 : : NXCPT_STACK,
3629 : : NXCPT_MIRRORS,
3630 : : NXCPT_CONNTRACKED,
3631 : : NXCPT_TABLE_ID,
3632 : : NXCPT_COOKIE,
3633 : : NXCPT_ACTIONS,
3634 : : NXCPT_ACTION_SET,
3635 : : };
3636 : :
3637 : : /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3638 : : * function omits it. The caller can add it itself if desired. */
3639 : : static void
3640 : 668 : ofputil_put_packet_in_private(const struct ofputil_packet_in_private *pin,
3641 : : enum ofp_version version, size_t include_bytes,
3642 : : struct ofpbuf *msg)
3643 : : {
3644 : 668 : ofputil_put_packet_in(&pin->public, version, include_bytes, msg);
3645 : :
3646 : 668 : size_t continuation_ofs = ofpprop_start_nested(msg, NXPINT_CONTINUATION);
3647 : 668 : size_t inner_ofs = msg->size;
3648 : :
3649 [ + + ]: 668 : if (!uuid_is_zero(&pin->bridge)) {
3650 : 199 : ofpprop_put_uuid(msg, NXCPT_BRIDGE, &pin->bridge);
3651 : : }
3652 : :
3653 [ - + ]: 668 : for (size_t i = 0; i < pin->n_stack; i++) {
3654 : 0 : const union mf_subvalue *s = &pin->stack[i];
3655 : : size_t ofs;
3656 [ # # ]: 0 : for (ofs = 0; ofs < sizeof *s; ofs++) {
3657 [ # # ]: 0 : if (s->u8[ofs]) {
3658 : 0 : break;
3659 : : }
3660 : : }
3661 : :
3662 : 0 : ofpprop_put(msg, NXCPT_STACK, &s->u8[ofs], sizeof *s - ofs);
3663 : : }
3664 : :
3665 [ + + ]: 668 : if (pin->mirrors) {
3666 : 9 : ofpprop_put_u32(msg, NXCPT_MIRRORS, pin->mirrors);
3667 : : }
3668 : :
3669 [ - + ]: 668 : if (pin->conntracked) {
3670 : 0 : ofpprop_put_flag(msg, NXCPT_CONNTRACKED);
3671 : : }
3672 : :
3673 [ + + ]: 668 : if (pin->actions_len) {
3674 : : /* Divide 'pin->actions' into groups that begins with an
3675 : : * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3676 : : * NXCPT_COOKIE property (if either has changed; each is initially
3677 : : * assumed 0), then a NXCPT_ACTIONS property with the grouped
3678 : : * actions.
3679 : : *
3680 : : * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3681 : : * always do that later, since this is a private property. */
3682 : 185 : const struct ofpact *const end = ofpact_end(pin->actions,
3683 : : pin->actions_len);
3684 : 185 : const struct ofpact_unroll_xlate *unroll = NULL;
3685 : 185 : uint8_t table_id = 0;
3686 : 185 : ovs_be64 cookie = 0;
3687 : :
3688 : : const struct ofpact *a;
3689 : 1745 : for (a = pin->actions; ; a = ofpact_next(a)) {
3690 [ + + ][ + + ]: 1745 : if (a == end || a->type == OFPACT_UNROLL_XLATE) {
3691 [ + + ]: 644 : if (unroll) {
3692 [ + + ]: 459 : if (table_id != unroll->rule_table_id) {
3693 : 355 : ofpprop_put_u8(msg, NXCPT_TABLE_ID,
3694 : 355 : unroll->rule_table_id);
3695 : 355 : table_id = unroll->rule_table_id;
3696 : : }
3697 [ - + ]: 459 : if (cookie != unroll->rule_cookie) {
3698 : 0 : ofpprop_put_be64(msg, NXCPT_COOKIE,
3699 : : unroll->rule_cookie);
3700 : 0 : cookie = unroll->rule_cookie;
3701 : : }
3702 : : }
3703 : :
3704 : 644 : const struct ofpact *start
3705 [ + + ]: 644 : = unroll ? ofpact_next(&unroll->ofpact) : pin->actions;
3706 : 644 : put_actions_property(msg, NXCPT_ACTIONS, version,
3707 : 644 : start, (a - start) * sizeof *a);
3708 : :
3709 [ + + ]: 644 : if (a == end) {
3710 : 185 : break;
3711 : : }
3712 : 459 : unroll = ofpact_get_UNROLL_XLATE(a);
3713 : : }
3714 : 1560 : }
3715 : : }
3716 : :
3717 [ + + ]: 668 : if (pin->action_set_len) {
3718 : 2 : size_t start = ofpprop_start_nested(msg, NXCPT_ACTION_SET);
3719 : 2 : ofpacts_put_openflow_actions(pin->action_set,
3720 : : pin->action_set_len, msg, version);
3721 : 2 : ofpprop_end(msg, start);
3722 : : }
3723 : :
3724 [ + + ]: 668 : if (msg->size > inner_ofs) {
3725 : 199 : ofpprop_end(msg, continuation_ofs);
3726 : : } else {
3727 : 469 : msg->size = continuation_ofs;
3728 : : }
3729 : 668 : }
3730 : :
3731 : : static struct ofpbuf *
3732 : 23 : ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3733 : : {
3734 : : struct ofp10_packet_in *opi;
3735 : : struct ofpbuf *msg;
3736 : :
3737 : 23 : msg = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3738 : : htonl(0), pin->packet_len);
3739 : 23 : opi = ofpbuf_put_zeros(msg, offsetof(struct ofp10_packet_in, data));
3740 : 23 : opi->total_len = htons(pin->packet_len);
3741 : 23 : opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3742 : 23 : opi->reason = encode_packet_in_reason(pin->reason, OFP10_VERSION);
3743 : 23 : opi->buffer_id = htonl(UINT32_MAX);
3744 : :
3745 : 23 : return msg;
3746 : : }
3747 : :
3748 : : static struct ofpbuf *
3749 : 207 : ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin,
3750 : : enum ofp_version version)
3751 : : {
3752 : : struct nx_packet_in *npi;
3753 : : struct ofpbuf *msg;
3754 : : size_t match_len;
3755 : :
3756 : : /* The final argument is just an estimate of the space required. */
3757 : 207 : msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, version,
3758 : 207 : htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3759 : 207 : ofpbuf_put_zeros(msg, sizeof *npi);
3760 : 207 : match_len = nx_put_match(msg, &pin->flow_metadata, 0, 0);
3761 : 207 : ofpbuf_put_zeros(msg, 2);
3762 : :
3763 : 207 : npi = msg->msg;
3764 : 207 : npi->buffer_id = htonl(UINT32_MAX);
3765 : 207 : npi->total_len = htons(pin->packet_len);
3766 : 207 : npi->reason = encode_packet_in_reason(pin->reason, version);
3767 : 207 : npi->table_id = pin->table_id;
3768 : 207 : npi->cookie = pin->cookie;
3769 : 207 : npi->match_len = htons(match_len);
3770 : :
3771 : 207 : return msg;
3772 : : }
3773 : :
3774 : : static struct ofpbuf *
3775 : 668 : ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private *pin,
3776 : : enum ofp_version version, size_t include_bytes)
3777 : : {
3778 : : /* 'extra' is just an estimate of the space required. */
3779 : 1336 : size_t extra = (pin->public.packet_len
3780 : : + NXM_TYPICAL_LEN /* flow_metadata */
3781 : 668 : + pin->n_stack * 16
3782 : 668 : + pin->actions_len
3783 : 668 : + pin->action_set_len
3784 : : + 256); /* fudge factor */
3785 : 668 : struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2, version,
3786 : : htonl(0), extra);
3787 : :
3788 : 668 : ofputil_put_packet_in_private(pin, version, include_bytes, msg);
3789 [ + + ]: 668 : if (pin->public.userdata_len) {
3790 : 459 : ofpprop_put(msg, NXPINT_USERDATA, pin->public.userdata,
3791 : : pin->public.userdata_len);
3792 : : }
3793 : :
3794 : 668 : ofpmsg_update_length(msg);
3795 : 668 : return msg;
3796 : : }
3797 : :
3798 : : static struct ofpbuf *
3799 : 2 : ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3800 : : {
3801 : : struct ofp11_packet_in *opi;
3802 : : struct ofpbuf *msg;
3803 : :
3804 : 2 : msg = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3805 : : htonl(0), pin->packet_len);
3806 : 2 : opi = ofpbuf_put_zeros(msg, sizeof *opi);
3807 : 2 : opi->buffer_id = htonl(UINT32_MAX);
3808 : 2 : opi->in_port = ofputil_port_to_ofp11(
3809 : : pin->flow_metadata.flow.in_port.ofp_port);
3810 : 2 : opi->in_phy_port = opi->in_port;
3811 : 2 : opi->total_len = htons(pin->packet_len);
3812 : 2 : opi->reason = encode_packet_in_reason(pin->reason, OFP11_VERSION);
3813 : 2 : opi->table_id = pin->table_id;
3814 : :
3815 : 2 : return msg;
3816 : : }
3817 : :
3818 : : static struct ofpbuf *
3819 : 91 : ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3820 : : enum ofp_version version)
3821 : : {
3822 [ + + ]: 91 : enum ofpraw raw = (version >= OFP13_VERSION
3823 : : ? OFPRAW_OFPT13_PACKET_IN
3824 : : : OFPRAW_OFPT12_PACKET_IN);
3825 : : struct ofpbuf *msg;
3826 : :
3827 : : /* The final argument is just an estimate of the space required. */
3828 : 91 : msg = ofpraw_alloc_xid(raw, version,
3829 : 91 : htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3830 : :
3831 : 91 : struct ofp12_packet_in *opi = ofpbuf_put_zeros(msg, sizeof *opi);
3832 : 91 : opi->buffer_id = htonl(UINT32_MAX);
3833 : 91 : opi->total_len = htons(pin->packet_len);
3834 : 91 : opi->reason = encode_packet_in_reason(pin->reason, version);
3835 : 91 : opi->table_id = pin->table_id;
3836 : :
3837 [ + + ]: 91 : if (version >= OFP13_VERSION) {
3838 : 36 : ovs_be64 cookie = pin->cookie;
3839 : 36 : ofpbuf_put(msg, &cookie, sizeof cookie);
3840 : : }
3841 : :
3842 : 91 : oxm_put_match(msg, &pin->flow_metadata, version);
3843 : 91 : ofpbuf_put_zeros(msg, 2);
3844 : :
3845 : 91 : return msg;
3846 : : }
3847 : :
3848 : : /* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3849 : : * for 'protocol', using the packet-in format specified by 'packet_in_format'.
3850 : : *
3851 : : * This function is really meant only for use by ovs-vswitchd. To any other
3852 : : * code, the "continuation" data, i.e. the data that is in struct
3853 : : * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3854 : : * to be opaque (and it might change from one OVS version to another). Thus,
3855 : : * if any other code wants to encode a packet-in, it should use a non-"private"
3856 : : * version of this function. (Such a version doesn't currently exist because
3857 : : * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3858 : : * write it...) */
3859 : : struct ofpbuf *
3860 : 991 : ofputil_encode_packet_in_private(const struct ofputil_packet_in_private *pin,
3861 : : enum ofputil_protocol protocol,
3862 : : enum nx_packet_in_format packet_in_format)
3863 : : {
3864 : 991 : enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
3865 : :
3866 : : struct ofpbuf *msg;
3867 [ + + + - ]: 991 : switch (packet_in_format) {
3868 : : case NXPIF_STANDARD:
3869 [ + + + - ]: 116 : switch (protocol) {
3870 : : case OFPUTIL_P_OF10_STD:
3871 : : case OFPUTIL_P_OF10_STD_TID:
3872 : : case OFPUTIL_P_OF10_NXM:
3873 : : case OFPUTIL_P_OF10_NXM_TID:
3874 : 23 : msg = ofputil_encode_ofp10_packet_in(&pin->public);
3875 : 23 : break;
3876 : :
3877 : : case OFPUTIL_P_OF11_STD:
3878 : 2 : msg = ofputil_encode_ofp11_packet_in(&pin->public);
3879 : 2 : break;
3880 : :
3881 : : case OFPUTIL_P_OF12_OXM:
3882 : : case OFPUTIL_P_OF13_OXM:
3883 : : case OFPUTIL_P_OF14_OXM:
3884 : : case OFPUTIL_P_OF15_OXM:
3885 : : case OFPUTIL_P_OF16_OXM:
3886 : 91 : msg = ofputil_encode_ofp12_packet_in(&pin->public, version);
3887 : 91 : break;
3888 : :
3889 : : default:
3890 : 0 : OVS_NOT_REACHED();
3891 : : }
3892 : 116 : break;
3893 : :
3894 : : case NXPIF_NXT_PACKET_IN:
3895 : 207 : msg = ofputil_encode_nx_packet_in(&pin->public, version);
3896 : 207 : break;
3897 : :
3898 : : case NXPIF_NXT_PACKET_IN2:
3899 : 668 : return ofputil_encode_nx_packet_in2(pin, version,
3900 : : pin->public.packet_len);
3901 : :
3902 : : default:
3903 : 0 : OVS_NOT_REACHED();
3904 : : }
3905 : :
3906 : 323 : ofpbuf_put(msg, pin->public.packet, pin->public.packet_len);
3907 : 323 : ofpmsg_update_length(msg);
3908 : 323 : return msg;
3909 : : }
3910 : :
3911 : : /* Returns a string form of 'reason'. The return value is either a statically
3912 : : * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3913 : : * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3914 : : const char *
3915 : 2081 : ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3916 : : char *reasonbuf, size_t bufsize)
3917 : : {
3918 [ + + + + : 2081 : switch (reason) {
+ - + - ]
3919 : : case OFPR_NO_MATCH:
3920 : 174 : return "no_match";
3921 : : case OFPR_ACTION:
3922 : 1830 : return "action";
3923 : : case OFPR_INVALID_TTL:
3924 : 36 : return "invalid_ttl";
3925 : : case OFPR_ACTION_SET:
3926 : 6 : return "action_set";
3927 : : case OFPR_GROUP:
3928 : 6 : return "group";
3929 : : case OFPR_PACKET_OUT:
3930 : 0 : return "packet_out";
3931 : : case OFPR_EXPLICIT_MISS:
3932 : : case OFPR_IMPLICIT_MISS:
3933 : 29 : return "";
3934 : :
3935 : : case OFPR_N_REASONS:
3936 : : default:
3937 : 0 : snprintf(reasonbuf, bufsize, "%d", (int) reason);
3938 : 0 : return reasonbuf;
3939 : : }
3940 : : }
3941 : :
3942 : : bool
3943 : 27 : ofputil_packet_in_reason_from_string(const char *s,
3944 : : enum ofp_packet_in_reason *reason)
3945 : : {
3946 : : int i;
3947 : :
3948 [ + - ]: 29 : for (i = 0; i < OFPR_N_REASONS; i++) {
3949 : : char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3950 : : const char *reason_s;
3951 : :
3952 : 29 : reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3953 : : sizeof reasonbuf);
3954 [ + + ]: 29 : if (!strcasecmp(s, reason_s)) {
3955 : 27 : *reason = i;
3956 : 27 : return true;
3957 : : }
3958 : : }
3959 : 0 : return false;
3960 : : }
3961 : :
3962 : : /* Returns a newly allocated NXT_RESUME message for 'pin', with the given
3963 : : * 'continuation', for 'protocol'. This message is suitable for resuming the
3964 : : * pipeline traveral of the packet represented by 'pin', if sent to the switch
3965 : : * from which 'pin' was received. */
3966 : : struct ofpbuf *
3967 : 199 : ofputil_encode_resume(const struct ofputil_packet_in *pin,
3968 : : const struct ofpbuf *continuation,
3969 : : enum ofputil_protocol protocol)
3970 : : {
3971 : 199 : enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
3972 : 199 : size_t extra = pin->packet_len + NXM_TYPICAL_LEN + continuation->size;
3973 : 199 : struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_RESUME, version,
3974 : : 0, extra);
3975 : 199 : ofputil_put_packet_in(pin, version, pin->packet_len, msg);
3976 : 199 : ofpprop_put_nested(msg, NXPINT_CONTINUATION, continuation);
3977 : 199 : ofpmsg_update_length(msg);
3978 : 199 : return msg;
3979 : : }
3980 : :
3981 : : static enum ofperr
3982 : 0 : parse_subvalue_prop(const struct ofpbuf *property, union mf_subvalue *sv)
3983 : : {
3984 : 0 : unsigned int len = ofpbuf_msgsize(property);
3985 [ # # ]: 0 : if (len > sizeof *sv) {
3986 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXCPT_STACK property has bad length %u",
3987 : : len);
3988 : 0 : return OFPERR_OFPBPC_BAD_LEN;
3989 : : }
3990 : 0 : memset(sv, 0, sizeof *sv);
3991 : 0 : memcpy(&sv->u8[sizeof *sv - len], property->msg, len);
3992 : 0 : return 0;
3993 : : }
3994 : :
3995 : : static enum ofperr
3996 : 2255 : parse_actions_property(struct ofpbuf *property, enum ofp_version version,
3997 : : struct ofpbuf *ofpacts)
3998 : : {
3999 [ - + ]: 2255 : if (!ofpbuf_try_pull(property, ROUND_UP(ofpbuf_headersize(property), 8))) {
4000 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "actions property has bad length %"PRIu32,
4001 : : property->size);
4002 : 0 : return OFPERR_OFPBPC_BAD_LEN;
4003 : : }
4004 : :
4005 : 2255 : return ofpacts_pull_openflow_actions(property, property->size,
4006 : : version, ofpacts);
4007 : : }
4008 : :
4009 : : /* This is like ofputil_decode_packet_in(), except that it decodes the
4010 : : * continuation data into 'pin'. The format of this data is supposed to be
4011 : : * opaque to any process other than ovs-vswitchd, so this function should not
4012 : : * be used outside ovs-vswitchd.
4013 : : *
4014 : : * When successful, 'pin' contains some dynamically allocated data. Call
4015 : : * ofputil_packet_in_private_destroy() to free this data. */
4016 : : enum ofperr
4017 : 2113 : ofputil_decode_packet_in_private(const struct ofp_header *oh, bool loose,
4018 : : struct ofputil_packet_in_private *pin,
4019 : : size_t *total_len, uint32_t *buffer_id)
4020 : : {
4021 : 2113 : memset(pin, 0, sizeof *pin);
4022 : :
4023 : : struct ofpbuf continuation;
4024 : : enum ofperr error;
4025 : 2113 : error = ofputil_decode_packet_in(oh, loose, &pin->public, total_len,
4026 : : buffer_id, &continuation);
4027 [ - + ]: 2113 : if (error) {
4028 : 0 : return error;
4029 : : }
4030 : :
4031 : : struct ofpbuf actions, action_set;
4032 : 2113 : ofpbuf_init(&actions, 0);
4033 : 2113 : ofpbuf_init(&action_set, 0);
4034 : :
4035 : 2113 : uint8_t table_id = 0;
4036 : 2113 : ovs_be64 cookie = 0;
4037 : :
4038 : 2113 : size_t allocated_stack = 0;
4039 : :
4040 [ + + ]: 7173 : while (continuation.size > 0) {
4041 : : struct ofpbuf payload;
4042 : : uint64_t type;
4043 : :
4044 : 5060 : error = ofpprop_pull(&continuation, &payload, &type);
4045 [ - + ]: 5060 : ovs_assert(!error);
4046 : :
4047 [ + - + - : 5060 : switch (type) {
+ - + +
- ]
4048 : : case NXCPT_BRIDGE:
4049 : 985 : error = ofpprop_parse_uuid(&payload, &pin->bridge);
4050 : 985 : break;
4051 : :
4052 : : case NXCPT_STACK:
4053 [ # # ]: 0 : if (pin->n_stack >= allocated_stack) {
4054 : 0 : pin->stack = x2nrealloc(pin->stack, &allocated_stack,
4055 : : sizeof *pin->stack);
4056 : : }
4057 : 0 : error = parse_subvalue_prop(&payload,
4058 : 0 : &pin->stack[pin->n_stack++]);
4059 : 0 : break;
4060 : :
4061 : : case NXCPT_MIRRORS:
4062 : 45 : error = ofpprop_parse_u32(&payload, &pin->mirrors);
4063 : 45 : break;
4064 : :
4065 : : case NXCPT_CONNTRACKED:
4066 : 0 : pin->conntracked = true;
4067 : 0 : break;
4068 : :
4069 : : case NXCPT_TABLE_ID:
4070 : 1775 : error = ofpprop_parse_u8(&payload, &table_id);
4071 : 1775 : break;
4072 : :
4073 : : case NXCPT_COOKIE:
4074 : 0 : error = ofpprop_parse_be64(&payload, &cookie);
4075 : 0 : break;
4076 : :
4077 : : case NXCPT_ACTIONS: {
4078 : 2245 : struct ofpact_unroll_xlate *unroll
4079 : : = ofpact_put_UNROLL_XLATE(&actions);
4080 : 2245 : unroll->rule_table_id = table_id;
4081 : 2245 : unroll->rule_cookie = cookie;
4082 : 2245 : error = parse_actions_property(&payload, oh->version, &actions);
4083 : 2245 : break;
4084 : : }
4085 : :
4086 : : case NXCPT_ACTION_SET:
4087 : 10 : error = parse_actions_property(&payload, oh->version, &action_set);
4088 : 10 : break;
4089 : :
4090 : : default:
4091 : 0 : error = OFPPROP_UNKNOWN(loose, "continuation", type);
4092 : 0 : break;
4093 : : }
4094 [ - + ]: 5060 : if (error) {
4095 : 5060 : break;
4096 : : }
4097 : : }
4098 : :
4099 : 2113 : pin->actions_len = actions.size;
4100 : 2113 : pin->actions = ofpbuf_steal_data(&actions);
4101 : 2113 : pin->action_set_len = action_set.size;
4102 : 2113 : pin->action_set = ofpbuf_steal_data(&action_set);
4103 : :
4104 [ - + ]: 2113 : if (error) {
4105 : 0 : ofputil_packet_in_private_destroy(pin);
4106 : : }
4107 : :
4108 : 2113 : return 0;
4109 : : }
4110 : :
4111 : : /* Frees data in 'pin' that is dynamically allocated by
4112 : : * ofputil_decode_packet_in_private().
4113 : : *
4114 : : * 'pin->public' contains some pointer members that
4115 : : * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4116 : : * data, so this function doesn't free those. */
4117 : : void
4118 : 2113 : ofputil_packet_in_private_destroy(struct ofputil_packet_in_private *pin)
4119 : : {
4120 [ + - ]: 2113 : if (pin) {
4121 : 2113 : free(pin->stack);
4122 : 2113 : free(pin->actions);
4123 : 2113 : free(pin->action_set);
4124 : : }
4125 : 2113 : }
4126 : :
4127 : : /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4128 : : * 'po'.
4129 : : *
4130 : : * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4131 : : * message's actions. The caller must initialize 'ofpacts' and retains
4132 : : * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4133 : : *
4134 : : * Returns 0 if successful, otherwise an OFPERR_* value. */
4135 : : enum ofperr
4136 : 3181 : ofputil_decode_packet_out(struct ofputil_packet_out *po,
4137 : : const struct ofp_header *oh,
4138 : : struct ofpbuf *ofpacts)
4139 : : {
4140 : 3181 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4141 : 3181 : enum ofpraw raw = ofpraw_pull_assert(&b);
4142 : :
4143 : 3181 : ofpbuf_clear(ofpacts);
4144 [ + + ]: 3181 : if (raw == OFPRAW_OFPT11_PACKET_OUT) {
4145 : : enum ofperr error;
4146 : 652 : const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4147 : :
4148 : 652 : po->buffer_id = ntohl(opo->buffer_id);
4149 : 652 : error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
4150 [ - + ]: 652 : if (error) {
4151 : 0 : return error;
4152 : : }
4153 : :
4154 : 652 : error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
4155 : 652 : oh->version, ofpacts);
4156 [ - + ]: 652 : if (error) {
4157 : 652 : return error;
4158 : : }
4159 [ + - ]: 2529 : } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
4160 : : enum ofperr error;
4161 : 2529 : const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
4162 : :
4163 : 2529 : po->buffer_id = ntohl(opo->buffer_id);
4164 : 2529 : po->in_port = u16_to_ofp(ntohs(opo->in_port));
4165 : :
4166 : 2529 : error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
4167 : 2529 : oh->version, ofpacts);
4168 [ - + ]: 2529 : if (error) {
4169 : 2529 : return error;
4170 : : }
4171 : : } else {
4172 : 0 : OVS_NOT_REACHED();
4173 : : }
4174 : :
4175 [ + + ]: 3181 : if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
4176 [ + + ]: 686 : && po->in_port != OFPP_LOCAL
4177 [ + + ][ - + ]: 684 : && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
4178 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
4179 : : po->in_port);
4180 : 0 : return OFPERR_OFPBRC_BAD_PORT;
4181 : : }
4182 : :
4183 : 3181 : po->ofpacts = ofpacts->data;
4184 : 3181 : po->ofpacts_len = ofpacts->size;
4185 : :
4186 [ + + ]: 3181 : if (po->buffer_id == UINT32_MAX) {
4187 : 3179 : po->packet = b.data;
4188 : 3179 : po->packet_len = b.size;
4189 : : } else {
4190 : 2 : po->packet = NULL;
4191 : 2 : po->packet_len = 0;
4192 : : }
4193 : :
4194 : 3181 : return 0;
4195 : : }
4196 : :
4197 : : /* ofputil_phy_port */
4198 : :
4199 : : /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4200 : : BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4201 : : BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4202 : : BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4203 : : BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4204 : : BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4205 : : BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4206 : : BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4207 : :
4208 : : /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
4209 : : BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
4210 : : BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
4211 : : BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
4212 : : BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
4213 : : BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
4214 : :
4215 : : static enum netdev_features
4216 : 1179 : netdev_port_features_from_ofp10(ovs_be32 ofp10_)
4217 : : {
4218 : 1179 : uint32_t ofp10 = ntohl(ofp10_);
4219 : 1179 : return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
4220 : : }
4221 : :
4222 : : static ovs_be32
4223 : 605 : netdev_port_features_to_ofp10(enum netdev_features features)
4224 : : {
4225 : 605 : return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
4226 : : }
4227 : :
4228 : : BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
4229 : : BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
4230 : : BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
4231 : : BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
4232 : : BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
4233 : : BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
4234 : : BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
4235 : : BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
4236 : : BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
4237 : : BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
4238 : : BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
4239 : : BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
4240 : : BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
4241 : : BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
4242 : : BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
4243 : : BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
4244 : :
4245 : : static enum netdev_features
4246 : 2659 : netdev_port_features_from_ofp11(ovs_be32 ofp11)
4247 : : {
4248 : 2659 : return ntohl(ofp11) & 0xffff;
4249 : : }
4250 : :
4251 : : static ovs_be32
4252 : 2145 : netdev_port_features_to_ofp11(enum netdev_features features)
4253 : : {
4254 : 2145 : return htonl(features & 0xffff);
4255 : : }
4256 : :
4257 : : static enum ofperr
4258 : 288 : ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
4259 : : const struct ofp10_phy_port *opp)
4260 : : {
4261 : 288 : pp->port_no = u16_to_ofp(ntohs(opp->port_no));
4262 : 288 : pp->hw_addr = opp->hw_addr;
4263 : 288 : ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
4264 : :
4265 : 288 : pp->config = ntohl(opp->config) & OFPPC10_ALL;
4266 : 288 : pp->state = ntohl(opp->state) & OFPPS10_ALL;
4267 : :
4268 : 288 : pp->curr = netdev_port_features_from_ofp10(opp->curr);
4269 : 288 : pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
4270 : 288 : pp->supported = netdev_port_features_from_ofp10(opp->supported);
4271 : 288 : pp->peer = netdev_port_features_from_ofp10(opp->peer);
4272 : :
4273 : 288 : pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
4274 : 288 : pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
4275 : :
4276 : 288 : return 0;
4277 : : }
4278 : :
4279 : : static enum ofperr
4280 : 547 : ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
4281 : : const struct ofp11_port *op)
4282 : : {
4283 : : enum ofperr error;
4284 : :
4285 : 547 : error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4286 [ - + ]: 547 : if (error) {
4287 : 0 : return error;
4288 : : }
4289 : 547 : pp->hw_addr = op->hw_addr;
4290 : 547 : ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
4291 : :
4292 : 547 : pp->config = ntohl(op->config) & OFPPC11_ALL;
4293 : 547 : pp->state = ntohl(op->state) & OFPPS11_ALL;
4294 : :
4295 : 547 : pp->curr = netdev_port_features_from_ofp11(op->curr);
4296 : 547 : pp->advertised = netdev_port_features_from_ofp11(op->advertised);
4297 : 547 : pp->supported = netdev_port_features_from_ofp11(op->supported);
4298 : 547 : pp->peer = netdev_port_features_from_ofp11(op->peer);
4299 : :
4300 : 547 : pp->curr_speed = ntohl(op->curr_speed);
4301 : 547 : pp->max_speed = ntohl(op->max_speed);
4302 : :
4303 : 547 : return 0;
4304 : : }
4305 : :
4306 : : static enum ofperr
4307 : 112 : parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
4308 : : struct ofputil_phy_port *pp)
4309 : : {
4310 : 112 : struct ofp14_port_desc_prop_ethernet *eth = payload->data;
4311 : :
4312 [ - + ]: 112 : if (payload->size != sizeof *eth) {
4313 : 0 : return OFPERR_OFPBPC_BAD_LEN;
4314 : : }
4315 : :
4316 : 112 : pp->curr = netdev_port_features_from_ofp11(eth->curr);
4317 : 112 : pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
4318 : 112 : pp->supported = netdev_port_features_from_ofp11(eth->supported);
4319 : 112 : pp->peer = netdev_port_features_from_ofp11(eth->peer);
4320 : :
4321 : 112 : pp->curr_speed = ntohl(eth->curr_speed);
4322 : 112 : pp->max_speed = ntohl(eth->max_speed);
4323 : :
4324 : 112 : return 0;
4325 : : }
4326 : :
4327 : : static enum ofperr
4328 : 112 : ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
4329 : : {
4330 : 112 : struct ofp14_port *op = ofpbuf_try_pull(msg, sizeof *op);
4331 [ - + ]: 112 : if (!op) {
4332 : 0 : return OFPERR_OFPBRC_BAD_LEN;
4333 : : }
4334 : :
4335 : 112 : size_t len = ntohs(op->length);
4336 [ + - ][ - + ]: 112 : if (len < sizeof *op || len - sizeof *op > msg->size) {
4337 : 0 : return OFPERR_OFPBRC_BAD_LEN;
4338 : : }
4339 : 112 : len -= sizeof *op;
4340 : :
4341 : 112 : enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4342 [ - + ]: 112 : if (error) {
4343 : 0 : return error;
4344 : : }
4345 : 112 : pp->hw_addr = op->hw_addr;
4346 : 112 : ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
4347 : :
4348 : 112 : pp->config = ntohl(op->config) & OFPPC11_ALL;
4349 : 112 : pp->state = ntohl(op->state) & OFPPS11_ALL;
4350 : :
4351 : 112 : struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
4352 : : len);
4353 [ + + ]: 224 : while (properties.size > 0) {
4354 : : struct ofpbuf payload;
4355 : : enum ofperr error;
4356 : : uint64_t type;
4357 : :
4358 : 112 : error = ofpprop_pull(&properties, &payload, &type);
4359 [ - + ]: 112 : if (error) {
4360 : 0 : return error;
4361 : : }
4362 : :
4363 [ + - ]: 112 : switch (type) {
4364 : : case OFPPDPT14_ETHERNET:
4365 : 112 : error = parse_ofp14_port_ethernet_property(&payload, pp);
4366 : 112 : break;
4367 : :
4368 : : default:
4369 : 0 : error = OFPPROP_UNKNOWN(true, "port", type);
4370 : 0 : break;
4371 : : }
4372 : :
4373 [ - + ]: 112 : if (error) {
4374 : 112 : return error;
4375 : : }
4376 : : }
4377 : :
4378 : 112 : return 0;
4379 : : }
4380 : :
4381 : : static void
4382 : 148 : ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
4383 : : struct ofp10_phy_port *opp)
4384 : : {
4385 : 148 : memset(opp, 0, sizeof *opp);
4386 : :
4387 : 148 : opp->port_no = htons(ofp_to_u16(pp->port_no));
4388 : 148 : opp->hw_addr = pp->hw_addr;
4389 : 148 : ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
4390 : :
4391 : 148 : opp->config = htonl(pp->config & OFPPC10_ALL);
4392 : 148 : opp->state = htonl(pp->state & OFPPS10_ALL);
4393 : :
4394 : 148 : opp->curr = netdev_port_features_to_ofp10(pp->curr);
4395 : 148 : opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
4396 : 148 : opp->supported = netdev_port_features_to_ofp10(pp->supported);
4397 : 148 : opp->peer = netdev_port_features_to_ofp10(pp->peer);
4398 : 148 : }
4399 : :
4400 : : static void
4401 : 478 : ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
4402 : : struct ofp11_port *op)
4403 : : {
4404 : 478 : memset(op, 0, sizeof *op);
4405 : :
4406 : 478 : op->port_no = ofputil_port_to_ofp11(pp->port_no);
4407 : 478 : op->hw_addr = pp->hw_addr;
4408 : 478 : ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
4409 : :
4410 : 478 : op->config = htonl(pp->config & OFPPC11_ALL);
4411 : 478 : op->state = htonl(pp->state & OFPPS11_ALL);
4412 : :
4413 : 478 : op->curr = netdev_port_features_to_ofp11(pp->curr);
4414 : 478 : op->advertised = netdev_port_features_to_ofp11(pp->advertised);
4415 : 478 : op->supported = netdev_port_features_to_ofp11(pp->supported);
4416 : 478 : op->peer = netdev_port_features_to_ofp11(pp->peer);
4417 : :
4418 : 478 : op->curr_speed = htonl(pp->curr_speed);
4419 : 478 : op->max_speed = htonl(pp->max_speed);
4420 : 478 : }
4421 : :
4422 : : static void
4423 : 56 : ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
4424 : : struct ofpbuf *b)
4425 : : {
4426 : : struct ofp14_port *op;
4427 : : struct ofp14_port_desc_prop_ethernet *eth;
4428 : :
4429 : 56 : ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
4430 : :
4431 : 56 : op = ofpbuf_put_zeros(b, sizeof *op);
4432 : 56 : op->port_no = ofputil_port_to_ofp11(pp->port_no);
4433 : 56 : op->length = htons(sizeof *op + sizeof *eth);
4434 : 56 : op->hw_addr = pp->hw_addr;
4435 : 56 : ovs_strlcpy(op->name, pp->name, sizeof op->name);
4436 : 56 : op->config = htonl(pp->config & OFPPC11_ALL);
4437 : 56 : op->state = htonl(pp->state & OFPPS11_ALL);
4438 : :
4439 : 56 : eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
4440 : 56 : eth->curr = netdev_port_features_to_ofp11(pp->curr);
4441 : 56 : eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
4442 : 56 : eth->supported = netdev_port_features_to_ofp11(pp->supported);
4443 : 56 : eth->peer = netdev_port_features_to_ofp11(pp->peer);
4444 : 56 : eth->curr_speed = htonl(pp->curr_speed);
4445 : 56 : eth->max_speed = htonl(pp->max_speed);
4446 : 56 : }
4447 : :
4448 : : static void
4449 : 682 : ofputil_put_phy_port(enum ofp_version ofp_version,
4450 : : const struct ofputil_phy_port *pp, struct ofpbuf *b)
4451 : : {
4452 [ + + + - ]: 682 : switch (ofp_version) {
4453 : : case OFP10_VERSION: {
4454 : 148 : struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
4455 : 148 : ofputil_encode_ofp10_phy_port(pp, opp);
4456 : 148 : break;
4457 : : }
4458 : :
4459 : : case OFP11_VERSION:
4460 : : case OFP12_VERSION:
4461 : : case OFP13_VERSION: {
4462 : 478 : struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
4463 : 478 : ofputil_encode_ofp11_port(pp, op);
4464 : 478 : break;
4465 : : }
4466 : :
4467 : : case OFP14_VERSION:
4468 : : case OFP15_VERSION:
4469 : : case OFP16_VERSION:
4470 : 56 : ofputil_put_ofp14_port(pp, b);
4471 : 56 : break;
4472 : :
4473 : : default:
4474 : 0 : OVS_NOT_REACHED();
4475 : : }
4476 : 682 : }
4477 : :
4478 : : enum ofperr
4479 : 74 : ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4480 : : ofp_port_t *port)
4481 : : {
4482 : 74 : struct ofpbuf b = ofpbuf_const_initializer(request,
4483 : 74 : ntohs(request->length));
4484 : 74 : enum ofpraw raw = ofpraw_pull_assert(&b);
4485 [ + + ]: 74 : if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4486 : 65 : *port = OFPP_ANY;
4487 : 65 : return 0;
4488 [ + - ]: 9 : } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4489 : : ovs_be32 *ofp11_port;
4490 : :
4491 : 9 : ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4492 : 9 : return ofputil_port_from_ofp11(*ofp11_port, port);
4493 : : } else {
4494 : 74 : OVS_NOT_REACHED();
4495 : : }
4496 : : }
4497 : :
4498 : : struct ofpbuf *
4499 : 36 : ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4500 : : ofp_port_t port)
4501 : : {
4502 : : struct ofpbuf *request;
4503 : :
4504 [ + + - ]: 36 : switch (ofp_version) {
4505 : : case OFP10_VERSION:
4506 : : case OFP11_VERSION:
4507 : : case OFP12_VERSION:
4508 : : case OFP13_VERSION:
4509 : : case OFP14_VERSION:
4510 : 32 : request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4511 : : ofp_version, 0);
4512 : 32 : break;
4513 : : case OFP15_VERSION:
4514 : : case OFP16_VERSION:{
4515 : : struct ofp15_port_desc_request *req;
4516 : 4 : request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4517 : : ofp_version, 0);
4518 : 4 : req = ofpbuf_put_zeros(request, sizeof *req);
4519 : 4 : req->port_no = ofputil_port_to_ofp11(port);
4520 : 4 : break;
4521 : : }
4522 : : default:
4523 : 0 : OVS_NOT_REACHED();
4524 : : }
4525 : :
4526 : 36 : return request;
4527 : : }
4528 : :
4529 : : void
4530 : 70 : ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4531 : : struct ovs_list *replies)
4532 : : {
4533 : 70 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
4534 : 70 : size_t start_ofs = reply->size;
4535 : :
4536 : 70 : ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4537 : 70 : ofpmp_postappend(replies, start_ofs);
4538 : 70 : }
4539 : :
4540 : : /* ofputil_switch_config */
4541 : :
4542 : : /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4543 : : * message, into 'config'. Returns false if 'oh' contained any flags that
4544 : : * aren't specified in its version of OpenFlow, true otherwise. */
4545 : : static bool
4546 : 964 : ofputil_decode_switch_config(const struct ofp_header *oh,
4547 : : struct ofputil_switch_config *config)
4548 : : {
4549 : 964 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4550 : 964 : ofpraw_pull_assert(&b);
4551 : :
4552 : 964 : const struct ofp_switch_config *osc = ofpbuf_pull(&b, sizeof *osc);
4553 : 964 : config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
4554 : 964 : config->miss_send_len = ntohs(osc->miss_send_len);
4555 : :
4556 : 964 : ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
4557 [ + + ]: 964 : if (oh->version < OFP13_VERSION) {
4558 : 714 : const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4559 : 714 : valid_mask |= ttl_bit;
4560 : 714 : config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4561 : : } else {
4562 : 250 : config->invalid_ttl_to_controller = -1;
4563 : : }
4564 : :
4565 : 964 : return !(osc->flags & ~valid_mask);
4566 : : }
4567 : :
4568 : : void
4569 : 539 : ofputil_decode_get_config_reply(const struct ofp_header *oh,
4570 : : struct ofputil_switch_config *config)
4571 : : {
4572 : 539 : ofputil_decode_switch_config(oh, config);
4573 : 539 : }
4574 : :
4575 : : enum ofperr
4576 : 425 : ofputil_decode_set_config(const struct ofp_header *oh,
4577 : : struct ofputil_switch_config *config)
4578 : : {
4579 [ + - ]: 425 : return (ofputil_decode_switch_config(oh, config)
4580 : : ? 0
4581 : : : OFPERR_OFPSCFC_BAD_FLAGS);
4582 : : }
4583 : :
4584 : : static struct ofpbuf *
4585 : 484 : ofputil_put_switch_config(const struct ofputil_switch_config *config,
4586 : : struct ofpbuf *b)
4587 : : {
4588 : 484 : const struct ofp_header *oh = b->data;
4589 : 484 : struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4590 : 484 : osc->flags = htons(config->frag);
4591 [ + + ][ + - ]: 484 : if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4592 : 57 : osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4593 : : }
4594 : 484 : osc->miss_send_len = htons(config->miss_send_len);
4595 : 484 : return b;
4596 : : }
4597 : :
4598 : : struct ofpbuf *
4599 : 282 : ofputil_encode_get_config_reply(const struct ofp_header *request,
4600 : : const struct ofputil_switch_config *config)
4601 : : {
4602 : 282 : struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4603 : : request, 0);
4604 : 282 : return ofputil_put_switch_config(config, b);
4605 : : }
4606 : :
4607 : : struct ofpbuf *
4608 : 202 : ofputil_encode_set_config(const struct ofputil_switch_config *config,
4609 : : enum ofp_version version)
4610 : : {
4611 : 202 : struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4612 : 202 : return ofputil_put_switch_config(config, b);
4613 : : }
4614 : :
4615 : : /* ofputil_switch_features */
4616 : :
4617 : : #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4618 : : OFPC_IP_REASM | OFPC_QUEUE_STATS)
4619 : : BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4620 : : BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4621 : : BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4622 : : BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4623 : : BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4624 : : BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4625 : :
4626 : : static uint32_t
4627 : 215 : ofputil_capabilities_mask(enum ofp_version ofp_version)
4628 : : {
4629 : : /* Handle capabilities whose bit is unique for all OpenFlow versions */
4630 [ + + - ]: 215 : switch (ofp_version) {
4631 : : case OFP10_VERSION:
4632 : : case OFP11_VERSION:
4633 : 113 : return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4634 : : case OFP12_VERSION:
4635 : : case OFP13_VERSION:
4636 : : case OFP14_VERSION:
4637 : : case OFP15_VERSION:
4638 : : case OFP16_VERSION:
4639 : 102 : return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4640 : : default:
4641 : : /* Caller needs to check osf->header.version itself */
4642 : 0 : return 0;
4643 : : }
4644 : : }
4645 : :
4646 : : /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4647 : : * an abstract representation in '*features', readying 'b' to iterate over the
4648 : : * OpenFlow port structures following 'osf' with later calls to
4649 : : * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4650 : : * value. */
4651 : : enum ofperr
4652 : 145 : ofputil_pull_switch_features(struct ofpbuf *b,
4653 : : struct ofputil_switch_features *features)
4654 : : {
4655 : 145 : const struct ofp_header *oh = b->data;
4656 : 145 : enum ofpraw raw = ofpraw_pull_assert(b);
4657 : 145 : const struct ofp_switch_features *osf = ofpbuf_pull(b, sizeof *osf);
4658 : 145 : features->datapath_id = ntohll(osf->datapath_id);
4659 : 145 : features->n_buffers = ntohl(osf->n_buffers);
4660 : 145 : features->n_tables = osf->n_tables;
4661 : 145 : features->auxiliary_id = 0;
4662 : :
4663 : 145 : features->capabilities = ntohl(osf->capabilities) &
4664 : 145 : ofputil_capabilities_mask(oh->version);
4665 : :
4666 [ + + ]: 145 : if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4667 [ - + ]: 75 : if (osf->capabilities & htonl(OFPC10_STP)) {
4668 : 0 : features->capabilities |= OFPUTIL_C_STP;
4669 : : }
4670 : 75 : features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4671 : : OFP10_VERSION);
4672 [ + + ]: 70 : } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4673 [ + - ]: 32 : || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4674 [ + + ]: 70 : if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4675 : 66 : features->capabilities |= OFPUTIL_C_GROUP_STATS;
4676 : : }
4677 : 70 : features->ofpacts = 0;
4678 [ + + ]: 102 : if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4679 : 32 : features->auxiliary_id = osf->auxiliary_id;
4680 : : }
4681 : : } else {
4682 : 0 : return OFPERR_OFPBRC_BAD_VERSION;
4683 : : }
4684 : :
4685 : 145 : return 0;
4686 : : }
4687 : :
4688 : : /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4689 : : * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4690 : : * later, an OFPT_FEATURES_REPLY does not list ports at all.
4691 : : *
4692 : : * Given a buffer 'b' that contains a Features Reply message, this message
4693 : : * checks if it contains a complete list of the switch's ports. Returns true,
4694 : : * if so. Returns false if the list is missing (OF1.3+) or incomplete
4695 : : * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4696 : : * message.
4697 : : *
4698 : : * When this function returns false, the caller should send an OFPST_PORT_DESC
4699 : : * stats request to get the ports. */
4700 : : bool
4701 : 70 : ofputil_switch_features_has_ports(struct ofpbuf *b)
4702 : : {
4703 : 70 : struct ofp_header *oh = b->data;
4704 : : size_t phy_port_size;
4705 : :
4706 [ + + ]: 70 : if (oh->version >= OFP13_VERSION) {
4707 : : /* OpenFlow 1.3+ never has ports in the feature reply. */
4708 : 15 : return false;
4709 : : }
4710 : :
4711 : 110 : phy_port_size = (oh->version == OFP10_VERSION
4712 : : ? sizeof(struct ofp10_phy_port)
4713 [ + + ]: 55 : : sizeof(struct ofp11_port));
4714 [ + - ]: 55 : if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4715 : : /* There's room for additional ports in the feature reply.
4716 : : * Assume that the list is complete. */
4717 : 55 : return true;
4718 : : }
4719 : :
4720 : : /* The feature reply has no room for more ports. Probably the list is
4721 : : * truncated. Drop the ports and tell the caller to retrieve them with
4722 : : * OFPST_PORT_DESC. */
4723 : 0 : b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4724 : 0 : ofpmsg_update_length(b);
4725 : 0 : return false;
4726 : : }
4727 : :
4728 : : /* Returns a buffer owned by the caller that encodes 'features' in the format
4729 : : * required by 'protocol' with the given 'xid'. The caller should append port
4730 : : * information to the buffer with subsequent calls to
4731 : : * ofputil_put_switch_features_port(). */
4732 : : struct ofpbuf *
4733 : 70 : ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4734 : : enum ofputil_protocol protocol, ovs_be32 xid)
4735 : : {
4736 : : struct ofp_switch_features *osf;
4737 : : struct ofpbuf *b;
4738 : : enum ofp_version version;
4739 : : enum ofpraw raw;
4740 : :
4741 : 70 : version = ofputil_protocol_to_ofp_version(protocol);
4742 [ + + + - ]: 70 : switch (version) {
4743 : : case OFP10_VERSION:
4744 : 37 : raw = OFPRAW_OFPT10_FEATURES_REPLY;
4745 : 37 : break;
4746 : : case OFP11_VERSION:
4747 : : case OFP12_VERSION:
4748 : 18 : raw = OFPRAW_OFPT11_FEATURES_REPLY;
4749 : 18 : break;
4750 : : case OFP13_VERSION:
4751 : : case OFP14_VERSION:
4752 : : case OFP15_VERSION:
4753 : : case OFP16_VERSION:
4754 : 15 : raw = OFPRAW_OFPT13_FEATURES_REPLY;
4755 : 15 : break;
4756 : : default:
4757 : 0 : OVS_NOT_REACHED();
4758 : : }
4759 : 70 : b = ofpraw_alloc_xid(raw, version, xid, 0);
4760 : 70 : osf = ofpbuf_put_zeros(b, sizeof *osf);
4761 : 70 : osf->datapath_id = htonll(features->datapath_id);
4762 : 70 : osf->n_buffers = htonl(features->n_buffers);
4763 : 70 : osf->n_tables = features->n_tables;
4764 : :
4765 : 70 : osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4766 : 70 : osf->capabilities = htonl(features->capabilities &
4767 : 70 : ofputil_capabilities_mask(version));
4768 [ + + + - ]: 70 : switch (version) {
4769 : : case OFP10_VERSION:
4770 [ - + ]: 37 : if (features->capabilities & OFPUTIL_C_STP) {
4771 : 0 : osf->capabilities |= htonl(OFPC10_STP);
4772 : : }
4773 : 37 : osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4774 : : OFP10_VERSION);
4775 : 37 : break;
4776 : : case OFP13_VERSION:
4777 : : case OFP14_VERSION:
4778 : : case OFP15_VERSION:
4779 : : case OFP16_VERSION:
4780 : 15 : osf->auxiliary_id = features->auxiliary_id;
4781 : : /* fall through */
4782 : : case OFP11_VERSION:
4783 : : case OFP12_VERSION:
4784 [ + - ]: 33 : if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4785 : 33 : osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4786 : : }
4787 : 33 : break;
4788 : : default:
4789 : 0 : OVS_NOT_REACHED();
4790 : : }
4791 : :
4792 : 70 : return b;
4793 : : }
4794 : :
4795 : : /* Encodes 'pp' into the format required by the switch_features message already
4796 : : * in 'b', which should have been returned by ofputil_encode_switch_features(),
4797 : : * and appends the encoded version to 'b'. */
4798 : : void
4799 : 192 : ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4800 : : struct ofpbuf *b)
4801 : : {
4802 : 192 : const struct ofp_header *oh = b->data;
4803 : :
4804 [ + + ]: 192 : if (oh->version < OFP13_VERSION) {
4805 : : /* Try adding a port description to the message, but drop it again if
4806 : : * the buffer overflows. (This possibility for overflow is why
4807 : : * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4808 : 154 : size_t start_ofs = b->size;
4809 : 154 : ofputil_put_phy_port(oh->version, pp, b);
4810 [ - + ]: 154 : if (b->size > UINT16_MAX) {
4811 : 0 : b->size = start_ofs;
4812 : : }
4813 : : }
4814 : 192 : }
4815 : :
4816 : : /* ofputil_port_status */
4817 : :
4818 : : /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4819 : : * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4820 : : enum ofperr
4821 : 505 : ofputil_decode_port_status(const struct ofp_header *oh,
4822 : : struct ofputil_port_status *ps)
4823 : : {
4824 : 505 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4825 : 505 : ofpraw_pull_assert(&b);
4826 : :
4827 : 505 : const struct ofp_port_status *ops = ofpbuf_pull(&b, sizeof *ops);
4828 [ + + ][ + + ]: 505 : if (ops->reason != OFPPR_ADD &&
4829 [ - + ]: 25 : ops->reason != OFPPR_DELETE &&
4830 : 25 : ops->reason != OFPPR_MODIFY) {
4831 : 0 : return OFPERR_NXBRC_BAD_REASON;
4832 : : }
4833 : 505 : ps->reason = ops->reason;
4834 : :
4835 : 505 : int retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4836 [ - + ]: 505 : ovs_assert(retval != EOF);
4837 : 505 : return retval;
4838 : : }
4839 : :
4840 : : /* Converts the abstract form of a "port status" message in '*ps' into an
4841 : : * OpenFlow message suitable for 'protocol', and returns that encoded form in
4842 : : * a buffer owned by the caller. */
4843 : : struct ofpbuf *
4844 : 458 : ofputil_encode_port_status(const struct ofputil_port_status *ps,
4845 : : enum ofputil_protocol protocol)
4846 : : {
4847 : : struct ofp_port_status *ops;
4848 : : struct ofpbuf *b;
4849 : : enum ofp_version version;
4850 : : enum ofpraw raw;
4851 : :
4852 : 458 : version = ofputil_protocol_to_ofp_version(protocol);
4853 [ + + + - ]: 458 : switch (version) {
4854 : : case OFP10_VERSION:
4855 : 11 : raw = OFPRAW_OFPT10_PORT_STATUS;
4856 : 11 : break;
4857 : :
4858 : : case OFP11_VERSION:
4859 : : case OFP12_VERSION:
4860 : : case OFP13_VERSION:
4861 : 430 : raw = OFPRAW_OFPT11_PORT_STATUS;
4862 : 430 : break;
4863 : :
4864 : : case OFP14_VERSION:
4865 : : case OFP15_VERSION:
4866 : : case OFP16_VERSION:
4867 : 17 : raw = OFPRAW_OFPT14_PORT_STATUS;
4868 : 17 : break;
4869 : :
4870 : : default:
4871 : 0 : OVS_NOT_REACHED();
4872 : : }
4873 : :
4874 : 458 : b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4875 : 458 : ops = ofpbuf_put_zeros(b, sizeof *ops);
4876 : 458 : ops->reason = ps->reason;
4877 : 458 : ofputil_put_phy_port(version, &ps->desc, b);
4878 : 458 : ofpmsg_update_length(b);
4879 : 458 : return b;
4880 : : }
4881 : :
4882 : : /* ofputil_port_mod */
4883 : :
4884 : : static enum ofperr
4885 : 2 : parse_port_mod_ethernet_property(struct ofpbuf *property,
4886 : : struct ofputil_port_mod *pm)
4887 : : {
4888 : : ovs_be32 advertise;
4889 : : enum ofperr error;
4890 : :
4891 : 2 : error = ofpprop_parse_be32(property, &advertise);
4892 [ + - ]: 2 : if (!error) {
4893 : 2 : pm->advertise = netdev_port_features_from_ofp11(advertise);
4894 : : }
4895 : 2 : return error;
4896 : : }
4897 : :
4898 : : /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4899 : : * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4900 : : enum ofperr
4901 : 84 : ofputil_decode_port_mod(const struct ofp_header *oh,
4902 : : struct ofputil_port_mod *pm, bool loose)
4903 : : {
4904 : 84 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4905 : 84 : enum ofpraw raw = ofpraw_pull_assert(&b);
4906 [ + + ]: 84 : if (raw == OFPRAW_OFPT10_PORT_MOD) {
4907 : 27 : const struct ofp10_port_mod *opm = b.data;
4908 : :
4909 : 27 : pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4910 : 27 : pm->hw_addr = opm->hw_addr;
4911 : 27 : pm->config = ntohl(opm->config) & OFPPC10_ALL;
4912 : 27 : pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4913 : 27 : pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4914 [ + + ]: 57 : } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4915 : 21 : const struct ofp11_port_mod *opm = b.data;
4916 : : enum ofperr error;
4917 : :
4918 : 21 : error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4919 [ - + ]: 21 : if (error) {
4920 : 0 : return error;
4921 : : }
4922 : :
4923 : 21 : pm->hw_addr = opm->hw_addr;
4924 : 21 : pm->config = ntohl(opm->config) & OFPPC11_ALL;
4925 : 21 : pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4926 : 21 : pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4927 [ + - ]: 36 : } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4928 : 36 : const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4929 : : enum ofperr error;
4930 : :
4931 : 36 : memset(pm, 0, sizeof *pm);
4932 : :
4933 : 36 : error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4934 [ - + ]: 36 : if (error) {
4935 : 0 : return error;
4936 : : }
4937 : :
4938 : 36 : pm->hw_addr = opm->hw_addr;
4939 : 36 : pm->config = ntohl(opm->config) & OFPPC11_ALL;
4940 : 36 : pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4941 : :
4942 [ + + ]: 38 : while (b.size > 0) {
4943 : : struct ofpbuf property;
4944 : : enum ofperr error;
4945 : : uint64_t type;
4946 : :
4947 : 2 : error = ofpprop_pull(&b, &property, &type);
4948 [ - + ]: 2 : if (error) {
4949 : 0 : return error;
4950 : : }
4951 : :
4952 [ + - ]: 2 : switch (type) {
4953 : : case OFPPMPT14_ETHERNET:
4954 : 2 : error = parse_port_mod_ethernet_property(&property, pm);
4955 : 2 : break;
4956 : :
4957 : : default:
4958 : 0 : error = OFPPROP_UNKNOWN(loose, "port_mod", type);
4959 : 0 : break;
4960 : : }
4961 : :
4962 [ - + ]: 2 : if (error) {
4963 : 2 : return error;
4964 : : }
4965 : : }
4966 : : } else {
4967 : 0 : return OFPERR_OFPBRC_BAD_TYPE;
4968 : : }
4969 : :
4970 : 84 : pm->config &= pm->mask;
4971 : 84 : return 0;
4972 : : }
4973 : :
4974 : : /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4975 : : * message suitable for 'protocol', and returns that encoded form in a buffer
4976 : : * owned by the caller. */
4977 : : struct ofpbuf *
4978 : 39 : ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4979 : : enum ofputil_protocol protocol)
4980 : : {
4981 : 39 : enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4982 : : struct ofpbuf *b;
4983 : :
4984 [ + + + - ]: 39 : switch (ofp_version) {
4985 : : case OFP10_VERSION: {
4986 : : struct ofp10_port_mod *opm;
4987 : :
4988 : 13 : b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4989 : 13 : opm = ofpbuf_put_zeros(b, sizeof *opm);
4990 : 13 : opm->port_no = htons(ofp_to_u16(pm->port_no));
4991 : 13 : opm->hw_addr = pm->hw_addr;
4992 : 13 : opm->config = htonl(pm->config & OFPPC10_ALL);
4993 : 13 : opm->mask = htonl(pm->mask & OFPPC10_ALL);
4994 : 13 : opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4995 : 13 : break;
4996 : : }
4997 : :
4998 : : case OFP11_VERSION:
4999 : : case OFP12_VERSION:
5000 : : case OFP13_VERSION: {
5001 : : struct ofp11_port_mod *opm;
5002 : :
5003 : 9 : b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
5004 : 9 : opm = ofpbuf_put_zeros(b, sizeof *opm);
5005 : 9 : opm->port_no = ofputil_port_to_ofp11(pm->port_no);
5006 : 9 : opm->hw_addr = pm->hw_addr;
5007 : 9 : opm->config = htonl(pm->config & OFPPC11_ALL);
5008 : 9 : opm->mask = htonl(pm->mask & OFPPC11_ALL);
5009 : 9 : opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
5010 : 9 : break;
5011 : : }
5012 : : case OFP14_VERSION:
5013 : : case OFP15_VERSION:
5014 : : case OFP16_VERSION: {
5015 : : struct ofp14_port_mod *opm;
5016 : :
5017 : 17 : b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, 0);
5018 : 17 : opm = ofpbuf_put_zeros(b, sizeof *opm);
5019 : 17 : opm->port_no = ofputil_port_to_ofp11(pm->port_no);
5020 : 17 : opm->hw_addr = pm->hw_addr;
5021 : 17 : opm->config = htonl(pm->config & OFPPC11_ALL);
5022 : 17 : opm->mask = htonl(pm->mask & OFPPC11_ALL);
5023 : :
5024 [ - + ]: 17 : if (pm->advertise) {
5025 : 0 : ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
5026 : : netdev_port_features_to_ofp11(pm->advertise));
5027 : : }
5028 : 17 : break;
5029 : : }
5030 : : default:
5031 : 0 : OVS_NOT_REACHED();
5032 : : }
5033 : :
5034 : 39 : return b;
5035 : : }
5036 : :
5037 : : /* Table features. */
5038 : :
5039 : : static enum ofperr
5040 : 14238 : pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
5041 : : uint64_t *typep)
5042 : : {
5043 : : enum ofperr error;
5044 : :
5045 : 14238 : error = ofpprop_pull(msg, payload, typep);
5046 [ + - ][ + - ]: 14238 : if (payload && !error) {
5047 : 14238 : ofpbuf_pull(payload, (char *)payload->msg - (char *)payload->header);
5048 : : }
5049 : 14238 : return error;
5050 : : }
5051 : :
5052 : : static enum ofperr
5053 : 4068 : parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
5054 : : uint64_t *ofpacts)
5055 : : {
5056 : 4068 : uint32_t types = 0;
5057 : :
5058 [ + + ]: 52900 : while (payload->size > 0) {
5059 : : enum ofperr error;
5060 : : uint64_t type;
5061 : :
5062 : 48832 : error = ofpprop_pull__(payload, NULL, 1, 0x10000, &type);
5063 [ - + ]: 48832 : if (error) {
5064 : 0 : return error;
5065 : : }
5066 [ + - ]: 48832 : if (type < CHAR_BIT * sizeof types) {
5067 : 48832 : types |= 1u << type;
5068 : : }
5069 : : }
5070 : :
5071 : 4068 : *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
5072 : 4068 : return 0;
5073 : : }
5074 : :
5075 : : static enum ofperr
5076 : 2034 : parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
5077 : : {
5078 : 2034 : *insts = 0;
5079 [ + + ]: 14228 : while (payload->size > 0) {
5080 : : enum ovs_instruction_type inst;
5081 : : enum ofperr error;
5082 : : uint64_t ofpit;
5083 : :
5084 : : /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5085 : : * It seems clear that they aren't padded to 8 bytes, though, because
5086 : : * both standards say that "non-experimenter instructions are 4 bytes"
5087 : : * and do not mention any padding before the first instruction ID.
5088 : : * (There wouldn't be any point in padding to 8 bytes if the IDs were
5089 : : * aligned on an odd 4-byte boundary.)
5090 : : *
5091 : : * Anyway, we just assume they're all glommed together on byte
5092 : : * boundaries. */
5093 : 12194 : error = ofpprop_pull__(payload, NULL, 1, 0x10000, &ofpit);
5094 [ - + ]: 12194 : if (error) {
5095 : 0 : return error;
5096 : : }
5097 : :
5098 : 12194 : error = ovs_instruction_type_from_inst_type(&inst, ofpit);
5099 [ + - ]: 12194 : if (!error) {
5100 : 12194 : *insts |= 1u << inst;
5101 [ # # ]: 0 : } else if (!loose) {
5102 : 12194 : return error;
5103 : : }
5104 : : }
5105 : 2034 : return 0;
5106 : : }
5107 : :
5108 : : static enum ofperr
5109 : 2034 : parse_table_features_next_table(struct ofpbuf *payload,
5110 : : unsigned long int *next_tables)
5111 : : {
5112 : : size_t i;
5113 : :
5114 : 2034 : memset(next_tables, 0, bitmap_n_bytes(255));
5115 [ + + ]: 259588 : for (i = 0; i < payload->size; i++) {
5116 : 257554 : uint8_t id = ((const uint8_t *) payload->data)[i];
5117 [ - + ]: 257554 : if (id >= 255) {
5118 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
5119 : : }
5120 : 257554 : bitmap_set1(next_tables, id);
5121 : : }
5122 : 2034 : return 0;
5123 : : }
5124 : :
5125 : : static enum ofperr
5126 : 6102 : parse_oxms(struct ofpbuf *payload, bool loose,
5127 : : struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
5128 : : {
5129 : 6102 : struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
5130 : 6102 : struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
5131 : :
5132 [ + + ]: 888262 : while (payload->size > 0) {
5133 : : const struct mf_field *field;
5134 : : enum ofperr error;
5135 : : bool hasmask;
5136 : :
5137 : 882160 : error = nx_pull_header(payload, &field, &hasmask);
5138 [ + - ]: 882160 : if (!error) {
5139 [ + + ]: 882160 : bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
5140 [ # # ][ # # ]: 0 : } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
5141 : 882160 : return error;
5142 : : }
5143 : : }
5144 [ + - ]: 6102 : if (exactp) {
5145 : 6102 : *exactp = exact;
5146 [ # # ]: 0 : } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
5147 : 0 : return OFPERR_OFPBMC_BAD_MASK;
5148 : : }
5149 [ + + ]: 6102 : if (maskedp) {
5150 : 1017 : *maskedp = masked;
5151 [ - + ]: 5085 : } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
5152 : 0 : return OFPERR_OFPBMC_BAD_MASK;
5153 : : }
5154 : 6102 : return 0;
5155 : : }
5156 : :
5157 : : /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5158 : : * ofputil_table_features in 'tf'.
5159 : : *
5160 : : * If 'loose' is true, this function ignores properties and values that it does
5161 : : * not understand, as a controller would want to do when interpreting
5162 : : * capabilities provided by a switch. If 'loose' is false, this function
5163 : : * treats unknown properties and values as an error, as a switch would want to
5164 : : * do when interpreting a configuration request made by a controller.
5165 : : *
5166 : : * A single OpenFlow message can specify features for multiple tables. Calling
5167 : : * this function multiple times for a single 'msg' iterates through the tables
5168 : : * in the message. The caller must initially leave 'msg''s layer pointers null
5169 : : * and not modify them between calls.
5170 : : *
5171 : : * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5172 : : * a positive "enum ofperr" value. */
5173 : : int
5174 : 1088 : ofputil_decode_table_features(struct ofpbuf *msg,
5175 : : struct ofputil_table_features *tf, bool loose)
5176 : : {
5177 : 1088 : memset(tf, 0, sizeof *tf);
5178 : :
5179 [ + + ]: 1088 : if (!msg->header) {
5180 : 71 : ofpraw_pull_assert(msg);
5181 : : }
5182 : :
5183 [ + + ]: 1088 : if (!msg->size) {
5184 : 71 : return EOF;
5185 : : }
5186 : :
5187 : 1017 : const struct ofp_header *oh = msg->header;
5188 : 1017 : struct ofp13_table_features *otf = msg->data;
5189 [ - + ]: 1017 : if (msg->size < sizeof *otf) {
5190 : 0 : return OFPERR_OFPBPC_BAD_LEN;
5191 : : }
5192 : :
5193 : 1017 : unsigned int len = ntohs(otf->length);
5194 [ + - ][ + - ]: 1017 : if (len < sizeof *otf || len % 8 || len > msg->size) {
[ - + ]
5195 : 0 : return OFPERR_OFPBPC_BAD_LEN;
5196 : : }
5197 : :
5198 : 1017 : tf->table_id = otf->table_id;
5199 [ - + ]: 1017 : if (tf->table_id == OFPTT_ALL) {
5200 : 0 : return OFPERR_OFPTFFC_BAD_TABLE;
5201 : : }
5202 : :
5203 : 1017 : ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
5204 : 1017 : tf->metadata_match = otf->metadata_match;
5205 : 1017 : tf->metadata_write = otf->metadata_write;
5206 : 1017 : tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
5207 [ - + ]: 1017 : if (oh->version >= OFP14_VERSION) {
5208 : 0 : uint32_t caps = ntohl(otf->capabilities);
5209 : 0 : tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
5210 : 0 : tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
5211 : : } else {
5212 : 1017 : tf->supports_eviction = -1;
5213 : 1017 : tf->supports_vacancy_events = -1;
5214 : : }
5215 : 1017 : tf->max_entries = ntohl(otf->max_entries);
5216 : :
5217 : 1017 : struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
5218 : : len);
5219 : 1017 : ofpbuf_pull(&properties, sizeof *otf);
5220 [ + + ]: 15255 : while (properties.size > 0) {
5221 : : struct ofpbuf payload;
5222 : : enum ofperr error;
5223 : : uint64_t type;
5224 : :
5225 : 14238 : error = pull_table_feature_property(&properties, &payload, &type);
5226 [ - + ]: 14238 : if (error) {
5227 : 0 : return error;
5228 : : }
5229 : :
5230 [ + + + + : 14238 : switch ((enum ofp13_table_feature_prop_type) type) {
+ + + + +
+ + + + +
- ]
5231 : : case OFPTFPT13_INSTRUCTIONS:
5232 : 1017 : error = parse_instruction_ids(&payload, loose,
5233 : : &tf->nonmiss.instructions);
5234 : 1017 : break;
5235 : : case OFPTFPT13_INSTRUCTIONS_MISS:
5236 : 1017 : error = parse_instruction_ids(&payload, loose,
5237 : : &tf->miss.instructions);
5238 : 1017 : break;
5239 : :
5240 : : case OFPTFPT13_NEXT_TABLES:
5241 : 1017 : error = parse_table_features_next_table(&payload,
5242 : 1017 : tf->nonmiss.next);
5243 : 1017 : break;
5244 : : case OFPTFPT13_NEXT_TABLES_MISS:
5245 : 1017 : error = parse_table_features_next_table(&payload, tf->miss.next);
5246 : 1017 : break;
5247 : :
5248 : : case OFPTFPT13_WRITE_ACTIONS:
5249 : 1017 : error = parse_action_bitmap(&payload, oh->version,
5250 : : &tf->nonmiss.write.ofpacts);
5251 : 1017 : break;
5252 : : case OFPTFPT13_WRITE_ACTIONS_MISS:
5253 : 1017 : error = parse_action_bitmap(&payload, oh->version,
5254 : : &tf->miss.write.ofpacts);
5255 : 1017 : break;
5256 : :
5257 : : case OFPTFPT13_APPLY_ACTIONS:
5258 : 1017 : error = parse_action_bitmap(&payload, oh->version,
5259 : : &tf->nonmiss.apply.ofpacts);
5260 : 1017 : break;
5261 : : case OFPTFPT13_APPLY_ACTIONS_MISS:
5262 : 1017 : error = parse_action_bitmap(&payload, oh->version,
5263 : : &tf->miss.apply.ofpacts);
5264 : 1017 : break;
5265 : :
5266 : : case OFPTFPT13_MATCH:
5267 : 1017 : error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
5268 : 1017 : break;
5269 : : case OFPTFPT13_WILDCARDS:
5270 : 1017 : error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
5271 : 1017 : break;
5272 : :
5273 : : case OFPTFPT13_WRITE_SETFIELD:
5274 : 1017 : error = parse_oxms(&payload, loose,
5275 : : &tf->nonmiss.write.set_fields, NULL);
5276 : 1017 : break;
5277 : : case OFPTFPT13_WRITE_SETFIELD_MISS:
5278 : 1017 : error = parse_oxms(&payload, loose,
5279 : : &tf->miss.write.set_fields, NULL);
5280 : 1017 : break;
5281 : : case OFPTFPT13_APPLY_SETFIELD:
5282 : 1017 : error = parse_oxms(&payload, loose,
5283 : : &tf->nonmiss.apply.set_fields, NULL);
5284 : 1017 : break;
5285 : : case OFPTFPT13_APPLY_SETFIELD_MISS:
5286 : 1017 : error = parse_oxms(&payload, loose,
5287 : : &tf->miss.apply.set_fields, NULL);
5288 : 1017 : break;
5289 : :
5290 : : case OFPTFPT13_EXPERIMENTER:
5291 : : case OFPTFPT13_EXPERIMENTER_MISS:
5292 : : default:
5293 : 0 : error = OFPPROP_UNKNOWN(loose, "table features", type);
5294 : 0 : break;
5295 : : }
5296 [ - + ]: 14238 : if (error) {
5297 : 14238 : return error;
5298 : : }
5299 : : }
5300 : :
5301 : : /* Fix inconsistencies:
5302 : : *
5303 : : * - Turn on 'match' bits that are set in 'mask', because maskable
5304 : : * fields are matchable.
5305 : : *
5306 : : * - Turn on 'wildcard' bits that are set in 'mask', because a field
5307 : : * that is arbitrarily maskable can be wildcarded entirely.
5308 : : *
5309 : : * - Turn off 'wildcard' bits that are not in 'match', because a field
5310 : : * must be matchable for it to be meaningfully wildcarded. */
5311 : 1017 : bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
5312 : 1017 : bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
5313 : 1017 : bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
5314 : :
5315 : 1088 : return 0;
5316 : : }
5317 : :
5318 : : /* Encodes and returns a request to obtain the table features of a switch.
5319 : : * The message is encoded for OpenFlow version 'ofp_version'. */
5320 : : struct ofpbuf *
5321 : 2 : ofputil_encode_table_features_request(enum ofp_version ofp_version)
5322 : : {
5323 : 2 : struct ofpbuf *request = NULL;
5324 : :
5325 [ - + - ]: 2 : switch (ofp_version) {
5326 : : case OFP10_VERSION:
5327 : : case OFP11_VERSION:
5328 : : case OFP12_VERSION:
5329 : 0 : ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5330 : : "(\'-O OpenFlow13\')");
5331 : : case OFP13_VERSION:
5332 : : case OFP14_VERSION:
5333 : : case OFP15_VERSION:
5334 : : case OFP16_VERSION:
5335 : 2 : request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
5336 : : ofp_version, 0);
5337 : 2 : break;
5338 : : default:
5339 : 0 : OVS_NOT_REACHED();
5340 : : }
5341 : :
5342 : 2 : return request;
5343 : : }
5344 : :
5345 : : static void
5346 : 3048 : put_fields_property(struct ofpbuf *reply,
5347 : : const struct mf_bitmap *fields,
5348 : : const struct mf_bitmap *masks,
5349 : : enum ofp13_table_feature_prop_type property,
5350 : : enum ofp_version version)
5351 : : {
5352 : : size_t start_ofs;
5353 : : int field;
5354 : :
5355 : 3048 : start_ofs = ofpprop_start(reply, property);
5356 [ + + ]: 443992 : BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
5357 : 440944 : nx_put_header(reply, field, version,
5358 [ + + ][ + + ]: 440944 : masks && bitmap_is_set(masks->bm, field));
5359 : : }
5360 : 3048 : ofpprop_end(reply, start_ofs);
5361 : 3048 : }
5362 : :
5363 : : static void
5364 : 2032 : put_table_action_features(struct ofpbuf *reply,
5365 : : const struct ofputil_table_action_features *taf,
5366 : : enum ofp13_table_feature_prop_type actions_type,
5367 : : enum ofp13_table_feature_prop_type set_fields_type,
5368 : : int miss_offset, enum ofp_version version)
5369 : : {
5370 : 2032 : ofpprop_put_bitmap(reply, actions_type + miss_offset,
5371 : 2032 : ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
5372 : : version)));
5373 : 2032 : put_fields_property(reply, &taf->set_fields, NULL,
5374 : 2032 : set_fields_type + miss_offset, version);
5375 : 2032 : }
5376 : :
5377 : : static void
5378 : 1016 : put_table_instruction_features(
5379 : : struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
5380 : : int miss_offset, enum ofp_version version)
5381 : : {
5382 : : size_t start_ofs;
5383 : : uint8_t table_id;
5384 : :
5385 : 1016 : ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
5386 : 1016 : ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
5387 : : version)));
5388 : :
5389 : 1016 : start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
5390 [ + + ]: 129540 : BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
5391 : 128524 : ofpbuf_put(reply, &table_id, 1);
5392 : : }
5393 : 1016 : ofpprop_end(reply, start_ofs);
5394 : :
5395 : 1016 : put_table_action_features(reply, &tif->write,
5396 : : OFPTFPT13_WRITE_ACTIONS,
5397 : : OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
5398 : 1016 : put_table_action_features(reply, &tif->apply,
5399 : : OFPTFPT13_APPLY_ACTIONS,
5400 : : OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
5401 : 1016 : }
5402 : :
5403 : : void
5404 : 508 : ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
5405 : : struct ovs_list *replies)
5406 : : {
5407 : 508 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
5408 : 508 : enum ofp_version version = ofpmp_version(replies);
5409 : 508 : size_t start_ofs = reply->size;
5410 : : struct ofp13_table_features *otf;
5411 : :
5412 : 508 : otf = ofpbuf_put_zeros(reply, sizeof *otf);
5413 : 508 : otf->table_id = tf->table_id;
5414 : 508 : ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
5415 : 508 : otf->metadata_match = tf->metadata_match;
5416 : 508 : otf->metadata_write = tf->metadata_write;
5417 [ - + ]: 508 : if (version >= OFP14_VERSION) {
5418 [ # # ]: 0 : if (tf->supports_eviction) {
5419 : 0 : otf->capabilities |= htonl(OFPTC14_EVICTION);
5420 : : }
5421 [ # # ]: 0 : if (tf->supports_vacancy_events) {
5422 : 0 : otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
5423 : : }
5424 : : }
5425 : 508 : otf->max_entries = htonl(tf->max_entries);
5426 : :
5427 : 508 : put_table_instruction_features(reply, &tf->nonmiss, 0, version);
5428 : 508 : put_table_instruction_features(reply, &tf->miss, 1, version);
5429 : :
5430 : 508 : put_fields_property(reply, &tf->match, &tf->mask,
5431 : : OFPTFPT13_MATCH, version);
5432 : 508 : put_fields_property(reply, &tf->wildcard, NULL,
5433 : : OFPTFPT13_WILDCARDS, version);
5434 : :
5435 : 508 : otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
5436 : 508 : otf->length = htons(reply->size - start_ofs);
5437 : 508 : ofpmp_postappend(replies, start_ofs);
5438 : 508 : }
5439 : :
5440 : : static enum ofperr
5441 : 10 : parse_table_desc_vacancy_property(struct ofpbuf *property,
5442 : : struct ofputil_table_desc *td)
5443 : : {
5444 : 10 : struct ofp14_table_mod_prop_vacancy *otv = property->data;
5445 : :
5446 [ - + ]: 10 : if (property->size != sizeof *otv) {
5447 : 0 : return OFPERR_OFPBPC_BAD_LEN;
5448 : : }
5449 : :
5450 : 10 : td->table_vacancy.vacancy_down = otv->vacancy_down;
5451 : 10 : td->table_vacancy.vacancy_up = otv->vacancy_up;
5452 : 10 : td->table_vacancy.vacancy = otv->vacancy;
5453 : 10 : return 0;
5454 : : }
5455 : :
5456 : : /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5457 : : * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5458 : : * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5459 : : * value. */
5460 : : int
5461 : 3341 : ofputil_decode_table_desc(struct ofpbuf *msg,
5462 : : struct ofputil_table_desc *td,
5463 : : enum ofp_version version)
5464 : : {
5465 : 3341 : memset(td, 0, sizeof *td);
5466 : :
5467 [ + + ]: 3341 : if (!msg->header) {
5468 : 23 : ofpraw_pull_assert(msg);
5469 : : }
5470 : :
5471 [ + + ]: 3341 : if (!msg->size) {
5472 : 23 : return EOF;
5473 : : }
5474 : :
5475 : 3318 : struct ofp14_table_desc *otd = ofpbuf_try_pull(msg, sizeof *otd);
5476 [ - + ]: 3318 : if (!otd) {
5477 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
5478 : : "leftover bytes at end", msg->size);
5479 : 0 : return OFPERR_OFPBRC_BAD_LEN;
5480 : : }
5481 : :
5482 : 3318 : td->table_id = otd->table_id;
5483 : 3318 : size_t length = ntohs(otd->length);
5484 [ + - ][ - + ]: 3318 : if (length < sizeof *otd || length - sizeof *otd > msg->size) {
5485 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
5486 : : "length %"PRIuSIZE, length);
5487 : 0 : return OFPERR_OFPBRC_BAD_LEN;
5488 : : }
5489 : 3318 : length -= sizeof *otd;
5490 : :
5491 : 3318 : td->eviction = ofputil_decode_table_eviction(otd->config, version);
5492 : 3318 : td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
5493 : 3318 : td->eviction_flags = UINT32_MAX;
5494 : :
5495 : 3318 : struct ofpbuf properties = ofpbuf_const_initializer(
5496 : 3318 : ofpbuf_pull(msg, length), length);
5497 [ + + ]: 6646 : while (properties.size > 0) {
5498 : : struct ofpbuf payload;
5499 : : enum ofperr error;
5500 : : uint64_t type;
5501 : :
5502 : 3328 : error = ofpprop_pull(&properties, &payload, &type);
5503 [ - + ]: 3328 : if (error) {
5504 : 0 : return error;
5505 : : }
5506 : :
5507 [ + + - ]: 3328 : switch (type) {
5508 : : case OFPTMPT14_EVICTION:
5509 : 3318 : error = ofpprop_parse_u32(&payload, &td->eviction_flags);
5510 : 3318 : break;
5511 : :
5512 : : case OFPTMPT14_VACANCY:
5513 : 10 : error = parse_table_desc_vacancy_property(&payload, td);
5514 : 10 : break;
5515 : :
5516 : : default:
5517 : 0 : error = OFPPROP_UNKNOWN(true, "table_desc", type);
5518 : 0 : break;
5519 : : }
5520 : :
5521 [ - + ]: 3328 : if (error) {
5522 : 3328 : return error;
5523 : : }
5524 : : }
5525 : :
5526 : 3341 : return 0;
5527 : : }
5528 : :
5529 : : /* Encodes and returns a request to obtain description of tables of a switch.
5530 : : * The message is encoded for OpenFlow version 'ofp_version'. */
5531 : : struct ofpbuf *
5532 : 10 : ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5533 : : {
5534 : 10 : struct ofpbuf *request = NULL;
5535 : :
5536 [ + - ]: 10 : if (ofp_version >= OFP14_VERSION) {
5537 : 10 : request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5538 : : ofp_version, 0);
5539 : : } else {
5540 : 0 : ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5541 : : "(\'-O OpenFlow14\')");
5542 : : }
5543 : :
5544 : 10 : return request;
5545 : : }
5546 : :
5547 : : /* Function to append Table desc information in a reply list. */
5548 : : void
5549 : 2540 : ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5550 : : struct ovs_list *replies,
5551 : : enum ofp_version version)
5552 : : {
5553 : 2540 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
5554 : : size_t start_otd;
5555 : : struct ofp14_table_desc *otd;
5556 : :
5557 : 2540 : start_otd = reply->size;
5558 : 2540 : ofpbuf_put_zeros(reply, sizeof *otd);
5559 [ + - ]: 2540 : if (td->eviction_flags != UINT32_MAX) {
5560 : 2540 : ofpprop_put_u32(reply, OFPTMPT14_EVICTION, td->eviction_flags);
5561 : : }
5562 [ + + ]: 2540 : if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5563 : : struct ofp14_table_mod_prop_vacancy *otv;
5564 : :
5565 : 2 : otv = ofpprop_put_zeros(reply, OFPTMPT14_VACANCY, sizeof *otv);
5566 : 2 : otv->vacancy_down = td->table_vacancy.vacancy_down;
5567 : 2 : otv->vacancy_up = td->table_vacancy.vacancy_up;
5568 : 2 : otv->vacancy = td->table_vacancy.vacancy;
5569 : : }
5570 : :
5571 : 2540 : otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5572 : 2540 : otd->length = htons(reply->size - start_otd);
5573 : 2540 : otd->table_id = td->table_id;
5574 : 2540 : otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5575 : : td->eviction, td->vacancy,
5576 : : version);
5577 : 2540 : ofpmp_postappend(replies, start_otd);
5578 : 2540 : }
5579 : :
5580 : : /* This function parses Vacancy property, and decodes the
5581 : : * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5582 : : * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5583 : : * greater than vacancy_up and also when current vacancy has non-zero
5584 : : * value. Returns 0 on success. */
5585 : : static enum ofperr
5586 : 6 : parse_table_mod_vacancy_property(struct ofpbuf *property,
5587 : : struct ofputil_table_mod *tm)
5588 : : {
5589 : 6 : struct ofp14_table_mod_prop_vacancy *otv = property->data;
5590 : :
5591 [ - + ]: 6 : if (property->size != sizeof *otv) {
5592 : 0 : return OFPERR_OFPBPC_BAD_LEN;
5593 : : }
5594 : 6 : tm->table_vacancy.vacancy_down = otv->vacancy_down;
5595 : 6 : tm->table_vacancy.vacancy_up = otv->vacancy_up;
5596 [ - + ]: 6 : if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
5597 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false,
5598 : : "Value of vacancy_down is greater than vacancy_up");
5599 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
5600 : : }
5601 [ + - ][ - + ]: 6 : if (tm->table_vacancy.vacancy_down > 100 ||
5602 : 6 : tm->table_vacancy.vacancy_up > 100) {
5603 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false, "Vacancy threshold percentage "
5604 : : "should not be greater than 100");
5605 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
5606 : : }
5607 : 6 : tm->table_vacancy.vacancy = otv->vacancy;
5608 [ - + ]: 6 : if (tm->table_vacancy.vacancy) {
5609 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false,
5610 : : "Vacancy value should be zero for table-mod messages");
5611 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
5612 : : }
5613 : 6 : return 0;
5614 : : }
5615 : :
5616 : : /* Given 'config', taken from an OpenFlow 'version' message that specifies
5617 : : * table configuration (a table mod, table stats, or table features message),
5618 : : * returns the table vacancy configuration that it specifies.
5619 : : *
5620 : : * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5621 : : * so for other 'version' this function always returns
5622 : : * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5623 : : static enum ofputil_table_vacancy
5624 : 3333 : ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5625 : : {
5626 [ + - + + ]: 6666 : return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5627 : 3333 : : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5628 : : : OFPUTIL_TABLE_VACANCY_OFF);
5629 : : }
5630 : :
5631 : : /* Given 'config', taken from an OpenFlow 'version' message that specifies
5632 : : * table configuration (a table mod, table stats, or table features message),
5633 : : * returns the table eviction configuration that it specifies.
5634 : : *
5635 : : * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5636 : : * so for other 'version' values this function always returns
5637 : : * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5638 : : static enum ofputil_table_eviction
5639 : 3333 : ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5640 : : {
5641 [ + - + + ]: 6666 : return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5642 : 3333 : : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5643 : : : OFPUTIL_TABLE_EVICTION_OFF);
5644 : : }
5645 : :
5646 : : /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5647 : : * OpenFlow messages of the given 'version', based on the provided 'miss' and
5648 : : * 'eviction' values. */
5649 : : static ovs_be32
5650 : 3065 : ofputil_encode_table_config(enum ofputil_table_miss miss,
5651 : : enum ofputil_table_eviction eviction,
5652 : : enum ofputil_table_vacancy vacancy,
5653 : : enum ofp_version version)
5654 : : {
5655 : 3065 : uint32_t config = 0;
5656 : : /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5657 : : * information on the crazy evolution of this field. */
5658 [ - + - + : 3065 : switch (version) {
- ]
5659 : : case OFP10_VERSION:
5660 : : /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5661 : 0 : return htonl(0);
5662 : :
5663 : : case OFP11_VERSION:
5664 : : case OFP12_VERSION:
5665 : : /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5666 [ + + + ]: 515 : switch (miss) {
5667 : : case OFPUTIL_TABLE_MISS_DEFAULT:
5668 : : /* Really this shouldn't be used for encoding (the caller should
5669 : : * provide a specific value) but I can't imagine that defaulting to
5670 : : * the fall-through case here will hurt. */
5671 : : case OFPUTIL_TABLE_MISS_CONTROLLER:
5672 : : default:
5673 : 508 : return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5674 : : case OFPUTIL_TABLE_MISS_CONTINUE:
5675 : 4 : return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5676 : : case OFPUTIL_TABLE_MISS_DROP:
5677 : 3 : return htonl(OFPTC11_TABLE_MISS_DROP);
5678 : : }
5679 : : OVS_NOT_REACHED();
5680 : :
5681 : : case OFP13_VERSION:
5682 : : /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5683 : : * flags, so this is correct. */
5684 : 0 : return htonl(0);
5685 : :
5686 : : case OFP14_VERSION:
5687 : : case OFP15_VERSION:
5688 : : case OFP16_VERSION:
5689 : : /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5690 : : * OFPTC14_VACANCY_EVENTS. */
5691 [ + + ]: 2550 : if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5692 : 7 : config |= OFPTC14_EVICTION;
5693 : : }
5694 [ + + ]: 2550 : if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5695 : 8 : config |= OFPTC14_VACANCY_EVENTS;
5696 : : }
5697 : 2550 : return htonl(config);
5698 : : }
5699 : :
5700 : 0 : OVS_NOT_REACHED();
5701 : : }
5702 : :
5703 : : /* Given 'config', taken from an OpenFlow 'version' message that specifies
5704 : : * table configuration (a table mod, table stats, or table features message),
5705 : : * returns the table miss configuration that it specifies.
5706 : : *
5707 : : * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5708 : : * other 'version' values this function always returns
5709 : : * OFPUTIL_TABLE_MISS_DEFAULT. */
5710 : : static enum ofputil_table_miss
5711 : 1303 : ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5712 : : {
5713 : 1303 : uint32_t config = ntohl(config_);
5714 : :
5715 [ + + ][ + + ]: 1303 : if (version == OFP11_VERSION || version == OFP12_VERSION) {
5716 [ + + + - ]: 1287 : switch (config & OFPTC11_TABLE_MISS_MASK) {
5717 : : case OFPTC11_TABLE_MISS_CONTROLLER:
5718 : 1271 : return OFPUTIL_TABLE_MISS_CONTROLLER;
5719 : :
5720 : : case OFPTC11_TABLE_MISS_CONTINUE:
5721 : 9 : return OFPUTIL_TABLE_MISS_CONTINUE;
5722 : :
5723 : : case OFPTC11_TABLE_MISS_DROP:
5724 : 7 : return OFPUTIL_TABLE_MISS_DROP;
5725 : :
5726 : : default:
5727 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5728 : 0 : return OFPUTIL_TABLE_MISS_CONTROLLER;
5729 : : }
5730 : : } else {
5731 : 16 : return OFPUTIL_TABLE_MISS_DEFAULT;
5732 : : }
5733 : : }
5734 : :
5735 : : /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5736 : : * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5737 : : enum ofperr
5738 : 32 : ofputil_decode_table_mod(const struct ofp_header *oh,
5739 : : struct ofputil_table_mod *pm)
5740 : : {
5741 : 32 : memset(pm, 0, sizeof *pm);
5742 : 32 : pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5743 : 32 : pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5744 : 32 : pm->eviction_flags = UINT32_MAX;
5745 : 32 : pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
5746 : :
5747 : 32 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5748 : 32 : enum ofpraw raw = ofpraw_pull_assert(&b);
5749 [ + + ]: 32 : if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5750 : 17 : const struct ofp11_table_mod *otm = b.data;
5751 : :
5752 : 17 : pm->table_id = otm->table_id;
5753 : 17 : pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5754 [ + - ]: 15 : } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5755 : 15 : const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5756 : :
5757 : 15 : pm->table_id = otm->table_id;
5758 : 15 : pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5759 : 15 : pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5760 : 15 : pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
5761 [ + + ]: 29 : while (b.size > 0) {
5762 : : struct ofpbuf property;
5763 : : enum ofperr error;
5764 : : uint64_t type;
5765 : :
5766 : 14 : error = ofpprop_pull(&b, &property, &type);
5767 [ - + ]: 14 : if (error) {
5768 : 0 : return error;
5769 : : }
5770 : :
5771 [ + + - ]: 14 : switch (type) {
5772 : : case OFPTMPT14_EVICTION:
5773 : 8 : error = ofpprop_parse_u32(&property, &pm->eviction);
5774 : 8 : break;
5775 : :
5776 : : case OFPTMPT14_VACANCY:
5777 : 6 : error = parse_table_mod_vacancy_property(&property, pm);
5778 : 6 : break;
5779 : :
5780 : : default:
5781 : 0 : error = OFPERR_OFPBRC_BAD_TYPE;
5782 : 0 : break;
5783 : : }
5784 : :
5785 [ - + ]: 14 : if (error) {
5786 : 14 : return error;
5787 : : }
5788 : : }
5789 : : } else {
5790 : 0 : return OFPERR_OFPBRC_BAD_TYPE;
5791 : : }
5792 : :
5793 : 32 : return 0;
5794 : : }
5795 : :
5796 : : /* Converts the abstract form of a "table mod" message in '*tm' into an
5797 : : * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5798 : : * buffer owned by the caller. */
5799 : : struct ofpbuf *
5800 : 14 : ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5801 : : enum ofputil_protocol protocol)
5802 : : {
5803 : 14 : enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5804 : : struct ofpbuf *b;
5805 : :
5806 [ - + + - ]: 14 : switch (ofp_version) {
5807 : : case OFP10_VERSION: {
5808 : 0 : ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5809 : : "(\'-O OpenFlow11\')");
5810 : : break;
5811 : : }
5812 : : case OFP11_VERSION:
5813 : : case OFP12_VERSION:
5814 : : case OFP13_VERSION: {
5815 : : struct ofp11_table_mod *otm;
5816 : :
5817 : 7 : b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5818 : 7 : otm = ofpbuf_put_zeros(b, sizeof *otm);
5819 : 7 : otm->table_id = tm->table_id;
5820 : 7 : otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5821 : : tm->vacancy, ofp_version);
5822 : 7 : break;
5823 : : }
5824 : : case OFP14_VERSION:
5825 : : case OFP15_VERSION:
5826 : : case OFP16_VERSION: {
5827 : : struct ofp14_table_mod *otm;
5828 : :
5829 : 7 : b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5830 : 7 : otm = ofpbuf_put_zeros(b, sizeof *otm);
5831 : 7 : otm->table_id = tm->table_id;
5832 : 7 : otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5833 : : tm->vacancy, ofp_version);
5834 : :
5835 [ + + ]: 7 : if (tm->eviction_flags != UINT32_MAX) {
5836 : 4 : ofpprop_put_u32(b, OFPTMPT14_EVICTION, tm->eviction_flags);
5837 : : }
5838 [ + + ]: 7 : if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5839 : : struct ofp14_table_mod_prop_vacancy *otv;
5840 : :
5841 : 3 : otv = ofpprop_put_zeros(b, OFPTMPT14_VACANCY, sizeof *otv);
5842 : 3 : otv->vacancy_down = tm->table_vacancy.vacancy_down;
5843 : 3 : otv->vacancy_up = tm->table_vacancy.vacancy_up;
5844 : : }
5845 : 7 : break;
5846 : : }
5847 : : default:
5848 : 0 : OVS_NOT_REACHED();
5849 : : }
5850 : :
5851 : 14 : return b;
5852 : : }
5853 : :
5854 : : /* ofputil_role_request */
5855 : :
5856 : : /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5857 : : * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5858 : : * OFPERR_* value. */
5859 : : enum ofperr
5860 : 125 : ofputil_decode_role_message(const struct ofp_header *oh,
5861 : : struct ofputil_role_request *rr)
5862 : : {
5863 : 125 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5864 : 125 : enum ofpraw raw = ofpraw_pull_assert(&b);
5865 [ + + ][ + + ]: 125 : if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5866 : 113 : raw == OFPRAW_OFPT12_ROLE_REPLY) {
5867 : 113 : const struct ofp12_role_request *orr = b.msg;
5868 : :
5869 [ + + + + ]: 213 : if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5870 [ + + ]: 192 : orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5871 [ - + ]: 133 : orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5872 : 41 : orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5873 : 0 : return OFPERR_OFPRRFC_BAD_ROLE;
5874 : : }
5875 : :
5876 : 113 : rr->role = ntohl(orr->role);
5877 [ + + ][ + + ]: 185 : if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5878 : 72 : ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5879 : 41 : : orr->generation_id == OVS_BE64_MAX) {
5880 : 21 : rr->have_generation_id = false;
5881 : 21 : rr->generation_id = 0;
5882 : : } else {
5883 : 92 : rr->have_generation_id = true;
5884 : 92 : rr->generation_id = ntohll(orr->generation_id);
5885 : : }
5886 [ + + ][ + - ]: 12 : } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5887 : 12 : raw == OFPRAW_NXT_ROLE_REPLY) {
5888 : 12 : const struct nx_role_request *nrr = b.msg;
5889 : :
5890 : : BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5891 : : BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5892 : : BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5893 : :
5894 [ + - + + ]: 24 : if (nrr->role != htonl(NX_ROLE_OTHER) &&
5895 [ - + ]: 18 : nrr->role != htonl(NX_ROLE_MASTER) &&
5896 : 6 : nrr->role != htonl(NX_ROLE_SLAVE)) {
5897 : 0 : return OFPERR_OFPRRFC_BAD_ROLE;
5898 : : }
5899 : :
5900 : 12 : rr->role = ntohl(nrr->role) + 1;
5901 : 12 : rr->have_generation_id = false;
5902 : 12 : rr->generation_id = 0;
5903 : : } else {
5904 : 0 : OVS_NOT_REACHED();
5905 : : }
5906 : :
5907 : 125 : return 0;
5908 : : }
5909 : :
5910 : : /* Returns an encoded form of a role reply suitable for the "request" in a
5911 : : * buffer owned by the caller. */
5912 : : struct ofpbuf *
5913 : 22 : ofputil_encode_role_reply(const struct ofp_header *request,
5914 : : const struct ofputil_role_request *rr)
5915 : : {
5916 : : struct ofpbuf *buf;
5917 : : enum ofpraw raw;
5918 : :
5919 : 22 : raw = ofpraw_decode_assert(request);
5920 [ + + ]: 22 : if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5921 : : struct ofp12_role_request *orr;
5922 : :
5923 : 20 : buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5924 : 20 : orr = ofpbuf_put_zeros(buf, sizeof *orr);
5925 : :
5926 : 20 : orr->role = htonl(rr->role);
5927 [ + + ]: 20 : orr->generation_id = htonll(rr->have_generation_id
5928 : : ? rr->generation_id
5929 : : : UINT64_MAX);
5930 [ + - ]: 2 : } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5931 : : struct nx_role_request *nrr;
5932 : :
5933 : : BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5934 : : BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5935 : : BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5936 : :
5937 : 2 : buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5938 : 2 : nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5939 : 2 : nrr->role = htonl(rr->role - 1);
5940 : : } else {
5941 : 0 : OVS_NOT_REACHED();
5942 : : }
5943 : :
5944 : 22 : return buf;
5945 : : }
5946 : :
5947 : : /* Encodes "role status" message 'status' for sending in the given
5948 : : * 'protocol'. Returns the role status message, if 'protocol' supports them,
5949 : : * otherwise a null pointer. */
5950 : : struct ofpbuf *
5951 : 2 : ofputil_encode_role_status(const struct ofputil_role_status *status,
5952 : : enum ofputil_protocol protocol)
5953 : : {
5954 : : enum ofp_version version;
5955 : :
5956 : 2 : version = ofputil_protocol_to_ofp_version(protocol);
5957 [ + + ]: 2 : if (version >= OFP14_VERSION) {
5958 : : struct ofp14_role_status *rstatus;
5959 : : struct ofpbuf *buf;
5960 : :
5961 : 1 : buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5962 : : 0);
5963 : 1 : rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5964 : 1 : rstatus->role = htonl(status->role);
5965 : 1 : rstatus->reason = status->reason;
5966 : 1 : rstatus->generation_id = htonll(status->generation_id);
5967 : :
5968 : 1 : return buf;
5969 : : } else {
5970 : 1 : return NULL;
5971 : : }
5972 : : }
5973 : :
5974 : : enum ofperr
5975 : 5 : ofputil_decode_role_status(const struct ofp_header *oh,
5976 : : struct ofputil_role_status *rs)
5977 : : {
5978 : 5 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5979 : 5 : enum ofpraw raw = ofpraw_pull_assert(&b);
5980 [ - + ]: 5 : ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5981 : :
5982 : 5 : const struct ofp14_role_status *r = b.msg;
5983 [ + - + - ]: 10 : if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5984 [ + + ]: 10 : r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5985 [ - + ]: 7 : r->role != htonl(OFPCR12_ROLE_MASTER) &&
5986 : 2 : r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5987 : 0 : return OFPERR_OFPRRFC_BAD_ROLE;
5988 : : }
5989 : :
5990 : 5 : rs->role = ntohl(r->role);
5991 : 5 : rs->generation_id = ntohll(r->generation_id);
5992 : 5 : rs->reason = r->reason;
5993 : :
5994 : 5 : return 0;
5995 : : }
5996 : :
5997 : : /* Encodes 'rf' according to 'protocol', and returns the encoded message.
5998 : : * 'protocol' must be for OpenFlow 1.4 or later. */
5999 : : struct ofpbuf *
6000 : 4 : ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
6001 : : enum ofputil_protocol protocol)
6002 : : {
6003 : 4 : enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6004 : : struct ofpbuf *inner;
6005 : :
6006 [ + - - ]: 4 : switch (rf->reason) {
6007 : : case OFPRFR_GROUP_MOD:
6008 : 4 : inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
6009 : 4 : break;
6010 : :
6011 : : case OFPRFR_METER_MOD:
6012 : 0 : inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
6013 : 0 : break;
6014 : :
6015 : : case OFPRFR_N_REASONS:
6016 : : default:
6017 : 0 : OVS_NOT_REACHED();
6018 : : }
6019 : :
6020 : 4 : struct ofp_header *inner_oh = inner->data;
6021 : 4 : inner_oh->xid = rf->xid;
6022 : 4 : inner_oh->length = htons(inner->size);
6023 : :
6024 : 4 : struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
6025 : : ofp_version, htonl(0),
6026 : 4 : inner->size);
6027 : 4 : ofpbuf_put(outer, inner->data, inner->size);
6028 : 4 : ofpbuf_delete(inner);
6029 : :
6030 : 4 : return outer;
6031 : : }
6032 : :
6033 : : /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6034 : : * form into '*rf' and returns 0, and the caller is later responsible for
6035 : : * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6036 : : * failure, returns an ofperr and '*rf' is indeterminate. */
6037 : : enum ofperr
6038 : 12 : ofputil_decode_requestforward(const struct ofp_header *outer,
6039 : : struct ofputil_requestforward *rf)
6040 : : {
6041 : 12 : struct ofpbuf b = ofpbuf_const_initializer(outer, ntohs(outer->length));
6042 : :
6043 : : /* Skip past outer message. */
6044 : 12 : enum ofpraw outer_raw = ofpraw_pull_assert(&b);
6045 [ - + ]: 12 : ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
6046 : :
6047 : : /* Validate inner message. */
6048 [ - + ]: 12 : if (b.size < sizeof(struct ofp_header)) {
6049 : 0 : return OFPERR_OFPBFC_MSG_BAD_LEN;
6050 : : }
6051 : 12 : const struct ofp_header *inner = b.data;
6052 : 12 : unsigned int inner_len = ntohs(inner->length);
6053 [ + - ][ - + ]: 12 : if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
6054 : 0 : return OFPERR_OFPBFC_MSG_BAD_LEN;
6055 : : }
6056 [ - + ]: 12 : if (inner->version != outer->version) {
6057 : 0 : return OFPERR_OFPBRC_BAD_VERSION;
6058 : : }
6059 : :
6060 : : /* Parse inner message. */
6061 : : enum ofptype type;
6062 : 12 : enum ofperr error = ofptype_decode(&type, inner);
6063 [ - + ]: 12 : if (error) {
6064 : 0 : return error;
6065 : : }
6066 : :
6067 : 12 : rf->xid = inner->xid;
6068 [ + + ]: 12 : if (type == OFPTYPE_GROUP_MOD) {
6069 : 10 : rf->reason = OFPRFR_GROUP_MOD;
6070 : 10 : rf->group_mod = xmalloc(sizeof *rf->group_mod);
6071 : 10 : error = ofputil_decode_group_mod(inner, rf->group_mod);
6072 [ - + ]: 10 : if (error) {
6073 : 0 : free(rf->group_mod);
6074 : 0 : return error;
6075 : : }
6076 [ + - ]: 2 : } else if (type == OFPTYPE_METER_MOD) {
6077 : 2 : rf->reason = OFPRFR_METER_MOD;
6078 : 2 : rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
6079 : 2 : ofpbuf_init(&rf->bands, 64);
6080 : 2 : error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
6081 [ - + ]: 2 : if (error) {
6082 : 0 : free(rf->meter_mod);
6083 : 0 : ofpbuf_uninit(&rf->bands);
6084 : 0 : return error;
6085 : : }
6086 : : } else {
6087 : 0 : return OFPERR_OFPBFC_MSG_UNSUP;
6088 : : }
6089 : :
6090 : 12 : return 0;
6091 : : }
6092 : :
6093 : : /* Frees the content of 'rf', which should have been initialized through a
6094 : : * successful call to ofputil_decode_requestforward(). */
6095 : : void
6096 : 12 : ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
6097 : : {
6098 [ - + ]: 12 : if (!rf) {
6099 : 0 : return;
6100 : : }
6101 : :
6102 [ + + - - ]: 12 : switch (rf->reason) {
6103 : : case OFPRFR_GROUP_MOD:
6104 : 10 : ofputil_uninit_group_mod(rf->group_mod);
6105 : 10 : free(rf->group_mod);
6106 : 10 : break;
6107 : :
6108 : : case OFPRFR_METER_MOD:
6109 : 2 : ofpbuf_uninit(&rf->bands);
6110 : 2 : free(rf->meter_mod);
6111 : 2 : break;
6112 : :
6113 : : case OFPRFR_N_REASONS:
6114 : 0 : OVS_NOT_REACHED();
6115 : : }
6116 : : }
6117 : :
6118 : : /* Table stats. */
6119 : :
6120 : : /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6121 : : * matched and a field that must be wildcarded. This function returns a bitmap
6122 : : * that contains both kinds of fields. */
6123 : : static struct mf_bitmap
6124 : 762 : wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
6125 : : {
6126 : 762 : struct mf_bitmap wc = features->match;
6127 : 762 : bitmap_not(wc.bm, MFF_N_IDS);
6128 : 762 : bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
6129 : 762 : return wc;
6130 : : }
6131 : :
6132 : : struct ofp10_wc_map {
6133 : : enum ofp10_flow_wildcards wc10;
6134 : : enum mf_field_id mf;
6135 : : };
6136 : :
6137 : : static const struct ofp10_wc_map ofp10_wc_map[] = {
6138 : : { OFPFW10_IN_PORT, MFF_IN_PORT },
6139 : : { OFPFW10_DL_VLAN, MFF_VLAN_VID },
6140 : : { OFPFW10_DL_SRC, MFF_ETH_SRC },
6141 : : { OFPFW10_DL_DST, MFF_ETH_DST},
6142 : : { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
6143 : : { OFPFW10_NW_PROTO, MFF_IP_PROTO },
6144 : : { OFPFW10_TP_SRC, MFF_TCP_SRC },
6145 : : { OFPFW10_TP_DST, MFF_TCP_DST },
6146 : : { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
6147 : : { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
6148 : : { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
6149 : : { OFPFW10_NW_TOS, MFF_IP_DSCP },
6150 : : };
6151 : :
6152 : : static ovs_be32
6153 : 762 : mf_bitmap_to_of10(const struct mf_bitmap *fields)
6154 : : {
6155 : : const struct ofp10_wc_map *p;
6156 : 762 : uint32_t wc10 = 0;
6157 : :
6158 [ + + ]: 9906 : for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6159 [ + - ]: 9144 : if (bitmap_is_set(fields->bm, p->mf)) {
6160 : 9144 : wc10 |= p->wc10;
6161 : : }
6162 : : }
6163 : 762 : return htonl(wc10);
6164 : : }
6165 : :
6166 : : static struct mf_bitmap
6167 : 1525 : mf_bitmap_from_of10(ovs_be32 wc10_)
6168 : : {
6169 : 1525 : struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6170 : : const struct ofp10_wc_map *p;
6171 : 1525 : uint32_t wc10 = ntohl(wc10_);
6172 : :
6173 [ + + ]: 19825 : for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
6174 [ + - ]: 18300 : if (wc10 & p->wc10) {
6175 : 18300 : bitmap_set1(fields.bm, p->mf);
6176 : : }
6177 : : }
6178 : 1525 : return fields;
6179 : : }
6180 : :
6181 : : static void
6182 : 762 : ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
6183 : : const struct ofputil_table_features *features,
6184 : : struct ofpbuf *buf)
6185 : : {
6186 : 762 : struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
6187 : : struct ofp10_table_stats *out;
6188 : :
6189 : 762 : out = ofpbuf_put_zeros(buf, sizeof *out);
6190 : 762 : out->table_id = features->table_id;
6191 : 762 : ovs_strlcpy(out->name, features->name, sizeof out->name);
6192 : 762 : out->wildcards = mf_bitmap_to_of10(&wc);
6193 : 762 : out->max_entries = htonl(features->max_entries);
6194 : 762 : out->active_count = htonl(stats->active_count);
6195 : 762 : put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
6196 : 762 : put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
6197 : 762 : }
6198 : :
6199 : : struct ofp11_wc_map {
6200 : : enum ofp11_flow_match_fields wc11;
6201 : : enum mf_field_id mf;
6202 : : };
6203 : :
6204 : : static const struct ofp11_wc_map ofp11_wc_map[] = {
6205 : : { OFPFMF11_IN_PORT, MFF_IN_PORT },
6206 : : { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
6207 : : { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
6208 : : { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
6209 : : { OFPFMF11_NW_TOS, MFF_IP_DSCP },
6210 : : { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
6211 : : { OFPFMF11_TP_SRC, MFF_TCP_SRC },
6212 : : { OFPFMF11_TP_DST, MFF_TCP_DST },
6213 : : { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
6214 : : { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
6215 : : /* I don't know what OFPFMF11_TYPE means. */
6216 : : { OFPFMF11_DL_SRC, MFF_ETH_SRC },
6217 : : { OFPFMF11_DL_DST, MFF_ETH_DST },
6218 : : { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
6219 : : { OFPFMF11_NW_DST, MFF_IPV4_DST },
6220 : : { OFPFMF11_METADATA, MFF_METADATA },
6221 : : };
6222 : :
6223 : : static ovs_be32
6224 : 0 : mf_bitmap_to_of11(const struct mf_bitmap *fields)
6225 : : {
6226 : : const struct ofp11_wc_map *p;
6227 : 0 : uint32_t wc11 = 0;
6228 : :
6229 [ # # ]: 0 : for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6230 [ # # ]: 0 : if (bitmap_is_set(fields->bm, p->mf)) {
6231 : 0 : wc11 |= p->wc11;
6232 : : }
6233 : : }
6234 : 0 : return htonl(wc11);
6235 : : }
6236 : :
6237 : : static struct mf_bitmap
6238 : 0 : mf_bitmap_from_of11(ovs_be32 wc11_)
6239 : : {
6240 : 0 : struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
6241 : : const struct ofp11_wc_map *p;
6242 : 0 : uint32_t wc11 = ntohl(wc11_);
6243 : :
6244 [ # # ]: 0 : for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
6245 [ # # ]: 0 : if (wc11 & p->wc11) {
6246 : 0 : bitmap_set1(fields.bm, p->mf);
6247 : : }
6248 : : }
6249 : 0 : return fields;
6250 : : }
6251 : :
6252 : : static void
6253 : 0 : ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
6254 : : const struct ofputil_table_features *features,
6255 : : struct ofpbuf *buf)
6256 : : {
6257 : 0 : struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
6258 : : struct ofp11_table_stats *out;
6259 : :
6260 : 0 : out = ofpbuf_put_zeros(buf, sizeof *out);
6261 : 0 : out->table_id = features->table_id;
6262 : 0 : ovs_strlcpy(out->name, features->name, sizeof out->name);
6263 : 0 : out->wildcards = mf_bitmap_to_of11(&wc);
6264 : 0 : out->match = mf_bitmap_to_of11(&features->match);
6265 : 0 : out->instructions = ovsinst_bitmap_to_openflow(
6266 : : features->nonmiss.instructions, OFP11_VERSION);
6267 : 0 : out->write_actions = ofpact_bitmap_to_openflow(
6268 : : features->nonmiss.write.ofpacts, OFP11_VERSION);
6269 : 0 : out->apply_actions = ofpact_bitmap_to_openflow(
6270 : : features->nonmiss.apply.ofpacts, OFP11_VERSION);
6271 : 0 : out->config = htonl(features->miss_config);
6272 : 0 : out->max_entries = htonl(features->max_entries);
6273 : 0 : out->active_count = htonl(stats->active_count);
6274 : 0 : out->lookup_count = htonll(stats->lookup_count);
6275 : 0 : out->matched_count = htonll(stats->matched_count);
6276 : 0 : }
6277 : :
6278 : : static void
6279 : 508 : ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
6280 : : const struct ofputil_table_features *features,
6281 : : struct ofpbuf *buf)
6282 : : {
6283 : : struct ofp12_table_stats *out;
6284 : :
6285 : 508 : out = ofpbuf_put_zeros(buf, sizeof *out);
6286 : 508 : out->table_id = features->table_id;
6287 : 508 : ovs_strlcpy(out->name, features->name, sizeof out->name);
6288 : 508 : out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
6289 : 508 : out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
6290 : : OFP12_VERSION);
6291 : 508 : out->write_actions = ofpact_bitmap_to_openflow(
6292 : : features->nonmiss.write.ofpacts, OFP12_VERSION);
6293 : 508 : out->apply_actions = ofpact_bitmap_to_openflow(
6294 : : features->nonmiss.apply.ofpacts, OFP12_VERSION);
6295 : 508 : out->write_setfields = oxm_bitmap_from_mf_bitmap(
6296 : : &features->nonmiss.write.set_fields, OFP12_VERSION);
6297 : 508 : out->apply_setfields = oxm_bitmap_from_mf_bitmap(
6298 : : &features->nonmiss.apply.set_fields, OFP12_VERSION);
6299 : 508 : out->metadata_match = features->metadata_match;
6300 : 508 : out->metadata_write = features->metadata_write;
6301 : 508 : out->instructions = ovsinst_bitmap_to_openflow(
6302 : : features->nonmiss.instructions, OFP12_VERSION);
6303 : 508 : out->config = ofputil_encode_table_config(features->miss_config,
6304 : : OFPUTIL_TABLE_EVICTION_DEFAULT,
6305 : : OFPUTIL_TABLE_VACANCY_DEFAULT,
6306 : : OFP12_VERSION);
6307 : 508 : out->max_entries = htonl(features->max_entries);
6308 : 508 : out->active_count = htonl(stats->active_count);
6309 : 508 : out->lookup_count = htonll(stats->lookup_count);
6310 : 508 : out->matched_count = htonll(stats->matched_count);
6311 : 508 : }
6312 : :
6313 : : static void
6314 : 1270 : ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
6315 : : struct ofpbuf *buf)
6316 : : {
6317 : : struct ofp13_table_stats *out;
6318 : :
6319 : 1270 : out = ofpbuf_put_zeros(buf, sizeof *out);
6320 : 1270 : out->table_id = stats->table_id;
6321 : 1270 : out->active_count = htonl(stats->active_count);
6322 : 1270 : out->lookup_count = htonll(stats->lookup_count);
6323 : 1270 : out->matched_count = htonll(stats->matched_count);
6324 : 1270 : }
6325 : :
6326 : : struct ofpbuf *
6327 : 10 : ofputil_encode_table_stats_reply(const struct ofp_header *request)
6328 : : {
6329 : 10 : return ofpraw_alloc_stats_reply(request, 0);
6330 : : }
6331 : :
6332 : : void
6333 : 2540 : ofputil_append_table_stats_reply(struct ofpbuf *reply,
6334 : : const struct ofputil_table_stats *stats,
6335 : : const struct ofputil_table_features *features)
6336 : : {
6337 : 2540 : struct ofp_header *oh = reply->header;
6338 : :
6339 [ - + ]: 2540 : ovs_assert(stats->table_id == features->table_id);
6340 : :
6341 [ + - + + : 2540 : switch ((enum ofp_version) oh->version) {
- ]
6342 : : case OFP10_VERSION:
6343 : 762 : ofputil_put_ofp10_table_stats(stats, features, reply);
6344 : 762 : break;
6345 : :
6346 : : case OFP11_VERSION:
6347 : 0 : ofputil_put_ofp11_table_stats(stats, features, reply);
6348 : 0 : break;
6349 : :
6350 : : case OFP12_VERSION:
6351 : 508 : ofputil_put_ofp12_table_stats(stats, features, reply);
6352 : 508 : break;
6353 : :
6354 : : case OFP13_VERSION:
6355 : : case OFP14_VERSION:
6356 : : case OFP15_VERSION:
6357 : : case OFP16_VERSION:
6358 : 1270 : ofputil_put_ofp13_table_stats(stats, reply);
6359 : 1270 : break;
6360 : :
6361 : : default:
6362 : 0 : OVS_NOT_REACHED();
6363 : : }
6364 : 2540 : }
6365 : :
6366 : : static int
6367 : 1525 : ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
6368 : : struct ofputil_table_stats *stats,
6369 : : struct ofputil_table_features *features)
6370 : : {
6371 : : struct ofp10_table_stats *ots;
6372 : :
6373 : 1525 : ots = ofpbuf_try_pull(msg, sizeof *ots);
6374 [ - + ]: 1525 : if (!ots) {
6375 : 0 : return OFPERR_OFPBRC_BAD_LEN;
6376 : : }
6377 : :
6378 : 1525 : features->table_id = ots->table_id;
6379 : 1525 : ovs_strlcpy(features->name, ots->name, sizeof features->name);
6380 : 1525 : features->max_entries = ntohl(ots->max_entries);
6381 : 1525 : features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
6382 : :
6383 : 1525 : stats->table_id = ots->table_id;
6384 : 1525 : stats->active_count = ntohl(ots->active_count);
6385 : 1525 : stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
6386 : 1525 : stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
6387 : :
6388 : 1525 : return 0;
6389 : : }
6390 : :
6391 : : static int
6392 : 0 : ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
6393 : : struct ofputil_table_stats *stats,
6394 : : struct ofputil_table_features *features)
6395 : : {
6396 : : struct ofp11_table_stats *ots;
6397 : :
6398 : 0 : ots = ofpbuf_try_pull(msg, sizeof *ots);
6399 [ # # ]: 0 : if (!ots) {
6400 : 0 : return OFPERR_OFPBRC_BAD_LEN;
6401 : : }
6402 : :
6403 : 0 : features->table_id = ots->table_id;
6404 : 0 : ovs_strlcpy(features->name, ots->name, sizeof features->name);
6405 : 0 : features->max_entries = ntohl(ots->max_entries);
6406 : 0 : features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6407 : : ots->instructions, OFP11_VERSION);
6408 : 0 : features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6409 : : ots->write_actions, OFP11_VERSION);
6410 : 0 : features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6411 : : ots->write_actions, OFP11_VERSION);
6412 : 0 : features->miss = features->nonmiss;
6413 : 0 : features->miss_config = ofputil_decode_table_miss(ots->config,
6414 : : OFP11_VERSION);
6415 : 0 : features->match = mf_bitmap_from_of11(ots->match);
6416 : 0 : features->wildcard = mf_bitmap_from_of11(ots->wildcards);
6417 : 0 : bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6418 : :
6419 : 0 : stats->table_id = ots->table_id;
6420 : 0 : stats->active_count = ntohl(ots->active_count);
6421 : 0 : stats->lookup_count = ntohll(ots->lookup_count);
6422 : 0 : stats->matched_count = ntohll(ots->matched_count);
6423 : :
6424 : 0 : return 0;
6425 : : }
6426 : :
6427 : : static int
6428 : 1271 : ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
6429 : : struct ofputil_table_stats *stats,
6430 : : struct ofputil_table_features *features)
6431 : : {
6432 : : struct ofp12_table_stats *ots;
6433 : :
6434 : 1271 : ots = ofpbuf_try_pull(msg, sizeof *ots);
6435 [ - + ]: 1271 : if (!ots) {
6436 : 0 : return OFPERR_OFPBRC_BAD_LEN;
6437 : : }
6438 : :
6439 : 1271 : features->table_id = ots->table_id;
6440 : 1271 : ovs_strlcpy(features->name, ots->name, sizeof features->name);
6441 : 1271 : features->metadata_match = ots->metadata_match;
6442 : 1271 : features->metadata_write = ots->metadata_write;
6443 : 1271 : features->miss_config = ofputil_decode_table_miss(ots->config,
6444 : : OFP12_VERSION);
6445 : 1271 : features->max_entries = ntohl(ots->max_entries);
6446 : :
6447 : 1271 : features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6448 : : ots->instructions, OFP12_VERSION);
6449 : 1271 : features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6450 : : ots->write_actions, OFP12_VERSION);
6451 : 1271 : features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6452 : : ots->apply_actions, OFP12_VERSION);
6453 : 1271 : features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
6454 : : ots->write_setfields, OFP12_VERSION);
6455 : 1271 : features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
6456 : : ots->apply_setfields, OFP12_VERSION);
6457 : 1271 : features->miss = features->nonmiss;
6458 : :
6459 : 1271 : features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
6460 : 1271 : features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
6461 : : OFP12_VERSION);
6462 : 1271 : bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6463 : :
6464 : 1271 : stats->table_id = ots->table_id;
6465 : 1271 : stats->active_count = ntohl(ots->active_count);
6466 : 1271 : stats->lookup_count = ntohll(ots->lookup_count);
6467 : 1271 : stats->matched_count = ntohll(ots->matched_count);
6468 : :
6469 : 1271 : return 0;
6470 : : }
6471 : :
6472 : : static int
6473 : 2542 : ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
6474 : : struct ofputil_table_stats *stats,
6475 : : struct ofputil_table_features *features)
6476 : : {
6477 : : struct ofp13_table_stats *ots;
6478 : :
6479 : 2542 : ots = ofpbuf_try_pull(msg, sizeof *ots);
6480 [ - + ]: 2542 : if (!ots) {
6481 : 0 : return OFPERR_OFPBRC_BAD_LEN;
6482 : : }
6483 : :
6484 : 2542 : features->table_id = ots->table_id;
6485 : :
6486 : 2542 : stats->table_id = ots->table_id;
6487 : 2542 : stats->active_count = ntohl(ots->active_count);
6488 : 2542 : stats->lookup_count = ntohll(ots->lookup_count);
6489 : 2542 : stats->matched_count = ntohll(ots->matched_count);
6490 : :
6491 : 2542 : return 0;
6492 : : }
6493 : :
6494 : : int
6495 : 5361 : ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6496 : : struct ofputil_table_stats *stats,
6497 : : struct ofputil_table_features *features)
6498 : : {
6499 : : const struct ofp_header *oh;
6500 : :
6501 [ - + ]: 5361 : if (!msg->header) {
6502 : 0 : ofpraw_pull_assert(msg);
6503 : : }
6504 : 5361 : oh = msg->header;
6505 : :
6506 [ + + ]: 5361 : if (!msg->size) {
6507 : 23 : return EOF;
6508 : : }
6509 : :
6510 : 5338 : memset(stats, 0, sizeof *stats);
6511 : 5338 : memset(features, 0, sizeof *features);
6512 : 5338 : features->supports_eviction = -1;
6513 : 5338 : features->supports_vacancy_events = -1;
6514 : :
6515 [ + - + + : 5338 : switch ((enum ofp_version) oh->version) {
- ]
6516 : : case OFP10_VERSION:
6517 : 1525 : return ofputil_decode_ofp10_table_stats(msg, stats, features);
6518 : :
6519 : : case OFP11_VERSION:
6520 : 0 : return ofputil_decode_ofp11_table_stats(msg, stats, features);
6521 : :
6522 : : case OFP12_VERSION:
6523 : 1271 : return ofputil_decode_ofp12_table_stats(msg, stats, features);
6524 : :
6525 : : case OFP13_VERSION:
6526 : : case OFP14_VERSION:
6527 : : case OFP15_VERSION:
6528 : : case OFP16_VERSION:
6529 : 2542 : return ofputil_decode_ofp13_table_stats(msg, stats, features);
6530 : :
6531 : : default:
6532 : 0 : OVS_NOT_REACHED();
6533 : : }
6534 : : }
6535 : :
6536 : : /* ofputil_flow_monitor_request */
6537 : :
6538 : : /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6539 : : * ofputil_flow_monitor_request in 'rq'.
6540 : : *
6541 : : * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6542 : : * message. Calling this function multiple times for a single 'msg' iterates
6543 : : * through the requests. The caller must initially leave 'msg''s layer
6544 : : * pointers null and not modify them between calls.
6545 : : *
6546 : : * Returns 0 if successful, EOF if no requests were left in this 'msg',
6547 : : * otherwise an OFPERR_* value. */
6548 : : int
6549 : 19 : ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6550 : : struct ofpbuf *msg)
6551 : : {
6552 : : struct nx_flow_monitor_request *nfmr;
6553 : : uint16_t flags;
6554 : :
6555 [ + + ]: 19 : if (!msg->header) {
6556 : 9 : ofpraw_pull_assert(msg);
6557 : : }
6558 : :
6559 [ + + ]: 19 : if (!msg->size) {
6560 : 9 : return EOF;
6561 : : }
6562 : :
6563 : 10 : nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6564 [ - + ]: 10 : if (!nfmr) {
6565 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6566 : : "leftover bytes at end", msg->size);
6567 : 0 : return OFPERR_OFPBRC_BAD_LEN;
6568 : : }
6569 : :
6570 : 10 : flags = ntohs(nfmr->flags);
6571 [ + - ]: 10 : if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6572 [ - + ]: 10 : || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6573 : : | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6574 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6575 : : flags);
6576 : 0 : return OFPERR_OFPMOFC_BAD_FLAGS;
6577 : : }
6578 : :
6579 [ - + ]: 10 : if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6580 : 0 : return OFPERR_NXBRC_MUST_BE_ZERO;
6581 : : }
6582 : :
6583 : 10 : rq->id = ntohl(nfmr->id);
6584 : 10 : rq->flags = flags;
6585 : 10 : rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
6586 : 10 : rq->table_id = nfmr->table_id;
6587 : :
6588 : 10 : return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
6589 : : }
6590 : :
6591 : : void
6592 : 4 : ofputil_append_flow_monitor_request(
6593 : : const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6594 : : {
6595 : : struct nx_flow_monitor_request *nfmr;
6596 : : size_t start_ofs;
6597 : : int match_len;
6598 : :
6599 [ + - ]: 4 : if (!msg->size) {
6600 : 4 : ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
6601 : : }
6602 : :
6603 : 4 : start_ofs = msg->size;
6604 : 4 : ofpbuf_put_zeros(msg, sizeof *nfmr);
6605 : 4 : match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
6606 : :
6607 : 4 : nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6608 : 4 : nfmr->id = htonl(rq->id);
6609 : 4 : nfmr->flags = htons(rq->flags);
6610 : 4 : nfmr->out_port = htons(ofp_to_u16(rq->out_port));
6611 : 4 : nfmr->match_len = htons(match_len);
6612 : 4 : nfmr->table_id = rq->table_id;
6613 : 4 : }
6614 : :
6615 : : /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6616 : : * into an abstract ofputil_flow_update in 'update'. The caller must have
6617 : : * initialized update->match to point to space allocated for a match.
6618 : : *
6619 : : * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6620 : : * actions (except for NXFME_ABBREV, which never includes actions). The caller
6621 : : * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6622 : : * will point into the 'ofpacts' buffer.
6623 : : *
6624 : : * Multiple flow updates can be packed into a single OpenFlow message. Calling
6625 : : * this function multiple times for a single 'msg' iterates through the
6626 : : * updates. The caller must initially leave 'msg''s layer pointers null and
6627 : : * not modify them between calls.
6628 : : *
6629 : : * Returns 0 if successful, EOF if no updates were left in this 'msg',
6630 : : * otherwise an OFPERR_* value. */
6631 : : int
6632 : 41037 : ofputil_decode_flow_update(struct ofputil_flow_update *update,
6633 : : struct ofpbuf *msg, struct ofpbuf *ofpacts)
6634 : : {
6635 : : struct nx_flow_update_header *nfuh;
6636 : : unsigned int length;
6637 : : struct ofp_header *oh;
6638 : :
6639 [ + + ]: 41037 : if (!msg->header) {
6640 : 17884 : ofpraw_pull_assert(msg);
6641 : : }
6642 : :
6643 : 41037 : ofpbuf_clear(ofpacts);
6644 [ + + ]: 41037 : if (!msg->size) {
6645 : 17884 : return EOF;
6646 : : }
6647 : :
6648 [ - + ]: 23153 : if (msg->size < sizeof(struct nx_flow_update_header)) {
6649 : 0 : goto bad_len;
6650 : : }
6651 : :
6652 : 23153 : oh = msg->header;
6653 : :
6654 : 23153 : nfuh = msg->data;
6655 : 23153 : update->event = ntohs(nfuh->event);
6656 : 23153 : length = ntohs(nfuh->length);
6657 [ + - ][ + - ]: 23153 : if (length > msg->size || length % 8) {
6658 : : goto bad_len;
6659 : : }
6660 : :
6661 [ + + ]: 23153 : if (update->event == NXFME_ABBREV) {
6662 : : struct nx_flow_update_abbrev *nfua;
6663 : :
6664 [ - + ]: 3 : if (length != sizeof *nfua) {
6665 : 0 : goto bad_len;
6666 : : }
6667 : :
6668 : 3 : nfua = ofpbuf_pull(msg, sizeof *nfua);
6669 : 3 : update->xid = nfua->xid;
6670 : 3 : return 0;
6671 [ + + ]: 23150 : } else if (update->event == NXFME_ADDED
6672 [ + + ]: 5293 : || update->event == NXFME_DELETED
6673 [ + - ]: 22 : || update->event == NXFME_MODIFIED) {
6674 : : struct nx_flow_update_full *nfuf;
6675 : : unsigned int actions_len;
6676 : : unsigned int match_len;
6677 : : enum ofperr error;
6678 : :
6679 [ - + ]: 23150 : if (length < sizeof *nfuf) {
6680 : 0 : goto bad_len;
6681 : : }
6682 : :
6683 : 23150 : nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6684 : 23150 : match_len = ntohs(nfuf->match_len);
6685 [ - + ]: 23150 : if (sizeof *nfuf + match_len > length) {
6686 : 0 : goto bad_len;
6687 : : }
6688 : :
6689 : 23150 : update->reason = ntohs(nfuf->reason);
6690 : 23150 : update->idle_timeout = ntohs(nfuf->idle_timeout);
6691 : 23150 : update->hard_timeout = ntohs(nfuf->hard_timeout);
6692 : 23150 : update->table_id = nfuf->table_id;
6693 : 23150 : update->cookie = nfuf->cookie;
6694 : 23150 : update->priority = ntohs(nfuf->priority);
6695 : :
6696 : 23150 : error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
6697 [ - + ]: 23150 : if (error) {
6698 : 0 : return error;
6699 : : }
6700 : :
6701 : 23150 : actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
6702 : 23150 : error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
6703 : : ofpacts);
6704 [ - + ]: 23150 : if (error) {
6705 : 0 : return error;
6706 : : }
6707 : :
6708 : 23150 : update->ofpacts = ofpacts->data;
6709 : 23150 : update->ofpacts_len = ofpacts->size;
6710 : 23150 : return 0;
6711 : : } else {
6712 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
6713 : : "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6714 : : ntohs(nfuh->event));
6715 : 0 : return OFPERR_NXBRC_FM_BAD_EVENT;
6716 : : }
6717 : :
6718 : : bad_len:
6719 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6720 : : "leftover bytes at end", msg->size);
6721 : 0 : return OFPERR_OFPBRC_BAD_LEN;
6722 : : }
6723 : :
6724 : : uint32_t
6725 : 1 : ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6726 : : {
6727 : 1 : const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6728 : :
6729 : 1 : return ntohl(cancel->id);
6730 : : }
6731 : :
6732 : : struct ofpbuf *
6733 : 0 : ofputil_encode_flow_monitor_cancel(uint32_t id)
6734 : : {
6735 : : struct nx_flow_monitor_cancel *nfmc;
6736 : : struct ofpbuf *msg;
6737 : :
6738 : 0 : msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6739 : 0 : nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
6740 : 0 : nfmc->id = htonl(id);
6741 : 0 : return msg;
6742 : : }
6743 : :
6744 : : void
6745 : 2656 : ofputil_start_flow_update(struct ovs_list *replies)
6746 : : {
6747 : : struct ofpbuf *msg;
6748 : :
6749 : 2656 : msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6750 : : htonl(0), 1024);
6751 : :
6752 : 2656 : ovs_list_init(replies);
6753 : 2656 : ovs_list_push_back(replies, &msg->list_node);
6754 : 2656 : }
6755 : :
6756 : : void
6757 : 5295 : ofputil_append_flow_update(const struct ofputil_flow_update *update,
6758 : : struct ovs_list *replies)
6759 : : {
6760 : 5295 : enum ofp_version version = ofpmp_version(replies);
6761 : : struct nx_flow_update_header *nfuh;
6762 : : struct ofpbuf *msg;
6763 : : size_t start_ofs;
6764 : :
6765 : 5295 : msg = ofpbuf_from_list(ovs_list_back(replies));
6766 : 5295 : start_ofs = msg->size;
6767 : :
6768 [ + + ]: 5295 : if (update->event == NXFME_ABBREV) {
6769 : : struct nx_flow_update_abbrev *nfua;
6770 : :
6771 : 1 : nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6772 : 1 : nfua->xid = update->xid;
6773 : : } else {
6774 : : struct nx_flow_update_full *nfuf;
6775 : : int match_len;
6776 : :
6777 : 5294 : ofpbuf_put_zeros(msg, sizeof *nfuf);
6778 : 5294 : match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6779 : 5294 : ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6780 : : version);
6781 : 5294 : nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6782 : 5294 : nfuf->reason = htons(update->reason);
6783 : 5294 : nfuf->priority = htons(update->priority);
6784 : 5294 : nfuf->idle_timeout = htons(update->idle_timeout);
6785 : 5294 : nfuf->hard_timeout = htons(update->hard_timeout);
6786 : 5294 : nfuf->match_len = htons(match_len);
6787 : 5294 : nfuf->table_id = update->table_id;
6788 : 5294 : nfuf->cookie = update->cookie;
6789 : : }
6790 : :
6791 : 5295 : nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6792 : 5295 : nfuh->length = htons(msg->size - start_ofs);
6793 : 5295 : nfuh->event = htons(update->event);
6794 : :
6795 : 5295 : ofpmp_postappend(replies, start_ofs);
6796 : 5295 : }
6797 : :
6798 : : struct ofpbuf *
6799 : 1562 : ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6800 : : enum ofputil_protocol protocol)
6801 : : {
6802 : 1562 : enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6803 : : struct ofpbuf *msg;
6804 : : size_t size;
6805 : :
6806 : 1562 : size = po->ofpacts_len;
6807 [ + - ]: 1562 : if (po->buffer_id == UINT32_MAX) {
6808 : 1562 : size += po->packet_len;
6809 : : }
6810 : :
6811 [ + + - ]: 1562 : switch (ofp_version) {
6812 : : case OFP10_VERSION: {
6813 : : struct ofp10_packet_out *opo;
6814 : : size_t actions_ofs;
6815 : :
6816 : 1256 : msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6817 : 1256 : ofpbuf_put_zeros(msg, sizeof *opo);
6818 : 1256 : actions_ofs = msg->size;
6819 : 1256 : ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6820 : : ofp_version);
6821 : :
6822 : 1256 : opo = msg->msg;
6823 : 1256 : opo->buffer_id = htonl(po->buffer_id);
6824 : 1256 : opo->in_port = htons(ofp_to_u16(po->in_port));
6825 : 1256 : opo->actions_len = htons(msg->size - actions_ofs);
6826 : 1256 : break;
6827 : : }
6828 : :
6829 : : case OFP11_VERSION:
6830 : : case OFP12_VERSION:
6831 : : case OFP13_VERSION:
6832 : : case OFP14_VERSION:
6833 : : case OFP15_VERSION:
6834 : : case OFP16_VERSION: {
6835 : : struct ofp11_packet_out *opo;
6836 : : size_t len;
6837 : :
6838 : 306 : msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6839 : 306 : ofpbuf_put_zeros(msg, sizeof *opo);
6840 : 306 : len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6841 : : ofp_version);
6842 : 306 : opo = msg->msg;
6843 : 306 : opo->buffer_id = htonl(po->buffer_id);
6844 : 306 : opo->in_port = ofputil_port_to_ofp11(po->in_port);
6845 : 306 : opo->actions_len = htons(len);
6846 : 306 : break;
6847 : : }
6848 : :
6849 : : default:
6850 : 0 : OVS_NOT_REACHED();
6851 : : }
6852 : :
6853 [ + - ]: 1562 : if (po->buffer_id == UINT32_MAX) {
6854 : 1562 : ofpbuf_put(msg, po->packet, po->packet_len);
6855 : : }
6856 : :
6857 : 1562 : ofpmsg_update_length(msg);
6858 : :
6859 : 1562 : return msg;
6860 : : }
6861 : :
6862 : : /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6863 : : struct ofpbuf *
6864 : 14 : make_echo_request(enum ofp_version ofp_version)
6865 : : {
6866 : 14 : return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6867 : : htonl(0), 0);
6868 : : }
6869 : :
6870 : : /* Creates and returns an OFPT_ECHO_REPLY message matching the
6871 : : * OFPT_ECHO_REQUEST message in 'rq'. */
6872 : : struct ofpbuf *
6873 : 14 : make_echo_reply(const struct ofp_header *rq)
6874 : : {
6875 : 14 : struct ofpbuf rq_buf = ofpbuf_const_initializer(rq, ntohs(rq->length));
6876 : 14 : ofpraw_pull_assert(&rq_buf);
6877 : :
6878 : 14 : struct ofpbuf *reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY,
6879 : 14 : rq, rq_buf.size);
6880 : 14 : ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6881 : 14 : return reply;
6882 : : }
6883 : :
6884 : : struct ofpbuf *
6885 : 15093 : ofputil_encode_barrier_request(enum ofp_version ofp_version)
6886 : : {
6887 : : enum ofpraw type;
6888 : :
6889 [ + + - ]: 15093 : switch (ofp_version) {
6890 : : case OFP16_VERSION:
6891 : : case OFP15_VERSION:
6892 : : case OFP14_VERSION:
6893 : : case OFP13_VERSION:
6894 : : case OFP12_VERSION:
6895 : : case OFP11_VERSION:
6896 : 2322 : type = OFPRAW_OFPT11_BARRIER_REQUEST;
6897 : 2322 : break;
6898 : :
6899 : : case OFP10_VERSION:
6900 : 12771 : type = OFPRAW_OFPT10_BARRIER_REQUEST;
6901 : 12771 : break;
6902 : :
6903 : : default:
6904 : 0 : OVS_NOT_REACHED();
6905 : : }
6906 : :
6907 : 15093 : return ofpraw_alloc(type, ofp_version, 0);
6908 : : }
6909 : :
6910 : : const char *
6911 : 509 : ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
6912 : : {
6913 [ + + + + : 509 : switch (frag) {
- ]
6914 : 498 : case OFPUTIL_FRAG_NORMAL: return "normal";
6915 : 6 : case OFPUTIL_FRAG_DROP: return "drop";
6916 : 1 : case OFPUTIL_FRAG_REASM: return "reassemble";
6917 : 4 : case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
6918 : : }
6919 : :
6920 : 0 : OVS_NOT_REACHED();
6921 : : }
6922 : :
6923 : : bool
6924 : 9 : ofputil_frag_handling_from_string(const char *s,
6925 : : enum ofputil_frag_handling *frag)
6926 : : {
6927 [ + + ]: 9 : if (!strcasecmp(s, "normal")) {
6928 : 5 : *frag = OFPUTIL_FRAG_NORMAL;
6929 [ + + ]: 4 : } else if (!strcasecmp(s, "drop")) {
6930 : 2 : *frag = OFPUTIL_FRAG_DROP;
6931 [ - + ]: 2 : } else if (!strcasecmp(s, "reassemble")) {
6932 : 0 : *frag = OFPUTIL_FRAG_REASM;
6933 [ + - ]: 2 : } else if (!strcasecmp(s, "nx-match")) {
6934 : 2 : *frag = OFPUTIL_FRAG_NX_MATCH;
6935 : : } else {
6936 : 0 : return false;
6937 : : }
6938 : 9 : return true;
6939 : : }
6940 : :
6941 : : /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6942 : : * port number and stores the latter in '*ofp10_port', for the purpose of
6943 : : * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6944 : : * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6945 : : *
6946 : : * See the definition of OFP11_MAX for an explanation of the mapping. */
6947 : : enum ofperr
6948 : 63441 : ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6949 : : {
6950 : 63441 : uint32_t ofp11_port_h = ntohl(ofp11_port);
6951 : :
6952 [ + + ]: 63441 : if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6953 : 15512 : *ofp10_port = u16_to_ofp(ofp11_port_h);
6954 : 15512 : return 0;
6955 [ + + ]: 47929 : } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6956 : 47928 : *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6957 : 47928 : return 0;
6958 : : } else {
6959 : 1 : *ofp10_port = OFPP_NONE;
6960 [ + - ]: 1 : VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6961 : : "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6962 : : ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6963 : : ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6964 : 1 : return OFPERR_OFPBAC_BAD_OUT_PORT;
6965 : : }
6966 : : }
6967 : :
6968 : : /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6969 : : * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6970 : : *
6971 : : * See the definition of OFP11_MAX for an explanation of the mapping. */
6972 : : ovs_be32
6973 : 29179 : ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6974 : : {
6975 [ + + ]: 58358 : return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6976 : 4987 : ? ofp_to_u16(ofp10_port)
6977 : 24192 : : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6978 : : }
6979 : :
6980 : : #define OFPUTIL_NAMED_PORTS \
6981 : : OFPUTIL_NAMED_PORT(IN_PORT) \
6982 : : OFPUTIL_NAMED_PORT(TABLE) \
6983 : : OFPUTIL_NAMED_PORT(NORMAL) \
6984 : : OFPUTIL_NAMED_PORT(FLOOD) \
6985 : : OFPUTIL_NAMED_PORT(ALL) \
6986 : : OFPUTIL_NAMED_PORT(CONTROLLER) \
6987 : : OFPUTIL_NAMED_PORT(LOCAL) \
6988 : : OFPUTIL_NAMED_PORT(ANY) \
6989 : : OFPUTIL_NAMED_PORT(UNSET)
6990 : :
6991 : : /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6992 : : #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6993 : : OFPUTIL_NAMED_PORTS \
6994 : : OFPUTIL_NAMED_PORT(NONE)
6995 : :
6996 : : /* Stores the port number represented by 's' into '*portp'. 's' may be an
6997 : : * integer or, for reserved ports, the standard OpenFlow name for the port
6998 : : * (e.g. "LOCAL").
6999 : : *
7000 : : * Returns true if successful, false if 's' is not a valid OpenFlow port number
7001 : : * or name. The caller should issue an error message in this case, because
7002 : : * this function usually does not. (This gives the caller an opportunity to
7003 : : * look up the port name another way, e.g. by contacting the switch and listing
7004 : : * the names of all its ports).
7005 : : *
7006 : : * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7007 : : * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7008 : : * range as described in include/openflow/openflow-1.1.h. */
7009 : : bool
7010 : 8393 : ofputil_port_from_string(const char *s, ofp_port_t *portp)
7011 : : {
7012 : : unsigned int port32; /* int is at least 32 bits wide. */
7013 : :
7014 [ - + ]: 8393 : if (*s == '-') {
7015 [ # # ]: 0 : VLOG_WARN("Negative value %s is not a valid port number.", s);
7016 : 0 : return false;
7017 : : }
7018 : 8393 : *portp = 0;
7019 [ + + ]: 8393 : if (str_to_uint(s, 10, &port32)) {
7020 [ + + ]: 7862 : if (port32 < ofp_to_u16(OFPP_MAX)) {
7021 : : /* Pass. */
7022 [ - + ]: 3 : } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
7023 [ # # ]: 0 : VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7024 : : "be translated to %u when talking to an OF1.1 or "
7025 : : "later controller", port32, port32 + OFPP11_OFFSET);
7026 [ - + ]: 3 : } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
7027 : : char name[OFP_MAX_PORT_NAME_LEN];
7028 : :
7029 : 0 : ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
7030 [ # # ]: 0 : VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
7031 : : "for compatibility with OpenFlow 1.1 and later",
7032 : : name, port32);
7033 [ + + ]: 3 : } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
7034 [ + - ]: 1 : VLOG_WARN("port %u is outside the supported range 0 through "
7035 : : "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
7036 : : UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
7037 : 1 : return false;
7038 : : } else {
7039 : 2 : port32 -= OFPP11_OFFSET;
7040 : : }
7041 : :
7042 : 7861 : *portp = u16_to_ofp(port32);
7043 : 7861 : return true;
7044 : : } else {
7045 : : struct pair {
7046 : : const char *name;
7047 : : ofp_port_t value;
7048 : : };
7049 : : static const struct pair pairs[] = {
7050 : : #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7051 : : OFPUTIL_NAMED_PORTS_WITH_NONE
7052 : : #undef OFPUTIL_NAMED_PORT
7053 : : };
7054 : : const struct pair *p;
7055 : :
7056 [ + + ]: 3009 : for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
7057 [ + + ]: 3002 : if (!strcasecmp(s, p->name)) {
7058 : 524 : *portp = p->value;
7059 : 524 : return true;
7060 : : }
7061 : : }
7062 : 8393 : return false;
7063 : : }
7064 : : }
7065 : :
7066 : : /* Appends to 's' a string representation of the OpenFlow port number 'port'.
7067 : : * Most ports' string representation is just the port number, but for special
7068 : : * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7069 : : void
7070 : 46404 : ofputil_format_port(ofp_port_t port, struct ds *s)
7071 : : {
7072 : : char name[OFP_MAX_PORT_NAME_LEN];
7073 : :
7074 : 46404 : ofputil_port_to_string(port, name, sizeof name);
7075 : 46404 : ds_put_cstr(s, name);
7076 : 46404 : }
7077 : :
7078 : : /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7079 : : * representation of OpenFlow port number 'port'. Most ports are represented
7080 : : * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7081 : : * by name, e.g. "LOCAL". */
7082 : : void
7083 : 46460 : ofputil_port_to_string(ofp_port_t port,
7084 : : char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
7085 : : {
7086 [ + - + + : 46460 : switch (port) {
+ + + + -
+ ]
7087 : : #define OFPUTIL_NAMED_PORT(NAME) \
7088 : : case OFPP_##NAME: \
7089 : : ovs_strlcpy(namebuf, #NAME, bufsize); \
7090 : : break;
7091 : 16909 : OFPUTIL_NAMED_PORTS
7092 : : #undef OFPUTIL_NAMED_PORT
7093 : :
7094 : : default:
7095 : 29551 : snprintf(namebuf, bufsize, "%"PRIu16, port);
7096 : 29551 : break;
7097 : : }
7098 : 46460 : }
7099 : :
7100 : : /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7101 : : * integer or, for reserved group IDs, the standard OpenFlow name for the group
7102 : : * (either "ANY" or "ALL").
7103 : : *
7104 : : * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7105 : : * name. */
7106 : : bool
7107 : 18 : ofputil_group_from_string(const char *s, uint32_t *group_idp)
7108 : : {
7109 [ - + ]: 18 : if (!strcasecmp(s, "any")) {
7110 : 0 : *group_idp = OFPG_ANY;
7111 [ - + ]: 18 : } else if (!strcasecmp(s, "all")) {
7112 : 0 : *group_idp = OFPG_ALL;
7113 [ - + ]: 18 : } else if (!str_to_uint(s, 10, group_idp)) {
7114 [ # # ]: 0 : VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7115 : : "32-bit nonnegative integers or the keywords ANY or "
7116 : : "ALL.)", s);
7117 : 0 : return false;
7118 : : }
7119 : :
7120 : 18 : return true;
7121 : : }
7122 : :
7123 : : /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7124 : : * Most groups' string representation is just the number, but for special
7125 : : * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7126 : : void
7127 : 63 : ofputil_format_group(uint32_t group_id, struct ds *s)
7128 : : {
7129 : : char name[MAX_GROUP_NAME_LEN];
7130 : :
7131 : 63 : ofputil_group_to_string(group_id, name, sizeof name);
7132 : 63 : ds_put_cstr(s, name);
7133 : 63 : }
7134 : :
7135 : :
7136 : : /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7137 : : * representation of OpenFlow group ID 'group_id'. Most group are represented
7138 : : * as just their number, but special groups, e.g. OFPG_ALL, are represented
7139 : : * by name, e.g. "ALL". */
7140 : : void
7141 : 63 : ofputil_group_to_string(uint32_t group_id,
7142 : : char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
7143 : : {
7144 [ + + + ]: 63 : switch (group_id) {
7145 : : case OFPG_ALL:
7146 : 48 : ovs_strlcpy(namebuf, "ALL", bufsize);
7147 : 48 : break;
7148 : :
7149 : : case OFPG_ANY:
7150 : 1 : ovs_strlcpy(namebuf, "ANY", bufsize);
7151 : 1 : break;
7152 : :
7153 : : default:
7154 : 14 : snprintf(namebuf, bufsize, "%"PRIu32, group_id);
7155 : 14 : break;
7156 : : }
7157 : 63 : }
7158 : :
7159 : : /* Given a buffer 'b' that contains an array of OpenFlow ports of type
7160 : : * 'ofp_version', tries to pull the first element from the array. If
7161 : : * successful, initializes '*pp' with an abstract representation of the
7162 : : * port and returns 0. If no ports remain to be decoded, returns EOF.
7163 : : * On an error, returns a positive OFPERR_* value. */
7164 : : int
7165 : 1093 : ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
7166 : : struct ofputil_phy_port *pp)
7167 : : {
7168 : 1093 : memset(pp, 0, sizeof *pp);
7169 : :
7170 [ + + + - ]: 1093 : switch (ofp_version) {
7171 : : case OFP10_VERSION: {
7172 : 351 : const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
7173 [ + + ]: 351 : return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
7174 : : }
7175 : : case OFP11_VERSION:
7176 : : case OFP12_VERSION:
7177 : : case OFP13_VERSION: {
7178 : 590 : const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
7179 [ + + ]: 590 : return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
7180 : : }
7181 : : case OFP14_VERSION:
7182 : : case OFP15_VERSION:
7183 : : case OFP16_VERSION:
7184 [ + + ]: 152 : return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
7185 : : default:
7186 : 0 : OVS_NOT_REACHED();
7187 : : }
7188 : : }
7189 : :
7190 : : static void
7191 : 18892 : ofputil_normalize_match__(struct match *match, bool may_log)
7192 : : {
7193 : : enum {
7194 : : MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
7195 : : MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
7196 : : MAY_NW_PROTO = 1 << 2, /* nw_proto */
7197 : : MAY_IPVx = 1 << 3, /* tos, frag, ttl */
7198 : : MAY_ARP_SHA = 1 << 4, /* arp_sha */
7199 : : MAY_ARP_THA = 1 << 5, /* arp_tha */
7200 : : MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
7201 : : MAY_ND_TARGET = 1 << 7, /* nd_target */
7202 : : MAY_MPLS = 1 << 8, /* mpls label and tc */
7203 : : } may_match;
7204 : :
7205 : : struct flow_wildcards wc;
7206 : :
7207 : : /* Figure out what fields may be matched. */
7208 [ + + ]: 18892 : if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
7209 : 804 : may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
7210 [ + + ][ + + ]: 804 : if (match->flow.nw_proto == IPPROTO_TCP ||
7211 [ + + ]: 434 : match->flow.nw_proto == IPPROTO_UDP ||
7212 [ + + ]: 423 : match->flow.nw_proto == IPPROTO_SCTP ||
7213 : 423 : match->flow.nw_proto == IPPROTO_ICMP) {
7214 : 804 : may_match |= MAY_TP_ADDR;
7215 : : }
7216 [ + + ]: 18088 : } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
7217 : 133 : may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
7218 [ + + ][ + + ]: 133 : if (match->flow.nw_proto == IPPROTO_TCP ||
7219 [ + + ]: 103 : match->flow.nw_proto == IPPROTO_UDP ||
7220 : 103 : match->flow.nw_proto == IPPROTO_SCTP) {
7221 : 31 : may_match |= MAY_TP_ADDR;
7222 [ + + ]: 102 : } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
7223 : 48 : may_match |= MAY_TP_ADDR;
7224 [ + + ]: 48 : if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
7225 : 14 : may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
7226 [ + + ]: 34 : } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
7227 : 133 : may_match |= MAY_ND_TARGET | MAY_ARP_THA;
7228 : : }
7229 : : }
7230 [ + + - + ]: 35760 : } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
7231 : 17805 : match->flow.dl_type == htons(ETH_TYPE_RARP)) {
7232 : 150 : may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
7233 [ + + ]: 17805 : } else if (eth_type_mpls(match->flow.dl_type)) {
7234 : 22 : may_match = MAY_MPLS;
7235 : : } else {
7236 : 17783 : may_match = 0;
7237 : : }
7238 : :
7239 : : /* Clear the fields that may not be matched. */
7240 : 18892 : wc = match->wc;
7241 [ + + ]: 18892 : if (!(may_match & MAY_NW_ADDR)) {
7242 : 17938 : wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
7243 : : }
7244 [ + + ]: 18892 : if (!(may_match & MAY_TP_ADDR)) {
7245 : 18307 : wc.masks.tp_src = wc.masks.tp_dst = htons(0);
7246 : : }
7247 [ + + ]: 18892 : if (!(may_match & MAY_NW_PROTO)) {
7248 : 17805 : wc.masks.nw_proto = 0;
7249 : : }
7250 [ + + ]: 18892 : if (!(may_match & MAY_IPVx)) {
7251 : 17955 : wc.masks.nw_tos = 0;
7252 : 17955 : wc.masks.nw_ttl = 0;
7253 : : }
7254 [ + + ]: 18892 : if (!(may_match & MAY_ARP_SHA)) {
7255 : 18728 : WC_UNMASK_FIELD(&wc, arp_sha);
7256 : : }
7257 [ + + ]: 18892 : if (!(may_match & MAY_ARP_THA)) {
7258 : 18732 : WC_UNMASK_FIELD(&wc, arp_tha);
7259 : : }
7260 [ + + ]: 18892 : if (!(may_match & MAY_IPV6)) {
7261 : 18759 : wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
7262 : 18759 : wc.masks.ipv6_label = htonl(0);
7263 : : }
7264 [ + + ]: 18892 : if (!(may_match & MAY_ND_TARGET)) {
7265 : 18868 : wc.masks.nd_target = in6addr_any;
7266 : : }
7267 [ + + ]: 18892 : if (!(may_match & MAY_MPLS)) {
7268 : 18870 : memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
7269 : : }
7270 : :
7271 : : /* Log any changes. */
7272 [ + + ]: 18892 : if (!flow_wildcards_equal(&wc, &match->wc)) {
7273 [ + - ][ + + ]: 11 : bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
7274 [ + + ]: 11 : char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
7275 : :
7276 : 11 : match->wc = wc;
7277 : 11 : match_zero_wildcarded_fields(match);
7278 : :
7279 [ + + ]: 11 : if (log) {
7280 : 10 : char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
7281 [ + - ]: 10 : VLOG_INFO("normalization changed ofp_match, details:");
7282 [ + - ]: 10 : VLOG_INFO(" pre: %s", pre);
7283 [ + - ]: 10 : VLOG_INFO("post: %s", post);
7284 : 10 : free(pre);
7285 : 10 : free(post);
7286 : : }
7287 : : }
7288 : 18892 : }
7289 : :
7290 : : /* "Normalizes" the wildcards in 'match'. That means:
7291 : : *
7292 : : * 1. If the type of level N is known, then only the valid fields for that
7293 : : * level may be specified. For example, ARP does not have a TOS field,
7294 : : * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
7295 : : * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
7296 : : * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
7297 : : * IPv4 flow.
7298 : : *
7299 : : * 2. If the type of level N is not known (or not understood by Open
7300 : : * vSwitch), then no fields at all for that level may be specified. For
7301 : : * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
7302 : : * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
7303 : : * SCTP flow.
7304 : : *
7305 : : * If this function changes 'match', it logs a rate-limited informational
7306 : : * message. */
7307 : : void
7308 : 18892 : ofputil_normalize_match(struct match *match)
7309 : : {
7310 : 18892 : ofputil_normalize_match__(match, true);
7311 : 18892 : }
7312 : :
7313 : : /* Same as ofputil_normalize_match() without the logging. Thus, this function
7314 : : * is suitable for a program's internal use, whereas ofputil_normalize_match()
7315 : : * sense for use on flows received from elsewhere (so that a bug in the program
7316 : : * that sent them can be reported and corrected). */
7317 : : void
7318 : 0 : ofputil_normalize_match_quiet(struct match *match)
7319 : : {
7320 : 0 : ofputil_normalize_match__(match, false);
7321 : 0 : }
7322 : :
7323 : : static size_t
7324 : 49633 : parse_value(const char *s, const char *delimiters)
7325 : : {
7326 : 49633 : size_t n = 0;
7327 : :
7328 : : /* Iterate until we reach a delimiter.
7329 : : *
7330 : : * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7331 : : * terminator at the end of 's'. */
7332 [ + + ]: 221546 : while (!strchr(delimiters, s[n])) {
7333 [ + + ]: 171914 : if (s[n] == '(') {
7334 : 149 : int level = 0;
7335 : : do {
7336 [ + + + + ]: 4041 : switch (s[n]) {
7337 : : case '\0':
7338 : 1 : return n;
7339 : : case '(':
7340 : 174 : level++;
7341 : 174 : break;
7342 : : case ')':
7343 : 173 : level--;
7344 : 173 : break;
7345 : : }
7346 : 4040 : n++;
7347 [ + + ]: 4040 : } while (level > 0);
7348 : : } else {
7349 : 171765 : n++;
7350 : : }
7351 : : }
7352 : 49632 : return n;
7353 : : }
7354 : :
7355 : : /* Parses a key or a key-value pair from '*stringp'.
7356 : : *
7357 : : * On success: Stores the key into '*keyp'. Stores the value, if present, into
7358 : : * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7359 : : * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7360 : : * are substrings of '*stringp' created by replacing some of its bytes by null
7361 : : * terminators. Returns true.
7362 : : *
7363 : : * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7364 : : * NULL and returns false. */
7365 : : bool
7366 : 121701 : ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
7367 : : {
7368 : : /* Skip white space and delimiters. If that brings us to the end of the
7369 : : * input string, we are done and there are no more key-value pairs. */
7370 : 121701 : *stringp += strspn(*stringp, ", \t\r\n");
7371 [ + + ]: 121701 : if (**stringp == '\0') {
7372 : 48081 : *keyp = *valuep = NULL;
7373 : 48081 : return false;
7374 : : }
7375 : :
7376 : : /* Extract the key and the delimiter that ends the key-value pair or begins
7377 : : * the value. Advance the input position past the key and delimiter. */
7378 : 73620 : char *key = *stringp;
7379 : 73620 : size_t key_len = strcspn(key, ":=(, \t\r\n");
7380 : 73620 : char key_delim = key[key_len];
7381 : 73620 : key[key_len] = '\0';
7382 : 73620 : *stringp += key_len + (key_delim != '\0');
7383 : :
7384 : : /* Figure out what delimiter ends the value:
7385 : : *
7386 : : * - If key_delim is ":" or "=", the value extends until white space
7387 : : * or a comma.
7388 : : *
7389 : : * - If key_delim is "(", the value extends until ")".
7390 : : *
7391 : : * If there is no value, we are done. */
7392 : : const char *value_delims;
7393 [ + + ][ + + ]: 73620 : if (key_delim == ':' || key_delim == '=') {
7394 : 47874 : value_delims = ", \t\r\n";
7395 [ + + ]: 25746 : } else if (key_delim == '(') {
7396 : 1759 : value_delims = ")";
7397 : : } else {
7398 : 23987 : *keyp = key;
7399 : 23987 : *valuep = key + key_len; /* Empty string. */
7400 : 23987 : return true;
7401 : : }
7402 : :
7403 : : /* Extract the value. Advance the input position past the value and
7404 : : * delimiter. */
7405 : 49633 : char *value = *stringp;
7406 : 49633 : size_t value_len = parse_value(value, value_delims);
7407 : 49633 : char value_delim = value[value_len];
7408 : 49633 : value[value_len] = '\0';
7409 : 49633 : *stringp += value_len + (value_delim != '\0');
7410 : :
7411 : 49633 : *keyp = key;
7412 : 49633 : *valuep = value;
7413 : 49633 : return true;
7414 : : }
7415 : :
7416 : : /* Encode a dump ports request for 'port', the encoded message
7417 : : * will be for OpenFlow version 'ofp_version'. Returns message
7418 : : * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7419 : : struct ofpbuf *
7420 : 19 : ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
7421 : : {
7422 : : struct ofpbuf *request;
7423 : :
7424 [ + + - ]: 19 : switch (ofp_version) {
7425 : : case OFP10_VERSION: {
7426 : : struct ofp10_port_stats_request *req;
7427 : 16 : request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
7428 : 16 : req = ofpbuf_put_zeros(request, sizeof *req);
7429 : 16 : req->port_no = htons(ofp_to_u16(port));
7430 : 16 : break;
7431 : : }
7432 : : case OFP11_VERSION:
7433 : : case OFP12_VERSION:
7434 : : case OFP13_VERSION:
7435 : : case OFP14_VERSION:
7436 : : case OFP15_VERSION:
7437 : : case OFP16_VERSION: {
7438 : : struct ofp11_port_stats_request *req;
7439 : 3 : request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
7440 : 3 : req = ofpbuf_put_zeros(request, sizeof *req);
7441 : 3 : req->port_no = ofputil_port_to_ofp11(port);
7442 : 3 : break;
7443 : : }
7444 : : default:
7445 : 0 : OVS_NOT_REACHED();
7446 : : }
7447 : :
7448 : 19 : return request;
7449 : : }
7450 : :
7451 : : static void
7452 : 41 : ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
7453 : : struct ofp10_port_stats *ps10)
7454 : : {
7455 : 41 : ps10->port_no = htons(ofp_to_u16(ops->port_no));
7456 : 41 : memset(ps10->pad, 0, sizeof ps10->pad);
7457 : 41 : put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
7458 : 41 : put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
7459 : 41 : put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
7460 : 41 : put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
7461 : 41 : put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
7462 : 41 : put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
7463 : 41 : put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
7464 : 41 : put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
7465 : 41 : put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
7466 : 41 : put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
7467 : 41 : put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
7468 : 41 : put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
7469 : 41 : }
7470 : :
7471 : : static void
7472 : 4 : ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
7473 : : struct ofp11_port_stats *ps11)
7474 : : {
7475 : 4 : ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
7476 : 4 : memset(ps11->pad, 0, sizeof ps11->pad);
7477 : 4 : ps11->rx_packets = htonll(ops->stats.rx_packets);
7478 : 4 : ps11->tx_packets = htonll(ops->stats.tx_packets);
7479 : 4 : ps11->rx_bytes = htonll(ops->stats.rx_bytes);
7480 : 4 : ps11->tx_bytes = htonll(ops->stats.tx_bytes);
7481 : 4 : ps11->rx_dropped = htonll(ops->stats.rx_dropped);
7482 : 4 : ps11->tx_dropped = htonll(ops->stats.tx_dropped);
7483 : 4 : ps11->rx_errors = htonll(ops->stats.rx_errors);
7484 : 4 : ps11->tx_errors = htonll(ops->stats.tx_errors);
7485 : 4 : ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7486 : 4 : ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
7487 : 4 : ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7488 : 4 : ps11->collisions = htonll(ops->stats.collisions);
7489 : 4 : }
7490 : :
7491 : : static void
7492 : 3 : ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
7493 : : struct ofp13_port_stats *ps13)
7494 : : {
7495 : 3 : ofputil_port_stats_to_ofp11(ops, &ps13->ps);
7496 : 3 : ps13->duration_sec = htonl(ops->duration_sec);
7497 : 3 : ps13->duration_nsec = htonl(ops->duration_nsec);
7498 : 3 : }
7499 : :
7500 : : static void
7501 : 1 : ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
7502 : : struct ovs_list *replies)
7503 : : {
7504 : : struct ofp14_port_stats_prop_ethernet *eth;
7505 : : struct intel_port_stats_rfc2819 *stats_rfc2819;
7506 : : struct ofp14_port_stats *ps14;
7507 : : struct ofpbuf *reply;
7508 : :
7509 : 1 : reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth +
7510 : : sizeof *stats_rfc2819);
7511 : :
7512 : 1 : ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
7513 : 1 : ps14->length = htons(sizeof *ps14 + sizeof *eth +
7514 : : sizeof *stats_rfc2819);
7515 : 1 : memset(ps14->pad, 0, sizeof ps14->pad);
7516 : 1 : ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7517 : 1 : ps14->duration_sec = htonl(ops->duration_sec);
7518 : 1 : ps14->duration_nsec = htonl(ops->duration_nsec);
7519 : 1 : ps14->rx_packets = htonll(ops->stats.rx_packets);
7520 : 1 : ps14->tx_packets = htonll(ops->stats.tx_packets);
7521 : 1 : ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7522 : 1 : ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7523 : 1 : ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7524 : 1 : ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7525 : 1 : ps14->rx_errors = htonll(ops->stats.rx_errors);
7526 : 1 : ps14->tx_errors = htonll(ops->stats.tx_errors);
7527 : :
7528 : 1 : eth = ofpprop_put_zeros(reply, OFPPSPT14_ETHERNET, sizeof *eth);
7529 : 1 : eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7530 : 1 : eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7531 : 1 : eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7532 : 1 : eth->collisions = htonll(ops->stats.collisions);
7533 : :
7534 : 1 : uint64_t prop_type = OFPPROP_EXP(INTEL_VENDOR_ID,
7535 : : INTEL_PORT_STATS_RFC2819);
7536 : :
7537 : 1 : stats_rfc2819 = ofpprop_put_zeros(reply, prop_type,
7538 : : sizeof *stats_rfc2819);
7539 : :
7540 : 1 : memset(stats_rfc2819->pad, 0, sizeof stats_rfc2819->pad);
7541 : 1 : stats_rfc2819->rx_1_to_64_packets = htonll(ops->stats.rx_1_to_64_packets);
7542 : 1 : stats_rfc2819->rx_65_to_127_packets =
7543 : 1 : htonll(ops->stats.rx_65_to_127_packets);
7544 : 1 : stats_rfc2819->rx_128_to_255_packets =
7545 : 1 : htonll(ops->stats.rx_128_to_255_packets);
7546 : 1 : stats_rfc2819->rx_256_to_511_packets =
7547 : 1 : htonll(ops->stats.rx_256_to_511_packets);
7548 : 1 : stats_rfc2819->rx_512_to_1023_packets =
7549 : 1 : htonll(ops->stats.rx_512_to_1023_packets);
7550 : 1 : stats_rfc2819->rx_1024_to_1522_packets =
7551 : 1 : htonll(ops->stats.rx_1024_to_1522_packets);
7552 : 1 : stats_rfc2819->rx_1523_to_max_packets =
7553 : 1 : htonll(ops->stats.rx_1523_to_max_packets);
7554 : :
7555 : 1 : stats_rfc2819->tx_1_to_64_packets = htonll(ops->stats.tx_1_to_64_packets);
7556 : 1 : stats_rfc2819->tx_65_to_127_packets =
7557 : 1 : htonll(ops->stats.tx_65_to_127_packets);
7558 : 1 : stats_rfc2819->tx_128_to_255_packets =
7559 : 1 : htonll(ops->stats.tx_128_to_255_packets);
7560 : 1 : stats_rfc2819->tx_256_to_511_packets =
7561 : 1 : htonll(ops->stats.tx_256_to_511_packets);
7562 : 1 : stats_rfc2819->tx_512_to_1023_packets =
7563 : 1 : htonll(ops->stats.tx_512_to_1023_packets);
7564 : 1 : stats_rfc2819->tx_1024_to_1522_packets =
7565 : 1 : htonll(ops->stats.tx_1024_to_1522_packets);
7566 : 1 : stats_rfc2819->tx_1523_to_max_packets =
7567 : 1 : htonll(ops->stats.tx_1523_to_max_packets);
7568 : :
7569 : 1 : stats_rfc2819->tx_multicast_packets =
7570 : 1 : htonll(ops->stats.tx_multicast_packets);
7571 : 1 : stats_rfc2819->rx_broadcast_packets =
7572 : 1 : htonll(ops->stats.rx_broadcast_packets);
7573 : 1 : stats_rfc2819->tx_broadcast_packets =
7574 : 1 : htonll(ops->stats.tx_broadcast_packets);
7575 : 1 : stats_rfc2819->rx_undersized_errors =
7576 : 1 : htonll(ops->stats.rx_undersized_errors);
7577 : 1 : stats_rfc2819->rx_oversize_errors =
7578 : 1 : htonll(ops->stats.rx_oversize_errors);
7579 : 1 : stats_rfc2819->rx_fragmented_errors =
7580 : 1 : htonll(ops->stats.rx_fragmented_errors);
7581 : 1 : stats_rfc2819->rx_jabber_errors =
7582 : 1 : htonll(ops->stats.rx_jabber_errors);
7583 : 1 : }
7584 : :
7585 : : /* Encode a ports stat for 'ops' and append it to 'replies'. */
7586 : : void
7587 : 46 : ofputil_append_port_stat(struct ovs_list *replies,
7588 : : const struct ofputil_port_stats *ops)
7589 : : {
7590 [ + + + + : 46 : switch (ofpmp_version(replies)) {
- ]
7591 : : case OFP13_VERSION: {
7592 : 3 : struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7593 : 3 : ofputil_port_stats_to_ofp13(ops, reply);
7594 : 3 : break;
7595 : : }
7596 : : case OFP12_VERSION:
7597 : : case OFP11_VERSION: {
7598 : 1 : struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7599 : 1 : ofputil_port_stats_to_ofp11(ops, reply);
7600 : 1 : break;
7601 : : }
7602 : :
7603 : : case OFP10_VERSION: {
7604 : 41 : struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7605 : 41 : ofputil_port_stats_to_ofp10(ops, reply);
7606 : 41 : break;
7607 : : }
7608 : :
7609 : : case OFP14_VERSION:
7610 : : case OFP15_VERSION:
7611 : : case OFP16_VERSION:
7612 : 1 : ofputil_append_ofp14_port_stats(ops, replies);
7613 : 1 : break;
7614 : :
7615 : : default:
7616 : 0 : OVS_NOT_REACHED();
7617 : : }
7618 : 46 : }
7619 : :
7620 : : static enum ofperr
7621 : 172 : ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7622 : : const struct ofp10_port_stats *ps10)
7623 : : {
7624 : :
7625 : 172 : ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
7626 : 172 : ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7627 : 172 : ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7628 : 172 : ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7629 : 172 : ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7630 : 172 : ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7631 : 172 : ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7632 : 172 : ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7633 : 172 : ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7634 : 172 : ops->stats.rx_frame_errors =
7635 : 172 : ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7636 : 172 : ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7637 : 172 : ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7638 : 172 : ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
7639 : 172 : ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7640 : :
7641 : 172 : return 0;
7642 : : }
7643 : :
7644 : : static enum ofperr
7645 : 28 : ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7646 : : const struct ofp11_port_stats *ps11)
7647 : : {
7648 : : enum ofperr error;
7649 : :
7650 : 28 : error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7651 [ - + ]: 28 : if (error) {
7652 : 0 : return error;
7653 : : }
7654 : :
7655 : 28 : ops->stats.rx_packets = ntohll(ps11->rx_packets);
7656 : 28 : ops->stats.tx_packets = ntohll(ps11->tx_packets);
7657 : 28 : ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7658 : 28 : ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7659 : 28 : ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7660 : 28 : ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7661 : 28 : ops->stats.rx_errors = ntohll(ps11->rx_errors);
7662 : 28 : ops->stats.tx_errors = ntohll(ps11->tx_errors);
7663 : 28 : ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7664 : 28 : ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7665 : 28 : ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7666 : 28 : ops->stats.collisions = ntohll(ps11->collisions);
7667 : 28 : ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7668 : :
7669 : 28 : return 0;
7670 : : }
7671 : :
7672 : : static enum ofperr
7673 : 18 : ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7674 : : const struct ofp13_port_stats *ps13)
7675 : : {
7676 : 18 : enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
7677 [ + - ]: 18 : if (!error) {
7678 : 18 : ops->duration_sec = ntohl(ps13->duration_sec);
7679 : 18 : ops->duration_nsec = ntohl(ps13->duration_nsec);
7680 : : }
7681 : 18 : return error;
7682 : : }
7683 : :
7684 : : static enum ofperr
7685 : 6 : parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7686 : : struct ofputil_port_stats *ops)
7687 : : {
7688 : 6 : const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
7689 : :
7690 [ - + ]: 6 : if (payload->size != sizeof *eth) {
7691 : 0 : return OFPERR_OFPBPC_BAD_LEN;
7692 : : }
7693 : :
7694 : 6 : ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7695 : 6 : ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7696 : 6 : ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7697 : 6 : ops->stats.collisions = ntohll(eth->collisions);
7698 : :
7699 : 6 : return 0;
7700 : : }
7701 : :
7702 : : static enum ofperr
7703 : 4 : parse_intel_port_stats_rfc2819_property(const struct ofpbuf *payload,
7704 : : struct ofputil_port_stats *ops)
7705 : : {
7706 : 4 : const struct intel_port_stats_rfc2819 *rfc2819 = payload->data;
7707 : :
7708 [ - + ]: 4 : if (payload->size != sizeof *rfc2819) {
7709 : 0 : return OFPERR_OFPBPC_BAD_LEN;
7710 : : }
7711 : 4 : ops->stats.rx_1_to_64_packets = ntohll(rfc2819->rx_1_to_64_packets);
7712 : 4 : ops->stats.rx_65_to_127_packets = ntohll(rfc2819->rx_65_to_127_packets);
7713 : 4 : ops->stats.rx_128_to_255_packets = ntohll(rfc2819->rx_128_to_255_packets);
7714 : 4 : ops->stats.rx_256_to_511_packets = ntohll(rfc2819->rx_256_to_511_packets);
7715 : 4 : ops->stats.rx_512_to_1023_packets =
7716 : 4 : ntohll(rfc2819->rx_512_to_1023_packets);
7717 : 4 : ops->stats.rx_1024_to_1522_packets =
7718 : 4 : ntohll(rfc2819->rx_1024_to_1522_packets);
7719 : 4 : ops->stats.rx_1523_to_max_packets =
7720 : 4 : ntohll(rfc2819->rx_1523_to_max_packets);
7721 : :
7722 : 4 : ops->stats.tx_1_to_64_packets = ntohll(rfc2819->tx_1_to_64_packets);
7723 : 4 : ops->stats.tx_65_to_127_packets = ntohll(rfc2819->tx_65_to_127_packets);
7724 : 4 : ops->stats.tx_128_to_255_packets = ntohll(rfc2819->tx_128_to_255_packets);
7725 : 4 : ops->stats.tx_256_to_511_packets = ntohll(rfc2819->tx_256_to_511_packets);
7726 : 4 : ops->stats.tx_512_to_1023_packets =
7727 : 4 : ntohll(rfc2819->tx_512_to_1023_packets);
7728 : 4 : ops->stats.tx_1024_to_1522_packets =
7729 : 4 : ntohll(rfc2819->tx_1024_to_1522_packets);
7730 : 4 : ops->stats.tx_1523_to_max_packets =
7731 : 4 : ntohll(rfc2819->tx_1523_to_max_packets);
7732 : :
7733 : 4 : ops->stats.tx_multicast_packets = ntohll(rfc2819->tx_multicast_packets);
7734 : 4 : ops->stats.rx_broadcast_packets = ntohll(rfc2819->rx_broadcast_packets);
7735 : 4 : ops->stats.tx_broadcast_packets = ntohll(rfc2819->tx_broadcast_packets);
7736 : 4 : ops->stats.rx_undersized_errors = ntohll(rfc2819->rx_undersized_errors);
7737 : :
7738 : 4 : ops->stats.rx_oversize_errors = ntohll(rfc2819->rx_oversize_errors);
7739 : 4 : ops->stats.rx_fragmented_errors = ntohll(rfc2819->rx_fragmented_errors);
7740 : 4 : ops->stats.rx_jabber_errors = ntohll(rfc2819->rx_jabber_errors);
7741 : :
7742 : 4 : return 0;
7743 : : }
7744 : :
7745 : : static enum ofperr
7746 : 4 : parse_intel_port_stats_property(const struct ofpbuf *payload,
7747 : : uint32_t exp_type,
7748 : : struct ofputil_port_stats *ops)
7749 : : {
7750 : : enum ofperr error;
7751 : :
7752 [ + - ]: 4 : switch (exp_type) {
7753 : : case INTEL_PORT_STATS_RFC2819:
7754 : 4 : error = parse_intel_port_stats_rfc2819_property(payload, ops);
7755 : 4 : break;
7756 : : default:
7757 : 0 : error = OFPERR_OFPBPC_BAD_EXP_TYPE;
7758 : 0 : break;
7759 : : }
7760 : :
7761 : 4 : return error;
7762 : : }
7763 : :
7764 : : static enum ofperr
7765 : 6 : ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
7766 : : struct ofpbuf *msg)
7767 : : {
7768 : 6 : const struct ofp14_port_stats *ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
7769 [ - + ]: 6 : if (!ps14) {
7770 : 0 : return OFPERR_OFPBRC_BAD_LEN;
7771 : : }
7772 : :
7773 : 6 : size_t len = ntohs(ps14->length);
7774 [ + - ][ - + ]: 6 : if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
7775 : 0 : return OFPERR_OFPBRC_BAD_LEN;
7776 : : }
7777 : 6 : len -= sizeof *ps14;
7778 : :
7779 : 6 : enum ofperr error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
7780 [ - + ]: 6 : if (error) {
7781 : 0 : return error;
7782 : : }
7783 : :
7784 : 6 : ops->duration_sec = ntohl(ps14->duration_sec);
7785 : 6 : ops->duration_nsec = ntohl(ps14->duration_nsec);
7786 : 6 : ops->stats.rx_packets = ntohll(ps14->rx_packets);
7787 : 6 : ops->stats.tx_packets = ntohll(ps14->tx_packets);
7788 : 6 : ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
7789 : 6 : ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
7790 : 6 : ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
7791 : 6 : ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
7792 : 6 : ops->stats.rx_errors = ntohll(ps14->rx_errors);
7793 : 6 : ops->stats.tx_errors = ntohll(ps14->tx_errors);
7794 : :
7795 : :
7796 : 6 : struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
7797 : : len);
7798 [ + + ]: 16 : while (properties.size > 0) {
7799 : : struct ofpbuf payload;
7800 : : enum ofperr error;
7801 : 10 : uint64_t type = 0;
7802 : :
7803 : 10 : error = ofpprop_pull(&properties, &payload, &type);
7804 [ - + ]: 10 : if (error) {
7805 : 0 : return error;
7806 : : }
7807 [ + + - ]: 10 : switch (type) {
7808 : : case OFPPSPT14_ETHERNET:
7809 : 6 : error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
7810 : 6 : break;
7811 : : case OFPPROP_EXP(INTEL_VENDOR_ID, INTEL_PORT_STATS_RFC2819):
7812 : 4 : error = parse_intel_port_stats_property(&payload,
7813 : : INTEL_PORT_STATS_RFC2819,
7814 : : ops);
7815 : 4 : break;
7816 : : default:
7817 : 0 : error = OFPPROP_UNKNOWN(true, "port stats", type);
7818 : 0 : break;
7819 : : }
7820 : :
7821 [ - + ]: 10 : if (error) {
7822 : 10 : return error;
7823 : : }
7824 : : }
7825 : :
7826 : 6 : return 0;
7827 : : }
7828 : :
7829 : : /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7830 : : * message 'oh'. */
7831 : : size_t
7832 : 42 : ofputil_count_port_stats(const struct ofp_header *oh)
7833 : : {
7834 : 42 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
7835 : 42 : ofpraw_pull_assert(&b);
7836 : :
7837 : 42 : for (size_t n = 0; ; n++) {
7838 : : struct ofputil_port_stats ps;
7839 [ + + ]: 145 : if (ofputil_decode_port_stats(&ps, &b)) {
7840 : 42 : return n;
7841 : : }
7842 : 103 : }
7843 : : }
7844 : :
7845 : : /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7846 : : * ofputil_port_stats in 'ps'.
7847 : : *
7848 : : * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7849 : : * message. Calling this function multiple times for a single 'msg' iterates
7850 : : * through the replies. The caller must initially leave 'msg''s layer pointers
7851 : : * null and not modify them between calls.
7852 : : *
7853 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
7854 : : * otherwise a positive errno value. */
7855 : : int
7856 : 290 : ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
7857 : : {
7858 : : enum ofperr error;
7859 : : enum ofpraw raw;
7860 : :
7861 : 290 : memset(&(ps->stats), 0xFF, sizeof (ps->stats));
7862 : :
7863 [ + + ]: 290 : error = (msg->header ? ofpraw_decode(&raw, msg->header)
7864 : : : ofpraw_pull(&raw, msg));
7865 [ - + ]: 290 : if (error) {
7866 : 0 : return error;
7867 : : }
7868 : :
7869 [ + + ]: 290 : if (!msg->size) {
7870 : 84 : return EOF;
7871 [ + + ]: 206 : } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
7872 : 6 : return ofputil_pull_ofp14_port_stats(ps, msg);
7873 [ + + ]: 200 : } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
7874 : : const struct ofp13_port_stats *ps13;
7875 : 18 : ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7876 [ - + ]: 18 : if (!ps13) {
7877 : 0 : goto bad_len;
7878 : : }
7879 : 18 : return ofputil_port_stats_from_ofp13(ps, ps13);
7880 [ + + ]: 182 : } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7881 : : const struct ofp11_port_stats *ps11;
7882 : :
7883 : 10 : ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7884 [ - + ]: 10 : if (!ps11) {
7885 : 0 : goto bad_len;
7886 : : }
7887 : 10 : return ofputil_port_stats_from_ofp11(ps, ps11);
7888 [ + - ]: 172 : } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7889 : : const struct ofp10_port_stats *ps10;
7890 : :
7891 : 172 : ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7892 [ - + ]: 172 : if (!ps10) {
7893 : 0 : goto bad_len;
7894 : : }
7895 : 172 : return ofputil_port_stats_from_ofp10(ps, ps10);
7896 : : } else {
7897 : 0 : OVS_NOT_REACHED();
7898 : : }
7899 : :
7900 : : bad_len:
7901 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7902 : : "bytes at end", msg->size);
7903 : 290 : return OFPERR_OFPBRC_BAD_LEN;
7904 : : }
7905 : :
7906 : : /* Parse a port status request message into a 16 bit OpenFlow 1.0
7907 : : * port number and stores the latter in '*ofp10_port'.
7908 : : * Returns 0 if successful, otherwise an OFPERR_* number. */
7909 : : enum ofperr
7910 : 42 : ofputil_decode_port_stats_request(const struct ofp_header *request,
7911 : : ofp_port_t *ofp10_port)
7912 : : {
7913 [ + + - ]: 42 : switch ((enum ofp_version)request->version) {
7914 : : case OFP16_VERSION:
7915 : : case OFP15_VERSION:
7916 : : case OFP14_VERSION:
7917 : : case OFP13_VERSION:
7918 : : case OFP12_VERSION:
7919 : : case OFP11_VERSION: {
7920 : 9 : const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7921 : 9 : return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7922 : : }
7923 : :
7924 : : case OFP10_VERSION: {
7925 : 33 : const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7926 : 33 : *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7927 : 33 : return 0;
7928 : : }
7929 : :
7930 : : default:
7931 : 0 : OVS_NOT_REACHED();
7932 : : }
7933 : : }
7934 : :
7935 : : static void
7936 : 2 : ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats *ois,
7937 : : struct nx_ipfix_stats_reply *reply)
7938 : : {
7939 : 2 : reply->collector_set_id = htonl(ois->collector_set_id);
7940 : 2 : reply->total_flows = htonll(ois->total_flows);
7941 : 2 : reply->current_flows = htonll(ois->current_flows);
7942 : 2 : reply->pkts = htonll(ois->pkts);
7943 : 2 : reply->ipv4_pkts = htonll(ois->ipv4_pkts);
7944 : 2 : reply->ipv6_pkts = htonll(ois->ipv6_pkts);
7945 : 2 : reply->error_pkts = htonll(ois->error_pkts);
7946 : 2 : reply->ipv4_error_pkts = htonll(ois->ipv4_error_pkts);
7947 : 2 : reply->ipv6_error_pkts = htonll(ois->ipv6_error_pkts);
7948 : 2 : reply->tx_pkts = htonll(ois->tx_pkts);
7949 : 2 : reply->tx_errors = htonll(ois->tx_errors);
7950 : 2 : memset(reply->pad, 0, sizeof reply->pad);
7951 : 2 : }
7952 : :
7953 : : /* Encode a ipfix stat for 'ois' and append it to 'replies'. */
7954 : : void
7955 : 2 : ofputil_append_ipfix_stat(struct ovs_list *replies,
7956 : : const struct ofputil_ipfix_stats *ois)
7957 : : {
7958 : 2 : struct nx_ipfix_stats_reply *reply = ofpmp_append(replies, sizeof *reply);
7959 : 2 : ofputil_ipfix_stats_to_reply(ois, reply);
7960 : 2 : }
7961 : :
7962 : : static enum ofperr
7963 : 7 : ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats *is,
7964 : : const struct nx_ipfix_stats_reply *reply)
7965 : : {
7966 : 7 : is->collector_set_id = ntohl(reply->collector_set_id);
7967 : 7 : is->total_flows = ntohll(reply->total_flows);
7968 : 7 : is->current_flows = ntohll(reply->current_flows);
7969 : 7 : is->pkts = ntohll(reply->pkts);
7970 : 7 : is->ipv4_pkts = ntohll(reply->ipv4_pkts);
7971 : 7 : is->ipv6_pkts = ntohll(reply->ipv6_pkts);
7972 : 7 : is->error_pkts = ntohll(reply->error_pkts);
7973 : 7 : is->ipv4_error_pkts = ntohll(reply->ipv4_error_pkts);
7974 : 7 : is->ipv6_error_pkts = ntohll(reply->ipv6_error_pkts);
7975 : 7 : is->tx_pkts = ntohll(reply->tx_pkts);
7976 : 7 : is->tx_errors = ntohll(reply->tx_errors);
7977 : :
7978 : 7 : return 0;
7979 : : }
7980 : :
7981 : : int
7982 : 13 : ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats *is, struct ofpbuf *msg)
7983 : : {
7984 : : enum ofperr error;
7985 : : enum ofpraw raw;
7986 : :
7987 : 13 : memset(is, 0xFF, sizeof (*is));
7988 : :
7989 [ + + ]: 13 : error = (msg->header ? ofpraw_decode(&raw, msg->header)
7990 : : : ofpraw_pull(&raw, msg));
7991 [ - + ]: 13 : if (error) {
7992 : 0 : return error;
7993 : : }
7994 : :
7995 [ + + ]: 13 : if (!msg->size) {
7996 : 6 : return EOF;
7997 [ + + ][ + - ]: 7 : } else if (raw == OFPRAW_NXST_IPFIX_BRIDGE_REPLY ||
7998 : 4 : raw == OFPRAW_NXST_IPFIX_FLOW_REPLY) {
7999 : : struct nx_ipfix_stats_reply *reply;
8000 : :
8001 : 7 : reply = ofpbuf_try_pull(msg, sizeof *reply);
8002 : 7 : return ofputil_ipfix_stats_from_nx(is, reply);
8003 : : } else {
8004 : 13 : OVS_NOT_REACHED();
8005 : : }
8006 : : }
8007 : :
8008 : :
8009 : : /* Returns the number of ipfix stats elements in
8010 : : * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8011 : : * message 'oh'. */
8012 : : size_t
8013 : 3 : ofputil_count_ipfix_stats(const struct ofp_header *oh)
8014 : : {
8015 : 3 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
8016 : 3 : ofpraw_pull_assert(&b);
8017 : :
8018 : 3 : return b.size / sizeof(struct ofputil_ipfix_stats);
8019 : : }
8020 : :
8021 : : /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8022 : : void
8023 : 788 : ofputil_bucket_list_destroy(struct ovs_list *buckets)
8024 : : {
8025 : : struct ofputil_bucket *bucket;
8026 : :
8027 [ + + ]: 1775 : LIST_FOR_EACH_POP (bucket, list_node, buckets) {
8028 : 987 : free(bucket->ofpacts);
8029 : 987 : free(bucket);
8030 : : }
8031 : 788 : }
8032 : :
8033 : : /* Clones 'bucket' and its ofpacts data */
8034 : : static struct ofputil_bucket *
8035 : 229 : ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
8036 : : {
8037 : : struct ofputil_bucket *new;
8038 : :
8039 : 229 : new = xmemdup(bucket, sizeof *bucket);
8040 : 229 : new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
8041 : :
8042 : 229 : return new;
8043 : : }
8044 : :
8045 : : /* Clones each of the buckets in the list 'src' appending them
8046 : : * in turn to 'dest' which should be an initialised list.
8047 : : * An exception is that if the pointer value of a bucket in 'src'
8048 : : * matches 'skip' then it is not cloned or appended to 'dest'.
8049 : : * This allows all of 'src' or 'all of 'src' except 'skip' to
8050 : : * be cloned and appended to 'dest'. */
8051 : : void
8052 : 129 : ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
8053 : : const struct ofputil_bucket *skip)
8054 : : {
8055 : : struct ofputil_bucket *bucket;
8056 : :
8057 [ + + ]: 364 : LIST_FOR_EACH (bucket, list_node, src) {
8058 : : struct ofputil_bucket *new_bucket;
8059 : :
8060 [ + + ]: 235 : if (bucket == skip) {
8061 : 6 : continue;
8062 : : }
8063 : :
8064 : 229 : new_bucket = ofputil_bucket_clone_data(bucket);
8065 : 229 : ovs_list_push_back(dest, &new_bucket->list_node);
8066 : : }
8067 : 129 : }
8068 : :
8069 : : /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8070 : : * Returns the first bucket found or NULL if no buckets are found. */
8071 : : struct ofputil_bucket *
8072 : 16 : ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
8073 : : {
8074 : : struct ofputil_bucket *bucket;
8075 : :
8076 [ - + ]: 16 : if (bucket_id > OFPG15_BUCKET_MAX) {
8077 : 0 : return NULL;
8078 : : }
8079 : :
8080 [ + + ]: 82 : LIST_FOR_EACH (bucket, list_node, buckets) {
8081 [ + + ]: 80 : if (bucket->bucket_id == bucket_id) {
8082 : 14 : return bucket;
8083 : : }
8084 : : }
8085 : :
8086 : 2 : return NULL;
8087 : : }
8088 : :
8089 : : /* Returns true if more than one bucket in the list 'buckets'
8090 : : * have the same bucket id. Returns false otherwise. */
8091 : : bool
8092 : 212 : ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
8093 : : {
8094 : : struct ofputil_bucket *i, *j;
8095 : :
8096 [ + + ]: 719 : LIST_FOR_EACH (i, list_node, buckets) {
8097 [ + - ]: 1575 : LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
8098 [ + + ]: 1575 : if (i == j) {
8099 : 507 : break;
8100 : : }
8101 [ + + ]: 1068 : if (i->bucket_id == j->bucket_id) {
8102 : 2 : return true;
8103 : : }
8104 : : }
8105 : : }
8106 : :
8107 : 210 : return false;
8108 : : }
8109 : :
8110 : : /* Returns the bucket at the front of the list 'buckets'.
8111 : : * Undefined if 'buckets is empty. */
8112 : : struct ofputil_bucket *
8113 : 4 : ofputil_bucket_list_front(const struct ovs_list *buckets)
8114 : : {
8115 : : static struct ofputil_bucket *bucket;
8116 : :
8117 : 4 : ASSIGN_CONTAINER(bucket, ovs_list_front(buckets), list_node);
8118 : :
8119 : 4 : return bucket;
8120 : : }
8121 : :
8122 : : /* Returns the bucket at the back of the list 'buckets'.
8123 : : * Undefined if 'buckets is empty. */
8124 : : struct ofputil_bucket *
8125 : 6 : ofputil_bucket_list_back(const struct ovs_list *buckets)
8126 : : {
8127 : : static struct ofputil_bucket *bucket;
8128 : :
8129 : 6 : ASSIGN_CONTAINER(bucket, ovs_list_back(buckets), list_node);
8130 : :
8131 : 6 : return bucket;
8132 : : }
8133 : :
8134 : : /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8135 : : * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8136 : : * for all groups.)
8137 : : *
8138 : : * Group statistics include packet and byte counts for each group. */
8139 : : struct ofpbuf *
8140 : 10 : ofputil_encode_group_stats_request(enum ofp_version ofp_version,
8141 : : uint32_t group_id)
8142 : : {
8143 : : struct ofpbuf *request;
8144 : :
8145 [ - + - ]: 10 : switch (ofp_version) {
8146 : : case OFP10_VERSION:
8147 : 0 : ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8148 : : "(\'-O OpenFlow11\')");
8149 : : case OFP11_VERSION:
8150 : : case OFP12_VERSION:
8151 : : case OFP13_VERSION:
8152 : : case OFP14_VERSION:
8153 : : case OFP15_VERSION:
8154 : : case OFP16_VERSION: {
8155 : : struct ofp11_group_stats_request *req;
8156 : 10 : request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
8157 : 10 : req = ofpbuf_put_zeros(request, sizeof *req);
8158 : 10 : req->group_id = htonl(group_id);
8159 : 10 : break;
8160 : : }
8161 : : default:
8162 : 0 : OVS_NOT_REACHED();
8163 : : }
8164 : :
8165 : 10 : return request;
8166 : : }
8167 : :
8168 : : void
8169 : 114 : ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
8170 : : {
8171 : 114 : ofputil_bucket_list_destroy(&gd->buckets);
8172 : 114 : ofputil_group_properties_destroy(&gd->props);
8173 : 114 : }
8174 : :
8175 : : /* Decodes the OpenFlow group description request in 'oh', returning the group
8176 : : * whose description is requested, or OFPG_ALL if stats for all groups was
8177 : : * requested. */
8178 : : uint32_t
8179 : 106 : ofputil_decode_group_desc_request(const struct ofp_header *oh)
8180 : : {
8181 : 106 : struct ofpbuf request = ofpbuf_const_initializer(oh, ntohs(oh->length));
8182 : 106 : enum ofpraw raw = ofpraw_pull_assert(&request);
8183 [ + + ]: 106 : if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
8184 : 37 : return OFPG_ALL;
8185 [ + - ]: 69 : } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
8186 : 69 : ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
8187 : 69 : return ntohl(*group_id);
8188 : : } else {
8189 : 106 : OVS_NOT_REACHED();
8190 : : }
8191 : : }
8192 : :
8193 : : /* Returns an OpenFlow group description request for OpenFlow version
8194 : : * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8195 : : * request stats for all groups (OpenFlow 1.4 and earlier always request all
8196 : : * groups).
8197 : : *
8198 : : * Group descriptions include the bucket and action configuration for each
8199 : : * group. */
8200 : : struct ofpbuf *
8201 : 53 : ofputil_encode_group_desc_request(enum ofp_version ofp_version,
8202 : : uint32_t group_id)
8203 : : {
8204 : : struct ofpbuf *request;
8205 : :
8206 [ + + + - ]: 53 : switch (ofp_version) {
8207 : : case OFP10_VERSION:
8208 : 1 : ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8209 : : "(\'-O OpenFlow11\')");
8210 : : case OFP11_VERSION:
8211 : : case OFP12_VERSION:
8212 : : case OFP13_VERSION:
8213 : : case OFP14_VERSION:
8214 : 18 : request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
8215 : : ofp_version, 0);
8216 : 18 : break;
8217 : : case OFP15_VERSION:
8218 : : case OFP16_VERSION: {
8219 : : struct ofp15_group_desc_request *req;
8220 : 34 : request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
8221 : : ofp_version, 0);
8222 : 34 : req = ofpbuf_put_zeros(request, sizeof *req);
8223 : 34 : req->group_id = htonl(group_id);
8224 : 34 : break;
8225 : : }
8226 : : default:
8227 : 0 : OVS_NOT_REACHED();
8228 : : }
8229 : :
8230 : 52 : return request;
8231 : : }
8232 : :
8233 : : static void
8234 : 11 : ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
8235 : : struct ofp11_bucket_counter bucket_cnts[])
8236 : : {
8237 : : int i;
8238 : :
8239 [ + + ]: 26 : for (i = 0; i < gs->n_buckets; i++) {
8240 : 15 : bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
8241 : 15 : bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
8242 : : }
8243 : 11 : }
8244 : :
8245 : : static void
8246 : 11 : ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
8247 : : struct ofp11_group_stats *gs11, size_t length,
8248 : : struct ofp11_bucket_counter bucket_cnts[])
8249 : : {
8250 : 11 : memset(gs11, 0, sizeof *gs11);
8251 : 11 : gs11->length = htons(length);
8252 : 11 : gs11->group_id = htonl(gs->group_id);
8253 : 11 : gs11->ref_count = htonl(gs->ref_count);
8254 : 11 : gs11->packet_count = htonll(gs->packet_count);
8255 : 11 : gs11->byte_count = htonll(gs->byte_count);
8256 : 11 : ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
8257 : 11 : }
8258 : :
8259 : : static void
8260 : 8 : ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
8261 : : struct ofp13_group_stats *gs13, size_t length,
8262 : : struct ofp11_bucket_counter bucket_cnts[])
8263 : : {
8264 : 8 : ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
8265 : 8 : gs13->duration_sec = htonl(gs->duration_sec);
8266 : 8 : gs13->duration_nsec = htonl(gs->duration_nsec);
8267 : :
8268 : 8 : }
8269 : :
8270 : : /* Encodes 'gs' properly for the format of the list of group statistics
8271 : : * replies already begun in 'replies' and appends it to the list. 'replies'
8272 : : * must have originally been initialized with ofpmp_init(). */
8273 : : void
8274 : 11 : ofputil_append_group_stats(struct ovs_list *replies,
8275 : : const struct ofputil_group_stats *gs)
8276 : : {
8277 : : size_t bucket_counter_size;
8278 : : struct ofp11_bucket_counter *bucket_counters;
8279 : : size_t length;
8280 : :
8281 : 11 : bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
8282 : :
8283 [ + + - ]: 11 : switch (ofpmp_version(replies)) {
8284 : : case OFP11_VERSION:
8285 : : case OFP12_VERSION:{
8286 : : struct ofp11_group_stats *gs11;
8287 : :
8288 : 3 : length = sizeof *gs11 + bucket_counter_size;
8289 : 3 : gs11 = ofpmp_append(replies, length);
8290 : 3 : bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
8291 : 3 : ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
8292 : 3 : break;
8293 : : }
8294 : :
8295 : : case OFP13_VERSION:
8296 : : case OFP14_VERSION:
8297 : : case OFP15_VERSION:
8298 : : case OFP16_VERSION: {
8299 : : struct ofp13_group_stats *gs13;
8300 : :
8301 : 8 : length = sizeof *gs13 + bucket_counter_size;
8302 : 8 : gs13 = ofpmp_append(replies, length);
8303 : 8 : bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
8304 : 8 : ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
8305 : 8 : break;
8306 : : }
8307 : :
8308 : : case OFP10_VERSION:
8309 : : default:
8310 : 0 : OVS_NOT_REACHED();
8311 : : }
8312 : 11 : }
8313 : : /* Returns an OpenFlow group features request for OpenFlow version
8314 : : * 'ofp_version'. */
8315 : : struct ofpbuf *
8316 : 1 : ofputil_encode_group_features_request(enum ofp_version ofp_version)
8317 : : {
8318 : 1 : struct ofpbuf *request = NULL;
8319 : :
8320 [ - + - ]: 1 : switch (ofp_version) {
8321 : : case OFP10_VERSION:
8322 : : case OFP11_VERSION:
8323 : 0 : ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8324 : : "(\'-O OpenFlow12\')");
8325 : : case OFP12_VERSION:
8326 : : case OFP13_VERSION:
8327 : : case OFP14_VERSION:
8328 : : case OFP15_VERSION:
8329 : : case OFP16_VERSION:
8330 : 1 : request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
8331 : : ofp_version, 0);
8332 : 1 : break;
8333 : : default:
8334 : 0 : OVS_NOT_REACHED();
8335 : : }
8336 : :
8337 : 1 : return request;
8338 : : }
8339 : :
8340 : : /* Returns a OpenFlow message that encodes 'features' properly as a reply to
8341 : : * group features request 'request'. */
8342 : : struct ofpbuf *
8343 : 1 : ofputil_encode_group_features_reply(
8344 : : const struct ofputil_group_features *features,
8345 : : const struct ofp_header *request)
8346 : : {
8347 : : struct ofp12_group_features_stats *ogf;
8348 : : struct ofpbuf *reply;
8349 : : int i;
8350 : :
8351 : 1 : reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
8352 : 1 : request->version, request->xid, 0);
8353 : 1 : ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
8354 : 1 : ogf->types = htonl(features->types);
8355 : 1 : ogf->capabilities = htonl(features->capabilities);
8356 [ + + ]: 5 : for (i = 0; i < OFPGT12_N_TYPES; i++) {
8357 : 4 : ogf->max_groups[i] = htonl(features->max_groups[i]);
8358 : 4 : ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
8359 : 4 : request->version);
8360 : : }
8361 : :
8362 : 1 : return reply;
8363 : : }
8364 : :
8365 : : /* Decodes group features reply 'oh' into 'features'. */
8366 : : void
8367 : 3 : ofputil_decode_group_features_reply(const struct ofp_header *oh,
8368 : : struct ofputil_group_features *features)
8369 : : {
8370 : 3 : const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
8371 : : int i;
8372 : :
8373 : 3 : features->types = ntohl(ogf->types);
8374 : 3 : features->capabilities = ntohl(ogf->capabilities);
8375 [ + + ]: 15 : for (i = 0; i < OFPGT12_N_TYPES; i++) {
8376 : 12 : features->max_groups[i] = ntohl(ogf->max_groups[i]);
8377 : 12 : features->ofpacts[i] = ofpact_bitmap_from_openflow(
8378 : 12 : ogf->actions[i], oh->version);
8379 : : }
8380 : 3 : }
8381 : :
8382 : : /* Parse a group status request message into a 32 bit OpenFlow 1.1
8383 : : * group ID and stores the latter in '*group_id'.
8384 : : * Returns 0 if successful, otherwise an OFPERR_* number. */
8385 : : enum ofperr
8386 : 17 : ofputil_decode_group_stats_request(const struct ofp_header *request,
8387 : : uint32_t *group_id)
8388 : : {
8389 : 17 : const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
8390 : 17 : *group_id = ntohl(gsr11->group_id);
8391 : 17 : return 0;
8392 : : }
8393 : :
8394 : : /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
8395 : : * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8396 : : * caller to eventually free.
8397 : : *
8398 : : * Multiple group stats replies can be packed into a single OpenFlow message.
8399 : : * Calling this function multiple times for a single 'msg' iterates through the
8400 : : * replies. The caller must initially leave 'msg''s layer pointers null and
8401 : : * not modify them between calls.
8402 : : *
8403 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
8404 : : * otherwise a positive errno value. */
8405 : : int
8406 : 48 : ofputil_decode_group_stats_reply(struct ofpbuf *msg,
8407 : : struct ofputil_group_stats *gs)
8408 : : {
8409 : : struct ofp11_bucket_counter *obc;
8410 : : struct ofp11_group_stats *ogs11;
8411 : : enum ofpraw raw;
8412 : : enum ofperr error;
8413 : : size_t base_len;
8414 : : size_t length;
8415 : : size_t i;
8416 : :
8417 : 48 : gs->bucket_stats = NULL;
8418 [ + + ]: 48 : error = (msg->header ? ofpraw_decode(&raw, msg->header)
8419 : : : ofpraw_pull(&raw, msg));
8420 [ - + ]: 48 : if (error) {
8421 : 0 : return error;
8422 : : }
8423 : :
8424 [ + + ]: 48 : if (!msg->size) {
8425 : 20 : return EOF;
8426 : : }
8427 : :
8428 [ + + ]: 28 : if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
8429 : 10 : base_len = sizeof *ogs11;
8430 : 10 : ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
8431 : 10 : gs->duration_sec = gs->duration_nsec = UINT32_MAX;
8432 [ + - ]: 18 : } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
8433 : : struct ofp13_group_stats *ogs13;
8434 : :
8435 : 18 : base_len = sizeof *ogs13;
8436 : 18 : ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
8437 [ + - ]: 18 : if (ogs13) {
8438 : 18 : ogs11 = &ogs13->gs;
8439 : 18 : gs->duration_sec = ntohl(ogs13->duration_sec);
8440 : 18 : gs->duration_nsec = ntohl(ogs13->duration_nsec);
8441 : : } else {
8442 : 18 : ogs11 = NULL;
8443 : : }
8444 : : } else {
8445 : 0 : OVS_NOT_REACHED();
8446 : : }
8447 : :
8448 [ - + ]: 28 : if (!ogs11) {
8449 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
8450 : : ofpraw_get_name(raw), msg->size);
8451 : 0 : return OFPERR_OFPBRC_BAD_LEN;
8452 : : }
8453 : 28 : length = ntohs(ogs11->length);
8454 [ - + ]: 28 : if (length < sizeof base_len) {
8455 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
8456 : : ofpraw_get_name(raw), length);
8457 : 0 : return OFPERR_OFPBRC_BAD_LEN;
8458 : : }
8459 : :
8460 : 28 : gs->group_id = ntohl(ogs11->group_id);
8461 : 28 : gs->ref_count = ntohl(ogs11->ref_count);
8462 : 28 : gs->packet_count = ntohll(ogs11->packet_count);
8463 : 28 : gs->byte_count = ntohll(ogs11->byte_count);
8464 : :
8465 : 28 : gs->n_buckets = (length - base_len) / sizeof *obc;
8466 : 28 : obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
8467 [ - + ]: 28 : if (!obc) {
8468 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
8469 : : ofpraw_get_name(raw), msg->size);
8470 : 0 : return OFPERR_OFPBRC_BAD_LEN;
8471 : : }
8472 : :
8473 : 28 : gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
8474 [ + + ]: 72 : for (i = 0; i < gs->n_buckets; i++) {
8475 : 44 : gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
8476 : 44 : gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
8477 : : }
8478 : :
8479 : 48 : return 0;
8480 : : }
8481 : :
8482 : : static void
8483 : 128 : ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
8484 : : struct ofpbuf *openflow, enum ofp_version ofp_version)
8485 : : {
8486 : : struct ofp11_bucket *ob;
8487 : : size_t start;
8488 : :
8489 : 128 : start = openflow->size;
8490 : 128 : ofpbuf_put_zeros(openflow, sizeof *ob);
8491 : 128 : ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8492 : : openflow, ofp_version);
8493 : 128 : ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
8494 : 128 : ob->len = htons(openflow->size - start);
8495 : 128 : ob->weight = htons(bucket->weight);
8496 : 128 : ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
8497 : 128 : ob->watch_group = htonl(bucket->watch_group);
8498 : 128 : }
8499 : :
8500 : : static void
8501 : 215 : ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
8502 : : uint32_t bucket_id, enum ofp11_group_type group_type,
8503 : : struct ofpbuf *openflow, enum ofp_version ofp_version)
8504 : : {
8505 : : struct ofp15_bucket *ob;
8506 : : size_t start, actions_start, actions_len;
8507 : :
8508 : 215 : start = openflow->size;
8509 : 215 : ofpbuf_put_zeros(openflow, sizeof *ob);
8510 : :
8511 : 215 : actions_start = openflow->size;
8512 : 215 : ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
8513 : : openflow, ofp_version);
8514 : 215 : actions_len = openflow->size - actions_start;
8515 : :
8516 [ + + ]: 215 : if (group_type == OFPGT11_SELECT) {
8517 : 32 : ofpprop_put_u16(openflow, OFPGBPT15_WEIGHT, bucket->weight);
8518 : : }
8519 [ - + ]: 215 : if (bucket->watch_port != OFPP_ANY) {
8520 : 0 : ofpprop_put_be32(openflow, OFPGBPT15_WATCH_PORT,
8521 : : ofputil_port_to_ofp11(bucket->watch_port));
8522 : : }
8523 [ - + ]: 215 : if (bucket->watch_group != OFPG_ANY) {
8524 : 0 : ofpprop_put_u32(openflow, OFPGBPT15_WATCH_GROUP, bucket->watch_group);
8525 : : }
8526 : :
8527 : 215 : ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
8528 : 215 : ob->len = htons(openflow->size - start);
8529 : 215 : ob->action_array_len = htons(actions_len);
8530 : 215 : ob->bucket_id = htonl(bucket_id);
8531 : 215 : }
8532 : :
8533 : : static void
8534 : 11 : ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
8535 : : const struct ofputil_group_props *gp,
8536 : : struct ofpbuf *openflow)
8537 : : {
8538 : : struct ntr_group_prop_selection_method *prop;
8539 : : size_t start;
8540 : :
8541 : 11 : start = openflow->size;
8542 : 11 : ofpbuf_put_zeros(openflow, sizeof *prop);
8543 : 11 : oxm_put_field_array(openflow, &gp->fields, ofp_version);
8544 : 11 : prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
8545 : 11 : prop->type = htons(OFPGPT15_EXPERIMENTER);
8546 : 11 : prop->experimenter = htonl(NTR_VENDOR_ID);
8547 : 11 : prop->exp_type = htonl(NTRT_SELECTION_METHOD);
8548 : 11 : strcpy(prop->selection_method, gp->selection_method);
8549 : 11 : prop->selection_method_param = htonll(gp->selection_method_param);
8550 : 11 : ofpprop_end(openflow, start);
8551 : 11 : }
8552 : :
8553 : : static void
8554 : 20 : ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
8555 : : const struct ovs_list *buckets,
8556 : : struct ovs_list *replies,
8557 : : enum ofp_version version)
8558 : : {
8559 : 20 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
8560 : : struct ofp11_group_desc_stats *ogds;
8561 : : struct ofputil_bucket *bucket;
8562 : : size_t start_ogds;
8563 : :
8564 : 20 : start_ogds = reply->size;
8565 : 20 : ofpbuf_put_zeros(reply, sizeof *ogds);
8566 [ + + ]: 50 : LIST_FOR_EACH (bucket, list_node, buckets) {
8567 : 30 : ofputil_put_ofp11_bucket(bucket, reply, version);
8568 : : }
8569 : 20 : ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8570 : 20 : ogds->length = htons(reply->size - start_ogds);
8571 : 20 : ogds->type = gds->type;
8572 : 20 : ogds->group_id = htonl(gds->group_id);
8573 : :
8574 : 20 : ofpmp_postappend(replies, start_ogds);
8575 : 20 : }
8576 : :
8577 : : static void
8578 : 36 : ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
8579 : : const struct ovs_list *buckets,
8580 : : struct ovs_list *replies,
8581 : : enum ofp_version version)
8582 : : {
8583 : 36 : struct ofpbuf *reply = ofpbuf_from_list(ovs_list_back(replies));
8584 : : struct ofp15_group_desc_stats *ogds;
8585 : : struct ofputil_bucket *bucket;
8586 : : size_t start_ogds, start_buckets;
8587 : :
8588 : 36 : start_ogds = reply->size;
8589 : 36 : ofpbuf_put_zeros(reply, sizeof *ogds);
8590 : 36 : start_buckets = reply->size;
8591 [ + + ]: 180 : LIST_FOR_EACH (bucket, list_node, buckets) {
8592 : 144 : ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
8593 : 144 : gds->type, reply, version);
8594 : : }
8595 : 36 : ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8596 : 36 : ogds->type = gds->type;
8597 : 36 : ogds->group_id = htonl(gds->group_id);
8598 : 36 : ogds->bucket_list_len = htons(reply->size - start_buckets);
8599 : :
8600 : : /* Add group properties */
8601 [ + + ]: 36 : if (gds->props.selection_method[0]) {
8602 : 6 : ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
8603 : : reply);
8604 : : }
8605 : 36 : ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8606 : 36 : ogds->length = htons(reply->size - start_ogds);
8607 : :
8608 : 36 : ofpmp_postappend(replies, start_ogds);
8609 : 36 : }
8610 : :
8611 : : /* Appends a group stats reply that contains the data in 'gds' to those already
8612 : : * present in the list of ofpbufs in 'replies'. 'replies' should have been
8613 : : * initialized with ofpmp_init(). */
8614 : : void
8615 : 56 : ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
8616 : : const struct ovs_list *buckets,
8617 : : struct ovs_list *replies)
8618 : : {
8619 : 56 : enum ofp_version version = ofpmp_version(replies);
8620 : :
8621 [ + + - ]: 56 : switch (version)
8622 : : {
8623 : : case OFP11_VERSION:
8624 : : case OFP12_VERSION:
8625 : : case OFP13_VERSION:
8626 : : case OFP14_VERSION:
8627 : 20 : ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
8628 : 20 : break;
8629 : :
8630 : : case OFP15_VERSION:
8631 : : case OFP16_VERSION:
8632 : 36 : ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
8633 : 36 : break;
8634 : :
8635 : : case OFP10_VERSION:
8636 : : default:
8637 : 0 : OVS_NOT_REACHED();
8638 : : }
8639 : 56 : }
8640 : :
8641 : : static enum ofperr
8642 : 316 : ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
8643 : : enum ofp_version version, struct ovs_list *buckets)
8644 : : {
8645 : : struct ofp11_bucket *ob;
8646 : 316 : uint32_t bucket_id = 0;
8647 : :
8648 : 316 : ovs_list_init(buckets);
8649 [ + + ]: 538 : while (buckets_length > 0) {
8650 : : struct ofputil_bucket *bucket;
8651 : : struct ofpbuf ofpacts;
8652 : : enum ofperr error;
8653 : : size_t ob_len;
8654 : :
8655 : 222 : ob = (buckets_length >= sizeof *ob
8656 : : ? ofpbuf_try_pull(msg, sizeof *ob)
8657 [ + - ]: 222 : : NULL);
8658 [ - + ]: 222 : if (!ob) {
8659 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
8660 : : buckets_length);
8661 : 0 : return OFPERR_OFPGMFC_BAD_BUCKET;
8662 : : }
8663 : :
8664 : 222 : ob_len = ntohs(ob->len);
8665 [ - + ]: 222 : if (ob_len < sizeof *ob) {
8666 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8667 : : "%"PRIuSIZE" is not valid", ob_len);
8668 : 0 : return OFPERR_OFPGMFC_BAD_BUCKET;
8669 [ - + ]: 222 : } else if (ob_len > buckets_length) {
8670 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8671 : : "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
8672 : : ob_len, buckets_length);
8673 : 0 : return OFPERR_OFPGMFC_BAD_BUCKET;
8674 : : }
8675 : 222 : buckets_length -= ob_len;
8676 : :
8677 : 222 : ofpbuf_init(&ofpacts, 0);
8678 : 222 : error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
8679 : : version, &ofpacts);
8680 [ - + ]: 222 : if (error) {
8681 : 0 : ofpbuf_uninit(&ofpacts);
8682 : 0 : ofputil_bucket_list_destroy(buckets);
8683 : 0 : return error;
8684 : : }
8685 : :
8686 : 222 : bucket = xzalloc(sizeof *bucket);
8687 : 222 : bucket->weight = ntohs(ob->weight);
8688 : 222 : error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8689 [ - + ]: 222 : if (error) {
8690 : 0 : ofpbuf_uninit(&ofpacts);
8691 : 0 : ofputil_bucket_list_destroy(buckets);
8692 : 0 : return OFPERR_OFPGMFC_BAD_WATCH;
8693 : : }
8694 : 222 : bucket->watch_group = ntohl(ob->watch_group);
8695 : 222 : bucket->bucket_id = bucket_id++;
8696 : :
8697 : 222 : bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8698 : 222 : bucket->ofpacts_len = ofpacts.size;
8699 : 222 : ovs_list_push_back(buckets, &bucket->list_node);
8700 : : }
8701 : :
8702 : 316 : return 0;
8703 : : }
8704 : :
8705 : : static enum ofperr
8706 : 196 : ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
8707 : : enum ofp_version version, uint8_t group_type,
8708 : : struct ovs_list *buckets)
8709 : : {
8710 : : struct ofp15_bucket *ob;
8711 : :
8712 : 196 : ovs_list_init(buckets);
8713 [ + + ]: 627 : while (buckets_length > 0) {
8714 : 431 : struct ofputil_bucket *bucket = NULL;
8715 : : struct ofpbuf ofpacts;
8716 : 431 : enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
8717 : : size_t ob_len, actions_len, properties_len;
8718 : 431 : ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8719 : 431 : ovs_be32 watch_group = htonl(OFPG_ANY);
8720 : 431 : ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
8721 : :
8722 : 431 : ofpbuf_init(&ofpacts, 0);
8723 : :
8724 : 431 : ob = ofpbuf_try_pull(msg, sizeof *ob);
8725 [ - + ]: 431 : if (!ob) {
8726 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
8727 : : " leftover bytes", buckets_length);
8728 : 0 : goto err;
8729 : : }
8730 : :
8731 : 431 : ob_len = ntohs(ob->len);
8732 : 431 : actions_len = ntohs(ob->action_array_len);
8733 : :
8734 [ - + ]: 431 : if (ob_len < sizeof *ob) {
8735 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8736 : : "%"PRIuSIZE" is not valid", ob_len);
8737 : 0 : goto err;
8738 [ - + ]: 431 : } else if (ob_len > buckets_length) {
8739 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8740 : : "%"PRIuSIZE" exceeds remaining buckets data size %"
8741 : : PRIuSIZE, ob_len, buckets_length);
8742 : 0 : goto err;
8743 [ - + ]: 431 : } else if (actions_len > ob_len - sizeof *ob) {
8744 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
8745 : : "length %"PRIuSIZE" exceeds remaining bucket "
8746 : : "data size %"PRIuSIZE, actions_len,
8747 : : ob_len - sizeof *ob);
8748 : 0 : goto err;
8749 : : }
8750 : 431 : buckets_length -= ob_len;
8751 : :
8752 : 431 : err = ofpacts_pull_openflow_actions(msg, actions_len, version,
8753 : : &ofpacts);
8754 [ - + ]: 431 : if (err) {
8755 : 0 : goto err;
8756 : : }
8757 : :
8758 : 431 : properties_len = ob_len - sizeof *ob - actions_len;
8759 : 431 : struct ofpbuf properties = ofpbuf_const_initializer(
8760 : 431 : ofpbuf_pull(msg, properties_len), properties_len);
8761 [ + + ]: 503 : while (properties.size > 0) {
8762 : : struct ofpbuf payload;
8763 : : uint64_t type;
8764 : :
8765 : 72 : err = ofpprop_pull(&properties, &payload, &type);
8766 [ - + ]: 72 : if (err) {
8767 : 0 : goto err;
8768 : : }
8769 : :
8770 [ + + - - ]: 72 : switch (type) {
8771 : : case OFPGBPT15_WEIGHT:
8772 : 63 : err = ofpprop_parse_be16(&payload, &weight);
8773 : 63 : break;
8774 : :
8775 : : case OFPGBPT15_WATCH_PORT:
8776 : 9 : err = ofpprop_parse_be32(&payload, &watch_port);
8777 : 9 : break;
8778 : :
8779 : : case OFPGBPT15_WATCH_GROUP:
8780 : 0 : err = ofpprop_parse_be32(&payload, &watch_group);
8781 : 0 : break;
8782 : :
8783 : : default:
8784 : 0 : err = OFPPROP_UNKNOWN(false, "group bucket", type);
8785 : 0 : break;
8786 : : }
8787 : :
8788 [ - + ]: 72 : if (err) {
8789 : 72 : goto err;
8790 : : }
8791 : : }
8792 : :
8793 : 431 : bucket = xzalloc(sizeof *bucket);
8794 : :
8795 : 431 : bucket->weight = ntohs(weight);
8796 : 431 : err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
8797 [ - + ]: 431 : if (err) {
8798 : 0 : err = OFPERR_OFPGMFC_BAD_WATCH;
8799 : 0 : goto err;
8800 : : }
8801 : 431 : bucket->watch_group = ntohl(watch_group);
8802 : 431 : bucket->bucket_id = ntohl(ob->bucket_id);
8803 [ - + ]: 431 : if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8804 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
8805 : : bucket->bucket_id);
8806 : 0 : err = OFPERR_OFPGMFC_BAD_BUCKET;
8807 : 0 : goto err;
8808 : : }
8809 : :
8810 : 431 : bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8811 : 431 : bucket->ofpacts_len = ofpacts.size;
8812 : 431 : ovs_list_push_back(buckets, &bucket->list_node);
8813 : :
8814 : 431 : continue;
8815 : :
8816 : : err:
8817 : 0 : free(bucket);
8818 : 0 : ofpbuf_uninit(&ofpacts);
8819 : 0 : ofputil_bucket_list_destroy(buckets);
8820 : 0 : return err;
8821 : : }
8822 : :
8823 [ - + ]: 196 : if (ofputil_bucket_check_duplicate_id(buckets)) {
8824 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
8825 : 0 : ofputil_bucket_list_destroy(buckets);
8826 : 0 : return OFPERR_OFPGMFC_BAD_BUCKET;
8827 : : }
8828 : :
8829 : 196 : return 0;
8830 : : }
8831 : :
8832 : : static void
8833 : 618 : ofputil_init_group_properties(struct ofputil_group_props *gp)
8834 : : {
8835 : 618 : memset(gp, 0, sizeof *gp);
8836 : 618 : }
8837 : :
8838 : : void
8839 : 103 : ofputil_group_properties_copy(struct ofputil_group_props *to,
8840 : : const struct ofputil_group_props *from)
8841 : : {
8842 : 103 : *to = *from;
8843 : 103 : to->fields.values = xmemdup(from->fields.values, from->fields.values_size);
8844 : 103 : }
8845 : :
8846 : : void
8847 : 788 : ofputil_group_properties_destroy(struct ofputil_group_props *gp)
8848 : : {
8849 : 788 : free(gp->fields.values);
8850 : 788 : }
8851 : :
8852 : : static enum ofperr
8853 : 22 : parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
8854 : : enum ofp11_group_type group_type,
8855 : : enum ofp15_group_mod_command group_cmd,
8856 : : struct ofputil_group_props *gp)
8857 : : {
8858 : 22 : struct ntr_group_prop_selection_method *prop = payload->data;
8859 : : size_t fields_len, method_len;
8860 : : enum ofperr error;
8861 : :
8862 [ + - - ]: 22 : switch (group_type) {
8863 : : case OFPGT11_SELECT:
8864 : 22 : break;
8865 : : case OFPGT11_ALL:
8866 : : case OFPGT11_INDIRECT:
8867 : : case OFPGT11_FF:
8868 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8869 : : "only allowed for select groups");
8870 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
8871 : : default:
8872 : 0 : OVS_NOT_REACHED();
8873 : : }
8874 : :
8875 [ + - - ]: 22 : switch (group_cmd) {
8876 : : case OFPGC15_ADD:
8877 : : case OFPGC15_MODIFY:
8878 : : case OFPGC15_ADD_OR_MOD:
8879 : 22 : break;
8880 : : case OFPGC15_DELETE:
8881 : : case OFPGC15_INSERT_BUCKET:
8882 : : case OFPGC15_REMOVE_BUCKET:
8883 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8884 : : "only allowed for add and delete group modifications");
8885 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
8886 : : default:
8887 : 0 : OVS_NOT_REACHED();
8888 : : }
8889 : :
8890 [ - + ]: 22 : if (payload->size < sizeof *prop) {
8891 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
8892 : : "length %u is not valid", payload->size);
8893 : 0 : return OFPERR_OFPBPC_BAD_LEN;
8894 : : }
8895 : :
8896 : 22 : method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
8897 : :
8898 [ - + ]: 22 : if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
8899 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false,
8900 : : "ntr selection method is not null terminated");
8901 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
8902 : : }
8903 : :
8904 [ - + ]: 22 : if (strcmp("hash", prop->selection_method)) {
8905 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false,
8906 : : "ntr selection method '%s' is not supported",
8907 : : prop->selection_method);
8908 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
8909 : : }
8910 : :
8911 : 22 : strcpy(gp->selection_method, prop->selection_method);
8912 : 22 : gp->selection_method_param = ntohll(prop->selection_method_param);
8913 : :
8914 [ - + ][ # # ]: 22 : if (!method_len && gp->selection_method_param) {
8915 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8916 : : "non-zero but selection method is empty");
8917 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
8918 : : }
8919 : :
8920 : 22 : ofpbuf_pull(payload, sizeof *prop);
8921 : :
8922 : 22 : fields_len = ntohs(prop->length) - sizeof *prop;
8923 [ - + ][ # # ]: 22 : if (!method_len && fields_len) {
8924 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8925 : : "zero but fields are provided");
8926 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
8927 : : }
8928 : :
8929 : 22 : error = oxm_pull_field_array(payload->data, fields_len,
8930 : : &gp->fields);
8931 [ - + ]: 22 : if (error) {
8932 [ # # ]: 0 : OFPPROP_LOG(&bad_ofmsg_rl, false,
8933 : : "ntr selection method fields are invalid");
8934 : 0 : return error;
8935 : : }
8936 : :
8937 : 22 : return 0;
8938 : : }
8939 : :
8940 : : static enum ofperr
8941 : 196 : parse_ofp15_group_properties(struct ofpbuf *msg,
8942 : : enum ofp11_group_type group_type,
8943 : : enum ofp15_group_mod_command group_cmd,
8944 : : struct ofputil_group_props *gp,
8945 : : size_t properties_len)
8946 : : {
8947 : 196 : struct ofpbuf properties = ofpbuf_const_initializer(
8948 : 196 : ofpbuf_pull(msg, properties_len), properties_len);
8949 [ + + ]: 218 : while (properties.size > 0) {
8950 : : struct ofpbuf payload;
8951 : : enum ofperr error;
8952 : : uint64_t type;
8953 : :
8954 : 22 : error = ofpprop_pull(&properties, &payload, &type);
8955 [ - + ]: 22 : if (error) {
8956 : 0 : return error;
8957 : : }
8958 : :
8959 [ + - ]: 22 : switch (type) {
8960 : : case OFPPROP_EXP(NTR_VENDOR_ID, NTRT_SELECTION_METHOD):
8961 : : case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID, NTRT_SELECTION_METHOD):
8962 : 22 : error = parse_group_prop_ntr_selection_method(&payload, group_type,
8963 : : group_cmd, gp);
8964 : 22 : break;
8965 : :
8966 : : default:
8967 : 0 : error = OFPPROP_UNKNOWN(false, "group", type);
8968 : 0 : break;
8969 : : }
8970 : :
8971 [ - + ]: 22 : if (error) {
8972 : 22 : return error;
8973 : : }
8974 : : }
8975 : :
8976 : 196 : return 0;
8977 : : }
8978 : :
8979 : : static int
8980 : 78 : ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
8981 : : struct ofpbuf *msg,
8982 : : enum ofp_version version)
8983 : : {
8984 : : struct ofp11_group_desc_stats *ogds;
8985 : : size_t length;
8986 : :
8987 [ + + ]: 78 : if (!msg->header) {
8988 : 37 : ofpraw_pull_assert(msg);
8989 : : }
8990 : :
8991 [ + + ]: 78 : if (!msg->size) {
8992 : 37 : return EOF;
8993 : : }
8994 : :
8995 : 41 : ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8996 [ - + ]: 41 : if (!ogds) {
8997 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8998 : : "leftover bytes at end", msg->size);
8999 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9000 : : }
9001 : 41 : gd->type = ogds->type;
9002 : 41 : gd->group_id = ntohl(ogds->group_id);
9003 : :
9004 : 41 : length = ntohs(ogds->length);
9005 [ + - ][ - + ]: 41 : if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
9006 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9007 : : "length %"PRIuSIZE, length);
9008 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9009 : : }
9010 : :
9011 : 41 : return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
9012 : : &gd->buckets);
9013 : : }
9014 : :
9015 : : static int
9016 : 142 : ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
9017 : : struct ofpbuf *msg,
9018 : : enum ofp_version version)
9019 : : {
9020 : : struct ofp15_group_desc_stats *ogds;
9021 : : uint16_t length, bucket_list_len;
9022 : : int error;
9023 : :
9024 [ + + ]: 142 : if (!msg->header) {
9025 : 69 : ofpraw_pull_assert(msg);
9026 : : }
9027 : :
9028 [ + + ]: 142 : if (!msg->size) {
9029 : 69 : return EOF;
9030 : : }
9031 : :
9032 : 73 : ogds = ofpbuf_try_pull(msg, sizeof *ogds);
9033 [ - + ]: 73 : if (!ogds) {
9034 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
9035 : : "leftover bytes at end", msg->size);
9036 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9037 : : }
9038 : 73 : gd->type = ogds->type;
9039 : 73 : gd->group_id = ntohl(ogds->group_id);
9040 : :
9041 : 73 : length = ntohs(ogds->length);
9042 [ + - ][ - + ]: 73 : if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
9043 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9044 : : "length %u", length);
9045 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9046 : : }
9047 : :
9048 : 73 : bucket_list_len = ntohs(ogds->bucket_list_len);
9049 [ - + ]: 73 : if (length < bucket_list_len + sizeof *ogds) {
9050 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
9051 : : "bucket list length %u", bucket_list_len);
9052 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9053 : : }
9054 : 73 : error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
9055 : : &gd->buckets);
9056 [ - + ]: 73 : if (error) {
9057 : 0 : return error;
9058 : : }
9059 : :
9060 : : /* By definition group desc messages don't have a group mod command.
9061 : : * However, parse_group_prop_ntr_selection_method() checks to make sure
9062 : : * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9063 : : * against group mod messages with other commands supplying
9064 : : * a NTR selection method group experimenter property.
9065 : : * Such properties are valid for group desc replies so
9066 : : * claim that the group mod command is OFPGC15_ADD to
9067 : : * satisfy the check in parse_group_prop_ntr_selection_method() */
9068 : 73 : return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
9069 : 73 : length - sizeof *ogds - bucket_list_len);
9070 : : }
9071 : :
9072 : : /* Converts a group description reply in 'msg' into an abstract
9073 : : * ofputil_group_desc in 'gd'.
9074 : : *
9075 : : * Multiple group description replies can be packed into a single OpenFlow
9076 : : * message. Calling this function multiple times for a single 'msg' iterates
9077 : : * through the replies. The caller must initially leave 'msg''s layer pointers
9078 : : * null and not modify them between calls.
9079 : : *
9080 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
9081 : : * otherwise a positive errno value. */
9082 : : int
9083 : 220 : ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
9084 : : struct ofpbuf *msg, enum ofp_version version)
9085 : : {
9086 : 220 : ofputil_init_group_properties(&gd->props);
9087 : :
9088 [ + + - ]: 220 : switch (version)
9089 : : {
9090 : : case OFP11_VERSION:
9091 : : case OFP12_VERSION:
9092 : : case OFP13_VERSION:
9093 : : case OFP14_VERSION:
9094 : 78 : return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
9095 : :
9096 : : case OFP15_VERSION:
9097 : : case OFP16_VERSION:
9098 : 142 : return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
9099 : :
9100 : : case OFP10_VERSION:
9101 : : default:
9102 : 0 : OVS_NOT_REACHED();
9103 : : }
9104 : : }
9105 : :
9106 : : void
9107 : 600 : ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
9108 : : {
9109 : 600 : ofputil_bucket_list_destroy(&gm->buckets);
9110 : 600 : ofputil_group_properties_destroy(&gm->props);
9111 : 600 : }
9112 : :
9113 : : static struct ofpbuf *
9114 : 145 : ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
9115 : : const struct ofputil_group_mod *gm)
9116 : : {
9117 : : struct ofpbuf *b;
9118 : : struct ofp11_group_mod *ogm;
9119 : : size_t start_ogm;
9120 : : struct ofputil_bucket *bucket;
9121 : :
9122 : 145 : b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
9123 : 145 : start_ogm = b->size;
9124 : 145 : ofpbuf_put_zeros(b, sizeof *ogm);
9125 : :
9126 [ + + ]: 243 : LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9127 : 98 : ofputil_put_ofp11_bucket(bucket, b, ofp_version);
9128 : : }
9129 : 145 : ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9130 : 145 : ogm->command = htons(gm->command);
9131 : 145 : ogm->type = gm->type;
9132 : 145 : ogm->group_id = htonl(gm->group_id);
9133 : :
9134 : 145 : return b;
9135 : : }
9136 : :
9137 : : static struct ofpbuf *
9138 : 60 : ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
9139 : : const struct ofputil_group_mod *gm)
9140 : : {
9141 : : struct ofpbuf *b;
9142 : : struct ofp15_group_mod *ogm;
9143 : : size_t start_ogm;
9144 : : struct ofputil_bucket *bucket;
9145 : 60 : struct id_pool *bucket_ids = NULL;
9146 : :
9147 : 60 : b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
9148 : 60 : start_ogm = b->size;
9149 : 60 : ofpbuf_put_zeros(b, sizeof *ogm);
9150 : :
9151 [ + + ]: 131 : LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9152 : : uint32_t bucket_id;
9153 : :
9154 : : /* Generate a bucket id if none was supplied */
9155 [ + + ]: 71 : if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
9156 [ + + ]: 23 : if (!bucket_ids) {
9157 : : const struct ofputil_bucket *bkt;
9158 : :
9159 : 12 : bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
9160 : :
9161 : : /* Mark all bucket_ids that are present in gm
9162 : : * as used in the pool. */
9163 [ + - ]: 27 : LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
9164 [ + + ]: 27 : if (bkt == bucket) {
9165 : 12 : break;
9166 : : }
9167 [ + + ]: 15 : if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
9168 : 4 : id_pool_add(bucket_ids, bkt->bucket_id);
9169 : : }
9170 : : }
9171 : : }
9172 : :
9173 [ - + ]: 23 : if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
9174 : 0 : OVS_NOT_REACHED();
9175 : : }
9176 : : } else {
9177 : 48 : bucket_id = bucket->bucket_id;
9178 : : }
9179 : :
9180 : 71 : ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
9181 : : }
9182 : 60 : ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
9183 : 60 : ogm->command = htons(gm->command);
9184 : 60 : ogm->type = gm->type;
9185 : 60 : ogm->group_id = htonl(gm->group_id);
9186 : 60 : ogm->command_bucket_id = htonl(gm->command_bucket_id);
9187 : 60 : ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
9188 : :
9189 : : /* Add group properties */
9190 [ + + ]: 60 : if (gm->props.selection_method[0]) {
9191 : 5 : ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
9192 : : }
9193 : :
9194 : 60 : id_pool_destroy(bucket_ids);
9195 : 60 : return b;
9196 : : }
9197 : :
9198 : : static void
9199 : 5 : bad_group_cmd(enum ofp15_group_mod_command cmd)
9200 : : {
9201 : : const char *opt_version;
9202 : : const char *version;
9203 : : const char *cmd_str;
9204 : :
9205 [ + + - ]: 5 : switch (cmd) {
9206 : : case OFPGC15_ADD:
9207 : : case OFPGC15_MODIFY:
9208 : : case OFPGC15_ADD_OR_MOD:
9209 : : case OFPGC15_DELETE:
9210 : 1 : version = "1.1";
9211 : 1 : opt_version = "11";
9212 : 1 : break;
9213 : :
9214 : : case OFPGC15_INSERT_BUCKET:
9215 : : case OFPGC15_REMOVE_BUCKET:
9216 : 4 : version = "1.5";
9217 : 4 : opt_version = "15";
9218 : 4 : break;
9219 : :
9220 : : default:
9221 : 0 : OVS_NOT_REACHED();
9222 : : }
9223 : :
9224 [ - + - + : 5 : switch (cmd) {
+ - ]
9225 : : case OFPGC15_ADD:
9226 : 0 : cmd_str = "add-group";
9227 : 0 : break;
9228 : :
9229 : : case OFPGC15_MODIFY:
9230 : : case OFPGC15_ADD_OR_MOD:
9231 : 1 : cmd_str = "mod-group";
9232 : 1 : break;
9233 : :
9234 : : case OFPGC15_DELETE:
9235 : 0 : cmd_str = "del-group";
9236 : 0 : break;
9237 : :
9238 : : case OFPGC15_INSERT_BUCKET:
9239 : 2 : cmd_str = "insert-bucket";
9240 : 2 : break;
9241 : :
9242 : : case OFPGC15_REMOVE_BUCKET:
9243 : 2 : cmd_str = "remove-bucket";
9244 : 2 : break;
9245 : :
9246 : : default:
9247 : 0 : OVS_NOT_REACHED();
9248 : : }
9249 : :
9250 : 5 : ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9251 : : cmd_str, version, opt_version);
9252 : :
9253 : : }
9254 : :
9255 : : /* Converts abstract group mod 'gm' into a message for OpenFlow version
9256 : : * 'ofp_version' and returns the message. */
9257 : : struct ofpbuf *
9258 : 210 : ofputil_encode_group_mod(enum ofp_version ofp_version,
9259 : : const struct ofputil_group_mod *gm)
9260 : : {
9261 : :
9262 [ + + + - ]: 210 : switch (ofp_version) {
9263 : : case OFP10_VERSION:
9264 : 1 : bad_group_cmd(gm->command);
9265 : :
9266 : : case OFP11_VERSION:
9267 : : case OFP12_VERSION:
9268 : : case OFP13_VERSION:
9269 : : case OFP14_VERSION:
9270 [ + + ][ + + ]: 149 : if (gm->command > OFPGC11_DELETE && gm->command != OFPGC11_ADD_OR_MOD) {
9271 : 4 : bad_group_cmd(gm->command);
9272 : : }
9273 : 145 : return ofputil_encode_ofp11_group_mod(ofp_version, gm);
9274 : :
9275 : : case OFP15_VERSION:
9276 : : case OFP16_VERSION:
9277 : 60 : return ofputil_encode_ofp15_group_mod(ofp_version, gm);
9278 : :
9279 : : default:
9280 : 0 : OVS_NOT_REACHED();
9281 : : }
9282 : : }
9283 : :
9284 : : static enum ofperr
9285 : 275 : ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9286 : : struct ofputil_group_mod *gm)
9287 : : {
9288 : : const struct ofp11_group_mod *ogm;
9289 : : enum ofperr error;
9290 : :
9291 : 275 : ogm = ofpbuf_pull(msg, sizeof *ogm);
9292 : 275 : gm->command = ntohs(ogm->command);
9293 : 275 : gm->type = ogm->type;
9294 : 275 : gm->group_id = ntohl(ogm->group_id);
9295 : 275 : gm->command_bucket_id = OFPG15_BUCKET_ALL;
9296 : :
9297 : 275 : error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
9298 : : &gm->buckets);
9299 : :
9300 : : /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9301 [ + - ]: 275 : if (!error
9302 [ + + ]: 275 : && ofp_version >= OFP13_VERSION
9303 [ + + ]: 228 : && gm->command == OFPGC11_DELETE
9304 [ - + ]: 128 : && !ovs_list_is_empty(&gm->buckets)) {
9305 : 0 : error = OFPERR_OFPGMFC_INVALID_GROUP;
9306 : : }
9307 : :
9308 : 275 : return error;
9309 : : }
9310 : :
9311 : : static enum ofperr
9312 : 123 : ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
9313 : : struct ofputil_group_mod *gm)
9314 : : {
9315 : : const struct ofp15_group_mod *ogm;
9316 : : uint16_t bucket_list_len;
9317 : 123 : enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
9318 : :
9319 : 123 : ogm = ofpbuf_pull(msg, sizeof *ogm);
9320 : 123 : gm->command = ntohs(ogm->command);
9321 : 123 : gm->type = ogm->type;
9322 : 123 : gm->group_id = ntohl(ogm->group_id);
9323 : :
9324 : 123 : gm->command_bucket_id = ntohl(ogm->command_bucket_id);
9325 [ + + + ]: 123 : switch (gm->command) {
9326 : : case OFPGC15_REMOVE_BUCKET:
9327 [ + + ]: 36 : if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9328 : 8 : error = 0;
9329 : : }
9330 : : /* Fall through */
9331 : : case OFPGC15_INSERT_BUCKET:
9332 [ + + ][ + + ]: 70 : if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
9333 : 46 : gm->command_bucket_id == OFPG15_BUCKET_FIRST
9334 [ + + ]: 29 : || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
9335 : 62 : error = 0;
9336 : : }
9337 : 70 : break;
9338 : :
9339 : : case OFPGC11_ADD:
9340 : : case OFPGC11_MODIFY:
9341 : : case OFPGC11_ADD_OR_MOD:
9342 : : case OFPGC11_DELETE:
9343 : : default:
9344 [ + - ]: 53 : if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
9345 : 53 : error = 0;
9346 : : }
9347 : 53 : break;
9348 : : }
9349 [ - + ]: 123 : if (error) {
9350 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
9351 : : "group command bucket id (%u) is out of range",
9352 : : gm->command_bucket_id);
9353 : 0 : return OFPERR_OFPGMFC_BAD_BUCKET;
9354 : : }
9355 : :
9356 : 123 : bucket_list_len = ntohs(ogm->bucket_array_len);
9357 : 123 : error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
9358 : 123 : gm->type, &gm->buckets);
9359 [ - + ]: 123 : if (error) {
9360 : 0 : return error;
9361 : : }
9362 : :
9363 : 123 : return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
9364 : 123 : msg->size);
9365 : : }
9366 : :
9367 : : /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9368 : : * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9369 : : enum ofperr
9370 : 398 : ofputil_decode_group_mod(const struct ofp_header *oh,
9371 : : struct ofputil_group_mod *gm)
9372 : : {
9373 : 398 : ofputil_init_group_properties(&gm->props);
9374 : :
9375 : 398 : enum ofp_version ofp_version = oh->version;
9376 : 398 : struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
9377 : 398 : ofpraw_pull_assert(&msg);
9378 : :
9379 : : enum ofperr err;
9380 [ + + - ]: 398 : switch (ofp_version)
9381 : : {
9382 : : case OFP11_VERSION:
9383 : : case OFP12_VERSION:
9384 : : case OFP13_VERSION:
9385 : : case OFP14_VERSION:
9386 : 275 : err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
9387 : 275 : break;
9388 : :
9389 : : case OFP15_VERSION:
9390 : : case OFP16_VERSION:
9391 : 123 : err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
9392 : 123 : break;
9393 : :
9394 : : case OFP10_VERSION:
9395 : : default:
9396 : 0 : OVS_NOT_REACHED();
9397 : : }
9398 [ - + ]: 398 : if (err) {
9399 : 0 : return err;
9400 : : }
9401 : :
9402 [ + + - ]: 398 : switch (gm->type) {
9403 : : case OFPGT11_INDIRECT:
9404 [ + - ]: 30 : if (gm->command != OFPGC11_DELETE
9405 [ + + ]: 30 : && !ovs_list_is_singleton(&gm->buckets) ) {
9406 : 8 : return OFPERR_OFPGMFC_INVALID_GROUP;
9407 : : }
9408 : 22 : break;
9409 : : case OFPGT11_ALL:
9410 : : case OFPGT11_SELECT:
9411 : : case OFPGT11_FF:
9412 : 368 : break;
9413 : : default:
9414 : 0 : return OFPERR_OFPGMFC_BAD_TYPE;
9415 : : }
9416 : :
9417 [ + + - ]: 390 : switch (gm->command) {
9418 : : case OFPGC11_ADD:
9419 : : case OFPGC11_MODIFY:
9420 : : case OFPGC11_ADD_OR_MOD:
9421 : : case OFPGC11_DELETE:
9422 : : case OFPGC15_INSERT_BUCKET:
9423 : 354 : break;
9424 : : case OFPGC15_REMOVE_BUCKET:
9425 [ - + ]: 36 : if (!ovs_list_is_empty(&gm->buckets)) {
9426 : 0 : return OFPERR_OFPGMFC_BAD_BUCKET;
9427 : : }
9428 : 36 : break;
9429 : : default:
9430 : 0 : return OFPERR_OFPGMFC_BAD_COMMAND;
9431 : : }
9432 : :
9433 : : struct ofputil_bucket *bucket;
9434 [ + + ]: 689 : LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
9435 [ + + ][ - + ]: 299 : if (bucket->weight && gm->type != OFPGT11_SELECT) {
9436 : 0 : return OFPERR_OFPGMFC_INVALID_GROUP;
9437 : : }
9438 : :
9439 [ + + - - ]: 299 : switch (gm->type) {
9440 : : case OFPGT11_ALL:
9441 : : case OFPGT11_INDIRECT:
9442 [ - + ]: 213 : if (ofputil_bucket_has_liveness(bucket)) {
9443 : 0 : return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
9444 : : }
9445 : 213 : break;
9446 : : case OFPGT11_SELECT:
9447 : 86 : break;
9448 : : case OFPGT11_FF:
9449 [ # # ]: 0 : if (!ofputil_bucket_has_liveness(bucket)) {
9450 : 0 : return OFPERR_OFPGMFC_INVALID_GROUP;
9451 : : }
9452 : 0 : break;
9453 : : default:
9454 : : /* Returning BAD TYPE to be consistent
9455 : : * though gm->type has been checked already. */
9456 : 0 : return OFPERR_OFPGMFC_BAD_TYPE;
9457 : : }
9458 : : }
9459 : :
9460 : 398 : return 0;
9461 : : }
9462 : :
9463 : : /* Destroys 'bms'. */
9464 : : void
9465 : 2 : ofputil_encode_bundle_msgs(struct ofputil_bundle_msg *bms, size_t n_bms,
9466 : : struct ovs_list *requests,
9467 : : enum ofputil_protocol protocol)
9468 : : {
9469 : 2 : enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
9470 : :
9471 [ + + ]: 14 : for (size_t i = 0; i < n_bms; i++) {
9472 : 12 : struct ofpbuf *request = NULL;
9473 : :
9474 [ + + - ]: 12 : switch ((int)bms[i].type) {
9475 : : case OFPTYPE_FLOW_MOD:
9476 : 8 : request = ofputil_encode_flow_mod(&bms[i].fm, protocol);
9477 : 8 : free(CONST_CAST(struct ofpact *, bms[i].fm.ofpacts));
9478 : 8 : break;
9479 : : case OFPTYPE_GROUP_MOD:
9480 : 4 : request = ofputil_encode_group_mod(version, &bms[i].gm);
9481 : 4 : ofputil_uninit_group_mod(&bms[i].gm);
9482 : 4 : break;
9483 : : default:
9484 : 0 : break;
9485 : : }
9486 [ + - ]: 12 : if (request) {
9487 : 12 : ovs_list_push_back(requests, &request->list_node);
9488 : : }
9489 : : }
9490 : 2 : free(bms);
9491 : 2 : }
9492 : :
9493 : : /* Parse a queue status request message into 'oqsr'.
9494 : : * Returns 0 if successful, otherwise an OFPERR_* number. */
9495 : : enum ofperr
9496 : 104 : ofputil_decode_queue_stats_request(const struct ofp_header *request,
9497 : : struct ofputil_queue_stats_request *oqsr)
9498 : : {
9499 [ + + - ]: 104 : switch ((enum ofp_version)request->version) {
9500 : : case OFP16_VERSION:
9501 : : case OFP15_VERSION:
9502 : : case OFP14_VERSION:
9503 : : case OFP13_VERSION:
9504 : : case OFP12_VERSION:
9505 : : case OFP11_VERSION: {
9506 : 83 : const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
9507 : 83 : oqsr->queue_id = ntohl(qsr11->queue_id);
9508 : 83 : return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
9509 : : }
9510 : :
9511 : : case OFP10_VERSION: {
9512 : 21 : const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
9513 : 21 : oqsr->queue_id = ntohl(qsr10->queue_id);
9514 : 21 : oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
9515 : : /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9516 [ + + ]: 21 : if (oqsr->port_no == OFPP_ALL) {
9517 : 9 : oqsr->port_no = OFPP_ANY;
9518 : : }
9519 : 21 : return 0;
9520 : : }
9521 : :
9522 : : default:
9523 : 0 : OVS_NOT_REACHED();
9524 : : }
9525 : : }
9526 : :
9527 : : /* Encode a queue stats request for 'oqsr', the encoded message
9528 : : * will be for OpenFlow version 'ofp_version'. Returns message
9529 : : * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
9530 : : struct ofpbuf *
9531 : 35 : ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
9532 : : const struct ofputil_queue_stats_request *oqsr)
9533 : : {
9534 : : struct ofpbuf *request;
9535 : :
9536 [ + + - ]: 35 : switch (ofp_version) {
9537 : : case OFP11_VERSION:
9538 : : case OFP12_VERSION:
9539 : : case OFP13_VERSION:
9540 : : case OFP14_VERSION:
9541 : : case OFP15_VERSION:
9542 : : case OFP16_VERSION: {
9543 : : struct ofp11_queue_stats_request *req;
9544 : 28 : request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
9545 : 28 : req = ofpbuf_put_zeros(request, sizeof *req);
9546 : 28 : req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
9547 : 28 : req->queue_id = htonl(oqsr->queue_id);
9548 : 28 : break;
9549 : : }
9550 : : case OFP10_VERSION: {
9551 : : struct ofp10_queue_stats_request *req;
9552 : 7 : request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
9553 : 7 : req = ofpbuf_put_zeros(request, sizeof *req);
9554 : : /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
9555 [ + + ]: 7 : req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
9556 : : ? OFPP_ALL : oqsr->port_no));
9557 : 7 : req->queue_id = htonl(oqsr->queue_id);
9558 : 7 : break;
9559 : : }
9560 : : default:
9561 : 0 : OVS_NOT_REACHED();
9562 : : }
9563 : :
9564 : 35 : return request;
9565 : : }
9566 : :
9567 : : /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9568 : : * message 'oh'. */
9569 : : size_t
9570 : 45 : ofputil_count_queue_stats(const struct ofp_header *oh)
9571 : : {
9572 : 45 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9573 : 45 : ofpraw_pull_assert(&b);
9574 : :
9575 : 45 : for (size_t n = 0; ; n++) {
9576 : : struct ofputil_queue_stats qs;
9577 [ + + ]: 115 : if (ofputil_decode_queue_stats(&qs, &b)) {
9578 : 45 : return n;
9579 : : }
9580 : 70 : }
9581 : : }
9582 : :
9583 : : static enum ofperr
9584 : 28 : ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
9585 : : const struct ofp10_queue_stats *qs10)
9586 : : {
9587 : 28 : oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
9588 : 28 : oqs->queue_id = ntohl(qs10->queue_id);
9589 : 28 : oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
9590 : 28 : oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
9591 : 28 : oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
9592 : 28 : oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9593 : :
9594 : 28 : return 0;
9595 : : }
9596 : :
9597 : : static enum ofperr
9598 : 112 : ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
9599 : : const struct ofp11_queue_stats *qs11)
9600 : : {
9601 : : enum ofperr error;
9602 : :
9603 : 112 : error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
9604 [ - + ]: 112 : if (error) {
9605 : 0 : return error;
9606 : : }
9607 : :
9608 : 112 : oqs->queue_id = ntohl(qs11->queue_id);
9609 : 112 : oqs->tx_bytes = ntohll(qs11->tx_bytes);
9610 : 112 : oqs->tx_packets = ntohll(qs11->tx_packets);
9611 : 112 : oqs->tx_errors = ntohll(qs11->tx_errors);
9612 : 112 : oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9613 : :
9614 : 112 : return 0;
9615 : : }
9616 : :
9617 : : static enum ofperr
9618 : 56 : ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
9619 : : const struct ofp13_queue_stats *qs13)
9620 : : {
9621 : 56 : enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
9622 [ + - ]: 56 : if (!error) {
9623 : 56 : oqs->duration_sec = ntohl(qs13->duration_sec);
9624 : 56 : oqs->duration_nsec = ntohl(qs13->duration_nsec);
9625 : : }
9626 : :
9627 : 56 : return error;
9628 : : }
9629 : :
9630 : : static enum ofperr
9631 : 28 : ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
9632 : : struct ofpbuf *msg)
9633 : : {
9634 : : const struct ofp14_queue_stats *qs14;
9635 : : size_t len;
9636 : :
9637 : 28 : qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
9638 [ - + ]: 28 : if (!qs14) {
9639 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9640 : : }
9641 : :
9642 : 28 : len = ntohs(qs14->length);
9643 [ + - ][ - + ]: 28 : if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
9644 : 0 : return OFPERR_OFPBRC_BAD_LEN;
9645 : : }
9646 : 28 : ofpbuf_pull(msg, len - sizeof *qs14);
9647 : :
9648 : : /* No properties yet defined, so ignore them for now. */
9649 : :
9650 : 28 : return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9651 : : }
9652 : :
9653 : : /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9654 : : * ofputil_queue_stats in 'qs'.
9655 : : *
9656 : : * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9657 : : * message. Calling this function multiple times for a single 'msg' iterates
9658 : : * through the replies. The caller must initially leave 'msg''s layer pointers
9659 : : * null and not modify them between calls.
9660 : : *
9661 : : * Returns 0 if successful, EOF if no replies were left in this 'msg',
9662 : : * otherwise a positive errno value. */
9663 : : int
9664 : 230 : ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9665 : : {
9666 : : enum ofperr error;
9667 : : enum ofpraw raw;
9668 : :
9669 [ + + ]: 230 : error = (msg->header ? ofpraw_decode(&raw, msg->header)
9670 : : : ofpraw_pull(&raw, msg));
9671 [ - + ]: 230 : if (error) {
9672 : 0 : return error;
9673 : : }
9674 : :
9675 [ + + ]: 230 : if (!msg->size) {
9676 : 90 : return EOF;
9677 [ + + ]: 140 : } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9678 : 28 : return ofputil_pull_ofp14_queue_stats(qs, msg);
9679 [ + + ]: 112 : } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9680 : : const struct ofp13_queue_stats *qs13;
9681 : :
9682 : 28 : qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9683 [ - + ]: 28 : if (!qs13) {
9684 : 0 : goto bad_len;
9685 : : }
9686 : 28 : return ofputil_queue_stats_from_ofp13(qs, qs13);
9687 [ + + ]: 84 : } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9688 : : const struct ofp11_queue_stats *qs11;
9689 : :
9690 : 56 : qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9691 [ - + ]: 56 : if (!qs11) {
9692 : 0 : goto bad_len;
9693 : : }
9694 : 56 : return ofputil_queue_stats_from_ofp11(qs, qs11);
9695 [ + - ]: 28 : } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9696 : : const struct ofp10_queue_stats *qs10;
9697 : :
9698 : 28 : qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9699 [ - + ]: 28 : if (!qs10) {
9700 : 0 : goto bad_len;
9701 : : }
9702 : 28 : return ofputil_queue_stats_from_ofp10(qs, qs10);
9703 : : } else {
9704 : 0 : OVS_NOT_REACHED();
9705 : : }
9706 : :
9707 : : bad_len:
9708 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
9709 : : "bytes at end", msg->size);
9710 : 230 : return OFPERR_OFPBRC_BAD_LEN;
9711 : : }
9712 : :
9713 : : static void
9714 : 4 : ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
9715 : : struct ofp10_queue_stats *qs10)
9716 : : {
9717 : 4 : qs10->port_no = htons(ofp_to_u16(oqs->port_no));
9718 : 4 : memset(qs10->pad, 0, sizeof qs10->pad);
9719 : 4 : qs10->queue_id = htonl(oqs->queue_id);
9720 : 4 : put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
9721 : 4 : put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
9722 : 4 : put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
9723 : 4 : }
9724 : :
9725 : : static void
9726 : 16 : ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
9727 : : struct ofp11_queue_stats *qs11)
9728 : : {
9729 : 16 : qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
9730 : 16 : qs11->queue_id = htonl(oqs->queue_id);
9731 : 16 : qs11->tx_bytes = htonll(oqs->tx_bytes);
9732 : 16 : qs11->tx_packets = htonll(oqs->tx_packets);
9733 : 16 : qs11->tx_errors = htonll(oqs->tx_errors);
9734 : 16 : }
9735 : :
9736 : : static void
9737 : 8 : ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
9738 : : struct ofp13_queue_stats *qs13)
9739 : : {
9740 : 8 : ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
9741 [ - + ]: 8 : if (oqs->duration_sec != UINT32_MAX) {
9742 : 0 : qs13->duration_sec = htonl(oqs->duration_sec);
9743 : 0 : qs13->duration_nsec = htonl(oqs->duration_nsec);
9744 : : } else {
9745 : 8 : qs13->duration_sec = OVS_BE32_MAX;
9746 : 8 : qs13->duration_nsec = OVS_BE32_MAX;
9747 : : }
9748 : 8 : }
9749 : :
9750 : : static void
9751 : 4 : ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
9752 : : struct ofp14_queue_stats *qs14)
9753 : : {
9754 : 4 : qs14->length = htons(sizeof *qs14);
9755 : 4 : memset(qs14->pad, 0, sizeof qs14->pad);
9756 : 4 : ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
9757 : 4 : }
9758 : :
9759 : :
9760 : : /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9761 : : void
9762 : 20 : ofputil_append_queue_stat(struct ovs_list *replies,
9763 : : const struct ofputil_queue_stats *oqs)
9764 : : {
9765 [ + + + + : 20 : switch (ofpmp_version(replies)) {
- ]
9766 : : case OFP13_VERSION: {
9767 : 4 : struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9768 : 4 : ofputil_queue_stats_to_ofp13(oqs, reply);
9769 : 4 : break;
9770 : : }
9771 : :
9772 : : case OFP12_VERSION:
9773 : : case OFP11_VERSION: {
9774 : 8 : struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9775 : 8 : ofputil_queue_stats_to_ofp11(oqs, reply);
9776 : 8 : break;
9777 : : }
9778 : :
9779 : : case OFP10_VERSION: {
9780 : 4 : struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9781 : 4 : ofputil_queue_stats_to_ofp10(oqs, reply);
9782 : 4 : break;
9783 : : }
9784 : :
9785 : : case OFP14_VERSION:
9786 : : case OFP15_VERSION:
9787 : : case OFP16_VERSION: {
9788 : 4 : struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9789 : 4 : ofputil_queue_stats_to_ofp14(oqs, reply);
9790 : 4 : break;
9791 : : }
9792 : :
9793 : : default:
9794 : 0 : OVS_NOT_REACHED();
9795 : : }
9796 : 20 : }
9797 : :
9798 : : enum ofperr
9799 : 1080 : ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
9800 : : struct ofputil_bundle_ctrl_msg *msg)
9801 : : {
9802 : 1080 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9803 : 1080 : enum ofpraw raw = ofpraw_pull_assert(&b);
9804 [ + + ][ - + ]: 1080 : ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL
9805 : : || raw == OFPRAW_ONFT13_BUNDLE_CONTROL);
9806 : :
9807 : 1080 : const struct ofp14_bundle_ctrl_msg *m = b.msg;
9808 : 1080 : msg->bundle_id = ntohl(m->bundle_id);
9809 : 1080 : msg->type = ntohs(m->type);
9810 : 1080 : msg->flags = ntohs(m->flags);
9811 : :
9812 : 1080 : return 0;
9813 : : }
9814 : :
9815 : : struct ofpbuf *
9816 : 238 : ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
9817 : : struct ofputil_bundle_ctrl_msg *bc)
9818 : : {
9819 : : struct ofpbuf *request;
9820 : : struct ofp14_bundle_ctrl_msg *m;
9821 : :
9822 [ - + - ]: 238 : switch (ofp_version) {
9823 : : case OFP10_VERSION:
9824 : : case OFP11_VERSION:
9825 : : case OFP12_VERSION:
9826 : 0 : ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
9827 : : "(\'-O OpenFlow14\')");
9828 : : case OFP13_VERSION:
9829 : : case OFP14_VERSION:
9830 : : case OFP15_VERSION:
9831 : : case OFP16_VERSION:
9832 [ + + ]: 238 : request = ofpraw_alloc(ofp_version == OFP13_VERSION
9833 : : ? OFPRAW_ONFT13_BUNDLE_CONTROL
9834 : : : OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
9835 : 238 : m = ofpbuf_put_zeros(request, sizeof *m);
9836 : :
9837 : 238 : m->bundle_id = htonl(bc->bundle_id);
9838 : 238 : m->type = htons(bc->type);
9839 : 238 : m->flags = htons(bc->flags);
9840 : 238 : break;
9841 : : default:
9842 : 0 : OVS_NOT_REACHED();
9843 : : }
9844 : :
9845 : 238 : return request;
9846 : : }
9847 : :
9848 : : struct ofpbuf *
9849 : 241 : ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
9850 : : struct ofputil_bundle_ctrl_msg *msg)
9851 : : {
9852 : : struct ofpbuf *buf;
9853 : : struct ofp14_bundle_ctrl_msg *m;
9854 : :
9855 [ + + ]: 241 : buf = ofpraw_alloc_reply(oh->version == OFP13_VERSION
9856 : : ? OFPRAW_ONFT13_BUNDLE_CONTROL
9857 : : : OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
9858 : 241 : m = ofpbuf_put_zeros(buf, sizeof *m);
9859 : :
9860 : 241 : m->bundle_id = htonl(msg->bundle_id);
9861 : 241 : m->type = htons(msg->type);
9862 : 241 : m->flags = htons(msg->flags);
9863 : :
9864 : 241 : return buf;
9865 : : }
9866 : :
9867 : : /* Return true for bundlable state change requests, false for other messages.
9868 : : */
9869 : : static bool
9870 : 1426 : ofputil_is_bundlable(enum ofptype type)
9871 : : {
9872 [ + + - ]: 1426 : switch (type) {
9873 : : /* Minimum required by OpenFlow 1.4. */
9874 : : case OFPTYPE_PORT_MOD:
9875 : : case OFPTYPE_FLOW_MOD:
9876 : : /* Other supported types. */
9877 : : case OFPTYPE_GROUP_MOD:
9878 : 1425 : return true;
9879 : :
9880 : : /* Nice to have later. */
9881 : : case OFPTYPE_FLOW_MOD_TABLE_ID:
9882 : : case OFPTYPE_TABLE_MOD:
9883 : : case OFPTYPE_METER_MOD:
9884 : : case OFPTYPE_PACKET_OUT:
9885 : : case OFPTYPE_NXT_TLV_TABLE_MOD:
9886 : :
9887 : : /* Not to be bundlable. */
9888 : : case OFPTYPE_ECHO_REQUEST:
9889 : : case OFPTYPE_FEATURES_REQUEST:
9890 : : case OFPTYPE_GET_CONFIG_REQUEST:
9891 : : case OFPTYPE_SET_CONFIG:
9892 : : case OFPTYPE_BARRIER_REQUEST:
9893 : : case OFPTYPE_ROLE_REQUEST:
9894 : : case OFPTYPE_ECHO_REPLY:
9895 : : case OFPTYPE_SET_FLOW_FORMAT:
9896 : : case OFPTYPE_SET_PACKET_IN_FORMAT:
9897 : : case OFPTYPE_SET_CONTROLLER_ID:
9898 : : case OFPTYPE_FLOW_AGE:
9899 : : case OFPTYPE_FLOW_MONITOR_CANCEL:
9900 : : case OFPTYPE_SET_ASYNC_CONFIG:
9901 : : case OFPTYPE_GET_ASYNC_REQUEST:
9902 : : case OFPTYPE_DESC_STATS_REQUEST:
9903 : : case OFPTYPE_FLOW_STATS_REQUEST:
9904 : : case OFPTYPE_AGGREGATE_STATS_REQUEST:
9905 : : case OFPTYPE_TABLE_STATS_REQUEST:
9906 : : case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9907 : : case OFPTYPE_TABLE_DESC_REQUEST:
9908 : : case OFPTYPE_PORT_STATS_REQUEST:
9909 : : case OFPTYPE_QUEUE_STATS_REQUEST:
9910 : : case OFPTYPE_PORT_DESC_STATS_REQUEST:
9911 : : case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9912 : : case OFPTYPE_METER_STATS_REQUEST:
9913 : : case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9914 : : case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9915 : : case OFPTYPE_GROUP_STATS_REQUEST:
9916 : : case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9917 : : case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9918 : : case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9919 : : case OFPTYPE_BUNDLE_CONTROL:
9920 : : case OFPTYPE_BUNDLE_ADD_MESSAGE:
9921 : : case OFPTYPE_HELLO:
9922 : : case OFPTYPE_ERROR:
9923 : : case OFPTYPE_FEATURES_REPLY:
9924 : : case OFPTYPE_GET_CONFIG_REPLY:
9925 : : case OFPTYPE_PACKET_IN:
9926 : : case OFPTYPE_FLOW_REMOVED:
9927 : : case OFPTYPE_PORT_STATUS:
9928 : : case OFPTYPE_BARRIER_REPLY:
9929 : : case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9930 : : case OFPTYPE_DESC_STATS_REPLY:
9931 : : case OFPTYPE_FLOW_STATS_REPLY:
9932 : : case OFPTYPE_QUEUE_STATS_REPLY:
9933 : : case OFPTYPE_PORT_STATS_REPLY:
9934 : : case OFPTYPE_TABLE_STATS_REPLY:
9935 : : case OFPTYPE_AGGREGATE_STATS_REPLY:
9936 : : case OFPTYPE_PORT_DESC_STATS_REPLY:
9937 : : case OFPTYPE_ROLE_REPLY:
9938 : : case OFPTYPE_FLOW_MONITOR_PAUSED:
9939 : : case OFPTYPE_FLOW_MONITOR_RESUMED:
9940 : : case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9941 : : case OFPTYPE_GET_ASYNC_REPLY:
9942 : : case OFPTYPE_GROUP_STATS_REPLY:
9943 : : case OFPTYPE_GROUP_DESC_STATS_REPLY:
9944 : : case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9945 : : case OFPTYPE_METER_STATS_REPLY:
9946 : : case OFPTYPE_METER_CONFIG_STATS_REPLY:
9947 : : case OFPTYPE_METER_FEATURES_STATS_REPLY:
9948 : : case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9949 : : case OFPTYPE_TABLE_DESC_REPLY:
9950 : : case OFPTYPE_ROLE_STATUS:
9951 : : case OFPTYPE_REQUESTFORWARD:
9952 : : case OFPTYPE_TABLE_STATUS:
9953 : : case OFPTYPE_NXT_TLV_TABLE_REQUEST:
9954 : : case OFPTYPE_NXT_TLV_TABLE_REPLY:
9955 : : case OFPTYPE_NXT_RESUME:
9956 : : case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
9957 : : case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
9958 : : case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
9959 : : case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
9960 : 1 : break;
9961 : : }
9962 : :
9963 : 1 : return false;
9964 : : }
9965 : :
9966 : : enum ofperr
9967 : 1427 : ofputil_decode_bundle_add(const struct ofp_header *oh,
9968 : : struct ofputil_bundle_add_msg *msg,
9969 : : enum ofptype *typep)
9970 : : {
9971 : 1427 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9972 : 1427 : enum ofpraw raw = ofpraw_pull_assert(&b);
9973 [ + + ][ - + ]: 1427 : ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
9974 : : || raw == OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE);
9975 : :
9976 : 1427 : const struct ofp14_bundle_ctrl_msg *m = ofpbuf_pull(&b, sizeof *m);
9977 : 1427 : msg->bundle_id = ntohl(m->bundle_id);
9978 : 1427 : msg->flags = ntohs(m->flags);
9979 : :
9980 : 1427 : msg->msg = b.data;
9981 [ - + ]: 1427 : if (msg->msg->version != oh->version) {
9982 : 0 : return OFPERR_OFPBFC_BAD_VERSION;
9983 : : }
9984 : 1427 : size_t inner_len = ntohs(msg->msg->length);
9985 [ + - ][ - + ]: 1427 : if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
9986 : 0 : return OFPERR_OFPBFC_MSG_BAD_LEN;
9987 : : }
9988 [ + + ]: 1427 : if (msg->msg->xid != oh->xid) {
9989 : 1 : return OFPERR_OFPBFC_MSG_BAD_XID;
9990 : : }
9991 : :
9992 : : /* Reject unbundlable messages. */
9993 : : enum ofptype type;
9994 : 1426 : enum ofperr error = ofptype_decode(&type, msg->msg);
9995 [ - + ]: 1426 : if (error) {
9996 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9997 : : "message is unparsable (%s)", ofperr_get_name(error));
9998 : 0 : return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
9999 : : }
10000 : :
10001 [ + + ]: 1426 : if (!ofputil_is_bundlable(type)) {
10002 [ + - ]: 1 : VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
10003 : : "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type));
10004 : 1 : return OFPERR_OFPBFC_MSG_UNSUP;
10005 : : }
10006 [ + + ]: 1425 : if (typep) {
10007 : 711 : *typep = type;
10008 : : }
10009 : :
10010 : 1427 : return 0;
10011 : : }
10012 : :
10013 : : struct ofpbuf *
10014 : 735 : ofputil_encode_bundle_add(enum ofp_version ofp_version,
10015 : : struct ofputil_bundle_add_msg *msg)
10016 : : {
10017 : : struct ofpbuf *request;
10018 : : struct ofp14_bundle_ctrl_msg *m;
10019 : :
10020 : : /* Must use the same xid as the embedded message. */
10021 [ + + ]: 735 : request = ofpraw_alloc_xid(ofp_version == OFP13_VERSION
10022 : : ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10023 : : : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
10024 : 735 : msg->msg->xid, ntohs(msg->msg->length));
10025 : 735 : m = ofpbuf_put_zeros(request, sizeof *m);
10026 : :
10027 : 735 : m->bundle_id = htonl(msg->bundle_id);
10028 : 735 : m->flags = htons(msg->flags);
10029 : 735 : ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
10030 : :
10031 : 735 : ofpmsg_update_length(request);
10032 : 735 : return request;
10033 : : }
10034 : :
10035 : : static void
10036 : 105 : encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
10037 : : {
10038 : : struct ofputil_tlv_map *map;
10039 : :
10040 [ + + ]: 168 : LIST_FOR_EACH (map, list_node, mappings) {
10041 : : struct nx_tlv_map *nx_map;
10042 : :
10043 : 63 : nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
10044 : 63 : nx_map->option_class = htons(map->option_class);
10045 : 63 : nx_map->option_type = map->option_type;
10046 : 63 : nx_map->option_len = map->option_len;
10047 : 63 : nx_map->index = htons(map->index);
10048 : : }
10049 : 105 : }
10050 : :
10051 : : struct ofpbuf *
10052 : 59 : ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
10053 : : struct ofputil_tlv_table_mod *ttm)
10054 : : {
10055 : : struct ofpbuf *b;
10056 : : struct nx_tlv_table_mod *nx_ttm;
10057 : :
10058 : 59 : b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
10059 : 59 : nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
10060 : 59 : nx_ttm->command = htons(ttm->command);
10061 : 59 : encode_tlv_table_mappings(b, &ttm->mappings);
10062 : :
10063 : 59 : return b;
10064 : : }
10065 : :
10066 : : static enum ofperr
10067 : 212 : decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
10068 : : struct ovs_list *mappings)
10069 : : {
10070 : 212 : ovs_list_init(mappings);
10071 : :
10072 [ + + ]: 342 : while (msg->size) {
10073 : : struct nx_tlv_map *nx_map;
10074 : : struct ofputil_tlv_map *map;
10075 : :
10076 : 130 : nx_map = ofpbuf_pull(msg, sizeof *nx_map);
10077 : 130 : map = xmalloc(sizeof *map);
10078 : 130 : ovs_list_push_back(mappings, &map->list_node);
10079 : :
10080 : 130 : map->option_class = ntohs(nx_map->option_class);
10081 : 130 : map->option_type = nx_map->option_type;
10082 : :
10083 : 130 : map->option_len = nx_map->option_len;
10084 [ + - ][ - + ]: 130 : if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
10085 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
10086 : : "tlv table option length (%u) is not a valid option size",
10087 : : map->option_len);
10088 : 0 : ofputil_uninit_tlv_table(mappings);
10089 : 0 : return OFPERR_NXTTMFC_BAD_OPT_LEN;
10090 : : }
10091 : :
10092 : 130 : map->index = ntohs(nx_map->index);
10093 [ - + ]: 130 : if (map->index >= max_fields) {
10094 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
10095 : : "tlv table field index (%u) is too large (max %u)",
10096 : : map->index, max_fields - 1);
10097 : 0 : ofputil_uninit_tlv_table(mappings);
10098 : 0 : return OFPERR_NXTTMFC_BAD_FIELD_IDX;
10099 : : }
10100 : : }
10101 : :
10102 : 212 : return 0;
10103 : : }
10104 : :
10105 : : enum ofperr
10106 : 120 : ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
10107 : : struct ofputil_tlv_table_mod *ttm)
10108 : : {
10109 : 120 : struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
10110 : 120 : ofpraw_pull_assert(&msg);
10111 : :
10112 : 120 : struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
10113 : 120 : ttm->command = ntohs(nx_ttm->command);
10114 [ - + ]: 120 : if (ttm->command > NXTTMC_CLEAR) {
10115 [ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl,
10116 : : "tlv table mod command (%u) is out of range",
10117 : : ttm->command);
10118 : 0 : return OFPERR_NXTTMFC_BAD_COMMAND;
10119 : : }
10120 : :
10121 : 120 : return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
10122 : : &ttm->mappings);
10123 : : }
10124 : :
10125 : : struct ofpbuf *
10126 : 46 : ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
10127 : : struct ofputil_tlv_table_reply *ttr)
10128 : : {
10129 : : struct ofpbuf *b;
10130 : : struct nx_tlv_table_reply *nx_ttr;
10131 : :
10132 : 46 : b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
10133 : 46 : nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
10134 : 46 : nx_ttr->max_option_space = htonl(ttr->max_option_space);
10135 : 46 : nx_ttr->max_fields = htons(ttr->max_fields);
10136 : :
10137 : 46 : encode_tlv_table_mappings(b, &ttr->mappings);
10138 : :
10139 : 46 : return b;
10140 : : }
10141 : :
10142 : : /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
10143 : : * if successful, otherwise an ofperr.
10144 : : *
10145 : : * The decoder verifies that the indexes in 'ttr->mappings' are less than
10146 : : * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
10147 : : * less than TUN_METADATA_NUM_OPTS. */
10148 : : enum ofperr
10149 : 92 : ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
10150 : : struct ofputil_tlv_table_reply *ttr)
10151 : : {
10152 : 92 : struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
10153 : 92 : ofpraw_pull_assert(&msg);
10154 : :
10155 : 92 : struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
10156 : 92 : ttr->max_option_space = ntohl(nx_ttr->max_option_space);
10157 : 92 : ttr->max_fields = ntohs(nx_ttr->max_fields);
10158 : :
10159 : 92 : return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
10160 : : }
10161 : :
10162 : : void
10163 : 287 : ofputil_uninit_tlv_table(struct ovs_list *mappings)
10164 : : {
10165 : : struct ofputil_tlv_map *map;
10166 : :
10167 [ + + ]: 434 : LIST_FOR_EACH_POP (map, list_node, mappings) {
10168 : 147 : free(map);
10169 : : }
10170 : 287 : }
10171 : :
10172 : : const char *
10173 : 253 : ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type)
10174 : : {
10175 [ + + + + : 253 : switch (type) {
+ + - ]
10176 : 43 : case OAM_PACKET_IN: return "PACKET_IN";
10177 : 42 : case OAM_PORT_STATUS: return "PORT_STATUS";
10178 : 42 : case OAM_FLOW_REMOVED: return "FLOW_REMOVED";
10179 : 42 : case OAM_ROLE_STATUS: return "ROLE_STATUS";
10180 : 42 : case OAM_TABLE_STATUS: return "TABLE_STATUS";
10181 : 42 : case OAM_REQUESTFORWARD: return "REQUESTFORWARD";
10182 : :
10183 : : case OAM_N_TYPES:
10184 : : default:
10185 : 0 : OVS_NOT_REACHED();
10186 : : }
10187 : : }
10188 : :
10189 : : struct ofp14_async_prop {
10190 : : uint64_t prop_type;
10191 : : enum ofputil_async_msg_type oam;
10192 : : bool master;
10193 : : uint32_t allowed10, allowed14;
10194 : : };
10195 : :
10196 : : #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10197 : : { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10198 : : { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10199 : :
10200 : : static const struct ofp14_async_prop async_props[] = {
10201 : : AP_PAIR( 0, OAM_PACKET_IN, OFPR10_BITS, OFPR14_BITS),
10202 : : AP_PAIR( 2, OAM_PORT_STATUS, (1 << OFPPR_N_REASONS) - 1, 0),
10203 : : AP_PAIR( 4, OAM_FLOW_REMOVED, (1 << OVS_OFPRR_NONE) - 1, 0),
10204 : : AP_PAIR( 6, OAM_ROLE_STATUS, (1 << OFPCRR_N_REASONS) - 1, 0),
10205 : : AP_PAIR( 8, OAM_TABLE_STATUS, OFPTR_BITS, 0),
10206 : : AP_PAIR(10, OAM_REQUESTFORWARD, (1 << OFPRFR_N_REASONS) - 1, 0),
10207 : : };
10208 : :
10209 : : #define FOR_EACH_ASYNC_PROP(VAR) \
10210 : : for (const struct ofp14_async_prop *VAR = async_props; \
10211 : : VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10212 : :
10213 : : static const struct ofp14_async_prop *
10214 : 66 : get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type)
10215 : : {
10216 [ + + ]: 318 : FOR_EACH_ASYNC_PROP (ap) {
10217 [ + + ]: 317 : if (prop_type == ap->prop_type) {
10218 : 65 : return ap;
10219 : : }
10220 : : }
10221 : 1 : return NULL;
10222 : : }
10223 : :
10224 : : static const struct ofp14_async_prop *
10225 : 102 : get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam,
10226 : : bool master)
10227 : : {
10228 [ + - ]: 357 : FOR_EACH_ASYNC_PROP (ap) {
10229 [ + + ][ + + ]: 357 : if (ap->oam == oam && ap->master == master) {
10230 : 102 : return ap;
10231 : : }
10232 : : }
10233 : 0 : return NULL;
10234 : : }
10235 : :
10236 : : static uint32_t
10237 : 167 : ofp14_async_prop_allowed(const struct ofp14_async_prop *prop,
10238 : : enum ofp_version version)
10239 : : {
10240 [ + + ]: 167 : return version >= OFP14_VERSION ? prop->allowed14 : prop->allowed10;
10241 : : }
10242 : :
10243 : : static ovs_be32
10244 : 6 : encode_async_mask(const struct ofputil_async_cfg *src,
10245 : : const struct ofp14_async_prop *ap,
10246 : : enum ofp_version version)
10247 : : {
10248 [ + + ]: 6 : uint32_t mask = ap->master ? src->master[ap->oam] : src->slave[ap->oam];
10249 : 6 : return htonl(mask & ofp14_async_prop_allowed(ap, version));
10250 : : }
10251 : :
10252 : : static enum ofperr
10253 : 161 : decode_async_mask(ovs_be32 src,
10254 : : const struct ofp14_async_prop *ap, enum ofp_version version,
10255 : : bool loose, struct ofputil_async_cfg *dst)
10256 : : {
10257 : 161 : uint32_t mask = ntohl(src);
10258 : 161 : uint32_t allowed = ofp14_async_prop_allowed(ap, version);
10259 [ + + ]: 161 : if (mask & ~allowed) {
10260 [ + + ][ + + ]: 5 : OFPPROP_LOG(&bad_ofmsg_rl, loose,
10261 : : "bad value %#x for %s (allowed mask %#x)",
10262 : : mask, ofputil_async_msg_type_to_string(ap->oam),
10263 : : allowed);
10264 : 5 : mask &= allowed;
10265 [ + + ]: 5 : if (!loose) {
10266 : 1 : return OFPERR_OFPACFC_INVALID;
10267 : : }
10268 : : }
10269 : :
10270 [ + + ]: 160 : if (ap->oam == OAM_PACKET_IN) {
10271 [ + + ]: 50 : if (mask & (1u << OFPR_NO_MATCH)) {
10272 : 29 : mask |= 1u << OFPR_EXPLICIT_MISS;
10273 [ + + ]: 29 : if (version < OFP13_VERSION) {
10274 : 8 : mask |= 1u << OFPR_IMPLICIT_MISS;
10275 : : }
10276 : : }
10277 : : }
10278 : :
10279 [ + + ]: 160 : uint32_t *array = ap->master ? dst->master : dst->slave;
10280 : 160 : array[ap->oam] = mask;
10281 : 160 : return 0;
10282 : : }
10283 : :
10284 : : static enum ofperr
10285 : 65 : parse_async_tlv(const struct ofpbuf *property,
10286 : : const struct ofp14_async_prop *ap,
10287 : : struct ofputil_async_cfg *ac,
10288 : : enum ofp_version version, bool loose)
10289 : : {
10290 : : enum ofperr error;
10291 : : ovs_be32 mask;
10292 : :
10293 : 65 : error = ofpprop_parse_be32(property, &mask);
10294 [ - + ]: 65 : if (error) {
10295 : 0 : return error;
10296 : : }
10297 : :
10298 [ - + ]: 65 : if (ofpprop_is_experimenter(ap->prop_type)) {
10299 : : /* For experimenter properties, whether a property is for the master or
10300 : : * slave role is indicated by both 'type' and 'exp_type' in struct
10301 : : * ofp_prop_experimenter. Check that these are consistent. */
10302 : 0 : const struct ofp_prop_experimenter *ope = property->data;
10303 : 0 : bool should_be_master = ope->type == htons(0xffff);
10304 [ # # ]: 0 : if (should_be_master != ap->master) {
10305 [ # # ][ # # ]: 0 : VLOG_WARN_RL(&bad_ofmsg_rl, "async property type %#"PRIx16" "
[ # # ]
10306 : : "indicates %s role but exp_type %"PRIu32" indicates "
10307 : : "%s role",
10308 : : ntohs(ope->type),
10309 : : should_be_master ? "master" : "slave",
10310 : : ntohl(ope->exp_type),
10311 : : ap->master ? "master" : "slave");
10312 : 0 : return OFPERR_OFPBPC_BAD_EXP_TYPE;
10313 : : }
10314 : : }
10315 : :
10316 : 65 : return decode_async_mask(mask, ap, version, loose, ac);
10317 : : }
10318 : :
10319 : : static void
10320 : 48 : decode_legacy_async_masks(const ovs_be32 masks[2],
10321 : : enum ofputil_async_msg_type oam,
10322 : : enum ofp_version version,
10323 : : struct ofputil_async_cfg *dst)
10324 : : {
10325 [ + + ]: 144 : for (int i = 0; i < 2; i++) {
10326 : 96 : bool master = i == 0;
10327 : 96 : const struct ofp14_async_prop *ap
10328 : 96 : = get_ofp14_async_config_prop_by_oam(oam, master);
10329 : 96 : decode_async_mask(masks[i], ap, version, true, dst);
10330 : : }
10331 : 48 : }
10332 : :
10333 : : /* Decodes the OpenFlow "set async config" request and "get async config
10334 : : * reply" message in '*oh' into an abstract form in 'ac'.
10335 : : *
10336 : : * Some versions of the "set async config" request change only some of the
10337 : : * settings and leave the others alone. This function uses 'basis' as the
10338 : : * initial state for decoding these. Other versions of the request change all
10339 : : * the settings; this function ignores 'basis' when decoding these.
10340 : : *
10341 : : * If 'loose' is true, this function ignores properties and values that it does
10342 : : * not understand, as a controller would want to do when interpreting
10343 : : * capabilities provided by a switch. If 'loose' is false, this function
10344 : : * treats unknown properties and values as an error, as a switch would want to
10345 : : * do when interpreting a configuration request made by a controller.
10346 : : *
10347 : : * Returns 0 if successful, otherwise an OFPERR_* value.
10348 : : *
10349 : : * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10350 : : * the valid range of mask.
10351 : : *
10352 : : * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10353 : : * supported.*/
10354 : : enum ofperr
10355 : 31 : ofputil_decode_set_async_config(const struct ofp_header *oh, bool loose,
10356 : : const struct ofputil_async_cfg *basis,
10357 : : struct ofputil_async_cfg *ac)
10358 : : {
10359 : 31 : struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
10360 : 31 : enum ofpraw raw = ofpraw_pull_assert(&b);
10361 : :
10362 [ + + ][ + + ]: 31 : if (raw == OFPRAW_OFPT13_SET_ASYNC ||
10363 [ + + ]: 17 : raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
10364 : 16 : raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
10365 : 16 : const struct nx_async_config *msg = ofpmsg_body(oh);
10366 : :
10367 : 16 : *ac = OFPUTIL_ASYNC_CFG_INIT;
10368 : 16 : decode_legacy_async_masks(msg->packet_in_mask, OAM_PACKET_IN,
10369 : 16 : oh->version, ac);
10370 : 16 : decode_legacy_async_masks(msg->port_status_mask, OAM_PORT_STATUS,
10371 : 16 : oh->version, ac);
10372 : 16 : decode_legacy_async_masks(msg->flow_removed_mask, OAM_FLOW_REMOVED,
10373 : 16 : oh->version, ac);
10374 [ - + ][ # # ]: 28 : } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
10375 [ # # ]: 0 : raw == OFPRAW_OFPT14_GET_ASYNC_REPLY ||
10376 : : raw == OFPRAW_NXT_SET_ASYNC_CONFIG2) {
10377 : 15 : *ac = *basis;
10378 [ + + ]: 79 : while (b.size > 0) {
10379 : : struct ofpbuf property;
10380 : : enum ofperr error;
10381 : : uint64_t type;
10382 : :
10383 : 66 : error = ofpprop_pull__(&b, &property, 8, 0xfffe, &type);
10384 [ - + ]: 66 : if (error) {
10385 : 2 : return error;
10386 : : }
10387 : :
10388 : 66 : const struct ofp14_async_prop *ap
10389 : 66 : = get_ofp14_async_config_prop_by_prop_type(type);
10390 [ + + ]: 66 : error = (ap
10391 : 65 : ? parse_async_tlv(&property, ap, ac, oh->version, loose)
10392 : 1 : : OFPPROP_UNKNOWN(loose, "async config", type));
10393 [ + + ]: 66 : if (error) {
10394 : : /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10395 : : * knows why, it's OpenFlow. */
10396 [ + + ]: 2 : if (error == OFPERR_OFPBPC_BAD_TYPE) {
10397 : 1 : error = OFPERR_OFPACFC_UNSUPPORTED;
10398 : : }
10399 : 66 : return error;
10400 : : }
10401 : : }
10402 : : } else {
10403 : 0 : return OFPERR_OFPBRC_BAD_VERSION;
10404 : : }
10405 : 31 : return 0;
10406 : : }
10407 : :
10408 : : static void
10409 : 3 : encode_legacy_async_masks(const struct ofputil_async_cfg *ac,
10410 : : enum ofputil_async_msg_type oam,
10411 : : enum ofp_version version,
10412 : : ovs_be32 masks[2])
10413 : : {
10414 [ + + ]: 9 : for (int i = 0; i < 2; i++) {
10415 : 6 : bool master = i == 0;
10416 : 6 : const struct ofp14_async_prop *ap
10417 : 6 : = get_ofp14_async_config_prop_by_oam(oam, master);
10418 : 6 : masks[i] = encode_async_mask(ac, ap, version);
10419 : : }
10420 : 3 : }
10421 : :
10422 : : static void
10423 : 1 : ofputil_put_async_config__(const struct ofputil_async_cfg *ac,
10424 : : struct ofpbuf *buf, bool tlv,
10425 : : enum ofp_version version, uint32_t oams)
10426 : : {
10427 [ + - ]: 1 : if (!tlv) {
10428 : 1 : struct nx_async_config *msg = ofpbuf_put_zeros(buf, sizeof *msg);
10429 : 1 : encode_legacy_async_masks(ac, OAM_PACKET_IN, version,
10430 : 1 : msg->packet_in_mask);
10431 : 1 : encode_legacy_async_masks(ac, OAM_PORT_STATUS, version,
10432 : 1 : msg->port_status_mask);
10433 : 1 : encode_legacy_async_masks(ac, OAM_FLOW_REMOVED, version,
10434 : 1 : msg->flow_removed_mask);
10435 : : } else {
10436 [ # # ]: 0 : FOR_EACH_ASYNC_PROP (ap) {
10437 [ # # ]: 0 : if (oams & (1u << ap->oam)) {
10438 : 0 : size_t ofs = buf->size;
10439 : 0 : ofpprop_put_be32(buf, ap->prop_type,
10440 : : encode_async_mask(ac, ap, version));
10441 : :
10442 : : /* For experimenter properties, we need to use type 0xfffe for
10443 : : * master and 0xffff for slaves. */
10444 [ # # ]: 0 : if (ofpprop_is_experimenter(ap->prop_type)) {
10445 : 0 : struct ofp_prop_experimenter *ope
10446 : : = ofpbuf_at_assert(buf, ofs, sizeof *ope);
10447 [ # # ]: 0 : ope->type = ap->master ? htons(0xffff) : htons(0xfffe);
10448 : : }
10449 : : }
10450 : : }
10451 : : }
10452 : 1 : }
10453 : :
10454 : : /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10455 : : * states that the asynchronous message configuration is 'ac'. */
10456 : : struct ofpbuf *
10457 : 1 : ofputil_encode_get_async_reply(const struct ofp_header *oh,
10458 : : const struct ofputil_async_cfg *ac)
10459 : : {
10460 : : struct ofpbuf *buf;
10461 : :
10462 [ + - ]: 1 : enum ofpraw raw = (oh->version < OFP14_VERSION
10463 : : ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10464 : : : OFPRAW_OFPT14_GET_ASYNC_REPLY);
10465 : 1 : struct ofpbuf *reply = ofpraw_alloc_reply(raw, oh, 0);
10466 : 1 : ofputil_put_async_config__(ac, reply,
10467 : : raw == OFPRAW_OFPT14_GET_ASYNC_REPLY,
10468 : 1 : oh->version, UINT32_MAX);
10469 : 1 : return reply;
10470 : :
10471 : : return buf;
10472 : : }
10473 : :
10474 : : /* Encodes and returns a message, in a format appropriate for OpenFlow version
10475 : : * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10476 : : *
10477 : : * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10478 : : * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10479 : : * messages, so more messages than requested may be configured. OF1.0 through
10480 : : * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10481 : : * any extensions then this function encodes an Open vSwitch extension message
10482 : : * that does support configuring OVS extension OAM_*. */
10483 : : struct ofpbuf *
10484 : 0 : ofputil_encode_set_async_config(const struct ofputil_async_cfg *ac,
10485 : : uint32_t oams, enum ofp_version ofp_version)
10486 : : {
10487 [ # # ][ # # ]: 0 : enum ofpraw raw = (ofp_version >= OFP14_VERSION ? OFPRAW_OFPT14_SET_ASYNC
10488 : : : oams & OAM_EXTENSIONS ? OFPRAW_NXT_SET_ASYNC_CONFIG2
10489 : : : ofp_version >= OFP13_VERSION ? OFPRAW_OFPT13_SET_ASYNC
10490 : : : OFPRAW_NXT_SET_ASYNC_CONFIG);
10491 : 0 : struct ofpbuf *request = ofpraw_alloc(raw, ofp_version, 0);
10492 : 0 : ofputil_put_async_config__(ac, request,
10493 [ # # ][ # # ]: 0 : (raw == OFPRAW_OFPT14_SET_ASYNC ||
10494 : : raw == OFPRAW_NXT_SET_ASYNC_CONFIG2),
10495 : : ofp_version, oams);
10496 : 0 : return request;
10497 : : }
10498 : :
10499 : : struct ofputil_async_cfg
10500 : 1311 : ofputil_async_cfg_default(enum ofp_version version)
10501 : : {
10502 : : /* We enable all of the OF1.4 reasons regardless of 'version' because the
10503 : : * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10504 : : * introduced in OF1.0, breaking it into more specific categories. When we
10505 : : * encode these for earlier OpenFlow versions, we translate them into
10506 : : * OFPR_ACTION. */
10507 : 1311 : uint32_t pin = OFPR14_BITS & ~(1u << OFPR_INVALID_TTL);
10508 : 1311 : pin |= 1u << OFPR_EXPLICIT_MISS;
10509 [ + + ]: 1311 : if (version <= OFP12_VERSION) {
10510 : 284 : pin |= 1u << OFPR_IMPLICIT_MISS;
10511 : : }
10512 : :
10513 : 1311 : return (struct ofputil_async_cfg) {
10514 : : .master[OAM_PACKET_IN] = pin,
10515 : :
10516 : : .master[OAM_FLOW_REMOVED]
10517 [ + + ]: 1311 : = (version >= OFP14_VERSION ? OFPRR14_BITS : OFPRR10_BITS),
10518 : :
10519 : : .master[OAM_PORT_STATUS] = OFPPR_BITS,
10520 : : .slave[OAM_PORT_STATUS] = OFPPR_BITS,
10521 : : };
10522 : : }
10523 : :
10524 : : static void
10525 : 3 : ofputil_put_ofp14_table_desc(const struct ofputil_table_desc *td,
10526 : : struct ofpbuf *b, enum ofp_version version)
10527 : : {
10528 : : struct ofp14_table_desc *otd;
10529 : : struct ofp14_table_mod_prop_vacancy *otv;
10530 : : size_t start_otd;
10531 : :
10532 : 3 : start_otd = b->size;
10533 : 3 : ofpbuf_put_zeros(b, sizeof *otd);
10534 : :
10535 : 3 : ofpprop_put_u32(b, OFPTMPT14_EVICTION, td->eviction_flags);
10536 : :
10537 : 3 : otv = ofpbuf_put_zeros(b, sizeof *otv);
10538 : 3 : otv->type = htons(OFPTMPT14_VACANCY);
10539 : 3 : otv->length = htons(sizeof *otv);
10540 : 3 : otv->vacancy_down = td->table_vacancy.vacancy_down;
10541 : 3 : otv->vacancy_up = td->table_vacancy.vacancy_up;
10542 : 3 : otv->vacancy = td->table_vacancy.vacancy;
10543 : :
10544 : 3 : otd = ofpbuf_at_assert(b, start_otd, sizeof *otd);
10545 : 3 : otd->length = htons(b->size - start_otd);
10546 : 3 : otd->table_id = td->table_id;
10547 : 3 : otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
10548 : : td->eviction, td->vacancy,
10549 : : version);
10550 : 3 : }
10551 : :
10552 : : /* Converts the abstract form of a "table status" message in '*ts' into an
10553 : : * OpenFlow message suitable for 'protocol', and returns that encoded form in
10554 : : * a buffer owned by the caller. */
10555 : : struct ofpbuf *
10556 : 3 : ofputil_encode_table_status(const struct ofputil_table_status *ts,
10557 : : enum ofputil_protocol protocol)
10558 : : {
10559 : : enum ofp_version version;
10560 : : struct ofpbuf *b;
10561 : :
10562 : 3 : version = ofputil_protocol_to_ofp_version(protocol);
10563 [ + - ]: 3 : if (version >= OFP14_VERSION) {
10564 : : enum ofpraw raw;
10565 : : struct ofp14_table_status *ots;
10566 : :
10567 : 3 : raw = OFPRAW_OFPT14_TABLE_STATUS;
10568 : 3 : b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
10569 : 3 : ots = ofpbuf_put_zeros(b, sizeof *ots);
10570 : 3 : ots->reason = ts->reason;
10571 : 3 : ofputil_put_ofp14_table_desc(&ts->desc, b, version);
10572 : 3 : ofpmsg_update_length(b);
10573 : 3 : return b;
10574 : : } else {
10575 : 0 : return NULL;
10576 : : }
10577 : : }
10578 : :
10579 : : /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10580 : : * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10581 : : enum ofperr
10582 : 6 : ofputil_decode_table_status(const struct ofp_header *oh,
10583 : : struct ofputil_table_status *ts)
10584 : : {
10585 : : const struct ofp14_table_status *ots;
10586 : : struct ofpbuf b;
10587 : : enum ofperr error;
10588 : : enum ofpraw raw;
10589 : :
10590 : 6 : ofpbuf_use_const(&b, oh, ntohs(oh->length));
10591 : 6 : raw = ofpraw_pull_assert(&b);
10592 : 6 : ots = ofpbuf_pull(&b, sizeof *ots);
10593 : :
10594 [ + - ]: 6 : if (raw == OFPRAW_OFPT14_TABLE_STATUS) {
10595 [ + + ]: 6 : if (ots->reason != OFPTR_VACANCY_DOWN
10596 [ - + ]: 4 : && ots->reason != OFPTR_VACANCY_UP) {
10597 : 0 : return OFPERR_OFPBPC_BAD_VALUE;
10598 : : }
10599 : 6 : ts->reason = ots->reason;
10600 : :
10601 : 6 : error = ofputil_decode_table_desc(&b, &ts->desc, oh->version);
10602 : 6 : return error;
10603 : : } else {
10604 : 6 : return OFPERR_OFPBRC_BAD_VERSION;
10605 : : }
10606 : :
10607 : : return 0;
10608 : : }
|