]>
Commit | Line | Data |
---|---|---|
fa37b408 | 1 | /* |
c37c0382 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. |
fa37b408 BP |
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 "ofp-print.h" | |
03e1125c | 19 | #include <ctype.h> |
dc4762ed | 20 | #include <errno.h> |
fa37b408 | 21 | #include <inttypes.h> |
6ca00f6f ETN |
22 | #include <sys/types.h> |
23 | #include <netinet/in.h> | |
b459a924 | 24 | #include <netinet/icmp6.h> |
fa37b408 | 25 | #include <stdlib.h> |
daff3353 | 26 | #include "bundle.h" |
10a24935 | 27 | #include "byte-order.h" |
d8ae4d67 | 28 | #include "classifier.h" |
dc4762ed | 29 | #include "dynamic-string.h" |
75a75043 | 30 | #include "learn.h" |
816fd533 | 31 | #include "meta-flow.h" |
9e1fd49b | 32 | #include "multipath.h" |
6c038611 | 33 | #include "netdev.h" |
b6c9e612 | 34 | #include "nx-match.h" |
f25d0cf3 | 35 | #include "ofp-actions.h" |
dc4762ed | 36 | #include "ofp-errors.h" |
982697a4 | 37 | #include "ofp-msgs.h" |
fa37b408 BP |
38 | #include "ofp-util.h" |
39 | #include "ofpbuf.h" | |
40 | #include "packets.h" | |
41 | #include "random.h" | |
4ffd1b43 | 42 | #include "unaligned.h" |
e41a9130 | 43 | #include "type-props.h" |
5136ce49 | 44 | #include "vlog.h" |
5deff5aa | 45 | #include "bitmap.h" |
fa37b408 | 46 | |
d98e6007 | 47 | VLOG_DEFINE_THIS_MODULE(ofp_util); |
fa37b408 BP |
48 | |
49 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
50 | * in the peer and so there's not much point in showing a lot of them. */ | |
51 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
52 | ||
9a4eddbb BP |
53 | struct ofp_prop_header { |
54 | ovs_be16 type; | |
55 | ovs_be16 len; | |
56 | }; | |
57 | ||
58 | /* Pulls a property, beginning with struct ofp_prop_header, from the beginning | |
59 | * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is | |
60 | * nonnull, the entire property, including the header, in '*property'. Returns | |
61 | * 0 if successful, otherwise an error code. */ | |
62 | static enum ofperr | |
63 | ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property, | |
64 | uint16_t *typep) | |
65 | { | |
66 | struct ofp_prop_header *oph; | |
67 | unsigned int len; | |
68 | ||
69 | if (ofpbuf_size(msg) < sizeof *oph) { | |
70 | return OFPERR_OFPBPC_BAD_LEN; | |
71 | } | |
72 | ||
73 | oph = ofpbuf_data(msg); | |
74 | len = ntohs(oph->len); | |
75 | if (len < sizeof *oph || ROUND_UP(len, 8) > ofpbuf_size(msg)) { | |
76 | return OFPERR_OFPBPC_BAD_LEN; | |
77 | } | |
78 | ||
79 | *typep = ntohs(oph->type); | |
80 | if (property) { | |
81 | ofpbuf_use_const(property, ofpbuf_data(msg), len); | |
82 | } | |
83 | ofpbuf_pull(msg, ROUND_UP(len, 8)); | |
84 | return 0; | |
85 | } | |
86 | ||
9a4eddbb BP |
87 | static void PRINTF_FORMAT(2, 3) |
88 | log_property(bool loose, const char *message, ...) | |
89 | { | |
90 | enum vlog_level level = loose ? VLL_DBG : VLL_WARN; | |
91 | if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) { | |
92 | va_list args; | |
93 | ||
94 | va_start(args, message); | |
95 | vlog_valist(THIS_MODULE, level, message, args); | |
96 | va_end(args); | |
97 | } | |
98 | } | |
8c3cc785 | 99 | |
0596e897 BP |
100 | /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns |
101 | * an IP netmask with a 1 in each bit that must match and a 0 in each bit that | |
102 | * is wildcarded. | |
103 | * | |
104 | * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0 | |
105 | * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, | |
106 | * ..., 32 and higher wildcard the entire field. This is the *opposite* of the | |
107 | * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are | |
108 | * wildcarded. */ | |
109 | ovs_be32 | |
110 | ofputil_wcbits_to_netmask(int wcbits) | |
111 | { | |
112 | wcbits &= 0x3f; | |
113 | return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0; | |
114 | } | |
115 | ||
116 | /* Given the IP netmask 'netmask', returns the number of bits of the IP address | |
c08201d6 BP |
117 | * that it wildcards, that is, the number of 0-bits in 'netmask', a number |
118 | * between 0 and 32 inclusive. | |
119 | * | |
120 | * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will | |
121 | * still be in the valid range but isn't otherwise meaningful. */ | |
0596e897 BP |
122 | int |
123 | ofputil_netmask_to_wcbits(ovs_be32 netmask) | |
124 | { | |
aad29cd1 | 125 | return 32 - ip_count_cidr_bits(netmask); |
0596e897 BP |
126 | } |
127 | ||
eec25dc1 | 128 | /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a |
81a76618 | 129 | * flow_wildcards in 'wc' for use in struct match. It is the caller's |
eec25dc1 BP |
130 | * responsibility to handle the special case where the flow match's dl_vlan is |
131 | * set to OFP_VLAN_NONE. */ | |
7286b1e1 | 132 | void |
eec25dc1 | 133 | ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc) |
d8ae4d67 | 134 | { |
0e612675 | 135 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27); |
a877206f | 136 | |
81a76618 | 137 | /* Initialize most of wc. */ |
f9ba8dad | 138 | flow_wildcards_init_catchall(wc); |
bad68a99 | 139 | |
0bdc4bec | 140 | if (!(ofpfw & OFPFW10_IN_PORT)) { |
4e022ec0 | 141 | wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX); |
27cafc5f | 142 | } |
5d9499c4 BP |
143 | |
144 | if (!(ofpfw & OFPFW10_NW_TOS)) { | |
26720e24 | 145 | wc->masks.nw_tos |= IP_DSCP_MASK; |
d8ae4d67 | 146 | } |
7257b535 | 147 | |
851d3105 | 148 | if (!(ofpfw & OFPFW10_NW_PROTO)) { |
26720e24 | 149 | wc->masks.nw_proto = UINT8_MAX; |
851d3105 | 150 | } |
26720e24 BP |
151 | wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw |
152 | >> OFPFW10_NW_SRC_SHIFT); | |
153 | wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw | |
154 | >> OFPFW10_NW_DST_SHIFT); | |
d8ae4d67 | 155 | |
eec25dc1 | 156 | if (!(ofpfw & OFPFW10_TP_SRC)) { |
b8266395 | 157 | wc->masks.tp_src = OVS_BE16_MAX; |
73f33563 | 158 | } |
eec25dc1 | 159 | if (!(ofpfw & OFPFW10_TP_DST)) { |
b8266395 | 160 | wc->masks.tp_dst = OVS_BE16_MAX; |
73f33563 BP |
161 | } |
162 | ||
eec25dc1 | 163 | if (!(ofpfw & OFPFW10_DL_SRC)) { |
26720e24 | 164 | memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN); |
73c0ce34 | 165 | } |
eec25dc1 | 166 | if (!(ofpfw & OFPFW10_DL_DST)) { |
26720e24 | 167 | memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN); |
d8ae4d67 | 168 | } |
e2170cff | 169 | if (!(ofpfw & OFPFW10_DL_TYPE)) { |
b8266395 | 170 | wc->masks.dl_type = OVS_BE16_MAX; |
e2170cff | 171 | } |
d8ae4d67 | 172 | |
eb6f28db | 173 | /* VLAN TCI mask. */ |
eec25dc1 | 174 | if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) { |
26720e24 | 175 | wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI); |
eb6f28db | 176 | } |
eec25dc1 | 177 | if (!(ofpfw & OFPFW10_DL_VLAN)) { |
26720e24 | 178 | wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); |
eb6f28db BP |
179 | } |
180 | } | |
181 | ||
81a76618 | 182 | /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */ |
eb6f28db | 183 | void |
81a76618 BP |
184 | ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch, |
185 | struct match *match) | |
186 | { | |
187 | uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL; | |
188 | ||
189 | /* Initialize match->wc. */ | |
296e07ac | 190 | memset(&match->flow, 0, sizeof match->flow); |
81a76618 BP |
191 | ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc); |
192 | ||
193 | /* Initialize most of match->flow. */ | |
194 | match->flow.nw_src = ofmatch->nw_src; | |
195 | match->flow.nw_dst = ofmatch->nw_dst; | |
4e022ec0 | 196 | match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port)); |
81a76618 BP |
197 | match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type); |
198 | match->flow.tp_src = ofmatch->tp_src; | |
199 | match->flow.tp_dst = ofmatch->tp_dst; | |
200 | memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN); | |
201 | memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN); | |
202 | match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK; | |
203 | match->flow.nw_proto = ofmatch->nw_proto; | |
d8ae4d67 | 204 | |
66642cb4 | 205 | /* Translate VLANs. */ |
0c436519 | 206 | if (!(ofpfw & OFPFW10_DL_VLAN) && |
81a76618 | 207 | ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) { |
47271d0d BP |
208 | /* Match only packets without 802.1Q header. |
209 | * | |
eec25dc1 | 210 | * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct. |
47271d0d | 211 | * |
eec25dc1 | 212 | * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory, |
47271d0d BP |
213 | * because we can't have a specific PCP without an 802.1Q header. |
214 | * However, older versions of OVS treated this as matching packets | |
215 | * withut an 802.1Q header, so we do here too. */ | |
81a76618 BP |
216 | match->flow.vlan_tci = htons(0); |
217 | match->wc.masks.vlan_tci = htons(0xffff); | |
47271d0d BP |
218 | } else { |
219 | ovs_be16 vid, pcp, tci; | |
bf062576 | 220 | uint16_t hpcp; |
47271d0d | 221 | |
81a76618 | 222 | vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK); |
bf062576 YT |
223 | hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK; |
224 | pcp = htons(hpcp); | |
47271d0d | 225 | tci = vid | pcp | htons(VLAN_CFI); |
81a76618 | 226 | match->flow.vlan_tci = tci & match->wc.masks.vlan_tci; |
66642cb4 BP |
227 | } |
228 | ||
d8ae4d67 | 229 | /* Clean up. */ |
81a76618 | 230 | match_zero_wildcarded_fields(match); |
d8ae4d67 BP |
231 | } |
232 | ||
81a76618 | 233 | /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */ |
d8ae4d67 | 234 | void |
81a76618 BP |
235 | ofputil_match_to_ofp10_match(const struct match *match, |
236 | struct ofp10_match *ofmatch) | |
d8ae4d67 | 237 | { |
81a76618 | 238 | const struct flow_wildcards *wc = &match->wc; |
eeba8e4f | 239 | uint32_t ofpfw; |
d8ae4d67 | 240 | |
66642cb4 | 241 | /* Figure out most OpenFlow wildcards. */ |
27cafc5f | 242 | ofpfw = 0; |
4e022ec0 | 243 | if (!wc->masks.in_port.ofp_port) { |
27cafc5f BP |
244 | ofpfw |= OFPFW10_IN_PORT; |
245 | } | |
26720e24 | 246 | if (!wc->masks.dl_type) { |
27cafc5f BP |
247 | ofpfw |= OFPFW10_DL_TYPE; |
248 | } | |
26720e24 | 249 | if (!wc->masks.nw_proto) { |
27cafc5f BP |
250 | ofpfw |= OFPFW10_NW_PROTO; |
251 | } | |
26720e24 | 252 | ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src) |
eec25dc1 | 253 | << OFPFW10_NW_SRC_SHIFT); |
26720e24 | 254 | ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst) |
eec25dc1 | 255 | << OFPFW10_NW_DST_SHIFT); |
26720e24 | 256 | if (!(wc->masks.nw_tos & IP_DSCP_MASK)) { |
eec25dc1 | 257 | ofpfw |= OFPFW10_NW_TOS; |
d8ae4d67 | 258 | } |
26720e24 | 259 | if (!wc->masks.tp_src) { |
eec25dc1 | 260 | ofpfw |= OFPFW10_TP_SRC; |
73f33563 | 261 | } |
26720e24 | 262 | if (!wc->masks.tp_dst) { |
eec25dc1 | 263 | ofpfw |= OFPFW10_TP_DST; |
73f33563 | 264 | } |
26720e24 | 265 | if (eth_addr_is_zero(wc->masks.dl_src)) { |
eec25dc1 | 266 | ofpfw |= OFPFW10_DL_SRC; |
73c0ce34 | 267 | } |
26720e24 | 268 | if (eth_addr_is_zero(wc->masks.dl_dst)) { |
eec25dc1 | 269 | ofpfw |= OFPFW10_DL_DST; |
73c0ce34 | 270 | } |
ff9d3826 | 271 | |
66642cb4 | 272 | /* Translate VLANs. */ |
81a76618 BP |
273 | ofmatch->dl_vlan = htons(0); |
274 | ofmatch->dl_vlan_pcp = 0; | |
275 | if (match->wc.masks.vlan_tci == htons(0)) { | |
eec25dc1 | 276 | ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP; |
81a76618 BP |
277 | } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI) |
278 | && !(match->flow.vlan_tci & htons(VLAN_CFI))) { | |
279 | ofmatch->dl_vlan = htons(OFP10_VLAN_NONE); | |
41ca9a1e | 280 | ofpfw |= OFPFW10_DL_VLAN_PCP; |
66642cb4 | 281 | } else { |
81a76618 | 282 | if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) { |
eec25dc1 | 283 | ofpfw |= OFPFW10_DL_VLAN; |
66642cb4 | 284 | } else { |
81a76618 | 285 | ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci)); |
66642cb4 BP |
286 | } |
287 | ||
81a76618 | 288 | if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) { |
eec25dc1 | 289 | ofpfw |= OFPFW10_DL_VLAN_PCP; |
66642cb4 | 290 | } else { |
81a76618 | 291 | ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci); |
66642cb4 BP |
292 | } |
293 | } | |
294 | ||
295 | /* Compose most of the match structure. */ | |
81a76618 | 296 | ofmatch->wildcards = htonl(ofpfw); |
4e022ec0 | 297 | ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port)); |
81a76618 BP |
298 | memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN); |
299 | memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN); | |
300 | ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type); | |
301 | ofmatch->nw_src = match->flow.nw_src; | |
302 | ofmatch->nw_dst = match->flow.nw_dst; | |
303 | ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK; | |
304 | ofmatch->nw_proto = match->flow.nw_proto; | |
305 | ofmatch->tp_src = match->flow.tp_src; | |
306 | ofmatch->tp_dst = match->flow.tp_dst; | |
307 | memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1); | |
308 | memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2); | |
d8ae4d67 BP |
309 | } |
310 | ||
aa319503 | 311 | enum ofperr |
81a76618 BP |
312 | ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match, |
313 | uint16_t *padded_match_len) | |
aa319503 | 314 | { |
1f317cb5 | 315 | struct ofp11_match_header *omh = ofpbuf_data(buf); |
36a16881 | 316 | uint16_t match_len; |
aa319503 | 317 | |
1f317cb5 | 318 | if (ofpbuf_size(buf) < sizeof *omh) { |
aa319503 BP |
319 | return OFPERR_OFPBMC_BAD_LEN; |
320 | } | |
321 | ||
36a16881 SH |
322 | match_len = ntohs(omh->length); |
323 | ||
aa319503 | 324 | switch (ntohs(omh->type)) { |
36a16881 SH |
325 | case OFPMT_STANDARD: { |
326 | struct ofp11_match *om; | |
327 | ||
1f317cb5 | 328 | if (match_len != sizeof *om || ofpbuf_size(buf) < sizeof *om) { |
aa319503 BP |
329 | return OFPERR_OFPBMC_BAD_LEN; |
330 | } | |
331 | om = ofpbuf_pull(buf, sizeof *om); | |
36a16881 SH |
332 | if (padded_match_len) { |
333 | *padded_match_len = match_len; | |
334 | } | |
81a76618 | 335 | return ofputil_match_from_ofp11_match(om, match); |
36a16881 SH |
336 | } |
337 | ||
338 | case OFPMT_OXM: | |
339 | if (padded_match_len) { | |
340 | *padded_match_len = ROUND_UP(match_len, 8); | |
341 | } | |
81a76618 | 342 | return oxm_pull_match(buf, match); |
aa319503 BP |
343 | |
344 | default: | |
345 | return OFPERR_OFPBMC_BAD_TYPE; | |
346 | } | |
347 | } | |
348 | ||
3f0f48cf YT |
349 | /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'. |
350 | * Returns 0 if successful, otherwise an OFPERR_* value. */ | |
410698cf | 351 | enum ofperr |
81a76618 BP |
352 | ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch, |
353 | struct match *match) | |
410698cf | 354 | { |
81a76618 | 355 | uint16_t wc = ntohl(ofmatch->wildcards); |
410698cf BP |
356 | uint8_t dl_src_mask[ETH_ADDR_LEN]; |
357 | uint8_t dl_dst_mask[ETH_ADDR_LEN]; | |
8087f5ff | 358 | bool ipv4, arp, rarp; |
410698cf BP |
359 | int i; |
360 | ||
81a76618 | 361 | match_init_catchall(match); |
410698cf BP |
362 | |
363 | if (!(wc & OFPFW11_IN_PORT)) { | |
4e022ec0 | 364 | ofp_port_t ofp_port; |
410698cf BP |
365 | enum ofperr error; |
366 | ||
81a76618 | 367 | error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port); |
410698cf BP |
368 | if (error) { |
369 | return OFPERR_OFPBMC_BAD_VALUE; | |
370 | } | |
81a76618 | 371 | match_set_in_port(match, ofp_port); |
410698cf BP |
372 | } |
373 | ||
374 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
81a76618 | 375 | dl_src_mask[i] = ~ofmatch->dl_src_mask[i]; |
410698cf | 376 | } |
81a76618 | 377 | match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask); |
410698cf BP |
378 | |
379 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
81a76618 | 380 | dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i]; |
410698cf | 381 | } |
81a76618 | 382 | match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask); |
410698cf BP |
383 | |
384 | if (!(wc & OFPFW11_DL_VLAN)) { | |
81a76618 | 385 | if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) { |
410698cf | 386 | /* Match only packets without a VLAN tag. */ |
81a76618 | 387 | match->flow.vlan_tci = htons(0); |
b8266395 | 388 | match->wc.masks.vlan_tci = OVS_BE16_MAX; |
410698cf | 389 | } else { |
81a76618 | 390 | if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) { |
410698cf | 391 | /* Match any packet with a VLAN tag regardless of VID. */ |
81a76618 BP |
392 | match->flow.vlan_tci = htons(VLAN_CFI); |
393 | match->wc.masks.vlan_tci = htons(VLAN_CFI); | |
394 | } else if (ntohs(ofmatch->dl_vlan) < 4096) { | |
410698cf | 395 | /* Match only packets with the specified VLAN VID. */ |
81a76618 BP |
396 | match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan; |
397 | match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK); | |
410698cf BP |
398 | } else { |
399 | /* Invalid VID. */ | |
400 | return OFPERR_OFPBMC_BAD_VALUE; | |
401 | } | |
402 | ||
403 | if (!(wc & OFPFW11_DL_VLAN_PCP)) { | |
81a76618 BP |
404 | if (ofmatch->dl_vlan_pcp <= 7) { |
405 | match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp | |
406 | << VLAN_PCP_SHIFT); | |
407 | match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK); | |
410698cf BP |
408 | } else { |
409 | /* Invalid PCP. */ | |
410 | return OFPERR_OFPBMC_BAD_VALUE; | |
411 | } | |
412 | } | |
413 | } | |
414 | } | |
415 | ||
416 | if (!(wc & OFPFW11_DL_TYPE)) { | |
81a76618 BP |
417 | match_set_dl_type(match, |
418 | ofputil_dl_type_from_openflow(ofmatch->dl_type)); | |
410698cf BP |
419 | } |
420 | ||
81a76618 BP |
421 | ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP); |
422 | arp = match->flow.dl_type == htons(ETH_TYPE_ARP); | |
8087f5ff | 423 | rarp = match->flow.dl_type == htons(ETH_TYPE_RARP); |
410698cf BP |
424 | |
425 | if (ipv4 && !(wc & OFPFW11_NW_TOS)) { | |
81a76618 | 426 | if (ofmatch->nw_tos & ~IP_DSCP_MASK) { |
410698cf BP |
427 | /* Invalid TOS. */ |
428 | return OFPERR_OFPBMC_BAD_VALUE; | |
429 | } | |
430 | ||
81a76618 | 431 | match_set_nw_dscp(match, ofmatch->nw_tos); |
410698cf BP |
432 | } |
433 | ||
8087f5ff | 434 | if (ipv4 || arp || rarp) { |
410698cf | 435 | if (!(wc & OFPFW11_NW_PROTO)) { |
81a76618 | 436 | match_set_nw_proto(match, ofmatch->nw_proto); |
410698cf | 437 | } |
81a76618 BP |
438 | match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask); |
439 | match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask); | |
410698cf BP |
440 | } |
441 | ||
442 | #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST) | |
443 | if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) { | |
81a76618 | 444 | switch (match->flow.nw_proto) { |
410698cf BP |
445 | case IPPROTO_ICMP: |
446 | /* "A.2.3 Flow Match Structures" in OF1.1 says: | |
447 | * | |
448 | * The tp_src and tp_dst fields will be ignored unless the | |
449 | * network protocol specified is as TCP, UDP or SCTP. | |
450 | * | |
451 | * but I'm pretty sure we should support ICMP too, otherwise | |
452 | * that's a regression from OF1.0. */ | |
453 | if (!(wc & OFPFW11_TP_SRC)) { | |
81a76618 | 454 | uint16_t icmp_type = ntohs(ofmatch->tp_src); |
410698cf | 455 | if (icmp_type < 0x100) { |
81a76618 | 456 | match_set_icmp_type(match, icmp_type); |
410698cf BP |
457 | } else { |
458 | return OFPERR_OFPBMC_BAD_FIELD; | |
459 | } | |
460 | } | |
461 | if (!(wc & OFPFW11_TP_DST)) { | |
81a76618 | 462 | uint16_t icmp_code = ntohs(ofmatch->tp_dst); |
410698cf | 463 | if (icmp_code < 0x100) { |
81a76618 | 464 | match_set_icmp_code(match, icmp_code); |
410698cf BP |
465 | } else { |
466 | return OFPERR_OFPBMC_BAD_FIELD; | |
467 | } | |
468 | } | |
469 | break; | |
470 | ||
471 | case IPPROTO_TCP: | |
472 | case IPPROTO_UDP: | |
0d56eaf2 | 473 | case IPPROTO_SCTP: |
410698cf | 474 | if (!(wc & (OFPFW11_TP_SRC))) { |
81a76618 | 475 | match_set_tp_src(match, ofmatch->tp_src); |
410698cf BP |
476 | } |
477 | if (!(wc & (OFPFW11_TP_DST))) { | |
81a76618 | 478 | match_set_tp_dst(match, ofmatch->tp_dst); |
410698cf BP |
479 | } |
480 | break; | |
481 | ||
410698cf BP |
482 | default: |
483 | /* OF1.1 says explicitly to ignore this. */ | |
484 | break; | |
485 | } | |
486 | } | |
487 | ||
b02475c5 | 488 | if (eth_type_mpls(match->flow.dl_type)) { |
097d4939 | 489 | if (!(wc & OFPFW11_MPLS_LABEL)) { |
8bfd0fda | 490 | match_set_mpls_label(match, 0, ofmatch->mpls_label); |
097d4939 JR |
491 | } |
492 | if (!(wc & OFPFW11_MPLS_TC)) { | |
8bfd0fda | 493 | match_set_mpls_tc(match, 0, ofmatch->mpls_tc); |
410698cf BP |
494 | } |
495 | } | |
496 | ||
81a76618 BP |
497 | match_set_metadata_masked(match, ofmatch->metadata, |
498 | ~ofmatch->metadata_mask); | |
410698cf BP |
499 | |
500 | return 0; | |
501 | } | |
502 | ||
81a76618 | 503 | /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */ |
410698cf | 504 | void |
81a76618 BP |
505 | ofputil_match_to_ofp11_match(const struct match *match, |
506 | struct ofp11_match *ofmatch) | |
410698cf BP |
507 | { |
508 | uint32_t wc = 0; | |
509 | int i; | |
510 | ||
81a76618 BP |
511 | memset(ofmatch, 0, sizeof *ofmatch); |
512 | ofmatch->omh.type = htons(OFPMT_STANDARD); | |
513 | ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH); | |
410698cf | 514 | |
4e022ec0 | 515 | if (!match->wc.masks.in_port.ofp_port) { |
410698cf BP |
516 | wc |= OFPFW11_IN_PORT; |
517 | } else { | |
4e022ec0 | 518 | ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port); |
410698cf BP |
519 | } |
520 | ||
81a76618 | 521 | memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN); |
410698cf | 522 | for (i = 0; i < ETH_ADDR_LEN; i++) { |
81a76618 | 523 | ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i]; |
410698cf BP |
524 | } |
525 | ||
81a76618 | 526 | memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN); |
410698cf | 527 | for (i = 0; i < ETH_ADDR_LEN; i++) { |
81a76618 | 528 | ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i]; |
410698cf BP |
529 | } |
530 | ||
81a76618 | 531 | if (match->wc.masks.vlan_tci == htons(0)) { |
410698cf | 532 | wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP; |
81a76618 BP |
533 | } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI) |
534 | && !(match->flow.vlan_tci & htons(VLAN_CFI))) { | |
535 | ofmatch->dl_vlan = htons(OFPVID11_NONE); | |
410698cf BP |
536 | wc |= OFPFW11_DL_VLAN_PCP; |
537 | } else { | |
81a76618 BP |
538 | if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) { |
539 | ofmatch->dl_vlan = htons(OFPVID11_ANY); | |
410698cf | 540 | } else { |
81a76618 | 541 | ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci)); |
410698cf BP |
542 | } |
543 | ||
81a76618 | 544 | if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) { |
410698cf BP |
545 | wc |= OFPFW11_DL_VLAN_PCP; |
546 | } else { | |
81a76618 | 547 | ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci); |
410698cf BP |
548 | } |
549 | } | |
550 | ||
81a76618 | 551 | if (!match->wc.masks.dl_type) { |
410698cf BP |
552 | wc |= OFPFW11_DL_TYPE; |
553 | } else { | |
81a76618 | 554 | ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type); |
410698cf BP |
555 | } |
556 | ||
81a76618 | 557 | if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) { |
410698cf BP |
558 | wc |= OFPFW11_NW_TOS; |
559 | } else { | |
81a76618 | 560 | ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK; |
410698cf BP |
561 | } |
562 | ||
81a76618 | 563 | if (!match->wc.masks.nw_proto) { |
410698cf BP |
564 | wc |= OFPFW11_NW_PROTO; |
565 | } else { | |
81a76618 | 566 | ofmatch->nw_proto = match->flow.nw_proto; |
410698cf BP |
567 | } |
568 | ||
81a76618 BP |
569 | ofmatch->nw_src = match->flow.nw_src; |
570 | ofmatch->nw_src_mask = ~match->wc.masks.nw_src; | |
571 | ofmatch->nw_dst = match->flow.nw_dst; | |
572 | ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst; | |
410698cf | 573 | |
81a76618 | 574 | if (!match->wc.masks.tp_src) { |
410698cf BP |
575 | wc |= OFPFW11_TP_SRC; |
576 | } else { | |
81a76618 | 577 | ofmatch->tp_src = match->flow.tp_src; |
410698cf BP |
578 | } |
579 | ||
81a76618 | 580 | if (!match->wc.masks.tp_dst) { |
410698cf BP |
581 | wc |= OFPFW11_TP_DST; |
582 | } else { | |
81a76618 | 583 | ofmatch->tp_dst = match->flow.tp_dst; |
410698cf BP |
584 | } |
585 | ||
8bfd0fda | 586 | if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) { |
097d4939 JR |
587 | wc |= OFPFW11_MPLS_LABEL; |
588 | } else { | |
8bfd0fda BP |
589 | ofmatch->mpls_label = htonl(mpls_lse_to_label( |
590 | match->flow.mpls_lse[0])); | |
097d4939 JR |
591 | } |
592 | ||
8bfd0fda | 593 | if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) { |
097d4939 JR |
594 | wc |= OFPFW11_MPLS_TC; |
595 | } else { | |
8bfd0fda | 596 | ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]); |
097d4939 | 597 | } |
410698cf | 598 | |
81a76618 BP |
599 | ofmatch->metadata = match->flow.metadata; |
600 | ofmatch->metadata_mask = ~match->wc.masks.metadata; | |
410698cf | 601 | |
81a76618 | 602 | ofmatch->wildcards = htonl(wc); |
410698cf BP |
603 | } |
604 | ||
75fa58f8 BP |
605 | /* Returns the "typical" length of a match for 'protocol', for use in |
606 | * estimating space to preallocate. */ | |
607 | int | |
608 | ofputil_match_typical_len(enum ofputil_protocol protocol) | |
609 | { | |
610 | switch (protocol) { | |
611 | case OFPUTIL_P_OF10_STD: | |
612 | case OFPUTIL_P_OF10_STD_TID: | |
613 | return sizeof(struct ofp10_match); | |
614 | ||
615 | case OFPUTIL_P_OF10_NXM: | |
616 | case OFPUTIL_P_OF10_NXM_TID: | |
617 | return NXM_TYPICAL_LEN; | |
618 | ||
619 | case OFPUTIL_P_OF11_STD: | |
620 | return sizeof(struct ofp11_match); | |
621 | ||
622 | case OFPUTIL_P_OF12_OXM: | |
623 | case OFPUTIL_P_OF13_OXM: | |
c37c0382 | 624 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 625 | case OFPUTIL_P_OF15_OXM: |
75fa58f8 BP |
626 | return NXM_TYPICAL_LEN; |
627 | ||
628 | default: | |
428b2edd | 629 | OVS_NOT_REACHED(); |
75fa58f8 BP |
630 | } |
631 | } | |
632 | ||
633 | /* Appends to 'b' an struct ofp11_match_header followed by a match that | |
634 | * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the | |
635 | * data appended out to a multiple of 8. 'protocol' must be one that is usable | |
636 | * in OpenFlow 1.1 or later. | |
637 | * | |
638 | * This function can cause 'b''s data to be reallocated. | |
639 | * | |
640 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
641 | * returns zero. */ | |
642 | int | |
643 | ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match, | |
644 | enum ofputil_protocol protocol) | |
645 | { | |
646 | switch (protocol) { | |
647 | case OFPUTIL_P_OF10_STD: | |
648 | case OFPUTIL_P_OF10_STD_TID: | |
649 | case OFPUTIL_P_OF10_NXM: | |
650 | case OFPUTIL_P_OF10_NXM_TID: | |
428b2edd | 651 | OVS_NOT_REACHED(); |
75fa58f8 BP |
652 | |
653 | case OFPUTIL_P_OF11_STD: { | |
654 | struct ofp11_match *om; | |
655 | ||
656 | /* Make sure that no padding is needed. */ | |
657 | BUILD_ASSERT_DECL(sizeof *om % 8 == 0); | |
658 | ||
659 | om = ofpbuf_put_uninit(b, sizeof *om); | |
660 | ofputil_match_to_ofp11_match(match, om); | |
661 | return sizeof *om; | |
662 | } | |
663 | ||
664 | case OFPUTIL_P_OF12_OXM: | |
665 | case OFPUTIL_P_OF13_OXM: | |
c37c0382 | 666 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 667 | case OFPUTIL_P_OF15_OXM: |
9d84066c BP |
668 | return oxm_put_match(b, match, |
669 | ofputil_protocol_to_ofp_version(protocol)); | |
75fa58f8 BP |
670 | } |
671 | ||
428b2edd | 672 | OVS_NOT_REACHED(); |
75fa58f8 BP |
673 | } |
674 | ||
36956a7d | 675 | /* Given a 'dl_type' value in the format used in struct flow, returns the |
eec25dc1 BP |
676 | * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match |
677 | * structure. */ | |
36956a7d BP |
678 | ovs_be16 |
679 | ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type) | |
680 | { | |
681 | return (flow_dl_type == htons(FLOW_DL_TYPE_NONE) | |
682 | ? htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
683 | : flow_dl_type); | |
684 | } | |
685 | ||
eec25dc1 | 686 | /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match |
36956a7d BP |
687 | * structure, returns the corresponding 'dl_type' value for use in struct |
688 | * flow. */ | |
689 | ovs_be16 | |
690 | ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type) | |
691 | { | |
692 | return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
693 | ? htons(FLOW_DL_TYPE_NONE) | |
694 | : ofp_dl_type); | |
695 | } | |
2e4f5fcf | 696 | \f |
27527aa0 | 697 | /* Protocols. */ |
7fa91113 | 698 | |
27527aa0 BP |
699 | struct proto_abbrev { |
700 | enum ofputil_protocol protocol; | |
701 | const char *name; | |
702 | }; | |
703 | ||
704 | /* Most users really don't care about some of the differences between | |
aa233d57 | 705 | * protocols. These abbreviations help with that. */ |
27527aa0 | 706 | static const struct proto_abbrev proto_abbrevs[] = { |
aa233d57 BP |
707 | { OFPUTIL_P_ANY, "any" }, |
708 | { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" }, | |
709 | { OFPUTIL_P_OF10_NXM_ANY, "NXM" }, | |
710 | { OFPUTIL_P_ANY_OXM, "OXM" }, | |
27527aa0 BP |
711 | }; |
712 | #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs) | |
713 | ||
714 | enum ofputil_protocol ofputil_flow_dump_protocols[] = { | |
42dccab5 | 715 | OFPUTIL_P_OF15_OXM, |
c37c0382 | 716 | OFPUTIL_P_OF14_OXM, |
2e1ae200 | 717 | OFPUTIL_P_OF13_OXM, |
8d7785bd | 718 | OFPUTIL_P_OF12_OXM, |
75fa58f8 | 719 | OFPUTIL_P_OF11_STD, |
85813857 BP |
720 | OFPUTIL_P_OF10_NXM, |
721 | OFPUTIL_P_OF10_STD, | |
27527aa0 BP |
722 | }; |
723 | size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols); | |
724 | ||
4f13da56 BP |
725 | /* Returns the set of ofputil_protocols that are supported with the given |
726 | * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version | |
727 | * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0 | |
728 | * if 'version' is not supported or outside the valid range. */ | |
27527aa0 | 729 | enum ofputil_protocol |
4f13da56 | 730 | ofputil_protocols_from_ofp_version(enum ofp_version version) |
27527aa0 BP |
731 | { |
732 | switch (version) { | |
2e3fa633 | 733 | case OFP10_VERSION: |
4f13da56 | 734 | return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY; |
75fa58f8 BP |
735 | case OFP11_VERSION: |
736 | return OFPUTIL_P_OF11_STD; | |
2e3fa633 | 737 | case OFP12_VERSION: |
85813857 | 738 | return OFPUTIL_P_OF12_OXM; |
2e1ae200 JR |
739 | case OFP13_VERSION: |
740 | return OFPUTIL_P_OF13_OXM; | |
c37c0382 AC |
741 | case OFP14_VERSION: |
742 | return OFPUTIL_P_OF14_OXM; | |
42dccab5 BP |
743 | case OFP15_VERSION: |
744 | return OFPUTIL_P_OF15_OXM; | |
2e3fa633 SH |
745 | default: |
746 | return 0; | |
27527aa0 BP |
747 | } |
748 | } | |
749 | ||
4f13da56 BP |
750 | /* Returns the ofputil_protocol that is initially in effect on an OpenFlow |
751 | * connection that has negotiated the given 'version'. 'version' should | |
752 | * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow | |
753 | * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or | |
754 | * outside the valid range. */ | |
755 | enum ofputil_protocol | |
756 | ofputil_protocol_from_ofp_version(enum ofp_version version) | |
757 | { | |
758 | return rightmost_1bit(ofputil_protocols_from_ofp_version(version)); | |
759 | } | |
760 | ||
44d3732d | 761 | /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION, |
2e1ae200 | 762 | * etc.) that corresponds to 'protocol'. */ |
2e3fa633 | 763 | enum ofp_version |
9e1fd49b BP |
764 | ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol) |
765 | { | |
766 | switch (protocol) { | |
85813857 BP |
767 | case OFPUTIL_P_OF10_STD: |
768 | case OFPUTIL_P_OF10_STD_TID: | |
769 | case OFPUTIL_P_OF10_NXM: | |
770 | case OFPUTIL_P_OF10_NXM_TID: | |
9e1fd49b | 771 | return OFP10_VERSION; |
75fa58f8 BP |
772 | case OFPUTIL_P_OF11_STD: |
773 | return OFP11_VERSION; | |
85813857 | 774 | case OFPUTIL_P_OF12_OXM: |
44d3732d | 775 | return OFP12_VERSION; |
2e1ae200 JR |
776 | case OFPUTIL_P_OF13_OXM: |
777 | return OFP13_VERSION; | |
c37c0382 AC |
778 | case OFPUTIL_P_OF14_OXM: |
779 | return OFP14_VERSION; | |
42dccab5 BP |
780 | case OFPUTIL_P_OF15_OXM: |
781 | return OFP15_VERSION; | |
9e1fd49b BP |
782 | } |
783 | ||
428b2edd | 784 | OVS_NOT_REACHED(); |
9e1fd49b BP |
785 | } |
786 | ||
4f13da56 BP |
787 | /* Returns a bitmap of OpenFlow versions that are supported by at |
788 | * least one of the 'protocols'. */ | |
789 | uint32_t | |
790 | ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols) | |
791 | { | |
792 | uint32_t bitmap = 0; | |
793 | ||
794 | for (; protocols; protocols = zero_rightmost_1bit(protocols)) { | |
795 | enum ofputil_protocol protocol = rightmost_1bit(protocols); | |
796 | ||
797 | bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol); | |
798 | } | |
799 | ||
800 | return bitmap; | |
801 | } | |
802 | ||
803 | /* Returns the set of protocols that are supported on top of the | |
804 | * OpenFlow versions included in 'bitmap'. */ | |
805 | enum ofputil_protocol | |
806 | ofputil_protocols_from_version_bitmap(uint32_t bitmap) | |
807 | { | |
808 | enum ofputil_protocol protocols = 0; | |
809 | ||
810 | for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) { | |
811 | enum ofp_version version = rightmost_1bit_idx(bitmap); | |
812 | ||
813 | protocols |= ofputil_protocols_from_ofp_version(version); | |
814 | } | |
815 | ||
816 | return protocols; | |
817 | } | |
818 | ||
27527aa0 BP |
819 | /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false |
820 | * otherwise. */ | |
7fa91113 | 821 | bool |
27527aa0 | 822 | ofputil_protocol_is_valid(enum ofputil_protocol protocol) |
7fa91113 | 823 | { |
27527aa0 BP |
824 | return protocol & OFPUTIL_P_ANY && is_pow2(protocol); |
825 | } | |
826 | ||
827 | /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id | |
828 | * extension turned on or off if 'enable' is true or false, respectively. | |
829 | * | |
830 | * This extension is only useful for protocols whose "standard" version does | |
831 | * not allow specific tables to be modified. In particular, this is true of | |
832 | * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always | |
833 | * specifies a table ID and so there is no need for such an extension. When | |
834 | * 'protocol' is such a protocol that doesn't need a flow_mod_table_id | |
835 | * extension, this function just returns its 'protocol' argument unchanged | |
836 | * regardless of the value of 'enable'. */ | |
837 | enum ofputil_protocol | |
838 | ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable) | |
839 | { | |
840 | switch (protocol) { | |
85813857 BP |
841 | case OFPUTIL_P_OF10_STD: |
842 | case OFPUTIL_P_OF10_STD_TID: | |
843 | return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD; | |
27527aa0 | 844 | |
85813857 BP |
845 | case OFPUTIL_P_OF10_NXM: |
846 | case OFPUTIL_P_OF10_NXM_TID: | |
847 | return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM; | |
27527aa0 | 848 | |
75fa58f8 BP |
849 | case OFPUTIL_P_OF11_STD: |
850 | return OFPUTIL_P_OF11_STD; | |
851 | ||
85813857 BP |
852 | case OFPUTIL_P_OF12_OXM: |
853 | return OFPUTIL_P_OF12_OXM; | |
44d3732d | 854 | |
2e1ae200 JR |
855 | case OFPUTIL_P_OF13_OXM: |
856 | return OFPUTIL_P_OF13_OXM; | |
857 | ||
c37c0382 AC |
858 | case OFPUTIL_P_OF14_OXM: |
859 | return OFPUTIL_P_OF14_OXM; | |
860 | ||
42dccab5 BP |
861 | case OFPUTIL_P_OF15_OXM: |
862 | return OFPUTIL_P_OF15_OXM; | |
863 | ||
27527aa0 | 864 | default: |
428b2edd | 865 | OVS_NOT_REACHED(); |
7fa91113 | 866 | } |
27527aa0 | 867 | } |
7fa91113 | 868 | |
27527aa0 BP |
869 | /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes |
870 | * some extension to a standard protocol version, the return value is the | |
871 | * standard version of that protocol without any extension. If 'protocol' is a | |
872 | * standard protocol version, returns 'protocol' unchanged. */ | |
873 | enum ofputil_protocol | |
874 | ofputil_protocol_to_base(enum ofputil_protocol protocol) | |
875 | { | |
876 | return ofputil_protocol_set_tid(protocol, false); | |
7fa91113 BP |
877 | } |
878 | ||
27527aa0 BP |
879 | /* Returns 'new_base' with any extensions taken from 'cur'. */ |
880 | enum ofputil_protocol | |
881 | ofputil_protocol_set_base(enum ofputil_protocol cur, | |
882 | enum ofputil_protocol new_base) | |
7fa91113 | 883 | { |
27527aa0 BP |
884 | bool tid = (cur & OFPUTIL_P_TID) != 0; |
885 | ||
886 | switch (new_base) { | |
85813857 BP |
887 | case OFPUTIL_P_OF10_STD: |
888 | case OFPUTIL_P_OF10_STD_TID: | |
889 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid); | |
27527aa0 | 890 | |
85813857 BP |
891 | case OFPUTIL_P_OF10_NXM: |
892 | case OFPUTIL_P_OF10_NXM_TID: | |
893 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid); | |
27527aa0 | 894 | |
75fa58f8 BP |
895 | case OFPUTIL_P_OF11_STD: |
896 | return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid); | |
897 | ||
85813857 BP |
898 | case OFPUTIL_P_OF12_OXM: |
899 | return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid); | |
44d3732d | 900 | |
2e1ae200 JR |
901 | case OFPUTIL_P_OF13_OXM: |
902 | return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid); | |
903 | ||
c37c0382 AC |
904 | case OFPUTIL_P_OF14_OXM: |
905 | return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid); | |
906 | ||
42dccab5 BP |
907 | case OFPUTIL_P_OF15_OXM: |
908 | return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid); | |
909 | ||
7fa91113 | 910 | default: |
428b2edd | 911 | OVS_NOT_REACHED(); |
7fa91113 BP |
912 | } |
913 | } | |
914 | ||
27527aa0 BP |
915 | /* Returns a string form of 'protocol', if a simple form exists (that is, if |
916 | * 'protocol' is either a single protocol or it is a combination of protocols | |
917 | * that have a single abbreviation). Otherwise, returns NULL. */ | |
918 | const char * | |
919 | ofputil_protocol_to_string(enum ofputil_protocol protocol) | |
88ca35ee | 920 | { |
27527aa0 BP |
921 | const struct proto_abbrev *p; |
922 | ||
923 | /* Use a "switch" statement for single-bit names so that we get a compiler | |
924 | * warning if we forget any. */ | |
925 | switch (protocol) { | |
85813857 | 926 | case OFPUTIL_P_OF10_NXM: |
27527aa0 BP |
927 | return "NXM-table_id"; |
928 | ||
85813857 | 929 | case OFPUTIL_P_OF10_NXM_TID: |
27527aa0 BP |
930 | return "NXM+table_id"; |
931 | ||
85813857 | 932 | case OFPUTIL_P_OF10_STD: |
27527aa0 BP |
933 | return "OpenFlow10-table_id"; |
934 | ||
85813857 | 935 | case OFPUTIL_P_OF10_STD_TID: |
27527aa0 | 936 | return "OpenFlow10+table_id"; |
44d3732d | 937 | |
75fa58f8 BP |
938 | case OFPUTIL_P_OF11_STD: |
939 | return "OpenFlow11"; | |
940 | ||
85813857 | 941 | case OFPUTIL_P_OF12_OXM: |
e71bff1b | 942 | return "OXM-OpenFlow12"; |
2e1ae200 JR |
943 | |
944 | case OFPUTIL_P_OF13_OXM: | |
e71bff1b | 945 | return "OXM-OpenFlow13"; |
c37c0382 AC |
946 | |
947 | case OFPUTIL_P_OF14_OXM: | |
948 | return "OXM-OpenFlow14"; | |
42dccab5 BP |
949 | |
950 | case OFPUTIL_P_OF15_OXM: | |
951 | return "OXM-OpenFlow15"; | |
27527aa0 BP |
952 | } |
953 | ||
954 | /* Check abbreviations. */ | |
955 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
956 | if (protocol == p->protocol) { | |
957 | return p->name; | |
958 | } | |
959 | } | |
960 | ||
961 | return NULL; | |
962 | } | |
963 | ||
964 | /* Returns a string that represents 'protocols'. The return value might be a | |
965 | * comma-separated list if 'protocols' doesn't have a simple name. The return | |
966 | * value is "none" if 'protocols' is 0. | |
967 | * | |
968 | * The caller must free the returned string (with free()). */ | |
969 | char * | |
970 | ofputil_protocols_to_string(enum ofputil_protocol protocols) | |
971 | { | |
972 | struct ds s; | |
973 | ||
cb22974d | 974 | ovs_assert(!(protocols & ~OFPUTIL_P_ANY)); |
27527aa0 BP |
975 | if (protocols == 0) { |
976 | return xstrdup("none"); | |
977 | } | |
978 | ||
979 | ds_init(&s); | |
980 | while (protocols) { | |
981 | const struct proto_abbrev *p; | |
982 | int i; | |
983 | ||
984 | if (s.length) { | |
985 | ds_put_char(&s, ','); | |
986 | } | |
987 | ||
988 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
989 | if ((protocols & p->protocol) == p->protocol) { | |
990 | ds_put_cstr(&s, p->name); | |
991 | protocols &= ~p->protocol; | |
992 | goto match; | |
993 | } | |
994 | } | |
995 | ||
996 | for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) { | |
997 | enum ofputil_protocol bit = 1u << i; | |
998 | ||
999 | if (protocols & bit) { | |
1000 | ds_put_cstr(&s, ofputil_protocol_to_string(bit)); | |
1001 | protocols &= ~bit; | |
1002 | goto match; | |
1003 | } | |
1004 | } | |
428b2edd | 1005 | OVS_NOT_REACHED(); |
27527aa0 BP |
1006 | |
1007 | match: ; | |
1008 | } | |
1009 | return ds_steal_cstr(&s); | |
1010 | } | |
1011 | ||
1012 | static enum ofputil_protocol | |
1013 | ofputil_protocol_from_string__(const char *s, size_t n) | |
1014 | { | |
1015 | const struct proto_abbrev *p; | |
1016 | int i; | |
1017 | ||
1018 | for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) { | |
1019 | enum ofputil_protocol bit = 1u << i; | |
1020 | const char *name = ofputil_protocol_to_string(bit); | |
1021 | ||
1022 | if (name && n == strlen(name) && !strncasecmp(s, name, n)) { | |
1023 | return bit; | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1028 | if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) { | |
1029 | return p->protocol; | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | return 0; | |
1034 | } | |
1035 | ||
1036 | /* Returns the nonempty set of protocols represented by 's', which can be a | |
1037 | * single protocol name or abbreviation or a comma-separated list of them. | |
1038 | * | |
1039 | * Aborts the program with an error message if 's' is invalid. */ | |
1040 | enum ofputil_protocol | |
1041 | ofputil_protocols_from_string(const char *s) | |
1042 | { | |
1043 | const char *orig_s = s; | |
1044 | enum ofputil_protocol protocols; | |
1045 | ||
1046 | protocols = 0; | |
1047 | while (*s) { | |
1048 | enum ofputil_protocol p; | |
1049 | size_t n; | |
1050 | ||
1051 | n = strcspn(s, ","); | |
1052 | if (n == 0) { | |
1053 | s++; | |
1054 | continue; | |
1055 | } | |
1056 | ||
1057 | p = ofputil_protocol_from_string__(s, n); | |
1058 | if (!p) { | |
1059 | ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s); | |
1060 | } | |
1061 | protocols |= p; | |
1062 | ||
1063 | s += n; | |
1064 | } | |
1065 | ||
1066 | if (!protocols) { | |
1067 | ovs_fatal(0, "%s: no flow protocol specified", orig_s); | |
1068 | } | |
1069 | return protocols; | |
88ca35ee BP |
1070 | } |
1071 | ||
9d84066c | 1072 | enum ofp_version |
03e1125c SH |
1073 | ofputil_version_from_string(const char *s) |
1074 | { | |
1075 | if (!strcasecmp(s, "OpenFlow10")) { | |
1076 | return OFP10_VERSION; | |
1077 | } | |
1078 | if (!strcasecmp(s, "OpenFlow11")) { | |
1079 | return OFP11_VERSION; | |
1080 | } | |
1081 | if (!strcasecmp(s, "OpenFlow12")) { | |
1082 | return OFP12_VERSION; | |
1083 | } | |
2e1ae200 JR |
1084 | if (!strcasecmp(s, "OpenFlow13")) { |
1085 | return OFP13_VERSION; | |
1086 | } | |
c37c0382 AC |
1087 | if (!strcasecmp(s, "OpenFlow14")) { |
1088 | return OFP14_VERSION; | |
1089 | } | |
42dccab5 BP |
1090 | if (!strcasecmp(s, "OpenFlow15")) { |
1091 | return OFP15_VERSION; | |
1092 | } | |
7beaa082 | 1093 | return 0; |
03e1125c SH |
1094 | } |
1095 | ||
1096 | static bool | |
e091ef84 | 1097 | is_delimiter(unsigned char c) |
03e1125c SH |
1098 | { |
1099 | return isspace(c) || c == ','; | |
1100 | } | |
1101 | ||
1102 | uint32_t | |
1103 | ofputil_versions_from_string(const char *s) | |
1104 | { | |
1105 | size_t i = 0; | |
1106 | uint32_t bitmap = 0; | |
1107 | ||
1108 | while (s[i]) { | |
1109 | size_t j; | |
7beaa082 | 1110 | int version; |
03e1125c SH |
1111 | char *key; |
1112 | ||
1113 | if (is_delimiter(s[i])) { | |
1114 | i++; | |
1115 | continue; | |
1116 | } | |
1117 | j = 0; | |
1118 | while (s[i + j] && !is_delimiter(s[i + j])) { | |
1119 | j++; | |
1120 | } | |
1121 | key = xmemdup0(s + i, j); | |
1122 | version = ofputil_version_from_string(key); | |
7beaa082 SH |
1123 | if (!version) { |
1124 | VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key); | |
1125 | } | |
03e1125c SH |
1126 | free(key); |
1127 | bitmap |= 1u << version; | |
1128 | i += j; | |
1129 | } | |
1130 | ||
1131 | return bitmap; | |
1132 | } | |
1133 | ||
7beaa082 SH |
1134 | uint32_t |
1135 | ofputil_versions_from_strings(char ** const s, size_t count) | |
1136 | { | |
1137 | uint32_t bitmap = 0; | |
1138 | ||
1139 | while (count--) { | |
1140 | int version = ofputil_version_from_string(s[count]); | |
1141 | if (!version) { | |
1142 | VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]); | |
1143 | } else { | |
1144 | bitmap |= 1u << version; | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | return bitmap; | |
1149 | } | |
1150 | ||
03e1125c SH |
1151 | const char * |
1152 | ofputil_version_to_string(enum ofp_version ofp_version) | |
1153 | { | |
1154 | switch (ofp_version) { | |
1155 | case OFP10_VERSION: | |
1156 | return "OpenFlow10"; | |
1157 | case OFP11_VERSION: | |
1158 | return "OpenFlow11"; | |
1159 | case OFP12_VERSION: | |
1160 | return "OpenFlow12"; | |
2e1ae200 JR |
1161 | case OFP13_VERSION: |
1162 | return "OpenFlow13"; | |
c37c0382 AC |
1163 | case OFP14_VERSION: |
1164 | return "OpenFlow14"; | |
42dccab5 BP |
1165 | case OFP15_VERSION: |
1166 | return "OpenFlow15"; | |
03e1125c | 1167 | default: |
428b2edd | 1168 | OVS_NOT_REACHED(); |
03e1125c SH |
1169 | } |
1170 | } | |
1171 | ||
54834960 EJ |
1172 | bool |
1173 | ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format) | |
1174 | { | |
1175 | switch (packet_in_format) { | |
1176 | case NXPIF_OPENFLOW10: | |
1177 | case NXPIF_NXM: | |
1178 | return true; | |
1179 | } | |
1180 | ||
1181 | return false; | |
1182 | } | |
1183 | ||
1184 | const char * | |
1185 | ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format) | |
1186 | { | |
1187 | switch (packet_in_format) { | |
1188 | case NXPIF_OPENFLOW10: | |
1189 | return "openflow10"; | |
1190 | case NXPIF_NXM: | |
1191 | return "nxm"; | |
1192 | default: | |
428b2edd | 1193 | OVS_NOT_REACHED(); |
54834960 EJ |
1194 | } |
1195 | } | |
1196 | ||
1197 | int | |
1198 | ofputil_packet_in_format_from_string(const char *s) | |
1199 | { | |
1200 | return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10 | |
1201 | : !strcmp(s, "nxm") ? NXPIF_NXM | |
1202 | : -1); | |
1203 | } | |
1204 | ||
03e1125c SH |
1205 | void |
1206 | ofputil_format_version(struct ds *msg, enum ofp_version version) | |
1207 | { | |
8989046d | 1208 | ds_put_format(msg, "0x%02x", version); |
03e1125c SH |
1209 | } |
1210 | ||
1211 | void | |
1212 | ofputil_format_version_name(struct ds *msg, enum ofp_version version) | |
1213 | { | |
1214 | ds_put_cstr(msg, ofputil_version_to_string(version)); | |
1215 | } | |
1216 | ||
1217 | static void | |
1218 | ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap, | |
1219 | void (*format_version)(struct ds *msg, | |
1220 | enum ofp_version)) | |
1221 | { | |
1222 | while (bitmap) { | |
1223 | format_version(msg, raw_ctz(bitmap)); | |
1224 | bitmap = zero_rightmost_1bit(bitmap); | |
1225 | if (bitmap) { | |
1226 | ds_put_cstr(msg, ", "); | |
1227 | } | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | void | |
1232 | ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap) | |
1233 | { | |
1234 | ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version); | |
1235 | } | |
1236 | ||
1237 | void | |
1238 | ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap) | |
1239 | { | |
1240 | ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name); | |
1241 | } | |
1242 | ||
de6c85b0 SH |
1243 | static bool |
1244 | ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh, | |
0935de41 | 1245 | uint32_t *allowed_versionsp) |
de6c85b0 SH |
1246 | { |
1247 | uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh; | |
db5a1019 | 1248 | const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1); |
0935de41 | 1249 | uint32_t allowed_versions; |
de6c85b0 SH |
1250 | |
1251 | if (!bitmap_len || bitmap_len % sizeof *bitmap) { | |
1252 | return false; | |
1253 | } | |
1254 | ||
1255 | /* Only use the first 32-bit element of the bitmap as that is all the | |
1256 | * current implementation supports. Subsequent elements are ignored which | |
1257 | * should have no effect on session negotiation until Open vSwtich supports | |
1258 | * wire-protocol versions greater than 31. | |
1259 | */ | |
0935de41 | 1260 | allowed_versions = ntohl(bitmap[0]); |
de6c85b0 | 1261 | |
0935de41 | 1262 | if (allowed_versions & 1) { |
de6c85b0 SH |
1263 | /* There's no OpenFlow version 0. */ |
1264 | VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow " | |
1265 | "version 0x00"); | |
0935de41 | 1266 | allowed_versions &= ~1u; |
de6c85b0 SH |
1267 | } |
1268 | ||
0935de41 | 1269 | if (!allowed_versions) { |
de6c85b0 SH |
1270 | VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow " |
1271 | "version (between 0x01 and 0x1f)"); | |
1272 | return false; | |
1273 | } | |
1274 | ||
0935de41 | 1275 | *allowed_versionsp = allowed_versions; |
de6c85b0 SH |
1276 | return true; |
1277 | } | |
1278 | ||
1279 | static uint32_t | |
1280 | version_bitmap_from_version(uint8_t ofp_version) | |
1281 | { | |
1282 | return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1; | |
1283 | } | |
1284 | ||
1285 | /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions' | |
1286 | * the set of OpenFlow versions for which 'oh' announces support. | |
1287 | * | |
1288 | * Because of how OpenFlow defines OFPT_HELLO messages, this function is always | |
1289 | * successful, and thus '*allowed_versions' is always initialized. However, it | |
1290 | * returns false if 'oh' contains some data that could not be fully understood, | |
1291 | * true if 'oh' was completely parsed. */ | |
1292 | bool | |
1293 | ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions) | |
1294 | { | |
1295 | struct ofpbuf msg; | |
1296 | bool ok = true; | |
1297 | ||
1298 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
1299 | ofpbuf_pull(&msg, sizeof *oh); | |
1300 | ||
1301 | *allowed_versions = version_bitmap_from_version(oh->version); | |
1f317cb5 | 1302 | while (ofpbuf_size(&msg)) { |
de6c85b0 SH |
1303 | const struct ofp_hello_elem_header *oheh; |
1304 | unsigned int len; | |
1305 | ||
1f317cb5 | 1306 | if (ofpbuf_size(&msg) < sizeof *oheh) { |
de6c85b0 SH |
1307 | return false; |
1308 | } | |
1309 | ||
1f317cb5 | 1310 | oheh = ofpbuf_data(&msg); |
de6c85b0 SH |
1311 | len = ntohs(oheh->length); |
1312 | if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) { | |
1313 | return false; | |
1314 | } | |
1315 | ||
1316 | if (oheh->type != htons(OFPHET_VERSIONBITMAP) | |
1317 | || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) { | |
1318 | ok = false; | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | return ok; | |
1323 | } | |
1324 | ||
1325 | /* Returns true if 'allowed_versions' needs to be accompanied by a version | |
1326 | * bitmap to be correctly expressed in an OFPT_HELLO message. */ | |
5ddea998 | 1327 | static bool |
de6c85b0 SH |
1328 | should_send_version_bitmap(uint32_t allowed_versions) |
1329 | { | |
1330 | return !is_pow2((allowed_versions >> 1) + 1); | |
1331 | } | |
1332 | ||
1333 | /* Create an OFPT_HELLO message that expresses support for the OpenFlow | |
1334 | * versions in the 'allowed_versions' bitmaps and returns the message. */ | |
1335 | struct ofpbuf * | |
1336 | ofputil_encode_hello(uint32_t allowed_versions) | |
1337 | { | |
1338 | enum ofp_version ofp_version; | |
1339 | struct ofpbuf *msg; | |
1340 | ||
1341 | ofp_version = leftmost_1bit_idx(allowed_versions); | |
1342 | msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0); | |
1343 | ||
1344 | if (should_send_version_bitmap(allowed_versions)) { | |
1345 | struct ofp_hello_elem_header *oheh; | |
1346 | uint16_t map_len; | |
1347 | ||
74c4b9c1 | 1348 | map_len = sizeof allowed_versions; |
de6c85b0 SH |
1349 | oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8)); |
1350 | oheh->type = htons(OFPHET_VERSIONBITMAP); | |
1351 | oheh->length = htons(map_len + sizeof *oheh); | |
db5a1019 | 1352 | *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions); |
1804d25a BP |
1353 | |
1354 | ofpmsg_update_length(msg); | |
de6c85b0 SH |
1355 | } |
1356 | ||
1357 | return msg; | |
1358 | } | |
1359 | ||
27527aa0 BP |
1360 | /* Returns an OpenFlow message that, sent on an OpenFlow connection whose |
1361 | * protocol is 'current', at least partly transitions the protocol to 'want'. | |
1362 | * Stores in '*next' the protocol that will be in effect on the OpenFlow | |
1363 | * connection if the switch processes the returned message correctly. (If | |
1364 | * '*next != want' then the caller will have to iterate.) | |
1365 | * | |
e43928f2 BP |
1366 | * If 'current == want', or if it is not possible to transition from 'current' |
1367 | * to 'want' (because, for example, 'current' and 'want' use different OpenFlow | |
1368 | * protocol versions), returns NULL and stores 'current' in '*next'. */ | |
27527aa0 BP |
1369 | struct ofpbuf * |
1370 | ofputil_encode_set_protocol(enum ofputil_protocol current, | |
1371 | enum ofputil_protocol want, | |
1372 | enum ofputil_protocol *next) | |
1373 | { | |
e43928f2 | 1374 | enum ofp_version cur_version, want_version; |
27527aa0 BP |
1375 | enum ofputil_protocol cur_base, want_base; |
1376 | bool cur_tid, want_tid; | |
1377 | ||
e43928f2 BP |
1378 | cur_version = ofputil_protocol_to_ofp_version(current); |
1379 | want_version = ofputil_protocol_to_ofp_version(want); | |
1380 | if (cur_version != want_version) { | |
1381 | *next = current; | |
1382 | return NULL; | |
1383 | } | |
1384 | ||
27527aa0 BP |
1385 | cur_base = ofputil_protocol_to_base(current); |
1386 | want_base = ofputil_protocol_to_base(want); | |
1387 | if (cur_base != want_base) { | |
1388 | *next = ofputil_protocol_set_base(current, want_base); | |
1389 | ||
1390 | switch (want_base) { | |
85813857 | 1391 | case OFPUTIL_P_OF10_NXM: |
27527aa0 BP |
1392 | return ofputil_encode_nx_set_flow_format(NXFF_NXM); |
1393 | ||
85813857 | 1394 | case OFPUTIL_P_OF10_STD: |
27527aa0 BP |
1395 | return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10); |
1396 | ||
75fa58f8 | 1397 | case OFPUTIL_P_OF11_STD: |
85813857 | 1398 | case OFPUTIL_P_OF12_OXM: |
2e1ae200 | 1399 | case OFPUTIL_P_OF13_OXM: |
c37c0382 | 1400 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 1401 | case OFPUTIL_P_OF15_OXM: |
75fa58f8 | 1402 | /* There is only one variant of each OpenFlow 1.1+ protocol, and we |
2e1ae200 | 1403 | * verified above that we're not trying to change versions. */ |
428b2edd | 1404 | OVS_NOT_REACHED(); |
44d3732d | 1405 | |
85813857 BP |
1406 | case OFPUTIL_P_OF10_STD_TID: |
1407 | case OFPUTIL_P_OF10_NXM_TID: | |
428b2edd | 1408 | OVS_NOT_REACHED(); |
27527aa0 BP |
1409 | } |
1410 | } | |
1411 | ||
1412 | cur_tid = (current & OFPUTIL_P_TID) != 0; | |
1413 | want_tid = (want & OFPUTIL_P_TID) != 0; | |
1414 | if (cur_tid != want_tid) { | |
1415 | *next = ofputil_protocol_set_tid(current, want_tid); | |
1416 | return ofputil_make_flow_mod_table_id(want_tid); | |
1417 | } | |
1418 | ||
cb22974d | 1419 | ovs_assert(current == want); |
27527aa0 BP |
1420 | |
1421 | *next = current; | |
1422 | return NULL; | |
88ca35ee BP |
1423 | } |
1424 | ||
27527aa0 BP |
1425 | /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow |
1426 | * format to 'nxff'. */ | |
88ca35ee | 1427 | struct ofpbuf * |
27527aa0 | 1428 | ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff) |
88ca35ee | 1429 | { |
73dbf4ab | 1430 | struct nx_set_flow_format *sff; |
88ca35ee BP |
1431 | struct ofpbuf *msg; |
1432 | ||
cb22974d | 1433 | ovs_assert(ofputil_nx_flow_format_is_valid(nxff)); |
27527aa0 | 1434 | |
982697a4 BP |
1435 | msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0); |
1436 | sff = ofpbuf_put_zeros(msg, sizeof *sff); | |
27527aa0 | 1437 | sff->format = htonl(nxff); |
88ca35ee BP |
1438 | |
1439 | return msg; | |
1440 | } | |
1441 | ||
27527aa0 BP |
1442 | /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false |
1443 | * otherwise. */ | |
1444 | enum ofputil_protocol | |
1445 | ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format) | |
1446 | { | |
1447 | switch (flow_format) { | |
1448 | case NXFF_OPENFLOW10: | |
85813857 | 1449 | return OFPUTIL_P_OF10_STD; |
27527aa0 BP |
1450 | |
1451 | case NXFF_NXM: | |
85813857 | 1452 | return OFPUTIL_P_OF10_NXM; |
27527aa0 BP |
1453 | |
1454 | default: | |
1455 | return 0; | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */ | |
1460 | bool | |
1461 | ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format) | |
1462 | { | |
1463 | return ofputil_nx_flow_format_to_protocol(flow_format) != 0; | |
1464 | } | |
1465 | ||
1466 | /* Returns a string version of 'flow_format', which must be a valid NXFF_* | |
1467 | * value. */ | |
1468 | const char * | |
1469 | ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format) | |
1470 | { | |
1471 | switch (flow_format) { | |
1472 | case NXFF_OPENFLOW10: | |
1473 | return "openflow10"; | |
1474 | case NXFF_NXM: | |
1475 | return "nxm"; | |
1476 | default: | |
428b2edd | 1477 | OVS_NOT_REACHED(); |
27527aa0 BP |
1478 | } |
1479 | } | |
1480 | ||
54834960 | 1481 | struct ofpbuf * |
3f4a1939 SH |
1482 | ofputil_make_set_packet_in_format(enum ofp_version ofp_version, |
1483 | enum nx_packet_in_format packet_in_format) | |
54834960 | 1484 | { |
73dbf4ab | 1485 | struct nx_set_packet_in_format *spif; |
54834960 EJ |
1486 | struct ofpbuf *msg; |
1487 | ||
3f4a1939 | 1488 | msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0); |
982697a4 | 1489 | spif = ofpbuf_put_zeros(msg, sizeof *spif); |
54834960 EJ |
1490 | spif->format = htonl(packet_in_format); |
1491 | ||
1492 | return msg; | |
1493 | } | |
1494 | ||
6c1491fb BP |
1495 | /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id |
1496 | * extension on or off (according to 'flow_mod_table_id'). */ | |
1497 | struct ofpbuf * | |
1498 | ofputil_make_flow_mod_table_id(bool flow_mod_table_id) | |
1499 | { | |
73dbf4ab | 1500 | struct nx_flow_mod_table_id *nfmti; |
6c1491fb BP |
1501 | struct ofpbuf *msg; |
1502 | ||
982697a4 BP |
1503 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0); |
1504 | nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti); | |
6c1491fb BP |
1505 | nfmti->set = flow_mod_table_id; |
1506 | return msg; | |
1507 | } | |
1508 | ||
0fb88c18 BP |
1509 | struct ofputil_flow_mod_flag { |
1510 | uint16_t raw_flag; | |
1511 | enum ofp_version min_version, max_version; | |
1512 | enum ofputil_flow_mod_flags flag; | |
1513 | }; | |
1514 | ||
1515 | static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = { | |
1516 | { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM }, | |
1517 | { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP }, | |
1518 | { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION, | |
1519 | OFPUTIL_FF_EMERG }, | |
1520 | { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS }, | |
1521 | { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS }, | |
1522 | { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS }, | |
1523 | { 0, 0, 0, 0 }, | |
1524 | }; | |
1525 | ||
1526 | static enum ofperr | |
1527 | ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_, | |
1528 | enum ofp_flow_mod_command command, | |
1529 | enum ofp_version version, | |
1530 | enum ofputil_flow_mod_flags *flagsp) | |
1531 | { | |
1532 | uint16_t raw_flags = ntohs(raw_flags_); | |
1533 | const struct ofputil_flow_mod_flag *f; | |
1534 | ||
1535 | *flagsp = 0; | |
1536 | for (f = ofputil_flow_mod_flags; f->raw_flag; f++) { | |
1537 | if (raw_flags & f->raw_flag | |
1538 | && version >= f->min_version | |
1539 | && (!f->max_version || version <= f->max_version)) { | |
1540 | raw_flags &= ~f->raw_flag; | |
1541 | *flagsp |= f->flag; | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | /* In OF1.0 and OF1.1, "add" always resets counters, and other commands | |
1546 | * never do. | |
1547 | * | |
1548 | * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command | |
1549 | * resets counters. */ | |
1550 | if ((version == OFP10_VERSION || version == OFP11_VERSION) | |
1551 | && command == OFPFC_ADD) { | |
1552 | *flagsp |= OFPUTIL_FF_RESET_COUNTS; | |
1553 | } | |
1554 | ||
1555 | return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0; | |
1556 | } | |
1557 | ||
1558 | static ovs_be16 | |
1559 | ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags, | |
1560 | enum ofp_version version) | |
1561 | { | |
1562 | const struct ofputil_flow_mod_flag *f; | |
1563 | uint16_t raw_flags; | |
1564 | ||
1565 | raw_flags = 0; | |
1566 | for (f = ofputil_flow_mod_flags; f->raw_flag; f++) { | |
1567 | if (f->flag & flags | |
1568 | && version >= f->min_version | |
1569 | && (!f->max_version || version <= f->max_version)) { | |
1570 | raw_flags |= f->raw_flag; | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | return htons(raw_flags); | |
1575 | } | |
1576 | ||
7fa91113 BP |
1577 | /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract |
1578 | * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error | |
1579 | * code. | |
1580 | * | |
f25d0cf3 BP |
1581 | * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions. |
1582 | * The caller must initialize 'ofpacts' and retains ownership of it. | |
1583 | * 'fm->ofpacts' will point into the 'ofpacts' buffer. | |
1584 | * | |
1585 | * Does not validate the flow_mod actions. The caller should do that, with | |
1586 | * ofpacts_check(). */ | |
90bf1e07 | 1587 | enum ofperr |
a9a2da38 | 1588 | ofputil_decode_flow_mod(struct ofputil_flow_mod *fm, |
27527aa0 | 1589 | const struct ofp_header *oh, |
f25d0cf3 | 1590 | enum ofputil_protocol protocol, |
7e9f8266 BP |
1591 | struct ofpbuf *ofpacts, |
1592 | ofp_port_t max_port, uint8_t max_table) | |
2e4f5fcf | 1593 | { |
0fb88c18 BP |
1594 | ovs_be16 raw_flags; |
1595 | enum ofperr error; | |
2e4f5fcf | 1596 | struct ofpbuf b; |
982697a4 | 1597 | enum ofpraw raw; |
2e4f5fcf | 1598 | |
cc40d06b SH |
1599 | /* Ignored for non-delete actions */ |
1600 | fm->delete_reason = OFPRR_DELETE; | |
1601 | ||
2013493b | 1602 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
982697a4 | 1603 | raw = ofpraw_pull_assert(&b); |
aa319503 | 1604 | if (raw == OFPRAW_OFPT11_FLOW_MOD) { |
1828ae51 | 1605 | /* Standard OpenFlow 1.1+ flow_mod. */ |
aa319503 | 1606 | const struct ofp11_flow_mod *ofm; |
2e4f5fcf | 1607 | |
bbc32a88 | 1608 | ofm = ofpbuf_pull(&b, sizeof *ofm); |
2e4f5fcf | 1609 | |
81a76618 | 1610 | error = ofputil_pull_ofp11_match(&b, &fm->match, NULL); |
aa319503 BP |
1611 | if (error) { |
1612 | return error; | |
1c0b7503 BP |
1613 | } |
1614 | ||
1f317cb5 | 1615 | error = ofpacts_pull_openflow_instructions(&b, ofpbuf_size(&b), oh->version, |
e3f8f887 | 1616 | ofpacts); |
f25d0cf3 BP |
1617 | if (error) { |
1618 | return error; | |
1619 | } | |
1620 | ||
2e4f5fcf | 1621 | /* Translate the message. */ |
81a76618 | 1622 | fm->priority = ntohs(ofm->priority); |
75fa58f8 BP |
1623 | if (ofm->command == OFPFC_ADD |
1624 | || (oh->version == OFP11_VERSION | |
1625 | && (ofm->command == OFPFC_MODIFY || | |
1626 | ofm->command == OFPFC_MODIFY_STRICT) | |
1627 | && ofm->cookie_mask == htonll(0))) { | |
1628 | /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does | |
1629 | * not match on the cookie is treated as an "add" if there is no | |
1630 | * match. */ | |
aa319503 BP |
1631 | fm->cookie = htonll(0); |
1632 | fm->cookie_mask = htonll(0); | |
1633 | fm->new_cookie = ofm->cookie; | |
1634 | } else { | |
aa319503 BP |
1635 | fm->cookie = ofm->cookie; |
1636 | fm->cookie_mask = ofm->cookie_mask; | |
b8266395 | 1637 | fm->new_cookie = OVS_BE64_MAX; |
aa319503 | 1638 | } |
23342857 | 1639 | fm->modify_cookie = false; |
aa319503 | 1640 | fm->command = ofm->command; |
0e197060 BP |
1641 | |
1642 | /* Get table ID. | |
1643 | * | |
083761ad SH |
1644 | * OF1.1 entirely forbids table_id == OFPTT_ALL. |
1645 | * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */ | |
aa319503 | 1646 | fm->table_id = ofm->table_id; |
083761ad | 1647 | if (fm->table_id == OFPTT_ALL |
0e197060 BP |
1648 | && (oh->version == OFP11_VERSION |
1649 | || (ofm->command != OFPFC_DELETE && | |
1650 | ofm->command != OFPFC_DELETE_STRICT))) { | |
1651 | return OFPERR_OFPFMFC_BAD_TABLE_ID; | |
1652 | } | |
1653 | ||
2e4f5fcf BP |
1654 | fm->idle_timeout = ntohs(ofm->idle_timeout); |
1655 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
1656 | fm->buffer_id = ntohl(ofm->buffer_id); | |
aa319503 | 1657 | error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port); |
2e4f5fcf BP |
1658 | if (error) { |
1659 | return error; | |
1660 | } | |
7395c052 | 1661 | |
1fe4c0a0 BP |
1662 | fm->out_group = (ofm->command == OFPFC_DELETE || |
1663 | ofm->command == OFPFC_DELETE_STRICT | |
1664 | ? ntohl(ofm->out_group) | |
1665 | : OFPG11_ANY); | |
0fb88c18 | 1666 | raw_flags = ofm->flags; |
aa319503 | 1667 | } else { |
0fb88c18 BP |
1668 | uint16_t command; |
1669 | ||
aa319503 BP |
1670 | if (raw == OFPRAW_OFPT10_FLOW_MOD) { |
1671 | /* Standard OpenFlow 1.0 flow_mod. */ | |
1672 | const struct ofp10_flow_mod *ofm; | |
aa319503 BP |
1673 | |
1674 | /* Get the ofp10_flow_mod. */ | |
1675 | ofm = ofpbuf_pull(&b, sizeof *ofm); | |
1676 | ||
aa319503 | 1677 | /* Translate the rule. */ |
81a76618 BP |
1678 | ofputil_match_from_ofp10_match(&ofm->match, &fm->match); |
1679 | ofputil_normalize_match(&fm->match); | |
aa319503 BP |
1680 | |
1681 | /* Now get the actions. */ | |
1f317cb5 | 1682 | error = ofpacts_pull_openflow_actions(&b, ofpbuf_size(&b), oh->version, |
e3f8f887 | 1683 | ofpacts); |
aa319503 BP |
1684 | if (error) { |
1685 | return error; | |
1686 | } | |
1687 | ||
81a76618 BP |
1688 | /* OpenFlow 1.0 says that exact-match rules have to have the |
1689 | * highest possible priority. */ | |
1690 | fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL) | |
1691 | ? ntohs(ofm->priority) | |
1692 | : UINT16_MAX); | |
1693 | ||
aa319503 BP |
1694 | /* Translate the message. */ |
1695 | command = ntohs(ofm->command); | |
1696 | fm->cookie = htonll(0); | |
1697 | fm->cookie_mask = htonll(0); | |
1698 | fm->new_cookie = ofm->cookie; | |
1699 | fm->idle_timeout = ntohs(ofm->idle_timeout); | |
1700 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
1701 | fm->buffer_id = ntohl(ofm->buffer_id); | |
4e022ec0 | 1702 | fm->out_port = u16_to_ofp(ntohs(ofm->out_port)); |
7395c052 | 1703 | fm->out_group = OFPG11_ANY; |
0fb88c18 | 1704 | raw_flags = ofm->flags; |
aa319503 BP |
1705 | } else if (raw == OFPRAW_NXT_FLOW_MOD) { |
1706 | /* Nicira extended flow_mod. */ | |
1707 | const struct nx_flow_mod *nfm; | |
aa319503 BP |
1708 | |
1709 | /* Dissect the message. */ | |
1710 | nfm = ofpbuf_pull(&b, sizeof *nfm); | |
81a76618 BP |
1711 | error = nx_pull_match(&b, ntohs(nfm->match_len), |
1712 | &fm->match, &fm->cookie, &fm->cookie_mask); | |
aa319503 BP |
1713 | if (error) { |
1714 | return error; | |
1715 | } | |
1f317cb5 | 1716 | error = ofpacts_pull_openflow_actions(&b, ofpbuf_size(&b), oh->version, |
e3f8f887 | 1717 | ofpacts); |
aa319503 BP |
1718 | if (error) { |
1719 | return error; | |
1720 | } | |
1721 | ||
1722 | /* Translate the message. */ | |
1723 | command = ntohs(nfm->command); | |
1724 | if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) { | |
1725 | /* Flow additions may only set a new cookie, not match an | |
1726 | * existing cookie. */ | |
1727 | return OFPERR_NXBRC_NXM_INVALID; | |
1728 | } | |
81a76618 | 1729 | fm->priority = ntohs(nfm->priority); |
aa319503 BP |
1730 | fm->new_cookie = nfm->cookie; |
1731 | fm->idle_timeout = ntohs(nfm->idle_timeout); | |
1732 | fm->hard_timeout = ntohs(nfm->hard_timeout); | |
1733 | fm->buffer_id = ntohl(nfm->buffer_id); | |
4e022ec0 | 1734 | fm->out_port = u16_to_ofp(ntohs(nfm->out_port)); |
7395c052 | 1735 | fm->out_group = OFPG11_ANY; |
0fb88c18 | 1736 | raw_flags = nfm->flags; |
aa319503 | 1737 | } else { |
428b2edd | 1738 | OVS_NOT_REACHED(); |
aa319503 BP |
1739 | } |
1740 | ||
b8266395 | 1741 | fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX; |
aa319503 BP |
1742 | if (protocol & OFPUTIL_P_TID) { |
1743 | fm->command = command & 0xff; | |
1744 | fm->table_id = command >> 8; | |
1745 | } else { | |
1746 | fm->command = command; | |
1747 | fm->table_id = 0xff; | |
e729e793 | 1748 | } |
2e4f5fcf BP |
1749 | } |
1750 | ||
1f317cb5 PS |
1751 | fm->ofpacts = ofpbuf_data(ofpacts); |
1752 | fm->ofpacts_len = ofpbuf_size(ofpacts); | |
6c1491fb | 1753 | |
0fb88c18 BP |
1754 | error = ofputil_decode_flow_mod_flags(raw_flags, fm->command, |
1755 | oh->version, &fm->flags); | |
1756 | if (error) { | |
1757 | return error; | |
1758 | } | |
1759 | ||
1760 | if (fm->flags & OFPUTIL_FF_EMERG) { | |
1761 | /* We do not support the OpenFlow 1.0 emergency flow cache, which | |
1762 | * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1. | |
1763 | * | |
1764 | * OpenFlow 1.0 specifies the error code to use when idle_timeout | |
1765 | * or hard_timeout is nonzero. Otherwise, there is no good error | |
1766 | * code, so just state that the flow table is full. */ | |
1767 | return (fm->hard_timeout || fm->idle_timeout | |
1768 | ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT | |
1769 | : OFPERR_OFPFMFC_TABLE_FULL); | |
1770 | } | |
1771 | ||
ba2fe8e9 BP |
1772 | return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len, |
1773 | &fm->match.flow, max_port, | |
1774 | fm->table_id, max_table, protocol); | |
2e4f5fcf BP |
1775 | } |
1776 | ||
638a19b0 JR |
1777 | static enum ofperr |
1778 | ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands, | |
1779 | struct ofpbuf *bands) | |
1780 | { | |
1781 | const struct ofp13_meter_band_header *ombh; | |
1782 | struct ofputil_meter_band *mb; | |
1783 | uint16_t n = 0; | |
1784 | ||
1785 | ombh = ofpbuf_try_pull(msg, len); | |
1786 | if (!ombh) { | |
1787 | return OFPERR_OFPBRC_BAD_LEN; | |
1788 | } | |
1789 | ||
1790 | while (len >= sizeof (struct ofp13_meter_band_drop)) { | |
1791 | size_t ombh_len = ntohs(ombh->len); | |
0445637d | 1792 | /* All supported band types have the same length. */ |
638a19b0 JR |
1793 | if (ombh_len != sizeof (struct ofp13_meter_band_drop)) { |
1794 | return OFPERR_OFPBRC_BAD_LEN; | |
1795 | } | |
1796 | mb = ofpbuf_put_uninit(bands, sizeof *mb); | |
1797 | mb->type = ntohs(ombh->type); | |
f99d6aa0 BP |
1798 | if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) { |
1799 | return OFPERR_OFPMMFC_BAD_BAND; | |
1800 | } | |
638a19b0 JR |
1801 | mb->rate = ntohl(ombh->rate); |
1802 | mb->burst_size = ntohl(ombh->burst_size); | |
1803 | mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ? | |
1804 | ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0; | |
1805 | n++; | |
1806 | len -= ombh_len; | |
db5a1019 AW |
1807 | ombh = ALIGNED_CAST(struct ofp13_meter_band_header *, |
1808 | (char *) ombh + ombh_len); | |
638a19b0 JR |
1809 | } |
1810 | if (len) { | |
1811 | return OFPERR_OFPBRC_BAD_LEN; | |
1812 | } | |
1813 | *n_bands = n; | |
1814 | return 0; | |
1815 | } | |
1816 | ||
1817 | enum ofperr | |
1818 | ofputil_decode_meter_mod(const struct ofp_header *oh, | |
1819 | struct ofputil_meter_mod *mm, | |
1820 | struct ofpbuf *bands) | |
1821 | { | |
1822 | const struct ofp13_meter_mod *omm; | |
1823 | struct ofpbuf b; | |
1824 | ||
1825 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1826 | ofpraw_pull_assert(&b); | |
1827 | omm = ofpbuf_pull(&b, sizeof *omm); | |
1828 | ||
1829 | /* Translate the message. */ | |
1830 | mm->command = ntohs(omm->command); | |
142cdb01 BP |
1831 | if (mm->command != OFPMC13_ADD && |
1832 | mm->command != OFPMC13_MODIFY && | |
1833 | mm->command != OFPMC13_DELETE) { | |
1834 | return OFPERR_OFPMMFC_BAD_COMMAND; | |
1835 | } | |
638a19b0 JR |
1836 | mm->meter.meter_id = ntohl(omm->meter_id); |
1837 | ||
1838 | if (mm->command == OFPMC13_DELETE) { | |
1839 | mm->meter.flags = 0; | |
1840 | mm->meter.n_bands = 0; | |
1841 | mm->meter.bands = NULL; | |
1842 | } else { | |
1843 | enum ofperr error; | |
1844 | ||
1845 | mm->meter.flags = ntohs(omm->flags); | |
13b1febe BP |
1846 | if (mm->meter.flags & OFPMF13_KBPS && |
1847 | mm->meter.flags & OFPMF13_PKTPS) { | |
1848 | return OFPERR_OFPMMFC_BAD_FLAGS; | |
1849 | } | |
1f317cb5 | 1850 | mm->meter.bands = ofpbuf_data(bands); |
638a19b0 | 1851 | |
1f317cb5 | 1852 | error = ofputil_pull_bands(&b, ofpbuf_size(&b), &mm->meter.n_bands, bands); |
638a19b0 JR |
1853 | if (error) { |
1854 | return error; | |
1855 | } | |
1856 | } | |
1857 | return 0; | |
1858 | } | |
1859 | ||
1860 | void | |
1861 | ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id) | |
1862 | { | |
1863 | const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh); | |
1864 | *meter_id = ntohl(omr->meter_id); | |
1865 | } | |
1866 | ||
1867 | struct ofpbuf * | |
1868 | ofputil_encode_meter_request(enum ofp_version ofp_version, | |
1869 | enum ofputil_meter_request_type type, | |
1870 | uint32_t meter_id) | |
1871 | { | |
1872 | struct ofpbuf *msg; | |
1873 | ||
1874 | enum ofpraw raw; | |
1875 | ||
1876 | switch (type) { | |
1877 | case OFPUTIL_METER_CONFIG: | |
1878 | raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST; | |
1879 | break; | |
1880 | case OFPUTIL_METER_STATS: | |
1881 | raw = OFPRAW_OFPST13_METER_REQUEST; | |
1882 | break; | |
1883 | default: | |
1884 | case OFPUTIL_METER_FEATURES: | |
1885 | raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST; | |
1886 | break; | |
1887 | } | |
1888 | ||
1889 | msg = ofpraw_alloc(raw, ofp_version, 0); | |
1890 | ||
1891 | if (type != OFPUTIL_METER_FEATURES) { | |
1892 | struct ofp13_meter_multipart_request *omr; | |
1893 | omr = ofpbuf_put_zeros(msg, sizeof *omr); | |
1894 | omr->meter_id = htonl(meter_id); | |
1895 | } | |
1896 | return msg; | |
1897 | } | |
1898 | ||
1899 | static void | |
1900 | ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb, | |
1901 | struct ofpbuf *msg) | |
1902 | { | |
1903 | uint16_t n = 0; | |
1904 | ||
1905 | for (n = 0; n < n_bands; ++n) { | |
0445637d | 1906 | /* Currently all band types have same size. */ |
638a19b0 JR |
1907 | struct ofp13_meter_band_dscp_remark *ombh; |
1908 | size_t ombh_len = sizeof *ombh; | |
1909 | ||
1910 | ombh = ofpbuf_put_zeros(msg, ombh_len); | |
1911 | ||
1912 | ombh->type = htons(mb->type); | |
1913 | ombh->len = htons(ombh_len); | |
1914 | ombh->rate = htonl(mb->rate); | |
1915 | ombh->burst_size = htonl(mb->burst_size); | |
1916 | ombh->prec_level = mb->prec_level; | |
1917 | ||
1918 | mb++; | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | /* Encode a meter stat for 'mc' and append it to 'replies'. */ | |
1923 | void | |
1924 | ofputil_append_meter_config(struct list *replies, | |
1925 | const struct ofputil_meter_config *mc) | |
1926 | { | |
1927 | struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); | |
1f317cb5 | 1928 | size_t start_ofs = ofpbuf_size(msg); |
638a19b0 JR |
1929 | struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply); |
1930 | reply->flags = htons(mc->flags); | |
1931 | reply->meter_id = htonl(mc->meter_id); | |
1932 | ||
1933 | ofputil_put_bands(mc->n_bands, mc->bands, msg); | |
1934 | ||
1f317cb5 | 1935 | reply->length = htons(ofpbuf_size(msg) - start_ofs); |
638a19b0 JR |
1936 | |
1937 | ofpmp_postappend(replies, start_ofs); | |
1938 | } | |
1939 | ||
1940 | /* Encode a meter stat for 'ms' and append it to 'replies'. */ | |
1941 | void | |
1942 | ofputil_append_meter_stats(struct list *replies, | |
1943 | const struct ofputil_meter_stats *ms) | |
1944 | { | |
1945 | struct ofp13_meter_stats *reply; | |
1946 | uint16_t n = 0; | |
1947 | uint16_t len; | |
1948 | ||
1949 | len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats); | |
1950 | reply = ofpmp_append(replies, len); | |
1951 | ||
1952 | reply->meter_id = htonl(ms->meter_id); | |
1953 | reply->len = htons(len); | |
1954 | memset(reply->pad, 0, sizeof reply->pad); | |
1955 | reply->flow_count = htonl(ms->flow_count); | |
1956 | reply->packet_in_count = htonll(ms->packet_in_count); | |
1957 | reply->byte_in_count = htonll(ms->byte_in_count); | |
1958 | reply->duration_sec = htonl(ms->duration_sec); | |
1959 | reply->duration_nsec = htonl(ms->duration_nsec); | |
1960 | ||
1961 | for (n = 0; n < ms->n_bands; ++n) { | |
1962 | const struct ofputil_meter_band_stats *src = &ms->bands[n]; | |
1963 | struct ofp13_meter_band_stats *dst = &reply->band_stats[n]; | |
1964 | ||
1965 | dst->packet_band_count = htonll(src->packet_count); | |
1966 | dst->byte_band_count = htonll(src->byte_count); | |
1967 | } | |
1968 | } | |
1969 | ||
1970 | /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract | |
1971 | * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into | |
1972 | * 'bands'. The caller must have initialized 'bands' and retains ownership of | |
1973 | * it across the call. | |
1974 | * | |
1975 | * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow | |
1976 | * message. Calling this function multiple times for a single 'msg' iterates | |
1977 | * through the replies. 'bands' is cleared for each reply. | |
1978 | * | |
1979 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
1980 | * otherwise a positive errno value. */ | |
1981 | int | |
1982 | ofputil_decode_meter_config(struct ofpbuf *msg, | |
1983 | struct ofputil_meter_config *mc, | |
1984 | struct ofpbuf *bands) | |
1985 | { | |
1986 | const struct ofp13_meter_config *omc; | |
1987 | enum ofperr err; | |
1988 | ||
1989 | /* Pull OpenFlow headers for the first call. */ | |
cf3b7538 | 1990 | if (!msg->frame) { |
638a19b0 JR |
1991 | ofpraw_pull_assert(msg); |
1992 | } | |
1993 | ||
1f317cb5 | 1994 | if (!ofpbuf_size(msg)) { |
638a19b0 JR |
1995 | return EOF; |
1996 | } | |
1997 | ||
1998 | omc = ofpbuf_try_pull(msg, sizeof *omc); | |
1999 | if (!omc) { | |
0445637d | 2000 | VLOG_WARN_RL(&bad_ofmsg_rl, |
437d0d22 | 2001 | "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end", |
1f317cb5 | 2002 | ofpbuf_size(msg)); |
638a19b0 JR |
2003 | return OFPERR_OFPBRC_BAD_LEN; |
2004 | } | |
2005 | ||
2006 | ofpbuf_clear(bands); | |
2007 | err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc, | |
2008 | &mc->n_bands, bands); | |
2009 | if (err) { | |
2010 | return err; | |
2011 | } | |
2012 | mc->meter_id = ntohl(omc->meter_id); | |
2013 | mc->flags = ntohs(omc->flags); | |
1f317cb5 | 2014 | mc->bands = ofpbuf_data(bands); |
638a19b0 JR |
2015 | |
2016 | return 0; | |
2017 | } | |
2018 | ||
2019 | static enum ofperr | |
2020 | ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands, | |
2021 | struct ofpbuf *bands) | |
2022 | { | |
2023 | const struct ofp13_meter_band_stats *ombs; | |
2024 | struct ofputil_meter_band_stats *mbs; | |
2025 | uint16_t n, i; | |
2026 | ||
0445637d JR |
2027 | ombs = ofpbuf_try_pull(msg, len); |
2028 | if (!ombs) { | |
2029 | return OFPERR_OFPBRC_BAD_LEN; | |
2030 | } | |
2031 | ||
638a19b0 JR |
2032 | n = len / sizeof *ombs; |
2033 | if (len != n * sizeof *ombs) { | |
2034 | return OFPERR_OFPBRC_BAD_LEN; | |
2035 | } | |
2036 | ||
638a19b0 JR |
2037 | mbs = ofpbuf_put_uninit(bands, len); |
2038 | ||
2039 | for (i = 0; i < n; ++i) { | |
2040 | mbs[i].packet_count = ntohll(ombs[i].packet_band_count); | |
2041 | mbs[i].byte_count = ntohll(ombs[i].byte_band_count); | |
2042 | } | |
2043 | *n_bands = n; | |
2044 | return 0; | |
2045 | } | |
2046 | ||
2047 | /* Converts an OFPMP_METER reply in 'msg' into an abstract | |
2048 | * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats | |
2049 | * decoded into 'bands'. | |
2050 | * | |
2051 | * Multiple OFPMP_METER replies can be packed into a single OpenFlow | |
2052 | * message. Calling this function multiple times for a single 'msg' iterates | |
2053 | * through the replies. 'bands' is cleared for each reply. | |
2054 | * | |
2055 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
2056 | * otherwise a positive errno value. */ | |
2057 | int | |
2058 | ofputil_decode_meter_stats(struct ofpbuf *msg, | |
2059 | struct ofputil_meter_stats *ms, | |
2060 | struct ofpbuf *bands) | |
2061 | { | |
2062 | const struct ofp13_meter_stats *oms; | |
638a19b0 JR |
2063 | enum ofperr err; |
2064 | ||
2065 | /* Pull OpenFlow headers for the first call. */ | |
cf3b7538 | 2066 | if (!msg->frame) { |
638a19b0 JR |
2067 | ofpraw_pull_assert(msg); |
2068 | } | |
2069 | ||
1f317cb5 | 2070 | if (!ofpbuf_size(msg)) { |
638a19b0 JR |
2071 | return EOF; |
2072 | } | |
2073 | ||
2074 | oms = ofpbuf_try_pull(msg, sizeof *oms); | |
2075 | if (!oms) { | |
0445637d | 2076 | VLOG_WARN_RL(&bad_ofmsg_rl, |
437d0d22 | 2077 | "OFPMP_METER reply has %"PRIu32" leftover bytes at end", |
1f317cb5 | 2078 | ofpbuf_size(msg)); |
638a19b0 JR |
2079 | return OFPERR_OFPBRC_BAD_LEN; |
2080 | } | |
638a19b0 JR |
2081 | |
2082 | ofpbuf_clear(bands); | |
0445637d JR |
2083 | err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms, |
2084 | &ms->n_bands, bands); | |
638a19b0 JR |
2085 | if (err) { |
2086 | return err; | |
2087 | } | |
2088 | ms->meter_id = ntohl(oms->meter_id); | |
2089 | ms->flow_count = ntohl(oms->flow_count); | |
2090 | ms->packet_in_count = ntohll(oms->packet_in_count); | |
2091 | ms->byte_in_count = ntohll(oms->byte_in_count); | |
2092 | ms->duration_sec = ntohl(oms->duration_sec); | |
2093 | ms->duration_nsec = ntohl(oms->duration_nsec); | |
1f317cb5 | 2094 | ms->bands = ofpbuf_data(bands); |
638a19b0 JR |
2095 | |
2096 | return 0; | |
2097 | } | |
2098 | ||
2099 | void | |
2100 | ofputil_decode_meter_features(const struct ofp_header *oh, | |
2101 | struct ofputil_meter_features *mf) | |
2102 | { | |
2103 | const struct ofp13_meter_features *omf = ofpmsg_body(oh); | |
2104 | ||
2105 | mf->max_meters = ntohl(omf->max_meter); | |
2106 | mf->band_types = ntohl(omf->band_types); | |
2107 | mf->capabilities = ntohl(omf->capabilities); | |
2108 | mf->max_bands = omf->max_bands; | |
2109 | mf->max_color = omf->max_color; | |
2110 | } | |
2111 | ||
2112 | struct ofpbuf * | |
2113 | ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf, | |
2114 | const struct ofp_header *request) | |
2115 | { | |
2116 | struct ofpbuf *reply; | |
2117 | struct ofp13_meter_features *omf; | |
2118 | ||
2119 | reply = ofpraw_alloc_stats_reply(request, 0); | |
2120 | omf = ofpbuf_put_zeros(reply, sizeof *omf); | |
2121 | ||
2122 | omf->max_meter = htonl(mf->max_meters); | |
2123 | omf->band_types = htonl(mf->band_types); | |
2124 | omf->capabilities = htonl(mf->capabilities); | |
2125 | omf->max_bands = mf->max_bands; | |
2126 | omf->max_color = mf->max_color; | |
2127 | ||
2128 | return reply; | |
2129 | } | |
2130 | ||
2131 | struct ofpbuf * | |
2132 | ofputil_encode_meter_mod(enum ofp_version ofp_version, | |
2133 | const struct ofputil_meter_mod *mm) | |
2134 | { | |
2135 | struct ofpbuf *msg; | |
2136 | ||
2137 | struct ofp13_meter_mod *omm; | |
2138 | ||
2139 | msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version, | |
2140 | NXM_TYPICAL_LEN + mm->meter.n_bands * 16); | |
2141 | omm = ofpbuf_put_zeros(msg, sizeof *omm); | |
2142 | omm->command = htons(mm->command); | |
2143 | if (mm->command != OFPMC13_DELETE) { | |
2144 | omm->flags = htons(mm->meter.flags); | |
2145 | } | |
2146 | omm->meter_id = htonl(mm->meter.meter_id); | |
2147 | ||
2148 | ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg); | |
2149 | ||
2150 | ofpmsg_update_length(msg); | |
2151 | return msg; | |
2152 | } | |
2153 | ||
aa6305ea SH |
2154 | static ovs_be16 |
2155 | ofputil_tid_command(const struct ofputil_flow_mod *fm, | |
2156 | enum ofputil_protocol protocol) | |
2157 | { | |
2158 | return htons(protocol & OFPUTIL_P_TID | |
2159 | ? (fm->command & 0xff) | (fm->table_id << 8) | |
2160 | : fm->command); | |
2161 | } | |
2162 | ||
2e4f5fcf | 2163 | /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to |
6cbe2c0f | 2164 | * 'protocol' and returns the message. */ |
2e4f5fcf | 2165 | struct ofpbuf * |
a9a2da38 | 2166 | ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm, |
27527aa0 | 2167 | enum ofputil_protocol protocol) |
2e4f5fcf | 2168 | { |
0fb88c18 BP |
2169 | enum ofp_version version = ofputil_protocol_to_ofp_version(protocol); |
2170 | ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version); | |
2e4f5fcf BP |
2171 | struct ofpbuf *msg; |
2172 | ||
27527aa0 | 2173 | switch (protocol) { |
75fa58f8 | 2174 | case OFPUTIL_P_OF11_STD: |
2e1ae200 | 2175 | case OFPUTIL_P_OF12_OXM: |
c37c0382 | 2176 | case OFPUTIL_P_OF13_OXM: |
42dccab5 BP |
2177 | case OFPUTIL_P_OF14_OXM: |
2178 | case OFPUTIL_P_OF15_OXM: { | |
aa6305ea | 2179 | struct ofp11_flow_mod *ofm; |
75fa58f8 | 2180 | int tailroom; |
aa6305ea | 2181 | |
75fa58f8 | 2182 | tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len; |
0fb88c18 | 2183 | msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom); |
aa6305ea | 2184 | ofm = ofpbuf_put_zeros(msg, sizeof *ofm); |
75fa58f8 BP |
2185 | if ((protocol == OFPUTIL_P_OF11_STD |
2186 | && (fm->command == OFPFC_MODIFY || | |
2187 | fm->command == OFPFC_MODIFY_STRICT) | |
2188 | && fm->cookie_mask == htonll(0)) | |
2189 | || fm->command == OFPFC_ADD) { | |
a5ff8823 SH |
2190 | ofm->cookie = fm->new_cookie; |
2191 | } else { | |
2192 | ofm->cookie = fm->cookie; | |
2193 | } | |
aa6305ea | 2194 | ofm->cookie_mask = fm->cookie_mask; |
083761ad | 2195 | if (fm->table_id != OFPTT_ALL |
0e197060 BP |
2196 | || (protocol != OFPUTIL_P_OF11_STD |
2197 | && (fm->command == OFPFC_DELETE || | |
2198 | fm->command == OFPFC_DELETE_STRICT))) { | |
2199 | ofm->table_id = fm->table_id; | |
2200 | } else { | |
2201 | ofm->table_id = 0; | |
2202 | } | |
aa6305ea SH |
2203 | ofm->command = fm->command; |
2204 | ofm->idle_timeout = htons(fm->idle_timeout); | |
2205 | ofm->hard_timeout = htons(fm->hard_timeout); | |
81a76618 | 2206 | ofm->priority = htons(fm->priority); |
aa6305ea SH |
2207 | ofm->buffer_id = htonl(fm->buffer_id); |
2208 | ofm->out_port = ofputil_port_to_ofp11(fm->out_port); | |
7395c052 | 2209 | ofm->out_group = htonl(fm->out_group); |
0fb88c18 | 2210 | ofm->flags = raw_flags; |
75fa58f8 | 2211 | ofputil_put_ofp11_match(msg, &fm->match, protocol); |
e3f8f887 JR |
2212 | ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg, |
2213 | version); | |
aa6305ea SH |
2214 | break; |
2215 | } | |
2216 | ||
85813857 BP |
2217 | case OFPUTIL_P_OF10_STD: |
2218 | case OFPUTIL_P_OF10_STD_TID: { | |
3f192f23 SH |
2219 | struct ofp10_flow_mod *ofm; |
2220 | ||
982697a4 BP |
2221 | msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION, |
2222 | fm->ofpacts_len); | |
2223 | ofm = ofpbuf_put_zeros(msg, sizeof *ofm); | |
81a76618 | 2224 | ofputil_match_to_ofp10_match(&fm->match, &ofm->match); |
623e1caf | 2225 | ofm->cookie = fm->new_cookie; |
aa6305ea | 2226 | ofm->command = ofputil_tid_command(fm, protocol); |
2e4f5fcf BP |
2227 | ofm->idle_timeout = htons(fm->idle_timeout); |
2228 | ofm->hard_timeout = htons(fm->hard_timeout); | |
81a76618 | 2229 | ofm->priority = htons(fm->priority); |
2e4f5fcf | 2230 | ofm->buffer_id = htonl(fm->buffer_id); |
4e022ec0 | 2231 | ofm->out_port = htons(ofp_to_u16(fm->out_port)); |
0fb88c18 | 2232 | ofm->flags = raw_flags; |
e3f8f887 JR |
2233 | ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg, |
2234 | version); | |
27527aa0 | 2235 | break; |
3f192f23 | 2236 | } |
2e4f5fcf | 2237 | |
85813857 BP |
2238 | case OFPUTIL_P_OF10_NXM: |
2239 | case OFPUTIL_P_OF10_NXM_TID: { | |
3f192f23 SH |
2240 | struct nx_flow_mod *nfm; |
2241 | int match_len; | |
2242 | ||
982697a4 BP |
2243 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION, |
2244 | NXM_TYPICAL_LEN + fm->ofpacts_len); | |
2245 | nfm = ofpbuf_put_zeros(msg, sizeof *nfm); | |
aa6305ea | 2246 | nfm->command = ofputil_tid_command(fm, protocol); |
623e1caf | 2247 | nfm->cookie = fm->new_cookie; |
81a76618 | 2248 | match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask); |
6b8c377a | 2249 | nfm = ofpbuf_l3(msg); |
2e4f5fcf BP |
2250 | nfm->idle_timeout = htons(fm->idle_timeout); |
2251 | nfm->hard_timeout = htons(fm->hard_timeout); | |
81a76618 | 2252 | nfm->priority = htons(fm->priority); |
2e4f5fcf | 2253 | nfm->buffer_id = htonl(fm->buffer_id); |
4e022ec0 | 2254 | nfm->out_port = htons(ofp_to_u16(fm->out_port)); |
0fb88c18 | 2255 | nfm->flags = raw_flags; |
2e4f5fcf | 2256 | nfm->match_len = htons(match_len); |
e3f8f887 JR |
2257 | ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg, |
2258 | version); | |
27527aa0 | 2259 | break; |
3f192f23 | 2260 | } |
27527aa0 BP |
2261 | |
2262 | default: | |
428b2edd | 2263 | OVS_NOT_REACHED(); |
2e4f5fcf BP |
2264 | } |
2265 | ||
982697a4 | 2266 | ofpmsg_update_length(msg); |
2e4f5fcf BP |
2267 | return msg; |
2268 | } | |
2269 | ||
90bf1e07 | 2270 | static enum ofperr |
0157ad3a SH |
2271 | ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr, |
2272 | const struct ofp10_flow_stats_request *ofsr, | |
2273 | bool aggregate) | |
2e4f5fcf | 2274 | { |
2e4f5fcf | 2275 | fsr->aggregate = aggregate; |
81a76618 | 2276 | ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match); |
4e022ec0 | 2277 | fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port)); |
7395c052 | 2278 | fsr->out_group = OFPG11_ANY; |
2e4f5fcf | 2279 | fsr->table_id = ofsr->table_id; |
e729e793 | 2280 | fsr->cookie = fsr->cookie_mask = htonll(0); |
2e4f5fcf BP |
2281 | |
2282 | return 0; | |
2283 | } | |
2284 | ||
0157ad3a SH |
2285 | static enum ofperr |
2286 | ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr, | |
2287 | struct ofpbuf *b, bool aggregate) | |
2288 | { | |
2289 | const struct ofp11_flow_stats_request *ofsr; | |
2290 | enum ofperr error; | |
2291 | ||
2292 | ofsr = ofpbuf_pull(b, sizeof *ofsr); | |
2293 | fsr->aggregate = aggregate; | |
2294 | fsr->table_id = ofsr->table_id; | |
2295 | error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port); | |
2296 | if (error) { | |
2297 | return error; | |
2298 | } | |
7395c052 | 2299 | fsr->out_group = ntohl(ofsr->out_group); |
0157ad3a SH |
2300 | fsr->cookie = ofsr->cookie; |
2301 | fsr->cookie_mask = ofsr->cookie_mask; | |
81a76618 | 2302 | error = ofputil_pull_ofp11_match(b, &fsr->match, NULL); |
0157ad3a SH |
2303 | if (error) { |
2304 | return error; | |
2305 | } | |
2306 | ||
2307 | return 0; | |
2308 | } | |
2309 | ||
90bf1e07 | 2310 | static enum ofperr |
81d1ea94 | 2311 | ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr, |
982697a4 | 2312 | struct ofpbuf *b, bool aggregate) |
2e4f5fcf BP |
2313 | { |
2314 | const struct nx_flow_stats_request *nfsr; | |
90bf1e07 | 2315 | enum ofperr error; |
2e4f5fcf | 2316 | |
982697a4 | 2317 | nfsr = ofpbuf_pull(b, sizeof *nfsr); |
81a76618 | 2318 | error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match, |
e729e793 | 2319 | &fsr->cookie, &fsr->cookie_mask); |
2e4f5fcf BP |
2320 | if (error) { |
2321 | return error; | |
2322 | } | |
1f317cb5 | 2323 | if (ofpbuf_size(b)) { |
90bf1e07 | 2324 | return OFPERR_OFPBRC_BAD_LEN; |
2e4f5fcf BP |
2325 | } |
2326 | ||
2327 | fsr->aggregate = aggregate; | |
4e022ec0 | 2328 | fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port)); |
7395c052 | 2329 | fsr->out_group = OFPG11_ANY; |
2e4f5fcf BP |
2330 | fsr->table_id = nfsr->table_id; |
2331 | ||
2332 | return 0; | |
2333 | } | |
2334 | ||
e8f9a7bb VG |
2335 | /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified |
2336 | * 'port', suitable for OpenFlow version 'version'. */ | |
2337 | struct ofpbuf * | |
2338 | ofputil_encode_queue_get_config_request(enum ofp_version version, | |
2339 | ofp_port_t port) | |
2340 | { | |
2341 | struct ofpbuf *request; | |
2342 | ||
2343 | if (version == OFP10_VERSION) { | |
2344 | struct ofp10_queue_get_config_request *qgcr10; | |
2345 | ||
2346 | request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST, | |
2347 | version, 0); | |
2348 | qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10); | |
2349 | qgcr10->port = htons(ofp_to_u16(port)); | |
2350 | } else { | |
2351 | struct ofp11_queue_get_config_request *qgcr11; | |
2352 | ||
2353 | request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST, | |
2354 | version, 0); | |
2355 | qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11); | |
2356 | qgcr11->port = ofputil_port_to_ofp11(port); | |
2357 | } | |
2358 | ||
2359 | return request; | |
2360 | } | |
2361 | ||
2362 | /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the | |
2363 | * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error | |
2364 | * code. */ | |
2365 | enum ofperr | |
2366 | ofputil_decode_queue_get_config_request(const struct ofp_header *oh, | |
2367 | ofp_port_t *port) | |
2368 | { | |
2369 | const struct ofp10_queue_get_config_request *qgcr10; | |
2370 | const struct ofp11_queue_get_config_request *qgcr11; | |
2371 | enum ofpraw raw; | |
2372 | struct ofpbuf b; | |
2373 | ||
2374 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2375 | raw = ofpraw_pull_assert(&b); | |
2376 | ||
2377 | switch ((int) raw) { | |
2378 | case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST: | |
1f317cb5 | 2379 | qgcr10 = ofpbuf_data(&b); |
e8f9a7bb VG |
2380 | *port = u16_to_ofp(ntohs(qgcr10->port)); |
2381 | return 0; | |
2382 | ||
2383 | case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST: | |
1f317cb5 | 2384 | qgcr11 = ofpbuf_data(&b); |
e8f9a7bb VG |
2385 | return ofputil_port_from_ofp11(qgcr11->port, port); |
2386 | } | |
2387 | ||
428b2edd | 2388 | OVS_NOT_REACHED(); |
e8f9a7bb VG |
2389 | } |
2390 | ||
2391 | /* Constructs and returns the beginning of a reply to | |
2392 | * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about | |
2393 | * individual queues with ofputil_append_queue_get_config_reply(). */ | |
2394 | struct ofpbuf * | |
2395 | ofputil_encode_queue_get_config_reply(const struct ofp_header *oh) | |
2396 | { | |
2397 | struct ofp10_queue_get_config_reply *qgcr10; | |
2398 | struct ofp11_queue_get_config_reply *qgcr11; | |
2399 | struct ofpbuf *reply; | |
2400 | enum ofperr error; | |
2401 | struct ofpbuf b; | |
2402 | enum ofpraw raw; | |
2403 | ofp_port_t port; | |
2404 | ||
2405 | error = ofputil_decode_queue_get_config_request(oh, &port); | |
2406 | ovs_assert(!error); | |
2407 | ||
2408 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2409 | raw = ofpraw_pull_assert(&b); | |
2410 | ||
2411 | switch ((int) raw) { | |
2412 | case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST: | |
2413 | reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY, | |
2414 | oh, 0); | |
2415 | qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10); | |
2416 | qgcr10->port = htons(ofp_to_u16(port)); | |
2417 | break; | |
2418 | ||
2419 | case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST: | |
2420 | reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY, | |
2421 | oh, 0); | |
2422 | qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11); | |
2423 | qgcr11->port = ofputil_port_to_ofp11(port); | |
2424 | break; | |
2425 | ||
2426 | default: | |
428b2edd | 2427 | OVS_NOT_REACHED(); |
e8f9a7bb VG |
2428 | } |
2429 | ||
2430 | return reply; | |
2431 | } | |
2432 | ||
2433 | static void | |
2434 | put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property, | |
2435 | uint16_t rate) | |
2436 | { | |
2437 | if (rate != UINT16_MAX) { | |
2438 | struct ofp_queue_prop_rate *oqpr; | |
2439 | ||
2440 | oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr); | |
2441 | oqpr->prop_header.property = htons(property); | |
2442 | oqpr->prop_header.len = htons(sizeof *oqpr); | |
2443 | oqpr->rate = htons(rate); | |
2444 | } | |
2445 | } | |
2446 | ||
2447 | /* Appends a queue description for 'queue_id' to the | |
2448 | * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */ | |
2449 | void | |
2450 | ofputil_append_queue_get_config_reply(struct ofpbuf *reply, | |
2451 | const struct ofputil_queue_config *oqc) | |
2452 | { | |
1f317cb5 | 2453 | const struct ofp_header *oh = ofpbuf_data(reply); |
e8f9a7bb VG |
2454 | size_t start_ofs, len_ofs; |
2455 | ovs_be16 *len; | |
2456 | ||
1f317cb5 | 2457 | start_ofs = ofpbuf_size(reply); |
e8f9a7bb VG |
2458 | if (oh->version < OFP12_VERSION) { |
2459 | struct ofp10_packet_queue *opq10; | |
2460 | ||
2461 | opq10 = ofpbuf_put_zeros(reply, sizeof *opq10); | |
2462 | opq10->queue_id = htonl(oqc->queue_id); | |
1f317cb5 | 2463 | len_ofs = (char *) &opq10->len - (char *) ofpbuf_data(reply); |
e8f9a7bb VG |
2464 | } else { |
2465 | struct ofp11_queue_get_config_reply *qgcr11; | |
2466 | struct ofp12_packet_queue *opq12; | |
2467 | ovs_be32 port; | |
2468 | ||
6b8c377a | 2469 | qgcr11 = ofpbuf_l3(reply); |
e8f9a7bb VG |
2470 | port = qgcr11->port; |
2471 | ||
2472 | opq12 = ofpbuf_put_zeros(reply, sizeof *opq12); | |
2473 | opq12->port = port; | |
2474 | opq12->queue_id = htonl(oqc->queue_id); | |
1f317cb5 | 2475 | len_ofs = (char *) &opq12->len - (char *) ofpbuf_data(reply); |
e8f9a7bb VG |
2476 | } |
2477 | ||
2478 | put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate); | |
2479 | put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate); | |
2480 | ||
2481 | len = ofpbuf_at(reply, len_ofs, sizeof *len); | |
1f317cb5 | 2482 | *len = htons(ofpbuf_size(reply) - start_ofs); |
e8f9a7bb VG |
2483 | } |
2484 | ||
2485 | /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and | |
2486 | * stores in '*port' the port that the reply is about. The caller may call | |
2487 | * ofputil_pull_queue_get_config_reply() to obtain information about individual | |
2488 | * queues included in the reply. Returns 0 if successful, otherwise an | |
2489 | * ofperr.*/ | |
2490 | enum ofperr | |
2491 | ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port) | |
2492 | { | |
2493 | const struct ofp10_queue_get_config_reply *qgcr10; | |
2494 | const struct ofp11_queue_get_config_reply *qgcr11; | |
2495 | enum ofpraw raw; | |
2496 | ||
2497 | raw = ofpraw_pull_assert(reply); | |
2498 | switch ((int) raw) { | |
2499 | case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY: | |
2500 | qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10); | |
2501 | *port = u16_to_ofp(ntohs(qgcr10->port)); | |
2502 | return 0; | |
2503 | ||
2504 | case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY: | |
2505 | qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11); | |
2506 | return ofputil_port_from_ofp11(qgcr11->port, port); | |
2507 | } | |
2508 | ||
428b2edd | 2509 | OVS_NOT_REACHED(); |
e8f9a7bb VG |
2510 | } |
2511 | ||
2512 | static enum ofperr | |
2513 | parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate) | |
2514 | { | |
2515 | const struct ofp_queue_prop_rate *oqpr; | |
2516 | ||
2517 | if (hdr->len == htons(sizeof *oqpr)) { | |
2518 | oqpr = (const struct ofp_queue_prop_rate *) hdr; | |
2519 | *rate = ntohs(oqpr->rate); | |
2520 | return 0; | |
2521 | } else { | |
2522 | return OFPERR_OFPBRC_BAD_LEN; | |
2523 | } | |
2524 | } | |
2525 | ||
2526 | /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in | |
2527 | * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply() | |
2528 | * must already have pulled off the main header. | |
2529 | * | |
2530 | * This function returns EOF if the last queue has already been decoded, 0 if a | |
2531 | * queue was successfully decoded into '*queue', or an ofperr if there was a | |
2532 | * problem decoding 'reply'. */ | |
2533 | int | |
2534 | ofputil_pull_queue_get_config_reply(struct ofpbuf *reply, | |
2535 | struct ofputil_queue_config *queue) | |
2536 | { | |
2537 | const struct ofp_header *oh; | |
2538 | unsigned int opq_len; | |
2539 | unsigned int len; | |
2540 | ||
1f317cb5 | 2541 | if (!ofpbuf_size(reply)) { |
e8f9a7bb VG |
2542 | return EOF; |
2543 | } | |
2544 | ||
2545 | queue->min_rate = UINT16_MAX; | |
2546 | queue->max_rate = UINT16_MAX; | |
2547 | ||
cf3b7538 | 2548 | oh = reply->frame; |
e8f9a7bb VG |
2549 | if (oh->version < OFP12_VERSION) { |
2550 | const struct ofp10_packet_queue *opq10; | |
2551 | ||
2552 | opq10 = ofpbuf_try_pull(reply, sizeof *opq10); | |
2553 | if (!opq10) { | |
2554 | return OFPERR_OFPBRC_BAD_LEN; | |
2555 | } | |
2556 | queue->queue_id = ntohl(opq10->queue_id); | |
2557 | len = ntohs(opq10->len); | |
2558 | opq_len = sizeof *opq10; | |
2559 | } else { | |
2560 | const struct ofp12_packet_queue *opq12; | |
2561 | ||
2562 | opq12 = ofpbuf_try_pull(reply, sizeof *opq12); | |
2563 | if (!opq12) { | |
2564 | return OFPERR_OFPBRC_BAD_LEN; | |
2565 | } | |
2566 | queue->queue_id = ntohl(opq12->queue_id); | |
2567 | len = ntohs(opq12->len); | |
2568 | opq_len = sizeof *opq12; | |
2569 | } | |
2570 | ||
1f317cb5 | 2571 | if (len < opq_len || len > ofpbuf_size(reply) + opq_len || len % 8) { |
e8f9a7bb VG |
2572 | return OFPERR_OFPBRC_BAD_LEN; |
2573 | } | |
2574 | len -= opq_len; | |
2575 | ||
2576 | while (len > 0) { | |
2577 | const struct ofp_queue_prop_header *hdr; | |
2578 | unsigned int property; | |
2579 | unsigned int prop_len; | |
2580 | enum ofperr error = 0; | |
2581 | ||
2582 | hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr); | |
2583 | prop_len = ntohs(hdr->len); | |
1f317cb5 | 2584 | if (prop_len < sizeof *hdr || prop_len > ofpbuf_size(reply) || prop_len % 8) { |
e8f9a7bb VG |
2585 | return OFPERR_OFPBRC_BAD_LEN; |
2586 | } | |
2587 | ||
2588 | property = ntohs(hdr->property); | |
2589 | switch (property) { | |
2590 | case OFPQT_MIN_RATE: | |
2591 | error = parse_queue_rate(hdr, &queue->min_rate); | |
2592 | break; | |
2593 | ||
2594 | case OFPQT_MAX_RATE: | |
2595 | error = parse_queue_rate(hdr, &queue->max_rate); | |
2596 | break; | |
2597 | ||
2598 | default: | |
2599 | VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property); | |
2600 | break; | |
2601 | } | |
2602 | if (error) { | |
2603 | return error; | |
2604 | } | |
2605 | ||
2606 | ofpbuf_pull(reply, prop_len); | |
2607 | len -= prop_len; | |
2608 | } | |
2609 | return 0; | |
2610 | } | |
2611 | ||
2e4f5fcf | 2612 | /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE |
b78f6b77 BP |
2613 | * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if |
2614 | * successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 2615 | enum ofperr |
81d1ea94 | 2616 | ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr, |
b78f6b77 | 2617 | const struct ofp_header *oh) |
2e4f5fcf | 2618 | { |
982697a4 | 2619 | enum ofpraw raw; |
2e4f5fcf | 2620 | struct ofpbuf b; |
2e4f5fcf | 2621 | |
2013493b | 2622 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
982697a4 BP |
2623 | raw = ofpraw_pull_assert(&b); |
2624 | switch ((int) raw) { | |
cfc23141 | 2625 | case OFPRAW_OFPST10_FLOW_REQUEST: |
1f317cb5 | 2626 | return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), false); |
2e4f5fcf | 2627 | |
617da9cd | 2628 | case OFPRAW_OFPST10_AGGREGATE_REQUEST: |
1f317cb5 | 2629 | return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), true); |
0157ad3a SH |
2630 | |
2631 | case OFPRAW_OFPST11_FLOW_REQUEST: | |
2632 | return ofputil_decode_ofpst11_flow_request(fsr, &b, false); | |
2e4f5fcf | 2633 | |
617da9cd SH |
2634 | case OFPRAW_OFPST11_AGGREGATE_REQUEST: |
2635 | return ofputil_decode_ofpst11_flow_request(fsr, &b, true); | |
2636 | ||
982697a4 BP |
2637 | case OFPRAW_NXST_FLOW_REQUEST: |
2638 | return ofputil_decode_nxst_flow_request(fsr, &b, false); | |
2e4f5fcf | 2639 | |
982697a4 BP |
2640 | case OFPRAW_NXST_AGGREGATE_REQUEST: |
2641 | return ofputil_decode_nxst_flow_request(fsr, &b, true); | |
2e4f5fcf BP |
2642 | |
2643 | default: | |
2644 | /* Hey, the caller lied. */ | |
428b2edd | 2645 | OVS_NOT_REACHED(); |
2e4f5fcf BP |
2646 | } |
2647 | } | |
2648 | ||
2649 | /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW, | |
4ffd1b43 | 2650 | * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to |
27527aa0 | 2651 | * 'protocol', and returns the message. */ |
2e4f5fcf | 2652 | struct ofpbuf * |
81d1ea94 | 2653 | ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr, |
27527aa0 | 2654 | enum ofputil_protocol protocol) |
2e4f5fcf BP |
2655 | { |
2656 | struct ofpbuf *msg; | |
982697a4 | 2657 | enum ofpraw raw; |
2e4f5fcf | 2658 | |
27527aa0 | 2659 | switch (protocol) { |
75fa58f8 | 2660 | case OFPUTIL_P_OF11_STD: |
2e1ae200 | 2661 | case OFPUTIL_P_OF12_OXM: |
c37c0382 | 2662 | case OFPUTIL_P_OF13_OXM: |
42dccab5 BP |
2663 | case OFPUTIL_P_OF14_OXM: |
2664 | case OFPUTIL_P_OF15_OXM: { | |
06516c65 SH |
2665 | struct ofp11_flow_stats_request *ofsr; |
2666 | ||
2667 | raw = (fsr->aggregate | |
617da9cd | 2668 | ? OFPRAW_OFPST11_AGGREGATE_REQUEST |
cfc23141 | 2669 | : OFPRAW_OFPST11_FLOW_REQUEST); |
2e1ae200 | 2670 | msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol), |
75fa58f8 | 2671 | ofputil_match_typical_len(protocol)); |
06516c65 SH |
2672 | ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr); |
2673 | ofsr->table_id = fsr->table_id; | |
2674 | ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port); | |
7395c052 | 2675 | ofsr->out_group = htonl(fsr->out_group); |
06516c65 SH |
2676 | ofsr->cookie = fsr->cookie; |
2677 | ofsr->cookie_mask = fsr->cookie_mask; | |
75fa58f8 | 2678 | ofputil_put_ofp11_match(msg, &fsr->match, protocol); |
06516c65 SH |
2679 | break; |
2680 | } | |
2681 | ||
85813857 BP |
2682 | case OFPUTIL_P_OF10_STD: |
2683 | case OFPUTIL_P_OF10_STD_TID: { | |
e2b9ac44 | 2684 | struct ofp10_flow_stats_request *ofsr; |
2e4f5fcf | 2685 | |
982697a4 | 2686 | raw = (fsr->aggregate |
617da9cd | 2687 | ? OFPRAW_OFPST10_AGGREGATE_REQUEST |
cfc23141 | 2688 | : OFPRAW_OFPST10_FLOW_REQUEST); |
982697a4 BP |
2689 | msg = ofpraw_alloc(raw, OFP10_VERSION, 0); |
2690 | ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr); | |
81a76618 | 2691 | ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match); |
2e4f5fcf | 2692 | ofsr->table_id = fsr->table_id; |
4e022ec0 | 2693 | ofsr->out_port = htons(ofp_to_u16(fsr->out_port)); |
27527aa0 BP |
2694 | break; |
2695 | } | |
2696 | ||
85813857 BP |
2697 | case OFPUTIL_P_OF10_NXM: |
2698 | case OFPUTIL_P_OF10_NXM_TID: { | |
2e4f5fcf BP |
2699 | struct nx_flow_stats_request *nfsr; |
2700 | int match_len; | |
2701 | ||
982697a4 BP |
2702 | raw = (fsr->aggregate |
2703 | ? OFPRAW_NXST_AGGREGATE_REQUEST | |
2704 | : OFPRAW_NXST_FLOW_REQUEST); | |
06516c65 | 2705 | msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN); |
982697a4 | 2706 | ofpbuf_put_zeros(msg, sizeof *nfsr); |
7623f4dd | 2707 | match_len = nx_put_match(msg, &fsr->match, |
e729e793 | 2708 | fsr->cookie, fsr->cookie_mask); |
2e4f5fcf | 2709 | |
6b8c377a | 2710 | nfsr = ofpbuf_l3(msg); |
4e022ec0 | 2711 | nfsr->out_port = htons(ofp_to_u16(fsr->out_port)); |
2e4f5fcf BP |
2712 | nfsr->match_len = htons(match_len); |
2713 | nfsr->table_id = fsr->table_id; | |
27527aa0 BP |
2714 | break; |
2715 | } | |
2716 | ||
2717 | default: | |
428b2edd | 2718 | OVS_NOT_REACHED(); |
2e4f5fcf BP |
2719 | } |
2720 | ||
2721 | return msg; | |
2722 | } | |
d1e2cf21 | 2723 | |
4ffd1b43 | 2724 | /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract |
b78f6b77 | 2725 | * ofputil_flow_stats in 'fs'. |
4ffd1b43 BP |
2726 | * |
2727 | * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single | |
2728 | * OpenFlow message. Calling this function multiple times for a single 'msg' | |
2729 | * iterates through the replies. The caller must initially leave 'msg''s layer | |
2730 | * pointers null and not modify them between calls. | |
2731 | * | |
f27f2134 BP |
2732 | * Most switches don't send the values needed to populate fs->idle_age and |
2733 | * fs->hard_age, so those members will usually be set to 0. If the switch from | |
2734 | * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass | |
2735 | * 'flow_age_extension' as true so that the contents of 'msg' determine the | |
2736 | * 'idle_age' and 'hard_age' members in 'fs'. | |
2737 | * | |
f25d0cf3 BP |
2738 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats |
2739 | * reply's actions. The caller must initialize 'ofpacts' and retains ownership | |
2740 | * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer. | |
2741 | * | |
4ffd1b43 BP |
2742 | * Returns 0 if successful, EOF if no replies were left in this 'msg', |
2743 | * otherwise a positive errno value. */ | |
2744 | int | |
2745 | ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs, | |
f27f2134 | 2746 | struct ofpbuf *msg, |
f25d0cf3 BP |
2747 | bool flow_age_extension, |
2748 | struct ofpbuf *ofpacts) | |
4ffd1b43 | 2749 | { |
0fb88c18 | 2750 | const struct ofp_header *oh; |
982697a4 BP |
2751 | enum ofperr error; |
2752 | enum ofpraw raw; | |
4ffd1b43 | 2753 | |
cf3b7538 JR |
2754 | error = (msg->frame |
2755 | ? ofpraw_decode(&raw, msg->frame) | |
982697a4 BP |
2756 | : ofpraw_pull(&raw, msg)); |
2757 | if (error) { | |
2758 | return error; | |
4ffd1b43 | 2759 | } |
cf3b7538 | 2760 | oh = msg->frame; |
4ffd1b43 | 2761 | |
1f317cb5 | 2762 | if (!ofpbuf_size(msg)) { |
4ffd1b43 | 2763 | return EOF; |
2e1ae200 JR |
2764 | } else if (raw == OFPRAW_OFPST11_FLOW_REPLY |
2765 | || raw == OFPRAW_OFPST13_FLOW_REPLY) { | |
6ec5f0c5 SH |
2766 | const struct ofp11_flow_stats *ofs; |
2767 | size_t length; | |
2768 | uint16_t padded_match_len; | |
2769 | ||
2770 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
2771 | if (!ofs) { | |
437d0d22 | 2772 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover " |
1f317cb5 | 2773 | "bytes at end", ofpbuf_size(msg)); |
6ec5f0c5 SH |
2774 | return EINVAL; |
2775 | } | |
2776 | ||
2777 | length = ntohs(ofs->length); | |
2778 | if (length < sizeof *ofs) { | |
2779 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
34582733 | 2780 | "length %"PRIuSIZE, length); |
6ec5f0c5 SH |
2781 | return EINVAL; |
2782 | } | |
2783 | ||
81a76618 | 2784 | if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) { |
6ec5f0c5 SH |
2785 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match"); |
2786 | return EINVAL; | |
2787 | } | |
2788 | ||
e3f8f887 JR |
2789 | if (ofpacts_pull_openflow_instructions(msg, length - sizeof *ofs - |
2790 | padded_match_len, oh->version, | |
2791 | ofpacts)) { | |
6ec5f0c5 SH |
2792 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions"); |
2793 | return EINVAL; | |
2794 | } | |
2795 | ||
81a76618 | 2796 | fs->priority = ntohs(ofs->priority); |
6ec5f0c5 SH |
2797 | fs->table_id = ofs->table_id; |
2798 | fs->duration_sec = ntohl(ofs->duration_sec); | |
2799 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
2800 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
2801 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
0fb88c18 BP |
2802 | if (raw == OFPRAW_OFPST13_FLOW_REPLY) { |
2803 | error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version, | |
2804 | &fs->flags); | |
2805 | if (error) { | |
2806 | return error; | |
2807 | } | |
2808 | } else { | |
2809 | fs->flags = 0; | |
2810 | } | |
6ec5f0c5 SH |
2811 | fs->idle_age = -1; |
2812 | fs->hard_age = -1; | |
2813 | fs->cookie = ofs->cookie; | |
2814 | fs->packet_count = ntohll(ofs->packet_count); | |
2815 | fs->byte_count = ntohll(ofs->byte_count); | |
cfc23141 | 2816 | } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) { |
e2b9ac44 | 2817 | const struct ofp10_flow_stats *ofs; |
4ffd1b43 BP |
2818 | size_t length; |
2819 | ||
2820 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
2821 | if (!ofs) { | |
437d0d22 | 2822 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover " |
1f317cb5 | 2823 | "bytes at end", ofpbuf_size(msg)); |
4ffd1b43 BP |
2824 | return EINVAL; |
2825 | } | |
2826 | ||
2827 | length = ntohs(ofs->length); | |
2828 | if (length < sizeof *ofs) { | |
2829 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
34582733 | 2830 | "length %"PRIuSIZE, length); |
4ffd1b43 BP |
2831 | return EINVAL; |
2832 | } | |
2833 | ||
e3f8f887 JR |
2834 | if (ofpacts_pull_openflow_actions(msg, length - sizeof *ofs, |
2835 | oh->version, ofpacts)) { | |
4ffd1b43 BP |
2836 | return EINVAL; |
2837 | } | |
2838 | ||
2839 | fs->cookie = get_32aligned_be64(&ofs->cookie); | |
81a76618 BP |
2840 | ofputil_match_from_ofp10_match(&ofs->match, &fs->match); |
2841 | fs->priority = ntohs(ofs->priority); | |
4ffd1b43 BP |
2842 | fs->table_id = ofs->table_id; |
2843 | fs->duration_sec = ntohl(ofs->duration_sec); | |
2844 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
2845 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
2846 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
f27f2134 BP |
2847 | fs->idle_age = -1; |
2848 | fs->hard_age = -1; | |
4ffd1b43 BP |
2849 | fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count)); |
2850 | fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count)); | |
2e1ae200 | 2851 | fs->flags = 0; |
982697a4 | 2852 | } else if (raw == OFPRAW_NXST_FLOW_REPLY) { |
4ffd1b43 | 2853 | const struct nx_flow_stats *nfs; |
f25d0cf3 | 2854 | size_t match_len, actions_len, length; |
4ffd1b43 BP |
2855 | |
2856 | nfs = ofpbuf_try_pull(msg, sizeof *nfs); | |
2857 | if (!nfs) { | |
437d0d22 | 2858 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover " |
1f317cb5 | 2859 | "bytes at end", ofpbuf_size(msg)); |
4ffd1b43 BP |
2860 | return EINVAL; |
2861 | } | |
2862 | ||
2863 | length = ntohs(nfs->length); | |
2864 | match_len = ntohs(nfs->match_len); | |
2865 | if (length < sizeof *nfs + ROUND_UP(match_len, 8)) { | |
34582733 AS |
2866 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" " |
2867 | "claims invalid length %"PRIuSIZE, match_len, length); | |
4ffd1b43 BP |
2868 | return EINVAL; |
2869 | } | |
81a76618 | 2870 | if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) { |
4ffd1b43 BP |
2871 | return EINVAL; |
2872 | } | |
2873 | ||
f25d0cf3 | 2874 | actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8); |
e3f8f887 JR |
2875 | if (ofpacts_pull_openflow_actions(msg, actions_len, oh->version, |
2876 | ofpacts)) { | |
4ffd1b43 BP |
2877 | return EINVAL; |
2878 | } | |
2879 | ||
2880 | fs->cookie = nfs->cookie; | |
2881 | fs->table_id = nfs->table_id; | |
2882 | fs->duration_sec = ntohl(nfs->duration_sec); | |
2883 | fs->duration_nsec = ntohl(nfs->duration_nsec); | |
81a76618 | 2884 | fs->priority = ntohs(nfs->priority); |
4ffd1b43 BP |
2885 | fs->idle_timeout = ntohs(nfs->idle_timeout); |
2886 | fs->hard_timeout = ntohs(nfs->hard_timeout); | |
f27f2134 BP |
2887 | fs->idle_age = -1; |
2888 | fs->hard_age = -1; | |
2889 | if (flow_age_extension) { | |
2890 | if (nfs->idle_age) { | |
2891 | fs->idle_age = ntohs(nfs->idle_age) - 1; | |
2892 | } | |
2893 | if (nfs->hard_age) { | |
2894 | fs->hard_age = ntohs(nfs->hard_age) - 1; | |
2895 | } | |
2896 | } | |
4ffd1b43 BP |
2897 | fs->packet_count = ntohll(nfs->packet_count); |
2898 | fs->byte_count = ntohll(nfs->byte_count); | |
2e1ae200 | 2899 | fs->flags = 0; |
4ffd1b43 | 2900 | } else { |
428b2edd | 2901 | OVS_NOT_REACHED(); |
4ffd1b43 BP |
2902 | } |
2903 | ||
1f317cb5 PS |
2904 | fs->ofpacts = ofpbuf_data(ofpacts); |
2905 | fs->ofpacts_len = ofpbuf_size(ofpacts); | |
f25d0cf3 | 2906 | |
4ffd1b43 BP |
2907 | return 0; |
2908 | } | |
2909 | ||
5e9d0469 BP |
2910 | /* Returns 'count' unchanged except that UINT64_MAX becomes 0. |
2911 | * | |
2912 | * We use this in situations where OVS internally uses UINT64_MAX to mean | |
2913 | * "value unknown" but OpenFlow 1.0 does not define any unknown value. */ | |
2914 | static uint64_t | |
2915 | unknown_to_zero(uint64_t count) | |
2916 | { | |
2917 | return count != UINT64_MAX ? count : 0; | |
2918 | } | |
2919 | ||
349adfb2 BP |
2920 | /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to |
2921 | * those already present in the list of ofpbufs in 'replies'. 'replies' should | |
7395c052 | 2922 | * have been initialized with ofpmp_init(). */ |
349adfb2 BP |
2923 | void |
2924 | ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs, | |
2925 | struct list *replies) | |
2926 | { | |
f25d0cf3 | 2927 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); |
1f317cb5 | 2928 | size_t start_ofs = ofpbuf_size(reply); |
e28ac5cf BP |
2929 | enum ofp_version version = ofpmp_version(replies); |
2930 | enum ofpraw raw = ofpmp_decode_raw(replies); | |
349adfb2 | 2931 | |
2e1ae200 | 2932 | if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) { |
22a86f18 SH |
2933 | struct ofp11_flow_stats *ofs; |
2934 | ||
2935 | ofpbuf_put_uninit(reply, sizeof *ofs); | |
9d84066c | 2936 | oxm_put_match(reply, &fs->match, version); |
e3f8f887 JR |
2937 | ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply, |
2938 | version); | |
22a86f18 SH |
2939 | |
2940 | ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs); | |
1f317cb5 | 2941 | ofs->length = htons(ofpbuf_size(reply) - start_ofs); |
22a86f18 SH |
2942 | ofs->table_id = fs->table_id; |
2943 | ofs->pad = 0; | |
2944 | ofs->duration_sec = htonl(fs->duration_sec); | |
2945 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
81a76618 | 2946 | ofs->priority = htons(fs->priority); |
22a86f18 SH |
2947 | ofs->idle_timeout = htons(fs->idle_timeout); |
2948 | ofs->hard_timeout = htons(fs->hard_timeout); | |
0fb88c18 | 2949 | if (raw == OFPRAW_OFPST13_FLOW_REPLY) { |
e3f8f887 | 2950 | ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version); |
0fb88c18 BP |
2951 | } else { |
2952 | ofs->flags = 0; | |
2953 | } | |
22a86f18 SH |
2954 | memset(ofs->pad2, 0, sizeof ofs->pad2); |
2955 | ofs->cookie = fs->cookie; | |
2956 | ofs->packet_count = htonll(unknown_to_zero(fs->packet_count)); | |
2957 | ofs->byte_count = htonll(unknown_to_zero(fs->byte_count)); | |
2958 | } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) { | |
e2b9ac44 | 2959 | struct ofp10_flow_stats *ofs; |
349adfb2 | 2960 | |
1a59dc2c | 2961 | ofpbuf_put_uninit(reply, sizeof *ofs); |
e3f8f887 JR |
2962 | ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply, |
2963 | version); | |
1a59dc2c | 2964 | ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs); |
1f317cb5 | 2965 | ofs->length = htons(ofpbuf_size(reply) - start_ofs); |
349adfb2 BP |
2966 | ofs->table_id = fs->table_id; |
2967 | ofs->pad = 0; | |
81a76618 | 2968 | ofputil_match_to_ofp10_match(&fs->match, &ofs->match); |
349adfb2 BP |
2969 | ofs->duration_sec = htonl(fs->duration_sec); |
2970 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
81a76618 | 2971 | ofs->priority = htons(fs->priority); |
349adfb2 BP |
2972 | ofs->idle_timeout = htons(fs->idle_timeout); |
2973 | ofs->hard_timeout = htons(fs->hard_timeout); | |
2974 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
2975 | put_32aligned_be64(&ofs->cookie, fs->cookie); | |
5e9d0469 BP |
2976 | put_32aligned_be64(&ofs->packet_count, |
2977 | htonll(unknown_to_zero(fs->packet_count))); | |
2978 | put_32aligned_be64(&ofs->byte_count, | |
2979 | htonll(unknown_to_zero(fs->byte_count))); | |
982697a4 | 2980 | } else if (raw == OFPRAW_NXST_FLOW_REPLY) { |
349adfb2 | 2981 | struct nx_flow_stats *nfs; |
1a59dc2c | 2982 | int match_len; |
349adfb2 | 2983 | |
1a59dc2c | 2984 | ofpbuf_put_uninit(reply, sizeof *nfs); |
81a76618 | 2985 | match_len = nx_put_match(reply, &fs->match, 0, 0); |
e3f8f887 JR |
2986 | ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply, |
2987 | version); | |
1a59dc2c | 2988 | nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs); |
1f317cb5 | 2989 | nfs->length = htons(ofpbuf_size(reply) - start_ofs); |
349adfb2 BP |
2990 | nfs->table_id = fs->table_id; |
2991 | nfs->pad = 0; | |
2992 | nfs->duration_sec = htonl(fs->duration_sec); | |
2993 | nfs->duration_nsec = htonl(fs->duration_nsec); | |
81a76618 | 2994 | nfs->priority = htons(fs->priority); |
349adfb2 BP |
2995 | nfs->idle_timeout = htons(fs->idle_timeout); |
2996 | nfs->hard_timeout = htons(fs->hard_timeout); | |
f27f2134 BP |
2997 | nfs->idle_age = htons(fs->idle_age < 0 ? 0 |
2998 | : fs->idle_age < UINT16_MAX ? fs->idle_age + 1 | |
2999 | : UINT16_MAX); | |
3000 | nfs->hard_age = htons(fs->hard_age < 0 ? 0 | |
3001 | : fs->hard_age < UINT16_MAX ? fs->hard_age + 1 | |
3002 | : UINT16_MAX); | |
1a59dc2c | 3003 | nfs->match_len = htons(match_len); |
349adfb2 BP |
3004 | nfs->cookie = fs->cookie; |
3005 | nfs->packet_count = htonll(fs->packet_count); | |
3006 | nfs->byte_count = htonll(fs->byte_count); | |
349adfb2 | 3007 | } else { |
428b2edd | 3008 | OVS_NOT_REACHED(); |
349adfb2 | 3009 | } |
f25d0cf3 | 3010 | |
982697a4 | 3011 | ofpmp_postappend(replies, start_ofs); |
349adfb2 BP |
3012 | } |
3013 | ||
76c93b22 | 3014 | /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or |
a814ba0f | 3015 | * NXST_AGGREGATE reply matching 'request', and returns the message. */ |
76c93b22 BP |
3016 | struct ofpbuf * |
3017 | ofputil_encode_aggregate_stats_reply( | |
3018 | const struct ofputil_aggregate_stats *stats, | |
982697a4 | 3019 | const struct ofp_header *request) |
76c93b22 | 3020 | { |
a814ba0f BP |
3021 | struct ofp_aggregate_stats_reply *asr; |
3022 | uint64_t packet_count; | |
3023 | uint64_t byte_count; | |
76c93b22 | 3024 | struct ofpbuf *msg; |
982697a4 | 3025 | enum ofpraw raw; |
76c93b22 | 3026 | |
982697a4 | 3027 | ofpraw_decode(&raw, request); |
617da9cd | 3028 | if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) { |
a814ba0f BP |
3029 | packet_count = unknown_to_zero(stats->packet_count); |
3030 | byte_count = unknown_to_zero(stats->byte_count); | |
76c93b22 | 3031 | } else { |
a814ba0f BP |
3032 | packet_count = stats->packet_count; |
3033 | byte_count = stats->byte_count; | |
76c93b22 BP |
3034 | } |
3035 | ||
a814ba0f BP |
3036 | msg = ofpraw_alloc_stats_reply(request, 0); |
3037 | asr = ofpbuf_put_zeros(msg, sizeof *asr); | |
3038 | put_32aligned_be64(&asr->packet_count, htonll(packet_count)); | |
3039 | put_32aligned_be64(&asr->byte_count, htonll(byte_count)); | |
3040 | asr->flow_count = htonl(stats->flow_count); | |
3041 | ||
76c93b22 BP |
3042 | return msg; |
3043 | } | |
3044 | ||
982697a4 BP |
3045 | enum ofperr |
3046 | ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats, | |
3047 | const struct ofp_header *reply) | |
3048 | { | |
a814ba0f | 3049 | struct ofp_aggregate_stats_reply *asr; |
982697a4 | 3050 | struct ofpbuf msg; |
982697a4 BP |
3051 | |
3052 | ofpbuf_use_const(&msg, reply, ntohs(reply->length)); | |
a814ba0f BP |
3053 | ofpraw_pull_assert(&msg); |
3054 | ||
6b8c377a | 3055 | asr = ofpbuf_l3(&msg); |
a814ba0f BP |
3056 | stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count)); |
3057 | stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count)); | |
3058 | stats->flow_count = ntohl(asr->flow_count); | |
982697a4 BP |
3059 | |
3060 | return 0; | |
3061 | } | |
3062 | ||
b78f6b77 BP |
3063 | /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an |
3064 | * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise | |
3065 | * an OpenFlow error code. */ | |
90bf1e07 | 3066 | enum ofperr |
9b045a0c | 3067 | ofputil_decode_flow_removed(struct ofputil_flow_removed *fr, |
b78f6b77 | 3068 | const struct ofp_header *oh) |
9b045a0c | 3069 | { |
982697a4 BP |
3070 | enum ofpraw raw; |
3071 | struct ofpbuf b; | |
9b045a0c | 3072 | |
982697a4 BP |
3073 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
3074 | raw = ofpraw_pull_assert(&b); | |
eefbf181 SH |
3075 | if (raw == OFPRAW_OFPT11_FLOW_REMOVED) { |
3076 | const struct ofp12_flow_removed *ofr; | |
3077 | enum ofperr error; | |
3078 | ||
3079 | ofr = ofpbuf_pull(&b, sizeof *ofr); | |
3080 | ||
81a76618 | 3081 | error = ofputil_pull_ofp11_match(&b, &fr->match, NULL); |
eefbf181 SH |
3082 | if (error) { |
3083 | return error; | |
3084 | } | |
3085 | ||
81a76618 | 3086 | fr->priority = ntohs(ofr->priority); |
eefbf181 SH |
3087 | fr->cookie = ofr->cookie; |
3088 | fr->reason = ofr->reason; | |
95216219 | 3089 | fr->table_id = ofr->table_id; |
eefbf181 SH |
3090 | fr->duration_sec = ntohl(ofr->duration_sec); |
3091 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
3092 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
3093 | fr->hard_timeout = ntohs(ofr->hard_timeout); | |
3094 | fr->packet_count = ntohll(ofr->packet_count); | |
3095 | fr->byte_count = ntohll(ofr->byte_count); | |
3096 | } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) { | |
31a9e63f | 3097 | const struct ofp10_flow_removed *ofr; |
9b045a0c | 3098 | |
982697a4 BP |
3099 | ofr = ofpbuf_pull(&b, sizeof *ofr); |
3100 | ||
81a76618 BP |
3101 | ofputil_match_from_ofp10_match(&ofr->match, &fr->match); |
3102 | fr->priority = ntohs(ofr->priority); | |
9b045a0c BP |
3103 | fr->cookie = ofr->cookie; |
3104 | fr->reason = ofr->reason; | |
95216219 | 3105 | fr->table_id = 255; |
9b045a0c BP |
3106 | fr->duration_sec = ntohl(ofr->duration_sec); |
3107 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
3108 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
fa2bad0f | 3109 | fr->hard_timeout = 0; |
9b045a0c BP |
3110 | fr->packet_count = ntohll(ofr->packet_count); |
3111 | fr->byte_count = ntohll(ofr->byte_count); | |
982697a4 | 3112 | } else if (raw == OFPRAW_NXT_FLOW_REMOVED) { |
9b045a0c | 3113 | struct nx_flow_removed *nfr; |
c2725b60 | 3114 | enum ofperr error; |
9b045a0c | 3115 | |
9b045a0c | 3116 | nfr = ofpbuf_pull(&b, sizeof *nfr); |
81a76618 BP |
3117 | error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match, |
3118 | NULL, NULL); | |
9b045a0c BP |
3119 | if (error) { |
3120 | return error; | |
3121 | } | |
1f317cb5 | 3122 | if (ofpbuf_size(&b)) { |
90bf1e07 | 3123 | return OFPERR_OFPBRC_BAD_LEN; |
9b045a0c BP |
3124 | } |
3125 | ||
81a76618 | 3126 | fr->priority = ntohs(nfr->priority); |
9b045a0c BP |
3127 | fr->cookie = nfr->cookie; |
3128 | fr->reason = nfr->reason; | |
745bfd5e | 3129 | fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255; |
9b045a0c BP |
3130 | fr->duration_sec = ntohl(nfr->duration_sec); |
3131 | fr->duration_nsec = ntohl(nfr->duration_nsec); | |
3132 | fr->idle_timeout = ntohs(nfr->idle_timeout); | |
fa2bad0f | 3133 | fr->hard_timeout = 0; |
9b045a0c BP |
3134 | fr->packet_count = ntohll(nfr->packet_count); |
3135 | fr->byte_count = ntohll(nfr->byte_count); | |
3136 | } else { | |
428b2edd | 3137 | OVS_NOT_REACHED(); |
9b045a0c BP |
3138 | } |
3139 | ||
3140 | return 0; | |
3141 | } | |
3142 | ||
588cd7b5 | 3143 | /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or |
27527aa0 | 3144 | * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the |
588cd7b5 BP |
3145 | * message. */ |
3146 | struct ofpbuf * | |
3147 | ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr, | |
27527aa0 | 3148 | enum ofputil_protocol protocol) |
588cd7b5 BP |
3149 | { |
3150 | struct ofpbuf *msg; | |
fa42f4f8 SH |
3151 | enum ofp_flow_removed_reason reason = fr->reason; |
3152 | ||
3153 | if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) { | |
3154 | reason = OFPRR_DELETE; | |
3155 | } | |
588cd7b5 | 3156 | |
27527aa0 | 3157 | switch (protocol) { |
75fa58f8 | 3158 | case OFPUTIL_P_OF11_STD: |
2e1ae200 | 3159 | case OFPUTIL_P_OF12_OXM: |
c37c0382 | 3160 | case OFPUTIL_P_OF13_OXM: |
42dccab5 BP |
3161 | case OFPUTIL_P_OF14_OXM: |
3162 | case OFPUTIL_P_OF15_OXM: { | |
83974732 SH |
3163 | struct ofp12_flow_removed *ofr; |
3164 | ||
3165 | msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED, | |
3166 | ofputil_protocol_to_ofp_version(protocol), | |
75fa58f8 BP |
3167 | htonl(0), |
3168 | ofputil_match_typical_len(protocol)); | |
83974732 SH |
3169 | ofr = ofpbuf_put_zeros(msg, sizeof *ofr); |
3170 | ofr->cookie = fr->cookie; | |
81a76618 | 3171 | ofr->priority = htons(fr->priority); |
fa42f4f8 | 3172 | ofr->reason = reason; |
95216219 | 3173 | ofr->table_id = fr->table_id; |
83974732 SH |
3174 | ofr->duration_sec = htonl(fr->duration_sec); |
3175 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
3176 | ofr->idle_timeout = htons(fr->idle_timeout); | |
fa2bad0f | 3177 | ofr->hard_timeout = htons(fr->hard_timeout); |
83974732 SH |
3178 | ofr->packet_count = htonll(fr->packet_count); |
3179 | ofr->byte_count = htonll(fr->byte_count); | |
75fa58f8 | 3180 | ofputil_put_ofp11_match(msg, &fr->match, protocol); |
83974732 SH |
3181 | break; |
3182 | } | |
3183 | ||
85813857 BP |
3184 | case OFPUTIL_P_OF10_STD: |
3185 | case OFPUTIL_P_OF10_STD_TID: { | |
31a9e63f | 3186 | struct ofp10_flow_removed *ofr; |
588cd7b5 | 3187 | |
982697a4 BP |
3188 | msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION, |
3189 | htonl(0), 0); | |
3190 | ofr = ofpbuf_put_zeros(msg, sizeof *ofr); | |
81a76618 | 3191 | ofputil_match_to_ofp10_match(&fr->match, &ofr->match); |
7fb563b9 | 3192 | ofr->cookie = fr->cookie; |
81a76618 | 3193 | ofr->priority = htons(fr->priority); |
fa42f4f8 | 3194 | ofr->reason = reason; |
588cd7b5 BP |
3195 | ofr->duration_sec = htonl(fr->duration_sec); |
3196 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
3197 | ofr->idle_timeout = htons(fr->idle_timeout); | |
5e9d0469 BP |
3198 | ofr->packet_count = htonll(unknown_to_zero(fr->packet_count)); |
3199 | ofr->byte_count = htonll(unknown_to_zero(fr->byte_count)); | |
27527aa0 BP |
3200 | break; |
3201 | } | |
3202 | ||
85813857 BP |
3203 | case OFPUTIL_P_OF10_NXM: |
3204 | case OFPUTIL_P_OF10_NXM_TID: { | |
588cd7b5 BP |
3205 | struct nx_flow_removed *nfr; |
3206 | int match_len; | |
3207 | ||
982697a4 BP |
3208 | msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION, |
3209 | htonl(0), NXM_TYPICAL_LEN); | |
3210 | nfr = ofpbuf_put_zeros(msg, sizeof *nfr); | |
81a76618 | 3211 | match_len = nx_put_match(msg, &fr->match, 0, 0); |
588cd7b5 | 3212 | |
6b8c377a | 3213 | nfr = ofpbuf_l3(msg); |
588cd7b5 | 3214 | nfr->cookie = fr->cookie; |
81a76618 | 3215 | nfr->priority = htons(fr->priority); |
fa42f4f8 | 3216 | nfr->reason = reason; |
745bfd5e | 3217 | nfr->table_id = fr->table_id + 1; |
588cd7b5 BP |
3218 | nfr->duration_sec = htonl(fr->duration_sec); |
3219 | nfr->duration_nsec = htonl(fr->duration_nsec); | |
3220 | nfr->idle_timeout = htons(fr->idle_timeout); | |
3221 | nfr->match_len = htons(match_len); | |
3222 | nfr->packet_count = htonll(fr->packet_count); | |
3223 | nfr->byte_count = htonll(fr->byte_count); | |
27527aa0 BP |
3224 | break; |
3225 | } | |
3226 | ||
3227 | default: | |
428b2edd | 3228 | OVS_NOT_REACHED(); |
588cd7b5 BP |
3229 | } |
3230 | ||
3231 | return msg; | |
3232 | } | |
3233 | ||
7cfb9651 SH |
3234 | static void |
3235 | ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin, | |
81a76618 | 3236 | struct match *match, struct ofpbuf *b) |
7cfb9651 | 3237 | { |
1f317cb5 PS |
3238 | pin->packet = ofpbuf_data(b); |
3239 | pin->packet_len = ofpbuf_size(b); | |
7cfb9651 | 3240 | |
4e022ec0 | 3241 | pin->fmd.in_port = match->flow.in_port.ofp_port; |
296e07ac | 3242 | pin->fmd.tun_id = match->flow.tunnel.tun_id; |
0ad90c84 JR |
3243 | pin->fmd.tun_src = match->flow.tunnel.ip_src; |
3244 | pin->fmd.tun_dst = match->flow.tunnel.ip_dst; | |
81a76618 BP |
3245 | pin->fmd.metadata = match->flow.metadata; |
3246 | memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs); | |
ac923e91 | 3247 | pin->fmd.pkt_mark = match->flow.pkt_mark; |
7cfb9651 SH |
3248 | } |
3249 | ||
f7cc6bd8 | 3250 | enum ofperr |
65120a8a EJ |
3251 | ofputil_decode_packet_in(struct ofputil_packet_in *pin, |
3252 | const struct ofp_header *oh) | |
3253 | { | |
982697a4 BP |
3254 | enum ofpraw raw; |
3255 | struct ofpbuf b; | |
65120a8a | 3256 | |
65120a8a | 3257 | memset(pin, 0, sizeof *pin); |
d4fa4e79 | 3258 | pin->cookie = OVS_BE64_MAX; |
65120a8a | 3259 | |
982697a4 BP |
3260 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
3261 | raw = ofpraw_pull_assert(&b); | |
2e1ae200 JR |
3262 | if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) { |
3263 | const struct ofp13_packet_in *opi; | |
81a76618 | 3264 | struct match match; |
7cfb9651 | 3265 | int error; |
2e1ae200 JR |
3266 | size_t packet_in_size; |
3267 | ||
3268 | if (raw == OFPRAW_OFPT12_PACKET_IN) { | |
3269 | packet_in_size = sizeof (struct ofp12_packet_in); | |
3270 | } else { | |
3271 | packet_in_size = sizeof (struct ofp13_packet_in); | |
3272 | } | |
7cfb9651 | 3273 | |
2e1ae200 | 3274 | opi = ofpbuf_pull(&b, packet_in_size); |
81a76618 | 3275 | error = oxm_pull_match_loose(&b, &match); |
7cfb9651 SH |
3276 | if (error) { |
3277 | return error; | |
3278 | } | |
3279 | ||
3280 | if (!ofpbuf_try_pull(&b, 2)) { | |
3281 | return OFPERR_OFPBRC_BAD_LEN; | |
3282 | } | |
3283 | ||
2e1ae200 JR |
3284 | pin->reason = opi->pi.reason; |
3285 | pin->table_id = opi->pi.table_id; | |
3286 | pin->buffer_id = ntohl(opi->pi.buffer_id); | |
3287 | pin->total_len = ntohs(opi->pi.total_len); | |
7cfb9651 | 3288 | |
2e1ae200 JR |
3289 | if (raw == OFPRAW_OFPT13_PACKET_IN) { |
3290 | pin->cookie = opi->cookie; | |
3291 | } | |
7cfb9651 | 3292 | |
81a76618 | 3293 | ofputil_decode_packet_in_finish(pin, &match, &b); |
7cfb9651 | 3294 | } else if (raw == OFPRAW_OFPT10_PACKET_IN) { |
31a9e63f | 3295 | const struct ofp10_packet_in *opi; |
982697a4 | 3296 | |
31a9e63f | 3297 | opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data)); |
65120a8a EJ |
3298 | |
3299 | pin->packet = opi->data; | |
1f317cb5 | 3300 | pin->packet_len = ofpbuf_size(&b); |
65120a8a | 3301 | |
4e022ec0 | 3302 | pin->fmd.in_port = u16_to_ofp(ntohs(opi->in_port)); |
65120a8a EJ |
3303 | pin->reason = opi->reason; |
3304 | pin->buffer_id = ntohl(opi->buffer_id); | |
3305 | pin->total_len = ntohs(opi->total_len); | |
4d197ebb BP |
3306 | } else if (raw == OFPRAW_OFPT11_PACKET_IN) { |
3307 | const struct ofp11_packet_in *opi; | |
3308 | enum ofperr error; | |
3309 | ||
3310 | opi = ofpbuf_pull(&b, sizeof *opi); | |
3311 | ||
1f317cb5 PS |
3312 | pin->packet = ofpbuf_data(&b); |
3313 | pin->packet_len = ofpbuf_size(&b); | |
4d197ebb BP |
3314 | |
3315 | pin->buffer_id = ntohl(opi->buffer_id); | |
3316 | error = ofputil_port_from_ofp11(opi->in_port, &pin->fmd.in_port); | |
3317 | if (error) { | |
3318 | return error; | |
3319 | } | |
3320 | pin->total_len = ntohs(opi->total_len); | |
3321 | pin->reason = opi->reason; | |
3322 | pin->table_id = opi->table_id; | |
982697a4 | 3323 | } else if (raw == OFPRAW_NXT_PACKET_IN) { |
73dbf4ab | 3324 | const struct nx_packet_in *npi; |
81a76618 | 3325 | struct match match; |
54834960 EJ |
3326 | int error; |
3327 | ||
54834960 | 3328 | npi = ofpbuf_pull(&b, sizeof *npi); |
81a76618 | 3329 | error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL, |
b5ae8913 | 3330 | NULL); |
54834960 EJ |
3331 | if (error) { |
3332 | return error; | |
3333 | } | |
3334 | ||
3335 | if (!ofpbuf_try_pull(&b, 2)) { | |
90bf1e07 | 3336 | return OFPERR_OFPBRC_BAD_LEN; |
54834960 EJ |
3337 | } |
3338 | ||
54834960 EJ |
3339 | pin->reason = npi->reason; |
3340 | pin->table_id = npi->table_id; | |
3341 | pin->cookie = npi->cookie; | |
3342 | ||
54834960 EJ |
3343 | pin->buffer_id = ntohl(npi->buffer_id); |
3344 | pin->total_len = ntohs(npi->total_len); | |
7cfb9651 | 3345 | |
81a76618 | 3346 | ofputil_decode_packet_in_finish(pin, &match, &b); |
65120a8a | 3347 | } else { |
428b2edd | 3348 | OVS_NOT_REACHED(); |
65120a8a EJ |
3349 | } |
3350 | ||
3351 | return 0; | |
3352 | } | |
3353 | ||
d94240ec | 3354 | static void |
81a76618 BP |
3355 | ofputil_packet_in_to_match(const struct ofputil_packet_in *pin, |
3356 | struct match *match) | |
d94240ec SH |
3357 | { |
3358 | int i; | |
3359 | ||
81a76618 | 3360 | match_init_catchall(match); |
42edbe39 | 3361 | if (pin->fmd.tun_id != htonll(0)) { |
81a76618 | 3362 | match_set_tun_id(match, pin->fmd.tun_id); |
42edbe39 | 3363 | } |
0ad90c84 JR |
3364 | if (pin->fmd.tun_src != htonl(0)) { |
3365 | match_set_tun_src(match, pin->fmd.tun_src); | |
3366 | } | |
3367 | if (pin->fmd.tun_dst != htonl(0)) { | |
3368 | match_set_tun_dst(match, pin->fmd.tun_dst); | |
3369 | } | |
42edbe39 | 3370 | if (pin->fmd.metadata != htonll(0)) { |
81a76618 | 3371 | match_set_metadata(match, pin->fmd.metadata); |
42edbe39 | 3372 | } |
d94240ec SH |
3373 | |
3374 | for (i = 0; i < FLOW_N_REGS; i++) { | |
42edbe39 | 3375 | if (pin->fmd.regs[i]) { |
81a76618 | 3376 | match_set_reg(match, i, pin->fmd.regs[i]); |
42edbe39 | 3377 | } |
d94240ec SH |
3378 | } |
3379 | ||
ac923e91 JG |
3380 | if (pin->fmd.pkt_mark != 0) { |
3381 | match_set_pkt_mark(match, pin->fmd.pkt_mark); | |
3382 | } | |
3383 | ||
81a76618 | 3384 | match_set_in_port(match, pin->fmd.in_port); |
d94240ec SH |
3385 | } |
3386 | ||
0f83fea0 BP |
3387 | static struct ofpbuf * |
3388 | ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin) | |
3389 | { | |
3390 | struct ofp10_packet_in *opi; | |
3391 | struct ofpbuf *packet; | |
3392 | ||
3393 | packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION, | |
3394 | htonl(0), pin->packet_len); | |
3395 | opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data)); | |
3396 | opi->total_len = htons(pin->total_len); | |
3397 | opi->in_port = htons(ofp_to_u16(pin->fmd.in_port)); | |
3398 | opi->reason = pin->reason; | |
3399 | opi->buffer_id = htonl(pin->buffer_id); | |
3400 | ||
3401 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3402 | ||
3403 | return packet; | |
3404 | } | |
3405 | ||
3406 | static struct ofpbuf * | |
3407 | ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin) | |
3408 | { | |
3409 | struct nx_packet_in *npi; | |
3410 | struct ofpbuf *packet; | |
3411 | struct match match; | |
3412 | size_t match_len; | |
3413 | ||
3414 | ofputil_packet_in_to_match(pin, &match); | |
3415 | ||
3416 | /* The final argument is just an estimate of the space required. */ | |
3417 | packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION, | |
3418 | htonl(0), (sizeof(struct flow_metadata) * 2 | |
3419 | + 2 + pin->packet_len)); | |
3420 | ofpbuf_put_zeros(packet, sizeof *npi); | |
3421 | match_len = nx_put_match(packet, &match, 0, 0); | |
3422 | ofpbuf_put_zeros(packet, 2); | |
3423 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3424 | ||
6b8c377a | 3425 | npi = ofpbuf_l3(packet); |
0f83fea0 BP |
3426 | npi->buffer_id = htonl(pin->buffer_id); |
3427 | npi->total_len = htons(pin->total_len); | |
3428 | npi->reason = pin->reason; | |
3429 | npi->table_id = pin->table_id; | |
3430 | npi->cookie = pin->cookie; | |
3431 | npi->match_len = htons(match_len); | |
3432 | ||
3433 | return packet; | |
3434 | } | |
3435 | ||
4d197ebb BP |
3436 | static struct ofpbuf * |
3437 | ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin) | |
3438 | { | |
3439 | struct ofp11_packet_in *opi; | |
3440 | struct ofpbuf *packet; | |
3441 | ||
3442 | packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION, | |
3443 | htonl(0), pin->packet_len); | |
3444 | opi = ofpbuf_put_zeros(packet, sizeof *opi); | |
3445 | opi->buffer_id = htonl(pin->buffer_id); | |
3446 | opi->in_port = ofputil_port_to_ofp11(pin->fmd.in_port); | |
3447 | opi->in_phy_port = opi->in_port; | |
3448 | opi->total_len = htons(pin->total_len); | |
3449 | opi->reason = pin->reason; | |
3450 | opi->table_id = pin->table_id; | |
3451 | ||
3452 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3453 | ||
3454 | return packet; | |
3455 | } | |
3456 | ||
0f83fea0 BP |
3457 | static struct ofpbuf * |
3458 | ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin, | |
3459 | enum ofputil_protocol protocol) | |
3460 | { | |
3461 | struct ofp13_packet_in *opi; | |
3462 | struct match match; | |
3463 | enum ofpraw packet_in_raw; | |
3464 | enum ofp_version packet_in_version; | |
3465 | size_t packet_in_size; | |
3466 | struct ofpbuf *packet; | |
3467 | ||
3468 | if (protocol == OFPUTIL_P_OF12_OXM) { | |
3469 | packet_in_raw = OFPRAW_OFPT12_PACKET_IN; | |
3470 | packet_in_version = OFP12_VERSION; | |
3471 | packet_in_size = sizeof (struct ofp12_packet_in); | |
3472 | } else { | |
3473 | packet_in_raw = OFPRAW_OFPT13_PACKET_IN; | |
3474 | packet_in_version = OFP13_VERSION; | |
3475 | packet_in_size = sizeof (struct ofp13_packet_in); | |
3476 | } | |
3477 | ||
3478 | ofputil_packet_in_to_match(pin, &match); | |
3479 | ||
3480 | /* The final argument is just an estimate of the space required. */ | |
3481 | packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version, | |
3482 | htonl(0), (sizeof(struct flow_metadata) * 2 | |
3483 | + 2 + pin->packet_len)); | |
3484 | ofpbuf_put_zeros(packet, packet_in_size); | |
9d84066c | 3485 | oxm_put_match(packet, &match, ofputil_protocol_to_ofp_version(protocol)); |
0f83fea0 BP |
3486 | ofpbuf_put_zeros(packet, 2); |
3487 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3488 | ||
6b8c377a | 3489 | opi = ofpbuf_l3(packet); |
0f83fea0 BP |
3490 | opi->pi.buffer_id = htonl(pin->buffer_id); |
3491 | opi->pi.total_len = htons(pin->total_len); | |
3492 | opi->pi.reason = pin->reason; | |
3493 | opi->pi.table_id = pin->table_id; | |
3494 | if (protocol == OFPUTIL_P_OF13_OXM) { | |
3495 | opi->cookie = pin->cookie; | |
3496 | } | |
3497 | ||
3498 | return packet; | |
3499 | } | |
3500 | ||
54834960 EJ |
3501 | /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message |
3502 | * in the format specified by 'packet_in_format'. */ | |
ebb57021 | 3503 | struct ofpbuf * |
54834960 | 3504 | ofputil_encode_packet_in(const struct ofputil_packet_in *pin, |
d94240ec | 3505 | enum ofputil_protocol protocol, |
54834960 | 3506 | enum nx_packet_in_format packet_in_format) |
ebb57021 | 3507 | { |
54834960 | 3508 | struct ofpbuf *packet; |
ebb57021 | 3509 | |
0f83fea0 BP |
3510 | switch (protocol) { |
3511 | case OFPUTIL_P_OF10_STD: | |
3512 | case OFPUTIL_P_OF10_STD_TID: | |
3513 | case OFPUTIL_P_OF10_NXM: | |
3514 | case OFPUTIL_P_OF10_NXM_TID: | |
3515 | packet = (packet_in_format == NXPIF_NXM | |
3516 | ? ofputil_encode_nx_packet_in(pin) | |
3517 | : ofputil_encode_ofp10_packet_in(pin)); | |
3518 | break; | |
2e1ae200 | 3519 | |
0f83fea0 | 3520 | case OFPUTIL_P_OF11_STD: |
4d197ebb BP |
3521 | packet = ofputil_encode_ofp11_packet_in(pin); |
3522 | break; | |
d94240ec | 3523 | |
0f83fea0 BP |
3524 | case OFPUTIL_P_OF12_OXM: |
3525 | case OFPUTIL_P_OF13_OXM: | |
c37c0382 | 3526 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 3527 | case OFPUTIL_P_OF15_OXM: |
0f83fea0 BP |
3528 | packet = ofputil_encode_ofp12_packet_in(pin, protocol); |
3529 | break; | |
3530 | ||
3531 | default: | |
428b2edd | 3532 | OVS_NOT_REACHED(); |
54834960 | 3533 | } |
54834960 | 3534 | |
0f83fea0 | 3535 | ofpmsg_update_length(packet); |
54834960 | 3536 | return packet; |
ebb57021 BP |
3537 | } |
3538 | ||
5014a89d BP |
3539 | /* Returns a string form of 'reason'. The return value is either a statically |
3540 | * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'. | |
3541 | * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */ | |
7c1a76a4 | 3542 | const char * |
5014a89d BP |
3543 | ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason, |
3544 | char *reasonbuf, size_t bufsize) | |
7c1a76a4 | 3545 | { |
7c1a76a4 BP |
3546 | switch (reason) { |
3547 | case OFPR_NO_MATCH: | |
3548 | return "no_match"; | |
3549 | case OFPR_ACTION: | |
3550 | return "action"; | |
3551 | case OFPR_INVALID_TTL: | |
3552 | return "invalid_ttl"; | |
3553 | ||
3554 | case OFPR_N_REASONS: | |
3555 | default: | |
5014a89d BP |
3556 | snprintf(reasonbuf, bufsize, "%d", (int) reason); |
3557 | return reasonbuf; | |
7c1a76a4 BP |
3558 | } |
3559 | } | |
3560 | ||
3561 | bool | |
3562 | ofputil_packet_in_reason_from_string(const char *s, | |
3563 | enum ofp_packet_in_reason *reason) | |
3564 | { | |
3565 | int i; | |
3566 | ||
3567 | for (i = 0; i < OFPR_N_REASONS; i++) { | |
5014a89d BP |
3568 | char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE]; |
3569 | const char *reason_s; | |
3570 | ||
3571 | reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf, | |
3572 | sizeof reasonbuf); | |
3573 | if (!strcasecmp(s, reason_s)) { | |
7c1a76a4 BP |
3574 | *reason = i; |
3575 | return true; | |
3576 | } | |
3577 | } | |
3578 | return false; | |
3579 | } | |
3580 | ||
f25d0cf3 BP |
3581 | /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in |
3582 | * 'po'. | |
3583 | * | |
3584 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out | |
3585 | * message's actions. The caller must initialize 'ofpacts' and retains | |
3586 | * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer. | |
3587 | * | |
3588 | * Returns 0 if successful, otherwise an OFPERR_* value. */ | |
c6a93eb7 BP |
3589 | enum ofperr |
3590 | ofputil_decode_packet_out(struct ofputil_packet_out *po, | |
982697a4 | 3591 | const struct ofp_header *oh, |
f25d0cf3 | 3592 | struct ofpbuf *ofpacts) |
c6a93eb7 | 3593 | { |
982697a4 | 3594 | enum ofpraw raw; |
c6a93eb7 BP |
3595 | struct ofpbuf b; |
3596 | ||
982697a4 BP |
3597 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
3598 | raw = ofpraw_pull_assert(&b); | |
982697a4 | 3599 | |
eb5ee596 SH |
3600 | if (raw == OFPRAW_OFPT11_PACKET_OUT) { |
3601 | enum ofperr error; | |
3602 | const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo); | |
3603 | ||
3604 | po->buffer_id = ntohl(opo->buffer_id); | |
3605 | error = ofputil_port_from_ofp11(opo->in_port, &po->in_port); | |
3606 | if (error) { | |
3607 | return error; | |
3608 | } | |
3609 | ||
e3f8f887 JR |
3610 | error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len), |
3611 | oh->version, ofpacts); | |
eb5ee596 SH |
3612 | if (error) { |
3613 | return error; | |
3614 | } | |
eb5ee596 | 3615 | } else if (raw == OFPRAW_OFPT10_PACKET_OUT) { |
8a6bc7cd | 3616 | enum ofperr error; |
31a9e63f | 3617 | const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo); |
8a6bc7cd SH |
3618 | |
3619 | po->buffer_id = ntohl(opo->buffer_id); | |
4e022ec0 | 3620 | po->in_port = u16_to_ofp(ntohs(opo->in_port)); |
8a6bc7cd | 3621 | |
e3f8f887 JR |
3622 | error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len), |
3623 | oh->version, ofpacts); | |
8a6bc7cd SH |
3624 | if (error) { |
3625 | return error; | |
3626 | } | |
3627 | } else { | |
428b2edd | 3628 | OVS_NOT_REACHED(); |
8a6bc7cd SH |
3629 | } |
3630 | ||
4e022ec0 AW |
3631 | if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX) |
3632 | && po->in_port != OFPP_LOCAL | |
751c7785 | 3633 | && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) { |
c6a93eb7 BP |
3634 | VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16, |
3635 | po->in_port); | |
2e1bfcb6 | 3636 | return OFPERR_OFPBRC_BAD_PORT; |
c6a93eb7 BP |
3637 | } |
3638 | ||
1f317cb5 PS |
3639 | po->ofpacts = ofpbuf_data(ofpacts); |
3640 | po->ofpacts_len = ofpbuf_size(ofpacts); | |
c6a93eb7 BP |
3641 | |
3642 | if (po->buffer_id == UINT32_MAX) { | |
1f317cb5 PS |
3643 | po->packet = ofpbuf_data(&b); |
3644 | po->packet_len = ofpbuf_size(&b); | |
c6a93eb7 BP |
3645 | } else { |
3646 | po->packet = NULL; | |
3647 | po->packet_len = 0; | |
3648 | } | |
3649 | ||
3650 | return 0; | |
3651 | } | |
6c038611 BP |
3652 | \f |
3653 | /* ofputil_phy_port */ | |
3654 | ||
3655 | /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */ | |
3656 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ | |
3657 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
3658 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
3659 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
3660 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
3661 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
3662 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
3663 | ||
3664 | /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */ | |
9e1fd49b BP |
3665 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4)); |
3666 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4)); | |
3667 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4)); | |
3668 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4)); | |
3669 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4)); | |
6c038611 | 3670 | |
9e1fd49b BP |
3671 | static enum netdev_features |
3672 | netdev_port_features_from_ofp10(ovs_be32 ofp10_) | |
6c038611 BP |
3673 | { |
3674 | uint32_t ofp10 = ntohl(ofp10_); | |
3675 | return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4); | |
3676 | } | |
3677 | ||
9e1fd49b BP |
3678 | static ovs_be32 |
3679 | netdev_port_features_to_ofp10(enum netdev_features features) | |
6c038611 BP |
3680 | { |
3681 | return htonl((features & 0x7f) | ((features & 0xf800) >> 4)); | |
3682 | } | |
c6a93eb7 | 3683 | |
9e1fd49b BP |
3684 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ |
3685 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
3686 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
3687 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
3688 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
3689 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
3690 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
3691 | BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */ | |
3692 | BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */ | |
3693 | BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */ | |
3694 | BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */ | |
3695 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */ | |
3696 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */ | |
3697 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */ | |
3698 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */ | |
3699 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */ | |
3700 | ||
3701 | static enum netdev_features | |
3702 | netdev_port_features_from_ofp11(ovs_be32 ofp11) | |
3703 | { | |
3704 | return ntohl(ofp11) & 0xffff; | |
3705 | } | |
3706 | ||
3707 | static ovs_be32 | |
3708 | netdev_port_features_to_ofp11(enum netdev_features features) | |
3709 | { | |
3710 | return htonl(features & 0xffff); | |
3711 | } | |
3712 | ||
3713 | static enum ofperr | |
3714 | ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp, | |
3715 | const struct ofp10_phy_port *opp) | |
3716 | { | |
4e022ec0 | 3717 | pp->port_no = u16_to_ofp(ntohs(opp->port_no)); |
9e1fd49b BP |
3718 | memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN); |
3719 | ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN); | |
3720 | ||
3721 | pp->config = ntohl(opp->config) & OFPPC10_ALL; | |
3722 | pp->state = ntohl(opp->state) & OFPPS10_ALL; | |
3723 | ||
3724 | pp->curr = netdev_port_features_from_ofp10(opp->curr); | |
3725 | pp->advertised = netdev_port_features_from_ofp10(opp->advertised); | |
3726 | pp->supported = netdev_port_features_from_ofp10(opp->supported); | |
3727 | pp->peer = netdev_port_features_from_ofp10(opp->peer); | |
3728 | ||
d02a5f8e BP |
3729 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000; |
3730 | pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000; | |
9e1fd49b BP |
3731 | |
3732 | return 0; | |
3733 | } | |
3734 | ||
3735 | static enum ofperr | |
3736 | ofputil_decode_ofp11_port(struct ofputil_phy_port *pp, | |
3737 | const struct ofp11_port *op) | |
3738 | { | |
3739 | enum ofperr error; | |
3740 | ||
9e1fd49b BP |
3741 | error = ofputil_port_from_ofp11(op->port_no, &pp->port_no); |
3742 | if (error) { | |
3743 | return error; | |
3744 | } | |
3745 | memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN); | |
3746 | ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN); | |
3747 | ||
3748 | pp->config = ntohl(op->config) & OFPPC11_ALL; | |
646f2b37 | 3749 | pp->state = ntohl(op->state) & OFPPS11_ALL; |
9e1fd49b BP |
3750 | |
3751 | pp->curr = netdev_port_features_from_ofp11(op->curr); | |
3752 | pp->advertised = netdev_port_features_from_ofp11(op->advertised); | |
3753 | pp->supported = netdev_port_features_from_ofp11(op->supported); | |
3754 | pp->peer = netdev_port_features_from_ofp11(op->peer); | |
3755 | ||
3756 | pp->curr_speed = ntohl(op->curr_speed); | |
3757 | pp->max_speed = ntohl(op->max_speed); | |
3758 | ||
3759 | return 0; | |
3760 | } | |
3761 | ||
8c3cc785 BP |
3762 | static enum ofperr |
3763 | parse_ofp14_port_ethernet_property(const struct ofpbuf *payload, | |
3764 | struct ofputil_phy_port *pp) | |
3765 | { | |
3766 | struct ofp14_port_desc_prop_ethernet *eth = ofpbuf_data(payload); | |
3767 | ||
3768 | if (ofpbuf_size(payload) != sizeof *eth) { | |
3769 | return OFPERR_OFPBPC_BAD_LEN; | |
3770 | } | |
3771 | ||
3772 | pp->curr = netdev_port_features_from_ofp11(eth->curr); | |
3773 | pp->advertised = netdev_port_features_from_ofp11(eth->advertised); | |
3774 | pp->supported = netdev_port_features_from_ofp11(eth->supported); | |
3775 | pp->peer = netdev_port_features_from_ofp11(eth->peer); | |
3776 | ||
3777 | pp->curr_speed = ntohl(eth->curr_speed); | |
3778 | pp->max_speed = ntohl(eth->max_speed); | |
3779 | ||
3780 | return 0; | |
3781 | } | |
3782 | ||
3783 | static enum ofperr | |
3784 | ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg) | |
3785 | { | |
3786 | struct ofpbuf properties; | |
3787 | struct ofp14_port *op; | |
3788 | enum ofperr error; | |
3789 | size_t len; | |
3790 | ||
3791 | op = ofpbuf_try_pull(msg, sizeof *op); | |
3792 | if (!op) { | |
3793 | return OFPERR_OFPBRC_BAD_LEN; | |
3794 | } | |
3795 | ||
3796 | len = ntohs(op->length); | |
3797 | if (len < sizeof *op || len - sizeof *op > ofpbuf_size(msg)) { | |
3798 | return OFPERR_OFPBRC_BAD_LEN; | |
3799 | } | |
3800 | len -= sizeof *op; | |
3801 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len); | |
3802 | ||
3803 | error = ofputil_port_from_ofp11(op->port_no, &pp->port_no); | |
3804 | if (error) { | |
3805 | return error; | |
3806 | } | |
3807 | memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN); | |
3808 | ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN); | |
3809 | ||
3810 | pp->config = ntohl(op->config) & OFPPC11_ALL; | |
3811 | pp->state = ntohl(op->state) & OFPPS11_ALL; | |
3812 | ||
3813 | while (ofpbuf_size(&properties) > 0) { | |
3814 | struct ofpbuf payload; | |
3815 | enum ofperr error; | |
3816 | uint16_t type; | |
3817 | ||
3818 | error = ofputil_pull_property(&properties, &payload, &type); | |
3819 | if (error) { | |
3820 | return error; | |
3821 | } | |
3822 | ||
3823 | switch (type) { | |
3824 | case OFPPDPT14_ETHERNET: | |
3825 | error = parse_ofp14_port_ethernet_property(&payload, pp); | |
3826 | break; | |
3827 | ||
3828 | default: | |
3829 | log_property(true, "unknown port property %"PRIu16, type); | |
3830 | error = 0; | |
3831 | break; | |
3832 | } | |
3833 | ||
3834 | if (error) { | |
3835 | return error; | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | return 0; | |
3840 | } | |
3841 | ||
9e1fd49b BP |
3842 | static void |
3843 | ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp, | |
3844 | struct ofp10_phy_port *opp) | |
3845 | { | |
3846 | memset(opp, 0, sizeof *opp); | |
3847 | ||
4e022ec0 | 3848 | opp->port_no = htons(ofp_to_u16(pp->port_no)); |
9e1fd49b BP |
3849 | memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN); |
3850 | ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
3851 | ||
3852 | opp->config = htonl(pp->config & OFPPC10_ALL); | |
3853 | opp->state = htonl(pp->state & OFPPS10_ALL); | |
3854 | ||
3855 | opp->curr = netdev_port_features_to_ofp10(pp->curr); | |
3856 | opp->advertised = netdev_port_features_to_ofp10(pp->advertised); | |
3857 | opp->supported = netdev_port_features_to_ofp10(pp->supported); | |
3858 | opp->peer = netdev_port_features_to_ofp10(pp->peer); | |
3859 | } | |
3860 | ||
3861 | static void | |
3862 | ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp, | |
3863 | struct ofp11_port *op) | |
3864 | { | |
3865 | memset(op, 0, sizeof *op); | |
3866 | ||
3867 | op->port_no = ofputil_port_to_ofp11(pp->port_no); | |
3868 | memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
3869 | ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
3870 | ||
3871 | op->config = htonl(pp->config & OFPPC11_ALL); | |
3872 | op->state = htonl(pp->state & OFPPS11_ALL); | |
3873 | ||
3874 | op->curr = netdev_port_features_to_ofp11(pp->curr); | |
3875 | op->advertised = netdev_port_features_to_ofp11(pp->advertised); | |
3876 | op->supported = netdev_port_features_to_ofp11(pp->supported); | |
3877 | op->peer = netdev_port_features_to_ofp11(pp->peer); | |
3878 | ||
3879 | op->curr_speed = htonl(pp->curr_speed); | |
3880 | op->max_speed = htonl(pp->max_speed); | |
3881 | } | |
3882 | ||
8c3cc785 BP |
3883 | static void |
3884 | ofputil_put_ofp14_port(const struct ofputil_phy_port *pp, | |
3885 | struct ofpbuf *b) | |
3886 | { | |
3887 | struct ofp14_port *op; | |
3888 | struct ofp14_port_desc_prop_ethernet *eth; | |
3889 | ||
3890 | ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth); | |
3891 | ||
3892 | op = ofpbuf_put_zeros(b, sizeof *op); | |
3893 | op->port_no = ofputil_port_to_ofp11(pp->port_no); | |
3894 | op->length = htons(sizeof *op + sizeof *eth); | |
3895 | memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
3896 | ovs_strlcpy(op->name, pp->name, sizeof op->name); | |
3897 | op->config = htonl(pp->config & OFPPC11_ALL); | |
3898 | op->state = htonl(pp->state & OFPPS11_ALL); | |
3899 | ||
3900 | eth = ofpbuf_put_zeros(b, sizeof *eth); | |
3901 | eth->type = htons(OFPPDPT14_ETHERNET); | |
3902 | eth->length = htons(sizeof *eth); | |
3903 | eth->curr = netdev_port_features_to_ofp11(pp->curr); | |
3904 | eth->advertised = netdev_port_features_to_ofp11(pp->advertised); | |
3905 | eth->supported = netdev_port_features_to_ofp11(pp->supported); | |
3906 | eth->peer = netdev_port_features_to_ofp11(pp->peer); | |
3907 | eth->curr_speed = htonl(pp->curr_speed); | |
3908 | eth->max_speed = htonl(pp->max_speed); | |
3909 | } | |
3910 | ||
9e1fd49b | 3911 | static void |
2e3fa633 SH |
3912 | ofputil_put_phy_port(enum ofp_version ofp_version, |
3913 | const struct ofputil_phy_port *pp, struct ofpbuf *b) | |
9e1fd49b | 3914 | { |
2e3fa633 SH |
3915 | switch (ofp_version) { |
3916 | case OFP10_VERSION: { | |
6b0f20ac BP |
3917 | struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp); |
3918 | ofputil_encode_ofp10_phy_port(pp, opp); | |
2e3fa633 SH |
3919 | break; |
3920 | } | |
3921 | ||
3922 | case OFP11_VERSION: | |
2e1ae200 JR |
3923 | case OFP12_VERSION: |
3924 | case OFP13_VERSION: { | |
6b0f20ac BP |
3925 | struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op); |
3926 | ofputil_encode_ofp11_port(pp, op); | |
2e3fa633 SH |
3927 | break; |
3928 | } | |
3929 | ||
c37c0382 | 3930 | case OFP14_VERSION: |
42dccab5 | 3931 | case OFP15_VERSION: |
8c3cc785 | 3932 | ofputil_put_ofp14_port(pp, b); |
c37c0382 AC |
3933 | break; |
3934 | ||
2e3fa633 | 3935 | default: |
428b2edd | 3936 | OVS_NOT_REACHED(); |
9e1fd49b BP |
3937 | } |
3938 | } | |
2be393ed | 3939 | |
70ae4f93 BP |
3940 | enum ofperr |
3941 | ofputil_decode_port_desc_stats_request(const struct ofp_header *request, | |
3942 | ofp_port_t *port) | |
3943 | { | |
3944 | struct ofpbuf b; | |
3945 | enum ofpraw raw; | |
3946 | ||
3947 | ofpbuf_use_const(&b, request, ntohs(request->length)); | |
3948 | raw = ofpraw_pull_assert(&b); | |
3949 | if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) { | |
3950 | *port = OFPP_ANY; | |
3951 | return 0; | |
3952 | } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) { | |
3953 | ovs_be32 *ofp11_port; | |
3954 | ||
3955 | ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port); | |
3956 | return ofputil_port_from_ofp11(*ofp11_port, port); | |
3957 | } else { | |
3958 | OVS_NOT_REACHED(); | |
3959 | } | |
3960 | } | |
3961 | ||
3962 | struct ofpbuf * | |
3963 | ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version, | |
3964 | ofp_port_t port) | |
3965 | { | |
3966 | struct ofpbuf *request; | |
3967 | ovs_be32 ofp11_port; | |
3968 | ||
3969 | switch (ofp_version) { | |
3970 | case OFP10_VERSION: | |
3971 | case OFP11_VERSION: | |
3972 | case OFP12_VERSION: | |
3973 | case OFP13_VERSION: | |
3974 | case OFP14_VERSION: | |
3975 | request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST, | |
3976 | ofp_version, 0); | |
3977 | break; | |
3978 | ||
3979 | case OFP15_VERSION: | |
3980 | request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST, | |
3981 | ofp_version, 0); | |
3982 | ofp11_port = ofputil_port_to_ofp11(port); | |
3983 | ofpbuf_put(request, &ofp11_port, sizeof ofp11_port); | |
3984 | break; | |
3985 | ||
3986 | default: | |
3987 | OVS_NOT_REACHED(); | |
3988 | } | |
3989 | ||
3990 | return request; | |
3991 | } | |
3992 | ||
2be393ed | 3993 | void |
e28ac5cf | 3994 | ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp, |
2be393ed JP |
3995 | struct list *replies) |
3996 | { | |
6b0f20ac BP |
3997 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); |
3998 | size_t start_ofs = ofpbuf_size(reply); | |
c37c0382 | 3999 | |
6b0f20ac BP |
4000 | ofputil_put_phy_port(ofpmp_version(replies), pp, reply); |
4001 | ofpmp_postappend(replies, start_ofs); | |
2be393ed | 4002 | } |
9e1fd49b BP |
4003 | \f |
4004 | /* ofputil_switch_features */ | |
4005 | ||
4006 | #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \ | |
60202987 | 4007 | OFPC_IP_REASM | OFPC_QUEUE_STATS) |
9e1fd49b BP |
4008 | BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS); |
4009 | BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS); | |
4010 | BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS); | |
4011 | BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM); | |
4012 | BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS); | |
4013 | BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP); | |
4014 | ||
4015 | struct ofputil_action_bit_translation { | |
4016 | enum ofputil_action_bitmap ofputil_bit; | |
4017 | int of_bit; | |
4018 | }; | |
4019 | ||
4020 | static const struct ofputil_action_bit_translation of10_action_bits[] = { | |
4021 | { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT }, | |
4022 | { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID }, | |
4023 | { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP }, | |
4024 | { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN }, | |
4025 | { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC }, | |
4026 | { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST }, | |
4027 | { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC }, | |
4028 | { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST }, | |
4029 | { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS }, | |
4030 | { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC }, | |
4031 | { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST }, | |
4032 | { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE }, | |
4033 | { 0, 0 }, | |
4034 | }; | |
4035 | ||
9e1fd49b BP |
4036 | static enum ofputil_action_bitmap |
4037 | decode_action_bits(ovs_be32 of_actions, | |
4038 | const struct ofputil_action_bit_translation *x) | |
4039 | { | |
4040 | enum ofputil_action_bitmap ofputil_actions; | |
4041 | ||
4042 | ofputil_actions = 0; | |
4043 | for (; x->ofputil_bit; x++) { | |
4044 | if (of_actions & htonl(1u << x->of_bit)) { | |
4045 | ofputil_actions |= x->ofputil_bit; | |
4046 | } | |
4047 | } | |
4048 | return ofputil_actions; | |
4049 | } | |
4050 | ||
60202987 SH |
4051 | static uint32_t |
4052 | ofputil_capabilities_mask(enum ofp_version ofp_version) | |
4053 | { | |
4054 | /* Handle capabilities whose bit is unique for all Open Flow versions */ | |
4055 | switch (ofp_version) { | |
4056 | case OFP10_VERSION: | |
4057 | case OFP11_VERSION: | |
4058 | return OFPC_COMMON | OFPC_ARP_MATCH_IP; | |
4059 | case OFP12_VERSION: | |
2e1ae200 | 4060 | case OFP13_VERSION: |
c37c0382 | 4061 | case OFP14_VERSION: |
42dccab5 | 4062 | case OFP15_VERSION: |
18cc69d9 | 4063 | return OFPC_COMMON | OFPC12_PORT_BLOCKED; |
60202987 SH |
4064 | default: |
4065 | /* Caller needs to check osf->header.version itself */ | |
4066 | return 0; | |
4067 | } | |
4068 | } | |
4069 | ||
9e1fd49b BP |
4070 | /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an |
4071 | * abstract representation in '*features'. Initializes '*b' to iterate over | |
4072 | * the OpenFlow port structures following 'osf' with later calls to | |
2be393ed | 4073 | * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an |
9e1fd49b BP |
4074 | * OFPERR_* value. */ |
4075 | enum ofperr | |
982697a4 | 4076 | ofputil_decode_switch_features(const struct ofp_header *oh, |
9e1fd49b BP |
4077 | struct ofputil_switch_features *features, |
4078 | struct ofpbuf *b) | |
4079 | { | |
982697a4 BP |
4080 | const struct ofp_switch_features *osf; |
4081 | enum ofpraw raw; | |
4082 | ||
4083 | ofpbuf_use_const(b, oh, ntohs(oh->length)); | |
4084 | raw = ofpraw_pull_assert(b); | |
9e1fd49b | 4085 | |
982697a4 | 4086 | osf = ofpbuf_pull(b, sizeof *osf); |
9e1fd49b BP |
4087 | features->datapath_id = ntohll(osf->datapath_id); |
4088 | features->n_buffers = ntohl(osf->n_buffers); | |
4089 | features->n_tables = osf->n_tables; | |
2e1ae200 | 4090 | features->auxiliary_id = 0; |
9e1fd49b | 4091 | |
60202987 SH |
4092 | features->capabilities = ntohl(osf->capabilities) & |
4093 | ofputil_capabilities_mask(oh->version); | |
9e1fd49b | 4094 | |
982697a4 | 4095 | if (raw == OFPRAW_OFPT10_FEATURES_REPLY) { |
9e1fd49b BP |
4096 | if (osf->capabilities & htonl(OFPC10_STP)) { |
4097 | features->capabilities |= OFPUTIL_C_STP; | |
4098 | } | |
4099 | features->actions = decode_action_bits(osf->actions, of10_action_bits); | |
2e1ae200 JR |
4100 | } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY |
4101 | || raw == OFPRAW_OFPT13_FEATURES_REPLY) { | |
9e1fd49b BP |
4102 | if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) { |
4103 | features->capabilities |= OFPUTIL_C_GROUP_STATS; | |
4104 | } | |
34b28fc7 | 4105 | features->actions = 0; |
2e1ae200 JR |
4106 | if (raw == OFPRAW_OFPT13_FEATURES_REPLY) { |
4107 | features->auxiliary_id = osf->auxiliary_id; | |
4108 | } | |
9e1fd49b BP |
4109 | } else { |
4110 | return OFPERR_OFPBRC_BAD_VERSION; | |
4111 | } | |
4112 | ||
4113 | return 0; | |
4114 | } | |
4115 | ||
fca6d553 BP |
4116 | /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the |
4117 | * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and | |
4118 | * later, an OFPT_FEATURES_REPLY does not list ports at all. | |
4119 | * | |
4120 | * Given a buffer 'b' that contains a Features Reply message, this message | |
4121 | * checks if it contains a complete list of the switch's ports. Returns true, | |
4122 | * if so. Returns false if the list is missing (OF1.3+) or incomplete | |
4123 | * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the | |
4124 | * message. | |
4125 | * | |
4126 | * When this function returns false, the caller should send an OFPST_PORT_DESC | |
4127 | * stats request to get the ports. */ | |
347b7ac4 | 4128 | bool |
fca6d553 | 4129 | ofputil_switch_features_has_ports(struct ofpbuf *b) |
347b7ac4 | 4130 | { |
1f317cb5 | 4131 | struct ofp_header *oh = ofpbuf_data(b); |
13e1aff8 | 4132 | size_t phy_port_size; |
347b7ac4 | 4133 | |
fca6d553 | 4134 | if (oh->version >= OFP13_VERSION) { |
13e1aff8 | 4135 | /* OpenFlow 1.3+ never has ports in the feature reply. */ |
fca6d553 | 4136 | return false; |
13e1aff8 BP |
4137 | } |
4138 | ||
4139 | phy_port_size = (oh->version == OFP10_VERSION | |
4140 | ? sizeof(struct ofp10_phy_port) | |
4141 | : sizeof(struct ofp11_port)); | |
4142 | if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) { | |
4143 | /* There's room for additional ports in the feature reply. | |
4144 | * Assume that the list is complete. */ | |
347b7ac4 JP |
4145 | return true; |
4146 | } | |
13e1aff8 BP |
4147 | |
4148 | /* The feature reply has no room for more ports. Probably the list is | |
4149 | * truncated. Drop the ports and tell the caller to retrieve them with | |
4150 | * OFPST_PORT_DESC. */ | |
4151 | ofpbuf_set_size(b, sizeof *oh + sizeof(struct ofp_switch_features)); | |
4152 | ofpmsg_update_length(b); | |
4153 | return false; | |
347b7ac4 JP |
4154 | } |
4155 | ||
9e1fd49b BP |
4156 | static ovs_be32 |
4157 | encode_action_bits(enum ofputil_action_bitmap ofputil_actions, | |
4158 | const struct ofputil_action_bit_translation *x) | |
4159 | { | |
4160 | uint32_t of_actions; | |
4161 | ||
4162 | of_actions = 0; | |
4163 | for (; x->ofputil_bit; x++) { | |
4164 | if (ofputil_actions & x->ofputil_bit) { | |
4165 | of_actions |= 1 << x->of_bit; | |
4166 | } | |
4167 | } | |
4168 | return htonl(of_actions); | |
4169 | } | |
4170 | ||
4171 | /* Returns a buffer owned by the caller that encodes 'features' in the format | |
4172 | * required by 'protocol' with the given 'xid'. The caller should append port | |
4173 | * information to the buffer with subsequent calls to | |
4174 | * ofputil_put_switch_features_port(). */ | |
4175 | struct ofpbuf * | |
4176 | ofputil_encode_switch_features(const struct ofputil_switch_features *features, | |
4177 | enum ofputil_protocol protocol, ovs_be32 xid) | |
4178 | { | |
4179 | struct ofp_switch_features *osf; | |
4180 | struct ofpbuf *b; | |
2e3fa633 SH |
4181 | enum ofp_version version; |
4182 | enum ofpraw raw; | |
982697a4 BP |
4183 | |
4184 | version = ofputil_protocol_to_ofp_version(protocol); | |
2e3fa633 SH |
4185 | switch (version) { |
4186 | case OFP10_VERSION: | |
4187 | raw = OFPRAW_OFPT10_FEATURES_REPLY; | |
4188 | break; | |
4189 | case OFP11_VERSION: | |
4190 | case OFP12_VERSION: | |
4191 | raw = OFPRAW_OFPT11_FEATURES_REPLY; | |
4192 | break; | |
2e1ae200 | 4193 | case OFP13_VERSION: |
c37c0382 | 4194 | case OFP14_VERSION: |
42dccab5 | 4195 | case OFP15_VERSION: |
2e1ae200 JR |
4196 | raw = OFPRAW_OFPT13_FEATURES_REPLY; |
4197 | break; | |
2e3fa633 | 4198 | default: |
428b2edd | 4199 | OVS_NOT_REACHED(); |
2e3fa633 SH |
4200 | } |
4201 | b = ofpraw_alloc_xid(raw, version, xid, 0); | |
982697a4 | 4202 | osf = ofpbuf_put_zeros(b, sizeof *osf); |
9e1fd49b BP |
4203 | osf->datapath_id = htonll(features->datapath_id); |
4204 | osf->n_buffers = htonl(features->n_buffers); | |
4205 | osf->n_tables = features->n_tables; | |
4206 | ||
4207 | osf->capabilities = htonl(features->capabilities & OFPC_COMMON); | |
60202987 SH |
4208 | osf->capabilities = htonl(features->capabilities & |
4209 | ofputil_capabilities_mask(version)); | |
2e3fa633 SH |
4210 | switch (version) { |
4211 | case OFP10_VERSION: | |
9e1fd49b BP |
4212 | if (features->capabilities & OFPUTIL_C_STP) { |
4213 | osf->capabilities |= htonl(OFPC10_STP); | |
4214 | } | |
4215 | osf->actions = encode_action_bits(features->actions, of10_action_bits); | |
2e3fa633 | 4216 | break; |
2e1ae200 | 4217 | case OFP13_VERSION: |
c37c0382 | 4218 | case OFP14_VERSION: |
42dccab5 | 4219 | case OFP15_VERSION: |
2e1ae200 JR |
4220 | osf->auxiliary_id = features->auxiliary_id; |
4221 | /* fall through */ | |
2e3fa633 SH |
4222 | case OFP11_VERSION: |
4223 | case OFP12_VERSION: | |
9e1fd49b BP |
4224 | if (features->capabilities & OFPUTIL_C_GROUP_STATS) { |
4225 | osf->capabilities |= htonl(OFPC11_GROUP_STATS); | |
4226 | } | |
2e3fa633 SH |
4227 | break; |
4228 | default: | |
428b2edd | 4229 | OVS_NOT_REACHED(); |
9e1fd49b BP |
4230 | } |
4231 | ||
4232 | return b; | |
4233 | } | |
4234 | ||
4235 | /* Encodes 'pp' into the format required by the switch_features message already | |
4236 | * in 'b', which should have been returned by ofputil_encode_switch_features(), | |
4237 | * and appends the encoded version to 'b'. */ | |
4238 | void | |
4239 | ofputil_put_switch_features_port(const struct ofputil_phy_port *pp, | |
4240 | struct ofpbuf *b) | |
4241 | { | |
1f317cb5 | 4242 | const struct ofp_header *oh = ofpbuf_data(b); |
9e1fd49b | 4243 | |
e0c91c0c | 4244 | if (oh->version < OFP13_VERSION) { |
6b0f20ac BP |
4245 | /* Try adding a port description to the message, but drop it again if |
4246 | * the buffer overflows. (This possibility for overflow is why | |
4247 | * OpenFlow 1.3+ moved port descriptions into a multipart message.) */ | |
4248 | size_t start_ofs = ofpbuf_size(b); | |
e0c91c0c | 4249 | ofputil_put_phy_port(oh->version, pp, b); |
6b0f20ac BP |
4250 | if (ofpbuf_size(b) > UINT16_MAX) { |
4251 | ofpbuf_set_size(b, start_ofs); | |
4252 | } | |
e0c91c0c | 4253 | } |
9e1fd49b BP |
4254 | } |
4255 | \f | |
4256 | /* ofputil_port_status */ | |
4257 | ||
4258 | /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form | |
4259 | * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
4260 | enum ofperr | |
982697a4 | 4261 | ofputil_decode_port_status(const struct ofp_header *oh, |
9e1fd49b BP |
4262 | struct ofputil_port_status *ps) |
4263 | { | |
982697a4 | 4264 | const struct ofp_port_status *ops; |
9e1fd49b BP |
4265 | struct ofpbuf b; |
4266 | int retval; | |
4267 | ||
982697a4 BP |
4268 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
4269 | ofpraw_pull_assert(&b); | |
4270 | ops = ofpbuf_pull(&b, sizeof *ops); | |
4271 | ||
9e1fd49b BP |
4272 | if (ops->reason != OFPPR_ADD && |
4273 | ops->reason != OFPPR_DELETE && | |
4274 | ops->reason != OFPPR_MODIFY) { | |
4275 | return OFPERR_NXBRC_BAD_REASON; | |
4276 | } | |
4277 | ps->reason = ops->reason; | |
4278 | ||
982697a4 | 4279 | retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc); |
cb22974d | 4280 | ovs_assert(retval != EOF); |
9e1fd49b BP |
4281 | return retval; |
4282 | } | |
4283 | ||
4284 | /* Converts the abstract form of a "port status" message in '*ps' into an | |
4285 | * OpenFlow message suitable for 'protocol', and returns that encoded form in | |
4286 | * a buffer owned by the caller. */ | |
4287 | struct ofpbuf * | |
4288 | ofputil_encode_port_status(const struct ofputil_port_status *ps, | |
4289 | enum ofputil_protocol protocol) | |
4290 | { | |
4291 | struct ofp_port_status *ops; | |
4292 | struct ofpbuf *b; | |
2e3fa633 SH |
4293 | enum ofp_version version; |
4294 | enum ofpraw raw; | |
982697a4 BP |
4295 | |
4296 | version = ofputil_protocol_to_ofp_version(protocol); | |
2e3fa633 SH |
4297 | switch (version) { |
4298 | case OFP10_VERSION: | |
4299 | raw = OFPRAW_OFPT10_PORT_STATUS; | |
4300 | break; | |
4301 | ||
4302 | case OFP11_VERSION: | |
4303 | case OFP12_VERSION: | |
2e1ae200 | 4304 | case OFP13_VERSION: |
2e3fa633 SH |
4305 | raw = OFPRAW_OFPT11_PORT_STATUS; |
4306 | break; | |
4307 | ||
8c3cc785 | 4308 | case OFP14_VERSION: |
42dccab5 | 4309 | case OFP15_VERSION: |
8c3cc785 BP |
4310 | raw = OFPRAW_OFPT14_PORT_STATUS; |
4311 | break; | |
4312 | ||
2e3fa633 | 4313 | default: |
428b2edd | 4314 | OVS_NOT_REACHED(); |
2e3fa633 SH |
4315 | } |
4316 | ||
4317 | b = ofpraw_alloc_xid(raw, version, htonl(0), 0); | |
982697a4 | 4318 | ops = ofpbuf_put_zeros(b, sizeof *ops); |
9e1fd49b | 4319 | ops->reason = ps->reason; |
982697a4 BP |
4320 | ofputil_put_phy_port(version, &ps->desc, b); |
4321 | ofpmsg_update_length(b); | |
9e1fd49b BP |
4322 | return b; |
4323 | } | |
918f2b82 | 4324 | |
9e1fd49b BP |
4325 | /* ofputil_port_mod */ |
4326 | ||
18cc69d9 BP |
4327 | static enum ofperr |
4328 | parse_port_mod_ethernet_property(struct ofpbuf *property, | |
4329 | struct ofputil_port_mod *pm) | |
4330 | { | |
4331 | struct ofp14_port_mod_prop_ethernet *eth = ofpbuf_data(property); | |
4332 | ||
4333 | if (ofpbuf_size(property) != sizeof *eth) { | |
4334 | return OFPERR_OFPBRC_BAD_LEN; | |
4335 | } | |
4336 | ||
4337 | pm->advertise = netdev_port_features_from_ofp11(eth->advertise); | |
4338 | return 0; | |
4339 | } | |
4340 | ||
9e1fd49b BP |
4341 | /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in |
4342 | * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
4343 | enum ofperr | |
4344 | ofputil_decode_port_mod(const struct ofp_header *oh, | |
18cc69d9 | 4345 | struct ofputil_port_mod *pm, bool loose) |
9e1fd49b | 4346 | { |
982697a4 BP |
4347 | enum ofpraw raw; |
4348 | struct ofpbuf b; | |
9e1fd49b | 4349 | |
982697a4 BP |
4350 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
4351 | raw = ofpraw_pull_assert(&b); | |
4352 | ||
4353 | if (raw == OFPRAW_OFPT10_PORT_MOD) { | |
1f317cb5 | 4354 | const struct ofp10_port_mod *opm = ofpbuf_data(&b); |
9e1fd49b | 4355 | |
4e022ec0 | 4356 | pm->port_no = u16_to_ofp(ntohs(opm->port_no)); |
9e1fd49b BP |
4357 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); |
4358 | pm->config = ntohl(opm->config) & OFPPC10_ALL; | |
4359 | pm->mask = ntohl(opm->mask) & OFPPC10_ALL; | |
4360 | pm->advertise = netdev_port_features_from_ofp10(opm->advertise); | |
982697a4 | 4361 | } else if (raw == OFPRAW_OFPT11_PORT_MOD) { |
1f317cb5 | 4362 | const struct ofp11_port_mod *opm = ofpbuf_data(&b); |
9e1fd49b BP |
4363 | enum ofperr error; |
4364 | ||
9e1fd49b BP |
4365 | error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no); |
4366 | if (error) { | |
4367 | return error; | |
4368 | } | |
4369 | ||
4370 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
4371 | pm->config = ntohl(opm->config) & OFPPC11_ALL; | |
4372 | pm->mask = ntohl(opm->mask) & OFPPC11_ALL; | |
4373 | pm->advertise = netdev_port_features_from_ofp11(opm->advertise); | |
18cc69d9 BP |
4374 | } else if (raw == OFPRAW_OFPT14_PORT_MOD) { |
4375 | const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm); | |
4376 | enum ofperr error; | |
4377 | ||
4378 | memset(pm, 0, sizeof *pm); | |
4379 | ||
4380 | error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no); | |
4381 | if (error) { | |
4382 | return error; | |
4383 | } | |
4384 | ||
4385 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
4386 | pm->config = ntohl(opm->config) & OFPPC11_ALL; | |
4387 | pm->mask = ntohl(opm->mask) & OFPPC11_ALL; | |
4388 | ||
4389 | while (ofpbuf_size(&b) > 0) { | |
4390 | struct ofpbuf property; | |
4391 | enum ofperr error; | |
4392 | uint16_t type; | |
4393 | ||
4394 | error = ofputil_pull_property(&b, &property, &type); | |
4395 | if (error) { | |
4396 | return error; | |
4397 | } | |
4398 | ||
4399 | switch (type) { | |
4400 | case OFPPMPT14_ETHERNET: | |
4401 | error = parse_port_mod_ethernet_property(&property, pm); | |
4402 | break; | |
4403 | ||
4404 | default: | |
4405 | log_property(loose, "unknown port_mod property %"PRIu16, type); | |
4406 | if (loose) { | |
4407 | error = 0; | |
4408 | } else if (type == OFPPMPT14_EXPERIMENTER) { | |
4409 | error = OFPERR_OFPBPC_BAD_EXPERIMENTER; | |
4410 | } else { | |
4411 | error = OFPERR_OFPBRC_BAD_TYPE; | |
4412 | } | |
4413 | break; | |
4414 | } | |
4415 | ||
4416 | if (error) { | |
4417 | return error; | |
4418 | } | |
4419 | } | |
9e1fd49b | 4420 | } else { |
982697a4 | 4421 | return OFPERR_OFPBRC_BAD_TYPE; |
9e1fd49b BP |
4422 | } |
4423 | ||
4424 | pm->config &= pm->mask; | |
4425 | return 0; | |
4426 | } | |
4427 | ||
4428 | /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow | |
4429 | * message suitable for 'protocol', and returns that encoded form in a buffer | |
4430 | * owned by the caller. */ | |
4431 | struct ofpbuf * | |
4432 | ofputil_encode_port_mod(const struct ofputil_port_mod *pm, | |
4433 | enum ofputil_protocol protocol) | |
4434 | { | |
2e3fa633 | 4435 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); |
9e1fd49b BP |
4436 | struct ofpbuf *b; |
4437 | ||
2e3fa633 SH |
4438 | switch (ofp_version) { |
4439 | case OFP10_VERSION: { | |
9e1fd49b BP |
4440 | struct ofp10_port_mod *opm; |
4441 | ||
982697a4 BP |
4442 | b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0); |
4443 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
4e022ec0 | 4444 | opm->port_no = htons(ofp_to_u16(pm->port_no)); |
9e1fd49b BP |
4445 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); |
4446 | opm->config = htonl(pm->config & OFPPC10_ALL); | |
4447 | opm->mask = htonl(pm->mask & OFPPC10_ALL); | |
4448 | opm->advertise = netdev_port_features_to_ofp10(pm->advertise); | |
2e3fa633 SH |
4449 | break; |
4450 | } | |
4451 | ||
5fe7c919 | 4452 | case OFP11_VERSION: |
2e1ae200 JR |
4453 | case OFP12_VERSION: |
4454 | case OFP13_VERSION: { | |
9e1fd49b BP |
4455 | struct ofp11_port_mod *opm; |
4456 | ||
982697a4 BP |
4457 | b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0); |
4458 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
026a5179 | 4459 | opm->port_no = ofputil_port_to_ofp11(pm->port_no); |
9e1fd49b BP |
4460 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); |
4461 | opm->config = htonl(pm->config & OFPPC11_ALL); | |
4462 | opm->mask = htonl(pm->mask & OFPPC11_ALL); | |
4463 | opm->advertise = netdev_port_features_to_ofp11(pm->advertise); | |
2e3fa633 SH |
4464 | break; |
4465 | } | |
42dccab5 BP |
4466 | case OFP14_VERSION: |
4467 | case OFP15_VERSION: { | |
18cc69d9 BP |
4468 | struct ofp14_port_mod_prop_ethernet *eth; |
4469 | struct ofp14_port_mod *opm; | |
4470 | ||
4471 | b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth); | |
4472 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
4473 | opm->port_no = ofputil_port_to_ofp11(pm->port_no); | |
4474 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); | |
4475 | opm->config = htonl(pm->config & OFPPC11_ALL); | |
4476 | opm->mask = htonl(pm->mask & OFPPC11_ALL); | |
4477 | ||
4478 | if (pm->advertise) { | |
4479 | eth = ofpbuf_put_zeros(b, sizeof *eth); | |
4480 | eth->type = htons(OFPPMPT14_ETHERNET); | |
4481 | eth->length = htons(sizeof *eth); | |
4482 | eth->advertise = netdev_port_features_to_ofp11(pm->advertise); | |
4483 | } | |
c37c0382 | 4484 | break; |
18cc69d9 | 4485 | } |
918f2b82 | 4486 | default: |
428b2edd | 4487 | OVS_NOT_REACHED(); |
918f2b82 AZ |
4488 | } |
4489 | ||
4490 | return b; | |
4491 | } | |
4492 | ||
5deff5aa | 4493 | static enum ofperr |
9a4eddbb BP |
4494 | pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload, |
4495 | uint16_t *typep) | |
5deff5aa | 4496 | { |
9a4eddbb | 4497 | enum ofperr error; |
5deff5aa | 4498 | |
9a4eddbb BP |
4499 | error = ofputil_pull_property(msg, payload, typep); |
4500 | if (payload && !error) { | |
4501 | ofpbuf_pull(payload, sizeof(struct ofp_prop_header)); | |
5deff5aa | 4502 | } |
9a4eddbb | 4503 | return error; |
5deff5aa AW |
4504 | } |
4505 | ||
4506 | static enum ofperr | |
4507 | parse_table_ids(struct ofpbuf *payload, uint32_t *ids) | |
4508 | { | |
4509 | uint16_t type; | |
4510 | ||
4511 | *ids = 0; | |
1f317cb5 | 4512 | while (ofpbuf_size(payload) > 0) { |
9a4eddbb | 4513 | enum ofperr error = pull_table_feature_property(payload, NULL, &type); |
5deff5aa AW |
4514 | if (error) { |
4515 | return error; | |
4516 | } | |
4517 | if (type < CHAR_BIT * sizeof *ids) { | |
4518 | *ids |= 1u << type; | |
4519 | } | |
4520 | } | |
4521 | return 0; | |
4522 | } | |
4523 | ||
4524 | static enum ofperr | |
4525 | parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts) | |
4526 | { | |
4527 | *insts = 0; | |
1f317cb5 | 4528 | while (ofpbuf_size(payload) > 0) { |
5deff5aa AW |
4529 | enum ovs_instruction_type inst; |
4530 | enum ofperr error; | |
4531 | uint16_t ofpit; | |
4532 | ||
9a4eddbb | 4533 | error = pull_table_feature_property(payload, NULL, &ofpit); |
5deff5aa AW |
4534 | if (error) { |
4535 | return error; | |
4536 | } | |
4537 | ||
4538 | error = ovs_instruction_type_from_inst_type(&inst, ofpit); | |
4539 | if (!error) { | |
4540 | *insts |= 1u << inst; | |
4541 | } else if (!loose) { | |
4542 | return error; | |
4543 | } | |
4544 | } | |
4545 | return 0; | |
4546 | } | |
4547 | ||
4548 | static enum ofperr | |
4549 | parse_table_features_next_table(struct ofpbuf *payload, | |
4550 | unsigned long int *next_tables) | |
4551 | { | |
4552 | size_t i; | |
4553 | ||
4554 | memset(next_tables, 0, bitmap_n_bytes(255)); | |
1f317cb5 PS |
4555 | for (i = 0; i < ofpbuf_size(payload); i++) { |
4556 | uint8_t id = ((const uint8_t *) ofpbuf_data(payload))[i]; | |
5deff5aa | 4557 | if (id >= 255) { |
9a4eddbb | 4558 | return OFPERR_OFPBPC_BAD_VALUE; |
5deff5aa AW |
4559 | } |
4560 | bitmap_set1(next_tables, id); | |
4561 | } | |
4562 | return 0; | |
4563 | } | |
4564 | ||
4565 | static enum ofperr | |
4566 | parse_oxm(struct ofpbuf *b, bool loose, | |
4567 | const struct mf_field **fieldp, bool *hasmask) | |
4568 | { | |
4569 | ovs_be32 *oxmp; | |
4570 | uint32_t oxm; | |
4571 | ||
4572 | oxmp = ofpbuf_try_pull(b, sizeof *oxmp); | |
4573 | if (!oxmp) { | |
9a4eddbb | 4574 | return OFPERR_OFPBPC_BAD_LEN; |
5deff5aa AW |
4575 | } |
4576 | oxm = ntohl(*oxmp); | |
4577 | ||
4578 | /* Determine '*hasmask'. If 'oxm' is masked, convert it to the equivalent | |
4579 | * unmasked version, because the table of OXM fields we support only has | |
4580 | * masked versions of fields that we support with masks, but we should be | |
4581 | * able to parse the masked versions of those here. */ | |
4582 | *hasmask = NXM_HASMASK(oxm); | |
4583 | if (*hasmask) { | |
4584 | if (NXM_LENGTH(oxm) & 1) { | |
9a4eddbb | 4585 | return OFPERR_OFPBPC_BAD_VALUE; |
5deff5aa AW |
4586 | } |
4587 | oxm = NXM_HEADER(NXM_VENDOR(oxm), NXM_FIELD(oxm), NXM_LENGTH(oxm) / 2); | |
4588 | } | |
4589 | ||
4590 | *fieldp = mf_from_nxm_header(oxm); | |
4591 | if (!*fieldp) { | |
4592 | log_property(loose, "unknown OXM field %#"PRIx32, ntohl(*oxmp)); | |
4593 | } | |
4594 | return *fieldp ? 0 : OFPERR_OFPBMC_BAD_FIELD; | |
4595 | } | |
4596 | ||
4597 | static enum ofperr | |
4598 | parse_oxms(struct ofpbuf *payload, bool loose, | |
4599 | uint64_t *exactp, uint64_t *maskedp) | |
4600 | { | |
4601 | uint64_t exact, masked; | |
4602 | ||
4603 | exact = masked = 0; | |
1f317cb5 | 4604 | while (ofpbuf_size(payload) > 0) { |
5deff5aa AW |
4605 | const struct mf_field *field; |
4606 | enum ofperr error; | |
4607 | bool hasmask; | |
4608 | ||
4609 | error = parse_oxm(payload, loose, &field, &hasmask); | |
4610 | if (!error) { | |
4611 | if (hasmask) { | |
4612 | masked |= UINT64_C(1) << field->id; | |
4613 | } else { | |
4614 | exact |= UINT64_C(1) << field->id; | |
4615 | } | |
4616 | } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) { | |
4617 | return error; | |
4618 | } | |
4619 | } | |
4620 | if (exactp) { | |
4621 | *exactp = exact; | |
4622 | } else if (exact) { | |
4623 | return OFPERR_OFPBMC_BAD_MASK; | |
4624 | } | |
4625 | if (maskedp) { | |
4626 | *maskedp = masked; | |
4627 | } else if (masked) { | |
4628 | return OFPERR_OFPBMC_BAD_MASK; | |
4629 | } | |
4630 | return 0; | |
4631 | } | |
4632 | ||
4633 | /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract | |
4634 | * ofputil_table_features in 'tf'. | |
4635 | * | |
4636 | * If 'loose' is true, this function ignores properties and values that it does | |
4637 | * not understand, as a controller would want to do when interpreting | |
4638 | * capabilities provided by a switch. If 'loose' is false, this function | |
4639 | * treats unknown properties and values as an error, as a switch would want to | |
4640 | * do when interpreting a configuration request made by a controller. | |
4641 | * | |
4642 | * A single OpenFlow message can specify features for multiple tables. Calling | |
4643 | * this function multiple times for a single 'msg' iterates through the tables | |
4644 | * in the message. The caller must initially leave 'msg''s layer pointers null | |
4645 | * and not modify them between calls. | |
4646 | * | |
4647 | * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise | |
4648 | * a positive "enum ofperr" value. */ | |
4649 | int | |
4650 | ofputil_decode_table_features(struct ofpbuf *msg, | |
4651 | struct ofputil_table_features *tf, bool loose) | |
4652 | { | |
4653 | struct ofp13_table_features *otf; | |
4654 | unsigned int len; | |
4655 | ||
cf3b7538 | 4656 | if (!msg->frame) { |
5deff5aa AW |
4657 | ofpraw_pull_assert(msg); |
4658 | } | |
4659 | ||
1f317cb5 | 4660 | if (!ofpbuf_size(msg)) { |
5deff5aa AW |
4661 | return EOF; |
4662 | } | |
4663 | ||
1f317cb5 | 4664 | if (ofpbuf_size(msg) < sizeof *otf) { |
9a4eddbb | 4665 | return OFPERR_OFPBPC_BAD_LEN; |
5deff5aa AW |
4666 | } |
4667 | ||
1f317cb5 | 4668 | otf = ofpbuf_data(msg); |
5deff5aa | 4669 | len = ntohs(otf->length); |
1f317cb5 | 4670 | if (len < sizeof *otf || len % 8 || len > ofpbuf_size(msg)) { |
9a4eddbb | 4671 | return OFPERR_OFPBPC_BAD_LEN; |
5deff5aa AW |
4672 | } |
4673 | ofpbuf_pull(msg, sizeof *otf); | |
4674 | ||
4675 | tf->table_id = otf->table_id; | |
4676 | if (tf->table_id == OFPTT_ALL) { | |
4677 | return OFPERR_OFPTFFC_BAD_TABLE; | |
4678 | } | |
4679 | ||
4680 | ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN); | |
4681 | tf->metadata_match = otf->metadata_match; | |
4682 | tf->metadata_write = otf->metadata_write; | |
4683 | tf->config = ntohl(otf->config); | |
4684 | tf->max_entries = ntohl(otf->max_entries); | |
4685 | ||
1f317cb5 | 4686 | while (ofpbuf_size(msg) > 0) { |
5deff5aa AW |
4687 | struct ofpbuf payload; |
4688 | enum ofperr error; | |
4689 | uint16_t type; | |
4690 | ||
9a4eddbb | 4691 | error = pull_table_feature_property(msg, &payload, &type); |
5deff5aa AW |
4692 | if (error) { |
4693 | return error; | |
4694 | } | |
4695 | ||
4696 | switch ((enum ofp13_table_feature_prop_type) type) { | |
4697 | case OFPTFPT13_INSTRUCTIONS: | |
4698 | error = parse_instruction_ids(&payload, loose, | |
4699 | &tf->nonmiss.instructions); | |
4700 | break; | |
4701 | case OFPTFPT13_INSTRUCTIONS_MISS: | |
4702 | error = parse_instruction_ids(&payload, loose, | |
4703 | &tf->miss.instructions); | |
4704 | break; | |
4705 | ||
4706 | case OFPTFPT13_NEXT_TABLES: | |
4707 | error = parse_table_features_next_table(&payload, | |
4708 | tf->nonmiss.next); | |
4709 | break; | |
4710 | case OFPTFPT13_NEXT_TABLES_MISS: | |
4711 | error = parse_table_features_next_table(&payload, tf->miss.next); | |
4712 | break; | |
4713 | ||
4714 | case OFPTFPT13_WRITE_ACTIONS: | |
4715 | error = parse_table_ids(&payload, &tf->nonmiss.write.actions); | |
4716 | break; | |
4717 | case OFPTFPT13_WRITE_ACTIONS_MISS: | |
4718 | error = parse_table_ids(&payload, &tf->miss.write.actions); | |
4719 | break; | |
4720 | ||
4721 | case OFPTFPT13_APPLY_ACTIONS: | |
4722 | error = parse_table_ids(&payload, &tf->nonmiss.apply.actions); | |
4723 | break; | |
4724 | case OFPTFPT13_APPLY_ACTIONS_MISS: | |
4725 | error = parse_table_ids(&payload, &tf->miss.apply.actions); | |
4726 | break; | |
4727 | ||
4728 | case OFPTFPT13_MATCH: | |
4729 | error = parse_oxms(&payload, loose, &tf->match, &tf->mask); | |
4730 | break; | |
4731 | case OFPTFPT13_WILDCARDS: | |
4732 | error = parse_oxms(&payload, loose, &tf->wildcard, NULL); | |
4733 | break; | |
4734 | ||
4735 | case OFPTFPT13_WRITE_SETFIELD: | |
4736 | error = parse_oxms(&payload, loose, | |
4737 | &tf->nonmiss.write.set_fields, NULL); | |
4738 | break; | |
4739 | case OFPTFPT13_WRITE_SETFIELD_MISS: | |
4740 | error = parse_oxms(&payload, loose, | |
4741 | &tf->miss.write.set_fields, NULL); | |
4742 | break; | |
4743 | case OFPTFPT13_APPLY_SETFIELD: | |
4744 | error = parse_oxms(&payload, loose, | |
4745 | &tf->nonmiss.apply.set_fields, NULL); | |
4746 | break; | |
4747 | case OFPTFPT13_APPLY_SETFIELD_MISS: | |
4748 | error = parse_oxms(&payload, loose, | |
4749 | &tf->miss.apply.set_fields, NULL); | |
4750 | break; | |
4751 | ||
4752 | case OFPTFPT13_EXPERIMENTER: | |
4753 | case OFPTFPT13_EXPERIMENTER_MISS: | |
9a4eddbb BP |
4754 | default: |
4755 | log_property(loose, "unknown table features property %"PRIu16, | |
4756 | type); | |
4757 | error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE; | |
5deff5aa AW |
4758 | break; |
4759 | } | |
4760 | if (error) { | |
4761 | return error; | |
4762 | } | |
4763 | } | |
4764 | ||
4765 | /* Fix inconsistencies: | |
4766 | * | |
4767 | * - Turn off 'mask' and 'wildcard' bits that are not in 'match', | |
4768 | * because a field must be matchable to be masked or wildcarded. | |
4769 | * | |
4770 | * - Turn on 'wildcard' bits that are set in 'mask', because a field | |
4771 | * that is arbitrarily maskable can be wildcarded entirely. */ | |
4772 | tf->mask &= tf->match; | |
4773 | tf->wildcard &= tf->match; | |
4774 | ||
4775 | tf->wildcard |= tf->mask; | |
4776 | ||
4777 | return 0; | |
4778 | } | |
4779 | ||
4780 | /* Encodes and returns a request to obtain the table features of a switch. | |
4781 | * The message is encoded for OpenFlow version 'ofp_version'. */ | |
4782 | struct ofpbuf * | |
4783 | ofputil_encode_table_features_request(enum ofp_version ofp_version) | |
4784 | { | |
4785 | struct ofpbuf *request = NULL; | |
4786 | ||
4787 | switch (ofp_version) { | |
4788 | case OFP10_VERSION: | |
4789 | case OFP11_VERSION: | |
4790 | case OFP12_VERSION: | |
4791 | ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later " | |
4792 | "(\'-O OpenFlow13\')"); | |
4793 | case OFP13_VERSION: | |
4794 | case OFP14_VERSION: | |
42dccab5 | 4795 | case OFP15_VERSION: |
5deff5aa AW |
4796 | request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST, |
4797 | ofp_version, 0); | |
4798 | break; | |
4799 | default: | |
4800 | OVS_NOT_REACHED(); | |
4801 | } | |
4802 | ||
4803 | return request; | |
4804 | } | |
4805 | ||
918f2b82 AZ |
4806 | /* ofputil_table_mod */ |
4807 | ||
4808 | /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in | |
4809 | * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
4810 | enum ofperr | |
4811 | ofputil_decode_table_mod(const struct ofp_header *oh, | |
4812 | struct ofputil_table_mod *pm) | |
4813 | { | |
4814 | enum ofpraw raw; | |
4815 | struct ofpbuf b; | |
4816 | ||
4817 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
4818 | raw = ofpraw_pull_assert(&b); | |
4819 | ||
4820 | if (raw == OFPRAW_OFPT11_TABLE_MOD) { | |
1f317cb5 | 4821 | const struct ofp11_table_mod *otm = ofpbuf_data(&b); |
918f2b82 AZ |
4822 | |
4823 | pm->table_id = otm->table_id; | |
4824 | pm->config = ntohl(otm->config); | |
37ab26e8 BP |
4825 | } else if (raw == OFPRAW_OFPT14_TABLE_MOD) { |
4826 | const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm); | |
4827 | ||
4828 | pm->table_id = otm->table_id; | |
4829 | pm->config = ntohl(otm->config); | |
4830 | /* We do not understand any properties yet, so we do not bother | |
4831 | * parsing them. */ | |
918f2b82 AZ |
4832 | } else { |
4833 | return OFPERR_OFPBRC_BAD_TYPE; | |
4834 | } | |
4835 | ||
4836 | return 0; | |
4837 | } | |
4838 | ||
4839 | /* Converts the abstract form of a "table mod" message in '*pm' into an OpenFlow | |
4840 | * message suitable for 'protocol', and returns that encoded form in a buffer | |
4841 | * owned by the caller. */ | |
4842 | struct ofpbuf * | |
4843 | ofputil_encode_table_mod(const struct ofputil_table_mod *pm, | |
4844 | enum ofputil_protocol protocol) | |
4845 | { | |
4846 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); | |
4847 | struct ofpbuf *b; | |
4848 | ||
4849 | switch (ofp_version) { | |
4850 | case OFP10_VERSION: { | |
4851 | ovs_fatal(0, "table mod needs OpenFlow 1.1 or later " | |
4852 | "(\'-O OpenFlow11\')"); | |
4853 | break; | |
4854 | } | |
4855 | case OFP11_VERSION: | |
4856 | case OFP12_VERSION: | |
4857 | case OFP13_VERSION: { | |
4858 | struct ofp11_table_mod *otm; | |
2e3fa633 | 4859 | |
918f2b82 AZ |
4860 | b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0); |
4861 | otm = ofpbuf_put_zeros(b, sizeof *otm); | |
4862 | otm->table_id = pm->table_id; | |
4863 | otm->config = htonl(pm->config); | |
4864 | break; | |
4865 | } | |
42dccab5 BP |
4866 | case OFP14_VERSION: |
4867 | case OFP15_VERSION: { | |
37ab26e8 BP |
4868 | struct ofp14_table_mod *otm; |
4869 | ||
4870 | b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0); | |
4871 | otm = ofpbuf_put_zeros(b, sizeof *otm); | |
4872 | otm->table_id = pm->table_id; | |
4873 | otm->config = htonl(pm->config); | |
c37c0382 | 4874 | break; |
37ab26e8 | 4875 | } |
2e3fa633 | 4876 | default: |
428b2edd | 4877 | OVS_NOT_REACHED(); |
9e1fd49b BP |
4878 | } |
4879 | ||
4880 | return b; | |
4881 | } | |
2b07c8b1 | 4882 | \f |
6ea4776b JR |
4883 | /* ofputil_role_request */ |
4884 | ||
4885 | /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into | |
4886 | * an abstract form in '*rr'. Returns 0 if successful, otherwise an | |
4887 | * OFPERR_* value. */ | |
4888 | enum ofperr | |
4889 | ofputil_decode_role_message(const struct ofp_header *oh, | |
4890 | struct ofputil_role_request *rr) | |
4891 | { | |
6ea4776b JR |
4892 | struct ofpbuf b; |
4893 | enum ofpraw raw; | |
4894 | ||
6ea4776b JR |
4895 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
4896 | raw = ofpraw_pull_assert(&b); | |
4897 | ||
f4f1ea7e BP |
4898 | if (raw == OFPRAW_OFPT12_ROLE_REQUEST || |
4899 | raw == OFPRAW_OFPT12_ROLE_REPLY) { | |
6b8c377a | 4900 | const struct ofp12_role_request *orr = ofpbuf_l3(&b); |
6ea4776b | 4901 | |
f4f1ea7e BP |
4902 | if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) && |
4903 | orr->role != htonl(OFPCR12_ROLE_EQUAL) && | |
4904 | orr->role != htonl(OFPCR12_ROLE_MASTER) && | |
4905 | orr->role != htonl(OFPCR12_ROLE_SLAVE)) { | |
4906 | return OFPERR_OFPRRFC_BAD_ROLE; | |
6ea4776b JR |
4907 | } |
4908 | ||
f4f1ea7e | 4909 | rr->role = ntohl(orr->role); |
147cc9d3 BP |
4910 | if (raw == OFPRAW_OFPT12_ROLE_REQUEST |
4911 | ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE) | |
b8266395 | 4912 | : orr->generation_id == OVS_BE64_MAX) { |
f4f1ea7e BP |
4913 | rr->have_generation_id = false; |
4914 | rr->generation_id = 0; | |
4915 | } else { | |
4916 | rr->have_generation_id = true; | |
4917 | rr->generation_id = ntohll(orr->generation_id); | |
4918 | } | |
4919 | } else if (raw == OFPRAW_NXT_ROLE_REQUEST || | |
4920 | raw == OFPRAW_NXT_ROLE_REPLY) { | |
6b8c377a | 4921 | const struct nx_role_request *nrr = ofpbuf_l3(&b); |
f4f1ea7e BP |
4922 | |
4923 | BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL); | |
4924 | BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER); | |
4925 | BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE); | |
4926 | ||
4927 | if (nrr->role != htonl(NX_ROLE_OTHER) && | |
4928 | nrr->role != htonl(NX_ROLE_MASTER) && | |
4929 | nrr->role != htonl(NX_ROLE_SLAVE)) { | |
4930 | return OFPERR_OFPRRFC_BAD_ROLE; | |
4931 | } | |
6ea4776b | 4932 | |
f4f1ea7e BP |
4933 | rr->role = ntohl(nrr->role) + 1; |
4934 | rr->have_generation_id = false; | |
4935 | rr->generation_id = 0; | |
4936 | } else { | |
428b2edd | 4937 | OVS_NOT_REACHED(); |
6ea4776b JR |
4938 | } |
4939 | ||
6ea4776b JR |
4940 | return 0; |
4941 | } | |
4942 | ||
4943 | /* Returns an encoded form of a role reply suitable for the "request" in a | |
4944 | * buffer owned by the caller. */ | |
4945 | struct ofpbuf * | |
4946 | ofputil_encode_role_reply(const struct ofp_header *request, | |
f4f1ea7e | 4947 | const struct ofputil_role_request *rr) |
6ea4776b | 4948 | { |
6ea4776b | 4949 | struct ofpbuf *buf; |
6ea4776b JR |
4950 | enum ofpraw raw; |
4951 | ||
f4f1ea7e | 4952 | raw = ofpraw_decode_assert(request); |
6ea4776b | 4953 | if (raw == OFPRAW_OFPT12_ROLE_REQUEST) { |
f4f1ea7e | 4954 | struct ofp12_role_request *orr; |
6ea4776b | 4955 | |
f4f1ea7e BP |
4956 | buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0); |
4957 | orr = ofpbuf_put_zeros(buf, sizeof *orr); | |
6ea4776b | 4958 | |
147cc9d3 BP |
4959 | orr->role = htonl(rr->role); |
4960 | orr->generation_id = htonll(rr->have_generation_id | |
4961 | ? rr->generation_id | |
4962 | : UINT64_MAX); | |
f4f1ea7e BP |
4963 | } else if (raw == OFPRAW_NXT_ROLE_REQUEST) { |
4964 | struct nx_role_request *nrr; | |
4965 | ||
4966 | BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1); | |
4967 | BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1); | |
4968 | BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1); | |
4969 | ||
4970 | buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0); | |
4971 | nrr = ofpbuf_put_zeros(buf, sizeof *nrr); | |
4972 | nrr->role = htonl(rr->role - 1); | |
4973 | } else { | |
428b2edd | 4974 | OVS_NOT_REACHED(); |
6ea4776b | 4975 | } |
6ea4776b JR |
4976 | |
4977 | return buf; | |
4978 | } | |
4979 | \f | |
00467f73 AC |
4980 | struct ofpbuf * |
4981 | ofputil_encode_role_status(const struct ofputil_role_status *status, | |
4982 | enum ofputil_protocol protocol) | |
4983 | { | |
4984 | struct ofpbuf *buf; | |
4985 | enum ofp_version version; | |
4986 | struct ofp14_role_status *rstatus; | |
4987 | ||
4988 | version = ofputil_protocol_to_ofp_version(protocol); | |
4989 | buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0), 0); | |
4990 | rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus); | |
4991 | rstatus->role = htonl(status->role); | |
4992 | rstatus->reason = status->reason; | |
4993 | rstatus->generation_id = htonll(status->generation_id); | |
4994 | ||
4995 | return buf; | |
4996 | } | |
4997 | ||
4998 | enum ofperr | |
4999 | ofputil_decode_role_status(const struct ofp_header *oh, | |
5000 | struct ofputil_role_status *rs) | |
5001 | { | |
5002 | struct ofpbuf b; | |
5003 | enum ofpraw raw; | |
5004 | const struct ofp14_role_status *r; | |
5005 | ||
5006 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
5007 | raw = ofpraw_pull_assert(&b); | |
5008 | ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS); | |
5009 | ||
6b8c377a | 5010 | r = ofpbuf_l3(&b); |
00467f73 AC |
5011 | if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) && |
5012 | r->role != htonl(OFPCR12_ROLE_EQUAL) && | |
5013 | r->role != htonl(OFPCR12_ROLE_MASTER) && | |
5014 | r->role != htonl(OFPCR12_ROLE_SLAVE)) { | |
5015 | return OFPERR_OFPRRFC_BAD_ROLE; | |
5016 | } | |
5017 | ||
5018 | rs->role = ntohl(r->role); | |
5019 | rs->generation_id = ntohll(r->generation_id); | |
5020 | rs->reason = r->reason; | |
5021 | ||
5022 | return 0; | |
5023 | } | |
5024 | ||
307975da SH |
5025 | /* Table stats. */ |
5026 | ||
5027 | static void | |
5028 | ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in, | |
5029 | struct ofpbuf *buf) | |
5030 | { | |
5031 | struct wc_map { | |
31a9e63f | 5032 | enum ofp10_flow_wildcards wc10; |
307975da SH |
5033 | enum oxm12_ofb_match_fields mf12; |
5034 | }; | |
5035 | ||
5036 | static const struct wc_map wc_map[] = { | |
5037 | { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT }, | |
5038 | { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID }, | |
5039 | { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC }, | |
5040 | { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST}, | |
5041 | { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE }, | |
5042 | { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO }, | |
5043 | { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC }, | |
5044 | { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST }, | |
5045 | { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC }, | |
5046 | { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST }, | |
5047 | { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP }, | |
5048 | { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP }, | |
5049 | }; | |
5050 | ||
5051 | struct ofp10_table_stats *out; | |
5052 | const struct wc_map *p; | |
5053 | ||
3bdc692b | 5054 | out = ofpbuf_put_zeros(buf, sizeof *out); |
307975da | 5055 | out->table_id = in->table_id; |
3bdc692b | 5056 | ovs_strlcpy(out->name, in->name, sizeof out->name); |
307975da SH |
5057 | out->wildcards = 0; |
5058 | for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) { | |
5059 | if (in->wildcards & htonll(1ULL << p->mf12)) { | |
5060 | out->wildcards |= htonl(p->wc10); | |
5061 | } | |
5062 | } | |
5063 | out->max_entries = in->max_entries; | |
5064 | out->active_count = in->active_count; | |
5065 | put_32aligned_be64(&out->lookup_count, in->lookup_count); | |
5066 | put_32aligned_be64(&out->matched_count, in->matched_count); | |
5067 | } | |
5068 | ||
5069 | static ovs_be32 | |
5070 | oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12) | |
5071 | { | |
5072 | struct map { | |
5073 | enum ofp11_flow_match_fields fmf11; | |
5074 | enum oxm12_ofb_match_fields mf12; | |
5075 | }; | |
5076 | ||
5077 | static const struct map map[] = { | |
5078 | { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT }, | |
5079 | { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID }, | |
5080 | { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP }, | |
5081 | { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE }, | |
5082 | { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP }, | |
5083 | { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO }, | |
5084 | { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC }, | |
5085 | { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST }, | |
5086 | { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL }, | |
5087 | { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC }, | |
5088 | /* I don't know what OFPFMF11_TYPE means. */ | |
5089 | { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC }, | |
5090 | { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST }, | |
5091 | { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC }, | |
5092 | { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST }, | |
5093 | { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA }, | |
5094 | }; | |
5095 | ||
5096 | const struct map *p; | |
5097 | uint32_t fmf11; | |
5098 | ||
5099 | fmf11 = 0; | |
5100 | for (p = map; p < &map[ARRAY_SIZE(map)]; p++) { | |
5101 | if (oxm12 & htonll(1ULL << p->mf12)) { | |
5102 | fmf11 |= p->fmf11; | |
5103 | } | |
5104 | } | |
5105 | return htonl(fmf11); | |
5106 | } | |
5107 | ||
5108 | static void | |
5109 | ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in, | |
5110 | struct ofpbuf *buf) | |
5111 | { | |
5112 | struct ofp11_table_stats *out; | |
5113 | ||
3bdc692b | 5114 | out = ofpbuf_put_zeros(buf, sizeof *out); |
307975da | 5115 | out->table_id = in->table_id; |
3bdc692b | 5116 | ovs_strlcpy(out->name, in->name, sizeof out->name); |
307975da SH |
5117 | out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards); |
5118 | out->match = oxm12_to_ofp11_flow_match_fields(in->match); | |
5119 | out->instructions = in->instructions; | |
5120 | out->write_actions = in->write_actions; | |
5121 | out->apply_actions = in->apply_actions; | |
5122 | out->config = in->config; | |
5123 | out->max_entries = in->max_entries; | |
5124 | out->active_count = in->active_count; | |
5125 | out->lookup_count = in->lookup_count; | |
5126 | out->matched_count = in->matched_count; | |
5127 | } | |
5128 | ||
6240624b BP |
5129 | static void |
5130 | ofputil_put_ofp12_table_stats(const struct ofp12_table_stats *in, | |
5131 | struct ofpbuf *buf) | |
5132 | { | |
5133 | struct ofp12_table_stats *out = ofpbuf_put(buf, in, sizeof *in); | |
5134 | ||
5135 | /* Trim off OF1.3-only capabilities. */ | |
5136 | out->match &= htonll(OFPXMT12_MASK); | |
5137 | out->wildcards &= htonll(OFPXMT12_MASK); | |
5138 | out->write_setfields &= htonll(OFPXMT12_MASK); | |
5139 | out->apply_setfields &= htonll(OFPXMT12_MASK); | |
5140 | } | |
5141 | ||
2e1ae200 JR |
5142 | static void |
5143 | ofputil_put_ofp13_table_stats(const struct ofp12_table_stats *in, | |
5144 | struct ofpbuf *buf) | |
5145 | { | |
5146 | struct ofp13_table_stats *out; | |
5147 | ||
5148 | /* OF 1.3 splits table features off the ofp_table_stats, | |
5149 | * so there is not much here. */ | |
5150 | ||
5151 | out = ofpbuf_put_uninit(buf, sizeof *out); | |
5152 | out->table_id = in->table_id; | |
5153 | out->active_count = in->active_count; | |
5154 | out->lookup_count = in->lookup_count; | |
5155 | out->matched_count = in->matched_count; | |
5156 | } | |
5157 | ||
307975da SH |
5158 | struct ofpbuf * |
5159 | ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n, | |
5160 | const struct ofp_header *request) | |
5161 | { | |
5162 | struct ofpbuf *reply; | |
5163 | int i; | |
5164 | ||
5165 | reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats); | |
5166 | ||
6240624b BP |
5167 | for (i = 0; i < n; i++) { |
5168 | switch ((enum ofp_version) request->version) { | |
5169 | case OFP10_VERSION: | |
307975da | 5170 | ofputil_put_ofp10_table_stats(&stats[i], reply); |
6240624b | 5171 | break; |
307975da | 5172 | |
6240624b | 5173 | case OFP11_VERSION: |
307975da | 5174 | ofputil_put_ofp11_table_stats(&stats[i], reply); |
6240624b | 5175 | break; |
307975da | 5176 | |
6240624b BP |
5177 | case OFP12_VERSION: |
5178 | ofputil_put_ofp12_table_stats(&stats[i], reply); | |
5179 | break; | |
307975da | 5180 | |
6240624b | 5181 | case OFP13_VERSION: |
c37c0382 | 5182 | case OFP14_VERSION: |
42dccab5 | 5183 | case OFP15_VERSION: |
2e1ae200 | 5184 | ofputil_put_ofp13_table_stats(&stats[i], reply); |
6240624b | 5185 | break; |
2e1ae200 | 5186 | |
6240624b | 5187 | default: |
428b2edd | 5188 | OVS_NOT_REACHED(); |
6240624b | 5189 | } |
307975da SH |
5190 | } |
5191 | ||
5192 | return reply; | |
5193 | } | |
5194 | \f | |
2b07c8b1 BP |
5195 | /* ofputil_flow_monitor_request */ |
5196 | ||
5197 | /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract | |
5198 | * ofputil_flow_monitor_request in 'rq'. | |
5199 | * | |
5200 | * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow | |
5201 | * message. Calling this function multiple times for a single 'msg' iterates | |
5202 | * through the requests. The caller must initially leave 'msg''s layer | |
5203 | * pointers null and not modify them between calls. | |
5204 | * | |
5205 | * Returns 0 if successful, EOF if no requests were left in this 'msg', | |
5206 | * otherwise an OFPERR_* value. */ | |
5207 | int | |
5208 | ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq, | |
5209 | struct ofpbuf *msg) | |
5210 | { | |
5211 | struct nx_flow_monitor_request *nfmr; | |
5212 | uint16_t flags; | |
5213 | ||
cf3b7538 | 5214 | if (!msg->frame) { |
982697a4 | 5215 | ofpraw_pull_assert(msg); |
2b07c8b1 BP |
5216 | } |
5217 | ||
1f317cb5 | 5218 | if (!ofpbuf_size(msg)) { |
2b07c8b1 BP |
5219 | return EOF; |
5220 | } | |
5221 | ||
5222 | nfmr = ofpbuf_try_pull(msg, sizeof *nfmr); | |
5223 | if (!nfmr) { | |
437d0d22 | 5224 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" " |
1f317cb5 | 5225 | "leftover bytes at end", ofpbuf_size(msg)); |
2b07c8b1 BP |
5226 | return OFPERR_OFPBRC_BAD_LEN; |
5227 | } | |
5228 | ||
5229 | flags = ntohs(nfmr->flags); | |
5230 | if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY)) | |
5231 | || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | |
5232 | | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) { | |
5233 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16, | |
5234 | flags); | |
04f9f286 | 5235 | return OFPERR_OFPMOFC_BAD_FLAGS; |
2b07c8b1 BP |
5236 | } |
5237 | ||
5238 | if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) { | |
5239 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
5240 | } | |
5241 | ||
5242 | rq->id = ntohl(nfmr->id); | |
5243 | rq->flags = flags; | |
4e022ec0 | 5244 | rq->out_port = u16_to_ofp(ntohs(nfmr->out_port)); |
2b07c8b1 BP |
5245 | rq->table_id = nfmr->table_id; |
5246 | ||
81a76618 | 5247 | return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL); |
2b07c8b1 BP |
5248 | } |
5249 | ||
5250 | void | |
5251 | ofputil_append_flow_monitor_request( | |
5252 | const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg) | |
5253 | { | |
5254 | struct nx_flow_monitor_request *nfmr; | |
5255 | size_t start_ofs; | |
5256 | int match_len; | |
5257 | ||
1f317cb5 | 5258 | if (!ofpbuf_size(msg)) { |
982697a4 | 5259 | ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg); |
2b07c8b1 BP |
5260 | } |
5261 | ||
1f317cb5 | 5262 | start_ofs = ofpbuf_size(msg); |
2b07c8b1 | 5263 | ofpbuf_put_zeros(msg, sizeof *nfmr); |
7623f4dd | 5264 | match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0)); |
2b07c8b1 BP |
5265 | |
5266 | nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr); | |
5267 | nfmr->id = htonl(rq->id); | |
5268 | nfmr->flags = htons(rq->flags); | |
4e022ec0 | 5269 | nfmr->out_port = htons(ofp_to_u16(rq->out_port)); |
2b07c8b1 BP |
5270 | nfmr->match_len = htons(match_len); |
5271 | nfmr->table_id = rq->table_id; | |
5272 | } | |
5273 | ||
5274 | /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg' | |
5275 | * into an abstract ofputil_flow_update in 'update'. The caller must have | |
81a76618 | 5276 | * initialized update->match to point to space allocated for a match. |
2b07c8b1 BP |
5277 | * |
5278 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's | |
5279 | * actions (except for NXFME_ABBREV, which never includes actions). The caller | |
5280 | * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts' | |
5281 | * will point into the 'ofpacts' buffer. | |
5282 | * | |
5283 | * Multiple flow updates can be packed into a single OpenFlow message. Calling | |
5284 | * this function multiple times for a single 'msg' iterates through the | |
5285 | * updates. The caller must initially leave 'msg''s layer pointers null and | |
5286 | * not modify them between calls. | |
5287 | * | |
5288 | * Returns 0 if successful, EOF if no updates were left in this 'msg', | |
5289 | * otherwise an OFPERR_* value. */ | |
5290 | int | |
5291 | ofputil_decode_flow_update(struct ofputil_flow_update *update, | |
5292 | struct ofpbuf *msg, struct ofpbuf *ofpacts) | |
5293 | { | |
5294 | struct nx_flow_update_header *nfuh; | |
5295 | unsigned int length; | |
e3f8f887 | 5296 | struct ofp_header *oh; |
2b07c8b1 | 5297 | |
cf3b7538 | 5298 | if (!msg->frame) { |
982697a4 | 5299 | ofpraw_pull_assert(msg); |
2b07c8b1 BP |
5300 | } |
5301 | ||
1f317cb5 | 5302 | if (!ofpbuf_size(msg)) { |
2b07c8b1 BP |
5303 | return EOF; |
5304 | } | |
5305 | ||
1f317cb5 | 5306 | if (ofpbuf_size(msg) < sizeof(struct nx_flow_update_header)) { |
2b07c8b1 BP |
5307 | goto bad_len; |
5308 | } | |
5309 | ||
cf3b7538 | 5310 | oh = msg->frame; |
e3f8f887 | 5311 | |
1f317cb5 | 5312 | nfuh = ofpbuf_data(msg); |
2b07c8b1 BP |
5313 | update->event = ntohs(nfuh->event); |
5314 | length = ntohs(nfuh->length); | |
1f317cb5 | 5315 | if (length > ofpbuf_size(msg) || length % 8) { |
2b07c8b1 BP |
5316 | goto bad_len; |
5317 | } | |
5318 | ||
5319 | if (update->event == NXFME_ABBREV) { | |
5320 | struct nx_flow_update_abbrev *nfua; | |
5321 | ||
5322 | if (length != sizeof *nfua) { | |
5323 | goto bad_len; | |
5324 | } | |
5325 | ||
5326 | nfua = ofpbuf_pull(msg, sizeof *nfua); | |
5327 | update->xid = nfua->xid; | |
5328 | return 0; | |
5329 | } else if (update->event == NXFME_ADDED | |
5330 | || update->event == NXFME_DELETED | |
5331 | || update->event == NXFME_MODIFIED) { | |
5332 | struct nx_flow_update_full *nfuf; | |
5333 | unsigned int actions_len; | |
5334 | unsigned int match_len; | |
5335 | enum ofperr error; | |
5336 | ||
5337 | if (length < sizeof *nfuf) { | |
5338 | goto bad_len; | |
5339 | } | |
5340 | ||
5341 | nfuf = ofpbuf_pull(msg, sizeof *nfuf); | |
5342 | match_len = ntohs(nfuf->match_len); | |
5343 | if (sizeof *nfuf + match_len > length) { | |
5344 | goto bad_len; | |
5345 | } | |
5346 | ||
5347 | update->reason = ntohs(nfuf->reason); | |
5348 | update->idle_timeout = ntohs(nfuf->idle_timeout); | |
5349 | update->hard_timeout = ntohs(nfuf->hard_timeout); | |
5350 | update->table_id = nfuf->table_id; | |
5351 | update->cookie = nfuf->cookie; | |
81a76618 | 5352 | update->priority = ntohs(nfuf->priority); |
2b07c8b1 | 5353 | |
81a76618 | 5354 | error = nx_pull_match(msg, match_len, update->match, NULL, NULL); |
2b07c8b1 BP |
5355 | if (error) { |
5356 | return error; | |
5357 | } | |
5358 | ||
5359 | actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8); | |
e3f8f887 JR |
5360 | error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version, |
5361 | ofpacts); | |
2b07c8b1 BP |
5362 | if (error) { |
5363 | return error; | |
5364 | } | |
5365 | ||
1f317cb5 PS |
5366 | update->ofpacts = ofpbuf_data(ofpacts); |
5367 | update->ofpacts_len = ofpbuf_size(ofpacts); | |
2b07c8b1 BP |
5368 | return 0; |
5369 | } else { | |
5370 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
5371 | "NXST_FLOW_MONITOR reply has bad event %"PRIu16, | |
5372 | ntohs(nfuh->event)); | |
15549878 | 5373 | return OFPERR_NXBRC_FM_BAD_EVENT; |
2b07c8b1 BP |
5374 | } |
5375 | ||
5376 | bad_len: | |
437d0d22 | 5377 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" " |
1f317cb5 | 5378 | "leftover bytes at end", ofpbuf_size(msg)); |
2b07c8b1 BP |
5379 | return OFPERR_OFPBRC_BAD_LEN; |
5380 | } | |
5381 | ||
5382 | uint32_t | |
5383 | ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh) | |
5384 | { | |
982697a4 BP |
5385 | const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh); |
5386 | ||
5387 | return ntohl(cancel->id); | |
2b07c8b1 | 5388 | } |
9e1fd49b | 5389 | |
2b07c8b1 BP |
5390 | struct ofpbuf * |
5391 | ofputil_encode_flow_monitor_cancel(uint32_t id) | |
5392 | { | |
5393 | struct nx_flow_monitor_cancel *nfmc; | |
5394 | struct ofpbuf *msg; | |
5395 | ||
982697a4 BP |
5396 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0); |
5397 | nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc); | |
2b07c8b1 BP |
5398 | nfmc->id = htonl(id); |
5399 | return msg; | |
5400 | } | |
5401 | ||
5402 | void | |
5403 | ofputil_start_flow_update(struct list *replies) | |
5404 | { | |
5405 | struct ofpbuf *msg; | |
5406 | ||
982697a4 BP |
5407 | msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION, |
5408 | htonl(0), 1024); | |
2b07c8b1 BP |
5409 | |
5410 | list_init(replies); | |
5411 | list_push_back(replies, &msg->list_node); | |
5412 | } | |
5413 | ||
5414 | void | |
5415 | ofputil_append_flow_update(const struct ofputil_flow_update *update, | |
5416 | struct list *replies) | |
5417 | { | |
e28ac5cf | 5418 | enum ofp_version version = ofpmp_version(replies); |
2b07c8b1 BP |
5419 | struct nx_flow_update_header *nfuh; |
5420 | struct ofpbuf *msg; | |
5421 | size_t start_ofs; | |
5422 | ||
5423 | msg = ofpbuf_from_list(list_back(replies)); | |
1f317cb5 | 5424 | start_ofs = ofpbuf_size(msg); |
2b07c8b1 BP |
5425 | |
5426 | if (update->event == NXFME_ABBREV) { | |
5427 | struct nx_flow_update_abbrev *nfua; | |
5428 | ||
5429 | nfua = ofpbuf_put_zeros(msg, sizeof *nfua); | |
5430 | nfua->xid = update->xid; | |
5431 | } else { | |
5432 | struct nx_flow_update_full *nfuf; | |
5433 | int match_len; | |
5434 | ||
5435 | ofpbuf_put_zeros(msg, sizeof *nfuf); | |
7623f4dd | 5436 | match_len = nx_put_match(msg, update->match, htonll(0), htonll(0)); |
e3f8f887 JR |
5437 | ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg, |
5438 | version); | |
2b07c8b1 BP |
5439 | nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf); |
5440 | nfuf->reason = htons(update->reason); | |
81a76618 | 5441 | nfuf->priority = htons(update->priority); |
2b07c8b1 BP |
5442 | nfuf->idle_timeout = htons(update->idle_timeout); |
5443 | nfuf->hard_timeout = htons(update->hard_timeout); | |
5444 | nfuf->match_len = htons(match_len); | |
5445 | nfuf->table_id = update->table_id; | |
5446 | nfuf->cookie = update->cookie; | |
5447 | } | |
5448 | ||
5449 | nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh); | |
1f317cb5 | 5450 | nfuh->length = htons(ofpbuf_size(msg) - start_ofs); |
2b07c8b1 BP |
5451 | nfuh->event = htons(update->event); |
5452 | ||
982697a4 | 5453 | ofpmp_postappend(replies, start_ofs); |
2b07c8b1 BP |
5454 | } |
5455 | \f | |
c6a93eb7 | 5456 | struct ofpbuf * |
de0f3156 SH |
5457 | ofputil_encode_packet_out(const struct ofputil_packet_out *po, |
5458 | enum ofputil_protocol protocol) | |
c6a93eb7 | 5459 | { |
de0f3156 | 5460 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); |
c6a93eb7 BP |
5461 | struct ofpbuf *msg; |
5462 | size_t size; | |
5463 | ||
982697a4 | 5464 | size = po->ofpacts_len; |
c6a93eb7 BP |
5465 | if (po->buffer_id == UINT32_MAX) { |
5466 | size += po->packet_len; | |
5467 | } | |
5468 | ||
de0f3156 SH |
5469 | switch (ofp_version) { |
5470 | case OFP10_VERSION: { | |
31a9e63f | 5471 | struct ofp10_packet_out *opo; |
de0f3156 SH |
5472 | size_t actions_ofs; |
5473 | ||
5474 | msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size); | |
5475 | ofpbuf_put_zeros(msg, sizeof *opo); | |
1f317cb5 | 5476 | actions_ofs = ofpbuf_size(msg); |
e3f8f887 JR |
5477 | ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg, |
5478 | ofp_version); | |
de0f3156 | 5479 | |
6b8c377a | 5480 | opo = ofpbuf_l3(msg); |
de0f3156 | 5481 | opo->buffer_id = htonl(po->buffer_id); |
4e022ec0 | 5482 | opo->in_port = htons(ofp_to_u16(po->in_port)); |
1f317cb5 | 5483 | opo->actions_len = htons(ofpbuf_size(msg) - actions_ofs); |
de0f3156 SH |
5484 | break; |
5485 | } | |
f25d0cf3 | 5486 | |
de0f3156 | 5487 | case OFP11_VERSION: |
2e1ae200 | 5488 | case OFP12_VERSION: |
c37c0382 | 5489 | case OFP13_VERSION: |
42dccab5 BP |
5490 | case OFP14_VERSION: |
5491 | case OFP15_VERSION: { | |
7c1b1a0d SH |
5492 | struct ofp11_packet_out *opo; |
5493 | size_t len; | |
5494 | ||
5495 | msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size); | |
5496 | ofpbuf_put_zeros(msg, sizeof *opo); | |
e3f8f887 JR |
5497 | len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg, |
5498 | ofp_version); | |
6b8c377a | 5499 | opo = ofpbuf_l3(msg); |
7c1b1a0d SH |
5500 | opo->buffer_id = htonl(po->buffer_id); |
5501 | opo->in_port = ofputil_port_to_ofp11(po->in_port); | |
5502 | opo->actions_len = htons(len); | |
5503 | break; | |
5504 | } | |
5505 | ||
de0f3156 | 5506 | default: |
428b2edd | 5507 | OVS_NOT_REACHED(); |
de0f3156 | 5508 | } |
f25d0cf3 | 5509 | |
c6a93eb7 BP |
5510 | if (po->buffer_id == UINT32_MAX) { |
5511 | ofpbuf_put(msg, po->packet, po->packet_len); | |
5512 | } | |
f25d0cf3 | 5513 | |
982697a4 | 5514 | ofpmsg_update_length(msg); |
c6a93eb7 BP |
5515 | |
5516 | return msg; | |
5517 | } | |
d1e2cf21 | 5518 | \f |
fa37b408 BP |
5519 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ |
5520 | struct ofpbuf * | |
1a126c0c | 5521 | make_echo_request(enum ofp_version ofp_version) |
fa37b408 | 5522 | { |
1a126c0c | 5523 | return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version, |
982697a4 | 5524 | htonl(0), 0); |
fa37b408 BP |
5525 | } |
5526 | ||
5527 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
5528 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
5529 | struct ofpbuf * | |
5530 | make_echo_reply(const struct ofp_header *rq) | |
5531 | { | |
982697a4 BP |
5532 | struct ofpbuf rq_buf; |
5533 | struct ofpbuf *reply; | |
5534 | ||
5535 | ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length)); | |
5536 | ofpraw_pull_assert(&rq_buf); | |
5537 | ||
1f317cb5 PS |
5538 | reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, ofpbuf_size(&rq_buf)); |
5539 | ofpbuf_put(reply, ofpbuf_data(&rq_buf), ofpbuf_size(&rq_buf)); | |
982697a4 | 5540 | return reply; |
fa37b408 BP |
5541 | } |
5542 | ||
efb80167 | 5543 | struct ofpbuf * |
a0ae0b6e | 5544 | ofputil_encode_barrier_request(enum ofp_version ofp_version) |
efb80167 | 5545 | { |
a0ae0b6e SH |
5546 | enum ofpraw type; |
5547 | ||
5548 | switch (ofp_version) { | |
42dccab5 | 5549 | case OFP15_VERSION: |
c37c0382 | 5550 | case OFP14_VERSION: |
2e1ae200 | 5551 | case OFP13_VERSION: |
a0ae0b6e SH |
5552 | case OFP12_VERSION: |
5553 | case OFP11_VERSION: | |
5554 | type = OFPRAW_OFPT11_BARRIER_REQUEST; | |
5555 | break; | |
5556 | ||
5557 | case OFP10_VERSION: | |
5558 | type = OFPRAW_OFPT10_BARRIER_REQUEST; | |
5559 | break; | |
5560 | ||
5561 | default: | |
428b2edd | 5562 | OVS_NOT_REACHED(); |
a0ae0b6e SH |
5563 | } |
5564 | ||
5565 | return ofpraw_alloc(type, ofp_version, 0); | |
efb80167 BP |
5566 | } |
5567 | ||
7257b535 BP |
5568 | const char * |
5569 | ofputil_frag_handling_to_string(enum ofp_config_flags flags) | |
5570 | { | |
5571 | switch (flags & OFPC_FRAG_MASK) { | |
5572 | case OFPC_FRAG_NORMAL: return "normal"; | |
5573 | case OFPC_FRAG_DROP: return "drop"; | |
5574 | case OFPC_FRAG_REASM: return "reassemble"; | |
5575 | case OFPC_FRAG_NX_MATCH: return "nx-match"; | |
5576 | } | |
5577 | ||
428b2edd | 5578 | OVS_NOT_REACHED(); |
7257b535 BP |
5579 | } |
5580 | ||
5581 | bool | |
5582 | ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags) | |
5583 | { | |
5584 | if (!strcasecmp(s, "normal")) { | |
5585 | *flags = OFPC_FRAG_NORMAL; | |
5586 | } else if (!strcasecmp(s, "drop")) { | |
5587 | *flags = OFPC_FRAG_DROP; | |
5588 | } else if (!strcasecmp(s, "reassemble")) { | |
5589 | *flags = OFPC_FRAG_REASM; | |
5590 | } else if (!strcasecmp(s, "nx-match")) { | |
5591 | *flags = OFPC_FRAG_NX_MATCH; | |
5592 | } else { | |
5593 | return false; | |
5594 | } | |
5595 | return true; | |
5596 | } | |
5597 | ||
7b7503ea BP |
5598 | /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0 |
5599 | * port number and stores the latter in '*ofp10_port', for the purpose of | |
5600 | * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful, | |
bc146369 | 5601 | * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'. |
7b7503ea BP |
5602 | * |
5603 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
5604 | enum ofperr | |
4e022ec0 | 5605 | ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port) |
7b7503ea BP |
5606 | { |
5607 | uint32_t ofp11_port_h = ntohl(ofp11_port); | |
5608 | ||
4e022ec0 AW |
5609 | if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) { |
5610 | *ofp10_port = u16_to_ofp(ofp11_port_h); | |
7b7503ea | 5611 | return 0; |
4e022ec0 AW |
5612 | } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) { |
5613 | *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET); | |
7b7503ea BP |
5614 | return 0; |
5615 | } else { | |
bc146369 | 5616 | *ofp10_port = OFPP_NONE; |
7b7503ea BP |
5617 | VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported " |
5618 | "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32, | |
4e022ec0 AW |
5619 | ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1, |
5620 | ofp11_to_u32(OFPP11_MAX), UINT32_MAX); | |
7b7503ea BP |
5621 | return OFPERR_OFPBAC_BAD_OUT_PORT; |
5622 | } | |
5623 | } | |
5624 | ||
5625 | /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port | |
5626 | * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages. | |
5627 | * | |
5628 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
5629 | ovs_be32 | |
4e022ec0 | 5630 | ofputil_port_to_ofp11(ofp_port_t ofp10_port) |
7b7503ea | 5631 | { |
4e022ec0 AW |
5632 | return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX) |
5633 | ? ofp_to_u16(ofp10_port) | |
5634 | : ofp_to_u16(ofp10_port) + OFPP11_OFFSET); | |
7b7503ea BP |
5635 | } |
5636 | ||
39dc9082 BP |
5637 | #define OFPUTIL_NAMED_PORTS \ |
5638 | OFPUTIL_NAMED_PORT(IN_PORT) \ | |
5639 | OFPUTIL_NAMED_PORT(TABLE) \ | |
5640 | OFPUTIL_NAMED_PORT(NORMAL) \ | |
5641 | OFPUTIL_NAMED_PORT(FLOOD) \ | |
5642 | OFPUTIL_NAMED_PORT(ALL) \ | |
5643 | OFPUTIL_NAMED_PORT(CONTROLLER) \ | |
5644 | OFPUTIL_NAMED_PORT(LOCAL) \ | |
7f05e7ab JR |
5645 | OFPUTIL_NAMED_PORT(ANY) |
5646 | ||
5647 | /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */ | |
5648 | #define OFPUTIL_NAMED_PORTS_WITH_NONE \ | |
5649 | OFPUTIL_NAMED_PORTS \ | |
39dc9082 BP |
5650 | OFPUTIL_NAMED_PORT(NONE) |
5651 | ||
8010100b BP |
5652 | /* Stores the port number represented by 's' into '*portp'. 's' may be an |
5653 | * integer or, for reserved ports, the standard OpenFlow name for the port | |
5654 | * (e.g. "LOCAL"). | |
c6100d92 | 5655 | * |
8010100b BP |
5656 | * Returns true if successful, false if 's' is not a valid OpenFlow port number |
5657 | * or name. The caller should issue an error message in this case, because | |
5658 | * this function usually does not. (This gives the caller an opportunity to | |
5659 | * look up the port name another way, e.g. by contacting the switch and listing | |
5660 | * the names of all its ports). | |
c6100d92 BP |
5661 | * |
5662 | * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset | |
5663 | * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit | |
5664 | * range as described in include/openflow/openflow-1.1.h. */ | |
8010100b | 5665 | bool |
4e022ec0 | 5666 | ofputil_port_from_string(const char *s, ofp_port_t *portp) |
39dc9082 | 5667 | { |
5b23ff7e | 5668 | unsigned int port32; /* int is at least 32 bits wide. */ |
c6100d92 | 5669 | |
5b23ff7e JR |
5670 | if (*s == '-') { |
5671 | VLOG_WARN("Negative value %s is not a valid port number.", s); | |
5672 | return false; | |
5673 | } | |
8010100b | 5674 | *portp = 0; |
c6100d92 | 5675 | if (str_to_uint(s, 10, &port32)) { |
4e022ec0 AW |
5676 | if (port32 < ofp_to_u16(OFPP_MAX)) { |
5677 | /* Pass. */ | |
5678 | } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) { | |
c6100d92 BP |
5679 | VLOG_WARN("port %u is a reserved OF1.0 port number that will " |
5680 | "be translated to %u when talking to an OF1.1 or " | |
5681 | "later controller", port32, port32 + OFPP11_OFFSET); | |
4e022ec0 | 5682 | } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) { |
28b11432 BP |
5683 | char name[OFP_MAX_PORT_NAME_LEN]; |
5684 | ||
5685 | ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name); | |
5686 | VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated " | |
5687 | "for compatibility with OpenFlow 1.1 and later", | |
5688 | name, port32); | |
4e022ec0 | 5689 | } else if (port32 < ofp11_to_u32(OFPP11_MAX)) { |
c6100d92 | 5690 | VLOG_WARN("port %u is outside the supported range 0 through " |
d047fd17 | 5691 | "%"PRIx16" or 0x%x through 0x%"PRIx32, port32, |
4e022ec0 | 5692 | UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX); |
8010100b | 5693 | return false; |
c6100d92 | 5694 | } else { |
4e022ec0 | 5695 | port32 -= OFPP11_OFFSET; |
c6100d92 | 5696 | } |
4e022ec0 AW |
5697 | |
5698 | *portp = u16_to_ofp(port32); | |
5699 | return true; | |
c6100d92 BP |
5700 | } else { |
5701 | struct pair { | |
5702 | const char *name; | |
4e022ec0 | 5703 | ofp_port_t value; |
c6100d92 BP |
5704 | }; |
5705 | static const struct pair pairs[] = { | |
39dc9082 | 5706 | #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME}, |
7f05e7ab | 5707 | OFPUTIL_NAMED_PORTS_WITH_NONE |
39dc9082 | 5708 | #undef OFPUTIL_NAMED_PORT |
c6100d92 BP |
5709 | }; |
5710 | const struct pair *p; | |
39dc9082 | 5711 | |
c6100d92 BP |
5712 | for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) { |
5713 | if (!strcasecmp(s, p->name)) { | |
8010100b BP |
5714 | *portp = p->value; |
5715 | return true; | |
c6100d92 | 5716 | } |
39dc9082 | 5717 | } |
8010100b | 5718 | return false; |
39dc9082 | 5719 | } |
39dc9082 BP |
5720 | } |
5721 | ||
5722 | /* Appends to 's' a string representation of the OpenFlow port number 'port'. | |
5723 | * Most ports' string representation is just the port number, but for special | |
5724 | * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */ | |
5725 | void | |
4e022ec0 | 5726 | ofputil_format_port(ofp_port_t port, struct ds *s) |
39dc9082 | 5727 | { |
28b11432 BP |
5728 | char name[OFP_MAX_PORT_NAME_LEN]; |
5729 | ||
5730 | ofputil_port_to_string(port, name, sizeof name); | |
5731 | ds_put_cstr(s, name); | |
5732 | } | |
39dc9082 | 5733 | |
28b11432 BP |
5734 | /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string |
5735 | * representation of OpenFlow port number 'port'. Most ports are represented | |
5736 | * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented | |
5737 | * by name, e.g. "LOCAL". */ | |
5738 | void | |
5739 | ofputil_port_to_string(ofp_port_t port, | |
5740 | char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize) | |
5741 | { | |
39dc9082 | 5742 | switch (port) { |
28b11432 BP |
5743 | #define OFPUTIL_NAMED_PORT(NAME) \ |
5744 | case OFPP_##NAME: \ | |
5745 | ovs_strlcpy(namebuf, #NAME, bufsize); \ | |
5746 | break; | |
39dc9082 BP |
5747 | OFPUTIL_NAMED_PORTS |
5748 | #undef OFPUTIL_NAMED_PORT | |
5749 | ||
5750 | default: | |
28b11432 BP |
5751 | snprintf(namebuf, bufsize, "%"PRIu16, port); |
5752 | break; | |
39dc9082 | 5753 | } |
39dc9082 BP |
5754 | } |
5755 | ||
7395c052 NZ |
5756 | /* Stores the group id represented by 's' into '*group_idp'. 's' may be an |
5757 | * integer or, for reserved group IDs, the standard OpenFlow name for the group | |
5758 | * (either "ANY" or "ALL"). | |
5759 | * | |
5760 | * Returns true if successful, false if 's' is not a valid OpenFlow group ID or | |
5761 | * name. */ | |
5762 | bool | |
5763 | ofputil_group_from_string(const char *s, uint32_t *group_idp) | |
5764 | { | |
5765 | if (!strcasecmp(s, "any")) { | |
5766 | *group_idp = OFPG11_ANY; | |
5767 | } else if (!strcasecmp(s, "all")) { | |
5768 | *group_idp = OFPG11_ALL; | |
5769 | } else if (!str_to_uint(s, 10, group_idp)) { | |
5770 | VLOG_WARN("%s is not a valid group ID. (Valid group IDs are " | |
5771 | "32-bit nonnegative integers or the keywords ANY or " | |
5772 | "ALL.)", s); | |
5773 | return false; | |
5774 | } | |
5775 | ||
5776 | return true; | |
5777 | } | |
5778 | ||
5779 | /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'. | |
5780 | * Most groups' string representation is just the number, but for special | |
5781 | * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */ | |
5782 | void | |
5783 | ofputil_format_group(uint32_t group_id, struct ds *s) | |
5784 | { | |
5785 | char name[MAX_GROUP_NAME_LEN]; | |
5786 | ||
5787 | ofputil_group_to_string(group_id, name, sizeof name); | |
5788 | ds_put_cstr(s, name); | |
5789 | } | |
5790 | ||
5791 | ||
5792 | /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string | |
5793 | * representation of OpenFlow group ID 'group_id'. Most group are represented | |
5794 | * as just their number, but special groups, e.g. OFPG11_ALL, are represented | |
5795 | * by name, e.g. "ALL". */ | |
5796 | void | |
5797 | ofputil_group_to_string(uint32_t group_id, | |
5798 | char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize) | |
5799 | { | |
5800 | switch (group_id) { | |
5801 | case OFPG11_ALL: | |
5802 | ovs_strlcpy(namebuf, "ALL", bufsize); | |
5803 | break; | |
5804 | ||
5805 | case OFPG11_ANY: | |
5806 | ovs_strlcpy(namebuf, "ANY", bufsize); | |
5807 | break; | |
5808 | ||
5809 | default: | |
5810 | snprintf(namebuf, bufsize, "%"PRIu32, group_id); | |
5811 | break; | |
5812 | } | |
5813 | } | |
5814 | ||
2be393ed JP |
5815 | /* Given a buffer 'b' that contains an array of OpenFlow ports of type |
5816 | * 'ofp_version', tries to pull the first element from the array. If | |
5817 | * successful, initializes '*pp' with an abstract representation of the | |
5818 | * port and returns 0. If no ports remain to be decoded, returns EOF. | |
5819 | * On an error, returns a positive OFPERR_* value. */ | |
5820 | int | |
2e3fa633 | 5821 | ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b, |
2be393ed JP |
5822 | struct ofputil_phy_port *pp) |
5823 | { | |
8c3cc785 BP |
5824 | memset(pp, 0, sizeof *pp); |
5825 | ||
2e3fa633 SH |
5826 | switch (ofp_version) { |
5827 | case OFP10_VERSION: { | |
2be393ed JP |
5828 | const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp); |
5829 | return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF; | |
2e3fa633 SH |
5830 | } |
5831 | case OFP11_VERSION: | |
2e1ae200 JR |
5832 | case OFP12_VERSION: |
5833 | case OFP13_VERSION: { | |
2be393ed JP |
5834 | const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op); |
5835 | return op ? ofputil_decode_ofp11_port(pp, op) : EOF; | |
5836 | } | |
c37c0382 | 5837 | case OFP14_VERSION: |
42dccab5 | 5838 | case OFP15_VERSION: |
8c3cc785 | 5839 | return ofpbuf_size(b) ? ofputil_pull_ofp14_port(pp, b) : EOF; |
2e3fa633 | 5840 | default: |
428b2edd | 5841 | OVS_NOT_REACHED(); |
2e3fa633 | 5842 | } |
2be393ed JP |
5843 | } |
5844 | ||
e3f8f887 JR |
5845 | /* ofp-util.def lists the mapping from names to action. */ |
5846 | static const char *const names[OFPUTIL_N_ACTIONS] = { | |
5847 | NULL, | |
3ddcaf2d BP |
5848 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME, |
5849 | #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, | |
9c4dbc1c | 5850 | #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, |
3ddcaf2d | 5851 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, |
e23ae585 | 5852 | #include "ofp-util.def" |
e3f8f887 | 5853 | }; |
e23ae585 | 5854 | |
e3f8f887 JR |
5855 | /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if |
5856 | * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 | |
5857 | * if 'name' is not the name of any action. */ | |
5858 | int | |
5859 | ofputil_action_code_from_name(const char *name) | |
5860 | { | |
a00d4bd0 | 5861 | const char *const *p; |
e23ae585 BP |
5862 | |
5863 | for (p = names; p < &names[ARRAY_SIZE(names)]; p++) { | |
5864 | if (*p && !strcasecmp(name, *p)) { | |
5865 | return p - names; | |
5866 | } | |
5867 | } | |
5868 | return -1; | |
5869 | } | |
5870 | ||
e3f8f887 JR |
5871 | /* Returns name corresponding to the 'enum ofputil_action_code', |
5872 | * or "Unkonwn action", if the name is not available. */ | |
5873 | const char * | |
5874 | ofputil_action_name_from_code(enum ofputil_action_code code) | |
5875 | { | |
5876 | return code < (int)OFPUTIL_N_ACTIONS && names[code] ? names[code] | |
5877 | : "Unknown action"; | |
5878 | } | |
5879 | ||
5deff5aa AW |
5880 | enum ofputil_action_code |
5881 | ofputil_action_code_from_ofp13_action(enum ofp13_action_type type) | |
5882 | { | |
5883 | switch (type) { | |
5884 | ||
5885 | #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
5886 | case ENUM: \ | |
5887 | return OFPUTIL_##ENUM; | |
5888 | #include "ofp-util.def" | |
5889 | ||
5890 | default: | |
5891 | return OFPUTIL_ACTION_INVALID; | |
5892 | } | |
5893 | } | |
5894 | ||
93996add BP |
5895 | /* Appends an action of the type specified by 'code' to 'buf' and returns the |
5896 | * action. Initializes the parts of 'action' that identify it as having type | |
5897 | * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that | |
5898 | * have variable length, the length used and cleared is that of struct | |
5899 | * <STRUCT>. */ | |
5900 | void * | |
5901 | ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf) | |
5902 | { | |
5903 | switch (code) { | |
690a61c5 | 5904 | case OFPUTIL_ACTION_INVALID: |
9c4dbc1c AW |
5905 | #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM: |
5906 | #include "ofp-util.def" | |
428b2edd | 5907 | OVS_NOT_REACHED(); |
690a61c5 | 5908 | |
3ddcaf2d BP |
5909 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ |
5910 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
5911 | #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
93996add BP |
5912 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); |
5913 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
5914 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
5915 | #include "ofp-util.def" | |
5916 | } | |
428b2edd | 5917 | OVS_NOT_REACHED(); |
93996add BP |
5918 | } |
5919 | ||
08f94c0e | 5920 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ |
93996add BP |
5921 | void \ |
5922 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
5923 | { \ | |
5924 | memset(s, 0, sizeof *s); \ | |
5925 | s->type = htons(ENUM); \ | |
5926 | s->len = htons(sizeof *s); \ | |
5927 | } \ | |
5928 | \ | |
5929 | struct STRUCT * \ | |
5930 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
5931 | { \ | |
5932 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
5933 | ofputil_init_##ENUM(s); \ | |
5934 | return s; \ | |
5935 | } | |
3ddcaf2d BP |
5936 | #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ |
5937 | OFPAT10_ACTION(ENUM, STRUCT, NAME) | |
9c4dbc1c AW |
5938 | #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ |
5939 | OFPAT10_ACTION(ENUM, STRUCT, NAME) | |
93996add BP |
5940 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ |
5941 | void \ | |
5942 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
5943 | { \ | |
5944 | memset(s, 0, sizeof *s); \ | |
08f94c0e | 5945 | s->type = htons(OFPAT10_VENDOR); \ |
93996add BP |
5946 | s->len = htons(sizeof *s); \ |
5947 | s->vendor = htonl(NX_VENDOR_ID); \ | |
5948 | s->subtype = htons(ENUM); \ | |
5949 | } \ | |
5950 | \ | |
5951 | struct STRUCT * \ | |
5952 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
5953 | { \ | |
5954 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
5955 | ofputil_init_##ENUM(s); \ | |
5956 | return s; \ | |
5957 | } | |
5958 | #include "ofp-util.def" | |
5959 | ||
3cbd9931 | 5960 | static void |
81a76618 | 5961 | ofputil_normalize_match__(struct match *match, bool may_log) |
b459a924 BP |
5962 | { |
5963 | enum { | |
5964 | MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */ | |
5965 | MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */ | |
5966 | MAY_NW_PROTO = 1 << 2, /* nw_proto */ | |
a61680c6 | 5967 | MAY_IPVx = 1 << 3, /* tos, frag, ttl */ |
b459a924 BP |
5968 | MAY_ARP_SHA = 1 << 4, /* arp_sha */ |
5969 | MAY_ARP_THA = 1 << 5, /* arp_tha */ | |
d78477ec | 5970 | MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */ |
b02475c5 SH |
5971 | MAY_ND_TARGET = 1 << 7, /* nd_target */ |
5972 | MAY_MPLS = 1 << 8, /* mpls label and tc */ | |
b459a924 BP |
5973 | } may_match; |
5974 | ||
5975 | struct flow_wildcards wc; | |
5976 | ||
5977 | /* Figure out what fields may be matched. */ | |
81a76618 | 5978 | if (match->flow.dl_type == htons(ETH_TYPE_IP)) { |
9e44d715 | 5979 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR; |
81a76618 BP |
5980 | if (match->flow.nw_proto == IPPROTO_TCP || |
5981 | match->flow.nw_proto == IPPROTO_UDP || | |
0d56eaf2 | 5982 | match->flow.nw_proto == IPPROTO_SCTP || |
81a76618 | 5983 | match->flow.nw_proto == IPPROTO_ICMP) { |
b459a924 BP |
5984 | may_match |= MAY_TP_ADDR; |
5985 | } | |
81a76618 | 5986 | } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) { |
d78477ec | 5987 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6; |
81a76618 | 5988 | if (match->flow.nw_proto == IPPROTO_TCP || |
0d56eaf2 JS |
5989 | match->flow.nw_proto == IPPROTO_UDP || |
5990 | match->flow.nw_proto == IPPROTO_SCTP) { | |
b459a924 | 5991 | may_match |= MAY_TP_ADDR; |
81a76618 | 5992 | } else if (match->flow.nw_proto == IPPROTO_ICMPV6) { |
b459a924 | 5993 | may_match |= MAY_TP_ADDR; |
81a76618 | 5994 | if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) { |
b459a924 | 5995 | may_match |= MAY_ND_TARGET | MAY_ARP_SHA; |
81a76618 | 5996 | } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) { |
b459a924 BP |
5997 | may_match |= MAY_ND_TARGET | MAY_ARP_THA; |
5998 | } | |
5999 | } | |
8087f5ff MM |
6000 | } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) || |
6001 | match->flow.dl_type == htons(ETH_TYPE_RARP)) { | |
27527aa0 | 6002 | may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA; |
b02475c5 SH |
6003 | } else if (eth_type_mpls(match->flow.dl_type)) { |
6004 | may_match = MAY_MPLS; | |
1c0b7503 | 6005 | } else { |
b459a924 BP |
6006 | may_match = 0; |
6007 | } | |
6008 | ||
6009 | /* Clear the fields that may not be matched. */ | |
81a76618 | 6010 | wc = match->wc; |
b459a924 | 6011 | if (!(may_match & MAY_NW_ADDR)) { |
26720e24 | 6012 | wc.masks.nw_src = wc.masks.nw_dst = htonl(0); |
b459a924 BP |
6013 | } |
6014 | if (!(may_match & MAY_TP_ADDR)) { | |
26720e24 | 6015 | wc.masks.tp_src = wc.masks.tp_dst = htons(0); |
b459a924 BP |
6016 | } |
6017 | if (!(may_match & MAY_NW_PROTO)) { | |
26720e24 | 6018 | wc.masks.nw_proto = 0; |
b459a924 | 6019 | } |
9e44d715 | 6020 | if (!(may_match & MAY_IPVx)) { |
26720e24 BP |
6021 | wc.masks.nw_tos = 0; |
6022 | wc.masks.nw_ttl = 0; | |
b459a924 BP |
6023 | } |
6024 | if (!(may_match & MAY_ARP_SHA)) { | |
26720e24 | 6025 | memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN); |
b459a924 BP |
6026 | } |
6027 | if (!(may_match & MAY_ARP_THA)) { | |
26720e24 | 6028 | memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN); |
b459a924 | 6029 | } |
d78477ec | 6030 | if (!(may_match & MAY_IPV6)) { |
26720e24 BP |
6031 | wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any; |
6032 | wc.masks.ipv6_label = htonl(0); | |
b459a924 BP |
6033 | } |
6034 | if (!(may_match & MAY_ND_TARGET)) { | |
26720e24 | 6035 | wc.masks.nd_target = in6addr_any; |
b459a924 | 6036 | } |
b02475c5 | 6037 | if (!(may_match & MAY_MPLS)) { |
8bfd0fda | 6038 | memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse); |
b02475c5 | 6039 | } |
b459a924 BP |
6040 | |
6041 | /* Log any changes. */ | |
81a76618 | 6042 | if (!flow_wildcards_equal(&wc, &match->wc)) { |
3cbd9931 | 6043 | bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl); |
81a76618 | 6044 | char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL; |
b459a924 | 6045 | |
81a76618 BP |
6046 | match->wc = wc; |
6047 | match_zero_wildcarded_fields(match); | |
b459a924 BP |
6048 | |
6049 | if (log) { | |
81a76618 | 6050 | char *post = match_to_string(match, OFP_DEFAULT_PRIORITY); |
b459a924 BP |
6051 | VLOG_INFO("normalization changed ofp_match, details:"); |
6052 | VLOG_INFO(" pre: %s", pre); | |
6053 | VLOG_INFO("post: %s", post); | |
6054 | free(pre); | |
6055 | free(post); | |
6056 | } | |
fa37b408 | 6057 | } |
3f09c339 | 6058 | } |
26c112c2 | 6059 | |
81a76618 | 6060 | /* "Normalizes" the wildcards in 'match'. That means: |
3cbd9931 BP |
6061 | * |
6062 | * 1. If the type of level N is known, then only the valid fields for that | |
6063 | * level may be specified. For example, ARP does not have a TOS field, | |
81a76618 | 6064 | * so nw_tos must be wildcarded if 'match' specifies an ARP flow. |
3cbd9931 | 6065 | * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and |
81a76618 | 6066 | * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an |
3cbd9931 BP |
6067 | * IPv4 flow. |
6068 | * | |
6069 | * 2. If the type of level N is not known (or not understood by Open | |
6070 | * vSwitch), then no fields at all for that level may be specified. For | |
6071 | * example, Open vSwitch does not understand SCTP, an L4 protocol, so the | |
81a76618 | 6072 | * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an |
3cbd9931 BP |
6073 | * SCTP flow. |
6074 | * | |
81a76618 | 6075 | * If this function changes 'match', it logs a rate-limited informational |
3cbd9931 BP |
6076 | * message. */ |
6077 | void | |
81a76618 | 6078 | ofputil_normalize_match(struct match *match) |
3cbd9931 | 6079 | { |
81a76618 | 6080 | ofputil_normalize_match__(match, true); |
3cbd9931 BP |
6081 | } |
6082 | ||
81a76618 BP |
6083 | /* Same as ofputil_normalize_match() without the logging. Thus, this function |
6084 | * is suitable for a program's internal use, whereas ofputil_normalize_match() | |
3cbd9931 BP |
6085 | * sense for use on flows received from elsewhere (so that a bug in the program |
6086 | * that sent them can be reported and corrected). */ | |
6087 | void | |
81a76618 | 6088 | ofputil_normalize_match_quiet(struct match *match) |
3cbd9931 | 6089 | { |
81a76618 | 6090 | ofputil_normalize_match__(match, false); |
3cbd9931 BP |
6091 | } |
6092 | ||
0ff22822 BP |
6093 | /* Parses a key or a key-value pair from '*stringp'. |
6094 | * | |
6095 | * On success: Stores the key into '*keyp'. Stores the value, if present, into | |
6096 | * '*valuep', otherwise an empty string. Advances '*stringp' past the end of | |
6097 | * the key-value pair, preparing it for another call. '*keyp' and '*valuep' | |
6098 | * are substrings of '*stringp' created by replacing some of its bytes by null | |
6099 | * terminators. Returns true. | |
6100 | * | |
6101 | * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to | |
6102 | * NULL and returns false. */ | |
6103 | bool | |
6104 | ofputil_parse_key_value(char **stringp, char **keyp, char **valuep) | |
6105 | { | |
6106 | char *pos, *key, *value; | |
6107 | size_t key_len; | |
6108 | ||
6109 | pos = *stringp; | |
6110 | pos += strspn(pos, ", \t\r\n"); | |
6111 | if (*pos == '\0') { | |
6112 | *keyp = *valuep = NULL; | |
6113 | return false; | |
6114 | } | |
6115 | ||
6116 | key = pos; | |
6117 | key_len = strcspn(pos, ":=(, \t\r\n"); | |
6118 | if (key[key_len] == ':' || key[key_len] == '=') { | |
6119 | /* The value can be separated by a colon. */ | |
6120 | size_t value_len; | |
6121 | ||
6122 | value = key + key_len + 1; | |
6123 | value_len = strcspn(value, ", \t\r\n"); | |
6124 | pos = value + value_len + (value[value_len] != '\0'); | |
6125 | value[value_len] = '\0'; | |
6126 | } else if (key[key_len] == '(') { | |
6127 | /* The value can be surrounded by balanced parentheses. The outermost | |
6128 | * set of parentheses is removed. */ | |
6129 | int level = 1; | |
6130 | size_t value_len; | |
6131 | ||
6132 | value = key + key_len + 1; | |
6133 | for (value_len = 0; level > 0; value_len++) { | |
6134 | switch (value[value_len]) { | |
6135 | case '\0': | |
33cadc50 BP |
6136 | level = 0; |
6137 | break; | |
0ff22822 BP |
6138 | |
6139 | case '(': | |
6140 | level++; | |
6141 | break; | |
6142 | ||
6143 | case ')': | |
6144 | level--; | |
6145 | break; | |
6146 | } | |
6147 | } | |
6148 | value[value_len - 1] = '\0'; | |
6149 | pos = value + value_len; | |
6150 | } else { | |
6151 | /* There might be no value at all. */ | |
6152 | value = key + key_len; /* Will become the empty string below. */ | |
6153 | pos = key + key_len + (key[key_len] != '\0'); | |
6154 | } | |
6155 | key[key_len] = '\0'; | |
6156 | ||
6157 | *stringp = pos; | |
6158 | *keyp = key; | |
6159 | *valuep = value; | |
6160 | return true; | |
6161 | } | |
f8e4867e SH |
6162 | |
6163 | /* Encode a dump ports request for 'port', the encoded message | |
2e1ae200 | 6164 | * will be for Open Flow version 'ofp_version'. Returns message |
f8e4867e SH |
6165 | * as a struct ofpbuf. Returns encoded message on success, NULL on error */ |
6166 | struct ofpbuf * | |
4e022ec0 | 6167 | ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port) |
f8e4867e SH |
6168 | { |
6169 | struct ofpbuf *request; | |
6170 | ||
6171 | switch (ofp_version) { | |
6172 | case OFP10_VERSION: { | |
6173 | struct ofp10_port_stats_request *req; | |
6174 | request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0); | |
6175 | req = ofpbuf_put_zeros(request, sizeof *req); | |
4e022ec0 | 6176 | req->port_no = htons(ofp_to_u16(port)); |
f8e4867e SH |
6177 | break; |
6178 | } | |
6179 | case OFP11_VERSION: | |
2e1ae200 | 6180 | case OFP12_VERSION: |
c37c0382 | 6181 | case OFP13_VERSION: |
42dccab5 BP |
6182 | case OFP14_VERSION: |
6183 | case OFP15_VERSION: { | |
f8e4867e SH |
6184 | struct ofp11_port_stats_request *req; |
6185 | request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0); | |
6186 | req = ofpbuf_put_zeros(request, sizeof *req); | |
6187 | req->port_no = ofputil_port_to_ofp11(port); | |
6188 | break; | |
6189 | } | |
6190 | default: | |
428b2edd | 6191 | OVS_NOT_REACHED(); |
f8e4867e SH |
6192 | } |
6193 | ||
6194 | return request; | |
6195 | } | |
6196 | ||
6197 | static void | |
6198 | ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops, | |
6199 | struct ofp10_port_stats *ps10) | |
6200 | { | |
4e022ec0 | 6201 | ps10->port_no = htons(ofp_to_u16(ops->port_no)); |
f8e4867e SH |
6202 | memset(ps10->pad, 0, sizeof ps10->pad); |
6203 | put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets)); | |
6204 | put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets)); | |
6205 | put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes)); | |
6206 | put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes)); | |
6207 | put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped)); | |
6208 | put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped)); | |
6209 | put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors)); | |
6210 | put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors)); | |
6211 | put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors)); | |
6212 | put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors)); | |
6213 | put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors)); | |
6214 | put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions)); | |
6215 | } | |
6216 | ||
6217 | static void | |
6218 | ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops, | |
6219 | struct ofp11_port_stats *ps11) | |
6220 | { | |
6221 | ps11->port_no = ofputil_port_to_ofp11(ops->port_no); | |
6222 | memset(ps11->pad, 0, sizeof ps11->pad); | |
6223 | ps11->rx_packets = htonll(ops->stats.rx_packets); | |
6224 | ps11->tx_packets = htonll(ops->stats.tx_packets); | |
6225 | ps11->rx_bytes = htonll(ops->stats.rx_bytes); | |
6226 | ps11->tx_bytes = htonll(ops->stats.tx_bytes); | |
6227 | ps11->rx_dropped = htonll(ops->stats.rx_dropped); | |
6228 | ps11->tx_dropped = htonll(ops->stats.tx_dropped); | |
6229 | ps11->rx_errors = htonll(ops->stats.rx_errors); | |
6230 | ps11->tx_errors = htonll(ops->stats.tx_errors); | |
6231 | ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors); | |
6232 | ps11->rx_over_err = htonll(ops->stats.rx_over_errors); | |
6233 | ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors); | |
6234 | ps11->collisions = htonll(ops->stats.collisions); | |
6235 | } | |
6236 | ||
2e1ae200 JR |
6237 | static void |
6238 | ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops, | |
6239 | struct ofp13_port_stats *ps13) | |
6240 | { | |
6241 | ofputil_port_stats_to_ofp11(ops, &ps13->ps); | |
65e0be10 BP |
6242 | ps13->duration_sec = htonl(ops->duration_sec); |
6243 | ps13->duration_nsec = htonl(ops->duration_nsec); | |
2e1ae200 JR |
6244 | } |
6245 | ||
5469537b BP |
6246 | static void |
6247 | ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops, | |
6248 | struct list *replies) | |
6249 | { | |
6250 | struct ofp14_port_stats_prop_ethernet *eth; | |
6251 | struct ofp14_port_stats *ps14; | |
6252 | struct ofpbuf *reply; | |
6253 | ||
6254 | reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth); | |
6255 | ||
6256 | ps14 = ofpbuf_put_uninit(reply, sizeof *ps14); | |
6257 | ps14->length = htons(sizeof *ps14 + sizeof *eth); | |
6258 | memset(ps14->pad, 0, sizeof ps14->pad); | |
6259 | ps14->port_no = ofputil_port_to_ofp11(ops->port_no); | |
6260 | ps14->duration_sec = htonl(ops->duration_sec); | |
6261 | ps14->duration_nsec = htonl(ops->duration_nsec); | |
6262 | ps14->rx_packets = htonll(ops->stats.rx_packets); | |
6263 | ps14->tx_packets = htonll(ops->stats.tx_packets); | |
6264 | ps14->rx_bytes = htonll(ops->stats.rx_bytes); | |
6265 | ps14->tx_bytes = htonll(ops->stats.tx_bytes); | |
6266 | ps14->rx_dropped = htonll(ops->stats.rx_dropped); | |
6267 | ps14->tx_dropped = htonll(ops->stats.tx_dropped); | |
6268 | ps14->rx_errors = htonll(ops->stats.rx_errors); | |
6269 | ps14->tx_errors = htonll(ops->stats.tx_errors); | |
6270 | ||
6271 | eth = ofpbuf_put_uninit(reply, sizeof *eth); | |
6272 | eth->type = htons(OFPPSPT14_ETHERNET); | |
6273 | eth->length = htons(sizeof *eth); | |
6274 | memset(eth->pad, 0, sizeof eth->pad); | |
6275 | eth->rx_frame_err = htonll(ops->stats.rx_frame_errors); | |
6276 | eth->rx_over_err = htonll(ops->stats.rx_over_errors); | |
6277 | eth->rx_crc_err = htonll(ops->stats.rx_crc_errors); | |
6278 | eth->collisions = htonll(ops->stats.collisions); | |
6279 | } | |
2e1ae200 | 6280 | |
ad4c35fe | 6281 | /* Encode a ports stat for 'ops' and append it to 'replies'. */ |
f8e4867e SH |
6282 | void |
6283 | ofputil_append_port_stat(struct list *replies, | |
6284 | const struct ofputil_port_stats *ops) | |
6285 | { | |
e28ac5cf | 6286 | switch (ofpmp_version(replies)) { |
2e1ae200 JR |
6287 | case OFP13_VERSION: { |
6288 | struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
6289 | ofputil_port_stats_to_ofp13(ops, reply); | |
6290 | break; | |
6291 | } | |
f8e4867e SH |
6292 | case OFP12_VERSION: |
6293 | case OFP11_VERSION: { | |
6294 | struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
6295 | ofputil_port_stats_to_ofp11(ops, reply); | |
6296 | break; | |
6297 | } | |
6298 | ||
6299 | case OFP10_VERSION: { | |
6300 | struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
6301 | ofputil_port_stats_to_ofp10(ops, reply); | |
6302 | break; | |
6303 | } | |
6304 | ||
c37c0382 | 6305 | case OFP14_VERSION: |
42dccab5 | 6306 | case OFP15_VERSION: |
5469537b | 6307 | ofputil_append_ofp14_port_stats(ops, replies); |
c37c0382 AC |
6308 | break; |
6309 | ||
f8e4867e | 6310 | default: |
428b2edd | 6311 | OVS_NOT_REACHED(); |
f8e4867e SH |
6312 | } |
6313 | } | |
6314 | ||
6315 | static enum ofperr | |
6316 | ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops, | |
6317 | const struct ofp10_port_stats *ps10) | |
6318 | { | |
6319 | memset(ops, 0, sizeof *ops); | |
6320 | ||
4e022ec0 | 6321 | ops->port_no = u16_to_ofp(ntohs(ps10->port_no)); |
f8e4867e SH |
6322 | ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets)); |
6323 | ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets)); | |
6324 | ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes)); | |
6325 | ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes)); | |
6326 | ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped)); | |
6327 | ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped)); | |
6328 | ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors)); | |
6329 | ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors)); | |
6330 | ops->stats.rx_frame_errors = | |
6331 | ntohll(get_32aligned_be64(&ps10->rx_frame_err)); | |
6332 | ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err)); | |
6333 | ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err)); | |
6334 | ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions)); | |
65e0be10 | 6335 | ops->duration_sec = ops->duration_nsec = UINT32_MAX; |
f8e4867e SH |
6336 | |
6337 | return 0; | |
6338 | } | |
6339 | ||
6340 | static enum ofperr | |
6341 | ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops, | |
6342 | const struct ofp11_port_stats *ps11) | |
6343 | { | |
6344 | enum ofperr error; | |
6345 | ||
6346 | memset(ops, 0, sizeof *ops); | |
6347 | error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no); | |
6348 | if (error) { | |
6349 | return error; | |
6350 | } | |
6351 | ||
6352 | ops->stats.rx_packets = ntohll(ps11->rx_packets); | |
6353 | ops->stats.tx_packets = ntohll(ps11->tx_packets); | |
6354 | ops->stats.rx_bytes = ntohll(ps11->rx_bytes); | |
6355 | ops->stats.tx_bytes = ntohll(ps11->tx_bytes); | |
6356 | ops->stats.rx_dropped = ntohll(ps11->rx_dropped); | |
6357 | ops->stats.tx_dropped = ntohll(ps11->tx_dropped); | |
6358 | ops->stats.rx_errors = ntohll(ps11->rx_errors); | |
6359 | ops->stats.tx_errors = ntohll(ps11->tx_errors); | |
6360 | ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err); | |
6361 | ops->stats.rx_over_errors = ntohll(ps11->rx_over_err); | |
6362 | ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err); | |
6363 | ops->stats.collisions = ntohll(ps11->collisions); | |
65e0be10 | 6364 | ops->duration_sec = ops->duration_nsec = UINT32_MAX; |
f8e4867e SH |
6365 | |
6366 | return 0; | |
6367 | } | |
6368 | ||
2e1ae200 JR |
6369 | static enum ofperr |
6370 | ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops, | |
6371 | const struct ofp13_port_stats *ps13) | |
6372 | { | |
65e0be10 | 6373 | enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps); |
2e1ae200 | 6374 | if (!error) { |
65e0be10 BP |
6375 | ops->duration_sec = ntohl(ps13->duration_sec); |
6376 | ops->duration_nsec = ntohl(ps13->duration_nsec); | |
2e1ae200 | 6377 | } |
2e1ae200 JR |
6378 | return error; |
6379 | } | |
6380 | ||
5469537b BP |
6381 | static enum ofperr |
6382 | parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload, | |
6383 | struct ofputil_port_stats *ops) | |
be0c30df | 6384 | { |
5469537b BP |
6385 | const struct ofp14_port_stats_prop_ethernet *eth = ofpbuf_data(payload); |
6386 | ||
6387 | if (ofpbuf_size(payload) != sizeof *eth) { | |
6388 | return OFPERR_OFPBPC_BAD_LEN; | |
6389 | } | |
6390 | ||
6391 | ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err); | |
6392 | ops->stats.rx_over_errors = ntohll(eth->rx_over_err); | |
6393 | ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err); | |
6394 | ops->stats.collisions = ntohll(eth->collisions); | |
6395 | ||
6396 | return 0; | |
6397 | } | |
6398 | ||
6399 | static enum ofperr | |
6400 | ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops, | |
6401 | struct ofpbuf *msg) | |
6402 | { | |
6403 | const struct ofp14_port_stats *ps14; | |
6404 | struct ofpbuf properties; | |
6405 | enum ofperr error; | |
6406 | size_t len; | |
6407 | ||
6408 | ps14 = ofpbuf_try_pull(msg, sizeof *ps14); | |
6409 | if (!ps14) { | |
6410 | return OFPERR_OFPBRC_BAD_LEN; | |
6411 | } | |
6412 | ||
6413 | len = ntohs(ps14->length); | |
6414 | if (len < sizeof *ps14 || len - sizeof *ps14 > ofpbuf_size(msg)) { | |
6415 | return OFPERR_OFPBRC_BAD_LEN; | |
be0c30df | 6416 | } |
5469537b BP |
6417 | len -= sizeof *ps14; |
6418 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len); | |
6419 | ||
6420 | error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no); | |
6421 | if (error) { | |
6422 | return error; | |
6423 | } | |
6424 | ||
6425 | ops->duration_sec = ntohl(ps14->duration_sec); | |
6426 | ops->duration_nsec = ntohl(ps14->duration_nsec); | |
6427 | ops->stats.rx_packets = ntohll(ps14->rx_packets); | |
6428 | ops->stats.tx_packets = ntohll(ps14->tx_packets); | |
6429 | ops->stats.rx_bytes = ntohll(ps14->rx_bytes); | |
6430 | ops->stats.tx_bytes = ntohll(ps14->tx_bytes); | |
6431 | ops->stats.rx_dropped = ntohll(ps14->rx_dropped); | |
6432 | ops->stats.tx_dropped = ntohll(ps14->tx_dropped); | |
6433 | ops->stats.rx_errors = ntohll(ps14->rx_errors); | |
6434 | ops->stats.tx_errors = ntohll(ps14->tx_errors); | |
6435 | ops->stats.rx_frame_errors = UINT64_MAX; | |
6436 | ops->stats.rx_over_errors = UINT64_MAX; | |
6437 | ops->stats.rx_crc_errors = UINT64_MAX; | |
6438 | ops->stats.collisions = UINT64_MAX; | |
6439 | ||
6440 | while (ofpbuf_size(&properties) > 0) { | |
6441 | struct ofpbuf payload; | |
6442 | enum ofperr error; | |
6443 | uint16_t type; | |
6444 | ||
6445 | error = ofputil_pull_property(&properties, &payload, &type); | |
6446 | if (error) { | |
6447 | return error; | |
6448 | } | |
6449 | ||
6450 | switch (type) { | |
6451 | case OFPPSPT14_ETHERNET: | |
6452 | error = parse_ofp14_port_stats_ethernet_property(&payload, ops); | |
6453 | break; | |
6454 | ||
6455 | default: | |
6456 | log_property(true, "unknown port stats property %"PRIu16, type); | |
6457 | error = 0; | |
6458 | break; | |
6459 | } | |
6460 | ||
6461 | if (error) { | |
6462 | return error; | |
6463 | } | |
6464 | } | |
6465 | ||
6466 | return 0; | |
be0c30df | 6467 | } |
2e1ae200 | 6468 | |
f8e4867e SH |
6469 | /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY |
6470 | * message 'oh'. */ | |
6471 | size_t | |
6472 | ofputil_count_port_stats(const struct ofp_header *oh) | |
6473 | { | |
5469537b | 6474 | struct ofputil_port_stats ps; |
f8e4867e | 6475 | struct ofpbuf b; |
5469537b | 6476 | size_t n = 0; |
f8e4867e SH |
6477 | |
6478 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
6479 | ofpraw_pull_assert(&b); | |
5469537b BP |
6480 | while (!ofputil_decode_port_stats(&ps, &b)) { |
6481 | n++; | |
6482 | } | |
6483 | return n; | |
f8e4867e SH |
6484 | } |
6485 | ||
6486 | /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract | |
6487 | * ofputil_port_stats in 'ps'. | |
6488 | * | |
6489 | * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow | |
6490 | * message. Calling this function multiple times for a single 'msg' iterates | |
6491 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
6492 | * null and not modify them between calls. | |
6493 | * | |
6494 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
6495 | * otherwise a positive errno value. */ | |
6496 | int | |
6497 | ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg) | |
6498 | { | |
6499 | enum ofperr error; | |
6500 | enum ofpraw raw; | |
6501 | ||
cf3b7538 JR |
6502 | error = (msg->frame |
6503 | ? ofpraw_decode(&raw, msg->frame) | |
f8e4867e SH |
6504 | : ofpraw_pull(&raw, msg)); |
6505 | if (error) { | |
6506 | return error; | |
6507 | } | |
6508 | ||
1f317cb5 | 6509 | if (!ofpbuf_size(msg)) { |
f8e4867e | 6510 | return EOF; |
5469537b BP |
6511 | } else if (raw == OFPRAW_OFPST14_PORT_REPLY) { |
6512 | return ofputil_pull_ofp14_port_stats(ps, msg); | |
2e1ae200 JR |
6513 | } else if (raw == OFPRAW_OFPST13_PORT_REPLY) { |
6514 | const struct ofp13_port_stats *ps13; | |
6515 | ||
6516 | ps13 = ofpbuf_try_pull(msg, sizeof *ps13); | |
6517 | if (!ps13) { | |
6518 | goto bad_len; | |
6519 | } | |
6520 | return ofputil_port_stats_from_ofp13(ps, ps13); | |
f8e4867e SH |
6521 | } else if (raw == OFPRAW_OFPST11_PORT_REPLY) { |
6522 | const struct ofp11_port_stats *ps11; | |
6523 | ||
6524 | ps11 = ofpbuf_try_pull(msg, sizeof *ps11); | |
6525 | if (!ps11) { | |
2e1ae200 | 6526 | goto bad_len; |
f8e4867e SH |
6527 | } |
6528 | return ofputil_port_stats_from_ofp11(ps, ps11); | |
6529 | } else if (raw == OFPRAW_OFPST10_PORT_REPLY) { | |
6530 | const struct ofp10_port_stats *ps10; | |
6531 | ||
6532 | ps10 = ofpbuf_try_pull(msg, sizeof *ps10); | |
6533 | if (!ps10) { | |
2e1ae200 | 6534 | goto bad_len; |
f8e4867e SH |
6535 | } |
6536 | return ofputil_port_stats_from_ofp10(ps, ps10); | |
6537 | } else { | |
428b2edd | 6538 | OVS_NOT_REACHED(); |
f8e4867e SH |
6539 | } |
6540 | ||
2e1ae200 | 6541 | bad_len: |
437d0d22 | 6542 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover " |
1f317cb5 | 6543 | "bytes at end", ofpbuf_size(msg)); |
2e1ae200 | 6544 | return OFPERR_OFPBRC_BAD_LEN; |
f8e4867e SH |
6545 | } |
6546 | ||
6547 | /* Parse a port status request message into a 16 bit OpenFlow 1.0 | |
6548 | * port number and stores the latter in '*ofp10_port'. | |
6549 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
6550 | enum ofperr | |
6551 | ofputil_decode_port_stats_request(const struct ofp_header *request, | |
4e022ec0 | 6552 | ofp_port_t *ofp10_port) |
f8e4867e SH |
6553 | { |
6554 | switch ((enum ofp_version)request->version) { | |
42dccab5 | 6555 | case OFP15_VERSION: |
5469537b | 6556 | case OFP14_VERSION: |
2e1ae200 | 6557 | case OFP13_VERSION: |
f8e4867e SH |
6558 | case OFP12_VERSION: |
6559 | case OFP11_VERSION: { | |
6560 | const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request); | |
6561 | return ofputil_port_from_ofp11(psr11->port_no, ofp10_port); | |
6562 | } | |
6563 | ||
6564 | case OFP10_VERSION: { | |
6565 | const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request); | |
4e022ec0 | 6566 | *ofp10_port = u16_to_ofp(ntohs(psr10->port_no)); |
f8e4867e SH |
6567 | return 0; |
6568 | } | |
6569 | ||
6570 | default: | |
428b2edd | 6571 | OVS_NOT_REACHED(); |
f8e4867e SH |
6572 | } |
6573 | } | |
64626975 | 6574 | |
7395c052 NZ |
6575 | /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */ |
6576 | void | |
6577 | ofputil_bucket_list_destroy(struct list *buckets) | |
6578 | { | |
6579 | struct ofputil_bucket *bucket, *next_bucket; | |
6580 | ||
6581 | LIST_FOR_EACH_SAFE (bucket, next_bucket, list_node, buckets) { | |
6582 | list_remove(&bucket->list_node); | |
6583 | free(bucket->ofpacts); | |
6584 | free(bucket); | |
6585 | } | |
6586 | } | |
6587 | ||
6588 | /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version', | |
6589 | * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats | |
6590 | * for all groups.) | |
6591 | * | |
6592 | * Group statistics include packet and byte counts for each group. */ | |
6593 | struct ofpbuf * | |
6594 | ofputil_encode_group_stats_request(enum ofp_version ofp_version, | |
6595 | uint32_t group_id) | |
6596 | { | |
6597 | struct ofpbuf *request; | |
6598 | ||
6599 | switch (ofp_version) { | |
6600 | case OFP10_VERSION: | |
6601 | ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later " | |
6602 | "(\'-O OpenFlow11\')"); | |
6603 | case OFP11_VERSION: | |
6604 | case OFP12_VERSION: | |
c37c0382 | 6605 | case OFP13_VERSION: |
42dccab5 BP |
6606 | case OFP14_VERSION: |
6607 | case OFP15_VERSION: { | |
7395c052 NZ |
6608 | struct ofp11_group_stats_request *req; |
6609 | request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0); | |
6610 | req = ofpbuf_put_zeros(request, sizeof *req); | |
6611 | req->group_id = htonl(group_id); | |
6612 | break; | |
6613 | } | |
6614 | default: | |
428b2edd | 6615 | OVS_NOT_REACHED(); |
7395c052 NZ |
6616 | } |
6617 | ||
6618 | return request; | |
6619 | } | |
6620 | ||
19187a71 BP |
6621 | /* Decodes the OpenFlow group description request in 'oh', returning the group |
6622 | * whose description is requested, or OFPG_ALL if stats for all groups was | |
6623 | * requested. */ | |
6624 | uint32_t | |
6625 | ofputil_decode_group_desc_request(const struct ofp_header *oh) | |
6626 | { | |
6627 | struct ofpbuf request; | |
6628 | enum ofpraw raw; | |
6629 | ||
6630 | ofpbuf_use_const(&request, oh, ntohs(oh->length)); | |
6631 | raw = ofpraw_pull_assert(&request); | |
6632 | if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) { | |
6633 | return OFPG_ALL; | |
6634 | } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) { | |
6635 | ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id); | |
6636 | return ntohl(*group_id); | |
6637 | } else { | |
6638 | OVS_NOT_REACHED(); | |
6639 | } | |
6640 | } | |
6641 | ||
7395c052 | 6642 | /* Returns an OpenFlow group description request for OpenFlow version |
19187a71 BP |
6643 | * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to |
6644 | * request stats for all groups (OpenFlow 1.4 and earlier always request all | |
6645 | * groups). | |
7395c052 NZ |
6646 | * |
6647 | * Group descriptions include the bucket and action configuration for each | |
6648 | * group. */ | |
6649 | struct ofpbuf * | |
19187a71 BP |
6650 | ofputil_encode_group_desc_request(enum ofp_version ofp_version, |
6651 | uint32_t group_id) | |
7395c052 NZ |
6652 | { |
6653 | struct ofpbuf *request; | |
19187a71 | 6654 | ovs_be32 gid; |
7395c052 NZ |
6655 | |
6656 | switch (ofp_version) { | |
6657 | case OFP10_VERSION: | |
6658 | ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later " | |
6659 | "(\'-O OpenFlow11\')"); | |
6660 | case OFP11_VERSION: | |
6661 | case OFP12_VERSION: | |
c37c0382 AC |
6662 | case OFP13_VERSION: |
6663 | case OFP14_VERSION: | |
19187a71 BP |
6664 | request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST, |
6665 | ofp_version, 0); | |
6666 | break; | |
42dccab5 | 6667 | case OFP15_VERSION: |
19187a71 BP |
6668 | request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST, |
6669 | ofp_version, 0); | |
6670 | gid = htonl(group_id); | |
6671 | ofpbuf_put(request, &gid, sizeof gid); | |
7395c052 | 6672 | break; |
7395c052 | 6673 | default: |
428b2edd | 6674 | OVS_NOT_REACHED(); |
7395c052 NZ |
6675 | } |
6676 | ||
6677 | return request; | |
6678 | } | |
6679 | ||
dcbe78ad | 6680 | static void |
63759e71 AZ |
6681 | ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs, |
6682 | struct ofp11_bucket_counter bucket_cnts[]) | |
7395c052 | 6683 | { |
7395c052 NZ |
6684 | int i; |
6685 | ||
dcbe78ad AZ |
6686 | for (i = 0; i < gs->n_buckets; i++) { |
6687 | bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count); | |
6688 | bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count); | |
7395c052 | 6689 | } |
7395c052 NZ |
6690 | } |
6691 | ||
6692 | static void | |
dcbe78ad | 6693 | ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs, |
63759e71 AZ |
6694 | struct ofp11_group_stats *gs11, size_t length, |
6695 | struct ofp11_bucket_counter bucket_cnts[]) | |
7395c052 | 6696 | { |
63759e71 AZ |
6697 | memset(gs11, 0, sizeof *gs11); |
6698 | gs11->length = htons(length); | |
6699 | gs11->group_id = htonl(gs->group_id); | |
6700 | gs11->ref_count = htonl(gs->ref_count); | |
6701 | gs11->packet_count = htonll(gs->packet_count); | |
6702 | gs11->byte_count = htonll(gs->byte_count); | |
6703 | ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts); | |
dcbe78ad | 6704 | } |
7395c052 | 6705 | |
dcbe78ad AZ |
6706 | static void |
6707 | ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs, | |
63759e71 AZ |
6708 | struct ofp13_group_stats *gs13, size_t length, |
6709 | struct ofp11_bucket_counter bucket_cnts[]) | |
dcbe78ad | 6710 | { |
63759e71 | 6711 | ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts); |
dcbe78ad AZ |
6712 | gs13->duration_sec = htonl(gs->duration_sec); |
6713 | gs13->duration_nsec = htonl(gs->duration_nsec); | |
63759e71 | 6714 | |
7395c052 NZ |
6715 | } |
6716 | ||
dcbe78ad | 6717 | /* Encodes 'gs' properly for the format of the list of group statistics |
7395c052 NZ |
6718 | * replies already begun in 'replies' and appends it to the list. 'replies' |
6719 | * must have originally been initialized with ofpmp_init(). */ | |
6720 | void | |
6721 | ofputil_append_group_stats(struct list *replies, | |
dcbe78ad | 6722 | const struct ofputil_group_stats *gs) |
7395c052 | 6723 | { |
63759e71 AZ |
6724 | size_t bucket_counter_size; |
6725 | struct ofp11_bucket_counter *bucket_counters; | |
dcbe78ad | 6726 | size_t length; |
7395c052 | 6727 | |
63759e71 AZ |
6728 | bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter); |
6729 | ||
e28ac5cf | 6730 | switch (ofpmp_version(replies)) { |
7395c052 | 6731 | case OFP11_VERSION: |
dcbe78ad | 6732 | case OFP12_VERSION:{ |
63759e71 | 6733 | struct ofp11_group_stats *gs11; |
dcbe78ad | 6734 | |
63759e71 AZ |
6735 | length = sizeof *gs11 + bucket_counter_size; |
6736 | gs11 = ofpmp_append(replies, length); | |
6737 | bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1); | |
6738 | ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters); | |
dcbe78ad AZ |
6739 | break; |
6740 | } | |
7395c052 NZ |
6741 | |
6742 | case OFP13_VERSION: | |
42dccab5 BP |
6743 | case OFP14_VERSION: |
6744 | case OFP15_VERSION: { | |
63759e71 | 6745 | struct ofp13_group_stats *gs13; |
7395c052 | 6746 | |
63759e71 AZ |
6747 | length = sizeof *gs13 + bucket_counter_size; |
6748 | gs13 = ofpmp_append(replies, length); | |
6749 | bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1); | |
6750 | ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters); | |
dcbe78ad AZ |
6751 | break; |
6752 | } | |
c37c0382 | 6753 | |
7395c052 NZ |
6754 | case OFP10_VERSION: |
6755 | default: | |
428b2edd | 6756 | OVS_NOT_REACHED(); |
7395c052 NZ |
6757 | } |
6758 | } | |
7395c052 NZ |
6759 | /* Returns an OpenFlow group features request for OpenFlow version |
6760 | * 'ofp_version'. */ | |
6761 | struct ofpbuf * | |
6762 | ofputil_encode_group_features_request(enum ofp_version ofp_version) | |
6763 | { | |
6764 | struct ofpbuf *request = NULL; | |
6765 | ||
6766 | switch (ofp_version) { | |
6767 | case OFP10_VERSION: | |
6768 | case OFP11_VERSION: | |
6769 | ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later " | |
6770 | "(\'-O OpenFlow12\')"); | |
6771 | case OFP12_VERSION: | |
c37c0382 AC |
6772 | case OFP13_VERSION: |
6773 | case OFP14_VERSION: | |
42dccab5 | 6774 | case OFP15_VERSION: |
7395c052 | 6775 | request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST, |
c37c0382 | 6776 | ofp_version, 0); |
7395c052 | 6777 | break; |
7395c052 | 6778 | default: |
428b2edd | 6779 | OVS_NOT_REACHED(); |
7395c052 NZ |
6780 | } |
6781 | ||
6782 | return request; | |
6783 | } | |
6784 | ||
6785 | /* Returns a OpenFlow message that encodes 'features' properly as a reply to | |
6786 | * group features request 'request'. */ | |
6787 | struct ofpbuf * | |
6788 | ofputil_encode_group_features_reply( | |
6789 | const struct ofputil_group_features *features, | |
6790 | const struct ofp_header *request) | |
6791 | { | |
6792 | struct ofp12_group_features_stats *ogf; | |
6793 | struct ofpbuf *reply; | |
6794 | ||
6795 | reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY, | |
6796 | request->version, request->xid, 0); | |
6797 | ogf = ofpbuf_put_zeros(reply, sizeof *ogf); | |
6798 | ogf->types = htonl(features->types); | |
6799 | ogf->capabilities = htonl(features->capabilities); | |
6800 | ogf->max_groups[0] = htonl(features->max_groups[0]); | |
6801 | ogf->max_groups[1] = htonl(features->max_groups[1]); | |
6802 | ogf->max_groups[2] = htonl(features->max_groups[2]); | |
6803 | ogf->max_groups[3] = htonl(features->max_groups[3]); | |
6804 | ogf->actions[0] = htonl(features->actions[0]); | |
6805 | ogf->actions[1] = htonl(features->actions[1]); | |
6806 | ogf->actions[2] = htonl(features->actions[2]); | |
6807 | ogf->actions[3] = htonl(features->actions[3]); | |
6808 | ||
6809 | return reply; | |
6810 | } | |
6811 | ||
6812 | /* Decodes group features reply 'oh' into 'features'. */ | |
6813 | void | |
6814 | ofputil_decode_group_features_reply(const struct ofp_header *oh, | |
6815 | struct ofputil_group_features *features) | |
6816 | { | |
6817 | const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh); | |
6818 | ||
6819 | features->types = ntohl(ogf->types); | |
6820 | features->capabilities = ntohl(ogf->capabilities); | |
6821 | features->max_groups[0] = ntohl(ogf->max_groups[0]); | |
6822 | features->max_groups[1] = ntohl(ogf->max_groups[1]); | |
6823 | features->max_groups[2] = ntohl(ogf->max_groups[2]); | |
6824 | features->max_groups[3] = ntohl(ogf->max_groups[3]); | |
6825 | features->actions[0] = ntohl(ogf->actions[0]); | |
6826 | features->actions[1] = ntohl(ogf->actions[1]); | |
6827 | features->actions[2] = ntohl(ogf->actions[2]); | |
6828 | features->actions[3] = ntohl(ogf->actions[3]); | |
6829 | } | |
6830 | ||
6831 | /* Parse a group status request message into a 32 bit OpenFlow 1.1 | |
6832 | * group ID and stores the latter in '*group_id'. | |
6833 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
6834 | enum ofperr | |
6835 | ofputil_decode_group_stats_request(const struct ofp_header *request, | |
6836 | uint32_t *group_id) | |
6837 | { | |
6838 | const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request); | |
6839 | *group_id = ntohl(gsr11->group_id); | |
6840 | return 0; | |
6841 | } | |
6842 | ||
6843 | /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats | |
646b2a9c SH |
6844 | * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the |
6845 | * caller to eventually free. | |
7395c052 NZ |
6846 | * |
6847 | * Multiple group stats replies can be packed into a single OpenFlow message. | |
6848 | * Calling this function multiple times for a single 'msg' iterates through the | |
6849 | * replies. The caller must initially leave 'msg''s layer pointers null and | |
6850 | * not modify them between calls. | |
6851 | * | |
6852 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
6853 | * otherwise a positive errno value. */ | |
6854 | int | |
6855 | ofputil_decode_group_stats_reply(struct ofpbuf *msg, | |
6856 | struct ofputil_group_stats *gs) | |
6857 | { | |
6858 | struct ofp11_bucket_counter *obc; | |
6859 | struct ofp11_group_stats *ogs11; | |
6860 | enum ofpraw raw; | |
6861 | enum ofperr error; | |
6862 | size_t base_len; | |
6863 | size_t length; | |
6864 | size_t i; | |
6865 | ||
646b2a9c | 6866 | gs->bucket_stats = NULL; |
cf3b7538 JR |
6867 | error = (msg->frame |
6868 | ? ofpraw_decode(&raw, msg->frame) | |
7395c052 NZ |
6869 | : ofpraw_pull(&raw, msg)); |
6870 | if (error) { | |
6871 | return error; | |
6872 | } | |
6873 | ||
1f317cb5 | 6874 | if (!ofpbuf_size(msg)) { |
7395c052 NZ |
6875 | return EOF; |
6876 | } | |
6877 | ||
6878 | if (raw == OFPRAW_OFPST11_GROUP_REPLY) { | |
6879 | base_len = sizeof *ogs11; | |
6880 | ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11); | |
6881 | gs->duration_sec = gs->duration_nsec = UINT32_MAX; | |
6882 | } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) { | |
6883 | struct ofp13_group_stats *ogs13; | |
6884 | ||
6885 | base_len = sizeof *ogs13; | |
6886 | ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13); | |
6887 | if (ogs13) { | |
6888 | ogs11 = &ogs13->gs; | |
6889 | gs->duration_sec = ntohl(ogs13->duration_sec); | |
6890 | gs->duration_nsec = ntohl(ogs13->duration_nsec); | |
6891 | } else { | |
6892 | ogs11 = NULL; | |
6893 | } | |
6894 | } else { | |
428b2edd | 6895 | OVS_NOT_REACHED(); |
7395c052 NZ |
6896 | } |
6897 | ||
6898 | if (!ogs11) { | |
437d0d22 | 6899 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end", |
1f317cb5 | 6900 | ofpraw_get_name(raw), ofpbuf_size(msg)); |
7395c052 NZ |
6901 | return OFPERR_OFPBRC_BAD_LEN; |
6902 | } | |
6903 | length = ntohs(ogs11->length); | |
6904 | if (length < sizeof base_len) { | |
34582733 | 6905 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE, |
7395c052 NZ |
6906 | ofpraw_get_name(raw), length); |
6907 | return OFPERR_OFPBRC_BAD_LEN; | |
6908 | } | |
6909 | ||
6910 | gs->group_id = ntohl(ogs11->group_id); | |
6911 | gs->ref_count = ntohl(ogs11->ref_count); | |
6912 | gs->packet_count = ntohll(ogs11->packet_count); | |
6913 | gs->byte_count = ntohll(ogs11->byte_count); | |
6914 | ||
6915 | gs->n_buckets = (length - base_len) / sizeof *obc; | |
6916 | obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc); | |
6917 | if (!obc) { | |
437d0d22 | 6918 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end", |
1f317cb5 | 6919 | ofpraw_get_name(raw), ofpbuf_size(msg)); |
7395c052 NZ |
6920 | return OFPERR_OFPBRC_BAD_LEN; |
6921 | } | |
6922 | ||
646b2a9c | 6923 | gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats); |
7395c052 NZ |
6924 | for (i = 0; i < gs->n_buckets; i++) { |
6925 | gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count); | |
6926 | gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count); | |
6927 | } | |
6928 | ||
6929 | return 0; | |
6930 | } | |
6931 | ||
6932 | /* Appends a group stats reply that contains the data in 'gds' to those already | |
6933 | * present in the list of ofpbufs in 'replies'. 'replies' should have been | |
6934 | * initialized with ofpmp_init(). */ | |
6935 | void | |
6936 | ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds, | |
6937 | struct list *buckets, | |
6938 | struct list *replies) | |
6939 | { | |
6940 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); | |
e28ac5cf | 6941 | enum ofp_version version = ofpmp_version(replies); |
7395c052 NZ |
6942 | struct ofp11_group_desc_stats *ogds; |
6943 | struct ofputil_bucket *bucket; | |
6944 | size_t start_ogds; | |
6945 | ||
1f317cb5 | 6946 | start_ogds = ofpbuf_size(reply); |
7395c052 NZ |
6947 | ofpbuf_put_zeros(reply, sizeof *ogds); |
6948 | LIST_FOR_EACH (bucket, list_node, buckets) { | |
6949 | struct ofp11_bucket *ob; | |
6950 | size_t start_ob; | |
6951 | ||
1f317cb5 | 6952 | start_ob = ofpbuf_size(reply); |
7395c052 | 6953 | ofpbuf_put_zeros(reply, sizeof *ob); |
e3f8f887 JR |
6954 | ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len, |
6955 | reply, version); | |
7395c052 | 6956 | ob = ofpbuf_at_assert(reply, start_ob, sizeof *ob); |
1f317cb5 | 6957 | ob->len = htons(ofpbuf_size(reply) - start_ob); |
7395c052 NZ |
6958 | ob->weight = htons(bucket->weight); |
6959 | ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port); | |
6960 | ob->watch_group = htonl(bucket->watch_group); | |
6961 | } | |
6962 | ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds); | |
1f317cb5 | 6963 | ogds->length = htons(ofpbuf_size(reply) - start_ogds); |
7395c052 NZ |
6964 | ogds->type = gds->type; |
6965 | ogds->group_id = htonl(gds->group_id); | |
6966 | ||
6967 | ofpmp_postappend(replies, start_ogds); | |
6968 | } | |
6969 | ||
6970 | static enum ofperr | |
e3f8f887 JR |
6971 | ofputil_pull_buckets(struct ofpbuf *msg, size_t buckets_length, |
6972 | enum ofp_version version, struct list *buckets) | |
7395c052 NZ |
6973 | { |
6974 | struct ofp11_bucket *ob; | |
6975 | ||
6976 | list_init(buckets); | |
6977 | while (buckets_length > 0) { | |
6978 | struct ofputil_bucket *bucket; | |
6979 | struct ofpbuf ofpacts; | |
6980 | enum ofperr error; | |
6981 | size_t ob_len; | |
6982 | ||
6983 | ob = (buckets_length >= sizeof *ob | |
6984 | ? ofpbuf_try_pull(msg, sizeof *ob) | |
6985 | : NULL); | |
6986 | if (!ob) { | |
34582733 | 6987 | VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes", |
7395c052 NZ |
6988 | buckets_length); |
6989 | } | |
6990 | ||
6991 | ob_len = ntohs(ob->len); | |
6992 | if (ob_len < sizeof *ob) { | |
6993 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length " | |
34582733 | 6994 | "%"PRIuSIZE" is not valid", ob_len); |
7395c052 NZ |
6995 | return OFPERR_OFPGMFC_BAD_BUCKET; |
6996 | } else if (ob_len > buckets_length) { | |
6997 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length " | |
34582733 | 6998 | "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE, |
7395c052 NZ |
6999 | ob_len, buckets_length); |
7000 | return OFPERR_OFPGMFC_BAD_BUCKET; | |
7001 | } | |
7002 | buckets_length -= ob_len; | |
7003 | ||
7004 | ofpbuf_init(&ofpacts, 0); | |
e3f8f887 JR |
7005 | error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob, |
7006 | version, &ofpacts); | |
7395c052 NZ |
7007 | if (error) { |
7008 | ofpbuf_uninit(&ofpacts); | |
7009 | ofputil_bucket_list_destroy(buckets); | |
7010 | return error; | |
7011 | } | |
7012 | ||
7013 | bucket = xzalloc(sizeof *bucket); | |
7014 | bucket->weight = ntohs(ob->weight); | |
7015 | error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port); | |
7016 | if (error) { | |
7017 | ofpbuf_uninit(&ofpacts); | |
7018 | ofputil_bucket_list_destroy(buckets); | |
7019 | return OFPERR_OFPGMFC_BAD_WATCH; | |
7020 | } | |
7021 | bucket->watch_group = ntohl(ob->watch_group); | |
7022 | bucket->ofpacts = ofpbuf_steal_data(&ofpacts); | |
1f317cb5 | 7023 | bucket->ofpacts_len = ofpbuf_size(&ofpacts); |
7395c052 NZ |
7024 | list_push_back(buckets, &bucket->list_node); |
7025 | } | |
7026 | ||
7027 | return 0; | |
7028 | } | |
7029 | ||
7030 | /* Converts a group description reply in 'msg' into an abstract | |
7031 | * ofputil_group_desc in 'gd'. | |
7032 | * | |
7033 | * Multiple group description replies can be packed into a single OpenFlow | |
7034 | * message. Calling this function multiple times for a single 'msg' iterates | |
7035 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
7036 | * null and not modify them between calls. | |
7037 | * | |
7038 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
7039 | * otherwise a positive errno value. */ | |
7040 | int | |
7041 | ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd, | |
a7a2d006 | 7042 | struct ofpbuf *msg, enum ofp_version version) |
7395c052 NZ |
7043 | { |
7044 | struct ofp11_group_desc_stats *ogds; | |
7045 | size_t length; | |
7046 | ||
cf3b7538 | 7047 | if (!msg->frame) { |
7395c052 NZ |
7048 | ofpraw_pull_assert(msg); |
7049 | } | |
7050 | ||
1f317cb5 | 7051 | if (!ofpbuf_size(msg)) { |
7395c052 NZ |
7052 | return EOF; |
7053 | } | |
7054 | ||
7055 | ogds = ofpbuf_try_pull(msg, sizeof *ogds); | |
7056 | if (!ogds) { | |
437d0d22 | 7057 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" " |
1f317cb5 | 7058 | "leftover bytes at end", ofpbuf_size(msg)); |
7395c052 NZ |
7059 | return OFPERR_OFPBRC_BAD_LEN; |
7060 | } | |
7061 | gd->type = ogds->type; | |
7062 | gd->group_id = ntohl(ogds->group_id); | |
7063 | ||
7064 | length = ntohs(ogds->length); | |
1f317cb5 | 7065 | if (length < sizeof *ogds || length - sizeof *ogds > ofpbuf_size(msg)) { |
7395c052 | 7066 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid " |
34582733 | 7067 | "length %"PRIuSIZE, length); |
7395c052 NZ |
7068 | return OFPERR_OFPBRC_BAD_LEN; |
7069 | } | |
7070 | ||
e3f8f887 | 7071 | return ofputil_pull_buckets(msg, length - sizeof *ogds, version, |
a7a2d006 | 7072 | &gd->buckets); |
7395c052 NZ |
7073 | } |
7074 | ||
7075 | /* Converts abstract group mod 'gm' into a message for OpenFlow version | |
7076 | * 'ofp_version' and returns the message. */ | |
7077 | struct ofpbuf * | |
7078 | ofputil_encode_group_mod(enum ofp_version ofp_version, | |
7079 | const struct ofputil_group_mod *gm) | |
7080 | { | |
7081 | struct ofpbuf *b; | |
7082 | struct ofp11_group_mod *ogm; | |
7083 | size_t start_ogm; | |
7084 | size_t start_bucket; | |
7085 | struct ofputil_bucket *bucket; | |
7086 | struct ofp11_bucket *ob; | |
7087 | ||
7088 | switch (ofp_version) { | |
7089 | case OFP10_VERSION: { | |
7090 | if (gm->command == OFPGC11_ADD) { | |
7091 | ovs_fatal(0, "add-group needs OpenFlow 1.1 or later " | |
7092 | "(\'-O OpenFlow11\')"); | |
7093 | } else if (gm->command == OFPGC11_MODIFY) { | |
7094 | ovs_fatal(0, "mod-group needs OpenFlow 1.1 or later " | |
7095 | "(\'-O OpenFlow11\')"); | |
7096 | } else { | |
7097 | ovs_fatal(0, "del-groups needs OpenFlow 1.1 or later " | |
7098 | "(\'-O OpenFlow11\')"); | |
7099 | } | |
7100 | } | |
7101 | ||
7102 | case OFP11_VERSION: | |
7103 | case OFP12_VERSION: | |
c37c0382 AC |
7104 | case OFP13_VERSION: |
7105 | case OFP14_VERSION: | |
42dccab5 | 7106 | case OFP15_VERSION: |
7395c052 | 7107 | b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0); |
1f317cb5 | 7108 | start_ogm = ofpbuf_size(b); |
2134b5ec | 7109 | ofpbuf_put_zeros(b, sizeof *ogm); |
7395c052 NZ |
7110 | |
7111 | LIST_FOR_EACH (bucket, list_node, &gm->buckets) { | |
1f317cb5 | 7112 | start_bucket = ofpbuf_size(b); |
2134b5ec | 7113 | ofpbuf_put_zeros(b, sizeof *ob); |
7395c052 | 7114 | if (bucket->ofpacts && bucket->ofpacts_len) { |
e3f8f887 JR |
7115 | ofpacts_put_openflow_actions(bucket->ofpacts, |
7116 | bucket->ofpacts_len, b, | |
7117 | ofp_version); | |
7395c052 NZ |
7118 | } |
7119 | ob = ofpbuf_at_assert(b, start_bucket, sizeof *ob); | |
1f317cb5 | 7120 | ob->len = htons(ofpbuf_size(b) - start_bucket);; |
7395c052 NZ |
7121 | ob->weight = htons(bucket->weight); |
7122 | ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port); | |
7123 | ob->watch_group = htonl(bucket->watch_group); | |
7124 | } | |
7125 | ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm); | |
7126 | ogm->command = htons(gm->command); | |
7127 | ogm->type = gm->type; | |
7395c052 NZ |
7128 | ogm->group_id = htonl(gm->group_id); |
7129 | ||
7130 | break; | |
7395c052 NZ |
7131 | |
7132 | default: | |
428b2edd | 7133 | OVS_NOT_REACHED(); |
7395c052 NZ |
7134 | } |
7135 | ||
7136 | return b; | |
7137 | } | |
7138 | ||
7139 | /* Converts OpenFlow group mod message 'oh' into an abstract group mod in | |
7140 | * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */ | |
7141 | enum ofperr | |
7142 | ofputil_decode_group_mod(const struct ofp_header *oh, | |
7143 | struct ofputil_group_mod *gm) | |
7144 | { | |
7145 | const struct ofp11_group_mod *ogm; | |
7146 | struct ofpbuf msg; | |
e57681e5 SH |
7147 | struct ofputil_bucket *bucket; |
7148 | enum ofperr err; | |
7395c052 NZ |
7149 | |
7150 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
7151 | ofpraw_pull_assert(&msg); | |
7152 | ||
7153 | ogm = ofpbuf_pull(&msg, sizeof *ogm); | |
7154 | gm->command = ntohs(ogm->command); | |
7155 | gm->type = ogm->type; | |
7156 | gm->group_id = ntohl(ogm->group_id); | |
7157 | ||
1f317cb5 | 7158 | err = ofputil_pull_buckets(&msg, ofpbuf_size(&msg), oh->version, &gm->buckets); |
e57681e5 SH |
7159 | if (err) { |
7160 | return err; | |
7161 | } | |
7162 | ||
7163 | LIST_FOR_EACH (bucket, list_node, &gm->buckets) { | |
7164 | switch (gm->type) { | |
7165 | case OFPGT11_ALL: | |
7166 | case OFPGT11_INDIRECT: | |
7167 | if (ofputil_bucket_has_liveness(bucket)) { | |
7168 | return OFPERR_OFPGMFC_WATCH_UNSUPPORTED; | |
7169 | } | |
7170 | break; | |
7171 | case OFPGT11_SELECT: | |
7172 | break; | |
7173 | case OFPGT11_FF: | |
7174 | if (!ofputil_bucket_has_liveness(bucket)) { | |
7175 | return OFPERR_OFPGMFC_INVALID_GROUP; | |
7176 | } | |
7177 | break; | |
7178 | default: | |
428b2edd | 7179 | OVS_NOT_REACHED(); |
e57681e5 SH |
7180 | } |
7181 | } | |
7182 | ||
7183 | return 0; | |
7395c052 NZ |
7184 | } |
7185 | ||
64626975 SH |
7186 | /* Parse a queue status request message into 'oqsr'. |
7187 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
7188 | enum ofperr | |
7189 | ofputil_decode_queue_stats_request(const struct ofp_header *request, | |
7190 | struct ofputil_queue_stats_request *oqsr) | |
7191 | { | |
7192 | switch ((enum ofp_version)request->version) { | |
42dccab5 | 7193 | case OFP15_VERSION: |
c37c0382 | 7194 | case OFP14_VERSION: |
2e1ae200 | 7195 | case OFP13_VERSION: |
64626975 SH |
7196 | case OFP12_VERSION: |
7197 | case OFP11_VERSION: { | |
7198 | const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request); | |
7199 | oqsr->queue_id = ntohl(qsr11->queue_id); | |
7200 | return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no); | |
7201 | } | |
7202 | ||
7203 | case OFP10_VERSION: { | |
7f05e7ab JR |
7204 | const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request); |
7205 | oqsr->queue_id = ntohl(qsr10->queue_id); | |
4e022ec0 | 7206 | oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no)); |
7f05e7ab JR |
7207 | /* OF 1.0 uses OFPP_ALL for OFPP_ANY */ |
7208 | if (oqsr->port_no == OFPP_ALL) { | |
7209 | oqsr->port_no = OFPP_ANY; | |
7210 | } | |
64626975 SH |
7211 | return 0; |
7212 | } | |
7213 | ||
7214 | default: | |
428b2edd | 7215 | OVS_NOT_REACHED(); |
64626975 SH |
7216 | } |
7217 | } | |
7218 | ||
7219 | /* Encode a queue statsrequest for 'oqsr', the encoded message | |
7220 | * will be fore Open Flow version 'ofp_version'. Returns message | |
7221 | * as a struct ofpbuf. Returns encoded message on success, NULL on error */ | |
7222 | struct ofpbuf * | |
7223 | ofputil_encode_queue_stats_request(enum ofp_version ofp_version, | |
7224 | const struct ofputil_queue_stats_request *oqsr) | |
7225 | { | |
7226 | struct ofpbuf *request; | |
7227 | ||
7228 | switch (ofp_version) { | |
7229 | case OFP11_VERSION: | |
2e1ae200 | 7230 | case OFP12_VERSION: |
c37c0382 | 7231 | case OFP13_VERSION: |
42dccab5 BP |
7232 | case OFP14_VERSION: |
7233 | case OFP15_VERSION: { | |
64626975 SH |
7234 | struct ofp11_queue_stats_request *req; |
7235 | request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0); | |
7236 | req = ofpbuf_put_zeros(request, sizeof *req); | |
7237 | req->port_no = ofputil_port_to_ofp11(oqsr->port_no); | |
7238 | req->queue_id = htonl(oqsr->queue_id); | |
7239 | break; | |
7240 | } | |
7241 | case OFP10_VERSION: { | |
7242 | struct ofp10_queue_stats_request *req; | |
7243 | request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0); | |
7244 | req = ofpbuf_put_zeros(request, sizeof *req); | |
7f05e7ab | 7245 | /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */ |
4e022ec0 AW |
7246 | req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY |
7247 | ? OFPP_ALL : oqsr->port_no)); | |
64626975 SH |
7248 | req->queue_id = htonl(oqsr->queue_id); |
7249 | break; | |
7250 | } | |
7251 | default: | |
428b2edd | 7252 | OVS_NOT_REACHED(); |
64626975 SH |
7253 | } |
7254 | ||
7255 | return request; | |
7256 | } | |
7257 | ||
7258 | /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY | |
7259 | * message 'oh'. */ | |
7260 | size_t | |
7261 | ofputil_count_queue_stats(const struct ofp_header *oh) | |
7262 | { | |
1bb2cdbe | 7263 | struct ofputil_queue_stats qs; |
64626975 | 7264 | struct ofpbuf b; |
1bb2cdbe | 7265 | size_t n = 0; |
64626975 SH |
7266 | |
7267 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
7268 | ofpraw_pull_assert(&b); | |
1bb2cdbe BP |
7269 | while (!ofputil_decode_queue_stats(&qs, &b)) { |
7270 | n++; | |
7271 | } | |
7272 | return n; | |
64626975 SH |
7273 | } |
7274 | ||
7275 | static enum ofperr | |
7276 | ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs, | |
7277 | const struct ofp10_queue_stats *qs10) | |
7278 | { | |
4e022ec0 | 7279 | oqs->port_no = u16_to_ofp(ntohs(qs10->port_no)); |
64626975 | 7280 | oqs->queue_id = ntohl(qs10->queue_id); |
6dc34a0d BP |
7281 | oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes)); |
7282 | oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets)); | |
7283 | oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors)); | |
7284 | oqs->duration_sec = oqs->duration_nsec = UINT32_MAX; | |
64626975 SH |
7285 | |
7286 | return 0; | |
7287 | } | |
7288 | ||
7289 | static enum ofperr | |
7290 | ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs, | |
7291 | const struct ofp11_queue_stats *qs11) | |
7292 | { | |
7293 | enum ofperr error; | |
7294 | ||
7295 | error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no); | |
7296 | if (error) { | |
7297 | return error; | |
7298 | } | |
7299 | ||
7300 | oqs->queue_id = ntohl(qs11->queue_id); | |
6dc34a0d BP |
7301 | oqs->tx_bytes = ntohll(qs11->tx_bytes); |
7302 | oqs->tx_packets = ntohll(qs11->tx_packets); | |
7303 | oqs->tx_errors = ntohll(qs11->tx_errors); | |
7304 | oqs->duration_sec = oqs->duration_nsec = UINT32_MAX; | |
64626975 SH |
7305 | |
7306 | return 0; | |
7307 | } | |
7308 | ||
2e1ae200 JR |
7309 | static enum ofperr |
7310 | ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs, | |
7311 | const struct ofp13_queue_stats *qs13) | |
7312 | { | |
6dc34a0d | 7313 | enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs); |
2e1ae200 | 7314 | if (!error) { |
6dc34a0d BP |
7315 | oqs->duration_sec = ntohl(qs13->duration_sec); |
7316 | oqs->duration_nsec = ntohl(qs13->duration_nsec); | |
2e1ae200 JR |
7317 | } |
7318 | ||
7319 | return error; | |
7320 | } | |
7321 | ||
1bb2cdbe BP |
7322 | static enum ofperr |
7323 | ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs, | |
7324 | struct ofpbuf *msg) | |
7325 | { | |
7326 | const struct ofp14_queue_stats *qs14; | |
7327 | size_t len; | |
7328 | ||
7329 | qs14 = ofpbuf_try_pull(msg, sizeof *qs14); | |
7330 | if (!qs14) { | |
7331 | return OFPERR_OFPBRC_BAD_LEN; | |
7332 | } | |
7333 | ||
7334 | len = ntohs(qs14->length); | |
7335 | if (len < sizeof *qs14 || len - sizeof *qs14 > ofpbuf_size(msg)) { | |
7336 | return OFPERR_OFPBRC_BAD_LEN; | |
7337 | } | |
7338 | ofpbuf_pull(msg, len - sizeof *qs14); | |
7339 | ||
7340 | /* No properties yet defined, so ignore them for now. */ | |
7341 | ||
7342 | return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs); | |
7343 | } | |
7344 | ||
64626975 SH |
7345 | /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract |
7346 | * ofputil_queue_stats in 'qs'. | |
7347 | * | |
7348 | * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow | |
7349 | * message. Calling this function multiple times for a single 'msg' iterates | |
7350 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
7351 | * null and not modify them between calls. | |
7352 | * | |
7353 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
7354 | * otherwise a positive errno value. */ | |
7355 | int | |
7356 | ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg) | |
7357 | { | |
7358 | enum ofperr error; | |
7359 | enum ofpraw raw; | |
7360 | ||
cf3b7538 JR |
7361 | error = (msg->frame |
7362 | ? ofpraw_decode(&raw, msg->frame) | |
64626975 SH |
7363 | : ofpraw_pull(&raw, msg)); |
7364 | if (error) { | |
7365 | return error; | |
7366 | } | |
7367 | ||
1f317cb5 | 7368 | if (!ofpbuf_size(msg)) { |
64626975 | 7369 | return EOF; |
1bb2cdbe BP |
7370 | } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) { |
7371 | return ofputil_pull_ofp14_queue_stats(qs, msg); | |
2e1ae200 JR |
7372 | } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) { |
7373 | const struct ofp13_queue_stats *qs13; | |
7374 | ||
7375 | qs13 = ofpbuf_try_pull(msg, sizeof *qs13); | |
7376 | if (!qs13) { | |
7377 | goto bad_len; | |
7378 | } | |
7379 | return ofputil_queue_stats_from_ofp13(qs, qs13); | |
64626975 SH |
7380 | } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) { |
7381 | const struct ofp11_queue_stats *qs11; | |
7382 | ||
7383 | qs11 = ofpbuf_try_pull(msg, sizeof *qs11); | |
7384 | if (!qs11) { | |
2e1ae200 | 7385 | goto bad_len; |
64626975 SH |
7386 | } |
7387 | return ofputil_queue_stats_from_ofp11(qs, qs11); | |
7388 | } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) { | |
7389 | const struct ofp10_queue_stats *qs10; | |
7390 | ||
7391 | qs10 = ofpbuf_try_pull(msg, sizeof *qs10); | |
7392 | if (!qs10) { | |
2e1ae200 | 7393 | goto bad_len; |
64626975 SH |
7394 | } |
7395 | return ofputil_queue_stats_from_ofp10(qs, qs10); | |
7396 | } else { | |
428b2edd | 7397 | OVS_NOT_REACHED(); |
64626975 | 7398 | } |
2e1ae200 JR |
7399 | |
7400 | bad_len: | |
437d0d22 | 7401 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover " |
1f317cb5 | 7402 | "bytes at end", ofpbuf_size(msg)); |
2e1ae200 | 7403 | return OFPERR_OFPBRC_BAD_LEN; |
64626975 SH |
7404 | } |
7405 | ||
7406 | static void | |
7407 | ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs, | |
7408 | struct ofp10_queue_stats *qs10) | |
7409 | { | |
4e022ec0 | 7410 | qs10->port_no = htons(ofp_to_u16(oqs->port_no)); |
64626975 SH |
7411 | memset(qs10->pad, 0, sizeof qs10->pad); |
7412 | qs10->queue_id = htonl(oqs->queue_id); | |
6dc34a0d BP |
7413 | put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes)); |
7414 | put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets)); | |
7415 | put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors)); | |
64626975 SH |
7416 | } |
7417 | ||
7418 | static void | |
7419 | ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs, | |
7420 | struct ofp11_queue_stats *qs11) | |
7421 | { | |
7422 | qs11->port_no = ofputil_port_to_ofp11(oqs->port_no); | |
7423 | qs11->queue_id = htonl(oqs->queue_id); | |
6dc34a0d BP |
7424 | qs11->tx_bytes = htonll(oqs->tx_bytes); |
7425 | qs11->tx_packets = htonll(oqs->tx_packets); | |
7426 | qs11->tx_errors = htonll(oqs->tx_errors); | |
64626975 SH |
7427 | } |
7428 | ||
2e1ae200 JR |
7429 | static void |
7430 | ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs, | |
7431 | struct ofp13_queue_stats *qs13) | |
7432 | { | |
7433 | ofputil_queue_stats_to_ofp11(oqs, &qs13->qs); | |
6dc34a0d BP |
7434 | if (oqs->duration_sec != UINT32_MAX) { |
7435 | qs13->duration_sec = htonl(oqs->duration_sec); | |
7436 | qs13->duration_nsec = htonl(oqs->duration_nsec); | |
7437 | } else { | |
b8266395 BP |
7438 | qs13->duration_sec = OVS_BE32_MAX; |
7439 | qs13->duration_nsec = OVS_BE32_MAX; | |
6dc34a0d | 7440 | } |
2e1ae200 JR |
7441 | } |
7442 | ||
1bb2cdbe BP |
7443 | static void |
7444 | ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs, | |
7445 | struct ofp14_queue_stats *qs14) | |
7446 | { | |
7447 | qs14->length = htons(sizeof *qs14); | |
7448 | memset(qs14->pad, 0, sizeof qs14->pad); | |
7449 | ofputil_queue_stats_to_ofp13(oqs, &qs14->qs); | |
7450 | } | |
7451 | ||
7452 | ||
64626975 SH |
7453 | /* Encode a queue stat for 'oqs' and append it to 'replies'. */ |
7454 | void | |
7455 | ofputil_append_queue_stat(struct list *replies, | |
7456 | const struct ofputil_queue_stats *oqs) | |
7457 | { | |
e28ac5cf | 7458 | switch (ofpmp_version(replies)) { |
2e1ae200 JR |
7459 | case OFP13_VERSION: { |
7460 | struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply); | |
7461 | ofputil_queue_stats_to_ofp13(oqs, reply); | |
7462 | break; | |
7463 | } | |
7464 | ||
64626975 SH |
7465 | case OFP12_VERSION: |
7466 | case OFP11_VERSION: { | |
2e1ae200 | 7467 | struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply); |
64626975 SH |
7468 | ofputil_queue_stats_to_ofp11(oqs, reply); |
7469 | break; | |
7470 | } | |
7471 | ||
7472 | case OFP10_VERSION: { | |
2e1ae200 | 7473 | struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply); |
64626975 SH |
7474 | ofputil_queue_stats_to_ofp10(oqs, reply); |
7475 | break; | |
7476 | } | |
7477 | ||
42dccab5 BP |
7478 | case OFP14_VERSION: |
7479 | case OFP15_VERSION: { | |
1bb2cdbe BP |
7480 | struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply); |
7481 | ofputil_queue_stats_to_ofp14(oqs, reply); | |
c37c0382 | 7482 | break; |
1bb2cdbe | 7483 | } |
c37c0382 | 7484 | |
64626975 | 7485 | default: |
428b2edd | 7486 | OVS_NOT_REACHED(); |
64626975 SH |
7487 | } |
7488 | } | |
777af88d AC |
7489 | |
7490 | enum ofperr | |
7491 | ofputil_decode_bundle_ctrl(const struct ofp_header *oh, | |
7492 | struct ofputil_bundle_ctrl_msg *msg) | |
7493 | { | |
7494 | struct ofpbuf b; | |
7495 | enum ofpraw raw; | |
7496 | const struct ofp14_bundle_ctrl_msg *m; | |
7497 | ||
7498 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
7499 | raw = ofpraw_pull_assert(&b); | |
7500 | ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL); | |
7501 | ||
7502 | m = ofpbuf_l3(&b); | |
7503 | msg->bundle_id = ntohl(m->bundle_id); | |
7504 | msg->type = ntohs(m->type); | |
7505 | msg->flags = ntohs(m->flags); | |
7506 | ||
7507 | return 0; | |
7508 | } | |
7509 | ||
7510 | struct ofpbuf * | |
7511 | ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh, | |
7512 | struct ofputil_bundle_ctrl_msg *msg) | |
7513 | { | |
7514 | struct ofpbuf *buf; | |
7515 | struct ofp14_bundle_ctrl_msg *m; | |
7516 | ||
7517 | buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0); | |
7518 | m = ofpbuf_put_zeros(buf, sizeof *m); | |
7519 | ||
7520 | m->bundle_id = htonl(msg->bundle_id); | |
7521 | m->type = htons(msg->type); | |
7522 | m->flags = htons(msg->flags); | |
7523 | ||
7524 | return buf; | |
7525 | } | |
7526 | ||
7527 | enum ofperr | |
7528 | ofputil_decode_bundle_add(const struct ofp_header *oh, | |
7529 | struct ofputil_bundle_add_msg *msg) | |
7530 | { | |
7531 | const struct ofp14_bundle_ctrl_msg *m; | |
7532 | struct ofpbuf b; | |
7533 | enum ofpraw raw; | |
7534 | size_t inner_len; | |
7535 | ||
7536 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
7537 | raw = ofpraw_pull_assert(&b); | |
7538 | ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE); | |
7539 | ||
7540 | m = ofpbuf_pull(&b, sizeof *m); | |
7541 | msg->bundle_id = ntohl(m->bundle_id); | |
7542 | msg->flags = ntohs(m->flags); | |
7543 | ||
7544 | msg->msg = ofpbuf_data(&b); | |
7545 | inner_len = ntohs(msg->msg->length); | |
7546 | if (inner_len < sizeof(struct ofp_header) || inner_len > ofpbuf_size(&b)) { | |
7547 | return OFPERR_OFPBFC_MSG_BAD_LEN; | |
7548 | } | |
7549 | ||
7550 | return 0; | |
7551 | } | |
7552 | ||
7553 | struct ofpbuf * | |
7554 | ofputil_encode_bundle_add(enum ofp_version ofp_version, | |
7555 | struct ofputil_bundle_add_msg *msg) | |
7556 | { | |
7557 | struct ofpbuf *request; | |
7558 | struct ofp14_bundle_ctrl_msg *m; | |
7559 | ||
7560 | request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version, 0); | |
7561 | m = ofpbuf_put_zeros(request, sizeof *m); | |
7562 | ||
7563 | m->bundle_id = htonl(msg->bundle_id); | |
7564 | m->flags = htons(msg->flags); | |
7565 | ofpbuf_put(request, msg->msg, ntohs(msg->msg->length)); | |
7566 | ||
7567 | return request; | |
7568 | } |