net/devif: reuse devif_send() for can/pkt/icmp stack

Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
chao an 2023-01-11 13:57:05 +08:00 committed by Xiang Xiao
parent bcb0abc05d
commit 0cbbbb9215
8 changed files with 35 additions and 264 deletions

View File

@ -106,7 +106,7 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev,
{ {
/* Copy the packet data into the device packet buffer and send it */ /* Copy the packet data into the device packet buffer and send it */
devif_can_send(dev, pstate->snd_buffer, pstate->snd_buflen); devif_send(dev, pstate->snd_buffer, pstate->snd_buflen, 0);
if (dev->d_sndlen == 0) if (dev->d_sndlen == 0)
{ {
return flags; return flags;

View File

@ -49,15 +49,6 @@ ifeq ($(CONFIG_MM_IOB),y)
NET_CSRCS += devif_poll.c NET_CSRCS += devif_poll.c
NET_CSRCS += devif_iobsend.c NET_CSRCS += devif_iobsend.c
# Raw packet socket support
ifeq ($(CONFIG_NET_PKT),y)
NET_CSRCS += devif_pktsend.c
endif
ifeq ($(CONFIG_NET_CAN),y)
NET_CSRCS += devif_cansend.c
endif
endif endif
# Include network device interface build support # Include network device interface build support

View File

@ -482,48 +482,6 @@ void devif_iob_send(FAR struct net_driver_s *dev, FAR struct iob_s *buf,
unsigned int target_offset); unsigned int target_offset);
#endif #endif
/****************************************************************************
* Name: devif_pkt_send
*
* Description:
* Called from socket logic in order to send a raw packet in response to
* an xmit or poll request from the network interface driver.
*
* This is almost identical to calling devif_send() except that the data to
* be sent is copied into dev->d_buf (vs. dev->d_appdata), since there is
* no header on the data.
*
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/
#if defined(CONFIG_NET_PKT)
void devif_pkt_send(FAR struct net_driver_s *dev, FAR const void *buf,
unsigned int len);
#endif
/****************************************************************************
* Name: devif_can_send
*
* Description:
* Called from socket logic in order to send a raw packet in response to
* an xmit or poll request from the network interface driver.
*
* This is almost identical to calling devif_send() except that the data to
* be sent is copied into dev->d_buf (vs. dev->d_appdata), since there is
* no header on the data.
*
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/
#if defined(CONFIG_NET_CAN)
void devif_can_send(FAR struct net_driver_s *dev, FAR const void *buf,
unsigned int len);
#endif
/**************************************************************************** /****************************************************************************
* Name: devif_out * Name: devif_out
* *

View File

@ -1,91 +0,0 @@
/****************************************************************************
* net/devif/devif_cansend.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/net/netdev.h>
#if defined(CONFIG_NET_CAN)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: devif_can_send
*
* Description:
* Called from socket logic in order to send a can packet in response to
* an xmit or poll request from the network interface driver.
*
* This is almost identical to calling devif_send() except that the data to
* be sent is copied into dev->d_buf (vs. dev->d_appdata), since there is
* no header on the data.
*
* Assumptions:
* Called with the network locked.
*
****************************************************************************/
void devif_can_send(FAR struct net_driver_s *dev, FAR const void *buf,
unsigned int len)
{
unsigned int limit = NETDEV_PKTSIZE(dev) - NET_LL_HDRLEN(dev);
if (dev == NULL || len == 0 || len > limit)
{
nerr("ERROR: devif_pkt_send fail: %p, sndlen: %u, pktlen: %u\n",
dev, len, limit);
return;
}
iob_update_pktlen(dev->d_iob, 0);
/* Copy the data into the device packet buffer and set the number of
* bytes to send
*/
if (len <= iob_navail(false) * CONFIG_IOB_BUFSIZE)
{
dev->d_sndlen = iob_trycopyin(dev->d_iob, buf, len, 0, false);
}
else
{
dev->d_sndlen = 0;
}
if (dev->d_sndlen != len)
{
netdev_iob_release(dev);
dev->d_sndlen = 0;
}
dev->d_len = dev->d_sndlen;
}
#endif /* CONFIG_NET_CAN */

View File

@ -1,91 +0,0 @@
/****************************************************************************
* net/devif/devif_pktsend.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/net/netdev.h>
#ifdef CONFIG_NET_PKT
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: devif_pkt_send
*
* Description:
* Called from socket logic in order to send a raw packet in response to
* an xmit or poll request from the network interface driver.
*
* This is almost identical to calling devif_send() except that the data to
* be sent is copied into dev->d_buf (vs. dev->d_appdata), since there is
* no header on the data.
*
* Assumptions:
* Called with the network locked.
*
****************************************************************************/
void devif_pkt_send(FAR struct net_driver_s *dev, FAR const void *buf,
unsigned int len)
{
unsigned int limit = NETDEV_PKTSIZE(dev) - NET_LL_HDRLEN(dev);
if (dev == NULL || len == 0 || len > limit)
{
nerr("ERROR: devif_pkt_send fail: %p, sndlen: %u, pktlen: %u\n",
dev, len, limit);
return;
}
iob_update_pktlen(dev->d_iob, 0);
/* Copy the data into the device packet buffer and set the number of
* bytes to send
*/
if (len <= iob_navail(false) * CONFIG_IOB_BUFSIZE)
{
dev->d_sndlen = iob_trycopyin(dev->d_iob, buf, len, 0, false);
}
else
{
dev->d_sndlen = 0;
}
if (dev->d_sndlen != len)
{
netdev_iob_release(dev);
dev->d_sndlen = 0;
}
dev->d_len = dev->d_sndlen;
}
#endif /* CONFIG_NET_PKT */

View File

@ -99,6 +99,14 @@ static void sendto_request(FAR struct net_driver_s *dev,
{ {
FAR struct icmp_hdr_s *icmp; FAR struct icmp_hdr_s *icmp;
/* Set-up to send that amount of data. */
devif_send(dev, pstate->snd_buf, pstate->snd_buflen, IPv4_HDRLEN);
if (dev->d_sndlen != pstate->snd_buflen)
{
return;
}
IFF_SET_IPv4(dev->d_flags); IFF_SET_IPv4(dev->d_flags);
/* The total length to send is the size of the application data plus the /* The total length to send is the size of the application data plus the
@ -107,29 +115,20 @@ static void sendto_request(FAR struct net_driver_s *dev,
dev->d_len = IPv4_HDRLEN + pstate->snd_buflen; dev->d_len = IPv4_HDRLEN + pstate->snd_buflen;
/* The total size of the data (including the size of the ICMP header) */
dev->d_sndlen += pstate->snd_buflen;
/* Copy the ICMP header and payload into place after the IPv4 header */
icmp = IPBUF(IPv4_HDRLEN);
iob_update_pktlen(dev->d_iob, IPv4_HDRLEN);
iob_copyin(dev->d_iob, pstate->snd_buf,
pstate->snd_buflen, IPv4_HDRLEN, false);
/* Initialize the IP header. */ /* Initialize the IP header. */
ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_ICMP, ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_ICMP,
&dev->d_ipaddr, &pstate->snd_toaddr, &dev->d_ipaddr, &pstate->snd_toaddr,
IP_TTL_DEFAULT, NULL); IP_TTL_DEFAULT, NULL);
/* Copy the ICMP header and payload into place after the IPv4 header */
icmp = IPBUF(IPv4_HDRLEN);
/* Calculate the ICMP checksum. */ /* Calculate the ICMP checksum. */
icmp->icmpchksum = 0; icmp->icmpchksum = 0;
icmp->icmpchksum = ~icmp_chksum_iob(dev->d_iob); icmp->icmpchksum = ~icmp_chksum_iob(dev->d_iob);
if (icmp->icmpchksum == 0) if (icmp->icmpchksum == 0)
{ {
icmp->icmpchksum = 0xffff; icmp->icmpchksum = 0xffff;
@ -204,8 +203,11 @@ static uint16_t sendto_eventhandler(FAR struct net_driver_s *dev,
ninfo("Send ICMP request\n"); ninfo("Send ICMP request\n");
sendto_request(dev, pstate); sendto_request(dev, pstate);
pstate->snd_result = OK; if (dev->d_sndlen > 0)
goto end_wait; {
pstate->snd_result = OK;
goto end_wait;
}
} }
/* Continue waiting */ /* Continue waiting */

View File

@ -98,6 +98,14 @@ static void sendto_request(FAR struct net_driver_s *dev,
{ {
FAR struct icmpv6_echo_request_s *icmpv6; FAR struct icmpv6_echo_request_s *icmpv6;
/* Set-up to send that amount of data. */
devif_send(dev, pstate->snd_buf, pstate->snd_buflen, IPv6_HDRLEN);
if (dev->d_sndlen != pstate->snd_buflen)
{
return;
}
IFF_SET_IPv6(dev->d_flags); IFF_SET_IPv6(dev->d_flags);
/* The total length to send is the size of the application data plus the /* The total length to send is the size of the application data plus the
@ -106,21 +114,12 @@ static void sendto_request(FAR struct net_driver_s *dev,
dev->d_len = IPv6_HDRLEN + pstate->snd_buflen; dev->d_len = IPv6_HDRLEN + pstate->snd_buflen;
/* The total size of the data (including the size of the ICMPv6 header) */
dev->d_sndlen += pstate->snd_buflen;
ipv6_build_header(IPv6BUF, pstate->snd_buflen, IP_PROTO_ICMP6, ipv6_build_header(IPv6BUF, pstate->snd_buflen, IP_PROTO_ICMP6,
dev->d_ipv6addr, pstate->snd_toaddr.s6_addr16, 255); dev->d_ipv6addr, pstate->snd_toaddr.s6_addr16, 255);
/* Copy the ICMPv6 request and payload into place after the IPv6 header */ /* Copy the ICMPv6 request and payload into place after the IPv6 header */
icmpv6 = IPBUF(IPv6_HDRLEN); icmpv6 = IPBUF(IPv6_HDRLEN);
iob_update_pktlen(dev->d_iob, IPv6_HDRLEN);
iob_copyin(dev->d_iob, pstate->snd_buf,
pstate->snd_buflen, IPv6_HDRLEN, false);
/* Calculate the ICMPv6 checksum over the ICMPv6 header and payload. */ /* Calculate the ICMPv6 checksum over the ICMPv6 header and payload. */
@ -200,8 +199,11 @@ static uint16_t sendto_eventhandler(FAR struct net_driver_s *dev,
ninfo("Send ICMPv6 ECHO request\n"); ninfo("Send ICMPv6 ECHO request\n");
sendto_request(dev, pstate); sendto_request(dev, pstate);
pstate->snd_result = OK; if (dev->d_sndlen > 0)
goto end_wait; {
pstate->snd_result = OK;
goto end_wait;
}
} }
/* Continue waiting */ /* Continue waiting */

View File

@ -105,7 +105,7 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev,
{ {
/* Copy the packet data into the device packet buffer and send it */ /* Copy the packet data into the device packet buffer and send it */
devif_pkt_send(dev, pstate->snd_buffer, pstate->snd_buflen); devif_send(dev, pstate->snd_buffer, pstate->snd_buflen, 0);
if (dev->d_sndlen == 0) if (dev->d_sndlen == 0)
{ {
return flags; return flags;