]>
Commit | Line | Data |
---|---|---|
fa37b408 | 1 | /* |
18080541 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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" |
18ac06d3 | 35 | #include "id-pool.h" |
f25d0cf3 | 36 | #include "ofp-actions.h" |
dc4762ed | 37 | #include "ofp-errors.h" |
982697a4 | 38 | #include "ofp-msgs.h" |
fa37b408 BP |
39 | #include "ofp-util.h" |
40 | #include "ofpbuf.h" | |
bc65c25a | 41 | #include "openflow/netronome-ext.h" |
fa37b408 BP |
42 | #include "packets.h" |
43 | #include "random.h" | |
6159c531 | 44 | #include "tun-metadata.h" |
4ffd1b43 | 45 | #include "unaligned.h" |
e41a9130 | 46 | #include "type-props.h" |
e6211adc | 47 | #include "openvswitch/vlog.h" |
5deff5aa | 48 | #include "bitmap.h" |
fa37b408 | 49 | |
d98e6007 | 50 | VLOG_DEFINE_THIS_MODULE(ofp_util); |
fa37b408 BP |
51 | |
52 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
53 | * in the peer and so there's not much point in showing a lot of them. */ | |
54 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
55 | ||
82c22d34 BP |
56 | static enum ofputil_table_eviction ofputil_decode_table_eviction( |
57 | ovs_be32 config, enum ofp_version); | |
58 | static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss, | |
59 | enum ofputil_table_eviction, | |
60 | enum ofp_version); | |
3c1bb396 | 61 | |
9a4eddbb BP |
62 | struct ofp_prop_header { |
63 | ovs_be16 type; | |
64 | ovs_be16 len; | |
65 | }; | |
66 | ||
bc65c25a SH |
67 | struct ofp_prop_experimenter { |
68 | ovs_be16 type; /* OFP*_EXPERIMENTER. */ | |
69 | ovs_be16 length; /* Length in bytes of this property. */ | |
70 | ovs_be32 experimenter; /* Experimenter ID which takes the same form as | |
71 | * in struct ofp_experimenter_header. */ | |
72 | ovs_be32 exp_type; /* Experimenter defined. */ | |
73 | }; | |
74 | ||
9a4eddbb BP |
75 | /* Pulls a property, beginning with struct ofp_prop_header, from the beginning |
76 | * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is | |
77 | * nonnull, the entire property, including the header, in '*property'. Returns | |
72b25867 BP |
78 | * 0 if successful, otherwise an error code. |
79 | * | |
80 | * This function pulls the property's stated size padded out to a multiple of | |
81 | * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so | |
82 | * you can use ofputil_pull_property() for that case. */ | |
9a4eddbb | 83 | static enum ofperr |
72b25867 BP |
84 | ofputil_pull_property__(struct ofpbuf *msg, struct ofpbuf *property, |
85 | unsigned int alignment, uint16_t *typep) | |
9a4eddbb BP |
86 | { |
87 | struct ofp_prop_header *oph; | |
72b25867 | 88 | unsigned int padded_len; |
9a4eddbb BP |
89 | unsigned int len; |
90 | ||
6fd6ed71 | 91 | if (msg->size < sizeof *oph) { |
9a4eddbb BP |
92 | return OFPERR_OFPBPC_BAD_LEN; |
93 | } | |
94 | ||
6fd6ed71 | 95 | oph = msg->data; |
9a4eddbb | 96 | len = ntohs(oph->len); |
72b25867 | 97 | padded_len = ROUND_UP(len, alignment); |
6fd6ed71 | 98 | if (len < sizeof *oph || padded_len > msg->size) { |
9a4eddbb BP |
99 | return OFPERR_OFPBPC_BAD_LEN; |
100 | } | |
101 | ||
102 | *typep = ntohs(oph->type); | |
103 | if (property) { | |
6fd6ed71 | 104 | ofpbuf_use_const(property, msg->data, len); |
9a4eddbb | 105 | } |
72b25867 | 106 | ofpbuf_pull(msg, padded_len); |
9a4eddbb BP |
107 | return 0; |
108 | } | |
109 | ||
72b25867 BP |
110 | /* Pulls a property, beginning with struct ofp_prop_header, from the beginning |
111 | * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is | |
112 | * nonnull, the entire property, including the header, in '*property'. Returns | |
113 | * 0 if successful, otherwise an error code. | |
114 | * | |
115 | * This function pulls the property's stated size padded out to a multiple of | |
116 | * 8 bytes, which is the common case for OpenFlow properties. */ | |
117 | static enum ofperr | |
118 | ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property, | |
119 | uint16_t *typep) | |
120 | { | |
121 | return ofputil_pull_property__(msg, property, 8, typep); | |
122 | } | |
123 | ||
cab50449 | 124 | static void OVS_PRINTF_FORMAT(2, 3) |
9a4eddbb BP |
125 | log_property(bool loose, const char *message, ...) |
126 | { | |
127 | enum vlog_level level = loose ? VLL_DBG : VLL_WARN; | |
128 | if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) { | |
129 | va_list args; | |
130 | ||
131 | va_start(args, message); | |
132 | vlog_valist(THIS_MODULE, level, message, args); | |
133 | va_end(args); | |
134 | } | |
135 | } | |
8c3cc785 | 136 | |
3c4e10fb BP |
137 | static size_t |
138 | start_property(struct ofpbuf *msg, uint16_t type) | |
139 | { | |
6fd6ed71 | 140 | size_t start_ofs = msg->size; |
3c4e10fb BP |
141 | struct ofp_prop_header *oph; |
142 | ||
143 | oph = ofpbuf_put_uninit(msg, sizeof *oph); | |
144 | oph->type = htons(type); | |
145 | oph->len = htons(4); /* May be updated later by end_property(). */ | |
146 | return start_ofs; | |
147 | } | |
148 | ||
149 | static void | |
150 | end_property(struct ofpbuf *msg, size_t start_ofs) | |
151 | { | |
152 | struct ofp_prop_header *oph; | |
153 | ||
154 | oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph); | |
6fd6ed71 PS |
155 | oph->len = htons(msg->size - start_ofs); |
156 | ofpbuf_padto(msg, ROUND_UP(msg->size, 8)); | |
3c4e10fb BP |
157 | } |
158 | ||
159 | static void | |
160 | put_bitmap_properties(struct ofpbuf *msg, uint64_t bitmap) | |
161 | { | |
162 | for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) { | |
163 | start_property(msg, rightmost_1bit_idx(bitmap)); | |
164 | } | |
165 | } | |
166 | ||
0596e897 BP |
167 | /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns |
168 | * an IP netmask with a 1 in each bit that must match and a 0 in each bit that | |
169 | * is wildcarded. | |
170 | * | |
171 | * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0 | |
172 | * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, | |
173 | * ..., 32 and higher wildcard the entire field. This is the *opposite* of the | |
174 | * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are | |
175 | * wildcarded. */ | |
176 | ovs_be32 | |
177 | ofputil_wcbits_to_netmask(int wcbits) | |
178 | { | |
179 | wcbits &= 0x3f; | |
180 | return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0; | |
181 | } | |
182 | ||
183 | /* Given the IP netmask 'netmask', returns the number of bits of the IP address | |
c08201d6 BP |
184 | * that it wildcards, that is, the number of 0-bits in 'netmask', a number |
185 | * between 0 and 32 inclusive. | |
186 | * | |
187 | * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will | |
188 | * still be in the valid range but isn't otherwise meaningful. */ | |
0596e897 BP |
189 | int |
190 | ofputil_netmask_to_wcbits(ovs_be32 netmask) | |
191 | { | |
aad29cd1 | 192 | return 32 - ip_count_cidr_bits(netmask); |
0596e897 BP |
193 | } |
194 | ||
eec25dc1 | 195 | /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a |
81a76618 | 196 | * flow_wildcards in 'wc' for use in struct match. It is the caller's |
eec25dc1 BP |
197 | * responsibility to handle the special case where the flow match's dl_vlan is |
198 | * set to OFP_VLAN_NONE. */ | |
7286b1e1 | 199 | void |
eec25dc1 | 200 | ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc) |
d8ae4d67 | 201 | { |
b666962b | 202 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33); |
a877206f | 203 | |
81a76618 | 204 | /* Initialize most of wc. */ |
f9ba8dad | 205 | flow_wildcards_init_catchall(wc); |
bad68a99 | 206 | |
0bdc4bec | 207 | if (!(ofpfw & OFPFW10_IN_PORT)) { |
4e022ec0 | 208 | wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX); |
27cafc5f | 209 | } |
5d9499c4 BP |
210 | |
211 | if (!(ofpfw & OFPFW10_NW_TOS)) { | |
26720e24 | 212 | wc->masks.nw_tos |= IP_DSCP_MASK; |
d8ae4d67 | 213 | } |
7257b535 | 214 | |
851d3105 | 215 | if (!(ofpfw & OFPFW10_NW_PROTO)) { |
26720e24 | 216 | wc->masks.nw_proto = UINT8_MAX; |
851d3105 | 217 | } |
26720e24 BP |
218 | wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw |
219 | >> OFPFW10_NW_SRC_SHIFT); | |
220 | wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw | |
221 | >> OFPFW10_NW_DST_SHIFT); | |
d8ae4d67 | 222 | |
eec25dc1 | 223 | if (!(ofpfw & OFPFW10_TP_SRC)) { |
b8266395 | 224 | wc->masks.tp_src = OVS_BE16_MAX; |
73f33563 | 225 | } |
eec25dc1 | 226 | if (!(ofpfw & OFPFW10_TP_DST)) { |
b8266395 | 227 | wc->masks.tp_dst = OVS_BE16_MAX; |
73f33563 BP |
228 | } |
229 | ||
eec25dc1 | 230 | if (!(ofpfw & OFPFW10_DL_SRC)) { |
26720e24 | 231 | memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN); |
73c0ce34 | 232 | } |
eec25dc1 | 233 | if (!(ofpfw & OFPFW10_DL_DST)) { |
26720e24 | 234 | memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN); |
d8ae4d67 | 235 | } |
e2170cff | 236 | if (!(ofpfw & OFPFW10_DL_TYPE)) { |
b8266395 | 237 | wc->masks.dl_type = OVS_BE16_MAX; |
e2170cff | 238 | } |
d8ae4d67 | 239 | |
eb6f28db | 240 | /* VLAN TCI mask. */ |
eec25dc1 | 241 | if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) { |
26720e24 | 242 | wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI); |
eb6f28db | 243 | } |
eec25dc1 | 244 | if (!(ofpfw & OFPFW10_DL_VLAN)) { |
26720e24 | 245 | wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); |
eb6f28db BP |
246 | } |
247 | } | |
248 | ||
81a76618 | 249 | /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */ |
eb6f28db | 250 | void |
81a76618 BP |
251 | ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch, |
252 | struct match *match) | |
253 | { | |
254 | uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL; | |
255 | ||
256 | /* Initialize match->wc. */ | |
296e07ac | 257 | memset(&match->flow, 0, sizeof match->flow); |
81a76618 BP |
258 | ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc); |
259 | ||
260 | /* Initialize most of match->flow. */ | |
261 | match->flow.nw_src = ofmatch->nw_src; | |
262 | match->flow.nw_dst = ofmatch->nw_dst; | |
4e022ec0 | 263 | match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port)); |
81a76618 BP |
264 | match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type); |
265 | match->flow.tp_src = ofmatch->tp_src; | |
266 | match->flow.tp_dst = ofmatch->tp_dst; | |
267 | memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN); | |
268 | memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN); | |
269 | match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK; | |
270 | match->flow.nw_proto = ofmatch->nw_proto; | |
d8ae4d67 | 271 | |
66642cb4 | 272 | /* Translate VLANs. */ |
0c436519 | 273 | if (!(ofpfw & OFPFW10_DL_VLAN) && |
81a76618 | 274 | ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) { |
47271d0d BP |
275 | /* Match only packets without 802.1Q header. |
276 | * | |
eec25dc1 | 277 | * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct. |
47271d0d | 278 | * |
eec25dc1 | 279 | * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory, |
47271d0d BP |
280 | * because we can't have a specific PCP without an 802.1Q header. |
281 | * However, older versions of OVS treated this as matching packets | |
282 | * withut an 802.1Q header, so we do here too. */ | |
81a76618 BP |
283 | match->flow.vlan_tci = htons(0); |
284 | match->wc.masks.vlan_tci = htons(0xffff); | |
47271d0d BP |
285 | } else { |
286 | ovs_be16 vid, pcp, tci; | |
bf062576 | 287 | uint16_t hpcp; |
47271d0d | 288 | |
81a76618 | 289 | vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK); |
bf062576 YT |
290 | hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK; |
291 | pcp = htons(hpcp); | |
47271d0d | 292 | tci = vid | pcp | htons(VLAN_CFI); |
81a76618 | 293 | match->flow.vlan_tci = tci & match->wc.masks.vlan_tci; |
66642cb4 BP |
294 | } |
295 | ||
d8ae4d67 | 296 | /* Clean up. */ |
81a76618 | 297 | match_zero_wildcarded_fields(match); |
d8ae4d67 BP |
298 | } |
299 | ||
81a76618 | 300 | /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */ |
d8ae4d67 | 301 | void |
81a76618 BP |
302 | ofputil_match_to_ofp10_match(const struct match *match, |
303 | struct ofp10_match *ofmatch) | |
d8ae4d67 | 304 | { |
81a76618 | 305 | const struct flow_wildcards *wc = &match->wc; |
eeba8e4f | 306 | uint32_t ofpfw; |
d8ae4d67 | 307 | |
66642cb4 | 308 | /* Figure out most OpenFlow wildcards. */ |
27cafc5f | 309 | ofpfw = 0; |
4e022ec0 | 310 | if (!wc->masks.in_port.ofp_port) { |
27cafc5f BP |
311 | ofpfw |= OFPFW10_IN_PORT; |
312 | } | |
26720e24 | 313 | if (!wc->masks.dl_type) { |
27cafc5f BP |
314 | ofpfw |= OFPFW10_DL_TYPE; |
315 | } | |
26720e24 | 316 | if (!wc->masks.nw_proto) { |
27cafc5f BP |
317 | ofpfw |= OFPFW10_NW_PROTO; |
318 | } | |
26720e24 | 319 | ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src) |
eec25dc1 | 320 | << OFPFW10_NW_SRC_SHIFT); |
26720e24 | 321 | ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst) |
eec25dc1 | 322 | << OFPFW10_NW_DST_SHIFT); |
26720e24 | 323 | if (!(wc->masks.nw_tos & IP_DSCP_MASK)) { |
eec25dc1 | 324 | ofpfw |= OFPFW10_NW_TOS; |
d8ae4d67 | 325 | } |
26720e24 | 326 | if (!wc->masks.tp_src) { |
eec25dc1 | 327 | ofpfw |= OFPFW10_TP_SRC; |
73f33563 | 328 | } |
26720e24 | 329 | if (!wc->masks.tp_dst) { |
eec25dc1 | 330 | ofpfw |= OFPFW10_TP_DST; |
73f33563 | 331 | } |
26720e24 | 332 | if (eth_addr_is_zero(wc->masks.dl_src)) { |
eec25dc1 | 333 | ofpfw |= OFPFW10_DL_SRC; |
73c0ce34 | 334 | } |
26720e24 | 335 | if (eth_addr_is_zero(wc->masks.dl_dst)) { |
eec25dc1 | 336 | ofpfw |= OFPFW10_DL_DST; |
73c0ce34 | 337 | } |
ff9d3826 | 338 | |
66642cb4 | 339 | /* Translate VLANs. */ |
81a76618 BP |
340 | ofmatch->dl_vlan = htons(0); |
341 | ofmatch->dl_vlan_pcp = 0; | |
342 | if (match->wc.masks.vlan_tci == htons(0)) { | |
eec25dc1 | 343 | ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP; |
81a76618 BP |
344 | } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI) |
345 | && !(match->flow.vlan_tci & htons(VLAN_CFI))) { | |
346 | ofmatch->dl_vlan = htons(OFP10_VLAN_NONE); | |
41ca9a1e | 347 | ofpfw |= OFPFW10_DL_VLAN_PCP; |
66642cb4 | 348 | } else { |
81a76618 | 349 | if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) { |
eec25dc1 | 350 | ofpfw |= OFPFW10_DL_VLAN; |
66642cb4 | 351 | } else { |
81a76618 | 352 | ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci)); |
66642cb4 BP |
353 | } |
354 | ||
81a76618 | 355 | if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) { |
eec25dc1 | 356 | ofpfw |= OFPFW10_DL_VLAN_PCP; |
66642cb4 | 357 | } else { |
81a76618 | 358 | ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci); |
66642cb4 BP |
359 | } |
360 | } | |
361 | ||
362 | /* Compose most of the match structure. */ | |
81a76618 | 363 | ofmatch->wildcards = htonl(ofpfw); |
4e022ec0 | 364 | ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port)); |
81a76618 BP |
365 | memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN); |
366 | memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN); | |
367 | ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type); | |
368 | ofmatch->nw_src = match->flow.nw_src; | |
369 | ofmatch->nw_dst = match->flow.nw_dst; | |
370 | ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK; | |
371 | ofmatch->nw_proto = match->flow.nw_proto; | |
372 | ofmatch->tp_src = match->flow.tp_src; | |
373 | ofmatch->tp_dst = match->flow.tp_dst; | |
374 | memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1); | |
375 | memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2); | |
d8ae4d67 BP |
376 | } |
377 | ||
aa319503 | 378 | enum ofperr |
81a76618 BP |
379 | ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match, |
380 | uint16_t *padded_match_len) | |
aa319503 | 381 | { |
6fd6ed71 | 382 | struct ofp11_match_header *omh = buf->data; |
36a16881 | 383 | uint16_t match_len; |
aa319503 | 384 | |
6fd6ed71 | 385 | if (buf->size < sizeof *omh) { |
aa319503 BP |
386 | return OFPERR_OFPBMC_BAD_LEN; |
387 | } | |
388 | ||
36a16881 SH |
389 | match_len = ntohs(omh->length); |
390 | ||
aa319503 | 391 | switch (ntohs(omh->type)) { |
36a16881 SH |
392 | case OFPMT_STANDARD: { |
393 | struct ofp11_match *om; | |
394 | ||
6fd6ed71 | 395 | if (match_len != sizeof *om || buf->size < sizeof *om) { |
aa319503 BP |
396 | return OFPERR_OFPBMC_BAD_LEN; |
397 | } | |
398 | om = ofpbuf_pull(buf, sizeof *om); | |
36a16881 SH |
399 | if (padded_match_len) { |
400 | *padded_match_len = match_len; | |
401 | } | |
81a76618 | 402 | return ofputil_match_from_ofp11_match(om, match); |
36a16881 SH |
403 | } |
404 | ||
405 | case OFPMT_OXM: | |
406 | if (padded_match_len) { | |
407 | *padded_match_len = ROUND_UP(match_len, 8); | |
408 | } | |
81a76618 | 409 | return oxm_pull_match(buf, match); |
aa319503 BP |
410 | |
411 | default: | |
412 | return OFPERR_OFPBMC_BAD_TYPE; | |
413 | } | |
414 | } | |
415 | ||
3f0f48cf YT |
416 | /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'. |
417 | * Returns 0 if successful, otherwise an OFPERR_* value. */ | |
410698cf | 418 | enum ofperr |
81a76618 BP |
419 | ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch, |
420 | struct match *match) | |
410698cf | 421 | { |
81a76618 | 422 | uint16_t wc = ntohl(ofmatch->wildcards); |
410698cf BP |
423 | uint8_t dl_src_mask[ETH_ADDR_LEN]; |
424 | uint8_t dl_dst_mask[ETH_ADDR_LEN]; | |
8087f5ff | 425 | bool ipv4, arp, rarp; |
410698cf BP |
426 | int i; |
427 | ||
81a76618 | 428 | match_init_catchall(match); |
410698cf BP |
429 | |
430 | if (!(wc & OFPFW11_IN_PORT)) { | |
4e022ec0 | 431 | ofp_port_t ofp_port; |
410698cf BP |
432 | enum ofperr error; |
433 | ||
81a76618 | 434 | error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port); |
410698cf BP |
435 | if (error) { |
436 | return OFPERR_OFPBMC_BAD_VALUE; | |
437 | } | |
81a76618 | 438 | match_set_in_port(match, ofp_port); |
410698cf BP |
439 | } |
440 | ||
441 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
81a76618 | 442 | dl_src_mask[i] = ~ofmatch->dl_src_mask[i]; |
410698cf | 443 | } |
81a76618 | 444 | match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask); |
410698cf BP |
445 | |
446 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
81a76618 | 447 | dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i]; |
410698cf | 448 | } |
81a76618 | 449 | match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask); |
410698cf BP |
450 | |
451 | if (!(wc & OFPFW11_DL_VLAN)) { | |
81a76618 | 452 | if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) { |
410698cf | 453 | /* Match only packets without a VLAN tag. */ |
81a76618 | 454 | match->flow.vlan_tci = htons(0); |
b8266395 | 455 | match->wc.masks.vlan_tci = OVS_BE16_MAX; |
410698cf | 456 | } else { |
81a76618 | 457 | if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) { |
410698cf | 458 | /* Match any packet with a VLAN tag regardless of VID. */ |
81a76618 BP |
459 | match->flow.vlan_tci = htons(VLAN_CFI); |
460 | match->wc.masks.vlan_tci = htons(VLAN_CFI); | |
461 | } else if (ntohs(ofmatch->dl_vlan) < 4096) { | |
410698cf | 462 | /* Match only packets with the specified VLAN VID. */ |
81a76618 BP |
463 | match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan; |
464 | match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK); | |
410698cf BP |
465 | } else { |
466 | /* Invalid VID. */ | |
467 | return OFPERR_OFPBMC_BAD_VALUE; | |
468 | } | |
469 | ||
470 | if (!(wc & OFPFW11_DL_VLAN_PCP)) { | |
81a76618 BP |
471 | if (ofmatch->dl_vlan_pcp <= 7) { |
472 | match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp | |
473 | << VLAN_PCP_SHIFT); | |
474 | match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK); | |
410698cf BP |
475 | } else { |
476 | /* Invalid PCP. */ | |
477 | return OFPERR_OFPBMC_BAD_VALUE; | |
478 | } | |
479 | } | |
480 | } | |
481 | } | |
482 | ||
483 | if (!(wc & OFPFW11_DL_TYPE)) { | |
81a76618 BP |
484 | match_set_dl_type(match, |
485 | ofputil_dl_type_from_openflow(ofmatch->dl_type)); | |
410698cf BP |
486 | } |
487 | ||
81a76618 BP |
488 | ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP); |
489 | arp = match->flow.dl_type == htons(ETH_TYPE_ARP); | |
8087f5ff | 490 | rarp = match->flow.dl_type == htons(ETH_TYPE_RARP); |
410698cf BP |
491 | |
492 | if (ipv4 && !(wc & OFPFW11_NW_TOS)) { | |
81a76618 | 493 | if (ofmatch->nw_tos & ~IP_DSCP_MASK) { |
410698cf BP |
494 | /* Invalid TOS. */ |
495 | return OFPERR_OFPBMC_BAD_VALUE; | |
496 | } | |
497 | ||
81a76618 | 498 | match_set_nw_dscp(match, ofmatch->nw_tos); |
410698cf BP |
499 | } |
500 | ||
8087f5ff | 501 | if (ipv4 || arp || rarp) { |
410698cf | 502 | if (!(wc & OFPFW11_NW_PROTO)) { |
81a76618 | 503 | match_set_nw_proto(match, ofmatch->nw_proto); |
410698cf | 504 | } |
81a76618 BP |
505 | match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask); |
506 | match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask); | |
410698cf BP |
507 | } |
508 | ||
509 | #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST) | |
510 | if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) { | |
81a76618 | 511 | switch (match->flow.nw_proto) { |
410698cf BP |
512 | case IPPROTO_ICMP: |
513 | /* "A.2.3 Flow Match Structures" in OF1.1 says: | |
514 | * | |
515 | * The tp_src and tp_dst fields will be ignored unless the | |
516 | * network protocol specified is as TCP, UDP or SCTP. | |
517 | * | |
518 | * but I'm pretty sure we should support ICMP too, otherwise | |
519 | * that's a regression from OF1.0. */ | |
520 | if (!(wc & OFPFW11_TP_SRC)) { | |
81a76618 | 521 | uint16_t icmp_type = ntohs(ofmatch->tp_src); |
410698cf | 522 | if (icmp_type < 0x100) { |
81a76618 | 523 | match_set_icmp_type(match, icmp_type); |
410698cf BP |
524 | } else { |
525 | return OFPERR_OFPBMC_BAD_FIELD; | |
526 | } | |
527 | } | |
528 | if (!(wc & OFPFW11_TP_DST)) { | |
81a76618 | 529 | uint16_t icmp_code = ntohs(ofmatch->tp_dst); |
410698cf | 530 | if (icmp_code < 0x100) { |
81a76618 | 531 | match_set_icmp_code(match, icmp_code); |
410698cf BP |
532 | } else { |
533 | return OFPERR_OFPBMC_BAD_FIELD; | |
534 | } | |
535 | } | |
536 | break; | |
537 | ||
538 | case IPPROTO_TCP: | |
539 | case IPPROTO_UDP: | |
0d56eaf2 | 540 | case IPPROTO_SCTP: |
410698cf | 541 | if (!(wc & (OFPFW11_TP_SRC))) { |
81a76618 | 542 | match_set_tp_src(match, ofmatch->tp_src); |
410698cf BP |
543 | } |
544 | if (!(wc & (OFPFW11_TP_DST))) { | |
81a76618 | 545 | match_set_tp_dst(match, ofmatch->tp_dst); |
410698cf BP |
546 | } |
547 | break; | |
548 | ||
410698cf BP |
549 | default: |
550 | /* OF1.1 says explicitly to ignore this. */ | |
551 | break; | |
552 | } | |
553 | } | |
554 | ||
b02475c5 | 555 | if (eth_type_mpls(match->flow.dl_type)) { |
097d4939 | 556 | if (!(wc & OFPFW11_MPLS_LABEL)) { |
8bfd0fda | 557 | match_set_mpls_label(match, 0, ofmatch->mpls_label); |
097d4939 JR |
558 | } |
559 | if (!(wc & OFPFW11_MPLS_TC)) { | |
8bfd0fda | 560 | match_set_mpls_tc(match, 0, ofmatch->mpls_tc); |
410698cf BP |
561 | } |
562 | } | |
563 | ||
81a76618 BP |
564 | match_set_metadata_masked(match, ofmatch->metadata, |
565 | ~ofmatch->metadata_mask); | |
410698cf BP |
566 | |
567 | return 0; | |
568 | } | |
569 | ||
81a76618 | 570 | /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */ |
410698cf | 571 | void |
81a76618 BP |
572 | ofputil_match_to_ofp11_match(const struct match *match, |
573 | struct ofp11_match *ofmatch) | |
410698cf BP |
574 | { |
575 | uint32_t wc = 0; | |
576 | int i; | |
577 | ||
81a76618 BP |
578 | memset(ofmatch, 0, sizeof *ofmatch); |
579 | ofmatch->omh.type = htons(OFPMT_STANDARD); | |
580 | ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH); | |
410698cf | 581 | |
4e022ec0 | 582 | if (!match->wc.masks.in_port.ofp_port) { |
410698cf BP |
583 | wc |= OFPFW11_IN_PORT; |
584 | } else { | |
4e022ec0 | 585 | ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port); |
410698cf BP |
586 | } |
587 | ||
81a76618 | 588 | memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN); |
410698cf | 589 | for (i = 0; i < ETH_ADDR_LEN; i++) { |
81a76618 | 590 | ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i]; |
410698cf BP |
591 | } |
592 | ||
81a76618 | 593 | memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN); |
410698cf | 594 | for (i = 0; i < ETH_ADDR_LEN; i++) { |
81a76618 | 595 | ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i]; |
410698cf BP |
596 | } |
597 | ||
81a76618 | 598 | if (match->wc.masks.vlan_tci == htons(0)) { |
410698cf | 599 | wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP; |
81a76618 BP |
600 | } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI) |
601 | && !(match->flow.vlan_tci & htons(VLAN_CFI))) { | |
602 | ofmatch->dl_vlan = htons(OFPVID11_NONE); | |
410698cf BP |
603 | wc |= OFPFW11_DL_VLAN_PCP; |
604 | } else { | |
81a76618 BP |
605 | if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) { |
606 | ofmatch->dl_vlan = htons(OFPVID11_ANY); | |
410698cf | 607 | } else { |
81a76618 | 608 | ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci)); |
410698cf BP |
609 | } |
610 | ||
81a76618 | 611 | if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) { |
410698cf BP |
612 | wc |= OFPFW11_DL_VLAN_PCP; |
613 | } else { | |
81a76618 | 614 | ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci); |
410698cf BP |
615 | } |
616 | } | |
617 | ||
81a76618 | 618 | if (!match->wc.masks.dl_type) { |
410698cf BP |
619 | wc |= OFPFW11_DL_TYPE; |
620 | } else { | |
81a76618 | 621 | ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type); |
410698cf BP |
622 | } |
623 | ||
81a76618 | 624 | if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) { |
410698cf BP |
625 | wc |= OFPFW11_NW_TOS; |
626 | } else { | |
81a76618 | 627 | ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK; |
410698cf BP |
628 | } |
629 | ||
81a76618 | 630 | if (!match->wc.masks.nw_proto) { |
410698cf BP |
631 | wc |= OFPFW11_NW_PROTO; |
632 | } else { | |
81a76618 | 633 | ofmatch->nw_proto = match->flow.nw_proto; |
410698cf BP |
634 | } |
635 | ||
81a76618 BP |
636 | ofmatch->nw_src = match->flow.nw_src; |
637 | ofmatch->nw_src_mask = ~match->wc.masks.nw_src; | |
638 | ofmatch->nw_dst = match->flow.nw_dst; | |
639 | ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst; | |
410698cf | 640 | |
81a76618 | 641 | if (!match->wc.masks.tp_src) { |
410698cf BP |
642 | wc |= OFPFW11_TP_SRC; |
643 | } else { | |
81a76618 | 644 | ofmatch->tp_src = match->flow.tp_src; |
410698cf BP |
645 | } |
646 | ||
81a76618 | 647 | if (!match->wc.masks.tp_dst) { |
410698cf BP |
648 | wc |= OFPFW11_TP_DST; |
649 | } else { | |
81a76618 | 650 | ofmatch->tp_dst = match->flow.tp_dst; |
410698cf BP |
651 | } |
652 | ||
8bfd0fda | 653 | if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) { |
097d4939 JR |
654 | wc |= OFPFW11_MPLS_LABEL; |
655 | } else { | |
8bfd0fda BP |
656 | ofmatch->mpls_label = htonl(mpls_lse_to_label( |
657 | match->flow.mpls_lse[0])); | |
097d4939 JR |
658 | } |
659 | ||
8bfd0fda | 660 | if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) { |
097d4939 JR |
661 | wc |= OFPFW11_MPLS_TC; |
662 | } else { | |
8bfd0fda | 663 | ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]); |
097d4939 | 664 | } |
410698cf | 665 | |
81a76618 BP |
666 | ofmatch->metadata = match->flow.metadata; |
667 | ofmatch->metadata_mask = ~match->wc.masks.metadata; | |
410698cf | 668 | |
81a76618 | 669 | ofmatch->wildcards = htonl(wc); |
410698cf BP |
670 | } |
671 | ||
75fa58f8 BP |
672 | /* Returns the "typical" length of a match for 'protocol', for use in |
673 | * estimating space to preallocate. */ | |
674 | int | |
675 | ofputil_match_typical_len(enum ofputil_protocol protocol) | |
676 | { | |
677 | switch (protocol) { | |
678 | case OFPUTIL_P_OF10_STD: | |
679 | case OFPUTIL_P_OF10_STD_TID: | |
680 | return sizeof(struct ofp10_match); | |
681 | ||
682 | case OFPUTIL_P_OF10_NXM: | |
683 | case OFPUTIL_P_OF10_NXM_TID: | |
684 | return NXM_TYPICAL_LEN; | |
685 | ||
686 | case OFPUTIL_P_OF11_STD: | |
687 | return sizeof(struct ofp11_match); | |
688 | ||
689 | case OFPUTIL_P_OF12_OXM: | |
690 | case OFPUTIL_P_OF13_OXM: | |
c37c0382 | 691 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 692 | case OFPUTIL_P_OF15_OXM: |
75fa58f8 BP |
693 | return NXM_TYPICAL_LEN; |
694 | ||
695 | default: | |
428b2edd | 696 | OVS_NOT_REACHED(); |
75fa58f8 BP |
697 | } |
698 | } | |
699 | ||
700 | /* Appends to 'b' an struct ofp11_match_header followed by a match that | |
701 | * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the | |
702 | * data appended out to a multiple of 8. 'protocol' must be one that is usable | |
703 | * in OpenFlow 1.1 or later. | |
704 | * | |
705 | * This function can cause 'b''s data to be reallocated. | |
706 | * | |
707 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
708 | * returns zero. */ | |
709 | int | |
710 | ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match, | |
711 | enum ofputil_protocol protocol) | |
712 | { | |
713 | switch (protocol) { | |
714 | case OFPUTIL_P_OF10_STD: | |
715 | case OFPUTIL_P_OF10_STD_TID: | |
716 | case OFPUTIL_P_OF10_NXM: | |
717 | case OFPUTIL_P_OF10_NXM_TID: | |
428b2edd | 718 | OVS_NOT_REACHED(); |
75fa58f8 BP |
719 | |
720 | case OFPUTIL_P_OF11_STD: { | |
721 | struct ofp11_match *om; | |
722 | ||
723 | /* Make sure that no padding is needed. */ | |
724 | BUILD_ASSERT_DECL(sizeof *om % 8 == 0); | |
725 | ||
726 | om = ofpbuf_put_uninit(b, sizeof *om); | |
727 | ofputil_match_to_ofp11_match(match, om); | |
728 | return sizeof *om; | |
729 | } | |
730 | ||
731 | case OFPUTIL_P_OF12_OXM: | |
732 | case OFPUTIL_P_OF13_OXM: | |
c37c0382 | 733 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 734 | case OFPUTIL_P_OF15_OXM: |
9d84066c BP |
735 | return oxm_put_match(b, match, |
736 | ofputil_protocol_to_ofp_version(protocol)); | |
75fa58f8 BP |
737 | } |
738 | ||
428b2edd | 739 | OVS_NOT_REACHED(); |
75fa58f8 BP |
740 | } |
741 | ||
36956a7d | 742 | /* Given a 'dl_type' value in the format used in struct flow, returns the |
eec25dc1 BP |
743 | * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match |
744 | * structure. */ | |
36956a7d BP |
745 | ovs_be16 |
746 | ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type) | |
747 | { | |
748 | return (flow_dl_type == htons(FLOW_DL_TYPE_NONE) | |
749 | ? htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
750 | : flow_dl_type); | |
751 | } | |
752 | ||
eec25dc1 | 753 | /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match |
36956a7d BP |
754 | * structure, returns the corresponding 'dl_type' value for use in struct |
755 | * flow. */ | |
756 | ovs_be16 | |
757 | ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type) | |
758 | { | |
759 | return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
760 | ? htons(FLOW_DL_TYPE_NONE) | |
761 | : ofp_dl_type); | |
762 | } | |
2e4f5fcf | 763 | \f |
27527aa0 | 764 | /* Protocols. */ |
7fa91113 | 765 | |
27527aa0 BP |
766 | struct proto_abbrev { |
767 | enum ofputil_protocol protocol; | |
768 | const char *name; | |
769 | }; | |
770 | ||
771 | /* Most users really don't care about some of the differences between | |
aa233d57 | 772 | * protocols. These abbreviations help with that. */ |
27527aa0 | 773 | static const struct proto_abbrev proto_abbrevs[] = { |
aa233d57 BP |
774 | { OFPUTIL_P_ANY, "any" }, |
775 | { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" }, | |
776 | { OFPUTIL_P_OF10_NXM_ANY, "NXM" }, | |
777 | { OFPUTIL_P_ANY_OXM, "OXM" }, | |
27527aa0 BP |
778 | }; |
779 | #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs) | |
780 | ||
781 | enum ofputil_protocol ofputil_flow_dump_protocols[] = { | |
42dccab5 | 782 | OFPUTIL_P_OF15_OXM, |
c37c0382 | 783 | OFPUTIL_P_OF14_OXM, |
2e1ae200 | 784 | OFPUTIL_P_OF13_OXM, |
8d7785bd | 785 | OFPUTIL_P_OF12_OXM, |
75fa58f8 | 786 | OFPUTIL_P_OF11_STD, |
85813857 BP |
787 | OFPUTIL_P_OF10_NXM, |
788 | OFPUTIL_P_OF10_STD, | |
27527aa0 BP |
789 | }; |
790 | size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols); | |
791 | ||
4f13da56 BP |
792 | /* Returns the set of ofputil_protocols that are supported with the given |
793 | * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version | |
794 | * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0 | |
795 | * if 'version' is not supported or outside the valid range. */ | |
27527aa0 | 796 | enum ofputil_protocol |
4f13da56 | 797 | ofputil_protocols_from_ofp_version(enum ofp_version version) |
27527aa0 BP |
798 | { |
799 | switch (version) { | |
2e3fa633 | 800 | case OFP10_VERSION: |
4f13da56 | 801 | return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY; |
75fa58f8 BP |
802 | case OFP11_VERSION: |
803 | return OFPUTIL_P_OF11_STD; | |
2e3fa633 | 804 | case OFP12_VERSION: |
85813857 | 805 | return OFPUTIL_P_OF12_OXM; |
2e1ae200 JR |
806 | case OFP13_VERSION: |
807 | return OFPUTIL_P_OF13_OXM; | |
c37c0382 AC |
808 | case OFP14_VERSION: |
809 | return OFPUTIL_P_OF14_OXM; | |
42dccab5 BP |
810 | case OFP15_VERSION: |
811 | return OFPUTIL_P_OF15_OXM; | |
2e3fa633 SH |
812 | default: |
813 | return 0; | |
27527aa0 BP |
814 | } |
815 | } | |
816 | ||
4f13da56 BP |
817 | /* Returns the ofputil_protocol that is initially in effect on an OpenFlow |
818 | * connection that has negotiated the given 'version'. 'version' should | |
819 | * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow | |
820 | * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or | |
821 | * outside the valid range. */ | |
822 | enum ofputil_protocol | |
823 | ofputil_protocol_from_ofp_version(enum ofp_version version) | |
824 | { | |
825 | return rightmost_1bit(ofputil_protocols_from_ofp_version(version)); | |
826 | } | |
827 | ||
44d3732d | 828 | /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION, |
2e1ae200 | 829 | * etc.) that corresponds to 'protocol'. */ |
2e3fa633 | 830 | enum ofp_version |
9e1fd49b BP |
831 | ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol) |
832 | { | |
833 | switch (protocol) { | |
85813857 BP |
834 | case OFPUTIL_P_OF10_STD: |
835 | case OFPUTIL_P_OF10_STD_TID: | |
836 | case OFPUTIL_P_OF10_NXM: | |
837 | case OFPUTIL_P_OF10_NXM_TID: | |
9e1fd49b | 838 | return OFP10_VERSION; |
75fa58f8 BP |
839 | case OFPUTIL_P_OF11_STD: |
840 | return OFP11_VERSION; | |
85813857 | 841 | case OFPUTIL_P_OF12_OXM: |
44d3732d | 842 | return OFP12_VERSION; |
2e1ae200 JR |
843 | case OFPUTIL_P_OF13_OXM: |
844 | return OFP13_VERSION; | |
c37c0382 AC |
845 | case OFPUTIL_P_OF14_OXM: |
846 | return OFP14_VERSION; | |
42dccab5 BP |
847 | case OFPUTIL_P_OF15_OXM: |
848 | return OFP15_VERSION; | |
9e1fd49b BP |
849 | } |
850 | ||
428b2edd | 851 | OVS_NOT_REACHED(); |
9e1fd49b BP |
852 | } |
853 | ||
4f13da56 BP |
854 | /* Returns a bitmap of OpenFlow versions that are supported by at |
855 | * least one of the 'protocols'. */ | |
856 | uint32_t | |
857 | ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols) | |
858 | { | |
859 | uint32_t bitmap = 0; | |
860 | ||
861 | for (; protocols; protocols = zero_rightmost_1bit(protocols)) { | |
862 | enum ofputil_protocol protocol = rightmost_1bit(protocols); | |
863 | ||
864 | bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol); | |
865 | } | |
866 | ||
867 | return bitmap; | |
868 | } | |
869 | ||
870 | /* Returns the set of protocols that are supported on top of the | |
871 | * OpenFlow versions included in 'bitmap'. */ | |
872 | enum ofputil_protocol | |
873 | ofputil_protocols_from_version_bitmap(uint32_t bitmap) | |
874 | { | |
875 | enum ofputil_protocol protocols = 0; | |
876 | ||
877 | for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) { | |
878 | enum ofp_version version = rightmost_1bit_idx(bitmap); | |
879 | ||
880 | protocols |= ofputil_protocols_from_ofp_version(version); | |
881 | } | |
882 | ||
883 | return protocols; | |
884 | } | |
885 | ||
27527aa0 BP |
886 | /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false |
887 | * otherwise. */ | |
7fa91113 | 888 | bool |
27527aa0 | 889 | ofputil_protocol_is_valid(enum ofputil_protocol protocol) |
7fa91113 | 890 | { |
27527aa0 BP |
891 | return protocol & OFPUTIL_P_ANY && is_pow2(protocol); |
892 | } | |
893 | ||
894 | /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id | |
895 | * extension turned on or off if 'enable' is true or false, respectively. | |
896 | * | |
897 | * This extension is only useful for protocols whose "standard" version does | |
898 | * not allow specific tables to be modified. In particular, this is true of | |
899 | * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always | |
900 | * specifies a table ID and so there is no need for such an extension. When | |
901 | * 'protocol' is such a protocol that doesn't need a flow_mod_table_id | |
902 | * extension, this function just returns its 'protocol' argument unchanged | |
903 | * regardless of the value of 'enable'. */ | |
904 | enum ofputil_protocol | |
905 | ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable) | |
906 | { | |
907 | switch (protocol) { | |
85813857 BP |
908 | case OFPUTIL_P_OF10_STD: |
909 | case OFPUTIL_P_OF10_STD_TID: | |
910 | return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD; | |
27527aa0 | 911 | |
85813857 BP |
912 | case OFPUTIL_P_OF10_NXM: |
913 | case OFPUTIL_P_OF10_NXM_TID: | |
914 | return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM; | |
27527aa0 | 915 | |
75fa58f8 BP |
916 | case OFPUTIL_P_OF11_STD: |
917 | return OFPUTIL_P_OF11_STD; | |
918 | ||
85813857 BP |
919 | case OFPUTIL_P_OF12_OXM: |
920 | return OFPUTIL_P_OF12_OXM; | |
44d3732d | 921 | |
2e1ae200 JR |
922 | case OFPUTIL_P_OF13_OXM: |
923 | return OFPUTIL_P_OF13_OXM; | |
924 | ||
c37c0382 AC |
925 | case OFPUTIL_P_OF14_OXM: |
926 | return OFPUTIL_P_OF14_OXM; | |
927 | ||
42dccab5 BP |
928 | case OFPUTIL_P_OF15_OXM: |
929 | return OFPUTIL_P_OF15_OXM; | |
930 | ||
27527aa0 | 931 | default: |
428b2edd | 932 | OVS_NOT_REACHED(); |
7fa91113 | 933 | } |
27527aa0 | 934 | } |
7fa91113 | 935 | |
27527aa0 BP |
936 | /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes |
937 | * some extension to a standard protocol version, the return value is the | |
938 | * standard version of that protocol without any extension. If 'protocol' is a | |
939 | * standard protocol version, returns 'protocol' unchanged. */ | |
940 | enum ofputil_protocol | |
941 | ofputil_protocol_to_base(enum ofputil_protocol protocol) | |
942 | { | |
943 | return ofputil_protocol_set_tid(protocol, false); | |
7fa91113 BP |
944 | } |
945 | ||
27527aa0 BP |
946 | /* Returns 'new_base' with any extensions taken from 'cur'. */ |
947 | enum ofputil_protocol | |
948 | ofputil_protocol_set_base(enum ofputil_protocol cur, | |
949 | enum ofputil_protocol new_base) | |
7fa91113 | 950 | { |
27527aa0 BP |
951 | bool tid = (cur & OFPUTIL_P_TID) != 0; |
952 | ||
953 | switch (new_base) { | |
85813857 BP |
954 | case OFPUTIL_P_OF10_STD: |
955 | case OFPUTIL_P_OF10_STD_TID: | |
956 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid); | |
27527aa0 | 957 | |
85813857 BP |
958 | case OFPUTIL_P_OF10_NXM: |
959 | case OFPUTIL_P_OF10_NXM_TID: | |
960 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid); | |
27527aa0 | 961 | |
75fa58f8 BP |
962 | case OFPUTIL_P_OF11_STD: |
963 | return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid); | |
964 | ||
85813857 BP |
965 | case OFPUTIL_P_OF12_OXM: |
966 | return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid); | |
44d3732d | 967 | |
2e1ae200 JR |
968 | case OFPUTIL_P_OF13_OXM: |
969 | return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid); | |
970 | ||
c37c0382 AC |
971 | case OFPUTIL_P_OF14_OXM: |
972 | return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid); | |
973 | ||
42dccab5 BP |
974 | case OFPUTIL_P_OF15_OXM: |
975 | return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid); | |
976 | ||
7fa91113 | 977 | default: |
428b2edd | 978 | OVS_NOT_REACHED(); |
7fa91113 BP |
979 | } |
980 | } | |
981 | ||
27527aa0 BP |
982 | /* Returns a string form of 'protocol', if a simple form exists (that is, if |
983 | * 'protocol' is either a single protocol or it is a combination of protocols | |
984 | * that have a single abbreviation). Otherwise, returns NULL. */ | |
985 | const char * | |
986 | ofputil_protocol_to_string(enum ofputil_protocol protocol) | |
88ca35ee | 987 | { |
27527aa0 BP |
988 | const struct proto_abbrev *p; |
989 | ||
990 | /* Use a "switch" statement for single-bit names so that we get a compiler | |
991 | * warning if we forget any. */ | |
992 | switch (protocol) { | |
85813857 | 993 | case OFPUTIL_P_OF10_NXM: |
27527aa0 BP |
994 | return "NXM-table_id"; |
995 | ||
85813857 | 996 | case OFPUTIL_P_OF10_NXM_TID: |
27527aa0 BP |
997 | return "NXM+table_id"; |
998 | ||
85813857 | 999 | case OFPUTIL_P_OF10_STD: |
27527aa0 BP |
1000 | return "OpenFlow10-table_id"; |
1001 | ||
85813857 | 1002 | case OFPUTIL_P_OF10_STD_TID: |
27527aa0 | 1003 | return "OpenFlow10+table_id"; |
44d3732d | 1004 | |
75fa58f8 BP |
1005 | case OFPUTIL_P_OF11_STD: |
1006 | return "OpenFlow11"; | |
1007 | ||
85813857 | 1008 | case OFPUTIL_P_OF12_OXM: |
e71bff1b | 1009 | return "OXM-OpenFlow12"; |
2e1ae200 JR |
1010 | |
1011 | case OFPUTIL_P_OF13_OXM: | |
e71bff1b | 1012 | return "OXM-OpenFlow13"; |
c37c0382 AC |
1013 | |
1014 | case OFPUTIL_P_OF14_OXM: | |
1015 | return "OXM-OpenFlow14"; | |
42dccab5 BP |
1016 | |
1017 | case OFPUTIL_P_OF15_OXM: | |
1018 | return "OXM-OpenFlow15"; | |
27527aa0 BP |
1019 | } |
1020 | ||
1021 | /* Check abbreviations. */ | |
1022 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1023 | if (protocol == p->protocol) { | |
1024 | return p->name; | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | return NULL; | |
1029 | } | |
1030 | ||
1031 | /* Returns a string that represents 'protocols'. The return value might be a | |
1032 | * comma-separated list if 'protocols' doesn't have a simple name. The return | |
1033 | * value is "none" if 'protocols' is 0. | |
1034 | * | |
1035 | * The caller must free the returned string (with free()). */ | |
1036 | char * | |
1037 | ofputil_protocols_to_string(enum ofputil_protocol protocols) | |
1038 | { | |
1039 | struct ds s; | |
1040 | ||
cb22974d | 1041 | ovs_assert(!(protocols & ~OFPUTIL_P_ANY)); |
27527aa0 BP |
1042 | if (protocols == 0) { |
1043 | return xstrdup("none"); | |
1044 | } | |
1045 | ||
1046 | ds_init(&s); | |
1047 | while (protocols) { | |
1048 | const struct proto_abbrev *p; | |
1049 | int i; | |
1050 | ||
1051 | if (s.length) { | |
1052 | ds_put_char(&s, ','); | |
1053 | } | |
1054 | ||
1055 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1056 | if ((protocols & p->protocol) == p->protocol) { | |
1057 | ds_put_cstr(&s, p->name); | |
1058 | protocols &= ~p->protocol; | |
1059 | goto match; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) { | |
1064 | enum ofputil_protocol bit = 1u << i; | |
1065 | ||
1066 | if (protocols & bit) { | |
1067 | ds_put_cstr(&s, ofputil_protocol_to_string(bit)); | |
1068 | protocols &= ~bit; | |
1069 | goto match; | |
1070 | } | |
1071 | } | |
428b2edd | 1072 | OVS_NOT_REACHED(); |
27527aa0 BP |
1073 | |
1074 | match: ; | |
1075 | } | |
1076 | return ds_steal_cstr(&s); | |
1077 | } | |
1078 | ||
1079 | static enum ofputil_protocol | |
1080 | ofputil_protocol_from_string__(const char *s, size_t n) | |
1081 | { | |
1082 | const struct proto_abbrev *p; | |
1083 | int i; | |
1084 | ||
1085 | for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) { | |
1086 | enum ofputil_protocol bit = 1u << i; | |
1087 | const char *name = ofputil_protocol_to_string(bit); | |
1088 | ||
1089 | if (name && n == strlen(name) && !strncasecmp(s, name, n)) { | |
1090 | return bit; | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1095 | if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) { | |
1096 | return p->protocol; | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | return 0; | |
1101 | } | |
1102 | ||
1103 | /* Returns the nonempty set of protocols represented by 's', which can be a | |
1104 | * single protocol name or abbreviation or a comma-separated list of them. | |
1105 | * | |
1106 | * Aborts the program with an error message if 's' is invalid. */ | |
1107 | enum ofputil_protocol | |
1108 | ofputil_protocols_from_string(const char *s) | |
1109 | { | |
1110 | const char *orig_s = s; | |
1111 | enum ofputil_protocol protocols; | |
1112 | ||
1113 | protocols = 0; | |
1114 | while (*s) { | |
1115 | enum ofputil_protocol p; | |
1116 | size_t n; | |
1117 | ||
1118 | n = strcspn(s, ","); | |
1119 | if (n == 0) { | |
1120 | s++; | |
1121 | continue; | |
1122 | } | |
1123 | ||
1124 | p = ofputil_protocol_from_string__(s, n); | |
1125 | if (!p) { | |
1126 | ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s); | |
1127 | } | |
1128 | protocols |= p; | |
1129 | ||
1130 | s += n; | |
1131 | } | |
1132 | ||
1133 | if (!protocols) { | |
1134 | ovs_fatal(0, "%s: no flow protocol specified", orig_s); | |
1135 | } | |
1136 | return protocols; | |
88ca35ee BP |
1137 | } |
1138 | ||
9d84066c | 1139 | enum ofp_version |
03e1125c SH |
1140 | ofputil_version_from_string(const char *s) |
1141 | { | |
1142 | if (!strcasecmp(s, "OpenFlow10")) { | |
1143 | return OFP10_VERSION; | |
1144 | } | |
1145 | if (!strcasecmp(s, "OpenFlow11")) { | |
1146 | return OFP11_VERSION; | |
1147 | } | |
1148 | if (!strcasecmp(s, "OpenFlow12")) { | |
1149 | return OFP12_VERSION; | |
1150 | } | |
2e1ae200 JR |
1151 | if (!strcasecmp(s, "OpenFlow13")) { |
1152 | return OFP13_VERSION; | |
1153 | } | |
c37c0382 AC |
1154 | if (!strcasecmp(s, "OpenFlow14")) { |
1155 | return OFP14_VERSION; | |
1156 | } | |
42dccab5 BP |
1157 | if (!strcasecmp(s, "OpenFlow15")) { |
1158 | return OFP15_VERSION; | |
1159 | } | |
7beaa082 | 1160 | return 0; |
03e1125c SH |
1161 | } |
1162 | ||
1163 | static bool | |
e091ef84 | 1164 | is_delimiter(unsigned char c) |
03e1125c SH |
1165 | { |
1166 | return isspace(c) || c == ','; | |
1167 | } | |
1168 | ||
1169 | uint32_t | |
1170 | ofputil_versions_from_string(const char *s) | |
1171 | { | |
1172 | size_t i = 0; | |
1173 | uint32_t bitmap = 0; | |
1174 | ||
1175 | while (s[i]) { | |
1176 | size_t j; | |
7beaa082 | 1177 | int version; |
03e1125c SH |
1178 | char *key; |
1179 | ||
1180 | if (is_delimiter(s[i])) { | |
1181 | i++; | |
1182 | continue; | |
1183 | } | |
1184 | j = 0; | |
1185 | while (s[i + j] && !is_delimiter(s[i + j])) { | |
1186 | j++; | |
1187 | } | |
1188 | key = xmemdup0(s + i, j); | |
1189 | version = ofputil_version_from_string(key); | |
7beaa082 SH |
1190 | if (!version) { |
1191 | VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key); | |
1192 | } | |
03e1125c SH |
1193 | free(key); |
1194 | bitmap |= 1u << version; | |
1195 | i += j; | |
1196 | } | |
1197 | ||
1198 | return bitmap; | |
1199 | } | |
1200 | ||
7beaa082 SH |
1201 | uint32_t |
1202 | ofputil_versions_from_strings(char ** const s, size_t count) | |
1203 | { | |
1204 | uint32_t bitmap = 0; | |
1205 | ||
1206 | while (count--) { | |
1207 | int version = ofputil_version_from_string(s[count]); | |
1208 | if (!version) { | |
1209 | VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]); | |
1210 | } else { | |
1211 | bitmap |= 1u << version; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | return bitmap; | |
1216 | } | |
1217 | ||
03e1125c SH |
1218 | const char * |
1219 | ofputil_version_to_string(enum ofp_version ofp_version) | |
1220 | { | |
1221 | switch (ofp_version) { | |
1222 | case OFP10_VERSION: | |
1223 | return "OpenFlow10"; | |
1224 | case OFP11_VERSION: | |
1225 | return "OpenFlow11"; | |
1226 | case OFP12_VERSION: | |
1227 | return "OpenFlow12"; | |
2e1ae200 JR |
1228 | case OFP13_VERSION: |
1229 | return "OpenFlow13"; | |
c37c0382 AC |
1230 | case OFP14_VERSION: |
1231 | return "OpenFlow14"; | |
42dccab5 BP |
1232 | case OFP15_VERSION: |
1233 | return "OpenFlow15"; | |
03e1125c | 1234 | default: |
428b2edd | 1235 | OVS_NOT_REACHED(); |
03e1125c SH |
1236 | } |
1237 | } | |
1238 | ||
54834960 EJ |
1239 | bool |
1240 | ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format) | |
1241 | { | |
1242 | switch (packet_in_format) { | |
1243 | case NXPIF_OPENFLOW10: | |
1244 | case NXPIF_NXM: | |
1245 | return true; | |
1246 | } | |
1247 | ||
1248 | return false; | |
1249 | } | |
1250 | ||
1251 | const char * | |
1252 | ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format) | |
1253 | { | |
1254 | switch (packet_in_format) { | |
1255 | case NXPIF_OPENFLOW10: | |
1256 | return "openflow10"; | |
1257 | case NXPIF_NXM: | |
1258 | return "nxm"; | |
1259 | default: | |
428b2edd | 1260 | OVS_NOT_REACHED(); |
54834960 EJ |
1261 | } |
1262 | } | |
1263 | ||
1264 | int | |
1265 | ofputil_packet_in_format_from_string(const char *s) | |
1266 | { | |
1267 | return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10 | |
1268 | : !strcmp(s, "nxm") ? NXPIF_NXM | |
1269 | : -1); | |
1270 | } | |
1271 | ||
03e1125c SH |
1272 | void |
1273 | ofputil_format_version(struct ds *msg, enum ofp_version version) | |
1274 | { | |
8989046d | 1275 | ds_put_format(msg, "0x%02x", version); |
03e1125c SH |
1276 | } |
1277 | ||
1278 | void | |
1279 | ofputil_format_version_name(struct ds *msg, enum ofp_version version) | |
1280 | { | |
1281 | ds_put_cstr(msg, ofputil_version_to_string(version)); | |
1282 | } | |
1283 | ||
1284 | static void | |
1285 | ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap, | |
1286 | void (*format_version)(struct ds *msg, | |
1287 | enum ofp_version)) | |
1288 | { | |
1289 | while (bitmap) { | |
1290 | format_version(msg, raw_ctz(bitmap)); | |
1291 | bitmap = zero_rightmost_1bit(bitmap); | |
1292 | if (bitmap) { | |
1293 | ds_put_cstr(msg, ", "); | |
1294 | } | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | void | |
1299 | ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap) | |
1300 | { | |
1301 | ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version); | |
1302 | } | |
1303 | ||
1304 | void | |
1305 | ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap) | |
1306 | { | |
1307 | ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name); | |
1308 | } | |
1309 | ||
de6c85b0 SH |
1310 | static bool |
1311 | ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh, | |
0935de41 | 1312 | uint32_t *allowed_versionsp) |
de6c85b0 SH |
1313 | { |
1314 | uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh; | |
db5a1019 | 1315 | const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1); |
0935de41 | 1316 | uint32_t allowed_versions; |
de6c85b0 SH |
1317 | |
1318 | if (!bitmap_len || bitmap_len % sizeof *bitmap) { | |
1319 | return false; | |
1320 | } | |
1321 | ||
1322 | /* Only use the first 32-bit element of the bitmap as that is all the | |
1323 | * current implementation supports. Subsequent elements are ignored which | |
7e9383cc | 1324 | * should have no effect on session negotiation until Open vSwitch supports |
de6c85b0 SH |
1325 | * wire-protocol versions greater than 31. |
1326 | */ | |
0935de41 | 1327 | allowed_versions = ntohl(bitmap[0]); |
de6c85b0 | 1328 | |
0935de41 | 1329 | if (allowed_versions & 1) { |
de6c85b0 SH |
1330 | /* There's no OpenFlow version 0. */ |
1331 | VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow " | |
1332 | "version 0x00"); | |
0935de41 | 1333 | allowed_versions &= ~1u; |
de6c85b0 SH |
1334 | } |
1335 | ||
0935de41 | 1336 | if (!allowed_versions) { |
de6c85b0 SH |
1337 | VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow " |
1338 | "version (between 0x01 and 0x1f)"); | |
1339 | return false; | |
1340 | } | |
1341 | ||
0935de41 | 1342 | *allowed_versionsp = allowed_versions; |
de6c85b0 SH |
1343 | return true; |
1344 | } | |
1345 | ||
1346 | static uint32_t | |
1347 | version_bitmap_from_version(uint8_t ofp_version) | |
1348 | { | |
1349 | return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1; | |
1350 | } | |
1351 | ||
1352 | /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions' | |
1353 | * the set of OpenFlow versions for which 'oh' announces support. | |
1354 | * | |
1355 | * Because of how OpenFlow defines OFPT_HELLO messages, this function is always | |
1356 | * successful, and thus '*allowed_versions' is always initialized. However, it | |
1357 | * returns false if 'oh' contains some data that could not be fully understood, | |
1358 | * true if 'oh' was completely parsed. */ | |
1359 | bool | |
1360 | ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions) | |
1361 | { | |
1362 | struct ofpbuf msg; | |
1363 | bool ok = true; | |
1364 | ||
1365 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
1366 | ofpbuf_pull(&msg, sizeof *oh); | |
1367 | ||
1368 | *allowed_versions = version_bitmap_from_version(oh->version); | |
6fd6ed71 | 1369 | while (msg.size) { |
de6c85b0 SH |
1370 | const struct ofp_hello_elem_header *oheh; |
1371 | unsigned int len; | |
1372 | ||
6fd6ed71 | 1373 | if (msg.size < sizeof *oheh) { |
de6c85b0 SH |
1374 | return false; |
1375 | } | |
1376 | ||
6fd6ed71 | 1377 | oheh = msg.data; |
de6c85b0 SH |
1378 | len = ntohs(oheh->length); |
1379 | if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) { | |
1380 | return false; | |
1381 | } | |
1382 | ||
1383 | if (oheh->type != htons(OFPHET_VERSIONBITMAP) | |
1384 | || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) { | |
1385 | ok = false; | |
1386 | } | |
1387 | } | |
1388 | ||
1389 | return ok; | |
1390 | } | |
1391 | ||
1392 | /* Returns true if 'allowed_versions' needs to be accompanied by a version | |
1393 | * bitmap to be correctly expressed in an OFPT_HELLO message. */ | |
5ddea998 | 1394 | static bool |
de6c85b0 SH |
1395 | should_send_version_bitmap(uint32_t allowed_versions) |
1396 | { | |
1397 | return !is_pow2((allowed_versions >> 1) + 1); | |
1398 | } | |
1399 | ||
1400 | /* Create an OFPT_HELLO message that expresses support for the OpenFlow | |
1401 | * versions in the 'allowed_versions' bitmaps and returns the message. */ | |
1402 | struct ofpbuf * | |
1403 | ofputil_encode_hello(uint32_t allowed_versions) | |
1404 | { | |
1405 | enum ofp_version ofp_version; | |
1406 | struct ofpbuf *msg; | |
1407 | ||
1408 | ofp_version = leftmost_1bit_idx(allowed_versions); | |
1409 | msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0); | |
1410 | ||
1411 | if (should_send_version_bitmap(allowed_versions)) { | |
1412 | struct ofp_hello_elem_header *oheh; | |
1413 | uint16_t map_len; | |
1414 | ||
74c4b9c1 | 1415 | map_len = sizeof allowed_versions; |
de6c85b0 SH |
1416 | oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8)); |
1417 | oheh->type = htons(OFPHET_VERSIONBITMAP); | |
1418 | oheh->length = htons(map_len + sizeof *oheh); | |
db5a1019 | 1419 | *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions); |
1804d25a BP |
1420 | |
1421 | ofpmsg_update_length(msg); | |
de6c85b0 SH |
1422 | } |
1423 | ||
1424 | return msg; | |
1425 | } | |
1426 | ||
27527aa0 BP |
1427 | /* Returns an OpenFlow message that, sent on an OpenFlow connection whose |
1428 | * protocol is 'current', at least partly transitions the protocol to 'want'. | |
1429 | * Stores in '*next' the protocol that will be in effect on the OpenFlow | |
1430 | * connection if the switch processes the returned message correctly. (If | |
1431 | * '*next != want' then the caller will have to iterate.) | |
1432 | * | |
e43928f2 BP |
1433 | * If 'current == want', or if it is not possible to transition from 'current' |
1434 | * to 'want' (because, for example, 'current' and 'want' use different OpenFlow | |
1435 | * protocol versions), returns NULL and stores 'current' in '*next'. */ | |
27527aa0 BP |
1436 | struct ofpbuf * |
1437 | ofputil_encode_set_protocol(enum ofputil_protocol current, | |
1438 | enum ofputil_protocol want, | |
1439 | enum ofputil_protocol *next) | |
1440 | { | |
e43928f2 | 1441 | enum ofp_version cur_version, want_version; |
27527aa0 BP |
1442 | enum ofputil_protocol cur_base, want_base; |
1443 | bool cur_tid, want_tid; | |
1444 | ||
e43928f2 BP |
1445 | cur_version = ofputil_protocol_to_ofp_version(current); |
1446 | want_version = ofputil_protocol_to_ofp_version(want); | |
1447 | if (cur_version != want_version) { | |
1448 | *next = current; | |
1449 | return NULL; | |
1450 | } | |
1451 | ||
27527aa0 BP |
1452 | cur_base = ofputil_protocol_to_base(current); |
1453 | want_base = ofputil_protocol_to_base(want); | |
1454 | if (cur_base != want_base) { | |
1455 | *next = ofputil_protocol_set_base(current, want_base); | |
1456 | ||
1457 | switch (want_base) { | |
85813857 | 1458 | case OFPUTIL_P_OF10_NXM: |
27527aa0 BP |
1459 | return ofputil_encode_nx_set_flow_format(NXFF_NXM); |
1460 | ||
85813857 | 1461 | case OFPUTIL_P_OF10_STD: |
27527aa0 BP |
1462 | return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10); |
1463 | ||
75fa58f8 | 1464 | case OFPUTIL_P_OF11_STD: |
85813857 | 1465 | case OFPUTIL_P_OF12_OXM: |
2e1ae200 | 1466 | case OFPUTIL_P_OF13_OXM: |
c37c0382 | 1467 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 1468 | case OFPUTIL_P_OF15_OXM: |
75fa58f8 | 1469 | /* There is only one variant of each OpenFlow 1.1+ protocol, and we |
2e1ae200 | 1470 | * verified above that we're not trying to change versions. */ |
428b2edd | 1471 | OVS_NOT_REACHED(); |
44d3732d | 1472 | |
85813857 BP |
1473 | case OFPUTIL_P_OF10_STD_TID: |
1474 | case OFPUTIL_P_OF10_NXM_TID: | |
428b2edd | 1475 | OVS_NOT_REACHED(); |
27527aa0 BP |
1476 | } |
1477 | } | |
1478 | ||
1479 | cur_tid = (current & OFPUTIL_P_TID) != 0; | |
1480 | want_tid = (want & OFPUTIL_P_TID) != 0; | |
1481 | if (cur_tid != want_tid) { | |
1482 | *next = ofputil_protocol_set_tid(current, want_tid); | |
1483 | return ofputil_make_flow_mod_table_id(want_tid); | |
1484 | } | |
1485 | ||
cb22974d | 1486 | ovs_assert(current == want); |
27527aa0 BP |
1487 | |
1488 | *next = current; | |
1489 | return NULL; | |
88ca35ee BP |
1490 | } |
1491 | ||
27527aa0 BP |
1492 | /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow |
1493 | * format to 'nxff'. */ | |
88ca35ee | 1494 | struct ofpbuf * |
27527aa0 | 1495 | ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff) |
88ca35ee | 1496 | { |
73dbf4ab | 1497 | struct nx_set_flow_format *sff; |
88ca35ee BP |
1498 | struct ofpbuf *msg; |
1499 | ||
cb22974d | 1500 | ovs_assert(ofputil_nx_flow_format_is_valid(nxff)); |
27527aa0 | 1501 | |
982697a4 BP |
1502 | msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0); |
1503 | sff = ofpbuf_put_zeros(msg, sizeof *sff); | |
27527aa0 | 1504 | sff->format = htonl(nxff); |
88ca35ee BP |
1505 | |
1506 | return msg; | |
1507 | } | |
1508 | ||
27527aa0 BP |
1509 | /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false |
1510 | * otherwise. */ | |
1511 | enum ofputil_protocol | |
1512 | ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format) | |
1513 | { | |
1514 | switch (flow_format) { | |
1515 | case NXFF_OPENFLOW10: | |
85813857 | 1516 | return OFPUTIL_P_OF10_STD; |
27527aa0 BP |
1517 | |
1518 | case NXFF_NXM: | |
85813857 | 1519 | return OFPUTIL_P_OF10_NXM; |
27527aa0 BP |
1520 | |
1521 | default: | |
1522 | return 0; | |
1523 | } | |
1524 | } | |
1525 | ||
1526 | /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */ | |
1527 | bool | |
1528 | ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format) | |
1529 | { | |
1530 | return ofputil_nx_flow_format_to_protocol(flow_format) != 0; | |
1531 | } | |
1532 | ||
1533 | /* Returns a string version of 'flow_format', which must be a valid NXFF_* | |
1534 | * value. */ | |
1535 | const char * | |
1536 | ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format) | |
1537 | { | |
1538 | switch (flow_format) { | |
1539 | case NXFF_OPENFLOW10: | |
1540 | return "openflow10"; | |
1541 | case NXFF_NXM: | |
1542 | return "nxm"; | |
1543 | default: | |
428b2edd | 1544 | OVS_NOT_REACHED(); |
27527aa0 BP |
1545 | } |
1546 | } | |
1547 | ||
54834960 | 1548 | struct ofpbuf * |
3f4a1939 SH |
1549 | ofputil_make_set_packet_in_format(enum ofp_version ofp_version, |
1550 | enum nx_packet_in_format packet_in_format) | |
54834960 | 1551 | { |
73dbf4ab | 1552 | struct nx_set_packet_in_format *spif; |
54834960 EJ |
1553 | struct ofpbuf *msg; |
1554 | ||
3f4a1939 | 1555 | msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0); |
982697a4 | 1556 | spif = ofpbuf_put_zeros(msg, sizeof *spif); |
54834960 EJ |
1557 | spif->format = htonl(packet_in_format); |
1558 | ||
1559 | return msg; | |
1560 | } | |
1561 | ||
6c1491fb BP |
1562 | /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id |
1563 | * extension on or off (according to 'flow_mod_table_id'). */ | |
1564 | struct ofpbuf * | |
1565 | ofputil_make_flow_mod_table_id(bool flow_mod_table_id) | |
1566 | { | |
73dbf4ab | 1567 | struct nx_flow_mod_table_id *nfmti; |
6c1491fb BP |
1568 | struct ofpbuf *msg; |
1569 | ||
982697a4 BP |
1570 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0); |
1571 | nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti); | |
6c1491fb BP |
1572 | nfmti->set = flow_mod_table_id; |
1573 | return msg; | |
1574 | } | |
1575 | ||
0fb88c18 BP |
1576 | struct ofputil_flow_mod_flag { |
1577 | uint16_t raw_flag; | |
1578 | enum ofp_version min_version, max_version; | |
1579 | enum ofputil_flow_mod_flags flag; | |
1580 | }; | |
1581 | ||
1582 | static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = { | |
1583 | { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM }, | |
1584 | { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP }, | |
1585 | { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION, | |
1586 | OFPUTIL_FF_EMERG }, | |
1587 | { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS }, | |
1588 | { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS }, | |
1589 | { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS }, | |
1590 | { 0, 0, 0, 0 }, | |
1591 | }; | |
1592 | ||
1593 | static enum ofperr | |
1594 | ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_, | |
1595 | enum ofp_flow_mod_command command, | |
1596 | enum ofp_version version, | |
1597 | enum ofputil_flow_mod_flags *flagsp) | |
1598 | { | |
1599 | uint16_t raw_flags = ntohs(raw_flags_); | |
1600 | const struct ofputil_flow_mod_flag *f; | |
1601 | ||
1602 | *flagsp = 0; | |
1603 | for (f = ofputil_flow_mod_flags; f->raw_flag; f++) { | |
1604 | if (raw_flags & f->raw_flag | |
1605 | && version >= f->min_version | |
1606 | && (!f->max_version || version <= f->max_version)) { | |
1607 | raw_flags &= ~f->raw_flag; | |
1608 | *flagsp |= f->flag; | |
1609 | } | |
1610 | } | |
1611 | ||
1612 | /* In OF1.0 and OF1.1, "add" always resets counters, and other commands | |
1613 | * never do. | |
1614 | * | |
1615 | * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command | |
1616 | * resets counters. */ | |
1617 | if ((version == OFP10_VERSION || version == OFP11_VERSION) | |
1618 | && command == OFPFC_ADD) { | |
1619 | *flagsp |= OFPUTIL_FF_RESET_COUNTS; | |
1620 | } | |
1621 | ||
1622 | return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0; | |
1623 | } | |
1624 | ||
1625 | static ovs_be16 | |
1626 | ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags, | |
1627 | enum ofp_version version) | |
1628 | { | |
1629 | const struct ofputil_flow_mod_flag *f; | |
1630 | uint16_t raw_flags; | |
1631 | ||
1632 | raw_flags = 0; | |
1633 | for (f = ofputil_flow_mod_flags; f->raw_flag; f++) { | |
1634 | if (f->flag & flags | |
1635 | && version >= f->min_version | |
1636 | && (!f->max_version || version <= f->max_version)) { | |
1637 | raw_flags |= f->raw_flag; | |
1638 | } | |
1639 | } | |
1640 | ||
1641 | return htons(raw_flags); | |
1642 | } | |
1643 | ||
7fa91113 BP |
1644 | /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract |
1645 | * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error | |
1646 | * code. | |
1647 | * | |
f25d0cf3 BP |
1648 | * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions. |
1649 | * The caller must initialize 'ofpacts' and retains ownership of it. | |
1650 | * 'fm->ofpacts' will point into the 'ofpacts' buffer. | |
1651 | * | |
1652 | * Does not validate the flow_mod actions. The caller should do that, with | |
1653 | * ofpacts_check(). */ | |
90bf1e07 | 1654 | enum ofperr |
a9a2da38 | 1655 | ofputil_decode_flow_mod(struct ofputil_flow_mod *fm, |
27527aa0 | 1656 | const struct ofp_header *oh, |
f25d0cf3 | 1657 | enum ofputil_protocol protocol, |
7e9f8266 BP |
1658 | struct ofpbuf *ofpacts, |
1659 | ofp_port_t max_port, uint8_t max_table) | |
2e4f5fcf | 1660 | { |
0fb88c18 BP |
1661 | ovs_be16 raw_flags; |
1662 | enum ofperr error; | |
2e4f5fcf | 1663 | struct ofpbuf b; |
982697a4 | 1664 | enum ofpraw raw; |
2e4f5fcf | 1665 | |
cc40d06b SH |
1666 | /* Ignored for non-delete actions */ |
1667 | fm->delete_reason = OFPRR_DELETE; | |
1668 | ||
2013493b | 1669 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
982697a4 | 1670 | raw = ofpraw_pull_assert(&b); |
aa319503 | 1671 | if (raw == OFPRAW_OFPT11_FLOW_MOD) { |
1828ae51 | 1672 | /* Standard OpenFlow 1.1+ flow_mod. */ |
aa319503 | 1673 | const struct ofp11_flow_mod *ofm; |
2e4f5fcf | 1674 | |
bbc32a88 | 1675 | ofm = ofpbuf_pull(&b, sizeof *ofm); |
2e4f5fcf | 1676 | |
81a76618 | 1677 | error = ofputil_pull_ofp11_match(&b, &fm->match, NULL); |
aa319503 BP |
1678 | if (error) { |
1679 | return error; | |
1c0b7503 BP |
1680 | } |
1681 | ||
2e4f5fcf | 1682 | /* Translate the message. */ |
81a76618 | 1683 | fm->priority = ntohs(ofm->priority); |
75fa58f8 BP |
1684 | if (ofm->command == OFPFC_ADD |
1685 | || (oh->version == OFP11_VERSION | |
1686 | && (ofm->command == OFPFC_MODIFY || | |
1687 | ofm->command == OFPFC_MODIFY_STRICT) | |
1688 | && ofm->cookie_mask == htonll(0))) { | |
1689 | /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does | |
1690 | * not match on the cookie is treated as an "add" if there is no | |
1691 | * match. */ | |
aa319503 BP |
1692 | fm->cookie = htonll(0); |
1693 | fm->cookie_mask = htonll(0); | |
1694 | fm->new_cookie = ofm->cookie; | |
1695 | } else { | |
aa319503 BP |
1696 | fm->cookie = ofm->cookie; |
1697 | fm->cookie_mask = ofm->cookie_mask; | |
b8266395 | 1698 | fm->new_cookie = OVS_BE64_MAX; |
aa319503 | 1699 | } |
23342857 | 1700 | fm->modify_cookie = false; |
aa319503 | 1701 | fm->command = ofm->command; |
0e197060 BP |
1702 | |
1703 | /* Get table ID. | |
1704 | * | |
083761ad SH |
1705 | * OF1.1 entirely forbids table_id == OFPTT_ALL. |
1706 | * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */ | |
aa319503 | 1707 | fm->table_id = ofm->table_id; |
083761ad | 1708 | if (fm->table_id == OFPTT_ALL |
0e197060 BP |
1709 | && (oh->version == OFP11_VERSION |
1710 | || (ofm->command != OFPFC_DELETE && | |
1711 | ofm->command != OFPFC_DELETE_STRICT))) { | |
1712 | return OFPERR_OFPFMFC_BAD_TABLE_ID; | |
1713 | } | |
1714 | ||
2e4f5fcf BP |
1715 | fm->idle_timeout = ntohs(ofm->idle_timeout); |
1716 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
ca26eb44 RB |
1717 | if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) { |
1718 | fm->importance = ntohs(ofm->importance); | |
1719 | } else { | |
1720 | fm->importance = 0; | |
1721 | } | |
2e4f5fcf | 1722 | fm->buffer_id = ntohl(ofm->buffer_id); |
aa319503 | 1723 | error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port); |
2e4f5fcf BP |
1724 | if (error) { |
1725 | return error; | |
1726 | } | |
7395c052 | 1727 | |
1fe4c0a0 BP |
1728 | fm->out_group = (ofm->command == OFPFC_DELETE || |
1729 | ofm->command == OFPFC_DELETE_STRICT | |
1730 | ? ntohl(ofm->out_group) | |
1731 | : OFPG11_ANY); | |
0fb88c18 | 1732 | raw_flags = ofm->flags; |
aa319503 | 1733 | } else { |
0fb88c18 BP |
1734 | uint16_t command; |
1735 | ||
aa319503 BP |
1736 | if (raw == OFPRAW_OFPT10_FLOW_MOD) { |
1737 | /* Standard OpenFlow 1.0 flow_mod. */ | |
1738 | const struct ofp10_flow_mod *ofm; | |
aa319503 BP |
1739 | |
1740 | /* Get the ofp10_flow_mod. */ | |
1741 | ofm = ofpbuf_pull(&b, sizeof *ofm); | |
1742 | ||
aa319503 | 1743 | /* Translate the rule. */ |
81a76618 BP |
1744 | ofputil_match_from_ofp10_match(&ofm->match, &fm->match); |
1745 | ofputil_normalize_match(&fm->match); | |
aa319503 | 1746 | |
81a76618 BP |
1747 | /* OpenFlow 1.0 says that exact-match rules have to have the |
1748 | * highest possible priority. */ | |
1749 | fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL) | |
1750 | ? ntohs(ofm->priority) | |
1751 | : UINT16_MAX); | |
1752 | ||
aa319503 BP |
1753 | /* Translate the message. */ |
1754 | command = ntohs(ofm->command); | |
1755 | fm->cookie = htonll(0); | |
1756 | fm->cookie_mask = htonll(0); | |
1757 | fm->new_cookie = ofm->cookie; | |
1758 | fm->idle_timeout = ntohs(ofm->idle_timeout); | |
1759 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
ca26eb44 | 1760 | fm->importance = 0; |
aa319503 | 1761 | fm->buffer_id = ntohl(ofm->buffer_id); |
4e022ec0 | 1762 | fm->out_port = u16_to_ofp(ntohs(ofm->out_port)); |
7395c052 | 1763 | fm->out_group = OFPG11_ANY; |
0fb88c18 | 1764 | raw_flags = ofm->flags; |
aa319503 BP |
1765 | } else if (raw == OFPRAW_NXT_FLOW_MOD) { |
1766 | /* Nicira extended flow_mod. */ | |
1767 | const struct nx_flow_mod *nfm; | |
aa319503 BP |
1768 | |
1769 | /* Dissect the message. */ | |
1770 | nfm = ofpbuf_pull(&b, sizeof *nfm); | |
81a76618 BP |
1771 | error = nx_pull_match(&b, ntohs(nfm->match_len), |
1772 | &fm->match, &fm->cookie, &fm->cookie_mask); | |
aa319503 BP |
1773 | if (error) { |
1774 | return error; | |
1775 | } | |
aa319503 BP |
1776 | |
1777 | /* Translate the message. */ | |
1778 | command = ntohs(nfm->command); | |
1779 | if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) { | |
1780 | /* Flow additions may only set a new cookie, not match an | |
1781 | * existing cookie. */ | |
1782 | return OFPERR_NXBRC_NXM_INVALID; | |
1783 | } | |
81a76618 | 1784 | fm->priority = ntohs(nfm->priority); |
aa319503 BP |
1785 | fm->new_cookie = nfm->cookie; |
1786 | fm->idle_timeout = ntohs(nfm->idle_timeout); | |
1787 | fm->hard_timeout = ntohs(nfm->hard_timeout); | |
ca26eb44 | 1788 | fm->importance = 0; |
aa319503 | 1789 | fm->buffer_id = ntohl(nfm->buffer_id); |
4e022ec0 | 1790 | fm->out_port = u16_to_ofp(ntohs(nfm->out_port)); |
7395c052 | 1791 | fm->out_group = OFPG11_ANY; |
0fb88c18 | 1792 | raw_flags = nfm->flags; |
aa319503 | 1793 | } else { |
428b2edd | 1794 | OVS_NOT_REACHED(); |
aa319503 BP |
1795 | } |
1796 | ||
b8266395 | 1797 | fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX; |
aa319503 BP |
1798 | if (protocol & OFPUTIL_P_TID) { |
1799 | fm->command = command & 0xff; | |
1800 | fm->table_id = command >> 8; | |
1801 | } else { | |
1f42be1c JR |
1802 | if (command > 0xff) { |
1803 | VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id " | |
1804 | "but flow_mod_table_id extension is not enabled"); | |
1805 | } | |
aa319503 BP |
1806 | fm->command = command; |
1807 | fm->table_id = 0xff; | |
e729e793 | 1808 | } |
2e4f5fcf BP |
1809 | } |
1810 | ||
1f42be1c JR |
1811 | if (fm->command > OFPFC_DELETE_STRICT) { |
1812 | return OFPERR_OFPFMFC_BAD_COMMAND; | |
1813 | } | |
1814 | ||
6fd6ed71 | 1815 | error = ofpacts_pull_openflow_instructions(&b, b.size, |
8f2cded4 BP |
1816 | oh->version, ofpacts); |
1817 | if (error) { | |
1818 | return error; | |
1819 | } | |
6fd6ed71 PS |
1820 | fm->ofpacts = ofpacts->data; |
1821 | fm->ofpacts_len = ofpacts->size; | |
6c1491fb | 1822 | |
0fb88c18 BP |
1823 | error = ofputil_decode_flow_mod_flags(raw_flags, fm->command, |
1824 | oh->version, &fm->flags); | |
1825 | if (error) { | |
1826 | return error; | |
1827 | } | |
1828 | ||
1829 | if (fm->flags & OFPUTIL_FF_EMERG) { | |
1830 | /* We do not support the OpenFlow 1.0 emergency flow cache, which | |
1831 | * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1. | |
1832 | * | |
1833 | * OpenFlow 1.0 specifies the error code to use when idle_timeout | |
1834 | * or hard_timeout is nonzero. Otherwise, there is no good error | |
1835 | * code, so just state that the flow table is full. */ | |
1836 | return (fm->hard_timeout || fm->idle_timeout | |
1837 | ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT | |
1838 | : OFPERR_OFPFMFC_TABLE_FULL); | |
1839 | } | |
1840 | ||
ba2fe8e9 BP |
1841 | return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len, |
1842 | &fm->match.flow, max_port, | |
1843 | fm->table_id, max_table, protocol); | |
2e4f5fcf BP |
1844 | } |
1845 | ||
638a19b0 JR |
1846 | static enum ofperr |
1847 | ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands, | |
1848 | struct ofpbuf *bands) | |
1849 | { | |
1850 | const struct ofp13_meter_band_header *ombh; | |
1851 | struct ofputil_meter_band *mb; | |
1852 | uint16_t n = 0; | |
1853 | ||
1854 | ombh = ofpbuf_try_pull(msg, len); | |
1855 | if (!ombh) { | |
1856 | return OFPERR_OFPBRC_BAD_LEN; | |
1857 | } | |
1858 | ||
1859 | while (len >= sizeof (struct ofp13_meter_band_drop)) { | |
1860 | size_t ombh_len = ntohs(ombh->len); | |
0445637d | 1861 | /* All supported band types have the same length. */ |
638a19b0 JR |
1862 | if (ombh_len != sizeof (struct ofp13_meter_band_drop)) { |
1863 | return OFPERR_OFPBRC_BAD_LEN; | |
1864 | } | |
1865 | mb = ofpbuf_put_uninit(bands, sizeof *mb); | |
1866 | mb->type = ntohs(ombh->type); | |
f99d6aa0 BP |
1867 | if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) { |
1868 | return OFPERR_OFPMMFC_BAD_BAND; | |
1869 | } | |
638a19b0 JR |
1870 | mb->rate = ntohl(ombh->rate); |
1871 | mb->burst_size = ntohl(ombh->burst_size); | |
1872 | mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ? | |
1873 | ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0; | |
1874 | n++; | |
1875 | len -= ombh_len; | |
db5a1019 AW |
1876 | ombh = ALIGNED_CAST(struct ofp13_meter_band_header *, |
1877 | (char *) ombh + ombh_len); | |
638a19b0 JR |
1878 | } |
1879 | if (len) { | |
1880 | return OFPERR_OFPBRC_BAD_LEN; | |
1881 | } | |
1882 | *n_bands = n; | |
1883 | return 0; | |
1884 | } | |
1885 | ||
1886 | enum ofperr | |
1887 | ofputil_decode_meter_mod(const struct ofp_header *oh, | |
1888 | struct ofputil_meter_mod *mm, | |
1889 | struct ofpbuf *bands) | |
1890 | { | |
1891 | const struct ofp13_meter_mod *omm; | |
1892 | struct ofpbuf b; | |
1893 | ||
1894 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1895 | ofpraw_pull_assert(&b); | |
1896 | omm = ofpbuf_pull(&b, sizeof *omm); | |
1897 | ||
1898 | /* Translate the message. */ | |
1899 | mm->command = ntohs(omm->command); | |
142cdb01 BP |
1900 | if (mm->command != OFPMC13_ADD && |
1901 | mm->command != OFPMC13_MODIFY && | |
1902 | mm->command != OFPMC13_DELETE) { | |
1903 | return OFPERR_OFPMMFC_BAD_COMMAND; | |
1904 | } | |
638a19b0 JR |
1905 | mm->meter.meter_id = ntohl(omm->meter_id); |
1906 | ||
1907 | if (mm->command == OFPMC13_DELETE) { | |
1908 | mm->meter.flags = 0; | |
1909 | mm->meter.n_bands = 0; | |
1910 | mm->meter.bands = NULL; | |
1911 | } else { | |
1912 | enum ofperr error; | |
1913 | ||
1914 | mm->meter.flags = ntohs(omm->flags); | |
13b1febe BP |
1915 | if (mm->meter.flags & OFPMF13_KBPS && |
1916 | mm->meter.flags & OFPMF13_PKTPS) { | |
1917 | return OFPERR_OFPMMFC_BAD_FLAGS; | |
1918 | } | |
6fd6ed71 | 1919 | mm->meter.bands = bands->data; |
638a19b0 | 1920 | |
6fd6ed71 | 1921 | error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands); |
638a19b0 JR |
1922 | if (error) { |
1923 | return error; | |
1924 | } | |
1925 | } | |
1926 | return 0; | |
1927 | } | |
1928 | ||
1929 | void | |
1930 | ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id) | |
1931 | { | |
1932 | const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh); | |
1933 | *meter_id = ntohl(omr->meter_id); | |
1934 | } | |
1935 | ||
1936 | struct ofpbuf * | |
1937 | ofputil_encode_meter_request(enum ofp_version ofp_version, | |
1938 | enum ofputil_meter_request_type type, | |
1939 | uint32_t meter_id) | |
1940 | { | |
1941 | struct ofpbuf *msg; | |
1942 | ||
1943 | enum ofpraw raw; | |
1944 | ||
1945 | switch (type) { | |
1946 | case OFPUTIL_METER_CONFIG: | |
1947 | raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST; | |
1948 | break; | |
1949 | case OFPUTIL_METER_STATS: | |
1950 | raw = OFPRAW_OFPST13_METER_REQUEST; | |
1951 | break; | |
1952 | default: | |
1953 | case OFPUTIL_METER_FEATURES: | |
1954 | raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST; | |
1955 | break; | |
1956 | } | |
1957 | ||
1958 | msg = ofpraw_alloc(raw, ofp_version, 0); | |
1959 | ||
1960 | if (type != OFPUTIL_METER_FEATURES) { | |
1961 | struct ofp13_meter_multipart_request *omr; | |
1962 | omr = ofpbuf_put_zeros(msg, sizeof *omr); | |
1963 | omr->meter_id = htonl(meter_id); | |
1964 | } | |
1965 | return msg; | |
1966 | } | |
1967 | ||
1968 | static void | |
1969 | ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb, | |
1970 | struct ofpbuf *msg) | |
1971 | { | |
1972 | uint16_t n = 0; | |
1973 | ||
1974 | for (n = 0; n < n_bands; ++n) { | |
0445637d | 1975 | /* Currently all band types have same size. */ |
638a19b0 JR |
1976 | struct ofp13_meter_band_dscp_remark *ombh; |
1977 | size_t ombh_len = sizeof *ombh; | |
1978 | ||
1979 | ombh = ofpbuf_put_zeros(msg, ombh_len); | |
1980 | ||
1981 | ombh->type = htons(mb->type); | |
1982 | ombh->len = htons(ombh_len); | |
1983 | ombh->rate = htonl(mb->rate); | |
1984 | ombh->burst_size = htonl(mb->burst_size); | |
1985 | ombh->prec_level = mb->prec_level; | |
1986 | ||
1987 | mb++; | |
1988 | } | |
1989 | } | |
1990 | ||
1991 | /* Encode a meter stat for 'mc' and append it to 'replies'. */ | |
1992 | void | |
ca6ba700 | 1993 | ofputil_append_meter_config(struct ovs_list *replies, |
638a19b0 JR |
1994 | const struct ofputil_meter_config *mc) |
1995 | { | |
1996 | struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); | |
6fd6ed71 | 1997 | size_t start_ofs = msg->size; |
638a19b0 JR |
1998 | struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply); |
1999 | reply->flags = htons(mc->flags); | |
2000 | reply->meter_id = htonl(mc->meter_id); | |
2001 | ||
2002 | ofputil_put_bands(mc->n_bands, mc->bands, msg); | |
2003 | ||
6fd6ed71 | 2004 | reply->length = htons(msg->size - start_ofs); |
638a19b0 JR |
2005 | |
2006 | ofpmp_postappend(replies, start_ofs); | |
2007 | } | |
2008 | ||
2009 | /* Encode a meter stat for 'ms' and append it to 'replies'. */ | |
2010 | void | |
ca6ba700 | 2011 | ofputil_append_meter_stats(struct ovs_list *replies, |
638a19b0 JR |
2012 | const struct ofputil_meter_stats *ms) |
2013 | { | |
2014 | struct ofp13_meter_stats *reply; | |
2015 | uint16_t n = 0; | |
2016 | uint16_t len; | |
2017 | ||
2018 | len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats); | |
2019 | reply = ofpmp_append(replies, len); | |
2020 | ||
2021 | reply->meter_id = htonl(ms->meter_id); | |
2022 | reply->len = htons(len); | |
2023 | memset(reply->pad, 0, sizeof reply->pad); | |
2024 | reply->flow_count = htonl(ms->flow_count); | |
2025 | reply->packet_in_count = htonll(ms->packet_in_count); | |
2026 | reply->byte_in_count = htonll(ms->byte_in_count); | |
2027 | reply->duration_sec = htonl(ms->duration_sec); | |
2028 | reply->duration_nsec = htonl(ms->duration_nsec); | |
2029 | ||
2030 | for (n = 0; n < ms->n_bands; ++n) { | |
2031 | const struct ofputil_meter_band_stats *src = &ms->bands[n]; | |
2032 | struct ofp13_meter_band_stats *dst = &reply->band_stats[n]; | |
2033 | ||
2034 | dst->packet_band_count = htonll(src->packet_count); | |
2035 | dst->byte_band_count = htonll(src->byte_count); | |
2036 | } | |
2037 | } | |
2038 | ||
2039 | /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract | |
2040 | * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into | |
2041 | * 'bands'. The caller must have initialized 'bands' and retains ownership of | |
2042 | * it across the call. | |
2043 | * | |
2044 | * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow | |
2045 | * message. Calling this function multiple times for a single 'msg' iterates | |
2046 | * through the replies. 'bands' is cleared for each reply. | |
2047 | * | |
2048 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
2049 | * otherwise a positive errno value. */ | |
2050 | int | |
2051 | ofputil_decode_meter_config(struct ofpbuf *msg, | |
2052 | struct ofputil_meter_config *mc, | |
2053 | struct ofpbuf *bands) | |
2054 | { | |
2055 | const struct ofp13_meter_config *omc; | |
2056 | enum ofperr err; | |
2057 | ||
2058 | /* Pull OpenFlow headers for the first call. */ | |
6fd6ed71 | 2059 | if (!msg->header) { |
638a19b0 JR |
2060 | ofpraw_pull_assert(msg); |
2061 | } | |
2062 | ||
6fd6ed71 | 2063 | if (!msg->size) { |
638a19b0 JR |
2064 | return EOF; |
2065 | } | |
2066 | ||
2067 | omc = ofpbuf_try_pull(msg, sizeof *omc); | |
2068 | if (!omc) { | |
0445637d | 2069 | VLOG_WARN_RL(&bad_ofmsg_rl, |
437d0d22 | 2070 | "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end", |
6fd6ed71 | 2071 | msg->size); |
638a19b0 JR |
2072 | return OFPERR_OFPBRC_BAD_LEN; |
2073 | } | |
2074 | ||
2075 | ofpbuf_clear(bands); | |
2076 | err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc, | |
2077 | &mc->n_bands, bands); | |
2078 | if (err) { | |
2079 | return err; | |
2080 | } | |
2081 | mc->meter_id = ntohl(omc->meter_id); | |
2082 | mc->flags = ntohs(omc->flags); | |
6fd6ed71 | 2083 | mc->bands = bands->data; |
638a19b0 JR |
2084 | |
2085 | return 0; | |
2086 | } | |
2087 | ||
2088 | static enum ofperr | |
2089 | ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands, | |
2090 | struct ofpbuf *bands) | |
2091 | { | |
2092 | const struct ofp13_meter_band_stats *ombs; | |
2093 | struct ofputil_meter_band_stats *mbs; | |
2094 | uint16_t n, i; | |
2095 | ||
0445637d JR |
2096 | ombs = ofpbuf_try_pull(msg, len); |
2097 | if (!ombs) { | |
2098 | return OFPERR_OFPBRC_BAD_LEN; | |
2099 | } | |
2100 | ||
638a19b0 JR |
2101 | n = len / sizeof *ombs; |
2102 | if (len != n * sizeof *ombs) { | |
2103 | return OFPERR_OFPBRC_BAD_LEN; | |
2104 | } | |
2105 | ||
638a19b0 JR |
2106 | mbs = ofpbuf_put_uninit(bands, len); |
2107 | ||
2108 | for (i = 0; i < n; ++i) { | |
2109 | mbs[i].packet_count = ntohll(ombs[i].packet_band_count); | |
2110 | mbs[i].byte_count = ntohll(ombs[i].byte_band_count); | |
2111 | } | |
2112 | *n_bands = n; | |
2113 | return 0; | |
2114 | } | |
2115 | ||
2116 | /* Converts an OFPMP_METER reply in 'msg' into an abstract | |
2117 | * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats | |
2118 | * decoded into 'bands'. | |
2119 | * | |
2120 | * Multiple OFPMP_METER replies can be packed into a single OpenFlow | |
2121 | * message. Calling this function multiple times for a single 'msg' iterates | |
2122 | * through the replies. 'bands' is cleared for each reply. | |
2123 | * | |
2124 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
2125 | * otherwise a positive errno value. */ | |
2126 | int | |
2127 | ofputil_decode_meter_stats(struct ofpbuf *msg, | |
2128 | struct ofputil_meter_stats *ms, | |
2129 | struct ofpbuf *bands) | |
2130 | { | |
2131 | const struct ofp13_meter_stats *oms; | |
638a19b0 JR |
2132 | enum ofperr err; |
2133 | ||
2134 | /* Pull OpenFlow headers for the first call. */ | |
6fd6ed71 | 2135 | if (!msg->header) { |
638a19b0 JR |
2136 | ofpraw_pull_assert(msg); |
2137 | } | |
2138 | ||
6fd6ed71 | 2139 | if (!msg->size) { |
638a19b0 JR |
2140 | return EOF; |
2141 | } | |
2142 | ||
2143 | oms = ofpbuf_try_pull(msg, sizeof *oms); | |
2144 | if (!oms) { | |
0445637d | 2145 | VLOG_WARN_RL(&bad_ofmsg_rl, |
437d0d22 | 2146 | "OFPMP_METER reply has %"PRIu32" leftover bytes at end", |
6fd6ed71 | 2147 | msg->size); |
638a19b0 JR |
2148 | return OFPERR_OFPBRC_BAD_LEN; |
2149 | } | |
638a19b0 JR |
2150 | |
2151 | ofpbuf_clear(bands); | |
0445637d JR |
2152 | err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms, |
2153 | &ms->n_bands, bands); | |
638a19b0 JR |
2154 | if (err) { |
2155 | return err; | |
2156 | } | |
2157 | ms->meter_id = ntohl(oms->meter_id); | |
2158 | ms->flow_count = ntohl(oms->flow_count); | |
2159 | ms->packet_in_count = ntohll(oms->packet_in_count); | |
2160 | ms->byte_in_count = ntohll(oms->byte_in_count); | |
2161 | ms->duration_sec = ntohl(oms->duration_sec); | |
2162 | ms->duration_nsec = ntohl(oms->duration_nsec); | |
6fd6ed71 | 2163 | ms->bands = bands->data; |
638a19b0 JR |
2164 | |
2165 | return 0; | |
2166 | } | |
2167 | ||
2168 | void | |
2169 | ofputil_decode_meter_features(const struct ofp_header *oh, | |
2170 | struct ofputil_meter_features *mf) | |
2171 | { | |
2172 | const struct ofp13_meter_features *omf = ofpmsg_body(oh); | |
2173 | ||
2174 | mf->max_meters = ntohl(omf->max_meter); | |
2175 | mf->band_types = ntohl(omf->band_types); | |
2176 | mf->capabilities = ntohl(omf->capabilities); | |
2177 | mf->max_bands = omf->max_bands; | |
2178 | mf->max_color = omf->max_color; | |
2179 | } | |
2180 | ||
2181 | struct ofpbuf * | |
2182 | ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf, | |
2183 | const struct ofp_header *request) | |
2184 | { | |
2185 | struct ofpbuf *reply; | |
2186 | struct ofp13_meter_features *omf; | |
2187 | ||
2188 | reply = ofpraw_alloc_stats_reply(request, 0); | |
2189 | omf = ofpbuf_put_zeros(reply, sizeof *omf); | |
2190 | ||
2191 | omf->max_meter = htonl(mf->max_meters); | |
2192 | omf->band_types = htonl(mf->band_types); | |
2193 | omf->capabilities = htonl(mf->capabilities); | |
2194 | omf->max_bands = mf->max_bands; | |
2195 | omf->max_color = mf->max_color; | |
2196 | ||
2197 | return reply; | |
2198 | } | |
2199 | ||
2200 | struct ofpbuf * | |
2201 | ofputil_encode_meter_mod(enum ofp_version ofp_version, | |
2202 | const struct ofputil_meter_mod *mm) | |
2203 | { | |
2204 | struct ofpbuf *msg; | |
2205 | ||
2206 | struct ofp13_meter_mod *omm; | |
2207 | ||
2208 | msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version, | |
2209 | NXM_TYPICAL_LEN + mm->meter.n_bands * 16); | |
2210 | omm = ofpbuf_put_zeros(msg, sizeof *omm); | |
2211 | omm->command = htons(mm->command); | |
2212 | if (mm->command != OFPMC13_DELETE) { | |
2213 | omm->flags = htons(mm->meter.flags); | |
2214 | } | |
2215 | omm->meter_id = htonl(mm->meter.meter_id); | |
2216 | ||
2217 | ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg); | |
2218 | ||
2219 | ofpmsg_update_length(msg); | |
2220 | return msg; | |
2221 | } | |
2222 | ||
aa6305ea SH |
2223 | static ovs_be16 |
2224 | ofputil_tid_command(const struct ofputil_flow_mod *fm, | |
2225 | enum ofputil_protocol protocol) | |
2226 | { | |
2227 | return htons(protocol & OFPUTIL_P_TID | |
2228 | ? (fm->command & 0xff) | (fm->table_id << 8) | |
2229 | : fm->command); | |
2230 | } | |
2231 | ||
2e4f5fcf | 2232 | /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to |
6cbe2c0f | 2233 | * 'protocol' and returns the message. */ |
2e4f5fcf | 2234 | struct ofpbuf * |
a9a2da38 | 2235 | ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm, |
27527aa0 | 2236 | enum ofputil_protocol protocol) |
2e4f5fcf | 2237 | { |
0fb88c18 BP |
2238 | enum ofp_version version = ofputil_protocol_to_ofp_version(protocol); |
2239 | ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version); | |
2e4f5fcf BP |
2240 | struct ofpbuf *msg; |
2241 | ||
27527aa0 | 2242 | switch (protocol) { |
75fa58f8 | 2243 | case OFPUTIL_P_OF11_STD: |
2e1ae200 | 2244 | case OFPUTIL_P_OF12_OXM: |
c37c0382 | 2245 | case OFPUTIL_P_OF13_OXM: |
42dccab5 BP |
2246 | case OFPUTIL_P_OF14_OXM: |
2247 | case OFPUTIL_P_OF15_OXM: { | |
aa6305ea | 2248 | struct ofp11_flow_mod *ofm; |
75fa58f8 | 2249 | int tailroom; |
aa6305ea | 2250 | |
75fa58f8 | 2251 | tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len; |
0fb88c18 | 2252 | msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom); |
aa6305ea | 2253 | ofm = ofpbuf_put_zeros(msg, sizeof *ofm); |
75fa58f8 BP |
2254 | if ((protocol == OFPUTIL_P_OF11_STD |
2255 | && (fm->command == OFPFC_MODIFY || | |
2256 | fm->command == OFPFC_MODIFY_STRICT) | |
2257 | && fm->cookie_mask == htonll(0)) | |
2258 | || fm->command == OFPFC_ADD) { | |
a5ff8823 SH |
2259 | ofm->cookie = fm->new_cookie; |
2260 | } else { | |
ba5cc068 | 2261 | ofm->cookie = fm->cookie & fm->cookie_mask; |
a5ff8823 | 2262 | } |
aa6305ea | 2263 | ofm->cookie_mask = fm->cookie_mask; |
083761ad | 2264 | if (fm->table_id != OFPTT_ALL |
0e197060 BP |
2265 | || (protocol != OFPUTIL_P_OF11_STD |
2266 | && (fm->command == OFPFC_DELETE || | |
2267 | fm->command == OFPFC_DELETE_STRICT))) { | |
2268 | ofm->table_id = fm->table_id; | |
2269 | } else { | |
2270 | ofm->table_id = 0; | |
2271 | } | |
aa6305ea SH |
2272 | ofm->command = fm->command; |
2273 | ofm->idle_timeout = htons(fm->idle_timeout); | |
2274 | ofm->hard_timeout = htons(fm->hard_timeout); | |
81a76618 | 2275 | ofm->priority = htons(fm->priority); |
aa6305ea SH |
2276 | ofm->buffer_id = htonl(fm->buffer_id); |
2277 | ofm->out_port = ofputil_port_to_ofp11(fm->out_port); | |
7395c052 | 2278 | ofm->out_group = htonl(fm->out_group); |
0fb88c18 | 2279 | ofm->flags = raw_flags; |
ca26eb44 RB |
2280 | if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) { |
2281 | ofm->importance = htons(fm->importance); | |
2282 | } else { | |
2283 | ofm->importance = 0; | |
2284 | } | |
75fa58f8 | 2285 | ofputil_put_ofp11_match(msg, &fm->match, protocol); |
e3f8f887 JR |
2286 | ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg, |
2287 | version); | |
aa6305ea SH |
2288 | break; |
2289 | } | |
2290 | ||
85813857 BP |
2291 | case OFPUTIL_P_OF10_STD: |
2292 | case OFPUTIL_P_OF10_STD_TID: { | |
3f192f23 SH |
2293 | struct ofp10_flow_mod *ofm; |
2294 | ||
982697a4 BP |
2295 | msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION, |
2296 | fm->ofpacts_len); | |
2297 | ofm = ofpbuf_put_zeros(msg, sizeof *ofm); | |
81a76618 | 2298 | ofputil_match_to_ofp10_match(&fm->match, &ofm->match); |
623e1caf | 2299 | ofm->cookie = fm->new_cookie; |
aa6305ea | 2300 | ofm->command = ofputil_tid_command(fm, protocol); |
2e4f5fcf BP |
2301 | ofm->idle_timeout = htons(fm->idle_timeout); |
2302 | ofm->hard_timeout = htons(fm->hard_timeout); | |
81a76618 | 2303 | ofm->priority = htons(fm->priority); |
2e4f5fcf | 2304 | ofm->buffer_id = htonl(fm->buffer_id); |
4e022ec0 | 2305 | ofm->out_port = htons(ofp_to_u16(fm->out_port)); |
0fb88c18 | 2306 | ofm->flags = raw_flags; |
e3f8f887 JR |
2307 | ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg, |
2308 | version); | |
27527aa0 | 2309 | break; |
3f192f23 | 2310 | } |
2e4f5fcf | 2311 | |
85813857 BP |
2312 | case OFPUTIL_P_OF10_NXM: |
2313 | case OFPUTIL_P_OF10_NXM_TID: { | |
3f192f23 SH |
2314 | struct nx_flow_mod *nfm; |
2315 | int match_len; | |
2316 | ||
982697a4 BP |
2317 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION, |
2318 | NXM_TYPICAL_LEN + fm->ofpacts_len); | |
2319 | nfm = ofpbuf_put_zeros(msg, sizeof *nfm); | |
aa6305ea | 2320 | nfm->command = ofputil_tid_command(fm, protocol); |
623e1caf | 2321 | nfm->cookie = fm->new_cookie; |
81a76618 | 2322 | match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask); |
6fd6ed71 | 2323 | nfm = msg->msg; |
2e4f5fcf BP |
2324 | nfm->idle_timeout = htons(fm->idle_timeout); |
2325 | nfm->hard_timeout = htons(fm->hard_timeout); | |
81a76618 | 2326 | nfm->priority = htons(fm->priority); |
2e4f5fcf | 2327 | nfm->buffer_id = htonl(fm->buffer_id); |
4e022ec0 | 2328 | nfm->out_port = htons(ofp_to_u16(fm->out_port)); |
0fb88c18 | 2329 | nfm->flags = raw_flags; |
2e4f5fcf | 2330 | nfm->match_len = htons(match_len); |
e3f8f887 JR |
2331 | ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg, |
2332 | version); | |
27527aa0 | 2333 | break; |
3f192f23 | 2334 | } |
27527aa0 BP |
2335 | |
2336 | default: | |
428b2edd | 2337 | OVS_NOT_REACHED(); |
2e4f5fcf BP |
2338 | } |
2339 | ||
982697a4 | 2340 | ofpmsg_update_length(msg); |
2e4f5fcf BP |
2341 | return msg; |
2342 | } | |
2343 | ||
90bf1e07 | 2344 | static enum ofperr |
0157ad3a SH |
2345 | ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr, |
2346 | const struct ofp10_flow_stats_request *ofsr, | |
2347 | bool aggregate) | |
2e4f5fcf | 2348 | { |
2e4f5fcf | 2349 | fsr->aggregate = aggregate; |
81a76618 | 2350 | ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match); |
4e022ec0 | 2351 | fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port)); |
7395c052 | 2352 | fsr->out_group = OFPG11_ANY; |
2e4f5fcf | 2353 | fsr->table_id = ofsr->table_id; |
e729e793 | 2354 | fsr->cookie = fsr->cookie_mask = htonll(0); |
2e4f5fcf BP |
2355 | |
2356 | return 0; | |
2357 | } | |
2358 | ||
0157ad3a SH |
2359 | static enum ofperr |
2360 | ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr, | |
2361 | struct ofpbuf *b, bool aggregate) | |
2362 | { | |
2363 | const struct ofp11_flow_stats_request *ofsr; | |
2364 | enum ofperr error; | |
2365 | ||
2366 | ofsr = ofpbuf_pull(b, sizeof *ofsr); | |
2367 | fsr->aggregate = aggregate; | |
2368 | fsr->table_id = ofsr->table_id; | |
2369 | error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port); | |
2370 | if (error) { | |
2371 | return error; | |
2372 | } | |
7395c052 | 2373 | fsr->out_group = ntohl(ofsr->out_group); |
0157ad3a SH |
2374 | fsr->cookie = ofsr->cookie; |
2375 | fsr->cookie_mask = ofsr->cookie_mask; | |
81a76618 | 2376 | error = ofputil_pull_ofp11_match(b, &fsr->match, NULL); |
0157ad3a SH |
2377 | if (error) { |
2378 | return error; | |
2379 | } | |
2380 | ||
2381 | return 0; | |
2382 | } | |
2383 | ||
90bf1e07 | 2384 | static enum ofperr |
81d1ea94 | 2385 | ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr, |
982697a4 | 2386 | struct ofpbuf *b, bool aggregate) |
2e4f5fcf BP |
2387 | { |
2388 | const struct nx_flow_stats_request *nfsr; | |
90bf1e07 | 2389 | enum ofperr error; |
2e4f5fcf | 2390 | |
982697a4 | 2391 | nfsr = ofpbuf_pull(b, sizeof *nfsr); |
81a76618 | 2392 | error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match, |
e729e793 | 2393 | &fsr->cookie, &fsr->cookie_mask); |
2e4f5fcf BP |
2394 | if (error) { |
2395 | return error; | |
2396 | } | |
6fd6ed71 | 2397 | if (b->size) { |
90bf1e07 | 2398 | return OFPERR_OFPBRC_BAD_LEN; |
2e4f5fcf BP |
2399 | } |
2400 | ||
2401 | fsr->aggregate = aggregate; | |
4e022ec0 | 2402 | fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port)); |
7395c052 | 2403 | fsr->out_group = OFPG11_ANY; |
2e4f5fcf BP |
2404 | fsr->table_id = nfsr->table_id; |
2405 | ||
2406 | return 0; | |
2407 | } | |
2408 | ||
e8f9a7bb VG |
2409 | /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified |
2410 | * 'port', suitable for OpenFlow version 'version'. */ | |
2411 | struct ofpbuf * | |
2412 | ofputil_encode_queue_get_config_request(enum ofp_version version, | |
2413 | ofp_port_t port) | |
2414 | { | |
2415 | struct ofpbuf *request; | |
2416 | ||
2417 | if (version == OFP10_VERSION) { | |
2418 | struct ofp10_queue_get_config_request *qgcr10; | |
2419 | ||
2420 | request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST, | |
2421 | version, 0); | |
2422 | qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10); | |
2423 | qgcr10->port = htons(ofp_to_u16(port)); | |
2424 | } else { | |
2425 | struct ofp11_queue_get_config_request *qgcr11; | |
2426 | ||
2427 | request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST, | |
2428 | version, 0); | |
2429 | qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11); | |
2430 | qgcr11->port = ofputil_port_to_ofp11(port); | |
2431 | } | |
2432 | ||
2433 | return request; | |
2434 | } | |
2435 | ||
2436 | /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the | |
2437 | * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error | |
2438 | * code. */ | |
2439 | enum ofperr | |
2440 | ofputil_decode_queue_get_config_request(const struct ofp_header *oh, | |
2441 | ofp_port_t *port) | |
2442 | { | |
2443 | const struct ofp10_queue_get_config_request *qgcr10; | |
2444 | const struct ofp11_queue_get_config_request *qgcr11; | |
2445 | enum ofpraw raw; | |
2446 | struct ofpbuf b; | |
2447 | ||
2448 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2449 | raw = ofpraw_pull_assert(&b); | |
2450 | ||
2451 | switch ((int) raw) { | |
2452 | case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST: | |
6fd6ed71 | 2453 | qgcr10 = b.data; |
e8f9a7bb VG |
2454 | *port = u16_to_ofp(ntohs(qgcr10->port)); |
2455 | return 0; | |
2456 | ||
2457 | case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST: | |
6fd6ed71 | 2458 | qgcr11 = b.data; |
e8f9a7bb VG |
2459 | return ofputil_port_from_ofp11(qgcr11->port, port); |
2460 | } | |
2461 | ||
428b2edd | 2462 | OVS_NOT_REACHED(); |
e8f9a7bb VG |
2463 | } |
2464 | ||
2465 | /* Constructs and returns the beginning of a reply to | |
2466 | * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about | |
2467 | * individual queues with ofputil_append_queue_get_config_reply(). */ | |
2468 | struct ofpbuf * | |
2469 | ofputil_encode_queue_get_config_reply(const struct ofp_header *oh) | |
2470 | { | |
2471 | struct ofp10_queue_get_config_reply *qgcr10; | |
2472 | struct ofp11_queue_get_config_reply *qgcr11; | |
2473 | struct ofpbuf *reply; | |
2474 | enum ofperr error; | |
2475 | struct ofpbuf b; | |
2476 | enum ofpraw raw; | |
2477 | ofp_port_t port; | |
2478 | ||
2479 | error = ofputil_decode_queue_get_config_request(oh, &port); | |
2480 | ovs_assert(!error); | |
2481 | ||
2482 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2483 | raw = ofpraw_pull_assert(&b); | |
2484 | ||
2485 | switch ((int) raw) { | |
2486 | case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST: | |
2487 | reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY, | |
2488 | oh, 0); | |
2489 | qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10); | |
2490 | qgcr10->port = htons(ofp_to_u16(port)); | |
2491 | break; | |
2492 | ||
2493 | case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST: | |
2494 | reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY, | |
2495 | oh, 0); | |
2496 | qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11); | |
2497 | qgcr11->port = ofputil_port_to_ofp11(port); | |
2498 | break; | |
2499 | ||
2500 | default: | |
428b2edd | 2501 | OVS_NOT_REACHED(); |
e8f9a7bb VG |
2502 | } |
2503 | ||
2504 | return reply; | |
2505 | } | |
2506 | ||
2507 | static void | |
2508 | put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property, | |
2509 | uint16_t rate) | |
2510 | { | |
2511 | if (rate != UINT16_MAX) { | |
2512 | struct ofp_queue_prop_rate *oqpr; | |
2513 | ||
2514 | oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr); | |
2515 | oqpr->prop_header.property = htons(property); | |
2516 | oqpr->prop_header.len = htons(sizeof *oqpr); | |
2517 | oqpr->rate = htons(rate); | |
2518 | } | |
2519 | } | |
2520 | ||
2521 | /* Appends a queue description for 'queue_id' to the | |
2522 | * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */ | |
2523 | void | |
2524 | ofputil_append_queue_get_config_reply(struct ofpbuf *reply, | |
2525 | const struct ofputil_queue_config *oqc) | |
2526 | { | |
6fd6ed71 | 2527 | const struct ofp_header *oh = reply->data; |
e8f9a7bb VG |
2528 | size_t start_ofs, len_ofs; |
2529 | ovs_be16 *len; | |
2530 | ||
6fd6ed71 | 2531 | start_ofs = reply->size; |
e8f9a7bb VG |
2532 | if (oh->version < OFP12_VERSION) { |
2533 | struct ofp10_packet_queue *opq10; | |
2534 | ||
2535 | opq10 = ofpbuf_put_zeros(reply, sizeof *opq10); | |
2536 | opq10->queue_id = htonl(oqc->queue_id); | |
6fd6ed71 | 2537 | len_ofs = (char *) &opq10->len - (char *) reply->data; |
e8f9a7bb VG |
2538 | } else { |
2539 | struct ofp11_queue_get_config_reply *qgcr11; | |
2540 | struct ofp12_packet_queue *opq12; | |
2541 | ovs_be32 port; | |
2542 | ||
6fd6ed71 | 2543 | qgcr11 = reply->msg; |
e8f9a7bb VG |
2544 | port = qgcr11->port; |
2545 | ||
2546 | opq12 = ofpbuf_put_zeros(reply, sizeof *opq12); | |
2547 | opq12->port = port; | |
2548 | opq12->queue_id = htonl(oqc->queue_id); | |
6fd6ed71 | 2549 | len_ofs = (char *) &opq12->len - (char *) reply->data; |
e8f9a7bb VG |
2550 | } |
2551 | ||
2552 | put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate); | |
2553 | put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate); | |
2554 | ||
2555 | len = ofpbuf_at(reply, len_ofs, sizeof *len); | |
6fd6ed71 | 2556 | *len = htons(reply->size - start_ofs); |
e8f9a7bb VG |
2557 | } |
2558 | ||
2559 | /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and | |
2560 | * stores in '*port' the port that the reply is about. The caller may call | |
2561 | * ofputil_pull_queue_get_config_reply() to obtain information about individual | |
2562 | * queues included in the reply. Returns 0 if successful, otherwise an | |
2563 | * ofperr.*/ | |
2564 | enum ofperr | |
2565 | ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port) | |
2566 | { | |
2567 | const struct ofp10_queue_get_config_reply *qgcr10; | |
2568 | const struct ofp11_queue_get_config_reply *qgcr11; | |
2569 | enum ofpraw raw; | |
2570 | ||
2571 | raw = ofpraw_pull_assert(reply); | |
2572 | switch ((int) raw) { | |
2573 | case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY: | |
2574 | qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10); | |
2575 | *port = u16_to_ofp(ntohs(qgcr10->port)); | |
2576 | return 0; | |
2577 | ||
2578 | case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY: | |
2579 | qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11); | |
2580 | return ofputil_port_from_ofp11(qgcr11->port, port); | |
2581 | } | |
2582 | ||
428b2edd | 2583 | OVS_NOT_REACHED(); |
e8f9a7bb VG |
2584 | } |
2585 | ||
2586 | static enum ofperr | |
2587 | parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate) | |
2588 | { | |
2589 | const struct ofp_queue_prop_rate *oqpr; | |
2590 | ||
2591 | if (hdr->len == htons(sizeof *oqpr)) { | |
2592 | oqpr = (const struct ofp_queue_prop_rate *) hdr; | |
2593 | *rate = ntohs(oqpr->rate); | |
2594 | return 0; | |
2595 | } else { | |
2596 | return OFPERR_OFPBRC_BAD_LEN; | |
2597 | } | |
2598 | } | |
2599 | ||
2600 | /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in | |
2601 | * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply() | |
2602 | * must already have pulled off the main header. | |
2603 | * | |
2604 | * This function returns EOF if the last queue has already been decoded, 0 if a | |
2605 | * queue was successfully decoded into '*queue', or an ofperr if there was a | |
2606 | * problem decoding 'reply'. */ | |
2607 | int | |
2608 | ofputil_pull_queue_get_config_reply(struct ofpbuf *reply, | |
2609 | struct ofputil_queue_config *queue) | |
2610 | { | |
2611 | const struct ofp_header *oh; | |
2612 | unsigned int opq_len; | |
2613 | unsigned int len; | |
2614 | ||
6fd6ed71 | 2615 | if (!reply->size) { |
e8f9a7bb VG |
2616 | return EOF; |
2617 | } | |
2618 | ||
2619 | queue->min_rate = UINT16_MAX; | |
2620 | queue->max_rate = UINT16_MAX; | |
2621 | ||
6fd6ed71 | 2622 | oh = reply->header; |
e8f9a7bb VG |
2623 | if (oh->version < OFP12_VERSION) { |
2624 | const struct ofp10_packet_queue *opq10; | |
2625 | ||
2626 | opq10 = ofpbuf_try_pull(reply, sizeof *opq10); | |
2627 | if (!opq10) { | |
2628 | return OFPERR_OFPBRC_BAD_LEN; | |
2629 | } | |
2630 | queue->queue_id = ntohl(opq10->queue_id); | |
2631 | len = ntohs(opq10->len); | |
2632 | opq_len = sizeof *opq10; | |
2633 | } else { | |
2634 | const struct ofp12_packet_queue *opq12; | |
2635 | ||
2636 | opq12 = ofpbuf_try_pull(reply, sizeof *opq12); | |
2637 | if (!opq12) { | |
2638 | return OFPERR_OFPBRC_BAD_LEN; | |
2639 | } | |
2640 | queue->queue_id = ntohl(opq12->queue_id); | |
2641 | len = ntohs(opq12->len); | |
2642 | opq_len = sizeof *opq12; | |
2643 | } | |
2644 | ||
6fd6ed71 | 2645 | if (len < opq_len || len > reply->size + opq_len || len % 8) { |
e8f9a7bb VG |
2646 | return OFPERR_OFPBRC_BAD_LEN; |
2647 | } | |
2648 | len -= opq_len; | |
2649 | ||
2650 | while (len > 0) { | |
2651 | const struct ofp_queue_prop_header *hdr; | |
2652 | unsigned int property; | |
2653 | unsigned int prop_len; | |
2654 | enum ofperr error = 0; | |
2655 | ||
2656 | hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr); | |
2657 | prop_len = ntohs(hdr->len); | |
6fd6ed71 | 2658 | if (prop_len < sizeof *hdr || prop_len > reply->size || prop_len % 8) { |
e8f9a7bb VG |
2659 | return OFPERR_OFPBRC_BAD_LEN; |
2660 | } | |
2661 | ||
2662 | property = ntohs(hdr->property); | |
2663 | switch (property) { | |
2664 | case OFPQT_MIN_RATE: | |
2665 | error = parse_queue_rate(hdr, &queue->min_rate); | |
2666 | break; | |
2667 | ||
2668 | case OFPQT_MAX_RATE: | |
2669 | error = parse_queue_rate(hdr, &queue->max_rate); | |
2670 | break; | |
2671 | ||
2672 | default: | |
2673 | VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property); | |
2674 | break; | |
2675 | } | |
2676 | if (error) { | |
2677 | return error; | |
2678 | } | |
2679 | ||
2680 | ofpbuf_pull(reply, prop_len); | |
2681 | len -= prop_len; | |
2682 | } | |
2683 | return 0; | |
2684 | } | |
2685 | ||
2e4f5fcf | 2686 | /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE |
b78f6b77 BP |
2687 | * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if |
2688 | * successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 2689 | enum ofperr |
81d1ea94 | 2690 | ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr, |
b78f6b77 | 2691 | const struct ofp_header *oh) |
2e4f5fcf | 2692 | { |
982697a4 | 2693 | enum ofpraw raw; |
2e4f5fcf | 2694 | struct ofpbuf b; |
2e4f5fcf | 2695 | |
2013493b | 2696 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
982697a4 BP |
2697 | raw = ofpraw_pull_assert(&b); |
2698 | switch ((int) raw) { | |
cfc23141 | 2699 | case OFPRAW_OFPST10_FLOW_REQUEST: |
6fd6ed71 | 2700 | return ofputil_decode_ofpst10_flow_request(fsr, b.data, false); |
2e4f5fcf | 2701 | |
617da9cd | 2702 | case OFPRAW_OFPST10_AGGREGATE_REQUEST: |
6fd6ed71 | 2703 | return ofputil_decode_ofpst10_flow_request(fsr, b.data, true); |
0157ad3a SH |
2704 | |
2705 | case OFPRAW_OFPST11_FLOW_REQUEST: | |
2706 | return ofputil_decode_ofpst11_flow_request(fsr, &b, false); | |
2e4f5fcf | 2707 | |
617da9cd SH |
2708 | case OFPRAW_OFPST11_AGGREGATE_REQUEST: |
2709 | return ofputil_decode_ofpst11_flow_request(fsr, &b, true); | |
2710 | ||
982697a4 BP |
2711 | case OFPRAW_NXST_FLOW_REQUEST: |
2712 | return ofputil_decode_nxst_flow_request(fsr, &b, false); | |
2e4f5fcf | 2713 | |
982697a4 BP |
2714 | case OFPRAW_NXST_AGGREGATE_REQUEST: |
2715 | return ofputil_decode_nxst_flow_request(fsr, &b, true); | |
2e4f5fcf BP |
2716 | |
2717 | default: | |
2718 | /* Hey, the caller lied. */ | |
428b2edd | 2719 | OVS_NOT_REACHED(); |
2e4f5fcf BP |
2720 | } |
2721 | } | |
2722 | ||
2723 | /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW, | |
4ffd1b43 | 2724 | * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to |
27527aa0 | 2725 | * 'protocol', and returns the message. */ |
2e4f5fcf | 2726 | struct ofpbuf * |
81d1ea94 | 2727 | ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr, |
27527aa0 | 2728 | enum ofputil_protocol protocol) |
2e4f5fcf BP |
2729 | { |
2730 | struct ofpbuf *msg; | |
982697a4 | 2731 | enum ofpraw raw; |
2e4f5fcf | 2732 | |
27527aa0 | 2733 | switch (protocol) { |
75fa58f8 | 2734 | case OFPUTIL_P_OF11_STD: |
2e1ae200 | 2735 | case OFPUTIL_P_OF12_OXM: |
c37c0382 | 2736 | case OFPUTIL_P_OF13_OXM: |
42dccab5 BP |
2737 | case OFPUTIL_P_OF14_OXM: |
2738 | case OFPUTIL_P_OF15_OXM: { | |
06516c65 SH |
2739 | struct ofp11_flow_stats_request *ofsr; |
2740 | ||
2741 | raw = (fsr->aggregate | |
617da9cd | 2742 | ? OFPRAW_OFPST11_AGGREGATE_REQUEST |
cfc23141 | 2743 | : OFPRAW_OFPST11_FLOW_REQUEST); |
2e1ae200 | 2744 | msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol), |
75fa58f8 | 2745 | ofputil_match_typical_len(protocol)); |
06516c65 SH |
2746 | ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr); |
2747 | ofsr->table_id = fsr->table_id; | |
2748 | ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port); | |
7395c052 | 2749 | ofsr->out_group = htonl(fsr->out_group); |
06516c65 SH |
2750 | ofsr->cookie = fsr->cookie; |
2751 | ofsr->cookie_mask = fsr->cookie_mask; | |
75fa58f8 | 2752 | ofputil_put_ofp11_match(msg, &fsr->match, protocol); |
06516c65 SH |
2753 | break; |
2754 | } | |
2755 | ||
85813857 BP |
2756 | case OFPUTIL_P_OF10_STD: |
2757 | case OFPUTIL_P_OF10_STD_TID: { | |
e2b9ac44 | 2758 | struct ofp10_flow_stats_request *ofsr; |
2e4f5fcf | 2759 | |
982697a4 | 2760 | raw = (fsr->aggregate |
617da9cd | 2761 | ? OFPRAW_OFPST10_AGGREGATE_REQUEST |
cfc23141 | 2762 | : OFPRAW_OFPST10_FLOW_REQUEST); |
982697a4 BP |
2763 | msg = ofpraw_alloc(raw, OFP10_VERSION, 0); |
2764 | ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr); | |
81a76618 | 2765 | ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match); |
2e4f5fcf | 2766 | ofsr->table_id = fsr->table_id; |
4e022ec0 | 2767 | ofsr->out_port = htons(ofp_to_u16(fsr->out_port)); |
27527aa0 BP |
2768 | break; |
2769 | } | |
2770 | ||
85813857 BP |
2771 | case OFPUTIL_P_OF10_NXM: |
2772 | case OFPUTIL_P_OF10_NXM_TID: { | |
2e4f5fcf BP |
2773 | struct nx_flow_stats_request *nfsr; |
2774 | int match_len; | |
2775 | ||
982697a4 BP |
2776 | raw = (fsr->aggregate |
2777 | ? OFPRAW_NXST_AGGREGATE_REQUEST | |
2778 | : OFPRAW_NXST_FLOW_REQUEST); | |
06516c65 | 2779 | msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN); |
982697a4 | 2780 | ofpbuf_put_zeros(msg, sizeof *nfsr); |
7623f4dd | 2781 | match_len = nx_put_match(msg, &fsr->match, |
e729e793 | 2782 | fsr->cookie, fsr->cookie_mask); |
2e4f5fcf | 2783 | |
6fd6ed71 | 2784 | nfsr = msg->msg; |
4e022ec0 | 2785 | nfsr->out_port = htons(ofp_to_u16(fsr->out_port)); |
2e4f5fcf BP |
2786 | nfsr->match_len = htons(match_len); |
2787 | nfsr->table_id = fsr->table_id; | |
27527aa0 BP |
2788 | break; |
2789 | } | |
2790 | ||
2791 | default: | |
428b2edd | 2792 | OVS_NOT_REACHED(); |
2e4f5fcf BP |
2793 | } |
2794 | ||
2795 | return msg; | |
2796 | } | |
d1e2cf21 | 2797 | |
4ffd1b43 | 2798 | /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract |
b78f6b77 | 2799 | * ofputil_flow_stats in 'fs'. |
4ffd1b43 BP |
2800 | * |
2801 | * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single | |
2802 | * OpenFlow message. Calling this function multiple times for a single 'msg' | |
2803 | * iterates through the replies. The caller must initially leave 'msg''s layer | |
2804 | * pointers null and not modify them between calls. | |
2805 | * | |
f27f2134 BP |
2806 | * Most switches don't send the values needed to populate fs->idle_age and |
2807 | * fs->hard_age, so those members will usually be set to 0. If the switch from | |
2808 | * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass | |
2809 | * 'flow_age_extension' as true so that the contents of 'msg' determine the | |
2810 | * 'idle_age' and 'hard_age' members in 'fs'. | |
2811 | * | |
f25d0cf3 BP |
2812 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats |
2813 | * reply's actions. The caller must initialize 'ofpacts' and retains ownership | |
2814 | * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer. | |
2815 | * | |
4ffd1b43 BP |
2816 | * Returns 0 if successful, EOF if no replies were left in this 'msg', |
2817 | * otherwise a positive errno value. */ | |
2818 | int | |
2819 | ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs, | |
f27f2134 | 2820 | struct ofpbuf *msg, |
f25d0cf3 BP |
2821 | bool flow_age_extension, |
2822 | struct ofpbuf *ofpacts) | |
4ffd1b43 | 2823 | { |
0fb88c18 | 2824 | const struct ofp_header *oh; |
8f2cded4 | 2825 | size_t instructions_len; |
982697a4 BP |
2826 | enum ofperr error; |
2827 | enum ofpraw raw; | |
4ffd1b43 | 2828 | |
6fd6ed71 | 2829 | error = (msg->header ? ofpraw_decode(&raw, msg->header) |
982697a4 BP |
2830 | : ofpraw_pull(&raw, msg)); |
2831 | if (error) { | |
2832 | return error; | |
4ffd1b43 | 2833 | } |
6fd6ed71 | 2834 | oh = msg->header; |
4ffd1b43 | 2835 | |
6fd6ed71 | 2836 | if (!msg->size) { |
4ffd1b43 | 2837 | return EOF; |
2e1ae200 JR |
2838 | } else if (raw == OFPRAW_OFPST11_FLOW_REPLY |
2839 | || raw == OFPRAW_OFPST13_FLOW_REPLY) { | |
6ec5f0c5 SH |
2840 | const struct ofp11_flow_stats *ofs; |
2841 | size_t length; | |
2842 | uint16_t padded_match_len; | |
2843 | ||
2844 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
2845 | if (!ofs) { | |
437d0d22 | 2846 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover " |
6fd6ed71 | 2847 | "bytes at end", msg->size); |
6ec5f0c5 SH |
2848 | return EINVAL; |
2849 | } | |
2850 | ||
2851 | length = ntohs(ofs->length); | |
2852 | if (length < sizeof *ofs) { | |
2853 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
34582733 | 2854 | "length %"PRIuSIZE, length); |
6ec5f0c5 SH |
2855 | return EINVAL; |
2856 | } | |
2857 | ||
81a76618 | 2858 | if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) { |
6ec5f0c5 SH |
2859 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match"); |
2860 | return EINVAL; | |
2861 | } | |
8f2cded4 | 2862 | instructions_len = length - sizeof *ofs - padded_match_len; |
6ec5f0c5 | 2863 | |
81a76618 | 2864 | fs->priority = ntohs(ofs->priority); |
6ec5f0c5 SH |
2865 | fs->table_id = ofs->table_id; |
2866 | fs->duration_sec = ntohl(ofs->duration_sec); | |
2867 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
2868 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
2869 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
ca26eb44 RB |
2870 | if (oh->version >= OFP14_VERSION) { |
2871 | fs->importance = ntohs(ofs->importance); | |
2872 | } else { | |
2873 | fs->importance = 0; | |
2874 | } | |
0fb88c18 BP |
2875 | if (raw == OFPRAW_OFPST13_FLOW_REPLY) { |
2876 | error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version, | |
2877 | &fs->flags); | |
2878 | if (error) { | |
2879 | return error; | |
2880 | } | |
2881 | } else { | |
2882 | fs->flags = 0; | |
2883 | } | |
6ec5f0c5 SH |
2884 | fs->idle_age = -1; |
2885 | fs->hard_age = -1; | |
2886 | fs->cookie = ofs->cookie; | |
2887 | fs->packet_count = ntohll(ofs->packet_count); | |
2888 | fs->byte_count = ntohll(ofs->byte_count); | |
cfc23141 | 2889 | } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) { |
e2b9ac44 | 2890 | const struct ofp10_flow_stats *ofs; |
4ffd1b43 BP |
2891 | size_t length; |
2892 | ||
2893 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
2894 | if (!ofs) { | |
437d0d22 | 2895 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover " |
6fd6ed71 | 2896 | "bytes at end", msg->size); |
4ffd1b43 BP |
2897 | return EINVAL; |
2898 | } | |
2899 | ||
2900 | length = ntohs(ofs->length); | |
2901 | if (length < sizeof *ofs) { | |
2902 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
34582733 | 2903 | "length %"PRIuSIZE, length); |
4ffd1b43 BP |
2904 | return EINVAL; |
2905 | } | |
8f2cded4 | 2906 | instructions_len = length - sizeof *ofs; |
4ffd1b43 BP |
2907 | |
2908 | fs->cookie = get_32aligned_be64(&ofs->cookie); | |
81a76618 BP |
2909 | ofputil_match_from_ofp10_match(&ofs->match, &fs->match); |
2910 | fs->priority = ntohs(ofs->priority); | |
4ffd1b43 BP |
2911 | fs->table_id = ofs->table_id; |
2912 | fs->duration_sec = ntohl(ofs->duration_sec); | |
2913 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
2914 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
2915 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
ca26eb44 | 2916 | fs->importance = 0; |
f27f2134 BP |
2917 | fs->idle_age = -1; |
2918 | fs->hard_age = -1; | |
4ffd1b43 BP |
2919 | fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count)); |
2920 | fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count)); | |
2e1ae200 | 2921 | fs->flags = 0; |
982697a4 | 2922 | } else if (raw == OFPRAW_NXST_FLOW_REPLY) { |
4ffd1b43 | 2923 | const struct nx_flow_stats *nfs; |
8f2cded4 | 2924 | size_t match_len, length; |
4ffd1b43 BP |
2925 | |
2926 | nfs = ofpbuf_try_pull(msg, sizeof *nfs); | |
2927 | if (!nfs) { | |
437d0d22 | 2928 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover " |
6fd6ed71 | 2929 | "bytes at end", msg->size); |
4ffd1b43 BP |
2930 | return EINVAL; |
2931 | } | |
2932 | ||
2933 | length = ntohs(nfs->length); | |
2934 | match_len = ntohs(nfs->match_len); | |
2935 | if (length < sizeof *nfs + ROUND_UP(match_len, 8)) { | |
34582733 AS |
2936 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" " |
2937 | "claims invalid length %"PRIuSIZE, match_len, length); | |
4ffd1b43 BP |
2938 | return EINVAL; |
2939 | } | |
81a76618 | 2940 | if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) { |
4ffd1b43 BP |
2941 | return EINVAL; |
2942 | } | |
8f2cded4 | 2943 | instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8); |
4ffd1b43 BP |
2944 | |
2945 | fs->cookie = nfs->cookie; | |
2946 | fs->table_id = nfs->table_id; | |
2947 | fs->duration_sec = ntohl(nfs->duration_sec); | |
2948 | fs->duration_nsec = ntohl(nfs->duration_nsec); | |
81a76618 | 2949 | fs->priority = ntohs(nfs->priority); |
4ffd1b43 BP |
2950 | fs->idle_timeout = ntohs(nfs->idle_timeout); |
2951 | fs->hard_timeout = ntohs(nfs->hard_timeout); | |
ca26eb44 | 2952 | fs->importance = 0; |
f27f2134 BP |
2953 | fs->idle_age = -1; |
2954 | fs->hard_age = -1; | |
2955 | if (flow_age_extension) { | |
2956 | if (nfs->idle_age) { | |
2957 | fs->idle_age = ntohs(nfs->idle_age) - 1; | |
2958 | } | |
2959 | if (nfs->hard_age) { | |
2960 | fs->hard_age = ntohs(nfs->hard_age) - 1; | |
2961 | } | |
2962 | } | |
4ffd1b43 BP |
2963 | fs->packet_count = ntohll(nfs->packet_count); |
2964 | fs->byte_count = ntohll(nfs->byte_count); | |
2e1ae200 | 2965 | fs->flags = 0; |
4ffd1b43 | 2966 | } else { |
428b2edd | 2967 | OVS_NOT_REACHED(); |
4ffd1b43 BP |
2968 | } |
2969 | ||
8f2cded4 BP |
2970 | if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version, |
2971 | ofpacts)) { | |
2972 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions"); | |
2973 | return EINVAL; | |
2974 | } | |
6fd6ed71 PS |
2975 | fs->ofpacts = ofpacts->data; |
2976 | fs->ofpacts_len = ofpacts->size; | |
f25d0cf3 | 2977 | |
4ffd1b43 BP |
2978 | return 0; |
2979 | } | |
2980 | ||
5e9d0469 BP |
2981 | /* Returns 'count' unchanged except that UINT64_MAX becomes 0. |
2982 | * | |
2983 | * We use this in situations where OVS internally uses UINT64_MAX to mean | |
2984 | * "value unknown" but OpenFlow 1.0 does not define any unknown value. */ | |
2985 | static uint64_t | |
2986 | unknown_to_zero(uint64_t count) | |
2987 | { | |
2988 | return count != UINT64_MAX ? count : 0; | |
2989 | } | |
2990 | ||
349adfb2 BP |
2991 | /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to |
2992 | * those already present in the list of ofpbufs in 'replies'. 'replies' should | |
7395c052 | 2993 | * have been initialized with ofpmp_init(). */ |
349adfb2 BP |
2994 | void |
2995 | ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs, | |
ca6ba700 | 2996 | struct ovs_list *replies) |
349adfb2 | 2997 | { |
f25d0cf3 | 2998 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); |
6fd6ed71 | 2999 | size_t start_ofs = reply->size; |
e28ac5cf BP |
3000 | enum ofp_version version = ofpmp_version(replies); |
3001 | enum ofpraw raw = ofpmp_decode_raw(replies); | |
349adfb2 | 3002 | |
2e1ae200 | 3003 | if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) { |
22a86f18 SH |
3004 | struct ofp11_flow_stats *ofs; |
3005 | ||
3006 | ofpbuf_put_uninit(reply, sizeof *ofs); | |
9d84066c | 3007 | oxm_put_match(reply, &fs->match, version); |
e3f8f887 JR |
3008 | ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply, |
3009 | version); | |
22a86f18 SH |
3010 | |
3011 | ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs); | |
6fd6ed71 | 3012 | ofs->length = htons(reply->size - start_ofs); |
22a86f18 SH |
3013 | ofs->table_id = fs->table_id; |
3014 | ofs->pad = 0; | |
3015 | ofs->duration_sec = htonl(fs->duration_sec); | |
3016 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
81a76618 | 3017 | ofs->priority = htons(fs->priority); |
22a86f18 SH |
3018 | ofs->idle_timeout = htons(fs->idle_timeout); |
3019 | ofs->hard_timeout = htons(fs->hard_timeout); | |
ca26eb44 RB |
3020 | if (version >= OFP14_VERSION) { |
3021 | ofs->importance = htons(fs->importance); | |
3022 | } else { | |
3023 | ofs->importance = 0; | |
3024 | } | |
0fb88c18 | 3025 | if (raw == OFPRAW_OFPST13_FLOW_REPLY) { |
e3f8f887 | 3026 | ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version); |
0fb88c18 BP |
3027 | } else { |
3028 | ofs->flags = 0; | |
3029 | } | |
22a86f18 SH |
3030 | memset(ofs->pad2, 0, sizeof ofs->pad2); |
3031 | ofs->cookie = fs->cookie; | |
3032 | ofs->packet_count = htonll(unknown_to_zero(fs->packet_count)); | |
3033 | ofs->byte_count = htonll(unknown_to_zero(fs->byte_count)); | |
3034 | } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) { | |
e2b9ac44 | 3035 | struct ofp10_flow_stats *ofs; |
349adfb2 | 3036 | |
1a59dc2c | 3037 | ofpbuf_put_uninit(reply, sizeof *ofs); |
e3f8f887 JR |
3038 | ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply, |
3039 | version); | |
1a59dc2c | 3040 | ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs); |
6fd6ed71 | 3041 | ofs->length = htons(reply->size - start_ofs); |
349adfb2 BP |
3042 | ofs->table_id = fs->table_id; |
3043 | ofs->pad = 0; | |
81a76618 | 3044 | ofputil_match_to_ofp10_match(&fs->match, &ofs->match); |
349adfb2 BP |
3045 | ofs->duration_sec = htonl(fs->duration_sec); |
3046 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
81a76618 | 3047 | ofs->priority = htons(fs->priority); |
349adfb2 BP |
3048 | ofs->idle_timeout = htons(fs->idle_timeout); |
3049 | ofs->hard_timeout = htons(fs->hard_timeout); | |
3050 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
3051 | put_32aligned_be64(&ofs->cookie, fs->cookie); | |
5e9d0469 BP |
3052 | put_32aligned_be64(&ofs->packet_count, |
3053 | htonll(unknown_to_zero(fs->packet_count))); | |
3054 | put_32aligned_be64(&ofs->byte_count, | |
3055 | htonll(unknown_to_zero(fs->byte_count))); | |
982697a4 | 3056 | } else if (raw == OFPRAW_NXST_FLOW_REPLY) { |
349adfb2 | 3057 | struct nx_flow_stats *nfs; |
1a59dc2c | 3058 | int match_len; |
349adfb2 | 3059 | |
1a59dc2c | 3060 | ofpbuf_put_uninit(reply, sizeof *nfs); |
81a76618 | 3061 | match_len = nx_put_match(reply, &fs->match, 0, 0); |
e3f8f887 JR |
3062 | ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply, |
3063 | version); | |
1a59dc2c | 3064 | nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs); |
6fd6ed71 | 3065 | nfs->length = htons(reply->size - start_ofs); |
349adfb2 BP |
3066 | nfs->table_id = fs->table_id; |
3067 | nfs->pad = 0; | |
3068 | nfs->duration_sec = htonl(fs->duration_sec); | |
3069 | nfs->duration_nsec = htonl(fs->duration_nsec); | |
81a76618 | 3070 | nfs->priority = htons(fs->priority); |
349adfb2 BP |
3071 | nfs->idle_timeout = htons(fs->idle_timeout); |
3072 | nfs->hard_timeout = htons(fs->hard_timeout); | |
f27f2134 BP |
3073 | nfs->idle_age = htons(fs->idle_age < 0 ? 0 |
3074 | : fs->idle_age < UINT16_MAX ? fs->idle_age + 1 | |
3075 | : UINT16_MAX); | |
3076 | nfs->hard_age = htons(fs->hard_age < 0 ? 0 | |
3077 | : fs->hard_age < UINT16_MAX ? fs->hard_age + 1 | |
3078 | : UINT16_MAX); | |
1a59dc2c | 3079 | nfs->match_len = htons(match_len); |
349adfb2 BP |
3080 | nfs->cookie = fs->cookie; |
3081 | nfs->packet_count = htonll(fs->packet_count); | |
3082 | nfs->byte_count = htonll(fs->byte_count); | |
349adfb2 | 3083 | } else { |
428b2edd | 3084 | OVS_NOT_REACHED(); |
349adfb2 | 3085 | } |
f25d0cf3 | 3086 | |
982697a4 | 3087 | ofpmp_postappend(replies, start_ofs); |
349adfb2 BP |
3088 | } |
3089 | ||
76c93b22 | 3090 | /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or |
a814ba0f | 3091 | * NXST_AGGREGATE reply matching 'request', and returns the message. */ |
76c93b22 BP |
3092 | struct ofpbuf * |
3093 | ofputil_encode_aggregate_stats_reply( | |
3094 | const struct ofputil_aggregate_stats *stats, | |
982697a4 | 3095 | const struct ofp_header *request) |
76c93b22 | 3096 | { |
a814ba0f BP |
3097 | struct ofp_aggregate_stats_reply *asr; |
3098 | uint64_t packet_count; | |
3099 | uint64_t byte_count; | |
76c93b22 | 3100 | struct ofpbuf *msg; |
982697a4 | 3101 | enum ofpraw raw; |
76c93b22 | 3102 | |
982697a4 | 3103 | ofpraw_decode(&raw, request); |
617da9cd | 3104 | if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) { |
a814ba0f BP |
3105 | packet_count = unknown_to_zero(stats->packet_count); |
3106 | byte_count = unknown_to_zero(stats->byte_count); | |
76c93b22 | 3107 | } else { |
a814ba0f BP |
3108 | packet_count = stats->packet_count; |
3109 | byte_count = stats->byte_count; | |
76c93b22 BP |
3110 | } |
3111 | ||
a814ba0f BP |
3112 | msg = ofpraw_alloc_stats_reply(request, 0); |
3113 | asr = ofpbuf_put_zeros(msg, sizeof *asr); | |
3114 | put_32aligned_be64(&asr->packet_count, htonll(packet_count)); | |
3115 | put_32aligned_be64(&asr->byte_count, htonll(byte_count)); | |
3116 | asr->flow_count = htonl(stats->flow_count); | |
3117 | ||
76c93b22 BP |
3118 | return msg; |
3119 | } | |
3120 | ||
982697a4 BP |
3121 | enum ofperr |
3122 | ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats, | |
3123 | const struct ofp_header *reply) | |
3124 | { | |
a814ba0f | 3125 | struct ofp_aggregate_stats_reply *asr; |
982697a4 | 3126 | struct ofpbuf msg; |
982697a4 BP |
3127 | |
3128 | ofpbuf_use_const(&msg, reply, ntohs(reply->length)); | |
a814ba0f BP |
3129 | ofpraw_pull_assert(&msg); |
3130 | ||
6fd6ed71 | 3131 | asr = msg.msg; |
a814ba0f BP |
3132 | stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count)); |
3133 | stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count)); | |
3134 | stats->flow_count = ntohl(asr->flow_count); | |
982697a4 BP |
3135 | |
3136 | return 0; | |
3137 | } | |
3138 | ||
b78f6b77 BP |
3139 | /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an |
3140 | * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise | |
3141 | * an OpenFlow error code. */ | |
90bf1e07 | 3142 | enum ofperr |
9b045a0c | 3143 | ofputil_decode_flow_removed(struct ofputil_flow_removed *fr, |
b78f6b77 | 3144 | const struct ofp_header *oh) |
9b045a0c | 3145 | { |
982697a4 BP |
3146 | enum ofpraw raw; |
3147 | struct ofpbuf b; | |
9b045a0c | 3148 | |
982697a4 BP |
3149 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
3150 | raw = ofpraw_pull_assert(&b); | |
eefbf181 SH |
3151 | if (raw == OFPRAW_OFPT11_FLOW_REMOVED) { |
3152 | const struct ofp12_flow_removed *ofr; | |
3153 | enum ofperr error; | |
3154 | ||
3155 | ofr = ofpbuf_pull(&b, sizeof *ofr); | |
3156 | ||
81a76618 | 3157 | error = ofputil_pull_ofp11_match(&b, &fr->match, NULL); |
eefbf181 SH |
3158 | if (error) { |
3159 | return error; | |
3160 | } | |
3161 | ||
81a76618 | 3162 | fr->priority = ntohs(ofr->priority); |
eefbf181 SH |
3163 | fr->cookie = ofr->cookie; |
3164 | fr->reason = ofr->reason; | |
95216219 | 3165 | fr->table_id = ofr->table_id; |
eefbf181 SH |
3166 | fr->duration_sec = ntohl(ofr->duration_sec); |
3167 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
3168 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
3169 | fr->hard_timeout = ntohs(ofr->hard_timeout); | |
3170 | fr->packet_count = ntohll(ofr->packet_count); | |
3171 | fr->byte_count = ntohll(ofr->byte_count); | |
3172 | } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) { | |
31a9e63f | 3173 | const struct ofp10_flow_removed *ofr; |
9b045a0c | 3174 | |
982697a4 BP |
3175 | ofr = ofpbuf_pull(&b, sizeof *ofr); |
3176 | ||
81a76618 BP |
3177 | ofputil_match_from_ofp10_match(&ofr->match, &fr->match); |
3178 | fr->priority = ntohs(ofr->priority); | |
9b045a0c BP |
3179 | fr->cookie = ofr->cookie; |
3180 | fr->reason = ofr->reason; | |
95216219 | 3181 | fr->table_id = 255; |
9b045a0c BP |
3182 | fr->duration_sec = ntohl(ofr->duration_sec); |
3183 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
3184 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
fa2bad0f | 3185 | fr->hard_timeout = 0; |
9b045a0c BP |
3186 | fr->packet_count = ntohll(ofr->packet_count); |
3187 | fr->byte_count = ntohll(ofr->byte_count); | |
982697a4 | 3188 | } else if (raw == OFPRAW_NXT_FLOW_REMOVED) { |
9b045a0c | 3189 | struct nx_flow_removed *nfr; |
c2725b60 | 3190 | enum ofperr error; |
9b045a0c | 3191 | |
9b045a0c | 3192 | nfr = ofpbuf_pull(&b, sizeof *nfr); |
81a76618 BP |
3193 | error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match, |
3194 | NULL, NULL); | |
9b045a0c BP |
3195 | if (error) { |
3196 | return error; | |
3197 | } | |
6fd6ed71 | 3198 | if (b.size) { |
90bf1e07 | 3199 | return OFPERR_OFPBRC_BAD_LEN; |
9b045a0c BP |
3200 | } |
3201 | ||
81a76618 | 3202 | fr->priority = ntohs(nfr->priority); |
9b045a0c BP |
3203 | fr->cookie = nfr->cookie; |
3204 | fr->reason = nfr->reason; | |
745bfd5e | 3205 | fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255; |
9b045a0c BP |
3206 | fr->duration_sec = ntohl(nfr->duration_sec); |
3207 | fr->duration_nsec = ntohl(nfr->duration_nsec); | |
3208 | fr->idle_timeout = ntohs(nfr->idle_timeout); | |
fa2bad0f | 3209 | fr->hard_timeout = 0; |
9b045a0c BP |
3210 | fr->packet_count = ntohll(nfr->packet_count); |
3211 | fr->byte_count = ntohll(nfr->byte_count); | |
3212 | } else { | |
428b2edd | 3213 | OVS_NOT_REACHED(); |
9b045a0c BP |
3214 | } |
3215 | ||
3216 | return 0; | |
3217 | } | |
3218 | ||
588cd7b5 | 3219 | /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or |
27527aa0 | 3220 | * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the |
588cd7b5 BP |
3221 | * message. */ |
3222 | struct ofpbuf * | |
3223 | ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr, | |
27527aa0 | 3224 | enum ofputil_protocol protocol) |
588cd7b5 BP |
3225 | { |
3226 | struct ofpbuf *msg; | |
fa42f4f8 SH |
3227 | enum ofp_flow_removed_reason reason = fr->reason; |
3228 | ||
3229 | if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) { | |
3230 | reason = OFPRR_DELETE; | |
3231 | } | |
588cd7b5 | 3232 | |
27527aa0 | 3233 | switch (protocol) { |
75fa58f8 | 3234 | case OFPUTIL_P_OF11_STD: |
2e1ae200 | 3235 | case OFPUTIL_P_OF12_OXM: |
c37c0382 | 3236 | case OFPUTIL_P_OF13_OXM: |
42dccab5 BP |
3237 | case OFPUTIL_P_OF14_OXM: |
3238 | case OFPUTIL_P_OF15_OXM: { | |
83974732 SH |
3239 | struct ofp12_flow_removed *ofr; |
3240 | ||
3241 | msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED, | |
3242 | ofputil_protocol_to_ofp_version(protocol), | |
75fa58f8 BP |
3243 | htonl(0), |
3244 | ofputil_match_typical_len(protocol)); | |
83974732 SH |
3245 | ofr = ofpbuf_put_zeros(msg, sizeof *ofr); |
3246 | ofr->cookie = fr->cookie; | |
81a76618 | 3247 | ofr->priority = htons(fr->priority); |
fa42f4f8 | 3248 | ofr->reason = reason; |
95216219 | 3249 | ofr->table_id = fr->table_id; |
83974732 SH |
3250 | ofr->duration_sec = htonl(fr->duration_sec); |
3251 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
3252 | ofr->idle_timeout = htons(fr->idle_timeout); | |
fa2bad0f | 3253 | ofr->hard_timeout = htons(fr->hard_timeout); |
83974732 SH |
3254 | ofr->packet_count = htonll(fr->packet_count); |
3255 | ofr->byte_count = htonll(fr->byte_count); | |
75fa58f8 | 3256 | ofputil_put_ofp11_match(msg, &fr->match, protocol); |
83974732 SH |
3257 | break; |
3258 | } | |
3259 | ||
85813857 BP |
3260 | case OFPUTIL_P_OF10_STD: |
3261 | case OFPUTIL_P_OF10_STD_TID: { | |
31a9e63f | 3262 | struct ofp10_flow_removed *ofr; |
588cd7b5 | 3263 | |
982697a4 BP |
3264 | msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION, |
3265 | htonl(0), 0); | |
3266 | ofr = ofpbuf_put_zeros(msg, sizeof *ofr); | |
81a76618 | 3267 | ofputil_match_to_ofp10_match(&fr->match, &ofr->match); |
7fb563b9 | 3268 | ofr->cookie = fr->cookie; |
81a76618 | 3269 | ofr->priority = htons(fr->priority); |
fa42f4f8 | 3270 | ofr->reason = reason; |
588cd7b5 BP |
3271 | ofr->duration_sec = htonl(fr->duration_sec); |
3272 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
3273 | ofr->idle_timeout = htons(fr->idle_timeout); | |
5e9d0469 BP |
3274 | ofr->packet_count = htonll(unknown_to_zero(fr->packet_count)); |
3275 | ofr->byte_count = htonll(unknown_to_zero(fr->byte_count)); | |
27527aa0 BP |
3276 | break; |
3277 | } | |
3278 | ||
85813857 BP |
3279 | case OFPUTIL_P_OF10_NXM: |
3280 | case OFPUTIL_P_OF10_NXM_TID: { | |
588cd7b5 BP |
3281 | struct nx_flow_removed *nfr; |
3282 | int match_len; | |
3283 | ||
982697a4 BP |
3284 | msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION, |
3285 | htonl(0), NXM_TYPICAL_LEN); | |
a3358085 | 3286 | ofpbuf_put_zeros(msg, sizeof *nfr); |
81a76618 | 3287 | match_len = nx_put_match(msg, &fr->match, 0, 0); |
588cd7b5 | 3288 | |
6fd6ed71 | 3289 | nfr = msg->msg; |
588cd7b5 | 3290 | nfr->cookie = fr->cookie; |
81a76618 | 3291 | nfr->priority = htons(fr->priority); |
fa42f4f8 | 3292 | nfr->reason = reason; |
745bfd5e | 3293 | nfr->table_id = fr->table_id + 1; |
588cd7b5 BP |
3294 | nfr->duration_sec = htonl(fr->duration_sec); |
3295 | nfr->duration_nsec = htonl(fr->duration_nsec); | |
3296 | nfr->idle_timeout = htons(fr->idle_timeout); | |
3297 | nfr->match_len = htons(match_len); | |
3298 | nfr->packet_count = htonll(fr->packet_count); | |
3299 | nfr->byte_count = htonll(fr->byte_count); | |
27527aa0 BP |
3300 | break; |
3301 | } | |
3302 | ||
3303 | default: | |
428b2edd | 3304 | OVS_NOT_REACHED(); |
588cd7b5 BP |
3305 | } |
3306 | ||
3307 | return msg; | |
3308 | } | |
3309 | ||
f7cc6bd8 | 3310 | enum ofperr |
65120a8a EJ |
3311 | ofputil_decode_packet_in(struct ofputil_packet_in *pin, |
3312 | const struct ofp_header *oh) | |
3313 | { | |
982697a4 BP |
3314 | enum ofpraw raw; |
3315 | struct ofpbuf b; | |
65120a8a | 3316 | |
65120a8a | 3317 | memset(pin, 0, sizeof *pin); |
d4fa4e79 | 3318 | pin->cookie = OVS_BE64_MAX; |
65120a8a | 3319 | |
982697a4 BP |
3320 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
3321 | raw = ofpraw_pull_assert(&b); | |
2e1ae200 JR |
3322 | if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) { |
3323 | const struct ofp13_packet_in *opi; | |
7cfb9651 | 3324 | int error; |
2e1ae200 JR |
3325 | size_t packet_in_size; |
3326 | ||
3327 | if (raw == OFPRAW_OFPT12_PACKET_IN) { | |
3328 | packet_in_size = sizeof (struct ofp12_packet_in); | |
3329 | } else { | |
3330 | packet_in_size = sizeof (struct ofp13_packet_in); | |
3331 | } | |
7cfb9651 | 3332 | |
2e1ae200 | 3333 | opi = ofpbuf_pull(&b, packet_in_size); |
50dcbd8e | 3334 | error = oxm_pull_match_loose(&b, &pin->flow_metadata); |
7cfb9651 SH |
3335 | if (error) { |
3336 | return error; | |
3337 | } | |
3338 | ||
3339 | if (!ofpbuf_try_pull(&b, 2)) { | |
3340 | return OFPERR_OFPBRC_BAD_LEN; | |
3341 | } | |
3342 | ||
2e1ae200 JR |
3343 | pin->reason = opi->pi.reason; |
3344 | pin->table_id = opi->pi.table_id; | |
3345 | pin->buffer_id = ntohl(opi->pi.buffer_id); | |
3346 | pin->total_len = ntohs(opi->pi.total_len); | |
7cfb9651 | 3347 | |
2e1ae200 JR |
3348 | if (raw == OFPRAW_OFPT13_PACKET_IN) { |
3349 | pin->cookie = opi->cookie; | |
3350 | } | |
7cfb9651 | 3351 | |
50dcbd8e JG |
3352 | pin->packet = b.data; |
3353 | pin->packet_len = b.size; | |
7cfb9651 | 3354 | } else if (raw == OFPRAW_OFPT10_PACKET_IN) { |
31a9e63f | 3355 | const struct ofp10_packet_in *opi; |
982697a4 | 3356 | |
31a9e63f | 3357 | opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data)); |
65120a8a EJ |
3358 | |
3359 | pin->packet = opi->data; | |
6fd6ed71 | 3360 | pin->packet_len = b.size; |
65120a8a | 3361 | |
50dcbd8e JG |
3362 | match_init_catchall(&pin->flow_metadata); |
3363 | match_set_in_port(&pin->flow_metadata, u16_to_ofp(ntohs(opi->in_port))); | |
65120a8a EJ |
3364 | pin->reason = opi->reason; |
3365 | pin->buffer_id = ntohl(opi->buffer_id); | |
3366 | pin->total_len = ntohs(opi->total_len); | |
4d197ebb BP |
3367 | } else if (raw == OFPRAW_OFPT11_PACKET_IN) { |
3368 | const struct ofp11_packet_in *opi; | |
50dcbd8e | 3369 | ofp_port_t in_port; |
4d197ebb BP |
3370 | enum ofperr error; |
3371 | ||
3372 | opi = ofpbuf_pull(&b, sizeof *opi); | |
3373 | ||
6fd6ed71 PS |
3374 | pin->packet = b.data; |
3375 | pin->packet_len = b.size; | |
4d197ebb BP |
3376 | |
3377 | pin->buffer_id = ntohl(opi->buffer_id); | |
50dcbd8e | 3378 | error = ofputil_port_from_ofp11(opi->in_port, &in_port); |
4d197ebb BP |
3379 | if (error) { |
3380 | return error; | |
3381 | } | |
50dcbd8e JG |
3382 | match_init_catchall(&pin->flow_metadata); |
3383 | match_set_in_port(&pin->flow_metadata, in_port); | |
4d197ebb BP |
3384 | pin->total_len = ntohs(opi->total_len); |
3385 | pin->reason = opi->reason; | |
3386 | pin->table_id = opi->table_id; | |
982697a4 | 3387 | } else if (raw == OFPRAW_NXT_PACKET_IN) { |
73dbf4ab | 3388 | const struct nx_packet_in *npi; |
54834960 EJ |
3389 | int error; |
3390 | ||
54834960 | 3391 | npi = ofpbuf_pull(&b, sizeof *npi); |
50dcbd8e JG |
3392 | error = nx_pull_match_loose(&b, ntohs(npi->match_len), |
3393 | &pin->flow_metadata, NULL, NULL); | |
54834960 EJ |
3394 | if (error) { |
3395 | return error; | |
3396 | } | |
3397 | ||
3398 | if (!ofpbuf_try_pull(&b, 2)) { | |
90bf1e07 | 3399 | return OFPERR_OFPBRC_BAD_LEN; |
54834960 EJ |
3400 | } |
3401 | ||
54834960 EJ |
3402 | pin->reason = npi->reason; |
3403 | pin->table_id = npi->table_id; | |
3404 | pin->cookie = npi->cookie; | |
3405 | ||
54834960 EJ |
3406 | pin->buffer_id = ntohl(npi->buffer_id); |
3407 | pin->total_len = ntohs(npi->total_len); | |
7cfb9651 | 3408 | |
50dcbd8e JG |
3409 | pin->packet = b.data; |
3410 | pin->packet_len = b.size; | |
65120a8a | 3411 | } else { |
428b2edd | 3412 | OVS_NOT_REACHED(); |
65120a8a EJ |
3413 | } |
3414 | ||
3415 | return 0; | |
3416 | } | |
3417 | ||
0f83fea0 BP |
3418 | static struct ofpbuf * |
3419 | ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin) | |
3420 | { | |
3421 | struct ofp10_packet_in *opi; | |
3422 | struct ofpbuf *packet; | |
3423 | ||
3424 | packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION, | |
3425 | htonl(0), pin->packet_len); | |
3426 | opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data)); | |
3427 | opi->total_len = htons(pin->total_len); | |
50dcbd8e | 3428 | opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port)); |
0f83fea0 BP |
3429 | opi->reason = pin->reason; |
3430 | opi->buffer_id = htonl(pin->buffer_id); | |
3431 | ||
3432 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3433 | ||
3434 | return packet; | |
3435 | } | |
3436 | ||
3437 | static struct ofpbuf * | |
3438 | ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin) | |
3439 | { | |
3440 | struct nx_packet_in *npi; | |
3441 | struct ofpbuf *packet; | |
0f83fea0 BP |
3442 | size_t match_len; |
3443 | ||
0f83fea0 BP |
3444 | /* The final argument is just an estimate of the space required. */ |
3445 | packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION, | |
50dcbd8e | 3446 | htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len); |
0f83fea0 | 3447 | ofpbuf_put_zeros(packet, sizeof *npi); |
50dcbd8e | 3448 | match_len = nx_put_match(packet, &pin->flow_metadata, 0, 0); |
0f83fea0 BP |
3449 | ofpbuf_put_zeros(packet, 2); |
3450 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3451 | ||
6fd6ed71 | 3452 | npi = packet->msg; |
0f83fea0 BP |
3453 | npi->buffer_id = htonl(pin->buffer_id); |
3454 | npi->total_len = htons(pin->total_len); | |
3455 | npi->reason = pin->reason; | |
3456 | npi->table_id = pin->table_id; | |
3457 | npi->cookie = pin->cookie; | |
3458 | npi->match_len = htons(match_len); | |
3459 | ||
3460 | return packet; | |
3461 | } | |
3462 | ||
4d197ebb BP |
3463 | static struct ofpbuf * |
3464 | ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin) | |
3465 | { | |
3466 | struct ofp11_packet_in *opi; | |
3467 | struct ofpbuf *packet; | |
3468 | ||
3469 | packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION, | |
3470 | htonl(0), pin->packet_len); | |
3471 | opi = ofpbuf_put_zeros(packet, sizeof *opi); | |
3472 | opi->buffer_id = htonl(pin->buffer_id); | |
50dcbd8e | 3473 | opi->in_port = ofputil_port_to_ofp11(pin->flow_metadata.flow.in_port.ofp_port); |
4d197ebb BP |
3474 | opi->in_phy_port = opi->in_port; |
3475 | opi->total_len = htons(pin->total_len); | |
3476 | opi->reason = pin->reason; | |
3477 | opi->table_id = pin->table_id; | |
3478 | ||
3479 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3480 | ||
3481 | return packet; | |
3482 | } | |
3483 | ||
0f83fea0 BP |
3484 | static struct ofpbuf * |
3485 | ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin, | |
3486 | enum ofputil_protocol protocol) | |
3487 | { | |
3488 | struct ofp13_packet_in *opi; | |
0f83fea0 BP |
3489 | enum ofpraw packet_in_raw; |
3490 | enum ofp_version packet_in_version; | |
3491 | size_t packet_in_size; | |
3492 | struct ofpbuf *packet; | |
3493 | ||
3494 | if (protocol == OFPUTIL_P_OF12_OXM) { | |
3495 | packet_in_raw = OFPRAW_OFPT12_PACKET_IN; | |
3496 | packet_in_version = OFP12_VERSION; | |
3497 | packet_in_size = sizeof (struct ofp12_packet_in); | |
3498 | } else { | |
3499 | packet_in_raw = OFPRAW_OFPT13_PACKET_IN; | |
b6a3dd9c | 3500 | packet_in_version = ofputil_protocol_to_ofp_version(protocol); |
0f83fea0 BP |
3501 | packet_in_size = sizeof (struct ofp13_packet_in); |
3502 | } | |
3503 | ||
0f83fea0 BP |
3504 | /* The final argument is just an estimate of the space required. */ |
3505 | packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version, | |
50dcbd8e | 3506 | htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len); |
0f83fea0 | 3507 | ofpbuf_put_zeros(packet, packet_in_size); |
50dcbd8e JG |
3508 | oxm_put_match(packet, &pin->flow_metadata, |
3509 | ofputil_protocol_to_ofp_version(protocol)); | |
0f83fea0 BP |
3510 | ofpbuf_put_zeros(packet, 2); |
3511 | ofpbuf_put(packet, pin->packet, pin->packet_len); | |
3512 | ||
6fd6ed71 | 3513 | opi = packet->msg; |
0f83fea0 BP |
3514 | opi->pi.buffer_id = htonl(pin->buffer_id); |
3515 | opi->pi.total_len = htons(pin->total_len); | |
3516 | opi->pi.reason = pin->reason; | |
3517 | opi->pi.table_id = pin->table_id; | |
b6a3dd9c | 3518 | if (protocol != OFPUTIL_P_OF12_OXM) { |
0f83fea0 BP |
3519 | opi->cookie = pin->cookie; |
3520 | } | |
3521 | ||
3522 | return packet; | |
3523 | } | |
3524 | ||
54834960 EJ |
3525 | /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message |
3526 | * in the format specified by 'packet_in_format'. */ | |
ebb57021 | 3527 | struct ofpbuf * |
54834960 | 3528 | ofputil_encode_packet_in(const struct ofputil_packet_in *pin, |
d94240ec | 3529 | enum ofputil_protocol protocol, |
54834960 | 3530 | enum nx_packet_in_format packet_in_format) |
ebb57021 | 3531 | { |
54834960 | 3532 | struct ofpbuf *packet; |
ebb57021 | 3533 | |
0f83fea0 BP |
3534 | switch (protocol) { |
3535 | case OFPUTIL_P_OF10_STD: | |
3536 | case OFPUTIL_P_OF10_STD_TID: | |
3537 | case OFPUTIL_P_OF10_NXM: | |
3538 | case OFPUTIL_P_OF10_NXM_TID: | |
3539 | packet = (packet_in_format == NXPIF_NXM | |
3540 | ? ofputil_encode_nx_packet_in(pin) | |
3541 | : ofputil_encode_ofp10_packet_in(pin)); | |
3542 | break; | |
2e1ae200 | 3543 | |
0f83fea0 | 3544 | case OFPUTIL_P_OF11_STD: |
4d197ebb BP |
3545 | packet = ofputil_encode_ofp11_packet_in(pin); |
3546 | break; | |
d94240ec | 3547 | |
0f83fea0 BP |
3548 | case OFPUTIL_P_OF12_OXM: |
3549 | case OFPUTIL_P_OF13_OXM: | |
c37c0382 | 3550 | case OFPUTIL_P_OF14_OXM: |
42dccab5 | 3551 | case OFPUTIL_P_OF15_OXM: |
0f83fea0 BP |
3552 | packet = ofputil_encode_ofp12_packet_in(pin, protocol); |
3553 | break; | |
3554 | ||
3555 | default: | |
428b2edd | 3556 | OVS_NOT_REACHED(); |
54834960 | 3557 | } |
54834960 | 3558 | |
0f83fea0 | 3559 | ofpmsg_update_length(packet); |
54834960 | 3560 | return packet; |
ebb57021 BP |
3561 | } |
3562 | ||
5014a89d BP |
3563 | /* Returns a string form of 'reason'. The return value is either a statically |
3564 | * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'. | |
3565 | * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */ | |
7c1a76a4 | 3566 | const char * |
5014a89d BP |
3567 | ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason, |
3568 | char *reasonbuf, size_t bufsize) | |
7c1a76a4 | 3569 | { |
7c1a76a4 BP |
3570 | switch (reason) { |
3571 | case OFPR_NO_MATCH: | |
3572 | return "no_match"; | |
3573 | case OFPR_ACTION: | |
3574 | return "action"; | |
3575 | case OFPR_INVALID_TTL: | |
3576 | return "invalid_ttl"; | |
424467c6 SS |
3577 | case OFPR_ACTION_SET: |
3578 | return "action_set"; | |
3579 | case OFPR_GROUP: | |
3580 | return "group"; | |
3581 | case OFPR_PACKET_OUT: | |
3582 | return "packet_out"; | |
7c1a76a4 BP |
3583 | |
3584 | case OFPR_N_REASONS: | |
3585 | default: | |
5014a89d BP |
3586 | snprintf(reasonbuf, bufsize, "%d", (int) reason); |
3587 | return reasonbuf; | |
7c1a76a4 BP |
3588 | } |
3589 | } | |
3590 | ||
3591 | bool | |
3592 | ofputil_packet_in_reason_from_string(const char *s, | |
3593 | enum ofp_packet_in_reason *reason) | |
3594 | { | |
3595 | int i; | |
3596 | ||
3597 | for (i = 0; i < OFPR_N_REASONS; i++) { | |
5014a89d BP |
3598 | char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE]; |
3599 | const char *reason_s; | |
3600 | ||
3601 | reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf, | |
3602 | sizeof reasonbuf); | |
3603 | if (!strcasecmp(s, reason_s)) { | |
7c1a76a4 BP |
3604 | *reason = i; |
3605 | return true; | |
3606 | } | |
3607 | } | |
3608 | return false; | |
3609 | } | |
3610 | ||
f25d0cf3 BP |
3611 | /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in |
3612 | * 'po'. | |
3613 | * | |
3614 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out | |
3615 | * message's actions. The caller must initialize 'ofpacts' and retains | |
3616 | * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer. | |
3617 | * | |
3618 | * Returns 0 if successful, otherwise an OFPERR_* value. */ | |
c6a93eb7 BP |
3619 | enum ofperr |
3620 | ofputil_decode_packet_out(struct ofputil_packet_out *po, | |
982697a4 | 3621 | const struct ofp_header *oh, |
f25d0cf3 | 3622 | struct ofpbuf *ofpacts) |
c6a93eb7 | 3623 | { |
982697a4 | 3624 | enum ofpraw raw; |
c6a93eb7 BP |
3625 | struct ofpbuf b; |
3626 | ||
982697a4 BP |
3627 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
3628 | raw = ofpraw_pull_assert(&b); | |
982697a4 | 3629 | |
eb5ee596 SH |
3630 | if (raw == OFPRAW_OFPT11_PACKET_OUT) { |
3631 | enum ofperr error; | |
3632 | const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo); | |
3633 | ||
3634 | po->buffer_id = ntohl(opo->buffer_id); | |
3635 | error = ofputil_port_from_ofp11(opo->in_port, &po->in_port); | |
3636 | if (error) { | |
3637 | return error; | |
3638 | } | |
3639 | ||
e3f8f887 JR |
3640 | error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len), |
3641 | oh->version, ofpacts); | |
eb5ee596 SH |
3642 | if (error) { |
3643 | return error; | |
3644 | } | |
eb5ee596 | 3645 | } else if (raw == OFPRAW_OFPT10_PACKET_OUT) { |
8a6bc7cd | 3646 | enum ofperr error; |
31a9e63f | 3647 | const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo); |
8a6bc7cd SH |
3648 | |
3649 | po->buffer_id = ntohl(opo->buffer_id); | |
4e022ec0 | 3650 | po->in_port = u16_to_ofp(ntohs(opo->in_port)); |
8a6bc7cd | 3651 | |
e3f8f887 JR |
3652 | error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len), |
3653 | oh->version, ofpacts); | |
8a6bc7cd SH |
3654 | if (error) { |
3655 | return error; | |
3656 | } | |
3657 | } else { | |
428b2edd | 3658 | OVS_NOT_REACHED(); |
8a6bc7cd SH |
3659 | } |
3660 | ||
4e022ec0 AW |
3661 | if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX) |
3662 | && po->in_port != OFPP_LOCAL | |
751c7785 | 3663 | && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) { |
c6a93eb7 BP |
3664 | VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16, |
3665 | po->in_port); | |
2e1bfcb6 | 3666 | return OFPERR_OFPBRC_BAD_PORT; |
c6a93eb7 BP |
3667 | } |
3668 | ||
6fd6ed71 PS |
3669 | po->ofpacts = ofpacts->data; |
3670 | po->ofpacts_len = ofpacts->size; | |
c6a93eb7 BP |
3671 | |
3672 | if (po->buffer_id == UINT32_MAX) { | |
6fd6ed71 PS |
3673 | po->packet = b.data; |
3674 | po->packet_len = b.size; | |
c6a93eb7 BP |
3675 | } else { |
3676 | po->packet = NULL; | |
3677 | po->packet_len = 0; | |
3678 | } | |
3679 | ||
3680 | return 0; | |
3681 | } | |
6c038611 BP |
3682 | \f |
3683 | /* ofputil_phy_port */ | |
3684 | ||
3685 | /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */ | |
3686 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ | |
3687 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
3688 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
3689 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
3690 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
3691 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
3692 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
3693 | ||
3694 | /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */ | |
9e1fd49b BP |
3695 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4)); |
3696 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4)); | |
3697 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4)); | |
3698 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4)); | |
3699 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4)); | |
6c038611 | 3700 | |
9e1fd49b BP |
3701 | static enum netdev_features |
3702 | netdev_port_features_from_ofp10(ovs_be32 ofp10_) | |
6c038611 BP |
3703 | { |
3704 | uint32_t ofp10 = ntohl(ofp10_); | |
3705 | return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4); | |
3706 | } | |
3707 | ||
9e1fd49b BP |
3708 | static ovs_be32 |
3709 | netdev_port_features_to_ofp10(enum netdev_features features) | |
6c038611 BP |
3710 | { |
3711 | return htonl((features & 0x7f) | ((features & 0xf800) >> 4)); | |
3712 | } | |
c6a93eb7 | 3713 | |
9e1fd49b BP |
3714 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ |
3715 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
3716 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
3717 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
3718 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
3719 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
3720 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
3721 | BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */ | |
3722 | BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */ | |
3723 | BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */ | |
3724 | BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */ | |
3725 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */ | |
3726 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */ | |
3727 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */ | |
3728 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */ | |
3729 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */ | |
3730 | ||
3731 | static enum netdev_features | |
3732 | netdev_port_features_from_ofp11(ovs_be32 ofp11) | |
3733 | { | |
3734 | return ntohl(ofp11) & 0xffff; | |
3735 | } | |
3736 | ||
3737 | static ovs_be32 | |
3738 | netdev_port_features_to_ofp11(enum netdev_features features) | |
3739 | { | |
3740 | return htonl(features & 0xffff); | |
3741 | } | |
3742 | ||
3743 | static enum ofperr | |
3744 | ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp, | |
3745 | const struct ofp10_phy_port *opp) | |
3746 | { | |
4e022ec0 | 3747 | pp->port_no = u16_to_ofp(ntohs(opp->port_no)); |
9e1fd49b BP |
3748 | memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN); |
3749 | ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN); | |
3750 | ||
3751 | pp->config = ntohl(opp->config) & OFPPC10_ALL; | |
3752 | pp->state = ntohl(opp->state) & OFPPS10_ALL; | |
3753 | ||
3754 | pp->curr = netdev_port_features_from_ofp10(opp->curr); | |
3755 | pp->advertised = netdev_port_features_from_ofp10(opp->advertised); | |
3756 | pp->supported = netdev_port_features_from_ofp10(opp->supported); | |
3757 | pp->peer = netdev_port_features_from_ofp10(opp->peer); | |
3758 | ||
d02a5f8e BP |
3759 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000; |
3760 | pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000; | |
9e1fd49b BP |
3761 | |
3762 | return 0; | |
3763 | } | |
3764 | ||
3765 | static enum ofperr | |
3766 | ofputil_decode_ofp11_port(struct ofputil_phy_port *pp, | |
3767 | const struct ofp11_port *op) | |
3768 | { | |
3769 | enum ofperr error; | |
3770 | ||
9e1fd49b BP |
3771 | error = ofputil_port_from_ofp11(op->port_no, &pp->port_no); |
3772 | if (error) { | |
3773 | return error; | |
3774 | } | |
3775 | memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN); | |
3776 | ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN); | |
3777 | ||
3778 | pp->config = ntohl(op->config) & OFPPC11_ALL; | |
646f2b37 | 3779 | pp->state = ntohl(op->state) & OFPPS11_ALL; |
9e1fd49b BP |
3780 | |
3781 | pp->curr = netdev_port_features_from_ofp11(op->curr); | |
3782 | pp->advertised = netdev_port_features_from_ofp11(op->advertised); | |
3783 | pp->supported = netdev_port_features_from_ofp11(op->supported); | |
3784 | pp->peer = netdev_port_features_from_ofp11(op->peer); | |
3785 | ||
3786 | pp->curr_speed = ntohl(op->curr_speed); | |
3787 | pp->max_speed = ntohl(op->max_speed); | |
3788 | ||
3789 | return 0; | |
3790 | } | |
3791 | ||
8c3cc785 BP |
3792 | static enum ofperr |
3793 | parse_ofp14_port_ethernet_property(const struct ofpbuf *payload, | |
3794 | struct ofputil_phy_port *pp) | |
3795 | { | |
6fd6ed71 | 3796 | struct ofp14_port_desc_prop_ethernet *eth = payload->data; |
8c3cc785 | 3797 | |
6fd6ed71 | 3798 | if (payload->size != sizeof *eth) { |
8c3cc785 BP |
3799 | return OFPERR_OFPBPC_BAD_LEN; |
3800 | } | |
3801 | ||
3802 | pp->curr = netdev_port_features_from_ofp11(eth->curr); | |
3803 | pp->advertised = netdev_port_features_from_ofp11(eth->advertised); | |
3804 | pp->supported = netdev_port_features_from_ofp11(eth->supported); | |
3805 | pp->peer = netdev_port_features_from_ofp11(eth->peer); | |
3806 | ||
3807 | pp->curr_speed = ntohl(eth->curr_speed); | |
3808 | pp->max_speed = ntohl(eth->max_speed); | |
3809 | ||
3810 | return 0; | |
3811 | } | |
3812 | ||
3813 | static enum ofperr | |
3814 | ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg) | |
3815 | { | |
3816 | struct ofpbuf properties; | |
3817 | struct ofp14_port *op; | |
3818 | enum ofperr error; | |
3819 | size_t len; | |
3820 | ||
3821 | op = ofpbuf_try_pull(msg, sizeof *op); | |
3822 | if (!op) { | |
3823 | return OFPERR_OFPBRC_BAD_LEN; | |
3824 | } | |
3825 | ||
3826 | len = ntohs(op->length); | |
6fd6ed71 | 3827 | if (len < sizeof *op || len - sizeof *op > msg->size) { |
8c3cc785 BP |
3828 | return OFPERR_OFPBRC_BAD_LEN; |
3829 | } | |
3830 | len -= sizeof *op; | |
3831 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len); | |
3832 | ||
3833 | error = ofputil_port_from_ofp11(op->port_no, &pp->port_no); | |
3834 | if (error) { | |
3835 | return error; | |
3836 | } | |
3837 | memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN); | |
3838 | ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN); | |
3839 | ||
3840 | pp->config = ntohl(op->config) & OFPPC11_ALL; | |
3841 | pp->state = ntohl(op->state) & OFPPS11_ALL; | |
3842 | ||
6fd6ed71 | 3843 | while (properties.size > 0) { |
8c3cc785 BP |
3844 | struct ofpbuf payload; |
3845 | enum ofperr error; | |
3846 | uint16_t type; | |
3847 | ||
3848 | error = ofputil_pull_property(&properties, &payload, &type); | |
3849 | if (error) { | |
3850 | return error; | |
3851 | } | |
3852 | ||
3853 | switch (type) { | |
3854 | case OFPPDPT14_ETHERNET: | |
3855 | error = parse_ofp14_port_ethernet_property(&payload, pp); | |
3856 | break; | |
3857 | ||
3858 | default: | |
3859 | log_property(true, "unknown port property %"PRIu16, type); | |
3860 | error = 0; | |
3861 | break; | |
3862 | } | |
3863 | ||
3864 | if (error) { | |
3865 | return error; | |
3866 | } | |
3867 | } | |
3868 | ||
3869 | return 0; | |
3870 | } | |
3871 | ||
9e1fd49b BP |
3872 | static void |
3873 | ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp, | |
3874 | struct ofp10_phy_port *opp) | |
3875 | { | |
3876 | memset(opp, 0, sizeof *opp); | |
3877 | ||
4e022ec0 | 3878 | opp->port_no = htons(ofp_to_u16(pp->port_no)); |
9e1fd49b BP |
3879 | memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN); |
3880 | ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
3881 | ||
3882 | opp->config = htonl(pp->config & OFPPC10_ALL); | |
3883 | opp->state = htonl(pp->state & OFPPS10_ALL); | |
3884 | ||
3885 | opp->curr = netdev_port_features_to_ofp10(pp->curr); | |
3886 | opp->advertised = netdev_port_features_to_ofp10(pp->advertised); | |
3887 | opp->supported = netdev_port_features_to_ofp10(pp->supported); | |
3888 | opp->peer = netdev_port_features_to_ofp10(pp->peer); | |
3889 | } | |
3890 | ||
3891 | static void | |
3892 | ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp, | |
3893 | struct ofp11_port *op) | |
3894 | { | |
3895 | memset(op, 0, sizeof *op); | |
3896 | ||
3897 | op->port_no = ofputil_port_to_ofp11(pp->port_no); | |
3898 | memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
3899 | ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
3900 | ||
3901 | op->config = htonl(pp->config & OFPPC11_ALL); | |
3902 | op->state = htonl(pp->state & OFPPS11_ALL); | |
3903 | ||
3904 | op->curr = netdev_port_features_to_ofp11(pp->curr); | |
3905 | op->advertised = netdev_port_features_to_ofp11(pp->advertised); | |
3906 | op->supported = netdev_port_features_to_ofp11(pp->supported); | |
3907 | op->peer = netdev_port_features_to_ofp11(pp->peer); | |
3908 | ||
3909 | op->curr_speed = htonl(pp->curr_speed); | |
3910 | op->max_speed = htonl(pp->max_speed); | |
3911 | } | |
3912 | ||
8c3cc785 BP |
3913 | static void |
3914 | ofputil_put_ofp14_port(const struct ofputil_phy_port *pp, | |
3915 | struct ofpbuf *b) | |
3916 | { | |
3917 | struct ofp14_port *op; | |
3918 | struct ofp14_port_desc_prop_ethernet *eth; | |
3919 | ||
3920 | ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth); | |
3921 | ||
3922 | op = ofpbuf_put_zeros(b, sizeof *op); | |
3923 | op->port_no = ofputil_port_to_ofp11(pp->port_no); | |
3924 | op->length = htons(sizeof *op + sizeof *eth); | |
3925 | memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
3926 | ovs_strlcpy(op->name, pp->name, sizeof op->name); | |
3927 | op->config = htonl(pp->config & OFPPC11_ALL); | |
3928 | op->state = htonl(pp->state & OFPPS11_ALL); | |
3929 | ||
3930 | eth = ofpbuf_put_zeros(b, sizeof *eth); | |
3931 | eth->type = htons(OFPPDPT14_ETHERNET); | |
3932 | eth->length = htons(sizeof *eth); | |
3933 | eth->curr = netdev_port_features_to_ofp11(pp->curr); | |
3934 | eth->advertised = netdev_port_features_to_ofp11(pp->advertised); | |
3935 | eth->supported = netdev_port_features_to_ofp11(pp->supported); | |
3936 | eth->peer = netdev_port_features_to_ofp11(pp->peer); | |
3937 | eth->curr_speed = htonl(pp->curr_speed); | |
3938 | eth->max_speed = htonl(pp->max_speed); | |
3939 | } | |
3940 | ||
9e1fd49b | 3941 | static void |
2e3fa633 SH |
3942 | ofputil_put_phy_port(enum ofp_version ofp_version, |
3943 | const struct ofputil_phy_port *pp, struct ofpbuf *b) | |
9e1fd49b | 3944 | { |
2e3fa633 SH |
3945 | switch (ofp_version) { |
3946 | case OFP10_VERSION: { | |
6b0f20ac BP |
3947 | struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp); |
3948 | ofputil_encode_ofp10_phy_port(pp, opp); | |
2e3fa633 SH |
3949 | break; |
3950 | } | |
3951 | ||
3952 | case OFP11_VERSION: | |
2e1ae200 JR |
3953 | case OFP12_VERSION: |
3954 | case OFP13_VERSION: { | |
6b0f20ac BP |
3955 | struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op); |
3956 | ofputil_encode_ofp11_port(pp, op); | |
2e3fa633 SH |
3957 | break; |
3958 | } | |
3959 | ||
c37c0382 | 3960 | case OFP14_VERSION: |
42dccab5 | 3961 | case OFP15_VERSION: |
8c3cc785 | 3962 | ofputil_put_ofp14_port(pp, b); |
c37c0382 AC |
3963 | break; |
3964 | ||
2e3fa633 | 3965 | default: |
428b2edd | 3966 | OVS_NOT_REACHED(); |
9e1fd49b BP |
3967 | } |
3968 | } | |
2be393ed | 3969 | |
70ae4f93 BP |
3970 | enum ofperr |
3971 | ofputil_decode_port_desc_stats_request(const struct ofp_header *request, | |
3972 | ofp_port_t *port) | |
3973 | { | |
3974 | struct ofpbuf b; | |
3975 | enum ofpraw raw; | |
3976 | ||
3977 | ofpbuf_use_const(&b, request, ntohs(request->length)); | |
3978 | raw = ofpraw_pull_assert(&b); | |
3979 | if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) { | |
3980 | *port = OFPP_ANY; | |
3981 | return 0; | |
3982 | } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) { | |
3983 | ovs_be32 *ofp11_port; | |
3984 | ||
3985 | ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port); | |
3986 | return ofputil_port_from_ofp11(*ofp11_port, port); | |
3987 | } else { | |
3988 | OVS_NOT_REACHED(); | |
3989 | } | |
3990 | } | |
3991 | ||
3992 | struct ofpbuf * | |
3993 | ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version, | |
3994 | ofp_port_t port) | |
3995 | { | |
3996 | struct ofpbuf *request; | |
3997 | ovs_be32 ofp11_port; | |
3998 | ||
3999 | switch (ofp_version) { | |
4000 | case OFP10_VERSION: | |
4001 | case OFP11_VERSION: | |
4002 | case OFP12_VERSION: | |
4003 | case OFP13_VERSION: | |
4004 | case OFP14_VERSION: | |
4005 | request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST, | |
4006 | ofp_version, 0); | |
4007 | break; | |
4008 | ||
4009 | case OFP15_VERSION: | |
4010 | request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST, | |
4011 | ofp_version, 0); | |
4012 | ofp11_port = ofputil_port_to_ofp11(port); | |
4013 | ofpbuf_put(request, &ofp11_port, sizeof ofp11_port); | |
4014 | break; | |
4015 | ||
4016 | default: | |
4017 | OVS_NOT_REACHED(); | |
4018 | } | |
4019 | ||
4020 | return request; | |
4021 | } | |
4022 | ||
2be393ed | 4023 | void |
e28ac5cf | 4024 | ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp, |
ca6ba700 | 4025 | struct ovs_list *replies) |
2be393ed | 4026 | { |
6b0f20ac | 4027 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); |
6fd6ed71 | 4028 | size_t start_ofs = reply->size; |
c37c0382 | 4029 | |
6b0f20ac BP |
4030 | ofputil_put_phy_port(ofpmp_version(replies), pp, reply); |
4031 | ofpmp_postappend(replies, start_ofs); | |
2be393ed | 4032 | } |
9e1fd49b BP |
4033 | \f |
4034 | /* ofputil_switch_features */ | |
4035 | ||
4036 | #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \ | |
60202987 | 4037 | OFPC_IP_REASM | OFPC_QUEUE_STATS) |
9e1fd49b BP |
4038 | BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS); |
4039 | BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS); | |
4040 | BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS); | |
4041 | BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM); | |
4042 | BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS); | |
4043 | BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP); | |
4044 | ||
60202987 SH |
4045 | static uint32_t |
4046 | ofputil_capabilities_mask(enum ofp_version ofp_version) | |
4047 | { | |
0746a84f | 4048 | /* Handle capabilities whose bit is unique for all OpenFlow versions */ |
60202987 SH |
4049 | switch (ofp_version) { |
4050 | case OFP10_VERSION: | |
4051 | case OFP11_VERSION: | |
4052 | return OFPC_COMMON | OFPC_ARP_MATCH_IP; | |
4053 | case OFP12_VERSION: | |
2e1ae200 | 4054 | case OFP13_VERSION: |
c37c0382 | 4055 | case OFP14_VERSION: |
42dccab5 | 4056 | case OFP15_VERSION: |
18cc69d9 | 4057 | return OFPC_COMMON | OFPC12_PORT_BLOCKED; |
60202987 SH |
4058 | default: |
4059 | /* Caller needs to check osf->header.version itself */ | |
4060 | return 0; | |
4061 | } | |
4062 | } | |
4063 | ||
9e1fd49b BP |
4064 | /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an |
4065 | * abstract representation in '*features'. Initializes '*b' to iterate over | |
4066 | * the OpenFlow port structures following 'osf' with later calls to | |
2be393ed | 4067 | * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an |
9e1fd49b BP |
4068 | * OFPERR_* value. */ |
4069 | enum ofperr | |
982697a4 | 4070 | ofputil_decode_switch_features(const struct ofp_header *oh, |
9e1fd49b BP |
4071 | struct ofputil_switch_features *features, |
4072 | struct ofpbuf *b) | |
4073 | { | |
982697a4 BP |
4074 | const struct ofp_switch_features *osf; |
4075 | enum ofpraw raw; | |
4076 | ||
4077 | ofpbuf_use_const(b, oh, ntohs(oh->length)); | |
4078 | raw = ofpraw_pull_assert(b); | |
9e1fd49b | 4079 | |
982697a4 | 4080 | osf = ofpbuf_pull(b, sizeof *osf); |
9e1fd49b BP |
4081 | features->datapath_id = ntohll(osf->datapath_id); |
4082 | features->n_buffers = ntohl(osf->n_buffers); | |
4083 | features->n_tables = osf->n_tables; | |
2e1ae200 | 4084 | features->auxiliary_id = 0; |
9e1fd49b | 4085 | |
60202987 SH |
4086 | features->capabilities = ntohl(osf->capabilities) & |
4087 | ofputil_capabilities_mask(oh->version); | |
9e1fd49b | 4088 | |
982697a4 | 4089 | if (raw == OFPRAW_OFPT10_FEATURES_REPLY) { |
9e1fd49b BP |
4090 | if (osf->capabilities & htonl(OFPC10_STP)) { |
4091 | features->capabilities |= OFPUTIL_C_STP; | |
4092 | } | |
08d1e234 BP |
4093 | features->ofpacts = ofpact_bitmap_from_openflow(osf->actions, |
4094 | OFP10_VERSION); | |
2e1ae200 JR |
4095 | } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY |
4096 | || raw == OFPRAW_OFPT13_FEATURES_REPLY) { | |
9e1fd49b BP |
4097 | if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) { |
4098 | features->capabilities |= OFPUTIL_C_GROUP_STATS; | |
4099 | } | |
08d1e234 | 4100 | features->ofpacts = 0; |
2e1ae200 JR |
4101 | if (raw == OFPRAW_OFPT13_FEATURES_REPLY) { |
4102 | features->auxiliary_id = osf->auxiliary_id; | |
4103 | } | |
9e1fd49b BP |
4104 | } else { |
4105 | return OFPERR_OFPBRC_BAD_VERSION; | |
4106 | } | |
4107 | ||
4108 | return 0; | |
4109 | } | |
4110 | ||
fca6d553 BP |
4111 | /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the |
4112 | * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and | |
4113 | * later, an OFPT_FEATURES_REPLY does not list ports at all. | |
4114 | * | |
4115 | * Given a buffer 'b' that contains a Features Reply message, this message | |
4116 | * checks if it contains a complete list of the switch's ports. Returns true, | |
4117 | * if so. Returns false if the list is missing (OF1.3+) or incomplete | |
4118 | * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the | |
4119 | * message. | |
4120 | * | |
4121 | * When this function returns false, the caller should send an OFPST_PORT_DESC | |
4122 | * stats request to get the ports. */ | |
347b7ac4 | 4123 | bool |
fca6d553 | 4124 | ofputil_switch_features_has_ports(struct ofpbuf *b) |
347b7ac4 | 4125 | { |
6fd6ed71 | 4126 | struct ofp_header *oh = b->data; |
13e1aff8 | 4127 | size_t phy_port_size; |
347b7ac4 | 4128 | |
fca6d553 | 4129 | if (oh->version >= OFP13_VERSION) { |
13e1aff8 | 4130 | /* OpenFlow 1.3+ never has ports in the feature reply. */ |
fca6d553 | 4131 | return false; |
13e1aff8 BP |
4132 | } |
4133 | ||
4134 | phy_port_size = (oh->version == OFP10_VERSION | |
4135 | ? sizeof(struct ofp10_phy_port) | |
4136 | : sizeof(struct ofp11_port)); | |
4137 | if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) { | |
4138 | /* There's room for additional ports in the feature reply. | |
4139 | * Assume that the list is complete. */ | |
347b7ac4 JP |
4140 | return true; |
4141 | } | |
13e1aff8 BP |
4142 | |
4143 | /* The feature reply has no room for more ports. Probably the list is | |
4144 | * truncated. Drop the ports and tell the caller to retrieve them with | |
4145 | * OFPST_PORT_DESC. */ | |
6fd6ed71 | 4146 | b->size = sizeof *oh + sizeof(struct ofp_switch_features); |
13e1aff8 BP |
4147 | ofpmsg_update_length(b); |
4148 | return false; | |
347b7ac4 JP |
4149 | } |
4150 | ||
9e1fd49b BP |
4151 | /* Returns a buffer owned by the caller that encodes 'features' in the format |
4152 | * required by 'protocol' with the given 'xid'. The caller should append port | |
4153 | * information to the buffer with subsequent calls to | |
4154 | * ofputil_put_switch_features_port(). */ | |
4155 | struct ofpbuf * | |
4156 | ofputil_encode_switch_features(const struct ofputil_switch_features *features, | |
4157 | enum ofputil_protocol protocol, ovs_be32 xid) | |
4158 | { | |
4159 | struct ofp_switch_features *osf; | |
4160 | struct ofpbuf *b; | |
2e3fa633 SH |
4161 | enum ofp_version version; |
4162 | enum ofpraw raw; | |
982697a4 BP |
4163 | |
4164 | version = ofputil_protocol_to_ofp_version(protocol); | |
2e3fa633 SH |
4165 | switch (version) { |
4166 | case OFP10_VERSION: | |
4167 | raw = OFPRAW_OFPT10_FEATURES_REPLY; | |
4168 | break; | |
4169 | case OFP11_VERSION: | |
4170 | case OFP12_VERSION: | |
4171 | raw = OFPRAW_OFPT11_FEATURES_REPLY; | |
4172 | break; | |
2e1ae200 | 4173 | case OFP13_VERSION: |
c37c0382 | 4174 | case OFP14_VERSION: |
42dccab5 | 4175 | case OFP15_VERSION: |
2e1ae200 JR |
4176 | raw = OFPRAW_OFPT13_FEATURES_REPLY; |
4177 | break; | |
2e3fa633 | 4178 | default: |
428b2edd | 4179 | OVS_NOT_REACHED(); |
2e3fa633 SH |
4180 | } |
4181 | b = ofpraw_alloc_xid(raw, version, xid, 0); | |
982697a4 | 4182 | osf = ofpbuf_put_zeros(b, sizeof *osf); |
9e1fd49b BP |
4183 | osf->datapath_id = htonll(features->datapath_id); |
4184 | osf->n_buffers = htonl(features->n_buffers); | |
4185 | osf->n_tables = features->n_tables; | |
4186 | ||
4187 | osf->capabilities = htonl(features->capabilities & OFPC_COMMON); | |
60202987 SH |
4188 | osf->capabilities = htonl(features->capabilities & |
4189 | ofputil_capabilities_mask(version)); | |
2e3fa633 SH |
4190 | switch (version) { |
4191 | case OFP10_VERSION: | |
9e1fd49b BP |
4192 | if (features->capabilities & OFPUTIL_C_STP) { |
4193 | osf->capabilities |= htonl(OFPC10_STP); | |
4194 | } | |
08d1e234 BP |
4195 | osf->actions = ofpact_bitmap_to_openflow(features->ofpacts, |
4196 | OFP10_VERSION); | |
2e3fa633 | 4197 | break; |
2e1ae200 | 4198 | case OFP13_VERSION: |
c37c0382 | 4199 | case OFP14_VERSION: |
42dccab5 | 4200 | case OFP15_VERSION: |
2e1ae200 JR |
4201 | osf->auxiliary_id = features->auxiliary_id; |
4202 | /* fall through */ | |
2e3fa633 SH |
4203 | case OFP11_VERSION: |
4204 | case OFP12_VERSION: | |
9e1fd49b BP |
4205 | if (features->capabilities & OFPUTIL_C_GROUP_STATS) { |
4206 | osf->capabilities |= htonl(OFPC11_GROUP_STATS); | |
4207 | } | |
2e3fa633 SH |
4208 | break; |
4209 | default: | |
428b2edd | 4210 | OVS_NOT_REACHED(); |
9e1fd49b BP |
4211 | } |
4212 | ||
4213 | return b; | |
4214 | } | |
4215 | ||
4216 | /* Encodes 'pp' into the format required by the switch_features message already | |
4217 | * in 'b', which should have been returned by ofputil_encode_switch_features(), | |
4218 | * and appends the encoded version to 'b'. */ | |
4219 | void | |
4220 | ofputil_put_switch_features_port(const struct ofputil_phy_port *pp, | |
4221 | struct ofpbuf *b) | |
4222 | { | |
6fd6ed71 | 4223 | const struct ofp_header *oh = b->data; |
9e1fd49b | 4224 | |
e0c91c0c | 4225 | if (oh->version < OFP13_VERSION) { |
6b0f20ac BP |
4226 | /* Try adding a port description to the message, but drop it again if |
4227 | * the buffer overflows. (This possibility for overflow is why | |
4228 | * OpenFlow 1.3+ moved port descriptions into a multipart message.) */ | |
6fd6ed71 | 4229 | size_t start_ofs = b->size; |
e0c91c0c | 4230 | ofputil_put_phy_port(oh->version, pp, b); |
6fd6ed71 PS |
4231 | if (b->size > UINT16_MAX) { |
4232 | b->size = start_ofs; | |
6b0f20ac | 4233 | } |
e0c91c0c | 4234 | } |
9e1fd49b BP |
4235 | } |
4236 | \f | |
4237 | /* ofputil_port_status */ | |
4238 | ||
4239 | /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form | |
4240 | * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
4241 | enum ofperr | |
982697a4 | 4242 | ofputil_decode_port_status(const struct ofp_header *oh, |
9e1fd49b BP |
4243 | struct ofputil_port_status *ps) |
4244 | { | |
982697a4 | 4245 | const struct ofp_port_status *ops; |
9e1fd49b BP |
4246 | struct ofpbuf b; |
4247 | int retval; | |
4248 | ||
982697a4 BP |
4249 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
4250 | ofpraw_pull_assert(&b); | |
4251 | ops = ofpbuf_pull(&b, sizeof *ops); | |
4252 | ||
9e1fd49b BP |
4253 | if (ops->reason != OFPPR_ADD && |
4254 | ops->reason != OFPPR_DELETE && | |
4255 | ops->reason != OFPPR_MODIFY) { | |
4256 | return OFPERR_NXBRC_BAD_REASON; | |
4257 | } | |
4258 | ps->reason = ops->reason; | |
4259 | ||
982697a4 | 4260 | retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc); |
cb22974d | 4261 | ovs_assert(retval != EOF); |
9e1fd49b BP |
4262 | return retval; |
4263 | } | |
4264 | ||
4265 | /* Converts the abstract form of a "port status" message in '*ps' into an | |
4266 | * OpenFlow message suitable for 'protocol', and returns that encoded form in | |
4267 | * a buffer owned by the caller. */ | |
4268 | struct ofpbuf * | |
4269 | ofputil_encode_port_status(const struct ofputil_port_status *ps, | |
4270 | enum ofputil_protocol protocol) | |
4271 | { | |
4272 | struct ofp_port_status *ops; | |
4273 | struct ofpbuf *b; | |
2e3fa633 SH |
4274 | enum ofp_version version; |
4275 | enum ofpraw raw; | |
982697a4 BP |
4276 | |
4277 | version = ofputil_protocol_to_ofp_version(protocol); | |
2e3fa633 SH |
4278 | switch (version) { |
4279 | case OFP10_VERSION: | |
4280 | raw = OFPRAW_OFPT10_PORT_STATUS; | |
4281 | break; | |
4282 | ||
4283 | case OFP11_VERSION: | |
4284 | case OFP12_VERSION: | |
2e1ae200 | 4285 | case OFP13_VERSION: |
2e3fa633 SH |
4286 | raw = OFPRAW_OFPT11_PORT_STATUS; |
4287 | break; | |
4288 | ||
8c3cc785 | 4289 | case OFP14_VERSION: |
42dccab5 | 4290 | case OFP15_VERSION: |
8c3cc785 BP |
4291 | raw = OFPRAW_OFPT14_PORT_STATUS; |
4292 | break; | |
4293 | ||
2e3fa633 | 4294 | default: |
428b2edd | 4295 | OVS_NOT_REACHED(); |
2e3fa633 SH |
4296 | } |
4297 | ||
4298 | b = ofpraw_alloc_xid(raw, version, htonl(0), 0); | |
982697a4 | 4299 | ops = ofpbuf_put_zeros(b, sizeof *ops); |
9e1fd49b | 4300 | ops->reason = ps->reason; |
982697a4 BP |
4301 | ofputil_put_phy_port(version, &ps->desc, b); |
4302 | ofpmsg_update_length(b); | |
9e1fd49b BP |
4303 | return b; |
4304 | } | |
918f2b82 | 4305 | |
9e1fd49b BP |
4306 | /* ofputil_port_mod */ |
4307 | ||
18cc69d9 BP |
4308 | static enum ofperr |
4309 | parse_port_mod_ethernet_property(struct ofpbuf *property, | |
4310 | struct ofputil_port_mod *pm) | |
4311 | { | |
6fd6ed71 | 4312 | struct ofp14_port_mod_prop_ethernet *eth = property->data; |
18cc69d9 | 4313 | |
6fd6ed71 | 4314 | if (property->size != sizeof *eth) { |
18cc69d9 BP |
4315 | return OFPERR_OFPBRC_BAD_LEN; |
4316 | } | |
4317 | ||
4318 | pm->advertise = netdev_port_features_from_ofp11(eth->advertise); | |
4319 | return 0; | |
4320 | } | |
4321 | ||
9e1fd49b BP |
4322 | /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in |
4323 | * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
4324 | enum ofperr | |
4325 | ofputil_decode_port_mod(const struct ofp_header *oh, | |
18cc69d9 | 4326 | struct ofputil_port_mod *pm, bool loose) |
9e1fd49b | 4327 | { |
982697a4 BP |
4328 | enum ofpraw raw; |
4329 | struct ofpbuf b; | |
9e1fd49b | 4330 | |
982697a4 BP |
4331 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
4332 | raw = ofpraw_pull_assert(&b); | |
4333 | ||
4334 | if (raw == OFPRAW_OFPT10_PORT_MOD) { | |
6fd6ed71 | 4335 | const struct ofp10_port_mod *opm = b.data; |
9e1fd49b | 4336 | |
4e022ec0 | 4337 | pm->port_no = u16_to_ofp(ntohs(opm->port_no)); |
9e1fd49b BP |
4338 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); |
4339 | pm->config = ntohl(opm->config) & OFPPC10_ALL; | |
4340 | pm->mask = ntohl(opm->mask) & OFPPC10_ALL; | |
4341 | pm->advertise = netdev_port_features_from_ofp10(opm->advertise); | |
982697a4 | 4342 | } else if (raw == OFPRAW_OFPT11_PORT_MOD) { |
6fd6ed71 | 4343 | const struct ofp11_port_mod *opm = b.data; |
9e1fd49b BP |
4344 | enum ofperr error; |
4345 | ||
9e1fd49b BP |
4346 | error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no); |
4347 | if (error) { | |
4348 | return error; | |
4349 | } | |
4350 | ||
4351 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
4352 | pm->config = ntohl(opm->config) & OFPPC11_ALL; | |
4353 | pm->mask = ntohl(opm->mask) & OFPPC11_ALL; | |
4354 | pm->advertise = netdev_port_features_from_ofp11(opm->advertise); | |
18cc69d9 BP |
4355 | } else if (raw == OFPRAW_OFPT14_PORT_MOD) { |
4356 | const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm); | |
4357 | enum ofperr error; | |
4358 | ||
4359 | memset(pm, 0, sizeof *pm); | |
4360 | ||
4361 | error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no); | |
4362 | if (error) { | |
4363 | return error; | |
4364 | } | |
4365 | ||
4366 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
4367 | pm->config = ntohl(opm->config) & OFPPC11_ALL; | |
4368 | pm->mask = ntohl(opm->mask) & OFPPC11_ALL; | |
4369 | ||
6fd6ed71 | 4370 | while (b.size > 0) { |
18cc69d9 BP |
4371 | struct ofpbuf property; |
4372 | enum ofperr error; | |
4373 | uint16_t type; | |
4374 | ||
4375 | error = ofputil_pull_property(&b, &property, &type); | |
4376 | if (error) { | |
4377 | return error; | |
4378 | } | |
4379 | ||
4380 | switch (type) { | |
4381 | case OFPPMPT14_ETHERNET: | |
4382 | error = parse_port_mod_ethernet_property(&property, pm); | |
4383 | break; | |
4384 | ||
4385 | default: | |
4386 | log_property(loose, "unknown port_mod property %"PRIu16, type); | |
4387 | if (loose) { | |
4388 | error = 0; | |
4389 | } else if (type == OFPPMPT14_EXPERIMENTER) { | |
4390 | error = OFPERR_OFPBPC_BAD_EXPERIMENTER; | |
4391 | } else { | |
4392 | error = OFPERR_OFPBRC_BAD_TYPE; | |
4393 | } | |
4394 | break; | |
4395 | } | |
4396 | ||
4397 | if (error) { | |
4398 | return error; | |
4399 | } | |
4400 | } | |
9e1fd49b | 4401 | } else { |
982697a4 | 4402 | return OFPERR_OFPBRC_BAD_TYPE; |
9e1fd49b BP |
4403 | } |
4404 | ||
4405 | pm->config &= pm->mask; | |
4406 | return 0; | |
4407 | } | |
4408 | ||
4409 | /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow | |
4410 | * message suitable for 'protocol', and returns that encoded form in a buffer | |
4411 | * owned by the caller. */ | |
4412 | struct ofpbuf * | |
4413 | ofputil_encode_port_mod(const struct ofputil_port_mod *pm, | |
4414 | enum ofputil_protocol protocol) | |
4415 | { | |
2e3fa633 | 4416 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); |
9e1fd49b BP |
4417 | struct ofpbuf *b; |
4418 | ||
2e3fa633 SH |
4419 | switch (ofp_version) { |
4420 | case OFP10_VERSION: { | |
9e1fd49b BP |
4421 | struct ofp10_port_mod *opm; |
4422 | ||
982697a4 BP |
4423 | b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0); |
4424 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
4e022ec0 | 4425 | opm->port_no = htons(ofp_to_u16(pm->port_no)); |
9e1fd49b BP |
4426 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); |
4427 | opm->config = htonl(pm->config & OFPPC10_ALL); | |
4428 | opm->mask = htonl(pm->mask & OFPPC10_ALL); | |
4429 | opm->advertise = netdev_port_features_to_ofp10(pm->advertise); | |
2e3fa633 SH |
4430 | break; |
4431 | } | |
4432 | ||
5fe7c919 | 4433 | case OFP11_VERSION: |
2e1ae200 JR |
4434 | case OFP12_VERSION: |
4435 | case OFP13_VERSION: { | |
9e1fd49b BP |
4436 | struct ofp11_port_mod *opm; |
4437 | ||
982697a4 BP |
4438 | b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0); |
4439 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
026a5179 | 4440 | opm->port_no = ofputil_port_to_ofp11(pm->port_no); |
9e1fd49b BP |
4441 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); |
4442 | opm->config = htonl(pm->config & OFPPC11_ALL); | |
4443 | opm->mask = htonl(pm->mask & OFPPC11_ALL); | |
4444 | opm->advertise = netdev_port_features_to_ofp11(pm->advertise); | |
2e3fa633 SH |
4445 | break; |
4446 | } | |
42dccab5 BP |
4447 | case OFP14_VERSION: |
4448 | case OFP15_VERSION: { | |
18cc69d9 BP |
4449 | struct ofp14_port_mod_prop_ethernet *eth; |
4450 | struct ofp14_port_mod *opm; | |
4451 | ||
4452 | b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth); | |
4453 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
4454 | opm->port_no = ofputil_port_to_ofp11(pm->port_no); | |
4455 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); | |
4456 | opm->config = htonl(pm->config & OFPPC11_ALL); | |
4457 | opm->mask = htonl(pm->mask & OFPPC11_ALL); | |
4458 | ||
4459 | if (pm->advertise) { | |
4460 | eth = ofpbuf_put_zeros(b, sizeof *eth); | |
4461 | eth->type = htons(OFPPMPT14_ETHERNET); | |
4462 | eth->length = htons(sizeof *eth); | |
4463 | eth->advertise = netdev_port_features_to_ofp11(pm->advertise); | |
4464 | } | |
c37c0382 | 4465 | break; |
18cc69d9 | 4466 | } |
918f2b82 | 4467 | default: |
428b2edd | 4468 | OVS_NOT_REACHED(); |
918f2b82 AZ |
4469 | } |
4470 | ||
4471 | return b; | |
4472 | } | |
72b25867 BP |
4473 | \f |
4474 | /* Table features. */ | |
918f2b82 | 4475 | |
5deff5aa | 4476 | static enum ofperr |
9a4eddbb | 4477 | pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload, |
72b25867 | 4478 | uint16_t *typep) |
5deff5aa | 4479 | { |
9a4eddbb | 4480 | enum ofperr error; |
5deff5aa | 4481 | |
9a4eddbb BP |
4482 | error = ofputil_pull_property(msg, payload, typep); |
4483 | if (payload && !error) { | |
4484 | ofpbuf_pull(payload, sizeof(struct ofp_prop_header)); | |
5deff5aa | 4485 | } |
9a4eddbb | 4486 | return error; |
5deff5aa AW |
4487 | } |
4488 | ||
4489 | static enum ofperr | |
08d1e234 BP |
4490 | parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version, |
4491 | uint64_t *ofpacts) | |
5deff5aa | 4492 | { |
08d1e234 | 4493 | uint32_t types = 0; |
5deff5aa | 4494 | |
6fd6ed71 | 4495 | while (payload->size > 0) { |
08d1e234 BP |
4496 | uint16_t type; |
4497 | enum ofperr error; | |
4498 | ||
72b25867 | 4499 | error = ofputil_pull_property__(payload, NULL, 1, &type); |
5deff5aa AW |
4500 | if (error) { |
4501 | return error; | |
4502 | } | |
08d1e234 BP |
4503 | if (type < CHAR_BIT * sizeof types) { |
4504 | types |= 1u << type; | |
5deff5aa AW |
4505 | } |
4506 | } | |
08d1e234 BP |
4507 | |
4508 | *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version); | |
5deff5aa AW |
4509 | return 0; |
4510 | } | |
4511 | ||
4512 | static enum ofperr | |
4513 | parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts) | |
4514 | { | |
4515 | *insts = 0; | |
6fd6ed71 | 4516 | while (payload->size > 0) { |
5deff5aa AW |
4517 | enum ovs_instruction_type inst; |
4518 | enum ofperr error; | |
4519 | uint16_t ofpit; | |
4520 | ||
72b25867 BP |
4521 | /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs. |
4522 | * It seems clear that they aren't padded to 8 bytes, though, because | |
4523 | * both standards say that "non-experimenter instructions are 4 bytes" | |
4524 | * and do not mention any padding before the first instruction ID. | |
4525 | * (There wouldn't be any point in padding to 8 bytes if the IDs were | |
4526 | * aligned on an odd 4-byte boundary.) | |
4527 | * | |
4528 | * Anyway, we just assume they're all glommed together on byte | |
4529 | * boundaries. */ | |
4530 | error = ofputil_pull_property__(payload, NULL, 1, &ofpit); | |
5deff5aa AW |
4531 | if (error) { |
4532 | return error; | |
4533 | } | |
4534 | ||
4535 | error = ovs_instruction_type_from_inst_type(&inst, ofpit); | |
4536 | if (!error) { | |
4537 | *insts |= 1u << inst; | |
4538 | } else if (!loose) { | |
4539 | return error; | |
4540 | } | |
4541 | } | |
4542 | return 0; | |
4543 | } | |
4544 | ||
4545 | static enum ofperr | |
4546 | parse_table_features_next_table(struct ofpbuf *payload, | |
4547 | unsigned long int *next_tables) | |
4548 | { | |
4549 | size_t i; | |
4550 | ||
4551 | memset(next_tables, 0, bitmap_n_bytes(255)); | |
6fd6ed71 PS |
4552 | for (i = 0; i < payload->size; i++) { |
4553 | uint8_t id = ((const uint8_t *) payload->data)[i]; | |
5deff5aa | 4554 | if (id >= 255) { |
9a4eddbb | 4555 | return OFPERR_OFPBPC_BAD_VALUE; |
5deff5aa AW |
4556 | } |
4557 | bitmap_set1(next_tables, id); | |
4558 | } | |
4559 | return 0; | |
4560 | } | |
4561 | ||
5deff5aa AW |
4562 | static enum ofperr |
4563 | parse_oxms(struct ofpbuf *payload, bool loose, | |
abadfcb0 | 4564 | struct mf_bitmap *exactp, struct mf_bitmap *maskedp) |
5deff5aa | 4565 | { |
abadfcb0 BP |
4566 | struct mf_bitmap exact = MF_BITMAP_INITIALIZER; |
4567 | struct mf_bitmap masked = MF_BITMAP_INITIALIZER; | |
5deff5aa | 4568 | |
6fd6ed71 | 4569 | while (payload->size > 0) { |
5deff5aa AW |
4570 | const struct mf_field *field; |
4571 | enum ofperr error; | |
4572 | bool hasmask; | |
4573 | ||
178742f9 | 4574 | error = nx_pull_header(payload, &field, &hasmask); |
5deff5aa | 4575 | if (!error) { |
abadfcb0 | 4576 | bitmap_set1(hasmask ? masked.bm : exact.bm, field->id); |
5deff5aa AW |
4577 | } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) { |
4578 | return error; | |
4579 | } | |
4580 | } | |
4581 | if (exactp) { | |
4582 | *exactp = exact; | |
abadfcb0 | 4583 | } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) { |
5deff5aa AW |
4584 | return OFPERR_OFPBMC_BAD_MASK; |
4585 | } | |
4586 | if (maskedp) { | |
4587 | *maskedp = masked; | |
abadfcb0 | 4588 | } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) { |
5deff5aa AW |
4589 | return OFPERR_OFPBMC_BAD_MASK; |
4590 | } | |
4591 | return 0; | |
4592 | } | |
4593 | ||
4594 | /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract | |
4595 | * ofputil_table_features in 'tf'. | |
4596 | * | |
4597 | * If 'loose' is true, this function ignores properties and values that it does | |
4598 | * not understand, as a controller would want to do when interpreting | |
4599 | * capabilities provided by a switch. If 'loose' is false, this function | |
4600 | * treats unknown properties and values as an error, as a switch would want to | |
4601 | * do when interpreting a configuration request made by a controller. | |
4602 | * | |
4603 | * A single OpenFlow message can specify features for multiple tables. Calling | |
4604 | * this function multiple times for a single 'msg' iterates through the tables | |
4605 | * in the message. The caller must initially leave 'msg''s layer pointers null | |
4606 | * and not modify them between calls. | |
4607 | * | |
4608 | * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise | |
4609 | * a positive "enum ofperr" value. */ | |
4610 | int | |
4611 | ofputil_decode_table_features(struct ofpbuf *msg, | |
4612 | struct ofputil_table_features *tf, bool loose) | |
4613 | { | |
08d1e234 | 4614 | const struct ofp_header *oh; |
5deff5aa | 4615 | struct ofp13_table_features *otf; |
ae665de5 | 4616 | struct ofpbuf properties; |
5deff5aa AW |
4617 | unsigned int len; |
4618 | ||
3c1bb396 BP |
4619 | memset(tf, 0, sizeof *tf); |
4620 | ||
6fd6ed71 | 4621 | if (!msg->header) { |
5deff5aa AW |
4622 | ofpraw_pull_assert(msg); |
4623 | } | |
6fd6ed71 | 4624 | oh = msg->header; |
5deff5aa | 4625 | |
6fd6ed71 | 4626 | if (!msg->size) { |
5deff5aa AW |
4627 | return EOF; |
4628 | } | |
4629 | ||
6fd6ed71 | 4630 | if (msg->size < sizeof *otf) { |
9a4eddbb | 4631 | return OFPERR_OFPBPC_BAD_LEN; |
5deff5aa AW |
4632 | } |
4633 | ||
6fd6ed71 | 4634 | otf = msg->data; |
5deff5aa | 4635 | len = ntohs(otf->length); |
6fd6ed71 | 4636 | if (len < sizeof *otf || len % 8 || len > msg->size) { |
9a4eddbb | 4637 | return OFPERR_OFPBPC_BAD_LEN; |
5deff5aa | 4638 | } |
ae665de5 BP |
4639 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len); |
4640 | ofpbuf_pull(&properties, sizeof *otf); | |
5deff5aa AW |
4641 | |
4642 | tf->table_id = otf->table_id; | |
4643 | if (tf->table_id == OFPTT_ALL) { | |
4644 | return OFPERR_OFPTFFC_BAD_TABLE; | |
4645 | } | |
4646 | ||
4647 | ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN); | |
4648 | tf->metadata_match = otf->metadata_match; | |
4649 | tf->metadata_write = otf->metadata_write; | |
82c22d34 BP |
4650 | tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT; |
4651 | if (oh->version >= OFP14_VERSION) { | |
4652 | uint32_t caps = ntohl(otf->capabilities); | |
4653 | tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0; | |
4654 | tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0; | |
4655 | } else { | |
4656 | tf->supports_eviction = -1; | |
4657 | tf->supports_vacancy_events = -1; | |
4658 | } | |
5deff5aa AW |
4659 | tf->max_entries = ntohl(otf->max_entries); |
4660 | ||
6fd6ed71 | 4661 | while (properties.size > 0) { |
5deff5aa AW |
4662 | struct ofpbuf payload; |
4663 | enum ofperr error; | |
4664 | uint16_t type; | |
4665 | ||
ae665de5 | 4666 | error = pull_table_feature_property(&properties, &payload, &type); |
5deff5aa AW |
4667 | if (error) { |
4668 | return error; | |
4669 | } | |
4670 | ||
4671 | switch ((enum ofp13_table_feature_prop_type) type) { | |
4672 | case OFPTFPT13_INSTRUCTIONS: | |
4673 | error = parse_instruction_ids(&payload, loose, | |
4674 | &tf->nonmiss.instructions); | |
4675 | break; | |
4676 | case OFPTFPT13_INSTRUCTIONS_MISS: | |
4677 | error = parse_instruction_ids(&payload, loose, | |
4678 | &tf->miss.instructions); | |
4679 | break; | |
4680 | ||
4681 | case OFPTFPT13_NEXT_TABLES: | |
4682 | error = parse_table_features_next_table(&payload, | |
4683 | tf->nonmiss.next); | |
4684 | break; | |
4685 | case OFPTFPT13_NEXT_TABLES_MISS: | |
4686 | error = parse_table_features_next_table(&payload, tf->miss.next); | |
4687 | break; | |
4688 | ||
4689 | case OFPTFPT13_WRITE_ACTIONS: | |
08d1e234 BP |
4690 | error = parse_action_bitmap(&payload, oh->version, |
4691 | &tf->nonmiss.write.ofpacts); | |
5deff5aa AW |
4692 | break; |
4693 | case OFPTFPT13_WRITE_ACTIONS_MISS: | |
08d1e234 BP |
4694 | error = parse_action_bitmap(&payload, oh->version, |
4695 | &tf->miss.write.ofpacts); | |
5deff5aa AW |
4696 | break; |
4697 | ||
4698 | case OFPTFPT13_APPLY_ACTIONS: | |
08d1e234 BP |
4699 | error = parse_action_bitmap(&payload, oh->version, |
4700 | &tf->nonmiss.apply.ofpacts); | |
5deff5aa AW |
4701 | break; |
4702 | case OFPTFPT13_APPLY_ACTIONS_MISS: | |
08d1e234 BP |
4703 | error = parse_action_bitmap(&payload, oh->version, |
4704 | &tf->miss.apply.ofpacts); | |
5deff5aa AW |
4705 | break; |
4706 | ||
4707 | case OFPTFPT13_MATCH: | |
4708 | error = parse_oxms(&payload, loose, &tf->match, &tf->mask); | |
4709 | break; | |
4710 | case OFPTFPT13_WILDCARDS: | |
4711 | error = parse_oxms(&payload, loose, &tf->wildcard, NULL); | |
4712 | break; | |
4713 | ||
4714 | case OFPTFPT13_WRITE_SETFIELD: | |
4715 | error = parse_oxms(&payload, loose, | |
4716 | &tf->nonmiss.write.set_fields, NULL); | |
4717 | break; | |
4718 | case OFPTFPT13_WRITE_SETFIELD_MISS: | |
4719 | error = parse_oxms(&payload, loose, | |
4720 | &tf->miss.write.set_fields, NULL); | |
4721 | break; | |
4722 | case OFPTFPT13_APPLY_SETFIELD: | |
4723 | error = parse_oxms(&payload, loose, | |
4724 | &tf->nonmiss.apply.set_fields, NULL); | |
4725 | break; | |
4726 | case OFPTFPT13_APPLY_SETFIELD_MISS: | |
4727 | error = parse_oxms(&payload, loose, | |
4728 | &tf->miss.apply.set_fields, NULL); | |
4729 | break; | |
4730 | ||
4731 | case OFPTFPT13_EXPERIMENTER: | |
4732 | case OFPTFPT13_EXPERIMENTER_MISS: | |
9a4eddbb BP |
4733 | default: |
4734 | log_property(loose, "unknown table features property %"PRIu16, | |
4735 | type); | |
4736 | error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE; | |
5deff5aa AW |
4737 | break; |
4738 | } | |
4739 | if (error) { | |
4740 | return error; | |
4741 | } | |
4742 | } | |
4743 | ||
4744 | /* Fix inconsistencies: | |
4745 | * | |
e7227821 BP |
4746 | * - Turn on 'match' bits that are set in 'mask', because maskable |
4747 | * fields are matchable. | |
5deff5aa AW |
4748 | * |
4749 | * - Turn on 'wildcard' bits that are set in 'mask', because a field | |
e7227821 BP |
4750 | * that is arbitrarily maskable can be wildcarded entirely. |
4751 | * | |
4752 | * - Turn off 'wildcard' bits that are not in 'match', because a field | |
4753 | * must be matchable for it to be meaningfully wildcarded. */ | |
4754 | bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS); | |
abadfcb0 | 4755 | bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS); |
e7227821 | 4756 | bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS); |
5deff5aa AW |
4757 | |
4758 | return 0; | |
4759 | } | |
4760 | ||
4761 | /* Encodes and returns a request to obtain the table features of a switch. | |
4762 | * The message is encoded for OpenFlow version 'ofp_version'. */ | |
4763 | struct ofpbuf * | |
4764 | ofputil_encode_table_features_request(enum ofp_version ofp_version) | |
4765 | { | |
4766 | struct ofpbuf *request = NULL; | |
4767 | ||
4768 | switch (ofp_version) { | |
4769 | case OFP10_VERSION: | |
4770 | case OFP11_VERSION: | |
4771 | case OFP12_VERSION: | |
4772 | ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later " | |
4773 | "(\'-O OpenFlow13\')"); | |
4774 | case OFP13_VERSION: | |
4775 | case OFP14_VERSION: | |
42dccab5 | 4776 | case OFP15_VERSION: |
5deff5aa AW |
4777 | request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST, |
4778 | ofp_version, 0); | |
4779 | break; | |
4780 | default: | |
4781 | OVS_NOT_REACHED(); | |
4782 | } | |
4783 | ||
4784 | return request; | |
4785 | } | |
4786 | ||
3c4e10fb BP |
4787 | static void |
4788 | put_fields_property(struct ofpbuf *reply, | |
4789 | const struct mf_bitmap *fields, | |
4790 | const struct mf_bitmap *masks, | |
4791 | enum ofp13_table_feature_prop_type property, | |
4792 | enum ofp_version version) | |
4793 | { | |
4794 | size_t start_ofs; | |
4795 | int field; | |
4796 | ||
4797 | start_ofs = start_property(reply, property); | |
4798 | BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) { | |
178742f9 BP |
4799 | nx_put_header(reply, field, version, |
4800 | masks && bitmap_is_set(masks->bm, field)); | |
3c4e10fb BP |
4801 | } |
4802 | end_property(reply, start_ofs); | |
4803 | } | |
4804 | ||
4805 | static void | |
4806 | put_table_action_features(struct ofpbuf *reply, | |
4807 | const struct ofputil_table_action_features *taf, | |
4808 | enum ofp13_table_feature_prop_type actions_type, | |
4809 | enum ofp13_table_feature_prop_type set_fields_type, | |
4810 | int miss_offset, enum ofp_version version) | |
4811 | { | |
4812 | size_t start_ofs; | |
4813 | ||
4814 | start_ofs = start_property(reply, actions_type + miss_offset); | |
4815 | put_bitmap_properties(reply, | |
4816 | ntohl(ofpact_bitmap_to_openflow(taf->ofpacts, | |
4817 | version))); | |
4818 | end_property(reply, start_ofs); | |
4819 | ||
4820 | put_fields_property(reply, &taf->set_fields, NULL, | |
4821 | set_fields_type + miss_offset, version); | |
4822 | } | |
4823 | ||
4824 | static void | |
4825 | put_table_instruction_features( | |
4826 | struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif, | |
4827 | int miss_offset, enum ofp_version version) | |
4828 | { | |
4829 | size_t start_ofs; | |
4830 | uint8_t table_id; | |
4831 | ||
4832 | start_ofs = start_property(reply, OFPTFPT13_INSTRUCTIONS + miss_offset); | |
4833 | put_bitmap_properties(reply, | |
4834 | ntohl(ovsinst_bitmap_to_openflow(tif->instructions, | |
4835 | version))); | |
4836 | end_property(reply, start_ofs); | |
4837 | ||
4838 | start_ofs = start_property(reply, OFPTFPT13_NEXT_TABLES + miss_offset); | |
4839 | BITMAP_FOR_EACH_1 (table_id, 255, tif->next) { | |
4840 | ofpbuf_put(reply, &table_id, 1); | |
4841 | } | |
4842 | end_property(reply, start_ofs); | |
4843 | ||
4844 | put_table_action_features(reply, &tif->write, | |
4845 | OFPTFPT13_WRITE_ACTIONS, | |
4846 | OFPTFPT13_WRITE_SETFIELD, miss_offset, version); | |
4847 | put_table_action_features(reply, &tif->apply, | |
4848 | OFPTFPT13_APPLY_ACTIONS, | |
4849 | OFPTFPT13_APPLY_SETFIELD, miss_offset, version); | |
4850 | } | |
4851 | ||
4852 | void | |
4853 | ofputil_append_table_features_reply(const struct ofputil_table_features *tf, | |
ca6ba700 | 4854 | struct ovs_list *replies) |
3c4e10fb BP |
4855 | { |
4856 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); | |
4857 | enum ofp_version version = ofpmp_version(replies); | |
6fd6ed71 | 4858 | size_t start_ofs = reply->size; |
3c4e10fb BP |
4859 | struct ofp13_table_features *otf; |
4860 | ||
4861 | otf = ofpbuf_put_zeros(reply, sizeof *otf); | |
4862 | otf->table_id = tf->table_id; | |
4863 | ovs_strlcpy(otf->name, tf->name, sizeof otf->name); | |
4864 | otf->metadata_match = tf->metadata_match; | |
4865 | otf->metadata_write = tf->metadata_write; | |
82c22d34 BP |
4866 | if (version >= OFP14_VERSION) { |
4867 | if (tf->supports_eviction) { | |
4868 | otf->capabilities |= htonl(OFPTC14_EVICTION); | |
4869 | } | |
4870 | if (tf->supports_vacancy_events) { | |
4871 | otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS); | |
4872 | } | |
4873 | } | |
3c4e10fb BP |
4874 | otf->max_entries = htonl(tf->max_entries); |
4875 | ||
4876 | put_table_instruction_features(reply, &tf->nonmiss, 0, version); | |
4877 | put_table_instruction_features(reply, &tf->miss, 1, version); | |
4878 | ||
4879 | put_fields_property(reply, &tf->match, &tf->mask, | |
4880 | OFPTFPT13_MATCH, version); | |
4881 | put_fields_property(reply, &tf->wildcard, NULL, | |
4882 | OFPTFPT13_WILDCARDS, version); | |
4883 | ||
4884 | otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf); | |
6fd6ed71 | 4885 | otf->length = htons(reply->size - start_ofs); |
3c4e10fb BP |
4886 | ofpmp_postappend(replies, start_ofs); |
4887 | } | |
4888 | ||
03c72922 BP |
4889 | static enum ofperr |
4890 | parse_table_desc_eviction_property(struct ofpbuf *property, | |
4891 | struct ofputil_table_desc *td) | |
4892 | { | |
4893 | struct ofp14_table_mod_prop_eviction *ote = property->data; | |
4894 | ||
4895 | if (property->size != sizeof *ote) { | |
4896 | return OFPERR_OFPBPC_BAD_LEN; | |
4897 | } | |
4898 | ||
4899 | td->eviction_flags = ntohl(ote->flags); | |
4900 | return 0; | |
4901 | } | |
4902 | ||
4903 | /* Decodes the next OpenFlow "table desc" message (of possibly several) from | |
4904 | * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the | |
4905 | * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_* | |
4906 | * value. */ | |
4907 | int | |
4908 | ofputil_decode_table_desc(struct ofpbuf *msg, | |
4909 | struct ofputil_table_desc *td, | |
4910 | enum ofp_version version) | |
4911 | { | |
4912 | struct ofp14_table_desc *otd; | |
4913 | struct ofpbuf properties; | |
4914 | size_t length; | |
4915 | ||
4916 | memset(td, 0, sizeof *td); | |
4917 | ||
4918 | if (!msg->header) { | |
4919 | ofpraw_pull_assert(msg); | |
4920 | } | |
4921 | ||
4922 | if (!msg->size) { | |
4923 | return EOF; | |
4924 | } | |
4925 | ||
4926 | otd = ofpbuf_try_pull(msg, sizeof *otd); | |
4927 | if (!otd) { | |
4928 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" " | |
4929 | "leftover bytes at end", msg->size); | |
4930 | return OFPERR_OFPBRC_BAD_LEN; | |
4931 | } | |
4932 | ||
4933 | td->table_id = otd->table_id; | |
4934 | length = ntohs(otd->length); | |
4935 | if (length < sizeof *otd || length - sizeof *otd > msg->size) { | |
4936 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid " | |
4937 | "length %"PRIuSIZE, length); | |
4938 | return OFPERR_OFPBRC_BAD_LEN; | |
4939 | } | |
4940 | length -= sizeof *otd; | |
4941 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, length), length); | |
4942 | ||
4943 | td->eviction = ofputil_decode_table_eviction(otd->config, version); | |
4944 | td->eviction_flags = UINT32_MAX; | |
4945 | ||
4946 | while (properties.size > 0) { | |
4947 | struct ofpbuf payload; | |
4948 | enum ofperr error; | |
4949 | uint16_t type; | |
4950 | ||
4951 | error = ofputil_pull_property(&properties, &payload, &type); | |
4952 | if (error) { | |
4953 | return error; | |
4954 | } | |
4955 | ||
4956 | switch (type) { | |
4957 | case OFPTMPT14_EVICTION: | |
4958 | error = parse_table_desc_eviction_property(&payload, td); | |
4959 | break; | |
4960 | ||
4961 | default: | |
4962 | log_property(true, "unknown table_desc property %"PRIu16, type); | |
4963 | error = 0; | |
4964 | break; | |
4965 | } | |
4966 | ||
4967 | if (error) { | |
4968 | return error; | |
4969 | } | |
4970 | } | |
4971 | ||
4972 | return 0; | |
4973 | } | |
4974 | ||
4975 | /* Encodes and returns a request to obtain description of tables of a switch. | |
4976 | * The message is encoded for OpenFlow version 'ofp_version'. */ | |
4977 | struct ofpbuf * | |
4978 | ofputil_encode_table_desc_request(enum ofp_version ofp_version) | |
4979 | { | |
4980 | struct ofpbuf *request = NULL; | |
4981 | ||
4982 | if (ofp_version >= OFP14_VERSION) { | |
4983 | request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST, | |
4984 | ofp_version, 0); | |
4985 | } else { | |
4986 | ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later " | |
4987 | "(\'-O OpenFlow14\')"); | |
4988 | } | |
4989 | ||
4990 | return request; | |
4991 | } | |
4992 | ||
4993 | /* Function to append Table desc information in a reply list. */ | |
4994 | void | |
4995 | ofputil_append_table_desc_reply(const struct ofputil_table_desc *td, | |
4996 | struct ovs_list *replies, | |
4997 | enum ofp_version version) | |
4998 | { | |
4999 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); | |
5000 | size_t start_otd; | |
5001 | struct ofp14_table_desc *otd; | |
5002 | ||
5003 | start_otd = reply->size; | |
5004 | ofpbuf_put_zeros(reply, sizeof *otd); | |
5005 | if (td->eviction_flags != UINT32_MAX) { | |
5006 | struct ofp14_table_mod_prop_eviction *ote; | |
5007 | ||
5008 | ote = ofpbuf_put_zeros(reply, sizeof *ote); | |
5009 | ote->type = htons(OFPTMPT14_EVICTION); | |
5010 | ote->length = htons(sizeof *ote); | |
5011 | ote->flags = htonl(td->eviction_flags); | |
5012 | } | |
5013 | ||
5014 | otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd); | |
5015 | otd->length = htons(reply->size - start_otd); | |
5016 | otd->table_id = td->table_id; | |
5017 | otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT, | |
5018 | td->eviction, version); | |
5019 | ofpmp_postappend(replies, start_otd); | |
5020 | } | |
5021 | ||
82c22d34 BP |
5022 | static enum ofperr |
5023 | parse_table_mod_eviction_property(struct ofpbuf *property, | |
5024 | struct ofputil_table_mod *tm) | |
5025 | { | |
5026 | struct ofp14_table_mod_prop_eviction *ote = property->data; | |
5027 | ||
5028 | if (property->size != sizeof *ote) { | |
5029 | return OFPERR_OFPBPC_BAD_LEN; | |
5030 | } | |
5031 | ||
5032 | tm->eviction_flags = ntohl(ote->flags); | |
5033 | return 0; | |
5034 | } | |
918f2b82 | 5035 | |
3c1bb396 BP |
5036 | /* Given 'config', taken from an OpenFlow 'version' message that specifies |
5037 | * table configuration (a table mod, table stats, or table features message), | |
82c22d34 BP |
5038 | * returns the table eviction configuration that it specifies. |
5039 | * | |
5040 | * Only OpenFlow 1.4 and later specify table eviction configuration this way, | |
5041 | * so for other 'version' values this function always returns | |
5042 | * OFPUTIL_TABLE_EVICTION_DEFAULT. */ | |
5043 | static enum ofputil_table_eviction | |
5044 | ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version) | |
5045 | { | |
5046 | return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT | |
5047 | : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON | |
5048 | : OFPUTIL_TABLE_EVICTION_OFF); | |
5049 | } | |
5050 | ||
5051 | /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various | |
5052 | * OpenFlow messages of the given 'version', based on the provided 'miss' and | |
5053 | * 'eviction' values. */ | |
5054 | static ovs_be32 | |
5055 | ofputil_encode_table_config(enum ofputil_table_miss miss, | |
5056 | enum ofputil_table_eviction eviction, | |
5057 | enum ofp_version version) | |
5058 | { | |
5059 | /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more | |
5060 | * information on the crazy evolution of this field. */ | |
5061 | switch (version) { | |
5062 | case OFP10_VERSION: | |
5063 | /* OpenFlow 1.0 didn't have such a field, any value ought to do. */ | |
5064 | return htonl(0); | |
5065 | ||
5066 | case OFP11_VERSION: | |
5067 | case OFP12_VERSION: | |
5068 | /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */ | |
5069 | switch (miss) { | |
5070 | case OFPUTIL_TABLE_MISS_DEFAULT: | |
5071 | /* Really this shouldn't be used for encoding (the caller should | |
5072 | * provide a specific value) but I can't imagine that defaulting to | |
5073 | * the fall-through case here will hurt. */ | |
5074 | case OFPUTIL_TABLE_MISS_CONTROLLER: | |
5075 | default: | |
5076 | return htonl(OFPTC11_TABLE_MISS_CONTROLLER); | |
5077 | case OFPUTIL_TABLE_MISS_CONTINUE: | |
5078 | return htonl(OFPTC11_TABLE_MISS_CONTINUE); | |
5079 | case OFPUTIL_TABLE_MISS_DROP: | |
5080 | return htonl(OFPTC11_TABLE_MISS_DROP); | |
5081 | } | |
5082 | OVS_NOT_REACHED(); | |
5083 | ||
5084 | case OFP13_VERSION: | |
5085 | /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new | |
5086 | * flags, so this is correct. */ | |
5087 | return htonl(0); | |
5088 | ||
5089 | case OFP14_VERSION: | |
5090 | case OFP15_VERSION: | |
5091 | /* OpenFlow 1.4 introduced OFPTC14_EVICTION and OFPTC14_VACANCY_EVENTS | |
5092 | * and we don't support the latter yet. */ | |
5093 | return htonl(eviction == OFPUTIL_TABLE_EVICTION_ON | |
5094 | ? OFPTC14_EVICTION : 0); | |
5095 | } | |
5096 | ||
5097 | OVS_NOT_REACHED(); | |
5098 | } | |
5099 | ||
5100 | /* Given 'config', taken from an OpenFlow 'version' message that specifies | |
5101 | * table configuration (a table mod, table stats, or table features message), | |
5102 | * returns the table miss configuration that it specifies. | |
5103 | * | |
5104 | * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for | |
5105 | * other 'version' values this function always returns | |
5106 | * OFPUTIL_TABLE_MISS_DEFAULT. */ | |
3c1bb396 | 5107 | static enum ofputil_table_miss |
82c22d34 | 5108 | ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version) |
3c1bb396 BP |
5109 | { |
5110 | uint32_t config = ntohl(config_); | |
5111 | ||
82c22d34 | 5112 | if (version == OFP11_VERSION || version == OFP12_VERSION) { |
3c1bb396 BP |
5113 | switch (config & OFPTC11_TABLE_MISS_MASK) { |
5114 | case OFPTC11_TABLE_MISS_CONTROLLER: | |
5115 | return OFPUTIL_TABLE_MISS_CONTROLLER; | |
5116 | ||
5117 | case OFPTC11_TABLE_MISS_CONTINUE: | |
5118 | return OFPUTIL_TABLE_MISS_CONTINUE; | |
5119 | ||
5120 | case OFPTC11_TABLE_MISS_DROP: | |
5121 | return OFPUTIL_TABLE_MISS_DROP; | |
5122 | ||
5123 | default: | |
5124 | VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config); | |
5125 | return OFPUTIL_TABLE_MISS_CONTROLLER; | |
5126 | } | |
5127 | } else { | |
5128 | return OFPUTIL_TABLE_MISS_DEFAULT; | |
5129 | } | |
5130 | } | |
5131 | ||
918f2b82 AZ |
5132 | /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in |
5133 | * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
5134 | enum ofperr | |
5135 | ofputil_decode_table_mod(const struct ofp_header *oh, | |
5136 | struct ofputil_table_mod *pm) | |
5137 | { | |
5138 | enum ofpraw raw; | |
5139 | struct ofpbuf b; | |
5140 | ||
82c22d34 BP |
5141 | memset(pm, 0, sizeof *pm); |
5142 | pm->miss = OFPUTIL_TABLE_MISS_DEFAULT; | |
5143 | pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT; | |
5144 | pm->eviction_flags = UINT32_MAX; | |
918f2b82 AZ |
5145 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
5146 | raw = ofpraw_pull_assert(&b); | |
5147 | ||
5148 | if (raw == OFPRAW_OFPT11_TABLE_MOD) { | |
6fd6ed71 | 5149 | const struct ofp11_table_mod *otm = b.data; |
918f2b82 AZ |
5150 | |
5151 | pm->table_id = otm->table_id; | |
82c22d34 | 5152 | pm->miss = ofputil_decode_table_miss(otm->config, oh->version); |
37ab26e8 BP |
5153 | } else if (raw == OFPRAW_OFPT14_TABLE_MOD) { |
5154 | const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm); | |
5155 | ||
5156 | pm->table_id = otm->table_id; | |
82c22d34 BP |
5157 | pm->miss = ofputil_decode_table_miss(otm->config, oh->version); |
5158 | pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version); | |
5159 | while (b.size > 0) { | |
5160 | struct ofpbuf property; | |
5161 | enum ofperr error; | |
5162 | uint16_t type; | |
5163 | ||
5164 | error = ofputil_pull_property(&b, &property, &type); | |
5165 | if (error) { | |
5166 | return error; | |
5167 | } | |
5168 | ||
5169 | switch (type) { | |
5170 | case OFPTMPT14_EVICTION: | |
5171 | error = parse_table_mod_eviction_property(&property, pm); | |
5172 | break; | |
5173 | ||
5174 | default: | |
5175 | error = OFPERR_OFPBRC_BAD_TYPE; | |
5176 | break; | |
5177 | } | |
5178 | ||
5179 | if (error) { | |
5180 | return error; | |
5181 | } | |
5182 | } | |
918f2b82 AZ |
5183 | } else { |
5184 | return OFPERR_OFPBRC_BAD_TYPE; | |
5185 | } | |
5186 | ||
5187 | return 0; | |
5188 | } | |
5189 | ||
82c22d34 BP |
5190 | /* Converts the abstract form of a "table mod" message in '*tm' into an |
5191 | * OpenFlow message suitable for 'protocol', and returns that encoded form in a | |
5192 | * buffer owned by the caller. */ | |
918f2b82 | 5193 | struct ofpbuf * |
82c22d34 | 5194 | ofputil_encode_table_mod(const struct ofputil_table_mod *tm, |
918f2b82 AZ |
5195 | enum ofputil_protocol protocol) |
5196 | { | |
5197 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); | |
5198 | struct ofpbuf *b; | |
5199 | ||
5200 | switch (ofp_version) { | |
5201 | case OFP10_VERSION: { | |
5202 | ovs_fatal(0, "table mod needs OpenFlow 1.1 or later " | |
5203 | "(\'-O OpenFlow11\')"); | |
5204 | break; | |
5205 | } | |
5206 | case OFP11_VERSION: | |
5207 | case OFP12_VERSION: | |
5208 | case OFP13_VERSION: { | |
5209 | struct ofp11_table_mod *otm; | |
2e3fa633 | 5210 | |
918f2b82 AZ |
5211 | b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0); |
5212 | otm = ofpbuf_put_zeros(b, sizeof *otm); | |
82c22d34 BP |
5213 | otm->table_id = tm->table_id; |
5214 | otm->config = ofputil_encode_table_config(tm->miss, tm->eviction, | |
5215 | ofp_version); | |
918f2b82 AZ |
5216 | break; |
5217 | } | |
42dccab5 BP |
5218 | case OFP14_VERSION: |
5219 | case OFP15_VERSION: { | |
37ab26e8 | 5220 | struct ofp14_table_mod *otm; |
82c22d34 | 5221 | struct ofp14_table_mod_prop_eviction *ote; |
37ab26e8 BP |
5222 | |
5223 | b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0); | |
5224 | otm = ofpbuf_put_zeros(b, sizeof *otm); | |
82c22d34 BP |
5225 | otm->table_id = tm->table_id; |
5226 | otm->config = ofputil_encode_table_config(tm->miss, tm->eviction, | |
5227 | ofp_version); | |
5228 | ||
5229 | if (tm->eviction_flags != UINT32_MAX) { | |
5230 | ote = ofpbuf_put_zeros(b, sizeof *ote); | |
5231 | ote->type = htons(OFPTMPT14_EVICTION); | |
5232 | ote->length = htons(sizeof *ote); | |
5233 | ote->flags = htonl(tm->eviction_flags); | |
5234 | } | |
c37c0382 | 5235 | break; |
37ab26e8 | 5236 | } |
2e3fa633 | 5237 | default: |
428b2edd | 5238 | OVS_NOT_REACHED(); |
9e1fd49b BP |
5239 | } |
5240 | ||
5241 | return b; | |
5242 | } | |
2b07c8b1 | 5243 | \f |
6ea4776b JR |
5244 | /* ofputil_role_request */ |
5245 | ||
5246 | /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into | |
5247 | * an abstract form in '*rr'. Returns 0 if successful, otherwise an | |
5248 | * OFPERR_* value. */ | |
5249 | enum ofperr | |
5250 | ofputil_decode_role_message(const struct ofp_header *oh, | |
5251 | struct ofputil_role_request *rr) | |
5252 | { | |
6ea4776b JR |
5253 | struct ofpbuf b; |
5254 | enum ofpraw raw; | |
5255 | ||
6ea4776b JR |
5256 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
5257 | raw = ofpraw_pull_assert(&b); | |
5258 | ||
f4f1ea7e BP |
5259 | if (raw == OFPRAW_OFPT12_ROLE_REQUEST || |
5260 | raw == OFPRAW_OFPT12_ROLE_REPLY) { | |
6fd6ed71 | 5261 | const struct ofp12_role_request *orr = b.msg; |
6ea4776b | 5262 | |
f4f1ea7e BP |
5263 | if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) && |
5264 | orr->role != htonl(OFPCR12_ROLE_EQUAL) && | |
5265 | orr->role != htonl(OFPCR12_ROLE_MASTER) && | |
5266 | orr->role != htonl(OFPCR12_ROLE_SLAVE)) { | |
5267 | return OFPERR_OFPRRFC_BAD_ROLE; | |
6ea4776b JR |
5268 | } |
5269 | ||
f4f1ea7e | 5270 | rr->role = ntohl(orr->role); |
147cc9d3 BP |
5271 | if (raw == OFPRAW_OFPT12_ROLE_REQUEST |
5272 | ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE) | |
b8266395 | 5273 | : orr->generation_id == OVS_BE64_MAX) { |
f4f1ea7e BP |
5274 | rr->have_generation_id = false; |
5275 | rr->generation_id = 0; | |
5276 | } else { | |
5277 | rr->have_generation_id = true; | |
5278 | rr->generation_id = ntohll(orr->generation_id); | |
5279 | } | |
5280 | } else if (raw == OFPRAW_NXT_ROLE_REQUEST || | |
5281 | raw == OFPRAW_NXT_ROLE_REPLY) { | |
6fd6ed71 | 5282 | const struct nx_role_request *nrr = b.msg; |
f4f1ea7e BP |
5283 | |
5284 | BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL); | |
5285 | BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER); | |
5286 | BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE); | |
5287 | ||
5288 | if (nrr->role != htonl(NX_ROLE_OTHER) && | |
5289 | nrr->role != htonl(NX_ROLE_MASTER) && | |
5290 | nrr->role != htonl(NX_ROLE_SLAVE)) { | |
5291 | return OFPERR_OFPRRFC_BAD_ROLE; | |
5292 | } | |
6ea4776b | 5293 | |
f4f1ea7e BP |
5294 | rr->role = ntohl(nrr->role) + 1; |
5295 | rr->have_generation_id = false; | |
5296 | rr->generation_id = 0; | |
5297 | } else { | |
428b2edd | 5298 | OVS_NOT_REACHED(); |
6ea4776b JR |
5299 | } |
5300 | ||
6ea4776b JR |
5301 | return 0; |
5302 | } | |
5303 | ||
5304 | /* Returns an encoded form of a role reply suitable for the "request" in a | |
5305 | * buffer owned by the caller. */ | |
5306 | struct ofpbuf * | |
5307 | ofputil_encode_role_reply(const struct ofp_header *request, | |
f4f1ea7e | 5308 | const struct ofputil_role_request *rr) |
6ea4776b | 5309 | { |
6ea4776b | 5310 | struct ofpbuf *buf; |
6ea4776b JR |
5311 | enum ofpraw raw; |
5312 | ||
f4f1ea7e | 5313 | raw = ofpraw_decode_assert(request); |
6ea4776b | 5314 | if (raw == OFPRAW_OFPT12_ROLE_REQUEST) { |
f4f1ea7e | 5315 | struct ofp12_role_request *orr; |
6ea4776b | 5316 | |
f4f1ea7e BP |
5317 | buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0); |
5318 | orr = ofpbuf_put_zeros(buf, sizeof *orr); | |
6ea4776b | 5319 | |
147cc9d3 BP |
5320 | orr->role = htonl(rr->role); |
5321 | orr->generation_id = htonll(rr->have_generation_id | |
5322 | ? rr->generation_id | |
5323 | : UINT64_MAX); | |
f4f1ea7e BP |
5324 | } else if (raw == OFPRAW_NXT_ROLE_REQUEST) { |
5325 | struct nx_role_request *nrr; | |
5326 | ||
5327 | BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1); | |
5328 | BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1); | |
5329 | BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1); | |
5330 | ||
5331 | buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0); | |
5332 | nrr = ofpbuf_put_zeros(buf, sizeof *nrr); | |
5333 | nrr->role = htonl(rr->role - 1); | |
5334 | } else { | |
428b2edd | 5335 | OVS_NOT_REACHED(); |
6ea4776b | 5336 | } |
6ea4776b JR |
5337 | |
5338 | return buf; | |
5339 | } | |
5340 | \f | |
6751a4b4 BP |
5341 | /* Encodes "role status" message 'status' for sending in the given |
5342 | * 'protocol'. Returns the role status message, if 'protocol' supports them, | |
5343 | * otherwise a null pointer. */ | |
00467f73 AC |
5344 | struct ofpbuf * |
5345 | ofputil_encode_role_status(const struct ofputil_role_status *status, | |
5346 | enum ofputil_protocol protocol) | |
5347 | { | |
00467f73 | 5348 | enum ofp_version version; |
00467f73 AC |
5349 | |
5350 | version = ofputil_protocol_to_ofp_version(protocol); | |
6751a4b4 BP |
5351 | if (version >= OFP14_VERSION) { |
5352 | struct ofp14_role_status *rstatus; | |
5353 | struct ofpbuf *buf; | |
5354 | ||
5355 | buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0), | |
5356 | 0); | |
5357 | rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus); | |
5358 | rstatus->role = htonl(status->role); | |
5359 | rstatus->reason = status->reason; | |
5360 | rstatus->generation_id = htonll(status->generation_id); | |
5361 | ||
5362 | return buf; | |
5363 | } else { | |
5364 | return NULL; | |
5365 | } | |
00467f73 AC |
5366 | } |
5367 | ||
5368 | enum ofperr | |
5369 | ofputil_decode_role_status(const struct ofp_header *oh, | |
5370 | struct ofputil_role_status *rs) | |
5371 | { | |
5372 | struct ofpbuf b; | |
5373 | enum ofpraw raw; | |
5374 | const struct ofp14_role_status *r; | |
5375 | ||
5376 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
5377 | raw = ofpraw_pull_assert(&b); | |
5378 | ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS); | |
5379 | ||
6fd6ed71 | 5380 | r = b.msg; |
00467f73 AC |
5381 | if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) && |
5382 | r->role != htonl(OFPCR12_ROLE_EQUAL) && | |
5383 | r->role != htonl(OFPCR12_ROLE_MASTER) && | |
5384 | r->role != htonl(OFPCR12_ROLE_SLAVE)) { | |
5385 | return OFPERR_OFPRRFC_BAD_ROLE; | |
5386 | } | |
5387 | ||
5388 | rs->role = ntohl(r->role); | |
5389 | rs->generation_id = ntohll(r->generation_id); | |
5390 | rs->reason = r->reason; | |
5391 | ||
5392 | return 0; | |
5393 | } | |
5394 | ||
307975da SH |
5395 | /* Table stats. */ |
5396 | ||
3c1bb396 BP |
5397 | /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be |
5398 | * matched and a field that must be wildcarded. This function returns a bitmap | |
5399 | * that contains both kinds of fields. */ | |
5400 | static struct mf_bitmap | |
5401 | wild_or_nonmatchable_fields(const struct ofputil_table_features *features) | |
5402 | { | |
5403 | struct mf_bitmap wc = features->match; | |
5404 | bitmap_not(wc.bm, MFF_N_IDS); | |
5405 | bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS); | |
5406 | return wc; | |
5407 | } | |
5408 | ||
5409 | struct ofp10_wc_map { | |
5410 | enum ofp10_flow_wildcards wc10; | |
5411 | enum mf_field_id mf; | |
5412 | }; | |
5413 | ||
5414 | static const struct ofp10_wc_map ofp10_wc_map[] = { | |
5415 | { OFPFW10_IN_PORT, MFF_IN_PORT }, | |
5416 | { OFPFW10_DL_VLAN, MFF_VLAN_VID }, | |
5417 | { OFPFW10_DL_SRC, MFF_ETH_SRC }, | |
5418 | { OFPFW10_DL_DST, MFF_ETH_DST}, | |
5419 | { OFPFW10_DL_TYPE, MFF_ETH_TYPE }, | |
5420 | { OFPFW10_NW_PROTO, MFF_IP_PROTO }, | |
5421 | { OFPFW10_TP_SRC, MFF_TCP_SRC }, | |
5422 | { OFPFW10_TP_DST, MFF_TCP_DST }, | |
5423 | { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC }, | |
5424 | { OFPFW10_NW_DST_MASK, MFF_IPV4_DST }, | |
5425 | { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP }, | |
5426 | { OFPFW10_NW_TOS, MFF_IP_DSCP }, | |
5427 | }; | |
5428 | ||
5429 | static ovs_be32 | |
5430 | mf_bitmap_to_of10(const struct mf_bitmap *fields) | |
5431 | { | |
5432 | const struct ofp10_wc_map *p; | |
5433 | uint32_t wc10 = 0; | |
5434 | ||
5435 | for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) { | |
5436 | if (bitmap_is_set(fields->bm, p->mf)) { | |
5437 | wc10 |= p->wc10; | |
5438 | } | |
5439 | } | |
5440 | return htonl(wc10); | |
5441 | } | |
5442 | ||
5443 | static struct mf_bitmap | |
5444 | mf_bitmap_from_of10(ovs_be32 wc10_) | |
5445 | { | |
5446 | struct mf_bitmap fields = MF_BITMAP_INITIALIZER; | |
5447 | const struct ofp10_wc_map *p; | |
5448 | uint32_t wc10 = ntohl(wc10_); | |
5449 | ||
5450 | for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) { | |
5451 | if (wc10 & p->wc10) { | |
5452 | bitmap_set1(fields.bm, p->mf); | |
5453 | } | |
5454 | } | |
5455 | return fields; | |
5456 | } | |
5457 | ||
307975da | 5458 | static void |
3c1bb396 BP |
5459 | ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats, |
5460 | const struct ofputil_table_features *features, | |
307975da SH |
5461 | struct ofpbuf *buf) |
5462 | { | |
3c1bb396 | 5463 | struct mf_bitmap wc = wild_or_nonmatchable_fields(features); |
307975da | 5464 | struct ofp10_table_stats *out; |
307975da | 5465 | |
3bdc692b | 5466 | out = ofpbuf_put_zeros(buf, sizeof *out); |
3c1bb396 BP |
5467 | out->table_id = features->table_id; |
5468 | ovs_strlcpy(out->name, features->name, sizeof out->name); | |
5469 | out->wildcards = mf_bitmap_to_of10(&wc); | |
5470 | out->max_entries = htonl(features->max_entries); | |
5471 | out->active_count = htonl(stats->active_count); | |
5472 | put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count)); | |
5473 | put_32aligned_be64(&out->matched_count, htonll(stats->matched_count)); | |
5474 | } | |
5475 | ||
5476 | struct ofp11_wc_map { | |
5477 | enum ofp11_flow_match_fields wc11; | |
5478 | enum mf_field_id mf; | |
5479 | }; | |
5480 | ||
5481 | static const struct ofp11_wc_map ofp11_wc_map[] = { | |
5482 | { OFPFMF11_IN_PORT, MFF_IN_PORT }, | |
5483 | { OFPFMF11_DL_VLAN, MFF_VLAN_VID }, | |
5484 | { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP }, | |
5485 | { OFPFMF11_DL_TYPE, MFF_ETH_TYPE }, | |
5486 | { OFPFMF11_NW_TOS, MFF_IP_DSCP }, | |
5487 | { OFPFMF11_NW_PROTO, MFF_IP_PROTO }, | |
5488 | { OFPFMF11_TP_SRC, MFF_TCP_SRC }, | |
5489 | { OFPFMF11_TP_DST, MFF_TCP_DST }, | |
5490 | { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL }, | |
5491 | { OFPFMF11_MPLS_TC, MFF_MPLS_TC }, | |
5492 | /* I don't know what OFPFMF11_TYPE means. */ | |
5493 | { OFPFMF11_DL_SRC, MFF_ETH_SRC }, | |
5494 | { OFPFMF11_DL_DST, MFF_ETH_DST }, | |
5495 | { OFPFMF11_NW_SRC, MFF_IPV4_SRC }, | |
5496 | { OFPFMF11_NW_DST, MFF_IPV4_DST }, | |
5497 | { OFPFMF11_METADATA, MFF_METADATA }, | |
5498 | }; | |
5499 | ||
5500 | static ovs_be32 | |
5501 | mf_bitmap_to_of11(const struct mf_bitmap *fields) | |
5502 | { | |
5503 | const struct ofp11_wc_map *p; | |
5504 | uint32_t wc11 = 0; | |
5505 | ||
5506 | for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) { | |
5507 | if (bitmap_is_set(fields->bm, p->mf)) { | |
5508 | wc11 |= p->wc11; | |
307975da SH |
5509 | } |
5510 | } | |
3c1bb396 | 5511 | return htonl(wc11); |
307975da SH |
5512 | } |
5513 | ||
3c1bb396 BP |
5514 | static struct mf_bitmap |
5515 | mf_bitmap_from_of11(ovs_be32 wc11_) | |
5516 | { | |
5517 | struct mf_bitmap fields = MF_BITMAP_INITIALIZER; | |
5518 | const struct ofp11_wc_map *p; | |
5519 | uint32_t wc11 = ntohl(wc11_); | |
5520 | ||
5521 | for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) { | |
5522 | if (wc11 & p->wc11) { | |
5523 | bitmap_set1(fields.bm, p->mf); | |
307975da SH |
5524 | } |
5525 | } | |
3c1bb396 | 5526 | return fields; |
307975da SH |
5527 | } |
5528 | ||
5529 | static void | |
3c1bb396 BP |
5530 | ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats, |
5531 | const struct ofputil_table_features *features, | |
307975da SH |
5532 | struct ofpbuf *buf) |
5533 | { | |
3c1bb396 | 5534 | struct mf_bitmap wc = wild_or_nonmatchable_fields(features); |
307975da SH |
5535 | struct ofp11_table_stats *out; |
5536 | ||
3bdc692b | 5537 | out = ofpbuf_put_zeros(buf, sizeof *out); |
3c1bb396 BP |
5538 | out->table_id = features->table_id; |
5539 | ovs_strlcpy(out->name, features->name, sizeof out->name); | |
5540 | out->wildcards = mf_bitmap_to_of11(&wc); | |
5541 | out->match = mf_bitmap_to_of11(&features->match); | |
5542 | out->instructions = ovsinst_bitmap_to_openflow( | |
5543 | features->nonmiss.instructions, OFP11_VERSION); | |
5544 | out->write_actions = ofpact_bitmap_to_openflow( | |
5545 | features->nonmiss.write.ofpacts, OFP11_VERSION); | |
5546 | out->apply_actions = ofpact_bitmap_to_openflow( | |
5547 | features->nonmiss.apply.ofpacts, OFP11_VERSION); | |
5548 | out->config = htonl(features->miss_config); | |
5549 | out->max_entries = htonl(features->max_entries); | |
5550 | out->active_count = htonl(stats->active_count); | |
5551 | out->lookup_count = htonll(stats->lookup_count); | |
5552 | out->matched_count = htonll(stats->matched_count); | |
08d1e234 BP |
5553 | } |
5554 | ||
6240624b | 5555 | static void |
3c1bb396 BP |
5556 | ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats, |
5557 | const struct ofputil_table_features *features, | |
6240624b BP |
5558 | struct ofpbuf *buf) |
5559 | { | |
08d1e234 | 5560 | struct ofp12_table_stats *out; |
6240624b | 5561 | |
08d1e234 | 5562 | out = ofpbuf_put_zeros(buf, sizeof *out); |
3c1bb396 BP |
5563 | out->table_id = features->table_id; |
5564 | ovs_strlcpy(out->name, features->name, sizeof out->name); | |
178742f9 BP |
5565 | out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION); |
5566 | out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard, | |
3c1bb396 BP |
5567 | OFP12_VERSION); |
5568 | out->write_actions = ofpact_bitmap_to_openflow( | |
5569 | features->nonmiss.write.ofpacts, OFP12_VERSION); | |
5570 | out->apply_actions = ofpact_bitmap_to_openflow( | |
5571 | features->nonmiss.apply.ofpacts, OFP12_VERSION); | |
178742f9 | 5572 | out->write_setfields = oxm_bitmap_from_mf_bitmap( |
3c1bb396 | 5573 | &features->nonmiss.write.set_fields, OFP12_VERSION); |
178742f9 | 5574 | out->apply_setfields = oxm_bitmap_from_mf_bitmap( |
3c1bb396 BP |
5575 | &features->nonmiss.apply.set_fields, OFP12_VERSION); |
5576 | out->metadata_match = features->metadata_match; | |
5577 | out->metadata_write = features->metadata_write; | |
5578 | out->instructions = ovsinst_bitmap_to_openflow( | |
5579 | features->nonmiss.instructions, OFP12_VERSION); | |
82c22d34 BP |
5580 | out->config = ofputil_encode_table_config(features->miss_config, |
5581 | OFPUTIL_TABLE_EVICTION_DEFAULT, | |
5582 | OFP12_VERSION); | |
3c1bb396 BP |
5583 | out->max_entries = htonl(features->max_entries); |
5584 | out->active_count = htonl(stats->active_count); | |
5585 | out->lookup_count = htonll(stats->lookup_count); | |
5586 | out->matched_count = htonll(stats->matched_count); | |
6240624b BP |
5587 | } |
5588 | ||
2e1ae200 | 5589 | static void |
3c1bb396 | 5590 | ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats, |
2e1ae200 JR |
5591 | struct ofpbuf *buf) |
5592 | { | |
5593 | struct ofp13_table_stats *out; | |
5594 | ||
3c1bb396 BP |
5595 | out = ofpbuf_put_zeros(buf, sizeof *out); |
5596 | out->table_id = stats->table_id; | |
5597 | out->active_count = htonl(stats->active_count); | |
5598 | out->lookup_count = htonll(stats->lookup_count); | |
5599 | out->matched_count = htonll(stats->matched_count); | |
2e1ae200 JR |
5600 | } |
5601 | ||
307975da | 5602 | struct ofpbuf * |
3c1bb396 | 5603 | ofputil_encode_table_stats_reply(const struct ofp_header *request) |
307975da | 5604 | { |
3c1bb396 BP |
5605 | return ofpraw_alloc_stats_reply(request, 0); |
5606 | } | |
307975da | 5607 | |
3c1bb396 BP |
5608 | void |
5609 | ofputil_append_table_stats_reply(struct ofpbuf *reply, | |
5610 | const struct ofputil_table_stats *stats, | |
5611 | const struct ofputil_table_features *features) | |
5612 | { | |
6fd6ed71 | 5613 | struct ofp_header *oh = reply->header; |
307975da | 5614 | |
3c1bb396 | 5615 | ovs_assert(stats->table_id == features->table_id); |
307975da | 5616 | |
3c1bb396 BP |
5617 | switch ((enum ofp_version) oh->version) { |
5618 | case OFP10_VERSION: | |
5619 | ofputil_put_ofp10_table_stats(stats, features, reply); | |
5620 | break; | |
307975da | 5621 | |
3c1bb396 BP |
5622 | case OFP11_VERSION: |
5623 | ofputil_put_ofp11_table_stats(stats, features, reply); | |
5624 | break; | |
307975da | 5625 | |
3c1bb396 BP |
5626 | case OFP12_VERSION: |
5627 | ofputil_put_ofp12_table_stats(stats, features, reply); | |
5628 | break; | |
2e1ae200 | 5629 | |
3c1bb396 BP |
5630 | case OFP13_VERSION: |
5631 | case OFP14_VERSION: | |
5632 | case OFP15_VERSION: | |
5633 | ofputil_put_ofp13_table_stats(stats, reply); | |
5634 | break; | |
5635 | ||
5636 | default: | |
5637 | OVS_NOT_REACHED(); | |
307975da | 5638 | } |
3c1bb396 | 5639 | } |
307975da | 5640 | |
3c1bb396 BP |
5641 | static int |
5642 | ofputil_decode_ofp10_table_stats(struct ofpbuf *msg, | |
5643 | struct ofputil_table_stats *stats, | |
5644 | struct ofputil_table_features *features) | |
5645 | { | |
5646 | struct ofp10_table_stats *ots; | |
5647 | ||
5648 | ots = ofpbuf_try_pull(msg, sizeof *ots); | |
5649 | if (!ots) { | |
5650 | return OFPERR_OFPBRC_BAD_LEN; | |
5651 | } | |
5652 | ||
5653 | features->table_id = ots->table_id; | |
5654 | ovs_strlcpy(features->name, ots->name, sizeof features->name); | |
5655 | features->max_entries = ntohl(ots->max_entries); | |
5656 | features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards); | |
5657 | ||
5658 | stats->table_id = ots->table_id; | |
5659 | stats->active_count = ntohl(ots->active_count); | |
5660 | stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count)); | |
5661 | stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count)); | |
5662 | ||
5663 | return 0; | |
5664 | } | |
5665 | ||
5666 | static int | |
5667 | ofputil_decode_ofp11_table_stats(struct ofpbuf *msg, | |
5668 | struct ofputil_table_stats *stats, | |
5669 | struct ofputil_table_features *features) | |
5670 | { | |
5671 | struct ofp11_table_stats *ots; | |
5672 | ||
5673 | ots = ofpbuf_try_pull(msg, sizeof *ots); | |
5674 | if (!ots) { | |
5675 | return OFPERR_OFPBRC_BAD_LEN; | |
5676 | } | |
5677 | ||
5678 | features->table_id = ots->table_id; | |
5679 | ovs_strlcpy(features->name, ots->name, sizeof features->name); | |
5680 | features->max_entries = ntohl(ots->max_entries); | |
5681 | features->nonmiss.instructions = ovsinst_bitmap_from_openflow( | |
5682 | ots->instructions, OFP11_VERSION); | |
5683 | features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow( | |
5684 | ots->write_actions, OFP11_VERSION); | |
5685 | features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow( | |
5686 | ots->write_actions, OFP11_VERSION); | |
5687 | features->miss = features->nonmiss; | |
82c22d34 BP |
5688 | features->miss_config = ofputil_decode_table_miss(ots->config, |
5689 | OFP11_VERSION); | |
3c1bb396 BP |
5690 | features->match = mf_bitmap_from_of11(ots->match); |
5691 | features->wildcard = mf_bitmap_from_of11(ots->wildcards); | |
5692 | bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS); | |
5693 | ||
5694 | stats->table_id = ots->table_id; | |
5695 | stats->active_count = ntohl(ots->active_count); | |
5696 | stats->lookup_count = ntohll(ots->lookup_count); | |
5697 | stats->matched_count = ntohll(ots->matched_count); | |
5698 | ||
5699 | return 0; | |
5700 | } | |
5701 | ||
5702 | static int | |
5703 | ofputil_decode_ofp12_table_stats(struct ofpbuf *msg, | |
5704 | struct ofputil_table_stats *stats, | |
5705 | struct ofputil_table_features *features) | |
5706 | { | |
5707 | struct ofp12_table_stats *ots; | |
5708 | ||
5709 | ots = ofpbuf_try_pull(msg, sizeof *ots); | |
5710 | if (!ots) { | |
5711 | return OFPERR_OFPBRC_BAD_LEN; | |
5712 | } | |
5713 | ||
5714 | features->table_id = ots->table_id; | |
5715 | ovs_strlcpy(features->name, ots->name, sizeof features->name); | |
5716 | features->metadata_match = ots->metadata_match; | |
5717 | features->metadata_write = ots->metadata_write; | |
82c22d34 BP |
5718 | features->miss_config = ofputil_decode_table_miss(ots->config, |
5719 | OFP12_VERSION); | |
3c1bb396 BP |
5720 | features->max_entries = ntohl(ots->max_entries); |
5721 | ||
5722 | features->nonmiss.instructions = ovsinst_bitmap_from_openflow( | |
5723 | ots->instructions, OFP12_VERSION); | |
5724 | features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow( | |
5725 | ots->write_actions, OFP12_VERSION); | |
5726 | features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow( | |
5727 | ots->apply_actions, OFP12_VERSION); | |
178742f9 | 5728 | features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap( |
3c1bb396 | 5729 | ots->write_setfields, OFP12_VERSION); |
178742f9 | 5730 | features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap( |
3c1bb396 BP |
5731 | ots->apply_setfields, OFP12_VERSION); |
5732 | features->miss = features->nonmiss; | |
5733 | ||
178742f9 BP |
5734 | features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION); |
5735 | features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards, | |
5736 | OFP12_VERSION); | |
3c1bb396 BP |
5737 | bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS); |
5738 | ||
5739 | stats->table_id = ots->table_id; | |
5740 | stats->active_count = ntohl(ots->active_count); | |
5741 | stats->lookup_count = ntohll(ots->lookup_count); | |
5742 | stats->matched_count = ntohll(ots->matched_count); | |
5743 | ||
5744 | return 0; | |
5745 | } | |
5746 | ||
5747 | static int | |
5748 | ofputil_decode_ofp13_table_stats(struct ofpbuf *msg, | |
5749 | struct ofputil_table_stats *stats, | |
5750 | struct ofputil_table_features *features) | |
5751 | { | |
5752 | struct ofp13_table_stats *ots; | |
5753 | ||
5754 | ots = ofpbuf_try_pull(msg, sizeof *ots); | |
5755 | if (!ots) { | |
5756 | return OFPERR_OFPBRC_BAD_LEN; | |
5757 | } | |
5758 | ||
5759 | features->table_id = ots->table_id; | |
5760 | ||
5761 | stats->table_id = ots->table_id; | |
5762 | stats->active_count = ntohl(ots->active_count); | |
5763 | stats->lookup_count = ntohll(ots->lookup_count); | |
5764 | stats->matched_count = ntohll(ots->matched_count); | |
5765 | ||
5766 | return 0; | |
5767 | } | |
5768 | ||
5769 | int | |
5770 | ofputil_decode_table_stats_reply(struct ofpbuf *msg, | |
5771 | struct ofputil_table_stats *stats, | |
5772 | struct ofputil_table_features *features) | |
5773 | { | |
5774 | const struct ofp_header *oh; | |
5775 | ||
6fd6ed71 | 5776 | if (!msg->header) { |
3c1bb396 BP |
5777 | ofpraw_pull_assert(msg); |
5778 | } | |
6fd6ed71 | 5779 | oh = msg->header; |
3c1bb396 | 5780 | |
6fd6ed71 | 5781 | if (!msg->size) { |
3c1bb396 BP |
5782 | return EOF; |
5783 | } | |
5784 | ||
5785 | memset(stats, 0, sizeof *stats); | |
5786 | memset(features, 0, sizeof *features); | |
82c22d34 BP |
5787 | features->supports_eviction = -1; |
5788 | features->supports_vacancy_events = -1; | |
3c1bb396 BP |
5789 | |
5790 | switch ((enum ofp_version) oh->version) { | |
5791 | case OFP10_VERSION: | |
5792 | return ofputil_decode_ofp10_table_stats(msg, stats, features); | |
5793 | ||
5794 | case OFP11_VERSION: | |
5795 | return ofputil_decode_ofp11_table_stats(msg, stats, features); | |
5796 | ||
5797 | case OFP12_VERSION: | |
5798 | return ofputil_decode_ofp12_table_stats(msg, stats, features); | |
5799 | ||
5800 | case OFP13_VERSION: | |
5801 | case OFP14_VERSION: | |
5802 | case OFP15_VERSION: | |
5803 | return ofputil_decode_ofp13_table_stats(msg, stats, features); | |
5804 | ||
5805 | default: | |
5806 | OVS_NOT_REACHED(); | |
5807 | } | |
307975da SH |
5808 | } |
5809 | \f | |
2b07c8b1 BP |
5810 | /* ofputil_flow_monitor_request */ |
5811 | ||
5812 | /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract | |
5813 | * ofputil_flow_monitor_request in 'rq'. | |
5814 | * | |
5815 | * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow | |
5816 | * message. Calling this function multiple times for a single 'msg' iterates | |
5817 | * through the requests. The caller must initially leave 'msg''s layer | |
5818 | * pointers null and not modify them between calls. | |
5819 | * | |
5820 | * Returns 0 if successful, EOF if no requests were left in this 'msg', | |
5821 | * otherwise an OFPERR_* value. */ | |
5822 | int | |
5823 | ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq, | |
5824 | struct ofpbuf *msg) | |
5825 | { | |
5826 | struct nx_flow_monitor_request *nfmr; | |
5827 | uint16_t flags; | |
5828 | ||
6fd6ed71 | 5829 | if (!msg->header) { |
982697a4 | 5830 | ofpraw_pull_assert(msg); |
2b07c8b1 BP |
5831 | } |
5832 | ||
6fd6ed71 | 5833 | if (!msg->size) { |
2b07c8b1 BP |
5834 | return EOF; |
5835 | } | |
5836 | ||
5837 | nfmr = ofpbuf_try_pull(msg, sizeof *nfmr); | |
5838 | if (!nfmr) { | |
437d0d22 | 5839 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" " |
6fd6ed71 | 5840 | "leftover bytes at end", msg->size); |
2b07c8b1 BP |
5841 | return OFPERR_OFPBRC_BAD_LEN; |
5842 | } | |
5843 | ||
5844 | flags = ntohs(nfmr->flags); | |
5845 | if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY)) | |
5846 | || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | |
5847 | | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) { | |
5848 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16, | |
5849 | flags); | |
04f9f286 | 5850 | return OFPERR_OFPMOFC_BAD_FLAGS; |
2b07c8b1 BP |
5851 | } |
5852 | ||
5853 | if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) { | |
5854 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
5855 | } | |
5856 | ||
5857 | rq->id = ntohl(nfmr->id); | |
5858 | rq->flags = flags; | |
4e022ec0 | 5859 | rq->out_port = u16_to_ofp(ntohs(nfmr->out_port)); |
2b07c8b1 BP |
5860 | rq->table_id = nfmr->table_id; |
5861 | ||
81a76618 | 5862 | return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL); |
2b07c8b1 BP |
5863 | } |
5864 | ||
5865 | void | |
5866 | ofputil_append_flow_monitor_request( | |
5867 | const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg) | |
5868 | { | |
5869 | struct nx_flow_monitor_request *nfmr; | |
5870 | size_t start_ofs; | |
5871 | int match_len; | |
5872 | ||
6fd6ed71 | 5873 | if (!msg->size) { |
982697a4 | 5874 | ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg); |
2b07c8b1 BP |
5875 | } |
5876 | ||
6fd6ed71 | 5877 | start_ofs = msg->size; |
2b07c8b1 | 5878 | ofpbuf_put_zeros(msg, sizeof *nfmr); |
7623f4dd | 5879 | match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0)); |
2b07c8b1 BP |
5880 | |
5881 | nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr); | |
5882 | nfmr->id = htonl(rq->id); | |
5883 | nfmr->flags = htons(rq->flags); | |
4e022ec0 | 5884 | nfmr->out_port = htons(ofp_to_u16(rq->out_port)); |
2b07c8b1 BP |
5885 | nfmr->match_len = htons(match_len); |
5886 | nfmr->table_id = rq->table_id; | |
5887 | } | |
5888 | ||
5889 | /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg' | |
5890 | * into an abstract ofputil_flow_update in 'update'. The caller must have | |
81a76618 | 5891 | * initialized update->match to point to space allocated for a match. |
2b07c8b1 BP |
5892 | * |
5893 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's | |
5894 | * actions (except for NXFME_ABBREV, which never includes actions). The caller | |
5895 | * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts' | |
5896 | * will point into the 'ofpacts' buffer. | |
5897 | * | |
5898 | * Multiple flow updates can be packed into a single OpenFlow message. Calling | |
5899 | * this function multiple times for a single 'msg' iterates through the | |
5900 | * updates. The caller must initially leave 'msg''s layer pointers null and | |
5901 | * not modify them between calls. | |
5902 | * | |
5903 | * Returns 0 if successful, EOF if no updates were left in this 'msg', | |
5904 | * otherwise an OFPERR_* value. */ | |
5905 | int | |
5906 | ofputil_decode_flow_update(struct ofputil_flow_update *update, | |
5907 | struct ofpbuf *msg, struct ofpbuf *ofpacts) | |
5908 | { | |
5909 | struct nx_flow_update_header *nfuh; | |
5910 | unsigned int length; | |
e3f8f887 | 5911 | struct ofp_header *oh; |
2b07c8b1 | 5912 | |
6fd6ed71 | 5913 | if (!msg->header) { |
982697a4 | 5914 | ofpraw_pull_assert(msg); |
2b07c8b1 BP |
5915 | } |
5916 | ||
6fd6ed71 | 5917 | if (!msg->size) { |
2b07c8b1 BP |
5918 | return EOF; |
5919 | } | |
5920 | ||
6fd6ed71 | 5921 | if (msg->size < sizeof(struct nx_flow_update_header)) { |
2b07c8b1 BP |
5922 | goto bad_len; |
5923 | } | |
5924 | ||
6fd6ed71 | 5925 | oh = msg->header; |
e3f8f887 | 5926 | |
6fd6ed71 | 5927 | nfuh = msg->data; |
2b07c8b1 BP |
5928 | update->event = ntohs(nfuh->event); |
5929 | length = ntohs(nfuh->length); | |
6fd6ed71 | 5930 | if (length > msg->size || length % 8) { |
2b07c8b1 BP |
5931 | goto bad_len; |
5932 | } | |
5933 | ||
5934 | if (update->event == NXFME_ABBREV) { | |
5935 | struct nx_flow_update_abbrev *nfua; | |
5936 | ||
5937 | if (length != sizeof *nfua) { | |
5938 | goto bad_len; | |
5939 | } | |
5940 | ||
5941 | nfua = ofpbuf_pull(msg, sizeof *nfua); | |
5942 | update->xid = nfua->xid; | |
5943 | return 0; | |
5944 | } else if (update->event == NXFME_ADDED | |
5945 | || update->event == NXFME_DELETED | |
5946 | || update->event == NXFME_MODIFIED) { | |
5947 | struct nx_flow_update_full *nfuf; | |
5948 | unsigned int actions_len; | |
5949 | unsigned int match_len; | |
5950 | enum ofperr error; | |
5951 | ||
5952 | if (length < sizeof *nfuf) { | |
5953 | goto bad_len; | |
5954 | } | |
5955 | ||
5956 | nfuf = ofpbuf_pull(msg, sizeof *nfuf); | |
5957 | match_len = ntohs(nfuf->match_len); | |
5958 | if (sizeof *nfuf + match_len > length) { | |
5959 | goto bad_len; | |
5960 | } | |
5961 | ||
5962 | update->reason = ntohs(nfuf->reason); | |
5963 | update->idle_timeout = ntohs(nfuf->idle_timeout); | |
5964 | update->hard_timeout = ntohs(nfuf->hard_timeout); | |
5965 | update->table_id = nfuf->table_id; | |
5966 | update->cookie = nfuf->cookie; | |
81a76618 | 5967 | update->priority = ntohs(nfuf->priority); |
2b07c8b1 | 5968 | |
81a76618 | 5969 | error = nx_pull_match(msg, match_len, update->match, NULL, NULL); |
2b07c8b1 BP |
5970 | if (error) { |
5971 | return error; | |
5972 | } | |
5973 | ||
5974 | actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8); | |
e3f8f887 JR |
5975 | error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version, |
5976 | ofpacts); | |
2b07c8b1 BP |
5977 | if (error) { |
5978 | return error; | |
5979 | } | |
5980 | ||
6fd6ed71 PS |
5981 | update->ofpacts = ofpacts->data; |
5982 | update->ofpacts_len = ofpacts->size; | |
2b07c8b1 BP |
5983 | return 0; |
5984 | } else { | |
5985 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
5986 | "NXST_FLOW_MONITOR reply has bad event %"PRIu16, | |
5987 | ntohs(nfuh->event)); | |
15549878 | 5988 | return OFPERR_NXBRC_FM_BAD_EVENT; |
2b07c8b1 BP |
5989 | } |
5990 | ||
5991 | bad_len: | |
437d0d22 | 5992 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" " |
6fd6ed71 | 5993 | "leftover bytes at end", msg->size); |
2b07c8b1 BP |
5994 | return OFPERR_OFPBRC_BAD_LEN; |
5995 | } | |
5996 | ||
5997 | uint32_t | |
5998 | ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh) | |
5999 | { | |
982697a4 BP |
6000 | const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh); |
6001 | ||
6002 | return ntohl(cancel->id); | |
2b07c8b1 | 6003 | } |
9e1fd49b | 6004 | |
2b07c8b1 BP |
6005 | struct ofpbuf * |
6006 | ofputil_encode_flow_monitor_cancel(uint32_t id) | |
6007 | { | |
6008 | struct nx_flow_monitor_cancel *nfmc; | |
6009 | struct ofpbuf *msg; | |
6010 | ||
982697a4 BP |
6011 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0); |
6012 | nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc); | |
2b07c8b1 BP |
6013 | nfmc->id = htonl(id); |
6014 | return msg; | |
6015 | } | |
6016 | ||
6017 | void | |
ca6ba700 | 6018 | ofputil_start_flow_update(struct ovs_list *replies) |
2b07c8b1 BP |
6019 | { |
6020 | struct ofpbuf *msg; | |
6021 | ||
982697a4 BP |
6022 | msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION, |
6023 | htonl(0), 1024); | |
2b07c8b1 BP |
6024 | |
6025 | list_init(replies); | |
6026 | list_push_back(replies, &msg->list_node); | |
6027 | } | |
6028 | ||
6029 | void | |
6030 | ofputil_append_flow_update(const struct ofputil_flow_update *update, | |
ca6ba700 | 6031 | struct ovs_list *replies) |
2b07c8b1 | 6032 | { |
e28ac5cf | 6033 | enum ofp_version version = ofpmp_version(replies); |
2b07c8b1 BP |
6034 | struct nx_flow_update_header *nfuh; |
6035 | struct ofpbuf *msg; | |
6036 | size_t start_ofs; | |
6037 | ||
6038 | msg = ofpbuf_from_list(list_back(replies)); | |
6fd6ed71 | 6039 | start_ofs = msg->size; |
2b07c8b1 BP |
6040 | |
6041 | if (update->event == NXFME_ABBREV) { | |
6042 | struct nx_flow_update_abbrev *nfua; | |
6043 | ||
6044 | nfua = ofpbuf_put_zeros(msg, sizeof *nfua); | |
6045 | nfua->xid = update->xid; | |
6046 | } else { | |
6047 | struct nx_flow_update_full *nfuf; | |
6048 | int match_len; | |
6049 | ||
6050 | ofpbuf_put_zeros(msg, sizeof *nfuf); | |
7623f4dd | 6051 | match_len = nx_put_match(msg, update->match, htonll(0), htonll(0)); |
e3f8f887 JR |
6052 | ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg, |
6053 | version); | |
2b07c8b1 BP |
6054 | nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf); |
6055 | nfuf->reason = htons(update->reason); | |
81a76618 | 6056 | nfuf->priority = htons(update->priority); |
2b07c8b1 BP |
6057 | nfuf->idle_timeout = htons(update->idle_timeout); |
6058 | nfuf->hard_timeout = htons(update->hard_timeout); | |
6059 | nfuf->match_len = htons(match_len); | |
6060 | nfuf->table_id = update->table_id; | |
6061 | nfuf->cookie = update->cookie; | |
6062 | } | |
6063 | ||
6064 | nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh); | |
6fd6ed71 | 6065 | nfuh->length = htons(msg->size - start_ofs); |
2b07c8b1 BP |
6066 | nfuh->event = htons(update->event); |
6067 | ||
982697a4 | 6068 | ofpmp_postappend(replies, start_ofs); |
2b07c8b1 BP |
6069 | } |
6070 | \f | |
c6a93eb7 | 6071 | struct ofpbuf * |
de0f3156 SH |
6072 | ofputil_encode_packet_out(const struct ofputil_packet_out *po, |
6073 | enum ofputil_protocol protocol) | |
c6a93eb7 | 6074 | { |
de0f3156 | 6075 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); |
c6a93eb7 BP |
6076 | struct ofpbuf *msg; |
6077 | size_t size; | |
6078 | ||
982697a4 | 6079 | size = po->ofpacts_len; |
c6a93eb7 BP |
6080 | if (po->buffer_id == UINT32_MAX) { |
6081 | size += po->packet_len; | |
6082 | } | |
6083 | ||
de0f3156 SH |
6084 | switch (ofp_version) { |
6085 | case OFP10_VERSION: { | |
31a9e63f | 6086 | struct ofp10_packet_out *opo; |
de0f3156 SH |
6087 | size_t actions_ofs; |
6088 | ||
6089 | msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size); | |
6090 | ofpbuf_put_zeros(msg, sizeof *opo); | |
6fd6ed71 | 6091 | actions_ofs = msg->size; |
e3f8f887 JR |
6092 | ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg, |
6093 | ofp_version); | |
de0f3156 | 6094 | |
6fd6ed71 | 6095 | opo = msg->msg; |
de0f3156 | 6096 | opo->buffer_id = htonl(po->buffer_id); |
4e022ec0 | 6097 | opo->in_port = htons(ofp_to_u16(po->in_port)); |
6fd6ed71 | 6098 | opo->actions_len = htons(msg->size - actions_ofs); |
de0f3156 SH |
6099 | break; |
6100 | } | |
f25d0cf3 | 6101 | |
de0f3156 | 6102 | case OFP11_VERSION: |
2e1ae200 | 6103 | case OFP12_VERSION: |
c37c0382 | 6104 | case OFP13_VERSION: |
42dccab5 BP |
6105 | case OFP14_VERSION: |
6106 | case OFP15_VERSION: { | |
7c1b1a0d SH |
6107 | struct ofp11_packet_out *opo; |
6108 | size_t len; | |
6109 | ||
6110 | msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size); | |
6111 | ofpbuf_put_zeros(msg, sizeof *opo); | |
e3f8f887 JR |
6112 | len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg, |
6113 | ofp_version); | |
6fd6ed71 | 6114 | opo = msg->msg; |
7c1b1a0d SH |
6115 | opo->buffer_id = htonl(po->buffer_id); |
6116 | opo->in_port = ofputil_port_to_ofp11(po->in_port); | |
6117 | opo->actions_len = htons(len); | |
6118 | break; | |
6119 | } | |
6120 | ||
de0f3156 | 6121 | default: |
428b2edd | 6122 | OVS_NOT_REACHED(); |
de0f3156 | 6123 | } |
f25d0cf3 | 6124 | |
c6a93eb7 BP |
6125 | if (po->buffer_id == UINT32_MAX) { |
6126 | ofpbuf_put(msg, po->packet, po->packet_len); | |
6127 | } | |
f25d0cf3 | 6128 | |
982697a4 | 6129 | ofpmsg_update_length(msg); |
c6a93eb7 BP |
6130 | |
6131 | return msg; | |
6132 | } | |
d1e2cf21 | 6133 | \f |
fa37b408 BP |
6134 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ |
6135 | struct ofpbuf * | |
1a126c0c | 6136 | make_echo_request(enum ofp_version ofp_version) |
fa37b408 | 6137 | { |
1a126c0c | 6138 | return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version, |
982697a4 | 6139 | htonl(0), 0); |
fa37b408 BP |
6140 | } |
6141 | ||
6142 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
6143 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
6144 | struct ofpbuf * | |
6145 | make_echo_reply(const struct ofp_header *rq) | |
6146 | { | |
982697a4 BP |
6147 | struct ofpbuf rq_buf; |
6148 | struct ofpbuf *reply; | |
6149 | ||
6150 | ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length)); | |
6151 | ofpraw_pull_assert(&rq_buf); | |
6152 | ||
6fd6ed71 PS |
6153 | reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size); |
6154 | ofpbuf_put(reply, rq_buf.data, rq_buf.size); | |
982697a4 | 6155 | return reply; |
fa37b408 BP |
6156 | } |
6157 | ||
efb80167 | 6158 | struct ofpbuf * |
a0ae0b6e | 6159 | ofputil_encode_barrier_request(enum ofp_version ofp_version) |
efb80167 | 6160 | { |
a0ae0b6e SH |
6161 | enum ofpraw type; |
6162 | ||
6163 | switch (ofp_version) { | |
42dccab5 | 6164 | case OFP15_VERSION: |
c37c0382 | 6165 | case OFP14_VERSION: |
2e1ae200 | 6166 | case OFP13_VERSION: |
a0ae0b6e SH |
6167 | case OFP12_VERSION: |
6168 | case OFP11_VERSION: | |
6169 | type = OFPRAW_OFPT11_BARRIER_REQUEST; | |
6170 | break; | |
6171 | ||
6172 | case OFP10_VERSION: | |
6173 | type = OFPRAW_OFPT10_BARRIER_REQUEST; | |
6174 | break; | |
6175 | ||
6176 | default: | |
428b2edd | 6177 | OVS_NOT_REACHED(); |
a0ae0b6e SH |
6178 | } |
6179 | ||
6180 | return ofpraw_alloc(type, ofp_version, 0); | |
efb80167 BP |
6181 | } |
6182 | ||
7257b535 BP |
6183 | const char * |
6184 | ofputil_frag_handling_to_string(enum ofp_config_flags flags) | |
6185 | { | |
6186 | switch (flags & OFPC_FRAG_MASK) { | |
6187 | case OFPC_FRAG_NORMAL: return "normal"; | |
6188 | case OFPC_FRAG_DROP: return "drop"; | |
6189 | case OFPC_FRAG_REASM: return "reassemble"; | |
6190 | case OFPC_FRAG_NX_MATCH: return "nx-match"; | |
6191 | } | |
6192 | ||
428b2edd | 6193 | OVS_NOT_REACHED(); |
7257b535 BP |
6194 | } |
6195 | ||
6196 | bool | |
6197 | ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags) | |
6198 | { | |
6199 | if (!strcasecmp(s, "normal")) { | |
6200 | *flags = OFPC_FRAG_NORMAL; | |
6201 | } else if (!strcasecmp(s, "drop")) { | |
6202 | *flags = OFPC_FRAG_DROP; | |
6203 | } else if (!strcasecmp(s, "reassemble")) { | |
6204 | *flags = OFPC_FRAG_REASM; | |
6205 | } else if (!strcasecmp(s, "nx-match")) { | |
6206 | *flags = OFPC_FRAG_NX_MATCH; | |
6207 | } else { | |
6208 | return false; | |
6209 | } | |
6210 | return true; | |
6211 | } | |
6212 | ||
7b7503ea BP |
6213 | /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0 |
6214 | * port number and stores the latter in '*ofp10_port', for the purpose of | |
6215 | * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful, | |
bc146369 | 6216 | * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'. |
7b7503ea BP |
6217 | * |
6218 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
6219 | enum ofperr | |
4e022ec0 | 6220 | ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port) |
7b7503ea BP |
6221 | { |
6222 | uint32_t ofp11_port_h = ntohl(ofp11_port); | |
6223 | ||
4e022ec0 AW |
6224 | if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) { |
6225 | *ofp10_port = u16_to_ofp(ofp11_port_h); | |
7b7503ea | 6226 | return 0; |
4e022ec0 AW |
6227 | } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) { |
6228 | *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET); | |
7b7503ea BP |
6229 | return 0; |
6230 | } else { | |
bc146369 | 6231 | *ofp10_port = OFPP_NONE; |
7b7503ea BP |
6232 | VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported " |
6233 | "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32, | |
4e022ec0 AW |
6234 | ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1, |
6235 | ofp11_to_u32(OFPP11_MAX), UINT32_MAX); | |
7b7503ea BP |
6236 | return OFPERR_OFPBAC_BAD_OUT_PORT; |
6237 | } | |
6238 | } | |
6239 | ||
6240 | /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port | |
6241 | * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages. | |
6242 | * | |
6243 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
6244 | ovs_be32 | |
4e022ec0 | 6245 | ofputil_port_to_ofp11(ofp_port_t ofp10_port) |
7b7503ea | 6246 | { |
4e022ec0 AW |
6247 | return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX) |
6248 | ? ofp_to_u16(ofp10_port) | |
6249 | : ofp_to_u16(ofp10_port) + OFPP11_OFFSET); | |
7b7503ea BP |
6250 | } |
6251 | ||
39dc9082 BP |
6252 | #define OFPUTIL_NAMED_PORTS \ |
6253 | OFPUTIL_NAMED_PORT(IN_PORT) \ | |
6254 | OFPUTIL_NAMED_PORT(TABLE) \ | |
6255 | OFPUTIL_NAMED_PORT(NORMAL) \ | |
6256 | OFPUTIL_NAMED_PORT(FLOOD) \ | |
6257 | OFPUTIL_NAMED_PORT(ALL) \ | |
6258 | OFPUTIL_NAMED_PORT(CONTROLLER) \ | |
6259 | OFPUTIL_NAMED_PORT(LOCAL) \ | |
c61f3870 BP |
6260 | OFPUTIL_NAMED_PORT(ANY) \ |
6261 | OFPUTIL_NAMED_PORT(UNSET) | |
7f05e7ab JR |
6262 | |
6263 | /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */ | |
6264 | #define OFPUTIL_NAMED_PORTS_WITH_NONE \ | |
6265 | OFPUTIL_NAMED_PORTS \ | |
39dc9082 BP |
6266 | OFPUTIL_NAMED_PORT(NONE) |
6267 | ||
8010100b BP |
6268 | /* Stores the port number represented by 's' into '*portp'. 's' may be an |
6269 | * integer or, for reserved ports, the standard OpenFlow name for the port | |
6270 | * (e.g. "LOCAL"). | |
c6100d92 | 6271 | * |
8010100b BP |
6272 | * Returns true if successful, false if 's' is not a valid OpenFlow port number |
6273 | * or name. The caller should issue an error message in this case, because | |
6274 | * this function usually does not. (This gives the caller an opportunity to | |
6275 | * look up the port name another way, e.g. by contacting the switch and listing | |
6276 | * the names of all its ports). | |
c6100d92 BP |
6277 | * |
6278 | * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset | |
6279 | * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit | |
6280 | * range as described in include/openflow/openflow-1.1.h. */ | |
8010100b | 6281 | bool |
4e022ec0 | 6282 | ofputil_port_from_string(const char *s, ofp_port_t *portp) |
39dc9082 | 6283 | { |
5b23ff7e | 6284 | unsigned int port32; /* int is at least 32 bits wide. */ |
c6100d92 | 6285 | |
5b23ff7e JR |
6286 | if (*s == '-') { |
6287 | VLOG_WARN("Negative value %s is not a valid port number.", s); | |
6288 | return false; | |
6289 | } | |
8010100b | 6290 | *portp = 0; |
c6100d92 | 6291 | if (str_to_uint(s, 10, &port32)) { |
4e022ec0 AW |
6292 | if (port32 < ofp_to_u16(OFPP_MAX)) { |
6293 | /* Pass. */ | |
6294 | } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) { | |
c6100d92 BP |
6295 | VLOG_WARN("port %u is a reserved OF1.0 port number that will " |
6296 | "be translated to %u when talking to an OF1.1 or " | |
6297 | "later controller", port32, port32 + OFPP11_OFFSET); | |
4e022ec0 | 6298 | } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) { |
28b11432 BP |
6299 | char name[OFP_MAX_PORT_NAME_LEN]; |
6300 | ||
6301 | ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name); | |
6302 | VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated " | |
6303 | "for compatibility with OpenFlow 1.1 and later", | |
6304 | name, port32); | |
4e022ec0 | 6305 | } else if (port32 < ofp11_to_u32(OFPP11_MAX)) { |
c6100d92 | 6306 | VLOG_WARN("port %u is outside the supported range 0 through " |
d047fd17 | 6307 | "%"PRIx16" or 0x%x through 0x%"PRIx32, port32, |
4e022ec0 | 6308 | UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX); |
8010100b | 6309 | return false; |
c6100d92 | 6310 | } else { |
4e022ec0 | 6311 | port32 -= OFPP11_OFFSET; |
c6100d92 | 6312 | } |
4e022ec0 AW |
6313 | |
6314 | *portp = u16_to_ofp(port32); | |
6315 | return true; | |
c6100d92 BP |
6316 | } else { |
6317 | struct pair { | |
6318 | const char *name; | |
4e022ec0 | 6319 | ofp_port_t value; |
c6100d92 BP |
6320 | }; |
6321 | static const struct pair pairs[] = { | |
39dc9082 | 6322 | #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME}, |
7f05e7ab | 6323 | OFPUTIL_NAMED_PORTS_WITH_NONE |
39dc9082 | 6324 | #undef OFPUTIL_NAMED_PORT |
c6100d92 BP |
6325 | }; |
6326 | const struct pair *p; | |
39dc9082 | 6327 | |
c6100d92 BP |
6328 | for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) { |
6329 | if (!strcasecmp(s, p->name)) { | |
8010100b BP |
6330 | *portp = p->value; |
6331 | return true; | |
c6100d92 | 6332 | } |
39dc9082 | 6333 | } |
8010100b | 6334 | return false; |
39dc9082 | 6335 | } |
39dc9082 BP |
6336 | } |
6337 | ||
6338 | /* Appends to 's' a string representation of the OpenFlow port number 'port'. | |
6339 | * Most ports' string representation is just the port number, but for special | |
6340 | * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */ | |
6341 | void | |
4e022ec0 | 6342 | ofputil_format_port(ofp_port_t port, struct ds *s) |
39dc9082 | 6343 | { |
28b11432 BP |
6344 | char name[OFP_MAX_PORT_NAME_LEN]; |
6345 | ||
6346 | ofputil_port_to_string(port, name, sizeof name); | |
6347 | ds_put_cstr(s, name); | |
6348 | } | |
39dc9082 | 6349 | |
28b11432 BP |
6350 | /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string |
6351 | * representation of OpenFlow port number 'port'. Most ports are represented | |
6352 | * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented | |
6353 | * by name, e.g. "LOCAL". */ | |
6354 | void | |
6355 | ofputil_port_to_string(ofp_port_t port, | |
6356 | char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize) | |
6357 | { | |
39dc9082 | 6358 | switch (port) { |
28b11432 BP |
6359 | #define OFPUTIL_NAMED_PORT(NAME) \ |
6360 | case OFPP_##NAME: \ | |
6361 | ovs_strlcpy(namebuf, #NAME, bufsize); \ | |
6362 | break; | |
39dc9082 BP |
6363 | OFPUTIL_NAMED_PORTS |
6364 | #undef OFPUTIL_NAMED_PORT | |
6365 | ||
6366 | default: | |
28b11432 BP |
6367 | snprintf(namebuf, bufsize, "%"PRIu16, port); |
6368 | break; | |
39dc9082 | 6369 | } |
39dc9082 BP |
6370 | } |
6371 | ||
7395c052 NZ |
6372 | /* Stores the group id represented by 's' into '*group_idp'. 's' may be an |
6373 | * integer or, for reserved group IDs, the standard OpenFlow name for the group | |
6374 | * (either "ANY" or "ALL"). | |
6375 | * | |
6376 | * Returns true if successful, false if 's' is not a valid OpenFlow group ID or | |
6377 | * name. */ | |
6378 | bool | |
6379 | ofputil_group_from_string(const char *s, uint32_t *group_idp) | |
6380 | { | |
6381 | if (!strcasecmp(s, "any")) { | |
6382 | *group_idp = OFPG11_ANY; | |
6383 | } else if (!strcasecmp(s, "all")) { | |
6384 | *group_idp = OFPG11_ALL; | |
6385 | } else if (!str_to_uint(s, 10, group_idp)) { | |
6386 | VLOG_WARN("%s is not a valid group ID. (Valid group IDs are " | |
6387 | "32-bit nonnegative integers or the keywords ANY or " | |
6388 | "ALL.)", s); | |
6389 | return false; | |
6390 | } | |
6391 | ||
6392 | return true; | |
6393 | } | |
6394 | ||
6395 | /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'. | |
6396 | * Most groups' string representation is just the number, but for special | |
6397 | * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */ | |
6398 | void | |
6399 | ofputil_format_group(uint32_t group_id, struct ds *s) | |
6400 | { | |
6401 | char name[MAX_GROUP_NAME_LEN]; | |
6402 | ||
6403 | ofputil_group_to_string(group_id, name, sizeof name); | |
6404 | ds_put_cstr(s, name); | |
6405 | } | |
6406 | ||
6407 | ||
6408 | /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string | |
6409 | * representation of OpenFlow group ID 'group_id'. Most group are represented | |
6410 | * as just their number, but special groups, e.g. OFPG11_ALL, are represented | |
6411 | * by name, e.g. "ALL". */ | |
6412 | void | |
6413 | ofputil_group_to_string(uint32_t group_id, | |
6414 | char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize) | |
6415 | { | |
6416 | switch (group_id) { | |
6417 | case OFPG11_ALL: | |
6418 | ovs_strlcpy(namebuf, "ALL", bufsize); | |
6419 | break; | |
6420 | ||
6421 | case OFPG11_ANY: | |
6422 | ovs_strlcpy(namebuf, "ANY", bufsize); | |
6423 | break; | |
6424 | ||
6425 | default: | |
6426 | snprintf(namebuf, bufsize, "%"PRIu32, group_id); | |
6427 | break; | |
6428 | } | |
6429 | } | |
6430 | ||
2be393ed JP |
6431 | /* Given a buffer 'b' that contains an array of OpenFlow ports of type |
6432 | * 'ofp_version', tries to pull the first element from the array. If | |
6433 | * successful, initializes '*pp' with an abstract representation of the | |
6434 | * port and returns 0. If no ports remain to be decoded, returns EOF. | |
6435 | * On an error, returns a positive OFPERR_* value. */ | |
6436 | int | |
2e3fa633 | 6437 | ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b, |
2be393ed JP |
6438 | struct ofputil_phy_port *pp) |
6439 | { | |
8c3cc785 BP |
6440 | memset(pp, 0, sizeof *pp); |
6441 | ||
2e3fa633 SH |
6442 | switch (ofp_version) { |
6443 | case OFP10_VERSION: { | |
2be393ed JP |
6444 | const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp); |
6445 | return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF; | |
2e3fa633 SH |
6446 | } |
6447 | case OFP11_VERSION: | |
2e1ae200 JR |
6448 | case OFP12_VERSION: |
6449 | case OFP13_VERSION: { | |
2be393ed JP |
6450 | const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op); |
6451 | return op ? ofputil_decode_ofp11_port(pp, op) : EOF; | |
6452 | } | |
c37c0382 | 6453 | case OFP14_VERSION: |
42dccab5 | 6454 | case OFP15_VERSION: |
6fd6ed71 | 6455 | return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF; |
2e3fa633 | 6456 | default: |
428b2edd | 6457 | OVS_NOT_REACHED(); |
2e3fa633 | 6458 | } |
2be393ed JP |
6459 | } |
6460 | ||
3cbd9931 | 6461 | static void |
81a76618 | 6462 | ofputil_normalize_match__(struct match *match, bool may_log) |
b459a924 BP |
6463 | { |
6464 | enum { | |
6465 | MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */ | |
6466 | MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */ | |
6467 | MAY_NW_PROTO = 1 << 2, /* nw_proto */ | |
a61680c6 | 6468 | MAY_IPVx = 1 << 3, /* tos, frag, ttl */ |
b459a924 BP |
6469 | MAY_ARP_SHA = 1 << 4, /* arp_sha */ |
6470 | MAY_ARP_THA = 1 << 5, /* arp_tha */ | |
d78477ec | 6471 | MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */ |
b02475c5 SH |
6472 | MAY_ND_TARGET = 1 << 7, /* nd_target */ |
6473 | MAY_MPLS = 1 << 8, /* mpls label and tc */ | |
b459a924 BP |
6474 | } may_match; |
6475 | ||
6476 | struct flow_wildcards wc; | |
6477 | ||
6478 | /* Figure out what fields may be matched. */ | |
81a76618 | 6479 | if (match->flow.dl_type == htons(ETH_TYPE_IP)) { |
9e44d715 | 6480 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR; |
81a76618 BP |
6481 | if (match->flow.nw_proto == IPPROTO_TCP || |
6482 | match->flow.nw_proto == IPPROTO_UDP || | |
0d56eaf2 | 6483 | match->flow.nw_proto == IPPROTO_SCTP || |
81a76618 | 6484 | match->flow.nw_proto == IPPROTO_ICMP) { |
b459a924 BP |
6485 | may_match |= MAY_TP_ADDR; |
6486 | } | |
81a76618 | 6487 | } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) { |
d78477ec | 6488 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6; |
81a76618 | 6489 | if (match->flow.nw_proto == IPPROTO_TCP || |
0d56eaf2 JS |
6490 | match->flow.nw_proto == IPPROTO_UDP || |
6491 | match->flow.nw_proto == IPPROTO_SCTP) { | |
b459a924 | 6492 | may_match |= MAY_TP_ADDR; |
81a76618 | 6493 | } else if (match->flow.nw_proto == IPPROTO_ICMPV6) { |
b459a924 | 6494 | may_match |= MAY_TP_ADDR; |
81a76618 | 6495 | if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) { |
b459a924 | 6496 | may_match |= MAY_ND_TARGET | MAY_ARP_SHA; |
81a76618 | 6497 | } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) { |
b459a924 BP |
6498 | may_match |= MAY_ND_TARGET | MAY_ARP_THA; |
6499 | } | |
6500 | } | |
8087f5ff MM |
6501 | } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) || |
6502 | match->flow.dl_type == htons(ETH_TYPE_RARP)) { | |
27527aa0 | 6503 | may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA; |
b02475c5 SH |
6504 | } else if (eth_type_mpls(match->flow.dl_type)) { |
6505 | may_match = MAY_MPLS; | |
1c0b7503 | 6506 | } else { |
b459a924 BP |
6507 | may_match = 0; |
6508 | } | |
6509 | ||
6510 | /* Clear the fields that may not be matched. */ | |
81a76618 | 6511 | wc = match->wc; |
b459a924 | 6512 | if (!(may_match & MAY_NW_ADDR)) { |
26720e24 | 6513 | wc.masks.nw_src = wc.masks.nw_dst = htonl(0); |
b459a924 BP |
6514 | } |
6515 | if (!(may_match & MAY_TP_ADDR)) { | |
26720e24 | 6516 | wc.masks.tp_src = wc.masks.tp_dst = htons(0); |
b459a924 BP |
6517 | } |
6518 | if (!(may_match & MAY_NW_PROTO)) { | |
26720e24 | 6519 | wc.masks.nw_proto = 0; |
b459a924 | 6520 | } |
9e44d715 | 6521 | if (!(may_match & MAY_IPVx)) { |
26720e24 BP |
6522 | wc.masks.nw_tos = 0; |
6523 | wc.masks.nw_ttl = 0; | |
b459a924 BP |
6524 | } |
6525 | if (!(may_match & MAY_ARP_SHA)) { | |
26720e24 | 6526 | memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN); |
b459a924 BP |
6527 | } |
6528 | if (!(may_match & MAY_ARP_THA)) { | |
26720e24 | 6529 | memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN); |
b459a924 | 6530 | } |
d78477ec | 6531 | if (!(may_match & MAY_IPV6)) { |
26720e24 BP |
6532 | wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any; |
6533 | wc.masks.ipv6_label = htonl(0); | |
b459a924 BP |
6534 | } |
6535 | if (!(may_match & MAY_ND_TARGET)) { | |
26720e24 | 6536 | wc.masks.nd_target = in6addr_any; |
b459a924 | 6537 | } |
b02475c5 | 6538 | if (!(may_match & MAY_MPLS)) { |
8bfd0fda | 6539 | memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse); |
b02475c5 | 6540 | } |
b459a924 BP |
6541 | |
6542 | /* Log any changes. */ | |
81a76618 | 6543 | if (!flow_wildcards_equal(&wc, &match->wc)) { |
3cbd9931 | 6544 | bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl); |
81a76618 | 6545 | char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL; |
b459a924 | 6546 | |
81a76618 BP |
6547 | match->wc = wc; |
6548 | match_zero_wildcarded_fields(match); | |
b459a924 BP |
6549 | |
6550 | if (log) { | |
81a76618 | 6551 | char *post = match_to_string(match, OFP_DEFAULT_PRIORITY); |
b459a924 BP |
6552 | VLOG_INFO("normalization changed ofp_match, details:"); |
6553 | VLOG_INFO(" pre: %s", pre); | |
6554 | VLOG_INFO("post: %s", post); | |
6555 | free(pre); | |
6556 | free(post); | |
6557 | } | |
fa37b408 | 6558 | } |
3f09c339 | 6559 | } |
26c112c2 | 6560 | |
81a76618 | 6561 | /* "Normalizes" the wildcards in 'match'. That means: |
3cbd9931 BP |
6562 | * |
6563 | * 1. If the type of level N is known, then only the valid fields for that | |
6564 | * level may be specified. For example, ARP does not have a TOS field, | |
81a76618 | 6565 | * so nw_tos must be wildcarded if 'match' specifies an ARP flow. |
3cbd9931 | 6566 | * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and |
81a76618 | 6567 | * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an |
3cbd9931 BP |
6568 | * IPv4 flow. |
6569 | * | |
6570 | * 2. If the type of level N is not known (or not understood by Open | |
6571 | * vSwitch), then no fields at all for that level may be specified. For | |
6572 | * example, Open vSwitch does not understand SCTP, an L4 protocol, so the | |
81a76618 | 6573 | * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an |
3cbd9931 BP |
6574 | * SCTP flow. |
6575 | * | |
81a76618 | 6576 | * If this function changes 'match', it logs a rate-limited informational |
3cbd9931 BP |
6577 | * message. */ |
6578 | void | |
81a76618 | 6579 | ofputil_normalize_match(struct match *match) |
3cbd9931 | 6580 | { |
81a76618 | 6581 | ofputil_normalize_match__(match, true); |
3cbd9931 BP |
6582 | } |
6583 | ||
81a76618 BP |
6584 | /* Same as ofputil_normalize_match() without the logging. Thus, this function |
6585 | * is suitable for a program's internal use, whereas ofputil_normalize_match() | |
3cbd9931 BP |
6586 | * sense for use on flows received from elsewhere (so that a bug in the program |
6587 | * that sent them can be reported and corrected). */ | |
6588 | void | |
81a76618 | 6589 | ofputil_normalize_match_quiet(struct match *match) |
3cbd9931 | 6590 | { |
81a76618 | 6591 | ofputil_normalize_match__(match, false); |
3cbd9931 BP |
6592 | } |
6593 | ||
0ff22822 BP |
6594 | /* Parses a key or a key-value pair from '*stringp'. |
6595 | * | |
6596 | * On success: Stores the key into '*keyp'. Stores the value, if present, into | |
6597 | * '*valuep', otherwise an empty string. Advances '*stringp' past the end of | |
6598 | * the key-value pair, preparing it for another call. '*keyp' and '*valuep' | |
6599 | * are substrings of '*stringp' created by replacing some of its bytes by null | |
6600 | * terminators. Returns true. | |
6601 | * | |
6602 | * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to | |
6603 | * NULL and returns false. */ | |
6604 | bool | |
6605 | ofputil_parse_key_value(char **stringp, char **keyp, char **valuep) | |
6606 | { | |
6607 | char *pos, *key, *value; | |
6608 | size_t key_len; | |
6609 | ||
6610 | pos = *stringp; | |
6611 | pos += strspn(pos, ", \t\r\n"); | |
6612 | if (*pos == '\0') { | |
6613 | *keyp = *valuep = NULL; | |
6614 | return false; | |
6615 | } | |
6616 | ||
6617 | key = pos; | |
6618 | key_len = strcspn(pos, ":=(, \t\r\n"); | |
6619 | if (key[key_len] == ':' || key[key_len] == '=') { | |
6620 | /* The value can be separated by a colon. */ | |
6621 | size_t value_len; | |
6622 | ||
6623 | value = key + key_len + 1; | |
6624 | value_len = strcspn(value, ", \t\r\n"); | |
6625 | pos = value + value_len + (value[value_len] != '\0'); | |
6626 | value[value_len] = '\0'; | |
6627 | } else if (key[key_len] == '(') { | |
6628 | /* The value can be surrounded by balanced parentheses. The outermost | |
6629 | * set of parentheses is removed. */ | |
6630 | int level = 1; | |
6631 | size_t value_len; | |
6632 | ||
6633 | value = key + key_len + 1; | |
6634 | for (value_len = 0; level > 0; value_len++) { | |
6635 | switch (value[value_len]) { | |
6636 | case '\0': | |
33cadc50 BP |
6637 | level = 0; |
6638 | break; | |
0ff22822 BP |
6639 | |
6640 | case '(': | |
6641 | level++; | |
6642 | break; | |
6643 | ||
6644 | case ')': | |
6645 | level--; | |
6646 | break; | |
6647 | } | |
6648 | } | |
6649 | value[value_len - 1] = '\0'; | |
6650 | pos = value + value_len; | |
6651 | } else { | |
6652 | /* There might be no value at all. */ | |
6653 | value = key + key_len; /* Will become the empty string below. */ | |
6654 | pos = key + key_len + (key[key_len] != '\0'); | |
6655 | } | |
6656 | key[key_len] = '\0'; | |
6657 | ||
6658 | *stringp = pos; | |
6659 | *keyp = key; | |
6660 | *valuep = value; | |
6661 | return true; | |
6662 | } | |
f8e4867e SH |
6663 | |
6664 | /* Encode a dump ports request for 'port', the encoded message | |
0746a84f | 6665 | * will be for OpenFlow version 'ofp_version'. Returns message |
f8e4867e SH |
6666 | * as a struct ofpbuf. Returns encoded message on success, NULL on error */ |
6667 | struct ofpbuf * | |
4e022ec0 | 6668 | ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port) |
f8e4867e SH |
6669 | { |
6670 | struct ofpbuf *request; | |
6671 | ||
6672 | switch (ofp_version) { | |
6673 | case OFP10_VERSION: { | |
6674 | struct ofp10_port_stats_request *req; | |
6675 | request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0); | |
6676 | req = ofpbuf_put_zeros(request, sizeof *req); | |
4e022ec0 | 6677 | req->port_no = htons(ofp_to_u16(port)); |
f8e4867e SH |
6678 | break; |
6679 | } | |
6680 | case OFP11_VERSION: | |
2e1ae200 | 6681 | case OFP12_VERSION: |
c37c0382 | 6682 | case OFP13_VERSION: |
42dccab5 BP |
6683 | case OFP14_VERSION: |
6684 | case OFP15_VERSION: { | |
f8e4867e SH |
6685 | struct ofp11_port_stats_request *req; |
6686 | request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0); | |
6687 | req = ofpbuf_put_zeros(request, sizeof *req); | |
6688 | req->port_no = ofputil_port_to_ofp11(port); | |
6689 | break; | |
6690 | } | |
6691 | default: | |
428b2edd | 6692 | OVS_NOT_REACHED(); |
f8e4867e SH |
6693 | } |
6694 | ||
6695 | return request; | |
6696 | } | |
6697 | ||
6698 | static void | |
6699 | ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops, | |
6700 | struct ofp10_port_stats *ps10) | |
6701 | { | |
4e022ec0 | 6702 | ps10->port_no = htons(ofp_to_u16(ops->port_no)); |
f8e4867e SH |
6703 | memset(ps10->pad, 0, sizeof ps10->pad); |
6704 | put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets)); | |
6705 | put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets)); | |
6706 | put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes)); | |
6707 | put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes)); | |
6708 | put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped)); | |
6709 | put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped)); | |
6710 | put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors)); | |
6711 | put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors)); | |
6712 | put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors)); | |
6713 | put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors)); | |
6714 | put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors)); | |
6715 | put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions)); | |
6716 | } | |
6717 | ||
6718 | static void | |
6719 | ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops, | |
6720 | struct ofp11_port_stats *ps11) | |
6721 | { | |
6722 | ps11->port_no = ofputil_port_to_ofp11(ops->port_no); | |
6723 | memset(ps11->pad, 0, sizeof ps11->pad); | |
6724 | ps11->rx_packets = htonll(ops->stats.rx_packets); | |
6725 | ps11->tx_packets = htonll(ops->stats.tx_packets); | |
6726 | ps11->rx_bytes = htonll(ops->stats.rx_bytes); | |
6727 | ps11->tx_bytes = htonll(ops->stats.tx_bytes); | |
6728 | ps11->rx_dropped = htonll(ops->stats.rx_dropped); | |
6729 | ps11->tx_dropped = htonll(ops->stats.tx_dropped); | |
6730 | ps11->rx_errors = htonll(ops->stats.rx_errors); | |
6731 | ps11->tx_errors = htonll(ops->stats.tx_errors); | |
6732 | ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors); | |
6733 | ps11->rx_over_err = htonll(ops->stats.rx_over_errors); | |
6734 | ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors); | |
6735 | ps11->collisions = htonll(ops->stats.collisions); | |
6736 | } | |
6737 | ||
2e1ae200 JR |
6738 | static void |
6739 | ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops, | |
6740 | struct ofp13_port_stats *ps13) | |
6741 | { | |
6742 | ofputil_port_stats_to_ofp11(ops, &ps13->ps); | |
65e0be10 BP |
6743 | ps13->duration_sec = htonl(ops->duration_sec); |
6744 | ps13->duration_nsec = htonl(ops->duration_nsec); | |
2e1ae200 JR |
6745 | } |
6746 | ||
5469537b BP |
6747 | static void |
6748 | ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops, | |
ca6ba700 | 6749 | struct ovs_list *replies) |
5469537b BP |
6750 | { |
6751 | struct ofp14_port_stats_prop_ethernet *eth; | |
6752 | struct ofp14_port_stats *ps14; | |
6753 | struct ofpbuf *reply; | |
6754 | ||
6755 | reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth); | |
6756 | ||
6757 | ps14 = ofpbuf_put_uninit(reply, sizeof *ps14); | |
6758 | ps14->length = htons(sizeof *ps14 + sizeof *eth); | |
6759 | memset(ps14->pad, 0, sizeof ps14->pad); | |
6760 | ps14->port_no = ofputil_port_to_ofp11(ops->port_no); | |
6761 | ps14->duration_sec = htonl(ops->duration_sec); | |
6762 | ps14->duration_nsec = htonl(ops->duration_nsec); | |
6763 | ps14->rx_packets = htonll(ops->stats.rx_packets); | |
6764 | ps14->tx_packets = htonll(ops->stats.tx_packets); | |
6765 | ps14->rx_bytes = htonll(ops->stats.rx_bytes); | |
6766 | ps14->tx_bytes = htonll(ops->stats.tx_bytes); | |
6767 | ps14->rx_dropped = htonll(ops->stats.rx_dropped); | |
6768 | ps14->tx_dropped = htonll(ops->stats.tx_dropped); | |
6769 | ps14->rx_errors = htonll(ops->stats.rx_errors); | |
6770 | ps14->tx_errors = htonll(ops->stats.tx_errors); | |
6771 | ||
6772 | eth = ofpbuf_put_uninit(reply, sizeof *eth); | |
6773 | eth->type = htons(OFPPSPT14_ETHERNET); | |
6774 | eth->length = htons(sizeof *eth); | |
6775 | memset(eth->pad, 0, sizeof eth->pad); | |
6776 | eth->rx_frame_err = htonll(ops->stats.rx_frame_errors); | |
6777 | eth->rx_over_err = htonll(ops->stats.rx_over_errors); | |
6778 | eth->rx_crc_err = htonll(ops->stats.rx_crc_errors); | |
6779 | eth->collisions = htonll(ops->stats.collisions); | |
6780 | } | |
2e1ae200 | 6781 | |
ad4c35fe | 6782 | /* Encode a ports stat for 'ops' and append it to 'replies'. */ |
f8e4867e | 6783 | void |
ca6ba700 | 6784 | ofputil_append_port_stat(struct ovs_list *replies, |
f8e4867e SH |
6785 | const struct ofputil_port_stats *ops) |
6786 | { | |
e28ac5cf | 6787 | switch (ofpmp_version(replies)) { |
2e1ae200 JR |
6788 | case OFP13_VERSION: { |
6789 | struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
6790 | ofputil_port_stats_to_ofp13(ops, reply); | |
6791 | break; | |
6792 | } | |
f8e4867e SH |
6793 | case OFP12_VERSION: |
6794 | case OFP11_VERSION: { | |
6795 | struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
6796 | ofputil_port_stats_to_ofp11(ops, reply); | |
6797 | break; | |
6798 | } | |
6799 | ||
6800 | case OFP10_VERSION: { | |
6801 | struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
6802 | ofputil_port_stats_to_ofp10(ops, reply); | |
6803 | break; | |
6804 | } | |
6805 | ||
c37c0382 | 6806 | case OFP14_VERSION: |
42dccab5 | 6807 | case OFP15_VERSION: |
5469537b | 6808 | ofputil_append_ofp14_port_stats(ops, replies); |
c37c0382 AC |
6809 | break; |
6810 | ||
f8e4867e | 6811 | default: |
428b2edd | 6812 | OVS_NOT_REACHED(); |
f8e4867e SH |
6813 | } |
6814 | } | |
6815 | ||
6816 | static enum ofperr | |
6817 | ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops, | |
6818 | const struct ofp10_port_stats *ps10) | |
6819 | { | |
6820 | memset(ops, 0, sizeof *ops); | |
6821 | ||
4e022ec0 | 6822 | ops->port_no = u16_to_ofp(ntohs(ps10->port_no)); |
f8e4867e SH |
6823 | ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets)); |
6824 | ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets)); | |
6825 | ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes)); | |
6826 | ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes)); | |
6827 | ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped)); | |
6828 | ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped)); | |
6829 | ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors)); | |
6830 | ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors)); | |
6831 | ops->stats.rx_frame_errors = | |
6832 | ntohll(get_32aligned_be64(&ps10->rx_frame_err)); | |
6833 | ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err)); | |
6834 | ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err)); | |
6835 | ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions)); | |
65e0be10 | 6836 | ops->duration_sec = ops->duration_nsec = UINT32_MAX; |
f8e4867e SH |
6837 | |
6838 | return 0; | |
6839 | } | |
6840 | ||
6841 | static enum ofperr | |
6842 | ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops, | |
6843 | const struct ofp11_port_stats *ps11) | |
6844 | { | |
6845 | enum ofperr error; | |
6846 | ||
6847 | memset(ops, 0, sizeof *ops); | |
6848 | error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no); | |
6849 | if (error) { | |
6850 | return error; | |
6851 | } | |
6852 | ||
6853 | ops->stats.rx_packets = ntohll(ps11->rx_packets); | |
6854 | ops->stats.tx_packets = ntohll(ps11->tx_packets); | |
6855 | ops->stats.rx_bytes = ntohll(ps11->rx_bytes); | |
6856 | ops->stats.tx_bytes = ntohll(ps11->tx_bytes); | |
6857 | ops->stats.rx_dropped = ntohll(ps11->rx_dropped); | |
6858 | ops->stats.tx_dropped = ntohll(ps11->tx_dropped); | |
6859 | ops->stats.rx_errors = ntohll(ps11->rx_errors); | |
6860 | ops->stats.tx_errors = ntohll(ps11->tx_errors); | |
6861 | ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err); | |
6862 | ops->stats.rx_over_errors = ntohll(ps11->rx_over_err); | |
6863 | ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err); | |
6864 | ops->stats.collisions = ntohll(ps11->collisions); | |
65e0be10 | 6865 | ops->duration_sec = ops->duration_nsec = UINT32_MAX; |
f8e4867e SH |
6866 | |
6867 | return 0; | |
6868 | } | |
6869 | ||
2e1ae200 JR |
6870 | static enum ofperr |
6871 | ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops, | |
6872 | const struct ofp13_port_stats *ps13) | |
6873 | { | |
65e0be10 | 6874 | enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps); |
2e1ae200 | 6875 | if (!error) { |
65e0be10 BP |
6876 | ops->duration_sec = ntohl(ps13->duration_sec); |
6877 | ops->duration_nsec = ntohl(ps13->duration_nsec); | |
2e1ae200 | 6878 | } |
2e1ae200 JR |
6879 | return error; |
6880 | } | |
6881 | ||
5469537b BP |
6882 | static enum ofperr |
6883 | parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload, | |
6884 | struct ofputil_port_stats *ops) | |
be0c30df | 6885 | { |
6fd6ed71 | 6886 | const struct ofp14_port_stats_prop_ethernet *eth = payload->data; |
5469537b | 6887 | |
6fd6ed71 | 6888 | if (payload->size != sizeof *eth) { |
5469537b BP |
6889 | return OFPERR_OFPBPC_BAD_LEN; |
6890 | } | |
6891 | ||
6892 | ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err); | |
6893 | ops->stats.rx_over_errors = ntohll(eth->rx_over_err); | |
6894 | ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err); | |
6895 | ops->stats.collisions = ntohll(eth->collisions); | |
6896 | ||
6897 | return 0; | |
6898 | } | |
6899 | ||
6900 | static enum ofperr | |
6901 | ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops, | |
6902 | struct ofpbuf *msg) | |
6903 | { | |
6904 | const struct ofp14_port_stats *ps14; | |
6905 | struct ofpbuf properties; | |
6906 | enum ofperr error; | |
6907 | size_t len; | |
6908 | ||
6909 | ps14 = ofpbuf_try_pull(msg, sizeof *ps14); | |
6910 | if (!ps14) { | |
6911 | return OFPERR_OFPBRC_BAD_LEN; | |
6912 | } | |
6913 | ||
6914 | len = ntohs(ps14->length); | |
6fd6ed71 | 6915 | if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) { |
5469537b | 6916 | return OFPERR_OFPBRC_BAD_LEN; |
be0c30df | 6917 | } |
5469537b BP |
6918 | len -= sizeof *ps14; |
6919 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len); | |
6920 | ||
6921 | error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no); | |
6922 | if (error) { | |
6923 | return error; | |
6924 | } | |
6925 | ||
6926 | ops->duration_sec = ntohl(ps14->duration_sec); | |
6927 | ops->duration_nsec = ntohl(ps14->duration_nsec); | |
6928 | ops->stats.rx_packets = ntohll(ps14->rx_packets); | |
6929 | ops->stats.tx_packets = ntohll(ps14->tx_packets); | |
6930 | ops->stats.rx_bytes = ntohll(ps14->rx_bytes); | |
6931 | ops->stats.tx_bytes = ntohll(ps14->tx_bytes); | |
6932 | ops->stats.rx_dropped = ntohll(ps14->rx_dropped); | |
6933 | ops->stats.tx_dropped = ntohll(ps14->tx_dropped); | |
6934 | ops->stats.rx_errors = ntohll(ps14->rx_errors); | |
6935 | ops->stats.tx_errors = ntohll(ps14->tx_errors); | |
6936 | ops->stats.rx_frame_errors = UINT64_MAX; | |
6937 | ops->stats.rx_over_errors = UINT64_MAX; | |
6938 | ops->stats.rx_crc_errors = UINT64_MAX; | |
6939 | ops->stats.collisions = UINT64_MAX; | |
6940 | ||
6fd6ed71 | 6941 | while (properties.size > 0) { |
5469537b BP |
6942 | struct ofpbuf payload; |
6943 | enum ofperr error; | |
6944 | uint16_t type; | |
6945 | ||
6946 | error = ofputil_pull_property(&properties, &payload, &type); | |
6947 | if (error) { | |
6948 | return error; | |
6949 | } | |
6950 | ||
6951 | switch (type) { | |
6952 | case OFPPSPT14_ETHERNET: | |
6953 | error = parse_ofp14_port_stats_ethernet_property(&payload, ops); | |
6954 | break; | |
6955 | ||
6956 | default: | |
6957 | log_property(true, "unknown port stats property %"PRIu16, type); | |
6958 | error = 0; | |
6959 | break; | |
6960 | } | |
6961 | ||
6962 | if (error) { | |
6963 | return error; | |
6964 | } | |
6965 | } | |
6966 | ||
6967 | return 0; | |
be0c30df | 6968 | } |
2e1ae200 | 6969 | |
f8e4867e SH |
6970 | /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY |
6971 | * message 'oh'. */ | |
6972 | size_t | |
6973 | ofputil_count_port_stats(const struct ofp_header *oh) | |
6974 | { | |
5469537b | 6975 | struct ofputil_port_stats ps; |
f8e4867e | 6976 | struct ofpbuf b; |
5469537b | 6977 | size_t n = 0; |
f8e4867e SH |
6978 | |
6979 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
6980 | ofpraw_pull_assert(&b); | |
5469537b BP |
6981 | while (!ofputil_decode_port_stats(&ps, &b)) { |
6982 | n++; | |
6983 | } | |
6984 | return n; | |
f8e4867e SH |
6985 | } |
6986 | ||
6987 | /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract | |
6988 | * ofputil_port_stats in 'ps'. | |
6989 | * | |
6990 | * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow | |
6991 | * message. Calling this function multiple times for a single 'msg' iterates | |
6992 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
6993 | * null and not modify them between calls. | |
6994 | * | |
6995 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
6996 | * otherwise a positive errno value. */ | |
6997 | int | |
6998 | ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg) | |
6999 | { | |
7000 | enum ofperr error; | |
7001 | enum ofpraw raw; | |
7002 | ||
6fd6ed71 | 7003 | error = (msg->header ? ofpraw_decode(&raw, msg->header) |
f8e4867e SH |
7004 | : ofpraw_pull(&raw, msg)); |
7005 | if (error) { | |
7006 | return error; | |
7007 | } | |
7008 | ||
6fd6ed71 | 7009 | if (!msg->size) { |
f8e4867e | 7010 | return EOF; |
5469537b BP |
7011 | } else if (raw == OFPRAW_OFPST14_PORT_REPLY) { |
7012 | return ofputil_pull_ofp14_port_stats(ps, msg); | |
2e1ae200 JR |
7013 | } else if (raw == OFPRAW_OFPST13_PORT_REPLY) { |
7014 | const struct ofp13_port_stats *ps13; | |
7015 | ||
7016 | ps13 = ofpbuf_try_pull(msg, sizeof *ps13); | |
7017 | if (!ps13) { | |
7018 | goto bad_len; | |
7019 | } | |
7020 | return ofputil_port_stats_from_ofp13(ps, ps13); | |
f8e4867e SH |
7021 | } else if (raw == OFPRAW_OFPST11_PORT_REPLY) { |
7022 | const struct ofp11_port_stats *ps11; | |
7023 | ||
7024 | ps11 = ofpbuf_try_pull(msg, sizeof *ps11); | |
7025 | if (!ps11) { | |
2e1ae200 | 7026 | goto bad_len; |
f8e4867e SH |
7027 | } |
7028 | return ofputil_port_stats_from_ofp11(ps, ps11); | |
7029 | } else if (raw == OFPRAW_OFPST10_PORT_REPLY) { | |
7030 | const struct ofp10_port_stats *ps10; | |
7031 | ||
7032 | ps10 = ofpbuf_try_pull(msg, sizeof *ps10); | |
7033 | if (!ps10) { | |
2e1ae200 | 7034 | goto bad_len; |
f8e4867e SH |
7035 | } |
7036 | return ofputil_port_stats_from_ofp10(ps, ps10); | |
7037 | } else { | |
428b2edd | 7038 | OVS_NOT_REACHED(); |
f8e4867e SH |
7039 | } |
7040 | ||
2e1ae200 | 7041 | bad_len: |
437d0d22 | 7042 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover " |
6fd6ed71 | 7043 | "bytes at end", msg->size); |
2e1ae200 | 7044 | return OFPERR_OFPBRC_BAD_LEN; |
f8e4867e SH |
7045 | } |
7046 | ||
7047 | /* Parse a port status request message into a 16 bit OpenFlow 1.0 | |
7048 | * port number and stores the latter in '*ofp10_port'. | |
7049 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
7050 | enum ofperr | |
7051 | ofputil_decode_port_stats_request(const struct ofp_header *request, | |
4e022ec0 | 7052 | ofp_port_t *ofp10_port) |
f8e4867e SH |
7053 | { |
7054 | switch ((enum ofp_version)request->version) { | |
42dccab5 | 7055 | case OFP15_VERSION: |
5469537b | 7056 | case OFP14_VERSION: |
2e1ae200 | 7057 | case OFP13_VERSION: |
f8e4867e SH |
7058 | case OFP12_VERSION: |
7059 | case OFP11_VERSION: { | |
7060 | const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request); | |
7061 | return ofputil_port_from_ofp11(psr11->port_no, ofp10_port); | |
7062 | } | |
7063 | ||
7064 | case OFP10_VERSION: { | |
7065 | const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request); | |
4e022ec0 | 7066 | *ofp10_port = u16_to_ofp(ntohs(psr10->port_no)); |
f8e4867e SH |
7067 | return 0; |
7068 | } | |
7069 | ||
7070 | default: | |
428b2edd | 7071 | OVS_NOT_REACHED(); |
f8e4867e SH |
7072 | } |
7073 | } | |
64626975 | 7074 | |
7395c052 NZ |
7075 | /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */ |
7076 | void | |
ca6ba700 | 7077 | ofputil_bucket_list_destroy(struct ovs_list *buckets) |
7395c052 | 7078 | { |
5f03c983 | 7079 | struct ofputil_bucket *bucket; |
7395c052 | 7080 | |
5f03c983 | 7081 | LIST_FOR_EACH_POP (bucket, list_node, buckets) { |
7395c052 NZ |
7082 | free(bucket->ofpacts); |
7083 | free(bucket); | |
7084 | } | |
7085 | } | |
7086 | ||
103b4866 SH |
7087 | /* Clones 'bucket' and its ofpacts data */ |
7088 | static struct ofputil_bucket * | |
7089 | ofputil_bucket_clone_data(const struct ofputil_bucket *bucket) | |
7090 | { | |
7091 | struct ofputil_bucket *new; | |
7092 | ||
7093 | new = xmemdup(bucket, sizeof *bucket); | |
7094 | new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len); | |
7095 | ||
7096 | return new; | |
7097 | } | |
7098 | ||
7099 | /* Clones each of the buckets in the list 'src' appending them | |
7100 | * in turn to 'dest' which should be an initialised list. | |
7101 | * An exception is that if the pointer value of a bucket in 'src' | |
7102 | * matches 'skip' then it is not cloned or appended to 'dest'. | |
7103 | * This allows all of 'src' or 'all of 'src' except 'skip' to | |
7104 | * be cloned and appended to 'dest'. */ | |
7105 | void | |
ca6ba700 | 7106 | ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src, |
103b4866 SH |
7107 | const struct ofputil_bucket *skip) |
7108 | { | |
7109 | struct ofputil_bucket *bucket; | |
7110 | ||
7111 | LIST_FOR_EACH (bucket, list_node, src) { | |
7112 | struct ofputil_bucket *new_bucket; | |
7113 | ||
7114 | if (bucket == skip) { | |
7115 | continue; | |
7116 | } | |
7117 | ||
7118 | new_bucket = ofputil_bucket_clone_data(bucket); | |
7119 | list_push_back(dest, &new_bucket->list_node); | |
7120 | } | |
7121 | } | |
7122 | ||
7123 | /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id' | |
7124 | * Returns the first bucket found or NULL if no buckets are found. */ | |
7125 | struct ofputil_bucket * | |
ca6ba700 | 7126 | ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id) |
103b4866 SH |
7127 | { |
7128 | struct ofputil_bucket *bucket; | |
7129 | ||
7130 | if (bucket_id > OFPG15_BUCKET_MAX) { | |
7131 | return NULL; | |
7132 | } | |
7133 | ||
7134 | LIST_FOR_EACH (bucket, list_node, buckets) { | |
7135 | if (bucket->bucket_id == bucket_id) { | |
7136 | return bucket; | |
7137 | } | |
7138 | } | |
7139 | ||
7140 | return NULL; | |
7141 | } | |
7142 | ||
7143 | /* Returns true if more than one bucket in the list 'buckets' | |
7144 | * have the same bucket id. Returns false otherwise. */ | |
18ac06d3 | 7145 | bool |
ca6ba700 | 7146 | ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets) |
18ac06d3 SH |
7147 | { |
7148 | struct ofputil_bucket *i, *j; | |
7149 | ||
7150 | LIST_FOR_EACH (i, list_node, buckets) { | |
7151 | LIST_FOR_EACH_REVERSE (j, list_node, buckets) { | |
7152 | if (i == j) { | |
7153 | break; | |
7154 | } | |
7155 | if (i->bucket_id == j->bucket_id) { | |
7156 | return true; | |
7157 | } | |
7158 | } | |
7159 | } | |
7160 | ||
7161 | return false; | |
7162 | } | |
7163 | ||
103b4866 SH |
7164 | /* Returns the bucket at the front of the list 'buckets'. |
7165 | * Undefined if 'buckets is empty. */ | |
7166 | struct ofputil_bucket * | |
ca6ba700 | 7167 | ofputil_bucket_list_front(const struct ovs_list *buckets) |
103b4866 SH |
7168 | { |
7169 | static struct ofputil_bucket *bucket; | |
7170 | ||
7171 | ASSIGN_CONTAINER(bucket, list_front(buckets), list_node); | |
7172 | ||
7173 | return bucket; | |
7174 | } | |
7175 | ||
7176 | /* Returns the bucket at the back of the list 'buckets'. | |
7177 | * Undefined if 'buckets is empty. */ | |
7178 | struct ofputil_bucket * | |
ca6ba700 | 7179 | ofputil_bucket_list_back(const struct ovs_list *buckets) |
103b4866 SH |
7180 | { |
7181 | static struct ofputil_bucket *bucket; | |
7182 | ||
7183 | ASSIGN_CONTAINER(bucket, list_back(buckets), list_node); | |
7184 | ||
7185 | return bucket; | |
7186 | } | |
7187 | ||
7395c052 NZ |
7188 | /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version', |
7189 | * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats | |
7190 | * for all groups.) | |
7191 | * | |
7192 | * Group statistics include packet and byte counts for each group. */ | |
7193 | struct ofpbuf * | |
7194 | ofputil_encode_group_stats_request(enum ofp_version ofp_version, | |
7195 | uint32_t group_id) | |
7196 | { | |
7197 | struct ofpbuf *request; | |
7198 | ||
7199 | switch (ofp_version) { | |
7200 | case OFP10_VERSION: | |
7201 | ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later " | |
7202 | "(\'-O OpenFlow11\')"); | |
7203 | case OFP11_VERSION: | |
7204 | case OFP12_VERSION: | |
c37c0382 | 7205 | case OFP13_VERSION: |
42dccab5 BP |
7206 | case OFP14_VERSION: |
7207 | case OFP15_VERSION: { | |
7395c052 NZ |
7208 | struct ofp11_group_stats_request *req; |
7209 | request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0); | |
7210 | req = ofpbuf_put_zeros(request, sizeof *req); | |
7211 | req->group_id = htonl(group_id); | |
7212 | break; | |
7213 | } | |
7214 | default: | |
428b2edd | 7215 | OVS_NOT_REACHED(); |
7395c052 NZ |
7216 | } |
7217 | ||
7218 | return request; | |
7219 | } | |
7220 | ||
bc65c25a SH |
7221 | void |
7222 | ofputil_uninit_group_desc(struct ofputil_group_desc *gd) | |
7223 | { | |
7224 | ofputil_bucket_list_destroy(&gd->buckets); | |
7225 | free(&gd->props.fields); | |
7226 | } | |
7227 | ||
19187a71 BP |
7228 | /* Decodes the OpenFlow group description request in 'oh', returning the group |
7229 | * whose description is requested, or OFPG_ALL if stats for all groups was | |
7230 | * requested. */ | |
7231 | uint32_t | |
7232 | ofputil_decode_group_desc_request(const struct ofp_header *oh) | |
7233 | { | |
7234 | struct ofpbuf request; | |
7235 | enum ofpraw raw; | |
7236 | ||
7237 | ofpbuf_use_const(&request, oh, ntohs(oh->length)); | |
7238 | raw = ofpraw_pull_assert(&request); | |
7239 | if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) { | |
7240 | return OFPG_ALL; | |
7241 | } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) { | |
7242 | ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id); | |
7243 | return ntohl(*group_id); | |
7244 | } else { | |
7245 | OVS_NOT_REACHED(); | |
7246 | } | |
7247 | } | |
7248 | ||
7395c052 | 7249 | /* Returns an OpenFlow group description request for OpenFlow version |
19187a71 BP |
7250 | * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to |
7251 | * request stats for all groups (OpenFlow 1.4 and earlier always request all | |
7252 | * groups). | |
7395c052 NZ |
7253 | * |
7254 | * Group descriptions include the bucket and action configuration for each | |
7255 | * group. */ | |
7256 | struct ofpbuf * | |
19187a71 BP |
7257 | ofputil_encode_group_desc_request(enum ofp_version ofp_version, |
7258 | uint32_t group_id) | |
7395c052 NZ |
7259 | { |
7260 | struct ofpbuf *request; | |
19187a71 | 7261 | ovs_be32 gid; |
7395c052 NZ |
7262 | |
7263 | switch (ofp_version) { | |
7264 | case OFP10_VERSION: | |
7265 | ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later " | |
7266 | "(\'-O OpenFlow11\')"); | |
7267 | case OFP11_VERSION: | |
7268 | case OFP12_VERSION: | |
c37c0382 AC |
7269 | case OFP13_VERSION: |
7270 | case OFP14_VERSION: | |
19187a71 BP |
7271 | request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST, |
7272 | ofp_version, 0); | |
7273 | break; | |
42dccab5 | 7274 | case OFP15_VERSION: |
19187a71 BP |
7275 | request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST, |
7276 | ofp_version, 0); | |
7277 | gid = htonl(group_id); | |
7278 | ofpbuf_put(request, &gid, sizeof gid); | |
7395c052 | 7279 | break; |
7395c052 | 7280 | default: |
428b2edd | 7281 | OVS_NOT_REACHED(); |
7395c052 NZ |
7282 | } |
7283 | ||
7284 | return request; | |
7285 | } | |
7286 | ||
dcbe78ad | 7287 | static void |
63759e71 AZ |
7288 | ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs, |
7289 | struct ofp11_bucket_counter bucket_cnts[]) | |
7395c052 | 7290 | { |
7395c052 NZ |
7291 | int i; |
7292 | ||
dcbe78ad AZ |
7293 | for (i = 0; i < gs->n_buckets; i++) { |
7294 | bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count); | |
7295 | bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count); | |
7395c052 | 7296 | } |
7395c052 NZ |
7297 | } |
7298 | ||
7299 | static void | |
dcbe78ad | 7300 | ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs, |
63759e71 AZ |
7301 | struct ofp11_group_stats *gs11, size_t length, |
7302 | struct ofp11_bucket_counter bucket_cnts[]) | |
7395c052 | 7303 | { |
63759e71 AZ |
7304 | memset(gs11, 0, sizeof *gs11); |
7305 | gs11->length = htons(length); | |
7306 | gs11->group_id = htonl(gs->group_id); | |
7307 | gs11->ref_count = htonl(gs->ref_count); | |
7308 | gs11->packet_count = htonll(gs->packet_count); | |
7309 | gs11->byte_count = htonll(gs->byte_count); | |
7310 | ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts); | |
dcbe78ad | 7311 | } |
7395c052 | 7312 | |
dcbe78ad AZ |
7313 | static void |
7314 | ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs, | |
63759e71 AZ |
7315 | struct ofp13_group_stats *gs13, size_t length, |
7316 | struct ofp11_bucket_counter bucket_cnts[]) | |
dcbe78ad | 7317 | { |
63759e71 | 7318 | ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts); |
dcbe78ad AZ |
7319 | gs13->duration_sec = htonl(gs->duration_sec); |
7320 | gs13->duration_nsec = htonl(gs->duration_nsec); | |
63759e71 | 7321 | |
7395c052 NZ |
7322 | } |
7323 | ||
dcbe78ad | 7324 | /* Encodes 'gs' properly for the format of the list of group statistics |
7395c052 NZ |
7325 | * replies already begun in 'replies' and appends it to the list. 'replies' |
7326 | * must have originally been initialized with ofpmp_init(). */ | |
7327 | void | |
ca6ba700 | 7328 | ofputil_append_group_stats(struct ovs_list *replies, |
dcbe78ad | 7329 | const struct ofputil_group_stats *gs) |
7395c052 | 7330 | { |
63759e71 AZ |
7331 | size_t bucket_counter_size; |
7332 | struct ofp11_bucket_counter *bucket_counters; | |
dcbe78ad | 7333 | size_t length; |
7395c052 | 7334 | |
63759e71 AZ |
7335 | bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter); |
7336 | ||
e28ac5cf | 7337 | switch (ofpmp_version(replies)) { |
7395c052 | 7338 | case OFP11_VERSION: |
dcbe78ad | 7339 | case OFP12_VERSION:{ |
63759e71 | 7340 | struct ofp11_group_stats *gs11; |
dcbe78ad | 7341 | |
63759e71 AZ |
7342 | length = sizeof *gs11 + bucket_counter_size; |
7343 | gs11 = ofpmp_append(replies, length); | |
7344 | bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1); | |
7345 | ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters); | |
dcbe78ad AZ |
7346 | break; |
7347 | } | |
7395c052 NZ |
7348 | |
7349 | case OFP13_VERSION: | |
42dccab5 BP |
7350 | case OFP14_VERSION: |
7351 | case OFP15_VERSION: { | |
63759e71 | 7352 | struct ofp13_group_stats *gs13; |
7395c052 | 7353 | |
63759e71 AZ |
7354 | length = sizeof *gs13 + bucket_counter_size; |
7355 | gs13 = ofpmp_append(replies, length); | |
7356 | bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1); | |
7357 | ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters); | |
dcbe78ad AZ |
7358 | break; |
7359 | } | |
c37c0382 | 7360 | |
7395c052 NZ |
7361 | case OFP10_VERSION: |
7362 | default: | |
428b2edd | 7363 | OVS_NOT_REACHED(); |
7395c052 NZ |
7364 | } |
7365 | } | |
7395c052 NZ |
7366 | /* Returns an OpenFlow group features request for OpenFlow version |
7367 | * 'ofp_version'. */ | |
7368 | struct ofpbuf * | |
7369 | ofputil_encode_group_features_request(enum ofp_version ofp_version) | |
7370 | { | |
7371 | struct ofpbuf *request = NULL; | |
7372 | ||
7373 | switch (ofp_version) { | |
7374 | case OFP10_VERSION: | |
7375 | case OFP11_VERSION: | |
7376 | ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later " | |
7377 | "(\'-O OpenFlow12\')"); | |
7378 | case OFP12_VERSION: | |
c37c0382 AC |
7379 | case OFP13_VERSION: |
7380 | case OFP14_VERSION: | |
42dccab5 | 7381 | case OFP15_VERSION: |
7395c052 | 7382 | request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST, |
c37c0382 | 7383 | ofp_version, 0); |
7395c052 | 7384 | break; |
7395c052 | 7385 | default: |
428b2edd | 7386 | OVS_NOT_REACHED(); |
7395c052 NZ |
7387 | } |
7388 | ||
7389 | return request; | |
7390 | } | |
7391 | ||
7392 | /* Returns a OpenFlow message that encodes 'features' properly as a reply to | |
7393 | * group features request 'request'. */ | |
7394 | struct ofpbuf * | |
7395 | ofputil_encode_group_features_reply( | |
7396 | const struct ofputil_group_features *features, | |
7397 | const struct ofp_header *request) | |
7398 | { | |
7399 | struct ofp12_group_features_stats *ogf; | |
7400 | struct ofpbuf *reply; | |
08d1e234 | 7401 | int i; |
7395c052 NZ |
7402 | |
7403 | reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY, | |
7404 | request->version, request->xid, 0); | |
7405 | ogf = ofpbuf_put_zeros(reply, sizeof *ogf); | |
7406 | ogf->types = htonl(features->types); | |
7407 | ogf->capabilities = htonl(features->capabilities); | |
08d1e234 BP |
7408 | for (i = 0; i < OFPGT12_N_TYPES; i++) { |
7409 | ogf->max_groups[i] = htonl(features->max_groups[i]); | |
7410 | ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i], | |
7411 | request->version); | |
7412 | } | |
7395c052 NZ |
7413 | |
7414 | return reply; | |
7415 | } | |
7416 | ||
7417 | /* Decodes group features reply 'oh' into 'features'. */ | |
7418 | void | |
7419 | ofputil_decode_group_features_reply(const struct ofp_header *oh, | |
7420 | struct ofputil_group_features *features) | |
7421 | { | |
7422 | const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh); | |
08d1e234 | 7423 | int i; |
7395c052 NZ |
7424 | |
7425 | features->types = ntohl(ogf->types); | |
7426 | features->capabilities = ntohl(ogf->capabilities); | |
08d1e234 BP |
7427 | for (i = 0; i < OFPGT12_N_TYPES; i++) { |
7428 | features->max_groups[i] = ntohl(ogf->max_groups[i]); | |
7429 | features->ofpacts[i] = ofpact_bitmap_from_openflow( | |
7430 | ogf->actions[i], oh->version); | |
7431 | } | |
7395c052 NZ |
7432 | } |
7433 | ||
7434 | /* Parse a group status request message into a 32 bit OpenFlow 1.1 | |
7435 | * group ID and stores the latter in '*group_id'. | |
7436 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
7437 | enum ofperr | |
7438 | ofputil_decode_group_stats_request(const struct ofp_header *request, | |
7439 | uint32_t *group_id) | |
7440 | { | |
7441 | const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request); | |
7442 | *group_id = ntohl(gsr11->group_id); | |
7443 | return 0; | |
7444 | } | |
7445 | ||
7446 | /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats | |
646b2a9c SH |
7447 | * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the |
7448 | * caller to eventually free. | |
7395c052 NZ |
7449 | * |
7450 | * Multiple group stats replies can be packed into a single OpenFlow message. | |
7451 | * Calling this function multiple times for a single 'msg' iterates through the | |
7452 | * replies. The caller must initially leave 'msg''s layer pointers null and | |
7453 | * not modify them between calls. | |
7454 | * | |
7455 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
7456 | * otherwise a positive errno value. */ | |
7457 | int | |
7458 | ofputil_decode_group_stats_reply(struct ofpbuf *msg, | |
7459 | struct ofputil_group_stats *gs) | |
7460 | { | |
7461 | struct ofp11_bucket_counter *obc; | |
7462 | struct ofp11_group_stats *ogs11; | |
7463 | enum ofpraw raw; | |
7464 | enum ofperr error; | |
7465 | size_t base_len; | |
7466 | size_t length; | |
7467 | size_t i; | |
7468 | ||
646b2a9c | 7469 | gs->bucket_stats = NULL; |
6fd6ed71 | 7470 | error = (msg->header ? ofpraw_decode(&raw, msg->header) |
7395c052 NZ |
7471 | : ofpraw_pull(&raw, msg)); |
7472 | if (error) { | |
7473 | return error; | |
7474 | } | |
7475 | ||
6fd6ed71 | 7476 | if (!msg->size) { |
7395c052 NZ |
7477 | return EOF; |
7478 | } | |
7479 | ||
7480 | if (raw == OFPRAW_OFPST11_GROUP_REPLY) { | |
7481 | base_len = sizeof *ogs11; | |
7482 | ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11); | |
7483 | gs->duration_sec = gs->duration_nsec = UINT32_MAX; | |
7484 | } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) { | |
7485 | struct ofp13_group_stats *ogs13; | |
7486 | ||
7487 | base_len = sizeof *ogs13; | |
7488 | ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13); | |
7489 | if (ogs13) { | |
7490 | ogs11 = &ogs13->gs; | |
7491 | gs->duration_sec = ntohl(ogs13->duration_sec); | |
7492 | gs->duration_nsec = ntohl(ogs13->duration_nsec); | |
7493 | } else { | |
7494 | ogs11 = NULL; | |
7495 | } | |
7496 | } else { | |
428b2edd | 7497 | OVS_NOT_REACHED(); |
7395c052 NZ |
7498 | } |
7499 | ||
7500 | if (!ogs11) { | |
437d0d22 | 7501 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end", |
6fd6ed71 | 7502 | ofpraw_get_name(raw), msg->size); |
7395c052 NZ |
7503 | return OFPERR_OFPBRC_BAD_LEN; |
7504 | } | |
7505 | length = ntohs(ogs11->length); | |
7506 | if (length < sizeof base_len) { | |
34582733 | 7507 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE, |
7395c052 NZ |
7508 | ofpraw_get_name(raw), length); |
7509 | return OFPERR_OFPBRC_BAD_LEN; | |
7510 | } | |
7511 | ||
7512 | gs->group_id = ntohl(ogs11->group_id); | |
7513 | gs->ref_count = ntohl(ogs11->ref_count); | |
7514 | gs->packet_count = ntohll(ogs11->packet_count); | |
7515 | gs->byte_count = ntohll(ogs11->byte_count); | |
7516 | ||
7517 | gs->n_buckets = (length - base_len) / sizeof *obc; | |
7518 | obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc); | |
7519 | if (!obc) { | |
437d0d22 | 7520 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end", |
6fd6ed71 | 7521 | ofpraw_get_name(raw), msg->size); |
7395c052 NZ |
7522 | return OFPERR_OFPBRC_BAD_LEN; |
7523 | } | |
7524 | ||
646b2a9c | 7525 | gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats); |
7395c052 NZ |
7526 | for (i = 0; i < gs->n_buckets; i++) { |
7527 | gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count); | |
7528 | gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count); | |
7529 | } | |
7530 | ||
7531 | return 0; | |
7532 | } | |
7533 | ||
5097fee5 SH |
7534 | static void |
7535 | ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket, | |
7536 | struct ofpbuf *openflow, enum ofp_version ofp_version) | |
7537 | { | |
7538 | struct ofp11_bucket *ob; | |
7539 | size_t start; | |
7540 | ||
6fd6ed71 | 7541 | start = openflow->size; |
5097fee5 SH |
7542 | ofpbuf_put_zeros(openflow, sizeof *ob); |
7543 | ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len, | |
7544 | openflow, ofp_version); | |
7545 | ob = ofpbuf_at_assert(openflow, start, sizeof *ob); | |
6fd6ed71 | 7546 | ob->len = htons(openflow->size - start); |
5097fee5 SH |
7547 | ob->weight = htons(bucket->weight); |
7548 | ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port); | |
7549 | ob->watch_group = htonl(bucket->watch_group); | |
7550 | } | |
7551 | ||
18ac06d3 SH |
7552 | static void |
7553 | ofputil_put_ofp15_group_bucket_prop_weight(ovs_be16 weight, | |
7554 | struct ofpbuf *openflow) | |
7555 | { | |
7556 | size_t start_ofs; | |
7557 | struct ofp15_group_bucket_prop_weight *prop; | |
7558 | ||
7559 | start_ofs = start_property(openflow, OFPGBPT15_WEIGHT); | |
7560 | ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header)); | |
7561 | prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop); | |
7562 | prop->weight = weight; | |
7563 | end_property(openflow, start_ofs); | |
7564 | } | |
7565 | ||
7566 | static void | |
7567 | ofputil_put_ofp15_group_bucket_prop_watch(ovs_be32 watch, uint16_t type, | |
7568 | struct ofpbuf *openflow) | |
7569 | { | |
7570 | size_t start_ofs; | |
7571 | struct ofp15_group_bucket_prop_watch *prop; | |
7572 | ||
7573 | start_ofs = start_property(openflow, type); | |
7574 | ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header)); | |
7575 | prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop); | |
7576 | prop->watch = watch; | |
7577 | end_property(openflow, start_ofs); | |
7578 | } | |
7579 | ||
7580 | static void | |
7581 | ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket, | |
7582 | uint32_t bucket_id, enum ofp11_group_type group_type, | |
7583 | struct ofpbuf *openflow, enum ofp_version ofp_version) | |
7584 | { | |
7585 | struct ofp15_bucket *ob; | |
7586 | size_t start, actions_start, actions_len; | |
7587 | ||
6fd6ed71 | 7588 | start = openflow->size; |
18ac06d3 SH |
7589 | ofpbuf_put_zeros(openflow, sizeof *ob); |
7590 | ||
6fd6ed71 | 7591 | actions_start = openflow->size; |
18ac06d3 SH |
7592 | ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len, |
7593 | openflow, ofp_version); | |
6fd6ed71 | 7594 | actions_len = openflow->size - actions_start; |
18ac06d3 SH |
7595 | |
7596 | if (group_type == OFPGT11_SELECT) { | |
7597 | ofputil_put_ofp15_group_bucket_prop_weight(htons(bucket->weight), | |
7598 | openflow); | |
7599 | } | |
7600 | if (bucket->watch_port != OFPP_ANY) { | |
7601 | ovs_be32 port = ofputil_port_to_ofp11(bucket->watch_port); | |
7602 | ofputil_put_ofp15_group_bucket_prop_watch(port, | |
7603 | OFPGBPT15_WATCH_PORT, | |
7604 | openflow); | |
7605 | } | |
7606 | if (bucket->watch_group != OFPG_ANY) { | |
7607 | ovs_be32 group = htonl(bucket->watch_group); | |
7608 | ofputil_put_ofp15_group_bucket_prop_watch(group, | |
7609 | OFPGBPT15_WATCH_GROUP, | |
7610 | openflow); | |
7611 | } | |
7612 | ||
7613 | ob = ofpbuf_at_assert(openflow, start, sizeof *ob); | |
6fd6ed71 | 7614 | ob->len = htons(openflow->size - start); |
79870963 | 7615 | ob->action_array_len = htons(actions_len); |
18ac06d3 SH |
7616 | ob->bucket_id = htonl(bucket_id); |
7617 | } | |
7618 | ||
53eb84a5 SH |
7619 | static void |
7620 | ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version, | |
7621 | const struct ofputil_group_props *gp, | |
7622 | struct ofpbuf *openflow) | |
7623 | { | |
7624 | struct ntr_group_prop_selection_method *prop; | |
7625 | size_t start; | |
7626 | ||
7627 | start = openflow->size; | |
7628 | ofpbuf_put_zeros(openflow, sizeof *prop); | |
7629 | oxm_put_field_array(openflow, &gp->fields, ofp_version); | |
7630 | prop = ofpbuf_at_assert(openflow, start, sizeof *prop); | |
7631 | prop->type = htons(OFPGPT15_EXPERIMENTER); | |
7632 | prop->experimenter = htonl(NTR_VENDOR_ID); | |
7633 | prop->exp_type = htonl(NTRT_SELECTION_METHOD); | |
7634 | strcpy(prop->selection_method, gp->selection_method); | |
7635 | prop->selection_method_param = htonll(gp->selection_method_param); | |
7636 | end_property(openflow, start); | |
7637 | } | |
7638 | ||
5be50252 SH |
7639 | static void |
7640 | ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds, | |
1667bb34 | 7641 | const struct ovs_list *buckets, |
ca6ba700 | 7642 | struct ovs_list *replies, |
5be50252 | 7643 | enum ofp_version version) |
7395c052 NZ |
7644 | { |
7645 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); | |
7646 | struct ofp11_group_desc_stats *ogds; | |
7647 | struct ofputil_bucket *bucket; | |
7648 | size_t start_ogds; | |
7649 | ||
6fd6ed71 | 7650 | start_ogds = reply->size; |
7395c052 NZ |
7651 | ofpbuf_put_zeros(reply, sizeof *ogds); |
7652 | LIST_FOR_EACH (bucket, list_node, buckets) { | |
5097fee5 | 7653 | ofputil_put_ofp11_bucket(bucket, reply, version); |
7395c052 NZ |
7654 | } |
7655 | ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds); | |
6fd6ed71 | 7656 | ogds->length = htons(reply->size - start_ogds); |
7395c052 NZ |
7657 | ogds->type = gds->type; |
7658 | ogds->group_id = htonl(gds->group_id); | |
7659 | ||
7660 | ofpmp_postappend(replies, start_ogds); | |
7661 | } | |
7662 | ||
18ac06d3 SH |
7663 | static void |
7664 | ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds, | |
1667bb34 | 7665 | const struct ovs_list *buckets, |
ca6ba700 | 7666 | struct ovs_list *replies, |
18ac06d3 SH |
7667 | enum ofp_version version) |
7668 | { | |
7669 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); | |
7670 | struct ofp15_group_desc_stats *ogds; | |
7671 | struct ofputil_bucket *bucket; | |
7672 | size_t start_ogds, start_buckets; | |
7673 | ||
6fd6ed71 | 7674 | start_ogds = reply->size; |
18ac06d3 | 7675 | ofpbuf_put_zeros(reply, sizeof *ogds); |
6fd6ed71 | 7676 | start_buckets = reply->size; |
18ac06d3 SH |
7677 | LIST_FOR_EACH (bucket, list_node, buckets) { |
7678 | ofputil_put_ofp15_bucket(bucket, bucket->bucket_id, | |
7679 | gds->type, reply, version); | |
7680 | } | |
7681 | ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds); | |
6fd6ed71 | 7682 | ogds->length = htons(reply->size - start_ogds); |
18ac06d3 SH |
7683 | ogds->type = gds->type; |
7684 | ogds->group_id = htonl(gds->group_id); | |
6fd6ed71 | 7685 | ogds->bucket_list_len = htons(reply->size - start_buckets); |
18ac06d3 | 7686 | |
53eb84a5 SH |
7687 | /* Add group properties */ |
7688 | if (gds->props.selection_method[0]) { | |
7689 | ofputil_put_group_prop_ntr_selection_method(version, &gds->props, | |
7690 | reply); | |
7691 | } | |
7692 | ||
18ac06d3 SH |
7693 | ofpmp_postappend(replies, start_ogds); |
7694 | } | |
7695 | ||
5be50252 SH |
7696 | /* Appends a group stats reply that contains the data in 'gds' to those already |
7697 | * present in the list of ofpbufs in 'replies'. 'replies' should have been | |
7698 | * initialized with ofpmp_init(). */ | |
7699 | void | |
7700 | ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds, | |
1667bb34 | 7701 | const struct ovs_list *buckets, |
ca6ba700 | 7702 | struct ovs_list *replies) |
5be50252 SH |
7703 | { |
7704 | enum ofp_version version = ofpmp_version(replies); | |
7705 | ||
7706 | switch (version) | |
7707 | { | |
7708 | case OFP11_VERSION: | |
7709 | case OFP12_VERSION: | |
7710 | case OFP13_VERSION: | |
7711 | case OFP14_VERSION: | |
5be50252 SH |
7712 | ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version); |
7713 | break; | |
7714 | ||
18ac06d3 SH |
7715 | case OFP15_VERSION: |
7716 | ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version); | |
7717 | break; | |
7718 | ||
5be50252 SH |
7719 | case OFP10_VERSION: |
7720 | default: | |
7721 | OVS_NOT_REACHED(); | |
7722 | } | |
7723 | } | |
7724 | ||
7395c052 | 7725 | static enum ofperr |
655ed3ae | 7726 | ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length, |
ca6ba700 | 7727 | enum ofp_version version, struct ovs_list *buckets) |
7395c052 NZ |
7728 | { |
7729 | struct ofp11_bucket *ob; | |
18ac06d3 | 7730 | uint32_t bucket_id = 0; |
7395c052 NZ |
7731 | |
7732 | list_init(buckets); | |
7733 | while (buckets_length > 0) { | |
7734 | struct ofputil_bucket *bucket; | |
7735 | struct ofpbuf ofpacts; | |
7736 | enum ofperr error; | |
7737 | size_t ob_len; | |
7738 | ||
7739 | ob = (buckets_length >= sizeof *ob | |
7740 | ? ofpbuf_try_pull(msg, sizeof *ob) | |
7741 | : NULL); | |
7742 | if (!ob) { | |
34582733 | 7743 | VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes", |
7395c052 | 7744 | buckets_length); |
e27e4ce9 | 7745 | return OFPERR_OFPGMFC_BAD_BUCKET; |
7395c052 NZ |
7746 | } |
7747 | ||
7748 | ob_len = ntohs(ob->len); | |
7749 | if (ob_len < sizeof *ob) { | |
7750 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length " | |
34582733 | 7751 | "%"PRIuSIZE" is not valid", ob_len); |
7395c052 NZ |
7752 | return OFPERR_OFPGMFC_BAD_BUCKET; |
7753 | } else if (ob_len > buckets_length) { | |
7754 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length " | |
34582733 | 7755 | "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE, |
7395c052 NZ |
7756 | ob_len, buckets_length); |
7757 | return OFPERR_OFPGMFC_BAD_BUCKET; | |
7758 | } | |
7759 | buckets_length -= ob_len; | |
7760 | ||
7761 | ofpbuf_init(&ofpacts, 0); | |
e3f8f887 JR |
7762 | error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob, |
7763 | version, &ofpacts); | |
7395c052 NZ |
7764 | if (error) { |
7765 | ofpbuf_uninit(&ofpacts); | |
7766 | ofputil_bucket_list_destroy(buckets); | |
7767 | return error; | |
7768 | } | |
7769 | ||
7770 | bucket = xzalloc(sizeof *bucket); | |
7771 | bucket->weight = ntohs(ob->weight); | |
7772 | error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port); | |
7773 | if (error) { | |
7774 | ofpbuf_uninit(&ofpacts); | |
7775 | ofputil_bucket_list_destroy(buckets); | |
7776 | return OFPERR_OFPGMFC_BAD_WATCH; | |
7777 | } | |
7778 | bucket->watch_group = ntohl(ob->watch_group); | |
18ac06d3 SH |
7779 | bucket->bucket_id = bucket_id++; |
7780 | ||
7395c052 | 7781 | bucket->ofpacts = ofpbuf_steal_data(&ofpacts); |
6fd6ed71 | 7782 | bucket->ofpacts_len = ofpacts.size; |
7395c052 NZ |
7783 | list_push_back(buckets, &bucket->list_node); |
7784 | } | |
7785 | ||
7786 | return 0; | |
7787 | } | |
7788 | ||
18ac06d3 SH |
7789 | static enum ofperr |
7790 | parse_ofp15_group_bucket_prop_weight(const struct ofpbuf *payload, | |
7791 | ovs_be16 *weight) | |
7792 | { | |
6fd6ed71 | 7793 | struct ofp15_group_bucket_prop_weight *prop = payload->data; |
18ac06d3 | 7794 | |
6fd6ed71 | 7795 | if (payload->size != sizeof *prop) { |
18ac06d3 | 7796 | log_property(false, "OpenFlow bucket weight property length " |
6fd6ed71 | 7797 | "%u is not valid", payload->size); |
18ac06d3 SH |
7798 | return OFPERR_OFPBPC_BAD_LEN; |
7799 | } | |
7800 | ||
7801 | *weight = prop->weight; | |
7802 | ||
7803 | return 0; | |
7804 | } | |
7805 | ||
7806 | static enum ofperr | |
7807 | parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload, | |
7808 | ovs_be32 *watch) | |
7809 | { | |
6fd6ed71 | 7810 | struct ofp15_group_bucket_prop_watch *prop = payload->data; |
18ac06d3 | 7811 | |
6fd6ed71 | 7812 | if (payload->size != sizeof *prop) { |
18ac06d3 | 7813 | log_property(false, "OpenFlow bucket watch port or group " |
6fd6ed71 | 7814 | "property length %u is not valid", payload->size); |
18ac06d3 SH |
7815 | return OFPERR_OFPBPC_BAD_LEN; |
7816 | } | |
7817 | ||
7818 | *watch = prop->watch; | |
7819 | ||
7820 | return 0; | |
7821 | } | |
7822 | ||
7823 | static enum ofperr | |
7824 | ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length, | |
ca6ba700 | 7825 | enum ofp_version version, struct ovs_list *buckets) |
18ac06d3 SH |
7826 | { |
7827 | struct ofp15_bucket *ob; | |
7828 | ||
7829 | list_init(buckets); | |
7830 | while (buckets_length > 0) { | |
7831 | struct ofputil_bucket *bucket = NULL; | |
7832 | struct ofpbuf ofpacts; | |
7833 | enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET; | |
7834 | struct ofpbuf properties; | |
7835 | size_t ob_len, actions_len, properties_len; | |
7836 | ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY); | |
7837 | ovs_be32 watch_group = htonl(OFPG_ANY); | |
7838 | ovs_be16 weight = htons(1); | |
7839 | ||
7840 | ofpbuf_init(&ofpacts, 0); | |
7841 | ||
7842 | ob = ofpbuf_try_pull(msg, sizeof *ob); | |
7843 | if (!ob) { | |
7844 | VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE | |
7845 | " leftover bytes", buckets_length); | |
7846 | goto err; | |
7847 | } | |
7848 | ||
7849 | ob_len = ntohs(ob->len); | |
79870963 | 7850 | actions_len = ntohs(ob->action_array_len); |
18ac06d3 SH |
7851 | |
7852 | if (ob_len < sizeof *ob) { | |
7853 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length " | |
7854 | "%"PRIuSIZE" is not valid", ob_len); | |
7855 | goto err; | |
7856 | } else if (ob_len > buckets_length) { | |
7857 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length " | |
7858 | "%"PRIuSIZE" exceeds remaining buckets data size %" | |
7859 | PRIuSIZE, ob_len, buckets_length); | |
7860 | goto err; | |
7861 | } else if (actions_len > ob_len - sizeof *ob) { | |
7862 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions " | |
7863 | "length %"PRIuSIZE" exceeds remaining bucket " | |
7864 | "data size %"PRIuSIZE, actions_len, | |
7865 | ob_len - sizeof *ob); | |
7866 | goto err; | |
7867 | } | |
7868 | buckets_length -= ob_len; | |
7869 | ||
7870 | err = ofpacts_pull_openflow_actions(msg, actions_len, version, | |
7871 | &ofpacts); | |
7872 | if (err) { | |
7873 | goto err; | |
7874 | } | |
7875 | ||
7876 | properties_len = ob_len - sizeof *ob - actions_len; | |
7877 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len), | |
7878 | properties_len); | |
7879 | ||
6fd6ed71 | 7880 | while (properties.size > 0) { |
18ac06d3 SH |
7881 | struct ofpbuf payload; |
7882 | uint16_t type; | |
7883 | ||
7884 | err = ofputil_pull_property(&properties, &payload, &type); | |
7885 | if (err) { | |
7886 | goto err; | |
7887 | } | |
7888 | ||
7889 | switch (type) { | |
7890 | case OFPGBPT15_WEIGHT: | |
7891 | err = parse_ofp15_group_bucket_prop_weight(&payload, &weight); | |
7892 | break; | |
7893 | ||
7894 | case OFPGBPT15_WATCH_PORT: | |
7895 | err = parse_ofp15_group_bucket_prop_watch(&payload, | |
7896 | &watch_port); | |
7897 | break; | |
7898 | ||
7899 | case OFPGBPT15_WATCH_GROUP: | |
7900 | err = parse_ofp15_group_bucket_prop_watch(&payload, | |
7901 | &watch_group); | |
7902 | break; | |
7903 | ||
7904 | default: | |
7905 | log_property(false, "unknown group bucket property %"PRIu16, | |
7906 | type); | |
7907 | err = OFPERR_OFPBPC_BAD_TYPE; | |
7908 | break; | |
7909 | } | |
7910 | ||
7911 | if (err) { | |
7912 | goto err; | |
7913 | } | |
7914 | } | |
7915 | ||
7916 | bucket = xzalloc(sizeof *bucket); | |
7917 | ||
7918 | bucket->weight = ntohs(weight); | |
7919 | err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port); | |
7920 | if (err) { | |
7921 | err = OFPERR_OFPGMFC_BAD_WATCH; | |
7922 | goto err; | |
7923 | } | |
7924 | bucket->watch_group = ntohl(watch_group); | |
7925 | bucket->bucket_id = ntohl(ob->bucket_id); | |
7926 | if (bucket->bucket_id > OFPG15_BUCKET_MAX) { | |
7927 | VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range", | |
7928 | bucket->bucket_id); | |
7929 | err = OFPERR_OFPGMFC_BAD_BUCKET; | |
7930 | goto err; | |
7931 | } | |
7932 | ||
7933 | bucket->ofpacts = ofpbuf_steal_data(&ofpacts); | |
6fd6ed71 | 7934 | bucket->ofpacts_len = ofpacts.size; |
18ac06d3 SH |
7935 | list_push_back(buckets, &bucket->list_node); |
7936 | ||
7937 | continue; | |
7938 | ||
7939 | err: | |
7940 | free(bucket); | |
7941 | ofpbuf_uninit(&ofpacts); | |
7942 | ofputil_bucket_list_destroy(buckets); | |
7943 | return err; | |
7944 | } | |
7945 | ||
7946 | if (ofputil_bucket_check_duplicate_id(buckets)) { | |
7947 | VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id"); | |
7948 | ofputil_bucket_list_destroy(buckets); | |
7949 | return OFPERR_OFPGMFC_BAD_BUCKET; | |
7950 | } | |
7951 | ||
7952 | return 0; | |
7953 | } | |
7954 | ||
bc65c25a SH |
7955 | static void |
7956 | ofputil_init_group_properties(struct ofputil_group_props *gp) | |
7957 | { | |
7958 | memset(gp, 0, sizeof *gp); | |
7959 | } | |
7960 | ||
7961 | static enum ofperr | |
7962 | parse_group_prop_ntr_selection_method(struct ofpbuf *payload, | |
7963 | enum ofp11_group_type group_type, | |
7964 | enum ofp15_group_mod_command group_cmd, | |
7965 | struct ofputil_group_props *gp) | |
7966 | { | |
7967 | struct ntr_group_prop_selection_method *prop = payload->data; | |
7968 | size_t fields_len, method_len; | |
7969 | enum ofperr error; | |
7970 | ||
7971 | switch (group_type) { | |
7972 | case OFPGT11_SELECT: | |
7973 | break; | |
7974 | case OFPGT11_ALL: | |
7975 | case OFPGT11_INDIRECT: | |
7976 | case OFPGT11_FF: | |
7977 | log_property(false, "ntr selection method property is only allowed " | |
7978 | "for select groups"); | |
7979 | return OFPERR_OFPBPC_BAD_VALUE; | |
7980 | default: | |
7981 | OVS_NOT_REACHED(); | |
7982 | } | |
7983 | ||
7984 | switch (group_cmd) { | |
7985 | case OFPGC15_ADD: | |
7986 | case OFPGC15_MODIFY: | |
7987 | break; | |
7988 | case OFPGC15_DELETE: | |
7989 | case OFPGC15_INSERT_BUCKET: | |
7990 | case OFPGC15_REMOVE_BUCKET: | |
7991 | log_property(false, "ntr selection method property is only allowed " | |
7992 | "for add and delete group modifications"); | |
7993 | return OFPERR_OFPBPC_BAD_VALUE; | |
7994 | default: | |
7995 | OVS_NOT_REACHED(); | |
7996 | } | |
7997 | ||
7998 | if (payload->size < sizeof *prop) { | |
7999 | log_property(false, "ntr selection method property length " | |
8000 | "%u is not valid", payload->size); | |
8001 | return OFPERR_OFPBPC_BAD_LEN; | |
8002 | } | |
8003 | ||
8004 | method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN); | |
8005 | ||
8006 | if (method_len == NTR_MAX_SELECTION_METHOD_LEN) { | |
8007 | log_property(false, "ntr selection method is not null terminated"); | |
8008 | return OFPERR_OFPBPC_BAD_VALUE; | |
8009 | } | |
8010 | ||
0c4b9393 SH |
8011 | if (strcmp("hash", prop->selection_method)) { |
8012 | log_property(false, "ntr selection method '%s' is not supported", | |
8013 | prop->selection_method); | |
8014 | return OFPERR_OFPBPC_BAD_VALUE; | |
8015 | } | |
bc65c25a SH |
8016 | |
8017 | strcpy(gp->selection_method, prop->selection_method); | |
8018 | gp->selection_method_param = ntohll(prop->selection_method_param); | |
8019 | ||
8020 | if (!method_len && gp->selection_method_param) { | |
8021 | log_property(false, "ntr selection method parameter is non-zero but " | |
8022 | "selection method is empty"); | |
8023 | return OFPERR_OFPBPC_BAD_VALUE; | |
8024 | } | |
8025 | ||
8026 | ofpbuf_pull(payload, sizeof *prop); | |
8027 | ||
8028 | fields_len = ntohs(prop->length) - sizeof *prop; | |
8029 | if (!method_len && fields_len) { | |
8030 | log_property(false, "ntr selection method parameter is zero " | |
8031 | "but fields are provided"); | |
8032 | return OFPERR_OFPBPC_BAD_VALUE; | |
8033 | } | |
8034 | ||
8035 | error = oxm_pull_field_array(payload->data, fields_len, | |
8036 | &gp->fields); | |
8037 | if (error) { | |
8038 | log_property(false, "ntr selection method fields are invalid"); | |
8039 | return error; | |
8040 | } | |
8041 | ||
8042 | return 0; | |
8043 | } | |
8044 | ||
8045 | static enum ofperr | |
8046 | parse_group_prop_ntr(struct ofpbuf *payload, uint32_t exp_type, | |
8047 | enum ofp11_group_type group_type, | |
8048 | enum ofp15_group_mod_command group_cmd, | |
8049 | struct ofputil_group_props *gp) | |
8050 | { | |
8051 | enum ofperr error; | |
8052 | ||
8053 | switch (exp_type) { | |
8054 | case NTRT_SELECTION_METHOD: | |
8055 | error = parse_group_prop_ntr_selection_method(payload, group_type, | |
8056 | group_cmd, gp); | |
8057 | break; | |
8058 | ||
8059 | default: | |
8060 | log_property(false, "unknown group property ntr experimenter type " | |
8061 | "%"PRIu32, exp_type); | |
8062 | error = OFPERR_OFPBPC_BAD_TYPE; | |
8063 | break; | |
8064 | } | |
8065 | ||
8066 | return error; | |
8067 | } | |
8068 | ||
8069 | static enum ofperr | |
8070 | parse_ofp15_group_prop_exp(struct ofpbuf *payload, | |
8071 | enum ofp11_group_type group_type, | |
8072 | enum ofp15_group_mod_command group_cmd, | |
8073 | struct ofputil_group_props *gp) | |
8074 | { | |
8075 | struct ofp_prop_experimenter *prop = payload->data; | |
8076 | uint16_t experimenter; | |
8077 | uint32_t exp_type; | |
8078 | enum ofperr error; | |
8079 | ||
8080 | if (payload->size < sizeof *prop) { | |
8081 | return OFPERR_OFPBPC_BAD_LEN; | |
8082 | } | |
8083 | ||
8084 | experimenter = ntohl(prop->experimenter); | |
8085 | exp_type = ntohl(prop->exp_type); | |
8086 | ||
8087 | switch (experimenter) { | |
8088 | case NTR_VENDOR_ID: | |
8089 | error = parse_group_prop_ntr(payload, exp_type, group_type, | |
8090 | group_cmd, gp); | |
8091 | break; | |
8092 | ||
8093 | default: | |
8094 | log_property(false, "unknown group property experimenter %"PRIu16, | |
8095 | experimenter); | |
8096 | error = OFPERR_OFPBPC_BAD_EXPERIMENTER; | |
8097 | break; | |
8098 | } | |
8099 | ||
8100 | return error; | |
8101 | } | |
8102 | ||
8103 | static enum ofperr | |
8104 | parse_ofp15_group_properties(struct ofpbuf *msg, | |
8105 | enum ofp11_group_type group_type, | |
8106 | enum ofp15_group_mod_command group_cmd, | |
8107 | struct ofputil_group_props *gp, | |
8108 | size_t properties_len) | |
8109 | { | |
8110 | struct ofpbuf properties; | |
8111 | ||
8112 | ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len), | |
8113 | properties_len); | |
8114 | ||
8115 | while (properties.size > 0) { | |
8116 | struct ofpbuf payload; | |
8117 | enum ofperr error; | |
8118 | uint16_t type; | |
8119 | ||
8120 | error = ofputil_pull_property(&properties, &payload, &type); | |
8121 | if (error) { | |
8122 | return error; | |
8123 | } | |
8124 | ||
8125 | switch (type) { | |
8126 | case OFPGPT15_EXPERIMENTER: | |
8127 | error = parse_ofp15_group_prop_exp(&payload, group_type, | |
8128 | group_cmd, gp); | |
8129 | break; | |
8130 | ||
8131 | default: | |
8132 | log_property(false, "unknown group property %"PRIu16, type); | |
8133 | error = OFPERR_OFPBPC_BAD_TYPE; | |
8134 | break; | |
8135 | } | |
8136 | ||
8137 | if (error) { | |
8138 | return error; | |
8139 | } | |
8140 | } | |
8141 | ||
8142 | return 0; | |
8143 | } | |
8144 | ||
975910f2 SH |
8145 | static int |
8146 | ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd, | |
8147 | struct ofpbuf *msg, | |
8148 | enum ofp_version version) | |
7395c052 NZ |
8149 | { |
8150 | struct ofp11_group_desc_stats *ogds; | |
8151 | size_t length; | |
8152 | ||
6fd6ed71 | 8153 | if (!msg->header) { |
7395c052 NZ |
8154 | ofpraw_pull_assert(msg); |
8155 | } | |
8156 | ||
6fd6ed71 | 8157 | if (!msg->size) { |
7395c052 NZ |
8158 | return EOF; |
8159 | } | |
8160 | ||
8161 | ogds = ofpbuf_try_pull(msg, sizeof *ogds); | |
8162 | if (!ogds) { | |
437d0d22 | 8163 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" " |
6fd6ed71 | 8164 | "leftover bytes at end", msg->size); |
7395c052 NZ |
8165 | return OFPERR_OFPBRC_BAD_LEN; |
8166 | } | |
8167 | gd->type = ogds->type; | |
8168 | gd->group_id = ntohl(ogds->group_id); | |
8169 | ||
8170 | length = ntohs(ogds->length); | |
6fd6ed71 | 8171 | if (length < sizeof *ogds || length - sizeof *ogds > msg->size) { |
7395c052 | 8172 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid " |
34582733 | 8173 | "length %"PRIuSIZE, length); |
7395c052 NZ |
8174 | return OFPERR_OFPBRC_BAD_LEN; |
8175 | } | |
8176 | ||
655ed3ae SH |
8177 | return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version, |
8178 | &gd->buckets); | |
7395c052 NZ |
8179 | } |
8180 | ||
18ac06d3 SH |
8181 | static int |
8182 | ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd, | |
8183 | struct ofpbuf *msg, | |
8184 | enum ofp_version version) | |
8185 | { | |
8186 | struct ofp15_group_desc_stats *ogds; | |
8187 | uint16_t length, bucket_list_len; | |
bc65c25a | 8188 | int error; |
18ac06d3 | 8189 | |
6fd6ed71 | 8190 | if (!msg->header) { |
18ac06d3 SH |
8191 | ofpraw_pull_assert(msg); |
8192 | } | |
8193 | ||
6fd6ed71 | 8194 | if (!msg->size) { |
18ac06d3 SH |
8195 | return EOF; |
8196 | } | |
8197 | ||
8198 | ogds = ofpbuf_try_pull(msg, sizeof *ogds); | |
8199 | if (!ogds) { | |
8200 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" " | |
6fd6ed71 | 8201 | "leftover bytes at end", msg->size); |
18ac06d3 SH |
8202 | return OFPERR_OFPBRC_BAD_LEN; |
8203 | } | |
8204 | gd->type = ogds->type; | |
8205 | gd->group_id = ntohl(ogds->group_id); | |
8206 | ||
8207 | length = ntohs(ogds->length); | |
6fd6ed71 | 8208 | if (length < sizeof *ogds || length - sizeof *ogds > msg->size) { |
18ac06d3 SH |
8209 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid " |
8210 | "length %u", length); | |
8211 | return OFPERR_OFPBRC_BAD_LEN; | |
8212 | } | |
8213 | ||
8214 | bucket_list_len = ntohs(ogds->bucket_list_len); | |
8215 | if (length < bucket_list_len + sizeof *ogds) { | |
8216 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid " | |
8217 | "bucket list length %u", bucket_list_len); | |
8218 | return OFPERR_OFPBRC_BAD_LEN; | |
8219 | } | |
bc65c25a SH |
8220 | error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, |
8221 | &gd->buckets); | |
8222 | if (error) { | |
8223 | return error; | |
8224 | } | |
18ac06d3 | 8225 | |
bc65c25a SH |
8226 | /* By definition group desc messages don't have a group mod command. |
8227 | * However, parse_group_prop_ntr_selection_method() checks to make sure | |
8228 | * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard | |
8229 | * against group mod messages with other commands supplying | |
8230 | * a NTR selection method group experimenter property. | |
8231 | * Such properties are valid for group desc replies so | |
8232 | * claim that the group mod command is OFPGC15_ADD to | |
8233 | * satisfy the check in parse_group_prop_ntr_selection_method() */ | |
8234 | return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props, | |
8235 | msg->size); | |
18ac06d3 SH |
8236 | } |
8237 | ||
975910f2 SH |
8238 | /* Converts a group description reply in 'msg' into an abstract |
8239 | * ofputil_group_desc in 'gd'. | |
8240 | * | |
8241 | * Multiple group description replies can be packed into a single OpenFlow | |
8242 | * message. Calling this function multiple times for a single 'msg' iterates | |
8243 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
8244 | * null and not modify them between calls. | |
8245 | * | |
8246 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
8247 | * otherwise a positive errno value. */ | |
8248 | int | |
8249 | ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd, | |
8250 | struct ofpbuf *msg, enum ofp_version version) | |
8251 | { | |
bc65c25a SH |
8252 | ofputil_init_group_properties(&gd->props); |
8253 | ||
975910f2 SH |
8254 | switch (version) |
8255 | { | |
8256 | case OFP11_VERSION: | |
8257 | case OFP12_VERSION: | |
8258 | case OFP13_VERSION: | |
8259 | case OFP14_VERSION: | |
975910f2 SH |
8260 | return ofputil_decode_ofp11_group_desc_reply(gd, msg, version); |
8261 | ||
18ac06d3 SH |
8262 | case OFP15_VERSION: |
8263 | return ofputil_decode_ofp15_group_desc_reply(gd, msg, version); | |
8264 | ||
975910f2 SH |
8265 | case OFP10_VERSION: |
8266 | default: | |
8267 | OVS_NOT_REACHED(); | |
8268 | } | |
8269 | } | |
8270 | ||
bc65c25a SH |
8271 | void |
8272 | ofputil_uninit_group_mod(struct ofputil_group_mod *gm) | |
8273 | { | |
8274 | ofputil_bucket_list_destroy(&gm->buckets); | |
8275 | } | |
8276 | ||
4498bea5 SH |
8277 | static struct ofpbuf * |
8278 | ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version, | |
8279 | const struct ofputil_group_mod *gm) | |
7395c052 NZ |
8280 | { |
8281 | struct ofpbuf *b; | |
8282 | struct ofp11_group_mod *ogm; | |
8283 | size_t start_ogm; | |
7395c052 | 8284 | struct ofputil_bucket *bucket; |
7395c052 | 8285 | |
4498bea5 | 8286 | b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0); |
6fd6ed71 | 8287 | start_ogm = b->size; |
4498bea5 SH |
8288 | ofpbuf_put_zeros(b, sizeof *ogm); |
8289 | ||
8290 | LIST_FOR_EACH (bucket, list_node, &gm->buckets) { | |
8291 | ofputil_put_ofp11_bucket(bucket, b, ofp_version); | |
8292 | } | |
8293 | ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm); | |
8294 | ogm->command = htons(gm->command); | |
8295 | ogm->type = gm->type; | |
8296 | ogm->group_id = htonl(gm->group_id); | |
8297 | ||
8298 | return b; | |
8299 | } | |
8300 | ||
18ac06d3 SH |
8301 | static struct ofpbuf * |
8302 | ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version, | |
8303 | const struct ofputil_group_mod *gm) | |
8304 | { | |
8305 | struct ofpbuf *b; | |
8306 | struct ofp15_group_mod *ogm; | |
8307 | size_t start_ogm; | |
8308 | struct ofputil_bucket *bucket; | |
8309 | struct id_pool *bucket_ids = NULL; | |
8310 | ||
8311 | b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0); | |
6fd6ed71 | 8312 | start_ogm = b->size; |
18ac06d3 SH |
8313 | ofpbuf_put_zeros(b, sizeof *ogm); |
8314 | ||
8315 | LIST_FOR_EACH (bucket, list_node, &gm->buckets) { | |
8316 | uint32_t bucket_id; | |
8317 | ||
8318 | /* Generate a bucket id if none was supplied */ | |
8319 | if (bucket->bucket_id > OFPG15_BUCKET_MAX) { | |
8320 | if (!bucket_ids) { | |
8321 | const struct ofputil_bucket *bkt; | |
8322 | ||
8323 | bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1); | |
8324 | ||
8325 | /* Mark all bucket_ids that are present in gm | |
8326 | * as used in the pool. */ | |
8327 | LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) { | |
8328 | if (bkt == bucket) { | |
8329 | break; | |
8330 | } | |
8331 | if (bkt->bucket_id <= OFPG15_BUCKET_MAX) { | |
8332 | id_pool_add(bucket_ids, bkt->bucket_id); | |
8333 | } | |
8334 | } | |
8335 | } | |
8336 | ||
8337 | if (!id_pool_alloc_id(bucket_ids, &bucket_id)) { | |
8338 | OVS_NOT_REACHED(); | |
8339 | } | |
8340 | } else { | |
8341 | bucket_id = bucket->bucket_id; | |
8342 | } | |
8343 | ||
8344 | ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version); | |
8345 | } | |
8346 | ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm); | |
8347 | ogm->command = htons(gm->command); | |
8348 | ogm->type = gm->type; | |
8349 | ogm->group_id = htonl(gm->group_id); | |
8350 | ogm->command_bucket_id = htonl(gm->command_bucket_id); | |
6fd6ed71 | 8351 | ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm); |
18ac06d3 | 8352 | |
53eb84a5 SH |
8353 | /* Add group properties */ |
8354 | if (gm->props.selection_method[0]) { | |
8355 | ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b); | |
8356 | } | |
8357 | ||
18ac06d3 SH |
8358 | id_pool_destroy(bucket_ids); |
8359 | return b; | |
8360 | } | |
8361 | ||
d4b1d0ca | 8362 | static void |
9dd30b05 BP |
8363 | bad_group_cmd(enum ofp15_group_mod_command cmd) |
8364 | { | |
d4b1d0ca SH |
8365 | const char *opt_version; |
8366 | const char *version; | |
8367 | const char *cmd_str; | |
8368 | ||
8369 | switch (cmd) { | |
8370 | case OFPGC15_ADD: | |
8371 | case OFPGC15_MODIFY: | |
8372 | case OFPGC15_DELETE: | |
8373 | version = "1.1"; | |
8374 | opt_version = "11"; | |
8375 | break; | |
8376 | ||
8377 | case OFPGC15_INSERT_BUCKET: | |
8378 | case OFPGC15_REMOVE_BUCKET: | |
8379 | version = "1.5"; | |
8380 | opt_version = "15"; | |
9dd30b05 | 8381 | break; |
d4b1d0ca SH |
8382 | |
8383 | default: | |
8384 | OVS_NOT_REACHED(); | |
8385 | } | |
8386 | ||
8387 | switch (cmd) { | |
8388 | case OFPGC15_ADD: | |
8389 | cmd_str = "add-group"; | |
8390 | break; | |
8391 | ||
8392 | case OFPGC15_MODIFY: | |
8393 | cmd_str = "mod-group"; | |
8394 | break; | |
8395 | ||
8396 | case OFPGC15_DELETE: | |
8397 | cmd_str = "del-group"; | |
8398 | break; | |
8399 | ||
8400 | case OFPGC15_INSERT_BUCKET: | |
8401 | cmd_str = "insert-bucket"; | |
8402 | break; | |
8403 | ||
8404 | case OFPGC15_REMOVE_BUCKET: | |
9dd30b05 | 8405 | cmd_str = "remove-bucket"; |
d4b1d0ca SH |
8406 | break; |
8407 | ||
8408 | default: | |
8409 | OVS_NOT_REACHED(); | |
8410 | } | |
8411 | ||
8412 | ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')", | |
8413 | cmd_str, version, opt_version); | |
8414 | ||
8415 | } | |
8416 | ||
4498bea5 SH |
8417 | /* Converts abstract group mod 'gm' into a message for OpenFlow version |
8418 | * 'ofp_version' and returns the message. */ | |
8419 | struct ofpbuf * | |
8420 | ofputil_encode_group_mod(enum ofp_version ofp_version, | |
8421 | const struct ofputil_group_mod *gm) | |
8422 | { | |
d4b1d0ca | 8423 | |
7395c052 | 8424 | switch (ofp_version) { |
d4b1d0ca SH |
8425 | case OFP10_VERSION: |
8426 | bad_group_cmd(gm->command); | |
7395c052 NZ |
8427 | |
8428 | case OFP11_VERSION: | |
8429 | case OFP12_VERSION: | |
c37c0382 AC |
8430 | case OFP13_VERSION: |
8431 | case OFP14_VERSION: | |
d4b1d0ca SH |
8432 | if (gm->command > OFPGC11_DELETE) { |
8433 | bad_group_cmd(gm->command); | |
8434 | } | |
4498bea5 | 8435 | return ofputil_encode_ofp11_group_mod(ofp_version, gm); |
7395c052 | 8436 | |
18ac06d3 SH |
8437 | case OFP15_VERSION: |
8438 | return ofputil_encode_ofp15_group_mod(ofp_version, gm); | |
8439 | ||
7395c052 | 8440 | default: |
428b2edd | 8441 | OVS_NOT_REACHED(); |
7395c052 | 8442 | } |
7395c052 NZ |
8443 | } |
8444 | ||
aead63f2 SH |
8445 | static enum ofperr |
8446 | ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version, | |
8447 | struct ofputil_group_mod *gm) | |
8448 | { | |
8449 | const struct ofp11_group_mod *ogm; | |
08ba0810 | 8450 | enum ofperr error; |
aead63f2 SH |
8451 | |
8452 | ogm = ofpbuf_pull(msg, sizeof *ogm); | |
8453 | gm->command = ntohs(ogm->command); | |
8454 | gm->type = ogm->type; | |
8455 | gm->group_id = ntohl(ogm->group_id); | |
18ac06d3 | 8456 | gm->command_bucket_id = OFPG15_BUCKET_ALL; |
aead63f2 | 8457 | |
6fd6ed71 | 8458 | error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version, |
08ba0810 BP |
8459 | &gm->buckets); |
8460 | ||
8461 | /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */ | |
8462 | if (!error | |
8463 | && ofp_version >= OFP13_VERSION | |
8464 | && gm->command == OFPGC11_DELETE | |
8465 | && !list_is_empty(&gm->buckets)) { | |
8466 | error = OFPERR_OFPGMFC_INVALID_GROUP; | |
8467 | } | |
8468 | ||
8469 | return error; | |
aead63f2 SH |
8470 | } |
8471 | ||
18ac06d3 SH |
8472 | static enum ofperr |
8473 | ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version, | |
8474 | struct ofputil_group_mod *gm) | |
8475 | { | |
8476 | const struct ofp15_group_mod *ogm; | |
8477 | uint16_t bucket_list_len; | |
8478 | enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET; | |
8479 | ||
8480 | ogm = ofpbuf_pull(msg, sizeof *ogm); | |
8481 | gm->command = ntohs(ogm->command); | |
8482 | gm->type = ogm->type; | |
8483 | gm->group_id = ntohl(ogm->group_id); | |
8484 | ||
8485 | gm->command_bucket_id = ntohl(ogm->command_bucket_id); | |
8486 | switch (gm->command) { | |
8487 | case OFPGC15_REMOVE_BUCKET: | |
8488 | if (gm->command_bucket_id == OFPG15_BUCKET_ALL) { | |
8489 | error = 0; | |
8490 | } | |
8491 | /* Fall through */ | |
8492 | case OFPGC15_INSERT_BUCKET: | |
8493 | if (gm->command_bucket_id <= OFPG15_BUCKET_MAX || | |
8494 | gm->command_bucket_id == OFPG15_BUCKET_FIRST | |
8495 | || gm->command_bucket_id == OFPG15_BUCKET_LAST) { | |
8496 | error = 0; | |
8497 | } | |
8498 | break; | |
8499 | ||
8500 | case OFPGC11_ADD: | |
8501 | case OFPGC11_MODIFY: | |
8502 | case OFPGC11_DELETE: | |
8503 | default: | |
8504 | if (gm->command_bucket_id == OFPG15_BUCKET_ALL) { | |
8505 | error = 0; | |
8506 | } | |
8507 | break; | |
8508 | } | |
8509 | if (error) { | |
8510 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
8511 | "group command bucket id (%u) is out of range", | |
8512 | gm->command_bucket_id); | |
8513 | return OFPERR_OFPGMFC_BAD_BUCKET; | |
8514 | } | |
8515 | ||
79870963 | 8516 | bucket_list_len = ntohs(ogm->bucket_array_len); |
bc65c25a SH |
8517 | error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version, |
8518 | &gm->buckets); | |
8519 | if (error) { | |
8520 | return error; | |
18ac06d3 SH |
8521 | } |
8522 | ||
bc65c25a SH |
8523 | return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props, |
8524 | msg->size); | |
18ac06d3 SH |
8525 | } |
8526 | ||
7395c052 NZ |
8527 | /* Converts OpenFlow group mod message 'oh' into an abstract group mod in |
8528 | * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */ | |
8529 | enum ofperr | |
8530 | ofputil_decode_group_mod(const struct ofp_header *oh, | |
8531 | struct ofputil_group_mod *gm) | |
8532 | { | |
aead63f2 | 8533 | enum ofp_version ofp_version = oh->version; |
7395c052 | 8534 | struct ofpbuf msg; |
e57681e5 SH |
8535 | struct ofputil_bucket *bucket; |
8536 | enum ofperr err; | |
7395c052 NZ |
8537 | |
8538 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
8539 | ofpraw_pull_assert(&msg); | |
8540 | ||
bc65c25a SH |
8541 | ofputil_init_group_properties(&gm->props); |
8542 | ||
aead63f2 SH |
8543 | switch (ofp_version) |
8544 | { | |
8545 | case OFP11_VERSION: | |
8546 | case OFP12_VERSION: | |
8547 | case OFP13_VERSION: | |
8548 | case OFP14_VERSION: | |
aead63f2 SH |
8549 | err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm); |
8550 | break; | |
8551 | ||
18ac06d3 SH |
8552 | case OFP15_VERSION: |
8553 | err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm); | |
8554 | break; | |
8555 | ||
aead63f2 SH |
8556 | case OFP10_VERSION: |
8557 | default: | |
8558 | OVS_NOT_REACHED(); | |
8559 | } | |
7395c052 | 8560 | |
e57681e5 SH |
8561 | if (err) { |
8562 | return err; | |
8563 | } | |
8564 | ||
e1799eb7 SH |
8565 | switch (gm->type) { |
8566 | case OFPGT11_INDIRECT: | |
8567 | if (!list_is_singleton(&gm->buckets)) { | |
36be51c5 | 8568 | return OFPERR_OFPGMFC_OUT_OF_BUCKETS; |
e1799eb7 SH |
8569 | } |
8570 | break; | |
8571 | case OFPGT11_ALL: | |
8572 | case OFPGT11_SELECT: | |
8573 | case OFPGT11_FF: | |
8574 | break; | |
8575 | default: | |
8576 | OVS_NOT_REACHED(); | |
65075b99 SH |
8577 | } |
8578 | ||
8579 | switch (gm->command) { | |
8580 | case OFPGC11_ADD: | |
8581 | case OFPGC11_MODIFY: | |
8582 | case OFPGC11_DELETE: | |
8583 | case OFPGC15_INSERT_BUCKET: | |
8584 | break; | |
8585 | case OFPGC15_REMOVE_BUCKET: | |
8586 | if (!list_is_empty(&gm->buckets)) { | |
8587 | return OFPERR_OFPGMFC_BAD_BUCKET; | |
8588 | } | |
8589 | break; | |
8590 | default: | |
8591 | OVS_NOT_REACHED(); | |
e1799eb7 SH |
8592 | } |
8593 | ||
e57681e5 SH |
8594 | LIST_FOR_EACH (bucket, list_node, &gm->buckets) { |
8595 | switch (gm->type) { | |
8596 | case OFPGT11_ALL: | |
8597 | case OFPGT11_INDIRECT: | |
8598 | if (ofputil_bucket_has_liveness(bucket)) { | |
8599 | return OFPERR_OFPGMFC_WATCH_UNSUPPORTED; | |
8600 | } | |
8601 | break; | |
8602 | case OFPGT11_SELECT: | |
8603 | break; | |
8604 | case OFPGT11_FF: | |
8605 | if (!ofputil_bucket_has_liveness(bucket)) { | |
8606 | return OFPERR_OFPGMFC_INVALID_GROUP; | |
8607 | } | |
8608 | break; | |
8609 | default: | |
428b2edd | 8610 | OVS_NOT_REACHED(); |
e57681e5 SH |
8611 | } |
8612 | } | |
8613 | ||
8614 | return 0; | |
7395c052 NZ |
8615 | } |
8616 | ||
64626975 SH |
8617 | /* Parse a queue status request message into 'oqsr'. |
8618 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
8619 | enum ofperr | |
8620 | ofputil_decode_queue_stats_request(const struct ofp_header *request, | |
8621 | struct ofputil_queue_stats_request *oqsr) | |
8622 | { | |
8623 | switch ((enum ofp_version)request->version) { | |
42dccab5 | 8624 | case OFP15_VERSION: |
c37c0382 | 8625 | case OFP14_VERSION: |
2e1ae200 | 8626 | case OFP13_VERSION: |
64626975 SH |
8627 | case OFP12_VERSION: |
8628 | case OFP11_VERSION: { | |
8629 | const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request); | |
8630 | oqsr->queue_id = ntohl(qsr11->queue_id); | |
8631 | return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no); | |
8632 | } | |
8633 | ||
8634 | case OFP10_VERSION: { | |
7f05e7ab JR |
8635 | const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request); |
8636 | oqsr->queue_id = ntohl(qsr10->queue_id); | |
4e022ec0 | 8637 | oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no)); |
7f05e7ab JR |
8638 | /* OF 1.0 uses OFPP_ALL for OFPP_ANY */ |
8639 | if (oqsr->port_no == OFPP_ALL) { | |
8640 | oqsr->port_no = OFPP_ANY; | |
8641 | } | |
64626975 SH |
8642 | return 0; |
8643 | } | |
8644 | ||
8645 | default: | |
428b2edd | 8646 | OVS_NOT_REACHED(); |
64626975 SH |
8647 | } |
8648 | } | |
8649 | ||
0746a84f BP |
8650 | /* Encode a queue stats request for 'oqsr', the encoded message |
8651 | * will be for OpenFlow version 'ofp_version'. Returns message | |
8652 | * as a struct ofpbuf. Returns encoded message on success, NULL on error. */ | |
64626975 SH |
8653 | struct ofpbuf * |
8654 | ofputil_encode_queue_stats_request(enum ofp_version ofp_version, | |
8655 | const struct ofputil_queue_stats_request *oqsr) | |
8656 | { | |
8657 | struct ofpbuf *request; | |
8658 | ||
8659 | switch (ofp_version) { | |
8660 | case OFP11_VERSION: | |
2e1ae200 | 8661 | case OFP12_VERSION: |
c37c0382 | 8662 | case OFP13_VERSION: |
42dccab5 BP |
8663 | case OFP14_VERSION: |
8664 | case OFP15_VERSION: { | |
64626975 SH |
8665 | struct ofp11_queue_stats_request *req; |
8666 | request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0); | |
8667 | req = ofpbuf_put_zeros(request, sizeof *req); | |
8668 | req->port_no = ofputil_port_to_ofp11(oqsr->port_no); | |
8669 | req->queue_id = htonl(oqsr->queue_id); | |
8670 | break; | |
8671 | } | |
8672 | case OFP10_VERSION: { | |
8673 | struct ofp10_queue_stats_request *req; | |
8674 | request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0); | |
8675 | req = ofpbuf_put_zeros(request, sizeof *req); | |
7f05e7ab | 8676 | /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */ |
4e022ec0 AW |
8677 | req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY |
8678 | ? OFPP_ALL : oqsr->port_no)); | |
64626975 SH |
8679 | req->queue_id = htonl(oqsr->queue_id); |
8680 | break; | |
8681 | } | |
8682 | default: | |
428b2edd | 8683 | OVS_NOT_REACHED(); |
64626975 SH |
8684 | } |
8685 | ||
8686 | return request; | |
8687 | } | |
8688 | ||
8689 | /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY | |
8690 | * message 'oh'. */ | |
8691 | size_t | |
8692 | ofputil_count_queue_stats(const struct ofp_header *oh) | |
8693 | { | |
1bb2cdbe | 8694 | struct ofputil_queue_stats qs; |
64626975 | 8695 | struct ofpbuf b; |
1bb2cdbe | 8696 | size_t n = 0; |
64626975 SH |
8697 | |
8698 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
8699 | ofpraw_pull_assert(&b); | |
1bb2cdbe BP |
8700 | while (!ofputil_decode_queue_stats(&qs, &b)) { |
8701 | n++; | |
8702 | } | |
8703 | return n; | |
64626975 SH |
8704 | } |
8705 | ||
8706 | static enum ofperr | |
8707 | ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs, | |
8708 | const struct ofp10_queue_stats *qs10) | |
8709 | { | |
4e022ec0 | 8710 | oqs->port_no = u16_to_ofp(ntohs(qs10->port_no)); |
64626975 | 8711 | oqs->queue_id = ntohl(qs10->queue_id); |
6dc34a0d BP |
8712 | oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes)); |
8713 | oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets)); | |
8714 | oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors)); | |
8715 | oqs->duration_sec = oqs->duration_nsec = UINT32_MAX; | |
64626975 SH |
8716 | |
8717 | return 0; | |
8718 | } | |
8719 | ||
8720 | static enum ofperr | |
8721 | ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs, | |
8722 | const struct ofp11_queue_stats *qs11) | |
8723 | { | |
8724 | enum ofperr error; | |
8725 | ||
8726 | error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no); | |
8727 | if (error) { | |
8728 | return error; | |
8729 | } | |
8730 | ||
8731 | oqs->queue_id = ntohl(qs11->queue_id); | |
6dc34a0d BP |
8732 | oqs->tx_bytes = ntohll(qs11->tx_bytes); |
8733 | oqs->tx_packets = ntohll(qs11->tx_packets); | |
8734 | oqs->tx_errors = ntohll(qs11->tx_errors); | |
8735 | oqs->duration_sec = oqs->duration_nsec = UINT32_MAX; | |
64626975 SH |
8736 | |
8737 | return 0; | |
8738 | } | |
8739 | ||
2e1ae200 JR |
8740 | static enum ofperr |
8741 | ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs, | |
8742 | const struct ofp13_queue_stats *qs13) | |
8743 | { | |
6dc34a0d | 8744 | enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs); |
2e1ae200 | 8745 | if (!error) { |
6dc34a0d BP |
8746 | oqs->duration_sec = ntohl(qs13->duration_sec); |
8747 | oqs->duration_nsec = ntohl(qs13->duration_nsec); | |
2e1ae200 JR |
8748 | } |
8749 | ||
8750 | return error; | |
8751 | } | |
8752 | ||
1bb2cdbe BP |
8753 | static enum ofperr |
8754 | ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs, | |
8755 | struct ofpbuf *msg) | |
8756 | { | |
8757 | const struct ofp14_queue_stats *qs14; | |
8758 | size_t len; | |
8759 | ||
8760 | qs14 = ofpbuf_try_pull(msg, sizeof *qs14); | |
8761 | if (!qs14) { | |
8762 | return OFPERR_OFPBRC_BAD_LEN; | |
8763 | } | |
8764 | ||
8765 | len = ntohs(qs14->length); | |
6fd6ed71 | 8766 | if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) { |
1bb2cdbe BP |
8767 | return OFPERR_OFPBRC_BAD_LEN; |
8768 | } | |
8769 | ofpbuf_pull(msg, len - sizeof *qs14); | |
8770 | ||
8771 | /* No properties yet defined, so ignore them for now. */ | |
8772 | ||
8773 | return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs); | |
8774 | } | |
8775 | ||
64626975 SH |
8776 | /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract |
8777 | * ofputil_queue_stats in 'qs'. | |
8778 | * | |
8779 | * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow | |
8780 | * message. Calling this function multiple times for a single 'msg' iterates | |
8781 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
8782 | * null and not modify them between calls. | |
8783 | * | |
8784 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
8785 | * otherwise a positive errno value. */ | |
8786 | int | |
8787 | ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg) | |
8788 | { | |
8789 | enum ofperr error; | |
8790 | enum ofpraw raw; | |
8791 | ||
6fd6ed71 | 8792 | error = (msg->header ? ofpraw_decode(&raw, msg->header) |
64626975 SH |
8793 | : ofpraw_pull(&raw, msg)); |
8794 | if (error) { | |
8795 | return error; | |
8796 | } | |
8797 | ||
6fd6ed71 | 8798 | if (!msg->size) { |
64626975 | 8799 | return EOF; |
1bb2cdbe BP |
8800 | } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) { |
8801 | return ofputil_pull_ofp14_queue_stats(qs, msg); | |
2e1ae200 JR |
8802 | } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) { |
8803 | const struct ofp13_queue_stats *qs13; | |
8804 | ||
8805 | qs13 = ofpbuf_try_pull(msg, sizeof *qs13); | |
8806 | if (!qs13) { | |
8807 | goto bad_len; | |
8808 | } | |
8809 | return ofputil_queue_stats_from_ofp13(qs, qs13); | |
64626975 SH |
8810 | } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) { |
8811 | const struct ofp11_queue_stats *qs11; | |
8812 | ||
8813 | qs11 = ofpbuf_try_pull(msg, sizeof *qs11); | |
8814 | if (!qs11) { | |
2e1ae200 | 8815 | goto bad_len; |
64626975 SH |
8816 | } |
8817 | return ofputil_queue_stats_from_ofp11(qs, qs11); | |
8818 | } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) { | |
8819 | const struct ofp10_queue_stats *qs10; | |
8820 | ||
8821 | qs10 = ofpbuf_try_pull(msg, sizeof *qs10); | |
8822 | if (!qs10) { | |
2e1ae200 | 8823 | goto bad_len; |
64626975 SH |
8824 | } |
8825 | return ofputil_queue_stats_from_ofp10(qs, qs10); | |
8826 | } else { | |
428b2edd | 8827 | OVS_NOT_REACHED(); |
64626975 | 8828 | } |
2e1ae200 JR |
8829 | |
8830 | bad_len: | |
437d0d22 | 8831 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover " |
6fd6ed71 | 8832 | "bytes at end", msg->size); |
2e1ae200 | 8833 | return OFPERR_OFPBRC_BAD_LEN; |
64626975 SH |
8834 | } |
8835 | ||
8836 | static void | |
8837 | ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs, | |
8838 | struct ofp10_queue_stats *qs10) | |
8839 | { | |
4e022ec0 | 8840 | qs10->port_no = htons(ofp_to_u16(oqs->port_no)); |
64626975 SH |
8841 | memset(qs10->pad, 0, sizeof qs10->pad); |
8842 | qs10->queue_id = htonl(oqs->queue_id); | |
6dc34a0d BP |
8843 | put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes)); |
8844 | put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets)); | |
8845 | put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors)); | |
64626975 SH |
8846 | } |
8847 | ||
8848 | static void | |
8849 | ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs, | |
8850 | struct ofp11_queue_stats *qs11) | |
8851 | { | |
8852 | qs11->port_no = ofputil_port_to_ofp11(oqs->port_no); | |
8853 | qs11->queue_id = htonl(oqs->queue_id); | |
6dc34a0d BP |
8854 | qs11->tx_bytes = htonll(oqs->tx_bytes); |
8855 | qs11->tx_packets = htonll(oqs->tx_packets); | |
8856 | qs11->tx_errors = htonll(oqs->tx_errors); | |
64626975 SH |
8857 | } |
8858 | ||
2e1ae200 JR |
8859 | static void |
8860 | ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs, | |
8861 | struct ofp13_queue_stats *qs13) | |
8862 | { | |
8863 | ofputil_queue_stats_to_ofp11(oqs, &qs13->qs); | |
6dc34a0d BP |
8864 | if (oqs->duration_sec != UINT32_MAX) { |
8865 | qs13->duration_sec = htonl(oqs->duration_sec); | |
8866 | qs13->duration_nsec = htonl(oqs->duration_nsec); | |
8867 | } else { | |
b8266395 BP |
8868 | qs13->duration_sec = OVS_BE32_MAX; |
8869 | qs13->duration_nsec = OVS_BE32_MAX; | |
6dc34a0d | 8870 | } |
2e1ae200 JR |
8871 | } |
8872 | ||
1bb2cdbe BP |
8873 | static void |
8874 | ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs, | |
8875 | struct ofp14_queue_stats *qs14) | |
8876 | { | |
8877 | qs14->length = htons(sizeof *qs14); | |
8878 | memset(qs14->pad, 0, sizeof qs14->pad); | |
8879 | ofputil_queue_stats_to_ofp13(oqs, &qs14->qs); | |
8880 | } | |
8881 | ||
8882 | ||
64626975 SH |
8883 | /* Encode a queue stat for 'oqs' and append it to 'replies'. */ |
8884 | void | |
ca6ba700 | 8885 | ofputil_append_queue_stat(struct ovs_list *replies, |
64626975 SH |
8886 | const struct ofputil_queue_stats *oqs) |
8887 | { | |
e28ac5cf | 8888 | switch (ofpmp_version(replies)) { |
2e1ae200 JR |
8889 | case OFP13_VERSION: { |
8890 | struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply); | |
8891 | ofputil_queue_stats_to_ofp13(oqs, reply); | |
8892 | break; | |
8893 | } | |
8894 | ||
64626975 SH |
8895 | case OFP12_VERSION: |
8896 | case OFP11_VERSION: { | |
2e1ae200 | 8897 | struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply); |
64626975 SH |
8898 | ofputil_queue_stats_to_ofp11(oqs, reply); |
8899 | break; | |
8900 | } | |
8901 | ||
8902 | case OFP10_VERSION: { | |
2e1ae200 | 8903 | struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply); |
64626975 SH |
8904 | ofputil_queue_stats_to_ofp10(oqs, reply); |
8905 | break; | |
8906 | } | |
8907 | ||
42dccab5 BP |
8908 | case OFP14_VERSION: |
8909 | case OFP15_VERSION: { | |
1bb2cdbe BP |
8910 | struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply); |
8911 | ofputil_queue_stats_to_ofp14(oqs, reply); | |
c37c0382 | 8912 | break; |
1bb2cdbe | 8913 | } |
c37c0382 | 8914 | |
64626975 | 8915 | default: |
428b2edd | 8916 | OVS_NOT_REACHED(); |
64626975 SH |
8917 | } |
8918 | } | |
777af88d AC |
8919 | |
8920 | enum ofperr | |
8921 | ofputil_decode_bundle_ctrl(const struct ofp_header *oh, | |
8922 | struct ofputil_bundle_ctrl_msg *msg) | |
8923 | { | |
8924 | struct ofpbuf b; | |
8925 | enum ofpraw raw; | |
8926 | const struct ofp14_bundle_ctrl_msg *m; | |
8927 | ||
8928 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
8929 | raw = ofpraw_pull_assert(&b); | |
8930 | ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL); | |
8931 | ||
6fd6ed71 | 8932 | m = b.msg; |
777af88d AC |
8933 | msg->bundle_id = ntohl(m->bundle_id); |
8934 | msg->type = ntohs(m->type); | |
8935 | msg->flags = ntohs(m->flags); | |
8936 | ||
8937 | return 0; | |
8938 | } | |
8939 | ||
db5076ee JR |
8940 | struct ofpbuf * |
8941 | ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version, | |
8942 | struct ofputil_bundle_ctrl_msg *bc) | |
8943 | { | |
8944 | struct ofpbuf *request; | |
8945 | struct ofp14_bundle_ctrl_msg *m; | |
8946 | ||
8947 | switch (ofp_version) { | |
8948 | case OFP10_VERSION: | |
8949 | case OFP11_VERSION: | |
8950 | case OFP12_VERSION: | |
8951 | case OFP13_VERSION: | |
8952 | ovs_fatal(0, "bundles need OpenFlow 1.4 or later " | |
8953 | "(\'-O OpenFlow14\')"); | |
8954 | case OFP14_VERSION: | |
8955 | case OFP15_VERSION: | |
8956 | request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0); | |
8957 | m = ofpbuf_put_zeros(request, sizeof *m); | |
8958 | ||
8959 | m->bundle_id = htonl(bc->bundle_id); | |
8960 | m->type = htons(bc->type); | |
8961 | m->flags = htons(bc->flags); | |
8962 | break; | |
8963 | default: | |
8964 | OVS_NOT_REACHED(); | |
8965 | } | |
8966 | ||
8967 | return request; | |
8968 | } | |
8969 | ||
777af88d AC |
8970 | struct ofpbuf * |
8971 | ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh, | |
8972 | struct ofputil_bundle_ctrl_msg *msg) | |
8973 | { | |
8974 | struct ofpbuf *buf; | |
8975 | struct ofp14_bundle_ctrl_msg *m; | |
8976 | ||
8977 | buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0); | |
8978 | m = ofpbuf_put_zeros(buf, sizeof *m); | |
8979 | ||
8980 | m->bundle_id = htonl(msg->bundle_id); | |
8981 | m->type = htons(msg->type); | |
8982 | m->flags = htons(msg->flags); | |
8983 | ||
8984 | return buf; | |
8985 | } | |
8986 | ||
c25ce22d JR |
8987 | /* Return true for bundlable state change requests, false for other messages. |
8988 | */ | |
8989 | static bool | |
8990 | ofputil_is_bundlable(enum ofptype type) | |
8991 | { | |
8992 | switch (type) { | |
8993 | /* Minimum required by OpenFlow 1.4. */ | |
8994 | case OFPTYPE_PORT_MOD: | |
8995 | case OFPTYPE_FLOW_MOD: | |
8996 | return true; | |
8997 | ||
8998 | /* Nice to have later. */ | |
8999 | case OFPTYPE_FLOW_MOD_TABLE_ID: | |
9000 | case OFPTYPE_GROUP_MOD: | |
9001 | case OFPTYPE_TABLE_MOD: | |
9002 | case OFPTYPE_METER_MOD: | |
9003 | case OFPTYPE_PACKET_OUT: | |
6159c531 | 9004 | case OFPTYPE_NXT_GENEVE_TABLE_MOD: |
c25ce22d JR |
9005 | |
9006 | /* Not to be bundlable. */ | |
9007 | case OFPTYPE_ECHO_REQUEST: | |
9008 | case OFPTYPE_FEATURES_REQUEST: | |
9009 | case OFPTYPE_GET_CONFIG_REQUEST: | |
9010 | case OFPTYPE_SET_CONFIG: | |
9011 | case OFPTYPE_BARRIER_REQUEST: | |
9012 | case OFPTYPE_ROLE_REQUEST: | |
9013 | case OFPTYPE_ECHO_REPLY: | |
9014 | case OFPTYPE_SET_FLOW_FORMAT: | |
9015 | case OFPTYPE_SET_PACKET_IN_FORMAT: | |
9016 | case OFPTYPE_SET_CONTROLLER_ID: | |
9017 | case OFPTYPE_FLOW_AGE: | |
9018 | case OFPTYPE_FLOW_MONITOR_CANCEL: | |
9019 | case OFPTYPE_SET_ASYNC_CONFIG: | |
9020 | case OFPTYPE_GET_ASYNC_REQUEST: | |
9021 | case OFPTYPE_DESC_STATS_REQUEST: | |
9022 | case OFPTYPE_FLOW_STATS_REQUEST: | |
9023 | case OFPTYPE_AGGREGATE_STATS_REQUEST: | |
9024 | case OFPTYPE_TABLE_STATS_REQUEST: | |
9025 | case OFPTYPE_TABLE_FEATURES_STATS_REQUEST: | |
03c72922 | 9026 | case OFPTYPE_TABLE_DESC_REQUEST: |
c25ce22d JR |
9027 | case OFPTYPE_PORT_STATS_REQUEST: |
9028 | case OFPTYPE_QUEUE_STATS_REQUEST: | |
9029 | case OFPTYPE_PORT_DESC_STATS_REQUEST: | |
9030 | case OFPTYPE_FLOW_MONITOR_STATS_REQUEST: | |
9031 | case OFPTYPE_METER_STATS_REQUEST: | |
9032 | case OFPTYPE_METER_CONFIG_STATS_REQUEST: | |
9033 | case OFPTYPE_METER_FEATURES_STATS_REQUEST: | |
9034 | case OFPTYPE_GROUP_STATS_REQUEST: | |
9035 | case OFPTYPE_GROUP_DESC_STATS_REQUEST: | |
9036 | case OFPTYPE_GROUP_FEATURES_STATS_REQUEST: | |
9037 | case OFPTYPE_QUEUE_GET_CONFIG_REQUEST: | |
9038 | case OFPTYPE_BUNDLE_CONTROL: | |
9039 | case OFPTYPE_BUNDLE_ADD_MESSAGE: | |
9040 | case OFPTYPE_HELLO: | |
9041 | case OFPTYPE_ERROR: | |
9042 | case OFPTYPE_FEATURES_REPLY: | |
9043 | case OFPTYPE_GET_CONFIG_REPLY: | |
9044 | case OFPTYPE_PACKET_IN: | |
9045 | case OFPTYPE_FLOW_REMOVED: | |
9046 | case OFPTYPE_PORT_STATUS: | |
9047 | case OFPTYPE_BARRIER_REPLY: | |
9048 | case OFPTYPE_QUEUE_GET_CONFIG_REPLY: | |
9049 | case OFPTYPE_DESC_STATS_REPLY: | |
9050 | case OFPTYPE_FLOW_STATS_REPLY: | |
9051 | case OFPTYPE_QUEUE_STATS_REPLY: | |
9052 | case OFPTYPE_PORT_STATS_REPLY: | |
9053 | case OFPTYPE_TABLE_STATS_REPLY: | |
9054 | case OFPTYPE_AGGREGATE_STATS_REPLY: | |
9055 | case OFPTYPE_PORT_DESC_STATS_REPLY: | |
9056 | case OFPTYPE_ROLE_REPLY: | |
9057 | case OFPTYPE_FLOW_MONITOR_PAUSED: | |
9058 | case OFPTYPE_FLOW_MONITOR_RESUMED: | |
9059 | case OFPTYPE_FLOW_MONITOR_STATS_REPLY: | |
9060 | case OFPTYPE_GET_ASYNC_REPLY: | |
9061 | case OFPTYPE_GROUP_STATS_REPLY: | |
9062 | case OFPTYPE_GROUP_DESC_STATS_REPLY: | |
9063 | case OFPTYPE_GROUP_FEATURES_STATS_REPLY: | |
9064 | case OFPTYPE_METER_STATS_REPLY: | |
9065 | case OFPTYPE_METER_CONFIG_STATS_REPLY: | |
9066 | case OFPTYPE_METER_FEATURES_STATS_REPLY: | |
9067 | case OFPTYPE_TABLE_FEATURES_STATS_REPLY: | |
03c72922 | 9068 | case OFPTYPE_TABLE_DESC_REPLY: |
c25ce22d | 9069 | case OFPTYPE_ROLE_STATUS: |
6159c531 JG |
9070 | case OFPTYPE_NXT_GENEVE_TABLE_REQUEST: |
9071 | case OFPTYPE_NXT_GENEVE_TABLE_REPLY: | |
c25ce22d JR |
9072 | break; |
9073 | } | |
9074 | ||
9075 | return false; | |
9076 | } | |
9077 | ||
777af88d AC |
9078 | enum ofperr |
9079 | ofputil_decode_bundle_add(const struct ofp_header *oh, | |
7ac27a04 JR |
9080 | struct ofputil_bundle_add_msg *msg, |
9081 | enum ofptype *type_ptr) | |
777af88d AC |
9082 | { |
9083 | const struct ofp14_bundle_ctrl_msg *m; | |
9084 | struct ofpbuf b; | |
9085 | enum ofpraw raw; | |
9086 | size_t inner_len; | |
c25ce22d JR |
9087 | enum ofperr error; |
9088 | enum ofptype type; | |
777af88d AC |
9089 | |
9090 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
9091 | raw = ofpraw_pull_assert(&b); | |
9092 | ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE); | |
9093 | ||
9094 | m = ofpbuf_pull(&b, sizeof *m); | |
9095 | msg->bundle_id = ntohl(m->bundle_id); | |
9096 | msg->flags = ntohs(m->flags); | |
9097 | ||
6fd6ed71 | 9098 | msg->msg = b.data; |
46be7132 BP |
9099 | if (msg->msg->version != oh->version) { |
9100 | return OFPERR_NXBFC_BAD_VERSION; | |
9101 | } | |
777af88d | 9102 | inner_len = ntohs(msg->msg->length); |
6fd6ed71 | 9103 | if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) { |
777af88d AC |
9104 | return OFPERR_OFPBFC_MSG_BAD_LEN; |
9105 | } | |
be6f6393 JR |
9106 | if (msg->msg->xid != oh->xid) { |
9107 | return OFPERR_OFPBFC_MSG_BAD_XID; | |
9108 | } | |
777af88d | 9109 | |
c25ce22d | 9110 | /* Reject unbundlable messages. */ |
7ac27a04 JR |
9111 | if (!type_ptr) { |
9112 | type_ptr = &type; | |
9113 | } | |
9114 | error = ofptype_decode(type_ptr, msg->msg); | |
c25ce22d JR |
9115 | if (error) { |
9116 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained " | |
9117 | "message is unparsable (%s)", ofperr_get_name(error)); | |
9118 | return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */ | |
9119 | } | |
9120 | ||
7ac27a04 | 9121 | if (!ofputil_is_bundlable(*type_ptr)) { |
44648b0f BP |
9122 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside " |
9123 | "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(*type_ptr)); | |
c25ce22d JR |
9124 | return OFPERR_OFPBFC_MSG_UNSUP; |
9125 | } | |
9126 | ||
777af88d AC |
9127 | return 0; |
9128 | } | |
9129 | ||
9130 | struct ofpbuf * | |
9131 | ofputil_encode_bundle_add(enum ofp_version ofp_version, | |
9132 | struct ofputil_bundle_add_msg *msg) | |
9133 | { | |
9134 | struct ofpbuf *request; | |
9135 | struct ofp14_bundle_ctrl_msg *m; | |
9136 | ||
7b3dca89 JR |
9137 | /* Must use the same xid as the embedded message. */ |
9138 | request = ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version, | |
9139 | msg->msg->xid, 0); | |
777af88d AC |
9140 | m = ofpbuf_put_zeros(request, sizeof *m); |
9141 | ||
9142 | m->bundle_id = htonl(msg->bundle_id); | |
9143 | m->flags = htons(msg->flags); | |
9144 | ofpbuf_put(request, msg->msg, ntohs(msg->msg->length)); | |
9145 | ||
9146 | return request; | |
9147 | } | |
6159c531 JG |
9148 | |
9149 | static void | |
9150 | encode_geneve_table_mappings(struct ofpbuf *b, struct ovs_list *mappings) | |
9151 | { | |
9152 | struct ofputil_geneve_map *map; | |
9153 | ||
9154 | LIST_FOR_EACH (map, list_node, mappings) { | |
9155 | struct nx_geneve_map *nx_map; | |
9156 | ||
9157 | nx_map = ofpbuf_put_zeros(b, sizeof *nx_map); | |
9158 | nx_map->option_class = htons(map->option_class); | |
9159 | nx_map->option_type = map->option_type; | |
9160 | nx_map->option_len = map->option_len; | |
9161 | nx_map->index = htons(map->index); | |
9162 | } | |
9163 | } | |
9164 | ||
9165 | struct ofpbuf * | |
9166 | ofputil_encode_geneve_table_mod(enum ofp_version ofp_version, | |
9167 | struct ofputil_geneve_table_mod *gtm) | |
9168 | { | |
9169 | struct ofpbuf *b; | |
9170 | struct nx_geneve_table_mod *nx_gtm; | |
9171 | ||
9172 | b = ofpraw_alloc(OFPRAW_NXT_GENEVE_TABLE_MOD, ofp_version, 0); | |
9173 | nx_gtm = ofpbuf_put_zeros(b, sizeof *nx_gtm); | |
9174 | nx_gtm->command = htons(gtm->command); | |
9175 | encode_geneve_table_mappings(b, >m->mappings); | |
9176 | ||
9177 | return b; | |
9178 | } | |
9179 | ||
9180 | static enum ofperr | |
9181 | decode_geneve_table_mappings(struct ofpbuf *msg, struct ovs_list *mappings) | |
9182 | { | |
9183 | list_init(mappings); | |
9184 | ||
9185 | while (msg->size) { | |
9186 | struct nx_geneve_map *nx_map; | |
9187 | struct ofputil_geneve_map *map; | |
9188 | ||
9189 | nx_map = ofpbuf_pull(msg, sizeof *nx_map); | |
9190 | map = xmalloc(sizeof *map); | |
9191 | list_push_back(mappings, &map->list_node); | |
9192 | ||
9193 | map->option_class = ntohs(nx_map->option_class); | |
9194 | map->option_type = nx_map->option_type; | |
9195 | ||
9196 | map->option_len = nx_map->option_len; | |
9197 | if (map->option_len == 0 || map->option_len % 4 || | |
9198 | map->option_len > GENEVE_MAX_OPT_SIZE) { | |
9199 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
9200 | "geneve table option length (%u) is not a valid option size", | |
9201 | map->option_len); | |
9202 | ofputil_uninit_geneve_table(mappings); | |
9203 | return OFPERR_NXGTMFC_BAD_OPT_LEN; | |
9204 | } | |
9205 | ||
9206 | map->index = ntohs(nx_map->index); | |
9207 | if (map->index >= TUN_METADATA_NUM_OPTS) { | |
9208 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
9209 | "geneve table field index (%u) is too large (max %u)", | |
9210 | map->index, TUN_METADATA_NUM_OPTS - 1); | |
9211 | ofputil_uninit_geneve_table(mappings); | |
9212 | return OFPERR_NXGTMFC_BAD_FIELD_IDX; | |
9213 | } | |
9214 | } | |
9215 | ||
9216 | return 0; | |
9217 | } | |
9218 | ||
9219 | enum ofperr | |
9220 | ofputil_decode_geneve_table_mod(const struct ofp_header *oh, | |
9221 | struct ofputil_geneve_table_mod *gtm) | |
9222 | { | |
9223 | struct ofpbuf msg; | |
9224 | struct nx_geneve_table_mod *nx_gtm; | |
9225 | ||
9226 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
9227 | ofpraw_pull_assert(&msg); | |
9228 | ||
9229 | nx_gtm = ofpbuf_pull(&msg, sizeof *nx_gtm); | |
9230 | gtm->command = ntohs(nx_gtm->command); | |
9231 | if (gtm->command > NXGTMC_CLEAR) { | |
9232 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
9233 | "geneve table mod command (%u) is out of range", | |
9234 | gtm->command); | |
9235 | return OFPERR_NXGTMFC_BAD_COMMAND; | |
9236 | } | |
9237 | ||
9238 | return decode_geneve_table_mappings(&msg, >m->mappings); | |
9239 | } | |
9240 | ||
9241 | struct ofpbuf * | |
9242 | ofputil_encode_geneve_table_reply(const struct ofp_header *oh, | |
9243 | struct ofputil_geneve_table_reply *gtr) | |
9244 | { | |
9245 | struct ofpbuf *b; | |
9246 | struct nx_geneve_table_reply *nx_gtr; | |
9247 | ||
9248 | b = ofpraw_alloc_reply(OFPRAW_NXT_GENEVE_TABLE_REPLY, oh, 0); | |
9249 | nx_gtr = ofpbuf_put_zeros(b, sizeof *nx_gtr); | |
9250 | nx_gtr->max_option_space = htonl(gtr->max_option_space); | |
9251 | nx_gtr->max_fields = htons(gtr->max_fields); | |
9252 | ||
9253 | encode_geneve_table_mappings(b, >r->mappings); | |
9254 | ||
9255 | return b; | |
9256 | } | |
9257 | ||
9258 | enum ofperr | |
9259 | ofputil_decode_geneve_table_reply(const struct ofp_header *oh, | |
9260 | struct ofputil_geneve_table_reply *gtr) | |
9261 | { | |
9262 | struct ofpbuf msg; | |
9263 | struct nx_geneve_table_reply *nx_gtr; | |
9264 | ||
9265 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
9266 | ofpraw_pull_assert(&msg); | |
9267 | ||
9268 | nx_gtr = ofpbuf_pull(&msg, sizeof *nx_gtr); | |
9269 | gtr->max_option_space = ntohl(nx_gtr->max_option_space); | |
9270 | gtr->max_fields = ntohs(nx_gtr->max_fields); | |
9271 | ||
9272 | return decode_geneve_table_mappings(&msg, >r->mappings); | |
9273 | } | |
9274 | ||
9275 | void | |
9276 | ofputil_uninit_geneve_table(struct ovs_list *mappings) | |
9277 | { | |
9278 | struct ofputil_geneve_map *map; | |
9279 | ||
9280 | LIST_FOR_EACH_POP (map, list_node, mappings) { | |
9281 | free(map); | |
9282 | } | |
9283 | } |