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
+ ARP_ETH_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);
/* 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);
* '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. */
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);
static bool
packet_rh_present(struct ofpbuf *packet)
{
- const struct ip6_hdr *nh;
+ const struct ovs_16aligned_ip6_hdr *nh;
int nexthdr;
size_t len;
size_t remaining;
if (remaining < sizeof *nh) {
return false;
}
- nh = ALIGNED_CAST(struct ip6_hdr *, data);
+ nh = ALIGNED_CAST(struct ovs_16aligned_ip6_hdr *, data);
data += sizeof *nh;
remaining -= sizeof *nh;
nexthdr = nh->ip6_nxt;
nexthdr = ext_hdr->ip6e_nxt;
len = (ext_hdr->ip6e_len + 2) * 4;
} else if (nexthdr == IPPROTO_FRAGMENT) {
- const struct ip6_frag *frag_hdr = ALIGNED_CAST(struct ip6_frag *,
- data);
+ const struct ovs_16aligned_ip6_frag *frag_hdr
+ = ALIGNED_CAST(struct ovs_16aligned_ip6_frag *, data);
nexthdr = frag_hdr->ip6f_nxt;
len = sizeof *frag_hdr;
static void
packet_update_csum128(struct ofpbuf *packet, uint8_t proto,
- ovs_be32 addr[4], const ovs_be32 new_addr[4])
+ ovs_16aligned_be32 addr[4], const ovs_be32 new_addr[4])
{
if (proto == IPPROTO_TCP && packet->l7) {
struct tcp_header *th = packet->l4;
static void
packet_set_ipv6_addr(struct ofpbuf *packet, uint8_t proto,
- struct in6_addr *addr, const ovs_be32 new_addr[4],
+ ovs_16aligned_be32 *addr, const ovs_be32 new_addr[4],
bool recalculate_csum)
{
if (recalculate_csum) {
- packet_update_csum128(packet, proto, (ovs_be32 *)addr, new_addr);
+ packet_update_csum128(packet, proto, addr, new_addr);
}
memcpy(addr, new_addr, sizeof(*addr));
}
static void
-packet_set_ipv6_flow_label(ovs_be32 *flow_label, ovs_be32 flow_key)
+packet_set_ipv6_flow_label(ovs_16aligned_be32 *flow_label, ovs_be32 flow_key)
{
- *flow_label = (*flow_label & htonl(~IPV6_LABEL_MASK)) | flow_key;
+ ovs_be32 old_label = get_16aligned_be32(flow_label);
+ ovs_be32 new_label = (old_label & htonl(~IPV6_LABEL_MASK)) | flow_key;
+ put_16aligned_be32(flow_label, new_label);
}
static void
-packet_set_ipv6_tc(ovs_be32 *flow_label, uint8_t tc)
+packet_set_ipv6_tc(ovs_16aligned_be32 *flow_label, uint8_t tc)
{
- *flow_label = (*flow_label & htonl(0xF00FFFFF)) | htonl(tc << 20);
+ ovs_be32 old_label = get_16aligned_be32(flow_label);
+ ovs_be32 new_label = (old_label & htonl(0xF00FFFFF)) | htonl(tc << 20);
+ put_16aligned_be32(flow_label, new_label);
}
/* Modifies the IPv4 header fields of 'packet' to be consistent with 'src',
const ovs_be32 dst[4], uint8_t key_tc, ovs_be32 key_fl,
uint8_t key_hl)
{
- struct ip6_hdr *nh = packet->l3;
+ struct ovs_16aligned_ip6_hdr *nh = packet->l3;
if (memcmp(&nh->ip6_src, src, sizeof(ovs_be32[4]))) {
- packet_set_ipv6_addr(packet, proto, &nh->ip6_src, src, true);
+ packet_set_ipv6_addr(packet, proto, nh->ip6_src.be32, src, true);
}
if (memcmp(&nh->ip6_dst, dst, sizeof(ovs_be32[4]))) {
- packet_set_ipv6_addr(packet, proto, &nh->ip6_dst, dst,
+ packet_set_ipv6_addr(packet, proto, nh->ip6_dst.be32, dst,
!packet_rh_present(packet));
}