LCOV - code coverage report
Current view: top level - mptcp/sched.c (source / functions) Coverage Total Hit
Test: export Lines: 92.6 % 95 88
Test Date: 2025-03-31 13:57:41 Functions: 100.0 % 13 13
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 66.3 % 92 61

             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                 :      399287 : static int mptcp_sched_default_get_send(struct mptcp_sock *msk)
      20                 :             : {
      21                 :      399287 :         struct sock *ssk;
      22                 :             : 
      23                 :      399287 :         ssk = mptcp_subflow_get_send(msk);
      24         [ +  + ]:      399287 :         if (!ssk)
      25                 :             :                 return -EINVAL;
      26                 :             : 
      27                 :      330294 :         mptcp_subflow_set_scheduled(mptcp_subflow_ctx(ssk), true);
      28                 :      330294 :         return 0;
      29                 :             : }
      30                 :             : 
      31                 :        3487 : static int mptcp_sched_default_get_retrans(struct mptcp_sock *msk)
      32                 :             : {
      33                 :        3487 :         struct sock *ssk;
      34                 :             : 
      35                 :        3487 :         ssk = mptcp_subflow_get_retrans(msk);
      36         [ +  + ]:        3487 :         if (!ssk)
      37                 :             :                 return -EINVAL;
      38                 :             : 
      39                 :        1472 :         mptcp_subflow_set_scheduled(mptcp_subflow_ctx(ssk), true);
      40                 :        1472 :         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                 :        2612 : struct mptcp_sched_ops *mptcp_sched_find(const char *name)
      52                 :             : {
      53                 :        2612 :         struct mptcp_sched_ops *sched, *ret = NULL;
      54                 :             : 
      55   [ +  +  -  +  :        2732 :         list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
          -  -  -  -  -  
                -  +  + ]
      56         [ +  + ]:        2698 :                 if (!strcmp(sched->name, name)) {
      57                 :             :                         ret = sched;
      58                 :             :                         break;
      59                 :             :                 }
      60                 :             :         }
      61                 :             : 
      62                 :        2612 :         return ret;
      63                 :             : }
      64                 :             : 
      65                 :             : /* Build string with list of available scheduler values.
      66                 :             :  * Similar to tcp_get_available_congestion_control()
      67                 :             :  */
      68                 :           8 : void mptcp_get_available_schedulers(char *buf, size_t maxlen)
      69                 :             : {
      70                 :           8 :         struct mptcp_sched_ops *sched;
      71                 :           8 :         size_t offs = 0;
      72                 :             : 
      73                 :           8 :         rcu_read_lock();
      74   [ +  +  -  +  :          16 :         list_for_each_entry_rcu(sched, &mptcp_sched_list, list) {
          -  -  -  -  -  
                -  +  + ]
      75                 :          16 :                 offs += snprintf(buf + offs, maxlen - offs,
      76                 :             :                                  "%s%s",
      77         [ -  + ]:           8 :                                  offs == 0 ? "" : " ", sched->name);
      78                 :             : 
      79   [ -  +  +  - ]:           8 :                 if (WARN_ON_ONCE(offs >= maxlen))
      80                 :             :                         break;
      81                 :             :         }
      82                 :           8 :         rcu_read_unlock();
      83                 :           8 : }
      84                 :             : 
      85                 :          62 : int mptcp_validate_scheduler(struct mptcp_sched_ops *sched)
      86                 :             : {
      87         [ -  + ]:          30 :         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                 :          34 : int mptcp_register_scheduler(struct mptcp_sched_ops *sched)
      96                 :             : {
      97                 :          34 :         int ret;
      98                 :             : 
      99         [ +  - ]:          34 :         ret = mptcp_validate_scheduler(sched);
     100                 :           0 :         if (ret)
     101                 :           0 :                 return ret;
     102                 :             : 
     103                 :          34 :         spin_lock(&mptcp_sched_list_lock);
     104         [ -  + ]:          34 :         if (mptcp_sched_find(sched->name)) {
     105                 :           0 :                 spin_unlock(&mptcp_sched_list_lock);
     106                 :           0 :                 return -EEXIST;
     107                 :             :         }
     108                 :          34 :         list_add_tail_rcu(&sched->list, &mptcp_sched_list);
     109                 :          34 :         spin_unlock(&mptcp_sched_list_lock);
     110                 :             : 
     111         [ -  + ]:          34 :         pr_debug("%s registered\n", sched->name);
     112                 :             :         return 0;
     113                 :             : }
     114                 :             : 
     115                 :          30 : void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched)
     116                 :             : {
     117         [ +  - ]:          30 :         if (sched == &mptcp_sched_default)
     118                 :             :                 return;
     119                 :             : 
     120                 :          30 :         spin_lock(&mptcp_sched_list_lock);
     121                 :          30 :         list_del_rcu(&sched->list);
     122                 :          30 :         spin_unlock(&mptcp_sched_list_lock);
     123                 :             : }
     124                 :             : 
     125                 :           4 : void mptcp_sched_init(void)
     126                 :             : {
     127                 :           4 :         mptcp_register_scheduler(&mptcp_sched_default);
     128                 :           4 : }
     129                 :             : 
     130                 :        3710 : int mptcp_init_sched(struct mptcp_sock *msk,
     131                 :             :                      struct mptcp_sched_ops *sched)
     132                 :             : {
     133         [ -  + ]:        3710 :         if (!sched)
     134                 :           0 :                 sched = &mptcp_sched_default;
     135                 :             : 
     136         [ +  - ]:        3710 :         if (!bpf_try_module_get(sched, sched->owner))
     137                 :             :                 return -EBUSY;
     138                 :             : 
     139                 :        3710 :         msk->sched = sched;
     140         [ +  + ]:        3710 :         if (msk->sched->init)
     141                 :          90 :                 msk->sched->init(msk);
     142                 :             : 
     143         [ -  + ]:        3710 :         pr_debug("sched=%s\n", msk->sched->name);
     144                 :             : 
     145                 :             :         return 0;
     146                 :             : }
     147                 :             : 
     148                 :        3637 : void mptcp_release_sched(struct mptcp_sock *msk)
     149                 :             : {
     150                 :        3637 :         struct mptcp_sched_ops *sched = msk->sched;
     151                 :             : 
     152         [ +  - ]:        3637 :         if (!sched)
     153                 :             :                 return;
     154                 :             : 
     155                 :        3637 :         msk->sched = NULL;
     156         [ +  + ]:        3637 :         if (sched->release)
     157                 :          90 :                 sched->release(msk);
     158                 :             : 
     159                 :        3637 :         bpf_module_put(sched, sched->owner);
     160                 :             : }
     161                 :             : 
     162                 :      318906 : void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,
     163                 :             :                                  bool scheduled)
     164                 :             : {
     165                 :      289552 :         WRITE_ONCE(subflow->scheduled, scheduled);
     166                 :      880302 : }
     167                 :             : 
     168                 :      650066 : int mptcp_sched_get_send(struct mptcp_sock *msk)
     169                 :             : {
     170                 :      650066 :         struct mptcp_subflow_context *subflow;
     171                 :             : 
     172                 :      650066 :         msk_owned_by_me(msk);
     173                 :             : 
     174                 :             :         /* the following check is moved out of mptcp_subflow_get_send */
     175         [ +  + ]:      650066 :         if (__mptcp_check_fallback(msk)) {
     176   [ +  -  +  - ]:       70168 :                 if (msk->first &&
     177         [ +  + ]:       56411 :                     __tcp_can_send(msk->first) &&
     178         [ #  # ]:       35084 :                     sk_stream_memory_free(msk->first)) {
     179                 :       29940 :                         mptcp_subflow_set_scheduled(mptcp_subflow_ctx(msk->first), true);
     180                 :       29940 :                         return 0;
     181                 :             :                 }
     182                 :        5144 :                 return -EINVAL;
     183                 :             :         }
     184                 :             : 
     185         [ +  + ]:     1762306 :         mptcp_for_each_subflow(msk, subflow) {
     186   [ +  +  +  + ]:     1148217 :                 if (READ_ONCE(subflow->scheduled))
     187                 :             :                         return 0;
     188                 :             :         }
     189                 :             : 
     190   [ +  +  -  + ]:      614089 :         if (msk->sched == &mptcp_sched_default || !msk->sched)
     191                 :      399287 :                 return mptcp_sched_default_get_send(msk);
     192                 :      214802 :         return msk->sched->get_send(msk);
     193                 :             : }
     194                 :             : 
     195                 :       11585 : int mptcp_sched_get_retrans(struct mptcp_sock *msk)
     196                 :             : {
     197                 :       11585 :         struct mptcp_subflow_context *subflow;
     198                 :             : 
     199                 :       11585 :         msk_owned_by_me(msk);
     200                 :             : 
     201                 :             :         /* the following check is moved out of mptcp_subflow_get_retrans */
     202         [ +  + ]:       11585 :         if (__mptcp_check_fallback(msk))
     203                 :             :                 return -EINVAL;
     204                 :             : 
     205         [ +  + ]:       17874 :         mptcp_for_each_subflow(msk, subflow) {
     206   [ +  +  +  + ]:       11779 :                 if (READ_ONCE(subflow->scheduled))
     207                 :             :                         return 0;
     208                 :             :         }
     209                 :             : 
     210   [ +  +  +  + ]:        6095 :         if (msk->sched == &mptcp_sched_default || !msk->sched)
     211                 :        3487 :                 return mptcp_sched_default_get_retrans(msk);
     212         [ +  + ]:        2608 :         if (msk->sched->get_retrans)
     213                 :           7 :                 return msk->sched->get_retrans(msk);
     214                 :        2601 :         return msk->sched->get_send(msk);
     215                 :             : }
        

Generated by: LCOV version 2.0-1