PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/netinet/in_rmx.c

https://bitbucket.org/cooljeanius/dragonflybsd
C | 459 lines | 271 code | 48 blank | 140 comment | 54 complexity | aba4240913a2e0290c224845479bf0b4 MD5 | raw file
  1. /*
  2. * Copyright 1994, 1995 Massachusetts Institute of Technology
  3. *
  4. * Permission to use, copy, modify, and distribute this software and
  5. * its documentation for any purpose and without fee is hereby
  6. * granted, provided that both the above copyright notice and this
  7. * permission notice appear in all copies, that both the above
  8. * copyright notice and this permission notice appear in all
  9. * supporting documentation, and that the name of M.I.T. not be used
  10. * in advertising or publicity pertaining to distribution of the
  11. * software without specific, written prior permission. M.I.T. makes
  12. * no representations about the suitability of this software for any
  13. * purpose. It is provided "as is" without express or implied
  14. * warranty.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
  17. * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
  20. * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  26. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.3 2002/08/09 14:49:23 ru Exp $
  30. * $DragonFly: src/sys/netinet/in_rmx.c,v 1.14 2006/04/11 06:59:34 dillon Exp $
  31. */
  32. /*
  33. * This code does two things necessary for the enhanced TCP metrics to
  34. * function in a useful manner:
  35. * 1) It marks all non-host routes as `cloning', thus ensuring that
  36. * every actual reference to such a route actually gets turned
  37. * into a reference to a host route to the specific destination
  38. * requested.
  39. * 2) When such routes lose all their references, it arranges for them
  40. * to be deleted in some random collection of circumstances, so that
  41. * a large quantity of stale routing data is not kept in kernel memory
  42. * indefinitely. See in_rtqtimo() below for the exact mechanism.
  43. */
  44. #include <sys/param.h>
  45. #include <sys/systm.h>
  46. #include <sys/kernel.h>
  47. #include <sys/sysctl.h>
  48. #include <sys/socket.h>
  49. #include <sys/mbuf.h>
  50. #include <sys/syslog.h>
  51. #include <sys/globaldata.h>
  52. #include <sys/thread2.h>
  53. #include <net/if.h>
  54. #include <net/route.h>
  55. #include <net/if_var.h>
  56. #include <netinet/in.h>
  57. #include <netinet/in_var.h>
  58. #include <netinet/ip_var.h>
  59. #include <netinet/ip_flow.h>
  60. #define RTPRF_EXPIRING RTF_PROTO3 /* set on routes we manage */
  61. static struct callout in_rtqtimo_ch[MAXCPU];
  62. /*
  63. * Do what we need to do when inserting a route.
  64. */
  65. static struct radix_node *
  66. in_addroute(char *key, char *mask, struct radix_node_head *head,
  67. struct radix_node *treenodes)
  68. {
  69. struct rtentry *rt = (struct rtentry *)treenodes;
  70. struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
  71. struct radix_node *ret;
  72. struct in_ifaddr_container *iac;
  73. struct in_ifaddr *ia;
  74. /*
  75. * For IP, mark routes to multicast addresses as such, because
  76. * it's easy to do and might be useful (but this is much more
  77. * dubious since it's so easy to inspect the address).
  78. *
  79. * For IP, all unicast non-host routes are automatically cloning.
  80. */
  81. if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
  82. rt->rt_flags |= RTF_MULTICAST;
  83. if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST)))
  84. rt->rt_flags |= RTF_PRCLONING;
  85. /*
  86. * For host routes, we make sure that RTF_BROADCAST
  87. * is set for anything that looks like a broadcast address.
  88. * This way, we can avoid an expensive call to in_broadcast()
  89. * in ip_output() most of the time (because the route passed
  90. * to ip_output() is almost always a host route).
  91. *
  92. * For local routes we set RTF_LOCAL allowing various shortcuts.
  93. *
  94. * A cloned network route will point to one of several possible
  95. * addresses if an interface has aliases and must be repointed
  96. * back to the correct address or arp_rtrequest() will not properly
  97. * detect the local ip.
  98. */
  99. if (rt->rt_flags & RTF_HOST) {
  100. if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
  101. rt->rt_flags |= RTF_BROADCAST;
  102. } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
  103. sin->sin_addr.s_addr) {
  104. rt->rt_flags |= RTF_LOCAL;
  105. } else {
  106. LIST_FOREACH(iac, INADDR_HASH(sin->sin_addr.s_addr),
  107. ia_hash) {
  108. ia = iac->ia;
  109. if (sin->sin_addr.s_addr ==
  110. ia->ia_addr.sin_addr.s_addr) {
  111. rt->rt_flags |= RTF_LOCAL;
  112. IFAREF(&ia->ia_ifa);
  113. IFAFREE(rt->rt_ifa);
  114. rt->rt_ifa = &ia->ia_ifa;
  115. rt->rt_ifp = rt->rt_ifa->ifa_ifp;
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. if (rt->rt_rmx.rmx_mtu != 0 && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
  122. rt->rt_ifp != NULL)
  123. rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  124. ret = rn_addroute(key, mask, head, treenodes);
  125. if (ret == NULL && (rt->rt_flags & RTF_HOST)) {
  126. struct rtentry *oldrt;
  127. /*
  128. * We are trying to add a host route, but can't.
  129. * Find out if it is because of an ARP entry and
  130. * delete it if so.
  131. */
  132. oldrt = rtpurelookup((struct sockaddr *)sin);
  133. if (oldrt != NULL) {
  134. --oldrt->rt_refcnt;
  135. if ((oldrt->rt_flags & RTF_LLINFO) &&
  136. (oldrt->rt_flags & RTF_HOST) &&
  137. oldrt->rt_gateway &&
  138. oldrt->rt_gateway->sa_family == AF_LINK) {
  139. rtrequest(RTM_DELETE, rt_key(oldrt),
  140. oldrt->rt_gateway, rt_mask(oldrt),
  141. oldrt->rt_flags, NULL);
  142. ret = rn_addroute(key, mask, head, treenodes);
  143. }
  144. }
  145. }
  146. /*
  147. * If the new route has been created successfully, and it is
  148. * not a multicast/broadcast or cloned route, then we will
  149. * have to flush the ipflow. Otherwise, we may end up using
  150. * the wrong route.
  151. */
  152. if (ret != NULL &&
  153. (rt->rt_flags &
  154. (RTF_MULTICAST | RTF_BROADCAST | RTF_WASCLONED)) == 0) {
  155. ipflow_flush_oncpu();
  156. }
  157. return ret;
  158. }
  159. /*
  160. * This code is the inverse of in_closeroute: on first reference, if we
  161. * were managing the route, stop doing so and set the expiration timer
  162. * back off again.
  163. */
  164. static struct radix_node *
  165. in_matchroute(char *key, struct radix_node_head *head)
  166. {
  167. struct radix_node *rn = rn_match(key, head);
  168. struct rtentry *rt = (struct rtentry *)rn;
  169. if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
  170. if (rt->rt_flags & RTPRF_EXPIRING) {
  171. rt->rt_flags &= ~RTPRF_EXPIRING;
  172. rt->rt_rmx.rmx_expire = 0;
  173. }
  174. }
  175. return rn;
  176. }
  177. static int rtq_reallyold = 60*60; /* one hour is ``really old'' */
  178. SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
  179. &rtq_reallyold , 0,
  180. "Default expiration time on cloned routes");
  181. static int rtq_minreallyold = 10; /* never automatically crank down to less */
  182. SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
  183. &rtq_minreallyold , 0,
  184. "Minimum time to attempt to hold onto cloned routes");
  185. static int rtq_toomany = 128; /* 128 cached routes is ``too many'' */
  186. SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
  187. &rtq_toomany , 0, "Upper limit on cloned routes");
  188. /*
  189. * On last reference drop, mark the route as belong to us so that it can be
  190. * timed out.
  191. */
  192. static void
  193. in_closeroute(struct radix_node *rn, struct radix_node_head *head)
  194. {
  195. struct rtentry *rt = (struct rtentry *)rn;
  196. if (!(rt->rt_flags & RTF_UP))
  197. return; /* prophylactic measures */
  198. if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
  199. return;
  200. if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_EXPIRING)) != RTF_WASCLONED)
  201. return;
  202. /*
  203. * As requested by David Greenman:
  204. * If rtq_reallyold is 0, just delete the route without
  205. * waiting for a timeout cycle to kill it.
  206. */
  207. if (rtq_reallyold != 0) {
  208. rt->rt_flags |= RTPRF_EXPIRING;
  209. rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
  210. } else {
  211. /*
  212. * Remove route from the radix tree, but defer deallocation
  213. * until we return to rtfree().
  214. */
  215. rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
  216. rt->rt_flags, &rt);
  217. }
  218. }
  219. struct rtqk_arg {
  220. struct radix_node_head *rnh;
  221. int draining;
  222. int killed;
  223. int found;
  224. int updating;
  225. time_t nextstop;
  226. };
  227. /*
  228. * Get rid of old routes. When draining, this deletes everything, even when
  229. * the timeout is not expired yet. When updating, this makes sure that
  230. * nothing has a timeout longer than the current value of rtq_reallyold.
  231. */
  232. static int
  233. in_rtqkill(struct radix_node *rn, void *rock)
  234. {
  235. struct rtqk_arg *ap = rock;
  236. struct rtentry *rt = (struct rtentry *)rn;
  237. int err;
  238. if (rt->rt_flags & RTPRF_EXPIRING) {
  239. ap->found++;
  240. if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
  241. if (rt->rt_refcnt > 0)
  242. panic("rtqkill route really not free");
  243. err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
  244. rt_mask(rt), rt->rt_flags, NULL);
  245. if (err)
  246. log(LOG_WARNING, "in_rtqkill: error %d\n", err);
  247. else
  248. ap->killed++;
  249. } else {
  250. if (ap->updating &&
  251. (rt->rt_rmx.rmx_expire - time_second >
  252. rtq_reallyold)) {
  253. rt->rt_rmx.rmx_expire = time_second +
  254. rtq_reallyold;
  255. }
  256. ap->nextstop = lmin(ap->nextstop,
  257. rt->rt_rmx.rmx_expire);
  258. }
  259. }
  260. return 0;
  261. }
  262. #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
  263. static int rtq_timeout = RTQ_TIMEOUT;
  264. static void
  265. in_rtqtimo(void *rock)
  266. {
  267. struct radix_node_head *rnh = rock;
  268. struct rtqk_arg arg;
  269. struct timeval atv;
  270. static time_t last_adjusted_timeout = 0;
  271. arg.found = arg.killed = 0;
  272. arg.rnh = rnh;
  273. arg.nextstop = time_second + rtq_timeout;
  274. arg.draining = arg.updating = 0;
  275. crit_enter();
  276. rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  277. crit_exit();
  278. /*
  279. * Attempt to be somewhat dynamic about this:
  280. * If there are ``too many'' routes sitting around taking up space,
  281. * then crank down the timeout, and see if we can't make some more
  282. * go away. However, we make sure that we will never adjust more
  283. * than once in rtq_timeout seconds, to keep from cranking down too
  284. * hard.
  285. */
  286. if ((arg.found - arg.killed > rtq_toomany) &&
  287. (time_second - last_adjusted_timeout >= rtq_timeout) &&
  288. rtq_reallyold > rtq_minreallyold) {
  289. rtq_reallyold = 2*rtq_reallyold / 3;
  290. if (rtq_reallyold < rtq_minreallyold) {
  291. rtq_reallyold = rtq_minreallyold;
  292. }
  293. last_adjusted_timeout = time_second;
  294. #ifdef DIAGNOSTIC
  295. log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
  296. rtq_reallyold);
  297. #endif
  298. arg.found = arg.killed = 0;
  299. arg.updating = 1;
  300. crit_enter();
  301. rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  302. crit_exit();
  303. }
  304. atv.tv_usec = 0;
  305. atv.tv_sec = arg.nextstop - time_second;
  306. callout_reset(&in_rtqtimo_ch[mycpuid], tvtohz_high(&atv), in_rtqtimo,
  307. rock);
  308. }
  309. void
  310. in_rtqdrain(void)
  311. {
  312. struct radix_node_head *rnh = rt_tables[mycpuid][AF_INET];
  313. struct rtqk_arg arg;
  314. arg.found = arg.killed = 0;
  315. arg.rnh = rnh;
  316. arg.nextstop = 0;
  317. arg.draining = 1;
  318. arg.updating = 0;
  319. crit_enter();
  320. rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  321. crit_exit();
  322. }
  323. /*
  324. * Initialize our routing tree.
  325. */
  326. int
  327. in_inithead(void **head, int off)
  328. {
  329. struct radix_node_head *rnh;
  330. if (!rn_inithead(head, rn_cpumaskhead(mycpuid), off))
  331. return 0;
  332. if (head != (void **)&rt_tables[mycpuid][AF_INET]) /* BOGUS! */
  333. return 1; /* only do this for the real routing table */
  334. rnh = *head;
  335. rnh->rnh_addaddr = in_addroute;
  336. rnh->rnh_matchaddr = in_matchroute;
  337. rnh->rnh_close = in_closeroute;
  338. callout_init(&in_rtqtimo_ch[mycpuid]);
  339. in_rtqtimo(rnh); /* kick off timeout first time */
  340. return 1;
  341. }
  342. /*
  343. * This zaps old routes when the interface goes down or interface
  344. * address is deleted. In the latter case, it deletes static routes
  345. * that point to this address. If we don't do this, we may end up
  346. * using the old address in the future. The ones we always want to
  347. * get rid of are things like ARP entries, since the user might down
  348. * the interface, walk over to a completely different network, and
  349. * plug back in.
  350. *
  351. * in_ifadown() is typically called when an interface is being brought
  352. * down. We must iterate through all per-cpu route tables and clean
  353. * them up.
  354. */
  355. struct in_ifadown_arg {
  356. struct radix_node_head *rnh;
  357. struct ifaddr *ifa;
  358. int del;
  359. };
  360. static int
  361. in_ifadownkill(struct radix_node *rn, void *xap)
  362. {
  363. struct in_ifadown_arg *ap = xap;
  364. struct rtentry *rt = (struct rtentry *)rn;
  365. int err;
  366. if (rt->rt_ifa == ap->ifa &&
  367. (ap->del || !(rt->rt_flags & RTF_STATIC))) {
  368. /*
  369. * We need to disable the automatic prune that happens
  370. * in this case in rtrequest() because it will blow
  371. * away the pointers that rn_walktree() needs in order
  372. * continue our descent. We will end up deleting all
  373. * the routes that rtrequest() would have in any case,
  374. * so that behavior is not needed there.
  375. */
  376. rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
  377. err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
  378. rt_mask(rt), rt->rt_flags, NULL);
  379. if (err)
  380. log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
  381. }
  382. return 0;
  383. }
  384. int
  385. in_ifadown(struct ifaddr *ifa, int delete)
  386. {
  387. struct in_ifadown_arg arg;
  388. struct radix_node_head *rnh;
  389. int origcpu;
  390. int cpu;
  391. if (ifa->ifa_addr->sa_family != AF_INET)
  392. return 1;
  393. /*
  394. * XXX individual requests are not independantly chained,
  395. * which means that the per-cpu route tables will not be
  396. * consistent in the middle of the operation. If routes
  397. * related to the interface are manipulated while we are
  398. * doing this the inconsistancy could trigger a panic.
  399. */
  400. origcpu = mycpuid;
  401. for (cpu = 0; cpu < ncpus; cpu++) {
  402. lwkt_migratecpu(cpu);
  403. arg.rnh = rnh = rt_tables[cpu][AF_INET];
  404. arg.ifa = ifa;
  405. arg.del = delete;
  406. rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
  407. ifa->ifa_flags &= ~IFA_ROUTE;
  408. }
  409. lwkt_migratecpu(origcpu);
  410. return 0;
  411. }