mirror of
https://github.com/openvswitch/ovs
synced 2025-08-29 13:27:59 +00:00
ofpbuf: Add ofpbuf_new_with_headroom(), ofpbuf_clone_with_headroom().
These new functions simplify an increasingly common usage pattern. Suggested-by: Jesse Gross <jesse@nicira.com>
This commit is contained in:
parent
f79cb67e68
commit
68efcbec41
@ -478,8 +478,7 @@ dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
|
|||||||
int retval;
|
int retval;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
buf = ofpbuf_new(65536 + DPIF_RECV_MSG_PADDING);
|
buf = ofpbuf_new_with_headroom(65536, DPIF_RECV_MSG_PADDING);
|
||||||
ofpbuf_reserve(buf, DPIF_RECV_MSG_PADDING);
|
|
||||||
retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
|
retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
|
||||||
if (retval < 0) {
|
if (retval < 0) {
|
||||||
error = errno;
|
error = errno;
|
||||||
|
@ -1262,8 +1262,7 @@ dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg_size = sizeof *header + packet->size;
|
msg_size = sizeof *header + packet->size;
|
||||||
msg = ofpbuf_new(msg_size + DPIF_RECV_MSG_PADDING);
|
msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
|
||||||
ofpbuf_reserve(msg, DPIF_RECV_MSG_PADDING);
|
|
||||||
header = ofpbuf_put_uninit(msg, sizeof *header);
|
header = ofpbuf_put_uninit(msg, sizeof *header);
|
||||||
header->type = queue_no;
|
header->type = queue_no;
|
||||||
header->length = msg_size;
|
header->length = msg_size;
|
||||||
|
20
lib/ofpbuf.c
20
lib/ofpbuf.c
@ -75,12 +75,32 @@ ofpbuf_new(size_t size)
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Creates and returns a new ofpbuf with an initial capacity of 'size +
|
||||||
|
* headroom' bytes, reserving the first 'headroom' bytes as headroom. */
|
||||||
|
struct ofpbuf *
|
||||||
|
ofpbuf_new_with_headroom(size_t size, size_t headroom)
|
||||||
|
{
|
||||||
|
struct ofpbuf *b = ofpbuf_new(size + headroom);
|
||||||
|
ofpbuf_reserve(b, headroom);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
struct ofpbuf *
|
struct ofpbuf *
|
||||||
ofpbuf_clone(const struct ofpbuf *buffer)
|
ofpbuf_clone(const struct ofpbuf *buffer)
|
||||||
{
|
{
|
||||||
return ofpbuf_clone_data(buffer->data, buffer->size);
|
return ofpbuf_clone_data(buffer->data, buffer->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Creates and returns a new ofpbuf whose data are copied from 'buffer'. The
|
||||||
|
* returned ofpbuf will additionally have 'headroom' bytes of headroom. */
|
||||||
|
struct ofpbuf *
|
||||||
|
ofpbuf_clone_with_headroom(const struct ofpbuf *buffer, size_t headroom)
|
||||||
|
{
|
||||||
|
struct ofpbuf *b = ofpbuf_new_with_headroom(buffer->size, headroom);
|
||||||
|
ofpbuf_put(b, buffer->data, buffer->size);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
struct ofpbuf *
|
struct ofpbuf *
|
||||||
ofpbuf_clone_data(const void *data, size_t size)
|
ofpbuf_clone_data(const void *data, size_t size)
|
||||||
{
|
{
|
||||||
|
@ -48,7 +48,10 @@ void ofpbuf_uninit(struct ofpbuf *);
|
|||||||
void ofpbuf_reinit(struct ofpbuf *, size_t);
|
void ofpbuf_reinit(struct ofpbuf *, size_t);
|
||||||
|
|
||||||
struct ofpbuf *ofpbuf_new(size_t);
|
struct ofpbuf *ofpbuf_new(size_t);
|
||||||
|
struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
|
||||||
struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
|
struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
|
||||||
|
struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
|
||||||
|
size_t headroom);
|
||||||
struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
|
struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
|
||||||
void ofpbuf_delete(struct ofpbuf *);
|
void ofpbuf_delete(struct ofpbuf *);
|
||||||
|
|
||||||
|
@ -112,8 +112,8 @@ pktbuf_save(struct pktbuf *pb, struct ofpbuf *buffer, uint16_t in_port)
|
|||||||
if (++p->cookie >= COOKIE_MAX) {
|
if (++p->cookie >= COOKIE_MAX) {
|
||||||
p->cookie = 0;
|
p->cookie = 0;
|
||||||
}
|
}
|
||||||
p->buffer = ofpbuf_new(sizeof(struct ofp_packet_in) + buffer->size);
|
p->buffer = ofpbuf_new_with_headroom(buffer->size,
|
||||||
ofpbuf_reserve(p->buffer, sizeof(struct ofp_packet_in));
|
sizeof(struct ofp_packet_in));
|
||||||
ofpbuf_put(p->buffer, buffer->data, buffer->size);
|
ofpbuf_put(p->buffer, buffer->data, buffer->size);
|
||||||
p->timeout = time_msec() + OVERWRITE_MSECS;
|
p->timeout = time_msec() + OVERWRITE_MSECS;
|
||||||
p->in_port = in_port;
|
p->in_port = in_port;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user