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