Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / ofproto / pktbuf.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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     uint16_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' and the port number 'in_port', which
96  * should be the datapath port number on which 'buffer' was received.
97  *
98  * If successful, returns the packet buffer id (a number other than
99  * UINT32_MAX).  pktbuf_retrieve() can later be used to retrieve the buffer and
100  * its input port number (buffers do expire after a time, so this is not
101  * guaranteed to be true forever).  On failure, returns UINT32_MAX.
102  *
103  * The caller retains ownership of 'buffer'. */
104 uint32_t
105 pktbuf_save(struct pktbuf *pb, struct ofpbuf *buffer, uint16_t in_port)
106 {
107     struct packet *p = &pb->packets[pb->buffer_idx];
108     pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
109     if (p->buffer) {
110         if (time_msec() < p->timeout) {
111             return UINT32_MAX;
112         }
113         ofpbuf_delete(p->buffer);
114     }
115
116     /* Don't use maximum cookie value since all-1-bits ID is special. */
117     if (++p->cookie >= COOKIE_MAX) {
118         p->cookie = 0;
119     }
120     p->buffer = ofpbuf_new_with_headroom(buffer->size,
121                                          sizeof(struct ofp_packet_in));
122     ofpbuf_put(p->buffer, buffer->data, buffer->size);
123     p->timeout = time_msec() + OVERWRITE_MSECS;
124     p->in_port = in_port;
125     return make_id(p - pb->packets, p->cookie);
126 }
127
128 /*
129  * Allocates and returns a "null" packet buffer id.  The returned packet buffer
130  * id is considered valid by pktbuf_retrieve(), but it is not associated with
131  * actual buffered data.
132  *
133  * This function is always successful.
134  *
135  * This is useful in one special case: with the current OpenFlow design, the
136  * "fail-open" code cannot always know whether a connection to a controller is
137  * actually valid until it receives a OFPT_PACKET_OUT or OFPT_FLOW_MOD request,
138  * but at that point the packet in question has already been forwarded (since
139  * we are still in "fail-open" mode).  If the packet was buffered in the usual
140  * way, then the OFPT_PACKET_OUT or OFPT_FLOW_MOD would cause a duplicate
141  * packet in the network.  Null packet buffer ids identify such a packet that
142  * has already been forwarded, so that Open vSwitch can quietly ignore the
143  * request to re-send it.  (After that happens, the switch exits fail-open
144  * mode.)
145  *
146  * See the top-level comment in fail-open.c for an overview.
147  */
148 uint32_t
149 pktbuf_get_null(void)
150 {
151     return make_id(0, COOKIE_MAX);
152 }
153
154 /* Attempts to retrieve a saved packet with the given 'id' from 'pb'.  Returns
155  * 0 if successful, otherwise an OpenFlow error code constructed with
156  * ofp_mkerr().
157  *
158  * On success, ordinarily stores the buffered packet in '*bufferp' and the
159  * datapath port number on which the packet was received in '*in_port'.  The
160  * caller becomes responsible for freeing the buffer.  However, if 'id'
161  * identifies a "null" packet buffer (created with pktbuf_get_null()), stores
162  * NULL in '*bufferp' and UINT16_max in '*in_port'.
163  *
164  * A returned packet will have at least sizeof(struct ofp_packet_in) bytes of
165  * headroom.
166  *
167  * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
168 int
169 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
170                 uint16_t *in_port)
171 {
172     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
173     struct packet *p;
174     int error;
175
176     if (!pb) {
177         VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
178                      "without buffers");
179         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN);
180     }
181
182     p = &pb->packets[id & PKTBUF_MASK];
183     if (p->cookie == id >> PKTBUF_BITS) {
184         struct ofpbuf *buffer = p->buffer;
185         if (buffer) {
186             *bufferp = buffer;
187             *in_port = p->in_port;
188             p->buffer = NULL;
189             COVERAGE_INC(pktbuf_retrieved);
190             return 0;
191         } else {
192             COVERAGE_INC(pktbuf_reuse_error);
193             VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
194             error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY);
195         }
196     } else if (id >> PKTBUF_BITS != COOKIE_MAX) {
197         COVERAGE_INC(pktbuf_buffer_unknown);
198         VLOG_WARN_RL(&rl, "cookie mismatch: %08"PRIx32" != %08"PRIx32,
199                      id, (id & PKTBUF_MASK) | (p->cookie << PKTBUF_BITS));
200         error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN);
201     } else {
202         COVERAGE_INC(pktbuf_null_cookie);
203         VLOG_INFO_RL(&rl, "Received null cookie %08"PRIx32" (this is normal "
204                      "if the switch was recently in fail-open mode)", id);
205         error = 0;
206     }
207     *bufferp = NULL;
208     *in_port = UINT16_MAX;
209     return error;
210 }
211
212 void
213 pktbuf_discard(struct pktbuf *pb, uint32_t id)
214 {
215     struct packet *p = &pb->packets[id & PKTBUF_MASK];
216     if (p->cookie == id >> PKTBUF_BITS) {
217         ofpbuf_delete(p->buffer);
218         p->buffer = NULL;
219     }
220 }