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