You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
11 KiB

  1. From fef2ed139d140f551cdfcbb21c5a023dea2e02cb Mon Sep 17 00:00:00 2001
  2. From: Philippe Guibert <philippe.guibert@6wind.com>
  3. Date: Thu, 26 Mar 2020 17:33:53 +0100
  4. Subject: [PATCH] nhrpd: cache config may disappear if iface not present at
  5. startup
  6. When interface not present at config time, store separately the list of
  7. config parameters. Then, when interface is ready and an address has been configured, the nbma setting is done. Reversely, when interface disappears,
  8. there is no need to keep the maps present, then keep only the configuration.
  9. Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
  10. ---
  11. nhrpd/nhrp_cache.c | 86 ++++++++++++++++++++++++++++++++++++++++++
  12. nhrpd/nhrp_interface.c | 63 ++++++++++++++++++++++++++++++-
  13. nhrpd/nhrp_vty.c | 49 ++++++++++++++++--------
  14. nhrpd/nhrpd.h | 14 +++++++
  15. 4 files changed, 195 insertions(+), 17 deletions(-)
  16. --- a/nhrpd/nhrp_cache.c
  17. +++ b/nhrpd/nhrp_cache.c
  18. @@ -16,6 +16,7 @@
  19. #include "netlink.h"
  20. DEFINE_MTYPE_STATIC(NHRPD, NHRP_CACHE, "NHRP cache entry")
  21. +DEFINE_MTYPE_STATIC(NHRPD, NHRP_CACHE_CONFIG, "NHRP cache config entry")
  22. unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES];
  23. @@ -77,6 +78,68 @@ static void nhrp_cache_free(struct nhrp_
  24. XFREE(MTYPE_NHRP_CACHE, c);
  25. }
  26. +static unsigned int nhrp_cache_config_protocol_key(const void *peer_data)
  27. +{
  28. + const struct nhrp_cache_config *p = peer_data;
  29. + return sockunion_hash(&p->remote_addr);
  30. +}
  31. +
  32. +static bool nhrp_cache_config_protocol_cmp(const void *cache_data,
  33. + const void *key_data)
  34. +{
  35. + const struct nhrp_cache_config *a = cache_data;
  36. + const struct nhrp_cache_config *b = key_data;
  37. +
  38. + if (!sockunion_same(&a->remote_addr, &b->remote_addr))
  39. + return false;
  40. + if (a->ifp != b->ifp)
  41. + return false;
  42. + return true;
  43. +}
  44. +
  45. +static void *nhrp_cache_config_alloc(void *data)
  46. +{
  47. + struct nhrp_cache_config *p, *key = data;
  48. +
  49. + p = XCALLOC(MTYPE_NHRP_CACHE_CONFIG, sizeof(struct nhrp_cache_config));
  50. +
  51. + *p = (struct nhrp_cache_config){
  52. + .remote_addr = key->remote_addr,
  53. + .ifp = key->ifp,
  54. + };
  55. + return p;
  56. +}
  57. +
  58. +void nhrp_cache_config_free(struct nhrp_cache_config *c)
  59. +{
  60. + struct nhrp_interface *nifp = c->ifp->info;
  61. +
  62. + hash_release(nifp->cache_config_hash, c);
  63. + XFREE(MTYPE_NHRP_CACHE_CONFIG, c);
  64. +}
  65. +
  66. +struct nhrp_cache_config *nhrp_cache_config_get(struct interface *ifp,
  67. + union sockunion *remote_addr,
  68. + int create)
  69. +{
  70. + struct nhrp_interface *nifp = ifp->info;
  71. + struct nhrp_cache_config key;
  72. +
  73. + if (!nifp->cache_config_hash) {
  74. + nifp->cache_config_hash =
  75. + hash_create(nhrp_cache_config_protocol_key,
  76. + nhrp_cache_config_protocol_cmp,
  77. + "NHRP Config Cache");
  78. + if (!nifp->cache_config_hash)
  79. + return NULL;
  80. + }
  81. + key.remote_addr = *remote_addr;
  82. + key.ifp = ifp;
  83. +
  84. + return hash_get(nifp->cache_config_hash, &key,
  85. + create ? nhrp_cache_config_alloc : NULL);
  86. +}
  87. +
  88. struct nhrp_cache *nhrp_cache_get(struct interface *ifp,
  89. union sockunion *remote_addr, int create)
  90. {
  91. @@ -423,12 +486,23 @@ struct nhrp_cache_iterator_ctx {
  92. void *ctx;
  93. };
  94. +struct nhrp_cache_config_iterator_ctx {
  95. + void (*cb)(struct nhrp_cache_config *, void *);
  96. + void *ctx;
  97. +};
  98. +
  99. static void nhrp_cache_iterator(struct hash_bucket *b, void *ctx)
  100. {
  101. struct nhrp_cache_iterator_ctx *ic = ctx;
  102. ic->cb(b->data, ic->ctx);
  103. }
  104. +static void nhrp_cache_config_iterator(struct hash_bucket *b, void *ctx)
  105. +{
  106. + struct nhrp_cache_config_iterator_ctx *ic = ctx;
  107. + ic->cb(b->data, ic->ctx);
  108. +}
  109. +
  110. void nhrp_cache_foreach(struct interface *ifp,
  111. void (*cb)(struct nhrp_cache *, void *), void *ctx)
  112. {
  113. @@ -441,6 +515,18 @@ void nhrp_cache_foreach(struct interface
  114. hash_iterate(nifp->cache_hash, nhrp_cache_iterator, &ic);
  115. }
  116. +void nhrp_cache_config_foreach(struct interface *ifp,
  117. + void (*cb)(struct nhrp_cache_config *, void *), void *ctx)
  118. +{
  119. + struct nhrp_interface *nifp = ifp->info;
  120. + struct nhrp_cache_config_iterator_ctx ic = {
  121. + .cb = cb, .ctx = ctx,
  122. + };
  123. +
  124. + if (nifp->cache_config_hash)
  125. + hash_iterate(nifp->cache_config_hash, nhrp_cache_config_iterator, &ic);
  126. +}
  127. +
  128. void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *n,
  129. notifier_fn_t fn)
  130. {
  131. --- a/nhrpd/nhrp_interface.c
  132. +++ b/nhrpd/nhrp_interface.c
  133. @@ -23,6 +23,10 @@
  134. DEFINE_MTYPE_STATIC(NHRPD, NHRP_IF, "NHRP interface")
  135. +static void nhrp_interface_update_cache_config(struct interface *ifp,
  136. + bool available,
  137. + uint8_t family);
  138. +
  139. static int nhrp_if_new_hook(struct interface *ifp)
  140. {
  141. struct nhrp_interface *nifp;
  142. @@ -311,11 +315,68 @@ int nhrp_ifp_destroy(struct interface *i
  143. {
  144. debugf(NHRP_DEBUG_IF, "if-delete: %s", ifp->name);
  145. + nhrp_interface_update_cache_config(ifp, false, AF_INET);
  146. + nhrp_interface_update_cache_config(ifp, false, AF_INET6);
  147. nhrp_interface_update(ifp);
  148. return 0;
  149. }
  150. +struct map_ctx {
  151. + int family;
  152. + bool enabled;
  153. +};
  154. +
  155. +static void interface_config_update_nhrp_map(struct nhrp_cache_config *cc, void *data)
  156. +{
  157. + struct map_ctx *ctx = data;
  158. + struct interface *ifp = cc->ifp;
  159. + struct nhrp_cache *c;
  160. + union sockunion nbma_addr;
  161. +
  162. + if (sockunion_family(&cc->remote_addr) != ctx->family)
  163. + return;
  164. +
  165. + /* gre layer not ready */
  166. + if (ifp->vrf_id == VRF_UNKNOWN)
  167. + return;
  168. +
  169. + c = nhrp_cache_get(ifp, &cc->remote_addr, ctx->enabled ? 1 : 0);
  170. + if (!c && !ctx->enabled)
  171. + return;
  172. + /* suppress */
  173. + if (!ctx->enabled) {
  174. + if (c && c->map) {
  175. + nhrp_cache_update_binding(c, c->cur.type, -1,
  176. + nhrp_peer_get(ifp, &nbma_addr), 0, NULL);
  177. + }
  178. + return;
  179. + }
  180. + /* create */
  181. + c->map = 1;
  182. + if (cc->type == NHRP_CACHE_LOCAL)
  183. + nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0,
  184. + NULL);
  185. + else {
  186. + nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0,
  187. + nhrp_peer_get(ifp, &cc->nbma), 0,
  188. + NULL);
  189. + }
  190. +}
  191. +
  192. +static void nhrp_interface_update_cache_config(struct interface *ifp, bool available, uint8_t family)
  193. +{
  194. + struct map_ctx mapctx;
  195. +
  196. + mapctx = (struct map_ctx){
  197. + .family = family,
  198. + .enabled = available
  199. + };
  200. + nhrp_cache_config_foreach(ifp, interface_config_update_nhrp_map,
  201. + &mapctx);
  202. +
  203. +}
  204. +
  205. int nhrp_ifp_up(struct interface *ifp)
  206. {
  207. debugf(NHRP_DEBUG_IF, "if-up: %s", ifp->name);
  208. @@ -346,7 +407,7 @@ int nhrp_interface_address_add(ZAPI_CALL
  209. nhrp_interface_update_address(
  210. ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
  211. -
  212. + nhrp_interface_update_cache_config(ifc->ifp, true, PREFIX_FAMILY(ifc->address));
  213. return 0;
  214. }
  215. --- a/nhrpd/nhrp_vty.c
  216. +++ b/nhrpd/nhrp_vty.c
  217. @@ -494,28 +494,42 @@ DEFUN(if_nhrp_map, if_nhrp_map_cmd,
  218. VTY_DECLVAR_CONTEXT(interface, ifp);
  219. afi_t afi = cmd_to_afi(argv[0]);
  220. union sockunion proto_addr, nbma_addr;
  221. + struct nhrp_cache_config *cc;
  222. struct nhrp_cache *c;
  223. + enum nhrp_cache_type type;
  224. if (str2sockunion(argv[3]->arg, &proto_addr) < 0
  225. || afi2family(afi) != sockunion_family(&proto_addr))
  226. return nhrp_vty_return(vty, NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH);
  227. + if (strmatch(argv[4]->text, "local"))
  228. + type = NHRP_CACHE_LOCAL;
  229. + else {
  230. + if (str2sockunion(argv[4]->arg, &nbma_addr) < 0)
  231. + return nhrp_vty_return(vty, NHRP_ERR_FAIL);
  232. + type = NHRP_CACHE_STATIC;
  233. + }
  234. + cc = nhrp_cache_config_get(ifp, &proto_addr, 1);
  235. + if (!cc)
  236. + return nhrp_vty_return(vty, NHRP_ERR_FAIL);
  237. + cc->nbma = nbma_addr;
  238. + cc->type = type;
  239. + /* gre layer not ready */
  240. + if (ifp->ifindex == IFINDEX_INTERNAL)
  241. + return CMD_SUCCESS;
  242. +
  243. c = nhrp_cache_get(ifp, &proto_addr, 1);
  244. if (!c)
  245. return nhrp_vty_return(vty, NHRP_ERR_FAIL);
  246. c->map = 1;
  247. - if (strmatch(argv[4]->text, "local")) {
  248. + if (type == NHRP_CACHE_LOCAL)
  249. nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0,
  250. NULL);
  251. - } else {
  252. - if (str2sockunion(argv[4]->arg, &nbma_addr) < 0)
  253. - return nhrp_vty_return(vty, NHRP_ERR_FAIL);
  254. + else
  255. nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0,
  256. nhrp_peer_get(ifp, &nbma_addr), 0,
  257. NULL);
  258. - }
  259. -
  260. return CMD_SUCCESS;
  261. }
  262. @@ -533,15 +547,22 @@ DEFUN(if_no_nhrp_map, if_no_nhrp_map_cmd
  263. VTY_DECLVAR_CONTEXT(interface, ifp);
  264. afi_t afi = cmd_to_afi(argv[1]);
  265. union sockunion proto_addr, nbma_addr;
  266. + struct nhrp_cache_config *cc;
  267. struct nhrp_cache *c;
  268. if (str2sockunion(argv[4]->arg, &proto_addr) < 0
  269. || afi2family(afi) != sockunion_family(&proto_addr))
  270. return nhrp_vty_return(vty, NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH);
  271. + cc = nhrp_cache_config_get(ifp, &proto_addr, 0);
  272. + if (!cc)
  273. + return nhrp_vty_return(vty, NHRP_ERR_FAIL);
  274. + nhrp_cache_config_free(cc);
  275. +
  276. c = nhrp_cache_get(ifp, &proto_addr, 0);
  277. + /* silently return */
  278. if (!c || !c->map)
  279. - return nhrp_vty_return(vty, NHRP_ERR_ENTRY_NOT_FOUND);
  280. + return CMD_SUCCESS;
  281. nhrp_cache_update_binding(c, c->cur.type, -1,
  282. nhrp_peer_get(ifp, &nbma_addr), 0, NULL);
  283. @@ -997,23 +1018,19 @@ struct write_map_ctx {
  284. const char *aficmd;
  285. };
  286. -static void interface_config_write_nhrp_map(struct nhrp_cache *c, void *data)
  287. +static void interface_config_write_nhrp_map(struct nhrp_cache_config *c, void *data)
  288. {
  289. struct write_map_ctx *ctx = data;
  290. struct vty *vty = ctx->vty;
  291. char buf[2][SU_ADDRSTRLEN];
  292. - if (!c->map)
  293. - return;
  294. if (sockunion_family(&c->remote_addr) != ctx->family)
  295. return;
  296. vty_out(vty, " %s nhrp map %s %s\n", ctx->aficmd,
  297. sockunion2str(&c->remote_addr, buf[0], sizeof(buf[0])),
  298. - c->cur.type == NHRP_CACHE_LOCAL
  299. - ? "local"
  300. - : sockunion2str(&c->cur.peer->vc->remote.nbma, buf[1],
  301. - sizeof(buf[1])));
  302. + c->type == NHRP_CACHE_LOCAL
  303. + ? "local" : sockunion2str(&c->nbma, buf[1], sizeof(buf[1])));
  304. }
  305. static int interface_config_write(struct vty *vty)
  306. @@ -1076,8 +1093,8 @@ static int interface_config_write(struct
  307. .family = afi2family(afi),
  308. .aficmd = aficmd,
  309. };
  310. - nhrp_cache_foreach(ifp, interface_config_write_nhrp_map,
  311. - &mapctx);
  312. + nhrp_cache_config_foreach(ifp, interface_config_write_nhrp_map,
  313. + &mapctx);
  314. list_for_each_entry(nhs, &ad->nhslist_head,
  315. nhslist_entry)
  316. --- a/nhrpd/nhrpd.h
  317. +++ b/nhrpd/nhrpd.h
  318. @@ -197,6 +197,13 @@ enum nhrp_cache_type {
  319. extern const char *const nhrp_cache_type_str[];
  320. extern unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES];
  321. +struct nhrp_cache_config {
  322. + struct interface *ifp;
  323. + union sockunion remote_addr;
  324. + enum nhrp_cache_type type;
  325. + union sockunion nbma;
  326. +};
  327. +
  328. struct nhrp_cache {
  329. struct interface *ifp;
  330. union sockunion remote_addr;
  331. @@ -280,6 +287,7 @@ struct nhrp_interface {
  332. uint32_t grekey;
  333. struct hash *peer_hash;
  334. + struct hash *cache_config_hash;
  335. struct hash *cache_hash;
  336. struct notifier_list notifier_list;
  337. @@ -358,10 +366,16 @@ void nhrp_shortcut_foreach(afi_t afi,
  338. void nhrp_shortcut_purge(struct nhrp_shortcut *s, int force);
  339. void nhrp_shortcut_prefix_change(const struct prefix *p, int deleted);
  340. +void nhrp_cache_config_free(struct nhrp_cache_config *c);
  341. +struct nhrp_cache_config *nhrp_cache_config_get(struct interface *ifp,
  342. + union sockunion *remote_addr,
  343. + int create);
  344. struct nhrp_cache *nhrp_cache_get(struct interface *ifp,
  345. union sockunion *remote_addr, int create);
  346. void nhrp_cache_foreach(struct interface *ifp,
  347. void (*cb)(struct nhrp_cache *, void *), void *ctx);
  348. +void nhrp_cache_config_foreach(struct interface *ifp,
  349. + void (*cb)(struct nhrp_cache_config *, void *), void *ctx);
  350. void nhrp_cache_set_used(struct nhrp_cache *, int);
  351. int nhrp_cache_update_binding(struct nhrp_cache *, enum nhrp_cache_type type,
  352. int holding_time, struct nhrp_peer *p,