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