Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-2.0
2 : : /* Multipath TCP
3 : : *
4 : : * Copyright (c) 2022, SUSE.
5 : : */
6 : :
7 : : #define pr_fmt(fmt) "MPTCP: " fmt
8 : :
9 : : #include <linux/kernel.h>
10 : : #include <linux/module.h>
11 : : #include <linux/list.h>
12 : : #include <linux/rculist.h>
13 : : #include <linux/spinlock.h>
14 : : #include "protocol.h"
15 : :
16 : : static DEFINE_SPINLOCK(mptcp_sched_list_lock);
17 : : static LIST_HEAD(mptcp_sched_list);
18 : :
19 : 380525 : static int mptcp_sched_default_get_subflow(struct mptcp_sock *msk,
20 : : struct mptcp_sched_data *data)
21 : : {
22 : 380525 : struct sock *ssk;
23 : :
24 [ + + + + ]: 380525 : ssk = data->reinject ? mptcp_subflow_get_retrans(msk) :
25 : 379056 : mptcp_subflow_get_send(msk);
26 [ + + ]: 380525 : if (!ssk)
27 : : return -EINVAL;
28 : :
29 : 313088 : mptcp_subflow_set_scheduled(mptcp_subflow_ctx(ssk), true);
30 : 313088 : return 0;
31 : : }
32 : :
33 : : static struct mptcp_sched_ops mptcp_sched_default = {
34 : : .get_subflow = mptcp_sched_default_get_subflow,
35 : : .name = "default",
36 : : .owner = THIS_MODULE,
37 : : };
38 : :
39 : : /* Must be called with rcu read lock held */
40 : 2606 : struct mptcp_sched_ops *mptcp_sched_find(const char *name)
41 : : {
42 : 2606 : struct mptcp_sched_ops *sched, *ret = NULL;
43 : :
44 [ + + ]: 2756 : list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
45 [ + + ]: 2692 : if (!strcmp(sched->name, name)) {
46 : : ret = sched;
47 : : break;
48 : : }
49 : : }
50 : :
51 : 2606 : return ret;
52 : : }
53 : :
54 : : /* Build string with list of available scheduler values.
55 : : * Similar to tcp_get_available_congestion_control()
56 : : */
57 : 0 : void mptcp_get_available_schedulers(char *buf, size_t maxlen)
58 : : {
59 : 0 : struct mptcp_sched_ops *sched;
60 : 0 : size_t offs = 0;
61 : :
62 : 0 : rcu_read_lock();
63 : 0 : spin_lock(&mptcp_sched_list_lock);
64 [ # # ]: 0 : list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
65 : 0 : offs += snprintf(buf + offs, maxlen - offs,
66 : : "%s%s",
67 [ # # ]: 0 : offs == 0 ? "" : " ", sched->name);
68 : :
69 [ # # # # ]: 0 : if (WARN_ON_ONCE(offs >= maxlen))
70 : : break;
71 : : }
72 : 0 : spin_unlock(&mptcp_sched_list_lock);
73 : 0 : rcu_read_unlock();
74 : 0 : }
75 : :
76 : 34 : int mptcp_register_scheduler(struct mptcp_sched_ops *sched)
77 : : {
78 [ + - ]: 34 : if (!sched->get_subflow)
79 : : return -EINVAL;
80 : :
81 : 34 : spin_lock(&mptcp_sched_list_lock);
82 [ - + ]: 34 : if (mptcp_sched_find(sched->name)) {
83 : 0 : spin_unlock(&mptcp_sched_list_lock);
84 : 0 : return -EEXIST;
85 : : }
86 : 34 : list_add_tail_rcu(&sched->list, &mptcp_sched_list);
87 : 34 : spin_unlock(&mptcp_sched_list_lock);
88 : :
89 [ - + ]: 34 : pr_debug("%s registered\n", sched->name);
90 : : return 0;
91 : : }
92 : :
93 : 30 : void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched)
94 : : {
95 [ + - ]: 30 : if (sched == &mptcp_sched_default)
96 : : return;
97 : :
98 : 30 : spin_lock(&mptcp_sched_list_lock);
99 : 30 : list_del_rcu(&sched->list);
100 : 30 : spin_unlock(&mptcp_sched_list_lock);
101 : : }
102 : :
103 : 4 : void mptcp_sched_init(void)
104 : : {
105 : 4 : mptcp_register_scheduler(&mptcp_sched_default);
106 : 4 : }
107 : :
108 : 3662 : int mptcp_init_sched(struct mptcp_sock *msk,
109 : : struct mptcp_sched_ops *sched)
110 : : {
111 [ - + ]: 3662 : if (!sched)
112 : 0 : sched = &mptcp_sched_default;
113 : :
114 [ + - ]: 3662 : if (!bpf_try_module_get(sched, sched->owner))
115 : : return -EBUSY;
116 : :
117 : 3662 : msk->sched = sched;
118 [ + + ]: 3662 : if (msk->sched->init)
119 : 90 : msk->sched->init(msk);
120 : :
121 [ - + ]: 3662 : pr_debug("sched=%s\n", msk->sched->name);
122 : :
123 : : return 0;
124 : : }
125 : :
126 : 3569 : void mptcp_release_sched(struct mptcp_sock *msk)
127 : : {
128 : 3569 : struct mptcp_sched_ops *sched = msk->sched;
129 : :
130 [ + - ]: 3569 : if (!sched)
131 : : return;
132 : :
133 : 3569 : msk->sched = NULL;
134 [ + + ]: 3569 : if (sched->release)
135 : 90 : sched->release(msk);
136 : :
137 : 3569 : bpf_module_put(sched, sched->owner);
138 : : }
139 : :
140 : 291430 : void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
141 : : bool scheduled)
142 : : {
143 : 0 : WRITE_ONCE(subflow->scheduled, scheduled);
144 : 810269 : }
145 : :
146 : 196234 : static void mptcp_sched_data_set_contexts(const struct mptcp_sock *msk,
147 : : struct mptcp_sched_data *data)
148 : : {
149 : 196234 : struct mptcp_subflow_context *subflow;
150 : 196234 : int i = 0;
151 : :
152 [ + + ]: 588138 : mptcp_for_each_subflow(msk, subflow) {
153 [ - + ]: 391904 : if (i == MPTCP_SUBFLOWS_MAX) {
154 [ # # # # ]: 0 : pr_warn_once("too many subflows");
155 : : break;
156 : : }
157 : 391904 : mptcp_subflow_set_scheduled(subflow, false);
158 : 391904 : data->contexts[i++] = subflow;
159 : : }
160 : 196234 : data->subflows = i;
161 : :
162 [ + + ]: 1374202 : for (; i < MPTCP_SUBFLOWS_MAX; i++)
163 : 1177968 : data->contexts[i] = NULL;
164 : 196234 : }
165 : :
166 : 623424 : int mptcp_sched_get_send(struct mptcp_sock *msk)
167 : : {
168 : 623424 : struct mptcp_subflow_context *subflow;
169 : 623424 : struct mptcp_sched_data data;
170 : :
171 : 623424 : msk_owned_by_me(msk);
172 : :
173 : : /* the following check is moved out of mptcp_subflow_get_send */
174 [ + + ]: 623424 : if (__mptcp_check_fallback(msk)) {
175 [ + - + - ]: 55870 : if (msk->first &&
176 [ + + ]: 35106 : __tcp_can_send(msk->first) &&
177 [ # # ]: 27935 : sk_stream_memory_free(msk->first)) {
178 : 21010 : mptcp_subflow_set_scheduled(mptcp_subflow_ctx(msk->first), true);
179 : 21010 : return 0;
180 : : }
181 : 6925 : return -EINVAL;
182 : : }
183 : :
184 [ + + ]: 1704532 : mptcp_for_each_subflow(msk, subflow) {
185 [ + + + + ]: 1132160 : if (READ_ONCE(subflow->scheduled))
186 : : return 0;
187 : : }
188 : :
189 : 572372 : data.reinject = false;
190 [ + + - + ]: 572372 : if (msk->sched == &mptcp_sched_default || !msk->sched)
191 : 379056 : return mptcp_sched_default_get_subflow(msk, &data);
192 : 193316 : mptcp_sched_data_set_contexts(msk, &data);
193 : 193316 : return msk->sched->get_subflow(msk, &data);
194 : : }
195 : :
196 : 8199 : int mptcp_sched_get_retrans(struct mptcp_sock *msk)
197 : : {
198 : 8199 : struct mptcp_subflow_context *subflow;
199 : 8199 : struct mptcp_sched_data data;
200 : :
201 : 8199 : msk_owned_by_me(msk);
202 : :
203 : : /* the following check is moved out of mptcp_subflow_get_retrans */
204 [ + + ]: 8199 : if (__mptcp_check_fallback(msk))
205 : : return -EINVAL;
206 : :
207 [ + + ]: 13075 : mptcp_for_each_subflow(msk, subflow) {
208 [ + + + + ]: 8688 : if (READ_ONCE(subflow->scheduled))
209 : : return 0;
210 : : }
211 : :
212 : 4387 : data.reinject = true;
213 [ + + + + ]: 4387 : if (msk->sched == &mptcp_sched_default || !msk->sched)
214 : 1469 : return mptcp_sched_default_get_subflow(msk, &data);
215 : 2918 : mptcp_sched_data_set_contexts(msk, &data);
216 : 2918 : return msk->sched->get_subflow(msk, &data);
217 : : }
|