uint32_t
hash_bytes(const void *p_, size_t n, uint32_t basis)
{
- const uint8_t *p = p_;
+ const uint32_t *p = p_;
size_t orig_n = n;
uint32_t hash;
hash = basis;
while (n >= 4) {
- hash = mhash_add(hash, get_unaligned_u32((const uint32_t *) p));
+ hash = mhash_add(hash, get_unaligned_u32(p));
n -= 4;
- p += 4;
+ p += 1;
}
if (n) {
uint32_t
jhash_bytes(const void *p_, size_t n, uint32_t basis)
{
- const uint8_t *p = p_;
+ const uint32_t *p = p_;
uint32_t a, b, c;
a = b = c = 0xdeadbeef + n + basis;
while (n >= 12) {
- a += get_unaligned_u32((uint32_t *) p);
- b += get_unaligned_u32((uint32_t *) (p + 4));
- c += get_unaligned_u32((uint32_t *) (p + 8));
+ a += get_unaligned_u32(p);
+ b += get_unaligned_u32(p + 1);
+ c += get_unaligned_u32(p + 2);
jhash_mix(&a, &b, &c);
n -= 12;
- p += 12;
+ p += 3;
}
if (n) {
mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
uint16_t vlan)
{
- unsigned int mac1 = get_unaligned_u32((uint32_t *) mac);
- unsigned int mac2 = get_unaligned_u16((uint16_t *) (mac + 4));
+ unsigned int mac1 = get_unaligned_u32(ALIGNED_CAST(uint32_t *, mac));
+ unsigned int mac2 = get_unaligned_u16(ALIGNED_CAST(uint16_t *, mac + 4));
return hash_3words(mac1, mac2 | (vlan << 16), ml->secret);
}
ifr.ifr_addr.sa_family = AF_INET;
error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
if (!error) {
- const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
+ const struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *,
+ &ifr.ifr_addr);
*ip = sin->sin_addr;
}
return error;
return false;
}
- NL_ATTR_FOR_EACH (nla, left,
- (struct nlattr *) ((char *) msg->data + nla_offset),
+ NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, nla_offset, 0),
msg->size - nla_offset)
{
uint16_t type = nl_attr_type(nla);
const struct nlattr *
nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type)
{
- const uint8_t *start = (const uint8_t *) buf->data + hdr_len;
- return nl_attr_find__((const struct nlattr *) start, buf->size - hdr_len,
+ return nl_attr_find__(ofpbuf_at(buf, hdr_len, 0), buf->size - hdr_len,
type);
}
match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh;
ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
- omh = (struct ofp11_match_header *)((char *)b->data + start_len);
+ omh = ofpbuf_at(b, start_len, sizeof *omh);
omh->type = htons(OFPMT_OXM);
omh->length = htons(match_len);
}
char *
-oxm_match_to_string(const uint8_t *p, unsigned int match_len)
+oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
{
- const struct ofp11_match_header *omh = (struct ofp11_match_header *)p;
+ const struct ofp11_match_header *omh = p->data;
uint16_t match_len_;
struct ds s;
goto err;
}
- return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh);
+ return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
+ match_len - sizeof *omh);
err:
return ds_steal_cstr(&s);
match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
- omh = (struct ofp11_match_header *)((char *)b->data + start_len);
+ omh = ofpbuf_at(b, start_len, sizeof *omh);
omh->type = htons(OFPMT_OXM);
omh->length = htons(match_len);
int oxm_put_match(struct ofpbuf *, const struct match *);
char *nx_match_to_string(const uint8_t *, unsigned int match_len);
-char *oxm_match_to_string(const uint8_t *, unsigned int match_len);
+char *oxm_match_to_string(const struct ofpbuf *, unsigned int match_len);
int nx_match_from_string(const char *, struct ofpbuf *);
int oxm_match_from_string(const char *, struct ofpbuf *);
odp_flow_key_hash(const struct nlattr *key, size_t key_len)
{
BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
- return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
+ return hash_words(ALIGNED_CAST(const uint32_t *, key),
+ key_len / sizeof(uint32_t), 0);
}
static void
break;
case OFPUTIL_NXAST_WRITE_METADATA:
- nawm = (const struct nx_action_write_metadata *) a;
+ nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
error = metadata_from_nxast(nawm, out);
break;
case OFPUTIL_NXAST_REG_LOAD:
error = nxm_reg_load_from_openflow(
- (const struct nx_action_reg_load *) a, out);
+ ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
break;
case OFPUTIL_NXAST_STACK_PUSH:
break;
case OFPUTIL_NXAST_SET_TUNNEL64:
- nast64 = (const struct nx_action_set_tunnel64 *) a;
+ nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
tunnel = ofpact_put_SET_TUNNEL(out);
tunnel->ofpact.compat = code;
tunnel->tun_id = ntohll(nast64->tun_id);
break;
case OFPUTIL_NXAST_LEARN:
- error = learn_from_openflow((const struct nx_action_learn *) a, out);
+ error = learn_from_openflow(
+ ALIGNED_CAST(const struct nx_action_learn *, a), out);
break;
case OFPUTIL_NXAST_EXIT:
instruction_get_##ENUM(const struct ofp11_instruction *inst)\
{ \
ovs_assert(inst->type == htons(ENUM)); \
- return (struct STRUCT *)inst; \
+ return ALIGNED_CAST(struct STRUCT *, inst); \
} \
\
static inline void \
static void
get_actions_from_instruction(const struct ofp11_instruction *inst,
- const union ofp_action **actions,
- size_t *n_actions)
+ const union ofp_action **actions,
+ size_t *n_actions)
{
- *actions = (const union ofp_action *) (inst + 1);
+ *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
*n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
}
const struct ofp13_instruction_meter *oim;
struct ofpact_meter *om;
- oim = (const struct ofp13_instruction_meter *)
- insts[OVSINST_OFPIT13_METER];
+ oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
+ insts[OVSINST_OFPIT13_METER]);
om = ofpact_put_METER(ofpacts);
om->meter_id = ntohl(oim->meter_id);
const struct ofp11_instruction_write_metadata *oiwm;
struct ofpact_metadata *om;
- oiwm = (const struct ofp11_instruction_write_metadata *)
- insts[OVSINST_OFPIT11_WRITE_METADATA];
+ oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
+ insts[OVSINST_OFPIT11_WRITE_METADATA]);
om = ofpact_put_WRITE_METADATA(ofpacts);
om->metadata = oiwm->metadata;
if (remainder) {
ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
}
- nan = (struct nx_action_note *)((char *)out->data + start_ofs);
+ nan = ofpbuf_at(out, start_ofs, sizeof *nan);
nan->len = htons(out->size - start_ofs);
}
ofpact_get_##ENUM(const struct ofpact *ofpact) \
{ \
ovs_assert(ofpact->type == OFPACT_##ENUM); \
- return (struct STRUCT *) ofpact; \
+ return ALIGNED_CAST(struct STRUCT *, ofpact); \
} \
\
static inline struct STRUCT * \
uint32_t *allowed_versionsp)
{
uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
- const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1);
+ const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
uint32_t allowed_versions;
if (!bitmap_len || bitmap_len % sizeof *bitmap) {
oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
oheh->type = htons(OFPHET_VERSIONBITMAP);
oheh->length = htons(map_len + sizeof *oheh);
- *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions);
+ *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
ofpmsg_update_length(msg);
}
((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
n++;
len -= ombh_len;
- ombh = (struct ofp13_meter_band_header *)(((char *)ombh) + ombh_len);
+ ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
+ (char *) ombh + ombh_len);
}
if (len) {
return OFPERR_OFPBRC_BAD_LEN;
__builtin_choose_expr( \
__builtin_types_compatible_p(typeof(OBJECT), struct locked_uint64), \
(THEN), (ELSE))
-#define AS_LOCKED_UINT64(OBJECT) ((struct locked_uint64 *) (OBJECT))
+#define AS_LOCKED_UINT64(OBJECT) ((struct locked_uint64 *) (void *) (OBJECT))
#define AS_UINT64(OBJECT) ((uint64_t *) (OBJECT))
struct locked_uint64 {
uint64_t value;
__builtin_choose_expr( \
__builtin_types_compatible_p(typeof(OBJECT), struct locked_int64), \
(THEN), (ELSE))
-#define AS_LOCKED_INT64(OBJECT) ((struct locked_int64 *) (OBJECT))
+#define AS_LOCKED_INT64(OBJECT) ((struct locked_int64 *) (void *) (OBJECT))
#define AS_INT64(OBJECT) ((int64_t *) (OBJECT))
struct locked_int64 {
int64_t value;
if (eh->eth_type == htons(ETH_TYPE_VLAN)) {
ovs_be16 *p;
- p = (ovs_be16 *)((char *)(packet->l2_5 ? packet->l2_5 : packet->l3) - 2);
+ p = ALIGNED_CAST(ovs_be16 *,
+ (char *)(packet->l2_5 ? packet->l2_5 : packet->l3) - 2);
*p = eth_type;
} else {
eh->eth_type = eth_type;
if (remaining < sizeof *nh) {
return false;
}
- nh = (struct ip6_hdr *)data;
+ nh = ALIGNED_CAST(struct 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 = (struct ip6_frag *)data;
+ const struct ip6_frag *frag_hdr = ALIGNED_CAST(struct ip6_frag *,
+ data);
nexthdr = frag_hdr->ip6f_nxt;
len = sizeof *frag_hdr;
const struct nlmsghdr *nlmsg;
nlmsg = buf->data;
- rtm = (const struct rtmsg *) ((const char *) buf->data + NLMSG_HDRLEN);
+ rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
if (rtm->rtm_family != AF_INET) {
VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
const struct ifinfomsg *ifinfo;
nlmsg = buf->data;
- ifinfo = ((const struct ifinfomsg *)
- ((const char *) buf->data + NLMSG_HDRLEN));
+ ifinfo = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifinfo);
change->nlmsg_type = nlmsg->nlmsg_type;
change->ifi_index = ifinfo->ifi_index;
switch (getaddrinfo(host_name, NULL, &hints, &result)) {
case 0:
- *addr = ((struct sockaddr_in *) result->ai_addr)->sin_addr;
+ *addr = ALIGNED_CAST(struct sockaddr_in *,
+ result->ai_addr)->sin_addr;
freeaddrinfo(result);
return 0;
goto error;
} else {
size_t n_fds = (p->cmsg_len - CMSG_LEN(0)) / sizeof *fds;
- const int *fds_data = (const int *) CMSG_DATA(p);
+ const int *fds_data = ALIGNED_CAST(const int *, CMSG_DATA(p));
ovs_assert(n_fds > 0);
if (n_fds > SOUTIL_MAX_FDS) {
ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
struct stream **streamp)
{
- const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
+ const struct sockaddr_in *sin = ALIGNED_CAST(const struct sockaddr_in *,
+ sa);
char name[128];
if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
#define ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER) \
((OBJECT) = OBJECT_CONTAINING(POINTER, OBJECT, MEMBER), (void) 0)
+/* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to
+ * (void *). This is to suppress the alignment warning issued by clang. */
+#define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))
+
#ifdef __cplusplus
extern "C" {
#endif
}
if (first_output) {
uint8_t *end = (uint8_t *) actions + length;
- sort_output_actions__(first_output, (struct nlattr *) end);
+ sort_output_actions__(first_output,
+ ALIGNED_CAST(struct nlattr *, end));
}
}
ofpbuf_init(&nx_match, 0);
if (oxm) {
match_len = oxm_put_match(&nx_match, &match);
- out = oxm_match_to_string(nx_match.data, match_len);
+ out = oxm_match_to_string(&nx_match, match_len);
} else {
match_len = nx_put_match(&nx_match, &match,
cookie, cookie_mask);
/* Convert to and from OXM. */
ofpbuf_init(&nxm, 0);
nxm_match_len = oxm_put_match(&nxm, &match);
- nxm_s = oxm_match_to_string(nxm.data, nxm_match_len);
+ nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
error = oxm_pull_match(&nxm, &nxm_match);
printf("OXM: %s -> ", nxm_s);
if (error) {