netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / pktbuf.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 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 "dp-packet.h"
24 #include "timeval.h"
25 #include "util.h"
26 #include "openvswitch/vconn.h"
27 #include "openvswitch/vlog.h"
28
29 VLOG_DEFINE_THIS_MODULE(pktbuf);
30
31 COVERAGE_DEFINE(pktbuf_buffer_unknown);
32 COVERAGE_DEFINE(pktbuf_retrieved);
33 COVERAGE_DEFINE(pktbuf_reuse_error);
34
35 /* Buffers are identified by a 32-bit opaque ID.  We divide the ID
36  * into a buffer number (low bits) and a cookie (high bits).  The buffer number
37  * is an index into an array of buffers.  The cookie distinguishes between
38  * different packets that have occupied a single buffer.  Thus, the more
39  * buffers we have, the lower-quality the cookie... */
40 #define PKTBUF_BITS     8
41 #define PKTBUF_MASK     (PKTBUF_CNT - 1)
42 #define PKTBUF_CNT      (1u << PKTBUF_BITS)
43
44 #define COOKIE_BITS     (32 - PKTBUF_BITS)
45 #define COOKIE_MAX      ((1u << COOKIE_BITS) - 1)
46
47 #define OVERWRITE_MSECS 5000
48
49 struct packet {
50     struct dp_packet *buffer;
51     uint32_t cookie;
52     long long int timeout;
53     ofp_port_t in_port;
54 };
55
56 struct pktbuf {
57     struct packet packets[PKTBUF_CNT];
58     unsigned int buffer_idx;
59     unsigned int null_idx;
60 };
61
62 int
63 pktbuf_capacity(void)
64 {
65     return PKTBUF_CNT;
66 }
67
68 struct pktbuf *
69 pktbuf_create(void)
70 {
71     return xzalloc(sizeof *pktbuf_create());
72 }
73
74 void
75 pktbuf_destroy(struct pktbuf *pb)
76 {
77     if (pb) {
78         size_t i;
79
80         for (i = 0; i < PKTBUF_CNT; i++) {
81             dp_packet_delete(pb->packets[i].buffer);
82         }
83         free(pb);
84     }
85 }
86
87 static unsigned int
88 make_id(unsigned int buffer_idx, unsigned int cookie)
89 {
90     return buffer_idx | (cookie << PKTBUF_BITS);
91 }
92
93 /* Attempts to allocate an OpenFlow packet buffer id within 'pb'.  The packet
94  * buffer will store a copy of 'buffer_size' bytes in 'buffer' and the port
95  * number 'in_port', which should be the OpenFlow port number on which 'buffer'
96  * 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, const void *buffer, size_t buffer_size,
106             ofp_port_t in_port)
107 {
108     struct packet *p = &pb->packets[pb->buffer_idx];
109     pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
110     if (p->buffer) {
111         if (time_msec() < p->timeout) {
112             return UINT32_MAX;
113         }
114         dp_packet_delete(p->buffer);
115     }
116
117     /* Don't use maximum cookie value since all-1-bits ID is special. */
118     if (++p->cookie >= COOKIE_MAX) {
119         p->cookie = 0;
120     }
121
122     /* Use 2 bytes of headroom to 32-bit align the L3 header. */
123     p->buffer = dp_packet_clone_data_with_headroom(buffer, buffer_size, 2);
124
125     p->timeout = time_msec() + OVERWRITE_MSECS;
126     p->in_port = in_port;
127     return make_id(p - pb->packets, p->cookie);
128 }
129
130 /* Attempts to retrieve a saved packet with the given 'id' from 'pb'.  Returns
131  * 0 if successful, otherwise an OpenFlow error code.
132  *
133  * On success, stores the buffered packet in '*bufferp' and the OpenFlow port
134  * number on which the packet was received in '*in_port'.  The caller becomes
135  * responsible for freeing the buffer.
136  *
137  * 'in_port' may be NULL if the input port is not of interest.
138  *
139  * The L3 header of a returned packet will be 32-bit aligned.
140  *
141  * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
142 enum ofperr
143 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct dp_packet **bufferp,
144                 ofp_port_t *in_port)
145 {
146     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
147     struct packet *p;
148     enum ofperr error;
149
150     if (id == UINT32_MAX) {
151         error = 0;
152         goto error;
153     }
154
155     if (!pb) {
156         VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
157                      "without buffers");
158         error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
159         goto error;
160     }
161
162     p = &pb->packets[id & PKTBUF_MASK];
163     if (p->cookie == id >> PKTBUF_BITS) {
164         struct dp_packet *buffer = p->buffer;
165         if (buffer) {
166             *bufferp = buffer;
167             if (in_port) {
168                 *in_port = p->in_port;
169             }
170             p->buffer = NULL;
171             COVERAGE_INC(pktbuf_retrieved);
172             return 0;
173         } else {
174             COVERAGE_INC(pktbuf_reuse_error);
175             VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
176             error = OFPERR_OFPBRC_BUFFER_EMPTY;
177         }
178     } else {
179         COVERAGE_INC(pktbuf_buffer_unknown);
180         VLOG_WARN_RL(&rl, "cookie mismatch: %08"PRIx32" != %08"PRIx32,
181                      id, (id & PKTBUF_MASK) | (p->cookie << PKTBUF_BITS));
182         error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
183     }
184 error:
185     *bufferp = NULL;
186     if (in_port) {
187         *in_port = OFPP_NONE;
188     }
189     return error;
190 }
191
192 void
193 pktbuf_discard(struct pktbuf *pb, uint32_t id)
194 {
195     struct packet *p = &pb->packets[id & PKTBUF_MASK];
196     if (p->cookie == id >> PKTBUF_BITS) {
197         dp_packet_delete(p->buffer);
198         p->buffer = NULL;
199     }
200 }
201
202 /* Returns the number of packets buffered in 'pb'.  Returns 0 if 'pb' is
203  * null. */
204 unsigned int
205 pktbuf_count_packets(const struct pktbuf *pb)
206 {
207     int n = 0;
208
209     if (pb) {
210         int i;
211
212         for (i = 0; i < PKTBUF_CNT; i++) {
213             if (pb->packets[i].buffer) {
214                 n++;
215             }
216         }
217     }
218
219     return n;
220 }