2009-07-08 13:19:16 -07:00
|
|
|
/*
|
2013-08-27 22:10:22 -07:00
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
2009-07-08 13:19:16 -07:00
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
2009-07-08 13:19:16 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include "pktbuf.h"
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include "coverage.h"
|
2010-05-27 13:14:05 -07:00
|
|
|
#include "ofp-util.h"
|
2015-02-22 03:21:09 -08:00
|
|
|
#include "dp-packet.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "timeval.h"
|
|
|
|
|
#include "util.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
#include "openvswitch/vconn.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
#include "openvswitch/vlog.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
VLOG_DEFINE_THIS_MODULE(pktbuf);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
coverage: Make the coverage counters catalog program-specific.
Until now, the collection of coverage counters supported by a given OVS
program was not specific to that program. That means that, for example,
even though ovs-dpctl does not have anything to do with mac_learning, it
still has a coverage counter for it. This is confusing, at best.
This commit fixes the problem on some systems, in particular on ones that
use GCC and the GNU linker. It uses the feature of the GNU linker
described in its manual as:
If an orphaned section's name is representable as a C identifier then
the linker will automatically see PROVIDE two symbols: __start_SECNAME
and __end_SECNAME, where SECNAME is the name of the section. These
indicate the start address and end address of the orphaned section
respectively.
Systems that don't support these features retain the earlier behavior.
This commit also fixes the annoyance that files that include coverage
counters must be listed on COVERAGE_FILES in lib/automake.mk.
This commit also fixes the annoyance that modifying any source file that
includes a coverage counter caused all programs that link against
libopenvswitch.a to relink, even programs that the source file was not
linked into. For example, modifying ofproto/ofproto.c (which includes
coverage counters) caused tests/test-aes128 to relink, even though
test-aes128 does not link again ofproto.o.
2010-11-01 14:14:27 -07:00
|
|
|
COVERAGE_DEFINE(pktbuf_buffer_unknown);
|
|
|
|
|
COVERAGE_DEFINE(pktbuf_null_cookie);
|
|
|
|
|
COVERAGE_DEFINE(pktbuf_retrieved);
|
|
|
|
|
COVERAGE_DEFINE(pktbuf_reuse_error);
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
/* Buffers are identified by a 32-bit opaque ID. We divide the ID
|
|
|
|
|
* into a buffer number (low bits) and a cookie (high bits). The buffer number
|
|
|
|
|
* is an index into an array of buffers. The cookie distinguishes between
|
|
|
|
|
* different packets that have occupied a single buffer. Thus, the more
|
|
|
|
|
* buffers we have, the lower-quality the cookie... */
|
|
|
|
|
#define PKTBUF_BITS 8
|
|
|
|
|
#define PKTBUF_MASK (PKTBUF_CNT - 1)
|
|
|
|
|
#define PKTBUF_CNT (1u << PKTBUF_BITS)
|
|
|
|
|
|
|
|
|
|
#define COOKIE_BITS (32 - PKTBUF_BITS)
|
|
|
|
|
#define COOKIE_MAX ((1u << COOKIE_BITS) - 1)
|
|
|
|
|
|
|
|
|
|
#define OVERWRITE_MSECS 5000
|
|
|
|
|
|
|
|
|
|
struct packet {
|
2015-02-22 03:21:09 -08:00
|
|
|
struct dp_packet *buffer;
|
2009-07-08 13:19:16 -07:00
|
|
|
uint32_t cookie;
|
|
|
|
|
long long int timeout;
|
2013-06-19 16:58:44 -07:00
|
|
|
ofp_port_t in_port;
|
2009-07-08 13:19:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pktbuf {
|
|
|
|
|
struct packet packets[PKTBUF_CNT];
|
|
|
|
|
unsigned int buffer_idx;
|
2009-09-16 15:12:19 -07:00
|
|
|
unsigned int null_idx;
|
2009-07-08 13:19:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
pktbuf_capacity(void)
|
|
|
|
|
{
|
|
|
|
|
return PKTBUF_CNT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct pktbuf *
|
|
|
|
|
pktbuf_create(void)
|
|
|
|
|
{
|
2009-09-28 13:56:42 -07:00
|
|
|
return xzalloc(sizeof *pktbuf_create());
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
pktbuf_destroy(struct pktbuf *pb)
|
|
|
|
|
{
|
|
|
|
|
if (pb) {
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PKTBUF_CNT; i++) {
|
2015-02-22 03:21:09 -08:00
|
|
|
dp_packet_delete(pb->packets[i].buffer);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
free(pb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
static unsigned int
|
|
|
|
|
make_id(unsigned int buffer_idx, unsigned int cookie)
|
|
|
|
|
{
|
|
|
|
|
return buffer_idx | (cookie << PKTBUF_BITS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Attempts to allocate an OpenFlow packet buffer id within 'pb'. The packet
|
2012-01-03 10:27:04 -08:00
|
|
|
* buffer will store a copy of 'buffer_size' bytes in 'buffer' and the port
|
|
|
|
|
* number 'in_port', which should be the OpenFlow port number on which 'buffer'
|
|
|
|
|
* was received.
|
2009-09-16 15:12:19 -07:00
|
|
|
*
|
|
|
|
|
* If successful, returns the packet buffer id (a number other than
|
|
|
|
|
* UINT32_MAX). pktbuf_retrieve() can later be used to retrieve the buffer and
|
|
|
|
|
* its input port number (buffers do expire after a time, so this is not
|
|
|
|
|
* guaranteed to be true forever). On failure, returns UINT32_MAX.
|
|
|
|
|
*
|
|
|
|
|
* The caller retains ownership of 'buffer'. */
|
2009-07-08 13:19:16 -07:00
|
|
|
uint32_t
|
2012-01-03 10:27:04 -08:00
|
|
|
pktbuf_save(struct pktbuf *pb, const void *buffer, size_t buffer_size,
|
2013-06-19 16:58:44 -07:00
|
|
|
ofp_port_t in_port)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
|
struct packet *p = &pb->packets[pb->buffer_idx];
|
|
|
|
|
pb->buffer_idx = (pb->buffer_idx + 1) & PKTBUF_MASK;
|
|
|
|
|
if (p->buffer) {
|
|
|
|
|
if (time_msec() < p->timeout) {
|
|
|
|
|
return UINT32_MAX;
|
|
|
|
|
}
|
2015-02-22 03:21:09 -08:00
|
|
|
dp_packet_delete(p->buffer);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Don't use maximum cookie value since all-1-bits ID is special. */
|
|
|
|
|
if (++p->cookie >= COOKIE_MAX) {
|
|
|
|
|
p->cookie = 0;
|
|
|
|
|
}
|
2012-01-03 10:27:04 -08:00
|
|
|
|
2013-08-27 22:10:22 -07:00
|
|
|
/* Use 2 bytes of headroom to 32-bit align the L3 header. */
|
2015-02-22 03:21:09 -08:00
|
|
|
p->buffer = dp_packet_clone_data_with_headroom(buffer, buffer_size, 2);
|
2012-01-03 10:27:04 -08:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
p->timeout = time_msec() + OVERWRITE_MSECS;
|
|
|
|
|
p->in_port = in_port;
|
2009-09-16 15:12:19 -07:00
|
|
|
return make_id(p - pb->packets, p->cookie);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Allocates and returns a "null" packet buffer id. The returned packet buffer
|
|
|
|
|
* id is considered valid by pktbuf_retrieve(), but it is not associated with
|
|
|
|
|
* actual buffered data.
|
|
|
|
|
*
|
|
|
|
|
* This function is always successful.
|
|
|
|
|
*
|
|
|
|
|
* This is useful in one special case: with the current OpenFlow design, the
|
|
|
|
|
* "fail-open" code cannot always know whether a connection to a controller is
|
|
|
|
|
* actually valid until it receives a OFPT_PACKET_OUT or OFPT_FLOW_MOD request,
|
|
|
|
|
* but at that point the packet in question has already been forwarded (since
|
|
|
|
|
* we are still in "fail-open" mode). If the packet was buffered in the usual
|
|
|
|
|
* way, then the OFPT_PACKET_OUT or OFPT_FLOW_MOD would cause a duplicate
|
|
|
|
|
* packet in the network. Null packet buffer ids identify such a packet that
|
|
|
|
|
* has already been forwarded, so that Open vSwitch can quietly ignore the
|
|
|
|
|
* request to re-send it. (After that happens, the switch exits fail-open
|
|
|
|
|
* mode.)
|
|
|
|
|
*
|
|
|
|
|
* See the top-level comment in fail-open.c for an overview.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t
|
|
|
|
|
pktbuf_get_null(void)
|
|
|
|
|
{
|
|
|
|
|
return make_id(0, COOKIE_MAX);
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
2009-09-16 15:12:19 -07:00
|
|
|
/* Attempts to retrieve a saved packet with the given 'id' from 'pb'. Returns
|
2012-01-12 15:48:19 -08:00
|
|
|
* 0 if successful, otherwise an OpenFlow error code.
|
2009-09-16 15:12:19 -07:00
|
|
|
*
|
|
|
|
|
* On success, ordinarily stores the buffered packet in '*bufferp' and the
|
2013-05-29 13:57:22 -07:00
|
|
|
* OpenFlow port number on which the packet was received in '*in_port'. The
|
2009-09-16 15:12:19 -07:00
|
|
|
* caller becomes responsible for freeing the buffer. However, if 'id'
|
|
|
|
|
* identifies a "null" packet buffer (created with pktbuf_get_null()), stores
|
2013-06-19 16:58:44 -07:00
|
|
|
* NULL in '*bufferp' and OFPP_NONE in '*in_port'.
|
2009-09-16 15:12:19 -07:00
|
|
|
*
|
2011-09-08 11:21:45 -07:00
|
|
|
* 'in_port' may be NULL if the input port is not of interest.
|
|
|
|
|
*
|
2013-08-27 22:10:22 -07:00
|
|
|
* The L3 header of a returned packet will be 32-bit aligned.
|
2010-08-10 11:05:01 -07:00
|
|
|
*
|
2010-02-11 12:33:29 -08:00
|
|
|
* On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
|
2012-01-12 15:48:19 -08:00
|
|
|
enum ofperr
|
2015-02-22 03:21:09 -08:00
|
|
|
pktbuf_retrieve(struct pktbuf *pb, uint32_t id, struct dp_packet **bufferp,
|
2013-06-19 16:58:44 -07:00
|
|
|
ofp_port_t *in_port)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 20);
|
|
|
|
|
struct packet *p;
|
2012-01-12 15:48:19 -08:00
|
|
|
enum ofperr error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
2011-05-11 12:13:10 -07:00
|
|
|
if (id == UINT32_MAX) {
|
|
|
|
|
error = 0;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
if (!pb) {
|
|
|
|
|
VLOG_WARN_RL(&rl, "attempt to send buffered packet via connection "
|
|
|
|
|
"without buffers");
|
2015-01-17 09:21:04 -08:00
|
|
|
error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
|
|
|
|
|
goto error;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = &pb->packets[id & PKTBUF_MASK];
|
|
|
|
|
if (p->cookie == id >> PKTBUF_BITS) {
|
2015-02-22 03:21:09 -08:00
|
|
|
struct dp_packet *buffer = p->buffer;
|
2009-07-08 13:19:16 -07:00
|
|
|
if (buffer) {
|
|
|
|
|
*bufferp = buffer;
|
2011-09-08 11:21:45 -07:00
|
|
|
if (in_port) {
|
|
|
|
|
*in_port = p->in_port;
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
p->buffer = NULL;
|
|
|
|
|
COVERAGE_INC(pktbuf_retrieved);
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
COVERAGE_INC(pktbuf_reuse_error);
|
|
|
|
|
VLOG_WARN_RL(&rl, "attempt to reuse buffer %08"PRIx32, id);
|
2012-01-12 15:48:19 -08:00
|
|
|
error = OFPERR_OFPBRC_BUFFER_EMPTY;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2009-09-16 15:12:19 -07:00
|
|
|
} else if (id >> PKTBUF_BITS != COOKIE_MAX) {
|
2009-11-12 15:40:33 -08:00
|
|
|
COVERAGE_INC(pktbuf_buffer_unknown);
|
2009-07-08 13:19:16 -07:00
|
|
|
VLOG_WARN_RL(&rl, "cookie mismatch: %08"PRIx32" != %08"PRIx32,
|
|
|
|
|
id, (id & PKTBUF_MASK) | (p->cookie << PKTBUF_BITS));
|
2012-01-12 15:48:19 -08:00
|
|
|
error = OFPERR_OFPBRC_BUFFER_UNKNOWN;
|
2009-09-16 15:12:19 -07:00
|
|
|
} else {
|
|
|
|
|
COVERAGE_INC(pktbuf_null_cookie);
|
|
|
|
|
VLOG_INFO_RL(&rl, "Received null cookie %08"PRIx32" (this is normal "
|
|
|
|
|
"if the switch was recently in fail-open mode)", id);
|
|
|
|
|
error = 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
}
|
2011-05-11 12:13:10 -07:00
|
|
|
error:
|
2009-07-08 13:19:16 -07:00
|
|
|
*bufferp = NULL;
|
2011-09-08 11:21:45 -07:00
|
|
|
if (in_port) {
|
2013-06-19 16:58:44 -07:00
|
|
|
*in_port = OFPP_NONE;
|
2011-09-08 11:21:45 -07:00
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
pktbuf_discard(struct pktbuf *pb, uint32_t id)
|
|
|
|
|
{
|
|
|
|
|
struct packet *p = &pb->packets[id & PKTBUF_MASK];
|
|
|
|
|
if (p->cookie == id >> PKTBUF_BITS) {
|
2015-02-22 03:21:09 -08:00
|
|
|
dp_packet_delete(p->buffer);
|
2009-07-08 13:19:16 -07:00
|
|
|
p->buffer = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-08 15:44:21 -07:00
|
|
|
|
|
|
|
|
/* Returns the number of packets buffered in 'pb'. Returns 0 if 'pb' is
|
|
|
|
|
* null. */
|
|
|
|
|
unsigned int
|
|
|
|
|
pktbuf_count_packets(const struct pktbuf *pb)
|
|
|
|
|
{
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
|
|
if (pb) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PKTBUF_CNT; i++) {
|
|
|
|
|
if (pb->packets[i].buffer) {
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|