2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "openvswitch/ofp-util.h"
23 #include "dp-packet.h"
26 #include "openvswitch/vconn.h"
27 #include "openvswitch/vlog.h"
29 VLOG_DEFINE_THIS_MODULE(pktbuf);
31 COVERAGE_DEFINE(pktbuf_buffer_unknown);
32 COVERAGE_DEFINE(pktbuf_retrieved);
33 COVERAGE_DEFINE(pktbuf_reuse_error);
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... */
41 #define PKTBUF_MASK (PKTBUF_CNT - 1)
42 #define PKTBUF_CNT (1u << PKTBUF_BITS)
44 #define COOKIE_BITS (32 - PKTBUF_BITS)
45 #define COOKIE_MAX ((1u << COOKIE_BITS) - 1)
47 #define OVERWRITE_MSECS 5000
50 struct dp_packet *buffer;
52 long long int timeout;
57 struct packet packets[PKTBUF_CNT];
58 unsigned int buffer_idx;
59 unsigned int null_idx;
71 return xzalloc(sizeof *pktbuf_create());
75 pktbuf_destroy(struct pktbuf *pb)
80 for (i = 0; i < PKTBUF_CNT; i++) {
81 dp_packet_delete(pb->packets[i].buffer);
88 make_id(unsigned int buffer_idx, unsigned int cookie)
90 return buffer_idx | (cookie << PKTBUF_BITS);
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'
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.
103 * The caller retains ownership of 'buffer'. */
105 pktbuf_save(struct pktbuf *pb, const void *buffer, size_t buffer_size,
108 struct packet *p = &pb->packets[pb->buffer_idx];
109 pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
111 if (time_msec() < p->timeout) {
114 dp_packet_delete(p->buffer);
117 /* Don't use maximum cookie value since all-1-bits ID is special. */
118 if (++p->cookie >= COOKIE_MAX) {
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);
125 p->timeout = time_msec() + OVERWRITE_MSECS;
126 p->in_port = in_port;
127 return make_id(p - pb->packets, p->cookie);
130 /* Attempts to retrieve a saved packet with the given 'id' from 'pb'. Returns
131 * 0 if successful, otherwise an OpenFlow error code.
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.
137 * 'in_port' may be NULL if the input port is not of interest.
139 * The L3 header of a returned packet will be 32-bit aligned.
141 * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
143 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct dp_packet **bufferp,
146 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
150 if (id == UINT32_MAX) {
156 VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
158 error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
162 p = &pb->packets[id & PKTBUF_MASK];
163 if (p->cookie == id >> PKTBUF_BITS) {
164 struct dp_packet *buffer = p->buffer;
168 *in_port = p->in_port;
171 COVERAGE_INC(pktbuf_retrieved);
174 COVERAGE_INC(pktbuf_reuse_error);
175 VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
176 error = OFPERR_OFPBRC_BUFFER_EMPTY;
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;
187 *in_port = OFPP_NONE;
193 pktbuf_discard(struct pktbuf *pb, uint32_t id)
195 struct packet *p = &pb->packets[id & PKTBUF_MASK];
196 if (p->cookie == id >> PKTBUF_BITS) {
197 dp_packet_delete(p->buffer);
202 /* Returns the number of packets buffered in 'pb'. Returns 0 if 'pb' is
205 pktbuf_count_packets(const struct pktbuf *pb)
212 for (i = 0; i < PKTBUF_CNT; i++) {
213 if (pb->packets[i].buffer) {