Introduce a procedural interface to the ifnet structure. The new

interface allows the ifnet structure to be defined as an opaque
type in NIC drivers.  This then allows the ifnet structure to be
changed without a need to change or recompile NIC drivers.

Put differently, NIC drivers can be written and compiled once and
be used with different network stack implementations, provided of
course that those network stack implementations have an API and
ABI compatible interface.

This commit introduces the 'if_t' type to replace 'struct ifnet *'
as the type of a network interface. The 'if_t' type is defined as
'void *' to enable the compiler to perform type conversion to
'struct ifnet *' and vice versa where needed and without warnings.
The functions that implement the API are the only functions that
need to have an explicit cast.

The MII code has been converted to use the driver API to avoid
unnecessary code churn. Code churn comes from having to work with
both converted and unconverted drivers in correlation with having
callback functions that take an interface. By converting the MII
code first, the callback functions can be defined so that the
compiler will perform the typecasts automatically.

As soon as all drivers have been converted, the if_t type can be
redefined as needed and the API functions can be fix to not need
an explicit cast.

The immediate benefactors of this change are:
1.  Juniper Networks - The network stack implementation in Junos
    is entirely different from FreeBSD's one and this change
    allows Juniper to build "stock" NIC drivers that can be used
    in combination with both the FreeBSD and Junos stacks.
2.  FreeBSD - This change opens the door towards changing ifnet
    and implementing new features and optimizations in the network
    stack without it requiring a change in the many NIC drivers
    FreeBSD has.

Submitted by:	Anuranjan Shukla <anshukla@juniper.net>
Reviewed by:	glebius@
Obtained from:	Juniper Networks, Inc.
This commit is contained in:
Marcel Moolenaar 2014-06-02 17:54:39 +00:00
parent e74d21571f
commit 62d76917b8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=266974
19 changed files with 842 additions and 59 deletions

View file

@ -153,7 +153,7 @@ dcphy_attach(device_t dev)
&dcphy_funcs, 0);
/*PHY_RESET(sc);*/
dc_sc = sc->mii_pdata->mii_ifp->if_softc;
dc_sc = if_getsoftc(sc->mii_pdata->mii_ifp);
CSR_WRITE_4(dc_sc, DC_10BTSTAT, 0);
CSR_WRITE_4(dc_sc, DC_10BTCTRL, 0);
@ -191,7 +191,7 @@ dcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
int reg;
u_int32_t mode;
dc_sc = mii->mii_ifp->if_softc;
dc_sc = if_getsoftc(mii->mii_ifp);
switch (cmd) {
case MII_POLLSTAT:
@ -201,7 +201,7 @@ dcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
/*
* If the interface is not up, don't do anything.
*/
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
break;
mii->mii_media_active = IFM_NONE;
@ -251,7 +251,7 @@ dcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
/*
* Is the interface even up?
*/
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
return (0);
/*
@ -298,12 +298,12 @@ dcphy_status(struct mii_softc *sc)
int anlpar, tstat;
struct dc_softc *dc_sc;
dc_sc = mii->mii_ifp->if_softc;
dc_sc = if_getsoftc(mii->mii_ifp);
mii->mii_media_status = IFM_AVALID;
mii->mii_media_active = IFM_ETHER;
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
return;
tstat = CSR_READ_4(dc_sc, DC_10BTSTAT);
@ -378,7 +378,7 @@ dcphy_auto(struct mii_softc *mii)
{
struct dc_softc *sc;
sc = mii->mii_pdata->mii_ifp->if_softc;
sc = if_getsoftc(mii->mii_pdata->mii_ifp);
DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
@ -399,7 +399,7 @@ dcphy_reset(struct mii_softc *mii)
{
struct dc_softc *sc;
sc = mii->mii_pdata->mii_ifp->if_softc;
sc = if_getsoftc(mii->mii_pdata->mii_ifp);
DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
DELAY(1000);

View file

@ -152,7 +152,7 @@ pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
/*
* If the interface is not up, don't do anything.
*/
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
break;
/*
@ -180,7 +180,7 @@ pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
/*
* Is the interface even up?
*/
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
return (0);
break;
@ -201,7 +201,7 @@ pnphy_status(struct mii_softc *sc)
int reg;
struct dc_softc *dc_sc;
dc_sc = mii->mii_ifp->if_softc;
dc_sc = if_getsoftc(mii->mii_ifp);
mii->mii_media_status = IFM_AVALID;
mii->mii_media_active = IFM_ETHER;

View file

@ -122,7 +122,7 @@ inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
/*
* If the interface is not up, don't do anything.
*/
if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
break;
mii_phy_setmedia(sc);

View file

@ -43,6 +43,7 @@ __FBSDID("$FreeBSD$");
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/bus.h>
#include <sys/taskqueue.h>
#include <net/if.h>
#include <net/if_var.h>
@ -197,7 +198,7 @@ brgphy_attach(device_t dev)
struct bge_softc *bge_sc = NULL;
struct bce_softc *bce_sc = NULL;
struct mii_softc *sc;
struct ifnet *ifp;
if_t ifp;
bsc = device_get_softc(dev);
sc = &bsc->mii_sc;
@ -209,10 +210,10 @@ brgphy_attach(device_t dev)
ifp = sc->mii_pdata->mii_ifp;
/* Find the MAC driver associated with this PHY. */
if (strcmp(ifp->if_dname, "bge") == 0)
bge_sc = ifp->if_softc;
else if (strcmp(ifp->if_dname, "bce") == 0)
bce_sc = ifp->if_softc;
if (strcmp(if_getdname(ifp), "bge") == 0)
bge_sc = if_getsoftc(ifp);
else if (strcmp(if_getdname(ifp), "bce") == 0)
bce_sc = if_getsoftc(ifp);
/* Handle any special cases based on the PHY ID */
switch (sc->mii_mpd_oui) {
@ -879,7 +880,7 @@ brgphy_reset(struct mii_softc *sc)
{
struct bge_softc *bge_sc = NULL;
struct bce_softc *bce_sc = NULL;
struct ifnet *ifp;
if_t ifp;
int i, val;
/*
@ -932,10 +933,10 @@ brgphy_reset(struct mii_softc *sc)
ifp = sc->mii_pdata->mii_ifp;
/* Find the driver associated with this PHY. */
if (strcmp(ifp->if_dname, "bge") == 0) {
bge_sc = ifp->if_softc;
} else if (strcmp(ifp->if_dname, "bce") == 0) {
bce_sc = ifp->if_softc;
if (strcmp(if_getdname(ifp), "bge") == 0) {
bge_sc = if_getsoftc(ifp);
} else if (strcmp(if_getdname(ifp), "bce") == 0) {
bce_sc = if_getsoftc(ifp);
}
if (bge_sc) {
@ -954,7 +955,7 @@ brgphy_reset(struct mii_softc *sc)
brgphy_fixup_jitter_bug(sc);
if (bge_sc->bge_flags & BGE_FLAG_JUMBO)
brgphy_jumbo_settings(sc, ifp->if_mtu);
brgphy_jumbo_settings(sc, if_getmtu(ifp));
if ((bge_sc->bge_phy_flags & BGE_PHY_NO_WIRESPEED) == 0)
brgphy_ethernet_wirespeed(sc);
@ -1065,11 +1066,11 @@ brgphy_reset(struct mii_softc *sc)
(BCE_CHIP_REV(bce_sc) == BCE_CHIP_REV_Bx))
brgphy_fixup_disable_early_dac(sc);
brgphy_jumbo_settings(sc, ifp->if_mtu);
brgphy_jumbo_settings(sc, if_getmtu(ifp));
brgphy_ethernet_wirespeed(sc);
} else {
brgphy_fixup_ber_bug(sc);
brgphy_jumbo_settings(sc, ifp->if_mtu);
brgphy_jumbo_settings(sc, if_getmtu(ifp));
brgphy_ethernet_wirespeed(sc);
}
}

View file

@ -132,14 +132,14 @@ static int
e1000phy_attach(device_t dev)
{
struct mii_softc *sc;
struct ifnet *ifp;
if_t ifp;
sc = device_get_softc(dev);
mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &e1000phy_funcs, 0);
ifp = sc->mii_pdata->mii_ifp;
if (strcmp(ifp->if_dname, "msk") == 0 &&
if (strcmp(if_getdname(ifp), "msk") == 0 &&
(sc->mii_flags & MIIF_MACPRIV0) != 0)
sc->mii_flags |= MIIF_PHYPRIV0;

View file

@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/taskqueue.h>
#include <sys/bus.h>
#include <net/if.h>
@ -109,7 +110,7 @@ ip1000phy_attach(device_t dev)
ma = device_get_ivars(dev);
flags = MIIF_NOISOLATE | MIIF_NOMANPAUSE;
if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxICPLUS_IP1000A &&
strcmp(ma->mii_data->mii_ifp->if_dname, "stge") == 0 &&
strcmp(if_getdname(ma->mii_data->mii_ifp), "stge") == 0 &&
(miibus_get_flags(dev) & MIIF_MACPRIV0) != 0)
flags |= MIIF_PHYPRIV0;
mii_phy_dev_attach(dev, flags, &ip1000phy_funcs, 1);

View file

@ -105,7 +105,7 @@ jmphy_attach(device_t dev)
ma = device_get_ivars(dev);
flags = 0;
if (strcmp(ma->mii_data->mii_ifp->if_dname, "jme") == 0 &&
if (strcmp(if_getdname(ma->mii_data->mii_ifp), "jme") == 0 &&
(miibus_get_flags(dev) & MIIF_MACPRIV0) != 0)
flags |= MIIF_PHYPRIV0;
mii_phy_dev_attach(dev, flags, &jmphy_funcs, 1);

View file

@ -106,7 +106,7 @@ driver_t miibus_driver = {
};
struct miibus_ivars {
struct ifnet *ifp;
if_t ifp;
ifm_change_cb_t ifmedia_upd;
ifm_stat_cb_t ifmedia_sts;
u_int mii_flags;
@ -147,8 +147,8 @@ miibus_attach(device_t dev)
ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
ivars->ifmedia_sts);
mii->mii_ifp = ivars->ifp;
mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
if_setcapabilitiesbit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
if_setcapenablebit(mii->mii_ifp, IFCAP_LINKSTATE, 0);
LIST_INIT(&mii->mii_phys);
return (bus_generic_attach(dev));
@ -308,7 +308,7 @@ miibus_statchg(device_t dev)
MIIBUS_STATCHG(parent);
mii = device_get_softc(dev);
mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active));
}
static void
@ -330,7 +330,7 @@ miibus_linkchg(device_t dev)
link_state = LINK_STATE_DOWN;
} else
link_state = LINK_STATE_UNKNOWN;
if_link_state_change(mii->mii_ifp, link_state);
if_linkstate_change_drv(mii->mii_ifp, link_state);
}
static void
@ -358,7 +358,7 @@ miibus_mediainit(device_t dev)
* the PHYs to the network interface driver parent.
*/
int
mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
mii_attach(device_t dev, device_t *miibus, void *ifp,
ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
int phyloc, int offloc, int flags)
{

View file

@ -36,6 +36,7 @@
#define _DEV_MII_MIIVAR_H_
#include <sys/queue.h>
#include <net/if_var.h> /* XXX driver API temporary */
/*
* Media Independent Interface data structure defintions
@ -57,7 +58,7 @@ typedef void (*mii_statchg_t)(struct device *);
*/
struct mii_data {
struct ifmedia mii_media; /* media information */
struct ifnet *mii_ifp; /* pointer back to network interface */
if_t mii_ifp; /* pointer back to network interface */
/*
* For network interfaces with multiple PHYs, a list of all
@ -246,7 +247,7 @@ MIIBUS_ACCESSOR(flags, FLAGS, u_int)
extern devclass_t miibus_devclass;
extern driver_t miibus_driver;
int mii_attach(device_t, device_t *, struct ifnet *, ifm_change_cb_t,
int mii_attach(device_t, device_t *, if_t, ifm_change_cb_t,
ifm_stat_cb_t, int, int, int, int);
void mii_down(struct mii_data *);
int mii_mediachg(struct mii_data *);

View file

@ -186,7 +186,7 @@ nsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
*/
reg |= 0x0100 | 0x0400;
if (strcmp(mii->mii_ifp->if_dname, "fxp") == 0)
if (strcmp(if_getdname(mii->mii_ifp), "fxp") == 0)
PHY_WRITE(sc, MII_NSPHY_PCR, reg);
mii_phy_setmedia(sc);

View file

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/taskqueue.h>
#include <sys/bus.h>
#include <net/if.h>
@ -118,7 +119,7 @@ rgephy_attach(device_t dev)
sc = device_get_softc(dev);
ma = device_get_ivars(dev);
flags = 0;
if (strcmp(ma->mii_data->mii_ifp->if_dname, "re") == 0)
if (strcmp(if_getdname(ma->mii_data->mii_ifp), "re") == 0)
flags |= MIIF_PHYPRIV0;
mii_phy_dev_attach(dev, flags, &rgephy_funcs, 0);

View file

@ -265,7 +265,7 @@ truephy_reset(struct mii_softc *sc)
mii_phy_reset(sc);
if (TRUEPHY_FRAMELEN(sc->mii_pdata->mii_ifp->if_mtu) > 2048) {
if (TRUEPHY_FRAMELEN((if_getmtu(sc->mii_pdata->mii_ifp)) > 2048)) {
int conf;
conf = PHY_READ(sc, TRUEPHY_CONF);

View file

@ -804,3 +804,9 @@ generic_netmap_attach(struct ifnet *ifp)
return retval;
}
struct netmap_adapter *
netmap_getna(if_t ifp)
{
return (NA((struct ifnet *)ifp));
}

View file

@ -1262,6 +1262,7 @@ void netmap_catch_tx(struct netmap_generic_adapter *na, int enable);
int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr);
int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
struct netmap_adapter *netmap_getna(if_t ifp);
/*
* netmap_mitigation API. This is used by the generic adapter
@ -1376,5 +1377,4 @@ void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
struct netmap_vp_adapter *dst_na,
struct nm_bdg_fwd *ft_p, struct netmap_ring *ring,
u_int *j, u_int lim, u_int *howmany);
#endif /* _NET_NETMAP_KERN_H_ */

View file

@ -451,6 +451,19 @@ netisr_poll(void)
mtx_unlock(&poll_mtx);
}
/* The following should be temporary, till all drivers use the driver API */
int
ether_poll_register_drv(poll_handler_drv_t *h, if_t ifh)
{
return (ether_poll_register((poll_handler_t *)h, (struct ifnet *)ifh));
}
int
ether_poll_deregister_drv(if_t ifh)
{
return (ether_poll_deregister((struct ifnet *)ifh));
}
/*
* Try to register routine for polling. Returns 0 if successful
* (and polling should be enabled), error code otherwise.

View file

@ -63,12 +63,16 @@
#include <machine/stdarg.h>
#include <vm/uma.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_clone.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net/if_media.h>
#include <net/if_vlan_var.h>
#include <net/radix.h>
#include <net/route.h>
#include <net/vnet.h>
@ -1400,17 +1404,17 @@ if_addr_runlock(struct ifnet *ifp)
}
void
if_maddr_rlock(struct ifnet *ifp)
if_maddr_rlock(if_t ifp)
{
IF_ADDR_RLOCK(ifp);
IF_ADDR_RLOCK((struct ifnet *)ifp);
}
void
if_maddr_runlock(struct ifnet *ifp)
if_maddr_runlock(if_t ifp)
{
IF_ADDR_RUNLOCK(ifp);
IF_ADDR_RUNLOCK((struct ifnet *)ifp);
}
/*
@ -3461,3 +3465,644 @@ if_deregister_com_alloc(u_char type)
if_com_alloc[type] = NULL;
if_com_free[type] = NULL;
}
/* API for driver access to network stack owned ifnet.*/
uint64_t
if_setbaudrate(void *arg, uint64_t baudrate)
{
struct ifnet *ifp = arg;
uint64_t oldbrate;
oldbrate = ifp->if_baudrate;
ifp->if_baudrate = baudrate;
return (oldbrate);
}
uint64_t
if_getbaudrate(if_t ifp)
{
return (((struct ifnet *)ifp)->if_baudrate);
}
int
if_setcapabilities(if_t ifp, int capabilities)
{
((struct ifnet *)ifp)->if_capabilities = capabilities;
return (0);
}
int
if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
{
((struct ifnet *)ifp)->if_capabilities |= setbit;
((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
return (0);
}
int
if_getcapabilities(if_t ifp)
{
return ((struct ifnet *)ifp)->if_capabilities;
}
int
if_setcapenable(if_t ifp, int capabilities)
{
((struct ifnet *)ifp)->if_capenable = capabilities;
return (0);
}
int
if_setcapenablebit(if_t ifp, int setcap, int clearcap)
{
if(setcap)
((struct ifnet *)ifp)->if_capenable |= setcap;
if(clearcap)
((struct ifnet *)ifp)->if_capenable &= ~clearcap;
return (0);
}
const char *
if_getdname(if_t ifp)
{
return ((struct ifnet *)ifp)->if_dname;
}
int
if_togglecapenable(if_t ifp, int togglecap)
{
((struct ifnet *)ifp)->if_capenable ^= togglecap;
return (0);
}
int
if_getcapenable(if_t ifp)
{
return ((struct ifnet *)ifp)->if_capenable;
}
/*
* This is largely undesirable because it ties ifnet to a device, but does
* provide flexiblity for an embedded product vendor. Should be used with
* the understanding that it violates the interface boundaries, and should be
* a last resort only.
*/
int
if_setdev(if_t ifp, void *dev)
{
return (0);
}
int
if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
{
((struct ifnet *)ifp)->if_drv_flags |= set_flags;
((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
return (0);
}
int
if_getdrvflags(if_t ifp)
{
return ((struct ifnet *)ifp)->if_drv_flags;
}
int
if_setdrvflags(if_t ifp, int flags)
{
((struct ifnet *)ifp)->if_drv_flags = flags;
return (0);
}
int
if_setflags(if_t ifp, int flags)
{
((struct ifnet *)ifp)->if_flags = flags;
return (0);
}
int
if_setflagbits(if_t ifp, int set, int clear)
{
((struct ifnet *)ifp)->if_flags |= set;
((struct ifnet *)ifp)->if_flags &= ~clear;
return (0);
}
int
if_getflags(if_t ifp)
{
return ((struct ifnet *)ifp)->if_flags;
}
int
if_clearhwassist(if_t ifp)
{
((struct ifnet *)ifp)->if_hwassist = 0;
return (0);
}
int
if_sethwassistbits(if_t ifp, int toset, int toclear)
{
((struct ifnet *)ifp)->if_hwassist |= toset;
((struct ifnet *)ifp)->if_hwassist &= ~toclear;
return (0);
}
int
if_sethwassist(if_t ifp, int hwassist_bit)
{
((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
return (0);
}
int
if_gethwassist(if_t ifp)
{
return ((struct ifnet *)ifp)->if_hwassist;
}
int
if_setmtu(if_t ifp, int mtu)
{
((struct ifnet *)ifp)->if_mtu = mtu;
return (0);
}
int
if_getmtu(if_t ifp)
{
return ((struct ifnet *)ifp)->if_mtu;
}
int
if_setsoftc(if_t ifp, void *softc)
{
((struct ifnet *)ifp)->if_softc = softc;
return (0);
}
void *
if_getsoftc(if_t ifp)
{
return ((struct ifnet *)ifp)->if_softc;
}
void
if_setrcvif(struct mbuf *m, if_t ifp)
{
m->m_pkthdr.rcvif = (struct ifnet *)ifp;
}
void
if_setvtag(struct mbuf *m, uint16_t tag)
{
m->m_pkthdr.ether_vtag = tag;
}
uint16_t
if_getvtag(struct mbuf *m)
{
return (m->m_pkthdr.ether_vtag);
}
/* Statistics */
int
if_incipackets(if_t ifp, int pkts)
{
((struct ifnet *)ifp)->if_ipackets += pkts;
return (0);
}
int
if_incopackets(if_t ifp, int pkts)
{
((struct ifnet *)ifp)->if_opackets += pkts;
return (0);
}
int
if_incierrors(if_t ifp, int ierrors)
{
((struct ifnet *)ifp)->if_ierrors += ierrors;
return (0);
}
int
if_setierrors(if_t ifp, int ierrors)
{
((struct ifnet *)ifp)->if_ierrors = ierrors;
return (0);
}
int
if_setoerrors(if_t ifp, int oerrors)
{
((struct ifnet *)ifp)->if_oerrors = oerrors;
return (0);
}
int if_incoerrors(if_t ifp, int oerrors)
{
((struct ifnet *)ifp)->if_oerrors += oerrors;
return (0);
}
int if_inciqdrops(if_t ifp, int val)
{
((struct ifnet *)ifp)->if_iqdrops += val;
return (0);
}
int
if_setcollisions(if_t ifp, int collisions)
{
((struct ifnet *)ifp)->if_collisions = collisions;
return (0);
}
int
if_inccollisions(if_t ifp, int collisions)
{
((struct ifnet *)ifp)->if_collisions += collisions;
return (0);
}
int
if_setipackets(if_t ifp, int pkts)
{
((struct ifnet *)ifp)->if_ipackets = pkts;
return (0);
}
int
if_setopackets(if_t ifp, int pkts)
{
((struct ifnet *)ifp)->if_opackets = pkts;
return (0);
}
int
if_incobytes(if_t ifp, int bytes)
{
((struct ifnet *)ifp)->if_obytes += bytes;
return (0);
}
int
if_setibytes(if_t ifp, int bytes)
{
((struct ifnet *)ifp)->if_ibytes = bytes;
return (0);
}
int
if_setobytes(if_t ifp, int bytes)
{
((struct ifnet *)ifp)->if_obytes = bytes;
return (0);
}
int
if_sendq_empty(if_t ifp)
{
return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
}
int if_getiqdrops(if_t ifp)
{
return ((struct ifnet *)ifp)->if_iqdrops;
}
int
if_incimcasts(if_t ifp, int mcast)
{
((struct ifnet *)ifp)->if_imcasts += mcast;
return (0);
}
int
if_incomcasts(if_t ifp, int mcast)
{
((struct ifnet *)ifp)->if_omcasts += mcast;
return (0);
}
int
if_setimcasts(if_t ifp, int mcast)
{
((struct ifnet *)ifp)->if_imcasts = mcast;
return (0);
}
struct ifaddr *
if_getifaddr(if_t ifp)
{
return ((struct ifnet *)ifp)->if_addr;
}
int
if_getamcount(if_t ifp)
{
return ((struct ifnet *)ifp)->if_amcount;
}
int
if_setsendqready(if_t ifp)
{
IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
return (0);
}
int
if_setsendqlen(if_t ifp, int tx_desc_count)
{
IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
return (0);
}
int
if_vlantrunkinuse(if_t ifp)
{
return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
}
int
if_input(if_t ifp, struct mbuf* sendmp)
{
(*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
return (0);
}
/* XXX */
#ifndef ETH_ADDR_LEN
#define ETH_ADDR_LEN 6
#endif
int
if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
{
struct ifmultiaddr *ifma;
uint8_t *lmta = (uint8_t *)mta;
int mcnt = 0;
TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_LINK)
continue;
if (mcnt == max)
break;
bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
&lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
mcnt++;
}
*cnt = mcnt;
return (0);
}
int
if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
{
int error;
if_maddr_rlock(ifp);
error = if_setupmultiaddr(ifp, mta, cnt, max);
if_maddr_runlock(ifp);
return (error);
}
int
if_multiaddr_count(if_t ifp, int max)
{
struct ifmultiaddr *ifma;
int count;
count = 0;
if_maddr_rlock(ifp);
TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_LINK)
continue;
count++;
if (count == max)
break;
}
if_maddr_runlock(ifp);
return (count);
}
struct mbuf *
if_dequeue(if_t ifp)
{
struct mbuf *m;
IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
return (m);
}
int
if_sendq_prepend(if_t ifp, struct mbuf *m)
{
IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
return (0);
}
int
if_setifheaderlen(if_t ifp, int len)
{
((struct ifnet *)ifp)->if_data.ifi_hdrlen = len;
return (0);
}
caddr_t
if_getlladdr(if_t ifp)
{
return (IF_LLADDR((struct ifnet *)ifp));
}
void *
if_gethandle(u_char type)
{
return (if_alloc(type));
}
void
if_bpfmtap(if_t ifh, struct mbuf *m)
{
struct ifnet *ifp = (struct ifnet *)ifh;
BPF_MTAP(ifp, m);
}
void
if_etherbpfmtap(if_t ifh, struct mbuf *m)
{
struct ifnet *ifp = (struct ifnet *)ifh;
ETHER_BPF_MTAP(ifp, m);
}
void
if_vlancap(if_t ifh)
{
struct ifnet *ifp = (struct ifnet *)ifh;
VLAN_CAPABILITIES(ifp);
}
void
if_setinitfn(if_t ifp, void (*init_fn)(void *))
{
((struct ifnet *)ifp)->if_init = init_fn;
}
void
if_setioctlfn(if_t ifp, int (*ioctl_fn)(void *, u_long, caddr_t))
{
((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
}
void
if_setstartfn(if_t ifp, void (*start_fn)(void *))
{
((struct ifnet *)ifp)->if_start = (void *)start_fn;
}
void
if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
{
((struct ifnet *)ifp)->if_transmit = start_fn;
}
void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
{
((struct ifnet *)ifp)->if_qflush = flush_fn;
}
/* These wrappers are hopefully temporary, till all drivers use drvapi */
#ifdef INET
void
arp_ifinit_drv(if_t ifh, struct ifaddr *ifa)
{
arp_ifinit((struct ifnet *)ifh, ifa);
}
#endif
void
ether_ifattach_drv(if_t ifh, const u_int8_t *lla)
{
ether_ifattach((struct ifnet *)ifh, lla);
}
void
ether_ifdetach_drv(if_t ifh)
{
ether_ifdetach((struct ifnet *)ifh);
}
int
ether_ioctl_drv(if_t ifh, u_long cmd, caddr_t data)
{
struct ifnet *ifp = (struct ifnet *)ifh;
return (ether_ioctl(ifp, cmd, data));
}
int
ifmedia_ioctl_drv(if_t ifh, struct ifreq *ifr, struct ifmedia *ifm,
u_long cmd)
{
struct ifnet *ifp = (struct ifnet *)ifh;
return (ifmedia_ioctl(ifp, ifr, ifm, cmd));
}
void
if_free_drv(if_t ifh)
{
if_free((struct ifnet *)ifh);
}
void
if_initname_drv(if_t ifh, const char *name, int unit)
{
if_initname((struct ifnet *)ifh, name, unit);
}
void
if_linkstate_change_drv(if_t ifh, int link_state)
{
if_link_state_change((struct ifnet *)ifh, link_state);
}
void
ifmedia_init_drv(struct ifmedia *ifm, int ncmask, int (*chg_cb)(void *),
void (*sts_cb)(void *, struct ifmediareq *))
{
ifmedia_init(ifm, ncmask, (ifm_change_cb_t)chg_cb,
(ifm_stat_cb_t)sts_cb);
}
void
if_addr_rlock_drv(if_t ifh)
{
if_addr_runlock((struct ifnet *)ifh);
}
void
if_addr_runlock_drv(if_t ifh)
{
if_addr_runlock((struct ifnet *)ifh);
}
void
if_qflush_drv(if_t ifh)
{
if_qflush((struct ifnet *)ifh);
}
/* Revisit these - These are inline functions originally. */
int
drbr_inuse_drv(if_t ifh, struct buf_ring *br)
{
return drbr_inuse_drv(ifh, br);
}
struct mbuf*
drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
{
return drbr_dequeue(ifh, br);
}
int
drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
{
return drbr_needs_enqueue(ifh, br);
}
int
drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
{
return drbr_enqueue(ifh, br, m);
}

View file

@ -59,8 +59,8 @@ struct ifnet;
/*
* Driver callbacks for media status and change requests.
*/
typedef int (*ifm_change_cb_t)(struct ifnet *ifp);
typedef void (*ifm_stat_cb_t)(struct ifnet *ifp, struct ifmediareq *req);
typedef int (*ifm_change_cb_t)(struct ifnet *);
typedef void (*ifm_stat_cb_t)(struct ifnet *, struct ifmediareq *req);
/*
* In-kernel representation of a single supported media type.
@ -106,6 +106,7 @@ void ifmedia_set(struct ifmedia *ifm, int mword);
int ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr,
struct ifmedia *ifm, u_long cmd);
/* Compute baudrate for a given media. */
uint64_t ifmedia_baudrate(int);

View file

@ -66,6 +66,7 @@ struct carp_softc;
struct ifvlantrunk;
struct route; /* if_output */
struct vnet;
struct ifmedia;
#ifdef _KERNEL
#include <sys/mbuf.h> /* ifqueue only? */
@ -93,6 +94,15 @@ VNET_DECLARE(struct pfil_head, link_pfil_hook); /* packet filter hooks */
#define V_link_pfil_hook VNET(link_pfil_hook)
#endif /* _KERNEL */
typedef void (*if_start_fn_t)(struct ifnet *);
typedef int (*if_ioctl_fn_t)(struct ifnet *, u_long, caddr_t);
typedef void (*if_init_fn_t)(void *);
typedef void (*if_qflush_fn_t)(struct ifnet *);
typedef int (*if_transmit_fn_t)(struct ifnet *, struct mbuf *);
/* Opaque object pointing to interface structure (ifnet) */
typedef void *if_t;
/*
* Structure defining a network interface.
*
@ -170,18 +180,14 @@ struct ifnet {
struct route *);
void (*if_input) /* input routine (from h/w driver) */
(struct ifnet *, struct mbuf *);
void (*if_start) /* initiate output routine */
(struct ifnet *);
int (*if_ioctl) /* ioctl routine */
(struct ifnet *, u_long, caddr_t);
void (*if_init) /* Init routine */
(void *);
if_start_fn_t if_start; /* initiate output routine */
if_ioctl_fn_t if_ioctl; /* ioctl routine */
if_init_fn_t if_init; /* Init routine */
int (*if_resolvemulti) /* validate/resolve multicast */
(struct ifnet *, struct sockaddr **, struct sockaddr *);
void (*if_qflush) /* flush any queues */
(struct ifnet *);
int (*if_transmit) /* initiate output routine */
(struct ifnet *, struct mbuf *);
if_qflush_fn_t if_qflush; /* flush any queue */
if_transmit_fn_t if_transmit; /* initiate output routine */
void (*if_reassign) /* reassign to vnet routine */
(struct ifnet *, struct vnet *, char *);
@ -254,8 +260,8 @@ struct ifnet {
*/
void if_addr_rlock(struct ifnet *ifp); /* if_addrhead */
void if_addr_runlock(struct ifnet *ifp); /* if_addrhead */
void if_maddr_rlock(struct ifnet *ifp); /* if_multiaddrs */
void if_maddr_runlock(struct ifnet *ifp); /* if_multiaddrs */
void if_maddr_rlock(if_t ifp); /* if_multiaddrs */
void if_maddr_runlock(if_t ifp); /* if_multiaddrs */
#ifdef _KERNEL
#ifdef _SYS_EVENTHANDLER_H_
@ -514,5 +520,109 @@ void if_deregister_com_alloc(u_char type);
#define IF_LLADDR(ifp) \
LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
uint64_t if_setbaudrate(if_t ifp, uint64_t baudrate);
uint64_t if_getbaudrate(if_t ifp);
int if_setcapabilities(if_t ifp, int capabilities);
int if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit);
int if_getcapabilities(if_t ifp);
int if_togglecapenable(if_t ifp, int togglecap);
int if_setcapenable(if_t ifp, int capenable);
int if_setcapenablebit(if_t ifp, int setcap, int clearcap);
int if_getcapenable(if_t ifp);
const char *if_getdname(if_t ifp);
int if_setdev(if_t ifp, void *dev);
int if_setdrvflagbits(if_t ifp, int if_setflags, int clear_flags);
int if_getdrvflags(if_t ifp);
int if_setdrvflags(if_t ifp, int flags);
int if_clearhwassist(if_t ifp);
int if_sethwassistbits(if_t ifp, int toset, int toclear);
int if_sethwassist(if_t ifp, int hwassist_bit);
int if_gethwassist(if_t ifp);
int if_setsoftc(if_t ifp, void *softc);
void *if_getsoftc(if_t ifp);
int if_setflags(if_t ifp, int flags);
int if_setmtu(if_t ifp, int mtu);
int if_getmtu(if_t ifp);
int if_setflagbits(if_t ifp, int set, int clear);
int if_getflags(if_t ifp);
int if_sendq_empty(if_t ifp);
int if_setsendqready(if_t ifp);
int if_setsendqlen(if_t ifp, int tx_desc_count);
int if_input(if_t ifp, struct mbuf* sendmp);
int if_sendq_prepend(if_t ifp, struct mbuf *m);
struct mbuf *if_dequeue(if_t ifp);
int if_setifheaderlen(if_t ifp, int len);
void if_setrcvif(struct mbuf *m, if_t ifp);
void if_setvtag(struct mbuf *m, u_int16_t tag);
u_int16_t if_getvtag(struct mbuf *m);
int if_vlantrunkinuse(if_t ifp);
caddr_t if_getlladdr(if_t ifp);
void *if_gethandle(u_char);
void if_bpfmtap(if_t ifp, struct mbuf *m);
void if_etherbpfmtap(if_t ifp, struct mbuf *m);
void if_vlancap(if_t ifp);
int if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max);
int if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max);
int if_multiaddr_count(if_t ifp, int max);
int if_getamcount(if_t ifp);
struct ifaddr * if_getifaddr(if_t ifp);
/* Shim for drivers using drvapi */
int ifmedia_ioctl_drv(if_t ifp, struct ifreq *ifr, struct ifmedia *ifm,
u_long cmd);
/* Statistics */
int if_incipackets(if_t ifp, int pkt);
int if_incopackets(if_t ifp, int pkts);
int if_incierrors(if_t ifp, int ierrors);
int if_incoerrors(if_t ifp, int oerrors);
int if_inciqdrops(if_t ifp, int val);
int if_setierrors(if_t ifp, int ierrors);
int if_setoerrors(if_t ifp, int oerrors);
int if_setcollisions(if_t ifp, int collisions);
int if_inccollisions(if_t ifp, int collisions);
int if_incobytes(if_t ifp, int bytes);
int if_getiqdrops(if_t ifp);
int if_incimcasts(if_t ifp, int imcasts);
int if_incomcasts(if_t ifp, int imcasts);
int if_setipackets(if_t ifp, int pkts);
int if_setopackets(if_t ifp, int pkts);
int if_setibytes(if_t ifp, int bytes);
int if_setobytes(if_t ifp, int bytes);
int if_setimcasts(if_t ifp, int pkts);
/* Functions */
void if_setinitfn(if_t ifp, void (*)(void *));
void if_setioctlfn(if_t ifp, int (*)(void *, u_long, caddr_t));
void if_setstartfn(if_t ifp, void (*)(void *));
void if_settransmitfn(if_t ifp, if_transmit_fn_t);
void if_setqflushfn(if_t ifp, if_qflush_fn_t);
/* Shim functions till all drivers use drvapi */
void arp_ifinit_drv(if_t ifp, struct ifaddr *ifa);
void ether_ifattach_drv(if_t ifp, const u_int8_t *lla);
void ether_ifdetach_drv(if_t ifp);
int ether_ioctl_drv(if_t ifp, u_long cmd, caddr_t data);
void if_free_drv(if_t ifp);
void if_initname_drv(if_t ifp, const char *name, int unit);
void if_linkstate_change_drv(if_t ifp, int link_state);
struct ifmedia;
void ifmedia_init_drv(struct ifmedia *, int, int (*)(void *),
void (*)(void *, struct ifmediareq *));
void if_addr_rlock_drv(if_t ifp);
void if_addr_runlock_drv(if_t ifp);
void if_qflush_drv(if_t ifp);
/* Revisit the below. These are inline functions originally */
int drbr_inuse_drv(if_t ifp, struct buf_ring *br);
struct mbuf* drbr_dequeue_drv(if_t ifp, struct buf_ring *br);
int drbr_needs_enqueue_drv(if_t ifp, struct buf_ring *br);
int drbr_enqueue_drv(if_t ifp, struct buf_ring *br, struct mbuf *m);
#endif /* _KERNEL */
#endif /* !_NET_IF_VAR_H_ */

View file

@ -484,6 +484,10 @@ enum poll_cmd { POLL_ONLY, POLL_AND_CHECK_STATUS };
typedef int poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
int ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
int ether_poll_deregister(struct ifnet *ifp);
/* The following should be temporary, till all drivers use the driver API */
typedef int poll_handler_drv_t(if_t ifh, enum poll_cmd cmd, int count);
int ether_poll_register_drv(poll_handler_drv_t *h, if_t ifh);
int ether_poll_deregister_drv(if_t ifh);
#endif /* DEVICE_POLLING */
#endif /* _KERNEL */