1 Star 0 Fork 0

sdlhk/lldpad

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
lldp_dcbx_nl.c 18.95 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Williams 提交于 2014-02-20 00:08 . trivial: upper-case some enum values
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2012 Intel Corporation.
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Contact Information:
open-lldp Mailing List <lldp-devel@open-lldp.org>
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include "linux/netlink.h"
#include "linux/rtnetlink.h"
#include "linux/dcbnl.h"
#include "lldp.h"
#include "lldp_util.h"
#include "dcb_types.h"
#include "dcb_protocol.h"
#include "dcb_driver_interface.h"
#include "lldp_dcbx_nl.h"
#include "messages.h"
#include "lldp_rtnl.h"
#include "lldp/ports.h"
static int nl_sd = 0;
static int rtseq = 0;
static int next_rtseq(void)
{
return ++rtseq;
}
static int init_socket(void)
{
int sd;
int rcv_size = MAX_MSG_SIZE;
struct sockaddr_nl snl;
int reuse = 1;
sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sd < 0)
return sd;
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, &rcv_size, sizeof(int)) < 0) {
close(sd);
return -EIO;
}
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0) {
close(sd);
return -EIO;
}
memset((void *)&snl, 0, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = 0;
if (connect(sd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl)) < 0) {
close(sd);
return -EIO;
}
return sd;
}
static struct nlmsghdr *start_msg(__u16 msg_type, __u8 arg)
{
struct nlmsghdr *nlh;
struct dcbmsg *d;
struct ifinfomsg *ifi;
/* nlh needs to be free'd by send_msg() */
nlh = (struct nlmsghdr *)malloc(MAX_MSG_SIZE);
if (NULL == nlh)
return NULL;
memset((void *)nlh, 0, MAX_MSG_SIZE);
nlh->nlmsg_type = msg_type;
nlh->nlmsg_flags = NLM_F_REQUEST;
nlh->nlmsg_seq = next_rtseq();
nlh->nlmsg_pid = getpid();
switch (msg_type) {
case RTM_GETDCB:
case RTM_SETDCB:
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct dcbmsg));
d = NLMSG_DATA(nlh);
d->cmd = arg;
d->dcb_family = AF_UNSPEC;
d->dcb_pad = 0;
break;
case RTM_GETLINK:
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
ifi = NLMSG_DATA(nlh);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_index = arg;
ifi->ifi_change = 0xffffffff;
break;
default:
free(nlh);
nlh = NULL;
break;
}
return nlh;
}
static struct rtattr *add_rta(struct nlmsghdr *nlh, __u16 rta_type,
void *attr, __u16 rta_len)
{
struct rtattr *rta;
rta = (struct rtattr *)((char *)nlh + nlh->nlmsg_len);
rta->rta_type = rta_type;
rta->rta_len = rta_len + NLA_HDRLEN;
if (attr)
memcpy(NLA_DATA(rta), attr, rta_len);
nlh->nlmsg_len += NLMSG_ALIGN(rta->rta_len);
return rta;
}
/* free's nlh which was allocated by start_msg */
static int send_msg(struct nlmsghdr *nlh)
{
struct sockaddr_nl nladdr;
void *buf = (void *)nlh;
int r, len = nlh->nlmsg_len;
if (nlh == NULL)
return 1;
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
do {
r = sendto(nl_sd, buf, len, 0, (struct sockaddr *)&nladdr,
sizeof(nladdr));
LLDPAD_DBG("send_msg: sendto = %d\n", r);
} while (r < 0 && errno == EINTR);
free(nlh);
if (r < 0)
return 1;
else
return 0;
}
static struct nlmsghdr *get_msg(unsigned int seq)
{
struct nlmsghdr *nlh;
unsigned len;
int res;
int found = 0;
/* nlh needs to be free'd by caller */
nlh = (struct nlmsghdr *)malloc(MAX_MSG_SIZE);
if (NULL == nlh)
return NULL;
memset(nlh, 0, MAX_MSG_SIZE);
while (!found) {
res = recv(nl_sd, (void *)nlh, MAX_MSG_SIZE, MSG_DONTWAIT);
if (res < 0) {
if (errno == EINTR)
continue;
perror("get_msg: recv error");
free(nlh);
nlh = NULL;
break;
}
len = res;
if (!(NLMSG_OK(nlh, len))) {
LLDPAD_DBG("get_msg: NLMSG_OK is false\n");
free(nlh);
nlh = NULL;
break;
}
if ((nlh->nlmsg_type == RTM_GETDCB ||
nlh->nlmsg_type == RTM_SETDCB) &&
nlh->nlmsg_seq == seq) {
break;
} else if (nlh->nlmsg_type == NLMSG_ERROR) {
if (nlh->nlmsg_seq != seq) {
continue;
} else {
free(nlh);
nlh = NULL;
break;
}
break;
}
}
return nlh;
}
static int recv_msg(int cmd, int attr, unsigned int seq)
{
struct nlmsghdr *nlh;
struct dcbmsg *d;
struct rtattr *rta;
int rval;
nlh = get_msg(seq);
if (NULL == nlh)
return -EIO;
d = (struct dcbmsg *)NLMSG_DATA(nlh);
rta = (struct rtattr *)(((char *)d) +
NLMSG_ALIGN(sizeof(struct dcbmsg)));
if ((d->cmd != cmd) || (rta->rta_type != attr)) {
LLDPAD_DBG("Bad netlink message attribute.");
return -EIO;
}
rval = *(__u8 *)NLA_DATA(rta);
free(nlh);
return rval;
}
static int get_state(char *ifname, __u8 *state)
{
struct nlmsghdr *nlh;
struct dcbmsg *d;
struct rtattr *rta;
int rval = 0;
unsigned int seq;
nlh = start_msg(RTM_GETDCB, DCB_CMD_GSTATE);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
if (send_msg(nlh))
return -EIO;
nlh = get_msg(seq);
if (NULL == nlh)
return -EIO;
d = (struct dcbmsg *)NLMSG_DATA(nlh);
rta = (struct rtattr *)(((char *)d) +
NLMSG_ALIGN(sizeof(struct dcbmsg)));
if (d->cmd != DCB_CMD_GSTATE) {
return -EIO;
}
if (rta->rta_type != DCB_ATTR_STATE) {
rval = -EIO;
} else {
*state = *(__u8 *)NLA_DATA(rta);
}
free(nlh);
return rval;
}
static int set_state(char *ifname, __u8 state)
{
struct nlmsghdr *nlh;
int seq;
nlh = start_msg(RTM_SETDCB, DCB_CMD_SSTATE);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
add_rta(nlh, DCB_ATTR_STATE, (void *)&state, sizeof(__u8));
if (send_msg(nlh))
return -EIO;
return(recv_msg(DCB_CMD_SSTATE, DCB_ATTR_STATE, seq));
}
/* returns: 0 on success
* non-zero on failure
*/
static int set_pfc_cfg(char *ifname, __u8 *pfc)
{
struct nlmsghdr *nlh;
struct rtattr *rta_parent, *rta_child;
int i;
int seq;
LLDPAD_DBG("set_pfc_cfg: %s\n", ifname);
nlh = start_msg(RTM_SETDCB, DCB_CMD_PFC_SCFG);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
rta_parent = add_rta(nlh, DCB_ATTR_PFC_CFG, NULL, 0);
for (i = DCB_PFC_UP_ATTR_0; i < DCB_PFC_UP_ATTR_MAX; i++) {
rta_child = add_rta(nlh, i, (void *)pfc, sizeof(__u8));
rta_parent->rta_len += NLA_ALIGN(rta_child->rta_len);
pfc++;
}
if (send_msg(nlh))
return -EIO;
return(recv_msg(DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG, seq));
}
/* returns: 0 on success
* non-zero on failure
*/
static int set_pfc_state(char *ifname, __u8 state)
{
struct nlmsghdr *nlh;
int seq;
LLDPAD_DBG("set_pfc_state: %s\n", ifname);
nlh = start_msg(RTM_SETDCB, DCB_CMD_PFC_SSTATE);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
add_rta(nlh, DCB_ATTR_PFC_STATE, (void *)&state, sizeof(__u8));
if (send_msg(nlh))
return -EIO;
return(recv_msg(DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE, seq));
return 0;
}
/* returns: 0 on success
* 1 on failure
*/
static int set_pg_cfg(char *ifname, struct tc_config *tc, __u8 *bwg, int cmd)
{
struct nlmsghdr *nlh;
struct rtattr *class_parent, *param_parent, *rta_child;
__u8 *p = (__u8 *)tc;
__u8 *b = (__u8 *)bwg;
int i, j;
int seq;
nlh = start_msg(RTM_SETDCB, cmd);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
class_parent = add_rta(nlh, DCB_ATTR_PG_CFG, NULL, 0);
for (i = DCB_PG_ATTR_TC_0; i < DCB_PG_ATTR_TC_MAX; i++) {
param_parent = add_rta(nlh, i, NULL, 0);
for (j = DCB_TC_ATTR_PARAM_UNDEFINED + 1;
j < DCB_TC_ATTR_PARAM_MAX; j++) {
rta_child = add_rta(nlh, j, (void *)p, sizeof(__u8));
param_parent->rta_len += NLA_ALIGN(rta_child->rta_len);
p++;
}
class_parent->rta_len += NLA_ALIGN(param_parent->rta_len);
}
for (i = DCB_PG_ATTR_BW_ID_0; i < DCB_PG_ATTR_BW_ID_MAX; i++) {
rta_child = add_rta(nlh, i, (void *)b, sizeof(__u8));
class_parent->rta_len += NLA_ALIGN(rta_child->rta_len);
b++;
}
if (send_msg(nlh))
return -EIO;
return(recv_msg(cmd, DCB_ATTR_PG_CFG, seq));
}
/* returns 0: on error initializing interface
* 1: if successful
*/
int init_drv_if(void)
{
int err = 0;
if ((nl_sd = init_socket()) < 0) {
LLDPAD_ERR("Error creating Netlink socket\n");
return err;
}
return 1;
}
int deinit_drv_if(void)
{
int rc;
rc = fcntl(nl_sd, F_GETFD);
if (rc != -1) {
rc = close(nl_sd);
if (rc)
LLDPAD_ERR("Failed to close NETLINK socket (%d)\n",
rc);
nl_sd = 0;
}
return 0;
}
int set_dcbx_mode(char *ifname, __u8 mode)
{
struct nlmsghdr *nlh;
int seq;
nlh = start_msg(RTM_SETDCB, DCB_CMD_SDCBX);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
add_rta(nlh, DCB_ATTR_DCBX, (void *)&mode, sizeof(__u8));
if (send_msg(nlh))
return -EIO;
return recv_msg(DCB_CMD_SDCBX, DCB_ATTR_DCBX, seq);
}
int get_dcb_capabilities(char *ifname,
struct feature_support *dcb_capabilities)
{
struct nlmsghdr *nlh;
struct dcbmsg *d;
struct rtattr *rta_parent;
struct rtattr *rta_child;
int rval = 0;
unsigned int seq;
int i;
u8 cap;
memset((char *)dcb_capabilities, 0, sizeof(struct feature_support));
nlh = start_msg(RTM_GETDCB, DCB_CMD_GCAP);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
rta_parent = add_rta(nlh, DCB_ATTR_CAP, NULL, 0);
rta_child = add_rta(nlh, DCB_CAP_ATTR_ALL, NULL, 0);
rta_parent->rta_len += NLMSG_ALIGN(rta_child->rta_len);
if (send_msg(nlh))
return -EIO;
nlh = get_msg(seq);
if (!nlh)
return -EIO;
d = (struct dcbmsg *)NLMSG_DATA(nlh);
rta_parent = (struct rtattr *)(((char *)d) +
NLMSG_ALIGN(sizeof(struct dcbmsg)));
if (d->cmd != DCB_CMD_GCAP)
return -EIO;
if (rta_parent->rta_type != DCB_ATTR_CAP)
return -EIO;
rta_child = NLA_DATA(rta_parent);
rta_parent = (struct rtattr *)((char *)rta_parent +
NLMSG_ALIGN(rta_parent->rta_len));
for (i = 0; rta_parent > rta_child; i++) {
cap = *(u8 *)NLA_DATA(rta_child);
switch (rta_child->rta_type) {
case DCB_CAP_ATTR_PG:
dcb_capabilities->pg = cap;
break;
case DCB_CAP_ATTR_PFC:
dcb_capabilities->pfc = cap;
break;
case DCB_CAP_ATTR_UP2TC:
dcb_capabilities->up2tc_mappable = cap;
break;
case DCB_CAP_ATTR_PG_TCS:
dcb_capabilities->traffic_classes = cap;
break;
case DCB_CAP_ATTR_PFC_TCS:
dcb_capabilities->pfc_traffic_classes = cap;
break;
case DCB_CAP_ATTR_GSP:
dcb_capabilities->gsp = cap;
break;
case DCB_CAP_ATTR_DCBX:
dcb_capabilities->dcbx = cap;
break;
default:
LLDPAD_DBG("unknown capability %d: %02x\n",
rta_child->rta_type, cap);
break;
}
rta_child = (struct rtattr *)((char *)rta_child +
NLMSG_ALIGN(rta_child->rta_len));
}
if (rta_parent != rta_child)
LLDPAD_DBG("rta pointers are off\n");
free(nlh);
return rval;
}
int get_dcb_numtcs(const char *ifname, u8 *pgtcs, u8 *pfctcs)
{
struct nlmsghdr *nlh;
struct dcbmsg *d;
struct rtattr *rta_parent;
struct rtattr *rta_child;
int rval = 0;
unsigned int seq;
int i;
int found;
char name[IFNAMSIZ];
nlh = start_msg(RTM_GETDCB, DCB_CMD_GNUMTCS);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
strncpy(name, ifname, sizeof(name));
add_rta(nlh, DCB_ATTR_IFNAME, (void *)name, strlen(name) + 1);
rta_parent = add_rta(nlh, DCB_ATTR_NUMTCS, NULL, 0);
rta_child = add_rta(nlh, DCB_NUMTCS_ATTR_ALL, NULL, 0);
rta_parent->rta_len += NLMSG_ALIGN(rta_child->rta_len);
if (send_msg(nlh))
return -EIO;
nlh = get_msg(seq);
if (!nlh)
return -EIO;
d = (struct dcbmsg *)NLMSG_DATA(nlh);
rta_parent = (struct rtattr *)(((char *)d) +
NLMSG_ALIGN(sizeof(struct dcbmsg)));
if (d->cmd != DCB_CMD_GNUMTCS)
return -EIO;
if (rta_parent->rta_type != DCB_ATTR_NUMTCS)
return -EIO;
rta_child = NLA_DATA(rta_parent);
rta_parent = (struct rtattr *)((char *)rta_parent +
NLMSG_ALIGN(rta_parent->rta_len));
found = 0;
for (i = 0; rta_parent > rta_child; i++) {
switch (rta_child->rta_type) {
case DCB_NUMTCS_ATTR_PG:
if (! (found & 0x01) ) {
*pgtcs = *(u8 *)NLA_DATA(rta_child);
found += 1;
}
break;
case DCB_NUMTCS_ATTR_PFC:
if (!(found & 0x02)) {
*pfctcs = *(u8 *)NLA_DATA(rta_child);
found += 2;
}
break;
default:
LLDPAD_DBG("unknown capability %d: %02x\n",
rta_child->rta_type,
*(u8 *)NLA_DATA(rta_child));
break;
}
rta_child = (struct rtattr *)((char *)rta_child +
NLMSG_ALIGN(rta_child->rta_len));
}
if (rta_parent != rta_child)
LLDPAD_DBG("rta pointers are off\n");
free(nlh);
if (found != 3)
rval = -EIO;
return rval;
}
int get_hw_state(char *ifname, int *dcb_state)
{
int err = 0;
__u8 state = 0;
err = get_state(ifname, &state);
if (err)
LLDPAD_INFO("Adapter %s does not support DCB.\n", ifname);
else
LLDPAD_DBG("Adapter %s supports DCB.\n", ifname);
*dcb_state = state;
return err;
}
int set_hw_state(char *ifname, int dcb_state)
{
int err;
int ifindex = get_ifidx(ifname);
err = set_linkmode(ifindex, ifname, dcb_state);
if (err)
LLDPAD_DBG("ERROR %s: set_linkmode dcbstate %i\n",
__func__, dcb_state);
return set_state(ifname, (__u8)dcb_state);
}
/* returns: 0 on success
* 1 on failure
*/
int set_hw_pg(char *ifname, pgroup_attribs *pg_data, bool oper_mode)
{
int i, j;
int rval = 0;
struct tc_config tc[MAX_TRAFFIC_CLASSES];
__u8 bwg[MAX_BANDWIDTH_GROUPS];
pg_attribs pg_df_store;
pgroup_attribs pg_df_data, *pg_temp;
if (!oper_mode) { /* oper mode is false */
get_pg(DEF_CFG_STORE, &pg_df_store);
memcpy(&pg_df_data.rx, &pg_df_store.rx, sizeof(pg_df_data.rx));
memcpy(&pg_df_data.tx, &pg_df_store.tx, sizeof(pg_df_data.tx));
pg_temp = &pg_df_data;
} else {
pg_temp = pg_data;
}
/* Configure TX PG per TC Settings */
for (i = 0; i < MAX_TRAFFIC_CLASSES; i++)
tc[i].up_to_tc_bitmap = 0;
for (i = 0; i < MAX_USER_PRIORITIES; i++) {
for (j = 0; j < MAX_TRAFFIC_CLASSES; j++) {
if (pg_temp->tx.up[i].pgid == j) {
tc[j].up_to_tc_bitmap |= (1 << i);
tc[j].prio_type = pg_temp->tx.up[i].strict_priority;
tc[j].tc_percent = pg_temp->tx.up[i].percent_of_pg_cap;
}
}
}
for (i = 0; i < MAX_TRAFFIC_CLASSES; i++) {
tc[i].bwgid = pg_temp->tx.up[i].bwgid;
bwg[i] = pg_temp->tx.pg_percent[i];
LLDPAD_DBG("%s %s: (%i) TX bwgid %i up_to_tc %i "
"prio %i percent %i\n",
__func__, ifname, i,
tc[i].bwgid,
tc[i].up_to_tc_bitmap,
tc[i].prio_type,
tc[i].tc_percent);
}
rval = set_pg_cfg(ifname, &tc[0], &bwg[0], DCB_CMD_PGTX_SCFG);
/* Configure RX PG per TC Settings */
for (i = 0; i < MAX_TRAFFIC_CLASSES; i++)
tc[i].up_to_tc_bitmap = 0;
for (i = 0; i < MAX_USER_PRIORITIES; i++) {
for (j = 0; j < MAX_TRAFFIC_CLASSES; j++) {
if (pg_temp->tx.up[i].pgid == j) {
tc[j].up_to_tc_bitmap |= (1 << i);
tc[j].prio_type = pg_temp->rx.up[i].strict_priority;
tc[j].tc_percent = pg_temp->rx.up[i].percent_of_pg_cap;
}
}
}
for (i = 0; i < MAX_TRAFFIC_CLASSES; i++) {
tc[i].bwgid = pg_temp->rx.up[i].bwgid;
bwg[i] = pg_temp->rx.pg_percent[i];
LLDPAD_DBG("%s %s: (%i) RX bwgid %i up_to_tc %i "
"prio %i percent %i\n",
__func__, ifname, i,
tc[i].bwgid,
tc[i].up_to_tc_bitmap,
tc[i].prio_type,
tc[i].tc_percent);
}
rval |= set_pg_cfg(ifname, &tc[0], &bwg[0], DCB_CMD_PGRX_SCFG);
return rval;
}
/* returns: 0 on success
* non-zero on failure
*/
int set_hw_pfc(char *ifname, dcb_pfc_list_type pfc_data,
bool oper_mode)
{
int i;
__u8 pfc[MAX_TRAFFIC_CLASSES];
pfc_attribs pfc_df_store;
pfc_type *pfc_temp;
int rval;
if (!oper_mode) /* oper mode is false */
{
get_pfc(DEF_CFG_STORE, &pfc_df_store);
pfc_temp = pfc_df_store.admin;
} else {
pfc_temp = pfc_data;
}
for (i = 0; i < MAX_TRAFFIC_CLASSES; i++) {
if (pfc_temp[i])
pfc[i] = PFC_ENABLED;
else
pfc[i] = PFC_DISABLED;
}
rval = set_pfc_cfg(ifname, &pfc[0]);
if (!rval)
rval = set_pfc_state(ifname, (__u8)oper_mode);
return rval;
}
/* returns: 0 on success
* 1 on failure
*/
int set_hw_app(char *ifname, appgroup_attribs *app_data)
{
struct nlmsghdr *nlh;
struct rtattr *rta_parent, *rta_child;
int seq;
LLDPAD_DBG("set_hw_app: %s\n", ifname);
nlh = start_msg(RTM_SETDCB, DCB_CMD_SAPP);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
rta_parent = add_rta(nlh, DCB_ATTR_APP, NULL, 0);
rta_child = add_rta(nlh, DCB_APP_ATTR_IDTYPE,
(void *)&app_data->dcb_app_idtype, sizeof(__u8));
rta_parent->rta_len += NLA_ALIGN(rta_child->rta_len);
rta_child = add_rta(nlh, DCB_APP_ATTR_ID,
(void *)&app_data->dcb_app_id, sizeof(__u16));
rta_parent->rta_len += NLA_ALIGN(rta_child->rta_len);
rta_child = add_rta(nlh, DCB_APP_ATTR_PRIORITY,
(void *)&app_data->dcb_app_priority, sizeof(__u8));
rta_parent->rta_len += NLA_ALIGN(rta_child->rta_len);
if (send_msg(nlh))
return -EIO;
return(recv_msg(DCB_CMD_SAPP, DCB_ATTR_APP, seq));
}
int run_cmd(char *cmd, ...)
{
char cbuf[128];
va_list args;
va_start(args, cmd);
vsprintf(cbuf, cmd, args);
va_end(args);
return system(cbuf);
}
int set_hw_all(char *ifname)
{
struct nlmsghdr *nlh;
int status = 1; /* status is always true */
int retval = -EIO;
int seq;
nlh = start_msg(RTM_SETDCB, DCB_CMD_SET_ALL);
if (NULL == nlh)
return -EIO;
seq = nlh->nlmsg_seq;
add_rta(nlh, DCB_ATTR_IFNAME, (void *)ifname, strlen(ifname) + 1);
add_rta(nlh, DCB_ATTR_SET_ALL, (void *)&status, sizeof(__u8));
if (send_msg(nlh))
return -EIO;
retval = recv_msg(DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, seq);
/* driver will respond with
* 0 = hw config changes made - with link reset
* 1 = no hw config changes were necessary
* 2 = hw config changes made - with no link reset
*/
if (retval == 0)
set_port_hw_resetting(ifname, 1);
return 0;
}
bool check_port_dcb_mode(char *ifname)
{
int dcb_state = 0;
if (get_hw_state(ifname, &dcb_state))
dcb_state = 0;
if (dcb_state) {
LLDPAD_DBG("config.c: %s dcb mode is ON.\n", ifname);
return true;
} else {
LLDPAD_DBG("config.c: %s dcb mode is OFF. \n", ifname);
return false;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/sdlhk/lldpad.git
git@gitee.com:sdlhk/lldpad.git
sdlhk
lldpad
lldpad
master

搜索帮助