]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/pktbuf.c
ovs-ofctl: Handle any number of buckets in group statistics
[mirror_ovs.git] / ofproto / pktbuf.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "pktbuf.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include "coverage.h"
22 #include "ofp-util.h"
23 #include "ofpbuf.h"
24 #include "timeval.h"
25 #include "util.h"
26 #include "vconn.h"
27 #include "vlog.h"
28
29 VLOG_DEFINE_THIS_MODULE(pktbuf);
30
31 COVERAGE_DEFINE(pktbuf_buffer_unknown);
32 COVERAGE_DEFINE(pktbuf_null_cookie);
33 COVERAGE_DEFINE(pktbuf_retrieved);
34 COVERAGE_DEFINE(pktbuf_reuse_error);
35
36 /* Buffers are identified by a 32-bit opaque ID. We divide the ID
37 * into a buffer number (low bits) and a cookie (high bits). The buffer number
38 * is an index into an array of buffers. The cookie distinguishes between
39 * different packets that have occupied a single buffer. Thus, the more
40 * buffers we have, the lower-quality the cookie... */
41 #define PKTBUF_BITS 8
42 #define PKTBUF_MASK (PKTBUF_CNT - 1)
43 #define PKTBUF_CNT (1u << PKTBUF_BITS)
44
45 #define COOKIE_BITS (32 - PKTBUF_BITS)
46 #define COOKIE_MAX ((1u << COOKIE_BITS) - 1)
47
48 #define OVERWRITE_MSECS 5000
49
50 struct packet {
51 struct ofpbuf *buffer;
52 uint32_t cookie;
53 long long int timeout;
54 ofp_port_t in_port;
55 };
56
57 struct pktbuf {
58 struct packet packets[PKTBUF_CNT];
59 unsigned int buffer_idx;
60 unsigned int null_idx;
61 };
62
63 int
64 pktbuf_capacity(void)
65 {
66 return PKTBUF_CNT;
67 }
68
69 struct pktbuf *
70 pktbuf_create(void)
71 {
72 return xzalloc(sizeof *pktbuf_create());
73 }
74
75 void
76 pktbuf_destroy(struct pktbuf *pb)
77 {
78 if (pb) {
79 size_t i;
80
81 for (i = 0; i < PKTBUF_CNT; i++) {
82 ofpbuf_delete(pb->packets[i].buffer);
83 }
84 free(pb);
85 }
86 }
87
88 static unsigned int
89 make_id(unsigned int buffer_idx, unsigned int cookie)
90 {
91 return buffer_idx | (cookie << PKTBUF_BITS);
92 }
93
94 /* Attempts to allocate an OpenFlow packet buffer id within 'pb'. The packet
95 * buffer will store a copy of 'buffer_size' bytes in 'buffer' and the port
96 * number 'in_port', which should be the OpenFlow port number on which 'buffer'
97 * was received.
98 *
99 * If successful, returns the packet buffer id (a number other than
100 * UINT32_MAX). pktbuf_retrieve() can later be used to retrieve the buffer and
101 * its input port number (buffers do expire after a time, so this is not
102 * guaranteed to be true forever). On failure, returns UINT32_MAX.
103 *
104 * The caller retains ownership of 'buffer'. */
105 uint32_t
106 pktbuf_save(struct pktbuf *pb, const void *buffer, size_t buffer_size,
107 ofp_port_t in_port)
108 {
109 struct packet *p = &pb->packets[pb->buffer_idx];
110 pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
111 if (p->buffer) {
112 if (time_msec() < p->timeout) {
113 return UINT32_MAX;
114 }
115 ofpbuf_delete(p->buffer);
116 }
117
118 /* Don't use maximum cookie value since all-1-bits ID is special. */
119 if (++p->cookie >= COOKIE_MAX) {
120 p->cookie = 0;
121 }
122
123 /* Use 2 bytes of headroom to 32-bit align the L3 header. */
124 p->buffer = ofpbuf_clone_data_with_headroom(buffer, buffer_size, 2);
125
126 p->timeout = time_msec() + OVERWRITE_MSECS;
127 p->in_port = in_port;
128 return make_id(p - pb->packets, p->cookie);
129 }
130
131 /*
132 * Allocates and returns a "null" packet buffer id. The returned packet buffer
133 * id is considered valid by pktbuf_retrieve(), but it is not associated with
134 * actual buffered data.
135 *
136 * This function is always successful.
137 *
138 * This is useful in one special case: with the current OpenFlow design, the
139 * "fail-open" code cannot always know whether a connection to a controller is
140 * actually valid until it receives a OFPT_PACKET_OUT or OFPT_FLOW_MOD request,
141 * but at that point the packet in question has already been forwarded (since
142 * we are still in "fail-open" mode). If the packet was buffered in the usual
143 * way, then the OFPT_PACKET_OUT or OFPT_FLOW_MOD would cause a duplicate
144 * packet in the network. Null packet buffer ids identify such a packet that
145 * has already been forwarded, so that Open vSwitch can quietly ignore the
146 * request to re-send it. (After that happens, the switch exits fail-open
147 * mode.)
148 *
149 * See the top-level comment in fail-open.c for an overview.
150 */
151 uint32_t
152 pktbuf_get_null(void)
153 {
154 return make_id(0, COOKIE_MAX);
155 }
156
157 /* Attempts to retrieve a saved packet with the given 'id' from 'pb'. Returns
158 * 0 if successful, otherwise an OpenFlow error code.
159 *
160 * On success, ordinarily stores the buffered packet in '*bufferp' and the
161 * OpenFlow port number on which the packet was received in '*in_port'. The
162 * caller becomes responsible for freeing the buffer. However, if 'id'
163 * identifies a "null" packet buffer (created with pktbuf_get_null()), stores
164 * NULL in '*bufferp' and OFPP_NONE in '*in_port'.
165 *
166 * 'in_port' may be NULL if the input port is not of interest.
167 *
168 * The L3 header of a returned packet will be 32-bit aligned.
169 *
170 * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
171 enum ofperr
172 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
173 ofp_port_t *in_port)
174 {
175 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
176 struct packet *p;
177 enum ofperr error;
178
179 if (id == UINT32_MAX) {
180 error = 0;
181 goto error;
182 }
183
184 if (!pb) {
185 VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
186 "without buffers");
187 return OFPERR_OFPBRC_BUFFER_UNKNOWN;
188 }
189
190 p = &pb->packets[id & PKTBUF_MASK];
191 if (p->cookie == id >> PKTBUF_BITS) {
192 struct ofpbuf *buffer = p->buffer;
193 if (buffer) {
194 *bufferp = buffer;
195 if (in_port) {
196 *in_port = p->in_port;
197 }
198 p->buffer = NULL;
199 COVERAGE_INC(pktbuf_retrieved);
200 return 0;
201 } else {
202 COVERAGE_INC(pktbuf_reuse_error);
203 VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
204 error = OFPERR_OFPBRC_BUFFER_EMPTY;
205 }
206 } else if (id >> PKTBUF_BITS != COOKIE_MAX) {
207 COVERAGE_INC(pktbuf_buffer_unknown);
208 VLOG_WARN_RL(&rl, "cookie mismatch: %08"PRIx32" != %08"PRIx32,
209 id, (id & PKTBUF_MASK) | (p->cookie << PKTBUF_BITS));
210 error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
211 } else {
212 COVERAGE_INC(pktbuf_null_cookie);
213 VLOG_INFO_RL(&rl, "Received null cookie %08"PRIx32" (this is normal "
214 "if the switch was recently in fail-open mode)", id);
215 error = 0;
216 }
217 error:
218 *bufferp = NULL;
219 if (in_port) {
220 *in_port = OFPP_NONE;
221 }
222 return error;
223 }
224
225 void
226 pktbuf_discard(struct pktbuf *pb, uint32_t id)
227 {
228 struct packet *p = &pb->packets[id & PKTBUF_MASK];
229 if (p->cookie == id >> PKTBUF_BITS) {
230 ofpbuf_delete(p->buffer);
231 p->buffer = NULL;
232 }
233 }
234
235 /* Returns the number of packets buffered in 'pb'. Returns 0 if 'pb' is
236 * null. */
237 unsigned int
238 pktbuf_count_packets(const struct pktbuf *pb)
239 {
240 int n = 0;
241
242 if (pb) {
243 int i;
244
245 for (i = 0; i < PKTBUF_CNT; i++) {
246 if (pb->packets[i].buffer) {
247 n++;
248 }
249 }
250 }
251
252 return n;
253 }