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