]>
Commit | Line | Data |
---|---|---|
fa37b408 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
fa37b408 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include "ofp-print.h" | |
dc4762ed | 19 | #include <errno.h> |
fa37b408 | 20 | #include <inttypes.h> |
6ca00f6f ETN |
21 | #include <sys/types.h> |
22 | #include <netinet/in.h> | |
b459a924 | 23 | #include <netinet/icmp6.h> |
fa37b408 | 24 | #include <stdlib.h> |
3b6a2571 | 25 | #include "autopath.h" |
daff3353 | 26 | #include "bundle.h" |
10a24935 | 27 | #include "byte-order.h" |
d8ae4d67 | 28 | #include "classifier.h" |
dc4762ed | 29 | #include "dynamic-string.h" |
75a75043 | 30 | #include "learn.h" |
816fd533 | 31 | #include "meta-flow.h" |
9e1fd49b | 32 | #include "multipath.h" |
6c038611 | 33 | #include "netdev.h" |
b6c9e612 | 34 | #include "nx-match.h" |
dc4762ed | 35 | #include "ofp-errors.h" |
fa37b408 BP |
36 | #include "ofp-util.h" |
37 | #include "ofpbuf.h" | |
38 | #include "packets.h" | |
39 | #include "random.h" | |
4ffd1b43 | 40 | #include "unaligned.h" |
e41a9130 | 41 | #include "type-props.h" |
5136ce49 | 42 | #include "vlog.h" |
fa37b408 | 43 | |
d98e6007 | 44 | VLOG_DEFINE_THIS_MODULE(ofp_util); |
fa37b408 BP |
45 | |
46 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
47 | * in the peer and so there's not much point in showing a lot of them. */ | |
48 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
49 | ||
0596e897 BP |
50 | /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns |
51 | * an IP netmask with a 1 in each bit that must match and a 0 in each bit that | |
52 | * is wildcarded. | |
53 | * | |
54 | * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0 | |
55 | * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, | |
56 | * ..., 32 and higher wildcard the entire field. This is the *opposite* of the | |
57 | * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are | |
58 | * wildcarded. */ | |
59 | ovs_be32 | |
60 | ofputil_wcbits_to_netmask(int wcbits) | |
61 | { | |
62 | wcbits &= 0x3f; | |
63 | return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0; | |
64 | } | |
65 | ||
66 | /* Given the IP netmask 'netmask', returns the number of bits of the IP address | |
aad29cd1 BP |
67 | * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask' |
68 | * must be a CIDR netmask (see ip_is_cidr()). */ | |
0596e897 BP |
69 | int |
70 | ofputil_netmask_to_wcbits(ovs_be32 netmask) | |
71 | { | |
aad29cd1 | 72 | return 32 - ip_count_cidr_bits(netmask); |
0596e897 BP |
73 | } |
74 | ||
d8ae4d67 BP |
75 | /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and |
76 | * name. */ | |
77 | #define WC_INVARIANT_LIST \ | |
78 | WC_INVARIANT_BIT(IN_PORT) \ | |
d8ae4d67 BP |
79 | WC_INVARIANT_BIT(DL_SRC) \ |
80 | WC_INVARIANT_BIT(DL_DST) \ | |
81 | WC_INVARIANT_BIT(DL_TYPE) \ | |
73f33563 | 82 | WC_INVARIANT_BIT(NW_PROTO) |
d8ae4d67 BP |
83 | |
84 | /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST) | |
85 | * actually have the same names and values. */ | |
86 | #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME); | |
87 | WC_INVARIANT_LIST | |
88 | #undef WC_INVARIANT_BIT | |
89 | ||
90 | /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all | |
91 | * OR'd together. */ | |
eeba8e4f | 92 | static const flow_wildcards_t WC_INVARIANTS = 0 |
d8ae4d67 BP |
93 | #define WC_INVARIANT_BIT(NAME) | FWW_##NAME |
94 | WC_INVARIANT_LIST | |
95 | #undef WC_INVARIANT_BIT | |
eeba8e4f | 96 | ; |
d8ae4d67 | 97 | |
eb6f28db BP |
98 | /* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in |
99 | * struct cls_rule. It is the caller's responsibility to handle the special | |
100 | * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */ | |
7286b1e1 | 101 | void |
eb6f28db | 102 | ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc) |
d8ae4d67 | 103 | { |
47284b1f | 104 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 10); |
a877206f | 105 | |
d8ae4d67 | 106 | /* Initialize most of rule->wc. */ |
f9ba8dad | 107 | flow_wildcards_init_catchall(wc); |
eeba8e4f | 108 | wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS; |
bad68a99 JP |
109 | |
110 | /* Wildcard fields that aren't defined by ofp_match or tun_id. */ | |
2486e66a | 111 | wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_NW_ECN | FWW_NW_TTL |
47284b1f | 112 | | FWW_IPV6_LABEL); |
bad68a99 | 113 | |
2486e66a JP |
114 | if (ofpfw & OFPFW_NW_TOS) { |
115 | /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in | |
116 | * the enum than we can use. */ | |
117 | wc->wildcards |= FWW_NW_DSCP; | |
d8ae4d67 | 118 | } |
7257b535 | 119 | |
d8ae4d67 BP |
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 | ||
73f33563 BP |
123 | if (!(ofpfw & OFPFW_TP_SRC)) { |
124 | wc->tp_src_mask = htons(UINT16_MAX); | |
125 | } | |
126 | if (!(ofpfw & OFPFW_TP_DST)) { | |
127 | wc->tp_dst_mask = htons(UINT16_MAX); | |
128 | } | |
129 | ||
d8ae4d67 BP |
130 | if (ofpfw & OFPFW_DL_DST) { |
131 | /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but | |
132 | * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST | |
133 | * and FWW_ETH_MCAST. */ | |
134 | wc->wildcards |= FWW_ETH_MCAST; | |
135 | } | |
136 | ||
eb6f28db BP |
137 | /* VLAN TCI mask. */ |
138 | if (!(ofpfw & OFPFW_DL_VLAN_PCP)) { | |
139 | wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI); | |
140 | } | |
141 | if (!(ofpfw & OFPFW_DL_VLAN)) { | |
142 | wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI); | |
143 | } | |
144 | } | |
145 | ||
146 | /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given | |
147 | * 'priority'. */ | |
148 | void | |
149 | ofputil_cls_rule_from_match(const struct ofp_match *match, | |
150 | unsigned int priority, struct cls_rule *rule) | |
151 | { | |
152 | uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL; | |
153 | ||
154 | /* Initialize rule->priority, rule->wc. */ | |
155 | rule->priority = !ofpfw ? UINT16_MAX : priority; | |
156 | ofputil_wildcard_from_openflow(ofpfw, &rule->wc); | |
157 | ||
66642cb4 | 158 | /* Initialize most of rule->flow. */ |
d8ae4d67 BP |
159 | rule->flow.nw_src = match->nw_src; |
160 | rule->flow.nw_dst = match->nw_dst; | |
abe529af | 161 | rule->flow.in_port = ntohs(match->in_port); |
36956a7d | 162 | rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type); |
d8ae4d67 BP |
163 | rule->flow.tp_src = match->tp_src; |
164 | rule->flow.tp_dst = match->tp_dst; | |
165 | memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN); | |
166 | memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN); | |
eadef313 | 167 | rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK; |
d8ae4d67 BP |
168 | rule->flow.nw_proto = match->nw_proto; |
169 | ||
66642cb4 | 170 | /* Translate VLANs. */ |
47271d0d BP |
171 | if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) { |
172 | /* Match only packets without 802.1Q header. | |
173 | * | |
174 | * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct. | |
175 | * | |
176 | * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory, | |
177 | * because we can't have a specific PCP without an 802.1Q header. | |
178 | * However, older versions of OVS treated this as matching packets | |
179 | * withut an 802.1Q header, so we do here too. */ | |
66642cb4 | 180 | rule->flow.vlan_tci = htons(0); |
eb6f28db | 181 | rule->wc.vlan_tci_mask = htons(0xffff); |
47271d0d BP |
182 | } else { |
183 | ovs_be16 vid, pcp, tci; | |
184 | ||
47271d0d BP |
185 | vid = match->dl_vlan & htons(VLAN_VID_MASK); |
186 | pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK); | |
187 | tci = vid | pcp | htons(VLAN_CFI); | |
eb6f28db | 188 | rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask; |
66642cb4 BP |
189 | } |
190 | ||
d8ae4d67 BP |
191 | /* Clean up. */ |
192 | cls_rule_zero_wildcarded_fields(rule); | |
193 | } | |
194 | ||
b78f6b77 | 195 | /* Convert 'rule' into the OpenFlow match structure 'match'. */ |
d8ae4d67 | 196 | void |
b78f6b77 | 197 | ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match) |
d8ae4d67 BP |
198 | { |
199 | const struct flow_wildcards *wc = &rule->wc; | |
eeba8e4f | 200 | uint32_t ofpfw; |
d8ae4d67 | 201 | |
66642cb4 | 202 | /* Figure out most OpenFlow wildcards. */ |
eeba8e4f | 203 | ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS); |
d8ae4d67 BP |
204 | ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT; |
205 | ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT; | |
2486e66a | 206 | if (wc->wildcards & FWW_NW_DSCP) { |
d8ae4d67 BP |
207 | ofpfw |= OFPFW_NW_TOS; |
208 | } | |
73f33563 BP |
209 | if (!wc->tp_src_mask) { |
210 | ofpfw |= OFPFW_TP_SRC; | |
211 | } | |
212 | if (!wc->tp_dst_mask) { | |
213 | ofpfw |= OFPFW_TP_DST; | |
214 | } | |
ff9d3826 | 215 | |
66642cb4 BP |
216 | /* Translate VLANs. */ |
217 | match->dl_vlan = htons(0); | |
218 | match->dl_vlan_pcp = 0; | |
219 | if (rule->wc.vlan_tci_mask == htons(0)) { | |
220 | ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP; | |
221 | } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI) | |
222 | && !(rule->flow.vlan_tci & htons(VLAN_CFI))) { | |
223 | match->dl_vlan = htons(OFP_VLAN_NONE); | |
224 | } else { | |
225 | if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) { | |
226 | ofpfw |= OFPFW_DL_VLAN; | |
227 | } else { | |
228 | match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci)); | |
229 | } | |
230 | ||
231 | if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) { | |
232 | ofpfw |= OFPFW_DL_VLAN_PCP; | |
233 | } else { | |
234 | match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci); | |
235 | } | |
236 | } | |
237 | ||
238 | /* Compose most of the match structure. */ | |
d8ae4d67 | 239 | match->wildcards = htonl(ofpfw); |
abe529af | 240 | match->in_port = htons(rule->flow.in_port); |
d8ae4d67 BP |
241 | memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN); |
242 | memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN); | |
36956a7d | 243 | match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type); |
d8ae4d67 BP |
244 | match->nw_src = rule->flow.nw_src; |
245 | match->nw_dst = rule->flow.nw_dst; | |
eadef313 | 246 | match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK; |
d8ae4d67 BP |
247 | match->nw_proto = rule->flow.nw_proto; |
248 | match->tp_src = rule->flow.tp_src; | |
249 | match->tp_dst = rule->flow.tp_dst; | |
250 | memset(match->pad1, '\0', sizeof match->pad1); | |
251 | memset(match->pad2, '\0', sizeof match->pad2); | |
252 | } | |
253 | ||
36956a7d BP |
254 | /* Given a 'dl_type' value in the format used in struct flow, returns the |
255 | * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */ | |
256 | ovs_be16 | |
257 | ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type) | |
258 | { | |
259 | return (flow_dl_type == htons(FLOW_DL_TYPE_NONE) | |
260 | ? htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
261 | : flow_dl_type); | |
262 | } | |
263 | ||
264 | /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match | |
265 | * structure, returns the corresponding 'dl_type' value for use in struct | |
266 | * flow. */ | |
267 | ovs_be16 | |
268 | ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type) | |
269 | { | |
270 | return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
271 | ? htons(FLOW_DL_TYPE_NONE) | |
272 | : ofp_dl_type); | |
273 | } | |
274 | ||
72fae175 | 275 | /* Returns a transaction ID to use for an outgoing OpenFlow message. */ |
44381c1b | 276 | static ovs_be32 |
fa37b408 BP |
277 | alloc_xid(void) |
278 | { | |
72fae175 | 279 | static uint32_t next_xid = 1; |
44381c1b | 280 | return htonl(next_xid++); |
fa37b408 | 281 | } |
d1e2cf21 BP |
282 | \f |
283 | /* Basic parsing of OpenFlow messages. */ | |
fa37b408 | 284 | |
d1e2cf21 BP |
285 | struct ofputil_msg_type { |
286 | enum ofputil_msg_code code; /* OFPUTIL_*. */ | |
a1893da1 | 287 | uint8_t ofp_version; /* An OpenFlow version or 0 for "any". */ |
d1e2cf21 BP |
288 | uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */ |
289 | const char *name; /* e.g. "OFPT_FLOW_REMOVED". */ | |
290 | unsigned int min_size; /* Minimum total message size in bytes. */ | |
291 | /* 0 if 'min_size' is the exact size that the message must be. Otherwise, | |
292 | * the message may exceed 'min_size' by an even multiple of this value. */ | |
293 | unsigned int extra_multiple; | |
294 | }; | |
295 | ||
5a020ef3 BP |
296 | /* Represents a malformed OpenFlow message. */ |
297 | static const struct ofputil_msg_type ofputil_invalid_type = { | |
a1893da1 | 298 | OFPUTIL_MSG_INVALID, 0, 0, "OFPUTIL_MSG_INVALID", 0, 0 |
5a020ef3 BP |
299 | }; |
300 | ||
d1e2cf21 BP |
301 | struct ofputil_msg_category { |
302 | const char *name; /* e.g. "OpenFlow message" */ | |
303 | const struct ofputil_msg_type *types; | |
304 | size_t n_types; | |
90bf1e07 | 305 | enum ofperr missing_error; /* Error value for missing type. */ |
d1e2cf21 BP |
306 | }; |
307 | ||
90bf1e07 | 308 | static enum ofperr |
5a020ef3 | 309 | ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size) |
d1e2cf21 BP |
310 | { |
311 | switch (type->extra_multiple) { | |
312 | case 0: | |
313 | if (size != type->min_size) { | |
5a020ef3 | 314 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect " |
d1e2cf21 | 315 | "length %u (expected length %u)", |
5a020ef3 | 316 | type->name, size, type->min_size); |
90bf1e07 | 317 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 | 318 | } |
5a020ef3 | 319 | return 0; |
d1e2cf21 BP |
320 | |
321 | case 1: | |
322 | if (size < type->min_size) { | |
5a020ef3 | 323 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect " |
d1e2cf21 | 324 | "length %u (expected length at least %u bytes)", |
5a020ef3 | 325 | type->name, size, type->min_size); |
90bf1e07 | 326 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 | 327 | } |
5a020ef3 | 328 | return 0; |
d1e2cf21 BP |
329 | |
330 | default: | |
331 | if (size < type->min_size | |
332 | || (size - type->min_size) % type->extra_multiple) { | |
5a020ef3 | 333 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect " |
d1e2cf21 BP |
334 | "length %u (must be exactly %u bytes or longer " |
335 | "by an integer multiple of %u bytes)", | |
5a020ef3 | 336 | type->name, size, |
d1e2cf21 | 337 | type->min_size, type->extra_multiple); |
90bf1e07 | 338 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 | 339 | } |
5a020ef3 | 340 | return 0; |
d1e2cf21 BP |
341 | } |
342 | } | |
343 | ||
90bf1e07 | 344 | static enum ofperr |
d1e2cf21 | 345 | ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat, |
a1893da1 | 346 | uint8_t version, uint32_t value, |
d1e2cf21 BP |
347 | const struct ofputil_msg_type **typep) |
348 | { | |
349 | const struct ofputil_msg_type *type; | |
350 | ||
351 | for (type = cat->types; type < &cat->types[cat->n_types]; type++) { | |
a1893da1 BP |
352 | if (type->value == value |
353 | && (!type->ofp_version || version == type->ofp_version)) { | |
d1e2cf21 BP |
354 | *typep = type; |
355 | return 0; | |
356 | } | |
357 | } | |
358 | ||
5c47debb | 359 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32, |
d1e2cf21 BP |
360 | cat->name, value); |
361 | return cat->missing_error; | |
362 | } | |
363 | ||
90bf1e07 | 364 | static enum ofperr |
5a020ef3 | 365 | ofputil_decode_vendor(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
366 | const struct ofputil_msg_type **typep) |
367 | { | |
368 | static const struct ofputil_msg_type nxt_messages[] = { | |
a1893da1 | 369 | { OFPUTIL_NXT_ROLE_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
370 | NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST", |
371 | sizeof(struct nx_role_request), 0 }, | |
372 | ||
a1893da1 | 373 | { OFPUTIL_NXT_ROLE_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
374 | NXT_ROLE_REPLY, "NXT_ROLE_REPLY", |
375 | sizeof(struct nx_role_request), 0 }, | |
376 | ||
a1893da1 | 377 | { OFPUTIL_NXT_SET_FLOW_FORMAT, OFP10_VERSION, |
d1e2cf21 | 378 | NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT", |
73dbf4ab | 379 | sizeof(struct nx_set_flow_format), 0 }, |
d1e2cf21 | 380 | |
a1893da1 | 381 | { OFPUTIL_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION, |
54834960 | 382 | NXT_SET_PACKET_IN_FORMAT, "NXT_SET_PACKET_IN_FORMAT", |
73dbf4ab | 383 | sizeof(struct nx_set_packet_in_format), 0 }, |
54834960 | 384 | |
a1893da1 | 385 | { OFPUTIL_NXT_PACKET_IN, OFP10_VERSION, |
54834960 | 386 | NXT_PACKET_IN, "NXT_PACKET_IN", |
73dbf4ab | 387 | sizeof(struct nx_packet_in), 1 }, |
54834960 | 388 | |
a1893da1 | 389 | { OFPUTIL_NXT_FLOW_MOD, OFP10_VERSION, |
d1e2cf21 BP |
390 | NXT_FLOW_MOD, "NXT_FLOW_MOD", |
391 | sizeof(struct nx_flow_mod), 8 }, | |
392 | ||
a1893da1 | 393 | { OFPUTIL_NXT_FLOW_REMOVED, OFP10_VERSION, |
d1e2cf21 BP |
394 | NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED", |
395 | sizeof(struct nx_flow_removed), 8 }, | |
d1e9b9bf | 396 | |
a1893da1 | 397 | { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, |
d1e9b9bf | 398 | NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID", |
73dbf4ab | 399 | sizeof(struct nx_flow_mod_table_id), 0 }, |
f27f2134 BP |
400 | |
401 | { OFPUTIL_NXT_FLOW_AGE, OFP10_VERSION, | |
402 | NXT_FLOW_AGE, "NXT_FLOW_AGE", | |
403 | sizeof(struct nicira_header), 0 }, | |
80d5aefd BP |
404 | |
405 | { OFPUTIL_NXT_SET_ASYNC_CONFIG, OFP10_VERSION, | |
406 | NXT_SET_ASYNC_CONFIG, "NXT_SET_ASYNC_CONFIG", | |
407 | sizeof(struct nx_async_config), 0 }, | |
a7349929 BP |
408 | |
409 | { OFPUTIL_NXT_SET_CONTROLLER_ID, OFP10_VERSION, | |
410 | NXT_SET_CONTROLLER_ID, "NXT_SET_CONTROLLER_ID", | |
411 | sizeof(struct nx_controller_id), 0 }, | |
d1e2cf21 BP |
412 | }; |
413 | ||
414 | static const struct ofputil_msg_category nxt_category = { | |
415 | "Nicira extension message", | |
416 | nxt_messages, ARRAY_SIZE(nxt_messages), | |
90bf1e07 | 417 | OFPERR_OFPBRC_BAD_SUBTYPE |
d1e2cf21 BP |
418 | }; |
419 | ||
420 | const struct ofp_vendor_header *ovh; | |
421 | const struct nicira_header *nh; | |
422 | ||
5a020ef3 BP |
423 | if (length < sizeof(struct ofp_vendor_header)) { |
424 | if (length == ntohs(oh->length)) { | |
425 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message"); | |
426 | } | |
90bf1e07 | 427 | return OFPERR_OFPBRC_BAD_LEN; |
5a020ef3 BP |
428 | } |
429 | ||
d1e2cf21 BP |
430 | ovh = (const struct ofp_vendor_header *) oh; |
431 | if (ovh->vendor != htonl(NX_VENDOR_ID)) { | |
432 | VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown " | |
433 | "vendor %"PRIx32, ntohl(ovh->vendor)); | |
90bf1e07 | 434 | return OFPERR_OFPBRC_BAD_VENDOR; |
d1e2cf21 BP |
435 | } |
436 | ||
5a020ef3 BP |
437 | if (length < sizeof(struct nicira_header)) { |
438 | if (length == ntohs(oh->length)) { | |
439 | VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of " | |
440 | "length %u (expected at least %zu)", | |
441 | ntohs(ovh->header.length), | |
442 | sizeof(struct nicira_header)); | |
443 | } | |
90bf1e07 | 444 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 BP |
445 | } |
446 | ||
447 | nh = (const struct nicira_header *) oh; | |
a1893da1 BP |
448 | return ofputil_lookup_openflow_message(&nxt_category, oh->version, |
449 | ntohl(nh->subtype), typep); | |
d1e2cf21 BP |
450 | } |
451 | ||
90bf1e07 | 452 | static enum ofperr |
5a020ef3 | 453 | check_nxstats_msg(const struct ofp_header *oh, size_t length) |
d1e2cf21 | 454 | { |
28c8bad1 | 455 | const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh; |
d1e2cf21 BP |
456 | ovs_be32 vendor; |
457 | ||
5a020ef3 BP |
458 | if (length < sizeof(struct ofp_vendor_stats_msg)) { |
459 | if (length == ntohs(oh->length)) { | |
460 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message"); | |
461 | } | |
90bf1e07 | 462 | return OFPERR_OFPBRC_BAD_LEN; |
5a020ef3 BP |
463 | } |
464 | ||
03cd3493 | 465 | memcpy(&vendor, osm + 1, sizeof vendor); |
d1e2cf21 BP |
466 | if (vendor != htonl(NX_VENDOR_ID)) { |
467 | VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for " | |
468 | "unknown vendor %"PRIx32, ntohl(vendor)); | |
90bf1e07 | 469 | return OFPERR_OFPBRC_BAD_VENDOR; |
d1e2cf21 BP |
470 | } |
471 | ||
5a020ef3 BP |
472 | if (length < sizeof(struct nicira_stats_msg)) { |
473 | if (length == ntohs(osm->header.length)) { | |
474 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message"); | |
475 | } | |
90bf1e07 | 476 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 BP |
477 | } |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
90bf1e07 | 482 | static enum ofperr |
5a020ef3 | 483 | ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
484 | const struct ofputil_msg_type **typep) |
485 | { | |
486 | static const struct ofputil_msg_type nxst_requests[] = { | |
a1893da1 | 487 | { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
488 | NXST_FLOW, "NXST_FLOW request", |
489 | sizeof(struct nx_flow_stats_request), 8 }, | |
490 | ||
a1893da1 | 491 | { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
492 | NXST_AGGREGATE, "NXST_AGGREGATE request", |
493 | sizeof(struct nx_aggregate_stats_request), 8 }, | |
494 | }; | |
495 | ||
496 | static const struct ofputil_msg_category nxst_request_category = { | |
08717852 | 497 | "Nicira extension statistics request", |
d1e2cf21 | 498 | nxst_requests, ARRAY_SIZE(nxst_requests), |
90bf1e07 | 499 | OFPERR_OFPBRC_BAD_SUBTYPE |
d1e2cf21 BP |
500 | }; |
501 | ||
502 | const struct nicira_stats_msg *nsm; | |
90bf1e07 | 503 | enum ofperr error; |
d1e2cf21 | 504 | |
5a020ef3 | 505 | error = check_nxstats_msg(oh, length); |
d1e2cf21 BP |
506 | if (error) { |
507 | return error; | |
508 | } | |
509 | ||
510 | nsm = (struct nicira_stats_msg *) oh; | |
a1893da1 | 511 | return ofputil_lookup_openflow_message(&nxst_request_category, oh->version, |
5a020ef3 | 512 | ntohl(nsm->subtype), typep); |
d1e2cf21 BP |
513 | } |
514 | ||
90bf1e07 | 515 | static enum ofperr |
5a020ef3 | 516 | ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
517 | const struct ofputil_msg_type **typep) |
518 | { | |
519 | static const struct ofputil_msg_type nxst_replies[] = { | |
a1893da1 | 520 | { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
521 | NXST_FLOW, "NXST_FLOW reply", |
522 | sizeof(struct nicira_stats_msg), 8 }, | |
523 | ||
a1893da1 | 524 | { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
525 | NXST_AGGREGATE, "NXST_AGGREGATE reply", |
526 | sizeof(struct nx_aggregate_stats_reply), 0 }, | |
527 | }; | |
528 | ||
529 | static const struct ofputil_msg_category nxst_reply_category = { | |
08717852 | 530 | "Nicira extension statistics reply", |
d1e2cf21 | 531 | nxst_replies, ARRAY_SIZE(nxst_replies), |
90bf1e07 | 532 | OFPERR_OFPBRC_BAD_SUBTYPE |
d1e2cf21 BP |
533 | }; |
534 | ||
535 | const struct nicira_stats_msg *nsm; | |
90bf1e07 | 536 | enum ofperr error; |
d1e2cf21 | 537 | |
5a020ef3 | 538 | error = check_nxstats_msg(oh, length); |
d1e2cf21 BP |
539 | if (error) { |
540 | return error; | |
541 | } | |
542 | ||
543 | nsm = (struct nicira_stats_msg *) oh; | |
a1893da1 | 544 | return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version, |
5a020ef3 BP |
545 | ntohl(nsm->subtype), typep); |
546 | } | |
547 | ||
90bf1e07 | 548 | static enum ofperr |
5a020ef3 BP |
549 | check_stats_msg(const struct ofp_header *oh, size_t length) |
550 | { | |
551 | if (length < sizeof(struct ofp_stats_msg)) { | |
552 | if (length == ntohs(oh->length)) { | |
553 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message"); | |
554 | } | |
90bf1e07 | 555 | return OFPERR_OFPBRC_BAD_LEN; |
5a020ef3 BP |
556 | } |
557 | ||
558 | return 0; | |
d1e2cf21 BP |
559 | } |
560 | ||
90bf1e07 | 561 | static enum ofperr |
5a020ef3 | 562 | ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
563 | const struct ofputil_msg_type **typep) |
564 | { | |
d1e2cf21 | 565 | static const struct ofputil_msg_type ofpst_requests[] = { |
a1893da1 | 566 | { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION, |
d1e2cf21 | 567 | OFPST_DESC, "OFPST_DESC request", |
63f2140a | 568 | sizeof(struct ofp_stats_msg), 0 }, |
d1e2cf21 | 569 | |
a1893da1 | 570 | { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION, |
d1e2cf21 | 571 | OFPST_FLOW, "OFPST_FLOW request", |
63f2140a | 572 | sizeof(struct ofp_flow_stats_request), 0 }, |
d1e2cf21 | 573 | |
a1893da1 | 574 | { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION, |
d1e2cf21 | 575 | OFPST_AGGREGATE, "OFPST_AGGREGATE request", |
63f2140a | 576 | sizeof(struct ofp_flow_stats_request), 0 }, |
d1e2cf21 | 577 | |
a1893da1 | 578 | { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION, |
d1e2cf21 | 579 | OFPST_TABLE, "OFPST_TABLE request", |
63f2140a | 580 | sizeof(struct ofp_stats_msg), 0 }, |
d1e2cf21 | 581 | |
a1893da1 | 582 | { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION, |
d1e2cf21 | 583 | OFPST_PORT, "OFPST_PORT request", |
63f2140a | 584 | sizeof(struct ofp_port_stats_request), 0 }, |
d1e2cf21 | 585 | |
a1893da1 | 586 | { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION, |
d1e2cf21 | 587 | OFPST_QUEUE, "OFPST_QUEUE request", |
63f2140a | 588 | sizeof(struct ofp_queue_stats_request), 0 }, |
d1e2cf21 | 589 | |
2be393ed JP |
590 | { OFPUTIL_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, |
591 | OFPST_PORT_DESC, "OFPST_PORT_DESC request", | |
592 | sizeof(struct ofp_stats_msg), 0 }, | |
593 | ||
a1893da1 | 594 | { 0, 0, |
d1e2cf21 | 595 | OFPST_VENDOR, "OFPST_VENDOR request", |
63f2140a | 596 | sizeof(struct ofp_vendor_stats_msg), 1 }, |
d1e2cf21 BP |
597 | }; |
598 | ||
599 | static const struct ofputil_msg_category ofpst_request_category = { | |
600 | "OpenFlow statistics", | |
601 | ofpst_requests, ARRAY_SIZE(ofpst_requests), | |
90bf1e07 | 602 | OFPERR_OFPBRC_BAD_STAT |
d1e2cf21 BP |
603 | }; |
604 | ||
28c8bad1 | 605 | const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh; |
90bf1e07 | 606 | enum ofperr error; |
d1e2cf21 | 607 | |
5a020ef3 BP |
608 | error = check_stats_msg(oh, length); |
609 | if (error) { | |
610 | return error; | |
611 | } | |
612 | ||
d1e2cf21 | 613 | error = ofputil_lookup_openflow_message(&ofpst_request_category, |
a1893da1 BP |
614 | oh->version, ntohs(request->type), |
615 | typep); | |
28c8bad1 | 616 | if (!error && request->type == htons(OFPST_VENDOR)) { |
5a020ef3 | 617 | error = ofputil_decode_nxst_request(oh, length, typep); |
d1e2cf21 BP |
618 | } |
619 | return error; | |
620 | } | |
621 | ||
90bf1e07 | 622 | static enum ofperr |
5a020ef3 | 623 | ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
624 | const struct ofputil_msg_type **typep) |
625 | { | |
d1e2cf21 | 626 | static const struct ofputil_msg_type ofpst_replies[] = { |
a1893da1 | 627 | { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION, |
d1e2cf21 | 628 | OFPST_DESC, "OFPST_DESC reply", |
63f2140a | 629 | sizeof(struct ofp_desc_stats), 0 }, |
d1e2cf21 | 630 | |
a1893da1 | 631 | { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION, |
d1e2cf21 | 632 | OFPST_FLOW, "OFPST_FLOW reply", |
63f2140a | 633 | sizeof(struct ofp_stats_msg), 1 }, |
d1e2cf21 | 634 | |
a1893da1 | 635 | { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION, |
d1e2cf21 | 636 | OFPST_AGGREGATE, "OFPST_AGGREGATE reply", |
63f2140a | 637 | sizeof(struct ofp_aggregate_stats_reply), 0 }, |
d1e2cf21 | 638 | |
a1893da1 | 639 | { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION, |
d1e2cf21 | 640 | OFPST_TABLE, "OFPST_TABLE reply", |
63f2140a | 641 | sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) }, |
d1e2cf21 | 642 | |
a1893da1 | 643 | { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION, |
d1e2cf21 | 644 | OFPST_PORT, "OFPST_PORT reply", |
63f2140a | 645 | sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) }, |
d1e2cf21 | 646 | |
a1893da1 | 647 | { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION, |
d1e2cf21 | 648 | OFPST_QUEUE, "OFPST_QUEUE reply", |
63f2140a | 649 | sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) }, |
d1e2cf21 | 650 | |
2be393ed JP |
651 | { OFPUTIL_OFPST_PORT_DESC_REPLY, OFP10_VERSION, |
652 | OFPST_PORT_DESC, "OFPST_PORT_DESC reply", | |
653 | sizeof(struct ofp_stats_msg), sizeof(struct ofp10_phy_port) }, | |
654 | ||
a1893da1 | 655 | { 0, 0, |
d1e2cf21 | 656 | OFPST_VENDOR, "OFPST_VENDOR reply", |
63f2140a | 657 | sizeof(struct ofp_vendor_stats_msg), 1 }, |
d1e2cf21 BP |
658 | }; |
659 | ||
660 | static const struct ofputil_msg_category ofpst_reply_category = { | |
661 | "OpenFlow statistics", | |
662 | ofpst_replies, ARRAY_SIZE(ofpst_replies), | |
90bf1e07 | 663 | OFPERR_OFPBRC_BAD_STAT |
d1e2cf21 BP |
664 | }; |
665 | ||
28c8bad1 | 666 | const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh; |
90bf1e07 | 667 | enum ofperr error; |
d1e2cf21 | 668 | |
5a020ef3 BP |
669 | error = check_stats_msg(oh, length); |
670 | if (error) { | |
671 | return error; | |
672 | } | |
673 | ||
a1893da1 | 674 | error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version, |
5a020ef3 | 675 | ntohs(reply->type), typep); |
28c8bad1 | 676 | if (!error && reply->type == htons(OFPST_VENDOR)) { |
5a020ef3 | 677 | error = ofputil_decode_nxst_reply(oh, length, typep); |
d1e2cf21 BP |
678 | } |
679 | return error; | |
680 | } | |
681 | ||
90bf1e07 | 682 | static enum ofperr |
5a020ef3 BP |
683 | ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length, |
684 | const struct ofputil_msg_type **typep) | |
d1e2cf21 BP |
685 | { |
686 | static const struct ofputil_msg_type ofpt_messages[] = { | |
a1893da1 | 687 | { OFPUTIL_OFPT_HELLO, OFP10_VERSION, |
d1e2cf21 BP |
688 | OFPT_HELLO, "OFPT_HELLO", |
689 | sizeof(struct ofp_hello), 1 }, | |
690 | ||
90bf1e07 | 691 | { OFPUTIL_OFPT_ERROR, 0, |
d1e2cf21 BP |
692 | OFPT_ERROR, "OFPT_ERROR", |
693 | sizeof(struct ofp_error_msg), 1 }, | |
694 | ||
a1893da1 | 695 | { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
696 | OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST", |
697 | sizeof(struct ofp_header), 1 }, | |
698 | ||
a1893da1 | 699 | { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
700 | OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY", |
701 | sizeof(struct ofp_header), 1 }, | |
702 | ||
a1893da1 | 703 | { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
704 | OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST", |
705 | sizeof(struct ofp_header), 0 }, | |
706 | ||
a1893da1 | 707 | { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION, |
d1e2cf21 | 708 | OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY", |
9e1fd49b BP |
709 | sizeof(struct ofp_switch_features), sizeof(struct ofp10_phy_port) }, |
710 | { OFPUTIL_OFPT_FEATURES_REPLY, OFP11_VERSION, | |
711 | OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY", | |
712 | sizeof(struct ofp_switch_features), sizeof(struct ofp11_port) }, | |
d1e2cf21 | 713 | |
a1893da1 | 714 | { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
715 | OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST", |
716 | sizeof(struct ofp_header), 0 }, | |
717 | ||
a1893da1 | 718 | { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
719 | OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY", |
720 | sizeof(struct ofp_switch_config), 0 }, | |
721 | ||
a1893da1 | 722 | { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION, |
d1e2cf21 BP |
723 | OFPT_SET_CONFIG, "OFPT_SET_CONFIG", |
724 | sizeof(struct ofp_switch_config), 0 }, | |
725 | ||
a1893da1 | 726 | { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION, |
d1e2cf21 BP |
727 | OFPT_PACKET_IN, "OFPT_PACKET_IN", |
728 | offsetof(struct ofp_packet_in, data), 1 }, | |
729 | ||
a1893da1 | 730 | { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION, |
d1e2cf21 BP |
731 | OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED", |
732 | sizeof(struct ofp_flow_removed), 0 }, | |
733 | ||
a1893da1 | 734 | { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION, |
d1e2cf21 | 735 | OFPT_PORT_STATUS, "OFPT_PORT_STATUS", |
9e1fd49b BP |
736 | sizeof(struct ofp_port_status) + sizeof(struct ofp10_phy_port), 0 }, |
737 | { OFPUTIL_OFPT_PORT_STATUS, OFP11_VERSION, | |
738 | OFPT_PORT_STATUS, "OFPT_PORT_STATUS", | |
739 | sizeof(struct ofp_port_status) + sizeof(struct ofp11_port), 0 }, | |
d1e2cf21 | 740 | |
a1893da1 | 741 | { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION, |
5293a2e1 | 742 | OFPT10_PACKET_OUT, "OFPT_PACKET_OUT", |
d1e2cf21 BP |
743 | sizeof(struct ofp_packet_out), 1 }, |
744 | ||
a1893da1 | 745 | { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION, |
5293a2e1 | 746 | OFPT10_FLOW_MOD, "OFPT_FLOW_MOD", |
d1e2cf21 BP |
747 | sizeof(struct ofp_flow_mod), 1 }, |
748 | ||
a1893da1 | 749 | { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION, |
5293a2e1 | 750 | OFPT10_PORT_MOD, "OFPT_PORT_MOD", |
9e1fd49b BP |
751 | sizeof(struct ofp10_port_mod), 0 }, |
752 | { OFPUTIL_OFPT_PORT_MOD, OFP11_VERSION, | |
753 | OFPT11_PORT_MOD, "OFPT_PORT_MOD", | |
754 | sizeof(struct ofp11_port_mod), 0 }, | |
d1e2cf21 | 755 | |
a1893da1 | 756 | { 0, OFP10_VERSION, |
5293a2e1 | 757 | OFPT10_STATS_REQUEST, "OFPT_STATS_REQUEST", |
28c8bad1 | 758 | sizeof(struct ofp_stats_msg), 1 }, |
d1e2cf21 | 759 | |
a1893da1 | 760 | { 0, OFP10_VERSION, |
5293a2e1 | 761 | OFPT10_STATS_REPLY, "OFPT_STATS_REPLY", |
28c8bad1 | 762 | sizeof(struct ofp_stats_msg), 1 }, |
d1e2cf21 | 763 | |
a1893da1 | 764 | { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION, |
5293a2e1 | 765 | OFPT10_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST", |
d1e2cf21 BP |
766 | sizeof(struct ofp_header), 0 }, |
767 | ||
a1893da1 | 768 | { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION, |
5293a2e1 | 769 | OFPT10_BARRIER_REPLY, "OFPT_BARRIER_REPLY", |
d1e2cf21 BP |
770 | sizeof(struct ofp_header), 0 }, |
771 | ||
a1893da1 | 772 | { 0, 0, |
d1e2cf21 BP |
773 | OFPT_VENDOR, "OFPT_VENDOR", |
774 | sizeof(struct ofp_vendor_header), 1 }, | |
775 | }; | |
776 | ||
777 | static const struct ofputil_msg_category ofpt_category = { | |
778 | "OpenFlow message", | |
779 | ofpt_messages, ARRAY_SIZE(ofpt_messages), | |
90bf1e07 | 780 | OFPERR_OFPBRC_BAD_TYPE |
d1e2cf21 BP |
781 | }; |
782 | ||
90bf1e07 | 783 | enum ofperr error; |
d1e2cf21 | 784 | |
a1893da1 BP |
785 | error = ofputil_lookup_openflow_message(&ofpt_category, oh->version, |
786 | oh->type, typep); | |
d1e2cf21 | 787 | if (!error) { |
7887679b BP |
788 | switch ((oh->version << 8) | oh->type) { |
789 | case (OFP10_VERSION << 8) | OFPT_VENDOR: | |
790 | case (OFP11_VERSION << 8) | OFPT_VENDOR: | |
5a020ef3 | 791 | error = ofputil_decode_vendor(oh, length, typep); |
d1e2cf21 BP |
792 | break; |
793 | ||
7887679b BP |
794 | case (OFP10_VERSION << 8) | OFPT10_STATS_REQUEST: |
795 | case (OFP11_VERSION << 8) | OFPT11_STATS_REQUEST: | |
5a020ef3 | 796 | error = ofputil_decode_ofpst_request(oh, length, typep); |
d1e2cf21 BP |
797 | break; |
798 | ||
7887679b BP |
799 | case (OFP10_VERSION << 8) | OFPT10_STATS_REPLY: |
800 | case (OFP11_VERSION << 8) | OFPT11_STATS_REPLY: | |
5a020ef3 | 801 | error = ofputil_decode_ofpst_reply(oh, length, typep); |
d1e2cf21 BP |
802 | |
803 | default: | |
804 | break; | |
805 | } | |
806 | } | |
5a020ef3 BP |
807 | return error; |
808 | } | |
809 | ||
90bf1e07 BP |
810 | /* Decodes the message type represented by 'oh'. Returns 0 if successful or an |
811 | * OpenFlow error code on failure. Either way, stores in '*typep' a type | |
812 | * structure that can be inspected with the ofputil_msg_type_*() functions. | |
5a020ef3 BP |
813 | * |
814 | * oh->length must indicate the correct length of the message (and must be at | |
815 | * least sizeof(struct ofp_header)). | |
816 | * | |
817 | * Success indicates that 'oh' is at least as long as the minimum-length | |
818 | * message of its type. */ | |
90bf1e07 | 819 | enum ofperr |
5a020ef3 BP |
820 | ofputil_decode_msg_type(const struct ofp_header *oh, |
821 | const struct ofputil_msg_type **typep) | |
822 | { | |
823 | size_t length = ntohs(oh->length); | |
90bf1e07 | 824 | enum ofperr error; |
5a020ef3 BP |
825 | |
826 | error = ofputil_decode_msg_type__(oh, length, typep); | |
827 | if (!error) { | |
828 | error = ofputil_check_length(*typep, length); | |
829 | } | |
d1e2cf21 | 830 | if (error) { |
5a020ef3 BP |
831 | *typep = &ofputil_invalid_type; |
832 | } | |
833 | return error; | |
834 | } | |
d1e2cf21 | 835 | |
5a020ef3 BP |
836 | /* Decodes the message type represented by 'oh', of which only the first |
837 | * 'length' bytes are available. Returns 0 if successful or an OpenFlow error | |
90bf1e07 BP |
838 | * code on failure. Either way, stores in '*typep' a type structure that can |
839 | * be inspected with the ofputil_msg_type_*() functions. */ | |
840 | enum ofperr | |
5a020ef3 BP |
841 | ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length, |
842 | const struct ofputil_msg_type **typep) | |
843 | { | |
90bf1e07 | 844 | enum ofperr error; |
5a020ef3 BP |
845 | |
846 | error = (length >= sizeof *oh | |
847 | ? ofputil_decode_msg_type__(oh, length, typep) | |
90bf1e07 | 848 | : OFPERR_OFPBRC_BAD_LEN); |
5a020ef3 | 849 | if (error) { |
d1e2cf21 BP |
850 | *typep = &ofputil_invalid_type; |
851 | } | |
852 | return error; | |
853 | } | |
854 | ||
855 | /* Returns an OFPUTIL_* message type code for 'type'. */ | |
856 | enum ofputil_msg_code | |
857 | ofputil_msg_type_code(const struct ofputil_msg_type *type) | |
858 | { | |
859 | return type->code; | |
860 | } | |
2e4f5fcf | 861 | \f |
27527aa0 | 862 | /* Protocols. */ |
7fa91113 | 863 | |
27527aa0 BP |
864 | struct proto_abbrev { |
865 | enum ofputil_protocol protocol; | |
866 | const char *name; | |
867 | }; | |
868 | ||
869 | /* Most users really don't care about some of the differences between | |
870 | * protocols. These abbreviations help with that. */ | |
871 | static const struct proto_abbrev proto_abbrevs[] = { | |
872 | { OFPUTIL_P_ANY, "any" }, | |
873 | { OFPUTIL_P_OF10_ANY, "OpenFlow10" }, | |
874 | { OFPUTIL_P_NXM_ANY, "NXM" }, | |
875 | }; | |
876 | #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs) | |
877 | ||
878 | enum ofputil_protocol ofputil_flow_dump_protocols[] = { | |
879 | OFPUTIL_P_NXM, | |
880 | OFPUTIL_P_OF10, | |
881 | }; | |
882 | size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols); | |
883 | ||
884 | /* Returns the ofputil_protocol that is initially in effect on an OpenFlow | |
885 | * connection that has negotiated the given 'version'. 'version' should | |
886 | * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow | |
887 | * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or | |
888 | * outside the valid range. */ | |
889 | enum ofputil_protocol | |
890 | ofputil_protocol_from_ofp_version(int version) | |
891 | { | |
892 | switch (version) { | |
87ea5e5e | 893 | case OFP10_VERSION: return OFPUTIL_P_OF10; |
27527aa0 BP |
894 | default: return 0; |
895 | } | |
896 | } | |
897 | ||
9e1fd49b BP |
898 | /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION or |
899 | * OFP11_VERSION) that corresponds to 'protocol'. */ | |
900 | uint8_t | |
901 | ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol) | |
902 | { | |
903 | switch (protocol) { | |
904 | case OFPUTIL_P_OF10: | |
905 | case OFPUTIL_P_OF10_TID: | |
906 | case OFPUTIL_P_NXM: | |
907 | case OFPUTIL_P_NXM_TID: | |
908 | return OFP10_VERSION; | |
909 | } | |
910 | ||
911 | NOT_REACHED(); | |
912 | } | |
913 | ||
27527aa0 BP |
914 | /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false |
915 | * otherwise. */ | |
7fa91113 | 916 | bool |
27527aa0 | 917 | ofputil_protocol_is_valid(enum ofputil_protocol protocol) |
7fa91113 | 918 | { |
27527aa0 BP |
919 | return protocol & OFPUTIL_P_ANY && is_pow2(protocol); |
920 | } | |
921 | ||
922 | /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id | |
923 | * extension turned on or off if 'enable' is true or false, respectively. | |
924 | * | |
925 | * This extension is only useful for protocols whose "standard" version does | |
926 | * not allow specific tables to be modified. In particular, this is true of | |
927 | * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always | |
928 | * specifies a table ID and so there is no need for such an extension. When | |
929 | * 'protocol' is such a protocol that doesn't need a flow_mod_table_id | |
930 | * extension, this function just returns its 'protocol' argument unchanged | |
931 | * regardless of the value of 'enable'. */ | |
932 | enum ofputil_protocol | |
933 | ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable) | |
934 | { | |
935 | switch (protocol) { | |
936 | case OFPUTIL_P_OF10: | |
937 | case OFPUTIL_P_OF10_TID: | |
938 | return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10; | |
939 | ||
940 | case OFPUTIL_P_NXM: | |
941 | case OFPUTIL_P_NXM_TID: | |
942 | return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM; | |
943 | ||
944 | default: | |
945 | NOT_REACHED(); | |
7fa91113 | 946 | } |
27527aa0 | 947 | } |
7fa91113 | 948 | |
27527aa0 BP |
949 | /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes |
950 | * some extension to a standard protocol version, the return value is the | |
951 | * standard version of that protocol without any extension. If 'protocol' is a | |
952 | * standard protocol version, returns 'protocol' unchanged. */ | |
953 | enum ofputil_protocol | |
954 | ofputil_protocol_to_base(enum ofputil_protocol protocol) | |
955 | { | |
956 | return ofputil_protocol_set_tid(protocol, false); | |
7fa91113 BP |
957 | } |
958 | ||
27527aa0 BP |
959 | /* Returns 'new_base' with any extensions taken from 'cur'. */ |
960 | enum ofputil_protocol | |
961 | ofputil_protocol_set_base(enum ofputil_protocol cur, | |
962 | enum ofputil_protocol new_base) | |
7fa91113 | 963 | { |
27527aa0 BP |
964 | bool tid = (cur & OFPUTIL_P_TID) != 0; |
965 | ||
966 | switch (new_base) { | |
967 | case OFPUTIL_P_OF10: | |
968 | case OFPUTIL_P_OF10_TID: | |
969 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid); | |
970 | ||
971 | case OFPUTIL_P_NXM: | |
972 | case OFPUTIL_P_NXM_TID: | |
973 | return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid); | |
974 | ||
7fa91113 BP |
975 | default: |
976 | NOT_REACHED(); | |
977 | } | |
978 | } | |
979 | ||
27527aa0 BP |
980 | /* Returns a string form of 'protocol', if a simple form exists (that is, if |
981 | * 'protocol' is either a single protocol or it is a combination of protocols | |
982 | * that have a single abbreviation). Otherwise, returns NULL. */ | |
983 | const char * | |
984 | ofputil_protocol_to_string(enum ofputil_protocol protocol) | |
88ca35ee | 985 | { |
27527aa0 BP |
986 | const struct proto_abbrev *p; |
987 | ||
988 | /* Use a "switch" statement for single-bit names so that we get a compiler | |
989 | * warning if we forget any. */ | |
990 | switch (protocol) { | |
991 | case OFPUTIL_P_NXM: | |
992 | return "NXM-table_id"; | |
993 | ||
994 | case OFPUTIL_P_NXM_TID: | |
995 | return "NXM+table_id"; | |
996 | ||
997 | case OFPUTIL_P_OF10: | |
998 | return "OpenFlow10-table_id"; | |
999 | ||
1000 | case OFPUTIL_P_OF10_TID: | |
1001 | return "OpenFlow10+table_id"; | |
1002 | } | |
1003 | ||
1004 | /* Check abbreviations. */ | |
1005 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1006 | if (protocol == p->protocol) { | |
1007 | return p->name; | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | return NULL; | |
1012 | } | |
1013 | ||
1014 | /* Returns a string that represents 'protocols'. The return value might be a | |
1015 | * comma-separated list if 'protocols' doesn't have a simple name. The return | |
1016 | * value is "none" if 'protocols' is 0. | |
1017 | * | |
1018 | * The caller must free the returned string (with free()). */ | |
1019 | char * | |
1020 | ofputil_protocols_to_string(enum ofputil_protocol protocols) | |
1021 | { | |
1022 | struct ds s; | |
1023 | ||
1024 | assert(!(protocols & ~OFPUTIL_P_ANY)); | |
1025 | if (protocols == 0) { | |
1026 | return xstrdup("none"); | |
1027 | } | |
1028 | ||
1029 | ds_init(&s); | |
1030 | while (protocols) { | |
1031 | const struct proto_abbrev *p; | |
1032 | int i; | |
1033 | ||
1034 | if (s.length) { | |
1035 | ds_put_char(&s, ','); | |
1036 | } | |
1037 | ||
1038 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1039 | if ((protocols & p->protocol) == p->protocol) { | |
1040 | ds_put_cstr(&s, p->name); | |
1041 | protocols &= ~p->protocol; | |
1042 | goto match; | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) { | |
1047 | enum ofputil_protocol bit = 1u << i; | |
1048 | ||
1049 | if (protocols & bit) { | |
1050 | ds_put_cstr(&s, ofputil_protocol_to_string(bit)); | |
1051 | protocols &= ~bit; | |
1052 | goto match; | |
1053 | } | |
1054 | } | |
1055 | NOT_REACHED(); | |
1056 | ||
1057 | match: ; | |
1058 | } | |
1059 | return ds_steal_cstr(&s); | |
1060 | } | |
1061 | ||
1062 | static enum ofputil_protocol | |
1063 | ofputil_protocol_from_string__(const char *s, size_t n) | |
1064 | { | |
1065 | const struct proto_abbrev *p; | |
1066 | int i; | |
1067 | ||
1068 | for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) { | |
1069 | enum ofputil_protocol bit = 1u << i; | |
1070 | const char *name = ofputil_protocol_to_string(bit); | |
1071 | ||
1072 | if (name && n == strlen(name) && !strncasecmp(s, name, n)) { | |
1073 | return bit; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) { | |
1078 | if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) { | |
1079 | return p->protocol; | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | return 0; | |
1084 | } | |
1085 | ||
1086 | /* Returns the nonempty set of protocols represented by 's', which can be a | |
1087 | * single protocol name or abbreviation or a comma-separated list of them. | |
1088 | * | |
1089 | * Aborts the program with an error message if 's' is invalid. */ | |
1090 | enum ofputil_protocol | |
1091 | ofputil_protocols_from_string(const char *s) | |
1092 | { | |
1093 | const char *orig_s = s; | |
1094 | enum ofputil_protocol protocols; | |
1095 | ||
1096 | protocols = 0; | |
1097 | while (*s) { | |
1098 | enum ofputil_protocol p; | |
1099 | size_t n; | |
1100 | ||
1101 | n = strcspn(s, ","); | |
1102 | if (n == 0) { | |
1103 | s++; | |
1104 | continue; | |
1105 | } | |
1106 | ||
1107 | p = ofputil_protocol_from_string__(s, n); | |
1108 | if (!p) { | |
1109 | ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s); | |
1110 | } | |
1111 | protocols |= p; | |
1112 | ||
1113 | s += n; | |
1114 | } | |
1115 | ||
1116 | if (!protocols) { | |
1117 | ovs_fatal(0, "%s: no flow protocol specified", orig_s); | |
1118 | } | |
1119 | return protocols; | |
88ca35ee BP |
1120 | } |
1121 | ||
54834960 EJ |
1122 | bool |
1123 | ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format) | |
1124 | { | |
1125 | switch (packet_in_format) { | |
1126 | case NXPIF_OPENFLOW10: | |
1127 | case NXPIF_NXM: | |
1128 | return true; | |
1129 | } | |
1130 | ||
1131 | return false; | |
1132 | } | |
1133 | ||
1134 | const char * | |
1135 | ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format) | |
1136 | { | |
1137 | switch (packet_in_format) { | |
1138 | case NXPIF_OPENFLOW10: | |
1139 | return "openflow10"; | |
1140 | case NXPIF_NXM: | |
1141 | return "nxm"; | |
1142 | default: | |
1143 | NOT_REACHED(); | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | int | |
1148 | ofputil_packet_in_format_from_string(const char *s) | |
1149 | { | |
1150 | return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10 | |
1151 | : !strcmp(s, "nxm") ? NXPIF_NXM | |
1152 | : -1); | |
1153 | } | |
1154 | ||
88ca35ee BP |
1155 | static bool |
1156 | regs_fully_wildcarded(const struct flow_wildcards *wc) | |
1157 | { | |
1158 | int i; | |
1159 | ||
1160 | for (i = 0; i < FLOW_N_REGS; i++) { | |
1161 | if (wc->reg_masks[i] != 0) { | |
1162 | return false; | |
1163 | } | |
1164 | } | |
1165 | return true; | |
1166 | } | |
1167 | ||
27527aa0 BP |
1168 | /* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule' |
1169 | * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, | |
1170 | * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to | |
1171 | * use OpenFlow 1.0 protocol for backward compatibility. */ | |
1172 | enum ofputil_protocol | |
1173 | ofputil_usable_protocols(const struct cls_rule *rule) | |
8368c090 BP |
1174 | { |
1175 | const struct flow_wildcards *wc = &rule->wc; | |
8368c090 | 1176 | |
47284b1f | 1177 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 10); |
a877206f | 1178 | |
8368c090 BP |
1179 | /* Only NXM supports separately wildcards the Ethernet multicast bit. */ |
1180 | if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) { | |
27527aa0 | 1181 | return OFPUTIL_P_NXM_ANY; |
8368c090 BP |
1182 | } |
1183 | ||
bad68a99 JP |
1184 | /* Only NXM supports matching ARP hardware addresses. */ |
1185 | if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) { | |
27527aa0 | 1186 | return OFPUTIL_P_NXM_ANY; |
bad68a99 JP |
1187 | } |
1188 | ||
d31f1109 JP |
1189 | /* Only NXM supports matching IPv6 traffic. */ |
1190 | if (!(wc->wildcards & FWW_DL_TYPE) | |
1191 | && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) { | |
27527aa0 | 1192 | return OFPUTIL_P_NXM_ANY; |
d31f1109 JP |
1193 | } |
1194 | ||
8368c090 BP |
1195 | /* Only NXM supports matching registers. */ |
1196 | if (!regs_fully_wildcarded(wc)) { | |
27527aa0 | 1197 | return OFPUTIL_P_NXM_ANY; |
8368c090 BP |
1198 | } |
1199 | ||
b78f6b77 BP |
1200 | /* Only NXM supports matching tun_id. */ |
1201 | if (wc->tun_id_mask != htonll(0)) { | |
27527aa0 | 1202 | return OFPUTIL_P_NXM_ANY; |
8368c090 BP |
1203 | } |
1204 | ||
7257b535 | 1205 | /* Only NXM supports matching fragments. */ |
eadef313 | 1206 | if (wc->nw_frag_mask) { |
27527aa0 | 1207 | return OFPUTIL_P_NXM_ANY; |
7257b535 BP |
1208 | } |
1209 | ||
fa8223b7 JP |
1210 | /* Only NXM supports matching IPv6 flow label. */ |
1211 | if (!(wc->wildcards & FWW_IPV6_LABEL)) { | |
27527aa0 | 1212 | return OFPUTIL_P_NXM_ANY; |
fa8223b7 JP |
1213 | } |
1214 | ||
530180fd | 1215 | /* Only NXM supports matching IP ECN bits. */ |
2486e66a | 1216 | if (!(wc->wildcards & FWW_NW_ECN)) { |
27527aa0 | 1217 | return OFPUTIL_P_NXM_ANY; |
530180fd JP |
1218 | } |
1219 | ||
a61680c6 JP |
1220 | /* Only NXM supports matching IP TTL/hop limit. */ |
1221 | if (!(wc->wildcards & FWW_NW_TTL)) { | |
27527aa0 | 1222 | return OFPUTIL_P_NXM_ANY; |
a61680c6 JP |
1223 | } |
1224 | ||
73f33563 BP |
1225 | /* Only NXM supports bitwise matching on transport port. */ |
1226 | if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) || | |
1227 | (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) { | |
27527aa0 | 1228 | return OFPUTIL_P_NXM_ANY; |
73f33563 BP |
1229 | } |
1230 | ||
8368c090 | 1231 | /* Other formats can express this rule. */ |
27527aa0 BP |
1232 | return OFPUTIL_P_ANY; |
1233 | } | |
1234 | ||
1235 | /* Returns an OpenFlow message that, sent on an OpenFlow connection whose | |
1236 | * protocol is 'current', at least partly transitions the protocol to 'want'. | |
1237 | * Stores in '*next' the protocol that will be in effect on the OpenFlow | |
1238 | * connection if the switch processes the returned message correctly. (If | |
1239 | * '*next != want' then the caller will have to iterate.) | |
1240 | * | |
1241 | * If 'current == want', returns NULL and stores 'current' in '*next'. */ | |
1242 | struct ofpbuf * | |
1243 | ofputil_encode_set_protocol(enum ofputil_protocol current, | |
1244 | enum ofputil_protocol want, | |
1245 | enum ofputil_protocol *next) | |
1246 | { | |
1247 | enum ofputil_protocol cur_base, want_base; | |
1248 | bool cur_tid, want_tid; | |
1249 | ||
1250 | cur_base = ofputil_protocol_to_base(current); | |
1251 | want_base = ofputil_protocol_to_base(want); | |
1252 | if (cur_base != want_base) { | |
1253 | *next = ofputil_protocol_set_base(current, want_base); | |
1254 | ||
1255 | switch (want_base) { | |
1256 | case OFPUTIL_P_NXM: | |
1257 | return ofputil_encode_nx_set_flow_format(NXFF_NXM); | |
1258 | ||
1259 | case OFPUTIL_P_OF10: | |
1260 | return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10); | |
1261 | ||
1262 | case OFPUTIL_P_OF10_TID: | |
1263 | case OFPUTIL_P_NXM_TID: | |
1264 | NOT_REACHED(); | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | cur_tid = (current & OFPUTIL_P_TID) != 0; | |
1269 | want_tid = (want & OFPUTIL_P_TID) != 0; | |
1270 | if (cur_tid != want_tid) { | |
1271 | *next = ofputil_protocol_set_tid(current, want_tid); | |
1272 | return ofputil_make_flow_mod_table_id(want_tid); | |
1273 | } | |
1274 | ||
1275 | assert(current == want); | |
1276 | ||
1277 | *next = current; | |
1278 | return NULL; | |
88ca35ee BP |
1279 | } |
1280 | ||
27527aa0 BP |
1281 | /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow |
1282 | * format to 'nxff'. */ | |
88ca35ee | 1283 | struct ofpbuf * |
27527aa0 | 1284 | ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff) |
88ca35ee | 1285 | { |
73dbf4ab | 1286 | struct nx_set_flow_format *sff; |
88ca35ee BP |
1287 | struct ofpbuf *msg; |
1288 | ||
27527aa0 BP |
1289 | assert(ofputil_nx_flow_format_is_valid(nxff)); |
1290 | ||
b78f6b77 | 1291 | sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg); |
27527aa0 | 1292 | sff->format = htonl(nxff); |
88ca35ee BP |
1293 | |
1294 | return msg; | |
1295 | } | |
1296 | ||
27527aa0 BP |
1297 | /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false |
1298 | * otherwise. */ | |
1299 | enum ofputil_protocol | |
1300 | ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format) | |
1301 | { | |
1302 | switch (flow_format) { | |
1303 | case NXFF_OPENFLOW10: | |
1304 | return OFPUTIL_P_OF10; | |
1305 | ||
1306 | case NXFF_NXM: | |
1307 | return OFPUTIL_P_NXM; | |
1308 | ||
1309 | default: | |
1310 | return 0; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */ | |
1315 | bool | |
1316 | ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format) | |
1317 | { | |
1318 | return ofputil_nx_flow_format_to_protocol(flow_format) != 0; | |
1319 | } | |
1320 | ||
1321 | /* Returns a string version of 'flow_format', which must be a valid NXFF_* | |
1322 | * value. */ | |
1323 | const char * | |
1324 | ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format) | |
1325 | { | |
1326 | switch (flow_format) { | |
1327 | case NXFF_OPENFLOW10: | |
1328 | return "openflow10"; | |
1329 | case NXFF_NXM: | |
1330 | return "nxm"; | |
1331 | default: | |
1332 | NOT_REACHED(); | |
1333 | } | |
1334 | } | |
1335 | ||
54834960 EJ |
1336 | struct ofpbuf * |
1337 | ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format) | |
1338 | { | |
73dbf4ab | 1339 | struct nx_set_packet_in_format *spif; |
54834960 EJ |
1340 | struct ofpbuf *msg; |
1341 | ||
1342 | spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg); | |
1343 | spif->format = htonl(packet_in_format); | |
1344 | ||
1345 | return msg; | |
1346 | } | |
1347 | ||
6c1491fb BP |
1348 | /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id |
1349 | * extension on or off (according to 'flow_mod_table_id'). */ | |
1350 | struct ofpbuf * | |
1351 | ofputil_make_flow_mod_table_id(bool flow_mod_table_id) | |
1352 | { | |
73dbf4ab | 1353 | struct nx_flow_mod_table_id *nfmti; |
6c1491fb BP |
1354 | struct ofpbuf *msg; |
1355 | ||
1356 | nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg); | |
1357 | nfmti->set = flow_mod_table_id; | |
1358 | return msg; | |
1359 | } | |
1360 | ||
7fa91113 BP |
1361 | /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract |
1362 | * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error | |
1363 | * code. | |
1364 | * | |
2e4f5fcf | 1365 | * Does not validate the flow_mod actions. */ |
90bf1e07 | 1366 | enum ofperr |
a9a2da38 | 1367 | ofputil_decode_flow_mod(struct ofputil_flow_mod *fm, |
27527aa0 BP |
1368 | const struct ofp_header *oh, |
1369 | enum ofputil_protocol protocol) | |
2e4f5fcf BP |
1370 | { |
1371 | const struct ofputil_msg_type *type; | |
6c1491fb | 1372 | uint16_t command; |
2e4f5fcf BP |
1373 | struct ofpbuf b; |
1374 | ||
2013493b | 1375 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf BP |
1376 | |
1377 | ofputil_decode_msg_type(oh, &type); | |
1378 | if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) { | |
1379 | /* Standard OpenFlow flow_mod. */ | |
2e4f5fcf | 1380 | const struct ofp_flow_mod *ofm; |
1c0b7503 | 1381 | uint16_t priority; |
90bf1e07 | 1382 | enum ofperr error; |
2e4f5fcf BP |
1383 | |
1384 | /* Dissect the message. */ | |
bbc32a88 | 1385 | ofm = ofpbuf_pull(&b, sizeof *ofm); |
2e4f5fcf BP |
1386 | error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions); |
1387 | if (error) { | |
1388 | return error; | |
1389 | } | |
1390 | ||
1c0b7503 BP |
1391 | /* Set priority based on original wildcards. Normally we'd allow |
1392 | * ofputil_cls_rule_from_match() to do this for us, but | |
b459a924 | 1393 | * ofputil_normalize_rule() can put wildcards where the original flow |
1c0b7503 BP |
1394 | * didn't have them. */ |
1395 | priority = ntohs(ofm->priority); | |
1396 | if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) { | |
1397 | priority = UINT16_MAX; | |
1398 | } | |
1399 | ||
b459a924 BP |
1400 | /* Translate the rule. */ |
1401 | ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr); | |
27527aa0 | 1402 | ofputil_normalize_rule(&fm->cr); |
2e4f5fcf BP |
1403 | |
1404 | /* Translate the message. */ | |
2e4f5fcf | 1405 | fm->cookie = ofm->cookie; |
e729e793 | 1406 | fm->cookie_mask = htonll(UINT64_MAX); |
6c1491fb | 1407 | command = ntohs(ofm->command); |
2e4f5fcf BP |
1408 | fm->idle_timeout = ntohs(ofm->idle_timeout); |
1409 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
1410 | fm->buffer_id = ntohl(ofm->buffer_id); | |
1411 | fm->out_port = ntohs(ofm->out_port); | |
1412 | fm->flags = ntohs(ofm->flags); | |
1413 | } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) { | |
1414 | /* Nicira extended flow_mod. */ | |
1415 | const struct nx_flow_mod *nfm; | |
90bf1e07 | 1416 | enum ofperr error; |
2e4f5fcf BP |
1417 | |
1418 | /* Dissect the message. */ | |
bbc32a88 | 1419 | nfm = ofpbuf_pull(&b, sizeof *nfm); |
2e4f5fcf | 1420 | error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority), |
e729e793 | 1421 | &fm->cr, &fm->cookie, &fm->cookie_mask); |
2e4f5fcf BP |
1422 | if (error) { |
1423 | return error; | |
1424 | } | |
1425 | error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions); | |
1426 | if (error) { | |
1427 | return error; | |
1428 | } | |
1429 | ||
1430 | /* Translate the message. */ | |
6c1491fb | 1431 | command = ntohs(nfm->command); |
e729e793 JP |
1432 | if (command == OFPFC_ADD) { |
1433 | if (fm->cookie_mask) { | |
1434 | /* The "NXM_NX_COOKIE*" matches are not valid for flow | |
1435 | * additions. Additions must use the "cookie" field of | |
1436 | * the "nx_flow_mod" structure. */ | |
90bf1e07 | 1437 | return OFPERR_NXBRC_NXM_INVALID; |
e729e793 JP |
1438 | } else { |
1439 | fm->cookie = nfm->cookie; | |
1440 | fm->cookie_mask = htonll(UINT64_MAX); | |
1441 | } | |
1442 | } | |
2e4f5fcf BP |
1443 | fm->idle_timeout = ntohs(nfm->idle_timeout); |
1444 | fm->hard_timeout = ntohs(nfm->hard_timeout); | |
1445 | fm->buffer_id = ntohl(nfm->buffer_id); | |
1446 | fm->out_port = ntohs(nfm->out_port); | |
1447 | fm->flags = ntohs(nfm->flags); | |
1448 | } else { | |
1449 | NOT_REACHED(); | |
1450 | } | |
1451 | ||
27527aa0 | 1452 | if (protocol & OFPUTIL_P_TID) { |
6c1491fb BP |
1453 | fm->command = command & 0xff; |
1454 | fm->table_id = command >> 8; | |
1455 | } else { | |
1456 | fm->command = command; | |
1457 | fm->table_id = 0xff; | |
1458 | } | |
1459 | ||
2e4f5fcf BP |
1460 | return 0; |
1461 | } | |
1462 | ||
1463 | /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to | |
27527aa0 | 1464 | * 'protocol' and returns the message. |
6c1491fb BP |
1465 | * |
1466 | * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is | |
1467 | * enabled, false otherwise. */ | |
2e4f5fcf | 1468 | struct ofpbuf * |
a9a2da38 | 1469 | ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm, |
27527aa0 | 1470 | enum ofputil_protocol protocol) |
2e4f5fcf BP |
1471 | { |
1472 | size_t actions_len = fm->n_actions * sizeof *fm->actions; | |
27527aa0 BP |
1473 | struct ofp_flow_mod *ofm; |
1474 | struct nx_flow_mod *nfm; | |
2e4f5fcf | 1475 | struct ofpbuf *msg; |
6c1491fb | 1476 | uint16_t command; |
27527aa0 | 1477 | int match_len; |
6c1491fb | 1478 | |
27527aa0 | 1479 | command = (protocol & OFPUTIL_P_TID |
6c1491fb BP |
1480 | ? (fm->command & 0xff) | (fm->table_id << 8) |
1481 | : fm->command); | |
2e4f5fcf | 1482 | |
27527aa0 BP |
1483 | switch (protocol) { |
1484 | case OFPUTIL_P_OF10: | |
1485 | case OFPUTIL_P_OF10_TID: | |
2e4f5fcf | 1486 | msg = ofpbuf_new(sizeof *ofm + actions_len); |
5293a2e1 | 1487 | ofm = put_openflow(sizeof *ofm, OFPT10_FLOW_MOD, msg); |
b78f6b77 BP |
1488 | ofputil_cls_rule_to_match(&fm->cr, &ofm->match); |
1489 | ofm->cookie = fm->cookie; | |
05411977 | 1490 | ofm->command = htons(command); |
2e4f5fcf BP |
1491 | ofm->idle_timeout = htons(fm->idle_timeout); |
1492 | ofm->hard_timeout = htons(fm->hard_timeout); | |
1493 | ofm->priority = htons(fm->cr.priority); | |
1494 | ofm->buffer_id = htonl(fm->buffer_id); | |
1495 | ofm->out_port = htons(fm->out_port); | |
1496 | ofm->flags = htons(fm->flags); | |
27527aa0 | 1497 | break; |
2e4f5fcf | 1498 | |
27527aa0 BP |
1499 | case OFPUTIL_P_NXM: |
1500 | case OFPUTIL_P_NXM_TID: | |
2e4f5fcf BP |
1501 | msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len); |
1502 | put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg); | |
2e4f5fcf | 1503 | nfm = msg->data; |
6c1491fb | 1504 | nfm->command = htons(command); |
e729e793 JP |
1505 | if (command == OFPFC_ADD) { |
1506 | nfm->cookie = fm->cookie; | |
1507 | match_len = nx_put_match(msg, &fm->cr, 0, 0); | |
1508 | } else { | |
1509 | nfm->cookie = 0; | |
1510 | match_len = nx_put_match(msg, &fm->cr, | |
1511 | fm->cookie, fm->cookie_mask); | |
1512 | } | |
2e4f5fcf BP |
1513 | nfm->idle_timeout = htons(fm->idle_timeout); |
1514 | nfm->hard_timeout = htons(fm->hard_timeout); | |
1515 | nfm->priority = htons(fm->cr.priority); | |
1516 | nfm->buffer_id = htonl(fm->buffer_id); | |
1517 | nfm->out_port = htons(fm->out_port); | |
1518 | nfm->flags = htons(fm->flags); | |
1519 | nfm->match_len = htons(match_len); | |
27527aa0 BP |
1520 | break; |
1521 | ||
1522 | default: | |
2e4f5fcf BP |
1523 | NOT_REACHED(); |
1524 | } | |
1525 | ||
1526 | ofpbuf_put(msg, fm->actions, actions_len); | |
1527 | update_openflow_length(msg); | |
1528 | return msg; | |
1529 | } | |
1530 | ||
27527aa0 BP |
1531 | /* Returns a bitmask with a 1-bit for each protocol that could be used to |
1532 | * send all of the 'n_fm's flow table modification requests in 'fms', and a | |
1533 | * 0-bit for each protocol that is inadequate. | |
1534 | * | |
1535 | * (The return value will have at least one 1-bit.) */ | |
1536 | enum ofputil_protocol | |
1537 | ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms, | |
1538 | size_t n_fms) | |
1539 | { | |
1540 | enum ofputil_protocol usable_protocols; | |
1541 | size_t i; | |
1542 | ||
1543 | usable_protocols = OFPUTIL_P_ANY; | |
1544 | for (i = 0; i < n_fms; i++) { | |
1545 | const struct ofputil_flow_mod *fm = &fms[i]; | |
1546 | ||
1547 | usable_protocols &= ofputil_usable_protocols(&fm->cr); | |
1548 | if (fm->table_id != 0xff) { | |
1549 | usable_protocols &= OFPUTIL_P_TID; | |
1550 | } | |
1551 | if (fm->command != OFPFC_ADD && fm->cookie_mask != htonll(0)) { | |
1552 | usable_protocols &= OFPUTIL_P_NXM_ANY; | |
1553 | } | |
1554 | } | |
1555 | assert(usable_protocols); | |
1556 | ||
1557 | return usable_protocols; | |
1558 | } | |
1559 | ||
90bf1e07 | 1560 | static enum ofperr |
81d1ea94 | 1561 | ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr, |
2e4f5fcf | 1562 | const struct ofp_header *oh, |
2e4f5fcf BP |
1563 | bool aggregate) |
1564 | { | |
63f2140a BP |
1565 | const struct ofp_flow_stats_request *ofsr = |
1566 | (const struct ofp_flow_stats_request *) oh; | |
2e4f5fcf BP |
1567 | |
1568 | fsr->aggregate = aggregate; | |
b78f6b77 | 1569 | ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match); |
2e4f5fcf BP |
1570 | fsr->out_port = ntohs(ofsr->out_port); |
1571 | fsr->table_id = ofsr->table_id; | |
e729e793 | 1572 | fsr->cookie = fsr->cookie_mask = htonll(0); |
2e4f5fcf BP |
1573 | |
1574 | return 0; | |
1575 | } | |
1576 | ||
90bf1e07 | 1577 | static enum ofperr |
81d1ea94 | 1578 | ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr, |
2e4f5fcf BP |
1579 | const struct ofp_header *oh, |
1580 | bool aggregate) | |
1581 | { | |
1582 | const struct nx_flow_stats_request *nfsr; | |
1583 | struct ofpbuf b; | |
90bf1e07 | 1584 | enum ofperr error; |
2e4f5fcf | 1585 | |
2013493b | 1586 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf | 1587 | |
bbc32a88 | 1588 | nfsr = ofpbuf_pull(&b, sizeof *nfsr); |
e729e793 JP |
1589 | error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match, |
1590 | &fsr->cookie, &fsr->cookie_mask); | |
2e4f5fcf BP |
1591 | if (error) { |
1592 | return error; | |
1593 | } | |
1594 | if (b.size) { | |
90bf1e07 | 1595 | return OFPERR_OFPBRC_BAD_LEN; |
2e4f5fcf BP |
1596 | } |
1597 | ||
1598 | fsr->aggregate = aggregate; | |
1599 | fsr->out_port = ntohs(nfsr->out_port); | |
1600 | fsr->table_id = nfsr->table_id; | |
1601 | ||
1602 | return 0; | |
1603 | } | |
1604 | ||
1605 | /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE | |
b78f6b77 BP |
1606 | * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if |
1607 | * successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 1608 | enum ofperr |
81d1ea94 | 1609 | ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr, |
b78f6b77 | 1610 | const struct ofp_header *oh) |
2e4f5fcf BP |
1611 | { |
1612 | const struct ofputil_msg_type *type; | |
1613 | struct ofpbuf b; | |
1614 | int code; | |
1615 | ||
2013493b | 1616 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf BP |
1617 | |
1618 | ofputil_decode_msg_type(oh, &type); | |
1619 | code = ofputil_msg_type_code(type); | |
1620 | switch (code) { | |
1621 | case OFPUTIL_OFPST_FLOW_REQUEST: | |
b78f6b77 | 1622 | return ofputil_decode_ofpst_flow_request(fsr, oh, false); |
2e4f5fcf BP |
1623 | |
1624 | case OFPUTIL_OFPST_AGGREGATE_REQUEST: | |
b78f6b77 | 1625 | return ofputil_decode_ofpst_flow_request(fsr, oh, true); |
2e4f5fcf BP |
1626 | |
1627 | case OFPUTIL_NXST_FLOW_REQUEST: | |
1628 | return ofputil_decode_nxst_flow_request(fsr, oh, false); | |
1629 | ||
1630 | case OFPUTIL_NXST_AGGREGATE_REQUEST: | |
1631 | return ofputil_decode_nxst_flow_request(fsr, oh, true); | |
1632 | ||
1633 | default: | |
1634 | /* Hey, the caller lied. */ | |
1635 | NOT_REACHED(); | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW, | |
4ffd1b43 | 1640 | * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to |
27527aa0 | 1641 | * 'protocol', and returns the message. */ |
2e4f5fcf | 1642 | struct ofpbuf * |
81d1ea94 | 1643 | ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr, |
27527aa0 | 1644 | enum ofputil_protocol protocol) |
2e4f5fcf BP |
1645 | { |
1646 | struct ofpbuf *msg; | |
1647 | ||
27527aa0 BP |
1648 | switch (protocol) { |
1649 | case OFPUTIL_P_OF10: | |
1650 | case OFPUTIL_P_OF10_TID: { | |
2e4f5fcf BP |
1651 | struct ofp_flow_stats_request *ofsr; |
1652 | int type; | |
1653 | ||
2e4f5fcf | 1654 | type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW; |
63f2140a | 1655 | ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg); |
b78f6b77 | 1656 | ofputil_cls_rule_to_match(&fsr->match, &ofsr->match); |
2e4f5fcf BP |
1657 | ofsr->table_id = fsr->table_id; |
1658 | ofsr->out_port = htons(fsr->out_port); | |
27527aa0 BP |
1659 | break; |
1660 | } | |
1661 | ||
1662 | case OFPUTIL_P_NXM: | |
1663 | case OFPUTIL_P_NXM_TID: { | |
2e4f5fcf BP |
1664 | struct nx_flow_stats_request *nfsr; |
1665 | int match_len; | |
9fb7fa87 | 1666 | int subtype; |
2e4f5fcf | 1667 | |
9fb7fa87 | 1668 | subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW; |
63f2140a | 1669 | ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg); |
e729e793 JP |
1670 | match_len = nx_put_match(msg, &fsr->match, |
1671 | fsr->cookie, fsr->cookie_mask); | |
2e4f5fcf BP |
1672 | |
1673 | nfsr = msg->data; | |
1674 | nfsr->out_port = htons(fsr->out_port); | |
1675 | nfsr->match_len = htons(match_len); | |
1676 | nfsr->table_id = fsr->table_id; | |
27527aa0 BP |
1677 | break; |
1678 | } | |
1679 | ||
1680 | default: | |
2e4f5fcf BP |
1681 | NOT_REACHED(); |
1682 | } | |
1683 | ||
1684 | return msg; | |
1685 | } | |
d1e2cf21 | 1686 | |
27527aa0 BP |
1687 | /* Returns a bitmask with a 1-bit for each protocol that could be used to |
1688 | * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate. | |
1689 | * | |
1690 | * (The return value will have at least one 1-bit.) */ | |
1691 | enum ofputil_protocol | |
1692 | ofputil_flow_stats_request_usable_protocols( | |
1693 | const struct ofputil_flow_stats_request *fsr) | |
1694 | { | |
1695 | enum ofputil_protocol usable_protocols; | |
1696 | ||
1697 | usable_protocols = ofputil_usable_protocols(&fsr->match); | |
1698 | if (fsr->cookie_mask != htonll(0)) { | |
1699 | usable_protocols &= OFPUTIL_P_NXM_ANY; | |
1700 | } | |
1701 | return usable_protocols; | |
1702 | } | |
1703 | ||
4ffd1b43 | 1704 | /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract |
b78f6b77 | 1705 | * ofputil_flow_stats in 'fs'. |
4ffd1b43 BP |
1706 | * |
1707 | * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single | |
1708 | * OpenFlow message. Calling this function multiple times for a single 'msg' | |
1709 | * iterates through the replies. The caller must initially leave 'msg''s layer | |
1710 | * pointers null and not modify them between calls. | |
1711 | * | |
f27f2134 BP |
1712 | * Most switches don't send the values needed to populate fs->idle_age and |
1713 | * fs->hard_age, so those members will usually be set to 0. If the switch from | |
1714 | * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass | |
1715 | * 'flow_age_extension' as true so that the contents of 'msg' determine the | |
1716 | * 'idle_age' and 'hard_age' members in 'fs'. | |
1717 | * | |
4ffd1b43 BP |
1718 | * Returns 0 if successful, EOF if no replies were left in this 'msg', |
1719 | * otherwise a positive errno value. */ | |
1720 | int | |
1721 | ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs, | |
f27f2134 BP |
1722 | struct ofpbuf *msg, |
1723 | bool flow_age_extension) | |
4ffd1b43 BP |
1724 | { |
1725 | const struct ofputil_msg_type *type; | |
1726 | int code; | |
1727 | ||
1728 | ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type); | |
1729 | code = ofputil_msg_type_code(type); | |
1730 | if (!msg->l2) { | |
1731 | msg->l2 = msg->data; | |
1732 | if (code == OFPUTIL_OFPST_FLOW_REPLY) { | |
28c8bad1 | 1733 | ofpbuf_pull(msg, sizeof(struct ofp_stats_msg)); |
4ffd1b43 BP |
1734 | } else if (code == OFPUTIL_NXST_FLOW_REPLY) { |
1735 | ofpbuf_pull(msg, sizeof(struct nicira_stats_msg)); | |
1736 | } else { | |
1737 | NOT_REACHED(); | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | if (!msg->size) { | |
1742 | return EOF; | |
1743 | } else if (code == OFPUTIL_OFPST_FLOW_REPLY) { | |
1744 | const struct ofp_flow_stats *ofs; | |
1745 | size_t length; | |
1746 | ||
1747 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
1748 | if (!ofs) { | |
1749 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover " | |
1750 | "bytes at end", msg->size); | |
1751 | return EINVAL; | |
1752 | } | |
1753 | ||
1754 | length = ntohs(ofs->length); | |
1755 | if (length < sizeof *ofs) { | |
1756 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
1757 | "length %zu", length); | |
1758 | return EINVAL; | |
1759 | } | |
1760 | ||
1761 | if (ofputil_pull_actions(msg, length - sizeof *ofs, | |
1762 | &fs->actions, &fs->n_actions)) { | |
1763 | return EINVAL; | |
1764 | } | |
1765 | ||
1766 | fs->cookie = get_32aligned_be64(&ofs->cookie); | |
1767 | ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority), | |
b78f6b77 | 1768 | &fs->rule); |
4ffd1b43 BP |
1769 | fs->table_id = ofs->table_id; |
1770 | fs->duration_sec = ntohl(ofs->duration_sec); | |
1771 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
1772 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
1773 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
f27f2134 BP |
1774 | fs->idle_age = -1; |
1775 | fs->hard_age = -1; | |
4ffd1b43 BP |
1776 | fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count)); |
1777 | fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count)); | |
1778 | } else if (code == OFPUTIL_NXST_FLOW_REPLY) { | |
1779 | const struct nx_flow_stats *nfs; | |
1780 | size_t match_len, length; | |
1781 | ||
1782 | nfs = ofpbuf_try_pull(msg, sizeof *nfs); | |
1783 | if (!nfs) { | |
1784 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover " | |
1785 | "bytes at end", msg->size); | |
1786 | return EINVAL; | |
1787 | } | |
1788 | ||
1789 | length = ntohs(nfs->length); | |
1790 | match_len = ntohs(nfs->match_len); | |
1791 | if (length < sizeof *nfs + ROUND_UP(match_len, 8)) { | |
1792 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu " | |
1793 | "claims invalid length %zu", match_len, length); | |
1794 | return EINVAL; | |
1795 | } | |
e729e793 JP |
1796 | if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule, |
1797 | NULL, NULL)) { | |
4ffd1b43 BP |
1798 | return EINVAL; |
1799 | } | |
1800 | ||
1801 | if (ofputil_pull_actions(msg, | |
1802 | length - sizeof *nfs - ROUND_UP(match_len, 8), | |
1803 | &fs->actions, &fs->n_actions)) { | |
1804 | return EINVAL; | |
1805 | } | |
1806 | ||
1807 | fs->cookie = nfs->cookie; | |
1808 | fs->table_id = nfs->table_id; | |
1809 | fs->duration_sec = ntohl(nfs->duration_sec); | |
1810 | fs->duration_nsec = ntohl(nfs->duration_nsec); | |
1811 | fs->idle_timeout = ntohs(nfs->idle_timeout); | |
1812 | fs->hard_timeout = ntohs(nfs->hard_timeout); | |
f27f2134 BP |
1813 | fs->idle_age = -1; |
1814 | fs->hard_age = -1; | |
1815 | if (flow_age_extension) { | |
1816 | if (nfs->idle_age) { | |
1817 | fs->idle_age = ntohs(nfs->idle_age) - 1; | |
1818 | } | |
1819 | if (nfs->hard_age) { | |
1820 | fs->hard_age = ntohs(nfs->hard_age) - 1; | |
1821 | } | |
1822 | } | |
4ffd1b43 BP |
1823 | fs->packet_count = ntohll(nfs->packet_count); |
1824 | fs->byte_count = ntohll(nfs->byte_count); | |
1825 | } else { | |
1826 | NOT_REACHED(); | |
1827 | } | |
1828 | ||
1829 | return 0; | |
1830 | } | |
1831 | ||
5e9d0469 BP |
1832 | /* Returns 'count' unchanged except that UINT64_MAX becomes 0. |
1833 | * | |
1834 | * We use this in situations where OVS internally uses UINT64_MAX to mean | |
1835 | * "value unknown" but OpenFlow 1.0 does not define any unknown value. */ | |
1836 | static uint64_t | |
1837 | unknown_to_zero(uint64_t count) | |
1838 | { | |
1839 | return count != UINT64_MAX ? count : 0; | |
1840 | } | |
1841 | ||
349adfb2 BP |
1842 | /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to |
1843 | * those already present in the list of ofpbufs in 'replies'. 'replies' should | |
1844 | * have been initialized with ofputil_start_stats_reply(). */ | |
1845 | void | |
1846 | ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs, | |
1847 | struct list *replies) | |
1848 | { | |
1849 | size_t act_len = fs->n_actions * sizeof *fs->actions; | |
1850 | const struct ofp_stats_msg *osm; | |
1851 | ||
1852 | osm = ofpbuf_from_list(list_back(replies))->data; | |
1853 | if (osm->type == htons(OFPST_FLOW)) { | |
1854 | size_t len = offsetof(struct ofp_flow_stats, actions) + act_len; | |
1855 | struct ofp_flow_stats *ofs; | |
1856 | ||
1857 | ofs = ofputil_append_stats_reply(len, replies); | |
1858 | ofs->length = htons(len); | |
1859 | ofs->table_id = fs->table_id; | |
1860 | ofs->pad = 0; | |
1861 | ofputil_cls_rule_to_match(&fs->rule, &ofs->match); | |
1862 | ofs->duration_sec = htonl(fs->duration_sec); | |
1863 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
1864 | ofs->priority = htons(fs->rule.priority); | |
1865 | ofs->idle_timeout = htons(fs->idle_timeout); | |
1866 | ofs->hard_timeout = htons(fs->hard_timeout); | |
1867 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
1868 | put_32aligned_be64(&ofs->cookie, fs->cookie); | |
5e9d0469 BP |
1869 | put_32aligned_be64(&ofs->packet_count, |
1870 | htonll(unknown_to_zero(fs->packet_count))); | |
1871 | put_32aligned_be64(&ofs->byte_count, | |
1872 | htonll(unknown_to_zero(fs->byte_count))); | |
349adfb2 BP |
1873 | memcpy(ofs->actions, fs->actions, act_len); |
1874 | } else if (osm->type == htons(OFPST_VENDOR)) { | |
1875 | struct nx_flow_stats *nfs; | |
1876 | struct ofpbuf *msg; | |
1877 | size_t start_len; | |
1878 | ||
1879 | msg = ofputil_reserve_stats_reply( | |
1880 | sizeof *nfs + NXM_MAX_LEN + act_len, replies); | |
1881 | start_len = msg->size; | |
1882 | ||
1883 | nfs = ofpbuf_put_uninit(msg, sizeof *nfs); | |
1884 | nfs->table_id = fs->table_id; | |
1885 | nfs->pad = 0; | |
1886 | nfs->duration_sec = htonl(fs->duration_sec); | |
1887 | nfs->duration_nsec = htonl(fs->duration_nsec); | |
1888 | nfs->priority = htons(fs->rule.priority); | |
1889 | nfs->idle_timeout = htons(fs->idle_timeout); | |
1890 | nfs->hard_timeout = htons(fs->hard_timeout); | |
f27f2134 BP |
1891 | nfs->idle_age = htons(fs->idle_age < 0 ? 0 |
1892 | : fs->idle_age < UINT16_MAX ? fs->idle_age + 1 | |
1893 | : UINT16_MAX); | |
1894 | nfs->hard_age = htons(fs->hard_age < 0 ? 0 | |
1895 | : fs->hard_age < UINT16_MAX ? fs->hard_age + 1 | |
1896 | : UINT16_MAX); | |
e729e793 | 1897 | nfs->match_len = htons(nx_put_match(msg, &fs->rule, 0, 0)); |
349adfb2 BP |
1898 | nfs->cookie = fs->cookie; |
1899 | nfs->packet_count = htonll(fs->packet_count); | |
1900 | nfs->byte_count = htonll(fs->byte_count); | |
1901 | ofpbuf_put(msg, fs->actions, act_len); | |
1902 | nfs->length = htons(msg->size - start_len); | |
1903 | } else { | |
1904 | NOT_REACHED(); | |
1905 | } | |
1906 | } | |
1907 | ||
76c93b22 | 1908 | /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or |
27527aa0 | 1909 | * NXST_AGGREGATE reply according to 'protocol', and returns the message. */ |
76c93b22 BP |
1910 | struct ofpbuf * |
1911 | ofputil_encode_aggregate_stats_reply( | |
1912 | const struct ofputil_aggregate_stats *stats, | |
1913 | const struct ofp_stats_msg *request) | |
1914 | { | |
1915 | struct ofpbuf *msg; | |
1916 | ||
1917 | if (request->type == htons(OFPST_AGGREGATE)) { | |
1918 | struct ofp_aggregate_stats_reply *asr; | |
1919 | ||
1920 | asr = ofputil_make_stats_reply(sizeof *asr, request, &msg); | |
5e9d0469 BP |
1921 | put_32aligned_be64(&asr->packet_count, |
1922 | htonll(unknown_to_zero(stats->packet_count))); | |
1923 | put_32aligned_be64(&asr->byte_count, | |
1924 | htonll(unknown_to_zero(stats->byte_count))); | |
76c93b22 BP |
1925 | asr->flow_count = htonl(stats->flow_count); |
1926 | } else if (request->type == htons(OFPST_VENDOR)) { | |
1927 | struct nx_aggregate_stats_reply *nasr; | |
1928 | ||
1929 | nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg); | |
1930 | assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE)); | |
1931 | nasr->packet_count = htonll(stats->packet_count); | |
1932 | nasr->byte_count = htonll(stats->byte_count); | |
1933 | nasr->flow_count = htonl(stats->flow_count); | |
1934 | } else { | |
1935 | NOT_REACHED(); | |
1936 | } | |
1937 | ||
1938 | return msg; | |
1939 | } | |
1940 | ||
b78f6b77 BP |
1941 | /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an |
1942 | * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise | |
1943 | * an OpenFlow error code. */ | |
90bf1e07 | 1944 | enum ofperr |
9b045a0c | 1945 | ofputil_decode_flow_removed(struct ofputil_flow_removed *fr, |
b78f6b77 | 1946 | const struct ofp_header *oh) |
9b045a0c BP |
1947 | { |
1948 | const struct ofputil_msg_type *type; | |
1949 | enum ofputil_msg_code code; | |
1950 | ||
1951 | ofputil_decode_msg_type(oh, &type); | |
1952 | code = ofputil_msg_type_code(type); | |
1953 | if (code == OFPUTIL_OFPT_FLOW_REMOVED) { | |
1954 | const struct ofp_flow_removed *ofr; | |
1955 | ||
1956 | ofr = (const struct ofp_flow_removed *) oh; | |
1957 | ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority), | |
b78f6b77 | 1958 | &fr->rule); |
9b045a0c BP |
1959 | fr->cookie = ofr->cookie; |
1960 | fr->reason = ofr->reason; | |
1961 | fr->duration_sec = ntohl(ofr->duration_sec); | |
1962 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
1963 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
1964 | fr->packet_count = ntohll(ofr->packet_count); | |
1965 | fr->byte_count = ntohll(ofr->byte_count); | |
1966 | } else if (code == OFPUTIL_NXT_FLOW_REMOVED) { | |
1967 | struct nx_flow_removed *nfr; | |
1968 | struct ofpbuf b; | |
1969 | int error; | |
1970 | ||
1971 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1972 | ||
1973 | nfr = ofpbuf_pull(&b, sizeof *nfr); | |
1974 | error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority), | |
e729e793 | 1975 | &fr->rule, NULL, NULL); |
9b045a0c BP |
1976 | if (error) { |
1977 | return error; | |
1978 | } | |
1979 | if (b.size) { | |
90bf1e07 | 1980 | return OFPERR_OFPBRC_BAD_LEN; |
9b045a0c BP |
1981 | } |
1982 | ||
1983 | fr->cookie = nfr->cookie; | |
1984 | fr->reason = nfr->reason; | |
1985 | fr->duration_sec = ntohl(nfr->duration_sec); | |
1986 | fr->duration_nsec = ntohl(nfr->duration_nsec); | |
1987 | fr->idle_timeout = ntohs(nfr->idle_timeout); | |
1988 | fr->packet_count = ntohll(nfr->packet_count); | |
1989 | fr->byte_count = ntohll(nfr->byte_count); | |
1990 | } else { | |
1991 | NOT_REACHED(); | |
1992 | } | |
1993 | ||
1994 | return 0; | |
1995 | } | |
1996 | ||
588cd7b5 | 1997 | /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or |
27527aa0 | 1998 | * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the |
588cd7b5 BP |
1999 | * message. */ |
2000 | struct ofpbuf * | |
2001 | ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr, | |
27527aa0 | 2002 | enum ofputil_protocol protocol) |
588cd7b5 BP |
2003 | { |
2004 | struct ofpbuf *msg; | |
2005 | ||
27527aa0 BP |
2006 | switch (protocol) { |
2007 | case OFPUTIL_P_OF10: | |
2008 | case OFPUTIL_P_OF10_TID: { | |
588cd7b5 BP |
2009 | struct ofp_flow_removed *ofr; |
2010 | ||
2011 | ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0), | |
2012 | &msg); | |
b78f6b77 | 2013 | ofputil_cls_rule_to_match(&fr->rule, &ofr->match); |
7fb563b9 | 2014 | ofr->cookie = fr->cookie; |
588cd7b5 BP |
2015 | ofr->priority = htons(fr->rule.priority); |
2016 | ofr->reason = fr->reason; | |
2017 | ofr->duration_sec = htonl(fr->duration_sec); | |
2018 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
2019 | ofr->idle_timeout = htons(fr->idle_timeout); | |
5e9d0469 BP |
2020 | ofr->packet_count = htonll(unknown_to_zero(fr->packet_count)); |
2021 | ofr->byte_count = htonll(unknown_to_zero(fr->byte_count)); | |
27527aa0 BP |
2022 | break; |
2023 | } | |
2024 | ||
2025 | case OFPUTIL_P_NXM: | |
2026 | case OFPUTIL_P_NXM_TID: { | |
588cd7b5 BP |
2027 | struct nx_flow_removed *nfr; |
2028 | int match_len; | |
2029 | ||
2030 | make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg); | |
e729e793 | 2031 | match_len = nx_put_match(msg, &fr->rule, 0, 0); |
588cd7b5 BP |
2032 | |
2033 | nfr = msg->data; | |
2034 | nfr->cookie = fr->cookie; | |
2035 | nfr->priority = htons(fr->rule.priority); | |
2036 | nfr->reason = fr->reason; | |
2037 | nfr->duration_sec = htonl(fr->duration_sec); | |
2038 | nfr->duration_nsec = htonl(fr->duration_nsec); | |
2039 | nfr->idle_timeout = htons(fr->idle_timeout); | |
2040 | nfr->match_len = htons(match_len); | |
2041 | nfr->packet_count = htonll(fr->packet_count); | |
2042 | nfr->byte_count = htonll(fr->byte_count); | |
27527aa0 BP |
2043 | break; |
2044 | } | |
2045 | ||
2046 | default: | |
588cd7b5 BP |
2047 | NOT_REACHED(); |
2048 | } | |
2049 | ||
2050 | return msg; | |
2051 | } | |
2052 | ||
65120a8a EJ |
2053 | int |
2054 | ofputil_decode_packet_in(struct ofputil_packet_in *pin, | |
2055 | const struct ofp_header *oh) | |
2056 | { | |
2057 | const struct ofputil_msg_type *type; | |
2058 | enum ofputil_msg_code code; | |
2059 | ||
2060 | ofputil_decode_msg_type(oh, &type); | |
2061 | code = ofputil_msg_type_code(type); | |
2062 | memset(pin, 0, sizeof *pin); | |
2063 | ||
2064 | if (code == OFPUTIL_OFPT_PACKET_IN) { | |
2065 | const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh; | |
2066 | ||
2067 | pin->packet = opi->data; | |
2068 | pin->packet_len = ntohs(opi->header.length) | |
2069 | - offsetof(struct ofp_packet_in, data); | |
2070 | ||
5d6c3af0 | 2071 | pin->fmd.in_port = ntohs(opi->in_port); |
65120a8a EJ |
2072 | pin->reason = opi->reason; |
2073 | pin->buffer_id = ntohl(opi->buffer_id); | |
2074 | pin->total_len = ntohs(opi->total_len); | |
54834960 | 2075 | } else if (code == OFPUTIL_NXT_PACKET_IN) { |
73dbf4ab | 2076 | const struct nx_packet_in *npi; |
54834960 EJ |
2077 | struct cls_rule rule; |
2078 | struct ofpbuf b; | |
2079 | int error; | |
2080 | ||
2081 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2082 | ||
2083 | npi = ofpbuf_pull(&b, sizeof *npi); | |
2084 | error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL, | |
2085 | NULL); | |
2086 | if (error) { | |
2087 | return error; | |
2088 | } | |
2089 | ||
2090 | if (!ofpbuf_try_pull(&b, 2)) { | |
90bf1e07 | 2091 | return OFPERR_OFPBRC_BAD_LEN; |
54834960 EJ |
2092 | } |
2093 | ||
2094 | pin->packet = b.data; | |
2095 | pin->packet_len = b.size; | |
2096 | pin->reason = npi->reason; | |
2097 | pin->table_id = npi->table_id; | |
2098 | pin->cookie = npi->cookie; | |
2099 | ||
2100 | pin->fmd.in_port = rule.flow.in_port; | |
2101 | ||
2102 | pin->fmd.tun_id = rule.flow.tun_id; | |
2103 | pin->fmd.tun_id_mask = rule.wc.tun_id_mask; | |
2104 | ||
2105 | memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs); | |
2106 | memcpy(pin->fmd.reg_masks, rule.wc.reg_masks, | |
2107 | sizeof pin->fmd.reg_masks); | |
2108 | ||
2109 | pin->buffer_id = ntohl(npi->buffer_id); | |
2110 | pin->total_len = ntohs(npi->total_len); | |
65120a8a EJ |
2111 | } else { |
2112 | NOT_REACHED(); | |
2113 | } | |
2114 | ||
2115 | return 0; | |
2116 | } | |
2117 | ||
54834960 EJ |
2118 | /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message |
2119 | * in the format specified by 'packet_in_format'. */ | |
ebb57021 | 2120 | struct ofpbuf * |
54834960 EJ |
2121 | ofputil_encode_packet_in(const struct ofputil_packet_in *pin, |
2122 | enum nx_packet_in_format packet_in_format) | |
ebb57021 | 2123 | { |
54834960 EJ |
2124 | size_t send_len = MIN(pin->send_len, pin->packet_len); |
2125 | struct ofpbuf *packet; | |
ebb57021 BP |
2126 | |
2127 | /* Add OFPT_PACKET_IN. */ | |
54834960 EJ |
2128 | if (packet_in_format == NXPIF_OPENFLOW10) { |
2129 | size_t header_len = offsetof(struct ofp_packet_in, data); | |
2130 | struct ofp_packet_in *opi; | |
2131 | ||
2132 | packet = ofpbuf_new(send_len + header_len); | |
2133 | opi = ofpbuf_put_zeros(packet, header_len); | |
87ea5e5e | 2134 | opi->header.version = OFP10_VERSION; |
54834960 EJ |
2135 | opi->header.type = OFPT_PACKET_IN; |
2136 | opi->total_len = htons(pin->total_len); | |
2137 | opi->in_port = htons(pin->fmd.in_port); | |
2138 | opi->reason = pin->reason; | |
2139 | opi->buffer_id = htonl(pin->buffer_id); | |
2140 | ||
2141 | ofpbuf_put(packet, pin->packet, send_len); | |
2142 | } else if (packet_in_format == NXPIF_NXM) { | |
73dbf4ab | 2143 | struct nx_packet_in *npi; |
54834960 EJ |
2144 | struct cls_rule rule; |
2145 | size_t match_len; | |
2146 | size_t i; | |
2147 | ||
2148 | /* Estimate of required PACKET_IN length includes the NPI header, space | |
2149 | * for the match (2 times sizeof the metadata seems like enough), 2 | |
2150 | * bytes for padding, and the packet length. */ | |
2151 | packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2 | |
2152 | + 2 + send_len); | |
2153 | ||
2154 | cls_rule_init_catchall(&rule, 0); | |
2155 | cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id, | |
2156 | pin->fmd.tun_id_mask); | |
2157 | ||
2158 | for (i = 0; i < FLOW_N_REGS; i++) { | |
2159 | cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i], | |
2160 | pin->fmd.reg_masks[i]); | |
2161 | } | |
2162 | ||
2163 | cls_rule_set_in_port(&rule, pin->fmd.in_port); | |
2164 | ||
2165 | ofpbuf_put_zeros(packet, sizeof *npi); | |
2166 | match_len = nx_put_match(packet, &rule, 0, 0); | |
2167 | ofpbuf_put_zeros(packet, 2); | |
2168 | ofpbuf_put(packet, pin->packet, send_len); | |
2169 | ||
2170 | npi = packet->data; | |
87ea5e5e | 2171 | npi->nxh.header.version = OFP10_VERSION; |
54834960 EJ |
2172 | npi->nxh.header.type = OFPT_VENDOR; |
2173 | npi->nxh.vendor = htonl(NX_VENDOR_ID); | |
2174 | npi->nxh.subtype = htonl(NXT_PACKET_IN); | |
2175 | ||
2176 | npi->buffer_id = htonl(pin->buffer_id); | |
2177 | npi->total_len = htons(pin->total_len); | |
2178 | npi->reason = pin->reason; | |
2179 | npi->table_id = pin->table_id; | |
2180 | npi->cookie = pin->cookie; | |
2181 | npi->match_len = htons(match_len); | |
2182 | } else { | |
2183 | NOT_REACHED(); | |
2184 | } | |
2185 | update_openflow_length(packet); | |
2186 | ||
2187 | return packet; | |
ebb57021 BP |
2188 | } |
2189 | ||
7c1a76a4 BP |
2190 | const char * |
2191 | ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason) | |
2192 | { | |
2193 | static char s[INT_STRLEN(int) + 1]; | |
2194 | ||
2195 | switch (reason) { | |
2196 | case OFPR_NO_MATCH: | |
2197 | return "no_match"; | |
2198 | case OFPR_ACTION: | |
2199 | return "action"; | |
2200 | case OFPR_INVALID_TTL: | |
2201 | return "invalid_ttl"; | |
2202 | ||
2203 | case OFPR_N_REASONS: | |
2204 | default: | |
2205 | sprintf(s, "%d", (int) reason); | |
2206 | return s; | |
2207 | } | |
2208 | } | |
2209 | ||
2210 | bool | |
2211 | ofputil_packet_in_reason_from_string(const char *s, | |
2212 | enum ofp_packet_in_reason *reason) | |
2213 | { | |
2214 | int i; | |
2215 | ||
2216 | for (i = 0; i < OFPR_N_REASONS; i++) { | |
2217 | if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) { | |
2218 | *reason = i; | |
2219 | return true; | |
2220 | } | |
2221 | } | |
2222 | return false; | |
2223 | } | |
2224 | ||
c6a93eb7 BP |
2225 | enum ofperr |
2226 | ofputil_decode_packet_out(struct ofputil_packet_out *po, | |
2227 | const struct ofp_packet_out *opo) | |
2228 | { | |
2229 | enum ofperr error; | |
2230 | struct ofpbuf b; | |
2231 | ||
2232 | po->buffer_id = ntohl(opo->buffer_id); | |
2233 | po->in_port = ntohs(opo->in_port); | |
2234 | if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL | |
751c7785 | 2235 | && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) { |
c6a93eb7 BP |
2236 | VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16, |
2237 | po->in_port); | |
2238 | return OFPERR_NXBRC_BAD_IN_PORT; | |
2239 | } | |
2240 | ||
2241 | ofpbuf_use_const(&b, opo, ntohs(opo->header.length)); | |
2242 | ofpbuf_pull(&b, sizeof *opo); | |
2243 | ||
2244 | error = ofputil_pull_actions(&b, ntohs(opo->actions_len), | |
2245 | &po->actions, &po->n_actions); | |
2246 | if (error) { | |
2247 | return error; | |
2248 | } | |
2249 | ||
2250 | if (po->buffer_id == UINT32_MAX) { | |
2251 | po->packet = b.data; | |
2252 | po->packet_len = b.size; | |
2253 | } else { | |
2254 | po->packet = NULL; | |
2255 | po->packet_len = 0; | |
2256 | } | |
2257 | ||
2258 | return 0; | |
2259 | } | |
6c038611 BP |
2260 | \f |
2261 | /* ofputil_phy_port */ | |
2262 | ||
2263 | /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */ | |
2264 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ | |
2265 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
2266 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
2267 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
2268 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
2269 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
2270 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
2271 | ||
2272 | /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */ | |
9e1fd49b BP |
2273 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4)); |
2274 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4)); | |
2275 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4)); | |
2276 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4)); | |
2277 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4)); | |
6c038611 | 2278 | |
9e1fd49b BP |
2279 | static enum netdev_features |
2280 | netdev_port_features_from_ofp10(ovs_be32 ofp10_) | |
6c038611 BP |
2281 | { |
2282 | uint32_t ofp10 = ntohl(ofp10_); | |
2283 | return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4); | |
2284 | } | |
2285 | ||
9e1fd49b BP |
2286 | static ovs_be32 |
2287 | netdev_port_features_to_ofp10(enum netdev_features features) | |
6c038611 BP |
2288 | { |
2289 | return htonl((features & 0x7f) | ((features & 0xf800) >> 4)); | |
2290 | } | |
c6a93eb7 | 2291 | |
9e1fd49b BP |
2292 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ |
2293 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
2294 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
2295 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
2296 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
2297 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
2298 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
2299 | BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */ | |
2300 | BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */ | |
2301 | BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */ | |
2302 | BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */ | |
2303 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */ | |
2304 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */ | |
2305 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */ | |
2306 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */ | |
2307 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */ | |
2308 | ||
2309 | static enum netdev_features | |
2310 | netdev_port_features_from_ofp11(ovs_be32 ofp11) | |
2311 | { | |
2312 | return ntohl(ofp11) & 0xffff; | |
2313 | } | |
2314 | ||
2315 | static ovs_be32 | |
2316 | netdev_port_features_to_ofp11(enum netdev_features features) | |
2317 | { | |
2318 | return htonl(features & 0xffff); | |
2319 | } | |
2320 | ||
2321 | static enum ofperr | |
2322 | ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp, | |
2323 | const struct ofp10_phy_port *opp) | |
2324 | { | |
2325 | memset(pp, 0, sizeof *pp); | |
2326 | ||
2327 | pp->port_no = ntohs(opp->port_no); | |
2328 | memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN); | |
2329 | ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN); | |
2330 | ||
2331 | pp->config = ntohl(opp->config) & OFPPC10_ALL; | |
2332 | pp->state = ntohl(opp->state) & OFPPS10_ALL; | |
2333 | ||
2334 | pp->curr = netdev_port_features_from_ofp10(opp->curr); | |
2335 | pp->advertised = netdev_port_features_from_ofp10(opp->advertised); | |
2336 | pp->supported = netdev_port_features_from_ofp10(opp->supported); | |
2337 | pp->peer = netdev_port_features_from_ofp10(opp->peer); | |
2338 | ||
2339 | pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000; | |
2340 | pp->max_speed = netdev_features_to_bps(pp->supported) / 1000; | |
2341 | ||
2342 | return 0; | |
2343 | } | |
2344 | ||
2345 | static enum ofperr | |
2346 | ofputil_decode_ofp11_port(struct ofputil_phy_port *pp, | |
2347 | const struct ofp11_port *op) | |
2348 | { | |
2349 | enum ofperr error; | |
2350 | ||
2351 | memset(pp, 0, sizeof *pp); | |
2352 | ||
2353 | error = ofputil_port_from_ofp11(op->port_no, &pp->port_no); | |
2354 | if (error) { | |
2355 | return error; | |
2356 | } | |
2357 | memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN); | |
2358 | ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN); | |
2359 | ||
2360 | pp->config = ntohl(op->config) & OFPPC11_ALL; | |
2361 | pp->state = ntohl(op->state) & OFPPC11_ALL; | |
2362 | ||
2363 | pp->curr = netdev_port_features_from_ofp11(op->curr); | |
2364 | pp->advertised = netdev_port_features_from_ofp11(op->advertised); | |
2365 | pp->supported = netdev_port_features_from_ofp11(op->supported); | |
2366 | pp->peer = netdev_port_features_from_ofp11(op->peer); | |
2367 | ||
2368 | pp->curr_speed = ntohl(op->curr_speed); | |
2369 | pp->max_speed = ntohl(op->max_speed); | |
2370 | ||
2371 | return 0; | |
2372 | } | |
2373 | ||
5b5a3a67 JP |
2374 | static size_t |
2375 | ofputil_get_phy_port_size(uint8_t ofp_version) | |
2376 | { | |
2377 | return ofp_version == OFP10_VERSION ? sizeof(struct ofp10_phy_port) | |
2378 | : sizeof(struct ofp11_port); | |
2379 | } | |
2380 | ||
9e1fd49b BP |
2381 | static void |
2382 | ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp, | |
2383 | struct ofp10_phy_port *opp) | |
2384 | { | |
2385 | memset(opp, 0, sizeof *opp); | |
2386 | ||
2387 | opp->port_no = htons(pp->port_no); | |
2388 | memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
2389 | ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
2390 | ||
2391 | opp->config = htonl(pp->config & OFPPC10_ALL); | |
2392 | opp->state = htonl(pp->state & OFPPS10_ALL); | |
2393 | ||
2394 | opp->curr = netdev_port_features_to_ofp10(pp->curr); | |
2395 | opp->advertised = netdev_port_features_to_ofp10(pp->advertised); | |
2396 | opp->supported = netdev_port_features_to_ofp10(pp->supported); | |
2397 | opp->peer = netdev_port_features_to_ofp10(pp->peer); | |
2398 | } | |
2399 | ||
2400 | static void | |
2401 | ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp, | |
2402 | struct ofp11_port *op) | |
2403 | { | |
2404 | memset(op, 0, sizeof *op); | |
2405 | ||
2406 | op->port_no = ofputil_port_to_ofp11(pp->port_no); | |
2407 | memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
2408 | ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
2409 | ||
2410 | op->config = htonl(pp->config & OFPPC11_ALL); | |
2411 | op->state = htonl(pp->state & OFPPS11_ALL); | |
2412 | ||
2413 | op->curr = netdev_port_features_to_ofp11(pp->curr); | |
2414 | op->advertised = netdev_port_features_to_ofp11(pp->advertised); | |
2415 | op->supported = netdev_port_features_to_ofp11(pp->supported); | |
2416 | op->peer = netdev_port_features_to_ofp11(pp->peer); | |
2417 | ||
2418 | op->curr_speed = htonl(pp->curr_speed); | |
2419 | op->max_speed = htonl(pp->max_speed); | |
2420 | } | |
2421 | ||
2422 | static void | |
2423 | ofputil_put_phy_port(uint8_t ofp_version, const struct ofputil_phy_port *pp, | |
2424 | struct ofpbuf *b) | |
2425 | { | |
2426 | if (ofp_version == OFP10_VERSION) { | |
2427 | struct ofp10_phy_port *opp; | |
2428 | if (b->size + sizeof *opp <= UINT16_MAX) { | |
2429 | opp = ofpbuf_put_uninit(b, sizeof *opp); | |
2430 | ofputil_encode_ofp10_phy_port(pp, opp); | |
2431 | } | |
2432 | } else { | |
2433 | struct ofp11_port *op; | |
2434 | if (b->size + sizeof *op <= UINT16_MAX) { | |
2435 | op = ofpbuf_put_uninit(b, sizeof *op); | |
2436 | ofputil_encode_ofp11_port(pp, op); | |
2437 | } | |
2438 | } | |
2439 | } | |
2be393ed JP |
2440 | |
2441 | void | |
2442 | ofputil_append_port_desc_stats_reply(uint8_t ofp_version, | |
2443 | const struct ofputil_phy_port *pp, | |
2444 | struct list *replies) | |
2445 | { | |
2446 | if (ofp_version == OFP10_VERSION) { | |
2447 | struct ofp10_phy_port *opp; | |
2448 | ||
2449 | opp = ofputil_append_stats_reply(sizeof *opp, replies); | |
2450 | ofputil_encode_ofp10_phy_port(pp, opp); | |
2451 | } else { | |
2452 | struct ofp11_port *op; | |
2453 | ||
2454 | op = ofputil_append_stats_reply(sizeof *op, replies); | |
2455 | ofputil_encode_ofp11_port(pp, op); | |
2456 | } | |
2457 | } | |
9e1fd49b BP |
2458 | \f |
2459 | /* ofputil_switch_features */ | |
2460 | ||
2461 | #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \ | |
2462 | OFPC_IP_REASM | OFPC_QUEUE_STATS | OFPC_ARP_MATCH_IP) | |
2463 | BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS); | |
2464 | BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS); | |
2465 | BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS); | |
2466 | BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM); | |
2467 | BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS); | |
2468 | BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP); | |
2469 | ||
2470 | struct ofputil_action_bit_translation { | |
2471 | enum ofputil_action_bitmap ofputil_bit; | |
2472 | int of_bit; | |
2473 | }; | |
2474 | ||
2475 | static const struct ofputil_action_bit_translation of10_action_bits[] = { | |
2476 | { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT }, | |
2477 | { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID }, | |
2478 | { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP }, | |
2479 | { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN }, | |
2480 | { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC }, | |
2481 | { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST }, | |
2482 | { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC }, | |
2483 | { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST }, | |
2484 | { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS }, | |
2485 | { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC }, | |
2486 | { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST }, | |
2487 | { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE }, | |
2488 | { 0, 0 }, | |
2489 | }; | |
2490 | ||
2491 | static const struct ofputil_action_bit_translation of11_action_bits[] = { | |
2492 | { OFPUTIL_A_OUTPUT, OFPAT11_OUTPUT }, | |
2493 | { OFPUTIL_A_SET_VLAN_VID, OFPAT11_SET_VLAN_VID }, | |
2494 | { OFPUTIL_A_SET_VLAN_PCP, OFPAT11_SET_VLAN_PCP }, | |
2495 | { OFPUTIL_A_SET_DL_SRC, OFPAT11_SET_DL_SRC }, | |
2496 | { OFPUTIL_A_SET_DL_DST, OFPAT11_SET_DL_DST }, | |
2497 | { OFPUTIL_A_SET_NW_SRC, OFPAT11_SET_NW_SRC }, | |
2498 | { OFPUTIL_A_SET_NW_DST, OFPAT11_SET_NW_DST }, | |
2499 | { OFPUTIL_A_SET_NW_TOS, OFPAT11_SET_NW_TOS }, | |
2500 | { OFPUTIL_A_SET_NW_ECN, OFPAT11_SET_NW_ECN }, | |
2501 | { OFPUTIL_A_SET_TP_SRC, OFPAT11_SET_TP_SRC }, | |
2502 | { OFPUTIL_A_SET_TP_DST, OFPAT11_SET_TP_DST }, | |
2503 | { OFPUTIL_A_COPY_TTL_OUT, OFPAT11_COPY_TTL_OUT }, | |
2504 | { OFPUTIL_A_COPY_TTL_IN, OFPAT11_COPY_TTL_IN }, | |
2505 | { OFPUTIL_A_SET_MPLS_LABEL, OFPAT11_SET_MPLS_LABEL }, | |
2506 | { OFPUTIL_A_SET_MPLS_TC, OFPAT11_SET_MPLS_TC }, | |
2507 | { OFPUTIL_A_SET_MPLS_TTL, OFPAT11_SET_MPLS_TTL }, | |
2508 | { OFPUTIL_A_DEC_MPLS_TTL, OFPAT11_DEC_MPLS_TTL }, | |
2509 | { OFPUTIL_A_PUSH_VLAN, OFPAT11_PUSH_VLAN }, | |
2510 | { OFPUTIL_A_POP_VLAN, OFPAT11_POP_VLAN }, | |
2511 | { OFPUTIL_A_PUSH_MPLS, OFPAT11_PUSH_MPLS }, | |
2512 | { OFPUTIL_A_POP_MPLS, OFPAT11_POP_MPLS }, | |
2513 | { OFPUTIL_A_SET_QUEUE, OFPAT11_SET_QUEUE }, | |
2514 | { OFPUTIL_A_GROUP, OFPAT11_GROUP }, | |
2515 | { OFPUTIL_A_SET_NW_TTL, OFPAT11_SET_NW_TTL }, | |
2516 | { OFPUTIL_A_DEC_NW_TTL, OFPAT11_DEC_NW_TTL }, | |
2517 | { 0, 0 }, | |
2518 | }; | |
2519 | ||
2520 | static enum ofputil_action_bitmap | |
2521 | decode_action_bits(ovs_be32 of_actions, | |
2522 | const struct ofputil_action_bit_translation *x) | |
2523 | { | |
2524 | enum ofputil_action_bitmap ofputil_actions; | |
2525 | ||
2526 | ofputil_actions = 0; | |
2527 | for (; x->ofputil_bit; x++) { | |
2528 | if (of_actions & htonl(1u << x->of_bit)) { | |
2529 | ofputil_actions |= x->ofputil_bit; | |
2530 | } | |
2531 | } | |
2532 | return ofputil_actions; | |
2533 | } | |
2534 | ||
2535 | /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an | |
2536 | * abstract representation in '*features'. Initializes '*b' to iterate over | |
2537 | * the OpenFlow port structures following 'osf' with later calls to | |
2be393ed | 2538 | * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an |
9e1fd49b BP |
2539 | * OFPERR_* value. */ |
2540 | enum ofperr | |
2541 | ofputil_decode_switch_features(const struct ofp_switch_features *osf, | |
2542 | struct ofputil_switch_features *features, | |
2543 | struct ofpbuf *b) | |
2544 | { | |
2545 | ofpbuf_use_const(b, osf, ntohs(osf->header.length)); | |
2546 | ofpbuf_pull(b, sizeof *osf); | |
9e1fd49b BP |
2547 | |
2548 | features->datapath_id = ntohll(osf->datapath_id); | |
2549 | features->n_buffers = ntohl(osf->n_buffers); | |
2550 | features->n_tables = osf->n_tables; | |
2551 | ||
2552 | features->capabilities = ntohl(osf->capabilities) & OFPC_COMMON; | |
9e1fd49b | 2553 | |
5b5a3a67 JP |
2554 | if (b->size % ofputil_get_phy_port_size(osf->header.version)) { |
2555 | return OFPERR_OFPBRC_BAD_LEN; | |
2556 | } | |
2557 | ||
2558 | if (osf->header.version == OFP10_VERSION) { | |
9e1fd49b BP |
2559 | if (osf->capabilities & htonl(OFPC10_STP)) { |
2560 | features->capabilities |= OFPUTIL_C_STP; | |
2561 | } | |
2562 | features->actions = decode_action_bits(osf->actions, of10_action_bits); | |
2563 | } else if (osf->header.version == OFP11_VERSION) { | |
9e1fd49b BP |
2564 | if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) { |
2565 | features->capabilities |= OFPUTIL_C_GROUP_STATS; | |
2566 | } | |
2567 | features->actions = decode_action_bits(osf->actions, of11_action_bits); | |
2568 | } else { | |
2569 | return OFPERR_OFPBRC_BAD_VERSION; | |
2570 | } | |
2571 | ||
2572 | return 0; | |
2573 | } | |
2574 | ||
347b7ac4 JP |
2575 | /* Returns true if the maximum number of ports are in 'osf'. */ |
2576 | static bool | |
2577 | max_ports_in_features(const struct ofp_switch_features *osf) | |
2578 | { | |
5b5a3a67 | 2579 | size_t pp_size = ofputil_get_phy_port_size(osf->header.version); |
347b7ac4 JP |
2580 | return ntohs(osf->header.length) + pp_size > UINT16_MAX; |
2581 | } | |
2582 | ||
2583 | /* Given a buffer 'b' that contains a Features Reply message, checks if | |
2584 | * it contains the maximum number of ports that will fit. If so, it | |
2585 | * returns true and removes the ports from the message. The caller | |
2586 | * should then send an OFPST_PORT_DESC stats request to get the ports, | |
2587 | * since the switch may have more ports than could be represented in the | |
2588 | * Features Reply. Otherwise, returns false. | |
2589 | */ | |
2590 | bool | |
2591 | ofputil_switch_features_ports_trunc(struct ofpbuf *b) | |
2592 | { | |
2593 | struct ofp_switch_features *osf = b->data; | |
2594 | ||
2595 | if (max_ports_in_features(osf)) { | |
2596 | /* Remove all the ports. */ | |
2597 | b->size = sizeof(*osf); | |
2598 | update_openflow_length(b); | |
2599 | ||
2600 | return true; | |
2601 | } | |
2602 | ||
2603 | return false; | |
2604 | } | |
2605 | ||
9e1fd49b BP |
2606 | static ovs_be32 |
2607 | encode_action_bits(enum ofputil_action_bitmap ofputil_actions, | |
2608 | const struct ofputil_action_bit_translation *x) | |
2609 | { | |
2610 | uint32_t of_actions; | |
2611 | ||
2612 | of_actions = 0; | |
2613 | for (; x->ofputil_bit; x++) { | |
2614 | if (ofputil_actions & x->ofputil_bit) { | |
2615 | of_actions |= 1 << x->of_bit; | |
2616 | } | |
2617 | } | |
2618 | return htonl(of_actions); | |
2619 | } | |
2620 | ||
2621 | /* Returns a buffer owned by the caller that encodes 'features' in the format | |
2622 | * required by 'protocol' with the given 'xid'. The caller should append port | |
2623 | * information to the buffer with subsequent calls to | |
2624 | * ofputil_put_switch_features_port(). */ | |
2625 | struct ofpbuf * | |
2626 | ofputil_encode_switch_features(const struct ofputil_switch_features *features, | |
2627 | enum ofputil_protocol protocol, ovs_be32 xid) | |
2628 | { | |
2629 | struct ofp_switch_features *osf; | |
2630 | struct ofpbuf *b; | |
2631 | ||
2632 | osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, xid, &b); | |
2633 | osf->header.version = ofputil_protocol_to_ofp_version(protocol); | |
2634 | osf->datapath_id = htonll(features->datapath_id); | |
2635 | osf->n_buffers = htonl(features->n_buffers); | |
2636 | osf->n_tables = features->n_tables; | |
2637 | ||
2638 | osf->capabilities = htonl(features->capabilities & OFPC_COMMON); | |
2639 | if (osf->header.version == OFP10_VERSION) { | |
2640 | if (features->capabilities & OFPUTIL_C_STP) { | |
2641 | osf->capabilities |= htonl(OFPC10_STP); | |
2642 | } | |
2643 | osf->actions = encode_action_bits(features->actions, of10_action_bits); | |
2644 | } else { | |
2645 | if (features->capabilities & OFPUTIL_C_GROUP_STATS) { | |
2646 | osf->capabilities |= htonl(OFPC11_GROUP_STATS); | |
2647 | } | |
2648 | osf->actions = encode_action_bits(features->actions, of11_action_bits); | |
2649 | } | |
2650 | ||
2651 | return b; | |
2652 | } | |
2653 | ||
2654 | /* Encodes 'pp' into the format required by the switch_features message already | |
2655 | * in 'b', which should have been returned by ofputil_encode_switch_features(), | |
2656 | * and appends the encoded version to 'b'. */ | |
2657 | void | |
2658 | ofputil_put_switch_features_port(const struct ofputil_phy_port *pp, | |
2659 | struct ofpbuf *b) | |
2660 | { | |
2661 | const struct ofp_switch_features *osf = b->data; | |
2662 | ||
2663 | ofputil_put_phy_port(osf->header.version, pp, b); | |
2664 | } | |
2665 | \f | |
2666 | /* ofputil_port_status */ | |
2667 | ||
2668 | /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form | |
2669 | * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
2670 | enum ofperr | |
2671 | ofputil_decode_port_status(const struct ofp_port_status *ops, | |
2672 | struct ofputil_port_status *ps) | |
2673 | { | |
2674 | struct ofpbuf b; | |
2675 | int retval; | |
2676 | ||
2677 | if (ops->reason != OFPPR_ADD && | |
2678 | ops->reason != OFPPR_DELETE && | |
2679 | ops->reason != OFPPR_MODIFY) { | |
2680 | return OFPERR_NXBRC_BAD_REASON; | |
2681 | } | |
2682 | ps->reason = ops->reason; | |
2683 | ||
2684 | ofpbuf_use_const(&b, ops, ntohs(ops->header.length)); | |
2685 | ofpbuf_pull(&b, sizeof *ops); | |
2686 | retval = ofputil_pull_phy_port(ops->header.version, &b, &ps->desc); | |
2687 | assert(retval != EOF); | |
2688 | return retval; | |
2689 | } | |
2690 | ||
2691 | /* Converts the abstract form of a "port status" message in '*ps' into an | |
2692 | * OpenFlow message suitable for 'protocol', and returns that encoded form in | |
2693 | * a buffer owned by the caller. */ | |
2694 | struct ofpbuf * | |
2695 | ofputil_encode_port_status(const struct ofputil_port_status *ps, | |
2696 | enum ofputil_protocol protocol) | |
2697 | { | |
2698 | struct ofp_port_status *ops; | |
2699 | struct ofpbuf *b; | |
2700 | ||
2701 | b = ofpbuf_new(sizeof *ops + sizeof(struct ofp11_port)); | |
2702 | ops = put_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, htonl(0), b); | |
2703 | ops->header.version = ofputil_protocol_to_ofp_version(protocol); | |
2704 | ops->reason = ps->reason; | |
2705 | ofputil_put_phy_port(ops->header.version, &ps->desc, b); | |
2706 | update_openflow_length(b); | |
2707 | return b; | |
2708 | } | |
2709 | \f | |
2710 | /* ofputil_port_mod */ | |
2711 | ||
2712 | /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in | |
2713 | * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
2714 | enum ofperr | |
2715 | ofputil_decode_port_mod(const struct ofp_header *oh, | |
2716 | struct ofputil_port_mod *pm) | |
2717 | { | |
2718 | if (oh->version == OFP10_VERSION) { | |
2719 | const struct ofp10_port_mod *opm = (const struct ofp10_port_mod *) oh; | |
2720 | ||
2721 | if (oh->length != htons(sizeof *opm)) { | |
2722 | return OFPERR_OFPBRC_BAD_LEN; | |
2723 | } | |
2724 | ||
2725 | pm->port_no = ntohs(opm->port_no); | |
2726 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
2727 | pm->config = ntohl(opm->config) & OFPPC10_ALL; | |
2728 | pm->mask = ntohl(opm->mask) & OFPPC10_ALL; | |
2729 | pm->advertise = netdev_port_features_from_ofp10(opm->advertise); | |
2730 | } else if (oh->version == OFP11_VERSION) { | |
2731 | const struct ofp11_port_mod *opm = (const struct ofp11_port_mod *) oh; | |
2732 | enum ofperr error; | |
2733 | ||
2734 | if (oh->length != htons(sizeof *opm)) { | |
2735 | return OFPERR_OFPBRC_BAD_LEN; | |
2736 | } | |
2737 | ||
2738 | error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no); | |
2739 | if (error) { | |
2740 | return error; | |
2741 | } | |
2742 | ||
2743 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
2744 | pm->config = ntohl(opm->config) & OFPPC11_ALL; | |
2745 | pm->mask = ntohl(opm->mask) & OFPPC11_ALL; | |
2746 | pm->advertise = netdev_port_features_from_ofp11(opm->advertise); | |
2747 | } else { | |
2748 | return OFPERR_OFPBRC_BAD_VERSION; | |
2749 | } | |
2750 | ||
2751 | pm->config &= pm->mask; | |
2752 | return 0; | |
2753 | } | |
2754 | ||
2755 | /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow | |
2756 | * message suitable for 'protocol', and returns that encoded form in a buffer | |
2757 | * owned by the caller. */ | |
2758 | struct ofpbuf * | |
2759 | ofputil_encode_port_mod(const struct ofputil_port_mod *pm, | |
2760 | enum ofputil_protocol protocol) | |
2761 | { | |
2762 | uint8_t ofp_version = ofputil_protocol_to_ofp_version(protocol); | |
2763 | struct ofpbuf *b; | |
2764 | ||
2765 | if (ofp_version == OFP10_VERSION) { | |
2766 | struct ofp10_port_mod *opm; | |
2767 | ||
2768 | opm = make_openflow(sizeof *opm, OFPT10_PORT_MOD, &b); | |
2769 | opm->port_no = htons(pm->port_no); | |
2770 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); | |
2771 | opm->config = htonl(pm->config & OFPPC10_ALL); | |
2772 | opm->mask = htonl(pm->mask & OFPPC10_ALL); | |
2773 | opm->advertise = netdev_port_features_to_ofp10(pm->advertise); | |
2774 | } else if (ofp_version == OFP11_VERSION) { | |
2775 | struct ofp11_port_mod *opm; | |
2776 | ||
2777 | opm = make_openflow(sizeof *opm, OFPT11_PORT_MOD, &b); | |
2778 | opm->port_no = htonl(pm->port_no); | |
2779 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); | |
2780 | opm->config = htonl(pm->config & OFPPC11_ALL); | |
2781 | opm->mask = htonl(pm->mask & OFPPC11_ALL); | |
2782 | opm->advertise = netdev_port_features_to_ofp11(pm->advertise); | |
2783 | } else { | |
2784 | NOT_REACHED(); | |
2785 | } | |
2786 | ||
2787 | return b; | |
2788 | } | |
2789 | ||
c6a93eb7 BP |
2790 | struct ofpbuf * |
2791 | ofputil_encode_packet_out(const struct ofputil_packet_out *po) | |
2792 | { | |
2793 | struct ofp_packet_out *opo; | |
2794 | size_t actions_len; | |
2795 | struct ofpbuf *msg; | |
2796 | size_t size; | |
2797 | ||
2798 | actions_len = po->n_actions * sizeof *po->actions; | |
2799 | size = sizeof *opo + actions_len; | |
2800 | if (po->buffer_id == UINT32_MAX) { | |
2801 | size += po->packet_len; | |
2802 | } | |
2803 | ||
2804 | msg = ofpbuf_new(size); | |
5293a2e1 | 2805 | opo = put_openflow(sizeof *opo, OFPT10_PACKET_OUT, msg); |
c6a93eb7 BP |
2806 | opo->buffer_id = htonl(po->buffer_id); |
2807 | opo->in_port = htons(po->in_port); | |
2808 | opo->actions_len = htons(actions_len); | |
2809 | ofpbuf_put(msg, po->actions, actions_len); | |
2810 | if (po->buffer_id == UINT32_MAX) { | |
2811 | ofpbuf_put(msg, po->packet, po->packet_len); | |
2812 | } | |
2813 | update_openflow_length(msg); | |
2814 | ||
2815 | return msg; | |
2816 | } | |
2817 | ||
d1e2cf21 BP |
2818 | /* Returns a string representing the message type of 'type'. The string is the |
2819 | * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics | |
2820 | * messages, the constant is followed by "request" or "reply", | |
2821 | * e.g. "OFPST_AGGREGATE reply". */ | |
2822 | const char * | |
2823 | ofputil_msg_type_name(const struct ofputil_msg_type *type) | |
2824 | { | |
2825 | return type->name; | |
2826 | } | |
2827 | \f | |
fa37b408 BP |
2828 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of |
2829 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
2830 | * an arbitrary transaction id. Allocated bytes beyond the header, if any, are | |
2831 | * zeroed. | |
2832 | * | |
2833 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
2834 | * needed. | |
2835 | * | |
2836 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
2837 | * message is later extended, the length should be updated with | |
2838 | * update_openflow_length() before sending. | |
2839 | * | |
2840 | * Returns the header. */ | |
2841 | void * | |
2842 | make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp) | |
2843 | { | |
2844 | *bufferp = ofpbuf_new(openflow_len); | |
2845 | return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp); | |
2846 | } | |
2847 | ||
0bd0c660 BP |
2848 | /* Similar to make_openflow() but creates a Nicira vendor extension message |
2849 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
2850 | void * | |
2851 | make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp) | |
2852 | { | |
2853 | return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp); | |
2854 | } | |
2855 | ||
fa37b408 BP |
2856 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of |
2857 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
2858 | * transaction id 'xid'. Allocated bytes beyond the header, if any, are | |
2859 | * zeroed. | |
2860 | * | |
2861 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
2862 | * needed. | |
2863 | * | |
2864 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
2865 | * message is later extended, the length should be updated with | |
2866 | * update_openflow_length() before sending. | |
2867 | * | |
2868 | * Returns the header. */ | |
2869 | void * | |
44381c1b | 2870 | make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid, |
fa37b408 BP |
2871 | struct ofpbuf **bufferp) |
2872 | { | |
2873 | *bufferp = ofpbuf_new(openflow_len); | |
2874 | return put_openflow_xid(openflow_len, type, xid, *bufferp); | |
2875 | } | |
2876 | ||
0bd0c660 BP |
2877 | /* Similar to make_openflow_xid() but creates a Nicira vendor extension message |
2878 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
2879 | void * | |
44381c1b | 2880 | make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid, |
0bd0c660 BP |
2881 | struct ofpbuf **bufferp) |
2882 | { | |
dfdfc8d4 BP |
2883 | *bufferp = ofpbuf_new(openflow_len); |
2884 | return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp); | |
0bd0c660 BP |
2885 | } |
2886 | ||
fa37b408 BP |
2887 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header |
2888 | * with the given 'type' and an arbitrary transaction id. Allocated bytes | |
2889 | * beyond the header, if any, are zeroed. | |
2890 | * | |
2891 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
2892 | * message is later extended, the length should be updated with | |
2893 | * update_openflow_length() before sending. | |
2894 | * | |
2895 | * Returns the header. */ | |
2896 | void * | |
2897 | put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer) | |
2898 | { | |
2899 | return put_openflow_xid(openflow_len, type, alloc_xid(), buffer); | |
2900 | } | |
2901 | ||
2902 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header | |
2903 | * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond | |
2904 | * the header, if any, are zeroed. | |
2905 | * | |
2906 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
2907 | * message is later extended, the length should be updated with | |
2908 | * update_openflow_length() before sending. | |
2909 | * | |
2910 | * Returns the header. */ | |
2911 | void * | |
44381c1b | 2912 | put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid, |
fa37b408 BP |
2913 | struct ofpbuf *buffer) |
2914 | { | |
2915 | struct ofp_header *oh; | |
2916 | ||
2917 | assert(openflow_len >= sizeof *oh); | |
2918 | assert(openflow_len <= UINT16_MAX); | |
2919 | ||
2920 | oh = ofpbuf_put_uninit(buffer, openflow_len); | |
87ea5e5e | 2921 | oh->version = OFP10_VERSION; |
fa37b408 BP |
2922 | oh->type = type; |
2923 | oh->length = htons(openflow_len); | |
2924 | oh->xid = xid; | |
2925 | memset(oh + 1, 0, openflow_len - sizeof *oh); | |
2926 | return oh; | |
2927 | } | |
2928 | ||
dfdfc8d4 BP |
2929 | /* Similar to put_openflow() but append a Nicira vendor extension message with |
2930 | * the specific 'subtype'. 'subtype' should be in host byte order. */ | |
2931 | void * | |
2932 | put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer) | |
2933 | { | |
2934 | return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer); | |
2935 | } | |
2936 | ||
2937 | /* Similar to put_openflow_xid() but append a Nicira vendor extension message | |
2938 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
2939 | void * | |
2940 | put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid, | |
2941 | struct ofpbuf *buffer) | |
2942 | { | |
2943 | struct nicira_header *nxh; | |
2944 | ||
2945 | nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer); | |
2946 | nxh->vendor = htonl(NX_VENDOR_ID); | |
2947 | nxh->subtype = htonl(subtype); | |
2948 | return nxh; | |
2949 | } | |
2950 | ||
fa37b408 BP |
2951 | /* Updates the 'length' field of the OpenFlow message in 'buffer' to |
2952 | * 'buffer->size'. */ | |
2953 | void | |
d295e8e9 | 2954 | update_openflow_length(struct ofpbuf *buffer) |
fa37b408 BP |
2955 | { |
2956 | struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh); | |
d295e8e9 | 2957 | oh->length = htons(buffer->size); |
fa37b408 BP |
2958 | } |
2959 | ||
63f2140a BP |
2960 | static void |
2961 | put_stats__(ovs_be32 xid, uint8_t ofp_type, | |
2962 | ovs_be16 ofpst_type, ovs_be32 nxst_subtype, | |
2963 | struct ofpbuf *msg) | |
2964 | { | |
2965 | if (ofpst_type == htons(OFPST_VENDOR)) { | |
2966 | struct nicira_stats_msg *nsm; | |
2967 | ||
2968 | nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg); | |
2969 | nsm->vsm.osm.type = ofpst_type; | |
2970 | nsm->vsm.vendor = htonl(NX_VENDOR_ID); | |
2971 | nsm->subtype = nxst_subtype; | |
2972 | } else { | |
2973 | struct ofp_stats_msg *osm; | |
2974 | ||
2975 | osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg); | |
2976 | osm->type = ofpst_type; | |
2977 | } | |
2978 | } | |
2979 | ||
2980 | /* Creates a statistics request message with total length 'openflow_len' | |
2981 | * (including all headers) and the given 'ofpst_type', and stores the buffer | |
2982 | * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR | |
2983 | * then 'nxst_subtype' is used as the Nicira vendor extension statistics | |
2984 | * subtype (otherwise 'nxst_subtype' is ignored). | |
2985 | * | |
2986 | * Initializes bytes following the headers to all-bits-zero. | |
2987 | * | |
2988 | * Returns the first byte of the new message. */ | |
dfdfc8d4 | 2989 | void * |
63f2140a BP |
2990 | ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type, |
2991 | uint32_t nxst_subtype, struct ofpbuf **bufferp) | |
dfdfc8d4 | 2992 | { |
63f2140a BP |
2993 | struct ofpbuf *msg; |
2994 | ||
2995 | msg = *bufferp = ofpbuf_new(openflow_len); | |
5293a2e1 | 2996 | put_stats__(alloc_xid(), OFPT10_STATS_REQUEST, |
63f2140a BP |
2997 | htons(ofpst_type), htonl(nxst_subtype), msg); |
2998 | ofpbuf_padto(msg, openflow_len); | |
2999 | ||
3000 | return msg->data; | |
3001 | } | |
3002 | ||
3003 | static void | |
3004 | put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg) | |
3005 | { | |
5293a2e1 BP |
3006 | assert(request->header.type == OFPT10_STATS_REQUEST || |
3007 | request->header.type == OFPT10_STATS_REPLY); | |
3008 | put_stats__(request->header.xid, OFPT10_STATS_REPLY, request->type, | |
63f2140a BP |
3009 | (request->type != htons(OFPST_VENDOR) |
3010 | ? htonl(0) | |
3011 | : ((const struct nicira_stats_msg *) request)->subtype), | |
3012 | msg); | |
3013 | } | |
3014 | ||
3015 | /* Creates a statistics reply message with total length 'openflow_len' | |
3016 | * (including all headers) and the same type (either a standard OpenFlow | |
3017 | * statistics type or a Nicira extension type and subtype) as 'request', and | |
3018 | * stores the buffer containing the new message in '*bufferp'. | |
3019 | * | |
3020 | * Initializes bytes following the headers to all-bits-zero. | |
3021 | * | |
3022 | * Returns the first byte of the new message. */ | |
3023 | void * | |
3024 | ofputil_make_stats_reply(size_t openflow_len, | |
3025 | const struct ofp_stats_msg *request, | |
3026 | struct ofpbuf **bufferp) | |
3027 | { | |
3028 | struct ofpbuf *msg; | |
3029 | ||
3030 | msg = *bufferp = ofpbuf_new(openflow_len); | |
3031 | put_stats_reply__(request, msg); | |
3032 | ofpbuf_padto(msg, openflow_len); | |
3033 | ||
3034 | return msg->data; | |
3035 | } | |
3036 | ||
3037 | /* Initializes 'replies' as a list of ofpbufs that will contain a series of | |
3038 | * replies to 'request', which should be an OpenFlow or Nicira extension | |
3039 | * statistics request. Initially 'replies' will have a single reply message | |
3040 | * that has only a header. The functions ofputil_reserve_stats_reply() and | |
3041 | * ofputil_append_stats_reply() may be used to add to the reply. */ | |
3042 | void | |
3043 | ofputil_start_stats_reply(const struct ofp_stats_msg *request, | |
3044 | struct list *replies) | |
3045 | { | |
3046 | struct ofpbuf *msg; | |
3047 | ||
3048 | msg = ofpbuf_new(1024); | |
3049 | put_stats_reply__(request, msg); | |
3050 | ||
3051 | list_init(replies); | |
3052 | list_push_back(replies, &msg->list_node); | |
3053 | } | |
3054 | ||
3055 | /* Prepares to append up to 'len' bytes to the series of statistics replies in | |
3056 | * 'replies', which should have been initialized with | |
3057 | * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of | |
3058 | * tailroom. (The 'len' bytes have not actually be allocated; the caller must | |
3059 | * do so with e.g. ofpbuf_put_uninit().) */ | |
3060 | struct ofpbuf * | |
3061 | ofputil_reserve_stats_reply(size_t len, struct list *replies) | |
3062 | { | |
3063 | struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); | |
3064 | struct ofp_stats_msg *osm = msg->data; | |
3065 | ||
3066 | if (msg->size + len <= UINT16_MAX) { | |
3067 | ofpbuf_prealloc_tailroom(msg, len); | |
3068 | } else { | |
3069 | osm->flags |= htons(OFPSF_REPLY_MORE); | |
3070 | ||
3071 | msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len)); | |
3072 | put_stats_reply__(osm, msg); | |
3073 | list_push_back(replies, &msg->list_node); | |
3074 | } | |
3075 | return msg; | |
dfdfc8d4 BP |
3076 | } |
3077 | ||
63f2140a BP |
3078 | /* Appends 'len' bytes to the series of statistics replies in 'replies', and |
3079 | * returns the first byte. */ | |
dfdfc8d4 | 3080 | void * |
63f2140a | 3081 | ofputil_append_stats_reply(size_t len, struct list *replies) |
dfdfc8d4 | 3082 | { |
63f2140a | 3083 | return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len); |
dfdfc8d4 BP |
3084 | } |
3085 | ||
03cd3493 | 3086 | /* Returns the first byte past the ofp_stats_msg header in 'oh'. */ |
d1e2cf21 BP |
3087 | const void * |
3088 | ofputil_stats_body(const struct ofp_header *oh) | |
3089 | { | |
5293a2e1 | 3090 | assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY); |
28c8bad1 | 3091 | return (const struct ofp_stats_msg *) oh + 1; |
d1e2cf21 BP |
3092 | } |
3093 | ||
03cd3493 | 3094 | /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */ |
d1e2cf21 BP |
3095 | size_t |
3096 | ofputil_stats_body_len(const struct ofp_header *oh) | |
3097 | { | |
5293a2e1 | 3098 | assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY); |
28c8bad1 | 3099 | return ntohs(oh->length) - sizeof(struct ofp_stats_msg); |
d1e2cf21 BP |
3100 | } |
3101 | ||
03cd3493 | 3102 | /* Returns the first byte past the nicira_stats_msg header in 'oh'. */ |
c6430da5 BP |
3103 | const void * |
3104 | ofputil_nxstats_body(const struct ofp_header *oh) | |
3105 | { | |
5293a2e1 | 3106 | assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY); |
c6430da5 BP |
3107 | return ((const struct nicira_stats_msg *) oh) + 1; |
3108 | } | |
3109 | ||
03cd3493 | 3110 | /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */ |
c6430da5 BP |
3111 | size_t |
3112 | ofputil_nxstats_body_len(const struct ofp_header *oh) | |
3113 | { | |
5293a2e1 | 3114 | assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY); |
c6430da5 BP |
3115 | return ntohs(oh->length) - sizeof(struct nicira_stats_msg); |
3116 | } | |
3117 | ||
fa37b408 | 3118 | struct ofpbuf * |
daa68e9f BP |
3119 | make_flow_mod(uint16_t command, const struct cls_rule *rule, |
3120 | size_t actions_len) | |
fa37b408 BP |
3121 | { |
3122 | struct ofp_flow_mod *ofm; | |
3123 | size_t size = sizeof *ofm + actions_len; | |
3124 | struct ofpbuf *out = ofpbuf_new(size); | |
3125 | ofm = ofpbuf_put_zeros(out, sizeof *ofm); | |
87ea5e5e | 3126 | ofm->header.version = OFP10_VERSION; |
5293a2e1 | 3127 | ofm->header.type = OFPT10_FLOW_MOD; |
fa37b408 BP |
3128 | ofm->header.length = htons(size); |
3129 | ofm->cookie = 0; | |
daa68e9f | 3130 | ofm->priority = htons(MIN(rule->priority, UINT16_MAX)); |
b78f6b77 | 3131 | ofputil_cls_rule_to_match(rule, &ofm->match); |
fa37b408 BP |
3132 | ofm->command = htons(command); |
3133 | return out; | |
3134 | } | |
3135 | ||
3136 | struct ofpbuf * | |
daa68e9f | 3137 | make_add_flow(const struct cls_rule *rule, uint32_t buffer_id, |
fa37b408 BP |
3138 | uint16_t idle_timeout, size_t actions_len) |
3139 | { | |
daa68e9f | 3140 | struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len); |
fa37b408 BP |
3141 | struct ofp_flow_mod *ofm = out->data; |
3142 | ofm->idle_timeout = htons(idle_timeout); | |
3143 | ofm->hard_timeout = htons(OFP_FLOW_PERMANENT); | |
3144 | ofm->buffer_id = htonl(buffer_id); | |
3145 | return out; | |
3146 | } | |
3147 | ||
3148 | struct ofpbuf * | |
daa68e9f | 3149 | make_del_flow(const struct cls_rule *rule) |
fa37b408 | 3150 | { |
daa68e9f | 3151 | struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0); |
fa37b408 BP |
3152 | struct ofp_flow_mod *ofm = out->data; |
3153 | ofm->out_port = htons(OFPP_NONE); | |
3154 | return out; | |
3155 | } | |
3156 | ||
3157 | struct ofpbuf * | |
daa68e9f | 3158 | make_add_simple_flow(const struct cls_rule *rule, |
fa37b408 BP |
3159 | uint32_t buffer_id, uint16_t out_port, |
3160 | uint16_t idle_timeout) | |
3161 | { | |
81f3cad4 BP |
3162 | if (out_port != OFPP_NONE) { |
3163 | struct ofp_action_output *oao; | |
3164 | struct ofpbuf *buffer; | |
3165 | ||
daa68e9f | 3166 | buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao); |
08f94c0e | 3167 | ofputil_put_OFPAT10_OUTPUT(buffer)->port = htons(out_port); |
81f3cad4 BP |
3168 | return buffer; |
3169 | } else { | |
daa68e9f | 3170 | return make_add_flow(rule, buffer_id, idle_timeout, 0); |
81f3cad4 | 3171 | } |
fa37b408 BP |
3172 | } |
3173 | ||
3174 | struct ofpbuf * | |
3175 | make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason, | |
3176 | const struct ofpbuf *payload, int max_send_len) | |
3177 | { | |
3178 | struct ofp_packet_in *opi; | |
3179 | struct ofpbuf *buf; | |
3180 | int send_len; | |
3181 | ||
3182 | send_len = MIN(max_send_len, payload->size); | |
3183 | buf = ofpbuf_new(sizeof *opi + send_len); | |
3184 | opi = put_openflow_xid(offsetof(struct ofp_packet_in, data), | |
3185 | OFPT_PACKET_IN, 0, buf); | |
3186 | opi->buffer_id = htonl(buffer_id); | |
3187 | opi->total_len = htons(payload->size); | |
3188 | opi->in_port = htons(in_port); | |
3189 | opi->reason = reason; | |
3190 | ofpbuf_put(buf, payload->data, send_len); | |
3191 | update_openflow_length(buf); | |
3192 | ||
3193 | return buf; | |
3194 | } | |
3195 | ||
fa37b408 BP |
3196 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ |
3197 | struct ofpbuf * | |
3198 | make_echo_request(void) | |
3199 | { | |
3200 | struct ofp_header *rq; | |
3201 | struct ofpbuf *out = ofpbuf_new(sizeof *rq); | |
3202 | rq = ofpbuf_put_uninit(out, sizeof *rq); | |
87ea5e5e | 3203 | rq->version = OFP10_VERSION; |
fa37b408 BP |
3204 | rq->type = OFPT_ECHO_REQUEST; |
3205 | rq->length = htons(sizeof *rq); | |
44381c1b | 3206 | rq->xid = htonl(0); |
fa37b408 BP |
3207 | return out; |
3208 | } | |
3209 | ||
3210 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
3211 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
3212 | struct ofpbuf * | |
3213 | make_echo_reply(const struct ofp_header *rq) | |
3214 | { | |
3215 | size_t size = ntohs(rq->length); | |
3216 | struct ofpbuf *out = ofpbuf_new(size); | |
3217 | struct ofp_header *reply = ofpbuf_put(out, rq, size); | |
3218 | reply->type = OFPT_ECHO_REPLY; | |
3219 | return out; | |
3220 | } | |
3221 | ||
efb80167 BP |
3222 | struct ofpbuf * |
3223 | ofputil_encode_barrier_request(void) | |
3224 | { | |
3225 | struct ofpbuf *msg; | |
3226 | ||
5293a2e1 | 3227 | make_openflow(sizeof(struct ofp_header), OFPT10_BARRIER_REQUEST, &msg); |
efb80167 BP |
3228 | return msg; |
3229 | } | |
3230 | ||
7257b535 BP |
3231 | const char * |
3232 | ofputil_frag_handling_to_string(enum ofp_config_flags flags) | |
3233 | { | |
3234 | switch (flags & OFPC_FRAG_MASK) { | |
3235 | case OFPC_FRAG_NORMAL: return "normal"; | |
3236 | case OFPC_FRAG_DROP: return "drop"; | |
3237 | case OFPC_FRAG_REASM: return "reassemble"; | |
3238 | case OFPC_FRAG_NX_MATCH: return "nx-match"; | |
3239 | } | |
3240 | ||
3241 | NOT_REACHED(); | |
3242 | } | |
3243 | ||
3244 | bool | |
3245 | ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags) | |
3246 | { | |
3247 | if (!strcasecmp(s, "normal")) { | |
3248 | *flags = OFPC_FRAG_NORMAL; | |
3249 | } else if (!strcasecmp(s, "drop")) { | |
3250 | *flags = OFPC_FRAG_DROP; | |
3251 | } else if (!strcasecmp(s, "reassemble")) { | |
3252 | *flags = OFPC_FRAG_REASM; | |
3253 | } else if (!strcasecmp(s, "nx-match")) { | |
3254 | *flags = OFPC_FRAG_NX_MATCH; | |
3255 | } else { | |
3256 | return false; | |
3257 | } | |
3258 | return true; | |
3259 | } | |
3260 | ||
7b7503ea BP |
3261 | /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0 |
3262 | * port number and stores the latter in '*ofp10_port', for the purpose of | |
3263 | * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful, | |
3264 | * otherwise an OFPERR_* number. | |
3265 | * | |
3266 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
3267 | enum ofperr | |
3268 | ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port) | |
3269 | { | |
3270 | uint32_t ofp11_port_h = ntohl(ofp11_port); | |
3271 | ||
3272 | if (ofp11_port_h < OFPP_MAX) { | |
3273 | *ofp10_port = ofp11_port_h; | |
3274 | return 0; | |
3275 | } else if (ofp11_port_h >= OFPP11_MAX) { | |
3276 | *ofp10_port = ofp11_port_h - OFPP11_OFFSET; | |
3277 | return 0; | |
3278 | } else { | |
3279 | VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported " | |
3280 | "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32, | |
3281 | ofp11_port_h, OFPP_MAX - 1, | |
3282 | (uint32_t) OFPP11_MAX, UINT32_MAX); | |
3283 | return OFPERR_OFPBAC_BAD_OUT_PORT; | |
3284 | } | |
3285 | } | |
3286 | ||
3287 | /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port | |
3288 | * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages. | |
3289 | * | |
3290 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
3291 | ovs_be32 | |
3292 | ofputil_port_to_ofp11(uint16_t ofp10_port) | |
3293 | { | |
3294 | return htonl(ofp10_port < OFPP_MAX | |
3295 | ? ofp10_port | |
3296 | : ofp10_port + OFPP11_OFFSET); | |
3297 | } | |
3298 | ||
08f94c0e | 3299 | /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given |
c1c9c9c4 | 3300 | * that the switch will never have more than 'max_ports' ports. Returns 0 if |
90bf1e07 BP |
3301 | * 'port' is valid, otherwise an OpenFlow return code. */ |
3302 | enum ofperr | |
77410139 | 3303 | ofputil_check_output_port(uint16_t port, int max_ports) |
fa37b408 BP |
3304 | { |
3305 | switch (port) { | |
3306 | case OFPP_IN_PORT: | |
3307 | case OFPP_TABLE: | |
3308 | case OFPP_NORMAL: | |
3309 | case OFPP_FLOOD: | |
3310 | case OFPP_ALL: | |
3311 | case OFPP_CONTROLLER: | |
0d193212 | 3312 | case OFPP_NONE: |
fa37b408 BP |
3313 | case OFPP_LOCAL: |
3314 | return 0; | |
3315 | ||
3316 | default: | |
c1c9c9c4 | 3317 | if (port < max_ports) { |
fa37b408 BP |
3318 | return 0; |
3319 | } | |
90bf1e07 | 3320 | return OFPERR_OFPBAC_BAD_OUT_PORT; |
fa37b408 BP |
3321 | } |
3322 | } | |
3323 | ||
39dc9082 BP |
3324 | #define OFPUTIL_NAMED_PORTS \ |
3325 | OFPUTIL_NAMED_PORT(IN_PORT) \ | |
3326 | OFPUTIL_NAMED_PORT(TABLE) \ | |
3327 | OFPUTIL_NAMED_PORT(NORMAL) \ | |
3328 | OFPUTIL_NAMED_PORT(FLOOD) \ | |
3329 | OFPUTIL_NAMED_PORT(ALL) \ | |
3330 | OFPUTIL_NAMED_PORT(CONTROLLER) \ | |
3331 | OFPUTIL_NAMED_PORT(LOCAL) \ | |
3332 | OFPUTIL_NAMED_PORT(NONE) | |
3333 | ||
3334 | /* Checks whether 's' is the string representation of an OpenFlow port number, | |
3335 | * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the | |
3336 | * number in '*port' and returns true. Otherwise, returns false. */ | |
3337 | bool | |
3338 | ofputil_port_from_string(const char *name, uint16_t *port) | |
3339 | { | |
3340 | struct pair { | |
3341 | const char *name; | |
3342 | uint16_t value; | |
3343 | }; | |
3344 | static const struct pair pairs[] = { | |
3345 | #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME}, | |
3346 | OFPUTIL_NAMED_PORTS | |
3347 | #undef OFPUTIL_NAMED_PORT | |
3348 | }; | |
3349 | static const int n_pairs = ARRAY_SIZE(pairs); | |
3350 | int i; | |
3351 | ||
3352 | if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) { | |
3353 | *port = i; | |
3354 | return true; | |
3355 | } | |
3356 | ||
3357 | for (i = 0; i < n_pairs; i++) { | |
3358 | if (!strcasecmp(name, pairs[i].name)) { | |
3359 | *port = pairs[i].value; | |
3360 | return true; | |
3361 | } | |
3362 | } | |
3363 | return false; | |
3364 | } | |
3365 | ||
3366 | /* Appends to 's' a string representation of the OpenFlow port number 'port'. | |
3367 | * Most ports' string representation is just the port number, but for special | |
3368 | * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */ | |
3369 | void | |
3370 | ofputil_format_port(uint16_t port, struct ds *s) | |
3371 | { | |
3372 | const char *name; | |
3373 | ||
3374 | switch (port) { | |
3375 | #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break; | |
3376 | OFPUTIL_NAMED_PORTS | |
3377 | #undef OFPUTIL_NAMED_PORT | |
3378 | ||
3379 | default: | |
3380 | ds_put_format(s, "%"PRIu16, port); | |
3381 | return; | |
3382 | } | |
3383 | ds_put_cstr(s, name); | |
3384 | } | |
3385 | ||
2be393ed JP |
3386 | /* Given a buffer 'b' that contains an array of OpenFlow ports of type |
3387 | * 'ofp_version', tries to pull the first element from the array. If | |
3388 | * successful, initializes '*pp' with an abstract representation of the | |
3389 | * port and returns 0. If no ports remain to be decoded, returns EOF. | |
3390 | * On an error, returns a positive OFPERR_* value. */ | |
3391 | int | |
3392 | ofputil_pull_phy_port(uint8_t ofp_version, struct ofpbuf *b, | |
3393 | struct ofputil_phy_port *pp) | |
3394 | { | |
3395 | if (ofp_version == OFP10_VERSION) { | |
3396 | const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp); | |
3397 | return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF; | |
3398 | } else { | |
3399 | const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op); | |
3400 | return op ? ofputil_decode_ofp11_port(pp, op) : EOF; | |
3401 | } | |
3402 | } | |
3403 | ||
3404 | /* Given a buffer 'b' that contains an array of OpenFlow ports of type | |
3405 | * 'ofp_version', returns the number of elements. */ | |
3406 | size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b) | |
3407 | { | |
5b5a3a67 | 3408 | return b->size / ofputil_get_phy_port_size(ofp_version); |
2be393ed JP |
3409 | } |
3410 | ||
90bf1e07 | 3411 | static enum ofperr |
29901626 BP |
3412 | check_resubmit_table(const struct nx_action_resubmit *nar) |
3413 | { | |
3414 | if (nar->pad[0] || nar->pad[1] || nar->pad[2]) { | |
90bf1e07 | 3415 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
29901626 BP |
3416 | } |
3417 | return 0; | |
3418 | } | |
3419 | ||
90bf1e07 | 3420 | static enum ofperr |
f694937d EJ |
3421 | check_output_reg(const struct nx_action_output_reg *naor, |
3422 | const struct flow *flow) | |
3423 | { | |
816fd533 | 3424 | struct mf_subfield src; |
f694937d EJ |
3425 | size_t i; |
3426 | ||
3427 | for (i = 0; i < sizeof naor->zero; i++) { | |
3428 | if (naor->zero[i]) { | |
90bf1e07 | 3429 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
f694937d EJ |
3430 | } |
3431 | } | |
3432 | ||
816fd533 BP |
3433 | nxm_decode(&src, naor->src, naor->ofs_nbits); |
3434 | return mf_check_src(&src, flow); | |
f694937d EJ |
3435 | } |
3436 | ||
90bf1e07 | 3437 | enum ofperr |
38f2e360 BP |
3438 | validate_actions(const union ofp_action *actions, size_t n_actions, |
3439 | const struct flow *flow, int max_ports) | |
c1c9c9c4 | 3440 | { |
38f2e360 BP |
3441 | const union ofp_action *a; |
3442 | size_t left; | |
c1c9c9c4 | 3443 | |
38f2e360 | 3444 | OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) { |
90bf1e07 | 3445 | enum ofperr error; |
38f2e360 | 3446 | uint16_t port; |
38f2e360 | 3447 | int code; |
c1c9c9c4 | 3448 | |
38f2e360 BP |
3449 | code = ofputil_decode_action(a); |
3450 | if (code < 0) { | |
38f2e360 | 3451 | error = -code; |
38f2e360 BP |
3452 | VLOG_WARN_RL(&bad_ofmsg_rl, |
3453 | "action decoding error at offset %td (%s)", | |
90bf1e07 | 3454 | (a - actions) * sizeof *a, ofperr_get_name(error)); |
fa37b408 | 3455 | |
38f2e360 BP |
3456 | return error; |
3457 | } | |
fa37b408 | 3458 | |
38f2e360 BP |
3459 | error = 0; |
3460 | switch ((enum ofputil_action_code) code) { | |
08f94c0e | 3461 | case OFPUTIL_OFPAT10_OUTPUT: |
77410139 EJ |
3462 | error = ofputil_check_output_port(ntohs(a->output.port), |
3463 | max_ports); | |
38f2e360 | 3464 | break; |
e41a9130 | 3465 | |
08f94c0e | 3466 | case OFPUTIL_OFPAT10_SET_VLAN_VID: |
38f2e360 | 3467 | if (a->vlan_vid.vlan_vid & ~htons(0xfff)) { |
90bf1e07 | 3468 | error = OFPERR_OFPBAC_BAD_ARGUMENT; |
38f2e360 BP |
3469 | } |
3470 | break; | |
96fc46e8 | 3471 | |
08f94c0e | 3472 | case OFPUTIL_OFPAT10_SET_VLAN_PCP: |
38f2e360 | 3473 | if (a->vlan_pcp.vlan_pcp & ~7) { |
90bf1e07 | 3474 | error = OFPERR_OFPBAC_BAD_ARGUMENT; |
38f2e360 BP |
3475 | } |
3476 | break; | |
96fc46e8 | 3477 | |
08f94c0e | 3478 | case OFPUTIL_OFPAT10_ENQUEUE: |
38f2e360 | 3479 | port = ntohs(((const struct ofp_action_enqueue *) a)->port); |
82172632 EJ |
3480 | if (port >= max_ports && port != OFPP_IN_PORT |
3481 | && port != OFPP_LOCAL) { | |
90bf1e07 | 3482 | error = OFPERR_OFPBAC_BAD_OUT_PORT; |
38f2e360 BP |
3483 | } |
3484 | break; | |
96fc46e8 | 3485 | |
38f2e360 BP |
3486 | case OFPUTIL_NXAST_REG_MOVE: |
3487 | error = nxm_check_reg_move((const struct nx_action_reg_move *) a, | |
3488 | flow); | |
3489 | break; | |
96fc46e8 | 3490 | |
38f2e360 BP |
3491 | case OFPUTIL_NXAST_REG_LOAD: |
3492 | error = nxm_check_reg_load((const struct nx_action_reg_load *) a, | |
3493 | flow); | |
3494 | break; | |
b9298d3f | 3495 | |
38f2e360 | 3496 | case OFPUTIL_NXAST_MULTIPATH: |
43edca57 EJ |
3497 | error = multipath_check((const struct nx_action_multipath *) a, |
3498 | flow); | |
38f2e360 BP |
3499 | break; |
3500 | ||
3501 | case OFPUTIL_NXAST_AUTOPATH: | |
43edca57 EJ |
3502 | error = autopath_check((const struct nx_action_autopath *) a, |
3503 | flow); | |
38f2e360 BP |
3504 | break; |
3505 | ||
daff3353 | 3506 | case OFPUTIL_NXAST_BUNDLE: |
a368bb53 | 3507 | case OFPUTIL_NXAST_BUNDLE_LOAD: |
daff3353 | 3508 | error = bundle_check((const struct nx_action_bundle *) a, |
a368bb53 | 3509 | max_ports, flow); |
daff3353 EJ |
3510 | break; |
3511 | ||
f694937d EJ |
3512 | case OFPUTIL_NXAST_OUTPUT_REG: |
3513 | error = check_output_reg((const struct nx_action_output_reg *) a, | |
3514 | flow); | |
3515 | break; | |
3516 | ||
29901626 BP |
3517 | case OFPUTIL_NXAST_RESUBMIT_TABLE: |
3518 | error = check_resubmit_table( | |
3519 | (const struct nx_action_resubmit *) a); | |
3520 | break; | |
3521 | ||
75a75043 BP |
3522 | case OFPUTIL_NXAST_LEARN: |
3523 | error = learn_check((const struct nx_action_learn *) a, flow); | |
3524 | break; | |
3525 | ||
a7349929 BP |
3526 | case OFPUTIL_NXAST_CONTROLLER: |
3527 | if (((const struct nx_action_controller *) a)->zero) { | |
3528 | error = OFPERR_NXBAC_MUST_BE_ZERO; | |
3529 | } | |
3530 | break; | |
3531 | ||
08f94c0e BP |
3532 | case OFPUTIL_OFPAT10_STRIP_VLAN: |
3533 | case OFPUTIL_OFPAT10_SET_NW_SRC: | |
3534 | case OFPUTIL_OFPAT10_SET_NW_DST: | |
3535 | case OFPUTIL_OFPAT10_SET_NW_TOS: | |
3536 | case OFPUTIL_OFPAT10_SET_TP_SRC: | |
3537 | case OFPUTIL_OFPAT10_SET_TP_DST: | |
3538 | case OFPUTIL_OFPAT10_SET_DL_SRC: | |
3539 | case OFPUTIL_OFPAT10_SET_DL_DST: | |
38f2e360 BP |
3540 | case OFPUTIL_NXAST_RESUBMIT: |
3541 | case OFPUTIL_NXAST_SET_TUNNEL: | |
3542 | case OFPUTIL_NXAST_SET_QUEUE: | |
3543 | case OFPUTIL_NXAST_POP_QUEUE: | |
3544 | case OFPUTIL_NXAST_NOTE: | |
3545 | case OFPUTIL_NXAST_SET_TUNNEL64: | |
848e8809 | 3546 | case OFPUTIL_NXAST_EXIT: |
f0fd1a17 | 3547 | case OFPUTIL_NXAST_DEC_TTL: |
0e553d9c | 3548 | case OFPUTIL_NXAST_FIN_TIMEOUT: |
38f2e360 | 3549 | break; |
53ddd40a | 3550 | } |
53ddd40a | 3551 | |
3b6a2571 | 3552 | if (error) { |
38f2e360 | 3553 | VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)", |
90bf1e07 | 3554 | (a - actions) * sizeof *a, ofperr_get_name(error)); |
3b6a2571 EJ |
3555 | return error; |
3556 | } | |
fa37b408 | 3557 | } |
38f2e360 BP |
3558 | if (left) { |
3559 | VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu", | |
3560 | (n_actions - left) * sizeof *a); | |
90bf1e07 | 3561 | return OFPERR_OFPBAC_BAD_LEN; |
38f2e360 BP |
3562 | } |
3563 | return 0; | |
fa37b408 BP |
3564 | } |
3565 | ||
51cb6e0c BP |
3566 | struct ofputil_action { |
3567 | int code; | |
38f2e360 BP |
3568 | unsigned int min_len; |
3569 | unsigned int max_len; | |
3570 | }; | |
27bcf966 | 3571 | |
51cb6e0c | 3572 | static const struct ofputil_action action_bad_type |
90bf1e07 | 3573 | = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX }; |
51cb6e0c | 3574 | static const struct ofputil_action action_bad_len |
90bf1e07 | 3575 | = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX }; |
51cb6e0c | 3576 | static const struct ofputil_action action_bad_vendor |
90bf1e07 | 3577 | = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX }; |
27bcf966 | 3578 | |
51cb6e0c | 3579 | static const struct ofputil_action * |
38f2e360 BP |
3580 | ofputil_decode_ofpat_action(const union ofp_action *a) |
3581 | { | |
08f94c0e | 3582 | enum ofp10_action_type type = ntohs(a->type); |
fa37b408 | 3583 | |
51cb6e0c | 3584 | switch (type) { |
08f94c0e | 3585 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ |
51cb6e0c BP |
3586 | case ENUM: { \ |
3587 | static const struct ofputil_action action = { \ | |
e23ae585 BP |
3588 | OFPUTIL_##ENUM, \ |
3589 | sizeof(struct STRUCT), \ | |
3590 | sizeof(struct STRUCT) \ | |
51cb6e0c BP |
3591 | }; \ |
3592 | return &action; \ | |
3593 | } | |
e23ae585 | 3594 | #include "ofp-util.def" |
51cb6e0c | 3595 | |
08f94c0e | 3596 | case OFPAT10_VENDOR: |
51cb6e0c BP |
3597 | default: |
3598 | return &action_bad_type; | |
38f2e360 BP |
3599 | } |
3600 | } | |
fa37b408 | 3601 | |
51cb6e0c | 3602 | static const struct ofputil_action * |
38f2e360 BP |
3603 | ofputil_decode_nxast_action(const union ofp_action *a) |
3604 | { | |
51cb6e0c BP |
3605 | const struct nx_action_header *nah = (const struct nx_action_header *) a; |
3606 | enum nx_action_subtype subtype = ntohs(nah->subtype); | |
3607 | ||
3608 | switch (subtype) { | |
e23ae585 BP |
3609 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ |
3610 | case ENUM: { \ | |
3611 | static const struct ofputil_action action = { \ | |
3612 | OFPUTIL_##ENUM, \ | |
3613 | sizeof(struct STRUCT), \ | |
3614 | EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \ | |
3615 | }; \ | |
3616 | return &action; \ | |
38f2e360 | 3617 | } |
e23ae585 | 3618 | #include "ofp-util.def" |
51cb6e0c BP |
3619 | |
3620 | case NXAST_SNAT__OBSOLETE: | |
3621 | case NXAST_DROP_SPOOFED_ARP__OBSOLETE: | |
3622 | default: | |
3623 | return &action_bad_type; | |
fa37b408 BP |
3624 | } |
3625 | } | |
3626 | ||
08f94c0e | 3627 | /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT10_* or |
90bf1e07 BP |
3628 | * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error |
3629 | * code. | |
38f2e360 BP |
3630 | * |
3631 | * The caller must have already verified that 'a''s length is correct (that is, | |
3632 | * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no | |
3633 | * longer than the amount of space allocated to 'a'). | |
3634 | * | |
3635 | * This function verifies that 'a''s length is correct for the type of action | |
3636 | * that it represents. */ | |
fa37b408 | 3637 | int |
38f2e360 | 3638 | ofputil_decode_action(const union ofp_action *a) |
fa37b408 | 3639 | { |
51cb6e0c BP |
3640 | const struct ofputil_action *action; |
3641 | uint16_t len = ntohs(a->header.len); | |
3642 | ||
08f94c0e | 3643 | if (a->type != htons(OFPAT10_VENDOR)) { |
51cb6e0c | 3644 | action = ofputil_decode_ofpat_action(a); |
38f2e360 | 3645 | } else { |
51cb6e0c BP |
3646 | switch (ntohl(a->vendor.vendor)) { |
3647 | case NX_VENDOR_ID: | |
3648 | if (len < sizeof(struct nx_action_header)) { | |
90bf1e07 | 3649 | return -OFPERR_OFPBAC_BAD_LEN; |
51cb6e0c BP |
3650 | } |
3651 | action = ofputil_decode_nxast_action(a); | |
3652 | break; | |
3653 | default: | |
3654 | action = &action_bad_vendor; | |
3655 | break; | |
3656 | } | |
38f2e360 | 3657 | } |
51cb6e0c BP |
3658 | |
3659 | return (len >= action->min_len && len <= action->max_len | |
3660 | ? action->code | |
90bf1e07 | 3661 | : -OFPERR_OFPBAC_BAD_LEN); |
38f2e360 | 3662 | } |
fa37b408 | 3663 | |
08f94c0e | 3664 | /* Parses 'a' and returns its type as an OFPUTIL_OFPAT10_* or OFPUTIL_NXAST_* |
38f2e360 BP |
3665 | * constant. The caller must have already validated that 'a' is a valid action |
3666 | * understood by Open vSwitch (e.g. by a previous successful call to | |
3667 | * ofputil_decode_action()). */ | |
3668 | enum ofputil_action_code | |
3669 | ofputil_decode_action_unsafe(const union ofp_action *a) | |
3670 | { | |
51cb6e0c BP |
3671 | const struct ofputil_action *action; |
3672 | ||
08f94c0e | 3673 | if (a->type != htons(OFPAT10_VENDOR)) { |
51cb6e0c | 3674 | action = ofputil_decode_ofpat_action(a); |
38f2e360 | 3675 | } else { |
51cb6e0c | 3676 | action = ofputil_decode_nxast_action(a); |
fa37b408 | 3677 | } |
51cb6e0c BP |
3678 | |
3679 | return action->code; | |
fa37b408 BP |
3680 | } |
3681 | ||
e23ae585 | 3682 | /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if |
08f94c0e | 3683 | * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if |
e23ae585 BP |
3684 | * 'name' is not the name of any action. |
3685 | * | |
3686 | * ofp-util.def lists the mapping from names to action. */ | |
3687 | int | |
3688 | ofputil_action_code_from_name(const char *name) | |
3689 | { | |
3690 | static const char *names[OFPUTIL_N_ACTIONS] = { | |
08f94c0e | 3691 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME, |
e23ae585 BP |
3692 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, |
3693 | #include "ofp-util.def" | |
3694 | }; | |
3695 | ||
3696 | const char **p; | |
3697 | ||
3698 | for (p = names; p < &names[ARRAY_SIZE(names)]; p++) { | |
3699 | if (*p && !strcasecmp(name, *p)) { | |
3700 | return p - names; | |
3701 | } | |
3702 | } | |
3703 | return -1; | |
3704 | } | |
3705 | ||
93996add BP |
3706 | /* Appends an action of the type specified by 'code' to 'buf' and returns the |
3707 | * action. Initializes the parts of 'action' that identify it as having type | |
3708 | * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that | |
3709 | * have variable length, the length used and cleared is that of struct | |
3710 | * <STRUCT>. */ | |
3711 | void * | |
3712 | ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf) | |
3713 | { | |
3714 | switch (code) { | |
08f94c0e | 3715 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ |
93996add BP |
3716 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); |
3717 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
3718 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
3719 | #include "ofp-util.def" | |
3720 | } | |
3721 | NOT_REACHED(); | |
3722 | } | |
3723 | ||
08f94c0e | 3724 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ |
93996add BP |
3725 | void \ |
3726 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
3727 | { \ | |
3728 | memset(s, 0, sizeof *s); \ | |
3729 | s->type = htons(ENUM); \ | |
3730 | s->len = htons(sizeof *s); \ | |
3731 | } \ | |
3732 | \ | |
3733 | struct STRUCT * \ | |
3734 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
3735 | { \ | |
3736 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
3737 | ofputil_init_##ENUM(s); \ | |
3738 | return s; \ | |
3739 | } | |
3740 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
3741 | void \ | |
3742 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
3743 | { \ | |
3744 | memset(s, 0, sizeof *s); \ | |
08f94c0e | 3745 | s->type = htons(OFPAT10_VENDOR); \ |
93996add BP |
3746 | s->len = htons(sizeof *s); \ |
3747 | s->vendor = htonl(NX_VENDOR_ID); \ | |
3748 | s->subtype = htons(ENUM); \ | |
3749 | } \ | |
3750 | \ | |
3751 | struct STRUCT * \ | |
3752 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
3753 | { \ | |
3754 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
3755 | ofputil_init_##ENUM(s); \ | |
3756 | return s; \ | |
3757 | } | |
3758 | #include "ofp-util.def" | |
3759 | ||
dbba996b | 3760 | /* Returns true if 'action' outputs to 'port', false otherwise. */ |
c1c9c9c4 | 3761 | bool |
dbba996b | 3762 | action_outputs_to_port(const union ofp_action *action, ovs_be16 port) |
c1c9c9c4 | 3763 | { |
145fd580 BP |
3764 | switch (ofputil_decode_action(action)) { |
3765 | case OFPUTIL_OFPAT10_OUTPUT: | |
c1c9c9c4 | 3766 | return action->output.port == port; |
145fd580 | 3767 | case OFPUTIL_OFPAT10_ENQUEUE: |
c1c9c9c4 | 3768 | return ((const struct ofp_action_enqueue *) action)->port == port; |
145fd580 BP |
3769 | case OFPUTIL_NXAST_CONTROLLER: |
3770 | return port == htons(OFPP_CONTROLLER); | |
c1c9c9c4 BP |
3771 | default: |
3772 | return false; | |
3773 | } | |
3774 | } | |
3775 | ||
b459a924 BP |
3776 | /* "Normalizes" the wildcards in 'rule'. That means: |
3777 | * | |
3778 | * 1. If the type of level N is known, then only the valid fields for that | |
3779 | * level may be specified. For example, ARP does not have a TOS field, | |
3780 | * so nw_tos must be wildcarded if 'rule' specifies an ARP flow. | |
3781 | * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and | |
3782 | * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an | |
3783 | * IPv4 flow. | |
3784 | * | |
3785 | * 2. If the type of level N is not known (or not understood by Open | |
3786 | * vSwitch), then no fields at all for that level may be specified. For | |
3787 | * example, Open vSwitch does not understand SCTP, an L4 protocol, so the | |
3788 | * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an | |
3789 | * SCTP flow. | |
27527aa0 | 3790 | */ |
b459a924 | 3791 | void |
27527aa0 | 3792 | ofputil_normalize_rule(struct cls_rule *rule) |
b459a924 BP |
3793 | { |
3794 | enum { | |
3795 | MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */ | |
3796 | MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */ | |
3797 | MAY_NW_PROTO = 1 << 2, /* nw_proto */ | |
a61680c6 | 3798 | MAY_IPVx = 1 << 3, /* tos, frag, ttl */ |
b459a924 BP |
3799 | MAY_ARP_SHA = 1 << 4, /* arp_sha */ |
3800 | MAY_ARP_THA = 1 << 5, /* arp_tha */ | |
d78477ec | 3801 | MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */ |
b459a924 BP |
3802 | MAY_ND_TARGET = 1 << 7 /* nd_target */ |
3803 | } may_match; | |
3804 | ||
3805 | struct flow_wildcards wc; | |
3806 | ||
3807 | /* Figure out what fields may be matched. */ | |
3808 | if (rule->flow.dl_type == htons(ETH_TYPE_IP)) { | |
9e44d715 | 3809 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR; |
b459a924 BP |
3810 | if (rule->flow.nw_proto == IPPROTO_TCP || |
3811 | rule->flow.nw_proto == IPPROTO_UDP || | |
3812 | rule->flow.nw_proto == IPPROTO_ICMP) { | |
3813 | may_match |= MAY_TP_ADDR; | |
3814 | } | |
27527aa0 | 3815 | } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) { |
d78477ec | 3816 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6; |
b459a924 BP |
3817 | if (rule->flow.nw_proto == IPPROTO_TCP || |
3818 | rule->flow.nw_proto == IPPROTO_UDP) { | |
3819 | may_match |= MAY_TP_ADDR; | |
3820 | } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) { | |
3821 | may_match |= MAY_TP_ADDR; | |
3822 | if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) { | |
3823 | may_match |= MAY_ND_TARGET | MAY_ARP_SHA; | |
3824 | } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
3825 | may_match |= MAY_ND_TARGET | MAY_ARP_THA; | |
3826 | } | |
3827 | } | |
3828 | } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) { | |
27527aa0 | 3829 | may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA; |
1c0b7503 | 3830 | } else { |
b459a924 BP |
3831 | may_match = 0; |
3832 | } | |
3833 | ||
3834 | /* Clear the fields that may not be matched. */ | |
3835 | wc = rule->wc; | |
3836 | if (!(may_match & MAY_NW_ADDR)) { | |
3837 | wc.nw_src_mask = wc.nw_dst_mask = htonl(0); | |
3838 | } | |
3839 | if (!(may_match & MAY_TP_ADDR)) { | |
73f33563 | 3840 | wc.tp_src_mask = wc.tp_dst_mask = htons(0); |
b459a924 BP |
3841 | } |
3842 | if (!(may_match & MAY_NW_PROTO)) { | |
3843 | wc.wildcards |= FWW_NW_PROTO; | |
3844 | } | |
9e44d715 | 3845 | if (!(may_match & MAY_IPVx)) { |
2486e66a JP |
3846 | wc.wildcards |= FWW_NW_DSCP; |
3847 | wc.wildcards |= FWW_NW_ECN; | |
a61680c6 | 3848 | wc.wildcards |= FWW_NW_TTL; |
b459a924 BP |
3849 | } |
3850 | if (!(may_match & MAY_ARP_SHA)) { | |
3851 | wc.wildcards |= FWW_ARP_SHA; | |
3852 | } | |
3853 | if (!(may_match & MAY_ARP_THA)) { | |
3854 | wc.wildcards |= FWW_ARP_THA; | |
3855 | } | |
d78477ec | 3856 | if (!(may_match & MAY_IPV6)) { |
b459a924 | 3857 | wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any; |
fa8223b7 | 3858 | wc.wildcards |= FWW_IPV6_LABEL; |
b459a924 BP |
3859 | } |
3860 | if (!(may_match & MAY_ND_TARGET)) { | |
47284b1f | 3861 | wc.nd_target_mask = in6addr_any; |
b459a924 BP |
3862 | } |
3863 | ||
3864 | /* Log any changes. */ | |
3865 | if (!flow_wildcards_equal(&wc, &rule->wc)) { | |
3866 | bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl); | |
3867 | char *pre = log ? cls_rule_to_string(rule) : NULL; | |
3868 | ||
3869 | rule->wc = wc; | |
3870 | cls_rule_zero_wildcarded_fields(rule); | |
3871 | ||
3872 | if (log) { | |
3873 | char *post = cls_rule_to_string(rule); | |
3874 | VLOG_INFO("normalization changed ofp_match, details:"); | |
3875 | VLOG_INFO(" pre: %s", pre); | |
3876 | VLOG_INFO("post: %s", post); | |
3877 | free(pre); | |
3878 | free(post); | |
3879 | } | |
fa37b408 | 3880 | } |
3f09c339 | 3881 | } |
26c112c2 | 3882 | |
3052b0c5 BP |
3883 | /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if |
3884 | * successful, otherwise an OpenFlow error. | |
3885 | * | |
3886 | * If successful, the first action is stored in '*actionsp' and the number of | |
3887 | * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0 | |
3888 | * are stored, respectively. | |
3889 | * | |
3890 | * This function does not check that the actions are valid (the caller should | |
3891 | * do so, with validate_actions()). The caller is also responsible for making | |
3892 | * sure that 'b->data' is initially aligned appropriately for "union | |
3893 | * ofp_action". */ | |
90bf1e07 | 3894 | enum ofperr |
3052b0c5 BP |
3895 | ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len, |
3896 | union ofp_action **actionsp, size_t *n_actionsp) | |
3897 | { | |
69b6be19 | 3898 | if (actions_len % OFP_ACTION_ALIGN != 0) { |
f4350529 BP |
3899 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u " |
3900 | "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN); | |
3052b0c5 BP |
3901 | goto error; |
3902 | } | |
3903 | ||
3904 | *actionsp = ofpbuf_try_pull(b, actions_len); | |
3905 | if (*actionsp == NULL) { | |
f4350529 BP |
3906 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u " |
3907 | "exceeds remaining message length (%zu)", | |
3908 | actions_len, b->size); | |
3052b0c5 BP |
3909 | goto error; |
3910 | } | |
3911 | ||
69b6be19 | 3912 | *n_actionsp = actions_len / OFP_ACTION_ALIGN; |
3052b0c5 BP |
3913 | return 0; |
3914 | ||
3915 | error: | |
3916 | *actionsp = NULL; | |
3917 | *n_actionsp = 0; | |
90bf1e07 | 3918 | return OFPERR_OFPBRC_BAD_LEN; |
3052b0c5 | 3919 | } |
18ddadc2 BP |
3920 | |
3921 | bool | |
3922 | ofputil_actions_equal(const union ofp_action *a, size_t n_a, | |
3923 | const union ofp_action *b, size_t n_b) | |
3924 | { | |
3925 | return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a)); | |
3926 | } | |
3927 | ||
3928 | union ofp_action * | |
3929 | ofputil_actions_clone(const union ofp_action *actions, size_t n) | |
3930 | { | |
3931 | return n ? xmemdup(actions, n * sizeof *actions) : NULL; | |
3932 | } | |
0ff22822 BP |
3933 | |
3934 | /* Parses a key or a key-value pair from '*stringp'. | |
3935 | * | |
3936 | * On success: Stores the key into '*keyp'. Stores the value, if present, into | |
3937 | * '*valuep', otherwise an empty string. Advances '*stringp' past the end of | |
3938 | * the key-value pair, preparing it for another call. '*keyp' and '*valuep' | |
3939 | * are substrings of '*stringp' created by replacing some of its bytes by null | |
3940 | * terminators. Returns true. | |
3941 | * | |
3942 | * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to | |
3943 | * NULL and returns false. */ | |
3944 | bool | |
3945 | ofputil_parse_key_value(char **stringp, char **keyp, char **valuep) | |
3946 | { | |
3947 | char *pos, *key, *value; | |
3948 | size_t key_len; | |
3949 | ||
3950 | pos = *stringp; | |
3951 | pos += strspn(pos, ", \t\r\n"); | |
3952 | if (*pos == '\0') { | |
3953 | *keyp = *valuep = NULL; | |
3954 | return false; | |
3955 | } | |
3956 | ||
3957 | key = pos; | |
3958 | key_len = strcspn(pos, ":=(, \t\r\n"); | |
3959 | if (key[key_len] == ':' || key[key_len] == '=') { | |
3960 | /* The value can be separated by a colon. */ | |
3961 | size_t value_len; | |
3962 | ||
3963 | value = key + key_len + 1; | |
3964 | value_len = strcspn(value, ", \t\r\n"); | |
3965 | pos = value + value_len + (value[value_len] != '\0'); | |
3966 | value[value_len] = '\0'; | |
3967 | } else if (key[key_len] == '(') { | |
3968 | /* The value can be surrounded by balanced parentheses. The outermost | |
3969 | * set of parentheses is removed. */ | |
3970 | int level = 1; | |
3971 | size_t value_len; | |
3972 | ||
3973 | value = key + key_len + 1; | |
3974 | for (value_len = 0; level > 0; value_len++) { | |
3975 | switch (value[value_len]) { | |
3976 | case '\0': | |
33cadc50 BP |
3977 | level = 0; |
3978 | break; | |
0ff22822 BP |
3979 | |
3980 | case '(': | |
3981 | level++; | |
3982 | break; | |
3983 | ||
3984 | case ')': | |
3985 | level--; | |
3986 | break; | |
3987 | } | |
3988 | } | |
3989 | value[value_len - 1] = '\0'; | |
3990 | pos = value + value_len; | |
3991 | } else { | |
3992 | /* There might be no value at all. */ | |
3993 | value = key + key_len; /* Will become the empty string below. */ | |
3994 | pos = key + key_len + (key[key_len] != '\0'); | |
3995 | } | |
3996 | key[key_len] = '\0'; | |
3997 | ||
3998 | *stringp = pos; | |
3999 | *keyp = key; | |
4000 | *valuep = value; | |
4001 | return true; | |
4002 | } |