net: more interface properties

This commit is contained in:
K Lange 2021-06-07 04:57:45 +09:00
parent 66ff044897
commit a0140f557b
3 changed files with 67 additions and 6 deletions

View File

@ -25,6 +25,23 @@ static void ip_ntoa(const uint32_t src_addr, char * out) {
(src_addr & 0xFF));
}
static char * flagsToStr(uint32_t flags) {
static char out[1024] = {0};
char * o = out;
#define FLAG(f) if (flags & IFF_ ## f) { \
if (o != out) o += sprintf(o,","); \
o += sprintf(o,"%s",#f); } \
FLAG(UP)
FLAG(BROADCAST)
FLAG(DEBUG)
FLAG(LOOPBACK)
FLAG(RUNNING)
FLAG(MULTICAST)
return out;
}
static int configure_interface(const char * if_name) {
char if_path[100];
@ -36,18 +53,28 @@ static int configure_interface(const char * if_name) {
return 1;
}
fprintf(stdout,"%s:\n", if_name); /* + flags? */
uint32_t flags = 0;
ioctl(netdev, 0x12340005, &flags);
uint32_t mtu = 0;
ioctl(netdev, 0x12340006, &mtu);
fprintf(stdout,"%s: flags=%d<%s> mtu %d\n", if_name, flags, flagsToStr(flags), mtu);
/* Get IPv4 address */
uint32_t ip_addr;
uint32_t ip_addr = 0;
if (!ioctl(netdev, 0x12340002, &ip_addr)) {
char ip_str[16];
ip_ntoa(ntohl(ip_addr), ip_str);
fprintf(stdout," inet %s", ip_str);
/* Netmask ? */
if (!ioctl(netdev, 0x12340004, &ip_addr)) {
ip_ntoa(ntohl(ip_addr), ip_str);
fprintf(stdout, " netmask %s", ip_str);
uint32_t netmask = 0;
if (!ioctl(netdev, 0x12340004, &netmask)) {
ip_ntoa(ntohl(netmask), ip_str);
fprintf(stdout, " netmask %s", ip_str);
uint32_t bcast = (ip_addr & netmask) | (~netmask);
ip_ntoa(ntohl(bcast), ip_str);
fprintf(stdout, " broadcast %s", ip_str);
}
fprintf(stdout,"\n");
}
@ -70,6 +97,10 @@ static int configure_interface(const char * if_name) {
mac_addr[3], mac_addr[4], mac_addr[5]);
}
/* TODO stats */
fprintf(stdout,"\n");
return 0;
}

View File

@ -111,6 +111,14 @@ extern int setsockopt(int sockfd, int level, int optname, const void *optval, so
extern int connect(int sockfd, const struct sockaddr * addr, socklen_t addrlen);
extern int shutdown(int sockfd, int how);
#define IFF_UP 0x0001
#define IFF_BROADCAST 0x0002
#define IFF_DEBUG 0x0004
#define IFF_LOOPBACK 0x0008
#define IFF_RUNNING 0x0010
#define IFF_MULTICAST 0x0020
_End_C_Header

View File

@ -23,9 +23,10 @@
#include <errno.h>
#include <kernel/arch/x86_64/irq.h>
#include <kernel/net/e1000.h>
#include <sys/socket.h>
#define INTS ((1 << 2) | (1 << 6) | (1 << 7) | (1 << 1) | (1 << 0))
struct e1000_nic {
@ -33,6 +34,8 @@ struct e1000_nic {
char if_name[32];
uint8_t mac[6];
size_t mtu;
/* XXX: just to get things going */
uint32_t ipv4_addr;
uint32_t ipv4_subnet;
@ -317,6 +320,23 @@ static int ioctl_e1000(fs_node_t * node, unsigned long request, void * argp) {
case 0x12340013:
memcpy(&nic->ipv6_addr, argp, sizeof(nic->ipv6_addr));
return 0;
case 0x12340005: {
uint32_t * flags = argp;
*flags = IFF_RUNNING;
if (nic->link_status) *flags |= IFF_UP;
/* We turn these on in our init_tx */
*flags |= IFF_BROADCAST;
*flags |= IFF_MULTICAST;
return 0;
}
case 0x12340006: {
uint32_t * mtu = argp;
*mtu = nic->mtu;
return 0;
}
default:
return -EINVAL;
}
@ -484,6 +504,8 @@ static void e1000_init(void * data) {
nic->device_node->selectwait = wait_e1000;
nic->device_node->device = nic;
nic->mtu = 1500; /* guess */
net_add_interface(nic->if_name, nic->device_node);
switch_task(0);