歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核DCB子系統

Linux內核DCB子系統

日期:2017/2/28 14:21:51   编辑:Linux內核

q1. 網絡設備是怎麼利用linux內核的DCB子系統,來達到融合網絡流量的各種各樣的QoS需求的?

q2.融合網卡或者存儲流量是否也可以使用到DCB子系統,他們是怎樣工作的?

本文將對上面這兩個問題進行解答;本文首先大體介紹了DCB機制和它的使用環境;然後介紹一個使用DCB的應用程序lldpad的例子;再然後介紹一個DCB子系統中重要的數據結構;最後介紹DCB內核模塊和驅動的具體實現。

Overview
首先,DCB是什麼呢?

整個DCB過程,是要把各種各樣的流量,可能是FCoE流量,可能是一般的TCP流量,還可能是其他的視頻流量等等,他們對於QoS的要求各不相同,有的要求不丟包,有的要求帶寬保證等等,但是他們也許都需要從這一個網絡接口發送到他們各自的目的地中去。由於意識到這個需求,在linux內核中,2.4?或者更早的版本中加入了TC(流量分類)模塊,用來對不同的流量類型進行不同的處理。之前說到驅動模塊是網絡接口的agent,網絡設備很可能也和內核一樣,對於不同的流量類型也有不同的QoS處理,不過如果網絡設備沒有這個多隊列和相關的處理,那麼也不影響QoS的處理,只不過不能提速,在網卡這一塊可能會成為瓶頸。使用了多個隊列的網卡,在內部可能有一個處理器來進行這些復雜的多隊列處理,然後驅動的DCB代碼部分很可能需要完成TC映射的功能。但是如果說網卡沒有多隊列,內核的QoS任然可以發揮它的作用(當然要消耗CPU是必須的啦)。

驅動是網卡的agent,就相當於網卡是個聾啞人,主機是不能和網卡說話的,通過驅動,網卡和主機能夠進行交互。主機說要從這接口發送包出去,於是調用驅動的發送函數,把包發送出去。如果網卡物理層收到包,則通過驅動的中斷函數來處理這些包(為了提高效率,現在也有可能使用到NAPI)。本來發送和接受都好說,當然是用最快的速度處理發包和收包就好。但是當流量類型漸漸復雜的時候,就要針對不同的流量進行不同的處理。比如說我們的主機上有飛機,坦克,自行車,當都必須經過這個接口出去時,我們按照飛機的優先級最高,自行車優先級最低,只有當飛機和坦克都過去了之後,自行車才能夠通行(我們的主機內存很多,給飛機一個隊,坦克一個隊,自行車一個隊)。如果這個接口很寬,我們還可以把這個接口設置幾個隊列,要出去的直接在接口上拍隊(當然也是先通過內存的平滑緩沖)。道路上認為一次只能通過一個東西。

用戶的流量特征我們可以通過lldpad設置,設置了這個值,而且設置的而這些信息是要和對端交換機進行交互的,於是通過那個接口發出去,設置的這個值是要和內核交互的,當內核協議棧收到這些信息的時候,需要配置相應的隊列和算法(同時還要獲取硬件信息,如果有必要還要把tc隊列映射到網卡隊列),和驅動交互的另外一個原因很可能是因為dcb-lldp需要使用驅動本身要傳遞的網卡DCB信息和要求。

關於存儲流量,比如FCoE流量,是不會經過內核的Qdisc的,他經過的是FcoE模塊,在FCoE模塊中也是會使用DCB模塊的額,從而達到和Qdisc一樣的目的?

Q.FCoE模塊為何也要用到dcb模塊中的函數?

A.FCoE模塊作為FCoE協議的處理模塊,需要配置相應的FCoE類型和DCB類型的包的DCB參數。(這是不是假如它不再經過802.1p網絡層次?)

Q.發揮了什麼作用?

A.獲取了FCoE和FIP類型的的優先級

Up(FCoE優先級);Fup(FIP優先級)

static void fcoe_dcb_create(struct fcoe_interface *fcoe)

{

#ifdef CONFIG_DCB

int dcbx;

u8 fup, up;

struct net_device *netdev = fcoe->realdev;

struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);

struct dcb_app app = {

.priority = 0,

.protocol = ETH_P_FCOE

};


...


app.selector = DCB_APP_IDTYPE_ETHTYPE;

up = dcb_getapp(netdev, &app);

app.protocol = ETH_P_FIP;

fup = dcb_getapp(netdev, &app);

}


port->priority = ffs(up) ? ffs(up) - 1 : 0;

fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;


除了DCB初始化函數,fcoe_dcb_create;內核提供了DCB,NET和CPU通知鏈注冊/注銷和工作隊列的接口函數。

LLDPAD

這個應用程序是用來配置Intel網絡設備的DCB特性的。

Listed below are the applicablestandards:

Enhanced Transmission Selection: IEEE 802.1Qaz

Lossless Traffic Class

Priority Flow Control: IEEE 802.1Qbb

Congestion Notification: IEEE 802.1Qau

DCB Capability exchange protocol (DCBX): IEEE 802.1Qaz

那麼怎麼使用lldpad配置DCB特性呢?比如使用用戶接口lldpad。


Dcbtool
Dcbtool可以用來查詢和設置DCB以太網接口的DCB特性。通用的命令有gc, sc等。(對應的DCB模塊的get和set函數),主要可以參考https://github.com/jrfastab/lldpad比如:<gc|go> dcbx:gets the configured or operational version of the DCB capabilities exchange protocol. 可以設置本地interface的配置特性。sc <ifname> <feature> <args> sets the configuration of feature on interface ifname.這些特性feature包括: dcb DCB state of the port pg priority groupspgid:xxxxxxxxPriority group ID for the 8 priorities. From left to right(priorities 0-7), x is the corresponding priority group IDvalue, which can be 0-7 for priority groups with bandwidth allo-cations or f (priority group ID 15) for the unrestricted prior-ity group. pfc priority flow control(特殊的參數有pfcup: xxxxxxxx)x是0或1,1指的是這個相應的優先級(總共有8個優先級0-7嘛)使用傳輸的pause幀機制,0就表示不使用。 app:<subtype> 特殊的參數是appcfg:xx xx是一個16進制的值代表一個8bit的bitmap,某一位為1代表著這個subtype使用這個優先級。 application specific data subtype can be: --------------- 0|fcoe Fiber Channel over Ethernet (FCoE) 下面是dcbtool的使用例子:達到的目的:使得PFC pause發生作用的傳輸優先級是3,並且將FCoE流量分配到這個第三優先級上。 dcbtool sc eth2 pfc pfcup:00010000 dcbtool sc eth2 app:0 appcfg:08(app:0是表示FcoE流量)另外帶寬分配的部分就是pg:比如 dcbtool sc eth2 pg pgid:0000111f pgpct:25,75,0,0,0,0,0,0
使用Netlink和內核DCB子系統交互

在net\netlink\af_netlink.c中:

static int __init netlink_proto_init(void)

sock_register(&netlink_family_ops);

}

static const struct net_proto_familynetlink_family_ops = {

.family = PF_NETLINK,

.create = netlink_create,

.owner = THIS_MODULE, /* for consistency 8) */

};


在netlink_create中調用

static int __netlink_create(struct net *net, struct socket *sock,

struct mutex *cb_mutex, int protocol)

{

struct sock *sk;

struct netlink_sock *nlk;

sock->ops = &netlink_ops;

sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);

}

static const struct proto_opsnetlink_ops= {

.family = PF_NETLINK,

.owner = THIS_MODULE,

.release = netlink_release,

.bind = netlink_bind,

.connect = netlink_connect,

.socketpair = sock_no_socketpair,

.accept = sock_no_accept,

.getname = netlink_getname,

.poll = datagram_poll,

.ioctl = sock_no_ioctl,

.listen = sock_no_listen,

.shutdown = sock_no_shutdown,

.setsockopt = netlink_setsockopt,

.getsockopt = netlink_getsockopt,

.sendmsg = netlink_sendmsg,//這可能就是和那些系統調用對應的函數

.recvmsg = netlink_recvmsg,

.mmap = sock_no_mmap,

.sendpage = sock_no_sendpage,

};


數據結構dcbnl_ops
這裡的dcbnl_ops應該算是routing子系統中的一個需要用到的結構。為了進一步理解,還是把這個過程弄清楚吧。 到現在lldpad怎麼操作,基本上梳理清楚,至於lldpad怎麼和內核程序進行交互。應該要看一看struct dcbnl_rtnl_ops出現在內核的哪些部分。include/net/dcbnl.h定義這個結構
定義:include/net/dcbnl.h, line 46
· *· 43 * Ops struct for the netlink callbacks. Used by DCB-enabled drivers through* the netdevice struct.· 45 */· 46 struct dcbnl_rtnl_ops {· 47 /* IEEE 802.1Qaz std */· 56 int (*ieee_delapp) (struct net_device *, struct dcb_app *);· 59· 60 /* CEE std */· 61 u8 (*getstate)(struct net_device *);· u16 *);· 96 int (*peer_getapptable)(struct net_device *, struct dcb_app *);· 97 · 98 /* CEE peer */· 99 int (*cee_peer_getpg) (struct net_device *, struct cee_pg *);· 100 int (*cee_peer_getpfc) (struct net_device *, struct cee_pfc *);· 101 };· 102· 103 #endif /* __NET_DCBNL_H__ */· 104
referenced in by驅動(DCB enabled)和DCB模塊
然後referenced in很多的地方:被很多的驅動引用,比如broadcom的bnx2x(這個編譯內核的時候沒有必要選上吧)。mellanox/mlx4;/qlogic/qlcnic;intel/ixgbe;等這些基本上都是定義並且實現了這個結構;並且把這個結構作為netdev的子結構傳遞給netdev結構體。 然後在net/dcb/dcbnl.c(DCB模塊)中有這些函數static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,int app_nested_type, int app_info_type, int app_entry_type)然後這個裡面使用了一個變量指針ops,將這個netdev的dcbnl_ops傳遞給它。const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops; static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)1035 {1036 struct nlattr *ieee, *app;1037 struct dcb_app_type *itr;1038 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;1039 int dcbx; static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,1142 int dir)1143 {1144 u8pgid, up_map, prio, tc_pct;1145 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops; 還有dcbnl_notifydcbnl_cee_fill(dcbnl_cee_get/* Handle CEE DCBX GET commands. */DCB模塊
Net/dcb目錄

主要是和應用程序交互,解析應用程序的包,執行相關的功能,然後去調用變量的callback函數進行get或者set操作,再將結果反饋給應用程序lldpad。

DCB子系統注冊rtnetlink
感覺看一下這個3.2的代碼完全沒有什麼問題的呀。差不多就是那個論文中提到的那樣子

Note:翻譯自代碼,可是翻譯起來真的好帶感呢。

__rtnl_register函數:注冊一個rtnetlink消息類型(是提供給模塊自己注冊的哦)

參數

@protocol:協議家族或者PF_UNSPEC

@msgtype:rtnetlink的消息類型

@doit:每次請求消息調用的函數指針

@dumpit:每次dump請求NLM_F_DUM調用的函數指針

@calit:計算dump消息大小的指針函數

static struct rtnl_link*rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];

int __rtnl_register(int protocol, intmsgtype,

rtnl_doit_func doit, rtnl_dumpit_funcdumpit,

rtnl_calcit_func calcit){

struct rtnl_link *tab;

intmsgindex;

BUG_ON(protocol< 0 || protocol > RTNL_FAMILY_MAX);

msgindex= rtm_msgindex(msgtype);

tab= rtnl_msg_handlers[protocol];

if(tab == NULL) {

tab= kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);

if(tab == NULL)

return-ENOBUFS;

rtnl_msg_handlers[protocol]= tab;

}

if(doit)

tab[msgindex].doit= doit;

if(dumpit)

tab[msgindex].dumpit= dumpit;

if(calcit)

tab[msgindex].calcit= calcit;

return0;

}

EXPORT_SYMBOL_GPL(__rtnl_register);

void rtnl_register(int protocol, intmsgtype,

rtnl_doit_func doit, rtnl_dumpit_funcdumpit,

rtnl_calcit_func calcit)

{

if(__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)

panic("Unableto register rtnetlink message handler, "

"protocol = %d, message type =%d\n",

protocol, msgtype);

}

EXPORT_SYMBOL_GPL(rtnl_register);

當消息到達doit之後:

Dcb/dcbnl.c中的

static int __init dcbnl_init(void)

{

INIT_LIST_HEAD(&dcb_app_list);

rtnl_register(PF_UNSPEC,RTM_GETDCB, dcb_doit, NULL, NULL);

rtnl_register(PF_UNSPEC,RTM_SETDCB, dcb_doit, NULL, NULL);

return0;

}

然後這個dcb_doit做了很多的事情,比如說解析skb和其他頭部,然後進行相關的操作。

static int dcb_doit(struct sk_buff *skb,structnlmsghdr *nlh, void *arg)

{

structnet *net = sock_net(skb->sk);

structnet_device *netdev;

structdcbmsg *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);

structnlattr *tb[DCB_ATTR_MAX + 1];

u32pid = skb ? NETLINK_CB(skb).pid : 0;

intret = -EINVAL;

if(!net_eq(net, &init_net))

return-EINVAL;

ret= nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,

dcbnl_rtnl_policy);

if(ret < 0)

returnret;

if(!tb[DCB_ATTR_IFNAME])

return-EINVAL;

netdev= dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));

if(!netdev)

return-EINVAL;

if(!netdev->dcbnl_ops)

gotoerrout;

switch(dcb->cmd) {

caseDCB_CMD_GSTATE:

ret= dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,

nlh->nlmsg_flags);

/**

* enum dcbnl_attrs - DCB top-level netlink attributes

*

* @DCB_ATTR_UNDEFINED: unspecified attribute to catch errors

* @DCB_ATTR_IFNAME: interface name of the underlying device (NLA_STRING)

* @DCB_ATTR_STATE: enable state of DCB in the device (NLA_U8)

* @DCB_ATTR_PFC_STATE: enable state of PFC in the device (NLA_U8)

* @DCB_ATTR_PFC_CFG: priority flow control configuration (NLA_NESTED)

* @DCB_ATTR_NUM_TC: number of traffic classes supported in the device (NLA_U8)

* @DCB_ATTR_PG_CFG: priority group configuration (NLA_NESTED)

* @DCB_ATTR_SET_ALL: bool to commit changes to hardware or not (NLA_U8)

* @DCB_ATTR_PERM_HWADDR: MAC address of the physical device (NLA_NESTED)

* @DCB_ATTR_CAP: DCB capabilities of the device (NLA_NESTED)

* @DCB_ATTR_NUMTCS: number of traffic classes supported (NLA_NESTED)

* @DCB_ATTR_BCN: backward congestion notification configuration (NLA_NESTED)

* @DCB_ATTR_IEEE: IEEE 802.1Qaz supported attributes (NLA_NESTED)

* @DCB_ATTR_DCBX: DCBX engine configuration in the device (NLA_U8)

* @DCB_ATTR_FEATCFG: DCBX features flags (NLA_NESTED)

* @DCB_ATTR_CEE: CEE std supported attributes (NLA_NESTED)

*/

struct nlattr {

__u16 nla_len;

__u16 nla_type;

};

struct nlmsghdr {

__u32 nlmsg_len; /* Length of message including header */

__u16 nlmsg_type; /* Message content */

__u16 nlmsg_flags; /* Additional flags */

__u32 nlmsg_seq; /* Sequence number */

__u32 nlmsg_pid; /* Sending process port ID */

};

自定義的一些DCB屬性,比如:

/* DCB netlink attributes policy */

static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {

[DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},

[DCB_ATTR_STATE] = {.type = NLA_U8},

[DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},

[DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},

[DCB_ATTR_SET_ALL] = {.type = NLA_U8},

[DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},

[DCB_ATTR_CAP] = {.type = NLA_NESTED},

[DCB_ATTR_PFC_STATE] = {.type = NLA_U8},

[DCB_ATTR_BCN] = {.type = NLA_NESTED},

[DCB_ATTR_APP] = {.type = NLA_NESTED},

[DCB_ATTR_IEEE] = {.type = NLA_NESTED},

[DCB_ATTR_DCBX] = {.type = NLA_U8},

[DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},

};

關於rtmsg的解析,是這樣實現的

/**

* nlmsg_parse - parse attributes of a netlink message

* @nlh: netlink message header

* @hdrlen: length of family specific header

* @tb: destination array with maxtype+1 elements

* @maxtype: maximum attribute type to be expected

* @policy: validation policy

*

* See nla_parse()

*/

static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,

struct nlattr *tb[], int maxtype,

const struct nla_policy *policy)

{

if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))

return -EINVAL;

return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),

nlmsg_attrlen(nlh, hdrlen), policy);

}

Parses a stream of attributes and stores a pointer to each attribute in

* the tb array accessible via the attribute type. Attributes with a type

* exceeding maxtype will be silently ignored for backwards compatibility

* reasons. policy may be set to NULL if no validation is required.

*

* Returns 0 on success or a negative error code.

*/

int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,

int len, const struct nla_policy *policy)

{

const struct nlattr *nla;

int rem, err;

memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));

nla_for_each_attr(nla, head, len, rem) {

u16 type = nla_type(nla);

if (type > 0 && type <= maxtype) {

if (policy) {

err = validate_nla(nla, maxtype, policy);

if (err < 0)

goto errout;

}

tb[type] = (struct nlattr *)nla;

}

}


關於dcbmsg結構是在dcbnl.h中定義的

struct dcbmsg {

__u8 dcb_family;

__u8 cmd;

__u16 dcb_pad;

};

命令包括:

/**

* enum dcbnl_commands - supported DCB commands

*

* @DCB_CMD_UNDEFINED: unspecified command to catch errors

* @DCB_CMD_GSTATE: request the state of DCB in the device

* @DCB_CMD_SSTATE: set the state of DCB in the device

* @DCB_CMD_PGTX_GCFG: request the priority group configuration for Tx

* @DCB_CMD_PGTX_SCFG: set the priority group configuration for Tx

* @DCB_CMD_PGRX_GCFG: request the priority group configuration for Rx

* @DCB_CMD_PGRX_SCFG: set the priority group configuration for Rx

* @DCB_CMD_PFC_GCFG: request the priority flow control configuration

* @DCB_CMD_PFC_SCFG: set the priority flow control configuration

* @DCB_CMD_SET_ALL: apply all changes to the underlying device

* @DCB_CMD_GPERM_HWADDR: get the permanent MAC address of the underlying

* device. Only useful when using bonding.

* @DCB_CMD_GCAP: request the DCB capabilities of the device

* @DCB_CMD_GNUMTCS: get the number of traffic classes currently supported

* @DCB_CMD_SNUMTCS: set the number of traffic classes

* @DCB_CMD_GBCN: set backward congestion notification configuration

* @DCB_CMD_SBCN: get backward congestion notification configration.

* @DCB_CMD_GAPP: get application protocol configuration

* @DCB_CMD_SAPP: set application protocol configuration

* @DCB_CMD_IEEE_SET: set IEEE 802.1Qaz configuration

* @DCB_CMD_IEEE_GET: get IEEE 802.1Qaz configuration

* @DCB_CMD_GDCBX: get DCBX engine configuration

* @DCB_CMD_SDCBX: set DCBX engine configuration

* @DCB_CMD_GFEATCFG: get DCBX features flags

* @DCB_CMD_SFEATCFG: set DCBX features negotiation flags

* @DCB_CMD_CEE_GET: get CEE aggregated configuration

* @DCB_CMD_IEEE_DEL: delete IEEE 802.1Qaz configuration

*/

一個標准的netlink replay call的例子如下,比如get的時候就是基本上調用了它。

static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,

u32 seq, u16 flags)

{

struct sk_buff *dcbnl_skb;

struct dcbmsg *dcb;

struct nlmsghdr *nlh;

int ret = -EINVAL

dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);

if (!dcbnl_skb)

return ret;

nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);

dcb = NLMSG_DATA(nlh);

dcb->dcb_family = AF_UNSPEC;

dcb->cmd = cmd;

dcb->dcb_pad = 0;

ret = nla_put_u8(dcbnl_skb, attr, value);

if (ret)

goto err;

/* end the message, assign the nlmsg_len. */

nlmsg_end(dcbnl_skb, nlh);

ret = rtnl_unicast(dcbnl_skb, &init_net, pid);

驅動定義dcbnl_ops
驅動:比如82599的驅動Ixgbe,或者netfpga的驅動都可以根據自己的需要來定義這個結構dcbnl_ops中的函數指針。Dcbnl_ops就是實現了很多的函數體,這些函數體都要驅動的dcb.c(差不多類似的名字)中實現。這個在netfpga的時候,主要是要和lldpad進行交互,82555可能設置了硬件的DCB配置,可是netfpga的硬件並沒有這個功能。不過DCB模塊也會和FCoE模塊進行交互,好像調用的就是DCB模塊中的函數(⊙▽⊙不知道這些函數還有沒有調用那個變量的函數,如果調用了,才有意思呢。!關於麗麗總是在問的app 優先級。。還是要結合lldpad一起看會比較好吧。其實我倒是不在乎這個)。

setall應該是重置

get/set pfc tcnum app等。

82599的硬件初始化
這些硬件的配置利用的結構是adapter->dcb_cfg(與硬件相關的結構體),這個是網卡的默認DCB配置。

參考文獻
lldpad的readme
lldpad源碼
ixgbe源碼
內核相關資料

Copyright © Linux教程網 All Rights Reserved