]>
Commit | Line | Data |
---|---|---|
fa37b408 | 1 | /* |
dc4762ed | 2 | * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. |
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" | |
dc4762ed | 19 | #include <errno.h> |
fa37b408 BP |
20 | #include <inttypes.h> |
21 | #include <stdlib.h> | |
3b6a2571 | 22 | #include "autopath.h" |
10a24935 | 23 | #include "byte-order.h" |
d8ae4d67 | 24 | #include "classifier.h" |
dc4762ed | 25 | #include "dynamic-string.h" |
53ddd40a | 26 | #include "multipath.h" |
b6c9e612 | 27 | #include "nx-match.h" |
dc4762ed | 28 | #include "ofp-errors.h" |
fa37b408 BP |
29 | #include "ofp-util.h" |
30 | #include "ofpbuf.h" | |
31 | #include "packets.h" | |
32 | #include "random.h" | |
4ffd1b43 | 33 | #include "unaligned.h" |
e41a9130 | 34 | #include "type-props.h" |
5136ce49 | 35 | #include "vlog.h" |
fa37b408 | 36 | |
d98e6007 | 37 | VLOG_DEFINE_THIS_MODULE(ofp_util); |
fa37b408 | 38 | |
1c0b7503 | 39 | static ovs_be32 normalize_wildcards(const struct ofp_match *); |
34276a57 | 40 | |
fa37b408 BP |
41 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug |
42 | * in the peer and so there's not much point in showing a lot of them. */ | |
43 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
44 | ||
0596e897 BP |
45 | /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns |
46 | * an IP netmask with a 1 in each bit that must match and a 0 in each bit that | |
47 | * is wildcarded. | |
48 | * | |
49 | * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0 | |
50 | * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, | |
51 | * ..., 32 and higher wildcard the entire field. This is the *opposite* of the | |
52 | * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are | |
53 | * wildcarded. */ | |
54 | ovs_be32 | |
55 | ofputil_wcbits_to_netmask(int wcbits) | |
56 | { | |
57 | wcbits &= 0x3f; | |
58 | return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0; | |
59 | } | |
60 | ||
61 | /* Given the IP netmask 'netmask', returns the number of bits of the IP address | |
62 | * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */ | |
63 | int | |
64 | ofputil_netmask_to_wcbits(ovs_be32 netmask) | |
65 | { | |
66 | assert(ip_is_cidr(netmask)); | |
67 | #if __GNUC__ >= 4 | |
68 | return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask)); | |
69 | #else | |
70 | int wcbits; | |
71 | ||
72 | for (wcbits = 32; netmask; wcbits--) { | |
73 | netmask &= netmask - 1; | |
74 | } | |
75 | ||
76 | return wcbits; | |
77 | #endif | |
78 | } | |
79 | ||
d8ae4d67 BP |
80 | /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and |
81 | * name. */ | |
82 | #define WC_INVARIANT_LIST \ | |
83 | WC_INVARIANT_BIT(IN_PORT) \ | |
d8ae4d67 BP |
84 | WC_INVARIANT_BIT(DL_SRC) \ |
85 | WC_INVARIANT_BIT(DL_DST) \ | |
86 | WC_INVARIANT_BIT(DL_TYPE) \ | |
87 | WC_INVARIANT_BIT(NW_PROTO) \ | |
88 | WC_INVARIANT_BIT(TP_SRC) \ | |
89 | WC_INVARIANT_BIT(TP_DST) | |
90 | ||
91 | /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST) | |
92 | * actually have the same names and values. */ | |
93 | #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME); | |
94 | WC_INVARIANT_LIST | |
95 | #undef WC_INVARIANT_BIT | |
96 | ||
97 | /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all | |
98 | * OR'd together. */ | |
eeba8e4f | 99 | static const flow_wildcards_t WC_INVARIANTS = 0 |
d8ae4d67 BP |
100 | #define WC_INVARIANT_BIT(NAME) | FWW_##NAME |
101 | WC_INVARIANT_LIST | |
102 | #undef WC_INVARIANT_BIT | |
eeba8e4f | 103 | ; |
d8ae4d67 BP |
104 | |
105 | /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given | |
b78f6b77 | 106 | * 'priority'. */ |
d8ae4d67 BP |
107 | void |
108 | ofputil_cls_rule_from_match(const struct ofp_match *match, | |
b78f6b77 | 109 | unsigned int priority, struct cls_rule *rule) |
d8ae4d67 BP |
110 | { |
111 | struct flow_wildcards *wc = &rule->wc; | |
eeba8e4f | 112 | uint32_t ofpfw; |
66642cb4 | 113 | ovs_be16 vid, pcp; |
d8ae4d67 BP |
114 | |
115 | /* Initialize rule->priority. */ | |
b78f6b77 | 116 | ofpfw = ntohl(match->wildcards) & OFPFW_ALL; |
d8ae4d67 BP |
117 | rule->priority = !ofpfw ? UINT16_MAX : priority; |
118 | ||
119 | /* Initialize most of rule->wc. */ | |
f9ba8dad | 120 | flow_wildcards_init_catchall(wc); |
eeba8e4f | 121 | wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS; |
bad68a99 JP |
122 | |
123 | /* Wildcard fields that aren't defined by ofp_match or tun_id. */ | |
685a51a5 | 124 | wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET); |
bad68a99 | 125 | |
d8ae4d67 BP |
126 | if (ofpfw & OFPFW_NW_TOS) { |
127 | wc->wildcards |= FWW_NW_TOS; | |
128 | } | |
d8ae4d67 BP |
129 | wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT); |
130 | wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT); | |
131 | ||
d8ae4d67 BP |
132 | if (ofpfw & OFPFW_DL_DST) { |
133 | /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but | |
134 | * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST | |
135 | * and FWW_ETH_MCAST. */ | |
136 | wc->wildcards |= FWW_ETH_MCAST; | |
137 | } | |
138 | ||
66642cb4 | 139 | /* Initialize most of rule->flow. */ |
d8ae4d67 BP |
140 | rule->flow.nw_src = match->nw_src; |
141 | rule->flow.nw_dst = match->nw_dst; | |
abe529af | 142 | rule->flow.in_port = ntohs(match->in_port); |
36956a7d | 143 | rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type); |
d8ae4d67 BP |
144 | rule->flow.tp_src = match->tp_src; |
145 | rule->flow.tp_dst = match->tp_dst; | |
146 | memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN); | |
147 | memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN); | |
148 | rule->flow.nw_tos = match->nw_tos; | |
149 | rule->flow.nw_proto = match->nw_proto; | |
150 | ||
66642cb4 BP |
151 | /* Translate VLANs. */ |
152 | vid = match->dl_vlan & htons(VLAN_VID_MASK); | |
153 | pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK); | |
154 | switch (ofpfw & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) { | |
155 | case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP: | |
156 | /* Wildcard everything. */ | |
157 | rule->flow.vlan_tci = htons(0); | |
158 | rule->wc.vlan_tci_mask = htons(0); | |
159 | break; | |
160 | ||
161 | case OFPFW_DL_VLAN_PCP: | |
162 | if (match->dl_vlan == htons(OFP_VLAN_NONE)) { | |
163 | /* Match only packets without 802.1Q header. */ | |
164 | rule->flow.vlan_tci = htons(0); | |
165 | rule->wc.vlan_tci_mask = htons(0xffff); | |
166 | } else { | |
167 | /* Wildcard PCP, specific VID. */ | |
168 | rule->flow.vlan_tci = vid | htons(VLAN_CFI); | |
169 | rule->wc.vlan_tci_mask = htons(VLAN_VID_MASK | VLAN_CFI); | |
170 | } | |
171 | break; | |
172 | ||
173 | case OFPFW_DL_VLAN: | |
174 | /* Wildcard VID, specific PCP. */ | |
175 | rule->flow.vlan_tci = pcp | htons(VLAN_CFI); | |
176 | rule->wc.vlan_tci_mask = htons(VLAN_PCP_MASK | VLAN_CFI); | |
177 | break; | |
178 | ||
179 | case 0: | |
180 | if (match->dl_vlan == htons(OFP_VLAN_NONE)) { | |
181 | /* This case is odd, since we can't have a specific PCP without an | |
182 | * 802.1Q header. However, older versions of OVS treated this as | |
183 | * matching packets withut an 802.1Q header, so we do here too. */ | |
184 | rule->flow.vlan_tci = htons(0); | |
185 | rule->wc.vlan_tci_mask = htons(0xffff); | |
186 | } else { | |
187 | /* Specific VID and PCP. */ | |
188 | rule->flow.vlan_tci = vid | pcp | htons(VLAN_CFI); | |
189 | rule->wc.vlan_tci_mask = htons(0xffff); | |
190 | } | |
191 | break; | |
192 | } | |
193 | ||
d8ae4d67 BP |
194 | /* Clean up. */ |
195 | cls_rule_zero_wildcarded_fields(rule); | |
196 | } | |
197 | ||
b78f6b77 | 198 | /* Convert 'rule' into the OpenFlow match structure 'match'. */ |
d8ae4d67 | 199 | void |
b78f6b77 | 200 | ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match) |
d8ae4d67 BP |
201 | { |
202 | const struct flow_wildcards *wc = &rule->wc; | |
eeba8e4f | 203 | uint32_t ofpfw; |
d8ae4d67 | 204 | |
66642cb4 | 205 | /* Figure out most OpenFlow wildcards. */ |
eeba8e4f | 206 | ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS); |
d8ae4d67 BP |
207 | ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT; |
208 | ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT; | |
d8ae4d67 BP |
209 | if (wc->wildcards & FWW_NW_TOS) { |
210 | ofpfw |= OFPFW_NW_TOS; | |
211 | } | |
ff9d3826 | 212 | |
66642cb4 BP |
213 | /* Translate VLANs. */ |
214 | match->dl_vlan = htons(0); | |
215 | match->dl_vlan_pcp = 0; | |
216 | if (rule->wc.vlan_tci_mask == htons(0)) { | |
217 | ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP; | |
218 | } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI) | |
219 | && !(rule->flow.vlan_tci & htons(VLAN_CFI))) { | |
220 | match->dl_vlan = htons(OFP_VLAN_NONE); | |
221 | } else { | |
222 | if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) { | |
223 | ofpfw |= OFPFW_DL_VLAN; | |
224 | } else { | |
225 | match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci)); | |
226 | } | |
227 | ||
228 | if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) { | |
229 | ofpfw |= OFPFW_DL_VLAN_PCP; | |
230 | } else { | |
231 | match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci); | |
232 | } | |
233 | } | |
234 | ||
235 | /* Compose most of the match structure. */ | |
d8ae4d67 | 236 | match->wildcards = htonl(ofpfw); |
abe529af | 237 | match->in_port = htons(rule->flow.in_port); |
d8ae4d67 BP |
238 | memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN); |
239 | memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN); | |
36956a7d | 240 | match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type); |
d8ae4d67 BP |
241 | match->nw_src = rule->flow.nw_src; |
242 | match->nw_dst = rule->flow.nw_dst; | |
243 | match->nw_tos = rule->flow.nw_tos; | |
244 | match->nw_proto = rule->flow.nw_proto; | |
245 | match->tp_src = rule->flow.tp_src; | |
246 | match->tp_dst = rule->flow.tp_dst; | |
247 | memset(match->pad1, '\0', sizeof match->pad1); | |
248 | memset(match->pad2, '\0', sizeof match->pad2); | |
249 | } | |
250 | ||
36956a7d BP |
251 | /* Given a 'dl_type' value in the format used in struct flow, returns the |
252 | * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */ | |
253 | ovs_be16 | |
254 | ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type) | |
255 | { | |
256 | return (flow_dl_type == htons(FLOW_DL_TYPE_NONE) | |
257 | ? htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
258 | : flow_dl_type); | |
259 | } | |
260 | ||
261 | /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match | |
262 | * structure, returns the corresponding 'dl_type' value for use in struct | |
263 | * flow. */ | |
264 | ovs_be16 | |
265 | ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type) | |
266 | { | |
267 | return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
268 | ? htons(FLOW_DL_TYPE_NONE) | |
269 | : ofp_dl_type); | |
270 | } | |
271 | ||
72fae175 | 272 | /* Returns a transaction ID to use for an outgoing OpenFlow message. */ |
44381c1b | 273 | static ovs_be32 |
fa37b408 BP |
274 | alloc_xid(void) |
275 | { | |
72fae175 | 276 | static uint32_t next_xid = 1; |
44381c1b | 277 | return htonl(next_xid++); |
fa37b408 | 278 | } |
d1e2cf21 BP |
279 | \f |
280 | /* Basic parsing of OpenFlow messages. */ | |
fa37b408 | 281 | |
d1e2cf21 BP |
282 | struct ofputil_msg_type { |
283 | enum ofputil_msg_code code; /* OFPUTIL_*. */ | |
284 | uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */ | |
285 | const char *name; /* e.g. "OFPT_FLOW_REMOVED". */ | |
286 | unsigned int min_size; /* Minimum total message size in bytes. */ | |
287 | /* 0 if 'min_size' is the exact size that the message must be. Otherwise, | |
288 | * the message may exceed 'min_size' by an even multiple of this value. */ | |
289 | unsigned int extra_multiple; | |
290 | }; | |
291 | ||
292 | struct ofputil_msg_category { | |
293 | const char *name; /* e.g. "OpenFlow message" */ | |
294 | const struct ofputil_msg_type *types; | |
295 | size_t n_types; | |
296 | int missing_error; /* ofp_mkerr() value for missing type. */ | |
297 | }; | |
298 | ||
299 | static bool | |
300 | ofputil_length_ok(const struct ofputil_msg_category *cat, | |
301 | const struct ofputil_msg_type *type, | |
302 | unsigned int size) | |
303 | { | |
304 | switch (type->extra_multiple) { | |
305 | case 0: | |
306 | if (size != type->min_size) { | |
307 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect " | |
308 | "length %u (expected length %u)", | |
309 | cat->name, type->name, size, type->min_size); | |
310 | return false; | |
311 | } | |
312 | return true; | |
313 | ||
314 | case 1: | |
315 | if (size < type->min_size) { | |
316 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect " | |
317 | "length %u (expected length at least %u bytes)", | |
318 | cat->name, type->name, size, type->min_size); | |
319 | return false; | |
320 | } | |
321 | return true; | |
322 | ||
323 | default: | |
324 | if (size < type->min_size | |
325 | || (size - type->min_size) % type->extra_multiple) { | |
326 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s %s with incorrect " | |
327 | "length %u (must be exactly %u bytes or longer " | |
328 | "by an integer multiple of %u bytes)", | |
329 | cat->name, type->name, size, | |
330 | type->min_size, type->extra_multiple); | |
331 | return false; | |
332 | } | |
333 | return true; | |
334 | } | |
335 | } | |
336 | ||
337 | static int | |
338 | ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat, | |
339 | uint32_t value, unsigned int size, | |
340 | const struct ofputil_msg_type **typep) | |
341 | { | |
342 | const struct ofputil_msg_type *type; | |
343 | ||
344 | for (type = cat->types; type < &cat->types[cat->n_types]; type++) { | |
345 | if (type->value == value) { | |
346 | if (!ofputil_length_ok(cat, type, size)) { | |
347 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
348 | } | |
349 | *typep = type; | |
350 | return 0; | |
351 | } | |
352 | } | |
353 | ||
5c47debb | 354 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32, |
d1e2cf21 BP |
355 | cat->name, value); |
356 | return cat->missing_error; | |
357 | } | |
358 | ||
359 | static int | |
360 | ofputil_decode_vendor(const struct ofp_header *oh, | |
361 | const struct ofputil_msg_type **typep) | |
362 | { | |
6c1491fb BP |
363 | BUILD_ASSERT_DECL(sizeof(struct nxt_set_flow_format) |
364 | != sizeof(struct nxt_flow_mod_table_id)); | |
365 | ||
d1e2cf21 | 366 | static const struct ofputil_msg_type nxt_messages[] = { |
d1e2cf21 BP |
367 | { OFPUTIL_NXT_ROLE_REQUEST, |
368 | NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST", | |
369 | sizeof(struct nx_role_request), 0 }, | |
370 | ||
371 | { OFPUTIL_NXT_ROLE_REPLY, | |
372 | NXT_ROLE_REPLY, "NXT_ROLE_REPLY", | |
373 | sizeof(struct nx_role_request), 0 }, | |
374 | ||
375 | { OFPUTIL_NXT_SET_FLOW_FORMAT, | |
376 | NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT", | |
377 | sizeof(struct nxt_set_flow_format), 0 }, | |
378 | ||
379 | { OFPUTIL_NXT_FLOW_MOD, | |
380 | NXT_FLOW_MOD, "NXT_FLOW_MOD", | |
381 | sizeof(struct nx_flow_mod), 8 }, | |
382 | ||
383 | { OFPUTIL_NXT_FLOW_REMOVED, | |
384 | NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED", | |
385 | sizeof(struct nx_flow_removed), 8 }, | |
386 | }; | |
387 | ||
388 | static const struct ofputil_msg_category nxt_category = { | |
389 | "Nicira extension message", | |
390 | nxt_messages, ARRAY_SIZE(nxt_messages), | |
391 | OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE) | |
392 | }; | |
393 | ||
394 | const struct ofp_vendor_header *ovh; | |
395 | const struct nicira_header *nh; | |
396 | ||
397 | ovh = (const struct ofp_vendor_header *) oh; | |
398 | if (ovh->vendor != htonl(NX_VENDOR_ID)) { | |
399 | VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown " | |
400 | "vendor %"PRIx32, ntohl(ovh->vendor)); | |
401 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR); | |
402 | } | |
403 | ||
404 | if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) { | |
405 | VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of " | |
406 | "length %u (expected at least %zu)", | |
407 | ntohs(ovh->header.length), sizeof(struct nicira_header)); | |
408 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
409 | } | |
410 | ||
411 | nh = (const struct nicira_header *) oh; | |
e16b024e BP |
412 | |
413 | if (nh->subtype == htonl(NXT_FLOW_MOD_TABLE_ID) | |
414 | && oh->length == htons(sizeof(struct nxt_flow_mod_table_id))) { | |
415 | /* NXT_SET_FLOW_FORMAT and NXT_FLOW_MOD_TABLE_ID accidentally have the | |
416 | * same value but different lengths. ofputil_lookup_openflow_message() | |
417 | * doesn't support this case, so special case it here. */ | |
418 | static const struct ofputil_msg_type nxt_flow_mod_table_id = | |
419 | { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, | |
420 | NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID", | |
421 | sizeof(struct nxt_flow_mod_table_id), 0 }; | |
422 | ||
423 | *typep = &nxt_flow_mod_table_id; | |
424 | return 0; | |
425 | } | |
426 | ||
d1e2cf21 BP |
427 | return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype), |
428 | ntohs(oh->length), typep); | |
429 | } | |
430 | ||
431 | static int | |
432 | check_nxstats_msg(const struct ofp_header *oh) | |
433 | { | |
434 | const struct ofp_stats_request *osr; | |
435 | ovs_be32 vendor; | |
436 | ||
437 | osr = (const struct ofp_stats_request *) oh; | |
438 | ||
439 | memcpy(&vendor, osr->body, sizeof vendor); | |
440 | if (vendor != htonl(NX_VENDOR_ID)) { | |
441 | VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for " | |
442 | "unknown vendor %"PRIx32, ntohl(vendor)); | |
443 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR); | |
444 | } | |
445 | ||
446 | if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) { | |
447 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message"); | |
448 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
449 | } | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | static int | |
455 | ofputil_decode_nxst_request(const struct ofp_header *oh, | |
456 | const struct ofputil_msg_type **typep) | |
457 | { | |
458 | static const struct ofputil_msg_type nxst_requests[] = { | |
459 | { OFPUTIL_NXST_FLOW_REQUEST, | |
460 | NXST_FLOW, "NXST_FLOW request", | |
461 | sizeof(struct nx_flow_stats_request), 8 }, | |
462 | ||
463 | { OFPUTIL_NXST_AGGREGATE_REQUEST, | |
464 | NXST_AGGREGATE, "NXST_AGGREGATE request", | |
465 | sizeof(struct nx_aggregate_stats_request), 8 }, | |
466 | }; | |
467 | ||
468 | static const struct ofputil_msg_category nxst_request_category = { | |
08717852 | 469 | "Nicira extension statistics request", |
d1e2cf21 BP |
470 | nxst_requests, ARRAY_SIZE(nxst_requests), |
471 | OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE) | |
472 | }; | |
473 | ||
474 | const struct nicira_stats_msg *nsm; | |
475 | int error; | |
476 | ||
477 | error = check_nxstats_msg(oh); | |
478 | if (error) { | |
479 | return error; | |
480 | } | |
481 | ||
482 | nsm = (struct nicira_stats_msg *) oh; | |
483 | return ofputil_lookup_openflow_message(&nxst_request_category, | |
484 | ntohl(nsm->subtype), | |
485 | ntohs(oh->length), typep); | |
486 | } | |
487 | ||
488 | static int | |
489 | ofputil_decode_nxst_reply(const struct ofp_header *oh, | |
490 | const struct ofputil_msg_type **typep) | |
491 | { | |
492 | static const struct ofputil_msg_type nxst_replies[] = { | |
493 | { OFPUTIL_NXST_FLOW_REPLY, | |
494 | NXST_FLOW, "NXST_FLOW reply", | |
495 | sizeof(struct nicira_stats_msg), 8 }, | |
496 | ||
497 | { OFPUTIL_NXST_AGGREGATE_REPLY, | |
498 | NXST_AGGREGATE, "NXST_AGGREGATE reply", | |
499 | sizeof(struct nx_aggregate_stats_reply), 0 }, | |
500 | }; | |
501 | ||
502 | static const struct ofputil_msg_category nxst_reply_category = { | |
08717852 | 503 | "Nicira extension statistics reply", |
d1e2cf21 BP |
504 | nxst_replies, ARRAY_SIZE(nxst_replies), |
505 | OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE) | |
506 | }; | |
507 | ||
508 | const struct nicira_stats_msg *nsm; | |
509 | int error; | |
510 | ||
511 | error = check_nxstats_msg(oh); | |
512 | if (error) { | |
513 | return error; | |
514 | } | |
515 | ||
516 | nsm = (struct nicira_stats_msg *) oh; | |
517 | return ofputil_lookup_openflow_message(&nxst_reply_category, | |
518 | ntohl(nsm->subtype), | |
519 | ntohs(oh->length), typep); | |
520 | } | |
521 | ||
522 | static int | |
523 | ofputil_decode_ofpst_request(const struct ofp_header *oh, | |
524 | const struct ofputil_msg_type **typep) | |
525 | { | |
526 | enum { OSR_SIZE = sizeof(struct ofp_stats_request) }; | |
527 | static const struct ofputil_msg_type ofpst_requests[] = { | |
528 | { OFPUTIL_OFPST_DESC_REQUEST, | |
529 | OFPST_DESC, "OFPST_DESC request", | |
530 | OSR_SIZE, 0 }, | |
531 | ||
532 | { OFPUTIL_OFPST_FLOW_REQUEST, | |
533 | OFPST_FLOW, "OFPST_FLOW request", | |
534 | OSR_SIZE + sizeof(struct ofp_flow_stats_request), 0 }, | |
535 | ||
536 | { OFPUTIL_OFPST_AGGREGATE_REQUEST, | |
537 | OFPST_AGGREGATE, "OFPST_AGGREGATE request", | |
538 | OSR_SIZE + sizeof(struct ofp_aggregate_stats_request), 0 }, | |
539 | ||
540 | { OFPUTIL_OFPST_TABLE_REQUEST, | |
541 | OFPST_TABLE, "OFPST_TABLE request", | |
542 | OSR_SIZE, 0 }, | |
543 | ||
544 | { OFPUTIL_OFPST_PORT_REQUEST, | |
545 | OFPST_PORT, "OFPST_PORT request", | |
546 | OSR_SIZE + sizeof(struct ofp_port_stats_request), 0 }, | |
547 | ||
548 | { OFPUTIL_OFPST_QUEUE_REQUEST, | |
549 | OFPST_QUEUE, "OFPST_QUEUE request", | |
550 | OSR_SIZE + sizeof(struct ofp_queue_stats_request), 0 }, | |
551 | ||
552 | { 0, | |
553 | OFPST_VENDOR, "OFPST_VENDOR request", | |
554 | OSR_SIZE + sizeof(uint32_t), 1 }, | |
555 | }; | |
556 | ||
557 | static const struct ofputil_msg_category ofpst_request_category = { | |
558 | "OpenFlow statistics", | |
559 | ofpst_requests, ARRAY_SIZE(ofpst_requests), | |
560 | OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT) | |
561 | }; | |
562 | ||
563 | const struct ofp_stats_request *osr; | |
564 | int error; | |
565 | ||
566 | osr = (const struct ofp_stats_request *) oh; | |
567 | error = ofputil_lookup_openflow_message(&ofpst_request_category, | |
568 | ntohs(osr->type), | |
569 | ntohs(oh->length), typep); | |
570 | if (!error && osr->type == htons(OFPST_VENDOR)) { | |
571 | error = ofputil_decode_nxst_request(oh, typep); | |
572 | } | |
573 | return error; | |
574 | } | |
575 | ||
576 | static int | |
577 | ofputil_decode_ofpst_reply(const struct ofp_header *oh, | |
578 | const struct ofputil_msg_type **typep) | |
579 | { | |
580 | enum { OSR_SIZE = sizeof(struct ofp_stats_reply) }; | |
581 | static const struct ofputil_msg_type ofpst_replies[] = { | |
582 | { OFPUTIL_OFPST_DESC_REPLY, | |
583 | OFPST_DESC, "OFPST_DESC reply", | |
584 | OSR_SIZE + sizeof(struct ofp_desc_stats), 0 }, | |
585 | ||
586 | { OFPUTIL_OFPST_FLOW_REPLY, | |
587 | OFPST_FLOW, "OFPST_FLOW reply", | |
588 | OSR_SIZE, 1 }, | |
589 | ||
590 | { OFPUTIL_OFPST_AGGREGATE_REPLY, | |
591 | OFPST_AGGREGATE, "OFPST_AGGREGATE reply", | |
592 | OSR_SIZE + sizeof(struct ofp_aggregate_stats_reply), 0 }, | |
593 | ||
594 | { OFPUTIL_OFPST_TABLE_REPLY, | |
595 | OFPST_TABLE, "OFPST_TABLE reply", | |
596 | OSR_SIZE, sizeof(struct ofp_table_stats) }, | |
597 | ||
598 | { OFPUTIL_OFPST_PORT_REPLY, | |
599 | OFPST_PORT, "OFPST_PORT reply", | |
600 | OSR_SIZE, sizeof(struct ofp_port_stats) }, | |
601 | ||
602 | { OFPUTIL_OFPST_QUEUE_REPLY, | |
603 | OFPST_QUEUE, "OFPST_QUEUE reply", | |
604 | OSR_SIZE, sizeof(struct ofp_queue_stats) }, | |
605 | ||
606 | { 0, | |
607 | OFPST_VENDOR, "OFPST_VENDOR reply", | |
608 | OSR_SIZE + sizeof(uint32_t), 1 }, | |
609 | }; | |
610 | ||
611 | static const struct ofputil_msg_category ofpst_reply_category = { | |
612 | "OpenFlow statistics", | |
613 | ofpst_replies, ARRAY_SIZE(ofpst_replies), | |
614 | OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT) | |
615 | }; | |
616 | ||
617 | const struct ofp_stats_reply *osr = (const struct ofp_stats_reply *) oh; | |
618 | int error; | |
619 | ||
620 | error = ofputil_lookup_openflow_message(&ofpst_reply_category, | |
621 | ntohs(osr->type), | |
622 | ntohs(oh->length), typep); | |
623 | if (!error && osr->type == htons(OFPST_VENDOR)) { | |
624 | error = ofputil_decode_nxst_reply(oh, typep); | |
625 | } | |
626 | return error; | |
627 | } | |
628 | ||
629 | /* Decodes the message type represented by 'oh'. Returns 0 if successful or | |
630 | * an OpenFlow error code constructed with ofp_mkerr() on failure. Either | |
631 | * way, stores in '*typep' a type structure that can be inspected with the | |
632 | * ofputil_msg_type_*() functions. | |
633 | * | |
634 | * oh->length must indicate the correct length of the message (and must be at | |
635 | * least sizeof(struct ofp_header)). | |
636 | * | |
637 | * Success indicates that 'oh' is at least as long as the minimum-length | |
638 | * message of its type. */ | |
639 | int | |
640 | ofputil_decode_msg_type(const struct ofp_header *oh, | |
641 | const struct ofputil_msg_type **typep) | |
642 | { | |
643 | static const struct ofputil_msg_type ofpt_messages[] = { | |
644 | { OFPUTIL_OFPT_HELLO, | |
645 | OFPT_HELLO, "OFPT_HELLO", | |
646 | sizeof(struct ofp_hello), 1 }, | |
647 | ||
648 | { OFPUTIL_OFPT_ERROR, | |
649 | OFPT_ERROR, "OFPT_ERROR", | |
650 | sizeof(struct ofp_error_msg), 1 }, | |
651 | ||
652 | { OFPUTIL_OFPT_ECHO_REQUEST, | |
653 | OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST", | |
654 | sizeof(struct ofp_header), 1 }, | |
655 | ||
656 | { OFPUTIL_OFPT_ECHO_REPLY, | |
657 | OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY", | |
658 | sizeof(struct ofp_header), 1 }, | |
659 | ||
660 | { OFPUTIL_OFPT_FEATURES_REQUEST, | |
661 | OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST", | |
662 | sizeof(struct ofp_header), 0 }, | |
663 | ||
664 | { OFPUTIL_OFPT_FEATURES_REPLY, | |
665 | OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY", | |
666 | sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) }, | |
667 | ||
668 | { OFPUTIL_OFPT_GET_CONFIG_REQUEST, | |
669 | OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST", | |
670 | sizeof(struct ofp_header), 0 }, | |
671 | ||
672 | { OFPUTIL_OFPT_GET_CONFIG_REPLY, | |
673 | OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY", | |
674 | sizeof(struct ofp_switch_config), 0 }, | |
675 | ||
676 | { OFPUTIL_OFPT_SET_CONFIG, | |
677 | OFPT_SET_CONFIG, "OFPT_SET_CONFIG", | |
678 | sizeof(struct ofp_switch_config), 0 }, | |
679 | ||
680 | { OFPUTIL_OFPT_PACKET_IN, | |
681 | OFPT_PACKET_IN, "OFPT_PACKET_IN", | |
682 | offsetof(struct ofp_packet_in, data), 1 }, | |
683 | ||
684 | { OFPUTIL_OFPT_FLOW_REMOVED, | |
685 | OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED", | |
686 | sizeof(struct ofp_flow_removed), 0 }, | |
687 | ||
688 | { OFPUTIL_OFPT_PORT_STATUS, | |
689 | OFPT_PORT_STATUS, "OFPT_PORT_STATUS", | |
690 | sizeof(struct ofp_port_status), 0 }, | |
691 | ||
692 | { OFPUTIL_OFPT_PACKET_OUT, | |
693 | OFPT_PACKET_OUT, "OFPT_PACKET_OUT", | |
694 | sizeof(struct ofp_packet_out), 1 }, | |
695 | ||
696 | { OFPUTIL_OFPT_FLOW_MOD, | |
697 | OFPT_FLOW_MOD, "OFPT_FLOW_MOD", | |
698 | sizeof(struct ofp_flow_mod), 1 }, | |
699 | ||
700 | { OFPUTIL_OFPT_PORT_MOD, | |
701 | OFPT_PORT_MOD, "OFPT_PORT_MOD", | |
702 | sizeof(struct ofp_port_mod), 0 }, | |
703 | ||
704 | { 0, | |
705 | OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST", | |
706 | sizeof(struct ofp_stats_request), 1 }, | |
707 | ||
708 | { 0, | |
709 | OFPT_STATS_REPLY, "OFPT_STATS_REPLY", | |
710 | sizeof(struct ofp_stats_reply), 1 }, | |
711 | ||
712 | { OFPUTIL_OFPT_BARRIER_REQUEST, | |
713 | OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST", | |
714 | sizeof(struct ofp_header), 0 }, | |
715 | ||
716 | { OFPUTIL_OFPT_BARRIER_REPLY, | |
717 | OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY", | |
718 | sizeof(struct ofp_header), 0 }, | |
719 | ||
720 | { 0, | |
721 | OFPT_VENDOR, "OFPT_VENDOR", | |
722 | sizeof(struct ofp_vendor_header), 1 }, | |
723 | }; | |
724 | ||
725 | static const struct ofputil_msg_category ofpt_category = { | |
726 | "OpenFlow message", | |
727 | ofpt_messages, ARRAY_SIZE(ofpt_messages), | |
728 | OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE) | |
729 | }; | |
730 | ||
731 | int error; | |
732 | ||
733 | error = ofputil_lookup_openflow_message(&ofpt_category, oh->type, | |
734 | ntohs(oh->length), typep); | |
735 | if (!error) { | |
736 | switch (oh->type) { | |
737 | case OFPT_VENDOR: | |
738 | error = ofputil_decode_vendor(oh, typep); | |
739 | break; | |
740 | ||
741 | case OFPT_STATS_REQUEST: | |
742 | error = ofputil_decode_ofpst_request(oh, typep); | |
743 | break; | |
744 | ||
745 | case OFPT_STATS_REPLY: | |
746 | error = ofputil_decode_ofpst_reply(oh, typep); | |
747 | ||
748 | default: | |
749 | break; | |
750 | } | |
751 | } | |
752 | if (error) { | |
753 | static const struct ofputil_msg_type ofputil_invalid_type = { | |
754 | OFPUTIL_INVALID, | |
755 | 0, "OFPUTIL_INVALID", | |
756 | 0, 0 | |
757 | }; | |
758 | ||
759 | *typep = &ofputil_invalid_type; | |
760 | } | |
761 | return error; | |
762 | } | |
763 | ||
764 | /* Returns an OFPUTIL_* message type code for 'type'. */ | |
765 | enum ofputil_msg_code | |
766 | ofputil_msg_type_code(const struct ofputil_msg_type *type) | |
767 | { | |
768 | return type->code; | |
769 | } | |
2e4f5fcf | 770 | \f |
7fa91113 BP |
771 | /* Flow formats. */ |
772 | ||
773 | bool | |
774 | ofputil_flow_format_is_valid(enum nx_flow_format flow_format) | |
775 | { | |
776 | switch (flow_format) { | |
777 | case NXFF_OPENFLOW10: | |
7fa91113 BP |
778 | case NXFF_NXM: |
779 | return true; | |
780 | } | |
781 | ||
782 | return false; | |
783 | } | |
784 | ||
785 | const char * | |
786 | ofputil_flow_format_to_string(enum nx_flow_format flow_format) | |
787 | { | |
788 | switch (flow_format) { | |
789 | case NXFF_OPENFLOW10: | |
790 | return "openflow10"; | |
7fa91113 BP |
791 | case NXFF_NXM: |
792 | return "nxm"; | |
793 | default: | |
794 | NOT_REACHED(); | |
795 | } | |
796 | } | |
797 | ||
88ca35ee BP |
798 | int |
799 | ofputil_flow_format_from_string(const char *s) | |
800 | { | |
801 | return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10 | |
88ca35ee BP |
802 | : !strcmp(s, "nxm") ? NXFF_NXM |
803 | : -1); | |
804 | } | |
805 | ||
806 | static bool | |
807 | regs_fully_wildcarded(const struct flow_wildcards *wc) | |
808 | { | |
809 | int i; | |
810 | ||
811 | for (i = 0; i < FLOW_N_REGS; i++) { | |
812 | if (wc->reg_masks[i] != 0) { | |
813 | return false; | |
814 | } | |
815 | } | |
816 | return true; | |
817 | } | |
818 | ||
b78f6b77 BP |
819 | /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch |
820 | * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers, | |
821 | * or fixing the Ethernet multicast bit. Otherwise, it's better to use | |
822 | * NXFF_OPENFLOW10 for backward compatibility. */ | |
823 | enum nx_flow_format | |
824 | ofputil_min_flow_format(const struct cls_rule *rule) | |
8368c090 BP |
825 | { |
826 | const struct flow_wildcards *wc = &rule->wc; | |
8368c090 BP |
827 | |
828 | /* Only NXM supports separately wildcards the Ethernet multicast bit. */ | |
829 | if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) { | |
b78f6b77 | 830 | return NXFF_NXM; |
8368c090 BP |
831 | } |
832 | ||
bad68a99 JP |
833 | /* Only NXM supports matching ARP hardware addresses. */ |
834 | if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) { | |
b78f6b77 | 835 | return NXFF_NXM; |
bad68a99 JP |
836 | } |
837 | ||
d31f1109 JP |
838 | /* Only NXM supports matching IPv6 traffic. */ |
839 | if (!(wc->wildcards & FWW_DL_TYPE) | |
840 | && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) { | |
b78f6b77 | 841 | return NXFF_NXM; |
d31f1109 JP |
842 | } |
843 | ||
8368c090 BP |
844 | /* Only NXM supports matching registers. */ |
845 | if (!regs_fully_wildcarded(wc)) { | |
b78f6b77 | 846 | return NXFF_NXM; |
8368c090 BP |
847 | } |
848 | ||
b78f6b77 BP |
849 | /* Only NXM supports matching tun_id. */ |
850 | if (wc->tun_id_mask != htonll(0)) { | |
851 | return NXFF_NXM; | |
8368c090 BP |
852 | } |
853 | ||
854 | /* Other formats can express this rule. */ | |
b78f6b77 | 855 | return NXFF_OPENFLOW10; |
88ca35ee BP |
856 | } |
857 | ||
858 | /* Returns an OpenFlow message that can be used to set the flow format to | |
859 | * 'flow_format'. */ | |
860 | struct ofpbuf * | |
861 | ofputil_make_set_flow_format(enum nx_flow_format flow_format) | |
862 | { | |
b78f6b77 | 863 | struct nxt_set_flow_format *sff; |
88ca35ee BP |
864 | struct ofpbuf *msg; |
865 | ||
b78f6b77 BP |
866 | sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg); |
867 | sff->format = htonl(flow_format); | |
88ca35ee BP |
868 | |
869 | return msg; | |
870 | } | |
871 | ||
6c1491fb BP |
872 | /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id |
873 | * extension on or off (according to 'flow_mod_table_id'). */ | |
874 | struct ofpbuf * | |
875 | ofputil_make_flow_mod_table_id(bool flow_mod_table_id) | |
876 | { | |
877 | struct nxt_flow_mod_table_id *nfmti; | |
878 | struct ofpbuf *msg; | |
879 | ||
880 | nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg); | |
881 | nfmti->set = flow_mod_table_id; | |
882 | return msg; | |
883 | } | |
884 | ||
7fa91113 BP |
885 | /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract |
886 | * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error | |
887 | * code. | |
888 | * | |
6c1491fb BP |
889 | * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is |
890 | * enabled, false otherwise. | |
891 | * | |
2e4f5fcf BP |
892 | * Does not validate the flow_mod actions. */ |
893 | int | |
894 | ofputil_decode_flow_mod(struct flow_mod *fm, const struct ofp_header *oh, | |
6c1491fb | 895 | bool flow_mod_table_id) |
2e4f5fcf BP |
896 | { |
897 | const struct ofputil_msg_type *type; | |
6c1491fb | 898 | uint16_t command; |
2e4f5fcf BP |
899 | struct ofpbuf b; |
900 | ||
2013493b | 901 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf BP |
902 | |
903 | ofputil_decode_msg_type(oh, &type); | |
904 | if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) { | |
905 | /* Standard OpenFlow flow_mod. */ | |
2e4f5fcf | 906 | const struct ofp_flow_mod *ofm; |
1c0b7503 BP |
907 | uint16_t priority; |
908 | ovs_be32 wc; | |
2e4f5fcf BP |
909 | int error; |
910 | ||
911 | /* Dissect the message. */ | |
bbc32a88 | 912 | ofm = ofpbuf_pull(&b, sizeof *ofm); |
2e4f5fcf BP |
913 | error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions); |
914 | if (error) { | |
915 | return error; | |
916 | } | |
917 | ||
1c0b7503 BP |
918 | /* Set priority based on original wildcards. Normally we'd allow |
919 | * ofputil_cls_rule_from_match() to do this for us, but | |
920 | * normalize_wildcards() can put wildcards where the original flow | |
921 | * didn't have them. */ | |
922 | priority = ntohs(ofm->priority); | |
923 | if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) { | |
924 | priority = UINT16_MAX; | |
925 | } | |
926 | ||
2e4f5fcf BP |
927 | /* Normalize ofm->match. If normalization actually changes anything, |
928 | * then log the differences. */ | |
1c0b7503 BP |
929 | wc = normalize_wildcards(&ofm->match); |
930 | if (wc == ofm->match.wildcards) { | |
931 | ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr); | |
932 | } else { | |
933 | struct ofp_match match = ofm->match; | |
934 | match.wildcards = wc; | |
935 | ofputil_cls_rule_from_match(&match, priority, &fm->cr); | |
936 | ||
2e4f5fcf | 937 | if (!VLOG_DROP_INFO(&bad_ofmsg_rl)) { |
1c0b7503 BP |
938 | char *pre = ofp_match_to_string(&ofm->match, 1); |
939 | char *post = ofp_match_to_string(&match, 1); | |
2e4f5fcf | 940 | VLOG_INFO("normalization changed ofp_match, details:"); |
1c0b7503 BP |
941 | VLOG_INFO(" pre: %s", pre); |
942 | VLOG_INFO("post: %s", post); | |
943 | free(pre); | |
944 | free(post); | |
2e4f5fcf BP |
945 | } |
946 | } | |
947 | ||
948 | /* Translate the message. */ | |
2e4f5fcf | 949 | fm->cookie = ofm->cookie; |
6c1491fb | 950 | command = ntohs(ofm->command); |
2e4f5fcf BP |
951 | fm->idle_timeout = ntohs(ofm->idle_timeout); |
952 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
953 | fm->buffer_id = ntohl(ofm->buffer_id); | |
954 | fm->out_port = ntohs(ofm->out_port); | |
955 | fm->flags = ntohs(ofm->flags); | |
956 | } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) { | |
957 | /* Nicira extended flow_mod. */ | |
958 | const struct nx_flow_mod *nfm; | |
959 | int error; | |
960 | ||
961 | /* Dissect the message. */ | |
bbc32a88 | 962 | nfm = ofpbuf_pull(&b, sizeof *nfm); |
2e4f5fcf BP |
963 | error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority), |
964 | &fm->cr); | |
965 | if (error) { | |
966 | return error; | |
967 | } | |
968 | error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions); | |
969 | if (error) { | |
970 | return error; | |
971 | } | |
972 | ||
973 | /* Translate the message. */ | |
974 | fm->cookie = nfm->cookie; | |
6c1491fb | 975 | command = ntohs(nfm->command); |
2e4f5fcf BP |
976 | fm->idle_timeout = ntohs(nfm->idle_timeout); |
977 | fm->hard_timeout = ntohs(nfm->hard_timeout); | |
978 | fm->buffer_id = ntohl(nfm->buffer_id); | |
979 | fm->out_port = ntohs(nfm->out_port); | |
980 | fm->flags = ntohs(nfm->flags); | |
981 | } else { | |
982 | NOT_REACHED(); | |
983 | } | |
984 | ||
6c1491fb BP |
985 | if (flow_mod_table_id) { |
986 | fm->command = command & 0xff; | |
987 | fm->table_id = command >> 8; | |
988 | } else { | |
989 | fm->command = command; | |
990 | fm->table_id = 0xff; | |
991 | } | |
992 | ||
2e4f5fcf BP |
993 | return 0; |
994 | } | |
995 | ||
996 | /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to | |
6c1491fb BP |
997 | * 'flow_format' and returns the message. |
998 | * | |
999 | * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is | |
1000 | * enabled, false otherwise. */ | |
2e4f5fcf BP |
1001 | struct ofpbuf * |
1002 | ofputil_encode_flow_mod(const struct flow_mod *fm, | |
6c1491fb BP |
1003 | enum nx_flow_format flow_format, |
1004 | bool flow_mod_table_id) | |
2e4f5fcf BP |
1005 | { |
1006 | size_t actions_len = fm->n_actions * sizeof *fm->actions; | |
1007 | struct ofpbuf *msg; | |
6c1491fb BP |
1008 | uint16_t command; |
1009 | ||
1010 | command = (flow_mod_table_id | |
1011 | ? (fm->command & 0xff) | (fm->table_id << 8) | |
1012 | : fm->command); | |
2e4f5fcf | 1013 | |
b78f6b77 | 1014 | if (flow_format == NXFF_OPENFLOW10) { |
2e4f5fcf BP |
1015 | struct ofp_flow_mod *ofm; |
1016 | ||
1017 | msg = ofpbuf_new(sizeof *ofm + actions_len); | |
1018 | ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg); | |
b78f6b77 BP |
1019 | ofputil_cls_rule_to_match(&fm->cr, &ofm->match); |
1020 | ofm->cookie = fm->cookie; | |
2e4f5fcf BP |
1021 | ofm->command = htons(fm->command); |
1022 | ofm->idle_timeout = htons(fm->idle_timeout); | |
1023 | ofm->hard_timeout = htons(fm->hard_timeout); | |
1024 | ofm->priority = htons(fm->cr.priority); | |
1025 | ofm->buffer_id = htonl(fm->buffer_id); | |
1026 | ofm->out_port = htons(fm->out_port); | |
1027 | ofm->flags = htons(fm->flags); | |
1028 | } else if (flow_format == NXFF_NXM) { | |
1029 | struct nx_flow_mod *nfm; | |
1030 | int match_len; | |
1031 | ||
1032 | msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len); | |
1033 | put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg); | |
1034 | match_len = nx_put_match(msg, &fm->cr); | |
1035 | ||
1036 | nfm = msg->data; | |
1037 | nfm->cookie = fm->cookie; | |
6c1491fb | 1038 | nfm->command = htons(command); |
2e4f5fcf BP |
1039 | nfm->idle_timeout = htons(fm->idle_timeout); |
1040 | nfm->hard_timeout = htons(fm->hard_timeout); | |
1041 | nfm->priority = htons(fm->cr.priority); | |
1042 | nfm->buffer_id = htonl(fm->buffer_id); | |
1043 | nfm->out_port = htons(fm->out_port); | |
1044 | nfm->flags = htons(fm->flags); | |
1045 | nfm->match_len = htons(match_len); | |
1046 | } else { | |
1047 | NOT_REACHED(); | |
1048 | } | |
1049 | ||
1050 | ofpbuf_put(msg, fm->actions, actions_len); | |
1051 | update_openflow_length(msg); | |
1052 | return msg; | |
1053 | } | |
1054 | ||
1055 | static int | |
1056 | ofputil_decode_ofpst_flow_request(struct flow_stats_request *fsr, | |
1057 | const struct ofp_header *oh, | |
2e4f5fcf BP |
1058 | bool aggregate) |
1059 | { | |
1060 | const struct ofp_flow_stats_request *ofsr = ofputil_stats_body(oh); | |
1061 | ||
1062 | fsr->aggregate = aggregate; | |
b78f6b77 | 1063 | ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match); |
2e4f5fcf BP |
1064 | fsr->out_port = ntohs(ofsr->out_port); |
1065 | fsr->table_id = ofsr->table_id; | |
1066 | ||
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | static int | |
1071 | ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr, | |
1072 | const struct ofp_header *oh, | |
1073 | bool aggregate) | |
1074 | { | |
1075 | const struct nx_flow_stats_request *nfsr; | |
1076 | struct ofpbuf b; | |
1077 | int error; | |
1078 | ||
2013493b | 1079 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf | 1080 | |
bbc32a88 | 1081 | nfsr = ofpbuf_pull(&b, sizeof *nfsr); |
2e4f5fcf BP |
1082 | error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match); |
1083 | if (error) { | |
1084 | return error; | |
1085 | } | |
1086 | if (b.size) { | |
1087 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
1088 | } | |
1089 | ||
1090 | fsr->aggregate = aggregate; | |
1091 | fsr->out_port = ntohs(nfsr->out_port); | |
1092 | fsr->table_id = nfsr->table_id; | |
1093 | ||
1094 | return 0; | |
1095 | } | |
1096 | ||
1097 | /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE | |
b78f6b77 BP |
1098 | * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if |
1099 | * successful, otherwise an OpenFlow error code. */ | |
2e4f5fcf BP |
1100 | int |
1101 | ofputil_decode_flow_stats_request(struct flow_stats_request *fsr, | |
b78f6b77 | 1102 | const struct ofp_header *oh) |
2e4f5fcf BP |
1103 | { |
1104 | const struct ofputil_msg_type *type; | |
1105 | struct ofpbuf b; | |
1106 | int code; | |
1107 | ||
2013493b | 1108 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf BP |
1109 | |
1110 | ofputil_decode_msg_type(oh, &type); | |
1111 | code = ofputil_msg_type_code(type); | |
1112 | switch (code) { | |
1113 | case OFPUTIL_OFPST_FLOW_REQUEST: | |
b78f6b77 | 1114 | return ofputil_decode_ofpst_flow_request(fsr, oh, false); |
2e4f5fcf BP |
1115 | |
1116 | case OFPUTIL_OFPST_AGGREGATE_REQUEST: | |
b78f6b77 | 1117 | return ofputil_decode_ofpst_flow_request(fsr, oh, true); |
2e4f5fcf BP |
1118 | |
1119 | case OFPUTIL_NXST_FLOW_REQUEST: | |
1120 | return ofputil_decode_nxst_flow_request(fsr, oh, false); | |
1121 | ||
1122 | case OFPUTIL_NXST_AGGREGATE_REQUEST: | |
1123 | return ofputil_decode_nxst_flow_request(fsr, oh, true); | |
1124 | ||
1125 | default: | |
1126 | /* Hey, the caller lied. */ | |
1127 | NOT_REACHED(); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW, | |
4ffd1b43 | 1132 | * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to |
2e4f5fcf BP |
1133 | * 'flow_format', and returns the message. */ |
1134 | struct ofpbuf * | |
1135 | ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr, | |
1136 | enum nx_flow_format flow_format) | |
1137 | { | |
1138 | struct ofpbuf *msg; | |
1139 | ||
b78f6b77 | 1140 | if (flow_format == NXFF_OPENFLOW10) { |
2e4f5fcf BP |
1141 | struct ofp_flow_stats_request *ofsr; |
1142 | int type; | |
1143 | ||
1144 | BUILD_ASSERT_DECL(sizeof(struct ofp_flow_stats_request) | |
1145 | == sizeof(struct ofp_aggregate_stats_request)); | |
1146 | ||
1147 | type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW; | |
1148 | ofsr = ofputil_make_stats_request(sizeof *ofsr, type, &msg); | |
b78f6b77 | 1149 | ofputil_cls_rule_to_match(&fsr->match, &ofsr->match); |
2e4f5fcf BP |
1150 | ofsr->table_id = fsr->table_id; |
1151 | ofsr->out_port = htons(fsr->out_port); | |
1152 | } else if (flow_format == NXFF_NXM) { | |
1153 | struct nx_flow_stats_request *nfsr; | |
1154 | int match_len; | |
9fb7fa87 | 1155 | int subtype; |
2e4f5fcf | 1156 | |
9fb7fa87 BP |
1157 | subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW; |
1158 | ofputil_make_nxstats_request(sizeof *nfsr, subtype, &msg); | |
2e4f5fcf BP |
1159 | match_len = nx_put_match(msg, &fsr->match); |
1160 | ||
1161 | nfsr = msg->data; | |
1162 | nfsr->out_port = htons(fsr->out_port); | |
1163 | nfsr->match_len = htons(match_len); | |
1164 | nfsr->table_id = fsr->table_id; | |
1165 | } else { | |
1166 | NOT_REACHED(); | |
1167 | } | |
1168 | ||
1169 | return msg; | |
1170 | } | |
d1e2cf21 | 1171 | |
4ffd1b43 | 1172 | /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract |
b78f6b77 | 1173 | * ofputil_flow_stats in 'fs'. |
4ffd1b43 BP |
1174 | * |
1175 | * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single | |
1176 | * OpenFlow message. Calling this function multiple times for a single 'msg' | |
1177 | * iterates through the replies. The caller must initially leave 'msg''s layer | |
1178 | * pointers null and not modify them between calls. | |
1179 | * | |
1180 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
1181 | * otherwise a positive errno value. */ | |
1182 | int | |
1183 | ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs, | |
b78f6b77 | 1184 | struct ofpbuf *msg) |
4ffd1b43 BP |
1185 | { |
1186 | const struct ofputil_msg_type *type; | |
1187 | int code; | |
1188 | ||
1189 | ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type); | |
1190 | code = ofputil_msg_type_code(type); | |
1191 | if (!msg->l2) { | |
1192 | msg->l2 = msg->data; | |
1193 | if (code == OFPUTIL_OFPST_FLOW_REPLY) { | |
1194 | ofpbuf_pull(msg, sizeof(struct ofp_stats_reply)); | |
1195 | } else if (code == OFPUTIL_NXST_FLOW_REPLY) { | |
1196 | ofpbuf_pull(msg, sizeof(struct nicira_stats_msg)); | |
1197 | } else { | |
1198 | NOT_REACHED(); | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | if (!msg->size) { | |
1203 | return EOF; | |
1204 | } else if (code == OFPUTIL_OFPST_FLOW_REPLY) { | |
1205 | const struct ofp_flow_stats *ofs; | |
1206 | size_t length; | |
1207 | ||
1208 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
1209 | if (!ofs) { | |
1210 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover " | |
1211 | "bytes at end", msg->size); | |
1212 | return EINVAL; | |
1213 | } | |
1214 | ||
1215 | length = ntohs(ofs->length); | |
1216 | if (length < sizeof *ofs) { | |
1217 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
1218 | "length %zu", length); | |
1219 | return EINVAL; | |
1220 | } | |
1221 | ||
1222 | if (ofputil_pull_actions(msg, length - sizeof *ofs, | |
1223 | &fs->actions, &fs->n_actions)) { | |
1224 | return EINVAL; | |
1225 | } | |
1226 | ||
1227 | fs->cookie = get_32aligned_be64(&ofs->cookie); | |
1228 | ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority), | |
b78f6b77 | 1229 | &fs->rule); |
4ffd1b43 BP |
1230 | fs->table_id = ofs->table_id; |
1231 | fs->duration_sec = ntohl(ofs->duration_sec); | |
1232 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
1233 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
1234 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
1235 | fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count)); | |
1236 | fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count)); | |
1237 | } else if (code == OFPUTIL_NXST_FLOW_REPLY) { | |
1238 | const struct nx_flow_stats *nfs; | |
1239 | size_t match_len, length; | |
1240 | ||
1241 | nfs = ofpbuf_try_pull(msg, sizeof *nfs); | |
1242 | if (!nfs) { | |
1243 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover " | |
1244 | "bytes at end", msg->size); | |
1245 | return EINVAL; | |
1246 | } | |
1247 | ||
1248 | length = ntohs(nfs->length); | |
1249 | match_len = ntohs(nfs->match_len); | |
1250 | if (length < sizeof *nfs + ROUND_UP(match_len, 8)) { | |
1251 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu " | |
1252 | "claims invalid length %zu", match_len, length); | |
1253 | return EINVAL; | |
1254 | } | |
1255 | if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) { | |
1256 | return EINVAL; | |
1257 | } | |
1258 | ||
1259 | if (ofputil_pull_actions(msg, | |
1260 | length - sizeof *nfs - ROUND_UP(match_len, 8), | |
1261 | &fs->actions, &fs->n_actions)) { | |
1262 | return EINVAL; | |
1263 | } | |
1264 | ||
1265 | fs->cookie = nfs->cookie; | |
1266 | fs->table_id = nfs->table_id; | |
1267 | fs->duration_sec = ntohl(nfs->duration_sec); | |
1268 | fs->duration_nsec = ntohl(nfs->duration_nsec); | |
1269 | fs->idle_timeout = ntohs(nfs->idle_timeout); | |
1270 | fs->hard_timeout = ntohs(nfs->hard_timeout); | |
1271 | fs->packet_count = ntohll(nfs->packet_count); | |
1272 | fs->byte_count = ntohll(nfs->byte_count); | |
1273 | } else { | |
1274 | NOT_REACHED(); | |
1275 | } | |
1276 | ||
1277 | return 0; | |
1278 | } | |
1279 | ||
b78f6b77 BP |
1280 | /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an |
1281 | * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise | |
1282 | * an OpenFlow error code. */ | |
9b045a0c BP |
1283 | int |
1284 | ofputil_decode_flow_removed(struct ofputil_flow_removed *fr, | |
b78f6b77 | 1285 | const struct ofp_header *oh) |
9b045a0c BP |
1286 | { |
1287 | const struct ofputil_msg_type *type; | |
1288 | enum ofputil_msg_code code; | |
1289 | ||
1290 | ofputil_decode_msg_type(oh, &type); | |
1291 | code = ofputil_msg_type_code(type); | |
1292 | if (code == OFPUTIL_OFPT_FLOW_REMOVED) { | |
1293 | const struct ofp_flow_removed *ofr; | |
1294 | ||
1295 | ofr = (const struct ofp_flow_removed *) oh; | |
1296 | ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority), | |
b78f6b77 | 1297 | &fr->rule); |
9b045a0c BP |
1298 | fr->cookie = ofr->cookie; |
1299 | fr->reason = ofr->reason; | |
1300 | fr->duration_sec = ntohl(ofr->duration_sec); | |
1301 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
1302 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
1303 | fr->packet_count = ntohll(ofr->packet_count); | |
1304 | fr->byte_count = ntohll(ofr->byte_count); | |
1305 | } else if (code == OFPUTIL_NXT_FLOW_REMOVED) { | |
1306 | struct nx_flow_removed *nfr; | |
1307 | struct ofpbuf b; | |
1308 | int error; | |
1309 | ||
1310 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1311 | ||
1312 | nfr = ofpbuf_pull(&b, sizeof *nfr); | |
1313 | error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority), | |
1314 | &fr->rule); | |
1315 | if (error) { | |
1316 | return error; | |
1317 | } | |
1318 | if (b.size) { | |
1319 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
1320 | } | |
1321 | ||
1322 | fr->cookie = nfr->cookie; | |
1323 | fr->reason = nfr->reason; | |
1324 | fr->duration_sec = ntohl(nfr->duration_sec); | |
1325 | fr->duration_nsec = ntohl(nfr->duration_nsec); | |
1326 | fr->idle_timeout = ntohs(nfr->idle_timeout); | |
1327 | fr->packet_count = ntohll(nfr->packet_count); | |
1328 | fr->byte_count = ntohll(nfr->byte_count); | |
1329 | } else { | |
1330 | NOT_REACHED(); | |
1331 | } | |
1332 | ||
1333 | return 0; | |
1334 | } | |
1335 | ||
588cd7b5 BP |
1336 | /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or |
1337 | * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the | |
1338 | * message. */ | |
1339 | struct ofpbuf * | |
1340 | ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr, | |
1341 | enum nx_flow_format flow_format) | |
1342 | { | |
1343 | struct ofpbuf *msg; | |
1344 | ||
b78f6b77 | 1345 | if (flow_format == NXFF_OPENFLOW10) { |
588cd7b5 BP |
1346 | struct ofp_flow_removed *ofr; |
1347 | ||
1348 | ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0), | |
1349 | &msg); | |
b78f6b77 | 1350 | ofputil_cls_rule_to_match(&fr->rule, &ofr->match); |
7fb563b9 | 1351 | ofr->cookie = fr->cookie; |
588cd7b5 BP |
1352 | ofr->priority = htons(fr->rule.priority); |
1353 | ofr->reason = fr->reason; | |
1354 | ofr->duration_sec = htonl(fr->duration_sec); | |
1355 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
1356 | ofr->idle_timeout = htons(fr->idle_timeout); | |
1357 | ofr->packet_count = htonll(fr->packet_count); | |
1358 | ofr->byte_count = htonll(fr->byte_count); | |
1359 | } else if (flow_format == NXFF_NXM) { | |
1360 | struct nx_flow_removed *nfr; | |
1361 | int match_len; | |
1362 | ||
1363 | make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg); | |
1364 | match_len = nx_put_match(msg, &fr->rule); | |
1365 | ||
1366 | nfr = msg->data; | |
1367 | nfr->cookie = fr->cookie; | |
1368 | nfr->priority = htons(fr->rule.priority); | |
1369 | nfr->reason = fr->reason; | |
1370 | nfr->duration_sec = htonl(fr->duration_sec); | |
1371 | nfr->duration_nsec = htonl(fr->duration_nsec); | |
1372 | nfr->idle_timeout = htons(fr->idle_timeout); | |
1373 | nfr->match_len = htons(match_len); | |
1374 | nfr->packet_count = htonll(fr->packet_count); | |
1375 | nfr->byte_count = htonll(fr->byte_count); | |
1376 | } else { | |
1377 | NOT_REACHED(); | |
1378 | } | |
1379 | ||
1380 | return msg; | |
1381 | } | |
1382 | ||
ebb57021 BP |
1383 | /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message |
1384 | * and returns the message. | |
1385 | * | |
1386 | * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated | |
1387 | * returned ofpbuf. | |
1388 | * | |
1389 | * If 'rw_packet' is nonnull, then it must contain the same data as | |
1390 | * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet. | |
1391 | * It is modified in-place into an OFPT_PACKET_IN message according to 'pin', | |
1392 | * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has | |
1393 | * enough headroom to insert a "struct ofp_packet_in", this is more efficient | |
1394 | * than ofputil_encode_packet_in() because it does not copy the packet | |
1395 | * payload. */ | |
1396 | struct ofpbuf * | |
1397 | ofputil_encode_packet_in(const struct ofputil_packet_in *pin, | |
1398 | struct ofpbuf *rw_packet) | |
1399 | { | |
1400 | int total_len = pin->packet->size; | |
1401 | struct ofp_packet_in *opi; | |
1402 | ||
1403 | if (rw_packet) { | |
1404 | if (pin->send_len < rw_packet->size) { | |
1405 | rw_packet->size = pin->send_len; | |
1406 | } | |
1407 | } else { | |
1408 | rw_packet = ofpbuf_clone_data_with_headroom( | |
1409 | pin->packet->data, MIN(pin->send_len, pin->packet->size), | |
1410 | offsetof(struct ofp_packet_in, data)); | |
1411 | } | |
1412 | ||
1413 | /* Add OFPT_PACKET_IN. */ | |
1414 | opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data)); | |
1415 | opi->header.version = OFP_VERSION; | |
1416 | opi->header.type = OFPT_PACKET_IN; | |
1417 | opi->total_len = htons(total_len); | |
1418 | opi->in_port = htons(pin->in_port); | |
1419 | opi->reason = pin->reason; | |
1420 | opi->buffer_id = htonl(pin->buffer_id); | |
1421 | update_openflow_length(rw_packet); | |
1422 | ||
1423 | return rw_packet; | |
1424 | } | |
1425 | ||
d1e2cf21 BP |
1426 | /* Returns a string representing the message type of 'type'. The string is the |
1427 | * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics | |
1428 | * messages, the constant is followed by "request" or "reply", | |
1429 | * e.g. "OFPST_AGGREGATE reply". */ | |
1430 | const char * | |
1431 | ofputil_msg_type_name(const struct ofputil_msg_type *type) | |
1432 | { | |
1433 | return type->name; | |
1434 | } | |
1435 | \f | |
fa37b408 BP |
1436 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of |
1437 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
1438 | * an arbitrary transaction id. Allocated bytes beyond the header, if any, are | |
1439 | * zeroed. | |
1440 | * | |
1441 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
1442 | * needed. | |
1443 | * | |
1444 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1445 | * message is later extended, the length should be updated with | |
1446 | * update_openflow_length() before sending. | |
1447 | * | |
1448 | * Returns the header. */ | |
1449 | void * | |
1450 | make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp) | |
1451 | { | |
1452 | *bufferp = ofpbuf_new(openflow_len); | |
1453 | return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp); | |
1454 | } | |
1455 | ||
0bd0c660 BP |
1456 | /* Similar to make_openflow() but creates a Nicira vendor extension message |
1457 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1458 | void * | |
1459 | make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp) | |
1460 | { | |
1461 | return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp); | |
1462 | } | |
1463 | ||
fa37b408 BP |
1464 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of |
1465 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
1466 | * transaction id 'xid'. Allocated bytes beyond the header, if any, are | |
1467 | * zeroed. | |
1468 | * | |
1469 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
1470 | * needed. | |
1471 | * | |
1472 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1473 | * message is later extended, the length should be updated with | |
1474 | * update_openflow_length() before sending. | |
1475 | * | |
1476 | * Returns the header. */ | |
1477 | void * | |
44381c1b | 1478 | make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid, |
fa37b408 BP |
1479 | struct ofpbuf **bufferp) |
1480 | { | |
1481 | *bufferp = ofpbuf_new(openflow_len); | |
1482 | return put_openflow_xid(openflow_len, type, xid, *bufferp); | |
1483 | } | |
1484 | ||
0bd0c660 BP |
1485 | /* Similar to make_openflow_xid() but creates a Nicira vendor extension message |
1486 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1487 | void * | |
44381c1b | 1488 | make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid, |
0bd0c660 BP |
1489 | struct ofpbuf **bufferp) |
1490 | { | |
dfdfc8d4 BP |
1491 | *bufferp = ofpbuf_new(openflow_len); |
1492 | return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp); | |
0bd0c660 BP |
1493 | } |
1494 | ||
fa37b408 BP |
1495 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header |
1496 | * with the given 'type' and an arbitrary transaction id. Allocated bytes | |
1497 | * beyond the header, if any, are zeroed. | |
1498 | * | |
1499 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1500 | * message is later extended, the length should be updated with | |
1501 | * update_openflow_length() before sending. | |
1502 | * | |
1503 | * Returns the header. */ | |
1504 | void * | |
1505 | put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer) | |
1506 | { | |
1507 | return put_openflow_xid(openflow_len, type, alloc_xid(), buffer); | |
1508 | } | |
1509 | ||
1510 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header | |
1511 | * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond | |
1512 | * the header, if any, are zeroed. | |
1513 | * | |
1514 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1515 | * message is later extended, the length should be updated with | |
1516 | * update_openflow_length() before sending. | |
1517 | * | |
1518 | * Returns the header. */ | |
1519 | void * | |
44381c1b | 1520 | put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid, |
fa37b408 BP |
1521 | struct ofpbuf *buffer) |
1522 | { | |
1523 | struct ofp_header *oh; | |
1524 | ||
1525 | assert(openflow_len >= sizeof *oh); | |
1526 | assert(openflow_len <= UINT16_MAX); | |
1527 | ||
1528 | oh = ofpbuf_put_uninit(buffer, openflow_len); | |
1529 | oh->version = OFP_VERSION; | |
1530 | oh->type = type; | |
1531 | oh->length = htons(openflow_len); | |
1532 | oh->xid = xid; | |
1533 | memset(oh + 1, 0, openflow_len - sizeof *oh); | |
1534 | return oh; | |
1535 | } | |
1536 | ||
dfdfc8d4 BP |
1537 | /* Similar to put_openflow() but append a Nicira vendor extension message with |
1538 | * the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1539 | void * | |
1540 | put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer) | |
1541 | { | |
1542 | return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer); | |
1543 | } | |
1544 | ||
1545 | /* Similar to put_openflow_xid() but append a Nicira vendor extension message | |
1546 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1547 | void * | |
1548 | put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid, | |
1549 | struct ofpbuf *buffer) | |
1550 | { | |
1551 | struct nicira_header *nxh; | |
1552 | ||
1553 | nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer); | |
1554 | nxh->vendor = htonl(NX_VENDOR_ID); | |
1555 | nxh->subtype = htonl(subtype); | |
1556 | return nxh; | |
1557 | } | |
1558 | ||
fa37b408 BP |
1559 | /* Updates the 'length' field of the OpenFlow message in 'buffer' to |
1560 | * 'buffer->size'. */ | |
1561 | void | |
d295e8e9 | 1562 | update_openflow_length(struct ofpbuf *buffer) |
fa37b408 BP |
1563 | { |
1564 | struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh); | |
d295e8e9 | 1565 | oh->length = htons(buffer->size); |
fa37b408 BP |
1566 | } |
1567 | ||
dfdfc8d4 BP |
1568 | /* Creates an ofp_stats_request with the given 'type' and 'body_len' bytes of |
1569 | * space allocated for the 'body' member. Returns the first byte of the 'body' | |
1570 | * member. */ | |
1571 | void * | |
1572 | ofputil_make_stats_request(size_t body_len, uint16_t type, | |
1573 | struct ofpbuf **bufferp) | |
1574 | { | |
1575 | struct ofp_stats_request *osr; | |
1576 | osr = make_openflow((offsetof(struct ofp_stats_request, body) | |
1577 | + body_len), OFPT_STATS_REQUEST, bufferp); | |
1578 | osr->type = htons(type); | |
1579 | osr->flags = htons(0); | |
1580 | return osr->body; | |
1581 | } | |
1582 | ||
1583 | /* Creates a stats request message with Nicira as vendor and the given | |
1584 | * 'subtype', of total length 'openflow_len'. Returns the message. */ | |
1585 | void * | |
1586 | ofputil_make_nxstats_request(size_t openflow_len, uint32_t subtype, | |
1587 | struct ofpbuf **bufferp) | |
1588 | { | |
1589 | struct nicira_stats_msg *nsm; | |
1590 | ||
1591 | nsm = make_openflow(openflow_len, OFPT_STATS_REQUEST, bufferp); | |
1592 | nsm->type = htons(OFPST_VENDOR); | |
1593 | nsm->flags = htons(0); | |
1594 | nsm->vendor = htonl(NX_VENDOR_ID); | |
1595 | nsm->subtype = htonl(subtype); | |
1596 | return nsm; | |
1597 | } | |
1598 | ||
d1e2cf21 BP |
1599 | /* Returns the first byte of the 'body' member of the ofp_stats_request or |
1600 | * ofp_stats_reply in 'oh'. */ | |
1601 | const void * | |
1602 | ofputil_stats_body(const struct ofp_header *oh) | |
1603 | { | |
1604 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
1605 | return ((const struct ofp_stats_request *) oh)->body; | |
1606 | } | |
1607 | ||
1608 | /* Returns the length of the 'body' member of the ofp_stats_request or | |
1609 | * ofp_stats_reply in 'oh'. */ | |
1610 | size_t | |
1611 | ofputil_stats_body_len(const struct ofp_header *oh) | |
1612 | { | |
1613 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
1614 | return ntohs(oh->length) - sizeof(struct ofp_stats_request); | |
1615 | } | |
1616 | ||
c6430da5 BP |
1617 | /* Returns the first byte of the body of the nicira_stats_msg in 'oh'. */ |
1618 | const void * | |
1619 | ofputil_nxstats_body(const struct ofp_header *oh) | |
1620 | { | |
1621 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
1622 | return ((const struct nicira_stats_msg *) oh) + 1; | |
1623 | } | |
1624 | ||
1625 | /* Returns the length of the body of the nicira_stats_msg in 'oh'. */ | |
1626 | size_t | |
1627 | ofputil_nxstats_body_len(const struct ofp_header *oh) | |
1628 | { | |
1629 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
1630 | return ntohs(oh->length) - sizeof(struct nicira_stats_msg); | |
1631 | } | |
1632 | ||
fa37b408 | 1633 | struct ofpbuf * |
daa68e9f BP |
1634 | make_flow_mod(uint16_t command, const struct cls_rule *rule, |
1635 | size_t actions_len) | |
fa37b408 BP |
1636 | { |
1637 | struct ofp_flow_mod *ofm; | |
1638 | size_t size = sizeof *ofm + actions_len; | |
1639 | struct ofpbuf *out = ofpbuf_new(size); | |
1640 | ofm = ofpbuf_put_zeros(out, sizeof *ofm); | |
1641 | ofm->header.version = OFP_VERSION; | |
1642 | ofm->header.type = OFPT_FLOW_MOD; | |
1643 | ofm->header.length = htons(size); | |
1644 | ofm->cookie = 0; | |
daa68e9f | 1645 | ofm->priority = htons(MIN(rule->priority, UINT16_MAX)); |
b78f6b77 | 1646 | ofputil_cls_rule_to_match(rule, &ofm->match); |
fa37b408 BP |
1647 | ofm->command = htons(command); |
1648 | return out; | |
1649 | } | |
1650 | ||
1651 | struct ofpbuf * | |
daa68e9f | 1652 | make_add_flow(const struct cls_rule *rule, uint32_t buffer_id, |
fa37b408 BP |
1653 | uint16_t idle_timeout, size_t actions_len) |
1654 | { | |
daa68e9f | 1655 | struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len); |
fa37b408 BP |
1656 | struct ofp_flow_mod *ofm = out->data; |
1657 | ofm->idle_timeout = htons(idle_timeout); | |
1658 | ofm->hard_timeout = htons(OFP_FLOW_PERMANENT); | |
1659 | ofm->buffer_id = htonl(buffer_id); | |
1660 | return out; | |
1661 | } | |
1662 | ||
1663 | struct ofpbuf * | |
daa68e9f | 1664 | make_del_flow(const struct cls_rule *rule) |
fa37b408 | 1665 | { |
daa68e9f | 1666 | struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0); |
fa37b408 BP |
1667 | struct ofp_flow_mod *ofm = out->data; |
1668 | ofm->out_port = htons(OFPP_NONE); | |
1669 | return out; | |
1670 | } | |
1671 | ||
1672 | struct ofpbuf * | |
daa68e9f | 1673 | make_add_simple_flow(const struct cls_rule *rule, |
fa37b408 BP |
1674 | uint32_t buffer_id, uint16_t out_port, |
1675 | uint16_t idle_timeout) | |
1676 | { | |
81f3cad4 BP |
1677 | if (out_port != OFPP_NONE) { |
1678 | struct ofp_action_output *oao; | |
1679 | struct ofpbuf *buffer; | |
1680 | ||
daa68e9f | 1681 | buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao); |
81f3cad4 BP |
1682 | oao = ofpbuf_put_zeros(buffer, sizeof *oao); |
1683 | oao->type = htons(OFPAT_OUTPUT); | |
1684 | oao->len = htons(sizeof *oao); | |
1685 | oao->port = htons(out_port); | |
1686 | return buffer; | |
1687 | } else { | |
daa68e9f | 1688 | return make_add_flow(rule, buffer_id, idle_timeout, 0); |
81f3cad4 | 1689 | } |
fa37b408 BP |
1690 | } |
1691 | ||
1692 | struct ofpbuf * | |
1693 | make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason, | |
1694 | const struct ofpbuf *payload, int max_send_len) | |
1695 | { | |
1696 | struct ofp_packet_in *opi; | |
1697 | struct ofpbuf *buf; | |
1698 | int send_len; | |
1699 | ||
1700 | send_len = MIN(max_send_len, payload->size); | |
1701 | buf = ofpbuf_new(sizeof *opi + send_len); | |
1702 | opi = put_openflow_xid(offsetof(struct ofp_packet_in, data), | |
1703 | OFPT_PACKET_IN, 0, buf); | |
1704 | opi->buffer_id = htonl(buffer_id); | |
1705 | opi->total_len = htons(payload->size); | |
1706 | opi->in_port = htons(in_port); | |
1707 | opi->reason = reason; | |
1708 | ofpbuf_put(buf, payload->data, send_len); | |
1709 | update_openflow_length(buf); | |
1710 | ||
1711 | return buf; | |
1712 | } | |
1713 | ||
1714 | struct ofpbuf * | |
1715 | make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id, | |
1716 | uint16_t in_port, | |
1717 | const struct ofp_action_header *actions, size_t n_actions) | |
1718 | { | |
1719 | size_t actions_len = n_actions * sizeof *actions; | |
1720 | struct ofp_packet_out *opo; | |
1721 | size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0); | |
1722 | struct ofpbuf *out = ofpbuf_new(size); | |
1723 | ||
1724 | opo = ofpbuf_put_uninit(out, sizeof *opo); | |
1725 | opo->header.version = OFP_VERSION; | |
1726 | opo->header.type = OFPT_PACKET_OUT; | |
1727 | opo->header.length = htons(size); | |
1728 | opo->header.xid = htonl(0); | |
1729 | opo->buffer_id = htonl(buffer_id); | |
1730 | opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port); | |
1731 | opo->actions_len = htons(actions_len); | |
1732 | ofpbuf_put(out, actions, actions_len); | |
1733 | if (packet) { | |
1734 | ofpbuf_put(out, packet->data, packet->size); | |
1735 | } | |
1736 | return out; | |
1737 | } | |
1738 | ||
1739 | struct ofpbuf * | |
1740 | make_unbuffered_packet_out(const struct ofpbuf *packet, | |
1741 | uint16_t in_port, uint16_t out_port) | |
1742 | { | |
1743 | struct ofp_action_output action; | |
1744 | action.type = htons(OFPAT_OUTPUT); | |
1745 | action.len = htons(sizeof action); | |
1746 | action.port = htons(out_port); | |
1747 | return make_packet_out(packet, UINT32_MAX, in_port, | |
1748 | (struct ofp_action_header *) &action, 1); | |
1749 | } | |
1750 | ||
1751 | struct ofpbuf * | |
1752 | make_buffered_packet_out(uint32_t buffer_id, | |
1753 | uint16_t in_port, uint16_t out_port) | |
1754 | { | |
81f3cad4 BP |
1755 | if (out_port != OFPP_NONE) { |
1756 | struct ofp_action_output action; | |
1757 | action.type = htons(OFPAT_OUTPUT); | |
1758 | action.len = htons(sizeof action); | |
1759 | action.port = htons(out_port); | |
1760 | return make_packet_out(NULL, buffer_id, in_port, | |
1761 | (struct ofp_action_header *) &action, 1); | |
1762 | } else { | |
1763 | return make_packet_out(NULL, buffer_id, in_port, NULL, 0); | |
1764 | } | |
fa37b408 BP |
1765 | } |
1766 | ||
1767 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ | |
1768 | struct ofpbuf * | |
1769 | make_echo_request(void) | |
1770 | { | |
1771 | struct ofp_header *rq; | |
1772 | struct ofpbuf *out = ofpbuf_new(sizeof *rq); | |
1773 | rq = ofpbuf_put_uninit(out, sizeof *rq); | |
1774 | rq->version = OFP_VERSION; | |
1775 | rq->type = OFPT_ECHO_REQUEST; | |
1776 | rq->length = htons(sizeof *rq); | |
44381c1b | 1777 | rq->xid = htonl(0); |
fa37b408 BP |
1778 | return out; |
1779 | } | |
1780 | ||
1781 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
1782 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
1783 | struct ofpbuf * | |
1784 | make_echo_reply(const struct ofp_header *rq) | |
1785 | { | |
1786 | size_t size = ntohs(rq->length); | |
1787 | struct ofpbuf *out = ofpbuf_new(size); | |
1788 | struct ofp_header *reply = ofpbuf_put(out, rq, size); | |
1789 | reply->type = OFPT_ECHO_REPLY; | |
1790 | return out; | |
1791 | } | |
1792 | ||
fa37b408 BP |
1793 | static int |
1794 | check_action_exact_len(const union ofp_action *a, unsigned int len, | |
1795 | unsigned int required_len) | |
1796 | { | |
1797 | if (len != required_len) { | |
35c4693a BP |
1798 | VLOG_WARN_RL(&bad_ofmsg_rl, "action %"PRIu16" has invalid length " |
1799 | "%"PRIu16" (must be %u)\n", | |
1800 | ntohs(a->type), ntohs(a->header.len), required_len); | |
fa37b408 BP |
1801 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); |
1802 | } | |
1803 | return 0; | |
1804 | } | |
1805 | ||
abcf9134 BP |
1806 | static int |
1807 | check_nx_action_exact_len(const struct nx_action_header *a, | |
1808 | unsigned int len, unsigned int required_len) | |
1809 | { | |
1810 | if (len != required_len) { | |
1811 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
1812 | "Nicira action %"PRIu16" has invalid length %"PRIu16" " | |
1813 | "(must be %u)\n", | |
1814 | ntohs(a->subtype), ntohs(a->len), required_len); | |
1815 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); | |
1816 | } | |
1817 | return 0; | |
1818 | } | |
1819 | ||
c1c9c9c4 BP |
1820 | /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given |
1821 | * that the switch will never have more than 'max_ports' ports. Returns 0 if | |
1822 | * 'port' is valid, otherwise an ofp_mkerr() return code. */ | |
fa37b408 | 1823 | static int |
c1c9c9c4 | 1824 | check_output_port(uint16_t port, int max_ports) |
fa37b408 BP |
1825 | { |
1826 | switch (port) { | |
1827 | case OFPP_IN_PORT: | |
1828 | case OFPP_TABLE: | |
1829 | case OFPP_NORMAL: | |
1830 | case OFPP_FLOOD: | |
1831 | case OFPP_ALL: | |
1832 | case OFPP_CONTROLLER: | |
1833 | case OFPP_LOCAL: | |
1834 | return 0; | |
1835 | ||
1836 | default: | |
c1c9c9c4 | 1837 | if (port < max_ports) { |
fa37b408 BP |
1838 | return 0; |
1839 | } | |
1840 | VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port); | |
1841 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT); | |
1842 | } | |
1843 | } | |
1844 | ||
c1c9c9c4 BP |
1845 | /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch |
1846 | * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid, | |
1847 | * otherwise an ofp_mkerr() return code. */ | |
1848 | static int | |
1849 | check_enqueue_action(const union ofp_action *a, unsigned int len, | |
1850 | int max_ports) | |
1851 | { | |
1852 | const struct ofp_action_enqueue *oae; | |
1853 | uint16_t port; | |
1854 | int error; | |
1855 | ||
1856 | error = check_action_exact_len(a, len, 16); | |
1857 | if (error) { | |
1858 | return error; | |
1859 | } | |
1860 | ||
1861 | oae = (const struct ofp_action_enqueue *) a; | |
1862 | port = ntohs(oae->port); | |
1863 | if (port < max_ports || port == OFPP_IN_PORT) { | |
1864 | return 0; | |
1865 | } | |
1866 | VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port); | |
1867 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT); | |
1868 | } | |
1869 | ||
fa37b408 | 1870 | static int |
b6c9e612 BP |
1871 | check_nicira_action(const union ofp_action *a, unsigned int len, |
1872 | const struct flow *flow) | |
fa37b408 BP |
1873 | { |
1874 | const struct nx_action_header *nah; | |
5160ab34 | 1875 | int subtype; |
b6c9e612 | 1876 | int error; |
fa37b408 BP |
1877 | |
1878 | if (len < 16) { | |
f4350529 BP |
1879 | VLOG_WARN_RL(&bad_ofmsg_rl, |
1880 | "Nicira vendor action only %u bytes", len); | |
fa37b408 BP |
1881 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); |
1882 | } | |
1883 | nah = (const struct nx_action_header *) a; | |
1884 | ||
e41a9130 BP |
1885 | subtype = ntohs(nah->subtype); |
1886 | if (subtype > TYPE_MAXIMUM(enum nx_action_subtype)) { | |
5160ab34 | 1887 | /* This is necessary because enum nx_action_subtype may be an |
e41a9130 BP |
1888 | * 8-bit type, so the cast below throws away the top 8 bits. */ |
1889 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE); | |
1890 | } | |
1891 | ||
1892 | switch ((enum nx_action_subtype) subtype) { | |
fa37b408 BP |
1893 | case NXAST_RESUBMIT: |
1894 | case NXAST_SET_TUNNEL: | |
92467099 | 1895 | case NXAST_DROP_SPOOFED_ARP: |
eedc0097 JP |
1896 | case NXAST_SET_QUEUE: |
1897 | case NXAST_POP_QUEUE: | |
abcf9134 | 1898 | return check_nx_action_exact_len(nah, len, 16); |
96fc46e8 | 1899 | |
b6c9e612 | 1900 | case NXAST_REG_MOVE: |
abcf9134 BP |
1901 | error = check_nx_action_exact_len(nah, len, |
1902 | sizeof(struct nx_action_reg_move)); | |
b6c9e612 BP |
1903 | if (error) { |
1904 | return error; | |
1905 | } | |
1906 | return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow); | |
96fc46e8 | 1907 | |
b6c9e612 | 1908 | case NXAST_REG_LOAD: |
abcf9134 BP |
1909 | error = check_nx_action_exact_len(nah, len, |
1910 | sizeof(struct nx_action_reg_load)); | |
b6c9e612 BP |
1911 | if (error) { |
1912 | return error; | |
1913 | } | |
1914 | return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow); | |
96fc46e8 BP |
1915 | |
1916 | case NXAST_NOTE: | |
1917 | return 0; | |
1918 | ||
b9298d3f | 1919 | case NXAST_SET_TUNNEL64: |
abcf9134 BP |
1920 | return check_nx_action_exact_len( |
1921 | nah, len, sizeof(struct nx_action_set_tunnel64)); | |
b9298d3f | 1922 | |
53ddd40a | 1923 | case NXAST_MULTIPATH: |
abcf9134 BP |
1924 | error = check_nx_action_exact_len( |
1925 | nah, len, sizeof(struct nx_action_multipath)); | |
53ddd40a BP |
1926 | if (error) { |
1927 | return error; | |
1928 | } | |
1929 | return multipath_check((const struct nx_action_multipath *) a); | |
1930 | ||
3b6a2571 EJ |
1931 | case NXAST_AUTOPATH: |
1932 | error = check_nx_action_exact_len( | |
1933 | nah, len, sizeof(struct nx_action_autopath)); | |
1934 | if (error) { | |
1935 | return error; | |
1936 | } | |
1937 | return autopath_check((const struct nx_action_autopath *) a); | |
1938 | ||
e41a9130 | 1939 | case NXAST_SNAT__OBSOLETE: |
fa37b408 | 1940 | default: |
abcf9134 | 1941 | VLOG_WARN_RL(&bad_ofmsg_rl, |
5160ab34 | 1942 | "unknown Nicira vendor action subtype %d", subtype); |
fa37b408 BP |
1943 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE); |
1944 | } | |
1945 | } | |
1946 | ||
1947 | static int | |
b6c9e612 BP |
1948 | check_action(const union ofp_action *a, unsigned int len, |
1949 | const struct flow *flow, int max_ports) | |
fa37b408 | 1950 | { |
e41a9130 | 1951 | enum ofp_action_type type = ntohs(a->type); |
fa37b408 BP |
1952 | int error; |
1953 | ||
e41a9130 | 1954 | switch (type) { |
fa37b408 | 1955 | case OFPAT_OUTPUT: |
c1c9c9c4 BP |
1956 | error = check_action_exact_len(a, len, 8); |
1957 | if (error) { | |
1958 | return error; | |
1959 | } | |
1960 | return check_output_port(ntohs(a->output.port), max_ports); | |
fa37b408 BP |
1961 | |
1962 | case OFPAT_SET_VLAN_VID: | |
27bcf966 BP |
1963 | error = check_action_exact_len(a, len, 8); |
1964 | if (error) { | |
1965 | return error; | |
1966 | } | |
1967 | if (a->vlan_vid.vlan_vid & ~htons(0xfff)) { | |
1968 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT); | |
1969 | } | |
1970 | return 0; | |
1971 | ||
fa37b408 | 1972 | case OFPAT_SET_VLAN_PCP: |
27bcf966 BP |
1973 | error = check_action_exact_len(a, len, 8); |
1974 | if (error) { | |
1975 | return error; | |
1976 | } | |
d93ca2a0 | 1977 | if (a->vlan_pcp.vlan_pcp & ~7) { |
27bcf966 BP |
1978 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT); |
1979 | } | |
1980 | return 0; | |
1981 | ||
fa37b408 BP |
1982 | case OFPAT_STRIP_VLAN: |
1983 | case OFPAT_SET_NW_SRC: | |
1984 | case OFPAT_SET_NW_DST: | |
1985 | case OFPAT_SET_NW_TOS: | |
1986 | case OFPAT_SET_TP_SRC: | |
1987 | case OFPAT_SET_TP_DST: | |
1988 | return check_action_exact_len(a, len, 8); | |
1989 | ||
1990 | case OFPAT_SET_DL_SRC: | |
1991 | case OFPAT_SET_DL_DST: | |
1992 | return check_action_exact_len(a, len, 16); | |
1993 | ||
1994 | case OFPAT_VENDOR: | |
1995 | return (a->vendor.vendor == htonl(NX_VENDOR_ID) | |
b6c9e612 | 1996 | ? check_nicira_action(a, len, flow) |
fa37b408 BP |
1997 | : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR)); |
1998 | ||
c1c9c9c4 BP |
1999 | case OFPAT_ENQUEUE: |
2000 | return check_enqueue_action(a, len, max_ports); | |
2001 | ||
fa37b408 | 2002 | default: |
e41a9130 | 2003 | VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %d", (int) type); |
fa37b408 BP |
2004 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE); |
2005 | } | |
2006 | } | |
2007 | ||
2008 | int | |
2009 | validate_actions(const union ofp_action *actions, size_t n_actions, | |
b6c9e612 | 2010 | const struct flow *flow, int max_ports) |
fa37b408 | 2011 | { |
3dffcf07 | 2012 | size_t i; |
fa37b408 | 2013 | |
3dffcf07 BP |
2014 | for (i = 0; i < n_actions; ) { |
2015 | const union ofp_action *a = &actions[i]; | |
fa37b408 | 2016 | unsigned int len = ntohs(a->header.len); |
69b6be19 | 2017 | unsigned int n_slots = len / OFP_ACTION_ALIGN; |
fa37b408 BP |
2018 | unsigned int slots_left = &actions[n_actions] - a; |
2019 | int error; | |
2020 | ||
2021 | if (n_slots > slots_left) { | |
f4350529 BP |
2022 | VLOG_WARN_RL(&bad_ofmsg_rl, |
2023 | "action requires %u slots but only %u remain", | |
2024 | n_slots, slots_left); | |
fa37b408 BP |
2025 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); |
2026 | } else if (!len) { | |
f4350529 | 2027 | VLOG_WARN_RL(&bad_ofmsg_rl, "action has invalid length 0"); |
fa37b408 | 2028 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); |
69b6be19 | 2029 | } else if (len % OFP_ACTION_ALIGN) { |
f4350529 BP |
2030 | VLOG_WARN_RL(&bad_ofmsg_rl, "action length %u is not a multiple " |
2031 | "of %d", len, OFP_ACTION_ALIGN); | |
fa37b408 BP |
2032 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); |
2033 | } | |
2034 | ||
b6c9e612 | 2035 | error = check_action(a, len, flow, max_ports); |
fa37b408 BP |
2036 | if (error) { |
2037 | return error; | |
2038 | } | |
3dffcf07 | 2039 | i += n_slots; |
fa37b408 BP |
2040 | } |
2041 | return 0; | |
2042 | } | |
2043 | ||
dbba996b | 2044 | /* Returns true if 'action' outputs to 'port', false otherwise. */ |
c1c9c9c4 | 2045 | bool |
dbba996b | 2046 | action_outputs_to_port(const union ofp_action *action, ovs_be16 port) |
c1c9c9c4 BP |
2047 | { |
2048 | switch (ntohs(action->type)) { | |
2049 | case OFPAT_OUTPUT: | |
2050 | return action->output.port == port; | |
2051 | case OFPAT_ENQUEUE: | |
2052 | return ((const struct ofp_action_enqueue *) action)->port == port; | |
2053 | default: | |
2054 | return false; | |
2055 | } | |
2056 | } | |
2057 | ||
fa37b408 BP |
2058 | /* The set of actions must either come from a trusted source or have been |
2059 | * previously validated with validate_actions(). */ | |
2060 | const union ofp_action * | |
2061 | actions_first(struct actions_iterator *iter, | |
2062 | const union ofp_action *oa, size_t n_actions) | |
2063 | { | |
2064 | iter->pos = oa; | |
2065 | iter->end = oa + n_actions; | |
2066 | return actions_next(iter); | |
2067 | } | |
2068 | ||
2069 | const union ofp_action * | |
2070 | actions_next(struct actions_iterator *iter) | |
2071 | { | |
3dffcf07 | 2072 | if (iter->pos != iter->end) { |
fa37b408 BP |
2073 | const union ofp_action *a = iter->pos; |
2074 | unsigned int len = ntohs(a->header.len); | |
69b6be19 | 2075 | iter->pos += len / OFP_ACTION_ALIGN; |
fa37b408 BP |
2076 | return a; |
2077 | } else { | |
2078 | return NULL; | |
2079 | } | |
2080 | } | |
2081 | ||
1c0b7503 BP |
2082 | static ovs_be32 |
2083 | normalize_wildcards(const struct ofp_match *m) | |
fa37b408 | 2084 | { |
f1f41fb6 | 2085 | enum { OFPFW_NW = (OFPFW_NW_SRC_ALL | OFPFW_NW_DST_ALL | OFPFW_NW_PROTO |
371e4acc | 2086 | | OFPFW_NW_TOS) }; |
fa37b408 | 2087 | enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST }; |
1c0b7503 | 2088 | ovs_be32 wc; |
fa37b408 | 2089 | |
1c0b7503 BP |
2090 | wc = m->wildcards; |
2091 | if (wc & htonl(OFPFW_DL_TYPE)) { | |
2092 | wc |= htonl(OFPFW_NW | OFPFW_TP); | |
fa37b408 | 2093 | } else if (m->dl_type == htons(ETH_TYPE_IP)) { |
1c0b7503 BP |
2094 | if (wc & htonl(OFPFW_NW_PROTO) || (m->nw_proto != IPPROTO_TCP && |
2095 | m->nw_proto != IPPROTO_UDP && | |
2096 | m->nw_proto != IPPROTO_ICMP)) { | |
2097 | wc |= htonl(OFPFW_TP); | |
fa37b408 | 2098 | } |
1c0b7503 | 2099 | } else if (m->dl_type == htons(ETH_TYPE_ARP)) { |
af6bce18 | 2100 | wc |= htonl(OFPFW_TP | OFPFW_NW_TOS); |
1c0b7503 BP |
2101 | } else { |
2102 | wc |= htonl(OFPFW_NW | OFPFW_TP); | |
fa37b408 | 2103 | } |
1c0b7503 | 2104 | return wc; |
3f09c339 | 2105 | } |
26c112c2 BP |
2106 | |
2107 | static uint32_t | |
2108 | vendor_code_to_id(uint8_t code) | |
2109 | { | |
2110 | switch (code) { | |
2111 | #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID; | |
3a75cda9 BP |
2112 | OFPUTIL_VENDORS |
2113 | #undef OFPUTIL_VENDOR | |
26c112c2 BP |
2114 | default: |
2115 | return UINT32_MAX; | |
2116 | } | |
2117 | } | |
2118 | ||
dc4762ed BP |
2119 | static int |
2120 | vendor_id_to_code(uint32_t id) | |
2121 | { | |
2122 | switch (id) { | |
2123 | #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME; | |
2124 | OFPUTIL_VENDORS | |
2125 | #undef OFPUTIL_VENDOR | |
2126 | default: | |
2127 | return -1; | |
2128 | } | |
2129 | } | |
2130 | ||
26c112c2 BP |
2131 | /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error |
2132 | * information taken from 'error', whose encoding must be as described in the | |
2133 | * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use | |
2134 | * oh->xid as its transaction ID, and it will include up to the first 64 bytes | |
2135 | * of 'oh'. | |
2136 | * | |
2137 | * Returns NULL if 'error' is not an OpenFlow error code. */ | |
2138 | struct ofpbuf * | |
dc4762ed | 2139 | ofputil_encode_error_msg(int error, const struct ofp_header *oh) |
26c112c2 BP |
2140 | { |
2141 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
2142 | ||
2143 | struct ofpbuf *buf; | |
2144 | const void *data; | |
2145 | size_t len; | |
2146 | uint8_t vendor; | |
2147 | uint16_t type; | |
2148 | uint16_t code; | |
44381c1b | 2149 | ovs_be32 xid; |
26c112c2 BP |
2150 | |
2151 | if (!is_ofp_error(error)) { | |
2152 | /* We format 'error' with strerror() here since it seems likely to be | |
2153 | * a system errno value. */ | |
2154 | VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)", | |
2155 | error, strerror(error)); | |
2156 | return NULL; | |
2157 | } | |
2158 | ||
2159 | if (oh) { | |
2160 | xid = oh->xid; | |
2161 | data = oh; | |
2162 | len = ntohs(oh->length); | |
2163 | if (len > 64) { | |
2164 | len = 64; | |
2165 | } | |
2166 | } else { | |
2167 | xid = 0; | |
2168 | data = NULL; | |
2169 | len = 0; | |
2170 | } | |
2171 | ||
2172 | vendor = get_ofp_err_vendor(error); | |
2173 | type = get_ofp_err_type(error); | |
2174 | code = get_ofp_err_code(error); | |
2175 | if (vendor == OFPUTIL_VENDOR_OPENFLOW) { | |
2176 | struct ofp_error_msg *oem; | |
2177 | ||
2178 | oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf); | |
2179 | oem->type = htons(type); | |
2180 | oem->code = htons(code); | |
2181 | } else { | |
2182 | struct ofp_error_msg *oem; | |
217f48c6 | 2183 | struct nx_vendor_error *nve; |
26c112c2 BP |
2184 | uint32_t vendor_id; |
2185 | ||
2186 | vendor_id = vendor_code_to_id(vendor); | |
2187 | if (vendor_id == UINT32_MAX) { | |
2188 | VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d", | |
2189 | error, vendor); | |
2190 | return NULL; | |
2191 | } | |
2192 | ||
217f48c6 | 2193 | oem = make_openflow_xid(len + sizeof *oem + sizeof *nve, |
26c112c2 BP |
2194 | OFPT_ERROR, xid, &buf); |
2195 | oem->type = htons(NXET_VENDOR); | |
2196 | oem->code = htons(NXVC_VENDOR_ERROR); | |
2197 | ||
17764fb2 | 2198 | nve = (struct nx_vendor_error *)oem->data; |
217f48c6 BP |
2199 | nve->vendor = htonl(vendor_id); |
2200 | nve->type = htons(type); | |
2201 | nve->code = htons(code); | |
26c112c2 BP |
2202 | } |
2203 | ||
2204 | if (len) { | |
59edb09c | 2205 | buf->size -= len; |
26c112c2 BP |
2206 | ofpbuf_put(buf, data, len); |
2207 | } | |
2208 | ||
2209 | return buf; | |
2210 | } | |
3052b0c5 | 2211 | |
dc4762ed BP |
2212 | /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an |
2213 | * Open vSwitch internal error code in the format described in the large | |
2214 | * comment in ofp-util.h. | |
2215 | * | |
2216 | * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset | |
2217 | * to the payload starting from 'oh' and on failure it is set to 0. */ | |
2218 | int | |
2219 | ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs) | |
2220 | { | |
2221 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
2222 | ||
2223 | const struct ofp_error_msg *oem; | |
2224 | uint16_t type, code; | |
2225 | struct ofpbuf b; | |
2226 | int vendor; | |
2227 | ||
2228 | if (payload_ofs) { | |
2229 | *payload_ofs = 0; | |
2230 | } | |
2231 | if (oh->type != OFPT_ERROR) { | |
2232 | return EPROTO; | |
2233 | } | |
2234 | ||
2235 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2236 | oem = ofpbuf_try_pull(&b, sizeof *oem); | |
2237 | if (!oem) { | |
2238 | return EPROTO; | |
2239 | } | |
2240 | ||
2241 | type = ntohs(oem->type); | |
2242 | code = ntohs(oem->code); | |
2243 | if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) { | |
2244 | const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve); | |
2245 | if (!nve) { | |
2246 | return EPROTO; | |
2247 | } | |
2248 | ||
2249 | vendor = vendor_id_to_code(ntohl(nve->vendor)); | |
2250 | if (vendor < 0) { | |
2251 | VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32, | |
2252 | ntohl(nve->vendor)); | |
2253 | return EPROTO; | |
2254 | } | |
2255 | type = ntohs(nve->type); | |
2256 | code = ntohs(nve->code); | |
2257 | } else { | |
2258 | vendor = OFPUTIL_VENDOR_OPENFLOW; | |
2259 | } | |
2260 | ||
2261 | if (type >= 1024) { | |
2262 | VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than " | |
2263 | "supported maximum value 1023", type); | |
2264 | return EPROTO; | |
2265 | } | |
2266 | ||
2267 | if (payload_ofs) { | |
2268 | *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh; | |
2269 | } | |
2270 | return ofp_mkerr_vendor(vendor, type, code); | |
2271 | } | |
2272 | ||
2273 | void | |
2274 | ofputil_format_error(struct ds *s, int error) | |
2275 | { | |
2276 | if (is_errno(error)) { | |
2277 | ds_put_cstr(s, strerror(error)); | |
2278 | } else { | |
2279 | uint16_t type = get_ofp_err_type(error); | |
2280 | uint16_t code = get_ofp_err_code(error); | |
2281 | const char *type_s = ofp_error_type_to_string(type); | |
2282 | const char *code_s = ofp_error_code_to_string(type, code); | |
2283 | ||
2284 | ds_put_format(s, "type "); | |
2285 | if (type_s) { | |
2286 | ds_put_cstr(s, type_s); | |
2287 | } else { | |
2288 | ds_put_format(s, "%"PRIu16, type); | |
2289 | } | |
2290 | ||
2291 | ds_put_cstr(s, ", code "); | |
2292 | if (code_s) { | |
2293 | ds_put_cstr(s, code_s); | |
2294 | } else { | |
2295 | ds_put_format(s, "%"PRIu16, code); | |
2296 | } | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | char * | |
2301 | ofputil_error_to_string(int error) | |
2302 | { | |
2303 | struct ds s = DS_EMPTY_INITIALIZER; | |
2304 | ofputil_format_error(&s, error); | |
2305 | return ds_steal_cstr(&s); | |
2306 | } | |
2307 | ||
3052b0c5 BP |
2308 | /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if |
2309 | * successful, otherwise an OpenFlow error. | |
2310 | * | |
2311 | * If successful, the first action is stored in '*actionsp' and the number of | |
2312 | * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0 | |
2313 | * are stored, respectively. | |
2314 | * | |
2315 | * This function does not check that the actions are valid (the caller should | |
2316 | * do so, with validate_actions()). The caller is also responsible for making | |
2317 | * sure that 'b->data' is initially aligned appropriately for "union | |
2318 | * ofp_action". */ | |
2319 | int | |
2320 | ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len, | |
2321 | union ofp_action **actionsp, size_t *n_actionsp) | |
2322 | { | |
69b6be19 | 2323 | if (actions_len % OFP_ACTION_ALIGN != 0) { |
f4350529 BP |
2324 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u " |
2325 | "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN); | |
3052b0c5 BP |
2326 | goto error; |
2327 | } | |
2328 | ||
2329 | *actionsp = ofpbuf_try_pull(b, actions_len); | |
2330 | if (*actionsp == NULL) { | |
f4350529 BP |
2331 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u " |
2332 | "exceeds remaining message length (%zu)", | |
2333 | actions_len, b->size); | |
3052b0c5 BP |
2334 | goto error; |
2335 | } | |
2336 | ||
69b6be19 | 2337 | *n_actionsp = actions_len / OFP_ACTION_ALIGN; |
3052b0c5 BP |
2338 | return 0; |
2339 | ||
2340 | error: | |
2341 | *actionsp = NULL; | |
2342 | *n_actionsp = 0; | |
2343 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
2344 | } |