The new helpers get_local_datapath() and get_patched_datapath() make code
a little shorter and easier to read. They also avoid a pitfall that was
present in at least a few of the instances: CONTAINER_OF is not safe on a
null pointer, because it does a raw pointer subtraction and will change
NULL to something else. This wasn't actually a problem in these particular
cases because the value it was subtracting was zero (although arguably it
is still undefined behavior because the compiler is allowed to assume that
a pointer on which arithmetic is performed is nonnull).
Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Russell Bryant <russell@ovn.org>
add_local_datapath(struct hmap *local_datapaths,
const struct sbrec_port_binding *binding_rec)
{
add_local_datapath(struct hmap *local_datapaths,
const struct sbrec_port_binding *binding_rec)
{
- if (hmap_first_with_hash(local_datapaths,
- binding_rec->datapath->tunnel_key)) {
+ if (get_local_datapath(local_datapaths,
+ binding_rec->datapath->tunnel_key)) {
* large lrouters and lswitches. This need to be studied further.
*/
* large lrouters and lswitches. This need to be studied further.
*/
- struct hmap_node *ld;
- ld = hmap_first_with_hash(local_datapaths, ldp->tunnel_key);
- if (!ld) {
+ if (!get_local_datapath(local_datapaths, ldp->tunnel_key)) {
if (!ingress) {
continue;
}
if (!ingress) {
continue;
}
- struct hmap_node *pd;
- pd = hmap_first_with_hash(patched_datapaths, ldp->tunnel_key);
- if (!pd) {
+ if (!get_patched_datapath(patched_datapaths,
+ ldp->tunnel_key)) {
+struct local_datapath *
+get_local_datapath(const struct hmap *local_datapaths, uint32_t tunnel_key)
+{
+ struct hmap_node *node = hmap_first_with_hash(local_datapaths, tunnel_key);
+ return (node
+ ? CONTAINER_OF(node, struct local_datapath, hmap_node)
+ : NULL);
+}
+
+struct patched_datapath *
+get_patched_datapath(const struct hmap *patched_datapaths, uint32_t tunnel_key)
+{
+ struct hmap_node *node = hmap_first_with_hash(patched_datapaths,
+ tunnel_key);
+ return (node
+ ? CONTAINER_OF(node, struct patched_datapath, hmap_node)
+ : NULL);
+}
+
const struct sbrec_chassis *
get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
{
const struct sbrec_chassis *
get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
{
-/* Copyright (c) 2015 Nicira, Inc.
+/* Copyright (c) 2015, 2016 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.
const struct sbrec_port_binding *localnet_port;
};
const struct sbrec_port_binding *localnet_port;
};
+struct local_datapath *get_local_datapath(const struct hmap *,
+ uint32_t tunnel_key);
+
/* Contains hmap_node whose hash values are the tunnel_key of datapaths
* with at least one logical patch port binding. */
struct patched_datapath {
struct hmap_node hmap_node;
};
/* Contains hmap_node whose hash values are the tunnel_key of datapaths
* with at least one logical patch port binding. */
struct patched_datapath {
struct hmap_node hmap_node;
};
+struct patched_datapath *get_patched_datapath(const struct hmap *,
+ uint32_t tunnel_key);
+
const struct ovsrec_bridge *get_bridge(struct ovsdb_idl *,
const char *br_name);
const struct ovsrec_bridge *get_bridge(struct ovsdb_idl *,
const char *br_name);
-/* Copyright (c) 2015 Nicira, Inc.
+/* Copyright (c) 2015, 2016 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.
const struct sbrec_port_binding *binding;
SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
if (!strcmp(binding->type, "localnet")) {
const struct sbrec_port_binding *binding;
SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
if (!strcmp(binding->type, "localnet")) {
- struct local_datapath *ld;
- ld = CONTAINER_OF(hmap_first_with_hash(local_datapaths,
- binding->datapath->tunnel_key),
- struct local_datapath, hmap_node);
+ struct local_datapath *ld
+ = get_local_datapath(local_datapaths,
+ binding->datapath->tunnel_key);
if (!ld) {
/* This localnet port is on a datapath with no
* logical ports bound to this chassis, so there's no need
if (!ld) {
/* This localnet port is on a datapath with no
* logical ports bound to this chassis, so there's no need
add_patched_datapath(struct hmap *patched_datapaths,
const struct sbrec_port_binding *binding_rec)
{
add_patched_datapath(struct hmap *patched_datapaths,
const struct sbrec_port_binding *binding_rec)
{
- if (hmap_first_with_hash(patched_datapaths,
+ if (get_patched_datapath(patched_datapaths,
binding_rec->datapath->tunnel_key)) {
return;
}
binding_rec->datapath->tunnel_key)) {
return;
}
static const struct sbrec_port_binding*
get_localnet_port(struct hmap *local_datapaths, int64_t tunnel_key)
{
static const struct sbrec_port_binding*
get_localnet_port(struct hmap *local_datapaths, int64_t tunnel_key)
{
- struct local_datapath *ld;
- ld = CONTAINER_OF(hmap_first_with_hash(local_datapaths, tunnel_key),
- struct local_datapath, hmap_node);
+ struct local_datapath *ld = get_local_datapath(local_datapaths,
+ tunnel_key);
return ld ? ld->localnet_port : NULL;
}
return ld ? ld->localnet_port : NULL;
}
*/
uint32_t dp_key = binding->datapath->tunnel_key;
uint32_t port_key = binding->tunnel_key;
*/
uint32_t dp_key = binding->datapath->tunnel_key;
uint32_t port_key = binding->tunnel_key;
- struct hmap_node *ld;
- ld = hmap_first_with_hash(local_datapaths, dp_key);
- if (!ld) {
- struct hmap_node *pd;
- pd = hmap_first_with_hash(patched_datapaths, dp_key);
- if (!pd) {
- continue;
- }
+ if (!get_local_datapath(local_datapaths, dp_key)
+ && !get_patched_datapath(patched_datapaths, dp_key)) {
+ continue;
}
/* Find the OpenFlow port for the logical port, as 'ofport'. This is
}
/* Find the OpenFlow port for the logical port, as 'ofport'. This is