enet: Use const for pointers to receive buffer
This commit is contained in:
parent
50cb528a71
commit
cdba8db5e2
@ -39,6 +39,6 @@
|
|||||||
/**
|
/**
|
||||||
* Calculate the internet checksum according to RFC1071
|
* Calculate the internet checksum according to RFC1071
|
||||||
*/
|
*/
|
||||||
uint16_t inet_checksum(void *dataptr, uint16_t len);
|
uint16_t inet_checksum(const void *dataptr, uint16_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -3,15 +3,15 @@
|
|||||||
|
|
||||||
|
|
||||||
static uint16_t
|
static uint16_t
|
||||||
lwip_standard_chksum(void *dataptr, uint16_t len)
|
lwip_standard_chksum(const void *dataptr, uint16_t len)
|
||||||
{
|
{
|
||||||
uint32_t acc;
|
uint32_t acc;
|
||||||
uint16_t src;
|
uint16_t src;
|
||||||
uint8_t *octetptr;
|
const uint8_t *octetptr;
|
||||||
|
|
||||||
acc = 0;
|
acc = 0;
|
||||||
/* dataptr may be at odd or even addresses */
|
/* dataptr may be at odd or even addresses */
|
||||||
octetptr = (uint8_t*)dataptr;
|
octetptr = (const uint8_t*)dataptr;
|
||||||
while (len > 1) {
|
while (len > 1) {
|
||||||
/* declare first octet as most significant
|
/* declare first octet as most significant
|
||||||
thus assume network order, ignoring host order */
|
thus assume network order, ignoring host order */
|
||||||
@ -39,7 +39,7 @@ lwip_standard_chksum(void *dataptr, uint16_t len)
|
|||||||
/**
|
/**
|
||||||
* Calculate a short such that ret + dataptr[..] becomes 0
|
* Calculate a short such that ret + dataptr[..] becomes 0
|
||||||
*/
|
*/
|
||||||
uint16_t inet_checksum(void *dataptr, uint16_t len)
|
uint16_t inet_checksum(const void *dataptr, uint16_t len)
|
||||||
{
|
{
|
||||||
return ~lwip_standard_chksum(dataptr, len);
|
return ~lwip_standard_chksum(dataptr, len);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -162,9 +162,9 @@ struct ump_client {
|
|||||||
struct icmp_echo_reply_meta {
|
struct icmp_echo_reply_meta {
|
||||||
struct devq_buf rx_buf;
|
struct devq_buf rx_buf;
|
||||||
struct tx_alloc_queue_entry tx_entry;
|
struct tx_alloc_queue_entry tx_entry;
|
||||||
struct icmp_echo_hdr *icmp_echo_hdr;
|
const struct icmp_echo_hdr *icmp_echo_hdr;
|
||||||
struct eth_hdr *eth_hdr;
|
const struct eth_hdr *eth_hdr;
|
||||||
struct ip_hdr *ip_hdr;
|
const struct ip_hdr *ip_hdr;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct udp_recv {
|
struct udp_recv {
|
||||||
|
|||||||
@ -122,7 +122,7 @@ static void arp_reply (void *cb_arg, struct devq_buf *buf, void *vaddr) {
|
|||||||
st->arp.tx_queue_len--;
|
st->arp.tx_queue_len--;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void arp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr) {
|
static void arp_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr) {
|
||||||
ENET_DEBUG("Received ARP packet\n");
|
ENET_DEBUG("Received ARP packet\n");
|
||||||
|
|
||||||
if (buf->valid_length < sizeof(struct arp_hdr)) {
|
if (buf->valid_length < sizeof(struct arp_hdr)) {
|
||||||
@ -130,7 +130,7 @@ static void arp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_h
|
|||||||
rx_release(buf);
|
rx_release(buf);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
struct arp_hdr *arp_hdr = vaddr + buf->valid_data;
|
const struct arp_hdr *arp_hdr = vaddr + buf->valid_data;
|
||||||
uint16_t opcode = uint16_rd(arp_hdr->opcode);
|
uint16_t opcode = uint16_rd(arp_hdr->opcode);
|
||||||
if (
|
if (
|
||||||
uint16_rd(arp_hdr->hwtype) != ARP_HW_TYPE_ETH ||
|
uint16_rd(arp_hdr->hwtype) != ARP_HW_TYPE_ETH ||
|
||||||
@ -218,7 +218,7 @@ static void icmp_reply (void *cb_arg, struct devq_buf *buf, void *vaddr) {
|
|||||||
tx_send(buf);
|
tx_send(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void icmp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr, struct ip_hdr *ip_hdr) {
|
static void icmp_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr, const struct ip_hdr *ip_hdr) {
|
||||||
if (buf->valid_length < 4) {
|
if (buf->valid_length < 4) {
|
||||||
ENET_WARN("Received ICMP packet too small\n");
|
ENET_WARN("Received ICMP packet too small\n");
|
||||||
rx_release(buf);
|
rx_release(buf);
|
||||||
@ -231,7 +231,7 @@ static void icmp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t type = *(uint8_t*)(vaddr + buf->valid_data);
|
uint8_t type = *(const uint8_t*)(vaddr + buf->valid_data);
|
||||||
if (type == ICMP_ECHO) {
|
if (type == ICMP_ECHO) {
|
||||||
if (buf->valid_length < sizeof(struct icmp_echo_hdr)) {
|
if (buf->valid_length < sizeof(struct icmp_echo_hdr)) {
|
||||||
ENET_WARN("Received ICMP packet too small\n");
|
ENET_WARN("Received ICMP packet too small\n");
|
||||||
@ -239,7 +239,7 @@ static void icmp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct icmp_echo_hdr *icmp_echo_hdr = vaddr + buf->valid_data;
|
const struct icmp_echo_hdr *icmp_echo_hdr = vaddr + buf->valid_data;
|
||||||
ENET_DEBUG("Received ICMP echo, seq=%d\n", uint16_rd(icmp_echo_hdr->seqno));
|
ENET_DEBUG("Received ICMP echo, seq=%d\n", uint16_rd(icmp_echo_hdr->seqno));
|
||||||
|
|
||||||
struct icmp_echo_reply_meta *meta = simpleslab_alloc(&st->rx_meta_slab);
|
struct icmp_echo_reply_meta *meta = simpleslab_alloc(&st->rx_meta_slab);
|
||||||
@ -265,7 +265,7 @@ static void udp_recv_ump_callback (void *arg, struct ump_send_queue_entry *entry
|
|||||||
simpleslab_free(&st->rx_meta_slab, meta);
|
simpleslab_free(&st->rx_meta_slab, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t udp_checksum (struct ip_hdr *ip_hdr, struct udp_hdr *udp_hdr) {
|
static uint16_t udp_checksum (const struct ip_hdr *ip_hdr, const struct udp_hdr *udp_hdr) {
|
||||||
uint16_t udp_len = uint16_rd(udp_hdr->len);
|
uint16_t udp_len = uint16_rd(udp_hdr->len);
|
||||||
uint32_t chksum = inet_checksum(udp_hdr, udp_len) ^ 0x0000ffffUL;
|
uint32_t chksum = inet_checksum(udp_hdr, udp_len) ^ 0x0000ffffUL;
|
||||||
// add pseudo header
|
// add pseudo header
|
||||||
@ -280,13 +280,13 @@ static uint16_t udp_checksum (struct ip_hdr *ip_hdr, struct udp_hdr *udp_hdr) {
|
|||||||
return ~(uint16_t)chksum;
|
return ~(uint16_t)chksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void udp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr, struct ip_hdr *ip_hdr) {
|
static void udp_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr, const struct ip_hdr *ip_hdr) {
|
||||||
if (buf->valid_length < UDP_HLEN) {
|
if (buf->valid_length < UDP_HLEN) {
|
||||||
ENET_WARN("Received UDP packet too small\n");
|
ENET_WARN("Received UDP packet too small\n");
|
||||||
rx_release(buf);
|
rx_release(buf);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
struct udp_hdr *udp_hdr = vaddr + buf->valid_data;
|
const struct udp_hdr *udp_hdr = vaddr + buf->valid_data;
|
||||||
uint16_t udp_len = uint16_rd(udp_hdr->len);
|
uint16_t udp_len = uint16_rd(udp_hdr->len);
|
||||||
if (udp_len < UDP_HLEN || buf->valid_length < udp_len) {
|
if (udp_len < UDP_HLEN || buf->valid_length < udp_len) {
|
||||||
ENET_WARN("Received UDP packet too small\n");
|
ENET_WARN("Received UDP packet too small\n");
|
||||||
@ -305,7 +305,7 @@ static void udp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_h
|
|||||||
|
|
||||||
uint16_t src_port = uint16_rd(udp_hdr->src);
|
uint16_t src_port = uint16_rd(udp_hdr->src);
|
||||||
uint16_t dest_port = uint16_rd(udp_hdr->dest);
|
uint16_t dest_port = uint16_rd(udp_hdr->dest);
|
||||||
void *payload = (void*)udp_hdr + UDP_HLEN;
|
const void *payload = (const void*)udp_hdr + UDP_HLEN;
|
||||||
uint16_t payload_len = udp_len - UDP_HLEN;
|
uint16_t payload_len = udp_len - UDP_HLEN;
|
||||||
ENET_DEBUG("Received UDP packet from %d to %d, len %d\n", src_port, dest_port, payload_len);
|
ENET_DEBUG("Received UDP packet from %d to %d, len %d\n", src_port, dest_port, payload_len);
|
||||||
|
|
||||||
@ -336,13 +336,13 @@ static void udp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_h
|
|||||||
|
|
||||||
// IP: https://datatracker.ietf.org/doc/html/rfc791#section-3.1
|
// IP: https://datatracker.ietf.org/doc/html/rfc791#section-3.1
|
||||||
|
|
||||||
static void ip_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr) {
|
static void ip_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr) {
|
||||||
if (buf->valid_length < sizeof(struct ip_hdr)) {
|
if (buf->valid_length < sizeof(struct ip_hdr)) {
|
||||||
ENET_WARN("Received IP packet too small\n");
|
ENET_WARN("Received IP packet too small\n");
|
||||||
rx_release(buf);
|
rx_release(buf);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
struct ip_hdr *ip_hdr = vaddr + buf->valid_data;
|
const struct ip_hdr *ip_hdr = vaddr + buf->valid_data;
|
||||||
uint16_t ip_len = uint16_rd(ip_hdr->len);
|
uint16_t ip_len = uint16_rd(ip_hdr->len);
|
||||||
uint16_t header_len = IPH_HL(ip_hdr) * 4;
|
uint16_t header_len = IPH_HL(ip_hdr) * 4;
|
||||||
if (
|
if (
|
||||||
@ -434,8 +434,8 @@ static void write_eth_ip_header (
|
|||||||
static void rx_handle (struct devq_buf *buf) {
|
static void rx_handle (struct devq_buf *buf) {
|
||||||
struct region_entry *entry = enet_get_region(st->rxq, buf->rid);
|
struct region_entry *entry = enet_get_region(st->rxq, buf->rid);
|
||||||
assert(entry != NULL);
|
assert(entry != NULL);
|
||||||
void *vaddr = (void*)entry->mem.vbase + buf->offset;
|
const void *vaddr = (void*)entry->mem.vbase + buf->offset;
|
||||||
void *eth_vaddr = vaddr + buf->valid_data;
|
const void *eth_vaddr = vaddr + buf->valid_data;
|
||||||
|
|
||||||
#if defined(ENET_DEBUG_OPTION)
|
#if defined(ENET_DEBUG_OPTION)
|
||||||
debug_printf("Received Packet of size %lu:", buf->valid_length);
|
debug_printf("Received Packet of size %lu:", buf->valid_length);
|
||||||
@ -451,7 +451,7 @@ static void rx_handle (struct devq_buf *buf) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct eth_hdr *eth_hdr = eth_vaddr;
|
const struct eth_hdr *eth_hdr = eth_vaddr;
|
||||||
uint16_t type = uint16_rd(eth_hdr->type);
|
uint16_t type = uint16_rd(eth_hdr->type);
|
||||||
|
|
||||||
buf->valid_data += ETH_HLEN;
|
buf->valid_data += ETH_HLEN;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user