[ovs-dev] [risc 4/4] packets, pktbuf: Align L3 headers on 32-bit boundary.
Ben Pfaff
blp at nicira.com
Thu Aug 15 20:00:55 UTC 2013
Memory access tends to be faster when data is properly aligned.
Signed-off-by: Ben Pfaff <blp at nicira.com>
---
lib/packets.c | 16 +++++++++++-----
ofproto/ofproto-provider.h | 1 +
ofproto/ofproto.c | 4 ++--
ofproto/pktbuf.c | 9 ++++-----
4 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/lib/packets.c b/lib/packets.c
index 61914ee..ac6a894 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -395,13 +395,16 @@ pop_mpls(struct ofpbuf *packet, ovs_be16 ethtype)
/* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'. The
* caller must free '*packetp'. On success, returns NULL. On failure, returns
- * an error message and stores NULL in '*packetp'. */
+ * an error message and stores NULL in '*packetp'.
+ *
+ * Aligns the L3 header of '*packetp' on a 32-bit boundary. */
const char *
eth_from_hex(const char *hex, struct ofpbuf **packetp)
{
struct ofpbuf *packet;
- packet = *packetp = ofpbuf_new(strlen(hex) / 2);
+ /* Use 2 bytes of headroom to 32-bit align the L3 header. */
+ packet = *packetp = ofpbuf_new_with_headroom(strlen(hex) / 2, 2);
if (ofpbuf_put_hex(packet, hex, NULL)[0] != '\0') {
ofpbuf_delete(packet);
@@ -602,7 +605,8 @@ ipv6_is_cidr(const struct in6_addr *netmask)
* 'eth_src' and 'eth_type' parameters. A payload of 'size' bytes is allocated
* in 'b' and returned. This payload may be populated with appropriate
* information by the caller. Sets 'b''s 'l2' and 'l3' pointers to the
- * Ethernet header and payload respectively.
+ * Ethernet header and payload respectively. Aligns b->l3 on a 32-bit
+ * boundary.
*
* The returned packet has enough headroom to insert an 802.1Q VLAN header if
* desired. */
@@ -616,8 +620,10 @@ eth_compose(struct ofpbuf *b, const uint8_t eth_dst[ETH_ADDR_LEN],
ofpbuf_clear(b);
- ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
- ofpbuf_reserve(b, VLAN_HEADER_LEN);
+ /* The magic 2 here ensures that the L3 header (when it is added later)
+ * will be 32-bit aligned. */
+ ofpbuf_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
+ ofpbuf_reserve(b, 2 + VLAN_HEADER_LEN);
eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
data = ofpbuf_put_uninit(b, size);
diff --git a/ofproto/ofproto-provider.h b/ofproto/ofproto-provider.h
index aa262bc..9f73462 100644
--- a/ofproto/ofproto-provider.h
+++ b/ofproto/ofproto-provider.h
@@ -1019,6 +1019,7 @@ struct ofproto_class {
* flow->tunnel and flow->in_port, which are assigned the correct values
* for the incoming packet. The register values are zeroed. 'packet''s
* header pointers (e.g. packet->l3) are appropriately initialized.
+ * packet->l3 is aligned on a 32-bit boundary.
*
* The implementation should add the statistics for 'packet' into 'rule'.
*
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index e7f29c6..ff1287b 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -2497,8 +2497,8 @@ handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
goto exit_free_ofpacts;
}
} else {
- payload = xmalloc(sizeof *payload);
- ofpbuf_use_const(payload, po.packet, po.packet_len);
+ /* Ensure that the L3 header is 32-bit aligned. */
+ payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
}
/* Verify actions against packet, then send packet if successful. */
diff --git a/ofproto/pktbuf.c b/ofproto/pktbuf.c
index 65fcef6..38ec348 100644
--- a/ofproto/pktbuf.c
+++ b/ofproto/pktbuf.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -119,9 +119,9 @@ pktbuf_save(struct pktbuf *pb, const void *buffer, size_t buffer_size,
if (++p->cookie >= COOKIE_MAX) {
p->cookie = 0;
}
- p->buffer = ofpbuf_clone_data_with_headroom(buffer, buffer_size,
- sizeof(struct ofp10_packet_in));
+ /* Use 2 bytes of headroom to 32-bit align the L3 header. */
+ p->buffer = ofpbuf_clone_data_with_headroom(buffer, buffer_size, 2);
p->timeout = time_msec() + OVERWRITE_MSECS;
p->in_port = in_port;
@@ -165,8 +165,7 @@ pktbuf_get_null(void)
*
* 'in_port' may be NULL if the input port is not of interest.
*
- * A returned packet will have at least sizeof(struct ofp10_packet_in) bytes of
- * headroom.
+ * The L3 header of a returned packet will be 32-bit aligned.
*
* On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
enum ofperr
--
1.7.10.4
More information about the dev
mailing list