文章目录
1 前言
SNMP(简单网络管理协议)是一种用于网络管理的标准化协议,它的出现极大地简化了网络管理员对网络设备的管理工作。SNMP的应用范围非常广泛,可以用于监控网络设备的运行状态、性能、故障等情况,帮助管理员及时发现和解决问题。同时,SNMP还可以实现网络设备的远程管理和控制,提高网络管理的效率和便利性。
W5100S/W5500是一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,同时也是一颗工业级以太网控制芯片。本教程将介绍W5100S/W5500以太网SNMP应用的基本原理、使用步骤、应用实例以及注意事项,帮助读者更好地掌握这一技术。
2 简介
2 .1 什么是SNMP?
SNMP(Simple Network Management Protocol)即简单网络管理协议,是一种用于网络设备管理的应用层协议。它允许网络管理员监视、配置和控制网络中的设备,以确保网络的正常运行和性能优化。SNMP协议基于客户端-服务器模型,其中网络设备被视为代理(Agent),而网络管理系统则充当管理者(Manager)。管理者通过发送SNMP消息到代理,获取设备的状态信息和执行操作。
2.2 SNMP的优点
SNMP的优点包括:
- 简单易用:SNMP协议的设计简单,易于理解和实现,不需要复杂的实现过程,也不会占用太多的网络资源,非常便于使用。
- 广泛支持:SNMP协议被广泛应用于网络设备管理中,几乎所有的网络管理人员都喜欢使用简单的SNMP来完成工作操作。各大网络硬件产品商在设计和生产网桥、路由器等网络设备时都加入了对SNMP协议的支持。
- 可扩展性:SNMP协议具有良好的可扩展性。因为协议本身非常简单,所以对协议的任何升级或扩展也非常方便,从而能够满足今后网络的发展需求。
- 标准化:SNMP协议是互联网标准协议之一,保证了其兼容性和互操作性,使得不同厂商和不同型号的设备可以相互通信和协作。
- 多功能性:SNMP协议提供了丰富的管理信息和控制功能,可以监视设备的状态、配置参数、性能指标等,还可以对设备进行远程配置和控制。
- 安全性:SNMP协议提供了加密和认证功能,可以保证数据的安全性和完整性。同时,SNMP还支持基于用户角色的访问控制,确保只有授权用户才能访问网络设备。
2.3 SNMP工作原理
SNMP的工作步骤如下:
- NMS向Agent发送SNMP请求报文,请求获取设备的某些管理变量。
- Agent接收到请求报文后,根据报文类型进行Read或Write操作,生成响应报文,并将报文返回给NMS。
- Agent在设备发生异常情况或状态改变时(如设备重新启动),也会主动向NMS发送Trap报文,向NMS汇报所发生的事件。
- Agent主动发出的报文,通知NMS进程有某些事情发生。
- Agent向NMS报告通报的消息。
2.4 SNMP应用场景
以下是一些常见的SNMP应用场景:
- 网络监控:SNMP可以用于监控网络设备的状态和性能,包括CPU利用率、内存使用情况、磁盘空间、端口状态等。通过实时监测这些指标,可以及时发现网络设备的异常情况,并采取相应的措施进行维护和故障排除。
- 故障排除:SNMP可以用于故障排除,通过获取网络设备的详细信息,可以快速定位和解决网络故障。例如,当网络连接出现问题时,可以使用SNMP检查设备的接口状态、错误计数器等信息,以确定故障的原因。
- 网络配置:SNMP可以用于远程配置网络设备,包括IP地址、设备名称、路由配置等。通过SNMP协议,网络管理员可以在远程对设备进行配置和管理,提高了管理效率和管理范围。
- 安全管理:SNMP可以用于网络安全管理,通过加密和认证机制保护数据的安全性和完整性。同时,SNMP还可以用于访问控制,只有授权用户才能访问网络设备,确保网络设备不会被未经授权的用户访问和攻击。
- 流量控制:SNMP可以用于流量控制,通过获取网络设备的流量数据,可以控制和优化网络流量。例如,可以使用SNMP协议对网络设备进行流量整形和流量控制,以确保网络设备的稳定运行和保证网络应用的性能。
3 WIZnet以太网芯片
WIZnet 主流硬件协议栈以太网芯片参数对比
Model | Embedded Core | Host I/F | TX/RX Buffer | HW Socket | Network Performance |
---|---|---|---|---|---|
W5100S | TCP/IPv4, MAC & PHY | 8bit BUS, SPI | 16KB | 4 | Max.25Mbps |
W6100 | TCP/IPv4/IPv6, MAC & PHY | 8bit BUS, Fast SPI | 32KB | 8 | Max.25Mbps |
W5500 | TCP/IPv4, MAC & PHY | Fast SPI | 32KB | 8 | Max 15Mbps |
- W5100S/W6100 支持 8bit数据总线接口,网络传输速度会优于W5500。
- W6100 支持IPV6,与W5100S 硬件兼容,若已使用W5100S的用户需要支持IPv6,可以Pin to Pin兼容。
- W5500 拥有比 W5100S更多的 Socket数量以及发送与接收缓存。
4 SNMP网络设置示例概述以及使用
4.1 流程图
程序的运行框图如下所示:
4.2 准备工作核心
软件
- Visual Studio Code
- WIZnet UartTool
- Net-SNMP
硬件
- W5100SIO模块 + RP2040 树莓派Pico开发板 或者 WIZnet W5100S-EVB-Pico开发板
- Micro USB 接口的数据线
- TTL 转 USB
- 网线
4.3 连接方式
-
通过数据线连接PC的USB口(主要用于烧录程序,也可以虚拟出串口使用)
-
通过TTL串口转USB,连接UART0 的默认引脚:
- RP2040 GPIO0(UART0 TX) <----> USB_TTL_RX
- RP2040 GPIO1(UART0 RX) <----> USB_TTL_TX
-
使用模块连接RP2040 进行接线时
- RP2040 GPIO16 <----> W5100S MISO
- RP2040 GPIO17 <----> W5100S CS
- RP2040 GPIO18 <----> W5100S SCK
- RP2040 GPIO19 <----> W5100S MOSI
- RP2040 GPIO20 <----> W5100S RST
-
通过PC和设备都通过网线连接路由器LAN口
4.4 主要代码概述
我们使用的是WIZnet官方的ioLibrary_Driver库。该库支持的协议丰富,操作简单,芯片在硬件上集成了TCP/IP协议栈,该库又封装好了TCP/IP层之上的协议,我们只需简单调用相应函数即可完成协议的应用。
第一步:snmp_run.c文件中加入对应的.h文件。
第二步:定义DHCP配置需要的宏和定义GPIO口的宏。
第三步:网络信息的配置,开启DHCP模式,定义和初始化HTTP服务器IP和端口号。
第四步:编写定时器回调处理函数,用于 DHCP 1s滴答定时器处理函数还有灯的初始化、灯的获取状态和修改状态函数用于显示效果。
第五步:主函数先是定义了一个定时器结构体参数用来触发定时器回调函数,对串口和SPI进行初始化,然后写入W5100S的网络配置参数,初始化DHCP后开始DHCP获取IP,获取到就打印获取到的IP,获取次数超过最大获取次数时就使用静态IP,然后进行灯的初始化和设置灯的状态,然后进行初始化SNMP函数需传入pc端的ip和自身ip以及socket号,主循环开始执行snmp执行函数。
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"
#include "hardware/gpio.h"
#include "wizchip_conf.h"
#include "bsp_spi.h"
#include "dhcp.h" // Use dhcp
#include "socket.h" // Use socket
#include "snmp_custom.h" // Use snmp
#define SOCKET_ID 0 // Socket number
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size
#define DHCP_RETRY_COUNT 5 // DHCP retry times
#define USER_LED_PIN 25 // Onboard led pin
/**
* @brief Timer callback processing function, used for snmp timing processing
* @param t: Timer structure
* @return bool
*/
bool precess_snmp_time_handle(struct repeating_timer *t);
/**
* @brief Timer callback processing function, used for dhcp timing processing
* @param t :Timer structure
* @return bool
*/
bool repeating_timer_callback(struct repeating_timer *t);
/**
* @brief Initialization of chip network information
* @param conf_info :Static configuration information
* @return none
*/
void network_init(wiz_NetInfo *conf_info);
/**
* @brief Initiallization led and Registration function
* @param none
* @return none
*/
void UserLED_Init(void);
/**
* @brief get led status, in order to adapt data format, see details the file: snmp_custom.c 's snmpData[]
* @param ptr: use to build string
* @param len: length of build string
* @return none
*/
void getUserLEDStatus(void *ptr, uint8_t *len);
/**
* @brief set led status, in order to adapt data format, see details the file: snmp_custom.c 's snmpData[]
* @param val: 0 -> led off, 1 -> led on
* @return none
*/
void setUserLEDStatus(int32_t val);
/* Network information to be configured. */
wiz_NetInfo net_info = {
.mac = {0x00, 0x08, 0xdc, 0x1e, 0xed, 0x2e}, // Configured MAC address
.ip = {192, 168, 1, 10}, // Configured IP address
.sn = {255, 255, 255, 0}, // Configured subnet mask
.gw = {192, 168, 1, 1}, // Configured gateway
.dns = {8, 8, 8, 8}, // Configured domain address
.dhcp = NETINFO_DHCP}; // Configured dhcp model,NETINFO_DHCP:use dhcp; NETINFO_STATIC: use static ip.
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
0,
}; // Send and receive cachestatic
uint8_t manager[4] = {192, 168, 1, 2}; // manager ip, (is your pc ip or others managers)
static uint8_t breakout_flag = 0; // Define the DHCP acquisition flag
int main()
{
struct repeating_timer timer; // Define the timer structure
struct repeating_timer timer1;
wiz_NetInfo get_info; // Stores the read configuration information
/* MCU init */
stdio_init_all(); // Initialize the main control peripheral
wizchip_initialize(); // Initialize the chip interface
/*dhcp init*/
DHCP_init(SOCKET_ID, ethernet_buf); // DHCP initialization
add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add DHCP 1s Tick Timer handler
add_repeating_timer_ms(10, precess_snmp_time_handle, NULL, &timer1); // Add SNMP 10ms Tick Timer handler
printf("wiznet chip snmp example.\r\n");
network_init(&net_info); // Configuring Network Information
print_network_information(&get_info); // Read back the configuration information and print it
UserLED_Init();
setUserLEDStatus(0);
snmpd_init(manager, get_info.ip, SOCKET_ID, SOCKET_ID + 1); // Initialize snmp
while (true)
{
snmpd_run(); // Run snmp
}
}
void network_init(wiz_NetInfo *conf_info)
{
int count = 0;
uint8_t dhcp_retry = 0;
if (conf_info->dhcp == NETINFO_DHCP)
{
while (true)
{
switch (DHCP_run()) // Do the DHCP client
{
case DHCP_IP_LEASED: // DHCP resolves the domain name successfully
{
if (breakout_flag == 0)
{
printf("DHCP success\r\n");
getIPfromDHCP((*conf_info).ip);
getGWfromDHCP((*conf_info).gw);
getSNfromDHCP((*conf_info).sn);
getDNSfromDHCP((*conf_info).dns);
wizchip_setnetinfo(conf_info); // Configuring Network Information
close(SOCKET_ID); // After dhcp close the socket, avoid errors in later use
breakout_flag = 1;
}
break;
}
case DHCP_FAILED:
{
printf(" DHCP failed \r\n");
count++;
if (count <= DHCP_RETRY_COUNT) // If the number of times is less than or equal to the maximum number of times, try again
{
printf("DHCP timeout occurred and retry %d \r\n", count);
}
else if (count > DHCP_RETRY_COUNT) // If the number of times is greater than DHCP fails
{
breakout_flag = 1; // if DHCP fail, use the static
DHCP_stop(); // Stop processing DHCP protocol
conf_info->dhcp = NETINFO_STATIC;
wizchip_setnetinfo(conf_info); // Configuring Network Information
break;
}
break;
}
}
if (breakout_flag)
{
printf("config succ\r\n");
break;
}
}
}
else
{
wizchip_setnetinfo(conf_info); // Configuring Network Information
}
}
bool repeating_timer_callback(struct repeating_timer *t)
{
DHCP_time_handler(); // DHCP 1s Tick Timer handler
return true;
}
bool precess_snmp_time_handle(struct repeating_timer *t)
{
SNMP_time_handler(); // SNMP 10ms Tick Timer handler
}
void UserLED_Init()
{
gpio_init(USER_LED_PIN); // Initialize LED
gpio_set_dir(USER_LED_PIN, GPIO_OUT); // Output mode
UserLED_Control_init(getUserLEDStatus, setUserLEDStatus); // Pass control control function
}
void getUserLEDStatus(void *ptr, uint8_t *len)
{
uint8_t led_status = 0;
led_status = gpio_get(USER_LED_PIN);
*len = sprintf((char *)ptr, "USER LED(green) [%s]. ", led_status ? "On" : "Off");
}
void setUserLEDStatus(int32_t val)
{
if (val == 0)
gpio_put(USER_LED_PIN, 0);
else
gpio_put(USER_LED_PIN, 1);
}
4.5 结果演示
- 烧录成功后,配置号串口的参数,”Windows+R“打开终端 。
- 按下复位键,串口打印出网络信息之后,在终端输入命令控制灯的亮灭。
- 如果发送命令出错,很有可能是未安装有Net-SNMP或者命令错误,如果串口能收到消息则表示成功控制了等。
5 注意事项
- 注意电脑要安装Net-SNMP软件,按默认选型安装后即可。
- 如果想用WIZnet的W5500来实现本章的示例,我们只需修改两个地方即可:
(1)在library/ioLibrary_Driver/Ethernet/下找到wizchip_conf.h这个头文件,将_WIZCHIP_ 宏定义修改为W5500。
(2)在library下找到CMakeLists.txt文件,将COMPILE_SEL设置为ON即可,OFF为W5100S,ON为W5500。
6 相关链接
想了解更多,评论留言哦!