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