声明:原创作品,严禁用于商业目的。 本系列文章将全面剖析以Bcm33xxx芯片开发Cablemodem产品的SDK源码为例,从编译系统到各个功能模块进行分析与探讨。
文章目录
0.写在前篇
本系列文章来自于博通公司相关项目开发中的学习资料和开发总结,是企业或者公司项目开发过程中必备的网络相关知识,主要涉及到ecos和linux系统编译、内存管理、接口管理、HAL抽象层、以及IPV6、DHCP、TR069等网络相关协议。为维护版权和相关知识产权,请购买官方SDK和相关服务,此系列文章仅为个人学习使用,如有不妥之处和技术相关知识,请私信留言!
博通Broadcom SDK源码学习与开发1——SDK源码探究与Cable Modem 系统编译
博通Broadcom SDK源码学习与开发2——Bootloader功能和编译过程
博通Broadcom SDK源码学习与开发3——Cable Modem Docsis3.0
博通Broadcom SDK源码学习与开发4——ECOS系统数据流
博通Broadcom SDK源码学习与开发5——ECOS系统层剖析
博通Broadcom SDK源码学习与开发6——支持Linux系统
博通Broadcom SDK源码学习与开发7——HAL硬件抽象层分析
博通Broadcom SDK源码学习与开发8——内存与参数管理
博通Broadcom SDK源码学习与开发9——Interface接口管理
博通Broadcom SDK源码学习与开发10——Cable Modem IPv6地址
博通Broadcom SDK源码学习与开发11——Cable Modem DHCP管理
博通Broadcom SDK源码学习与开发12终结篇——TR069网管协议
1. 内存管理
1.1 源码介绍
源文件路径 : REV/rbb_cm_src/Bfc/BcmHeapManager/
主要功能:REV/rbb_cm_src/Bfc/BcmHeapManager/BcmHeapManager.c 重新实现了malloc/free/realloc,区别于ecos系统。
更多技术细节可以参考博文:支持Linux系统
1.2 基本方法
动态不定长内存分配
使用双向链表管理内存节点.一个为空闲内存链表,一个为已分配内存链表。
主要数据结构和变量
内存节点
typedef struct _BcmMemoryNode
{
struct _BcmMemoryNode *pNextNode;
struct _BcmMemoryNode *pPreviousNode;
uint32 memorySizeBytes; //head len + actual len
#if (BCM_HEAP_BOUNDS_CHECK)//默认关闭
uint32 requestedSizeBytes; //actual len
#endif
#if (BCM_HEAP_THREAD_TRACKING)//默认关闭
/// This is the id of the thread that allocated or freed the block of memory.
/// When a node is freed and then merged with other nodes, this information
/// is lost.
unsigned long threadId;
#endif
#if ( BCM_HEAP_MEMORY_LEAK_DEBUG ) //默认关闭
uint32 allocateTime;
#if ( BCM_HEAP_MEMORY_LEAK_CALLSTACK_SIZE )
void* callstack[BCM_HEAP_MEMORY_LEAK_CALLSTACK_SIZE];
#endif
#endif
} BcmMemoryNode;
//内存块
/// PR9188 - Used to keep track of memory regions controlled by the heap
/// manager. There will always be at least 1 region when BcmHeapInitialize()
/// is called, and calling BcmHeapAddMemory() will add more regions.
typedef struct
{
/// The starting address of the region.
void *pRegionStart;
/// The ending address of the region.
void *pRegionEnd;
/// The ending address of the region, backed up by the size of a memory
/// node.
void *pRegionEndWithOverhead;
/// The size of the region, in bytes.
unsigned int regionSize;
} MemoryRegion;
主要全局变量如下:
static MemoryRegion gMemoryRegions[kMaxMemoryRegions] = {{0}};
static unsigned int gMemoryRegionIndex = 0;
// This is the head of the list of available memory.
static BcmMemoryNode *pgFreeMemory = NULL;
// This is the head of the list of allocated memory.
static BcmMemoryNode *pgAllocatedMemory = NULL;
1.3 内存初始化流程

1.4 相关CLI命令
提供了一些Cmd可用于调试:
CM>HeapManager
bcheck bcheck_crash bcheck_enable last_error maxAlloc
memShow options stats threadUsage time
trace walk walk_alloc walk_alloc_time
bcheck #检查内存是否有错误,内存节点指针/大小...的合法性
last_error #显示management的错误号
maxAlloc #显示最大可用内存节点大小
memShow #显示内存总大小/可用/已用大小情况
options #显示目前打开的Management调试选项
stats #内存的一些信息,区块大小/节点个数...
threadUsage#查看所有thread的内存使用情况
time #查看系统时间,walk_alloc_time时有用
trace #追踪指定thread的内存使用过程
walk #查看当前可用内存节点
walk_alloc #walk_alloc taskid,查看指定thread的内存使用情况
walk_alloc_time #walk_alloc_time tastid starttime endtime <callstack>,查看指定thread的在starttime~endtime时间内的内存使用情况,并可查看其callstack
2. 参数管理
在flash上有“permanent"和"dynamic"2块用于存储参数的区块。2块区域有相同的参数格式,但顾名思义,dynamic区块用于存储系统运行过程中的参数, 而permanent区块即起到NVRAM的作用,同于保存出厂配置参数,Vendor/MAC/WiFi参数等。
可以参考文档: AN-V2 Software Non-Volatile Settings for DOCSIS Applications
2.1 Flash分区
REV/rbb_cm_src/Bfc/Target/FlashDriver/FlashDeviceDriver.h
typedef enum
{
kBootloaderRegion = 0,
kImage1Region,
kImage2Region,
kImage3Region,
kImageLinuxAppsRegion,
kPermanentNonVolRegion,
kDhtmlRegion,
kDynamicNonVolRegion,
kNumberOfFlashRegions
} FlashRegion;
2.2 源码文件
NonVolDevice.cpp
NonVolSettings.cpp
NonVolDeviceDriverBridge.cpp
NonVolDeviceDriver.c
2.3 相关CLI命令操作
(1)现有参数的get/set已在console中提供了命令 查看已实现的参数group:
CM> cd non-vol
CM/NonVol> ?
[802.11] [802.11Srom] [avs] [bfcApp] [bpi] [cm_aux_sw] [docsis] [docsis30]
[ds-cal] [eRouter] [emtaConfig] [emtaConfigVoice] [emtaSecurity] [emtaSnmp]
[event_log] [factory] [halif] [ipsec] [propane] [snmp_cm] [system_event_log]
[us-cal] [userif] [vendor]
(2)查看其中halif的参数
CM/NonVol> cd halif
CM/NonVol/> ?
3420_optimized_map DS_AGI DS_AGT STABW1
STABW2 STAGI STAGT STATHR STPGA1
bcmalloc bcmalloc_numSizes board_rev
bt_master_enable chan_plan_annex_b clear_device
cm_1024qam cm_20_clipping cm_45 cm_45i cm_aboard_v2
cm_aboard_v3 cm_adv_map_runahead cm_amp cm_annex
cm_cboard cm_clc cm_diplexer cm_ds_ref_freq cm_iboard_v1a
cm_iboard_v1b cm_phy_ref_freq cm_sboard cm_tuner
cm_upstream_priority_queue_support cm_us_ref_freq
cm_us_sample_freq cm_wideband_tuner defaults
del_extra_data eth_auto_power_down eth_auto_power_down_enable
eth_auto_power_down_sleep_timer eth_auto_power_down_wakeup_timer
eth_autoneg eth_duplex_full eth_reject_cam eth_speed fap_bypass
{flush_unrecognized} fpm_token_depletion_wdog_enable high_output_pa
hpna_message_level interfaces local_bt_mac_address
mac_address manufactured num_enabled_tuners
power_save_enable read recover_space
remote_bt_mac_address scdma_code_matrix shack_tries
show spreader_scale stats tftp_read tftp_write
usb_device_id usb_mac_address usb_rndis usb_vendor_id wdog_enable
wdog_timeout write zone
(3)查看和修改IP Stack5的mac
CM/NonVol/HalIf NonVol> mac_address 5
MAC address for IP stack 5 is 00:10:18:de:ad:05
CM/NonVol/HalIf NonVol> mac_address 5 00:10:18:de:ad:23
NOTE: You must write the settings and reboot for the change to take effect!
CM/NonVol/HalIf NonVol> write perm ###修改后需保存至permanent,否则不会保存到flash
2.4 注意
如果"NVRAM"中没有配置或者获取配置失败,系统无法启动
BfcSystem.cpp文件:
if(ReadNonVolSection()==false){...}
HalIf MAC factory setting
使用REV/rbb_cm_src/Bfc/Custom/BfcNonVolDefaults.h中的default
3. 参数配置管理
1)reset操作:
dynamic数据恢复默认设置:web操作 Status->Security 页面 ”Restore Factory Defaults“ 选中 “Yes” 后按"Apply"按钮。
form处理过程:
VendorCgiHelper.cpp 中的 ProcessCgiRequest 调用各BcmNonVolSettings子类的ResetDefaults,将内存中的值恢复为默认值,然后再写到flash
2)Save 操作
备份dynamic数据: web操作 Basic->Backup 页面 按"Backup"按钮后 GET /GatewaySettings.bin文件。
form处理过程:
调用HttpRgExportConfigFormHandler.cpp MyHtml() 生成GatewaySettings.bin文件
GatewaySettings.bin文件格式: MD5value + Header + MIB Groups
16 bytes MD5value: header和MIB Groups数据的MD5码
Header: header string + major version + minor version + export setting size
header string/ major version/minor version 使用宏定义 “6u9e9ewf0jt9y85w690je4669jye4d-056t9p48jp4ee6u9ee659jy9e-54e4j6r0j069k-056” “1” “2”
4 bytes export settings size, 包含header和MIB Groups长度
MIB Groups
Group: 2 bytes groupLength + 4 bytes Magic Number + major version + minor version + {App settings data…}
Magic Number: This is an id (magic number) that is unique to the application. It lets the app determine whether or not the non-vol settings were written by this application or a different one. This is necessary because the CRC may check out correctly, and the version number may seem reasonable, but the structure of the settings may not match this app, e.g. the SNMP group uses the ASCII hex representation of “snmp”, which is 0x736e6d70.
3)Restore 操作
dynamic数据使用Config设置:web操作 Basic->Backup 页面 "Browse…"选择config文件后, 后按"Restore"按钮
form处理过程:
HttpFormHandlerRgBackup.cpp ProcessForm 分析数据:
(1)check MD5value and Header
(2)处理MIB Groups数据, 通过Group Magic Number 找到对应 NonVolsetting, 使用 NonVolsetting 的 ResetDefaults, 再调用 ReadFromImpl 读取settings data
(3)循环处理完所有Groups后, write to flash and reboot。
本文档详细介绍了博通Broadcom SDK中内存管理的源码分析,包括内存分配方法、数据结构和初始化流程,并提及相关CLI命令。同时,阐述了参数管理的Flash分区、源码文件、CLI命令操作,强调了动态和永久参数存储的重要性。

被折叠的 条评论
为什么被折叠?



