LCOV - code coverage report
Current view: top level - mptcp/sched.c (source / functions) Coverage Total Hit
Test: export-net Lines: 78.9 % 95 75
Test Date: 2026-10-20 11:32:20 Functions: 84.6 % 13 11
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 49.3 % 73 36

             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                 :      780849 : static int mptcp_sched_default_get_send(struct mptcp_sock *msk)
      20                 :             : {
      21                 :      780849 :         struct sock *ssk;
      22                 :             : 
      23                 :      780849 :         ssk = mptcp_subflow_get_send(msk);
      24            [ + ]:      780849 :         if (!ssk)
      25                 :             :                 return -EINVAL;
      26                 :             : 
      27                 :      606682 :         mptcp_subflow_set_scheduled(mptcp_subflow_ctx(ssk), true);
      28                 :      606682 :         return 0;
      29                 :             : }
      30                 :             : 
      31                 :        4945 : static int mptcp_sched_default_get_retrans(struct mptcp_sock *msk)
      32                 :             : {
      33                 :        4945 :         struct sock *ssk;
      34                 :             : 
      35                 :        4945 :         ssk = mptcp_subflow_get_retrans(msk);
      36            [ + ]:        4945 :         if (!ssk)
      37                 :             :                 return -EINVAL;
      38                 :             : 
      39                 :        2283 :         mptcp_subflow_set_scheduled(mptcp_subflow_ctx(ssk), true);
      40                 :        2283 :         return 0;
      41                 :             : }
      42                 :             : 
      43                 :             : static struct mptcp_sched_ops mptcp_sched_default = {
      44                 :             :         .get_send       = mptcp_sched_default_get_send,
      45                 :             :         .get_retrans    = mptcp_sched_default_get_retrans,
      46                 :             :         .name           = "default",
      47                 :             :         .owner          = THIS_MODULE,
      48                 :             : };
      49                 :             : 
      50                 :             : /* Must be called with rcu read lock held */
      51                 :        3519 : struct mptcp_sched_ops *mptcp_sched_find(const char *name)
      52                 :             : {
      53                 :        3519 :         struct mptcp_sched_ops *sched, *ret = NULL;
      54                 :             : 
      55   [ -  +  -  -  :        3519 :         list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
              -  + ][ + ]
      56       [ + ][ + ]:        3513 :                 if (!strcmp(sched->name, name)) {
      57                 :             :                         ret = sched;
      58                 :             :                         break;
      59                 :             :                 }
      60                 :             :         }
      61                 :             : 
      62                 :        3519 :         return ret;
      63                 :             : }
      64                 :             : 
      65                 :             : /* Build string with list of available scheduler values.
      66                 :             :  * Similar to tcp_get_available_congestion_control()
      67                 :             :  */
      68                 :          20 : void mptcp_get_available_schedulers(char *buf, size_t maxlen)
      69                 :             : {
      70                 :          20 :         struct mptcp_sched_ops *sched;
      71                 :          20 :         size_t offs = 0;
      72                 :             : 
      73                 :          20 :         rcu_read_lock();
      74   [ -  +  -  -  :          40 :         list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
              -  + ][ + ]
      75                 :          40 :                 offs += snprintf(buf + offs, maxlen - offs,
      76                 :             :                                  "%s%s",
      77       [ + ][ + ]:          20 :                                  offs == 0 ? "" : " ", sched->name);
      78                 :             : 
      79 [ +  - ][ +  - ]:          20 :                 if (WARN_ON_ONCE(offs >= maxlen))
      80                 :             :                         break;
      81                 :             :         }
      82                 :          20 :         rcu_read_unlock();
      83                 :          20 : }
      84                 :             : 
      85                 :           0 : int mptcp_validate_scheduler(struct mptcp_sched_ops *sched)
      86                 :             : {
      87            [ # ]:           0 :         if (!sched->get_send) {
      88                 :           0 :                 pr_err("%s does not implement required ops\n", sched->name);
      89                 :           0 :                 return -EINVAL;
      90                 :             :         }
      91                 :             : 
      92                 :             :         return 0;
      93                 :             : }
      94                 :             : 
      95                 :           6 : int mptcp_register_scheduler(struct mptcp_sched_ops *sched)
      96                 :             : {
      97                 :           6 :         int ret;
      98                 :             : 
      99            [ - ]:           6 :         ret = mptcp_validate_scheduler(sched);
     100                 :           0 :         if (ret)
     101                 :           0 :                 return ret;
     102                 :             : 
     103                 :           6 :         spin_lock(&mptcp_sched_list_lock);
     104         [ -  + ]:           6 :         if (mptcp_sched_find(sched->name)) {
     105                 :           0 :                 spin_unlock(&mptcp_sched_list_lock);
     106                 :           0 :                 return -EEXIST;
     107                 :             :         }
     108                 :           6 :         list_add_tail_rcu(&sched->list, &mptcp_sched_list);
     109                 :           6 :         spin_unlock(&mptcp_sched_list_lock);
     110                 :             : 
     111         [ -  - ]:           6 :         pr_debug("%s registered\n", sched->name);
     112                 :             :         return 0;
     113                 :             : }
     114                 :             : 
     115                 :           0 : void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched)
     116                 :             : {
     117       [ # ][ # ]:           0 :         if (sched == &mptcp_sched_default)
     118                 :             :                 return;
     119                 :             : 
     120                 :           0 :         spin_lock(&mptcp_sched_list_lock);
     121                 :           0 :         list_del_rcu(&sched->list);
     122                 :           0 :         spin_unlock(&mptcp_sched_list_lock);
     123                 :             : }
     124                 :             : 
     125                 :           6 : void mptcp_sched_init(void)
     126                 :             : {
     127                 :           6 :         mptcp_register_scheduler(&mptcp_sched_default);
     128                 :           6 : }
     129                 :             : 
     130                 :        5155 : int mptcp_init_sched(struct mptcp_sock *msk,
     131                 :             :                      struct mptcp_sched_ops *sched)
     132                 :             : {
     133            [ + ]:        5155 :         if (!sched)
     134                 :           0 :                 sched = &mptcp_sched_default;
     135                 :             : 
     136            [ - ]:        5155 :         if (!bpf_try_module_get(sched, sched->owner))
     137                 :             :                 return -EBUSY;
     138                 :             : 
     139                 :        5155 :         msk->sched = sched;
     140            [ + ]:        5155 :         if (msk->sched->init)
     141                 :           0 :                 msk->sched->init(msk);
     142                 :             : 
     143         [ -  - ]:        5155 :         pr_debug("sched=%s\n", msk->sched->name);
     144                 :             : 
     145                 :             :         return 0;
     146                 :             : }
     147                 :             : 
     148                 :        5036 : void mptcp_release_sched(struct mptcp_sock *msk)
     149                 :             : {
     150                 :        5036 :         struct mptcp_sched_ops *sched = msk->sched;
     151                 :             : 
     152            [ - ]:        5036 :         if (!sched)
     153                 :             :                 return;
     154                 :             : 
     155                 :        5036 :         msk->sched = NULL;
     156            [ + ]:        5036 :         if (sched->release)
     157                 :           0 :                 sched->release(msk);
     158                 :             : 
     159                 :        5036 :         bpf_module_put(sched, sched->owner);
     160                 :             : }
     161                 :             : 
     162                 :      680932 : void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
     163                 :             :                                  bool scheduled)
     164                 :             : {
     165                 :      608946 :         WRITE_ONCE(subflow->scheduled, scheduled);
     166                 :      680951 : }
     167                 :             : 
     168                 :      857564 : int mptcp_sched_get_send(struct mptcp_sock *msk)
     169                 :             : {
     170                 :      857564 :         struct mptcp_subflow_context *subflow;
     171                 :             : 
     172                 :      857564 :         msk_owned_by_me(msk);
     173                 :             : 
     174                 :             :         /* the following check is moved out of mptcp_subflow_get_send */
     175       [ + ][ + ]:      857564 :         if (__mptcp_check_fallback(msk)) {
     176 [ -  - ][ -  - ]:      149348 :                 if (msk->first &&
                    [ # ]
     177  [ + ][ + ][ # ]:      108952 :                     __tcp_can_send(msk->first) &&
     178       [ # ][ # ]:       74674 :                     sk_stream_memory_free(msk->first)) {
     179                 :       71517 :                         mptcp_subflow_set_scheduled(mptcp_subflow_ctx(msk->first), true);
     180                 :       71517 :                         return 0;
     181                 :             :                 }
     182                 :        3157 :                 return -EINVAL;
     183                 :             :         }
     184                 :             : 
     185       [ + ][ + ]:     2051285 :         mptcp_for_each_subflow(msk, subflow) {
     186    [ +  + ][ + ]:     1270436 :                 if (READ_ONCE(subflow->scheduled))
     187                 :             :                         return 0;
     188                 :             :         }
     189                 :             : 
     190 [ +  - ][ +  - ]:      780849 :         if (msk->sched == &mptcp_sched_default || !msk->sched)
     191                 :      780849 :                 return mptcp_sched_default_get_send(msk);
     192                 :           0 :         return msk->sched->get_send(msk);
     193                 :             : }
     194                 :             : 
     195                 :        4951 : int mptcp_sched_get_retrans(struct mptcp_sock *msk)
     196                 :             : {
     197                 :        4951 :         struct mptcp_subflow_context *subflow;
     198                 :             : 
     199                 :        4951 :         msk_owned_by_me(msk);
     200                 :             : 
     201                 :             :         /* the following check is moved out of mptcp_subflow_get_retrans */
     202       [ + ][ + ]:        4951 :         if (__mptcp_check_fallback(msk))
     203                 :             :                 return -EINVAL;
     204                 :             : 
     205       [ + ][ + ]:       12714 :         mptcp_for_each_subflow(msk, subflow) {
     206    [ +  - ][ - ]:        7769 :                 if (READ_ONCE(subflow->scheduled))
     207                 :             :                         return 0;
     208                 :             :         }
     209                 :             : 
     210 [ +  - ][ +  - ]:        4945 :         if (msk->sched == &mptcp_sched_default || !msk->sched)
     211                 :        4945 :                 return mptcp_sched_default_get_retrans(msk);
     212       [ # ][ # ]:           0 :         if (msk->sched->get_retrans)
     213                 :           0 :                 return msk->sched->get_retrans(msk);
     214                 :           0 :         return msk->sched->get_send(msk);
     215                 :             : }
        

Generated by: LCOV version 2.3.1-1