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