Merge tag 'hwmon-for-v6.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / net / mptcp / sched.c
blobdf7dbcfa3b71370cc4d7e4e4f16cc1e41a50dddf
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
4 * Copyright (c) 2022, SUSE.
5 */
7 #define pr_fmt(fmt) "MPTCP: " fmt
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"
16 static DEFINE_SPINLOCK(mptcp_sched_list_lock);
17 static LIST_HEAD(mptcp_sched_list);
19 static int mptcp_sched_default_get_subflow(struct mptcp_sock *msk,
20 struct mptcp_sched_data *data)
22 struct sock *ssk;
24 ssk = data->reinject ? mptcp_subflow_get_retrans(msk) :
25 mptcp_subflow_get_send(msk);
26 if (!ssk)
27 return -EINVAL;
29 mptcp_subflow_set_scheduled(mptcp_subflow_ctx(ssk), true);
30 return 0;
33 static struct mptcp_sched_ops mptcp_sched_default = {
34 .get_subflow = mptcp_sched_default_get_subflow,
35 .name = "default",
36 .owner = THIS_MODULE,
39 /* Must be called with rcu read lock held */
40 struct mptcp_sched_ops *mptcp_sched_find(const char *name)
42 struct mptcp_sched_ops *sched, *ret = NULL;
44 list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
45 if (!strcmp(sched->name, name)) {
46 ret = sched;
47 break;
51 return ret;
54 /* Build string with list of available scheduler values.
55 * Similar to tcp_get_available_congestion_control()
57 void mptcp_get_available_schedulers(char *buf, size_t maxlen)
59 struct mptcp_sched_ops *sched;
60 size_t offs = 0;
62 rcu_read_lock();
63 list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
64 offs += snprintf(buf + offs, maxlen - offs,
65 "%s%s",
66 offs == 0 ? "" : " ", sched->name);
68 if (WARN_ON_ONCE(offs >= maxlen))
69 break;
71 rcu_read_unlock();
74 int mptcp_register_scheduler(struct mptcp_sched_ops *sched)
76 if (!sched->get_subflow)
77 return -EINVAL;
79 spin_lock(&mptcp_sched_list_lock);
80 if (mptcp_sched_find(sched->name)) {
81 spin_unlock(&mptcp_sched_list_lock);
82 return -EEXIST;
84 list_add_tail_rcu(&sched->list, &mptcp_sched_list);
85 spin_unlock(&mptcp_sched_list_lock);
87 pr_debug("%s registered\n", sched->name);
88 return 0;
91 void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched)
93 if (sched == &mptcp_sched_default)
94 return;
96 spin_lock(&mptcp_sched_list_lock);
97 list_del_rcu(&sched->list);
98 spin_unlock(&mptcp_sched_list_lock);
101 void mptcp_sched_init(void)
103 mptcp_register_scheduler(&mptcp_sched_default);
106 int mptcp_init_sched(struct mptcp_sock *msk,
107 struct mptcp_sched_ops *sched)
109 if (!sched)
110 sched = &mptcp_sched_default;
112 if (!bpf_try_module_get(sched, sched->owner))
113 return -EBUSY;
115 msk->sched = sched;
116 if (msk->sched->init)
117 msk->sched->init(msk);
119 pr_debug("sched=%s\n", msk->sched->name);
121 return 0;
124 void mptcp_release_sched(struct mptcp_sock *msk)
126 struct mptcp_sched_ops *sched = msk->sched;
128 if (!sched)
129 return;
131 msk->sched = NULL;
132 if (sched->release)
133 sched->release(msk);
135 bpf_module_put(sched, sched->owner);
138 void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
139 bool scheduled)
141 WRITE_ONCE(subflow->scheduled, scheduled);
144 int mptcp_sched_get_send(struct mptcp_sock *msk)
146 struct mptcp_subflow_context *subflow;
147 struct mptcp_sched_data data;
149 msk_owned_by_me(msk);
151 /* the following check is moved out of mptcp_subflow_get_send */
152 if (__mptcp_check_fallback(msk)) {
153 if (msk->first &&
154 __tcp_can_send(msk->first) &&
155 sk_stream_memory_free(msk->first)) {
156 mptcp_subflow_set_scheduled(mptcp_subflow_ctx(msk->first), true);
157 return 0;
159 return -EINVAL;
162 mptcp_for_each_subflow(msk, subflow) {
163 if (READ_ONCE(subflow->scheduled))
164 return 0;
167 data.reinject = false;
168 if (msk->sched == &mptcp_sched_default || !msk->sched)
169 return mptcp_sched_default_get_subflow(msk, &data);
170 return msk->sched->get_subflow(msk, &data);
173 int mptcp_sched_get_retrans(struct mptcp_sock *msk)
175 struct mptcp_subflow_context *subflow;
176 struct mptcp_sched_data data;
178 msk_owned_by_me(msk);
180 /* the following check is moved out of mptcp_subflow_get_retrans */
181 if (__mptcp_check_fallback(msk))
182 return -EINVAL;
184 mptcp_for_each_subflow(msk, subflow) {
185 if (READ_ONCE(subflow->scheduled))
186 return 0;
189 data.reinject = true;
190 if (msk->sched == &mptcp_sched_default || !msk->sched)
191 return mptcp_sched_default_get_subflow(msk, &data);
192 return msk->sched->get_subflow(msk, &data);