]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/route/mpath_ctl.c
Implement flowid calculation for outbound connections to balance
[FreeBSD/FreeBSD.git] / sys / net / route / mpath_ctl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #include "opt_inet.h"
31 #include "opt_route.h"
32
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/lock.h>
37 #include <sys/rmlock.h>
38 #include <sys/rwlock.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 #include <sys/kernel.h>
44
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <net/route/route_ctl.h>
50 #include <net/route/route_var.h>
51 #include <net/vnet.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_fib.h>
56
57 #include <net/route/nhop_utils.h>
58 #include <net/route/nhop.h>
59 #include <net/route/nhop_var.h>
60
61 /*
62  * This file contains the supporting functions for adding/deleting/updating
63  *  multipath routes to the routing table.
64  */
65
66 SYSCTL_DECL(_net_route);
67 VNET_DEFINE(u_int, fib_hash_outbound) = 0;
68 SYSCTL_UINT(_net_route, OID_AUTO, hash_outbound, CTLFLAG_RD | CTLFLAG_VNET,
69     &VNET_NAME(fib_hash_outbound), 0,
70     "Compute flowid for locally-originated packets");
71
72 /* Default entropy to add to the hash calculation for the outbound connections*/
73 uint8_t mpath_entropy_key[MPATH_ENTROPY_KEY_LEN] = {
74         0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
75         0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
76         0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
77         0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
78         0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
79 };
80
81
82 /*
83  * Tries to add @rnd_add nhop to the existing set of nhops (@nh_orig) for the
84  * prefix specified by @rt.
85  *
86  * Return 0 ans consumes rt / rnd_add nhop references. @rc gets populated
87  *   with the operation result.
88  * Otherwise errno is returned.
89  *
90  * caller responsibility is to unlock/free rt and
91  *  rt->rt_nhop.
92  */
93 int
94 add_route_mpath(struct rib_head *rnh, struct rt_addrinfo *info,
95     struct rtentry *rt, struct route_nhop_data *rnd_add,
96     struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
97 {
98         RIB_RLOCK_TRACKER;
99         struct route_nhop_data rnd_new;
100         int error = 0;
101
102         /*
103          * It is possible that multiple rtsock speakers will try to update
104          * the same route simultaneously. Reduce the chance of failing the
105          * request by retrying the cycle multiple times.
106          */
107         for (int i = 0; i < RIB_MAX_RETRIES; i++) {
108                 error = nhgrp_get_addition_group(rnh, rnd_orig, rnd_add,
109                     &rnd_new);
110                 if (error != 0) {
111                         if (error != EAGAIN)
112                                 break;
113
114                         /*
115                          * Group creation failed, most probably because
116                          * @rnd_orig data got scheduled for deletion.
117                          * Refresh @rnd_orig data and retry.
118                          */
119                         RIB_RLOCK(rnh);
120                         lookup_prefix(rnh, info, rnd_orig);
121                         RIB_RUNLOCK(rnh);
122                         continue;
123                 }
124
125                 error = change_route_conditional(rnh, rt, info, rnd_orig,
126                     &rnd_new, rc);
127                 if (error != EAGAIN)
128                         break;
129                 RTSTAT_INC(rts_add_retry);
130         }
131
132         if (V_fib_hash_outbound == 0 && error == 0 &&
133             NH_IS_NHGRP(rc->rc_nh_new)) {
134                 /*
135                  * First multipath route got installed. Enable local
136                  * outbound connections hashing.
137                  */
138                 if (bootverbose)
139                         printf("FIB: enabled flowid calculation for locally-originated packets\n");
140                 V_fib_hash_outbound = 1;
141         }
142
143         return (error);
144 }
145
146 struct rt_match_info {
147         struct rt_addrinfo *info;
148         struct rtentry *rt;
149 };
150
151 static bool
152 gw_filter_func(const struct nhop_object *nh, void *_data)
153 {
154         struct rt_match_info *ri = (struct rt_match_info *)_data;
155
156         return (check_info_match_nhop(ri->info, ri->rt, nh) == 0);
157 }
158
159 /*
160  * Tries to delete matching paths from @nhg.
161  * Returns 0 on success and updates operation result in @rc.
162  */
163 int
164 del_route_mpath(struct rib_head *rh, struct rt_addrinfo *info,
165     struct rtentry *rt, struct nhgrp_object *nhg,
166     struct rib_cmd_info *rc)
167 {
168         struct route_nhop_data rnd;
169         struct rt_match_info ri = { .info = info, .rt = rt };
170         int error;
171
172         RIB_WLOCK_ASSERT(rh);
173
174         /*
175          * Require gateway to delete multipath routes, to forbid
176          *  deleting all paths at once.
177          * If the filter function is provided, skip gateway check to
178          *  allow rib_walk_del() delete routes for any criteria based
179          *  on provided callback.
180          */
181         if ((info->rti_info[RTAX_GATEWAY] == NULL) && (info->rti_filter == NULL))
182                 return (ESRCH);
183
184         error = nhgrp_get_filtered_group(rh, nhg, gw_filter_func, (void *)&ri,
185             &rnd);
186         if (error == 0)
187                 error = change_route_nhop(rh, rt, info, &rnd, rc);
188         return (error);
189 }
190