Path: blob/main/sys/compat/linuxkpi/common/src/linux_netdev.c
39586 views
/*-1* Copyright (c) 2021 The FreeBSD Foundation2* Copyright (c) 2022 Bjoern A. Zeeb3*4* This software was developed by Björn Zeeb under sponsorship from5* the FreeBSD Foundation.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/types.h>31#include <sys/kernel.h>32#include <sys/sysctl.h>3334#include <linux/bitops.h>35#include <linux/list.h>36#include <linux/netdevice.h>3738MALLOC_DEFINE(M_NETDEV, "lkpindev", "Linux KPI netdevice compat");3940#define NAPI_LOCK_INIT(_ndev) \41mtx_init(&(_ndev)->napi_mtx, "napi_mtx", NULL, MTX_DEF)42#define NAPI_LOCK_DESTROY(_ndev) mtx_destroy(&(_ndev)->napi_mtx)43#define NAPI_LOCK_ASSERT(_ndev) mtx_assert(&(_ndev)->napi_mtx, MA_OWNED)44#define NAPI_LOCK(_ndev) mtx_lock(&(_ndev)->napi_mtx)45#define NAPI_UNLOCK(_ndev) mtx_unlock(&(_ndev)->napi_mtx)4647/* -------------------------------------------------------------------------- */4849#define LKPI_NAPI_FLAGS \50"\20\1DISABLE_PENDING\2IS_SCHEDULED\3LOST_RACE_TRY_AGAIN"5152/* #define NAPI_DEBUG */53#ifdef NAPI_DEBUG54static int debug_napi;55SYSCTL_INT(_compat_linuxkpi, OID_AUTO, debug_napi, CTLFLAG_RWTUN,56&debug_napi, 0, "NAPI debug level");5758#define DNAPI_TODO 0x0159#define DNAPI_IMPROVE 0x0260#define DNAPI_TRACE 0x1061#define DNAPI_TRACE_TASK 0x2062#define DNAPI_DIRECT_DISPATCH 0x10006364#define NAPI_TRACE(_n) if (debug_napi & DNAPI_TRACE) \65printf("NAPI_TRACE %s:%d %lu %p (%#jx %b)\n", __func__, __LINE__, \66jiffies, _n, (uintmax_t)(_n)->state, \67(int)(_n)->state, LKPI_NAPI_FLAGS)68#define NAPI_TRACE2D(_n, _d) if (debug_napi & DNAPI_TRACE) \69printf("NAPI_TRACE %s:%d %lu %p (%#jx %b) %d\n", __func__, __LINE__, \70jiffies, _n, (uintmax_t)(_n)->state, \71(int)(_n)->state, LKPI_NAPI_FLAGS, _d)72#define NAPI_TRACE_TASK(_n, _p, _c) if (debug_napi & DNAPI_TRACE_TASK) \73printf("NAPI_TRACE %s:%d %lu %p (%#jx %b) pending %d count %d " \74"rx_count %d\n", __func__, __LINE__, \75jiffies, _n, (uintmax_t)(_n)->state, \76(int)(_n)->state, LKPI_NAPI_FLAGS, _p, _c, (_n)->rx_count)77#define NAPI_TODO() if (debug_napi & DNAPI_TODO) \78printf("NAPI_TODO %s:%d %lu\n", __func__, __LINE__, jiffies)79#define NAPI_IMPROVE() if (debug_napi & DNAPI_IMPROVE) \80printf("NAPI_IMPROVE %s:%d %lu\n", __func__, __LINE__, jiffies)8182#define NAPI_DIRECT_DISPATCH() ((debug_napi & DNAPI_DIRECT_DISPATCH) != 0)83#else84#define NAPI_TRACE(_n) do { } while(0)85#define NAPI_TRACE2D(_n, _d) do { } while(0)86#define NAPI_TRACE_TASK(_n, _p, _c) do { } while(0)87#define NAPI_TODO() do { } while(0)88#define NAPI_IMPROVE() do { } while(0)8990#define NAPI_DIRECT_DISPATCH() (0)91#endif9293/* -------------------------------------------------------------------------- */9495/*96* Check if a poll is running or can run and and if the latter97* make us as running. That way we ensure that only one poll98* can only ever run at the same time. Returns true if no poll99* was scheduled yet.100*/101bool102linuxkpi_napi_schedule_prep(struct napi_struct *napi)103{104unsigned long old, new;105106NAPI_TRACE(napi);107108/* Can can only update/return if all flags agree. */109do {110old = READ_ONCE(napi->state);111112/* If we are stopping, cannot run again. */113if ((old & BIT(LKPI_NAPI_FLAG_DISABLE_PENDING)) != 0) {114NAPI_TRACE(napi);115return (false);116}117118new = old;119/* We were already scheduled. Need to try again? */120if ((old & BIT(LKPI_NAPI_FLAG_IS_SCHEDULED)) != 0)121new |= BIT(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN);122new |= BIT(LKPI_NAPI_FLAG_IS_SCHEDULED);123124} while (atomic_cmpset_acq_long(&napi->state, old, new) == 0);125126NAPI_TRACE(napi);127return ((old & BIT(LKPI_NAPI_FLAG_IS_SCHEDULED)) == 0);128}129130static void131lkpi___napi_schedule_dd(struct napi_struct *napi)132{133unsigned long old, new;134int rc;135136rc = 0;137again:138NAPI_TRACE2D(napi, rc);139if (napi->poll != NULL)140rc = napi->poll(napi, napi->budget);141napi->rx_count += rc;142143/* Check if interrupts are still disabled, more work to do. */144/* Bandaid for now. */145if (rc >= napi->budget)146goto again;147148/* Bandaid for now. */149if (test_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &napi->state))150goto again;151152do {153new = old = READ_ONCE(napi->state);154clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &new);155clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &new);156} while (atomic_cmpset_acq_long(&napi->state, old, new) == 0);157158NAPI_TRACE2D(napi, rc);159}160161void162linuxkpi___napi_schedule(struct napi_struct *napi)163{164int rc;165166NAPI_TRACE(napi);167if (test_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->state)) {168clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &napi->state);169clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state);170NAPI_TRACE(napi);171return;172}173174if (NAPI_DIRECT_DISPATCH()) {175lkpi___napi_schedule_dd(napi);176} else {177rc = taskqueue_enqueue(napi->dev->napi_tq, &napi->napi_task);178NAPI_TRACE2D(napi, rc);179if (rc != 0) {180/* Should we assert EPIPE? */181return;182}183}184}185186bool187linuxkpi_napi_schedule(struct napi_struct *napi)188{189190NAPI_TRACE(napi);191192/*193* iwlwifi calls this sequence instead of napi_schedule()194* to be able to test the prep result.195*/196if (napi_schedule_prep(napi)) {197__napi_schedule(napi);198return (true);199}200201return (false);202}203204void205linuxkpi_napi_reschedule(struct napi_struct *napi)206{207208NAPI_TRACE(napi);209210/* Not sure what is different to napi_schedule yet. */211if (napi_schedule_prep(napi))212__napi_schedule(napi);213}214215bool216linuxkpi_napi_complete_done(struct napi_struct *napi, int ret)217{218unsigned long old, new;219220NAPI_TRACE(napi);221if (NAPI_DIRECT_DISPATCH())222return (true);223224do {225new = old = READ_ONCE(napi->state);226227/*228* If we lost a race before, we need to re-schedule.229* Leave IS_SCHEDULED set essentially doing "_prep".230*/231if (!test_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &old))232clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &new);233clear_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &new);234} while (atomic_cmpset_acq_long(&napi->state, old, new) == 0);235236NAPI_TRACE(napi);237238/* Someone tried to schedule while poll was running. Re-sched. */239if (test_bit(LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN, &old)) {240__napi_schedule(napi);241return (false);242}243244return (true);245}246247bool248linuxkpi_napi_complete(struct napi_struct *napi)249{250251NAPI_TRACE(napi);252return (napi_complete_done(napi, 0));253}254255void256linuxkpi_napi_disable(struct napi_struct *napi)257{258NAPI_TRACE(napi);259set_bit(LKPI_NAPI_FLAG_DISABLE_PENDING, &napi->state);260while (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state))261pause_sbt("napidslp", SBT_1MS, 0, C_HARDCLOCK);262clear_bit(LKPI_NAPI_FLAG_DISABLE_PENDING, &napi->state);263}264265void266linuxkpi_napi_enable(struct napi_struct *napi)267{268269NAPI_TRACE(napi);270KASSERT(!test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state),271("%s: enabling napi %p already scheduled\n", __func__, napi));272mb();273/* Let us be scheduled. */274clear_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state);275}276277void278linuxkpi_napi_synchronize(struct napi_struct *napi)279{280NAPI_TRACE(napi);281#if defined(SMP)282/* Check & sleep while a napi is scheduled. */283while (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state))284pause_sbt("napisslp", SBT_1MS, 0, C_HARDCLOCK);285#else286mb();287#endif288}289290/* -------------------------------------------------------------------------- */291292static void293lkpi_napi_task(void *ctx, int pending)294{295struct napi_struct *napi;296int count;297298KASSERT(ctx != NULL, ("%s: napi %p, pending %d\n",299__func__, ctx, pending));300napi = ctx;301KASSERT(napi->poll != NULL, ("%s: napi %p poll is NULL\n",302__func__, napi));303304NAPI_TRACE_TASK(napi, pending, napi->budget);305count = napi->poll(napi, napi->budget);306napi->rx_count += count;307NAPI_TRACE_TASK(napi, pending, count);308309/*310* We must not check against count < pending here. There are situations311* when a driver may "poll" and we may not have any work to do and that312* would make us re-schedule ourseless for ever.313*/314if (count >= napi->budget) {315/*316* Have to re-schedule ourselves. napi_complete() was not run317* in this case which means we are still SCHEDULED.318* In order to queue another task we have to directly call319* __napi_schedule() without _prep() in the way.320*/321__napi_schedule(napi);322}323}324325/* -------------------------------------------------------------------------- */326327void328linuxkpi_netif_napi_add(struct net_device *ndev, struct napi_struct *napi,329int(*napi_poll)(struct napi_struct *, int))330{331332napi->dev = ndev;333napi->poll = napi_poll;334napi->budget = NAPI_POLL_WEIGHT;335336INIT_LIST_HEAD(&napi->rx_list);337napi->rx_count = 0;338339TASK_INIT(&napi->napi_task, 0, lkpi_napi_task, napi);340341NAPI_LOCK(ndev);342TAILQ_INSERT_TAIL(&ndev->napi_head, napi, entry);343NAPI_UNLOCK(ndev);344345/* Anything else to do on the ndev? */346clear_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->state);347}348349static void350lkpi_netif_napi_del_locked(struct napi_struct *napi)351{352struct net_device *ndev;353354ndev = napi->dev;355NAPI_LOCK_ASSERT(ndev);356357set_bit(LKPI_NAPI_FLAG_SHUTDOWN, &napi->state);358TAILQ_REMOVE(&ndev->napi_head, napi, entry);359while (taskqueue_cancel(ndev->napi_tq, &napi->napi_task, NULL) != 0)360taskqueue_drain(ndev->napi_tq, &napi->napi_task);361}362363void364linuxkpi_netif_napi_del(struct napi_struct *napi)365{366struct net_device *ndev;367368ndev = napi->dev;369NAPI_LOCK(ndev);370lkpi_netif_napi_del_locked(napi);371NAPI_UNLOCK(ndev);372}373374/* -------------------------------------------------------------------------- */375376void377linuxkpi_init_dummy_netdev(struct net_device *ndev)378{379380memset(ndev, 0, sizeof(*ndev));381382ndev->reg_state = NETREG_DUMMY;383NAPI_LOCK_INIT(ndev);384TAILQ_INIT(&ndev->napi_head);385/* Anything else? */386387ndev->napi_tq = taskqueue_create("tq_ndev_napi", M_WAITOK,388taskqueue_thread_enqueue, &ndev->napi_tq);389/* One thread for now. */390(void) taskqueue_start_threads(&ndev->napi_tq, 1, PWAIT,391"ndev napi taskq");392}393394struct net_device *395linuxkpi_alloc_netdev(size_t len, const char *name, uint32_t flags,396void(*setup_func)(struct net_device *))397{398struct net_device *ndev;399400ndev = malloc(sizeof(*ndev) + len, M_NETDEV, M_NOWAIT);401if (ndev == NULL)402return (ndev);403404/* Always first as it zeros! */405linuxkpi_init_dummy_netdev(ndev);406407strlcpy(ndev->name, name, sizeof(*ndev->name));408409/* This needs extending as we support more. */410411if (setup_func != NULL)412setup_func(ndev);413414return (ndev);415}416417void418linuxkpi_free_netdev(struct net_device *ndev)419{420struct napi_struct *napi, *temp;421422NAPI_LOCK(ndev);423TAILQ_FOREACH_SAFE(napi, &ndev->napi_head, entry, temp) {424lkpi_netif_napi_del_locked(napi);425}426NAPI_UNLOCK(ndev);427428taskqueue_free(ndev->napi_tq);429ndev->napi_tq = NULL;430NAPI_LOCK_DESTROY(ndev);431432/* This needs extending as we support more. */433434free(ndev, M_NETDEV);435}436437438