Commit 677c4ca4 authored by Alessandro Rubini's avatar Alessandro Rubini

net: move to caller-based allocation

We removed the pool of sockets, to only keep a list of pointers.
The caller provides the socket (and the buffer).

This saves from 440 to 1570 bytes in the binary (more in the ones
with no CONFIG_IP, beause sockets are not used.

Now we can move to a caller-defined queue size, when I find the time and
willingness.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 9c0cfdfe
......@@ -44,7 +44,6 @@ struct sockq {
};
struct wrpc_socket {
int in_use;
struct wr_sockaddr bind_addr;
mac_addr_t local_mac;
......@@ -78,8 +77,9 @@ PACKED struct wr_timestamp {
// to bind_addr. If PTPD_FLAG_MULTICAST is set, the socket is
// automatically added to multicast group. User can specify
// physical_port field to bind the socket to specific switch port only.
struct wrpc_socket *ptpd_netif_create_socket(int unused, int unused2,
struct wr_sockaddr * bind_addr);
struct wrpc_socket *ptpd_netif_create_socket(struct wrpc_socket *s,
int unused, int unused2,
struct wr_sockaddr * bind_addr);
// Sends a UDP/RAW packet (data, data_length) to addr in wr_sockaddr.
// For raw frames, mac/ethertype needs to be provided, for UDP - ip/port.
......
......@@ -16,6 +16,7 @@
#define htons(x) x
#endif
static struct wrpc_socket __static_arp_socket;
static struct wrpc_socket *arp_socket;
#define ARP_HTYPE 0
......@@ -38,7 +39,8 @@ void arp_init(void)
memset(&saddr.mac, 0xFF, 6); /* Broadcast */
saddr.ethertype = htons(0x0806); /* ARP */
arp_socket = ptpd_netif_create_socket(0, 0 /* both unused */, &saddr);
arp_socket = ptpd_netif_create_socket(&__static_arp_socket,
0, 0 /* both unused */, &saddr);
}
static int process_arp(uint8_t * buf, int len)
......
......@@ -20,6 +20,7 @@
int needIP = 1;
static uint8_t myIP[4];
static struct wrpc_socket __static_ipv4_socket;
static struct wrpc_socket *ipv4_socket;
unsigned int ipv4_checksum(unsigned short *buf, int shorts)
......@@ -49,7 +50,8 @@ void ipv4_init(void)
get_mac_addr(&saddr.mac[0]); /* Unicast */
saddr.ethertype = htons(0x0800); /* IPv4 */
ipv4_socket = ptpd_netif_create_socket(0, 0 /* both unused */, &saddr);
ipv4_socket = ptpd_netif_create_socket(&__static_ipv4_socket,
0, 0 /* both unused */, &saddr);
}
static int bootp_retry = 0;
......
......@@ -31,7 +31,7 @@ struct ethhdr {
uint16_t ethtype;
};
static struct wrpc_socket socks[NET_MAX_SOCKETS];
static struct wrpc_socket *socks[NET_MAX_SOCKETS];
//#define net_verbose pp_printf
int ptpd_netif_get_hw_addr(struct wrpc_socket *sock, mac_addr_t *mac)
......@@ -45,28 +45,27 @@ void ptpd_netif_set_phase_transition(uint32_t phase)
{
int i;
for (i=0; i< NET_MAX_SOCKETS; ++i) {
socks[i].phase_transition = phase;
for (i=0; i< ARRAY_SIZE(socks); ++i) {
socks[i]->phase_transition = phase;
}
}
struct wrpc_socket *ptpd_netif_create_socket(int unused, int unusd2,
struct wr_sockaddr * bind_addr)
struct wrpc_socket *ptpd_netif_create_socket(struct wrpc_socket *sock,
int unused, int unusd2,
struct wr_sockaddr * bind_addr)
{
int i;
struct hal_port_state pstate;
struct wrpc_socket *sock;
/* Look for the first available socket. */
for (sock = NULL, i = 0; i < NET_MAX_SOCKETS; i++)
if (!socks[i].in_use) {
sock = &socks[i];
for (i = 0; i < ARRAY_SIZE(socks); i++)
if (!socks[i]) {
socks[i] = sock;
break;
}
if (!sock) {
net_verbose("No sockets left.\n");
if (i == ARRAY_SIZE(socks)) {
pp_printf("%s: no socket slots left\n", __func__);
return NULL;
}
......@@ -85,15 +84,16 @@ struct wrpc_socket *ptpd_netif_create_socket(int unused, int unusd2,
sock->queue.head = sock->queue.tail = 0;
sock->queue.avail = NET_SKBUF_SIZE;
sock->queue.n = 0;
sock->in_use = 1;
return sock;
}
int ptpd_netif_close_socket(struct wrpc_socket *s)
{
if (s)
s->in_use = 0;
int i;
for (i = 0; i < ARRAY_SIZE(socks); i++)
if (socks[i] == s)
socks[i] = NULL;
return 0;
}
......@@ -300,12 +300,12 @@ void update_rx_queues()
if (recvd <= 0) /* No data received? */
return;
for (i = 0; i < NET_MAX_SOCKETS; i++) {
s = &socks[i];
if (s->in_use && !memcmp(hdr.dstmac, s->bind_addr.mac, 6)
for (i = 0; i < ARRAY_SIZE(socks); i++) {
s = socks[i];
if (s && !memcmp(hdr.dstmac, s->bind_addr.mac, 6)
&& hdr.ethtype == s->bind_addr.ethertype)
break; /*they match */
s = NULL;
s = NULL; /* may be non-null but not matching */
}
if (!s) {
......
ppsi @ f68c1ad4
Subproject commit b20f86cdf1d97bcf1ecd46411e25bdd02db3fc33
Subproject commit f68c1ad458fe4bbef0d41dd32b43bab1f406538c
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment