From: bluhm Date: Fri, 9 Feb 2024 14:02:11 +0000 (+0000) Subject: Route cache function returns hit or miss. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=18747b914d090bf4a4f4b86118b544251540fce7;p=openbsd Route cache function returns hit or miss. The route_cache() function can easily return whether it was a cache hit or miss. Then the logic to perform a route lookup gets a bit simpler. Some more complicated if (ro->ro_rt == NULL) checks still exist elsewhere. Also use route cache in in_pcbselsrc() instead of filling struct route manually. OK claudio@ --- diff --git a/sys/net/route.c b/sys/net/route.c index ae8867da407..bb9c7b05099 100644 --- a/sys/net/route.c +++ b/sys/net/route.c @@ -1,4 +1,4 @@ -/* $OpenBSD: route.c,v 1.430 2024/02/07 23:52:20 bluhm Exp $ */ +/* $OpenBSD: route.c,v 1.431 2024/02/09 14:02:11 bluhm Exp $ */ /* $NetBSD: route.c,v 1.14 1996/02/13 22:00:46 christos Exp $ */ /* @@ -201,7 +201,7 @@ route_init(void) #endif } -void +int route_cache(struct route *ro, struct in_addr addr, u_int rtableid) { u_long gen; @@ -215,7 +215,7 @@ route_cache(struct route *ro, struct in_addr addr, u_int rtableid) ro->ro_dst.sa_family == AF_INET && satosin(&ro->ro_dst)->sin_addr.s_addr == addr.s_addr) { ipstat_inc(ips_rtcachehit); - return; + return (0); } ipstat_inc(ips_rtcachemiss); @@ -228,10 +228,12 @@ route_cache(struct route *ro, struct in_addr addr, u_int rtableid) satosin(&ro->ro_dst)->sin_family = AF_INET; satosin(&ro->ro_dst)->sin_len = sizeof(struct sockaddr_in); satosin(&ro->ro_dst)->sin_addr = addr; + + return (ESRCH); } #ifdef INET6 -void +int route6_cache(struct route_in6 *ro, const struct in6_addr *addr, u_int rtableid) { @@ -246,7 +248,7 @@ route6_cache(struct route_in6 *ro, const struct in6_addr *addr, ro->ro_dst.sin6_family == AF_INET6 && IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, addr)) { ip6stat_inc(ip6s_rtcachehit); - return; + return (0); } ip6stat_inc(ip6s_rtcachemiss); @@ -259,6 +261,8 @@ route6_cache(struct route_in6 *ro, const struct in6_addr *addr, ro->ro_dst.sin6_family = AF_INET6; ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); ro->ro_dst.sin6_addr = *addr; + + return (ESRCH); } #endif diff --git a/sys/netinet/in.h b/sys/netinet/in.h index 65110f27e98..7b75fe8a7cc 100644 --- a/sys/netinet/in.h +++ b/sys/netinet/in.h @@ -1,4 +1,4 @@ -/* $OpenBSD: in.h,v 1.146 2024/02/05 12:52:11 aoyama Exp $ */ +/* $OpenBSD: in.h,v 1.147 2024/02/09 14:02:11 bluhm Exp $ */ /* $NetBSD: in.h,v 1.20 1996/02/13 23:41:47 christos Exp $ */ /* @@ -789,7 +789,7 @@ void in_len2mask(struct in_addr *, int); int in_nam2sin(const struct mbuf *, struct sockaddr_in **); int in_sa2sin(struct sockaddr *, struct sockaddr_in **); -void route_cache(struct route *, struct in_addr, u_int); +int route_cache(struct route *, struct in_addr, u_int); char *inet_ntoa(struct in_addr); int inet_nat64(int, const void *, void *, const void *, u_int8_t); diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c index 456135a722c..2acf37e1b7b 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: in_pcb.c,v 1.290 2024/02/07 23:40:40 bluhm Exp $ */ +/* $OpenBSD: in_pcb.c,v 1.291 2024/02/09 14:02:11 bluhm Exp $ */ /* $NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $ */ /* @@ -918,8 +918,7 @@ in_pcbrtentry(struct inpcb *inp) if (inp->inp_faddr.s_addr == INADDR_ANY) return (NULL); - route_cache(ro, inp->inp_faddr, inp->inp_rtableid); - if (ro->ro_rt == NULL) { + if (route_cache(ro, inp->inp_faddr, inp->inp_rtableid)) { ro->ro_rt = rtalloc_mpath(&ro->ro_dst, &inp->inp_laddr.s_addr, ro->ro_tableid); } @@ -941,8 +940,6 @@ in_pcbselsrc(struct in_addr *insrc, struct sockaddr_in *sin, const struct in_addr *laddr = &inp->inp_laddr; u_int rtableid = inp->inp_rtableid; struct sockaddr *ip4_source = NULL; - - struct sockaddr_in *sin2; struct in_ifaddr *ia = NULL; /* @@ -984,25 +981,9 @@ in_pcbselsrc(struct in_addr *insrc, struct sockaddr_in *sin, * If route is known or can be allocated now, * our src addr is taken from the i/f, else punt. */ - if (!rtisvalid(ro->ro_rt) || (ro->ro_tableid != rtableid) || - (satosin(&ro->ro_dst)->sin_addr.s_addr != sin->sin_addr.s_addr)) { - rtfree(ro->ro_rt); - ro->ro_rt = NULL; - } - if (ro->ro_rt == NULL) { + if (route_cache(ro, sin->sin_addr, rtableid)) { /* No route yet, so try to acquire one */ - ro->ro_dst.sa_family = AF_INET; - ro->ro_dst.sa_len = sizeof(struct sockaddr_in); - satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; - ro->ro_tableid = rtableid; ro->ro_rt = rtalloc_mpath(&ro->ro_dst, NULL, ro->ro_tableid); - - /* - * It is important to zero out the rest of the - * struct sockaddr_in when mixing v6 & v4! - */ - sin2 = satosin(&ro->ro_dst); - memset(sin2->sin_zero, 0, sizeof(sin2->sin_zero)); } /* diff --git a/sys/netinet6/in6.h b/sys/netinet6/in6.h index 771f26c9cbb..da02561288d 100644 --- a/sys/netinet6/in6.h +++ b/sys/netinet6/in6.h @@ -1,4 +1,4 @@ -/* $OpenBSD: in6.h,v 1.114 2024/02/07 23:40:40 bluhm Exp $ */ +/* $OpenBSD: in6.h,v 1.115 2024/02/09 14:02:12 bluhm Exp $ */ /* $KAME: in6.h,v 1.83 2001/03/29 02:55:07 jinmei Exp $ */ /* @@ -428,7 +428,7 @@ int in6_mask2len(struct in6_addr *, u_char *); int in6_nam2sin6(const struct mbuf *, struct sockaddr_in6 **); int in6_sa2sin6(struct sockaddr *, struct sockaddr_in6 **); -void route6_cache(struct route_in6 *, const struct in6_addr *, u_int); +int route6_cache(struct route_in6 *, const struct in6_addr *, u_int); struct ip6_pktopts; struct ip6_moptions; diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c index 0be0c4587d0..ab7acb7d766 100644 --- a/sys/netinet6/in6_pcb.c +++ b/sys/netinet6/in6_pcb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: in6_pcb.c,v 1.135 2024/02/07 23:40:40 bluhm Exp $ */ +/* $OpenBSD: in6_pcb.c,v 1.136 2024/02/09 14:02:12 bluhm Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -568,8 +568,7 @@ in6_pcbrtentry(struct inpcb *inp) if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) return (NULL); - route6_cache(ro, &inp->inp_faddr6, inp->inp_rtableid); - if (ro->ro_rt == NULL) { + if (route6_cache(ro, &inp->inp_faddr6, inp->inp_rtableid)) { ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst), &inp->inp_laddr6.s6_addr32[0], ro->ro_tableid); } diff --git a/sys/netinet6/in6_src.c b/sys/netinet6/in6_src.c index 78205a08d83..d34af50652d 100644 --- a/sys/netinet6/in6_src.c +++ b/sys/netinet6/in6_src.c @@ -1,4 +1,4 @@ -/* $OpenBSD: in6_src.c,v 1.92 2024/02/07 23:40:40 bluhm Exp $ */ +/* $OpenBSD: in6_src.c,v 1.93 2024/02/09 14:02:12 bluhm Exp $ */ /* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $ */ /* @@ -179,8 +179,7 @@ in6_pcbselsrc(const struct in6_addr **in6src, struct sockaddr_in6 *dstsock, * If route is known or can be allocated now, * our src addr is taken from the i/f, else punt. */ - route6_cache(ro, dst, rtableid); - if (ro->ro_rt == NULL) { + if (route6_cache(ro, dst, rtableid)) { ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst), RT_RESOLVE, ro->ro_tableid); } @@ -306,8 +305,7 @@ in6_selectroute(const struct in6_addr *dst, struct ip6_pktopts *opts, * a new one. */ if (ro) { - route6_cache(ro, dst, rtableid); - if (ro->ro_rt == NULL) { + if (route6_cache(ro, dst, rtableid)) { /* No route yet, so try to acquire one */ ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst), NULL, ro->ro_tableid);