ofpbuf: Add ofpbuf_new_with_headroom(), ofpbuf_clone_with_headroom().
[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 /* Buffers are identified by a 32-bit opaque ID.  We divide the ID
32  * into a buffer number (low bits) and a cookie (high bits).  The buffer number
33  * is an index into an array of buffers.  The cookie distinguishes between
34  * different packets that have occupied a single buffer.  Thus, the more
35  * buffers we have, the lower-quality the cookie... */
36 #define PKTBUF_BITS     8
37 #define PKTBUF_MASK     (PKTBUF_CNT - 1)
38 #define PKTBUF_CNT      (1u << PKTBUF_BITS)
39
40 #define COOKIE_BITS     (32 - PKTBUF_BITS)
41 #define COOKIE_MAX      ((1u << COOKIE_BITS) - 1)
42
43 #define OVERWRITE_MSECS 5000
44
45 struct packet {
46     struct ofpbuf *buffer;
47     uint32_t cookie;
48     long long int timeout;
49     uint16_t in_port;
50 };
51
52 struct pktbuf {
53     struct packet packets[PKTBUF_CNT];
54     unsigned int buffer_idx;
55     unsigned int null_idx;
56 };
57
58 int
59 pktbuf_capacity(void)
60 {
61     return PKTBUF_CNT;
62 }
63
64 struct pktbuf *
65 pktbuf_create(void)
66 {
67     return xzalloc(sizeof *pktbuf_create());
68 }
69
70 void
71 pktbuf_destroy(struct pktbuf *pb)
72 {
73     if (pb) {
74         size_t i;
75
76         for (i = 0; i < PKTBUF_CNT; i++) {
77             ofpbuf_delete(pb->packets[i].buffer);
78         }
79         free(pb);
80     }
81 }
82
83 static unsigned int
84 make_id(unsigned int buffer_idx, unsigned int cookie)
85 {
86     return buffer_idx | (cookie << PKTBUF_BITS);
87 }
88
89 /* Attempts to allocate an OpenFlow packet buffer id within 'pb'.  The packet
90  * buffer will store a copy of 'buffer' and the port number 'in_port', which
91  * should be the datapath port number on which 'buffer' was received.
92  *
93  * If successful, returns the packet buffer id (a number other than
94  * UINT32_MAX).  pktbuf_retrieve() can later be used to retrieve the buffer and
95  * its input port number (buffers do expire after a time, so this is not
96  * guaranteed to be true forever).  On failure, returns UINT32_MAX.
97  *
98  * The caller retains ownership of 'buffer'. */
99 uint32_t
100 pktbuf_save(struct pktbuf *pb, struct ofpbuf *buffer, uint16_t in_port)
101 {
102     struct packet *p = &pb->packets[pb->buffer_idx];
103     pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
104     if (p->buffer) {
105         if (time_msec() < p->timeout) {
106             return UINT32_MAX;
107         }
108         ofpbuf_delete(p->buffer);
109     }
110
111     /* Don't use maximum cookie value since all-1-bits ID is special. */
112     if (++p->cookie >= COOKIE_MAX) {
113         p->cookie = 0;
114     }
115     p->buffer = ofpbuf_new_with_headroom(buffer->size,
116                                          sizeof(struct ofp_packet_in));
117     ofpbuf_put(p->buffer, buffer->data, buffer->size);
118     p->timeout = time_msec() + OVERWRITE_MSECS;
119     p->in_port = in_port;
120     return make_id(p - pb->packets, p->cookie);
121 }
122
123 /*
124  * Allocates and returns a "null" packet buffer id.  The returned packet buffer
125  * id is considered valid by pktbuf_retrieve(), but it is not associated with
126  * actual buffered data.
127  *
128  * This function is always successful.
129  *
130  * This is useful in one special case: with the current OpenFlow design, the
131  * "fail-open" code cannot always know whether a connection to a controller is
132  * actually valid until it receives a OFPT_PACKET_OUT or OFPT_FLOW_MOD request,
133  * but at that point the packet in question has already been forwarded (since
134  * we are still in "fail-open" mode).  If the packet was buffered in the usual
135  * way, then the OFPT_PACKET_OUT or OFPT_FLOW_MOD would cause a duplicate
136  * packet in the network.  Null packet buffer ids identify such a packet that
137  * has already been forwarded, so that Open vSwitch can quietly ignore the
138  * request to re-send it.  (After that happens, the switch exits fail-open
139  * mode.)
140  *
141  * See the top-level comment in fail-open.c for an overview.
142  */
143 uint32_t
144 pktbuf_get_null(void)
145 {
146     return make_id(0, COOKIE_MAX);
147 }
148
149 /* Attempts to retrieve a saved packet with the given 'id' from 'pb'.  Returns
150  * 0 if successful, otherwise an OpenFlow error code constructed with
151  * ofp_mkerr().
152  *
153  * On success, ordinarily stores the buffered packet in '*bufferp' and the
154  * datapath port number on which the packet was received in '*in_port'.  The
155  * caller becomes responsible for freeing the buffer.  However, if 'id'
156  * identifies a "null" packet buffer (created with pktbuf_get_null()), stores
157  * NULL in '*bufferp' and UINT16_max in '*in_port'.
158  *
159  * A returned packet will have at least sizeof(struct ofp_packet_in) bytes of
160  * headroom.
161  *
162  * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
163 int
164 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
165                 uint16_t *in_port)
166 {
167     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
168     struct packet *p;
169     int error;
170
171     if (!pb) {
172         VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
173                      "without buffers");
174         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN);
175     }
176
177     p = &pb->packets[id & PKTBUF_MASK];
178     if (p->cookie == id >> PKTBUF_BITS) {
179         struct ofpbuf *buffer = p->buffer;
180         if (buffer) {
181             *bufferp = buffer;
182             *in_port = p->in_port;
183             p->buffer = NULL;
184             COVERAGE_INC(pktbuf_retrieved);
185             return 0;
186         } else {
187             COVERAGE_INC(pktbuf_reuse_error);
188             VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
189             error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY);
190         }
191     } else if (id >> PKTBUF_BITS != COOKIE_MAX) {
192         COVERAGE_INC(pktbuf_buffer_unknown);
193         VLOG_WARN_RL(&rl, "cookie mismatch: %08"PRIx32" != %08"PRIx32,
194                      id, (id & PKTBUF_MASK) | (p->cookie << PKTBUF_BITS));
195         error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN);
196     } else {
197         COVERAGE_INC(pktbuf_null_cookie);
198         VLOG_INFO_RL(&rl, "Received null cookie %08"PRIx32" (this is normal "
199                      "if the switch was recently in fail-open mode)", id);
200         error = 0;
201     }
202     *bufferp = NULL;
203     *in_port = UINT16_MAX;
204     return error;
205 }
206
207 void
208 pktbuf_discard(struct pktbuf *pb, uint32_t id)
209 {
210     struct packet *p = &pb->packets[id & PKTBUF_MASK];
211     if (p->cookie == id >> PKTBUF_BITS) {
212         ofpbuf_delete(p->buffer);
213         p->buffer = NULL;
214     }
215 }