]>
Commit | Line | Data |
---|---|---|
fa37b408 | 1 | /* |
73dbf4ab | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks. |
fa37b408 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include "ofp-print.h" | |
dc4762ed | 19 | #include <errno.h> |
fa37b408 | 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" |
53ddd40a | 31 | #include "multipath.h" |
b6c9e612 | 32 | #include "nx-match.h" |
dc4762ed | 33 | #include "ofp-errors.h" |
fa37b408 BP |
34 | #include "ofp-util.h" |
35 | #include "ofpbuf.h" | |
36 | #include "packets.h" | |
37 | #include "random.h" | |
4ffd1b43 | 38 | #include "unaligned.h" |
e41a9130 | 39 | #include "type-props.h" |
5136ce49 | 40 | #include "vlog.h" |
fa37b408 | 41 | |
d98e6007 | 42 | VLOG_DEFINE_THIS_MODULE(ofp_util); |
fa37b408 BP |
43 | |
44 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
45 | * in the peer and so there's not much point in showing a lot of them. */ | |
46 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
47 | ||
0596e897 BP |
48 | /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns |
49 | * an IP netmask with a 1 in each bit that must match and a 0 in each bit that | |
50 | * is wildcarded. | |
51 | * | |
52 | * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0 | |
53 | * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, | |
54 | * ..., 32 and higher wildcard the entire field. This is the *opposite* of the | |
55 | * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are | |
56 | * wildcarded. */ | |
57 | ovs_be32 | |
58 | ofputil_wcbits_to_netmask(int wcbits) | |
59 | { | |
60 | wcbits &= 0x3f; | |
61 | return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0; | |
62 | } | |
63 | ||
64 | /* Given the IP netmask 'netmask', returns the number of bits of the IP address | |
aad29cd1 BP |
65 | * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask' |
66 | * must be a CIDR netmask (see ip_is_cidr()). */ | |
0596e897 BP |
67 | int |
68 | ofputil_netmask_to_wcbits(ovs_be32 netmask) | |
69 | { | |
aad29cd1 | 70 | return 32 - ip_count_cidr_bits(netmask); |
0596e897 BP |
71 | } |
72 | ||
d8ae4d67 BP |
73 | /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and |
74 | * name. */ | |
75 | #define WC_INVARIANT_LIST \ | |
76 | WC_INVARIANT_BIT(IN_PORT) \ | |
d8ae4d67 BP |
77 | WC_INVARIANT_BIT(DL_SRC) \ |
78 | WC_INVARIANT_BIT(DL_DST) \ | |
79 | WC_INVARIANT_BIT(DL_TYPE) \ | |
80 | WC_INVARIANT_BIT(NW_PROTO) \ | |
81 | WC_INVARIANT_BIT(TP_SRC) \ | |
82 | WC_INVARIANT_BIT(TP_DST) | |
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 | { |
2486e66a | 104 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7); |
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 |
a61680c6 | 112 | | FWW_ND_TARGET | 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 | ||
d8ae4d67 BP |
123 | if (ofpfw & OFPFW_DL_DST) { |
124 | /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but | |
125 | * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST | |
126 | * and FWW_ETH_MCAST. */ | |
127 | wc->wildcards |= FWW_ETH_MCAST; | |
128 | } | |
129 | ||
eb6f28db BP |
130 | /* VLAN TCI mask. */ |
131 | if (!(ofpfw & OFPFW_DL_VLAN_PCP)) { | |
132 | wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI); | |
133 | } | |
134 | if (!(ofpfw & OFPFW_DL_VLAN)) { | |
135 | wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI); | |
136 | } | |
137 | } | |
138 | ||
139 | /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given | |
140 | * 'priority'. */ | |
141 | void | |
142 | ofputil_cls_rule_from_match(const struct ofp_match *match, | |
143 | unsigned int priority, struct cls_rule *rule) | |
144 | { | |
145 | uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL; | |
146 | ||
147 | /* Initialize rule->priority, rule->wc. */ | |
148 | rule->priority = !ofpfw ? UINT16_MAX : priority; | |
149 | ofputil_wildcard_from_openflow(ofpfw, &rule->wc); | |
150 | ||
66642cb4 | 151 | /* Initialize most of rule->flow. */ |
d8ae4d67 BP |
152 | rule->flow.nw_src = match->nw_src; |
153 | rule->flow.nw_dst = match->nw_dst; | |
abe529af | 154 | rule->flow.in_port = ntohs(match->in_port); |
36956a7d | 155 | rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type); |
d8ae4d67 BP |
156 | rule->flow.tp_src = match->tp_src; |
157 | rule->flow.tp_dst = match->tp_dst; | |
158 | memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN); | |
159 | memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN); | |
eadef313 | 160 | rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK; |
d8ae4d67 BP |
161 | rule->flow.nw_proto = match->nw_proto; |
162 | ||
66642cb4 | 163 | /* Translate VLANs. */ |
47271d0d BP |
164 | if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) { |
165 | /* Match only packets without 802.1Q header. | |
166 | * | |
167 | * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct. | |
168 | * | |
169 | * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory, | |
170 | * because we can't have a specific PCP without an 802.1Q header. | |
171 | * However, older versions of OVS treated this as matching packets | |
172 | * withut an 802.1Q header, so we do here too. */ | |
66642cb4 | 173 | rule->flow.vlan_tci = htons(0); |
eb6f28db | 174 | rule->wc.vlan_tci_mask = htons(0xffff); |
47271d0d BP |
175 | } else { |
176 | ovs_be16 vid, pcp, tci; | |
177 | ||
47271d0d BP |
178 | vid = match->dl_vlan & htons(VLAN_VID_MASK); |
179 | pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK); | |
180 | tci = vid | pcp | htons(VLAN_CFI); | |
eb6f28db | 181 | rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask; |
66642cb4 BP |
182 | } |
183 | ||
d8ae4d67 BP |
184 | /* Clean up. */ |
185 | cls_rule_zero_wildcarded_fields(rule); | |
186 | } | |
187 | ||
b78f6b77 | 188 | /* Convert 'rule' into the OpenFlow match structure 'match'. */ |
d8ae4d67 | 189 | void |
b78f6b77 | 190 | ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match) |
d8ae4d67 BP |
191 | { |
192 | const struct flow_wildcards *wc = &rule->wc; | |
eeba8e4f | 193 | uint32_t ofpfw; |
d8ae4d67 | 194 | |
66642cb4 | 195 | /* Figure out most OpenFlow wildcards. */ |
eeba8e4f | 196 | ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS); |
d8ae4d67 BP |
197 | ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT; |
198 | ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT; | |
2486e66a | 199 | if (wc->wildcards & FWW_NW_DSCP) { |
d8ae4d67 BP |
200 | ofpfw |= OFPFW_NW_TOS; |
201 | } | |
ff9d3826 | 202 | |
66642cb4 BP |
203 | /* Translate VLANs. */ |
204 | match->dl_vlan = htons(0); | |
205 | match->dl_vlan_pcp = 0; | |
206 | if (rule->wc.vlan_tci_mask == htons(0)) { | |
207 | ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP; | |
208 | } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI) | |
209 | && !(rule->flow.vlan_tci & htons(VLAN_CFI))) { | |
210 | match->dl_vlan = htons(OFP_VLAN_NONE); | |
211 | } else { | |
212 | if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) { | |
213 | ofpfw |= OFPFW_DL_VLAN; | |
214 | } else { | |
215 | match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci)); | |
216 | } | |
217 | ||
218 | if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) { | |
219 | ofpfw |= OFPFW_DL_VLAN_PCP; | |
220 | } else { | |
221 | match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci); | |
222 | } | |
223 | } | |
224 | ||
225 | /* Compose most of the match structure. */ | |
d8ae4d67 | 226 | match->wildcards = htonl(ofpfw); |
abe529af | 227 | match->in_port = htons(rule->flow.in_port); |
d8ae4d67 BP |
228 | memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN); |
229 | memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN); | |
36956a7d | 230 | match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type); |
d8ae4d67 BP |
231 | match->nw_src = rule->flow.nw_src; |
232 | match->nw_dst = rule->flow.nw_dst; | |
eadef313 | 233 | match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK; |
d8ae4d67 BP |
234 | match->nw_proto = rule->flow.nw_proto; |
235 | match->tp_src = rule->flow.tp_src; | |
236 | match->tp_dst = rule->flow.tp_dst; | |
237 | memset(match->pad1, '\0', sizeof match->pad1); | |
238 | memset(match->pad2, '\0', sizeof match->pad2); | |
239 | } | |
240 | ||
36956a7d BP |
241 | /* Given a 'dl_type' value in the format used in struct flow, returns the |
242 | * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */ | |
243 | ovs_be16 | |
244 | ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type) | |
245 | { | |
246 | return (flow_dl_type == htons(FLOW_DL_TYPE_NONE) | |
247 | ? htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
248 | : flow_dl_type); | |
249 | } | |
250 | ||
251 | /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match | |
252 | * structure, returns the corresponding 'dl_type' value for use in struct | |
253 | * flow. */ | |
254 | ovs_be16 | |
255 | ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type) | |
256 | { | |
257 | return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
258 | ? htons(FLOW_DL_TYPE_NONE) | |
259 | : ofp_dl_type); | |
260 | } | |
261 | ||
72fae175 | 262 | /* Returns a transaction ID to use for an outgoing OpenFlow message. */ |
44381c1b | 263 | static ovs_be32 |
fa37b408 BP |
264 | alloc_xid(void) |
265 | { | |
72fae175 | 266 | static uint32_t next_xid = 1; |
44381c1b | 267 | return htonl(next_xid++); |
fa37b408 | 268 | } |
d1e2cf21 BP |
269 | \f |
270 | /* Basic parsing of OpenFlow messages. */ | |
fa37b408 | 271 | |
d1e2cf21 BP |
272 | struct ofputil_msg_type { |
273 | enum ofputil_msg_code code; /* OFPUTIL_*. */ | |
a1893da1 | 274 | uint8_t ofp_version; /* An OpenFlow version or 0 for "any". */ |
d1e2cf21 BP |
275 | uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */ |
276 | const char *name; /* e.g. "OFPT_FLOW_REMOVED". */ | |
277 | unsigned int min_size; /* Minimum total message size in bytes. */ | |
278 | /* 0 if 'min_size' is the exact size that the message must be. Otherwise, | |
279 | * the message may exceed 'min_size' by an even multiple of this value. */ | |
280 | unsigned int extra_multiple; | |
281 | }; | |
282 | ||
5a020ef3 BP |
283 | /* Represents a malformed OpenFlow message. */ |
284 | static const struct ofputil_msg_type ofputil_invalid_type = { | |
a1893da1 | 285 | OFPUTIL_MSG_INVALID, 0, 0, "OFPUTIL_MSG_INVALID", 0, 0 |
5a020ef3 BP |
286 | }; |
287 | ||
d1e2cf21 BP |
288 | struct ofputil_msg_category { |
289 | const char *name; /* e.g. "OpenFlow message" */ | |
290 | const struct ofputil_msg_type *types; | |
291 | size_t n_types; | |
90bf1e07 | 292 | enum ofperr missing_error; /* Error value for missing type. */ |
d1e2cf21 BP |
293 | }; |
294 | ||
90bf1e07 | 295 | static enum ofperr |
5a020ef3 | 296 | ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size) |
d1e2cf21 BP |
297 | { |
298 | switch (type->extra_multiple) { | |
299 | case 0: | |
300 | if (size != type->min_size) { | |
5a020ef3 | 301 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect " |
d1e2cf21 | 302 | "length %u (expected length %u)", |
5a020ef3 | 303 | type->name, size, type->min_size); |
90bf1e07 | 304 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 | 305 | } |
5a020ef3 | 306 | return 0; |
d1e2cf21 BP |
307 | |
308 | case 1: | |
309 | if (size < type->min_size) { | |
5a020ef3 | 310 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect " |
d1e2cf21 | 311 | "length %u (expected length at least %u bytes)", |
5a020ef3 | 312 | type->name, size, type->min_size); |
90bf1e07 | 313 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 | 314 | } |
5a020ef3 | 315 | return 0; |
d1e2cf21 BP |
316 | |
317 | default: | |
318 | if (size < type->min_size | |
319 | || (size - type->min_size) % type->extra_multiple) { | |
5a020ef3 | 320 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect " |
d1e2cf21 BP |
321 | "length %u (must be exactly %u bytes or longer " |
322 | "by an integer multiple of %u bytes)", | |
5a020ef3 | 323 | type->name, size, |
d1e2cf21 | 324 | type->min_size, type->extra_multiple); |
90bf1e07 | 325 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 | 326 | } |
5a020ef3 | 327 | return 0; |
d1e2cf21 BP |
328 | } |
329 | } | |
330 | ||
90bf1e07 | 331 | static enum ofperr |
d1e2cf21 | 332 | ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat, |
a1893da1 | 333 | uint8_t version, uint32_t value, |
d1e2cf21 BP |
334 | const struct ofputil_msg_type **typep) |
335 | { | |
336 | const struct ofputil_msg_type *type; | |
337 | ||
338 | for (type = cat->types; type < &cat->types[cat->n_types]; type++) { | |
a1893da1 BP |
339 | if (type->value == value |
340 | && (!type->ofp_version || version == type->ofp_version)) { | |
d1e2cf21 BP |
341 | *typep = type; |
342 | return 0; | |
343 | } | |
344 | } | |
345 | ||
5c47debb | 346 | VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32, |
d1e2cf21 BP |
347 | cat->name, value); |
348 | return cat->missing_error; | |
349 | } | |
350 | ||
90bf1e07 | 351 | static enum ofperr |
5a020ef3 | 352 | ofputil_decode_vendor(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
353 | const struct ofputil_msg_type **typep) |
354 | { | |
355 | static const struct ofputil_msg_type nxt_messages[] = { | |
a1893da1 | 356 | { OFPUTIL_NXT_ROLE_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
357 | NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST", |
358 | sizeof(struct nx_role_request), 0 }, | |
359 | ||
a1893da1 | 360 | { OFPUTIL_NXT_ROLE_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
361 | NXT_ROLE_REPLY, "NXT_ROLE_REPLY", |
362 | sizeof(struct nx_role_request), 0 }, | |
363 | ||
a1893da1 | 364 | { OFPUTIL_NXT_SET_FLOW_FORMAT, OFP10_VERSION, |
d1e2cf21 | 365 | NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT", |
73dbf4ab | 366 | sizeof(struct nx_set_flow_format), 0 }, |
d1e2cf21 | 367 | |
a1893da1 | 368 | { OFPUTIL_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION, |
54834960 | 369 | NXT_SET_PACKET_IN_FORMAT, "NXT_SET_PACKET_IN_FORMAT", |
73dbf4ab | 370 | sizeof(struct nx_set_packet_in_format), 0 }, |
54834960 | 371 | |
a1893da1 | 372 | { OFPUTIL_NXT_PACKET_IN, OFP10_VERSION, |
54834960 | 373 | NXT_PACKET_IN, "NXT_PACKET_IN", |
73dbf4ab | 374 | sizeof(struct nx_packet_in), 1 }, |
54834960 | 375 | |
a1893da1 | 376 | { OFPUTIL_NXT_FLOW_MOD, OFP10_VERSION, |
d1e2cf21 BP |
377 | NXT_FLOW_MOD, "NXT_FLOW_MOD", |
378 | sizeof(struct nx_flow_mod), 8 }, | |
379 | ||
a1893da1 | 380 | { OFPUTIL_NXT_FLOW_REMOVED, OFP10_VERSION, |
d1e2cf21 BP |
381 | NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED", |
382 | sizeof(struct nx_flow_removed), 8 }, | |
d1e9b9bf | 383 | |
a1893da1 | 384 | { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, |
d1e9b9bf | 385 | NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID", |
73dbf4ab | 386 | sizeof(struct nx_flow_mod_table_id), 0 }, |
d1e2cf21 BP |
387 | }; |
388 | ||
389 | static const struct ofputil_msg_category nxt_category = { | |
390 | "Nicira extension message", | |
391 | nxt_messages, ARRAY_SIZE(nxt_messages), | |
90bf1e07 | 392 | OFPERR_OFPBRC_BAD_SUBTYPE |
d1e2cf21 BP |
393 | }; |
394 | ||
395 | const struct ofp_vendor_header *ovh; | |
396 | const struct nicira_header *nh; | |
397 | ||
5a020ef3 BP |
398 | if (length < sizeof(struct ofp_vendor_header)) { |
399 | if (length == ntohs(oh->length)) { | |
400 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message"); | |
401 | } | |
90bf1e07 | 402 | return OFPERR_OFPBRC_BAD_LEN; |
5a020ef3 BP |
403 | } |
404 | ||
d1e2cf21 BP |
405 | ovh = (const struct ofp_vendor_header *) oh; |
406 | if (ovh->vendor != htonl(NX_VENDOR_ID)) { | |
407 | VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown " | |
408 | "vendor %"PRIx32, ntohl(ovh->vendor)); | |
90bf1e07 | 409 | return OFPERR_OFPBRC_BAD_VENDOR; |
d1e2cf21 BP |
410 | } |
411 | ||
5a020ef3 BP |
412 | if (length < sizeof(struct nicira_header)) { |
413 | if (length == ntohs(oh->length)) { | |
414 | VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of " | |
415 | "length %u (expected at least %zu)", | |
416 | ntohs(ovh->header.length), | |
417 | sizeof(struct nicira_header)); | |
418 | } | |
90bf1e07 | 419 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 BP |
420 | } |
421 | ||
422 | nh = (const struct nicira_header *) oh; | |
a1893da1 BP |
423 | return ofputil_lookup_openflow_message(&nxt_category, oh->version, |
424 | ntohl(nh->subtype), typep); | |
d1e2cf21 BP |
425 | } |
426 | ||
90bf1e07 | 427 | static enum ofperr |
5a020ef3 | 428 | check_nxstats_msg(const struct ofp_header *oh, size_t length) |
d1e2cf21 | 429 | { |
28c8bad1 | 430 | const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh; |
d1e2cf21 BP |
431 | ovs_be32 vendor; |
432 | ||
5a020ef3 BP |
433 | if (length < sizeof(struct ofp_vendor_stats_msg)) { |
434 | if (length == ntohs(oh->length)) { | |
435 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message"); | |
436 | } | |
90bf1e07 | 437 | return OFPERR_OFPBRC_BAD_LEN; |
5a020ef3 BP |
438 | } |
439 | ||
03cd3493 | 440 | memcpy(&vendor, osm + 1, sizeof vendor); |
d1e2cf21 BP |
441 | if (vendor != htonl(NX_VENDOR_ID)) { |
442 | VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for " | |
443 | "unknown vendor %"PRIx32, ntohl(vendor)); | |
90bf1e07 | 444 | return OFPERR_OFPBRC_BAD_VENDOR; |
d1e2cf21 BP |
445 | } |
446 | ||
5a020ef3 BP |
447 | if (length < sizeof(struct nicira_stats_msg)) { |
448 | if (length == ntohs(osm->header.length)) { | |
449 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message"); | |
450 | } | |
90bf1e07 | 451 | return OFPERR_OFPBRC_BAD_LEN; |
d1e2cf21 BP |
452 | } |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
90bf1e07 | 457 | static enum ofperr |
5a020ef3 | 458 | ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
459 | const struct ofputil_msg_type **typep) |
460 | { | |
461 | static const struct ofputil_msg_type nxst_requests[] = { | |
a1893da1 | 462 | { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
463 | NXST_FLOW, "NXST_FLOW request", |
464 | sizeof(struct nx_flow_stats_request), 8 }, | |
465 | ||
a1893da1 | 466 | { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
467 | NXST_AGGREGATE, "NXST_AGGREGATE request", |
468 | sizeof(struct nx_aggregate_stats_request), 8 }, | |
469 | }; | |
470 | ||
471 | static const struct ofputil_msg_category nxst_request_category = { | |
08717852 | 472 | "Nicira extension statistics request", |
d1e2cf21 | 473 | nxst_requests, ARRAY_SIZE(nxst_requests), |
90bf1e07 | 474 | OFPERR_OFPBRC_BAD_SUBTYPE |
d1e2cf21 BP |
475 | }; |
476 | ||
477 | const struct nicira_stats_msg *nsm; | |
90bf1e07 | 478 | enum ofperr error; |
d1e2cf21 | 479 | |
5a020ef3 | 480 | error = check_nxstats_msg(oh, length); |
d1e2cf21 BP |
481 | if (error) { |
482 | return error; | |
483 | } | |
484 | ||
485 | nsm = (struct nicira_stats_msg *) oh; | |
a1893da1 | 486 | return ofputil_lookup_openflow_message(&nxst_request_category, oh->version, |
5a020ef3 | 487 | ntohl(nsm->subtype), typep); |
d1e2cf21 BP |
488 | } |
489 | ||
90bf1e07 | 490 | static enum ofperr |
5a020ef3 | 491 | ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
492 | const struct ofputil_msg_type **typep) |
493 | { | |
494 | static const struct ofputil_msg_type nxst_replies[] = { | |
a1893da1 | 495 | { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
496 | NXST_FLOW, "NXST_FLOW reply", |
497 | sizeof(struct nicira_stats_msg), 8 }, | |
498 | ||
a1893da1 | 499 | { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
500 | NXST_AGGREGATE, "NXST_AGGREGATE reply", |
501 | sizeof(struct nx_aggregate_stats_reply), 0 }, | |
502 | }; | |
503 | ||
504 | static const struct ofputil_msg_category nxst_reply_category = { | |
08717852 | 505 | "Nicira extension statistics reply", |
d1e2cf21 | 506 | nxst_replies, ARRAY_SIZE(nxst_replies), |
90bf1e07 | 507 | OFPERR_OFPBRC_BAD_SUBTYPE |
d1e2cf21 BP |
508 | }; |
509 | ||
510 | const struct nicira_stats_msg *nsm; | |
90bf1e07 | 511 | enum ofperr error; |
d1e2cf21 | 512 | |
5a020ef3 | 513 | error = check_nxstats_msg(oh, length); |
d1e2cf21 BP |
514 | if (error) { |
515 | return error; | |
516 | } | |
517 | ||
518 | nsm = (struct nicira_stats_msg *) oh; | |
a1893da1 | 519 | return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version, |
5a020ef3 BP |
520 | ntohl(nsm->subtype), typep); |
521 | } | |
522 | ||
90bf1e07 | 523 | static enum ofperr |
5a020ef3 BP |
524 | check_stats_msg(const struct ofp_header *oh, size_t length) |
525 | { | |
526 | if (length < sizeof(struct ofp_stats_msg)) { | |
527 | if (length == ntohs(oh->length)) { | |
528 | VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message"); | |
529 | } | |
90bf1e07 | 530 | return OFPERR_OFPBRC_BAD_LEN; |
5a020ef3 BP |
531 | } |
532 | ||
533 | return 0; | |
d1e2cf21 BP |
534 | } |
535 | ||
90bf1e07 | 536 | static enum ofperr |
5a020ef3 | 537 | ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
538 | const struct ofputil_msg_type **typep) |
539 | { | |
d1e2cf21 | 540 | static const struct ofputil_msg_type ofpst_requests[] = { |
a1893da1 | 541 | { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION, |
d1e2cf21 | 542 | OFPST_DESC, "OFPST_DESC request", |
63f2140a | 543 | sizeof(struct ofp_stats_msg), 0 }, |
d1e2cf21 | 544 | |
a1893da1 | 545 | { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION, |
d1e2cf21 | 546 | OFPST_FLOW, "OFPST_FLOW request", |
63f2140a | 547 | sizeof(struct ofp_flow_stats_request), 0 }, |
d1e2cf21 | 548 | |
a1893da1 | 549 | { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION, |
d1e2cf21 | 550 | OFPST_AGGREGATE, "OFPST_AGGREGATE request", |
63f2140a | 551 | sizeof(struct ofp_flow_stats_request), 0 }, |
d1e2cf21 | 552 | |
a1893da1 | 553 | { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION, |
d1e2cf21 | 554 | OFPST_TABLE, "OFPST_TABLE request", |
63f2140a | 555 | sizeof(struct ofp_stats_msg), 0 }, |
d1e2cf21 | 556 | |
a1893da1 | 557 | { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION, |
d1e2cf21 | 558 | OFPST_PORT, "OFPST_PORT request", |
63f2140a | 559 | sizeof(struct ofp_port_stats_request), 0 }, |
d1e2cf21 | 560 | |
a1893da1 | 561 | { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION, |
d1e2cf21 | 562 | OFPST_QUEUE, "OFPST_QUEUE request", |
63f2140a | 563 | sizeof(struct ofp_queue_stats_request), 0 }, |
d1e2cf21 | 564 | |
a1893da1 | 565 | { 0, 0, |
d1e2cf21 | 566 | OFPST_VENDOR, "OFPST_VENDOR request", |
63f2140a | 567 | sizeof(struct ofp_vendor_stats_msg), 1 }, |
d1e2cf21 BP |
568 | }; |
569 | ||
570 | static const struct ofputil_msg_category ofpst_request_category = { | |
571 | "OpenFlow statistics", | |
572 | ofpst_requests, ARRAY_SIZE(ofpst_requests), | |
90bf1e07 | 573 | OFPERR_OFPBRC_BAD_STAT |
d1e2cf21 BP |
574 | }; |
575 | ||
28c8bad1 | 576 | const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh; |
90bf1e07 | 577 | enum ofperr error; |
d1e2cf21 | 578 | |
5a020ef3 BP |
579 | error = check_stats_msg(oh, length); |
580 | if (error) { | |
581 | return error; | |
582 | } | |
583 | ||
d1e2cf21 | 584 | error = ofputil_lookup_openflow_message(&ofpst_request_category, |
a1893da1 BP |
585 | oh->version, ntohs(request->type), |
586 | typep); | |
28c8bad1 | 587 | if (!error && request->type == htons(OFPST_VENDOR)) { |
5a020ef3 | 588 | error = ofputil_decode_nxst_request(oh, length, typep); |
d1e2cf21 BP |
589 | } |
590 | return error; | |
591 | } | |
592 | ||
90bf1e07 | 593 | static enum ofperr |
5a020ef3 | 594 | ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length, |
d1e2cf21 BP |
595 | const struct ofputil_msg_type **typep) |
596 | { | |
d1e2cf21 | 597 | static const struct ofputil_msg_type ofpst_replies[] = { |
a1893da1 | 598 | { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION, |
d1e2cf21 | 599 | OFPST_DESC, "OFPST_DESC reply", |
63f2140a | 600 | sizeof(struct ofp_desc_stats), 0 }, |
d1e2cf21 | 601 | |
a1893da1 | 602 | { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION, |
d1e2cf21 | 603 | OFPST_FLOW, "OFPST_FLOW reply", |
63f2140a | 604 | sizeof(struct ofp_stats_msg), 1 }, |
d1e2cf21 | 605 | |
a1893da1 | 606 | { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION, |
d1e2cf21 | 607 | OFPST_AGGREGATE, "OFPST_AGGREGATE reply", |
63f2140a | 608 | sizeof(struct ofp_aggregate_stats_reply), 0 }, |
d1e2cf21 | 609 | |
a1893da1 | 610 | { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION, |
d1e2cf21 | 611 | OFPST_TABLE, "OFPST_TABLE reply", |
63f2140a | 612 | sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) }, |
d1e2cf21 | 613 | |
a1893da1 | 614 | { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION, |
d1e2cf21 | 615 | OFPST_PORT, "OFPST_PORT reply", |
63f2140a | 616 | sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) }, |
d1e2cf21 | 617 | |
a1893da1 | 618 | { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION, |
d1e2cf21 | 619 | OFPST_QUEUE, "OFPST_QUEUE reply", |
63f2140a | 620 | sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) }, |
d1e2cf21 | 621 | |
a1893da1 | 622 | { 0, 0, |
d1e2cf21 | 623 | OFPST_VENDOR, "OFPST_VENDOR reply", |
63f2140a | 624 | sizeof(struct ofp_vendor_stats_msg), 1 }, |
d1e2cf21 BP |
625 | }; |
626 | ||
627 | static const struct ofputil_msg_category ofpst_reply_category = { | |
628 | "OpenFlow statistics", | |
629 | ofpst_replies, ARRAY_SIZE(ofpst_replies), | |
90bf1e07 | 630 | OFPERR_OFPBRC_BAD_STAT |
d1e2cf21 BP |
631 | }; |
632 | ||
28c8bad1 | 633 | const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh; |
90bf1e07 | 634 | enum ofperr error; |
d1e2cf21 | 635 | |
5a020ef3 BP |
636 | error = check_stats_msg(oh, length); |
637 | if (error) { | |
638 | return error; | |
639 | } | |
640 | ||
a1893da1 | 641 | error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version, |
5a020ef3 | 642 | ntohs(reply->type), typep); |
28c8bad1 | 643 | if (!error && reply->type == htons(OFPST_VENDOR)) { |
5a020ef3 | 644 | error = ofputil_decode_nxst_reply(oh, length, typep); |
d1e2cf21 BP |
645 | } |
646 | return error; | |
647 | } | |
648 | ||
90bf1e07 | 649 | static enum ofperr |
5a020ef3 BP |
650 | ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length, |
651 | const struct ofputil_msg_type **typep) | |
d1e2cf21 BP |
652 | { |
653 | static const struct ofputil_msg_type ofpt_messages[] = { | |
a1893da1 | 654 | { OFPUTIL_OFPT_HELLO, OFP10_VERSION, |
d1e2cf21 BP |
655 | OFPT_HELLO, "OFPT_HELLO", |
656 | sizeof(struct ofp_hello), 1 }, | |
657 | ||
90bf1e07 | 658 | { OFPUTIL_OFPT_ERROR, 0, |
d1e2cf21 BP |
659 | OFPT_ERROR, "OFPT_ERROR", |
660 | sizeof(struct ofp_error_msg), 1 }, | |
661 | ||
a1893da1 | 662 | { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
663 | OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST", |
664 | sizeof(struct ofp_header), 1 }, | |
665 | ||
a1893da1 | 666 | { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
667 | OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY", |
668 | sizeof(struct ofp_header), 1 }, | |
669 | ||
a1893da1 | 670 | { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
671 | OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST", |
672 | sizeof(struct ofp_header), 0 }, | |
673 | ||
a1893da1 | 674 | { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
675 | OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY", |
676 | sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) }, | |
677 | ||
a1893da1 | 678 | { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
679 | OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST", |
680 | sizeof(struct ofp_header), 0 }, | |
681 | ||
a1893da1 | 682 | { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
683 | OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY", |
684 | sizeof(struct ofp_switch_config), 0 }, | |
685 | ||
a1893da1 | 686 | { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION, |
d1e2cf21 BP |
687 | OFPT_SET_CONFIG, "OFPT_SET_CONFIG", |
688 | sizeof(struct ofp_switch_config), 0 }, | |
689 | ||
a1893da1 | 690 | { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION, |
d1e2cf21 BP |
691 | OFPT_PACKET_IN, "OFPT_PACKET_IN", |
692 | offsetof(struct ofp_packet_in, data), 1 }, | |
693 | ||
a1893da1 | 694 | { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION, |
d1e2cf21 BP |
695 | OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED", |
696 | sizeof(struct ofp_flow_removed), 0 }, | |
697 | ||
a1893da1 | 698 | { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION, |
d1e2cf21 BP |
699 | OFPT_PORT_STATUS, "OFPT_PORT_STATUS", |
700 | sizeof(struct ofp_port_status), 0 }, | |
701 | ||
a1893da1 | 702 | { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION, |
d1e2cf21 BP |
703 | OFPT_PACKET_OUT, "OFPT_PACKET_OUT", |
704 | sizeof(struct ofp_packet_out), 1 }, | |
705 | ||
a1893da1 | 706 | { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION, |
d1e2cf21 BP |
707 | OFPT_FLOW_MOD, "OFPT_FLOW_MOD", |
708 | sizeof(struct ofp_flow_mod), 1 }, | |
709 | ||
a1893da1 | 710 | { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION, |
d1e2cf21 BP |
711 | OFPT_PORT_MOD, "OFPT_PORT_MOD", |
712 | sizeof(struct ofp_port_mod), 0 }, | |
713 | ||
a1893da1 | 714 | { 0, OFP10_VERSION, |
d1e2cf21 | 715 | OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST", |
28c8bad1 | 716 | sizeof(struct ofp_stats_msg), 1 }, |
d1e2cf21 | 717 | |
a1893da1 | 718 | { 0, OFP10_VERSION, |
d1e2cf21 | 719 | OFPT_STATS_REPLY, "OFPT_STATS_REPLY", |
28c8bad1 | 720 | sizeof(struct ofp_stats_msg), 1 }, |
d1e2cf21 | 721 | |
a1893da1 | 722 | { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION, |
d1e2cf21 BP |
723 | OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST", |
724 | sizeof(struct ofp_header), 0 }, | |
725 | ||
a1893da1 | 726 | { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION, |
d1e2cf21 BP |
727 | OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY", |
728 | sizeof(struct ofp_header), 0 }, | |
729 | ||
a1893da1 | 730 | { 0, 0, |
d1e2cf21 BP |
731 | OFPT_VENDOR, "OFPT_VENDOR", |
732 | sizeof(struct ofp_vendor_header), 1 }, | |
733 | }; | |
734 | ||
735 | static const struct ofputil_msg_category ofpt_category = { | |
736 | "OpenFlow message", | |
737 | ofpt_messages, ARRAY_SIZE(ofpt_messages), | |
90bf1e07 | 738 | OFPERR_OFPBRC_BAD_TYPE |
d1e2cf21 BP |
739 | }; |
740 | ||
90bf1e07 | 741 | enum ofperr error; |
d1e2cf21 | 742 | |
a1893da1 BP |
743 | error = ofputil_lookup_openflow_message(&ofpt_category, oh->version, |
744 | oh->type, typep); | |
d1e2cf21 BP |
745 | if (!error) { |
746 | switch (oh->type) { | |
747 | case OFPT_VENDOR: | |
5a020ef3 | 748 | error = ofputil_decode_vendor(oh, length, typep); |
d1e2cf21 BP |
749 | break; |
750 | ||
751 | case OFPT_STATS_REQUEST: | |
5a020ef3 | 752 | error = ofputil_decode_ofpst_request(oh, length, typep); |
d1e2cf21 BP |
753 | break; |
754 | ||
755 | case OFPT_STATS_REPLY: | |
5a020ef3 | 756 | error = ofputil_decode_ofpst_reply(oh, length, typep); |
d1e2cf21 BP |
757 | |
758 | default: | |
759 | break; | |
760 | } | |
761 | } | |
5a020ef3 BP |
762 | return error; |
763 | } | |
764 | ||
90bf1e07 BP |
765 | /* Decodes the message type represented by 'oh'. Returns 0 if successful or an |
766 | * OpenFlow error code on failure. Either way, stores in '*typep' a type | |
767 | * structure that can be inspected with the ofputil_msg_type_*() functions. | |
5a020ef3 BP |
768 | * |
769 | * oh->length must indicate the correct length of the message (and must be at | |
770 | * least sizeof(struct ofp_header)). | |
771 | * | |
772 | * Success indicates that 'oh' is at least as long as the minimum-length | |
773 | * message of its type. */ | |
90bf1e07 | 774 | enum ofperr |
5a020ef3 BP |
775 | ofputil_decode_msg_type(const struct ofp_header *oh, |
776 | const struct ofputil_msg_type **typep) | |
777 | { | |
778 | size_t length = ntohs(oh->length); | |
90bf1e07 | 779 | enum ofperr error; |
5a020ef3 BP |
780 | |
781 | error = ofputil_decode_msg_type__(oh, length, typep); | |
782 | if (!error) { | |
783 | error = ofputil_check_length(*typep, length); | |
784 | } | |
d1e2cf21 | 785 | if (error) { |
5a020ef3 BP |
786 | *typep = &ofputil_invalid_type; |
787 | } | |
788 | return error; | |
789 | } | |
d1e2cf21 | 790 | |
5a020ef3 BP |
791 | /* Decodes the message type represented by 'oh', of which only the first |
792 | * 'length' bytes are available. Returns 0 if successful or an OpenFlow error | |
90bf1e07 BP |
793 | * code on failure. Either way, stores in '*typep' a type structure that can |
794 | * be inspected with the ofputil_msg_type_*() functions. */ | |
795 | enum ofperr | |
5a020ef3 BP |
796 | ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length, |
797 | const struct ofputil_msg_type **typep) | |
798 | { | |
90bf1e07 | 799 | enum ofperr error; |
5a020ef3 BP |
800 | |
801 | error = (length >= sizeof *oh | |
802 | ? ofputil_decode_msg_type__(oh, length, typep) | |
90bf1e07 | 803 | : OFPERR_OFPBRC_BAD_LEN); |
5a020ef3 | 804 | if (error) { |
d1e2cf21 BP |
805 | *typep = &ofputil_invalid_type; |
806 | } | |
807 | return error; | |
808 | } | |
809 | ||
810 | /* Returns an OFPUTIL_* message type code for 'type'. */ | |
811 | enum ofputil_msg_code | |
812 | ofputil_msg_type_code(const struct ofputil_msg_type *type) | |
813 | { | |
814 | return type->code; | |
815 | } | |
2e4f5fcf | 816 | \f |
7fa91113 BP |
817 | /* Flow formats. */ |
818 | ||
819 | bool | |
820 | ofputil_flow_format_is_valid(enum nx_flow_format flow_format) | |
821 | { | |
822 | switch (flow_format) { | |
823 | case NXFF_OPENFLOW10: | |
7fa91113 BP |
824 | case NXFF_NXM: |
825 | return true; | |
826 | } | |
827 | ||
828 | return false; | |
829 | } | |
830 | ||
831 | const char * | |
832 | ofputil_flow_format_to_string(enum nx_flow_format flow_format) | |
833 | { | |
834 | switch (flow_format) { | |
835 | case NXFF_OPENFLOW10: | |
836 | return "openflow10"; | |
7fa91113 BP |
837 | case NXFF_NXM: |
838 | return "nxm"; | |
839 | default: | |
840 | NOT_REACHED(); | |
841 | } | |
842 | } | |
843 | ||
88ca35ee BP |
844 | int |
845 | ofputil_flow_format_from_string(const char *s) | |
846 | { | |
847 | return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10 | |
88ca35ee BP |
848 | : !strcmp(s, "nxm") ? NXFF_NXM |
849 | : -1); | |
850 | } | |
851 | ||
54834960 EJ |
852 | bool |
853 | ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format) | |
854 | { | |
855 | switch (packet_in_format) { | |
856 | case NXPIF_OPENFLOW10: | |
857 | case NXPIF_NXM: | |
858 | return true; | |
859 | } | |
860 | ||
861 | return false; | |
862 | } | |
863 | ||
864 | const char * | |
865 | ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format) | |
866 | { | |
867 | switch (packet_in_format) { | |
868 | case NXPIF_OPENFLOW10: | |
869 | return "openflow10"; | |
870 | case NXPIF_NXM: | |
871 | return "nxm"; | |
872 | default: | |
873 | NOT_REACHED(); | |
874 | } | |
875 | } | |
876 | ||
877 | int | |
878 | ofputil_packet_in_format_from_string(const char *s) | |
879 | { | |
880 | return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10 | |
881 | : !strcmp(s, "nxm") ? NXPIF_NXM | |
882 | : -1); | |
883 | } | |
884 | ||
88ca35ee BP |
885 | static bool |
886 | regs_fully_wildcarded(const struct flow_wildcards *wc) | |
887 | { | |
888 | int i; | |
889 | ||
890 | for (i = 0; i < FLOW_N_REGS; i++) { | |
891 | if (wc->reg_masks[i] != 0) { | |
892 | return false; | |
893 | } | |
894 | } | |
895 | return true; | |
896 | } | |
897 | ||
b78f6b77 BP |
898 | /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch |
899 | * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers, | |
900 | * or fixing the Ethernet multicast bit. Otherwise, it's better to use | |
901 | * NXFF_OPENFLOW10 for backward compatibility. */ | |
902 | enum nx_flow_format | |
903 | ofputil_min_flow_format(const struct cls_rule *rule) | |
8368c090 BP |
904 | { |
905 | const struct flow_wildcards *wc = &rule->wc; | |
8368c090 | 906 | |
2486e66a | 907 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7); |
a877206f | 908 | |
8368c090 BP |
909 | /* Only NXM supports separately wildcards the Ethernet multicast bit. */ |
910 | if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) { | |
b78f6b77 | 911 | return NXFF_NXM; |
8368c090 BP |
912 | } |
913 | ||
bad68a99 JP |
914 | /* Only NXM supports matching ARP hardware addresses. */ |
915 | if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) { | |
b78f6b77 | 916 | return NXFF_NXM; |
bad68a99 JP |
917 | } |
918 | ||
d31f1109 JP |
919 | /* Only NXM supports matching IPv6 traffic. */ |
920 | if (!(wc->wildcards & FWW_DL_TYPE) | |
921 | && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) { | |
b78f6b77 | 922 | return NXFF_NXM; |
d31f1109 JP |
923 | } |
924 | ||
8368c090 BP |
925 | /* Only NXM supports matching registers. */ |
926 | if (!regs_fully_wildcarded(wc)) { | |
b78f6b77 | 927 | return NXFF_NXM; |
8368c090 BP |
928 | } |
929 | ||
b78f6b77 BP |
930 | /* Only NXM supports matching tun_id. */ |
931 | if (wc->tun_id_mask != htonll(0)) { | |
932 | return NXFF_NXM; | |
8368c090 BP |
933 | } |
934 | ||
7257b535 | 935 | /* Only NXM supports matching fragments. */ |
eadef313 | 936 | if (wc->nw_frag_mask) { |
7257b535 BP |
937 | return NXFF_NXM; |
938 | } | |
939 | ||
fa8223b7 JP |
940 | /* Only NXM supports matching IPv6 flow label. */ |
941 | if (!(wc->wildcards & FWW_IPV6_LABEL)) { | |
942 | return NXFF_NXM; | |
943 | } | |
944 | ||
530180fd | 945 | /* Only NXM supports matching IP ECN bits. */ |
2486e66a | 946 | if (!(wc->wildcards & FWW_NW_ECN)) { |
530180fd JP |
947 | return NXFF_NXM; |
948 | } | |
949 | ||
a61680c6 JP |
950 | /* Only NXM supports matching IP TTL/hop limit. */ |
951 | if (!(wc->wildcards & FWW_NW_TTL)) { | |
952 | return NXFF_NXM; | |
953 | } | |
954 | ||
8368c090 | 955 | /* Other formats can express this rule. */ |
b78f6b77 | 956 | return NXFF_OPENFLOW10; |
88ca35ee BP |
957 | } |
958 | ||
959 | /* Returns an OpenFlow message that can be used to set the flow format to | |
960 | * 'flow_format'. */ | |
961 | struct ofpbuf * | |
962 | ofputil_make_set_flow_format(enum nx_flow_format flow_format) | |
963 | { | |
73dbf4ab | 964 | struct nx_set_flow_format *sff; |
88ca35ee BP |
965 | struct ofpbuf *msg; |
966 | ||
b78f6b77 BP |
967 | sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg); |
968 | sff->format = htonl(flow_format); | |
88ca35ee BP |
969 | |
970 | return msg; | |
971 | } | |
972 | ||
54834960 EJ |
973 | struct ofpbuf * |
974 | ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format) | |
975 | { | |
73dbf4ab | 976 | struct nx_set_packet_in_format *spif; |
54834960 EJ |
977 | struct ofpbuf *msg; |
978 | ||
979 | spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg); | |
980 | spif->format = htonl(packet_in_format); | |
981 | ||
982 | return msg; | |
983 | } | |
984 | ||
6c1491fb BP |
985 | /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id |
986 | * extension on or off (according to 'flow_mod_table_id'). */ | |
987 | struct ofpbuf * | |
988 | ofputil_make_flow_mod_table_id(bool flow_mod_table_id) | |
989 | { | |
73dbf4ab | 990 | struct nx_flow_mod_table_id *nfmti; |
6c1491fb BP |
991 | struct ofpbuf *msg; |
992 | ||
993 | nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg); | |
994 | nfmti->set = flow_mod_table_id; | |
995 | return msg; | |
996 | } | |
997 | ||
7fa91113 BP |
998 | /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract |
999 | * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error | |
1000 | * code. | |
1001 | * | |
6c1491fb BP |
1002 | * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is |
1003 | * enabled, false otherwise. | |
1004 | * | |
2e4f5fcf | 1005 | * Does not validate the flow_mod actions. */ |
90bf1e07 | 1006 | enum ofperr |
a9a2da38 BP |
1007 | ofputil_decode_flow_mod(struct ofputil_flow_mod *fm, |
1008 | const struct ofp_header *oh, bool flow_mod_table_id) | |
2e4f5fcf BP |
1009 | { |
1010 | const struct ofputil_msg_type *type; | |
6c1491fb | 1011 | uint16_t command; |
2e4f5fcf BP |
1012 | struct ofpbuf b; |
1013 | ||
2013493b | 1014 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf BP |
1015 | |
1016 | ofputil_decode_msg_type(oh, &type); | |
1017 | if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) { | |
1018 | /* Standard OpenFlow flow_mod. */ | |
2e4f5fcf | 1019 | const struct ofp_flow_mod *ofm; |
1c0b7503 | 1020 | uint16_t priority; |
90bf1e07 | 1021 | enum ofperr error; |
2e4f5fcf BP |
1022 | |
1023 | /* Dissect the message. */ | |
bbc32a88 | 1024 | ofm = ofpbuf_pull(&b, sizeof *ofm); |
2e4f5fcf BP |
1025 | error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions); |
1026 | if (error) { | |
1027 | return error; | |
1028 | } | |
1029 | ||
1c0b7503 BP |
1030 | /* Set priority based on original wildcards. Normally we'd allow |
1031 | * ofputil_cls_rule_from_match() to do this for us, but | |
b459a924 | 1032 | * ofputil_normalize_rule() can put wildcards where the original flow |
1c0b7503 BP |
1033 | * didn't have them. */ |
1034 | priority = ntohs(ofm->priority); | |
1035 | if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) { | |
1036 | priority = UINT16_MAX; | |
1037 | } | |
1038 | ||
b459a924 BP |
1039 | /* Translate the rule. */ |
1040 | ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr); | |
1041 | ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10); | |
2e4f5fcf BP |
1042 | |
1043 | /* Translate the message. */ | |
2e4f5fcf | 1044 | fm->cookie = ofm->cookie; |
e729e793 | 1045 | fm->cookie_mask = htonll(UINT64_MAX); |
6c1491fb | 1046 | command = ntohs(ofm->command); |
2e4f5fcf BP |
1047 | fm->idle_timeout = ntohs(ofm->idle_timeout); |
1048 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
1049 | fm->buffer_id = ntohl(ofm->buffer_id); | |
1050 | fm->out_port = ntohs(ofm->out_port); | |
1051 | fm->flags = ntohs(ofm->flags); | |
1052 | } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) { | |
1053 | /* Nicira extended flow_mod. */ | |
1054 | const struct nx_flow_mod *nfm; | |
90bf1e07 | 1055 | enum ofperr error; |
2e4f5fcf BP |
1056 | |
1057 | /* Dissect the message. */ | |
bbc32a88 | 1058 | nfm = ofpbuf_pull(&b, sizeof *nfm); |
2e4f5fcf | 1059 | error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority), |
e729e793 | 1060 | &fm->cr, &fm->cookie, &fm->cookie_mask); |
2e4f5fcf BP |
1061 | if (error) { |
1062 | return error; | |
1063 | } | |
1064 | error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions); | |
1065 | if (error) { | |
1066 | return error; | |
1067 | } | |
1068 | ||
1069 | /* Translate the message. */ | |
6c1491fb | 1070 | command = ntohs(nfm->command); |
e729e793 JP |
1071 | if (command == OFPFC_ADD) { |
1072 | if (fm->cookie_mask) { | |
1073 | /* The "NXM_NX_COOKIE*" matches are not valid for flow | |
1074 | * additions. Additions must use the "cookie" field of | |
1075 | * the "nx_flow_mod" structure. */ | |
90bf1e07 | 1076 | return OFPERR_NXBRC_NXM_INVALID; |
e729e793 JP |
1077 | } else { |
1078 | fm->cookie = nfm->cookie; | |
1079 | fm->cookie_mask = htonll(UINT64_MAX); | |
1080 | } | |
1081 | } | |
2e4f5fcf BP |
1082 | fm->idle_timeout = ntohs(nfm->idle_timeout); |
1083 | fm->hard_timeout = ntohs(nfm->hard_timeout); | |
1084 | fm->buffer_id = ntohl(nfm->buffer_id); | |
1085 | fm->out_port = ntohs(nfm->out_port); | |
1086 | fm->flags = ntohs(nfm->flags); | |
1087 | } else { | |
1088 | NOT_REACHED(); | |
1089 | } | |
1090 | ||
6c1491fb BP |
1091 | if (flow_mod_table_id) { |
1092 | fm->command = command & 0xff; | |
1093 | fm->table_id = command >> 8; | |
1094 | } else { | |
1095 | fm->command = command; | |
1096 | fm->table_id = 0xff; | |
1097 | } | |
1098 | ||
2e4f5fcf BP |
1099 | return 0; |
1100 | } | |
1101 | ||
1102 | /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to | |
6c1491fb BP |
1103 | * 'flow_format' and returns the message. |
1104 | * | |
1105 | * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is | |
1106 | * enabled, false otherwise. */ | |
2e4f5fcf | 1107 | struct ofpbuf * |
a9a2da38 | 1108 | ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm, |
6c1491fb BP |
1109 | enum nx_flow_format flow_format, |
1110 | bool flow_mod_table_id) | |
2e4f5fcf BP |
1111 | { |
1112 | size_t actions_len = fm->n_actions * sizeof *fm->actions; | |
1113 | struct ofpbuf *msg; | |
6c1491fb BP |
1114 | uint16_t command; |
1115 | ||
1116 | command = (flow_mod_table_id | |
1117 | ? (fm->command & 0xff) | (fm->table_id << 8) | |
1118 | : fm->command); | |
2e4f5fcf | 1119 | |
b78f6b77 | 1120 | if (flow_format == NXFF_OPENFLOW10) { |
2e4f5fcf BP |
1121 | struct ofp_flow_mod *ofm; |
1122 | ||
1123 | msg = ofpbuf_new(sizeof *ofm + actions_len); | |
1124 | ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg); | |
b78f6b77 BP |
1125 | ofputil_cls_rule_to_match(&fm->cr, &ofm->match); |
1126 | ofm->cookie = fm->cookie; | |
05411977 | 1127 | ofm->command = htons(command); |
2e4f5fcf BP |
1128 | ofm->idle_timeout = htons(fm->idle_timeout); |
1129 | ofm->hard_timeout = htons(fm->hard_timeout); | |
1130 | ofm->priority = htons(fm->cr.priority); | |
1131 | ofm->buffer_id = htonl(fm->buffer_id); | |
1132 | ofm->out_port = htons(fm->out_port); | |
1133 | ofm->flags = htons(fm->flags); | |
1134 | } else if (flow_format == NXFF_NXM) { | |
1135 | struct nx_flow_mod *nfm; | |
1136 | int match_len; | |
1137 | ||
1138 | msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len); | |
1139 | put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg); | |
2e4f5fcf | 1140 | nfm = msg->data; |
6c1491fb | 1141 | nfm->command = htons(command); |
e729e793 JP |
1142 | if (command == OFPFC_ADD) { |
1143 | nfm->cookie = fm->cookie; | |
1144 | match_len = nx_put_match(msg, &fm->cr, 0, 0); | |
1145 | } else { | |
1146 | nfm->cookie = 0; | |
1147 | match_len = nx_put_match(msg, &fm->cr, | |
1148 | fm->cookie, fm->cookie_mask); | |
1149 | } | |
2e4f5fcf BP |
1150 | nfm->idle_timeout = htons(fm->idle_timeout); |
1151 | nfm->hard_timeout = htons(fm->hard_timeout); | |
1152 | nfm->priority = htons(fm->cr.priority); | |
1153 | nfm->buffer_id = htonl(fm->buffer_id); | |
1154 | nfm->out_port = htons(fm->out_port); | |
1155 | nfm->flags = htons(fm->flags); | |
1156 | nfm->match_len = htons(match_len); | |
1157 | } else { | |
1158 | NOT_REACHED(); | |
1159 | } | |
1160 | ||
1161 | ofpbuf_put(msg, fm->actions, actions_len); | |
1162 | update_openflow_length(msg); | |
1163 | return msg; | |
1164 | } | |
1165 | ||
90bf1e07 | 1166 | static enum ofperr |
81d1ea94 | 1167 | ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr, |
2e4f5fcf | 1168 | const struct ofp_header *oh, |
2e4f5fcf BP |
1169 | bool aggregate) |
1170 | { | |
63f2140a BP |
1171 | const struct ofp_flow_stats_request *ofsr = |
1172 | (const struct ofp_flow_stats_request *) oh; | |
2e4f5fcf BP |
1173 | |
1174 | fsr->aggregate = aggregate; | |
b78f6b77 | 1175 | ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match); |
2e4f5fcf BP |
1176 | fsr->out_port = ntohs(ofsr->out_port); |
1177 | fsr->table_id = ofsr->table_id; | |
e729e793 | 1178 | fsr->cookie = fsr->cookie_mask = htonll(0); |
2e4f5fcf BP |
1179 | |
1180 | return 0; | |
1181 | } | |
1182 | ||
90bf1e07 | 1183 | static enum ofperr |
81d1ea94 | 1184 | ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr, |
2e4f5fcf BP |
1185 | const struct ofp_header *oh, |
1186 | bool aggregate) | |
1187 | { | |
1188 | const struct nx_flow_stats_request *nfsr; | |
1189 | struct ofpbuf b; | |
90bf1e07 | 1190 | enum ofperr error; |
2e4f5fcf | 1191 | |
2013493b | 1192 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf | 1193 | |
bbc32a88 | 1194 | nfsr = ofpbuf_pull(&b, sizeof *nfsr); |
e729e793 JP |
1195 | error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match, |
1196 | &fsr->cookie, &fsr->cookie_mask); | |
2e4f5fcf BP |
1197 | if (error) { |
1198 | return error; | |
1199 | } | |
1200 | if (b.size) { | |
90bf1e07 | 1201 | return OFPERR_OFPBRC_BAD_LEN; |
2e4f5fcf BP |
1202 | } |
1203 | ||
1204 | fsr->aggregate = aggregate; | |
1205 | fsr->out_port = ntohs(nfsr->out_port); | |
1206 | fsr->table_id = nfsr->table_id; | |
1207 | ||
1208 | return 0; | |
1209 | } | |
1210 | ||
1211 | /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE | |
b78f6b77 BP |
1212 | * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if |
1213 | * successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 1214 | enum ofperr |
81d1ea94 | 1215 | ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr, |
b78f6b77 | 1216 | const struct ofp_header *oh) |
2e4f5fcf BP |
1217 | { |
1218 | const struct ofputil_msg_type *type; | |
1219 | struct ofpbuf b; | |
1220 | int code; | |
1221 | ||
2013493b | 1222 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2e4f5fcf BP |
1223 | |
1224 | ofputil_decode_msg_type(oh, &type); | |
1225 | code = ofputil_msg_type_code(type); | |
1226 | switch (code) { | |
1227 | case OFPUTIL_OFPST_FLOW_REQUEST: | |
b78f6b77 | 1228 | return ofputil_decode_ofpst_flow_request(fsr, oh, false); |
2e4f5fcf BP |
1229 | |
1230 | case OFPUTIL_OFPST_AGGREGATE_REQUEST: | |
b78f6b77 | 1231 | return ofputil_decode_ofpst_flow_request(fsr, oh, true); |
2e4f5fcf BP |
1232 | |
1233 | case OFPUTIL_NXST_FLOW_REQUEST: | |
1234 | return ofputil_decode_nxst_flow_request(fsr, oh, false); | |
1235 | ||
1236 | case OFPUTIL_NXST_AGGREGATE_REQUEST: | |
1237 | return ofputil_decode_nxst_flow_request(fsr, oh, true); | |
1238 | ||
1239 | default: | |
1240 | /* Hey, the caller lied. */ | |
1241 | NOT_REACHED(); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW, | |
4ffd1b43 | 1246 | * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to |
2e4f5fcf BP |
1247 | * 'flow_format', and returns the message. */ |
1248 | struct ofpbuf * | |
81d1ea94 | 1249 | ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr, |
2e4f5fcf BP |
1250 | enum nx_flow_format flow_format) |
1251 | { | |
1252 | struct ofpbuf *msg; | |
1253 | ||
b78f6b77 | 1254 | if (flow_format == NXFF_OPENFLOW10) { |
2e4f5fcf BP |
1255 | struct ofp_flow_stats_request *ofsr; |
1256 | int type; | |
1257 | ||
2e4f5fcf | 1258 | type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW; |
63f2140a | 1259 | ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg); |
b78f6b77 | 1260 | ofputil_cls_rule_to_match(&fsr->match, &ofsr->match); |
2e4f5fcf BP |
1261 | ofsr->table_id = fsr->table_id; |
1262 | ofsr->out_port = htons(fsr->out_port); | |
1263 | } else if (flow_format == NXFF_NXM) { | |
1264 | struct nx_flow_stats_request *nfsr; | |
1265 | int match_len; | |
9fb7fa87 | 1266 | int subtype; |
2e4f5fcf | 1267 | |
9fb7fa87 | 1268 | subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW; |
63f2140a | 1269 | ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg); |
e729e793 JP |
1270 | match_len = nx_put_match(msg, &fsr->match, |
1271 | fsr->cookie, fsr->cookie_mask); | |
2e4f5fcf BP |
1272 | |
1273 | nfsr = msg->data; | |
1274 | nfsr->out_port = htons(fsr->out_port); | |
1275 | nfsr->match_len = htons(match_len); | |
1276 | nfsr->table_id = fsr->table_id; | |
1277 | } else { | |
1278 | NOT_REACHED(); | |
1279 | } | |
1280 | ||
1281 | return msg; | |
1282 | } | |
d1e2cf21 | 1283 | |
4ffd1b43 | 1284 | /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract |
b78f6b77 | 1285 | * ofputil_flow_stats in 'fs'. |
4ffd1b43 BP |
1286 | * |
1287 | * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single | |
1288 | * OpenFlow message. Calling this function multiple times for a single 'msg' | |
1289 | * iterates through the replies. The caller must initially leave 'msg''s layer | |
1290 | * pointers null and not modify them between calls. | |
1291 | * | |
1292 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
1293 | * otherwise a positive errno value. */ | |
1294 | int | |
1295 | ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs, | |
b78f6b77 | 1296 | struct ofpbuf *msg) |
4ffd1b43 BP |
1297 | { |
1298 | const struct ofputil_msg_type *type; | |
1299 | int code; | |
1300 | ||
1301 | ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type); | |
1302 | code = ofputil_msg_type_code(type); | |
1303 | if (!msg->l2) { | |
1304 | msg->l2 = msg->data; | |
1305 | if (code == OFPUTIL_OFPST_FLOW_REPLY) { | |
28c8bad1 | 1306 | ofpbuf_pull(msg, sizeof(struct ofp_stats_msg)); |
4ffd1b43 BP |
1307 | } else if (code == OFPUTIL_NXST_FLOW_REPLY) { |
1308 | ofpbuf_pull(msg, sizeof(struct nicira_stats_msg)); | |
1309 | } else { | |
1310 | NOT_REACHED(); | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | if (!msg->size) { | |
1315 | return EOF; | |
1316 | } else if (code == OFPUTIL_OFPST_FLOW_REPLY) { | |
1317 | const struct ofp_flow_stats *ofs; | |
1318 | size_t length; | |
1319 | ||
1320 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
1321 | if (!ofs) { | |
1322 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover " | |
1323 | "bytes at end", msg->size); | |
1324 | return EINVAL; | |
1325 | } | |
1326 | ||
1327 | length = ntohs(ofs->length); | |
1328 | if (length < sizeof *ofs) { | |
1329 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
1330 | "length %zu", length); | |
1331 | return EINVAL; | |
1332 | } | |
1333 | ||
1334 | if (ofputil_pull_actions(msg, length - sizeof *ofs, | |
1335 | &fs->actions, &fs->n_actions)) { | |
1336 | return EINVAL; | |
1337 | } | |
1338 | ||
1339 | fs->cookie = get_32aligned_be64(&ofs->cookie); | |
1340 | ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority), | |
b78f6b77 | 1341 | &fs->rule); |
4ffd1b43 BP |
1342 | fs->table_id = ofs->table_id; |
1343 | fs->duration_sec = ntohl(ofs->duration_sec); | |
1344 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
1345 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
1346 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
1347 | fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count)); | |
1348 | fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count)); | |
1349 | } else if (code == OFPUTIL_NXST_FLOW_REPLY) { | |
1350 | const struct nx_flow_stats *nfs; | |
1351 | size_t match_len, length; | |
1352 | ||
1353 | nfs = ofpbuf_try_pull(msg, sizeof *nfs); | |
1354 | if (!nfs) { | |
1355 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover " | |
1356 | "bytes at end", msg->size); | |
1357 | return EINVAL; | |
1358 | } | |
1359 | ||
1360 | length = ntohs(nfs->length); | |
1361 | match_len = ntohs(nfs->match_len); | |
1362 | if (length < sizeof *nfs + ROUND_UP(match_len, 8)) { | |
1363 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu " | |
1364 | "claims invalid length %zu", match_len, length); | |
1365 | return EINVAL; | |
1366 | } | |
e729e793 JP |
1367 | if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule, |
1368 | NULL, NULL)) { | |
4ffd1b43 BP |
1369 | return EINVAL; |
1370 | } | |
1371 | ||
1372 | if (ofputil_pull_actions(msg, | |
1373 | length - sizeof *nfs - ROUND_UP(match_len, 8), | |
1374 | &fs->actions, &fs->n_actions)) { | |
1375 | return EINVAL; | |
1376 | } | |
1377 | ||
1378 | fs->cookie = nfs->cookie; | |
1379 | fs->table_id = nfs->table_id; | |
1380 | fs->duration_sec = ntohl(nfs->duration_sec); | |
1381 | fs->duration_nsec = ntohl(nfs->duration_nsec); | |
1382 | fs->idle_timeout = ntohs(nfs->idle_timeout); | |
1383 | fs->hard_timeout = ntohs(nfs->hard_timeout); | |
1384 | fs->packet_count = ntohll(nfs->packet_count); | |
1385 | fs->byte_count = ntohll(nfs->byte_count); | |
1386 | } else { | |
1387 | NOT_REACHED(); | |
1388 | } | |
1389 | ||
1390 | return 0; | |
1391 | } | |
1392 | ||
5e9d0469 BP |
1393 | /* Returns 'count' unchanged except that UINT64_MAX becomes 0. |
1394 | * | |
1395 | * We use this in situations where OVS internally uses UINT64_MAX to mean | |
1396 | * "value unknown" but OpenFlow 1.0 does not define any unknown value. */ | |
1397 | static uint64_t | |
1398 | unknown_to_zero(uint64_t count) | |
1399 | { | |
1400 | return count != UINT64_MAX ? count : 0; | |
1401 | } | |
1402 | ||
349adfb2 BP |
1403 | /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to |
1404 | * those already present in the list of ofpbufs in 'replies'. 'replies' should | |
1405 | * have been initialized with ofputil_start_stats_reply(). */ | |
1406 | void | |
1407 | ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs, | |
1408 | struct list *replies) | |
1409 | { | |
1410 | size_t act_len = fs->n_actions * sizeof *fs->actions; | |
1411 | const struct ofp_stats_msg *osm; | |
1412 | ||
1413 | osm = ofpbuf_from_list(list_back(replies))->data; | |
1414 | if (osm->type == htons(OFPST_FLOW)) { | |
1415 | size_t len = offsetof(struct ofp_flow_stats, actions) + act_len; | |
1416 | struct ofp_flow_stats *ofs; | |
1417 | ||
1418 | ofs = ofputil_append_stats_reply(len, replies); | |
1419 | ofs->length = htons(len); | |
1420 | ofs->table_id = fs->table_id; | |
1421 | ofs->pad = 0; | |
1422 | ofputil_cls_rule_to_match(&fs->rule, &ofs->match); | |
1423 | ofs->duration_sec = htonl(fs->duration_sec); | |
1424 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
1425 | ofs->priority = htons(fs->rule.priority); | |
1426 | ofs->idle_timeout = htons(fs->idle_timeout); | |
1427 | ofs->hard_timeout = htons(fs->hard_timeout); | |
1428 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
1429 | put_32aligned_be64(&ofs->cookie, fs->cookie); | |
5e9d0469 BP |
1430 | put_32aligned_be64(&ofs->packet_count, |
1431 | htonll(unknown_to_zero(fs->packet_count))); | |
1432 | put_32aligned_be64(&ofs->byte_count, | |
1433 | htonll(unknown_to_zero(fs->byte_count))); | |
349adfb2 BP |
1434 | memcpy(ofs->actions, fs->actions, act_len); |
1435 | } else if (osm->type == htons(OFPST_VENDOR)) { | |
1436 | struct nx_flow_stats *nfs; | |
1437 | struct ofpbuf *msg; | |
1438 | size_t start_len; | |
1439 | ||
1440 | msg = ofputil_reserve_stats_reply( | |
1441 | sizeof *nfs + NXM_MAX_LEN + act_len, replies); | |
1442 | start_len = msg->size; | |
1443 | ||
1444 | nfs = ofpbuf_put_uninit(msg, sizeof *nfs); | |
1445 | nfs->table_id = fs->table_id; | |
1446 | nfs->pad = 0; | |
1447 | nfs->duration_sec = htonl(fs->duration_sec); | |
1448 | nfs->duration_nsec = htonl(fs->duration_nsec); | |
1449 | nfs->priority = htons(fs->rule.priority); | |
1450 | nfs->idle_timeout = htons(fs->idle_timeout); | |
1451 | nfs->hard_timeout = htons(fs->hard_timeout); | |
e729e793 | 1452 | nfs->match_len = htons(nx_put_match(msg, &fs->rule, 0, 0)); |
349adfb2 BP |
1453 | memset(nfs->pad2, 0, sizeof nfs->pad2); |
1454 | nfs->cookie = fs->cookie; | |
1455 | nfs->packet_count = htonll(fs->packet_count); | |
1456 | nfs->byte_count = htonll(fs->byte_count); | |
1457 | ofpbuf_put(msg, fs->actions, act_len); | |
1458 | nfs->length = htons(msg->size - start_len); | |
1459 | } else { | |
1460 | NOT_REACHED(); | |
1461 | } | |
1462 | } | |
1463 | ||
76c93b22 BP |
1464 | /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or |
1465 | * NXST_AGGREGATE reply according to 'flow_format', and returns the message. */ | |
1466 | struct ofpbuf * | |
1467 | ofputil_encode_aggregate_stats_reply( | |
1468 | const struct ofputil_aggregate_stats *stats, | |
1469 | const struct ofp_stats_msg *request) | |
1470 | { | |
1471 | struct ofpbuf *msg; | |
1472 | ||
1473 | if (request->type == htons(OFPST_AGGREGATE)) { | |
1474 | struct ofp_aggregate_stats_reply *asr; | |
1475 | ||
1476 | asr = ofputil_make_stats_reply(sizeof *asr, request, &msg); | |
5e9d0469 BP |
1477 | put_32aligned_be64(&asr->packet_count, |
1478 | htonll(unknown_to_zero(stats->packet_count))); | |
1479 | put_32aligned_be64(&asr->byte_count, | |
1480 | htonll(unknown_to_zero(stats->byte_count))); | |
76c93b22 BP |
1481 | asr->flow_count = htonl(stats->flow_count); |
1482 | } else if (request->type == htons(OFPST_VENDOR)) { | |
1483 | struct nx_aggregate_stats_reply *nasr; | |
1484 | ||
1485 | nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg); | |
1486 | assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE)); | |
1487 | nasr->packet_count = htonll(stats->packet_count); | |
1488 | nasr->byte_count = htonll(stats->byte_count); | |
1489 | nasr->flow_count = htonl(stats->flow_count); | |
1490 | } else { | |
1491 | NOT_REACHED(); | |
1492 | } | |
1493 | ||
1494 | return msg; | |
1495 | } | |
1496 | ||
b78f6b77 BP |
1497 | /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an |
1498 | * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise | |
1499 | * an OpenFlow error code. */ | |
90bf1e07 | 1500 | enum ofperr |
9b045a0c | 1501 | ofputil_decode_flow_removed(struct ofputil_flow_removed *fr, |
b78f6b77 | 1502 | const struct ofp_header *oh) |
9b045a0c BP |
1503 | { |
1504 | const struct ofputil_msg_type *type; | |
1505 | enum ofputil_msg_code code; | |
1506 | ||
1507 | ofputil_decode_msg_type(oh, &type); | |
1508 | code = ofputil_msg_type_code(type); | |
1509 | if (code == OFPUTIL_OFPT_FLOW_REMOVED) { | |
1510 | const struct ofp_flow_removed *ofr; | |
1511 | ||
1512 | ofr = (const struct ofp_flow_removed *) oh; | |
1513 | ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority), | |
b78f6b77 | 1514 | &fr->rule); |
9b045a0c BP |
1515 | fr->cookie = ofr->cookie; |
1516 | fr->reason = ofr->reason; | |
1517 | fr->duration_sec = ntohl(ofr->duration_sec); | |
1518 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
1519 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
1520 | fr->packet_count = ntohll(ofr->packet_count); | |
1521 | fr->byte_count = ntohll(ofr->byte_count); | |
1522 | } else if (code == OFPUTIL_NXT_FLOW_REMOVED) { | |
1523 | struct nx_flow_removed *nfr; | |
1524 | struct ofpbuf b; | |
1525 | int error; | |
1526 | ||
1527 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1528 | ||
1529 | nfr = ofpbuf_pull(&b, sizeof *nfr); | |
1530 | error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority), | |
e729e793 | 1531 | &fr->rule, NULL, NULL); |
9b045a0c BP |
1532 | if (error) { |
1533 | return error; | |
1534 | } | |
1535 | if (b.size) { | |
90bf1e07 | 1536 | return OFPERR_OFPBRC_BAD_LEN; |
9b045a0c BP |
1537 | } |
1538 | ||
1539 | fr->cookie = nfr->cookie; | |
1540 | fr->reason = nfr->reason; | |
1541 | fr->duration_sec = ntohl(nfr->duration_sec); | |
1542 | fr->duration_nsec = ntohl(nfr->duration_nsec); | |
1543 | fr->idle_timeout = ntohs(nfr->idle_timeout); | |
1544 | fr->packet_count = ntohll(nfr->packet_count); | |
1545 | fr->byte_count = ntohll(nfr->byte_count); | |
1546 | } else { | |
1547 | NOT_REACHED(); | |
1548 | } | |
1549 | ||
1550 | return 0; | |
1551 | } | |
1552 | ||
588cd7b5 BP |
1553 | /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or |
1554 | * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the | |
1555 | * message. */ | |
1556 | struct ofpbuf * | |
1557 | ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr, | |
1558 | enum nx_flow_format flow_format) | |
1559 | { | |
1560 | struct ofpbuf *msg; | |
1561 | ||
b78f6b77 | 1562 | if (flow_format == NXFF_OPENFLOW10) { |
588cd7b5 BP |
1563 | struct ofp_flow_removed *ofr; |
1564 | ||
1565 | ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0), | |
1566 | &msg); | |
b78f6b77 | 1567 | ofputil_cls_rule_to_match(&fr->rule, &ofr->match); |
7fb563b9 | 1568 | ofr->cookie = fr->cookie; |
588cd7b5 BP |
1569 | ofr->priority = htons(fr->rule.priority); |
1570 | ofr->reason = fr->reason; | |
1571 | ofr->duration_sec = htonl(fr->duration_sec); | |
1572 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
1573 | ofr->idle_timeout = htons(fr->idle_timeout); | |
5e9d0469 BP |
1574 | ofr->packet_count = htonll(unknown_to_zero(fr->packet_count)); |
1575 | ofr->byte_count = htonll(unknown_to_zero(fr->byte_count)); | |
588cd7b5 BP |
1576 | } else if (flow_format == NXFF_NXM) { |
1577 | struct nx_flow_removed *nfr; | |
1578 | int match_len; | |
1579 | ||
1580 | make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg); | |
e729e793 | 1581 | match_len = nx_put_match(msg, &fr->rule, 0, 0); |
588cd7b5 BP |
1582 | |
1583 | nfr = msg->data; | |
1584 | nfr->cookie = fr->cookie; | |
1585 | nfr->priority = htons(fr->rule.priority); | |
1586 | nfr->reason = fr->reason; | |
1587 | nfr->duration_sec = htonl(fr->duration_sec); | |
1588 | nfr->duration_nsec = htonl(fr->duration_nsec); | |
1589 | nfr->idle_timeout = htons(fr->idle_timeout); | |
1590 | nfr->match_len = htons(match_len); | |
1591 | nfr->packet_count = htonll(fr->packet_count); | |
1592 | nfr->byte_count = htonll(fr->byte_count); | |
1593 | } else { | |
1594 | NOT_REACHED(); | |
1595 | } | |
1596 | ||
1597 | return msg; | |
1598 | } | |
1599 | ||
65120a8a EJ |
1600 | int |
1601 | ofputil_decode_packet_in(struct ofputil_packet_in *pin, | |
1602 | const struct ofp_header *oh) | |
1603 | { | |
1604 | const struct ofputil_msg_type *type; | |
1605 | enum ofputil_msg_code code; | |
1606 | ||
1607 | ofputil_decode_msg_type(oh, &type); | |
1608 | code = ofputil_msg_type_code(type); | |
1609 | memset(pin, 0, sizeof *pin); | |
1610 | ||
1611 | if (code == OFPUTIL_OFPT_PACKET_IN) { | |
1612 | const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh; | |
1613 | ||
1614 | pin->packet = opi->data; | |
1615 | pin->packet_len = ntohs(opi->header.length) | |
1616 | - offsetof(struct ofp_packet_in, data); | |
1617 | ||
5d6c3af0 | 1618 | pin->fmd.in_port = ntohs(opi->in_port); |
65120a8a EJ |
1619 | pin->reason = opi->reason; |
1620 | pin->buffer_id = ntohl(opi->buffer_id); | |
1621 | pin->total_len = ntohs(opi->total_len); | |
54834960 | 1622 | } else if (code == OFPUTIL_NXT_PACKET_IN) { |
73dbf4ab | 1623 | const struct nx_packet_in *npi; |
54834960 EJ |
1624 | struct cls_rule rule; |
1625 | struct ofpbuf b; | |
1626 | int error; | |
1627 | ||
1628 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1629 | ||
1630 | npi = ofpbuf_pull(&b, sizeof *npi); | |
1631 | error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL, | |
1632 | NULL); | |
1633 | if (error) { | |
1634 | return error; | |
1635 | } | |
1636 | ||
1637 | if (!ofpbuf_try_pull(&b, 2)) { | |
90bf1e07 | 1638 | return OFPERR_OFPBRC_BAD_LEN; |
54834960 EJ |
1639 | } |
1640 | ||
1641 | pin->packet = b.data; | |
1642 | pin->packet_len = b.size; | |
1643 | pin->reason = npi->reason; | |
1644 | pin->table_id = npi->table_id; | |
1645 | pin->cookie = npi->cookie; | |
1646 | ||
1647 | pin->fmd.in_port = rule.flow.in_port; | |
1648 | ||
1649 | pin->fmd.tun_id = rule.flow.tun_id; | |
1650 | pin->fmd.tun_id_mask = rule.wc.tun_id_mask; | |
1651 | ||
1652 | memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs); | |
1653 | memcpy(pin->fmd.reg_masks, rule.wc.reg_masks, | |
1654 | sizeof pin->fmd.reg_masks); | |
1655 | ||
1656 | pin->buffer_id = ntohl(npi->buffer_id); | |
1657 | pin->total_len = ntohs(npi->total_len); | |
65120a8a EJ |
1658 | } else { |
1659 | NOT_REACHED(); | |
1660 | } | |
1661 | ||
1662 | return 0; | |
1663 | } | |
1664 | ||
54834960 EJ |
1665 | /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message |
1666 | * in the format specified by 'packet_in_format'. */ | |
ebb57021 | 1667 | struct ofpbuf * |
54834960 EJ |
1668 | ofputil_encode_packet_in(const struct ofputil_packet_in *pin, |
1669 | enum nx_packet_in_format packet_in_format) | |
ebb57021 | 1670 | { |
54834960 EJ |
1671 | size_t send_len = MIN(pin->send_len, pin->packet_len); |
1672 | struct ofpbuf *packet; | |
ebb57021 BP |
1673 | |
1674 | /* Add OFPT_PACKET_IN. */ | |
54834960 EJ |
1675 | if (packet_in_format == NXPIF_OPENFLOW10) { |
1676 | size_t header_len = offsetof(struct ofp_packet_in, data); | |
1677 | struct ofp_packet_in *opi; | |
1678 | ||
1679 | packet = ofpbuf_new(send_len + header_len); | |
1680 | opi = ofpbuf_put_zeros(packet, header_len); | |
1681 | opi->header.version = OFP_VERSION; | |
1682 | opi->header.type = OFPT_PACKET_IN; | |
1683 | opi->total_len = htons(pin->total_len); | |
1684 | opi->in_port = htons(pin->fmd.in_port); | |
1685 | opi->reason = pin->reason; | |
1686 | opi->buffer_id = htonl(pin->buffer_id); | |
1687 | ||
1688 | ofpbuf_put(packet, pin->packet, send_len); | |
1689 | } else if (packet_in_format == NXPIF_NXM) { | |
73dbf4ab | 1690 | struct nx_packet_in *npi; |
54834960 EJ |
1691 | struct cls_rule rule; |
1692 | size_t match_len; | |
1693 | size_t i; | |
1694 | ||
1695 | /* Estimate of required PACKET_IN length includes the NPI header, space | |
1696 | * for the match (2 times sizeof the metadata seems like enough), 2 | |
1697 | * bytes for padding, and the packet length. */ | |
1698 | packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2 | |
1699 | + 2 + send_len); | |
1700 | ||
1701 | cls_rule_init_catchall(&rule, 0); | |
1702 | cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id, | |
1703 | pin->fmd.tun_id_mask); | |
1704 | ||
1705 | for (i = 0; i < FLOW_N_REGS; i++) { | |
1706 | cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i], | |
1707 | pin->fmd.reg_masks[i]); | |
1708 | } | |
1709 | ||
1710 | cls_rule_set_in_port(&rule, pin->fmd.in_port); | |
1711 | ||
1712 | ofpbuf_put_zeros(packet, sizeof *npi); | |
1713 | match_len = nx_put_match(packet, &rule, 0, 0); | |
1714 | ofpbuf_put_zeros(packet, 2); | |
1715 | ofpbuf_put(packet, pin->packet, send_len); | |
1716 | ||
1717 | npi = packet->data; | |
1718 | npi->nxh.header.version = OFP_VERSION; | |
1719 | npi->nxh.header.type = OFPT_VENDOR; | |
1720 | npi->nxh.vendor = htonl(NX_VENDOR_ID); | |
1721 | npi->nxh.subtype = htonl(NXT_PACKET_IN); | |
1722 | ||
1723 | npi->buffer_id = htonl(pin->buffer_id); | |
1724 | npi->total_len = htons(pin->total_len); | |
1725 | npi->reason = pin->reason; | |
1726 | npi->table_id = pin->table_id; | |
1727 | npi->cookie = pin->cookie; | |
1728 | npi->match_len = htons(match_len); | |
1729 | } else { | |
1730 | NOT_REACHED(); | |
1731 | } | |
1732 | update_openflow_length(packet); | |
1733 | ||
1734 | return packet; | |
ebb57021 BP |
1735 | } |
1736 | ||
d1e2cf21 BP |
1737 | /* Returns a string representing the message type of 'type'. The string is the |
1738 | * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics | |
1739 | * messages, the constant is followed by "request" or "reply", | |
1740 | * e.g. "OFPST_AGGREGATE reply". */ | |
1741 | const char * | |
1742 | ofputil_msg_type_name(const struct ofputil_msg_type *type) | |
1743 | { | |
1744 | return type->name; | |
1745 | } | |
1746 | \f | |
fa37b408 BP |
1747 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of |
1748 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
1749 | * an arbitrary transaction id. Allocated bytes beyond the header, if any, are | |
1750 | * zeroed. | |
1751 | * | |
1752 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
1753 | * needed. | |
1754 | * | |
1755 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1756 | * message is later extended, the length should be updated with | |
1757 | * update_openflow_length() before sending. | |
1758 | * | |
1759 | * Returns the header. */ | |
1760 | void * | |
1761 | make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp) | |
1762 | { | |
1763 | *bufferp = ofpbuf_new(openflow_len); | |
1764 | return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp); | |
1765 | } | |
1766 | ||
0bd0c660 BP |
1767 | /* Similar to make_openflow() but creates a Nicira vendor extension message |
1768 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1769 | void * | |
1770 | make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp) | |
1771 | { | |
1772 | return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp); | |
1773 | } | |
1774 | ||
fa37b408 BP |
1775 | /* Allocates and stores in '*bufferp' a new ofpbuf with a size of |
1776 | * 'openflow_len', starting with an OpenFlow header with the given 'type' and | |
1777 | * transaction id 'xid'. Allocated bytes beyond the header, if any, are | |
1778 | * zeroed. | |
1779 | * | |
1780 | * The caller is responsible for freeing '*bufferp' when it is no longer | |
1781 | * needed. | |
1782 | * | |
1783 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1784 | * message is later extended, the length should be updated with | |
1785 | * update_openflow_length() before sending. | |
1786 | * | |
1787 | * Returns the header. */ | |
1788 | void * | |
44381c1b | 1789 | make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid, |
fa37b408 BP |
1790 | struct ofpbuf **bufferp) |
1791 | { | |
1792 | *bufferp = ofpbuf_new(openflow_len); | |
1793 | return put_openflow_xid(openflow_len, type, xid, *bufferp); | |
1794 | } | |
1795 | ||
0bd0c660 BP |
1796 | /* Similar to make_openflow_xid() but creates a Nicira vendor extension message |
1797 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1798 | void * | |
44381c1b | 1799 | make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid, |
0bd0c660 BP |
1800 | struct ofpbuf **bufferp) |
1801 | { | |
dfdfc8d4 BP |
1802 | *bufferp = ofpbuf_new(openflow_len); |
1803 | return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp); | |
0bd0c660 BP |
1804 | } |
1805 | ||
fa37b408 BP |
1806 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header |
1807 | * with the given 'type' and an arbitrary transaction id. Allocated bytes | |
1808 | * beyond the header, if any, are zeroed. | |
1809 | * | |
1810 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1811 | * message is later extended, the length should be updated with | |
1812 | * update_openflow_length() before sending. | |
1813 | * | |
1814 | * Returns the header. */ | |
1815 | void * | |
1816 | put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer) | |
1817 | { | |
1818 | return put_openflow_xid(openflow_len, type, alloc_xid(), buffer); | |
1819 | } | |
1820 | ||
1821 | /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header | |
1822 | * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond | |
1823 | * the header, if any, are zeroed. | |
1824 | * | |
1825 | * The OpenFlow header length is initially set to 'openflow_len'; if the | |
1826 | * message is later extended, the length should be updated with | |
1827 | * update_openflow_length() before sending. | |
1828 | * | |
1829 | * Returns the header. */ | |
1830 | void * | |
44381c1b | 1831 | put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid, |
fa37b408 BP |
1832 | struct ofpbuf *buffer) |
1833 | { | |
1834 | struct ofp_header *oh; | |
1835 | ||
1836 | assert(openflow_len >= sizeof *oh); | |
1837 | assert(openflow_len <= UINT16_MAX); | |
1838 | ||
1839 | oh = ofpbuf_put_uninit(buffer, openflow_len); | |
1840 | oh->version = OFP_VERSION; | |
1841 | oh->type = type; | |
1842 | oh->length = htons(openflow_len); | |
1843 | oh->xid = xid; | |
1844 | memset(oh + 1, 0, openflow_len - sizeof *oh); | |
1845 | return oh; | |
1846 | } | |
1847 | ||
dfdfc8d4 BP |
1848 | /* Similar to put_openflow() but append a Nicira vendor extension message with |
1849 | * the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1850 | void * | |
1851 | put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer) | |
1852 | { | |
1853 | return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer); | |
1854 | } | |
1855 | ||
1856 | /* Similar to put_openflow_xid() but append a Nicira vendor extension message | |
1857 | * with the specific 'subtype'. 'subtype' should be in host byte order. */ | |
1858 | void * | |
1859 | put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid, | |
1860 | struct ofpbuf *buffer) | |
1861 | { | |
1862 | struct nicira_header *nxh; | |
1863 | ||
1864 | nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer); | |
1865 | nxh->vendor = htonl(NX_VENDOR_ID); | |
1866 | nxh->subtype = htonl(subtype); | |
1867 | return nxh; | |
1868 | } | |
1869 | ||
fa37b408 BP |
1870 | /* Updates the 'length' field of the OpenFlow message in 'buffer' to |
1871 | * 'buffer->size'. */ | |
1872 | void | |
d295e8e9 | 1873 | update_openflow_length(struct ofpbuf *buffer) |
fa37b408 BP |
1874 | { |
1875 | struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh); | |
d295e8e9 | 1876 | oh->length = htons(buffer->size); |
fa37b408 BP |
1877 | } |
1878 | ||
63f2140a BP |
1879 | static void |
1880 | put_stats__(ovs_be32 xid, uint8_t ofp_type, | |
1881 | ovs_be16 ofpst_type, ovs_be32 nxst_subtype, | |
1882 | struct ofpbuf *msg) | |
1883 | { | |
1884 | if (ofpst_type == htons(OFPST_VENDOR)) { | |
1885 | struct nicira_stats_msg *nsm; | |
1886 | ||
1887 | nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg); | |
1888 | nsm->vsm.osm.type = ofpst_type; | |
1889 | nsm->vsm.vendor = htonl(NX_VENDOR_ID); | |
1890 | nsm->subtype = nxst_subtype; | |
1891 | } else { | |
1892 | struct ofp_stats_msg *osm; | |
1893 | ||
1894 | osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg); | |
1895 | osm->type = ofpst_type; | |
1896 | } | |
1897 | } | |
1898 | ||
1899 | /* Creates a statistics request message with total length 'openflow_len' | |
1900 | * (including all headers) and the given 'ofpst_type', and stores the buffer | |
1901 | * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR | |
1902 | * then 'nxst_subtype' is used as the Nicira vendor extension statistics | |
1903 | * subtype (otherwise 'nxst_subtype' is ignored). | |
1904 | * | |
1905 | * Initializes bytes following the headers to all-bits-zero. | |
1906 | * | |
1907 | * Returns the first byte of the new message. */ | |
dfdfc8d4 | 1908 | void * |
63f2140a BP |
1909 | ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type, |
1910 | uint32_t nxst_subtype, struct ofpbuf **bufferp) | |
dfdfc8d4 | 1911 | { |
63f2140a BP |
1912 | struct ofpbuf *msg; |
1913 | ||
1914 | msg = *bufferp = ofpbuf_new(openflow_len); | |
1915 | put_stats__(alloc_xid(), OFPT_STATS_REQUEST, | |
1916 | htons(ofpst_type), htonl(nxst_subtype), msg); | |
1917 | ofpbuf_padto(msg, openflow_len); | |
1918 | ||
1919 | return msg->data; | |
1920 | } | |
1921 | ||
1922 | static void | |
1923 | put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg) | |
1924 | { | |
1925 | assert(request->header.type == OFPT_STATS_REQUEST || | |
1926 | request->header.type == OFPT_STATS_REPLY); | |
1927 | put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type, | |
1928 | (request->type != htons(OFPST_VENDOR) | |
1929 | ? htonl(0) | |
1930 | : ((const struct nicira_stats_msg *) request)->subtype), | |
1931 | msg); | |
1932 | } | |
1933 | ||
1934 | /* Creates a statistics reply message with total length 'openflow_len' | |
1935 | * (including all headers) and the same type (either a standard OpenFlow | |
1936 | * statistics type or a Nicira extension type and subtype) as 'request', and | |
1937 | * stores the buffer containing the new message in '*bufferp'. | |
1938 | * | |
1939 | * Initializes bytes following the headers to all-bits-zero. | |
1940 | * | |
1941 | * Returns the first byte of the new message. */ | |
1942 | void * | |
1943 | ofputil_make_stats_reply(size_t openflow_len, | |
1944 | const struct ofp_stats_msg *request, | |
1945 | struct ofpbuf **bufferp) | |
1946 | { | |
1947 | struct ofpbuf *msg; | |
1948 | ||
1949 | msg = *bufferp = ofpbuf_new(openflow_len); | |
1950 | put_stats_reply__(request, msg); | |
1951 | ofpbuf_padto(msg, openflow_len); | |
1952 | ||
1953 | return msg->data; | |
1954 | } | |
1955 | ||
1956 | /* Initializes 'replies' as a list of ofpbufs that will contain a series of | |
1957 | * replies to 'request', which should be an OpenFlow or Nicira extension | |
1958 | * statistics request. Initially 'replies' will have a single reply message | |
1959 | * that has only a header. The functions ofputil_reserve_stats_reply() and | |
1960 | * ofputil_append_stats_reply() may be used to add to the reply. */ | |
1961 | void | |
1962 | ofputil_start_stats_reply(const struct ofp_stats_msg *request, | |
1963 | struct list *replies) | |
1964 | { | |
1965 | struct ofpbuf *msg; | |
1966 | ||
1967 | msg = ofpbuf_new(1024); | |
1968 | put_stats_reply__(request, msg); | |
1969 | ||
1970 | list_init(replies); | |
1971 | list_push_back(replies, &msg->list_node); | |
1972 | } | |
1973 | ||
1974 | /* Prepares to append up to 'len' bytes to the series of statistics replies in | |
1975 | * 'replies', which should have been initialized with | |
1976 | * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of | |
1977 | * tailroom. (The 'len' bytes have not actually be allocated; the caller must | |
1978 | * do so with e.g. ofpbuf_put_uninit().) */ | |
1979 | struct ofpbuf * | |
1980 | ofputil_reserve_stats_reply(size_t len, struct list *replies) | |
1981 | { | |
1982 | struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); | |
1983 | struct ofp_stats_msg *osm = msg->data; | |
1984 | ||
1985 | if (msg->size + len <= UINT16_MAX) { | |
1986 | ofpbuf_prealloc_tailroom(msg, len); | |
1987 | } else { | |
1988 | osm->flags |= htons(OFPSF_REPLY_MORE); | |
1989 | ||
1990 | msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len)); | |
1991 | put_stats_reply__(osm, msg); | |
1992 | list_push_back(replies, &msg->list_node); | |
1993 | } | |
1994 | return msg; | |
dfdfc8d4 BP |
1995 | } |
1996 | ||
63f2140a BP |
1997 | /* Appends 'len' bytes to the series of statistics replies in 'replies', and |
1998 | * returns the first byte. */ | |
dfdfc8d4 | 1999 | void * |
63f2140a | 2000 | ofputil_append_stats_reply(size_t len, struct list *replies) |
dfdfc8d4 | 2001 | { |
63f2140a | 2002 | return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len); |
dfdfc8d4 BP |
2003 | } |
2004 | ||
03cd3493 | 2005 | /* Returns the first byte past the ofp_stats_msg header in 'oh'. */ |
d1e2cf21 BP |
2006 | const void * |
2007 | ofputil_stats_body(const struct ofp_header *oh) | |
2008 | { | |
2009 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
28c8bad1 | 2010 | return (const struct ofp_stats_msg *) oh + 1; |
d1e2cf21 BP |
2011 | } |
2012 | ||
03cd3493 | 2013 | /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */ |
d1e2cf21 BP |
2014 | size_t |
2015 | ofputil_stats_body_len(const struct ofp_header *oh) | |
2016 | { | |
2017 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
28c8bad1 | 2018 | return ntohs(oh->length) - sizeof(struct ofp_stats_msg); |
d1e2cf21 BP |
2019 | } |
2020 | ||
03cd3493 | 2021 | /* Returns the first byte past the nicira_stats_msg header in 'oh'. */ |
c6430da5 BP |
2022 | const void * |
2023 | ofputil_nxstats_body(const struct ofp_header *oh) | |
2024 | { | |
2025 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
2026 | return ((const struct nicira_stats_msg *) oh) + 1; | |
2027 | } | |
2028 | ||
03cd3493 | 2029 | /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */ |
c6430da5 BP |
2030 | size_t |
2031 | ofputil_nxstats_body_len(const struct ofp_header *oh) | |
2032 | { | |
2033 | assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY); | |
2034 | return ntohs(oh->length) - sizeof(struct nicira_stats_msg); | |
2035 | } | |
2036 | ||
fa37b408 | 2037 | struct ofpbuf * |
daa68e9f BP |
2038 | make_flow_mod(uint16_t command, const struct cls_rule *rule, |
2039 | size_t actions_len) | |
fa37b408 BP |
2040 | { |
2041 | struct ofp_flow_mod *ofm; | |
2042 | size_t size = sizeof *ofm + actions_len; | |
2043 | struct ofpbuf *out = ofpbuf_new(size); | |
2044 | ofm = ofpbuf_put_zeros(out, sizeof *ofm); | |
2045 | ofm->header.version = OFP_VERSION; | |
2046 | ofm->header.type = OFPT_FLOW_MOD; | |
2047 | ofm->header.length = htons(size); | |
2048 | ofm->cookie = 0; | |
daa68e9f | 2049 | ofm->priority = htons(MIN(rule->priority, UINT16_MAX)); |
b78f6b77 | 2050 | ofputil_cls_rule_to_match(rule, &ofm->match); |
fa37b408 BP |
2051 | ofm->command = htons(command); |
2052 | return out; | |
2053 | } | |
2054 | ||
2055 | struct ofpbuf * | |
daa68e9f | 2056 | make_add_flow(const struct cls_rule *rule, uint32_t buffer_id, |
fa37b408 BP |
2057 | uint16_t idle_timeout, size_t actions_len) |
2058 | { | |
daa68e9f | 2059 | struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len); |
fa37b408 BP |
2060 | struct ofp_flow_mod *ofm = out->data; |
2061 | ofm->idle_timeout = htons(idle_timeout); | |
2062 | ofm->hard_timeout = htons(OFP_FLOW_PERMANENT); | |
2063 | ofm->buffer_id = htonl(buffer_id); | |
2064 | return out; | |
2065 | } | |
2066 | ||
2067 | struct ofpbuf * | |
daa68e9f | 2068 | make_del_flow(const struct cls_rule *rule) |
fa37b408 | 2069 | { |
daa68e9f | 2070 | struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0); |
fa37b408 BP |
2071 | struct ofp_flow_mod *ofm = out->data; |
2072 | ofm->out_port = htons(OFPP_NONE); | |
2073 | return out; | |
2074 | } | |
2075 | ||
2076 | struct ofpbuf * | |
daa68e9f | 2077 | make_add_simple_flow(const struct cls_rule *rule, |
fa37b408 BP |
2078 | uint32_t buffer_id, uint16_t out_port, |
2079 | uint16_t idle_timeout) | |
2080 | { | |
81f3cad4 BP |
2081 | if (out_port != OFPP_NONE) { |
2082 | struct ofp_action_output *oao; | |
2083 | struct ofpbuf *buffer; | |
2084 | ||
daa68e9f | 2085 | buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao); |
93996add | 2086 | ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port); |
81f3cad4 BP |
2087 | return buffer; |
2088 | } else { | |
daa68e9f | 2089 | return make_add_flow(rule, buffer_id, idle_timeout, 0); |
81f3cad4 | 2090 | } |
fa37b408 BP |
2091 | } |
2092 | ||
2093 | struct ofpbuf * | |
2094 | make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason, | |
2095 | const struct ofpbuf *payload, int max_send_len) | |
2096 | { | |
2097 | struct ofp_packet_in *opi; | |
2098 | struct ofpbuf *buf; | |
2099 | int send_len; | |
2100 | ||
2101 | send_len = MIN(max_send_len, payload->size); | |
2102 | buf = ofpbuf_new(sizeof *opi + send_len); | |
2103 | opi = put_openflow_xid(offsetof(struct ofp_packet_in, data), | |
2104 | OFPT_PACKET_IN, 0, buf); | |
2105 | opi->buffer_id = htonl(buffer_id); | |
2106 | opi->total_len = htons(payload->size); | |
2107 | opi->in_port = htons(in_port); | |
2108 | opi->reason = reason; | |
2109 | ofpbuf_put(buf, payload->data, send_len); | |
2110 | update_openflow_length(buf); | |
2111 | ||
2112 | return buf; | |
2113 | } | |
2114 | ||
2115 | struct ofpbuf * | |
2116 | make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id, | |
2117 | uint16_t in_port, | |
2118 | const struct ofp_action_header *actions, size_t n_actions) | |
2119 | { | |
2120 | size_t actions_len = n_actions * sizeof *actions; | |
2121 | struct ofp_packet_out *opo; | |
2122 | size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0); | |
2123 | struct ofpbuf *out = ofpbuf_new(size); | |
2124 | ||
2125 | opo = ofpbuf_put_uninit(out, sizeof *opo); | |
2126 | opo->header.version = OFP_VERSION; | |
2127 | opo->header.type = OFPT_PACKET_OUT; | |
2128 | opo->header.length = htons(size); | |
2129 | opo->header.xid = htonl(0); | |
2130 | opo->buffer_id = htonl(buffer_id); | |
6c24d402 | 2131 | opo->in_port = htons(in_port); |
fa37b408 BP |
2132 | opo->actions_len = htons(actions_len); |
2133 | ofpbuf_put(out, actions, actions_len); | |
2134 | if (packet) { | |
2135 | ofpbuf_put(out, packet->data, packet->size); | |
2136 | } | |
2137 | return out; | |
2138 | } | |
2139 | ||
2140 | struct ofpbuf * | |
2141 | make_unbuffered_packet_out(const struct ofpbuf *packet, | |
2142 | uint16_t in_port, uint16_t out_port) | |
2143 | { | |
2144 | struct ofp_action_output action; | |
2145 | action.type = htons(OFPAT_OUTPUT); | |
2146 | action.len = htons(sizeof action); | |
2147 | action.port = htons(out_port); | |
2148 | return make_packet_out(packet, UINT32_MAX, in_port, | |
2149 | (struct ofp_action_header *) &action, 1); | |
2150 | } | |
2151 | ||
2152 | struct ofpbuf * | |
2153 | make_buffered_packet_out(uint32_t buffer_id, | |
2154 | uint16_t in_port, uint16_t out_port) | |
2155 | { | |
81f3cad4 BP |
2156 | if (out_port != OFPP_NONE) { |
2157 | struct ofp_action_output action; | |
2158 | action.type = htons(OFPAT_OUTPUT); | |
2159 | action.len = htons(sizeof action); | |
2160 | action.port = htons(out_port); | |
2161 | return make_packet_out(NULL, buffer_id, in_port, | |
2162 | (struct ofp_action_header *) &action, 1); | |
2163 | } else { | |
2164 | return make_packet_out(NULL, buffer_id, in_port, NULL, 0); | |
2165 | } | |
fa37b408 BP |
2166 | } |
2167 | ||
2168 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ | |
2169 | struct ofpbuf * | |
2170 | make_echo_request(void) | |
2171 | { | |
2172 | struct ofp_header *rq; | |
2173 | struct ofpbuf *out = ofpbuf_new(sizeof *rq); | |
2174 | rq = ofpbuf_put_uninit(out, sizeof *rq); | |
2175 | rq->version = OFP_VERSION; | |
2176 | rq->type = OFPT_ECHO_REQUEST; | |
2177 | rq->length = htons(sizeof *rq); | |
44381c1b | 2178 | rq->xid = htonl(0); |
fa37b408 BP |
2179 | return out; |
2180 | } | |
2181 | ||
2182 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
2183 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
2184 | struct ofpbuf * | |
2185 | make_echo_reply(const struct ofp_header *rq) | |
2186 | { | |
2187 | size_t size = ntohs(rq->length); | |
2188 | struct ofpbuf *out = ofpbuf_new(size); | |
2189 | struct ofp_header *reply = ofpbuf_put(out, rq, size); | |
2190 | reply->type = OFPT_ECHO_REPLY; | |
2191 | return out; | |
2192 | } | |
2193 | ||
7257b535 BP |
2194 | const char * |
2195 | ofputil_frag_handling_to_string(enum ofp_config_flags flags) | |
2196 | { | |
2197 | switch (flags & OFPC_FRAG_MASK) { | |
2198 | case OFPC_FRAG_NORMAL: return "normal"; | |
2199 | case OFPC_FRAG_DROP: return "drop"; | |
2200 | case OFPC_FRAG_REASM: return "reassemble"; | |
2201 | case OFPC_FRAG_NX_MATCH: return "nx-match"; | |
2202 | } | |
2203 | ||
2204 | NOT_REACHED(); | |
2205 | } | |
2206 | ||
2207 | bool | |
2208 | ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags) | |
2209 | { | |
2210 | if (!strcasecmp(s, "normal")) { | |
2211 | *flags = OFPC_FRAG_NORMAL; | |
2212 | } else if (!strcasecmp(s, "drop")) { | |
2213 | *flags = OFPC_FRAG_DROP; | |
2214 | } else if (!strcasecmp(s, "reassemble")) { | |
2215 | *flags = OFPC_FRAG_REASM; | |
2216 | } else if (!strcasecmp(s, "nx-match")) { | |
2217 | *flags = OFPC_FRAG_NX_MATCH; | |
2218 | } else { | |
2219 | return false; | |
2220 | } | |
2221 | return true; | |
2222 | } | |
2223 | ||
c1c9c9c4 BP |
2224 | /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given |
2225 | * that the switch will never have more than 'max_ports' ports. Returns 0 if | |
90bf1e07 BP |
2226 | * 'port' is valid, otherwise an OpenFlow return code. */ |
2227 | enum ofperr | |
77410139 | 2228 | ofputil_check_output_port(uint16_t port, int max_ports) |
fa37b408 BP |
2229 | { |
2230 | switch (port) { | |
2231 | case OFPP_IN_PORT: | |
2232 | case OFPP_TABLE: | |
2233 | case OFPP_NORMAL: | |
2234 | case OFPP_FLOOD: | |
2235 | case OFPP_ALL: | |
2236 | case OFPP_CONTROLLER: | |
2237 | case OFPP_LOCAL: | |
2238 | return 0; | |
2239 | ||
2240 | default: | |
c1c9c9c4 | 2241 | if (port < max_ports) { |
fa37b408 BP |
2242 | return 0; |
2243 | } | |
90bf1e07 | 2244 | return OFPERR_OFPBAC_BAD_OUT_PORT; |
fa37b408 BP |
2245 | } |
2246 | } | |
2247 | ||
39dc9082 BP |
2248 | #define OFPUTIL_NAMED_PORTS \ |
2249 | OFPUTIL_NAMED_PORT(IN_PORT) \ | |
2250 | OFPUTIL_NAMED_PORT(TABLE) \ | |
2251 | OFPUTIL_NAMED_PORT(NORMAL) \ | |
2252 | OFPUTIL_NAMED_PORT(FLOOD) \ | |
2253 | OFPUTIL_NAMED_PORT(ALL) \ | |
2254 | OFPUTIL_NAMED_PORT(CONTROLLER) \ | |
2255 | OFPUTIL_NAMED_PORT(LOCAL) \ | |
2256 | OFPUTIL_NAMED_PORT(NONE) | |
2257 | ||
2258 | /* Checks whether 's' is the string representation of an OpenFlow port number, | |
2259 | * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the | |
2260 | * number in '*port' and returns true. Otherwise, returns false. */ | |
2261 | bool | |
2262 | ofputil_port_from_string(const char *name, uint16_t *port) | |
2263 | { | |
2264 | struct pair { | |
2265 | const char *name; | |
2266 | uint16_t value; | |
2267 | }; | |
2268 | static const struct pair pairs[] = { | |
2269 | #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME}, | |
2270 | OFPUTIL_NAMED_PORTS | |
2271 | #undef OFPUTIL_NAMED_PORT | |
2272 | }; | |
2273 | static const int n_pairs = ARRAY_SIZE(pairs); | |
2274 | int i; | |
2275 | ||
2276 | if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) { | |
2277 | *port = i; | |
2278 | return true; | |
2279 | } | |
2280 | ||
2281 | for (i = 0; i < n_pairs; i++) { | |
2282 | if (!strcasecmp(name, pairs[i].name)) { | |
2283 | *port = pairs[i].value; | |
2284 | return true; | |
2285 | } | |
2286 | } | |
2287 | return false; | |
2288 | } | |
2289 | ||
2290 | /* Appends to 's' a string representation of the OpenFlow port number 'port'. | |
2291 | * Most ports' string representation is just the port number, but for special | |
2292 | * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */ | |
2293 | void | |
2294 | ofputil_format_port(uint16_t port, struct ds *s) | |
2295 | { | |
2296 | const char *name; | |
2297 | ||
2298 | switch (port) { | |
2299 | #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break; | |
2300 | OFPUTIL_NAMED_PORTS | |
2301 | #undef OFPUTIL_NAMED_PORT | |
2302 | ||
2303 | default: | |
2304 | ds_put_format(s, "%"PRIu16, port); | |
2305 | return; | |
2306 | } | |
2307 | ds_put_cstr(s, name); | |
2308 | } | |
2309 | ||
90bf1e07 | 2310 | static enum ofperr |
29901626 BP |
2311 | check_resubmit_table(const struct nx_action_resubmit *nar) |
2312 | { | |
2313 | if (nar->pad[0] || nar->pad[1] || nar->pad[2]) { | |
90bf1e07 | 2314 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
29901626 BP |
2315 | } |
2316 | return 0; | |
2317 | } | |
2318 | ||
90bf1e07 | 2319 | static enum ofperr |
f694937d EJ |
2320 | check_output_reg(const struct nx_action_output_reg *naor, |
2321 | const struct flow *flow) | |
2322 | { | |
2323 | size_t i; | |
2324 | ||
2325 | for (i = 0; i < sizeof naor->zero; i++) { | |
2326 | if (naor->zero[i]) { | |
90bf1e07 | 2327 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
f694937d EJ |
2328 | } |
2329 | } | |
2330 | ||
2331 | return nxm_src_check(naor->src, nxm_decode_ofs(naor->ofs_nbits), | |
2332 | nxm_decode_n_bits(naor->ofs_nbits), flow); | |
2333 | } | |
2334 | ||
90bf1e07 | 2335 | enum ofperr |
38f2e360 BP |
2336 | validate_actions(const union ofp_action *actions, size_t n_actions, |
2337 | const struct flow *flow, int max_ports) | |
c1c9c9c4 | 2338 | { |
38f2e360 BP |
2339 | const union ofp_action *a; |
2340 | size_t left; | |
c1c9c9c4 | 2341 | |
38f2e360 | 2342 | OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) { |
90bf1e07 | 2343 | enum ofperr error; |
38f2e360 | 2344 | uint16_t port; |
38f2e360 | 2345 | int code; |
c1c9c9c4 | 2346 | |
38f2e360 BP |
2347 | code = ofputil_decode_action(a); |
2348 | if (code < 0) { | |
38f2e360 | 2349 | error = -code; |
38f2e360 BP |
2350 | VLOG_WARN_RL(&bad_ofmsg_rl, |
2351 | "action decoding error at offset %td (%s)", | |
90bf1e07 | 2352 | (a - actions) * sizeof *a, ofperr_get_name(error)); |
fa37b408 | 2353 | |
38f2e360 BP |
2354 | return error; |
2355 | } | |
fa37b408 | 2356 | |
38f2e360 BP |
2357 | error = 0; |
2358 | switch ((enum ofputil_action_code) code) { | |
2359 | case OFPUTIL_OFPAT_OUTPUT: | |
77410139 EJ |
2360 | error = ofputil_check_output_port(ntohs(a->output.port), |
2361 | max_ports); | |
38f2e360 | 2362 | break; |
e41a9130 | 2363 | |
38f2e360 BP |
2364 | case OFPUTIL_OFPAT_SET_VLAN_VID: |
2365 | if (a->vlan_vid.vlan_vid & ~htons(0xfff)) { | |
90bf1e07 | 2366 | error = OFPERR_OFPBAC_BAD_ARGUMENT; |
38f2e360 BP |
2367 | } |
2368 | break; | |
96fc46e8 | 2369 | |
38f2e360 BP |
2370 | case OFPUTIL_OFPAT_SET_VLAN_PCP: |
2371 | if (a->vlan_pcp.vlan_pcp & ~7) { | |
90bf1e07 | 2372 | error = OFPERR_OFPBAC_BAD_ARGUMENT; |
38f2e360 BP |
2373 | } |
2374 | break; | |
96fc46e8 | 2375 | |
38f2e360 BP |
2376 | case OFPUTIL_OFPAT_ENQUEUE: |
2377 | port = ntohs(((const struct ofp_action_enqueue *) a)->port); | |
82172632 EJ |
2378 | if (port >= max_ports && port != OFPP_IN_PORT |
2379 | && port != OFPP_LOCAL) { | |
90bf1e07 | 2380 | error = OFPERR_OFPBAC_BAD_OUT_PORT; |
38f2e360 BP |
2381 | } |
2382 | break; | |
96fc46e8 | 2383 | |
38f2e360 BP |
2384 | case OFPUTIL_NXAST_REG_MOVE: |
2385 | error = nxm_check_reg_move((const struct nx_action_reg_move *) a, | |
2386 | flow); | |
2387 | break; | |
96fc46e8 | 2388 | |
38f2e360 BP |
2389 | case OFPUTIL_NXAST_REG_LOAD: |
2390 | error = nxm_check_reg_load((const struct nx_action_reg_load *) a, | |
2391 | flow); | |
2392 | break; | |
b9298d3f | 2393 | |
38f2e360 | 2394 | case OFPUTIL_NXAST_MULTIPATH: |
43edca57 EJ |
2395 | error = multipath_check((const struct nx_action_multipath *) a, |
2396 | flow); | |
38f2e360 BP |
2397 | break; |
2398 | ||
2399 | case OFPUTIL_NXAST_AUTOPATH: | |
43edca57 EJ |
2400 | error = autopath_check((const struct nx_action_autopath *) a, |
2401 | flow); | |
38f2e360 BP |
2402 | break; |
2403 | ||
daff3353 | 2404 | case OFPUTIL_NXAST_BUNDLE: |
a368bb53 | 2405 | case OFPUTIL_NXAST_BUNDLE_LOAD: |
daff3353 | 2406 | error = bundle_check((const struct nx_action_bundle *) a, |
a368bb53 | 2407 | max_ports, flow); |
daff3353 EJ |
2408 | break; |
2409 | ||
f694937d EJ |
2410 | case OFPUTIL_NXAST_OUTPUT_REG: |
2411 | error = check_output_reg((const struct nx_action_output_reg *) a, | |
2412 | flow); | |
2413 | break; | |
2414 | ||
29901626 BP |
2415 | case OFPUTIL_NXAST_RESUBMIT_TABLE: |
2416 | error = check_resubmit_table( | |
2417 | (const struct nx_action_resubmit *) a); | |
2418 | break; | |
2419 | ||
75a75043 BP |
2420 | case OFPUTIL_NXAST_LEARN: |
2421 | error = learn_check((const struct nx_action_learn *) a, flow); | |
2422 | break; | |
2423 | ||
38f2e360 BP |
2424 | case OFPUTIL_OFPAT_STRIP_VLAN: |
2425 | case OFPUTIL_OFPAT_SET_NW_SRC: | |
2426 | case OFPUTIL_OFPAT_SET_NW_DST: | |
2427 | case OFPUTIL_OFPAT_SET_NW_TOS: | |
2428 | case OFPUTIL_OFPAT_SET_TP_SRC: | |
2429 | case OFPUTIL_OFPAT_SET_TP_DST: | |
2430 | case OFPUTIL_OFPAT_SET_DL_SRC: | |
2431 | case OFPUTIL_OFPAT_SET_DL_DST: | |
2432 | case OFPUTIL_NXAST_RESUBMIT: | |
2433 | case OFPUTIL_NXAST_SET_TUNNEL: | |
2434 | case OFPUTIL_NXAST_SET_QUEUE: | |
2435 | case OFPUTIL_NXAST_POP_QUEUE: | |
2436 | case OFPUTIL_NXAST_NOTE: | |
2437 | case OFPUTIL_NXAST_SET_TUNNEL64: | |
848e8809 | 2438 | case OFPUTIL_NXAST_EXIT: |
f0fd1a17 | 2439 | case OFPUTIL_NXAST_DEC_TTL: |
38f2e360 | 2440 | break; |
53ddd40a | 2441 | } |
53ddd40a | 2442 | |
3b6a2571 | 2443 | if (error) { |
38f2e360 | 2444 | VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)", |
90bf1e07 | 2445 | (a - actions) * sizeof *a, ofperr_get_name(error)); |
3b6a2571 EJ |
2446 | return error; |
2447 | } | |
fa37b408 | 2448 | } |
38f2e360 BP |
2449 | if (left) { |
2450 | VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu", | |
2451 | (n_actions - left) * sizeof *a); | |
90bf1e07 | 2452 | return OFPERR_OFPBAC_BAD_LEN; |
38f2e360 BP |
2453 | } |
2454 | return 0; | |
fa37b408 BP |
2455 | } |
2456 | ||
51cb6e0c BP |
2457 | struct ofputil_action { |
2458 | int code; | |
38f2e360 BP |
2459 | unsigned int min_len; |
2460 | unsigned int max_len; | |
2461 | }; | |
27bcf966 | 2462 | |
51cb6e0c | 2463 | static const struct ofputil_action action_bad_type |
90bf1e07 | 2464 | = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX }; |
51cb6e0c | 2465 | static const struct ofputil_action action_bad_len |
90bf1e07 | 2466 | = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX }; |
51cb6e0c | 2467 | static const struct ofputil_action action_bad_vendor |
90bf1e07 | 2468 | = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX }; |
27bcf966 | 2469 | |
51cb6e0c | 2470 | static const struct ofputil_action * |
38f2e360 BP |
2471 | ofputil_decode_ofpat_action(const union ofp_action *a) |
2472 | { | |
51cb6e0c | 2473 | enum ofp_action_type type = ntohs(a->type); |
fa37b408 | 2474 | |
51cb6e0c | 2475 | switch (type) { |
e23ae585 | 2476 | #define OFPAT_ACTION(ENUM, STRUCT, NAME) \ |
51cb6e0c BP |
2477 | case ENUM: { \ |
2478 | static const struct ofputil_action action = { \ | |
e23ae585 BP |
2479 | OFPUTIL_##ENUM, \ |
2480 | sizeof(struct STRUCT), \ | |
2481 | sizeof(struct STRUCT) \ | |
51cb6e0c BP |
2482 | }; \ |
2483 | return &action; \ | |
2484 | } | |
e23ae585 | 2485 | #include "ofp-util.def" |
51cb6e0c BP |
2486 | |
2487 | case OFPAT_VENDOR: | |
2488 | default: | |
2489 | return &action_bad_type; | |
38f2e360 BP |
2490 | } |
2491 | } | |
fa37b408 | 2492 | |
51cb6e0c | 2493 | static const struct ofputil_action * |
38f2e360 BP |
2494 | ofputil_decode_nxast_action(const union ofp_action *a) |
2495 | { | |
51cb6e0c BP |
2496 | const struct nx_action_header *nah = (const struct nx_action_header *) a; |
2497 | enum nx_action_subtype subtype = ntohs(nah->subtype); | |
2498 | ||
2499 | switch (subtype) { | |
e23ae585 BP |
2500 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ |
2501 | case ENUM: { \ | |
2502 | static const struct ofputil_action action = { \ | |
2503 | OFPUTIL_##ENUM, \ | |
2504 | sizeof(struct STRUCT), \ | |
2505 | EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \ | |
2506 | }; \ | |
2507 | return &action; \ | |
38f2e360 | 2508 | } |
e23ae585 | 2509 | #include "ofp-util.def" |
51cb6e0c BP |
2510 | |
2511 | case NXAST_SNAT__OBSOLETE: | |
2512 | case NXAST_DROP_SPOOFED_ARP__OBSOLETE: | |
2513 | default: | |
2514 | return &action_bad_type; | |
fa37b408 BP |
2515 | } |
2516 | } | |
2517 | ||
38f2e360 | 2518 | /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT_* or |
90bf1e07 BP |
2519 | * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error |
2520 | * code. | |
38f2e360 BP |
2521 | * |
2522 | * The caller must have already verified that 'a''s length is correct (that is, | |
2523 | * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no | |
2524 | * longer than the amount of space allocated to 'a'). | |
2525 | * | |
2526 | * This function verifies that 'a''s length is correct for the type of action | |
2527 | * that it represents. */ | |
fa37b408 | 2528 | int |
38f2e360 | 2529 | ofputil_decode_action(const union ofp_action *a) |
fa37b408 | 2530 | { |
51cb6e0c BP |
2531 | const struct ofputil_action *action; |
2532 | uint16_t len = ntohs(a->header.len); | |
2533 | ||
38f2e360 | 2534 | if (a->type != htons(OFPAT_VENDOR)) { |
51cb6e0c | 2535 | action = ofputil_decode_ofpat_action(a); |
38f2e360 | 2536 | } else { |
51cb6e0c BP |
2537 | switch (ntohl(a->vendor.vendor)) { |
2538 | case NX_VENDOR_ID: | |
2539 | if (len < sizeof(struct nx_action_header)) { | |
90bf1e07 | 2540 | return -OFPERR_OFPBAC_BAD_LEN; |
51cb6e0c BP |
2541 | } |
2542 | action = ofputil_decode_nxast_action(a); | |
2543 | break; | |
2544 | default: | |
2545 | action = &action_bad_vendor; | |
2546 | break; | |
2547 | } | |
38f2e360 | 2548 | } |
51cb6e0c BP |
2549 | |
2550 | return (len >= action->min_len && len <= action->max_len | |
2551 | ? action->code | |
90bf1e07 | 2552 | : -OFPERR_OFPBAC_BAD_LEN); |
38f2e360 | 2553 | } |
fa37b408 | 2554 | |
38f2e360 BP |
2555 | /* Parses 'a' and returns its type as an OFPUTIL_OFPAT_* or OFPUTIL_NXAST_* |
2556 | * constant. The caller must have already validated that 'a' is a valid action | |
2557 | * understood by Open vSwitch (e.g. by a previous successful call to | |
2558 | * ofputil_decode_action()). */ | |
2559 | enum ofputil_action_code | |
2560 | ofputil_decode_action_unsafe(const union ofp_action *a) | |
2561 | { | |
51cb6e0c BP |
2562 | const struct ofputil_action *action; |
2563 | ||
38f2e360 | 2564 | if (a->type != htons(OFPAT_VENDOR)) { |
51cb6e0c | 2565 | action = ofputil_decode_ofpat_action(a); |
38f2e360 | 2566 | } else { |
51cb6e0c | 2567 | action = ofputil_decode_nxast_action(a); |
fa37b408 | 2568 | } |
51cb6e0c BP |
2569 | |
2570 | return action->code; | |
fa37b408 BP |
2571 | } |
2572 | ||
e23ae585 BP |
2573 | /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if |
2574 | * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if | |
2575 | * 'name' is not the name of any action. | |
2576 | * | |
2577 | * ofp-util.def lists the mapping from names to action. */ | |
2578 | int | |
2579 | ofputil_action_code_from_name(const char *name) | |
2580 | { | |
2581 | static const char *names[OFPUTIL_N_ACTIONS] = { | |
2582 | #define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME, | |
2583 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, | |
2584 | #include "ofp-util.def" | |
2585 | }; | |
2586 | ||
2587 | const char **p; | |
2588 | ||
2589 | for (p = names; p < &names[ARRAY_SIZE(names)]; p++) { | |
2590 | if (*p && !strcasecmp(name, *p)) { | |
2591 | return p - names; | |
2592 | } | |
2593 | } | |
2594 | return -1; | |
2595 | } | |
2596 | ||
93996add BP |
2597 | /* Appends an action of the type specified by 'code' to 'buf' and returns the |
2598 | * action. Initializes the parts of 'action' that identify it as having type | |
2599 | * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that | |
2600 | * have variable length, the length used and cleared is that of struct | |
2601 | * <STRUCT>. */ | |
2602 | void * | |
2603 | ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf) | |
2604 | { | |
2605 | switch (code) { | |
2606 | #define OFPAT_ACTION(ENUM, STRUCT, NAME) \ | |
2607 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
2608 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
2609 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
2610 | #include "ofp-util.def" | |
2611 | } | |
2612 | NOT_REACHED(); | |
2613 | } | |
2614 | ||
2615 | #define OFPAT_ACTION(ENUM, STRUCT, NAME) \ | |
2616 | void \ | |
2617 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
2618 | { \ | |
2619 | memset(s, 0, sizeof *s); \ | |
2620 | s->type = htons(ENUM); \ | |
2621 | s->len = htons(sizeof *s); \ | |
2622 | } \ | |
2623 | \ | |
2624 | struct STRUCT * \ | |
2625 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
2626 | { \ | |
2627 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
2628 | ofputil_init_##ENUM(s); \ | |
2629 | return s; \ | |
2630 | } | |
2631 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
2632 | void \ | |
2633 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
2634 | { \ | |
2635 | memset(s, 0, sizeof *s); \ | |
2636 | s->type = htons(OFPAT_VENDOR); \ | |
2637 | s->len = htons(sizeof *s); \ | |
2638 | s->vendor = htonl(NX_VENDOR_ID); \ | |
2639 | s->subtype = htons(ENUM); \ | |
2640 | } \ | |
2641 | \ | |
2642 | struct STRUCT * \ | |
2643 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
2644 | { \ | |
2645 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
2646 | ofputil_init_##ENUM(s); \ | |
2647 | return s; \ | |
2648 | } | |
2649 | #include "ofp-util.def" | |
2650 | ||
dbba996b | 2651 | /* Returns true if 'action' outputs to 'port', false otherwise. */ |
c1c9c9c4 | 2652 | bool |
dbba996b | 2653 | action_outputs_to_port(const union ofp_action *action, ovs_be16 port) |
c1c9c9c4 BP |
2654 | { |
2655 | switch (ntohs(action->type)) { | |
2656 | case OFPAT_OUTPUT: | |
2657 | return action->output.port == port; | |
2658 | case OFPAT_ENQUEUE: | |
2659 | return ((const struct ofp_action_enqueue *) action)->port == port; | |
2660 | default: | |
2661 | return false; | |
2662 | } | |
2663 | } | |
2664 | ||
b459a924 BP |
2665 | /* "Normalizes" the wildcards in 'rule'. That means: |
2666 | * | |
2667 | * 1. If the type of level N is known, then only the valid fields for that | |
2668 | * level may be specified. For example, ARP does not have a TOS field, | |
2669 | * so nw_tos must be wildcarded if 'rule' specifies an ARP flow. | |
2670 | * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and | |
2671 | * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an | |
2672 | * IPv4 flow. | |
2673 | * | |
2674 | * 2. If the type of level N is not known (or not understood by Open | |
2675 | * vSwitch), then no fields at all for that level may be specified. For | |
2676 | * example, Open vSwitch does not understand SCTP, an L4 protocol, so the | |
2677 | * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an | |
2678 | * SCTP flow. | |
2679 | * | |
2680 | * 'flow_format' specifies the format of the flow as received or as intended to | |
2681 | * be sent. This is important for IPv6 and ARP, for which NXM supports more | |
2682 | * detailed matching. */ | |
2683 | void | |
2684 | ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format) | |
2685 | { | |
2686 | enum { | |
2687 | MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */ | |
2688 | MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */ | |
2689 | MAY_NW_PROTO = 1 << 2, /* nw_proto */ | |
a61680c6 | 2690 | MAY_IPVx = 1 << 3, /* tos, frag, ttl */ |
b459a924 BP |
2691 | MAY_ARP_SHA = 1 << 4, /* arp_sha */ |
2692 | MAY_ARP_THA = 1 << 5, /* arp_tha */ | |
d78477ec | 2693 | MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */ |
b459a924 BP |
2694 | MAY_ND_TARGET = 1 << 7 /* nd_target */ |
2695 | } may_match; | |
2696 | ||
2697 | struct flow_wildcards wc; | |
2698 | ||
2699 | /* Figure out what fields may be matched. */ | |
2700 | if (rule->flow.dl_type == htons(ETH_TYPE_IP)) { | |
9e44d715 | 2701 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR; |
b459a924 BP |
2702 | if (rule->flow.nw_proto == IPPROTO_TCP || |
2703 | rule->flow.nw_proto == IPPROTO_UDP || | |
2704 | rule->flow.nw_proto == IPPROTO_ICMP) { | |
2705 | may_match |= MAY_TP_ADDR; | |
2706 | } | |
2707 | } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6) | |
2708 | && flow_format == NXFF_NXM) { | |
d78477ec | 2709 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6; |
b459a924 BP |
2710 | if (rule->flow.nw_proto == IPPROTO_TCP || |
2711 | rule->flow.nw_proto == IPPROTO_UDP) { | |
2712 | may_match |= MAY_TP_ADDR; | |
2713 | } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) { | |
2714 | may_match |= MAY_TP_ADDR; | |
2715 | if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) { | |
2716 | may_match |= MAY_ND_TARGET | MAY_ARP_SHA; | |
2717 | } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
2718 | may_match |= MAY_ND_TARGET | MAY_ARP_THA; | |
2719 | } | |
2720 | } | |
2721 | } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) { | |
2722 | may_match = MAY_NW_PROTO | MAY_NW_ADDR; | |
2723 | if (flow_format == NXFF_NXM) { | |
2724 | may_match |= MAY_ARP_SHA | MAY_ARP_THA; | |
fa37b408 | 2725 | } |
1c0b7503 | 2726 | } else { |
b459a924 BP |
2727 | may_match = 0; |
2728 | } | |
2729 | ||
2730 | /* Clear the fields that may not be matched. */ | |
2731 | wc = rule->wc; | |
2732 | if (!(may_match & MAY_NW_ADDR)) { | |
2733 | wc.nw_src_mask = wc.nw_dst_mask = htonl(0); | |
2734 | } | |
2735 | if (!(may_match & MAY_TP_ADDR)) { | |
2736 | wc.wildcards |= FWW_TP_SRC | FWW_TP_DST; | |
2737 | } | |
2738 | if (!(may_match & MAY_NW_PROTO)) { | |
2739 | wc.wildcards |= FWW_NW_PROTO; | |
2740 | } | |
9e44d715 | 2741 | if (!(may_match & MAY_IPVx)) { |
2486e66a JP |
2742 | wc.wildcards |= FWW_NW_DSCP; |
2743 | wc.wildcards |= FWW_NW_ECN; | |
a61680c6 | 2744 | wc.wildcards |= FWW_NW_TTL; |
b459a924 BP |
2745 | } |
2746 | if (!(may_match & MAY_ARP_SHA)) { | |
2747 | wc.wildcards |= FWW_ARP_SHA; | |
2748 | } | |
2749 | if (!(may_match & MAY_ARP_THA)) { | |
2750 | wc.wildcards |= FWW_ARP_THA; | |
2751 | } | |
d78477ec | 2752 | if (!(may_match & MAY_IPV6)) { |
b459a924 | 2753 | wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any; |
fa8223b7 | 2754 | wc.wildcards |= FWW_IPV6_LABEL; |
b459a924 BP |
2755 | } |
2756 | if (!(may_match & MAY_ND_TARGET)) { | |
2757 | wc.wildcards |= FWW_ND_TARGET; | |
2758 | } | |
2759 | ||
2760 | /* Log any changes. */ | |
2761 | if (!flow_wildcards_equal(&wc, &rule->wc)) { | |
2762 | bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl); | |
2763 | char *pre = log ? cls_rule_to_string(rule) : NULL; | |
2764 | ||
2765 | rule->wc = wc; | |
2766 | cls_rule_zero_wildcarded_fields(rule); | |
2767 | ||
2768 | if (log) { | |
2769 | char *post = cls_rule_to_string(rule); | |
2770 | VLOG_INFO("normalization changed ofp_match, details:"); | |
2771 | VLOG_INFO(" pre: %s", pre); | |
2772 | VLOG_INFO("post: %s", post); | |
2773 | free(pre); | |
2774 | free(post); | |
2775 | } | |
fa37b408 | 2776 | } |
3f09c339 | 2777 | } |
26c112c2 | 2778 | |
3052b0c5 BP |
2779 | /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if |
2780 | * successful, otherwise an OpenFlow error. | |
2781 | * | |
2782 | * If successful, the first action is stored in '*actionsp' and the number of | |
2783 | * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0 | |
2784 | * are stored, respectively. | |
2785 | * | |
2786 | * This function does not check that the actions are valid (the caller should | |
2787 | * do so, with validate_actions()). The caller is also responsible for making | |
2788 | * sure that 'b->data' is initially aligned appropriately for "union | |
2789 | * ofp_action". */ | |
90bf1e07 | 2790 | enum ofperr |
3052b0c5 BP |
2791 | ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len, |
2792 | union ofp_action **actionsp, size_t *n_actionsp) | |
2793 | { | |
69b6be19 | 2794 | if (actions_len % OFP_ACTION_ALIGN != 0) { |
f4350529 BP |
2795 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u " |
2796 | "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN); | |
3052b0c5 BP |
2797 | goto error; |
2798 | } | |
2799 | ||
2800 | *actionsp = ofpbuf_try_pull(b, actions_len); | |
2801 | if (*actionsp == NULL) { | |
f4350529 BP |
2802 | VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u " |
2803 | "exceeds remaining message length (%zu)", | |
2804 | actions_len, b->size); | |
3052b0c5 BP |
2805 | goto error; |
2806 | } | |
2807 | ||
69b6be19 | 2808 | *n_actionsp = actions_len / OFP_ACTION_ALIGN; |
3052b0c5 BP |
2809 | return 0; |
2810 | ||
2811 | error: | |
2812 | *actionsp = NULL; | |
2813 | *n_actionsp = 0; | |
90bf1e07 | 2814 | return OFPERR_OFPBRC_BAD_LEN; |
3052b0c5 | 2815 | } |
18ddadc2 BP |
2816 | |
2817 | bool | |
2818 | ofputil_actions_equal(const union ofp_action *a, size_t n_a, | |
2819 | const union ofp_action *b, size_t n_b) | |
2820 | { | |
2821 | return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a)); | |
2822 | } | |
2823 | ||
2824 | union ofp_action * | |
2825 | ofputil_actions_clone(const union ofp_action *actions, size_t n) | |
2826 | { | |
2827 | return n ? xmemdup(actions, n * sizeof *actions) : NULL; | |
2828 | } | |
0ff22822 BP |
2829 | |
2830 | /* Parses a key or a key-value pair from '*stringp'. | |
2831 | * | |
2832 | * On success: Stores the key into '*keyp'. Stores the value, if present, into | |
2833 | * '*valuep', otherwise an empty string. Advances '*stringp' past the end of | |
2834 | * the key-value pair, preparing it for another call. '*keyp' and '*valuep' | |
2835 | * are substrings of '*stringp' created by replacing some of its bytes by null | |
2836 | * terminators. Returns true. | |
2837 | * | |
2838 | * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to | |
2839 | * NULL and returns false. */ | |
2840 | bool | |
2841 | ofputil_parse_key_value(char **stringp, char **keyp, char **valuep) | |
2842 | { | |
2843 | char *pos, *key, *value; | |
2844 | size_t key_len; | |
2845 | ||
2846 | pos = *stringp; | |
2847 | pos += strspn(pos, ", \t\r\n"); | |
2848 | if (*pos == '\0') { | |
2849 | *keyp = *valuep = NULL; | |
2850 | return false; | |
2851 | } | |
2852 | ||
2853 | key = pos; | |
2854 | key_len = strcspn(pos, ":=(, \t\r\n"); | |
2855 | if (key[key_len] == ':' || key[key_len] == '=') { | |
2856 | /* The value can be separated by a colon. */ | |
2857 | size_t value_len; | |
2858 | ||
2859 | value = key + key_len + 1; | |
2860 | value_len = strcspn(value, ", \t\r\n"); | |
2861 | pos = value + value_len + (value[value_len] != '\0'); | |
2862 | value[value_len] = '\0'; | |
2863 | } else if (key[key_len] == '(') { | |
2864 | /* The value can be surrounded by balanced parentheses. The outermost | |
2865 | * set of parentheses is removed. */ | |
2866 | int level = 1; | |
2867 | size_t value_len; | |
2868 | ||
2869 | value = key + key_len + 1; | |
2870 | for (value_len = 0; level > 0; value_len++) { | |
2871 | switch (value[value_len]) { | |
2872 | case '\0': | |
2873 | ovs_fatal(0, "unbalanced parentheses in argument to %s", key); | |
2874 | ||
2875 | case '(': | |
2876 | level++; | |
2877 | break; | |
2878 | ||
2879 | case ')': | |
2880 | level--; | |
2881 | break; | |
2882 | } | |
2883 | } | |
2884 | value[value_len - 1] = '\0'; | |
2885 | pos = value + value_len; | |
2886 | } else { | |
2887 | /* There might be no value at all. */ | |
2888 | value = key + key_len; /* Will become the empty string below. */ | |
2889 | pos = key + key_len + (key[key_len] != '\0'); | |
2890 | } | |
2891 | key[key_len] = '\0'; | |
2892 | ||
2893 | *stringp = pos; | |
2894 | *keyp = key; | |
2895 | *valuep = value; | |
2896 | return true; | |
2897 | } |