1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* Copyright (c) 2025 Canonical, Ltd.
* Copyright (c) 2025, STACKIT GmbH & Co. KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <errno.h>
#include <net/if.h>
#include <stdbool.h>
#include "openvswitch/poll-loop.h"
#include "openvswitch/vlog.h"
#include "openvswitch/list.h"
#include "lib/ovn-sb-idl.h"
#include "binding.h"
#include "ha-chassis.h"
#include "local_data.h"
#include "route.h"
#include "route-table-notify.h"
#include "route-exchange.h"
#include "route-exchange-netlink.h"
VLOG_DEFINE_THIS_MODULE(route_exchange);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
struct maintained_route_table_entry {
struct hmap_node node;
uint32_t table_id;
};
static struct hmap _maintained_route_tables =
HMAP_INITIALIZER(&_maintained_route_tables);
static struct sset _maintained_vrfs = SSET_INITIALIZER(&_maintained_vrfs);
struct route_entry {
struct hmap_node hmap_node;
const struct sbrec_learned_route *sb_route;
};
static uint32_t
maintained_route_table_hash(uint32_t table_id)
{
return hash_int(table_id, 0);
}
static bool
maintained_route_table_contains(uint32_t table_id)
{
uint32_t hash = maintained_route_table_hash(table_id);
struct maintained_route_table_entry *mrt;
HMAP_FOR_EACH_WITH_HASH (mrt, node, hash,
&_maintained_route_tables) {
if (mrt->table_id == table_id) {
return true;
}
}
return false;
}
static void
maintained_route_table_add(uint32_t table_id)
{
if (maintained_route_table_contains(table_id)) {
return;
}
uint32_t hash = maintained_route_table_hash(table_id);
struct maintained_route_table_entry *mrt = xmalloc(sizeof *mrt);
mrt->table_id = table_id;
hmap_insert(&_maintained_route_tables, &mrt->node, hash);
}
static void
route_add_entry(struct hmap *routes,
const struct sbrec_learned_route *sb_route)
{
struct route_entry *route_e = xmalloc(sizeof *route_e);
route_e->sb_route = sb_route;
uint32_t hash = uuid_hash(&sb_route->datapath->header_.uuid);
hash = hash_string(sb_route->logical_port->logical_port, hash);
hash = hash_string(sb_route->ip_prefix, hash);
hmap_insert(routes, &route_e->hmap_node, hash);
}
static struct route_entry *
route_lookup(struct hmap *route_map,
const struct sbrec_datapath_binding *sb_db,
const struct sbrec_port_binding *logical_port,
const char *ip_prefix, const char *nexthop)
{
struct route_entry *route_e;
uint32_t hash;
hash = uuid_hash(&sb_db->header_.uuid);
hash = hash_string(logical_port->logical_port, hash);
hash = hash_string(ip_prefix, hash);
HMAP_FOR_EACH_WITH_HASH (route_e, hmap_node, hash, route_map) {
if (route_e->sb_route->datapath != sb_db) {
continue;
}
if (route_e->sb_route->logical_port != logical_port) {
continue;
}
if (strcmp(route_e->sb_route->ip_prefix, ip_prefix)) {
continue;
}
if (strcmp(route_e->sb_route->nexthop, nexthop)) {
continue;
}
return route_e;
}
return NULL;
}
static void
sb_sync_learned_routes(const struct vector *learned_routes,
const struct sbrec_datapath_binding *datapath,
const struct smap *bound_ports,
struct ovsdb_idl_txn *ovnsb_idl_txn,
struct ovsdb_idl_index *sbrec_port_binding_by_name,
struct ovsdb_idl_index *sbrec_learned_route_by_datapath,
bool *sb_changes_pending)
{
struct hmap sync_routes = HMAP_INITIALIZER(&sync_routes);
const struct sbrec_learned_route *sb_route;
struct route_entry *route_e;
struct sbrec_learned_route *filter =
sbrec_learned_route_index_init_row(sbrec_learned_route_by_datapath);
sbrec_learned_route_index_set_datapath(filter, datapath);
SBREC_LEARNED_ROUTE_FOR_EACH_EQUAL (sb_route, filter,
sbrec_learned_route_by_datapath) {
/* If the port is not local we don't care about it.
* Some other ovn-controller will handle it.
* We may not use smap_get since the value might be validly NULL. */
if (!smap_get_node(bound_ports,
sb_route->logical_port->logical_port)) {
continue;
}
route_add_entry(&sync_routes, sb_route);
}
sbrec_learned_route_index_destroy_row(filter);
struct re_nl_received_route_node *learned_route;
VECTOR_FOR_EACH_PTR (learned_routes, learned_route) {
char *ip_prefix = normalize_v46_prefix(&learned_route->prefix,
learned_route->plen);
char *nexthop = normalize_v46(&learned_route->nexthop);
struct smap_node *port_node;
SMAP_FOR_EACH (port_node, bound_ports) {
/* The user specified an ifname, but we learned it on a different
* port. */
if (port_node->value && strcmp(port_node->value,
learned_route->ifname)) {
continue;
}
const struct sbrec_port_binding *logical_port =
lport_lookup_by_name(sbrec_port_binding_by_name,
port_node->key);
if (!logical_port) {
continue;
}
route_e = route_lookup(&sync_routes, datapath,
logical_port, ip_prefix, nexthop);
if (route_e) {
hmap_remove(&sync_routes, &route_e->hmap_node);
free(route_e);
} else {
if (!ovnsb_idl_txn) {
*sb_changes_pending = true;
continue;
}
sb_route = sbrec_learned_route_insert(ovnsb_idl_txn);
sbrec_learned_route_set_datapath(sb_route, datapath);
sbrec_learned_route_set_logical_port(sb_route, logical_port);
sbrec_learned_route_set_ip_prefix(sb_route, ip_prefix);
sbrec_learned_route_set_nexthop(sb_route, nexthop);
}
}
free(ip_prefix);
free(nexthop);
}
HMAP_FOR_EACH_POP (route_e, hmap_node, &sync_routes) {
sbrec_learned_route_delete(route_e->sb_route);
free(route_e);
}
hmap_destroy(&sync_routes);
}
/* Last route_exchange netlink operation. */
static int route_exchange_nl_status;
#define CLEAR_ROUTE_EXCHANGE_NL_STATUS() \
do { \
route_exchange_nl_status = 0; \
} while (0)
#define SET_ROUTE_EXCHANGE_NL_STATUS(error) \
do { \
if (!route_exchange_nl_status) { \
route_exchange_nl_status = (error); \
if (error) { \
poll_immediate_wake(); \
} \
} \
} while (0)
void
route_exchange_run(const struct route_exchange_ctx_in *r_ctx_in,
struct route_exchange_ctx_out *r_ctx_out)
{
struct sset old_maintained_vrfs = SSET_INITIALIZER(&old_maintained_vrfs);
sset_swap(&_maintained_vrfs, &old_maintained_vrfs);
struct hmap old_maintained_route_table =
HMAP_INITIALIZER(&old_maintained_route_table);
hmap_swap(&_maintained_route_tables, &old_maintained_route_table);
int error;
CLEAR_ROUTE_EXCHANGE_NL_STATUS();
const struct advertise_datapath_entry *ad;
HMAP_FOR_EACH (ad, node, r_ctx_in->announce_routes) {
uint32_t table_id = ad->db->tunnel_key;
if (ad->maintain_vrf) {
if (!sset_contains(&old_maintained_vrfs, ad->vrf_name)) {
error = re_nl_create_vrf(ad->vrf_name, table_id);
if (error && error != EEXIST) {
VLOG_WARN_RL(&rl,
"Unable to create VRF %s for datapath "
"%"PRIi32": %s.",
ad->vrf_name, table_id,
ovs_strerror(error));
SET_ROUTE_EXCHANGE_NL_STATUS(error);
continue;
}
}
sset_add(&_maintained_vrfs, ad->vrf_name);
} else {
/* A previous maintain-vrf flag was removed. We should therefore
* also not delete it even if we created it previously. */
sset_find_and_delete(&_maintained_vrfs, ad->vrf_name);
sset_find_and_delete(&old_maintained_vrfs, ad->vrf_name);
}
maintained_route_table_add(table_id);
struct vector received_routes =
VECTOR_EMPTY_INITIALIZER(struct re_nl_received_route_node);
error = re_nl_sync_routes(ad->db->tunnel_key, &ad->routes,
&received_routes, ad->db);
SET_ROUTE_EXCHANGE_NL_STATUS(error);
sb_sync_learned_routes(&received_routes, ad->db,
&ad->bound_ports, r_ctx_in->ovnsb_idl_txn,
r_ctx_in->sbrec_port_binding_by_name,
r_ctx_in->sbrec_learned_route_by_datapath,
&r_ctx_out->sb_changes_pending);
route_table_add_watch_request(&r_ctx_out->route_table_watches,
table_id);
vector_destroy(&received_routes);
}
/* Remove routes in tables previously maintained by us. */
struct maintained_route_table_entry *mrt;
HMAP_FOR_EACH_POP (mrt, node, &old_maintained_route_table) {
if (!maintained_route_table_contains(mrt->table_id)) {
error = re_nl_cleanup_routes(mrt->table_id);
if (error) {
/* If netlink transaction fails, we will retry next time. */
maintained_route_table_add(mrt->table_id);
SET_ROUTE_EXCHANGE_NL_STATUS(error);
}
}
free(mrt);
}
hmap_destroy(&old_maintained_route_table);
/* Remove VRFs previously maintained by us not found in the above loop. */
const char *vrf_name;
SSET_FOR_EACH_SAFE (vrf_name, &old_maintained_vrfs) {
if (!sset_contains(&_maintained_vrfs, vrf_name)) {
error = re_nl_delete_vrf(vrf_name);
if (error && error != ENODEV) {
/* If netlink transaction fails, we will retry next time. */
sset_add(&_maintained_vrfs, vrf_name);
SET_ROUTE_EXCHANGE_NL_STATUS(error);
}
}
sset_delete(&old_maintained_vrfs, SSET_NODE_FROM_NAME(vrf_name));
}
sset_destroy(&old_maintained_vrfs);
}
void
route_exchange_cleanup_vrfs(void)
{
struct maintained_route_table_entry *mrt;
HMAP_FOR_EACH (mrt, node, &_maintained_route_tables) {
re_nl_cleanup_routes(mrt->table_id);
}
const char *vrf_name;
SSET_FOR_EACH (vrf_name, &_maintained_vrfs) {
re_nl_delete_vrf(vrf_name);
}
}
void
route_exchange_destroy(void)
{
struct maintained_route_table_entry *mrt;
HMAP_FOR_EACH_POP (mrt, node, &_maintained_route_tables) {
free(mrt);
}
const char *vrf_name;
SSET_FOR_EACH_SAFE (vrf_name, &_maintained_vrfs) {
sset_delete(&_maintained_vrfs, SSET_NODE_FROM_NAME(vrf_name));
}
sset_destroy(&_maintained_vrfs);
hmap_destroy(&_maintained_route_tables);
}
int
route_exchange_status_run(void)
{
return route_exchange_nl_status;
}
|