2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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.
24 #include "openflow/openflow.h"
30 VLOG_DEFINE_THIS_MODULE(pktbuf)
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... */
38 #define PKTBUF_MASK (PKTBUF_CNT - 1)
39 #define PKTBUF_CNT (1u << PKTBUF_BITS)
41 #define COOKIE_BITS (32 - PKTBUF_BITS)
42 #define COOKIE_MAX ((1u << COOKIE_BITS) - 1)
44 #define OVERWRITE_MSECS 5000
47 struct ofpbuf *buffer;
49 long long int timeout;
54 struct packet packets[PKTBUF_CNT];
55 unsigned int buffer_idx;
56 unsigned int null_idx;
68 return xzalloc(sizeof *pktbuf_create());
72 pktbuf_destroy(struct pktbuf *pb)
77 for (i = 0; i < PKTBUF_CNT; i++) {
78 ofpbuf_delete(pb->packets[i].buffer);
85 make_id(unsigned int buffer_idx, unsigned int cookie)
87 return buffer_idx | (cookie << PKTBUF_BITS);
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.
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.
99 * The caller retains ownership of 'buffer'. */
101 pktbuf_save(struct pktbuf *pb, struct ofpbuf *buffer, uint16_t in_port)
103 struct packet *p = &pb->packets[pb->buffer_idx];
104 pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
106 if (time_msec() < p->timeout) {
109 ofpbuf_delete(p->buffer);
112 /* Don't use maximum cookie value since all-1-bits ID is special. */
113 if (++p->cookie >= COOKIE_MAX) {
116 p->buffer = ofpbuf_clone(buffer);
117 p->timeout = time_msec() + OVERWRITE_MSECS;
118 p->in_port = in_port;
119 return make_id(p - pb->packets, p->cookie);
123 * Allocates and returns a "null" packet buffer id. The returned packet buffer
124 * id is considered valid by pktbuf_retrieve(), but it is not associated with
125 * actual buffered data.
127 * This function is always successful.
129 * This is useful in one special case: with the current OpenFlow design, the
130 * "fail-open" code cannot always know whether a connection to a controller is
131 * actually valid until it receives a OFPT_PACKET_OUT or OFPT_FLOW_MOD request,
132 * but at that point the packet in question has already been forwarded (since
133 * we are still in "fail-open" mode). If the packet was buffered in the usual
134 * way, then the OFPT_PACKET_OUT or OFPT_FLOW_MOD would cause a duplicate
135 * packet in the network. Null packet buffer ids identify such a packet that
136 * has already been forwarded, so that Open vSwitch can quietly ignore the
137 * request to re-send it. (After that happens, the switch exits fail-open
140 * See the top-level comment in fail-open.c for an overview.
143 pktbuf_get_null(void)
145 return make_id(0, COOKIE_MAX);
148 /* Attempts to retrieve a saved packet with the given 'id' from 'pb'. Returns
149 * 0 if successful, otherwise an OpenFlow error code constructed with
152 * On success, ordinarily stores the buffered packet in '*bufferp' and the
153 * datapath port number on which the packet was received in '*in_port'. The
154 * caller becomes responsible for freeing the buffer. However, if 'id'
155 * identifies a "null" packet buffer (created with pktbuf_get_null()), stores
156 * NULL in '*bufferp' and OFPP_NONE in '*in_port'.
158 * On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
160 pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct ofpbuf **bufferp,
163 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
168 VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
170 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN);
173 p = &pb->packets[id & PKTBUF_MASK];
174 if (p->cookie == id >> PKTBUF_BITS) {
175 struct ofpbuf *buffer = p->buffer;
178 *in_port = p->in_port;
180 COVERAGE_INC(pktbuf_retrieved);
183 COVERAGE_INC(pktbuf_reuse_error);
184 VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
185 error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY);
187 } else if (id >> PKTBUF_BITS != COOKIE_MAX) {
188 COVERAGE_INC(pktbuf_buffer_unknown);
189 VLOG_WARN_RL(&rl, "cookie mismatch: %08"PRIx32" != %08"PRIx32,
190 id, (id & PKTBUF_MASK) | (p->cookie << PKTBUF_BITS));
191 error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN);
193 COVERAGE_INC(pktbuf_null_cookie);
194 VLOG_INFO_RL(&rl, "Received null cookie %08"PRIx32" (this is normal "
195 "if the switch was recently in fail-open mode)", id);
199 *in_port = OFPP_NONE;
204 pktbuf_discard(struct pktbuf *pb, uint32_t id)
206 struct packet *p = &pb->packets[id & PKTBUF_MASK];
207 if (p->cookie == id >> PKTBUF_BITS) {
208 ofpbuf_delete(p->buffer);