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