Skip to content

Commit 36e89eb

Browse files
committed
add: backplane NIC info implementation
1 parent 4a93082 commit 36e89eb

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.vscode
2+
/.cache
23
/build
34
*.bkp
45
/doc/*.ods#

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ project(mmc-mailbox VERSION 1.0.2)
55
include(GNUInstallDirs)
66
include(CMakePackageConfigHelpers)
77

8+
set(CMAKE_C_STANDARD 11)
9+
set(CMAKE_C_STANDARD_REQUIRED TRUE)
10+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
11+
812
# mmcmb library
913

1014
add_library(mmcmb SHARED mmcmb.c)

mmcctrld.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@
1111
***************************************************************************/
1212

1313
#include <errno.h>
14+
#include <net/if.h>
15+
#include <netinet/in.h>
1416
#include <signal.h>
1517
#include <stdbool.h>
1618
#include <stdio.h>
1719
#include <stdlib.h>
1820
#include <string.h>
21+
#include <sys/ioctl.h>
22+
#include <sys/socket.h>
1923
#include <sys/stat.h>
2024
#include <sys/types.h>
2125
#include <syslog.h>
2226
#include <time.h>
2327
#include <unistd.h>
28+
#include <ifaddrs.h>
2429

30+
#include "mmcmb/fpga_mailbox_layout.h"
2531
#include "mmcmb/mmcmb.h"
2632

2733
// Poll FPGA control register 4 times per second.
@@ -93,6 +99,48 @@ void handle_fpga_ctrl(const mb_fpga_ctrl_t* ctrl)
9399
terminate = true;
94100
}
95101

102+
mb_nic_information_t get_nic_info(const char* ifname)
103+
{
104+
mb_nic_information_t result = {0};
105+
106+
// Get MAC address via ioctl
107+
int fd = socket(AF_INET, SOCK_DGRAM, 0);
108+
if (fd > 0) {
109+
struct ifreq ifr;
110+
ifr.ifr_addr.sa_family = AF_INET;
111+
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
112+
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
113+
const uint8_t* mac_addr = (const uint8_t*)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
114+
memcpy(result.mac_addr, mac_addr, sizeof(result.mac_addr));
115+
} else {
116+
syslog(LOG_ERR, "Could not get MAC address of %s: %s", ifname, strerror(errno));
117+
}
118+
close(fd);
119+
}
120+
121+
// Get primary IPv4 & IPv6 addresses via getifaddrs()
122+
struct ifaddrs *ifap, *ifa;
123+
getifaddrs(&ifap);
124+
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
125+
if (ifa->ifa_addr && !strcmp(ifa->ifa_name, ifname)) {
126+
switch (ifa->ifa_addr->sa_family) {
127+
case AF_INET: {
128+
const struct sockaddr_in* sa = (struct sockaddr_in*)ifa->ifa_addr;
129+
memcpy(result.ipv4_addr, &sa->sin_addr, sizeof(result.ipv4_addr));
130+
} break;
131+
case AF_INET6: {
132+
const struct sockaddr_in6* sa = (struct sockaddr_in6*)ifa->ifa_addr;
133+
memcpy(result.ipv6_addr, sa->sin6_addr.s6_addr, sizeof(result.ipv6_addr));
134+
} break;
135+
default:
136+
break;
137+
}
138+
}
139+
}
140+
141+
return result;
142+
}
143+
96144
int main()
97145
{
98146
if (geteuid() != 0) {
@@ -122,6 +170,11 @@ int main()
122170
goto finish;
123171
}
124172

173+
const char* bp_eth_ifname = getenv("BP_ETH_IFNAME");
174+
if (!bp_eth_ifname) {
175+
bp_eth_ifname = "eth0";
176+
}
177+
125178
const struct timespec ts_poll = {
126179
.tv_nsec = POLL_INTERVAL_MS * 1e6,
127180
};
@@ -135,6 +188,10 @@ int main()
135188
break;
136189
}
137190
handle_fpga_ctrl(&ctrl);
191+
192+
mb_nic_information_t nic_info = get_nic_info(bp_eth_ifname);
193+
mb_set_bp_eth_info(&nic_info);
194+
138195
nanosleep(&ts_poll, NULL);
139196
}
140197

mmcmb.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* *
1111
***************************************************************************/
1212

13+
#include "mmcmb/mmcmb.h"
14+
1315
#include <dirent.h>
1416
#include <errno.h>
1517
#include <fcntl.h>
@@ -207,3 +209,8 @@ bool mb_set_fpga_status(const mb_fpga_status_t* stat)
207209
{
208210
return mb_write_at(MB_EEPROM_OFFS(fpga_status), stat, sizeof(*stat));
209211
}
212+
213+
bool mb_set_bp_eth_info(const mb_nic_information_t* nic_info)
214+
{
215+
return mb_write_at(MB_EEPROM_OFFS(bp_eth_info), nic_info, sizeof(*nic_info));
216+
}

mmcmb/fpga_mailbox_layout.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <assert.h>
1616
#include <stdint.h>
17+
#include <stddef.h>
1718

1819
#define MB_PACKED __attribute__((packed)) __attribute__((scalar_storage_order("little-endian")))
1920

@@ -112,7 +113,7 @@ typedef struct mb_memory_contents {
112113
mb_mmc_information_t mmc_information;
113114
mb_mmc_sensor_t mmc_sensor[40];
114115
uint8_t reserved[43];
115-
mb_nic_information_t bp_eth_information;
116+
mb_nic_information_t bp_eth_info;
116117
mb_fpga_ctrl_t fpga_ctrl;
117118
mb_fpga_status_t fpga_status;
118119
} MB_PACKED mb_memory_contents_t;
@@ -147,6 +148,6 @@ static_assert(MB_EEPROM_OFFS(application_data) == 1032, "Offset of application_d
147148
static_assert(MB_EEPROM_OFFS(mmc_information) == 1288, "Offset of mmc_information incorrect");
148149
static_assert(MB_EEPROM_OFFS(mmc_sensor) == 1336, "Offset of mmc_sensor incorrect");
149150
static_assert(MB_EEPROM_OFFS(reserved) == 1976, "Offset of reserved incorrect");
150-
static_assert(MB_EEPROM_OFFS(bp_eth_information) == 2019, "Offset of NIC information incorrect");
151+
static_assert(MB_EEPROM_OFFS(bp_eth_info) == 2019, "Offset of NIC information incorrect");
151152
static_assert(MB_EEPROM_OFFS(fpga_ctrl) == 2045, "Offset of fpga_ctrl incorrect");
152153
static_assert(MB_EEPROM_OFFS(fpga_status) == 2046, "Offset of fpga_status incorrect");

mmcmb/mmcmb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
extern "C" {
1717
#endif
1818

19+
#include <stdbool.h>
1920
#include <stddef.h>
2021

2122
#include "fpga_mailbox_layout.h"
@@ -46,6 +47,9 @@ bool mb_get_fpga_ctrl(mb_fpga_ctrl_t* ctrl);
4647
// Set FPGA status
4748
bool mb_set_fpga_status(const mb_fpga_status_t* stat);
4849

50+
// Set Backplane NIC addresses
51+
bool mb_set_bp_eth_info(const mb_nic_information_t* nic_info);
52+
4953
// Get mmc-mailbox "EEPROM" device path, returns NULL on error
5054
const char* mb_get_eeprom_path(void);
5155

0 commit comments

Comments
 (0)