设置网卡网速
int phy_mii_ioctl(struct phy_device *phydev,
struct mii_ioctl_data *mii_data, int cmd)
{
u16 val = mii_data->val_in;
switch (cmd) {
case SIOCGMIIPHY:
mii_data->phy_id = phydev->addr;
break;
case SIOCGMIIREG:
mii_data->val_out = phy_read(phydev, mii_data->reg_num);
break;
case SIOCSMIIREG:
if (!capable(CAP_NET_ADMIN))
return -EPERM;
if (mii_data->phy_id == phydev->addr) {
switch(mii_data->reg_num) {
case MII_BMCR:
if (val & (BMCR_RESET|BMCR_ANENABLE))
phydev->autoneg = AUTONEG_DISABLE;
else
phydev->autoneg = AUTONEG_ENABLE;
if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
phydev->duplex = DUPLEX_FULL;
else
phydev->duplex = DUPLEX_HALF;
if ((!phydev->autoneg) &&
(val & BMCR_SPEED1000))
phydev->speed = SPEED_1000;
else if ((!phydev->autoneg) &&
(val & BMCR_SPEED100))
phydev->speed = SPEED_100;
break;
case MII_ADVERTISE:
phydev->advertising = val;
break;
default:
/* do nothing */
break;
}
}
phy_write(phydev, mii_data->reg_num, val);
if (mii_data->reg_num == MII_BMCR
&& val & BMCR_RESET
&& phydev->drv->config_init)
phydev->drv->config_init(phydev);
break;
}
return 0;
}
发这个ioctl来设置SIOCSMIIREG
ifconfig修改mac地址
有三种方法:
方法一:1.关闭网卡设备
ifconfig eth0 down
2.修改MAC地址
ifconfig eth0 hw ether 00:AA:BB:CC:DD:11
3.重启网卡
ifconfig eth0 up
service network restart
方法三:
很简单的,只是在./etc/sysconfig/network-scripts/ifcfg-eth0中加入下面一句话:
MACADDR=00:AA:BB:CC:DD:EE
SIOCSIFHWADDR 设置mac地址
用ioctl控制,通过SIOCGIFHWADDR获取MAC地址,SIOCSIFHWADDR设置MAC地址,不过在设
置MAC地址之前,要先把网卡down掉,设置好了以后,再UP起来
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
typedef signed char INT8S;
typedef unsigned char INT8U;
typedef signed short INT16S;
typedef unsigned short INT16U;
typedef signed int INT32S;
typedef unsigned int INT32U;
//add by lightd, 2014-06-04
//============================================================================
//Function: ifconfig_ethx_down_API
//Description: 关闭本地指定网卡 - eg: ifconfig eth0 down
//Input:
//Output:
//Return:
//Others: None
//============================================================================
INT8S ifconfig_ethx_down_API(const INT8U *interface_name)
{
INT32S sock_fd;
struct ifreq ifr;
int selector;
//传入参数合法性检测
if(interface_name == NULL)
{
fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__);
return -1;
}
//禁止关闭回环
if(strncmp((char *)interface_name, (char *)"lo", 2) == 0)
{
fprintf(stdout, "%s:%d: You can't pull down interface lo!", __FUNCTION__, __LINE__);
return 0;
}
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_fd < 0)
{
fprintf(stdout, "%s:%d: socket failed!", __FUNCTION__, __LINE__);
return -2;
}
sprintf(ifr.ifr_name, "%s", interface_name);
if(ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0)
{
fprintf(stdout, "%s:%d: ioctl failed 1!", __FUNCTION__, __LINE__);
return -3;
}
selector = IFF_UP;
ifr.ifr_flags &= ~selector;
if(ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0)
{
fprintf(stdout, "%s:%d: ioctl failed 2!", __FUNCTION__, __LINE__);
return -4;
}
close( sock_fd );
return 0;
}
//add by lightd, 2014-06-04
//============================================================================
//Function: ifconfig_ethx_up_API
//Description: 打开本地指定网卡 - eg: ifconfig eth0 up
//Input:
//Output:
//Return:
//Others: None
//============================================================================
INT8S ifconfig_ethx_up_API(const INT8U *interface_name)
{
INT32S sock_fd;
struct ifreq ifr;
int selector;
//传入参数合法性检测
if(interface_name == NULL)
{
fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__);
return -1;
}
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_fd < 0)
{
fprintf(stdout, "%s:%d: create socket failed!", __FUNCTION__, __LINE__);
return -2;
}
sprintf(ifr.ifr_name, "%s", interface_name);
if(ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0)
{
fprintf(stdout, "%s:%d: ioctl error 1", __FUNCTION__, __LINE__);
return -3;
}
selector = (IFF_UP | IFF_RUNNING);
ifr.ifr_flags |= selector;
if(ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0)
{
fprintf(stdout, "%s:%d: ioctl error 2", __FUNCTION__, __LINE__);
return -4;
}
close( sock_fd );
return 0;
}
//add by lightd, 2014-06-04
//============================================================================
//Function: SetLocalMACAddr_API
//Description: 设置本地指定网卡的MAC
//Input:
//Output:
//Return:
//Others: None
//Test result: 测试结果 - 连续调用20次不出错
//============================================================================
INT8S SetLocalMACAddr_API(const INT8U *interface_name, const INT8U *str_macaddr)
{
int ret;
int sock_fd;
struct ifreq ifr;
INT32U mac2bit[6];
//传入参数合法性检测
if(interface_name == NULL || str_macaddr == NULL)
{
fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__);
return -1;
}
//提取mac格式
sscanf((char *)str_macaddr, "%02X:%02X:%02X:%02X:%02X:%02X", (INT8U *)&mac2bit[0], (INT8U *)&mac2bit[1], (INT8U *)&mac2bit[2], (INT8U *)&mac2bit[3], (INT8U *)&mac2bit[4], (INT8U *)&mac2bit[5]);
sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0)
{
perror("socket error");
return -2;
}
//设置mac前,必须关闭对应的网卡 - 否则出错
ret = ifconfig_ethx_down_API( interface_name );
if(ret < 0)
{
fprintf(stdout, "%s:%d: close eth0 error", __FUNCTION__, __LINE__);
return -3;
}
sleep(1); //等待网卡关闭OK
sprintf(ifr.ifr_ifrn.ifrn_name, "%s", interface_name);
ifr.ifr_ifru.ifru_hwaddr.sa_family = 1;
ifr.ifr_ifru.ifru_hwaddr.sa_data[0] = mac2bit[0];
ifr.ifr_ifru.ifru_hwaddr.sa_data[1] = mac2bit[1];
ifr.ifr_ifru.ifru_hwaddr.sa_data[2] = mac2bit[2];
ifr.ifr_ifru.ifru_hwaddr.sa_data[3] = mac2bit[3];
ifr.ifr_ifru.ifru_hwaddr.sa_data[4] = mac2bit[4];
ifr.ifr_ifru.ifru_hwaddr.sa_data[5] = mac2bit[5];
ret = ioctl(sock_fd, SIOCSIFHWADDR, &ifr);
if (ret != 0)
{
perror("set mac address erorr");
return -4;
}
close( sock_fd );
ret = ifconfig_ethx_up_API( interface_name );
if(ret < 0)
{
fprintf(stdout, "%s:%d: open eth0 error!", __FUNCTION__, __LINE__);
return -5;
}
sleep(2); //等待网卡打开OK
return 0;
}
//add by lightd, 2014-06-04
//============================================================================
//Function: GetLocalMACAddr_API
//Description: 获取本地指定网卡的MAC
//Input:
//Output:
//Return:
//Others: None
//============================================================================
INT8S GetLocalMACAddr_API(const INT8U *interface_name, INT8U *str_macaddr)
{
INT32S sock_fd;
struct ifreq ifr_mac;
//传入参数合法性检测
if(interface_name == NULL || str_macaddr == NULL)
{
fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__);
return -1;
}
sock_fd = socket( AF_INET, SOCK_STREAM, 0 );
if( sock_fd == -1)
{
perror("create socket failed");
sprintf((char *)str_macaddr, "00:00:00:00:00:00");
return -2;
}
//指定网卡
memset(&ifr_mac, 0, sizeof(ifr_mac));
sprintf(ifr_mac.ifr_name, "%s", interface_name);
//获取指定网卡的mac地址
if( (ioctl( sock_fd, SIOCGIFHWADDR, &ifr_mac)) < 0 )
{
perror("mac ioctl error");
sprintf((char *)str_macaddr, "00:00:00:00:00:00");
return -3;
}
close( sock_fd );
sprintf((char *)str_macaddr,"%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifr_mac.ifr_hwaddr.sa_data[0],
(unsigned char)ifr_mac.ifr_hwaddr.sa_data[1],
(unsigned char)ifr_mac.ifr_hwaddr.sa_data[2],
(unsigned char)ifr_mac.ifr_hwaddr.sa_data[3],
(unsigned char)ifr_mac.ifr_hwaddr.sa_data[4],
(unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]);
printf("local mac:<%s> \n", str_macaddr);
return 0;
}
int main(void)
{
INT8U str_macaddr[20];
memset(str_macaddr, 0, sizeof(str_macaddr));
GetLocalMACAddr_API("eth0", str_macaddr);
fprintf(stdout, "1 mac: %s\n", str_macaddr);
//ifconfig_ethx_down_API("eth0");
//system("ifconfig eth0 down");
//usleep(500000);
//system("ifconfig eth0 up");
//usleep(500000);
//sleep(1); //10ms
//ifconfig_ethx_down_API("eth0");
//sleep(1);
SetLocalMACAddr_API("eth0", "08:00:11:22:33:44");
//ifconfig_ethx_up_API("eth0");
//ifconfig_ethx_up_API("eth0");
//sleep(2);
memset(str_macaddr, 0, sizeof(str_macaddr));
GetLocalMACAddr_API("eth0", str_macaddr);
fprintf(stdout, "2 mac: %s\n", str_macaddr);
system("ping 200.200.200.100");
//usleep(50000);
//ifconfig_ethx_down_API("eth0");
//ifconfig_ethx_up_API("eth0");
//memset(str_macaddr, 0, sizeof(str_macaddr));
//GetLocalMACAddr_API("eth0", str_macaddr);
fprintf(stdout, "2 mac: %s\n", str_macaddr);
return 0;
}
SIOCSMIIREG SIOCGMIIREG获取以及设置mac地址
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/mii.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/types.h>
#include <netinet/in.h>
#define reteck(ret) \
if(ret < 0){ \
printf("%m! \"%s\" : line: %d\n", __func__, __LINE__); \
goto lab; \
}
#define help() \
printf("mdio:\n"); \
printf("read operation: mdio reg_addr\n"); \
printf("write operation: mdio reg_addr value\n"); \
printf("For example:\n"); \
printf("mdio eth0 1\n"); \
printf("mdio eth0 0 0x12\n\n"); \
exit(0);
int sockfd;
int main(int argc, char *argv[]){
if(argc == 1 || !strcmp(argv[1], "-h")){
help();
}
struct mii_ioctl_data *mii = NULL;
struct ifreq ifr;
int ret;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
reteck(sockfd);
//get phy address in smi bus
ret = ioctl(sockfd, SIOCGMIIPHY, &ifr);
reteck(ret);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
if(argc == 3){
mii->reg_num = (uint16_t)strtoul(argv[2], NULL, 0);
ret = ioctl(sockfd, SIOCGMIIREG, &ifr);
reteck(ret);
printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n\n", mii->phy_id, mii->reg_num, mii->val_out);
}else if(argc == 4){
mii->reg_num = (uint16_t)strtoul(argv[2], NULL, 0);
mii->val_in = (uint16_t)strtoul(argv[3], NULL, 0);
ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
reteck(ret);
printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n\n", mii->phy_id, mii->reg_num, mii->val_in);
}
lab:
close(sockfd);
return 0;
}
很多人在read操作里面判断phy的link状态,
if(mii->val_out& 0x0004){
printf("linkup\n");
}else{
printf("linkdown\n");
}
Uboot通过bootm命令向内核传递MAC地址
一、内核参数的传递
向Linux驱动传递参数的方式有两种,一为在系统启动的时候由bootloader传入,还有一种是将驱动编译成模块,将参数作为模块加载的参数传入。
本系统使用的是由bootloader传入的方式。内核通过setup接口接受Bootloader传入的参数。方式如下:
static int __init param_mac_setup(char *str)
{
……
}
__setup("mac=", param_mac_setup);
这样,当在Bootloader中指定“mac=00:2E:79:38:6D:4E”,系统在加载这个模块的时候,就会执行相应的param_mac_setup()函数,而传入给它的参数就是等号后面的物理地址“00:2E:79:38:6D:4E”。这样,该函数就可以对它进行相应的处理。
二、bootm传递参数的方式
在bootm执行的流程图中,可以看到会调用do_bootm_linux()在执行Linux内核,内核的起始地址如下:
void (*theKernel)(int zero, int arch, uint params);
image_header_t *hdr = &header;
theKernel = (void (*)(int, int, uint))ntohl(hdr->ih_ep);
header是uImage的头部,通过头部,得到内核映像起始的执行地址,标识为theKernel。从中也可以看到,内核接受三个参数,第一个为0,第二个为系统的ID号,第三个是传入内核的参数。
在do_bootm_linux()的最后,会跳到内核去执行:
theKernel (0, bd->bi_arch_number, bd->bi_boot_params);
最后两个参数在board/smdk2410/smdk2410.c的board_init()中被初始化:
/* arch number of SMDK2410-Board */
gd->bd->bi_arch_number = MACH_TYPE_SMDK2410; /* 193 */
/* adress of boot parameters */
gd->bd->bi_boot_params = 0x30000100;
可以看到,uboot传给内核的参数表存放在内存中起始偏移0x100的位置,这里只是指定了“指针”的位置,但还没初始化其中的值,这是在do_bootm_linux()中跳到内核前去完成的。
值得注意的是, 内核的默认运行地址的0x30008000,前面就是留给参数用的。所以一般不要将内核下载到该地址之前,以免冲掉了传给内核的参数。
三、参数列表的构建
#if defined (CONFIG_SETUP_MEMORY_TAGS) || /
defined (CONFIG_CMDLINE_TAG) || /
defined (CONFIG_INITRD_TAG) || /
defined (CONFIG_SERIAL_TAG)
setup_start_tag (bd);
#ifdef CONFIG_SERIAL_TAG
setup_serial_tag (¶ms);
#endif
#ifdef CONFIG_REVISION_TAG
setup_revision_tag (¶ms);
#endif
#ifdef CONFIG_SETUP_MEMORY_TAGS
setup_memory_tags (bd);
#endif
#ifdef CONFIG_CMDLINE_TAG
setup_commandline_tag (bd, commandline);
#endif
#ifdef CONFIG_INITRD_TAG
if (initrd_start && initrd_end)
setup_initrd_tag (bd, initrd_start, initrd_end);
#endif
#if defined (CONFIG_VFD) || defined (CONFIG_LCD)
setup_videolfb_tag ((gd_t *) gd);
#endif
setup_end_tag (bd);
#endif
四、将MAC地址参数传入命令行参数
为了传入命令行参数,uboot所作的是:
char *commandline = getenv ("bootargs");
setup_commandline_tag (bd, commandline);
现在想要把MAC地址也加入到命令行参数中,只需要实现:
char *commandline = getenv ("bootargs") + “ mac=” + getenv ("ethaddr");
setup_commandline_tag (bd, commandline);
即可。这样就不需要每次在命令行后面手工追加MAC地址参数了。