From bfb87bf83d100ce361c6482be5bc06da449e6f28 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Thu, 30 Nov 2023 12:21:35 -0500 Subject: [PATCH] More functionality, little commentary --- i3e_template_driver.c | 134 ++++++++++++++++++++++++++++++++++++++---- i3e_template_driver.h | 15 ++++- 2 files changed, 133 insertions(+), 16 deletions(-) diff --git a/i3e_template_driver.c b/i3e_template_driver.c index c9bc08b..4e255d1 100644 --- a/i3e_template_driver.c +++ b/i3e_template_driver.c @@ -39,6 +39,8 @@ #include __FBSDID("$FreeBSD$"); +#include "opt_global.h" + #include #include #include @@ -102,6 +104,10 @@ i3e_template_detach(struct i3e_template_softc *sc) sc->sc_detached = 1; I3E_TEMPLATE_UNLOCK(sc); + ieee80211_ifdetach(&sc->sc_ic); + mbufq_drain(&sc->sc_snd); + mtx_destroy(&sc->sc_mtx); + return (0); } @@ -109,6 +115,7 @@ static int i3e_template_init(struct i3e_template_softc *sc) { printf("i3e_template_init\n"); + sc->sc_running = 1; return (0); } @@ -118,19 +125,51 @@ i3e_template_stop(struct i3e_template_softc *sc) printf("i3e_template_stop\n"); } +/* + * This is the handler for when a user runs ifconfig wlanX channel [CHAN NUMBER] + * Your handler code communicates with the device to change the device channel + * Very basic example handler: wi_set_channel + * Helper function ieee80211_chan2ieee, converts channel to IEEE channel number + */ static void i3e_template_set_channel(struct ieee80211com *ic) { struct i3e_template_softc *sc = ic->ic_softc; I3E_TEMPLATE_LOCK(sc); - printf("i3e_template_set_channel\n"); + printf("i3e_template_set_channel to %d\n", ieee80211_chan2ieee(ic, ic->ic_curchan)); I3E_TEMPLATE_UNLOCK(sc); } +/* + * Example driver: ural_transmit + */ +static int +i3e_template_transmit(struct ieee80211com *ic, struct mbuf *m) +{ + struct i3e_template_softc *sc = ic->ic_softc; + int ret = 0; + + I3E_TEMPLATE_LOCK(sc); + if (!sc->sc_running) { + ret = ENXIO; + goto fail; + } + + ret = mbufq_enqueue(&sc->sc_snd, m); + if (ret) { + goto fail; + } + +// i3e_template_start(sc); +fail: + I3E_TEMPLATE_UNLOCK(sc); + return (ret); +} + /* * Raw Transmission is handled here - * Come back to this, needs more detail + * XXX Come back to this, needs more detail */ static int i3e_template_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, @@ -149,7 +188,7 @@ i3e_template_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, } /* Raw transmission happens here */ fail: - printf("i3e_template_raw_xmit, return with %d\n", ret); +// printf("i3e_template_raw_xmit, return with %d\n", ret); I3E_TEMPLATE_UNLOCK(sc); return (ret); } @@ -210,6 +249,72 @@ i3e_template_scan_end(struct ieee80211com *ic) I3E_TEMPLATE_UNLOCK(sc); } +static int +i3e_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) +{ + struct i3e_template_vap *ivp = I3E_TEMPLATE_VAP(vap); + struct ieee80211com *ic = vap->iv_ic; + struct i3e_template_softc *sc = ic->ic_softc; + enum ieee80211_state ostate; + + printf("%s: newstate\n", sc->sc_ic.ic_name); + // XXX Figure out why the IEEE80211_UNLOCK + IEEE80211_UNLOCK(ic); + I3E_TEMPLATE_LOCK(sc); + + // Here, we may choose to handle the previous state + // The new state, potential values are listed in sys/net80211/ieee80211_proto.h + ostate = vap->iv_state; + + switch(ostate) { + case IEEE80211_S_INIT: + break; + case IEEE80211_S_SCAN: + break; + case IEEE80211_S_AUTH: + break; + case IEEE80211_S_ASSOC: + break; + case IEEE80211_S_CAC: + break; + case IEEE80211_S_RUN: + break; + case IEEE80211_S_CSA: + break; + case IEEE80211_S_SLEEP: + break; + default: + break; + } + + switch(nstate) { + case IEEE80211_S_INIT: + break; + case IEEE80211_S_SCAN: + break; + case IEEE80211_S_AUTH: + break; + case IEEE80211_S_ASSOC: + break; + case IEEE80211_S_CAC: + break; + case IEEE80211_S_RUN: + break; + case IEEE80211_S_CSA: + break; + case IEEE80211_S_SLEEP: + break; + default: + break; + } + + I3E_TEMPLATE_UNLOCK(sc); + IEEE80211_LOCK(ic); + + // Also execute the default newstate handler + return (ivp->iv_newstate(vap, nstate, arg)); +} + static struct ieee80211vap * i3e_template_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode, int flags, @@ -255,7 +360,9 @@ i3e_template_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int return (NULL); } - ivp->newstate = vap->iv_newstate; + /* Common practice is to override the default method for changing the state */ + ivp->iv_newstate = vap->iv_newstate; + vap->iv_newstate = i3e_newstate; ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status, mac); @@ -270,6 +377,8 @@ static int i3e_template_attach(struct i3e_template_softc *sc) I3E_TEMPLATE_LOCK_INIT(sc); // Initialize the Mutex lock + mbufq_init(&sc->sc_snd, ifqmaxlen); + ic->ic_softc = sc; ic->ic_name = "i3e0"; /* Ordinarily this would be device_get_nameunit(self), but manually setting it because * this is not a real driver */ @@ -321,6 +430,7 @@ static int i3e_template_attach(struct i3e_template_softc *sc) ic->ic_vap_delete = i3e_template_vap_delete; ic->ic_set_channel = i3e_template_set_channel; ic->ic_raw_xmit = i3e_template_raw_xmit; + ic->ic_transmit = i3e_template_transmit; ic->ic_update_mcast = i3e_template_update_mcast; ieee80211_announce(ic); @@ -330,26 +440,24 @@ static int i3e_template_attach(struct i3e_template_softc *sc) static int i3e_event_handler(struct module *module, int event_type, void *arg) { - int retval = 0; // function returns an integer error code, default 0 for OK + int retval = 0; // function returns an integer error code, default 0 for OK - switch (event_type) { // event_type is an enum; let's switch on it - case MOD_LOAD: // if we're loading + switch (event_type) { // event_type is an enum; let's switch on it + case MOD_LOAD: // if we're loading sc = malloc(sizeof(struct i3e_template_softc), M_80211_VAP, M_WAITOK | M_ZERO); i3e_template_attach(sc); - uprintf("LKM Loaded\n"); // spit out a loading message break; - case MOD_UNLOAD: // if were unloading - uprintf("LKM Unloaded\n"); // spit out an unloading messge + case MOD_UNLOAD: // if were unloading i3e_template_detach(sc); break; - default: // if we're doing anything else - retval = EOPNOTSUPP; // return a 'not supported' error + default: // if we're doing anything else + retval = EOPNOTSUPP; // return a 'not supported' error break; } - return(retval); // return the appropriate value + return(retval); // return the appropriate value } diff --git a/i3e_template_driver.h b/i3e_template_driver.h index bdc54c8..4b144b6 100644 --- a/i3e_template_driver.h +++ b/i3e_template_driver.h @@ -83,8 +83,6 @@ #include #include -/////////////////////////// Header File - /* * BSD drivers store driver instance-specific variables in their "softc" * structure. @@ -95,15 +93,26 @@ struct i3e_template_softc { int sc_detached; int sc_running; + + /* + * mbufq and its associated functions are not clearly defined anywhere. It is an implementation of + * `struct mbuf`. It is a method of queueing frames to be sent in memory prior to sending it. + * The purpose of this is performance, specifically when software operates faster than the hardware + * can push hardware in the data out. + * Its associated functions are located in sys/sys/mbuf.h. + */ + struct mbufq sc_snd; }; static struct i3e_template_softc *sc; struct i3e_template_vap { struct ieee80211vap vap; - int (*newstate)(struct ieee80211vap *, enum ieee80211_state, int); + int (*iv_newstate)(struct ieee80211vap *, enum ieee80211_state, int); }; +#define I3E_TEMPLATE_VAP(vap) ((struct i3e_template_vap *)(vap)) + //#define I3E_TEMPLATE_LOCK_INIT(_sc) mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), MTX_NETWORK_LOCK, MTX_DEF); #define I3E_TEMPLATE_LOCK_INIT(_sc) mtx_init(&(sc)->sc_mtx, "i3e0", MTX_NETWORK_LOCK, MTX_DEF); #define I3E_TEMPLATE_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)