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