Chinaunix首页 | 论坛 | 博客
  • 博客访问: 801795
  • 博文数量: 106
  • 博客积分: 1250
  • 博客等级: 少尉
  • 技术积分: 1349
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-09 09:38
文章分类

全部博文(106)

文章存档

2014年(1)

2013年(13)

2012年(92)

分类:

2012-09-18 13:02:10

该函数为实际上只有struct ath_node *an参数是需要传递的,

ieee80211_ioctl_giwrate输入的参数为net_device *dev,因此,需要将该输入参数转换得到struct ath_node *an参数:


因此,我们需要关注三个结构体:

struct net_device *dev结构体包含了ieee80211vap *vap结构体:
  1. ieee80211vap *vap = dev->priv;
  2. an = ATH_NODE(ni);

  3. struct ath_softc {

  4.       struct ieee80211com sc_ic;        /* NB: must be first */

  5.       struct net_device *sc_dev;

  6.       struct semaphore sc_lock;         /* dev-level lock */

  7. struct ath_node {

  8.       struct ieee80211_node an_node;         /* base class */

  9.       u_int16_t an_decomp_index;        /* decompression mask index */

  10.       u_int32_t an_avgrssi;             /* average rssi over all rx frames */

  11.       u_int8_t  an_prevdatarix;         /* rate ix of last data frame */

  12.       u_int16_t an_minffrate;                /* mimum rate in kbps for ff to aggragate */

  13.       HAL_NODE_STATS an_halstats;       /* rssi statistics used by hal */

  14.       struct ath_buf *an_tx_ffbuf[WME_NUM_AC]; /* ff staging area */

  15.       ath_bufhead an_uapsd_q;                /* U-APSD delivery queue */

  16.       int an_uapsd_qdepth;              /* U-APSD delivery queue depth */

  17.       ath_bufhead an_uapsd_overflowq;   /* U-APSD overflow queue (for > MaxSp frames) */

  18.       int an_uapsd_overflowqdepth;           /* U-APSD overflow queue depth */

  19.       spinlock_t an_uapsd_lock;         /* U-APSD deleivery queue lock */

  20.       /* variable-length rate control state follows */

  21. };



  22. struct ieee80211_node {

  23.       struct ieee80211vap *ni_vap;

  24.       struct ieee80211com *ni_ic;

  25.       struct ieee80211_node_table *ni_table;

  26. …………..



  27. struct ieee80211vap {

  28.       struct net_device *iv_dev;        /* associated device */

  29.       struct net_device_stats     iv_devstats;     /* interface statistics */

  30.       struct ifmedia iv_media;               /* interface media config */

  31. #ifdef CONFIG_NET_WIRELESS

  32.       struct iw_statistics iv_iwstats;       /* wireless statistics block */

  33. #endif

  34. …………

  35. struct ieee80211com *iv_ic;           /* back ptr to common state */

  36. ……………
复制代码
下面三个结构体的包含关系为:

ath_node-> ieee80211_node-> ieee80211vap

并且由于首地址是一样的,因此,只要获得任意一个结构体,可以使用指针强制转换为另外两个结构体,例如已知ieee80211_node结构体,可以获得ath_node,在if_ath.c 的ath_tx_start函数中,使用
  1. an = ATH_NODE(ni)命令获得,

  2. #define    ATH_NODE(_n)                ((struct ath_node *)(_n))


  3. struct ath_softc *sc = dev->priv;

  4. struct ieee80211com *an = dev->priv;

  5. ath_rate_findrate(sc, an, shortPreamble, skb->len, &rix, &try0, &txrate);
复制代码
调试中发现,始终不能正确的传递参数,经过调试,发现ieee80211.c和if_ath.c代码的struct net_device变量是不一样的,两者的参数名字分别为ath0和wifi0,也就是两个网络设备的名字,因此,没有办法传递参数。



下面还是分析iwconfig中的参数是如何传递的,以ieee80211_ioctl_siwpower函数为例,设置发送功率。
  1. ieee80211_ioctl_siwpower(struct net_device *dev, struct iw_request_info *info,

  2.       struct iw_param *wrq, char *extra)

  3. {

  4.       struct ieee80211vap *vap = dev->priv;

  5.       struct ieee80211com *ic = vap->iv_ic;

  6. ……………..

  7. return IS_UP(ic->ic_dev) ? ic->ic_reset(ic->ic_dev) : 0;  如果设备已经UP,那么以新的参数对设备设置。

复制代码
这里面调用了一个函数指针,ic->ic_reset,经过检查,只有 if_ath.c文件调用了,
  1. if_ath.c(692):      ic->ic_reset = ath_reset;

  2. ath_attach(u_int16_t devid, struct net_device *dev)

  3. {

  4.       struct ath_softc *sc = dev->priv;

  5.       struct ieee80211com *ic = &sc->sc_ic;

  6.       struct ath_hal *ah;

  7. ………….

  8. ic->ic_reset = ath_reset;



  9. ath_reset(struct net_device *dev)

  10. {

  11.       struct ath_softc *sc = dev->priv;

  12.       struct ieee80211com *ic = &sc->sc_ic;

  13.       struct ath_hal *ah = sc->sc_ah;

  14.       struct ieee80211_channel *c;

  15.       HAL_STATUS status;

复制代码
iwconfig命令会最终调用ieee80211_wireless.c中的各种参数设置和控制命令,查看代码,发现ieee80211_wireless中本身定义了很多的参数变量,如果需要设置,再调用底层的函数将参数写入网卡芯片,如果读参数,就直接读自己已经保存好的参数,而并不是读真正的物理层的参数!



ieee80211_ioctl_siwpower代码最终会调用ath_reset,这两个函数的输入参数都是struct net_device *dev,他是如何进行参数转换的呢?将ath0变为wifi0?

Iwconfig中,首先由输入参数ath0(struct net_device *dev)获得vap

      struct ieee80211vap *vap = dev->priv;

该结构体定义为:
  1. struct ieee80211vap {

  2.       struct net_device *iv_dev;        /* associated device */

  3. struct ieee80211com *iv_ic;      /* back ptr to common state */

  4. ……………

复制代码
然后由vap获得ic,
  1.       struct ieee80211com *ic = vap->iv_ic;



复制代码
而该结构体定义为:
  1. struct ieee80211com {

  2.      struct net_device *ic_dev;        /* associated device */

复制代码
又关联了一个net_device设备,这个设备正是wifi0,因此,调用ic_reset的时候,并不是直接将

ieee80211_ioctl_siwpower的输出参数dev放进去,而是转了两个弯,

ic->ic_reset(ic->ic_dev)。



再次回到获取速率的函数,调用下面的函数获得速率,
  1. ath_rate_findrate(sc, an, shortPreamble, skb->len, &rix, &try0, &txrate);

  2. 这里,sc以前直接定义为:ieee80211_ioctl_siwpower输入参数的dev,

  3. struct ath_softc *sc = dev->priv;

  4. 需要将其定义为wifi0,因此,需要定义为:

  5. struct ieee80211vap *vap = dev->priv; 由ath0 device获得vap

  6. struct ieee80211com *ic = vap->iv_ic;

  7. struct net_device *wifi= ic->ic_dev;  再次获得wifi device

  8. struct ath_softc *sc = wifi ->priv; 获得sc
复制代码
如果调用findrate函数,第二个参数ath_node需要传递过来,发现这比较困难,因此,暂时不使用这个方法,而是自己在ieee80211com结构体中定义一个变量,利用该变量传递参数。
  1. ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,

  2.       int shortPreamble, size_t frameLen,

  3.       u_int8_t *rix, int *try0, u_int8_t *txrate)

复制代码
9、madwifi程序阅读(二)

前面的阅读已经发现,madwifi驱动里面有两套net_device设备,一套是if_ath­_xx.c程序使用的,名字为“wifi0”,另一套是ieee802.11_xx程序使用的,为ath0。
  1. ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)

  2. \if_ath_pci.c(179):   dev = alloc_netdev(sizeof(struct ath_pci_softc), "wifi%d", ether_setup);



  3. int ath_attach(u_int16_t devid, struct net_device *dev)

  4. \if_ath.c(897): error = ieee80211_create_vap(ic, "ath%d", dev,

  5.                        autocreatemode, IEEE80211_CLONE_BSSID);
复制代码
相应的,两个设备都定义了自己的发送函数

这是ath0的发送函数,
  1. int ieee80211_vap_setup(struct ieee80211com *ic, struct net_device *dev,

  2.       const char *name, int unit, int opmode, int flags)

  3. \ieee80211.c(395):    dev->hard_start_xmit = ieee80211_hardstart;

  4. 这是wifi的发送函数,

  5. int ath_attach(u_int16_t devid, struct net_device *dev)

  6. if_ath.c(670):   dev->hard_start_xmit = ath_hardstart;

复制代码
ieee80211_hardstart函数调用ieee80211_parent_queue_xmit,ieee80211_parent_queue_xmit 继续调用(void) dev_queue_xmit(skb),dev_queue_xmit函数是在linux内核中定义的

\net\core\dev.c(994):int dev_queue_xmit(struct sk_buff *skb),该函数会调用 dev->hard_start_xmit,这个函数估计就是ath_hardstart。



ath_hardstart调用ath_tx_start函数,ath_tx_start继续调用ath_hal_filltxdesc等与硬件相关的函数,完成最终的发送任务。

这样就出来一个问题了,由谁来调用ieee80211_hardstart?

经过查找,两个hard_start_xmit函数都没有在madfiwi程序中调用,看来都是在 linux内核中调用的。



也就说,madwifi同时注册了两个网络设备,一个是ath0,一个是wifi0,应用程序发送数据的流程是这样的:应用程序->内核->ath0设备->内核->wifi0设备。

要搞清楚为什么会这样,就必须了解madwifi中的VAP(虚拟AP的概念),可以在一个实际的无线设备上创建多个逻辑设备,要使用设备,必须使用下面的命令创建ath设备

wlanconfig athX create wlandev wifiX wlanmode

例如:

wlanconfig ath0 create wlandev wifi0 wlanmode ap

wlanconfig ath1 create wlandev wifi0 wlanmode ap

wlanconfig ath2 create wlandev wifi0 wlanmode ap

iwconfig ath0 essid "lmn"

iwconfig ath1 essid "XYZ"

iwconfig ath2 essid "abc"

因此,可以认为ath是逻辑设备,wifi是物理设备。

(上面部分参考了madwifi.pdf文档,一个ppt,上面对madwifi进行了粗略的介绍,包括存在的问题)

The OpenHAL ported to MADWiFi at



因此,我们在发送数据的时候,已经指明了使用ath0虚拟网卡,自然就会调用ieee80211_hardstart,问题就变成了两次调用内核操作是如何实现的?



还是先看看几个结构体是如何关联的,
  1. ath_attach(u_int16_t devid, struct net_device *dev)

  2. {

  3.       struct ath_softc *sc = dev->priv;  由wifi设备获得sc

  4.       struct ieee80211com *ic = &sc->sc_ic; 由wifi设备获得com设备ic,

  5. ……….

  6.       ic->ic_dev = dev;          ic的设备指向wifi

  7.       ic->ic_mgtstart = ath_mgtstart;

  8.       ic->ic_init = ath_init;

  9.       ic->ic_reset = ath_reset;

  10.       ic->ic_newassoc = ath_newassoc;

  11.       ic->ic_updateslot = ath_updateslot;

  12. ………

  13.       dev->open = ath_init;

  14.       dev->stop = ath_stop;

  15.       dev->hard_start_xmit = ath_hardstart;

  16.       dev->tx_timeout = ath_tx_timeout;
复制代码
ieee80211_ifattach(ic); 加载ieee802设备



           创建VAP,得到ath0设备。
  1. error = ieee80211_create_vap(ic, "ath%d", dev,

  2.                       autocreatemode, IEEE80211_CLONE_BSSID);
复制代码
阅读(2955) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~