Memory access tends to be faster when data is properly aligned.
Signed-off-by: Ben Pfaff <blp@nicira.com>
struct arp_eth_header *arp;
ofpbuf_clear(b);
struct arp_eth_header *arp;
ofpbuf_clear(b);
- ofpbuf_prealloc_tailroom(b, ETH_HEADER_LEN + VLAN_HEADER_LEN
+ ofpbuf_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN
- ofpbuf_reserve(b, VLAN_HEADER_LEN);
+ ofpbuf_reserve(b, 2 + VLAN_HEADER_LEN);
eth = ofpbuf_put_uninit(b, sizeof *eth);
memcpy(eth->eth_dst, eth_addr_broadcast, ETH_ADDR_LEN);
memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
eth = ofpbuf_put_uninit(b, sizeof *eth);
memcpy(eth->eth_dst, eth_addr_broadcast, ETH_ADDR_LEN);
memcpy(eth->eth_src, eth_src, ETH_ADDR_LEN);
/* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'. The
* caller must free '*packetp'. On success, returns NULL. On failure, returns
/* 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;
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);
if (ofpbuf_put_hex(packet, hex, NULL)[0] != '\0') {
ofpbuf_delete(packet);
* '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
* '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. */
*
* The returned packet has enough headroom to insert an 802.1Q VLAN header if
* desired. */
- 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);
eth = ofpbuf_put_uninit(b, ETH_HEADER_LEN);
data = ofpbuf_put_uninit(b, size);
* 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.
* 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'.
*
*
* The implementation should add the statistics for 'packet' into 'rule'.
*
goto exit_free_ofpacts;
}
} else {
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. */
}
/* Verify actions against packet, then send packet if successful. */
- * 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.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
if (++p->cookie >= COOKIE_MAX) {
p->cookie = 0;
}
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;
p->timeout = time_msec() + OVERWRITE_MSECS;
p->in_port = in_port;
*
* 'in_port' may be NULL if the input port is not of interest.
*
*
* '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
*
* On failure, stores NULL in in '*bufferp' and UINT16_MAX in '*in_port'. */
enum ofperr