]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include "ofp-print.h" | |
19 | #include <ctype.h> | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
22 | #include <sys/types.h> | |
23 | #include <netinet/in.h> | |
24 | #include <netinet/icmp6.h> | |
25 | #include <stdlib.h> | |
26 | #include "autopath.h" | |
27 | #include "bundle.h" | |
28 | #include "byte-order.h" | |
29 | #include "classifier.h" | |
30 | #include "dynamic-string.h" | |
31 | #include "learn.h" | |
32 | #include "meta-flow.h" | |
33 | #include "multipath.h" | |
34 | #include "netdev.h" | |
35 | #include "nx-match.h" | |
36 | #include "ofp-actions.h" | |
37 | #include "ofp-errors.h" | |
38 | #include "ofp-msgs.h" | |
39 | #include "ofp-util.h" | |
40 | #include "ofpbuf.h" | |
41 | #include "packets.h" | |
42 | #include "random.h" | |
43 | #include "unaligned.h" | |
44 | #include "type-props.h" | |
45 | #include "vlog.h" | |
46 | ||
47 | VLOG_DEFINE_THIS_MODULE(ofp_util); | |
48 | ||
49 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
50 | * in the peer and so there's not much point in showing a lot of them. */ | |
51 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
52 | ||
53 | /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns | |
54 | * an IP netmask with a 1 in each bit that must match and a 0 in each bit that | |
55 | * is wildcarded. | |
56 | * | |
57 | * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0 | |
58 | * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits, | |
59 | * ..., 32 and higher wildcard the entire field. This is the *opposite* of the | |
60 | * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are | |
61 | * wildcarded. */ | |
62 | ovs_be32 | |
63 | ofputil_wcbits_to_netmask(int wcbits) | |
64 | { | |
65 | wcbits &= 0x3f; | |
66 | return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0; | |
67 | } | |
68 | ||
69 | /* Given the IP netmask 'netmask', returns the number of bits of the IP address | |
70 | * that it wildcards, that is, the number of 0-bits in 'netmask', a number | |
71 | * between 0 and 32 inclusive. | |
72 | * | |
73 | * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will | |
74 | * still be in the valid range but isn't otherwise meaningful. */ | |
75 | int | |
76 | ofputil_netmask_to_wcbits(ovs_be32 netmask) | |
77 | { | |
78 | return 32 - ip_count_cidr_bits(netmask); | |
79 | } | |
80 | ||
81 | /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a | |
82 | * flow_wildcards in 'wc' for use in struct match. It is the caller's | |
83 | * responsibility to handle the special case where the flow match's dl_vlan is | |
84 | * set to OFP_VLAN_NONE. */ | |
85 | void | |
86 | ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc) | |
87 | { | |
88 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18); | |
89 | ||
90 | /* Initialize most of wc. */ | |
91 | flow_wildcards_init_catchall(wc); | |
92 | ||
93 | if (!(ofpfw & OFPFW10_IN_PORT)) { | |
94 | wc->masks.in_port = UINT16_MAX; | |
95 | } | |
96 | ||
97 | if (!(ofpfw & OFPFW10_NW_TOS)) { | |
98 | wc->masks.nw_tos |= IP_DSCP_MASK; | |
99 | } | |
100 | ||
101 | if (!(ofpfw & OFPFW10_NW_PROTO)) { | |
102 | wc->masks.nw_proto = UINT8_MAX; | |
103 | } | |
104 | wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw | |
105 | >> OFPFW10_NW_SRC_SHIFT); | |
106 | wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw | |
107 | >> OFPFW10_NW_DST_SHIFT); | |
108 | ||
109 | if (!(ofpfw & OFPFW10_TP_SRC)) { | |
110 | wc->masks.tp_src = htons(UINT16_MAX); | |
111 | } | |
112 | if (!(ofpfw & OFPFW10_TP_DST)) { | |
113 | wc->masks.tp_dst = htons(UINT16_MAX); | |
114 | } | |
115 | ||
116 | if (!(ofpfw & OFPFW10_DL_SRC)) { | |
117 | memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN); | |
118 | } | |
119 | if (!(ofpfw & OFPFW10_DL_DST)) { | |
120 | memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN); | |
121 | } | |
122 | if (!(ofpfw & OFPFW10_DL_TYPE)) { | |
123 | wc->masks.dl_type = htons(UINT16_MAX); | |
124 | } | |
125 | ||
126 | /* VLAN TCI mask. */ | |
127 | if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) { | |
128 | wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI); | |
129 | } | |
130 | if (!(ofpfw & OFPFW10_DL_VLAN)) { | |
131 | wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); | |
132 | } | |
133 | } | |
134 | ||
135 | /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */ | |
136 | void | |
137 | ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch, | |
138 | struct match *match) | |
139 | { | |
140 | uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL; | |
141 | ||
142 | /* Initialize match->wc. */ | |
143 | memset(&match->flow, 0, sizeof match->flow); | |
144 | ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc); | |
145 | ||
146 | /* Initialize most of match->flow. */ | |
147 | match->flow.nw_src = ofmatch->nw_src; | |
148 | match->flow.nw_dst = ofmatch->nw_dst; | |
149 | match->flow.in_port = ntohs(ofmatch->in_port); | |
150 | match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type); | |
151 | match->flow.tp_src = ofmatch->tp_src; | |
152 | match->flow.tp_dst = ofmatch->tp_dst; | |
153 | memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN); | |
154 | memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN); | |
155 | match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK; | |
156 | match->flow.nw_proto = ofmatch->nw_proto; | |
157 | ||
158 | /* Translate VLANs. */ | |
159 | if (!(ofpfw & OFPFW10_DL_VLAN) && | |
160 | ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) { | |
161 | /* Match only packets without 802.1Q header. | |
162 | * | |
163 | * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct. | |
164 | * | |
165 | * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory, | |
166 | * because we can't have a specific PCP without an 802.1Q header. | |
167 | * However, older versions of OVS treated this as matching packets | |
168 | * withut an 802.1Q header, so we do here too. */ | |
169 | match->flow.vlan_tci = htons(0); | |
170 | match->wc.masks.vlan_tci = htons(0xffff); | |
171 | } else { | |
172 | ovs_be16 vid, pcp, tci; | |
173 | ||
174 | vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK); | |
175 | pcp = htons((ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK); | |
176 | tci = vid | pcp | htons(VLAN_CFI); | |
177 | match->flow.vlan_tci = tci & match->wc.masks.vlan_tci; | |
178 | } | |
179 | ||
180 | /* Clean up. */ | |
181 | match_zero_wildcarded_fields(match); | |
182 | } | |
183 | ||
184 | /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */ | |
185 | void | |
186 | ofputil_match_to_ofp10_match(const struct match *match, | |
187 | struct ofp10_match *ofmatch) | |
188 | { | |
189 | const struct flow_wildcards *wc = &match->wc; | |
190 | uint32_t ofpfw; | |
191 | ||
192 | /* Figure out most OpenFlow wildcards. */ | |
193 | ofpfw = 0; | |
194 | if (!wc->masks.in_port) { | |
195 | ofpfw |= OFPFW10_IN_PORT; | |
196 | } | |
197 | if (!wc->masks.dl_type) { | |
198 | ofpfw |= OFPFW10_DL_TYPE; | |
199 | } | |
200 | if (!wc->masks.nw_proto) { | |
201 | ofpfw |= OFPFW10_NW_PROTO; | |
202 | } | |
203 | ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src) | |
204 | << OFPFW10_NW_SRC_SHIFT); | |
205 | ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst) | |
206 | << OFPFW10_NW_DST_SHIFT); | |
207 | if (!(wc->masks.nw_tos & IP_DSCP_MASK)) { | |
208 | ofpfw |= OFPFW10_NW_TOS; | |
209 | } | |
210 | if (!wc->masks.tp_src) { | |
211 | ofpfw |= OFPFW10_TP_SRC; | |
212 | } | |
213 | if (!wc->masks.tp_dst) { | |
214 | ofpfw |= OFPFW10_TP_DST; | |
215 | } | |
216 | if (eth_addr_is_zero(wc->masks.dl_src)) { | |
217 | ofpfw |= OFPFW10_DL_SRC; | |
218 | } | |
219 | if (eth_addr_is_zero(wc->masks.dl_dst)) { | |
220 | ofpfw |= OFPFW10_DL_DST; | |
221 | } | |
222 | ||
223 | /* Translate VLANs. */ | |
224 | ofmatch->dl_vlan = htons(0); | |
225 | ofmatch->dl_vlan_pcp = 0; | |
226 | if (match->wc.masks.vlan_tci == htons(0)) { | |
227 | ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP; | |
228 | } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI) | |
229 | && !(match->flow.vlan_tci & htons(VLAN_CFI))) { | |
230 | ofmatch->dl_vlan = htons(OFP10_VLAN_NONE); | |
231 | ofpfw |= OFPFW10_DL_VLAN_PCP; | |
232 | } else { | |
233 | if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) { | |
234 | ofpfw |= OFPFW10_DL_VLAN; | |
235 | } else { | |
236 | ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci)); | |
237 | } | |
238 | ||
239 | if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) { | |
240 | ofpfw |= OFPFW10_DL_VLAN_PCP; | |
241 | } else { | |
242 | ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci); | |
243 | } | |
244 | } | |
245 | ||
246 | /* Compose most of the match structure. */ | |
247 | ofmatch->wildcards = htonl(ofpfw); | |
248 | ofmatch->in_port = htons(match->flow.in_port); | |
249 | memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN); | |
250 | memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN); | |
251 | ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type); | |
252 | ofmatch->nw_src = match->flow.nw_src; | |
253 | ofmatch->nw_dst = match->flow.nw_dst; | |
254 | ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK; | |
255 | ofmatch->nw_proto = match->flow.nw_proto; | |
256 | ofmatch->tp_src = match->flow.tp_src; | |
257 | ofmatch->tp_dst = match->flow.tp_dst; | |
258 | memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1); | |
259 | memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2); | |
260 | } | |
261 | ||
262 | enum ofperr | |
263 | ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match, | |
264 | uint16_t *padded_match_len) | |
265 | { | |
266 | struct ofp11_match_header *omh = buf->data; | |
267 | uint16_t match_len; | |
268 | ||
269 | if (buf->size < sizeof *omh) { | |
270 | return OFPERR_OFPBMC_BAD_LEN; | |
271 | } | |
272 | ||
273 | match_len = ntohs(omh->length); | |
274 | ||
275 | switch (ntohs(omh->type)) { | |
276 | case OFPMT_STANDARD: { | |
277 | struct ofp11_match *om; | |
278 | ||
279 | if (match_len != sizeof *om || buf->size < sizeof *om) { | |
280 | return OFPERR_OFPBMC_BAD_LEN; | |
281 | } | |
282 | om = ofpbuf_pull(buf, sizeof *om); | |
283 | if (padded_match_len) { | |
284 | *padded_match_len = match_len; | |
285 | } | |
286 | return ofputil_match_from_ofp11_match(om, match); | |
287 | } | |
288 | ||
289 | case OFPMT_OXM: | |
290 | if (padded_match_len) { | |
291 | *padded_match_len = ROUND_UP(match_len, 8); | |
292 | } | |
293 | return oxm_pull_match(buf, match); | |
294 | ||
295 | default: | |
296 | return OFPERR_OFPBMC_BAD_TYPE; | |
297 | } | |
298 | } | |
299 | ||
300 | /* Converts the ofp11_match in 'match' into a struct match in 'match. Returns | |
301 | * 0 if successful, otherwise an OFPERR_* value. */ | |
302 | enum ofperr | |
303 | ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch, | |
304 | struct match *match) | |
305 | { | |
306 | uint16_t wc = ntohl(ofmatch->wildcards); | |
307 | uint8_t dl_src_mask[ETH_ADDR_LEN]; | |
308 | uint8_t dl_dst_mask[ETH_ADDR_LEN]; | |
309 | bool ipv4, arp, rarp; | |
310 | int i; | |
311 | ||
312 | match_init_catchall(match); | |
313 | ||
314 | if (!(wc & OFPFW11_IN_PORT)) { | |
315 | uint16_t ofp_port; | |
316 | enum ofperr error; | |
317 | ||
318 | error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port); | |
319 | if (error) { | |
320 | return OFPERR_OFPBMC_BAD_VALUE; | |
321 | } | |
322 | match_set_in_port(match, ofp_port); | |
323 | } | |
324 | ||
325 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
326 | dl_src_mask[i] = ~ofmatch->dl_src_mask[i]; | |
327 | } | |
328 | match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask); | |
329 | ||
330 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
331 | dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i]; | |
332 | } | |
333 | match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask); | |
334 | ||
335 | if (!(wc & OFPFW11_DL_VLAN)) { | |
336 | if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) { | |
337 | /* Match only packets without a VLAN tag. */ | |
338 | match->flow.vlan_tci = htons(0); | |
339 | match->wc.masks.vlan_tci = htons(UINT16_MAX); | |
340 | } else { | |
341 | if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) { | |
342 | /* Match any packet with a VLAN tag regardless of VID. */ | |
343 | match->flow.vlan_tci = htons(VLAN_CFI); | |
344 | match->wc.masks.vlan_tci = htons(VLAN_CFI); | |
345 | } else if (ntohs(ofmatch->dl_vlan) < 4096) { | |
346 | /* Match only packets with the specified VLAN VID. */ | |
347 | match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan; | |
348 | match->wc.masks.vlan_tci = 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 (ofmatch->dl_vlan_pcp <= 7) { | |
356 | match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp | |
357 | << VLAN_PCP_SHIFT); | |
358 | match->wc.masks.vlan_tci |= 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 | match_set_dl_type(match, | |
369 | ofputil_dl_type_from_openflow(ofmatch->dl_type)); | |
370 | } | |
371 | ||
372 | ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP); | |
373 | arp = match->flow.dl_type == htons(ETH_TYPE_ARP); | |
374 | rarp = match->flow.dl_type == htons(ETH_TYPE_RARP); | |
375 | ||
376 | if (ipv4 && !(wc & OFPFW11_NW_TOS)) { | |
377 | if (ofmatch->nw_tos & ~IP_DSCP_MASK) { | |
378 | /* Invalid TOS. */ | |
379 | return OFPERR_OFPBMC_BAD_VALUE; | |
380 | } | |
381 | ||
382 | match_set_nw_dscp(match, ofmatch->nw_tos); | |
383 | } | |
384 | ||
385 | if (ipv4 || arp || rarp) { | |
386 | if (!(wc & OFPFW11_NW_PROTO)) { | |
387 | match_set_nw_proto(match, ofmatch->nw_proto); | |
388 | } | |
389 | match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask); | |
390 | match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask); | |
391 | } | |
392 | ||
393 | #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST) | |
394 | if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) { | |
395 | switch (match->flow.nw_proto) { | |
396 | case IPPROTO_ICMP: | |
397 | /* "A.2.3 Flow Match Structures" in OF1.1 says: | |
398 | * | |
399 | * The tp_src and tp_dst fields will be ignored unless the | |
400 | * network protocol specified is as TCP, UDP or SCTP. | |
401 | * | |
402 | * but I'm pretty sure we should support ICMP too, otherwise | |
403 | * that's a regression from OF1.0. */ | |
404 | if (!(wc & OFPFW11_TP_SRC)) { | |
405 | uint16_t icmp_type = ntohs(ofmatch->tp_src); | |
406 | if (icmp_type < 0x100) { | |
407 | match_set_icmp_type(match, icmp_type); | |
408 | } else { | |
409 | return OFPERR_OFPBMC_BAD_FIELD; | |
410 | } | |
411 | } | |
412 | if (!(wc & OFPFW11_TP_DST)) { | |
413 | uint16_t icmp_code = ntohs(ofmatch->tp_dst); | |
414 | if (icmp_code < 0x100) { | |
415 | match_set_icmp_code(match, icmp_code); | |
416 | } else { | |
417 | return OFPERR_OFPBMC_BAD_FIELD; | |
418 | } | |
419 | } | |
420 | break; | |
421 | ||
422 | case IPPROTO_TCP: | |
423 | case IPPROTO_UDP: | |
424 | if (!(wc & (OFPFW11_TP_SRC))) { | |
425 | match_set_tp_src(match, ofmatch->tp_src); | |
426 | } | |
427 | if (!(wc & (OFPFW11_TP_DST))) { | |
428 | match_set_tp_dst(match, ofmatch->tp_dst); | |
429 | } | |
430 | break; | |
431 | ||
432 | case IPPROTO_SCTP: | |
433 | /* We don't support SCTP and it seems that we should tell the | |
434 | * controller, since OF1.1 implementations are supposed to. */ | |
435 | return OFPERR_OFPBMC_BAD_FIELD; | |
436 | ||
437 | default: | |
438 | /* OF1.1 says explicitly to ignore this. */ | |
439 | break; | |
440 | } | |
441 | } | |
442 | ||
443 | if (match->flow.dl_type == htons(ETH_TYPE_MPLS) || | |
444 | match->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) { | |
445 | enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC }; | |
446 | ||
447 | if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) { | |
448 | /* MPLS not supported. */ | |
449 | return OFPERR_OFPBMC_BAD_TAG; | |
450 | } | |
451 | } | |
452 | ||
453 | match_set_metadata_masked(match, ofmatch->metadata, | |
454 | ~ofmatch->metadata_mask); | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */ | |
460 | void | |
461 | ofputil_match_to_ofp11_match(const struct match *match, | |
462 | struct ofp11_match *ofmatch) | |
463 | { | |
464 | uint32_t wc = 0; | |
465 | int i; | |
466 | ||
467 | memset(ofmatch, 0, sizeof *ofmatch); | |
468 | ofmatch->omh.type = htons(OFPMT_STANDARD); | |
469 | ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH); | |
470 | ||
471 | if (!match->wc.masks.in_port) { | |
472 | wc |= OFPFW11_IN_PORT; | |
473 | } else { | |
474 | ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port); | |
475 | } | |
476 | ||
477 | memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN); | |
478 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
479 | ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i]; | |
480 | } | |
481 | ||
482 | memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN); | |
483 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
484 | ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i]; | |
485 | } | |
486 | ||
487 | if (match->wc.masks.vlan_tci == htons(0)) { | |
488 | wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP; | |
489 | } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI) | |
490 | && !(match->flow.vlan_tci & htons(VLAN_CFI))) { | |
491 | ofmatch->dl_vlan = htons(OFPVID11_NONE); | |
492 | wc |= OFPFW11_DL_VLAN_PCP; | |
493 | } else { | |
494 | if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) { | |
495 | ofmatch->dl_vlan = htons(OFPVID11_ANY); | |
496 | } else { | |
497 | ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci)); | |
498 | } | |
499 | ||
500 | if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) { | |
501 | wc |= OFPFW11_DL_VLAN_PCP; | |
502 | } else { | |
503 | ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci); | |
504 | } | |
505 | } | |
506 | ||
507 | if (!match->wc.masks.dl_type) { | |
508 | wc |= OFPFW11_DL_TYPE; | |
509 | } else { | |
510 | ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type); | |
511 | } | |
512 | ||
513 | if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) { | |
514 | wc |= OFPFW11_NW_TOS; | |
515 | } else { | |
516 | ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK; | |
517 | } | |
518 | ||
519 | if (!match->wc.masks.nw_proto) { | |
520 | wc |= OFPFW11_NW_PROTO; | |
521 | } else { | |
522 | ofmatch->nw_proto = match->flow.nw_proto; | |
523 | } | |
524 | ||
525 | ofmatch->nw_src = match->flow.nw_src; | |
526 | ofmatch->nw_src_mask = ~match->wc.masks.nw_src; | |
527 | ofmatch->nw_dst = match->flow.nw_dst; | |
528 | ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst; | |
529 | ||
530 | if (!match->wc.masks.tp_src) { | |
531 | wc |= OFPFW11_TP_SRC; | |
532 | } else { | |
533 | ofmatch->tp_src = match->flow.tp_src; | |
534 | } | |
535 | ||
536 | if (!match->wc.masks.tp_dst) { | |
537 | wc |= OFPFW11_TP_DST; | |
538 | } else { | |
539 | ofmatch->tp_dst = match->flow.tp_dst; | |
540 | } | |
541 | ||
542 | /* MPLS not supported. */ | |
543 | wc |= OFPFW11_MPLS_LABEL; | |
544 | wc |= OFPFW11_MPLS_TC; | |
545 | ||
546 | ofmatch->metadata = match->flow.metadata; | |
547 | ofmatch->metadata_mask = ~match->wc.masks.metadata; | |
548 | ||
549 | ofmatch->wildcards = htonl(wc); | |
550 | } | |
551 | ||
552 | /* Given a 'dl_type' value in the format used in struct flow, returns the | |
553 | * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match | |
554 | * structure. */ | |
555 | ovs_be16 | |
556 | ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type) | |
557 | { | |
558 | return (flow_dl_type == htons(FLOW_DL_TYPE_NONE) | |
559 | ? htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
560 | : flow_dl_type); | |
561 | } | |
562 | ||
563 | /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match | |
564 | * structure, returns the corresponding 'dl_type' value for use in struct | |
565 | * flow. */ | |
566 | ovs_be16 | |
567 | ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type) | |
568 | { | |
569 | return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE) | |
570 | ? htons(FLOW_DL_TYPE_NONE) | |
571 | : ofp_dl_type); | |
572 | } | |
573 | \f | |
574 | /* Protocols. */ | |
575 | ||
576 | struct proto_abbrev { | |
577 | enum ofputil_protocol protocol; | |
578 | const char *name; | |
579 | }; | |
580 | ||
581 | /* Most users really don't care about some of the differences between | |
582 | * protocols. These abbreviations help with that. */ | |
583 | static const struct proto_abbrev proto_abbrevs[] = { | |
584 | { OFPUTIL_P_ANY, "any" }, | |
585 | { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" }, | |
586 | { OFPUTIL_P_OF10_NXM_ANY, "NXM" }, | |
587 | }; | |
588 | #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs) | |
589 | ||
590 | enum ofputil_protocol ofputil_flow_dump_protocols[] = { | |
591 | OFPUTIL_P_OF12_OXM, | |
592 | OFPUTIL_P_OF10_NXM, | |
593 | OFPUTIL_P_OF10_STD, | |
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 | |
603 | ofputil_protocol_from_ofp_version(enum ofp_version version) | |
604 | { | |
605 | switch (version) { | |
606 | case OFP10_VERSION: | |
607 | return OFPUTIL_P_OF10_STD; | |
608 | case OFP12_VERSION: | |
609 | return OFPUTIL_P_OF12_OXM; | |
610 | case OFP11_VERSION: | |
611 | default: | |
612 | return 0; | |
613 | } | |
614 | } | |
615 | ||
616 | /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION, | |
617 | * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */ | |
618 | enum ofp_version | |
619 | ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol) | |
620 | { | |
621 | switch (protocol) { | |
622 | case OFPUTIL_P_OF10_STD: | |
623 | case OFPUTIL_P_OF10_STD_TID: | |
624 | case OFPUTIL_P_OF10_NXM: | |
625 | case OFPUTIL_P_OF10_NXM_TID: | |
626 | return OFP10_VERSION; | |
627 | case OFPUTIL_P_OF12_OXM: | |
628 | return OFP12_VERSION; | |
629 | } | |
630 | ||
631 | NOT_REACHED(); | |
632 | } | |
633 | ||
634 | /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false | |
635 | * otherwise. */ | |
636 | bool | |
637 | ofputil_protocol_is_valid(enum ofputil_protocol protocol) | |
638 | { | |
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_STD: | |
657 | case OFPUTIL_P_OF10_STD_TID: | |
658 | return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD; | |
659 | ||
660 | case OFPUTIL_P_OF10_NXM: | |
661 | case OFPUTIL_P_OF10_NXM_TID: | |
662 | return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM; | |
663 | ||
664 | case OFPUTIL_P_OF12_OXM: | |
665 | return OFPUTIL_P_OF12_OXM; | |
666 | ||
667 | default: | |
668 | NOT_REACHED(); | |
669 | } | |
670 | } | |
671 | ||
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); | |
680 | } | |
681 | ||
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) | |
686 | { | |
687 | bool tid = (cur & OFPUTIL_P_TID) != 0; | |
688 | ||
689 | switch (new_base) { | |
690 | case OFPUTIL_P_OF10_STD: | |
691 | case OFPUTIL_P_OF10_STD_TID: | |
692 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid); | |
693 | ||
694 | case OFPUTIL_P_OF10_NXM: | |
695 | case OFPUTIL_P_OF10_NXM_TID: | |
696 | return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid); | |
697 | ||
698 | case OFPUTIL_P_OF12_OXM: | |
699 | return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid); | |
700 | ||
701 | default: | |
702 | NOT_REACHED(); | |
703 | } | |
704 | } | |
705 | ||
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) | |
711 | { | |
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_OF10_NXM: | |
718 | return "NXM-table_id"; | |
719 | ||
720 | case OFPUTIL_P_OF10_NXM_TID: | |
721 | return "NXM+table_id"; | |
722 | ||
723 | case OFPUTIL_P_OF10_STD: | |
724 | return "OpenFlow10-table_id"; | |
725 | ||
726 | case OFPUTIL_P_OF10_STD_TID: | |
727 | return "OpenFlow10+table_id"; | |
728 | ||
729 | case OFPUTIL_P_OF12_OXM: | |
730 | return "OXM"; | |
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; | |
849 | } | |
850 | ||
851 | static int | |
852 | ofputil_version_from_string(const char *s) | |
853 | { | |
854 | if (!strcasecmp(s, "OpenFlow10")) { | |
855 | return OFP10_VERSION; | |
856 | } | |
857 | if (!strcasecmp(s, "OpenFlow11")) { | |
858 | return OFP11_VERSION; | |
859 | } | |
860 | if (!strcasecmp(s, "OpenFlow12")) { | |
861 | return OFP12_VERSION; | |
862 | } | |
863 | return 0; | |
864 | } | |
865 | ||
866 | static bool | |
867 | is_delimiter(char c) | |
868 | { | |
869 | return isspace(c) || c == ','; | |
870 | } | |
871 | ||
872 | uint32_t | |
873 | ofputil_versions_from_string(const char *s) | |
874 | { | |
875 | size_t i = 0; | |
876 | uint32_t bitmap = 0; | |
877 | ||
878 | while (s[i]) { | |
879 | size_t j; | |
880 | int version; | |
881 | char *key; | |
882 | ||
883 | if (is_delimiter(s[i])) { | |
884 | i++; | |
885 | continue; | |
886 | } | |
887 | j = 0; | |
888 | while (s[i + j] && !is_delimiter(s[i + j])) { | |
889 | j++; | |
890 | } | |
891 | key = xmemdup0(s + i, j); | |
892 | version = ofputil_version_from_string(key); | |
893 | if (!version) { | |
894 | VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key); | |
895 | } | |
896 | free(key); | |
897 | bitmap |= 1u << version; | |
898 | i += j; | |
899 | } | |
900 | ||
901 | return bitmap; | |
902 | } | |
903 | ||
904 | uint32_t | |
905 | ofputil_versions_from_strings(char ** const s, size_t count) | |
906 | { | |
907 | uint32_t bitmap = 0; | |
908 | ||
909 | while (count--) { | |
910 | int version = ofputil_version_from_string(s[count]); | |
911 | if (!version) { | |
912 | VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]); | |
913 | } else { | |
914 | bitmap |= 1u << version; | |
915 | } | |
916 | } | |
917 | ||
918 | return bitmap; | |
919 | } | |
920 | ||
921 | const char * | |
922 | ofputil_version_to_string(enum ofp_version ofp_version) | |
923 | { | |
924 | switch (ofp_version) { | |
925 | case OFP10_VERSION: | |
926 | return "OpenFlow10"; | |
927 | case OFP11_VERSION: | |
928 | return "OpenFlow11"; | |
929 | case OFP12_VERSION: | |
930 | return "OpenFlow12"; | |
931 | default: | |
932 | NOT_REACHED(); | |
933 | } | |
934 | } | |
935 | ||
936 | bool | |
937 | ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format) | |
938 | { | |
939 | switch (packet_in_format) { | |
940 | case NXPIF_OPENFLOW10: | |
941 | case NXPIF_NXM: | |
942 | return true; | |
943 | } | |
944 | ||
945 | return false; | |
946 | } | |
947 | ||
948 | const char * | |
949 | ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format) | |
950 | { | |
951 | switch (packet_in_format) { | |
952 | case NXPIF_OPENFLOW10: | |
953 | return "openflow10"; | |
954 | case NXPIF_NXM: | |
955 | return "nxm"; | |
956 | default: | |
957 | NOT_REACHED(); | |
958 | } | |
959 | } | |
960 | ||
961 | int | |
962 | ofputil_packet_in_format_from_string(const char *s) | |
963 | { | |
964 | return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10 | |
965 | : !strcmp(s, "nxm") ? NXPIF_NXM | |
966 | : -1); | |
967 | } | |
968 | ||
969 | static bool | |
970 | regs_fully_wildcarded(const struct flow_wildcards *wc) | |
971 | { | |
972 | int i; | |
973 | ||
974 | for (i = 0; i < FLOW_N_REGS; i++) { | |
975 | if (wc->masks.regs[i] != 0) { | |
976 | return false; | |
977 | } | |
978 | } | |
979 | return true; | |
980 | } | |
981 | ||
982 | static bool | |
983 | tun_parms_fully_wildcarded(const struct flow_wildcards *wc) | |
984 | { | |
985 | return (!wc->masks.tunnel.ip_src && | |
986 | !wc->masks.tunnel.ip_dst && | |
987 | !wc->masks.tunnel.ip_ttl && | |
988 | !wc->masks.tunnel.ip_tos && | |
989 | !wc->masks.tunnel.flags); | |
990 | } | |
991 | ||
992 | /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match' | |
993 | * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, | |
994 | * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to | |
995 | * use OpenFlow 1.0 protocol for backward compatibility. */ | |
996 | enum ofputil_protocol | |
997 | ofputil_usable_protocols(const struct match *match) | |
998 | { | |
999 | const struct flow_wildcards *wc = &match->wc; | |
1000 | ||
1001 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18); | |
1002 | ||
1003 | /* tunnel params other than tun_id can't be sent in a flow_mod */ | |
1004 | if (!tun_parms_fully_wildcarded(wc)) { | |
1005 | return OFPUTIL_P_NONE; | |
1006 | } | |
1007 | ||
1008 | /* NXM, OXM, and OF1.1 support bitwise matching on ethernet addresses. */ | |
1009 | if (!eth_mask_is_exact(wc->masks.dl_src) | |
1010 | && !eth_addr_is_zero(wc->masks.dl_src)) { | |
1011 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1012 | } | |
1013 | if (!eth_mask_is_exact(wc->masks.dl_dst) | |
1014 | && !eth_addr_is_zero(wc->masks.dl_dst)) { | |
1015 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1016 | } | |
1017 | ||
1018 | /* NXM, OXM, and OF1.1+ support matching metadata. */ | |
1019 | if (wc->masks.metadata != htonll(0)) { | |
1020 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1021 | } | |
1022 | ||
1023 | /* NXM and OXM support matching ARP hardware addresses. */ | |
1024 | if (!eth_addr_is_zero(wc->masks.arp_sha) || | |
1025 | !eth_addr_is_zero(wc->masks.arp_tha)) { | |
1026 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1027 | } | |
1028 | ||
1029 | /* NXM and OXM support matching IPv6 traffic. */ | |
1030 | if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) { | |
1031 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1032 | } | |
1033 | ||
1034 | /* NXM and OXM support matching registers. */ | |
1035 | if (!regs_fully_wildcarded(wc)) { | |
1036 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1037 | } | |
1038 | ||
1039 | /* NXM and OXM support matching tun_id. */ | |
1040 | if (wc->masks.tunnel.tun_id != htonll(0)) { | |
1041 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1042 | } | |
1043 | ||
1044 | /* NXM and OXM support matching fragments. */ | |
1045 | if (wc->masks.nw_frag) { | |
1046 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1047 | } | |
1048 | ||
1049 | /* NXM and OXM support matching IPv6 flow label. */ | |
1050 | if (wc->masks.ipv6_label) { | |
1051 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1052 | } | |
1053 | ||
1054 | /* NXM and OXM support matching IP ECN bits. */ | |
1055 | if (wc->masks.nw_tos & IP_ECN_MASK) { | |
1056 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1057 | } | |
1058 | ||
1059 | /* NXM and OXM support matching IP TTL/hop limit. */ | |
1060 | if (wc->masks.nw_ttl) { | |
1061 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1062 | } | |
1063 | ||
1064 | /* NXM and OXM support non-CIDR IPv4 address masks. */ | |
1065 | if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) { | |
1066 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1067 | } | |
1068 | ||
1069 | /* NXM and OXM support bitwise matching on transport port. */ | |
1070 | if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) || | |
1071 | (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) { | |
1072 | return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1073 | } | |
1074 | ||
1075 | /* Other formats can express this rule. */ | |
1076 | return OFPUTIL_P_ANY; | |
1077 | } | |
1078 | ||
1079 | void | |
1080 | ofputil_format_version(struct ds *msg, enum ofp_version version) | |
1081 | { | |
1082 | ds_put_format(msg, "0x%02x", version); | |
1083 | } | |
1084 | ||
1085 | void | |
1086 | ofputil_format_version_name(struct ds *msg, enum ofp_version version) | |
1087 | { | |
1088 | ds_put_cstr(msg, ofputil_version_to_string(version)); | |
1089 | } | |
1090 | ||
1091 | static void | |
1092 | ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap, | |
1093 | void (*format_version)(struct ds *msg, | |
1094 | enum ofp_version)) | |
1095 | { | |
1096 | while (bitmap) { | |
1097 | format_version(msg, raw_ctz(bitmap)); | |
1098 | bitmap = zero_rightmost_1bit(bitmap); | |
1099 | if (bitmap) { | |
1100 | ds_put_cstr(msg, ", "); | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | void | |
1106 | ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap) | |
1107 | { | |
1108 | ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version); | |
1109 | } | |
1110 | ||
1111 | void | |
1112 | ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap) | |
1113 | { | |
1114 | ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name); | |
1115 | } | |
1116 | ||
1117 | static bool | |
1118 | ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh, | |
1119 | uint32_t *allowed_versionsp) | |
1120 | { | |
1121 | uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh; | |
1122 | const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1); | |
1123 | uint32_t allowed_versions; | |
1124 | ||
1125 | if (!bitmap_len || bitmap_len % sizeof *bitmap) { | |
1126 | return false; | |
1127 | } | |
1128 | ||
1129 | /* Only use the first 32-bit element of the bitmap as that is all the | |
1130 | * current implementation supports. Subsequent elements are ignored which | |
1131 | * should have no effect on session negotiation until Open vSwtich supports | |
1132 | * wire-protocol versions greater than 31. | |
1133 | */ | |
1134 | allowed_versions = ntohl(bitmap[0]); | |
1135 | ||
1136 | if (allowed_versions & 1) { | |
1137 | /* There's no OpenFlow version 0. */ | |
1138 | VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow " | |
1139 | "version 0x00"); | |
1140 | allowed_versions &= ~1u; | |
1141 | } | |
1142 | ||
1143 | if (!allowed_versions) { | |
1144 | VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow " | |
1145 | "version (between 0x01 and 0x1f)"); | |
1146 | return false; | |
1147 | } | |
1148 | ||
1149 | *allowed_versionsp = allowed_versions; | |
1150 | return true; | |
1151 | } | |
1152 | ||
1153 | static uint32_t | |
1154 | version_bitmap_from_version(uint8_t ofp_version) | |
1155 | { | |
1156 | return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1; | |
1157 | } | |
1158 | ||
1159 | /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions' | |
1160 | * the set of OpenFlow versions for which 'oh' announces support. | |
1161 | * | |
1162 | * Because of how OpenFlow defines OFPT_HELLO messages, this function is always | |
1163 | * successful, and thus '*allowed_versions' is always initialized. However, it | |
1164 | * returns false if 'oh' contains some data that could not be fully understood, | |
1165 | * true if 'oh' was completely parsed. */ | |
1166 | bool | |
1167 | ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions) | |
1168 | { | |
1169 | struct ofpbuf msg; | |
1170 | bool ok = true; | |
1171 | ||
1172 | ofpbuf_use_const(&msg, oh, ntohs(oh->length)); | |
1173 | ofpbuf_pull(&msg, sizeof *oh); | |
1174 | ||
1175 | *allowed_versions = version_bitmap_from_version(oh->version); | |
1176 | while (msg.size) { | |
1177 | const struct ofp_hello_elem_header *oheh; | |
1178 | unsigned int len; | |
1179 | ||
1180 | if (msg.size < sizeof *oheh) { | |
1181 | return false; | |
1182 | } | |
1183 | ||
1184 | oheh = msg.data; | |
1185 | len = ntohs(oheh->length); | |
1186 | if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) { | |
1187 | return false; | |
1188 | } | |
1189 | ||
1190 | if (oheh->type != htons(OFPHET_VERSIONBITMAP) | |
1191 | || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) { | |
1192 | ok = false; | |
1193 | } | |
1194 | } | |
1195 | ||
1196 | return ok; | |
1197 | } | |
1198 | ||
1199 | /* Returns true if 'allowed_versions' needs to be accompanied by a version | |
1200 | * bitmap to be correctly expressed in an OFPT_HELLO message. */ | |
1201 | static inline bool | |
1202 | should_send_version_bitmap(uint32_t allowed_versions) | |
1203 | { | |
1204 | return !is_pow2((allowed_versions >> 1) + 1); | |
1205 | } | |
1206 | ||
1207 | /* Create an OFPT_HELLO message that expresses support for the OpenFlow | |
1208 | * versions in the 'allowed_versions' bitmaps and returns the message. */ | |
1209 | struct ofpbuf * | |
1210 | ofputil_encode_hello(uint32_t allowed_versions) | |
1211 | { | |
1212 | enum ofp_version ofp_version; | |
1213 | struct ofpbuf *msg; | |
1214 | ||
1215 | ofp_version = leftmost_1bit_idx(allowed_versions); | |
1216 | msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0); | |
1217 | ||
1218 | if (should_send_version_bitmap(allowed_versions)) { | |
1219 | struct ofp_hello_elem_header *oheh; | |
1220 | uint16_t map_len; | |
1221 | ||
1222 | map_len = sizeof allowed_versions; | |
1223 | oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8)); | |
1224 | oheh->type = htons(OFPHET_VERSIONBITMAP); | |
1225 | oheh->length = htons(map_len + sizeof *oheh); | |
1226 | *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions); | |
1227 | ||
1228 | ofpmsg_update_length(msg); | |
1229 | } | |
1230 | ||
1231 | return msg; | |
1232 | } | |
1233 | ||
1234 | /* Returns an OpenFlow message that, sent on an OpenFlow connection whose | |
1235 | * protocol is 'current', at least partly transitions the protocol to 'want'. | |
1236 | * Stores in '*next' the protocol that will be in effect on the OpenFlow | |
1237 | * connection if the switch processes the returned message correctly. (If | |
1238 | * '*next != want' then the caller will have to iterate.) | |
1239 | * | |
1240 | * If 'current == want', or if it is not possible to transition from 'current' | |
1241 | * to 'want' (because, for example, 'current' and 'want' use different OpenFlow | |
1242 | * protocol versions), returns NULL and stores 'current' in '*next'. */ | |
1243 | struct ofpbuf * | |
1244 | ofputil_encode_set_protocol(enum ofputil_protocol current, | |
1245 | enum ofputil_protocol want, | |
1246 | enum ofputil_protocol *next) | |
1247 | { | |
1248 | enum ofp_version cur_version, want_version; | |
1249 | enum ofputil_protocol cur_base, want_base; | |
1250 | bool cur_tid, want_tid; | |
1251 | ||
1252 | cur_version = ofputil_protocol_to_ofp_version(current); | |
1253 | want_version = ofputil_protocol_to_ofp_version(want); | |
1254 | if (cur_version != want_version) { | |
1255 | *next = current; | |
1256 | return NULL; | |
1257 | } | |
1258 | ||
1259 | cur_base = ofputil_protocol_to_base(current); | |
1260 | want_base = ofputil_protocol_to_base(want); | |
1261 | if (cur_base != want_base) { | |
1262 | *next = ofputil_protocol_set_base(current, want_base); | |
1263 | ||
1264 | switch (want_base) { | |
1265 | case OFPUTIL_P_OF10_NXM: | |
1266 | return ofputil_encode_nx_set_flow_format(NXFF_NXM); | |
1267 | ||
1268 | case OFPUTIL_P_OF10_STD: | |
1269 | return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10); | |
1270 | ||
1271 | case OFPUTIL_P_OF12_OXM: | |
1272 | /* There's only one OpenFlow 1.2 protocol and we already verified | |
1273 | * above that we're not trying to change versions. */ | |
1274 | NOT_REACHED(); | |
1275 | ||
1276 | case OFPUTIL_P_OF10_STD_TID: | |
1277 | case OFPUTIL_P_OF10_NXM_TID: | |
1278 | NOT_REACHED(); | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | cur_tid = (current & OFPUTIL_P_TID) != 0; | |
1283 | want_tid = (want & OFPUTIL_P_TID) != 0; | |
1284 | if (cur_tid != want_tid) { | |
1285 | *next = ofputil_protocol_set_tid(current, want_tid); | |
1286 | return ofputil_make_flow_mod_table_id(want_tid); | |
1287 | } | |
1288 | ||
1289 | assert(current == want); | |
1290 | ||
1291 | *next = current; | |
1292 | return NULL; | |
1293 | } | |
1294 | ||
1295 | /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow | |
1296 | * format to 'nxff'. */ | |
1297 | struct ofpbuf * | |
1298 | ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff) | |
1299 | { | |
1300 | struct nx_set_flow_format *sff; | |
1301 | struct ofpbuf *msg; | |
1302 | ||
1303 | assert(ofputil_nx_flow_format_is_valid(nxff)); | |
1304 | ||
1305 | msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0); | |
1306 | sff = ofpbuf_put_zeros(msg, sizeof *sff); | |
1307 | sff->format = htonl(nxff); | |
1308 | ||
1309 | return msg; | |
1310 | } | |
1311 | ||
1312 | /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false | |
1313 | * otherwise. */ | |
1314 | enum ofputil_protocol | |
1315 | ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format) | |
1316 | { | |
1317 | switch (flow_format) { | |
1318 | case NXFF_OPENFLOW10: | |
1319 | return OFPUTIL_P_OF10_STD; | |
1320 | ||
1321 | case NXFF_NXM: | |
1322 | return OFPUTIL_P_OF10_NXM; | |
1323 | ||
1324 | default: | |
1325 | return 0; | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */ | |
1330 | bool | |
1331 | ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format) | |
1332 | { | |
1333 | return ofputil_nx_flow_format_to_protocol(flow_format) != 0; | |
1334 | } | |
1335 | ||
1336 | /* Returns a string version of 'flow_format', which must be a valid NXFF_* | |
1337 | * value. */ | |
1338 | const char * | |
1339 | ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format) | |
1340 | { | |
1341 | switch (flow_format) { | |
1342 | case NXFF_OPENFLOW10: | |
1343 | return "openflow10"; | |
1344 | case NXFF_NXM: | |
1345 | return "nxm"; | |
1346 | default: | |
1347 | NOT_REACHED(); | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | struct ofpbuf * | |
1352 | ofputil_make_set_packet_in_format(enum ofp_version ofp_version, | |
1353 | enum nx_packet_in_format packet_in_format) | |
1354 | { | |
1355 | struct nx_set_packet_in_format *spif; | |
1356 | struct ofpbuf *msg; | |
1357 | ||
1358 | msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0); | |
1359 | spif = ofpbuf_put_zeros(msg, sizeof *spif); | |
1360 | spif->format = htonl(packet_in_format); | |
1361 | ||
1362 | return msg; | |
1363 | } | |
1364 | ||
1365 | /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id | |
1366 | * extension on or off (according to 'flow_mod_table_id'). */ | |
1367 | struct ofpbuf * | |
1368 | ofputil_make_flow_mod_table_id(bool flow_mod_table_id) | |
1369 | { | |
1370 | struct nx_flow_mod_table_id *nfmti; | |
1371 | struct ofpbuf *msg; | |
1372 | ||
1373 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0); | |
1374 | nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti); | |
1375 | nfmti->set = flow_mod_table_id; | |
1376 | return msg; | |
1377 | } | |
1378 | ||
1379 | /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract | |
1380 | * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error | |
1381 | * code. | |
1382 | * | |
1383 | * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions. | |
1384 | * The caller must initialize 'ofpacts' and retains ownership of it. | |
1385 | * 'fm->ofpacts' will point into the 'ofpacts' buffer. | |
1386 | * | |
1387 | * Does not validate the flow_mod actions. The caller should do that, with | |
1388 | * ofpacts_check(). */ | |
1389 | enum ofperr | |
1390 | ofputil_decode_flow_mod(struct ofputil_flow_mod *fm, | |
1391 | const struct ofp_header *oh, | |
1392 | enum ofputil_protocol protocol, | |
1393 | struct ofpbuf *ofpacts) | |
1394 | { | |
1395 | uint16_t command; | |
1396 | struct ofpbuf b; | |
1397 | enum ofpraw raw; | |
1398 | ||
1399 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1400 | raw = ofpraw_pull_assert(&b); | |
1401 | if (raw == OFPRAW_OFPT11_FLOW_MOD) { | |
1402 | /* Standard OpenFlow 1.1 flow_mod. */ | |
1403 | const struct ofp11_flow_mod *ofm; | |
1404 | enum ofperr error; | |
1405 | ||
1406 | ofm = ofpbuf_pull(&b, sizeof *ofm); | |
1407 | ||
1408 | error = ofputil_pull_ofp11_match(&b, &fm->match, NULL); | |
1409 | if (error) { | |
1410 | return error; | |
1411 | } | |
1412 | ||
1413 | error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts); | |
1414 | if (error) { | |
1415 | return error; | |
1416 | } | |
1417 | ||
1418 | /* Translate the message. */ | |
1419 | fm->priority = ntohs(ofm->priority); | |
1420 | if (ofm->command == OFPFC_ADD) { | |
1421 | fm->cookie = htonll(0); | |
1422 | fm->cookie_mask = htonll(0); | |
1423 | fm->new_cookie = ofm->cookie; | |
1424 | } else { | |
1425 | fm->cookie = ofm->cookie; | |
1426 | fm->cookie_mask = ofm->cookie_mask; | |
1427 | fm->new_cookie = htonll(UINT64_MAX); | |
1428 | } | |
1429 | fm->command = ofm->command; | |
1430 | fm->table_id = ofm->table_id; | |
1431 | fm->idle_timeout = ntohs(ofm->idle_timeout); | |
1432 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
1433 | fm->buffer_id = ntohl(ofm->buffer_id); | |
1434 | error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port); | |
1435 | if (error) { | |
1436 | return error; | |
1437 | } | |
1438 | if ((ofm->command == OFPFC_DELETE | |
1439 | || ofm->command == OFPFC_DELETE_STRICT) | |
1440 | && ofm->out_group != htonl(OFPG_ANY)) { | |
1441 | return OFPERR_OFPFMFC_UNKNOWN; | |
1442 | } | |
1443 | fm->flags = ntohs(ofm->flags); | |
1444 | } else { | |
1445 | if (raw == OFPRAW_OFPT10_FLOW_MOD) { | |
1446 | /* Standard OpenFlow 1.0 flow_mod. */ | |
1447 | const struct ofp10_flow_mod *ofm; | |
1448 | enum ofperr error; | |
1449 | ||
1450 | /* Get the ofp10_flow_mod. */ | |
1451 | ofm = ofpbuf_pull(&b, sizeof *ofm); | |
1452 | ||
1453 | /* Translate the rule. */ | |
1454 | ofputil_match_from_ofp10_match(&ofm->match, &fm->match); | |
1455 | ofputil_normalize_match(&fm->match); | |
1456 | ||
1457 | /* Now get the actions. */ | |
1458 | error = ofpacts_pull_openflow10(&b, b.size, ofpacts); | |
1459 | if (error) { | |
1460 | return error; | |
1461 | } | |
1462 | ||
1463 | /* OpenFlow 1.0 says that exact-match rules have to have the | |
1464 | * highest possible priority. */ | |
1465 | fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL) | |
1466 | ? ntohs(ofm->priority) | |
1467 | : UINT16_MAX); | |
1468 | ||
1469 | /* Translate the message. */ | |
1470 | command = ntohs(ofm->command); | |
1471 | fm->cookie = htonll(0); | |
1472 | fm->cookie_mask = htonll(0); | |
1473 | fm->new_cookie = ofm->cookie; | |
1474 | fm->idle_timeout = ntohs(ofm->idle_timeout); | |
1475 | fm->hard_timeout = ntohs(ofm->hard_timeout); | |
1476 | fm->buffer_id = ntohl(ofm->buffer_id); | |
1477 | fm->out_port = ntohs(ofm->out_port); | |
1478 | fm->flags = ntohs(ofm->flags); | |
1479 | } else if (raw == OFPRAW_NXT_FLOW_MOD) { | |
1480 | /* Nicira extended flow_mod. */ | |
1481 | const struct nx_flow_mod *nfm; | |
1482 | enum ofperr error; | |
1483 | ||
1484 | /* Dissect the message. */ | |
1485 | nfm = ofpbuf_pull(&b, sizeof *nfm); | |
1486 | error = nx_pull_match(&b, ntohs(nfm->match_len), | |
1487 | &fm->match, &fm->cookie, &fm->cookie_mask); | |
1488 | if (error) { | |
1489 | return error; | |
1490 | } | |
1491 | error = ofpacts_pull_openflow10(&b, b.size, ofpacts); | |
1492 | if (error) { | |
1493 | return error; | |
1494 | } | |
1495 | ||
1496 | /* Translate the message. */ | |
1497 | command = ntohs(nfm->command); | |
1498 | if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) { | |
1499 | /* Flow additions may only set a new cookie, not match an | |
1500 | * existing cookie. */ | |
1501 | return OFPERR_NXBRC_NXM_INVALID; | |
1502 | } | |
1503 | fm->priority = ntohs(nfm->priority); | |
1504 | fm->new_cookie = nfm->cookie; | |
1505 | fm->idle_timeout = ntohs(nfm->idle_timeout); | |
1506 | fm->hard_timeout = ntohs(nfm->hard_timeout); | |
1507 | fm->buffer_id = ntohl(nfm->buffer_id); | |
1508 | fm->out_port = ntohs(nfm->out_port); | |
1509 | fm->flags = ntohs(nfm->flags); | |
1510 | } else { | |
1511 | NOT_REACHED(); | |
1512 | } | |
1513 | ||
1514 | if (protocol & OFPUTIL_P_TID) { | |
1515 | fm->command = command & 0xff; | |
1516 | fm->table_id = command >> 8; | |
1517 | } else { | |
1518 | fm->command = command; | |
1519 | fm->table_id = 0xff; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | fm->ofpacts = ofpacts->data; | |
1524 | fm->ofpacts_len = ofpacts->size; | |
1525 | ||
1526 | return 0; | |
1527 | } | |
1528 | ||
1529 | static ovs_be16 | |
1530 | ofputil_tid_command(const struct ofputil_flow_mod *fm, | |
1531 | enum ofputil_protocol protocol) | |
1532 | { | |
1533 | return htons(protocol & OFPUTIL_P_TID | |
1534 | ? (fm->command & 0xff) | (fm->table_id << 8) | |
1535 | : fm->command); | |
1536 | } | |
1537 | ||
1538 | /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to | |
1539 | * 'protocol' and returns the message. */ | |
1540 | struct ofpbuf * | |
1541 | ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm, | |
1542 | enum ofputil_protocol protocol) | |
1543 | { | |
1544 | struct ofpbuf *msg; | |
1545 | ||
1546 | switch (protocol) { | |
1547 | case OFPUTIL_P_OF12_OXM: { | |
1548 | struct ofp11_flow_mod *ofm; | |
1549 | ||
1550 | msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION, | |
1551 | NXM_TYPICAL_LEN + fm->ofpacts_len); | |
1552 | ofm = ofpbuf_put_zeros(msg, sizeof *ofm); | |
1553 | if (fm->command == OFPFC_ADD) { | |
1554 | ofm->cookie = fm->new_cookie; | |
1555 | } else { | |
1556 | ofm->cookie = fm->cookie; | |
1557 | } | |
1558 | ofm->cookie_mask = fm->cookie_mask; | |
1559 | ofm->table_id = fm->table_id; | |
1560 | ofm->command = fm->command; | |
1561 | ofm->idle_timeout = htons(fm->idle_timeout); | |
1562 | ofm->hard_timeout = htons(fm->hard_timeout); | |
1563 | ofm->priority = htons(fm->priority); | |
1564 | ofm->buffer_id = htonl(fm->buffer_id); | |
1565 | ofm->out_port = ofputil_port_to_ofp11(fm->out_port); | |
1566 | ofm->out_group = htonl(OFPG11_ANY); | |
1567 | ofm->flags = htons(fm->flags); | |
1568 | oxm_put_match(msg, &fm->match); | |
1569 | ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg); | |
1570 | break; | |
1571 | } | |
1572 | ||
1573 | case OFPUTIL_P_OF10_STD: | |
1574 | case OFPUTIL_P_OF10_STD_TID: { | |
1575 | struct ofp10_flow_mod *ofm; | |
1576 | ||
1577 | msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION, | |
1578 | fm->ofpacts_len); | |
1579 | ofm = ofpbuf_put_zeros(msg, sizeof *ofm); | |
1580 | ofputil_match_to_ofp10_match(&fm->match, &ofm->match); | |
1581 | ofm->cookie = fm->new_cookie; | |
1582 | ofm->command = ofputil_tid_command(fm, protocol); | |
1583 | ofm->idle_timeout = htons(fm->idle_timeout); | |
1584 | ofm->hard_timeout = htons(fm->hard_timeout); | |
1585 | ofm->priority = htons(fm->priority); | |
1586 | ofm->buffer_id = htonl(fm->buffer_id); | |
1587 | ofm->out_port = htons(fm->out_port); | |
1588 | ofm->flags = htons(fm->flags); | |
1589 | ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg); | |
1590 | break; | |
1591 | } | |
1592 | ||
1593 | case OFPUTIL_P_OF10_NXM: | |
1594 | case OFPUTIL_P_OF10_NXM_TID: { | |
1595 | struct nx_flow_mod *nfm; | |
1596 | int match_len; | |
1597 | ||
1598 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION, | |
1599 | NXM_TYPICAL_LEN + fm->ofpacts_len); | |
1600 | nfm = ofpbuf_put_zeros(msg, sizeof *nfm); | |
1601 | nfm->command = ofputil_tid_command(fm, protocol); | |
1602 | nfm->cookie = fm->new_cookie; | |
1603 | match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask); | |
1604 | nfm = msg->l3; | |
1605 | nfm->idle_timeout = htons(fm->idle_timeout); | |
1606 | nfm->hard_timeout = htons(fm->hard_timeout); | |
1607 | nfm->priority = htons(fm->priority); | |
1608 | nfm->buffer_id = htonl(fm->buffer_id); | |
1609 | nfm->out_port = htons(fm->out_port); | |
1610 | nfm->flags = htons(fm->flags); | |
1611 | nfm->match_len = htons(match_len); | |
1612 | ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg); | |
1613 | break; | |
1614 | } | |
1615 | ||
1616 | default: | |
1617 | NOT_REACHED(); | |
1618 | } | |
1619 | ||
1620 | ofpmsg_update_length(msg); | |
1621 | return msg; | |
1622 | } | |
1623 | ||
1624 | /* Returns a bitmask with a 1-bit for each protocol that could be used to | |
1625 | * send all of the 'n_fm's flow table modification requests in 'fms', and a | |
1626 | * 0-bit for each protocol that is inadequate. | |
1627 | * | |
1628 | * (The return value will have at least one 1-bit.) */ | |
1629 | enum ofputil_protocol | |
1630 | ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms, | |
1631 | size_t n_fms) | |
1632 | { | |
1633 | enum ofputil_protocol usable_protocols; | |
1634 | size_t i; | |
1635 | ||
1636 | usable_protocols = OFPUTIL_P_ANY; | |
1637 | for (i = 0; i < n_fms; i++) { | |
1638 | const struct ofputil_flow_mod *fm = &fms[i]; | |
1639 | ||
1640 | usable_protocols &= ofputil_usable_protocols(&fm->match); | |
1641 | if (fm->table_id != 0xff) { | |
1642 | usable_protocols &= OFPUTIL_P_TID; | |
1643 | } | |
1644 | ||
1645 | /* Matching of the cookie is only supported through NXM or OF1.1+. */ | |
1646 | if (fm->cookie_mask != htonll(0)) { | |
1647 | usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | return usable_protocols; | |
1652 | } | |
1653 | ||
1654 | static enum ofperr | |
1655 | ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr, | |
1656 | const struct ofp10_flow_stats_request *ofsr, | |
1657 | bool aggregate) | |
1658 | { | |
1659 | fsr->aggregate = aggregate; | |
1660 | ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match); | |
1661 | fsr->out_port = ntohs(ofsr->out_port); | |
1662 | fsr->table_id = ofsr->table_id; | |
1663 | fsr->cookie = fsr->cookie_mask = htonll(0); | |
1664 | ||
1665 | return 0; | |
1666 | } | |
1667 | ||
1668 | static enum ofperr | |
1669 | ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr, | |
1670 | struct ofpbuf *b, bool aggregate) | |
1671 | { | |
1672 | const struct ofp11_flow_stats_request *ofsr; | |
1673 | enum ofperr error; | |
1674 | ||
1675 | ofsr = ofpbuf_pull(b, sizeof *ofsr); | |
1676 | fsr->aggregate = aggregate; | |
1677 | fsr->table_id = ofsr->table_id; | |
1678 | error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port); | |
1679 | if (error) { | |
1680 | return error; | |
1681 | } | |
1682 | if (ofsr->out_group != htonl(OFPG11_ANY)) { | |
1683 | return OFPERR_OFPFMFC_UNKNOWN; | |
1684 | } | |
1685 | fsr->cookie = ofsr->cookie; | |
1686 | fsr->cookie_mask = ofsr->cookie_mask; | |
1687 | error = ofputil_pull_ofp11_match(b, &fsr->match, NULL); | |
1688 | if (error) { | |
1689 | return error; | |
1690 | } | |
1691 | ||
1692 | return 0; | |
1693 | } | |
1694 | ||
1695 | static enum ofperr | |
1696 | ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr, | |
1697 | struct ofpbuf *b, bool aggregate) | |
1698 | { | |
1699 | const struct nx_flow_stats_request *nfsr; | |
1700 | enum ofperr error; | |
1701 | ||
1702 | nfsr = ofpbuf_pull(b, sizeof *nfsr); | |
1703 | error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match, | |
1704 | &fsr->cookie, &fsr->cookie_mask); | |
1705 | if (error) { | |
1706 | return error; | |
1707 | } | |
1708 | if (b->size) { | |
1709 | return OFPERR_OFPBRC_BAD_LEN; | |
1710 | } | |
1711 | ||
1712 | fsr->aggregate = aggregate; | |
1713 | fsr->out_port = ntohs(nfsr->out_port); | |
1714 | fsr->table_id = nfsr->table_id; | |
1715 | ||
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE | |
1720 | * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if | |
1721 | * successful, otherwise an OpenFlow error code. */ | |
1722 | enum ofperr | |
1723 | ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr, | |
1724 | const struct ofp_header *oh) | |
1725 | { | |
1726 | enum ofpraw raw; | |
1727 | struct ofpbuf b; | |
1728 | ||
1729 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
1730 | raw = ofpraw_pull_assert(&b); | |
1731 | switch ((int) raw) { | |
1732 | case OFPRAW_OFPST10_FLOW_REQUEST: | |
1733 | return ofputil_decode_ofpst10_flow_request(fsr, b.data, false); | |
1734 | ||
1735 | case OFPRAW_OFPST10_AGGREGATE_REQUEST: | |
1736 | return ofputil_decode_ofpst10_flow_request(fsr, b.data, true); | |
1737 | ||
1738 | case OFPRAW_OFPST11_FLOW_REQUEST: | |
1739 | return ofputil_decode_ofpst11_flow_request(fsr, &b, false); | |
1740 | ||
1741 | case OFPRAW_OFPST11_AGGREGATE_REQUEST: | |
1742 | return ofputil_decode_ofpst11_flow_request(fsr, &b, true); | |
1743 | ||
1744 | case OFPRAW_NXST_FLOW_REQUEST: | |
1745 | return ofputil_decode_nxst_flow_request(fsr, &b, false); | |
1746 | ||
1747 | case OFPRAW_NXST_AGGREGATE_REQUEST: | |
1748 | return ofputil_decode_nxst_flow_request(fsr, &b, true); | |
1749 | ||
1750 | default: | |
1751 | /* Hey, the caller lied. */ | |
1752 | NOT_REACHED(); | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW, | |
1757 | * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to | |
1758 | * 'protocol', and returns the message. */ | |
1759 | struct ofpbuf * | |
1760 | ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr, | |
1761 | enum ofputil_protocol protocol) | |
1762 | { | |
1763 | struct ofpbuf *msg; | |
1764 | enum ofpraw raw; | |
1765 | ||
1766 | switch (protocol) { | |
1767 | case OFPUTIL_P_OF12_OXM: { | |
1768 | struct ofp11_flow_stats_request *ofsr; | |
1769 | ||
1770 | raw = (fsr->aggregate | |
1771 | ? OFPRAW_OFPST11_AGGREGATE_REQUEST | |
1772 | : OFPRAW_OFPST11_FLOW_REQUEST); | |
1773 | msg = ofpraw_alloc(raw, OFP12_VERSION, NXM_TYPICAL_LEN); | |
1774 | ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr); | |
1775 | ofsr->table_id = fsr->table_id; | |
1776 | ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port); | |
1777 | ofsr->out_group = htonl(OFPG11_ANY); | |
1778 | ofsr->cookie = fsr->cookie; | |
1779 | ofsr->cookie_mask = fsr->cookie_mask; | |
1780 | oxm_put_match(msg, &fsr->match); | |
1781 | break; | |
1782 | } | |
1783 | ||
1784 | case OFPUTIL_P_OF10_STD: | |
1785 | case OFPUTIL_P_OF10_STD_TID: { | |
1786 | struct ofp10_flow_stats_request *ofsr; | |
1787 | ||
1788 | raw = (fsr->aggregate | |
1789 | ? OFPRAW_OFPST10_AGGREGATE_REQUEST | |
1790 | : OFPRAW_OFPST10_FLOW_REQUEST); | |
1791 | msg = ofpraw_alloc(raw, OFP10_VERSION, 0); | |
1792 | ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr); | |
1793 | ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match); | |
1794 | ofsr->table_id = fsr->table_id; | |
1795 | ofsr->out_port = htons(fsr->out_port); | |
1796 | break; | |
1797 | } | |
1798 | ||
1799 | case OFPUTIL_P_OF10_NXM: | |
1800 | case OFPUTIL_P_OF10_NXM_TID: { | |
1801 | struct nx_flow_stats_request *nfsr; | |
1802 | int match_len; | |
1803 | ||
1804 | raw = (fsr->aggregate | |
1805 | ? OFPRAW_NXST_AGGREGATE_REQUEST | |
1806 | : OFPRAW_NXST_FLOW_REQUEST); | |
1807 | msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN); | |
1808 | ofpbuf_put_zeros(msg, sizeof *nfsr); | |
1809 | match_len = nx_put_match(msg, &fsr->match, | |
1810 | fsr->cookie, fsr->cookie_mask); | |
1811 | ||
1812 | nfsr = msg->l3; | |
1813 | nfsr->out_port = htons(fsr->out_port); | |
1814 | nfsr->match_len = htons(match_len); | |
1815 | nfsr->table_id = fsr->table_id; | |
1816 | break; | |
1817 | } | |
1818 | ||
1819 | default: | |
1820 | NOT_REACHED(); | |
1821 | } | |
1822 | ||
1823 | return msg; | |
1824 | } | |
1825 | ||
1826 | /* Returns a bitmask with a 1-bit for each protocol that could be used to | |
1827 | * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate. | |
1828 | * | |
1829 | * (The return value will have at least one 1-bit.) */ | |
1830 | enum ofputil_protocol | |
1831 | ofputil_flow_stats_request_usable_protocols( | |
1832 | const struct ofputil_flow_stats_request *fsr) | |
1833 | { | |
1834 | enum ofputil_protocol usable_protocols; | |
1835 | ||
1836 | usable_protocols = ofputil_usable_protocols(&fsr->match); | |
1837 | if (fsr->cookie_mask != htonll(0)) { | |
1838 | usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM; | |
1839 | } | |
1840 | return usable_protocols; | |
1841 | } | |
1842 | ||
1843 | /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract | |
1844 | * ofputil_flow_stats in 'fs'. | |
1845 | * | |
1846 | * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single | |
1847 | * OpenFlow message. Calling this function multiple times for a single 'msg' | |
1848 | * iterates through the replies. The caller must initially leave 'msg''s layer | |
1849 | * pointers null and not modify them between calls. | |
1850 | * | |
1851 | * Most switches don't send the values needed to populate fs->idle_age and | |
1852 | * fs->hard_age, so those members will usually be set to 0. If the switch from | |
1853 | * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass | |
1854 | * 'flow_age_extension' as true so that the contents of 'msg' determine the | |
1855 | * 'idle_age' and 'hard_age' members in 'fs'. | |
1856 | * | |
1857 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats | |
1858 | * reply's actions. The caller must initialize 'ofpacts' and retains ownership | |
1859 | * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer. | |
1860 | * | |
1861 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
1862 | * otherwise a positive errno value. */ | |
1863 | int | |
1864 | ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs, | |
1865 | struct ofpbuf *msg, | |
1866 | bool flow_age_extension, | |
1867 | struct ofpbuf *ofpacts) | |
1868 | { | |
1869 | enum ofperr error; | |
1870 | enum ofpraw raw; | |
1871 | ||
1872 | error = (msg->l2 | |
1873 | ? ofpraw_decode(&raw, msg->l2) | |
1874 | : ofpraw_pull(&raw, msg)); | |
1875 | if (error) { | |
1876 | return error; | |
1877 | } | |
1878 | ||
1879 | if (!msg->size) { | |
1880 | return EOF; | |
1881 | } else if (raw == OFPRAW_OFPST11_FLOW_REPLY) { | |
1882 | const struct ofp11_flow_stats *ofs; | |
1883 | size_t length; | |
1884 | uint16_t padded_match_len; | |
1885 | ||
1886 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
1887 | if (!ofs) { | |
1888 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover " | |
1889 | "bytes at end", msg->size); | |
1890 | return EINVAL; | |
1891 | } | |
1892 | ||
1893 | length = ntohs(ofs->length); | |
1894 | if (length < sizeof *ofs) { | |
1895 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
1896 | "length %zu", length); | |
1897 | return EINVAL; | |
1898 | } | |
1899 | ||
1900 | if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) { | |
1901 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match"); | |
1902 | return EINVAL; | |
1903 | } | |
1904 | ||
1905 | if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs - | |
1906 | padded_match_len, ofpacts)) { | |
1907 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions"); | |
1908 | return EINVAL; | |
1909 | } | |
1910 | ||
1911 | fs->priority = ntohs(ofs->priority); | |
1912 | fs->table_id = ofs->table_id; | |
1913 | fs->duration_sec = ntohl(ofs->duration_sec); | |
1914 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
1915 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
1916 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
1917 | fs->idle_age = -1; | |
1918 | fs->hard_age = -1; | |
1919 | fs->cookie = ofs->cookie; | |
1920 | fs->packet_count = ntohll(ofs->packet_count); | |
1921 | fs->byte_count = ntohll(ofs->byte_count); | |
1922 | } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) { | |
1923 | const struct ofp10_flow_stats *ofs; | |
1924 | size_t length; | |
1925 | ||
1926 | ofs = ofpbuf_try_pull(msg, sizeof *ofs); | |
1927 | if (!ofs) { | |
1928 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover " | |
1929 | "bytes at end", msg->size); | |
1930 | return EINVAL; | |
1931 | } | |
1932 | ||
1933 | length = ntohs(ofs->length); | |
1934 | if (length < sizeof *ofs) { | |
1935 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid " | |
1936 | "length %zu", length); | |
1937 | return EINVAL; | |
1938 | } | |
1939 | ||
1940 | if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) { | |
1941 | return EINVAL; | |
1942 | } | |
1943 | ||
1944 | fs->cookie = get_32aligned_be64(&ofs->cookie); | |
1945 | ofputil_match_from_ofp10_match(&ofs->match, &fs->match); | |
1946 | fs->priority = ntohs(ofs->priority); | |
1947 | fs->table_id = ofs->table_id; | |
1948 | fs->duration_sec = ntohl(ofs->duration_sec); | |
1949 | fs->duration_nsec = ntohl(ofs->duration_nsec); | |
1950 | fs->idle_timeout = ntohs(ofs->idle_timeout); | |
1951 | fs->hard_timeout = ntohs(ofs->hard_timeout); | |
1952 | fs->idle_age = -1; | |
1953 | fs->hard_age = -1; | |
1954 | fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count)); | |
1955 | fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count)); | |
1956 | } else if (raw == OFPRAW_NXST_FLOW_REPLY) { | |
1957 | const struct nx_flow_stats *nfs; | |
1958 | size_t match_len, actions_len, length; | |
1959 | ||
1960 | nfs = ofpbuf_try_pull(msg, sizeof *nfs); | |
1961 | if (!nfs) { | |
1962 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover " | |
1963 | "bytes at end", msg->size); | |
1964 | return EINVAL; | |
1965 | } | |
1966 | ||
1967 | length = ntohs(nfs->length); | |
1968 | match_len = ntohs(nfs->match_len); | |
1969 | if (length < sizeof *nfs + ROUND_UP(match_len, 8)) { | |
1970 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu " | |
1971 | "claims invalid length %zu", match_len, length); | |
1972 | return EINVAL; | |
1973 | } | |
1974 | if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) { | |
1975 | return EINVAL; | |
1976 | } | |
1977 | ||
1978 | actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8); | |
1979 | if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) { | |
1980 | return EINVAL; | |
1981 | } | |
1982 | ||
1983 | fs->cookie = nfs->cookie; | |
1984 | fs->table_id = nfs->table_id; | |
1985 | fs->duration_sec = ntohl(nfs->duration_sec); | |
1986 | fs->duration_nsec = ntohl(nfs->duration_nsec); | |
1987 | fs->priority = ntohs(nfs->priority); | |
1988 | fs->idle_timeout = ntohs(nfs->idle_timeout); | |
1989 | fs->hard_timeout = ntohs(nfs->hard_timeout); | |
1990 | fs->idle_age = -1; | |
1991 | fs->hard_age = -1; | |
1992 | if (flow_age_extension) { | |
1993 | if (nfs->idle_age) { | |
1994 | fs->idle_age = ntohs(nfs->idle_age) - 1; | |
1995 | } | |
1996 | if (nfs->hard_age) { | |
1997 | fs->hard_age = ntohs(nfs->hard_age) - 1; | |
1998 | } | |
1999 | } | |
2000 | fs->packet_count = ntohll(nfs->packet_count); | |
2001 | fs->byte_count = ntohll(nfs->byte_count); | |
2002 | } else { | |
2003 | NOT_REACHED(); | |
2004 | } | |
2005 | ||
2006 | fs->ofpacts = ofpacts->data; | |
2007 | fs->ofpacts_len = ofpacts->size; | |
2008 | ||
2009 | return 0; | |
2010 | } | |
2011 | ||
2012 | /* Returns 'count' unchanged except that UINT64_MAX becomes 0. | |
2013 | * | |
2014 | * We use this in situations where OVS internally uses UINT64_MAX to mean | |
2015 | * "value unknown" but OpenFlow 1.0 does not define any unknown value. */ | |
2016 | static uint64_t | |
2017 | unknown_to_zero(uint64_t count) | |
2018 | { | |
2019 | return count != UINT64_MAX ? count : 0; | |
2020 | } | |
2021 | ||
2022 | /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to | |
2023 | * those already present in the list of ofpbufs in 'replies'. 'replies' should | |
2024 | * have been initialized with ofputil_start_stats_reply(). */ | |
2025 | void | |
2026 | ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs, | |
2027 | struct list *replies) | |
2028 | { | |
2029 | struct ofpbuf *reply = ofpbuf_from_list(list_back(replies)); | |
2030 | size_t start_ofs = reply->size; | |
2031 | enum ofpraw raw; | |
2032 | ||
2033 | ofpraw_decode_partial(&raw, reply->data, reply->size); | |
2034 | if (raw == OFPRAW_OFPST11_FLOW_REPLY) { | |
2035 | struct ofp11_flow_stats *ofs; | |
2036 | ||
2037 | ofpbuf_put_uninit(reply, sizeof *ofs); | |
2038 | oxm_put_match(reply, &fs->match); | |
2039 | ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len, | |
2040 | reply); | |
2041 | ||
2042 | ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs); | |
2043 | ofs->length = htons(reply->size - start_ofs); | |
2044 | ofs->table_id = fs->table_id; | |
2045 | ofs->pad = 0; | |
2046 | ofs->duration_sec = htonl(fs->duration_sec); | |
2047 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
2048 | ofs->priority = htons(fs->priority); | |
2049 | ofs->idle_timeout = htons(fs->idle_timeout); | |
2050 | ofs->hard_timeout = htons(fs->hard_timeout); | |
2051 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
2052 | ofs->cookie = fs->cookie; | |
2053 | ofs->packet_count = htonll(unknown_to_zero(fs->packet_count)); | |
2054 | ofs->byte_count = htonll(unknown_to_zero(fs->byte_count)); | |
2055 | } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) { | |
2056 | struct ofp10_flow_stats *ofs; | |
2057 | ||
2058 | ofpbuf_put_uninit(reply, sizeof *ofs); | |
2059 | ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply); | |
2060 | ||
2061 | ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs); | |
2062 | ofs->length = htons(reply->size - start_ofs); | |
2063 | ofs->table_id = fs->table_id; | |
2064 | ofs->pad = 0; | |
2065 | ofputil_match_to_ofp10_match(&fs->match, &ofs->match); | |
2066 | ofs->duration_sec = htonl(fs->duration_sec); | |
2067 | ofs->duration_nsec = htonl(fs->duration_nsec); | |
2068 | ofs->priority = htons(fs->priority); | |
2069 | ofs->idle_timeout = htons(fs->idle_timeout); | |
2070 | ofs->hard_timeout = htons(fs->hard_timeout); | |
2071 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
2072 | put_32aligned_be64(&ofs->cookie, fs->cookie); | |
2073 | put_32aligned_be64(&ofs->packet_count, | |
2074 | htonll(unknown_to_zero(fs->packet_count))); | |
2075 | put_32aligned_be64(&ofs->byte_count, | |
2076 | htonll(unknown_to_zero(fs->byte_count))); | |
2077 | } else if (raw == OFPRAW_NXST_FLOW_REPLY) { | |
2078 | struct nx_flow_stats *nfs; | |
2079 | int match_len; | |
2080 | ||
2081 | ofpbuf_put_uninit(reply, sizeof *nfs); | |
2082 | match_len = nx_put_match(reply, &fs->match, 0, 0); | |
2083 | ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply); | |
2084 | ||
2085 | nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs); | |
2086 | nfs->length = htons(reply->size - start_ofs); | |
2087 | nfs->table_id = fs->table_id; | |
2088 | nfs->pad = 0; | |
2089 | nfs->duration_sec = htonl(fs->duration_sec); | |
2090 | nfs->duration_nsec = htonl(fs->duration_nsec); | |
2091 | nfs->priority = htons(fs->priority); | |
2092 | nfs->idle_timeout = htons(fs->idle_timeout); | |
2093 | nfs->hard_timeout = htons(fs->hard_timeout); | |
2094 | nfs->idle_age = htons(fs->idle_age < 0 ? 0 | |
2095 | : fs->idle_age < UINT16_MAX ? fs->idle_age + 1 | |
2096 | : UINT16_MAX); | |
2097 | nfs->hard_age = htons(fs->hard_age < 0 ? 0 | |
2098 | : fs->hard_age < UINT16_MAX ? fs->hard_age + 1 | |
2099 | : UINT16_MAX); | |
2100 | nfs->match_len = htons(match_len); | |
2101 | nfs->cookie = fs->cookie; | |
2102 | nfs->packet_count = htonll(fs->packet_count); | |
2103 | nfs->byte_count = htonll(fs->byte_count); | |
2104 | } else { | |
2105 | NOT_REACHED(); | |
2106 | } | |
2107 | ||
2108 | ofpmp_postappend(replies, start_ofs); | |
2109 | } | |
2110 | ||
2111 | /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or | |
2112 | * NXST_AGGREGATE reply matching 'request', and returns the message. */ | |
2113 | struct ofpbuf * | |
2114 | ofputil_encode_aggregate_stats_reply( | |
2115 | const struct ofputil_aggregate_stats *stats, | |
2116 | const struct ofp_header *request) | |
2117 | { | |
2118 | struct ofp_aggregate_stats_reply *asr; | |
2119 | uint64_t packet_count; | |
2120 | uint64_t byte_count; | |
2121 | struct ofpbuf *msg; | |
2122 | enum ofpraw raw; | |
2123 | ||
2124 | ofpraw_decode(&raw, request); | |
2125 | if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) { | |
2126 | packet_count = unknown_to_zero(stats->packet_count); | |
2127 | byte_count = unknown_to_zero(stats->byte_count); | |
2128 | } else { | |
2129 | packet_count = stats->packet_count; | |
2130 | byte_count = stats->byte_count; | |
2131 | } | |
2132 | ||
2133 | msg = ofpraw_alloc_stats_reply(request, 0); | |
2134 | asr = ofpbuf_put_zeros(msg, sizeof *asr); | |
2135 | put_32aligned_be64(&asr->packet_count, htonll(packet_count)); | |
2136 | put_32aligned_be64(&asr->byte_count, htonll(byte_count)); | |
2137 | asr->flow_count = htonl(stats->flow_count); | |
2138 | ||
2139 | return msg; | |
2140 | } | |
2141 | ||
2142 | enum ofperr | |
2143 | ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats, | |
2144 | const struct ofp_header *reply) | |
2145 | { | |
2146 | struct ofp_aggregate_stats_reply *asr; | |
2147 | struct ofpbuf msg; | |
2148 | ||
2149 | ofpbuf_use_const(&msg, reply, ntohs(reply->length)); | |
2150 | ofpraw_pull_assert(&msg); | |
2151 | ||
2152 | asr = msg.l3; | |
2153 | stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count)); | |
2154 | stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count)); | |
2155 | stats->flow_count = ntohl(asr->flow_count); | |
2156 | ||
2157 | return 0; | |
2158 | } | |
2159 | ||
2160 | /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an | |
2161 | * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise | |
2162 | * an OpenFlow error code. */ | |
2163 | enum ofperr | |
2164 | ofputil_decode_flow_removed(struct ofputil_flow_removed *fr, | |
2165 | const struct ofp_header *oh) | |
2166 | { | |
2167 | enum ofpraw raw; | |
2168 | struct ofpbuf b; | |
2169 | ||
2170 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2171 | raw = ofpraw_pull_assert(&b); | |
2172 | if (raw == OFPRAW_OFPT11_FLOW_REMOVED) { | |
2173 | const struct ofp12_flow_removed *ofr; | |
2174 | enum ofperr error; | |
2175 | ||
2176 | ofr = ofpbuf_pull(&b, sizeof *ofr); | |
2177 | ||
2178 | error = ofputil_pull_ofp11_match(&b, &fr->match, NULL); | |
2179 | if (error) { | |
2180 | return error; | |
2181 | } | |
2182 | ||
2183 | fr->priority = ntohs(ofr->priority); | |
2184 | fr->cookie = ofr->cookie; | |
2185 | fr->reason = ofr->reason; | |
2186 | fr->table_id = ofr->table_id; | |
2187 | fr->duration_sec = ntohl(ofr->duration_sec); | |
2188 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
2189 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
2190 | fr->hard_timeout = ntohs(ofr->hard_timeout); | |
2191 | fr->packet_count = ntohll(ofr->packet_count); | |
2192 | fr->byte_count = ntohll(ofr->byte_count); | |
2193 | } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) { | |
2194 | const struct ofp_flow_removed *ofr; | |
2195 | ||
2196 | ofr = ofpbuf_pull(&b, sizeof *ofr); | |
2197 | ||
2198 | ofputil_match_from_ofp10_match(&ofr->match, &fr->match); | |
2199 | fr->priority = ntohs(ofr->priority); | |
2200 | fr->cookie = ofr->cookie; | |
2201 | fr->reason = ofr->reason; | |
2202 | fr->table_id = 255; | |
2203 | fr->duration_sec = ntohl(ofr->duration_sec); | |
2204 | fr->duration_nsec = ntohl(ofr->duration_nsec); | |
2205 | fr->idle_timeout = ntohs(ofr->idle_timeout); | |
2206 | fr->hard_timeout = 0; | |
2207 | fr->packet_count = ntohll(ofr->packet_count); | |
2208 | fr->byte_count = ntohll(ofr->byte_count); | |
2209 | } else if (raw == OFPRAW_NXT_FLOW_REMOVED) { | |
2210 | struct nx_flow_removed *nfr; | |
2211 | enum ofperr error; | |
2212 | ||
2213 | nfr = ofpbuf_pull(&b, sizeof *nfr); | |
2214 | error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match, | |
2215 | NULL, NULL); | |
2216 | if (error) { | |
2217 | return error; | |
2218 | } | |
2219 | if (b.size) { | |
2220 | return OFPERR_OFPBRC_BAD_LEN; | |
2221 | } | |
2222 | ||
2223 | fr->priority = ntohs(nfr->priority); | |
2224 | fr->cookie = nfr->cookie; | |
2225 | fr->reason = nfr->reason; | |
2226 | fr->table_id = 255; | |
2227 | fr->duration_sec = ntohl(nfr->duration_sec); | |
2228 | fr->duration_nsec = ntohl(nfr->duration_nsec); | |
2229 | fr->idle_timeout = ntohs(nfr->idle_timeout); | |
2230 | fr->hard_timeout = 0; | |
2231 | fr->packet_count = ntohll(nfr->packet_count); | |
2232 | fr->byte_count = ntohll(nfr->byte_count); | |
2233 | } else { | |
2234 | NOT_REACHED(); | |
2235 | } | |
2236 | ||
2237 | return 0; | |
2238 | } | |
2239 | ||
2240 | /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or | |
2241 | * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the | |
2242 | * message. */ | |
2243 | struct ofpbuf * | |
2244 | ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr, | |
2245 | enum ofputil_protocol protocol) | |
2246 | { | |
2247 | struct ofpbuf *msg; | |
2248 | ||
2249 | switch (protocol) { | |
2250 | case OFPUTIL_P_OF12_OXM: { | |
2251 | struct ofp12_flow_removed *ofr; | |
2252 | ||
2253 | msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED, | |
2254 | ofputil_protocol_to_ofp_version(protocol), | |
2255 | htonl(0), NXM_TYPICAL_LEN); | |
2256 | ofr = ofpbuf_put_zeros(msg, sizeof *ofr); | |
2257 | ofr->cookie = fr->cookie; | |
2258 | ofr->priority = htons(fr->priority); | |
2259 | ofr->reason = fr->reason; | |
2260 | ofr->table_id = fr->table_id; | |
2261 | ofr->duration_sec = htonl(fr->duration_sec); | |
2262 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
2263 | ofr->idle_timeout = htons(fr->idle_timeout); | |
2264 | ofr->hard_timeout = htons(fr->hard_timeout); | |
2265 | ofr->packet_count = htonll(fr->packet_count); | |
2266 | ofr->byte_count = htonll(fr->byte_count); | |
2267 | oxm_put_match(msg, &fr->match); | |
2268 | break; | |
2269 | } | |
2270 | ||
2271 | case OFPUTIL_P_OF10_STD: | |
2272 | case OFPUTIL_P_OF10_STD_TID: { | |
2273 | struct ofp_flow_removed *ofr; | |
2274 | ||
2275 | msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION, | |
2276 | htonl(0), 0); | |
2277 | ofr = ofpbuf_put_zeros(msg, sizeof *ofr); | |
2278 | ofputil_match_to_ofp10_match(&fr->match, &ofr->match); | |
2279 | ofr->cookie = fr->cookie; | |
2280 | ofr->priority = htons(fr->priority); | |
2281 | ofr->reason = fr->reason; | |
2282 | ofr->duration_sec = htonl(fr->duration_sec); | |
2283 | ofr->duration_nsec = htonl(fr->duration_nsec); | |
2284 | ofr->idle_timeout = htons(fr->idle_timeout); | |
2285 | ofr->packet_count = htonll(unknown_to_zero(fr->packet_count)); | |
2286 | ofr->byte_count = htonll(unknown_to_zero(fr->byte_count)); | |
2287 | break; | |
2288 | } | |
2289 | ||
2290 | case OFPUTIL_P_OF10_NXM: | |
2291 | case OFPUTIL_P_OF10_NXM_TID: { | |
2292 | struct nx_flow_removed *nfr; | |
2293 | int match_len; | |
2294 | ||
2295 | msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION, | |
2296 | htonl(0), NXM_TYPICAL_LEN); | |
2297 | nfr = ofpbuf_put_zeros(msg, sizeof *nfr); | |
2298 | match_len = nx_put_match(msg, &fr->match, 0, 0); | |
2299 | ||
2300 | nfr = msg->l3; | |
2301 | nfr->cookie = fr->cookie; | |
2302 | nfr->priority = htons(fr->priority); | |
2303 | nfr->reason = fr->reason; | |
2304 | nfr->duration_sec = htonl(fr->duration_sec); | |
2305 | nfr->duration_nsec = htonl(fr->duration_nsec); | |
2306 | nfr->idle_timeout = htons(fr->idle_timeout); | |
2307 | nfr->match_len = htons(match_len); | |
2308 | nfr->packet_count = htonll(fr->packet_count); | |
2309 | nfr->byte_count = htonll(fr->byte_count); | |
2310 | break; | |
2311 | } | |
2312 | ||
2313 | default: | |
2314 | NOT_REACHED(); | |
2315 | } | |
2316 | ||
2317 | return msg; | |
2318 | } | |
2319 | ||
2320 | static void | |
2321 | ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin, | |
2322 | struct match *match, struct ofpbuf *b) | |
2323 | { | |
2324 | pin->packet = b->data; | |
2325 | pin->packet_len = b->size; | |
2326 | ||
2327 | pin->fmd.in_port = match->flow.in_port; | |
2328 | pin->fmd.tun_id = match->flow.tunnel.tun_id; | |
2329 | pin->fmd.metadata = match->flow.metadata; | |
2330 | memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs); | |
2331 | } | |
2332 | ||
2333 | enum ofperr | |
2334 | ofputil_decode_packet_in(struct ofputil_packet_in *pin, | |
2335 | const struct ofp_header *oh) | |
2336 | { | |
2337 | enum ofpraw raw; | |
2338 | struct ofpbuf b; | |
2339 | ||
2340 | memset(pin, 0, sizeof *pin); | |
2341 | ||
2342 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2343 | raw = ofpraw_pull_assert(&b); | |
2344 | if (raw == OFPRAW_OFPT12_PACKET_IN) { | |
2345 | const struct ofp12_packet_in *opi; | |
2346 | struct match match; | |
2347 | int error; | |
2348 | ||
2349 | opi = ofpbuf_pull(&b, sizeof *opi); | |
2350 | error = oxm_pull_match_loose(&b, &match); | |
2351 | if (error) { | |
2352 | return error; | |
2353 | } | |
2354 | ||
2355 | if (!ofpbuf_try_pull(&b, 2)) { | |
2356 | return OFPERR_OFPBRC_BAD_LEN; | |
2357 | } | |
2358 | ||
2359 | pin->reason = opi->reason; | |
2360 | pin->table_id = opi->table_id; | |
2361 | ||
2362 | pin->buffer_id = ntohl(opi->buffer_id); | |
2363 | pin->total_len = ntohs(opi->total_len); | |
2364 | ||
2365 | ofputil_decode_packet_in_finish(pin, &match, &b); | |
2366 | } else if (raw == OFPRAW_OFPT10_PACKET_IN) { | |
2367 | const struct ofp_packet_in *opi; | |
2368 | ||
2369 | opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data)); | |
2370 | ||
2371 | pin->packet = opi->data; | |
2372 | pin->packet_len = b.size; | |
2373 | ||
2374 | pin->fmd.in_port = ntohs(opi->in_port); | |
2375 | pin->reason = opi->reason; | |
2376 | pin->buffer_id = ntohl(opi->buffer_id); | |
2377 | pin->total_len = ntohs(opi->total_len); | |
2378 | } else if (raw == OFPRAW_NXT_PACKET_IN) { | |
2379 | const struct nx_packet_in *npi; | |
2380 | struct match match; | |
2381 | int error; | |
2382 | ||
2383 | npi = ofpbuf_pull(&b, sizeof *npi); | |
2384 | error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL, | |
2385 | NULL); | |
2386 | if (error) { | |
2387 | return error; | |
2388 | } | |
2389 | ||
2390 | if (!ofpbuf_try_pull(&b, 2)) { | |
2391 | return OFPERR_OFPBRC_BAD_LEN; | |
2392 | } | |
2393 | ||
2394 | pin->reason = npi->reason; | |
2395 | pin->table_id = npi->table_id; | |
2396 | pin->cookie = npi->cookie; | |
2397 | ||
2398 | pin->buffer_id = ntohl(npi->buffer_id); | |
2399 | pin->total_len = ntohs(npi->total_len); | |
2400 | ||
2401 | ofputil_decode_packet_in_finish(pin, &match, &b); | |
2402 | } else { | |
2403 | NOT_REACHED(); | |
2404 | } | |
2405 | ||
2406 | return 0; | |
2407 | } | |
2408 | ||
2409 | static void | |
2410 | ofputil_packet_in_to_match(const struct ofputil_packet_in *pin, | |
2411 | struct match *match) | |
2412 | { | |
2413 | int i; | |
2414 | ||
2415 | match_init_catchall(match); | |
2416 | if (pin->fmd.tun_id != htonll(0)) { | |
2417 | match_set_tun_id(match, pin->fmd.tun_id); | |
2418 | } | |
2419 | if (pin->fmd.metadata != htonll(0)) { | |
2420 | match_set_metadata(match, pin->fmd.metadata); | |
2421 | } | |
2422 | ||
2423 | for (i = 0; i < FLOW_N_REGS; i++) { | |
2424 | if (pin->fmd.regs[i]) { | |
2425 | match_set_reg(match, i, pin->fmd.regs[i]); | |
2426 | } | |
2427 | } | |
2428 | ||
2429 | match_set_in_port(match, pin->fmd.in_port); | |
2430 | } | |
2431 | ||
2432 | /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message | |
2433 | * in the format specified by 'packet_in_format'. */ | |
2434 | struct ofpbuf * | |
2435 | ofputil_encode_packet_in(const struct ofputil_packet_in *pin, | |
2436 | enum ofputil_protocol protocol, | |
2437 | enum nx_packet_in_format packet_in_format) | |
2438 | { | |
2439 | size_t send_len = MIN(pin->send_len, pin->packet_len); | |
2440 | struct ofpbuf *packet; | |
2441 | ||
2442 | /* Add OFPT_PACKET_IN. */ | |
2443 | if (protocol == OFPUTIL_P_OF12_OXM) { | |
2444 | struct ofp12_packet_in *opi; | |
2445 | struct match match; | |
2446 | ||
2447 | ofputil_packet_in_to_match(pin, &match); | |
2448 | ||
2449 | /* The final argument is just an estimate of the space required. */ | |
2450 | packet = ofpraw_alloc_xid(OFPRAW_OFPT12_PACKET_IN, OFP12_VERSION, | |
2451 | htonl(0), (sizeof(struct flow_metadata) * 2 | |
2452 | + 2 + send_len)); | |
2453 | ofpbuf_put_zeros(packet, sizeof *opi); | |
2454 | oxm_put_match(packet, &match); | |
2455 | ofpbuf_put_zeros(packet, 2); | |
2456 | ofpbuf_put(packet, pin->packet, send_len); | |
2457 | ||
2458 | opi = packet->l3; | |
2459 | opi->buffer_id = htonl(pin->buffer_id); | |
2460 | opi->total_len = htons(pin->total_len); | |
2461 | opi->reason = pin->reason; | |
2462 | opi->table_id = pin->table_id; | |
2463 | } else if (packet_in_format == NXPIF_OPENFLOW10) { | |
2464 | struct ofp_packet_in *opi; | |
2465 | ||
2466 | packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION, | |
2467 | htonl(0), send_len); | |
2468 | opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data)); | |
2469 | opi->total_len = htons(pin->total_len); | |
2470 | opi->in_port = htons(pin->fmd.in_port); | |
2471 | opi->reason = pin->reason; | |
2472 | opi->buffer_id = htonl(pin->buffer_id); | |
2473 | ||
2474 | ofpbuf_put(packet, pin->packet, send_len); | |
2475 | } else if (packet_in_format == NXPIF_NXM) { | |
2476 | struct nx_packet_in *npi; | |
2477 | struct match match; | |
2478 | size_t match_len; | |
2479 | ||
2480 | ofputil_packet_in_to_match(pin, &match); | |
2481 | ||
2482 | /* The final argument is just an estimate of the space required. */ | |
2483 | packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION, | |
2484 | htonl(0), (sizeof(struct flow_metadata) * 2 | |
2485 | + 2 + send_len)); | |
2486 | ofpbuf_put_zeros(packet, sizeof *npi); | |
2487 | match_len = nx_put_match(packet, &match, 0, 0); | |
2488 | ofpbuf_put_zeros(packet, 2); | |
2489 | ofpbuf_put(packet, pin->packet, send_len); | |
2490 | ||
2491 | npi = packet->l3; | |
2492 | npi->buffer_id = htonl(pin->buffer_id); | |
2493 | npi->total_len = htons(pin->total_len); | |
2494 | npi->reason = pin->reason; | |
2495 | npi->table_id = pin->table_id; | |
2496 | npi->cookie = pin->cookie; | |
2497 | npi->match_len = htons(match_len); | |
2498 | } else { | |
2499 | NOT_REACHED(); | |
2500 | } | |
2501 | ofpmsg_update_length(packet); | |
2502 | ||
2503 | return packet; | |
2504 | } | |
2505 | ||
2506 | const char * | |
2507 | ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason) | |
2508 | { | |
2509 | static char s[INT_STRLEN(int) + 1]; | |
2510 | ||
2511 | switch (reason) { | |
2512 | case OFPR_NO_MATCH: | |
2513 | return "no_match"; | |
2514 | case OFPR_ACTION: | |
2515 | return "action"; | |
2516 | case OFPR_INVALID_TTL: | |
2517 | return "invalid_ttl"; | |
2518 | ||
2519 | case OFPR_N_REASONS: | |
2520 | default: | |
2521 | sprintf(s, "%d", (int) reason); | |
2522 | return s; | |
2523 | } | |
2524 | } | |
2525 | ||
2526 | bool | |
2527 | ofputil_packet_in_reason_from_string(const char *s, | |
2528 | enum ofp_packet_in_reason *reason) | |
2529 | { | |
2530 | int i; | |
2531 | ||
2532 | for (i = 0; i < OFPR_N_REASONS; i++) { | |
2533 | if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) { | |
2534 | *reason = i; | |
2535 | return true; | |
2536 | } | |
2537 | } | |
2538 | return false; | |
2539 | } | |
2540 | ||
2541 | /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in | |
2542 | * 'po'. | |
2543 | * | |
2544 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out | |
2545 | * message's actions. The caller must initialize 'ofpacts' and retains | |
2546 | * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer. | |
2547 | * | |
2548 | * Returns 0 if successful, otherwise an OFPERR_* value. */ | |
2549 | enum ofperr | |
2550 | ofputil_decode_packet_out(struct ofputil_packet_out *po, | |
2551 | const struct ofp_header *oh, | |
2552 | struct ofpbuf *ofpacts) | |
2553 | { | |
2554 | enum ofpraw raw; | |
2555 | struct ofpbuf b; | |
2556 | ||
2557 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
2558 | raw = ofpraw_pull_assert(&b); | |
2559 | ||
2560 | if (raw == OFPRAW_OFPT11_PACKET_OUT) { | |
2561 | enum ofperr error; | |
2562 | const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo); | |
2563 | ||
2564 | po->buffer_id = ntohl(opo->buffer_id); | |
2565 | error = ofputil_port_from_ofp11(opo->in_port, &po->in_port); | |
2566 | if (error) { | |
2567 | return error; | |
2568 | } | |
2569 | ||
2570 | error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len), | |
2571 | ofpacts); | |
2572 | if (error) { | |
2573 | return error; | |
2574 | } | |
2575 | } else if (raw == OFPRAW_OFPT10_PACKET_OUT) { | |
2576 | enum ofperr error; | |
2577 | const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo); | |
2578 | ||
2579 | po->buffer_id = ntohl(opo->buffer_id); | |
2580 | po->in_port = ntohs(opo->in_port); | |
2581 | ||
2582 | error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts); | |
2583 | if (error) { | |
2584 | return error; | |
2585 | } | |
2586 | } else { | |
2587 | NOT_REACHED(); | |
2588 | } | |
2589 | ||
2590 | if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL | |
2591 | && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) { | |
2592 | VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16, | |
2593 | po->in_port); | |
2594 | return OFPERR_OFPBRC_BAD_PORT; | |
2595 | } | |
2596 | ||
2597 | po->ofpacts = ofpacts->data; | |
2598 | po->ofpacts_len = ofpacts->size; | |
2599 | ||
2600 | if (po->buffer_id == UINT32_MAX) { | |
2601 | po->packet = b.data; | |
2602 | po->packet_len = b.size; | |
2603 | } else { | |
2604 | po->packet = NULL; | |
2605 | po->packet_len = 0; | |
2606 | } | |
2607 | ||
2608 | return 0; | |
2609 | } | |
2610 | \f | |
2611 | /* ofputil_phy_port */ | |
2612 | ||
2613 | /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */ | |
2614 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ | |
2615 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
2616 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
2617 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
2618 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
2619 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
2620 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
2621 | ||
2622 | /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */ | |
2623 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4)); | |
2624 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4)); | |
2625 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4)); | |
2626 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4)); | |
2627 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4)); | |
2628 | ||
2629 | static enum netdev_features | |
2630 | netdev_port_features_from_ofp10(ovs_be32 ofp10_) | |
2631 | { | |
2632 | uint32_t ofp10 = ntohl(ofp10_); | |
2633 | return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4); | |
2634 | } | |
2635 | ||
2636 | static ovs_be32 | |
2637 | netdev_port_features_to_ofp10(enum netdev_features features) | |
2638 | { | |
2639 | return htonl((features & 0x7f) | ((features & 0xf800) >> 4)); | |
2640 | } | |
2641 | ||
2642 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */ | |
2643 | BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */ | |
2644 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */ | |
2645 | BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */ | |
2646 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */ | |
2647 | BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */ | |
2648 | BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */ | |
2649 | BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */ | |
2650 | BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */ | |
2651 | BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */ | |
2652 | BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */ | |
2653 | BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */ | |
2654 | BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */ | |
2655 | BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */ | |
2656 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */ | |
2657 | BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */ | |
2658 | ||
2659 | static enum netdev_features | |
2660 | netdev_port_features_from_ofp11(ovs_be32 ofp11) | |
2661 | { | |
2662 | return ntohl(ofp11) & 0xffff; | |
2663 | } | |
2664 | ||
2665 | static ovs_be32 | |
2666 | netdev_port_features_to_ofp11(enum netdev_features features) | |
2667 | { | |
2668 | return htonl(features & 0xffff); | |
2669 | } | |
2670 | ||
2671 | static enum ofperr | |
2672 | ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp, | |
2673 | const struct ofp10_phy_port *opp) | |
2674 | { | |
2675 | memset(pp, 0, sizeof *pp); | |
2676 | ||
2677 | pp->port_no = ntohs(opp->port_no); | |
2678 | memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN); | |
2679 | ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN); | |
2680 | ||
2681 | pp->config = ntohl(opp->config) & OFPPC10_ALL; | |
2682 | pp->state = ntohl(opp->state) & OFPPS10_ALL; | |
2683 | ||
2684 | pp->curr = netdev_port_features_from_ofp10(opp->curr); | |
2685 | pp->advertised = netdev_port_features_from_ofp10(opp->advertised); | |
2686 | pp->supported = netdev_port_features_from_ofp10(opp->supported); | |
2687 | pp->peer = netdev_port_features_from_ofp10(opp->peer); | |
2688 | ||
2689 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000; | |
2690 | pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000; | |
2691 | ||
2692 | return 0; | |
2693 | } | |
2694 | ||
2695 | static enum ofperr | |
2696 | ofputil_decode_ofp11_port(struct ofputil_phy_port *pp, | |
2697 | const struct ofp11_port *op) | |
2698 | { | |
2699 | enum ofperr error; | |
2700 | ||
2701 | memset(pp, 0, sizeof *pp); | |
2702 | ||
2703 | error = ofputil_port_from_ofp11(op->port_no, &pp->port_no); | |
2704 | if (error) { | |
2705 | return error; | |
2706 | } | |
2707 | memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN); | |
2708 | ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN); | |
2709 | ||
2710 | pp->config = ntohl(op->config) & OFPPC11_ALL; | |
2711 | pp->state = ntohl(op->state) & OFPPC11_ALL; | |
2712 | ||
2713 | pp->curr = netdev_port_features_from_ofp11(op->curr); | |
2714 | pp->advertised = netdev_port_features_from_ofp11(op->advertised); | |
2715 | pp->supported = netdev_port_features_from_ofp11(op->supported); | |
2716 | pp->peer = netdev_port_features_from_ofp11(op->peer); | |
2717 | ||
2718 | pp->curr_speed = ntohl(op->curr_speed); | |
2719 | pp->max_speed = ntohl(op->max_speed); | |
2720 | ||
2721 | return 0; | |
2722 | } | |
2723 | ||
2724 | static size_t | |
2725 | ofputil_get_phy_port_size(enum ofp_version ofp_version) | |
2726 | { | |
2727 | switch (ofp_version) { | |
2728 | case OFP10_VERSION: | |
2729 | return sizeof(struct ofp10_phy_port); | |
2730 | case OFP11_VERSION: | |
2731 | case OFP12_VERSION: | |
2732 | return sizeof(struct ofp11_port); | |
2733 | default: | |
2734 | NOT_REACHED(); | |
2735 | } | |
2736 | } | |
2737 | ||
2738 | static void | |
2739 | ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp, | |
2740 | struct ofp10_phy_port *opp) | |
2741 | { | |
2742 | memset(opp, 0, sizeof *opp); | |
2743 | ||
2744 | opp->port_no = htons(pp->port_no); | |
2745 | memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
2746 | ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
2747 | ||
2748 | opp->config = htonl(pp->config & OFPPC10_ALL); | |
2749 | opp->state = htonl(pp->state & OFPPS10_ALL); | |
2750 | ||
2751 | opp->curr = netdev_port_features_to_ofp10(pp->curr); | |
2752 | opp->advertised = netdev_port_features_to_ofp10(pp->advertised); | |
2753 | opp->supported = netdev_port_features_to_ofp10(pp->supported); | |
2754 | opp->peer = netdev_port_features_to_ofp10(pp->peer); | |
2755 | } | |
2756 | ||
2757 | static void | |
2758 | ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp, | |
2759 | struct ofp11_port *op) | |
2760 | { | |
2761 | memset(op, 0, sizeof *op); | |
2762 | ||
2763 | op->port_no = ofputil_port_to_ofp11(pp->port_no); | |
2764 | memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN); | |
2765 | ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN); | |
2766 | ||
2767 | op->config = htonl(pp->config & OFPPC11_ALL); | |
2768 | op->state = htonl(pp->state & OFPPS11_ALL); | |
2769 | ||
2770 | op->curr = netdev_port_features_to_ofp11(pp->curr); | |
2771 | op->advertised = netdev_port_features_to_ofp11(pp->advertised); | |
2772 | op->supported = netdev_port_features_to_ofp11(pp->supported); | |
2773 | op->peer = netdev_port_features_to_ofp11(pp->peer); | |
2774 | ||
2775 | op->curr_speed = htonl(pp->curr_speed); | |
2776 | op->max_speed = htonl(pp->max_speed); | |
2777 | } | |
2778 | ||
2779 | static void | |
2780 | ofputil_put_phy_port(enum ofp_version ofp_version, | |
2781 | const struct ofputil_phy_port *pp, struct ofpbuf *b) | |
2782 | { | |
2783 | switch (ofp_version) { | |
2784 | case OFP10_VERSION: { | |
2785 | struct ofp10_phy_port *opp; | |
2786 | if (b->size + sizeof *opp <= UINT16_MAX) { | |
2787 | opp = ofpbuf_put_uninit(b, sizeof *opp); | |
2788 | ofputil_encode_ofp10_phy_port(pp, opp); | |
2789 | } | |
2790 | break; | |
2791 | } | |
2792 | ||
2793 | case OFP11_VERSION: | |
2794 | case OFP12_VERSION: { | |
2795 | struct ofp11_port *op; | |
2796 | if (b->size + sizeof *op <= UINT16_MAX) { | |
2797 | op = ofpbuf_put_uninit(b, sizeof *op); | |
2798 | ofputil_encode_ofp11_port(pp, op); | |
2799 | } | |
2800 | break; | |
2801 | } | |
2802 | ||
2803 | default: | |
2804 | NOT_REACHED(); | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | void | |
2809 | ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version, | |
2810 | const struct ofputil_phy_port *pp, | |
2811 | struct list *replies) | |
2812 | { | |
2813 | switch (ofp_version) { | |
2814 | case OFP10_VERSION: { | |
2815 | struct ofp10_phy_port *opp; | |
2816 | ||
2817 | opp = ofpmp_append(replies, sizeof *opp); | |
2818 | ofputil_encode_ofp10_phy_port(pp, opp); | |
2819 | break; | |
2820 | } | |
2821 | ||
2822 | case OFP11_VERSION: | |
2823 | case OFP12_VERSION: { | |
2824 | struct ofp11_port *op; | |
2825 | ||
2826 | op = ofpmp_append(replies, sizeof *op); | |
2827 | ofputil_encode_ofp11_port(pp, op); | |
2828 | break; | |
2829 | } | |
2830 | ||
2831 | default: | |
2832 | NOT_REACHED(); | |
2833 | } | |
2834 | } | |
2835 | \f | |
2836 | /* ofputil_switch_features */ | |
2837 | ||
2838 | #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \ | |
2839 | OFPC_IP_REASM | OFPC_QUEUE_STATS) | |
2840 | BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS); | |
2841 | BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS); | |
2842 | BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS); | |
2843 | BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM); | |
2844 | BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS); | |
2845 | BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP); | |
2846 | ||
2847 | struct ofputil_action_bit_translation { | |
2848 | enum ofputil_action_bitmap ofputil_bit; | |
2849 | int of_bit; | |
2850 | }; | |
2851 | ||
2852 | static const struct ofputil_action_bit_translation of10_action_bits[] = { | |
2853 | { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT }, | |
2854 | { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID }, | |
2855 | { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP }, | |
2856 | { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN }, | |
2857 | { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC }, | |
2858 | { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST }, | |
2859 | { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC }, | |
2860 | { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST }, | |
2861 | { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS }, | |
2862 | { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC }, | |
2863 | { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST }, | |
2864 | { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE }, | |
2865 | { 0, 0 }, | |
2866 | }; | |
2867 | ||
2868 | static enum ofputil_action_bitmap | |
2869 | decode_action_bits(ovs_be32 of_actions, | |
2870 | const struct ofputil_action_bit_translation *x) | |
2871 | { | |
2872 | enum ofputil_action_bitmap ofputil_actions; | |
2873 | ||
2874 | ofputil_actions = 0; | |
2875 | for (; x->ofputil_bit; x++) { | |
2876 | if (of_actions & htonl(1u << x->of_bit)) { | |
2877 | ofputil_actions |= x->ofputil_bit; | |
2878 | } | |
2879 | } | |
2880 | return ofputil_actions; | |
2881 | } | |
2882 | ||
2883 | static uint32_t | |
2884 | ofputil_capabilities_mask(enum ofp_version ofp_version) | |
2885 | { | |
2886 | /* Handle capabilities whose bit is unique for all Open Flow versions */ | |
2887 | switch (ofp_version) { | |
2888 | case OFP10_VERSION: | |
2889 | case OFP11_VERSION: | |
2890 | return OFPC_COMMON | OFPC_ARP_MATCH_IP; | |
2891 | case OFP12_VERSION: | |
2892 | return OFPC_COMMON | OFPC12_PORT_BLOCKED; | |
2893 | default: | |
2894 | /* Caller needs to check osf->header.version itself */ | |
2895 | return 0; | |
2896 | } | |
2897 | } | |
2898 | ||
2899 | /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an | |
2900 | * abstract representation in '*features'. Initializes '*b' to iterate over | |
2901 | * the OpenFlow port structures following 'osf' with later calls to | |
2902 | * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an | |
2903 | * OFPERR_* value. */ | |
2904 | enum ofperr | |
2905 | ofputil_decode_switch_features(const struct ofp_header *oh, | |
2906 | struct ofputil_switch_features *features, | |
2907 | struct ofpbuf *b) | |
2908 | { | |
2909 | const struct ofp_switch_features *osf; | |
2910 | enum ofpraw raw; | |
2911 | ||
2912 | ofpbuf_use_const(b, oh, ntohs(oh->length)); | |
2913 | raw = ofpraw_pull_assert(b); | |
2914 | ||
2915 | osf = ofpbuf_pull(b, sizeof *osf); | |
2916 | features->datapath_id = ntohll(osf->datapath_id); | |
2917 | features->n_buffers = ntohl(osf->n_buffers); | |
2918 | features->n_tables = osf->n_tables; | |
2919 | ||
2920 | features->capabilities = ntohl(osf->capabilities) & | |
2921 | ofputil_capabilities_mask(oh->version); | |
2922 | ||
2923 | if (b->size % ofputil_get_phy_port_size(oh->version)) { | |
2924 | return OFPERR_OFPBRC_BAD_LEN; | |
2925 | } | |
2926 | ||
2927 | if (raw == OFPRAW_OFPT10_FEATURES_REPLY) { | |
2928 | if (osf->capabilities & htonl(OFPC10_STP)) { | |
2929 | features->capabilities |= OFPUTIL_C_STP; | |
2930 | } | |
2931 | features->actions = decode_action_bits(osf->actions, of10_action_bits); | |
2932 | } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) { | |
2933 | if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) { | |
2934 | features->capabilities |= OFPUTIL_C_GROUP_STATS; | |
2935 | } | |
2936 | features->actions = 0; | |
2937 | } else { | |
2938 | return OFPERR_OFPBRC_BAD_VERSION; | |
2939 | } | |
2940 | ||
2941 | return 0; | |
2942 | } | |
2943 | ||
2944 | /* Returns true if the maximum number of ports are in 'oh'. */ | |
2945 | static bool | |
2946 | max_ports_in_features(const struct ofp_header *oh) | |
2947 | { | |
2948 | size_t pp_size = ofputil_get_phy_port_size(oh->version); | |
2949 | return ntohs(oh->length) + pp_size > UINT16_MAX; | |
2950 | } | |
2951 | ||
2952 | /* Given a buffer 'b' that contains a Features Reply message, checks if | |
2953 | * it contains the maximum number of ports that will fit. If so, it | |
2954 | * returns true and removes the ports from the message. The caller | |
2955 | * should then send an OFPST_PORT_DESC stats request to get the ports, | |
2956 | * since the switch may have more ports than could be represented in the | |
2957 | * Features Reply. Otherwise, returns false. | |
2958 | */ | |
2959 | bool | |
2960 | ofputil_switch_features_ports_trunc(struct ofpbuf *b) | |
2961 | { | |
2962 | struct ofp_header *oh = b->data; | |
2963 | ||
2964 | if (max_ports_in_features(oh)) { | |
2965 | /* Remove all the ports. */ | |
2966 | b->size = (sizeof(struct ofp_header) | |
2967 | + sizeof(struct ofp_switch_features)); | |
2968 | ofpmsg_update_length(b); | |
2969 | ||
2970 | return true; | |
2971 | } | |
2972 | ||
2973 | return false; | |
2974 | } | |
2975 | ||
2976 | static ovs_be32 | |
2977 | encode_action_bits(enum ofputil_action_bitmap ofputil_actions, | |
2978 | const struct ofputil_action_bit_translation *x) | |
2979 | { | |
2980 | uint32_t of_actions; | |
2981 | ||
2982 | of_actions = 0; | |
2983 | for (; x->ofputil_bit; x++) { | |
2984 | if (ofputil_actions & x->ofputil_bit) { | |
2985 | of_actions |= 1 << x->of_bit; | |
2986 | } | |
2987 | } | |
2988 | return htonl(of_actions); | |
2989 | } | |
2990 | ||
2991 | /* Returns a buffer owned by the caller that encodes 'features' in the format | |
2992 | * required by 'protocol' with the given 'xid'. The caller should append port | |
2993 | * information to the buffer with subsequent calls to | |
2994 | * ofputil_put_switch_features_port(). */ | |
2995 | struct ofpbuf * | |
2996 | ofputil_encode_switch_features(const struct ofputil_switch_features *features, | |
2997 | enum ofputil_protocol protocol, ovs_be32 xid) | |
2998 | { | |
2999 | struct ofp_switch_features *osf; | |
3000 | struct ofpbuf *b; | |
3001 | enum ofp_version version; | |
3002 | enum ofpraw raw; | |
3003 | ||
3004 | version = ofputil_protocol_to_ofp_version(protocol); | |
3005 | switch (version) { | |
3006 | case OFP10_VERSION: | |
3007 | raw = OFPRAW_OFPT10_FEATURES_REPLY; | |
3008 | break; | |
3009 | case OFP11_VERSION: | |
3010 | case OFP12_VERSION: | |
3011 | raw = OFPRAW_OFPT11_FEATURES_REPLY; | |
3012 | break; | |
3013 | default: | |
3014 | NOT_REACHED(); | |
3015 | } | |
3016 | b = ofpraw_alloc_xid(raw, version, xid, 0); | |
3017 | osf = ofpbuf_put_zeros(b, sizeof *osf); | |
3018 | osf->datapath_id = htonll(features->datapath_id); | |
3019 | osf->n_buffers = htonl(features->n_buffers); | |
3020 | osf->n_tables = features->n_tables; | |
3021 | ||
3022 | osf->capabilities = htonl(features->capabilities & OFPC_COMMON); | |
3023 | osf->capabilities = htonl(features->capabilities & | |
3024 | ofputil_capabilities_mask(version)); | |
3025 | switch (version) { | |
3026 | case OFP10_VERSION: | |
3027 | if (features->capabilities & OFPUTIL_C_STP) { | |
3028 | osf->capabilities |= htonl(OFPC10_STP); | |
3029 | } | |
3030 | osf->actions = encode_action_bits(features->actions, of10_action_bits); | |
3031 | break; | |
3032 | case OFP11_VERSION: | |
3033 | case OFP12_VERSION: | |
3034 | if (features->capabilities & OFPUTIL_C_GROUP_STATS) { | |
3035 | osf->capabilities |= htonl(OFPC11_GROUP_STATS); | |
3036 | } | |
3037 | break; | |
3038 | default: | |
3039 | NOT_REACHED(); | |
3040 | } | |
3041 | ||
3042 | return b; | |
3043 | } | |
3044 | ||
3045 | /* Encodes 'pp' into the format required by the switch_features message already | |
3046 | * in 'b', which should have been returned by ofputil_encode_switch_features(), | |
3047 | * and appends the encoded version to 'b'. */ | |
3048 | void | |
3049 | ofputil_put_switch_features_port(const struct ofputil_phy_port *pp, | |
3050 | struct ofpbuf *b) | |
3051 | { | |
3052 | const struct ofp_header *oh = b->data; | |
3053 | ||
3054 | ofputil_put_phy_port(oh->version, pp, b); | |
3055 | } | |
3056 | \f | |
3057 | /* ofputil_port_status */ | |
3058 | ||
3059 | /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form | |
3060 | * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
3061 | enum ofperr | |
3062 | ofputil_decode_port_status(const struct ofp_header *oh, | |
3063 | struct ofputil_port_status *ps) | |
3064 | { | |
3065 | const struct ofp_port_status *ops; | |
3066 | struct ofpbuf b; | |
3067 | int retval; | |
3068 | ||
3069 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
3070 | ofpraw_pull_assert(&b); | |
3071 | ops = ofpbuf_pull(&b, sizeof *ops); | |
3072 | ||
3073 | if (ops->reason != OFPPR_ADD && | |
3074 | ops->reason != OFPPR_DELETE && | |
3075 | ops->reason != OFPPR_MODIFY) { | |
3076 | return OFPERR_NXBRC_BAD_REASON; | |
3077 | } | |
3078 | ps->reason = ops->reason; | |
3079 | ||
3080 | retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc); | |
3081 | assert(retval != EOF); | |
3082 | return retval; | |
3083 | } | |
3084 | ||
3085 | /* Converts the abstract form of a "port status" message in '*ps' into an | |
3086 | * OpenFlow message suitable for 'protocol', and returns that encoded form in | |
3087 | * a buffer owned by the caller. */ | |
3088 | struct ofpbuf * | |
3089 | ofputil_encode_port_status(const struct ofputil_port_status *ps, | |
3090 | enum ofputil_protocol protocol) | |
3091 | { | |
3092 | struct ofp_port_status *ops; | |
3093 | struct ofpbuf *b; | |
3094 | enum ofp_version version; | |
3095 | enum ofpraw raw; | |
3096 | ||
3097 | version = ofputil_protocol_to_ofp_version(protocol); | |
3098 | switch (version) { | |
3099 | case OFP10_VERSION: | |
3100 | raw = OFPRAW_OFPT10_PORT_STATUS; | |
3101 | break; | |
3102 | ||
3103 | case OFP11_VERSION: | |
3104 | case OFP12_VERSION: | |
3105 | raw = OFPRAW_OFPT11_PORT_STATUS; | |
3106 | break; | |
3107 | ||
3108 | default: | |
3109 | NOT_REACHED(); | |
3110 | } | |
3111 | ||
3112 | b = ofpraw_alloc_xid(raw, version, htonl(0), 0); | |
3113 | ops = ofpbuf_put_zeros(b, sizeof *ops); | |
3114 | ops->reason = ps->reason; | |
3115 | ofputil_put_phy_port(version, &ps->desc, b); | |
3116 | ofpmsg_update_length(b); | |
3117 | return b; | |
3118 | } | |
3119 | \f | |
3120 | /* ofputil_port_mod */ | |
3121 | ||
3122 | /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in | |
3123 | * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */ | |
3124 | enum ofperr | |
3125 | ofputil_decode_port_mod(const struct ofp_header *oh, | |
3126 | struct ofputil_port_mod *pm) | |
3127 | { | |
3128 | enum ofpraw raw; | |
3129 | struct ofpbuf b; | |
3130 | ||
3131 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
3132 | raw = ofpraw_pull_assert(&b); | |
3133 | ||
3134 | if (raw == OFPRAW_OFPT10_PORT_MOD) { | |
3135 | const struct ofp10_port_mod *opm = b.data; | |
3136 | ||
3137 | pm->port_no = ntohs(opm->port_no); | |
3138 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
3139 | pm->config = ntohl(opm->config) & OFPPC10_ALL; | |
3140 | pm->mask = ntohl(opm->mask) & OFPPC10_ALL; | |
3141 | pm->advertise = netdev_port_features_from_ofp10(opm->advertise); | |
3142 | } else if (raw == OFPRAW_OFPT11_PORT_MOD) { | |
3143 | const struct ofp11_port_mod *opm = b.data; | |
3144 | enum ofperr error; | |
3145 | ||
3146 | error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no); | |
3147 | if (error) { | |
3148 | return error; | |
3149 | } | |
3150 | ||
3151 | memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN); | |
3152 | pm->config = ntohl(opm->config) & OFPPC11_ALL; | |
3153 | pm->mask = ntohl(opm->mask) & OFPPC11_ALL; | |
3154 | pm->advertise = netdev_port_features_from_ofp11(opm->advertise); | |
3155 | } else { | |
3156 | return OFPERR_OFPBRC_BAD_TYPE; | |
3157 | } | |
3158 | ||
3159 | pm->config &= pm->mask; | |
3160 | return 0; | |
3161 | } | |
3162 | ||
3163 | /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow | |
3164 | * message suitable for 'protocol', and returns that encoded form in a buffer | |
3165 | * owned by the caller. */ | |
3166 | struct ofpbuf * | |
3167 | ofputil_encode_port_mod(const struct ofputil_port_mod *pm, | |
3168 | enum ofputil_protocol protocol) | |
3169 | { | |
3170 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); | |
3171 | struct ofpbuf *b; | |
3172 | ||
3173 | switch (ofp_version) { | |
3174 | case OFP10_VERSION: { | |
3175 | struct ofp10_port_mod *opm; | |
3176 | ||
3177 | b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0); | |
3178 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
3179 | opm->port_no = htons(pm->port_no); | |
3180 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); | |
3181 | opm->config = htonl(pm->config & OFPPC10_ALL); | |
3182 | opm->mask = htonl(pm->mask & OFPPC10_ALL); | |
3183 | opm->advertise = netdev_port_features_to_ofp10(pm->advertise); | |
3184 | break; | |
3185 | } | |
3186 | ||
3187 | case OFP11_VERSION: | |
3188 | case OFP12_VERSION: { | |
3189 | struct ofp11_port_mod *opm; | |
3190 | ||
3191 | b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0); | |
3192 | opm = ofpbuf_put_zeros(b, sizeof *opm); | |
3193 | opm->port_no = ofputil_port_to_ofp11(pm->port_no); | |
3194 | memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN); | |
3195 | opm->config = htonl(pm->config & OFPPC11_ALL); | |
3196 | opm->mask = htonl(pm->mask & OFPPC11_ALL); | |
3197 | opm->advertise = netdev_port_features_to_ofp11(pm->advertise); | |
3198 | break; | |
3199 | } | |
3200 | ||
3201 | default: | |
3202 | NOT_REACHED(); | |
3203 | } | |
3204 | ||
3205 | return b; | |
3206 | } | |
3207 | \f | |
3208 | /* Table stats. */ | |
3209 | ||
3210 | static void | |
3211 | ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in, | |
3212 | struct ofpbuf *buf) | |
3213 | { | |
3214 | struct wc_map { | |
3215 | enum ofp_flow_wildcards wc10; | |
3216 | enum oxm12_ofb_match_fields mf12; | |
3217 | }; | |
3218 | ||
3219 | static const struct wc_map wc_map[] = { | |
3220 | { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT }, | |
3221 | { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID }, | |
3222 | { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC }, | |
3223 | { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST}, | |
3224 | { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE }, | |
3225 | { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO }, | |
3226 | { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC }, | |
3227 | { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST }, | |
3228 | { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC }, | |
3229 | { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST }, | |
3230 | { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP }, | |
3231 | { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP }, | |
3232 | }; | |
3233 | ||
3234 | struct ofp10_table_stats *out; | |
3235 | const struct wc_map *p; | |
3236 | ||
3237 | out = ofpbuf_put_uninit(buf, sizeof *out); | |
3238 | out->table_id = in->table_id; | |
3239 | strcpy(out->name, in->name); | |
3240 | out->wildcards = 0; | |
3241 | for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) { | |
3242 | if (in->wildcards & htonll(1ULL << p->mf12)) { | |
3243 | out->wildcards |= htonl(p->wc10); | |
3244 | } | |
3245 | } | |
3246 | out->max_entries = in->max_entries; | |
3247 | out->active_count = in->active_count; | |
3248 | put_32aligned_be64(&out->lookup_count, in->lookup_count); | |
3249 | put_32aligned_be64(&out->matched_count, in->matched_count); | |
3250 | } | |
3251 | ||
3252 | static ovs_be32 | |
3253 | oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12) | |
3254 | { | |
3255 | struct map { | |
3256 | enum ofp11_flow_match_fields fmf11; | |
3257 | enum oxm12_ofb_match_fields mf12; | |
3258 | }; | |
3259 | ||
3260 | static const struct map map[] = { | |
3261 | { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT }, | |
3262 | { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID }, | |
3263 | { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP }, | |
3264 | { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE }, | |
3265 | { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP }, | |
3266 | { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO }, | |
3267 | { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC }, | |
3268 | { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST }, | |
3269 | { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL }, | |
3270 | { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC }, | |
3271 | /* I don't know what OFPFMF11_TYPE means. */ | |
3272 | { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC }, | |
3273 | { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST }, | |
3274 | { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC }, | |
3275 | { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST }, | |
3276 | { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA }, | |
3277 | }; | |
3278 | ||
3279 | const struct map *p; | |
3280 | uint32_t fmf11; | |
3281 | ||
3282 | fmf11 = 0; | |
3283 | for (p = map; p < &map[ARRAY_SIZE(map)]; p++) { | |
3284 | if (oxm12 & htonll(1ULL << p->mf12)) { | |
3285 | fmf11 |= p->fmf11; | |
3286 | } | |
3287 | } | |
3288 | return htonl(fmf11); | |
3289 | } | |
3290 | ||
3291 | static void | |
3292 | ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in, | |
3293 | struct ofpbuf *buf) | |
3294 | { | |
3295 | struct ofp11_table_stats *out; | |
3296 | ||
3297 | out = ofpbuf_put_uninit(buf, sizeof *out); | |
3298 | out->table_id = in->table_id; | |
3299 | strcpy(out->name, in->name); | |
3300 | out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards); | |
3301 | out->match = oxm12_to_ofp11_flow_match_fields(in->match); | |
3302 | out->instructions = in->instructions; | |
3303 | out->write_actions = in->write_actions; | |
3304 | out->apply_actions = in->apply_actions; | |
3305 | out->config = in->config; | |
3306 | out->max_entries = in->max_entries; | |
3307 | out->active_count = in->active_count; | |
3308 | out->lookup_count = in->lookup_count; | |
3309 | out->matched_count = in->matched_count; | |
3310 | } | |
3311 | ||
3312 | struct ofpbuf * | |
3313 | ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n, | |
3314 | const struct ofp_header *request) | |
3315 | { | |
3316 | struct ofpbuf *reply; | |
3317 | int i; | |
3318 | ||
3319 | reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats); | |
3320 | ||
3321 | switch ((enum ofp_version) request->version) { | |
3322 | case OFP10_VERSION: | |
3323 | for (i = 0; i < n; i++) { | |
3324 | ofputil_put_ofp10_table_stats(&stats[i], reply); | |
3325 | } | |
3326 | break; | |
3327 | ||
3328 | case OFP11_VERSION: | |
3329 | for (i = 0; i < n; i++) { | |
3330 | ofputil_put_ofp11_table_stats(&stats[i], reply); | |
3331 | } | |
3332 | break; | |
3333 | ||
3334 | case OFP12_VERSION: | |
3335 | ofpbuf_put(reply, stats, n * sizeof *stats); | |
3336 | break; | |
3337 | ||
3338 | default: | |
3339 | NOT_REACHED(); | |
3340 | } | |
3341 | ||
3342 | return reply; | |
3343 | } | |
3344 | \f | |
3345 | /* ofputil_flow_monitor_request */ | |
3346 | ||
3347 | /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract | |
3348 | * ofputil_flow_monitor_request in 'rq'. | |
3349 | * | |
3350 | * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow | |
3351 | * message. Calling this function multiple times for a single 'msg' iterates | |
3352 | * through the requests. The caller must initially leave 'msg''s layer | |
3353 | * pointers null and not modify them between calls. | |
3354 | * | |
3355 | * Returns 0 if successful, EOF if no requests were left in this 'msg', | |
3356 | * otherwise an OFPERR_* value. */ | |
3357 | int | |
3358 | ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq, | |
3359 | struct ofpbuf *msg) | |
3360 | { | |
3361 | struct nx_flow_monitor_request *nfmr; | |
3362 | uint16_t flags; | |
3363 | ||
3364 | if (!msg->l2) { | |
3365 | msg->l2 = msg->data; | |
3366 | ofpraw_pull_assert(msg); | |
3367 | } | |
3368 | ||
3369 | if (!msg->size) { | |
3370 | return EOF; | |
3371 | } | |
3372 | ||
3373 | nfmr = ofpbuf_try_pull(msg, sizeof *nfmr); | |
3374 | if (!nfmr) { | |
3375 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu " | |
3376 | "leftover bytes at end", msg->size); | |
3377 | return OFPERR_OFPBRC_BAD_LEN; | |
3378 | } | |
3379 | ||
3380 | flags = ntohs(nfmr->flags); | |
3381 | if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY)) | |
3382 | || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | |
3383 | | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) { | |
3384 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16, | |
3385 | flags); | |
3386 | return OFPERR_NXBRC_FM_BAD_FLAGS; | |
3387 | } | |
3388 | ||
3389 | if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) { | |
3390 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
3391 | } | |
3392 | ||
3393 | rq->id = ntohl(nfmr->id); | |
3394 | rq->flags = flags; | |
3395 | rq->out_port = ntohs(nfmr->out_port); | |
3396 | rq->table_id = nfmr->table_id; | |
3397 | ||
3398 | return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL); | |
3399 | } | |
3400 | ||
3401 | void | |
3402 | ofputil_append_flow_monitor_request( | |
3403 | const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg) | |
3404 | { | |
3405 | struct nx_flow_monitor_request *nfmr; | |
3406 | size_t start_ofs; | |
3407 | int match_len; | |
3408 | ||
3409 | if (!msg->size) { | |
3410 | ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg); | |
3411 | } | |
3412 | ||
3413 | start_ofs = msg->size; | |
3414 | ofpbuf_put_zeros(msg, sizeof *nfmr); | |
3415 | match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0)); | |
3416 | ||
3417 | nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr); | |
3418 | nfmr->id = htonl(rq->id); | |
3419 | nfmr->flags = htons(rq->flags); | |
3420 | nfmr->out_port = htons(rq->out_port); | |
3421 | nfmr->match_len = htons(match_len); | |
3422 | nfmr->table_id = rq->table_id; | |
3423 | } | |
3424 | ||
3425 | /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg' | |
3426 | * into an abstract ofputil_flow_update in 'update'. The caller must have | |
3427 | * initialized update->match to point to space allocated for a match. | |
3428 | * | |
3429 | * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's | |
3430 | * actions (except for NXFME_ABBREV, which never includes actions). The caller | |
3431 | * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts' | |
3432 | * will point into the 'ofpacts' buffer. | |
3433 | * | |
3434 | * Multiple flow updates can be packed into a single OpenFlow message. Calling | |
3435 | * this function multiple times for a single 'msg' iterates through the | |
3436 | * updates. The caller must initially leave 'msg''s layer pointers null and | |
3437 | * not modify them between calls. | |
3438 | * | |
3439 | * Returns 0 if successful, EOF if no updates were left in this 'msg', | |
3440 | * otherwise an OFPERR_* value. */ | |
3441 | int | |
3442 | ofputil_decode_flow_update(struct ofputil_flow_update *update, | |
3443 | struct ofpbuf *msg, struct ofpbuf *ofpacts) | |
3444 | { | |
3445 | struct nx_flow_update_header *nfuh; | |
3446 | unsigned int length; | |
3447 | ||
3448 | if (!msg->l2) { | |
3449 | msg->l2 = msg->data; | |
3450 | ofpraw_pull_assert(msg); | |
3451 | } | |
3452 | ||
3453 | if (!msg->size) { | |
3454 | return EOF; | |
3455 | } | |
3456 | ||
3457 | if (msg->size < sizeof(struct nx_flow_update_header)) { | |
3458 | goto bad_len; | |
3459 | } | |
3460 | ||
3461 | nfuh = msg->data; | |
3462 | update->event = ntohs(nfuh->event); | |
3463 | length = ntohs(nfuh->length); | |
3464 | if (length > msg->size || length % 8) { | |
3465 | goto bad_len; | |
3466 | } | |
3467 | ||
3468 | if (update->event == NXFME_ABBREV) { | |
3469 | struct nx_flow_update_abbrev *nfua; | |
3470 | ||
3471 | if (length != sizeof *nfua) { | |
3472 | goto bad_len; | |
3473 | } | |
3474 | ||
3475 | nfua = ofpbuf_pull(msg, sizeof *nfua); | |
3476 | update->xid = nfua->xid; | |
3477 | return 0; | |
3478 | } else if (update->event == NXFME_ADDED | |
3479 | || update->event == NXFME_DELETED | |
3480 | || update->event == NXFME_MODIFIED) { | |
3481 | struct nx_flow_update_full *nfuf; | |
3482 | unsigned int actions_len; | |
3483 | unsigned int match_len; | |
3484 | enum ofperr error; | |
3485 | ||
3486 | if (length < sizeof *nfuf) { | |
3487 | goto bad_len; | |
3488 | } | |
3489 | ||
3490 | nfuf = ofpbuf_pull(msg, sizeof *nfuf); | |
3491 | match_len = ntohs(nfuf->match_len); | |
3492 | if (sizeof *nfuf + match_len > length) { | |
3493 | goto bad_len; | |
3494 | } | |
3495 | ||
3496 | update->reason = ntohs(nfuf->reason); | |
3497 | update->idle_timeout = ntohs(nfuf->idle_timeout); | |
3498 | update->hard_timeout = ntohs(nfuf->hard_timeout); | |
3499 | update->table_id = nfuf->table_id; | |
3500 | update->cookie = nfuf->cookie; | |
3501 | update->priority = ntohs(nfuf->priority); | |
3502 | ||
3503 | error = nx_pull_match(msg, match_len, update->match, NULL, NULL); | |
3504 | if (error) { | |
3505 | return error; | |
3506 | } | |
3507 | ||
3508 | actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8); | |
3509 | error = ofpacts_pull_openflow10(msg, actions_len, ofpacts); | |
3510 | if (error) { | |
3511 | return error; | |
3512 | } | |
3513 | ||
3514 | update->ofpacts = ofpacts->data; | |
3515 | update->ofpacts_len = ofpacts->size; | |
3516 | return 0; | |
3517 | } else { | |
3518 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
3519 | "NXST_FLOW_MONITOR reply has bad event %"PRIu16, | |
3520 | ntohs(nfuh->event)); | |
3521 | return OFPERR_OFPET_BAD_REQUEST; | |
3522 | } | |
3523 | ||
3524 | bad_len: | |
3525 | VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu " | |
3526 | "leftover bytes at end", msg->size); | |
3527 | return OFPERR_OFPBRC_BAD_LEN; | |
3528 | } | |
3529 | ||
3530 | uint32_t | |
3531 | ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh) | |
3532 | { | |
3533 | const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh); | |
3534 | ||
3535 | return ntohl(cancel->id); | |
3536 | } | |
3537 | ||
3538 | struct ofpbuf * | |
3539 | ofputil_encode_flow_monitor_cancel(uint32_t id) | |
3540 | { | |
3541 | struct nx_flow_monitor_cancel *nfmc; | |
3542 | struct ofpbuf *msg; | |
3543 | ||
3544 | msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0); | |
3545 | nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc); | |
3546 | nfmc->id = htonl(id); | |
3547 | return msg; | |
3548 | } | |
3549 | ||
3550 | void | |
3551 | ofputil_start_flow_update(struct list *replies) | |
3552 | { | |
3553 | struct ofpbuf *msg; | |
3554 | ||
3555 | msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION, | |
3556 | htonl(0), 1024); | |
3557 | ||
3558 | list_init(replies); | |
3559 | list_push_back(replies, &msg->list_node); | |
3560 | } | |
3561 | ||
3562 | void | |
3563 | ofputil_append_flow_update(const struct ofputil_flow_update *update, | |
3564 | struct list *replies) | |
3565 | { | |
3566 | struct nx_flow_update_header *nfuh; | |
3567 | struct ofpbuf *msg; | |
3568 | size_t start_ofs; | |
3569 | ||
3570 | msg = ofpbuf_from_list(list_back(replies)); | |
3571 | start_ofs = msg->size; | |
3572 | ||
3573 | if (update->event == NXFME_ABBREV) { | |
3574 | struct nx_flow_update_abbrev *nfua; | |
3575 | ||
3576 | nfua = ofpbuf_put_zeros(msg, sizeof *nfua); | |
3577 | nfua->xid = update->xid; | |
3578 | } else { | |
3579 | struct nx_flow_update_full *nfuf; | |
3580 | int match_len; | |
3581 | ||
3582 | ofpbuf_put_zeros(msg, sizeof *nfuf); | |
3583 | match_len = nx_put_match(msg, update->match, htonll(0), htonll(0)); | |
3584 | ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg); | |
3585 | ||
3586 | nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf); | |
3587 | nfuf->reason = htons(update->reason); | |
3588 | nfuf->priority = htons(update->priority); | |
3589 | nfuf->idle_timeout = htons(update->idle_timeout); | |
3590 | nfuf->hard_timeout = htons(update->hard_timeout); | |
3591 | nfuf->match_len = htons(match_len); | |
3592 | nfuf->table_id = update->table_id; | |
3593 | nfuf->cookie = update->cookie; | |
3594 | } | |
3595 | ||
3596 | nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh); | |
3597 | nfuh->length = htons(msg->size - start_ofs); | |
3598 | nfuh->event = htons(update->event); | |
3599 | ||
3600 | ofpmp_postappend(replies, start_ofs); | |
3601 | } | |
3602 | \f | |
3603 | struct ofpbuf * | |
3604 | ofputil_encode_packet_out(const struct ofputil_packet_out *po, | |
3605 | enum ofputil_protocol protocol) | |
3606 | { | |
3607 | enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol); | |
3608 | struct ofpbuf *msg; | |
3609 | size_t size; | |
3610 | ||
3611 | size = po->ofpacts_len; | |
3612 | if (po->buffer_id == UINT32_MAX) { | |
3613 | size += po->packet_len; | |
3614 | } | |
3615 | ||
3616 | switch (ofp_version) { | |
3617 | case OFP10_VERSION: { | |
3618 | struct ofp_packet_out *opo; | |
3619 | size_t actions_ofs; | |
3620 | ||
3621 | msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size); | |
3622 | ofpbuf_put_zeros(msg, sizeof *opo); | |
3623 | actions_ofs = msg->size; | |
3624 | ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg); | |
3625 | ||
3626 | opo = msg->l3; | |
3627 | opo->buffer_id = htonl(po->buffer_id); | |
3628 | opo->in_port = htons(po->in_port); | |
3629 | opo->actions_len = htons(msg->size - actions_ofs); | |
3630 | break; | |
3631 | } | |
3632 | ||
3633 | case OFP11_VERSION: | |
3634 | case OFP12_VERSION: { | |
3635 | struct ofp11_packet_out *opo; | |
3636 | size_t len; | |
3637 | ||
3638 | msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size); | |
3639 | ofpbuf_put_zeros(msg, sizeof *opo); | |
3640 | len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg); | |
3641 | ||
3642 | opo = msg->l3; | |
3643 | opo->buffer_id = htonl(po->buffer_id); | |
3644 | opo->in_port = ofputil_port_to_ofp11(po->in_port); | |
3645 | opo->actions_len = htons(len); | |
3646 | break; | |
3647 | } | |
3648 | ||
3649 | default: | |
3650 | NOT_REACHED(); | |
3651 | } | |
3652 | ||
3653 | if (po->buffer_id == UINT32_MAX) { | |
3654 | ofpbuf_put(msg, po->packet, po->packet_len); | |
3655 | } | |
3656 | ||
3657 | ofpmsg_update_length(msg); | |
3658 | ||
3659 | return msg; | |
3660 | } | |
3661 | \f | |
3662 | /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */ | |
3663 | struct ofpbuf * | |
3664 | make_echo_request(enum ofp_version ofp_version) | |
3665 | { | |
3666 | return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version, | |
3667 | htonl(0), 0); | |
3668 | } | |
3669 | ||
3670 | /* Creates and returns an OFPT_ECHO_REPLY message matching the | |
3671 | * OFPT_ECHO_REQUEST message in 'rq'. */ | |
3672 | struct ofpbuf * | |
3673 | make_echo_reply(const struct ofp_header *rq) | |
3674 | { | |
3675 | struct ofpbuf rq_buf; | |
3676 | struct ofpbuf *reply; | |
3677 | ||
3678 | ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length)); | |
3679 | ofpraw_pull_assert(&rq_buf); | |
3680 | ||
3681 | reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size); | |
3682 | ofpbuf_put(reply, rq_buf.data, rq_buf.size); | |
3683 | return reply; | |
3684 | } | |
3685 | ||
3686 | struct ofpbuf * | |
3687 | ofputil_encode_barrier_request(enum ofp_version ofp_version) | |
3688 | { | |
3689 | enum ofpraw type; | |
3690 | ||
3691 | switch (ofp_version) { | |
3692 | case OFP12_VERSION: | |
3693 | case OFP11_VERSION: | |
3694 | type = OFPRAW_OFPT11_BARRIER_REQUEST; | |
3695 | break; | |
3696 | ||
3697 | case OFP10_VERSION: | |
3698 | type = OFPRAW_OFPT10_BARRIER_REQUEST; | |
3699 | break; | |
3700 | ||
3701 | default: | |
3702 | NOT_REACHED(); | |
3703 | } | |
3704 | ||
3705 | return ofpraw_alloc(type, ofp_version, 0); | |
3706 | } | |
3707 | ||
3708 | const char * | |
3709 | ofputil_frag_handling_to_string(enum ofp_config_flags flags) | |
3710 | { | |
3711 | switch (flags & OFPC_FRAG_MASK) { | |
3712 | case OFPC_FRAG_NORMAL: return "normal"; | |
3713 | case OFPC_FRAG_DROP: return "drop"; | |
3714 | case OFPC_FRAG_REASM: return "reassemble"; | |
3715 | case OFPC_FRAG_NX_MATCH: return "nx-match"; | |
3716 | } | |
3717 | ||
3718 | NOT_REACHED(); | |
3719 | } | |
3720 | ||
3721 | bool | |
3722 | ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags) | |
3723 | { | |
3724 | if (!strcasecmp(s, "normal")) { | |
3725 | *flags = OFPC_FRAG_NORMAL; | |
3726 | } else if (!strcasecmp(s, "drop")) { | |
3727 | *flags = OFPC_FRAG_DROP; | |
3728 | } else if (!strcasecmp(s, "reassemble")) { | |
3729 | *flags = OFPC_FRAG_REASM; | |
3730 | } else if (!strcasecmp(s, "nx-match")) { | |
3731 | *flags = OFPC_FRAG_NX_MATCH; | |
3732 | } else { | |
3733 | return false; | |
3734 | } | |
3735 | return true; | |
3736 | } | |
3737 | ||
3738 | /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0 | |
3739 | * port number and stores the latter in '*ofp10_port', for the purpose of | |
3740 | * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful, | |
3741 | * otherwise an OFPERR_* number. | |
3742 | * | |
3743 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
3744 | enum ofperr | |
3745 | ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port) | |
3746 | { | |
3747 | uint32_t ofp11_port_h = ntohl(ofp11_port); | |
3748 | ||
3749 | if (ofp11_port_h < OFPP_MAX) { | |
3750 | *ofp10_port = ofp11_port_h; | |
3751 | return 0; | |
3752 | } else if (ofp11_port_h >= OFPP11_MAX) { | |
3753 | *ofp10_port = ofp11_port_h - OFPP11_OFFSET; | |
3754 | return 0; | |
3755 | } else { | |
3756 | VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported " | |
3757 | "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32, | |
3758 | ofp11_port_h, OFPP_MAX - 1, | |
3759 | (uint32_t) OFPP11_MAX, UINT32_MAX); | |
3760 | return OFPERR_OFPBAC_BAD_OUT_PORT; | |
3761 | } | |
3762 | } | |
3763 | ||
3764 | /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port | |
3765 | * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages. | |
3766 | * | |
3767 | * See the definition of OFP11_MAX for an explanation of the mapping. */ | |
3768 | ovs_be32 | |
3769 | ofputil_port_to_ofp11(uint16_t ofp10_port) | |
3770 | { | |
3771 | return htonl(ofp10_port < OFPP_MAX | |
3772 | ? ofp10_port | |
3773 | : ofp10_port + OFPP11_OFFSET); | |
3774 | } | |
3775 | ||
3776 | /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given | |
3777 | * that the switch will never have more than 'max_ports' ports. Returns 0 if | |
3778 | * 'port' is valid, otherwise an OpenFlow return code. */ | |
3779 | enum ofperr | |
3780 | ofputil_check_output_port(uint16_t port, int max_ports) | |
3781 | { | |
3782 | switch (port) { | |
3783 | case OFPP_IN_PORT: | |
3784 | case OFPP_TABLE: | |
3785 | case OFPP_NORMAL: | |
3786 | case OFPP_FLOOD: | |
3787 | case OFPP_ALL: | |
3788 | case OFPP_CONTROLLER: | |
3789 | case OFPP_NONE: | |
3790 | case OFPP_LOCAL: | |
3791 | return 0; | |
3792 | ||
3793 | default: | |
3794 | if (port < max_ports) { | |
3795 | return 0; | |
3796 | } | |
3797 | return OFPERR_OFPBAC_BAD_OUT_PORT; | |
3798 | } | |
3799 | } | |
3800 | ||
3801 | #define OFPUTIL_NAMED_PORTS \ | |
3802 | OFPUTIL_NAMED_PORT(IN_PORT) \ | |
3803 | OFPUTIL_NAMED_PORT(TABLE) \ | |
3804 | OFPUTIL_NAMED_PORT(NORMAL) \ | |
3805 | OFPUTIL_NAMED_PORT(FLOOD) \ | |
3806 | OFPUTIL_NAMED_PORT(ALL) \ | |
3807 | OFPUTIL_NAMED_PORT(CONTROLLER) \ | |
3808 | OFPUTIL_NAMED_PORT(LOCAL) \ | |
3809 | OFPUTIL_NAMED_PORT(ANY) | |
3810 | ||
3811 | /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */ | |
3812 | #define OFPUTIL_NAMED_PORTS_WITH_NONE \ | |
3813 | OFPUTIL_NAMED_PORTS \ | |
3814 | OFPUTIL_NAMED_PORT(NONE) | |
3815 | ||
3816 | /* Stores the port number represented by 's' into '*portp'. 's' may be an | |
3817 | * integer or, for reserved ports, the standard OpenFlow name for the port | |
3818 | * (e.g. "LOCAL"). | |
3819 | * | |
3820 | * Returns true if successful, false if 's' is not a valid OpenFlow port number | |
3821 | * or name. The caller should issue an error message in this case, because | |
3822 | * this function usually does not. (This gives the caller an opportunity to | |
3823 | * look up the port name another way, e.g. by contacting the switch and listing | |
3824 | * the names of all its ports). | |
3825 | * | |
3826 | * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset | |
3827 | * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit | |
3828 | * range as described in include/openflow/openflow-1.1.h. */ | |
3829 | bool | |
3830 | ofputil_port_from_string(const char *s, uint16_t *portp) | |
3831 | { | |
3832 | unsigned int port32; | |
3833 | ||
3834 | *portp = 0; | |
3835 | if (str_to_uint(s, 10, &port32)) { | |
3836 | if (port32 < OFPP_MAX) { | |
3837 | *portp = port32; | |
3838 | return true; | |
3839 | } else if (port32 < OFPP_FIRST_RESV) { | |
3840 | VLOG_WARN("port %u is a reserved OF1.0 port number that will " | |
3841 | "be translated to %u when talking to an OF1.1 or " | |
3842 | "later controller", port32, port32 + OFPP11_OFFSET); | |
3843 | *portp = port32; | |
3844 | return true; | |
3845 | } else if (port32 <= OFPP_LAST_RESV) { | |
3846 | struct ds s; | |
3847 | ||
3848 | ds_init(&s); | |
3849 | ofputil_format_port(port32, &s); | |
3850 | VLOG_WARN_ONCE("referring to port %s as %u is deprecated for " | |
3851 | "compatibility with future versions of OpenFlow", | |
3852 | ds_cstr(&s), port32); | |
3853 | ds_destroy(&s); | |
3854 | ||
3855 | *portp = port32; | |
3856 | return true; | |
3857 | } else if (port32 < OFPP11_MAX) { | |
3858 | VLOG_WARN("port %u is outside the supported range 0 through " | |
3859 | "%"PRIx16"or 0x%x through 0x%"PRIx32, port32, | |
3860 | UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX); | |
3861 | return false; | |
3862 | } else { | |
3863 | *portp = port32 - OFPP11_OFFSET; | |
3864 | return true; | |
3865 | } | |
3866 | } else { | |
3867 | struct pair { | |
3868 | const char *name; | |
3869 | uint16_t value; | |
3870 | }; | |
3871 | static const struct pair pairs[] = { | |
3872 | #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME}, | |
3873 | OFPUTIL_NAMED_PORTS_WITH_NONE | |
3874 | #undef OFPUTIL_NAMED_PORT | |
3875 | }; | |
3876 | const struct pair *p; | |
3877 | ||
3878 | for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) { | |
3879 | if (!strcasecmp(s, p->name)) { | |
3880 | *portp = p->value; | |
3881 | return true; | |
3882 | } | |
3883 | } | |
3884 | return false; | |
3885 | } | |
3886 | } | |
3887 | ||
3888 | /* Appends to 's' a string representation of the OpenFlow port number 'port'. | |
3889 | * Most ports' string representation is just the port number, but for special | |
3890 | * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */ | |
3891 | void | |
3892 | ofputil_format_port(uint16_t port, struct ds *s) | |
3893 | { | |
3894 | const char *name; | |
3895 | ||
3896 | switch (port) { | |
3897 | #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break; | |
3898 | OFPUTIL_NAMED_PORTS | |
3899 | #undef OFPUTIL_NAMED_PORT | |
3900 | ||
3901 | default: | |
3902 | ds_put_format(s, "%"PRIu16, port); | |
3903 | return; | |
3904 | } | |
3905 | ds_put_cstr(s, name); | |
3906 | } | |
3907 | ||
3908 | /* Given a buffer 'b' that contains an array of OpenFlow ports of type | |
3909 | * 'ofp_version', tries to pull the first element from the array. If | |
3910 | * successful, initializes '*pp' with an abstract representation of the | |
3911 | * port and returns 0. If no ports remain to be decoded, returns EOF. | |
3912 | * On an error, returns a positive OFPERR_* value. */ | |
3913 | int | |
3914 | ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b, | |
3915 | struct ofputil_phy_port *pp) | |
3916 | { | |
3917 | switch (ofp_version) { | |
3918 | case OFP10_VERSION: { | |
3919 | const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp); | |
3920 | return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF; | |
3921 | } | |
3922 | case OFP11_VERSION: | |
3923 | case OFP12_VERSION: { | |
3924 | const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op); | |
3925 | return op ? ofputil_decode_ofp11_port(pp, op) : EOF; | |
3926 | } | |
3927 | default: | |
3928 | NOT_REACHED(); | |
3929 | } | |
3930 | } | |
3931 | ||
3932 | /* Given a buffer 'b' that contains an array of OpenFlow ports of type | |
3933 | * 'ofp_version', returns the number of elements. */ | |
3934 | size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b) | |
3935 | { | |
3936 | return b->size / ofputil_get_phy_port_size(ofp_version); | |
3937 | } | |
3938 | ||
3939 | /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if | |
3940 | * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if | |
3941 | * 'name' is not the name of any action. | |
3942 | * | |
3943 | * ofp-util.def lists the mapping from names to action. */ | |
3944 | int | |
3945 | ofputil_action_code_from_name(const char *name) | |
3946 | { | |
3947 | static const char *names[OFPUTIL_N_ACTIONS] = { | |
3948 | NULL, | |
3949 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME, | |
3950 | #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, | |
3951 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, | |
3952 | #include "ofp-util.def" | |
3953 | }; | |
3954 | ||
3955 | const char **p; | |
3956 | ||
3957 | for (p = names; p < &names[ARRAY_SIZE(names)]; p++) { | |
3958 | if (*p && !strcasecmp(name, *p)) { | |
3959 | return p - names; | |
3960 | } | |
3961 | } | |
3962 | return -1; | |
3963 | } | |
3964 | ||
3965 | /* Appends an action of the type specified by 'code' to 'buf' and returns the | |
3966 | * action. Initializes the parts of 'action' that identify it as having type | |
3967 | * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that | |
3968 | * have variable length, the length used and cleared is that of struct | |
3969 | * <STRUCT>. */ | |
3970 | void * | |
3971 | ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf) | |
3972 | { | |
3973 | switch (code) { | |
3974 | case OFPUTIL_ACTION_INVALID: | |
3975 | NOT_REACHED(); | |
3976 | ||
3977 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ | |
3978 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
3979 | #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
3980 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
3981 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
3982 | case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); | |
3983 | #include "ofp-util.def" | |
3984 | } | |
3985 | NOT_REACHED(); | |
3986 | } | |
3987 | ||
3988 | #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ | |
3989 | void \ | |
3990 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
3991 | { \ | |
3992 | memset(s, 0, sizeof *s); \ | |
3993 | s->type = htons(ENUM); \ | |
3994 | s->len = htons(sizeof *s); \ | |
3995 | } \ | |
3996 | \ | |
3997 | struct STRUCT * \ | |
3998 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
3999 | { \ | |
4000 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
4001 | ofputil_init_##ENUM(s); \ | |
4002 | return s; \ | |
4003 | } | |
4004 | #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
4005 | OFPAT10_ACTION(ENUM, STRUCT, NAME) | |
4006 | #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ | |
4007 | void \ | |
4008 | ofputil_init_##ENUM(struct STRUCT *s) \ | |
4009 | { \ | |
4010 | memset(s, 0, sizeof *s); \ | |
4011 | s->type = htons(OFPAT10_VENDOR); \ | |
4012 | s->len = htons(sizeof *s); \ | |
4013 | s->vendor = htonl(NX_VENDOR_ID); \ | |
4014 | s->subtype = htons(ENUM); \ | |
4015 | } \ | |
4016 | \ | |
4017 | struct STRUCT * \ | |
4018 | ofputil_put_##ENUM(struct ofpbuf *buf) \ | |
4019 | { \ | |
4020 | struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ | |
4021 | ofputil_init_##ENUM(s); \ | |
4022 | return s; \ | |
4023 | } | |
4024 | #include "ofp-util.def" | |
4025 | ||
4026 | static void | |
4027 | ofputil_normalize_match__(struct match *match, bool may_log) | |
4028 | { | |
4029 | enum { | |
4030 | MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */ | |
4031 | MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */ | |
4032 | MAY_NW_PROTO = 1 << 2, /* nw_proto */ | |
4033 | MAY_IPVx = 1 << 3, /* tos, frag, ttl */ | |
4034 | MAY_ARP_SHA = 1 << 4, /* arp_sha */ | |
4035 | MAY_ARP_THA = 1 << 5, /* arp_tha */ | |
4036 | MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */ | |
4037 | MAY_ND_TARGET = 1 << 7 /* nd_target */ | |
4038 | } may_match; | |
4039 | ||
4040 | struct flow_wildcards wc; | |
4041 | ||
4042 | /* Figure out what fields may be matched. */ | |
4043 | if (match->flow.dl_type == htons(ETH_TYPE_IP)) { | |
4044 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR; | |
4045 | if (match->flow.nw_proto == IPPROTO_TCP || | |
4046 | match->flow.nw_proto == IPPROTO_UDP || | |
4047 | match->flow.nw_proto == IPPROTO_ICMP) { | |
4048 | may_match |= MAY_TP_ADDR; | |
4049 | } | |
4050 | } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) { | |
4051 | may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6; | |
4052 | if (match->flow.nw_proto == IPPROTO_TCP || | |
4053 | match->flow.nw_proto == IPPROTO_UDP) { | |
4054 | may_match |= MAY_TP_ADDR; | |
4055 | } else if (match->flow.nw_proto == IPPROTO_ICMPV6) { | |
4056 | may_match |= MAY_TP_ADDR; | |
4057 | if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) { | |
4058 | may_match |= MAY_ND_TARGET | MAY_ARP_SHA; | |
4059 | } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
4060 | may_match |= MAY_ND_TARGET | MAY_ARP_THA; | |
4061 | } | |
4062 | } | |
4063 | } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) || | |
4064 | match->flow.dl_type == htons(ETH_TYPE_RARP)) { | |
4065 | may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA; | |
4066 | } else { | |
4067 | may_match = 0; | |
4068 | } | |
4069 | ||
4070 | /* Clear the fields that may not be matched. */ | |
4071 | wc = match->wc; | |
4072 | if (!(may_match & MAY_NW_ADDR)) { | |
4073 | wc.masks.nw_src = wc.masks.nw_dst = htonl(0); | |
4074 | } | |
4075 | if (!(may_match & MAY_TP_ADDR)) { | |
4076 | wc.masks.tp_src = wc.masks.tp_dst = htons(0); | |
4077 | } | |
4078 | if (!(may_match & MAY_NW_PROTO)) { | |
4079 | wc.masks.nw_proto = 0; | |
4080 | } | |
4081 | if (!(may_match & MAY_IPVx)) { | |
4082 | wc.masks.nw_tos = 0; | |
4083 | wc.masks.nw_ttl = 0; | |
4084 | } | |
4085 | if (!(may_match & MAY_ARP_SHA)) { | |
4086 | memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN); | |
4087 | } | |
4088 | if (!(may_match & MAY_ARP_THA)) { | |
4089 | memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN); | |
4090 | } | |
4091 | if (!(may_match & MAY_IPV6)) { | |
4092 | wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any; | |
4093 | wc.masks.ipv6_label = htonl(0); | |
4094 | } | |
4095 | if (!(may_match & MAY_ND_TARGET)) { | |
4096 | wc.masks.nd_target = in6addr_any; | |
4097 | } | |
4098 | ||
4099 | /* Log any changes. */ | |
4100 | if (!flow_wildcards_equal(&wc, &match->wc)) { | |
4101 | bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl); | |
4102 | char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL; | |
4103 | ||
4104 | match->wc = wc; | |
4105 | match_zero_wildcarded_fields(match); | |
4106 | ||
4107 | if (log) { | |
4108 | char *post = match_to_string(match, OFP_DEFAULT_PRIORITY); | |
4109 | VLOG_INFO("normalization changed ofp_match, details:"); | |
4110 | VLOG_INFO(" pre: %s", pre); | |
4111 | VLOG_INFO("post: %s", post); | |
4112 | free(pre); | |
4113 | free(post); | |
4114 | } | |
4115 | } | |
4116 | } | |
4117 | ||
4118 | /* "Normalizes" the wildcards in 'match'. That means: | |
4119 | * | |
4120 | * 1. If the type of level N is known, then only the valid fields for that | |
4121 | * level may be specified. For example, ARP does not have a TOS field, | |
4122 | * so nw_tos must be wildcarded if 'match' specifies an ARP flow. | |
4123 | * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and | |
4124 | * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an | |
4125 | * IPv4 flow. | |
4126 | * | |
4127 | * 2. If the type of level N is not known (or not understood by Open | |
4128 | * vSwitch), then no fields at all for that level may be specified. For | |
4129 | * example, Open vSwitch does not understand SCTP, an L4 protocol, so the | |
4130 | * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an | |
4131 | * SCTP flow. | |
4132 | * | |
4133 | * If this function changes 'match', it logs a rate-limited informational | |
4134 | * message. */ | |
4135 | void | |
4136 | ofputil_normalize_match(struct match *match) | |
4137 | { | |
4138 | ofputil_normalize_match__(match, true); | |
4139 | } | |
4140 | ||
4141 | /* Same as ofputil_normalize_match() without the logging. Thus, this function | |
4142 | * is suitable for a program's internal use, whereas ofputil_normalize_match() | |
4143 | * sense for use on flows received from elsewhere (so that a bug in the program | |
4144 | * that sent them can be reported and corrected). */ | |
4145 | void | |
4146 | ofputil_normalize_match_quiet(struct match *match) | |
4147 | { | |
4148 | ofputil_normalize_match__(match, false); | |
4149 | } | |
4150 | ||
4151 | /* Parses a key or a key-value pair from '*stringp'. | |
4152 | * | |
4153 | * On success: Stores the key into '*keyp'. Stores the value, if present, into | |
4154 | * '*valuep', otherwise an empty string. Advances '*stringp' past the end of | |
4155 | * the key-value pair, preparing it for another call. '*keyp' and '*valuep' | |
4156 | * are substrings of '*stringp' created by replacing some of its bytes by null | |
4157 | * terminators. Returns true. | |
4158 | * | |
4159 | * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to | |
4160 | * NULL and returns false. */ | |
4161 | bool | |
4162 | ofputil_parse_key_value(char **stringp, char **keyp, char **valuep) | |
4163 | { | |
4164 | char *pos, *key, *value; | |
4165 | size_t key_len; | |
4166 | ||
4167 | pos = *stringp; | |
4168 | pos += strspn(pos, ", \t\r\n"); | |
4169 | if (*pos == '\0') { | |
4170 | *keyp = *valuep = NULL; | |
4171 | return false; | |
4172 | } | |
4173 | ||
4174 | key = pos; | |
4175 | key_len = strcspn(pos, ":=(, \t\r\n"); | |
4176 | if (key[key_len] == ':' || key[key_len] == '=') { | |
4177 | /* The value can be separated by a colon. */ | |
4178 | size_t value_len; | |
4179 | ||
4180 | value = key + key_len + 1; | |
4181 | value_len = strcspn(value, ", \t\r\n"); | |
4182 | pos = value + value_len + (value[value_len] != '\0'); | |
4183 | value[value_len] = '\0'; | |
4184 | } else if (key[key_len] == '(') { | |
4185 | /* The value can be surrounded by balanced parentheses. The outermost | |
4186 | * set of parentheses is removed. */ | |
4187 | int level = 1; | |
4188 | size_t value_len; | |
4189 | ||
4190 | value = key + key_len + 1; | |
4191 | for (value_len = 0; level > 0; value_len++) { | |
4192 | switch (value[value_len]) { | |
4193 | case '\0': | |
4194 | level = 0; | |
4195 | break; | |
4196 | ||
4197 | case '(': | |
4198 | level++; | |
4199 | break; | |
4200 | ||
4201 | case ')': | |
4202 | level--; | |
4203 | break; | |
4204 | } | |
4205 | } | |
4206 | value[value_len - 1] = '\0'; | |
4207 | pos = value + value_len; | |
4208 | } else { | |
4209 | /* There might be no value at all. */ | |
4210 | value = key + key_len; /* Will become the empty string below. */ | |
4211 | pos = key + key_len + (key[key_len] != '\0'); | |
4212 | } | |
4213 | key[key_len] = '\0'; | |
4214 | ||
4215 | *stringp = pos; | |
4216 | *keyp = key; | |
4217 | *valuep = value; | |
4218 | return true; | |
4219 | } | |
4220 | ||
4221 | /* Encode a dump ports request for 'port', the encoded message | |
4222 | * will be fore Open Flow version 'ofp_version'. Returns message | |
4223 | * as a struct ofpbuf. Returns encoded message on success, NULL on error */ | |
4224 | struct ofpbuf * | |
4225 | ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port) | |
4226 | { | |
4227 | struct ofpbuf *request; | |
4228 | ||
4229 | switch (ofp_version) { | |
4230 | case OFP10_VERSION: { | |
4231 | struct ofp10_port_stats_request *req; | |
4232 | request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0); | |
4233 | req = ofpbuf_put_zeros(request, sizeof *req); | |
4234 | req->port_no = htons(port); | |
4235 | break; | |
4236 | } | |
4237 | case OFP11_VERSION: | |
4238 | case OFP12_VERSION: { | |
4239 | struct ofp11_port_stats_request *req; | |
4240 | request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0); | |
4241 | req = ofpbuf_put_zeros(request, sizeof *req); | |
4242 | req->port_no = ofputil_port_to_ofp11(port); | |
4243 | break; | |
4244 | } | |
4245 | default: | |
4246 | NOT_REACHED(); | |
4247 | } | |
4248 | ||
4249 | return request; | |
4250 | } | |
4251 | ||
4252 | static void | |
4253 | ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops, | |
4254 | struct ofp10_port_stats *ps10) | |
4255 | { | |
4256 | ps10->port_no = htons(ops->port_no); | |
4257 | memset(ps10->pad, 0, sizeof ps10->pad); | |
4258 | put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets)); | |
4259 | put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets)); | |
4260 | put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes)); | |
4261 | put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes)); | |
4262 | put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped)); | |
4263 | put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped)); | |
4264 | put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors)); | |
4265 | put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors)); | |
4266 | put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors)); | |
4267 | put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors)); | |
4268 | put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors)); | |
4269 | put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions)); | |
4270 | } | |
4271 | ||
4272 | static void | |
4273 | ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops, | |
4274 | struct ofp11_port_stats *ps11) | |
4275 | { | |
4276 | ps11->port_no = ofputil_port_to_ofp11(ops->port_no); | |
4277 | memset(ps11->pad, 0, sizeof ps11->pad); | |
4278 | ps11->rx_packets = htonll(ops->stats.rx_packets); | |
4279 | ps11->tx_packets = htonll(ops->stats.tx_packets); | |
4280 | ps11->rx_bytes = htonll(ops->stats.rx_bytes); | |
4281 | ps11->tx_bytes = htonll(ops->stats.tx_bytes); | |
4282 | ps11->rx_dropped = htonll(ops->stats.rx_dropped); | |
4283 | ps11->tx_dropped = htonll(ops->stats.tx_dropped); | |
4284 | ps11->rx_errors = htonll(ops->stats.rx_errors); | |
4285 | ps11->tx_errors = htonll(ops->stats.tx_errors); | |
4286 | ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors); | |
4287 | ps11->rx_over_err = htonll(ops->stats.rx_over_errors); | |
4288 | ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors); | |
4289 | ps11->collisions = htonll(ops->stats.collisions); | |
4290 | } | |
4291 | ||
4292 | /* Encode a ports stat for 'ops' and append it to 'replies'. */ | |
4293 | void | |
4294 | ofputil_append_port_stat(struct list *replies, | |
4295 | const struct ofputil_port_stats *ops) | |
4296 | { | |
4297 | struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); | |
4298 | struct ofp_header *oh = msg->data; | |
4299 | ||
4300 | switch ((enum ofp_version)oh->version) { | |
4301 | case OFP12_VERSION: | |
4302 | case OFP11_VERSION: { | |
4303 | struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
4304 | ofputil_port_stats_to_ofp11(ops, reply); | |
4305 | break; | |
4306 | } | |
4307 | ||
4308 | case OFP10_VERSION: { | |
4309 | struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply); | |
4310 | ofputil_port_stats_to_ofp10(ops, reply); | |
4311 | break; | |
4312 | } | |
4313 | ||
4314 | default: | |
4315 | NOT_REACHED(); | |
4316 | } | |
4317 | } | |
4318 | ||
4319 | static enum ofperr | |
4320 | ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops, | |
4321 | const struct ofp10_port_stats *ps10) | |
4322 | { | |
4323 | memset(ops, 0, sizeof *ops); | |
4324 | ||
4325 | ops->port_no = ntohs(ps10->port_no); | |
4326 | ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets)); | |
4327 | ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets)); | |
4328 | ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes)); | |
4329 | ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes)); | |
4330 | ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped)); | |
4331 | ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped)); | |
4332 | ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors)); | |
4333 | ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors)); | |
4334 | ops->stats.rx_frame_errors = | |
4335 | ntohll(get_32aligned_be64(&ps10->rx_frame_err)); | |
4336 | ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err)); | |
4337 | ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err)); | |
4338 | ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions)); | |
4339 | ||
4340 | return 0; | |
4341 | } | |
4342 | ||
4343 | static enum ofperr | |
4344 | ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops, | |
4345 | const struct ofp11_port_stats *ps11) | |
4346 | { | |
4347 | enum ofperr error; | |
4348 | ||
4349 | memset(ops, 0, sizeof *ops); | |
4350 | error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no); | |
4351 | if (error) { | |
4352 | return error; | |
4353 | } | |
4354 | ||
4355 | ops->stats.rx_packets = ntohll(ps11->rx_packets); | |
4356 | ops->stats.tx_packets = ntohll(ps11->tx_packets); | |
4357 | ops->stats.rx_bytes = ntohll(ps11->rx_bytes); | |
4358 | ops->stats.tx_bytes = ntohll(ps11->tx_bytes); | |
4359 | ops->stats.rx_dropped = ntohll(ps11->rx_dropped); | |
4360 | ops->stats.tx_dropped = ntohll(ps11->tx_dropped); | |
4361 | ops->stats.rx_errors = ntohll(ps11->rx_errors); | |
4362 | ops->stats.tx_errors = ntohll(ps11->tx_errors); | |
4363 | ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err); | |
4364 | ops->stats.rx_over_errors = ntohll(ps11->rx_over_err); | |
4365 | ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err); | |
4366 | ops->stats.collisions = ntohll(ps11->collisions); | |
4367 | ||
4368 | return 0; | |
4369 | } | |
4370 | ||
4371 | /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY | |
4372 | * message 'oh'. */ | |
4373 | size_t | |
4374 | ofputil_count_port_stats(const struct ofp_header *oh) | |
4375 | { | |
4376 | struct ofpbuf b; | |
4377 | ||
4378 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
4379 | ofpraw_pull_assert(&b); | |
4380 | ||
4381 | BUILD_ASSERT(sizeof(struct ofp10_port_stats) == | |
4382 | sizeof(struct ofp11_port_stats)); | |
4383 | return b.size / sizeof(struct ofp10_port_stats); | |
4384 | } | |
4385 | ||
4386 | /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract | |
4387 | * ofputil_port_stats in 'ps'. | |
4388 | * | |
4389 | * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow | |
4390 | * message. Calling this function multiple times for a single 'msg' iterates | |
4391 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
4392 | * null and not modify them between calls. | |
4393 | * | |
4394 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
4395 | * otherwise a positive errno value. */ | |
4396 | int | |
4397 | ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg) | |
4398 | { | |
4399 | enum ofperr error; | |
4400 | enum ofpraw raw; | |
4401 | ||
4402 | error = (msg->l2 | |
4403 | ? ofpraw_decode(&raw, msg->l2) | |
4404 | : ofpraw_pull(&raw, msg)); | |
4405 | if (error) { | |
4406 | return error; | |
4407 | } | |
4408 | ||
4409 | if (!msg->size) { | |
4410 | return EOF; | |
4411 | } else if (raw == OFPRAW_OFPST11_PORT_REPLY) { | |
4412 | const struct ofp11_port_stats *ps11; | |
4413 | ||
4414 | ps11 = ofpbuf_try_pull(msg, sizeof *ps11); | |
4415 | if (!ps11) { | |
4416 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover " | |
4417 | "bytes at end", msg->size); | |
4418 | return OFPERR_OFPBRC_BAD_LEN; | |
4419 | } | |
4420 | return ofputil_port_stats_from_ofp11(ps, ps11); | |
4421 | } else if (raw == OFPRAW_OFPST10_PORT_REPLY) { | |
4422 | const struct ofp10_port_stats *ps10; | |
4423 | ||
4424 | ps10 = ofpbuf_try_pull(msg, sizeof *ps10); | |
4425 | if (!ps10) { | |
4426 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover " | |
4427 | "bytes at end", msg->size); | |
4428 | return OFPERR_OFPBRC_BAD_LEN; | |
4429 | } | |
4430 | return ofputil_port_stats_from_ofp10(ps, ps10); | |
4431 | } else { | |
4432 | NOT_REACHED(); | |
4433 | } | |
4434 | ||
4435 | } | |
4436 | ||
4437 | /* Parse a port status request message into a 16 bit OpenFlow 1.0 | |
4438 | * port number and stores the latter in '*ofp10_port'. | |
4439 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
4440 | enum ofperr | |
4441 | ofputil_decode_port_stats_request(const struct ofp_header *request, | |
4442 | uint16_t *ofp10_port) | |
4443 | { | |
4444 | switch ((enum ofp_version)request->version) { | |
4445 | case OFP12_VERSION: | |
4446 | case OFP11_VERSION: { | |
4447 | const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request); | |
4448 | return ofputil_port_from_ofp11(psr11->port_no, ofp10_port); | |
4449 | } | |
4450 | ||
4451 | case OFP10_VERSION: { | |
4452 | const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request); | |
4453 | *ofp10_port = ntohs(psr10->port_no); | |
4454 | return 0; | |
4455 | } | |
4456 | ||
4457 | default: | |
4458 | NOT_REACHED(); | |
4459 | } | |
4460 | } | |
4461 | ||
4462 | /* Parse a queue status request message into 'oqsr'. | |
4463 | * Returns 0 if successful, otherwise an OFPERR_* number. */ | |
4464 | enum ofperr | |
4465 | ofputil_decode_queue_stats_request(const struct ofp_header *request, | |
4466 | struct ofputil_queue_stats_request *oqsr) | |
4467 | { | |
4468 | switch ((enum ofp_version)request->version) { | |
4469 | case OFP12_VERSION: | |
4470 | case OFP11_VERSION: { | |
4471 | const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request); | |
4472 | oqsr->queue_id = ntohl(qsr11->queue_id); | |
4473 | return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no); | |
4474 | } | |
4475 | ||
4476 | case OFP10_VERSION: { | |
4477 | const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request); | |
4478 | oqsr->queue_id = ntohl(qsr10->queue_id); | |
4479 | oqsr->port_no = ntohs(qsr10->port_no); | |
4480 | /* OF 1.0 uses OFPP_ALL for OFPP_ANY */ | |
4481 | if (oqsr->port_no == OFPP_ALL) { | |
4482 | oqsr->port_no = OFPP_ANY; | |
4483 | } | |
4484 | return 0; | |
4485 | } | |
4486 | ||
4487 | default: | |
4488 | NOT_REACHED(); | |
4489 | } | |
4490 | } | |
4491 | ||
4492 | /* Encode a queue statsrequest for 'oqsr', the encoded message | |
4493 | * will be fore Open Flow version 'ofp_version'. Returns message | |
4494 | * as a struct ofpbuf. Returns encoded message on success, NULL on error */ | |
4495 | struct ofpbuf * | |
4496 | ofputil_encode_queue_stats_request(enum ofp_version ofp_version, | |
4497 | const struct ofputil_queue_stats_request *oqsr) | |
4498 | { | |
4499 | struct ofpbuf *request; | |
4500 | ||
4501 | switch (ofp_version) { | |
4502 | case OFP11_VERSION: | |
4503 | case OFP12_VERSION: { | |
4504 | struct ofp11_queue_stats_request *req; | |
4505 | request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0); | |
4506 | req = ofpbuf_put_zeros(request, sizeof *req); | |
4507 | req->port_no = ofputil_port_to_ofp11(oqsr->port_no); | |
4508 | req->queue_id = htonl(oqsr->queue_id); | |
4509 | break; | |
4510 | } | |
4511 | case OFP10_VERSION: { | |
4512 | struct ofp10_queue_stats_request *req; | |
4513 | request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0); | |
4514 | req = ofpbuf_put_zeros(request, sizeof *req); | |
4515 | /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */ | |
4516 | req->port_no = htons(oqsr->port_no == OFPP_ANY | |
4517 | ? OFPP_ALL : oqsr->port_no); | |
4518 | req->queue_id = htonl(oqsr->queue_id); | |
4519 | break; | |
4520 | } | |
4521 | default: | |
4522 | NOT_REACHED(); | |
4523 | } | |
4524 | ||
4525 | return request; | |
4526 | } | |
4527 | ||
4528 | /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY | |
4529 | * message 'oh'. */ | |
4530 | size_t | |
4531 | ofputil_count_queue_stats(const struct ofp_header *oh) | |
4532 | { | |
4533 | struct ofpbuf b; | |
4534 | ||
4535 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
4536 | ofpraw_pull_assert(&b); | |
4537 | ||
4538 | BUILD_ASSERT(sizeof(struct ofp10_queue_stats) == | |
4539 | sizeof(struct ofp11_queue_stats)); | |
4540 | return b.size / sizeof(struct ofp10_queue_stats); | |
4541 | } | |
4542 | ||
4543 | static enum ofperr | |
4544 | ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs, | |
4545 | const struct ofp10_queue_stats *qs10) | |
4546 | { | |
4547 | oqs->port_no = ntohs(qs10->port_no); | |
4548 | oqs->queue_id = ntohl(qs10->queue_id); | |
4549 | oqs->stats.tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes)); | |
4550 | oqs->stats.tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets)); | |
4551 | oqs->stats.tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors)); | |
4552 | ||
4553 | return 0; | |
4554 | } | |
4555 | ||
4556 | static enum ofperr | |
4557 | ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs, | |
4558 | const struct ofp11_queue_stats *qs11) | |
4559 | { | |
4560 | enum ofperr error; | |
4561 | ||
4562 | error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no); | |
4563 | if (error) { | |
4564 | return error; | |
4565 | } | |
4566 | ||
4567 | oqs->queue_id = ntohl(qs11->queue_id); | |
4568 | oqs->stats.tx_bytes = ntohll(qs11->tx_bytes); | |
4569 | oqs->stats.tx_packets = ntohll(qs11->tx_packets); | |
4570 | oqs->stats.tx_errors = ntohll(qs11->tx_errors); | |
4571 | ||
4572 | return 0; | |
4573 | } | |
4574 | ||
4575 | /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract | |
4576 | * ofputil_queue_stats in 'qs'. | |
4577 | * | |
4578 | * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow | |
4579 | * message. Calling this function multiple times for a single 'msg' iterates | |
4580 | * through the replies. The caller must initially leave 'msg''s layer pointers | |
4581 | * null and not modify them between calls. | |
4582 | * | |
4583 | * Returns 0 if successful, EOF if no replies were left in this 'msg', | |
4584 | * otherwise a positive errno value. */ | |
4585 | int | |
4586 | ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg) | |
4587 | { | |
4588 | enum ofperr error; | |
4589 | enum ofpraw raw; | |
4590 | ||
4591 | error = (msg->l2 | |
4592 | ? ofpraw_decode(&raw, msg->l2) | |
4593 | : ofpraw_pull(&raw, msg)); | |
4594 | if (error) { | |
4595 | return error; | |
4596 | } | |
4597 | ||
4598 | if (!msg->size) { | |
4599 | return EOF; | |
4600 | } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) { | |
4601 | const struct ofp11_queue_stats *qs11; | |
4602 | ||
4603 | qs11 = ofpbuf_try_pull(msg, sizeof *qs11); | |
4604 | if (!qs11) { | |
4605 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover " | |
4606 | "bytes at end", msg->size); | |
4607 | return OFPERR_OFPBRC_BAD_LEN; | |
4608 | } | |
4609 | return ofputil_queue_stats_from_ofp11(qs, qs11); | |
4610 | } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) { | |
4611 | const struct ofp10_queue_stats *qs10; | |
4612 | ||
4613 | qs10 = ofpbuf_try_pull(msg, sizeof *qs10); | |
4614 | if (!qs10) { | |
4615 | VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover " | |
4616 | "bytes at end", msg->size); | |
4617 | return OFPERR_OFPBRC_BAD_LEN; | |
4618 | } | |
4619 | return ofputil_queue_stats_from_ofp10(qs, qs10); | |
4620 | } else { | |
4621 | NOT_REACHED(); | |
4622 | } | |
4623 | } | |
4624 | ||
4625 | static void | |
4626 | ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs, | |
4627 | struct ofp10_queue_stats *qs10) | |
4628 | { | |
4629 | qs10->port_no = htons(oqs->port_no); | |
4630 | memset(qs10->pad, 0, sizeof qs10->pad); | |
4631 | qs10->queue_id = htonl(oqs->queue_id); | |
4632 | put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->stats.tx_bytes)); | |
4633 | put_32aligned_be64(&qs10->tx_packets, htonll(oqs->stats.tx_packets)); | |
4634 | put_32aligned_be64(&qs10->tx_errors, htonll(oqs->stats.tx_errors)); | |
4635 | } | |
4636 | ||
4637 | static void | |
4638 | ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs, | |
4639 | struct ofp11_queue_stats *qs11) | |
4640 | { | |
4641 | qs11->port_no = ofputil_port_to_ofp11(oqs->port_no); | |
4642 | qs11->queue_id = htonl(oqs->queue_id); | |
4643 | qs11->tx_bytes = htonll(oqs->stats.tx_bytes); | |
4644 | qs11->tx_packets = htonll(oqs->stats.tx_packets); | |
4645 | qs11->tx_errors = htonll(oqs->stats.tx_errors); | |
4646 | } | |
4647 | ||
4648 | /* Encode a queue stat for 'oqs' and append it to 'replies'. */ | |
4649 | void | |
4650 | ofputil_append_queue_stat(struct list *replies, | |
4651 | const struct ofputil_queue_stats *oqs) | |
4652 | { | |
4653 | struct ofpbuf *msg = ofpbuf_from_list(list_back(replies)); | |
4654 | struct ofp_header *oh = msg->data; | |
4655 | ||
4656 | switch ((enum ofp_version)oh->version) { | |
4657 | case OFP12_VERSION: | |
4658 | case OFP11_VERSION: { | |
4659 | struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);; | |
4660 | ofputil_queue_stats_to_ofp11(oqs, reply); | |
4661 | break; | |
4662 | } | |
4663 | ||
4664 | case OFP10_VERSION: { | |
4665 | struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);; | |
4666 | ofputil_queue_stats_to_ofp10(oqs, reply); | |
4667 | break; | |
4668 | } | |
4669 | ||
4670 | default: | |
4671 | NOT_REACHED(); | |
4672 | } | |
4673 | } |