Merge pull request #2227 from rgrr/ncm_device_new2

Rewrite of NCM device driver
This commit is contained in:
HiFiPhile 2024-05-07 09:08:43 +02:00 committed by GitHub
commit e5b6f93df3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 997 additions and 516 deletions

View File

@ -74,6 +74,7 @@ target_sources(${PROJECT} PUBLIC
${LWIP}/src/netif/slipif.c ${LWIP}/src/netif/slipif.c
${LWIP}/src/apps/http/httpd.c ${LWIP}/src/apps/http/httpd.c
${LWIP}/src/apps/http/fs.c ${LWIP}/src/apps/http/fs.c
${LWIP}/src/apps/lwiperf/lwiperf.c
) )
# due to warnings from other net source, we need to prevent error from some of the warnings options # due to warnings from other net source, we need to prevent error from some of the warnings options

View File

@ -63,6 +63,7 @@ SRC_C += \
lib/lwip/src/netif/slipif.c \ lib/lwip/src/netif/slipif.c \
lib/lwip/src/apps/http/httpd.c \ lib/lwip/src/apps/http/httpd.c \
lib/lwip/src/apps/http/fs.c \ lib/lwip/src/apps/http/fs.c \
lib/lwip/src/apps/lwiperf/lwiperf.c \
lib/networking/dhserver.c \ lib/networking/dhserver.c \
lib/networking/dnserver.c \ lib/networking/dnserver.c \
lib/networking/rndis_reports.c lib/networking/rndis_reports.c

View File

@ -1,9 +1,11 @@
mcu:LPC11UXX mcu:LPC11UXX
mcu:LPC13XX mcu:LPC13XX
mcu:LPC15XX
mcu:MSP430x5xx mcu:MSP430x5xx
mcu:NUC121 mcu:NUC121
mcu:SAMD11 mcu:SAMD11
mcu:STM32L0 mcu:STM32L0
mcu:STM32F0
mcu:KINETIS_KL mcu:KINETIS_KL
family:broadcom_64bit family:broadcom_64bit
family:broadcom_32bit family:broadcom_32bit

View File

@ -48,8 +48,8 @@
#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67)) #define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))
#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/) #define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (2 * TCP_MSS) #define TCP_SND_BUF (4 * TCP_MSS)
#define TCP_WND (TCP_MSS) #define TCP_WND (4 * TCP_MSS)
#define ETHARP_SUPPORT_STATIC_ENTRIES 1 #define ETHARP_SUPPORT_STATIC_ENTRIES 1
@ -59,7 +59,7 @@
#define LWIP_SINGLE_NETIF 1 #define LWIP_SINGLE_NETIF 1
#define PBUF_POOL_SIZE 2 #define PBUF_POOL_SIZE 4
#define HTTPD_USE_CUSTOM_FSDATA 0 #define HTTPD_USE_CUSTOM_FSDATA 0

View File

@ -48,12 +48,17 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
#include "dhserver.h" #include "dhserver.h"
#include "dnserver.h" #include "dnserver.h"
#include "httpd.h"
#include "lwip/ethip6.h"
#include "lwip/init.h" #include "lwip/init.h"
#include "lwip/timeouts.h" #include "lwip/timeouts.h"
#include "lwip/ethip6.h"
#include "httpd.h"
#define INIT_IP4(a,b,c,d) { PP_HTONL(LWIP_MAKEU32(a,b,c,d)) } #ifdef INCLUDE_IPERF
#include "lwip/apps/lwiperf.h"
#endif
#define INIT_IP4(a, b, c, d) \
{ PP_HTONL(LWIP_MAKEU32(a, b, c, d)) }
/* lwip context */ /* lwip context */
static struct netif netif_data; static struct netif netif_data;
@ -72,16 +77,14 @@ static const ip4_addr_t netmask = INIT_IP4(255, 255, 255, 0);
static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0); static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0);
/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */ /* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
static dhcp_entry_t entries[] = static dhcp_entry_t entries[] = {
{
/* mac ip address lease time */ /* mac ip address lease time */
{{0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60}, {{0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60},
{{0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60}, {{0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60},
{{0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60}, {{0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60},
}; };
static const dhcp_config_t dhcp_config = static const dhcp_config_t dhcp_config = {
{
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */ .router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
.port = 67, /* listen port */ .port = 67, /* listen port */
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */ .dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
@ -89,19 +92,17 @@ static const dhcp_config_t dhcp_config =
TU_ARRAY_SIZE(entries), /* num entry */ TU_ARRAY_SIZE(entries), /* num entry */
entries /* entries */ entries /* entries */
}; };
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
{ static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
(void) netif; (void) netif;
for (;;) for (;;) {
{
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */ /* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
if (!tud_ready()) if (!tud_ready())
return ERR_USE; return ERR_USE;
/* if the network driver can accept another packet, we make it happen */ /* if the network driver can accept another packet, we make it happen */
if (tud_network_can_xmit(p->tot_len)) if (tud_network_can_xmit(p->tot_len)) {
{
tud_network_xmit(p, 0 /* unused for this example */); tud_network_xmit(p, 0 /* unused for this example */);
return ERR_OK; return ERR_OK;
} }
@ -111,20 +112,17 @@ static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
} }
} }
static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr) static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr) {
{
return etharp_output(netif, p, addr); return etharp_output(netif, p, addr);
} }
#if LWIP_IPV6 #if LWIP_IPV6
static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) {
{
return ethip6_output(netif, p, addr); return ethip6_output(netif, p, addr);
} }
#endif #endif
static err_t netif_init_cb(struct netif *netif) static err_t netif_init_cb(struct netif *netif) {
{
LWIP_ASSERT("netif != NULL", (netif != NULL)); LWIP_ASSERT("netif != NULL", (netif != NULL));
netif->mtu = CFG_TUD_NET_MTU; netif->mtu = CFG_TUD_NET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP; netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
@ -139,8 +137,7 @@ static err_t netif_init_cb(struct netif *netif)
return ERR_OK; return ERR_OK;
} }
static void init_lwip(void) static void init_lwip(void) {
{
struct netif *netif = &netif_data; struct netif *netif = &netif_data;
lwip_init(); lwip_init();
@ -158,28 +155,23 @@ static void init_lwip(void)
} }
/* handle any DNS requests from dns-server */ /* handle any DNS requests from dns-server */
bool dns_query_proc(const char *name, ip4_addr_t *addr) bool dns_query_proc(const char *name, ip4_addr_t *addr) {
{ if (0 == strcmp(name, "tiny.usb")) {
if (0 == strcmp(name, "tiny.usb"))
{
*addr = ipaddr; *addr = ipaddr;
return true; return true;
} }
return false; return false;
} }
bool tud_network_recv_cb(const uint8_t *src, uint16_t size) bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
{
/* this shouldn't happen, but if we get another packet before /* this shouldn't happen, but if we get another packet before
parsing the previous, we must signal our inability to accept it */ parsing the previous, we must signal our inability to accept it */
if (received_frame) return false; if (received_frame) return false;
if (size) if (size) {
{
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL); struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
if (p) if (p) {
{
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */ /* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
memcpy(p->payload, src, size); memcpy(p->payload, src, size);
@ -191,8 +183,7 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
return true; return true;
} }
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
{
struct pbuf *p = (struct pbuf *) ref; struct pbuf *p = (struct pbuf *) ref;
(void) arg; /* unused for this example */ (void) arg; /* unused for this example */
@ -200,11 +191,9 @@ uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
return pbuf_copy_partial(p, dst, p->tot_len, 0); return pbuf_copy_partial(p, dst, p->tot_len, 0);
} }
static void service_traffic(void) static void service_traffic(void) {
{
/* handle any packet received by tud_network_recv_cb() */ /* handle any packet received by tud_network_recv_cb() */
if (received_frame) if (received_frame) {
{
ethernet_input(received_frame, &netif_data); ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame); pbuf_free(received_frame);
received_frame = NULL; received_frame = NULL;
@ -214,18 +203,15 @@ static void service_traffic(void)
sys_check_timeouts(); sys_check_timeouts();
} }
void tud_network_init_cb(void) void tud_network_init_cb(void) {
{
/* if the network is re-initializing and we have a leftover packet, we must do a cleanup */ /* if the network is re-initializing and we have a leftover packet, we must do a cleanup */
if (received_frame) if (received_frame) {
{
pbuf_free(received_frame); pbuf_free(received_frame);
received_frame = NULL; received_frame = NULL;
} }
} }
int main(void) int main(void) {
{
/* initialize TinyUSB */ /* initialize TinyUSB */
board_init(); board_init();
@ -243,8 +229,12 @@ int main(void)
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK); while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
httpd_init(); httpd_init();
while (1) #ifdef INCLUDE_IPERF
{ // test with: iperf -c 192.168.7.1 -e -i 1 -M 5000 -l 8192 -r
lwiperf_start_tcp_server_default(NULL, NULL);
#endif
while (1) {
tud_task(); tud_task();
service_traffic(); service_traffic();
} }
@ -253,17 +243,14 @@ int main(void)
} }
/* lwip has provision for using a mutex, when applicable */ /* lwip has provision for using a mutex, when applicable */
sys_prot_t sys_arch_protect(void) sys_prot_t sys_arch_protect(void) {
{
return 0; return 0;
} }
void sys_arch_unprotect(sys_prot_t pval) void sys_arch_unprotect(sys_prot_t pval) {
{
(void) pval; (void) pval;
} }
/* lwip needs a millisecond time source, and the TinyUSB board support code has one available */ /* lwip needs a millisecond time source, and the TinyUSB board support code has one available */
uint32_t sys_now(void) uint32_t sys_now(void) {
{
return board_millis(); return board_millis();
} }

View File

@ -30,6 +30,8 @@
extern "C" { extern "C" {
#endif #endif
#include "lwipopts.h"
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Board Specific Configuration // Board Specific Configuration
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -82,6 +84,40 @@
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4))) #define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
#endif #endif
// Use different configurations to test all net devices (also due to resource limitations)
#if TU_CHECK_MCU(OPT_MCU_LPC15XX, OPT_MCU_LPC40XX, OPT_MCU_LPC51UXX, OPT_MCU_LPC54)
#define USE_ECM 1
#elif TU_CHECK_MCU(OPT_MCU_SAMD21, OPT_MCU_SAML21, OPT_MCU_SAML22)
#define USE_ECM 1
#elif TU_CHECK_MCU(OPT_MCU_STM32F0, OPT_MCU_STM32F1)
#define USE_ECM 1
#else
#define USE_ECM 0
#define INCLUDE_IPERF
#endif
//--------------------------------------------------------------------
// NCM CLASS CONFIGURATION, SEE "ncm.h" FOR PERFORMANCE TUNING
//--------------------------------------------------------------------
// Must be >> MTU
// Can be set to 2048 without impact
#define CFG_TUD_NCM_IN_NTB_MAX_SIZE (2 * TCP_MSS + 100)
// Must be >> MTU
// Can be set to smaller values if wNtbOutMaxDatagrams==1
#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE (2 * TCP_MSS + 100)
// Number of NCM transfer blocks for reception side
#ifndef CFG_TUD_NCM_OUT_NTB_N
#define CFG_TUD_NCM_OUT_NTB_N 1
#endif
// Number of NCM transfer blocks for transmission side
#ifndef CFG_TUD_NCM_IN_NTB_N
#define CFG_TUD_NCM_IN_NTB_N 1
#endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// DEVICE CONFIGURATION // DEVICE CONFIGURATION
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -94,7 +130,7 @@
// Network class has 2 drivers: ECM/RNDIS and NCM. // Network class has 2 drivers: ECM/RNDIS and NCM.
// Only one of the drivers can be enabled // Only one of the drivers can be enabled
#define CFG_TUD_ECM_RNDIS 1 #define CFG_TUD_ECM_RNDIS USE_ECM
#define CFG_TUD_NCM (1 - CFG_TUD_ECM_RNDIS) #define CFG_TUD_NCM (1 - CFG_TUD_ECM_RNDIS)
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -2,6 +2,7 @@
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2021, Ha Thach (tinyusb.org) * Copyright (c) 2021, Ha Thach (tinyusb.org)
* Copyright (c) 2024, Hardy Griech
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -24,22 +25,58 @@
* This file is part of the TinyUSB stack. * This file is part of the TinyUSB stack.
*/ */
#ifndef _TUSB_NCM_H_ #ifndef _TUSB_NCM_H_
#define _TUSB_NCM_H_ #define _TUSB_NCM_H_
#include "common/tusb_common.h" #include "common/tusb_common.h"
#ifdef __cplusplus // NTB buffers size for reception side, must be >> MTU to avoid TCP retransmission (driver issue ?)
extern "C" { // Linux use 2048 as minimal size
#ifndef CFG_TUD_NCM_OUT_NTB_MAX_SIZE
#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200
#endif #endif
// Table 4.3 Data Class Interface Protocol Codes // NTB buffers size for reception side, must be > MTU
typedef enum // Linux use 2048 as minimal size
{ #ifndef CFG_TUD_NCM_IN_NTB_MAX_SIZE
NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK = 0x01 #define CFG_TUD_NCM_IN_NTB_MAX_SIZE 3200
} ncm_data_interface_protocol_code_t; #endif
// Number of NTB buffers for reception side
// Depending on the configuration, this parameter could be increased with the cost of additional RAM requirements
// On Full-Speed (RP2040) :
// 1 - good performance
// 2 - up to 30% more performance with iperf with small packets
// >2 - no performance gain
// On High-Speed (STM32F7) :
// No performance gain
#ifndef CFG_TUD_NCM_OUT_NTB_N
#define CFG_TUD_NCM_OUT_NTB_N 1
#endif
// Number of NTB buffers for transmission side
// Depending on the configuration, this parameter could be increased with the cost of additional RAM requirements
// On Full-Speed (RP2040) :
// 1 - good performance but SystemView shows lost events (on load test)
// 2 - up to 50% more performance with iperf with small packets, "tud_network_can_xmit: request blocked"
// happens from time to time with SystemView
// 3 - "tud_network_can_xmit: request blocked" never happens
// >3 - no performance gain
// On High-Speed (STM32F7) :
// No performance gain
#ifndef CFG_TUD_NCM_IN_NTB_N
#define CFG_TUD_NCM_IN_NTB_N 1
#endif
// How many datagrams it is allowed to put into an NTB for transmission side
#ifndef CFG_TUD_NCM_IN_MAX_DATAGRAMS_PER_NTB
#define CFG_TUD_NCM_IN_MAX_DATAGRAMS_PER_NTB 8
#endif
// This tells the host how many datagrams it is allowed to put into an NTB
#ifndef CFG_TUD_NCM_OUT_MAX_DATAGRAMS_PER_NTB
#define CFG_TUD_NCM_OUT_MAX_DATAGRAMS_PER_NTB 6
#endif
// Table 6.2 Class-Specific Request Codes for Network Control Model subclass // Table 6.2 Class-Specific Request Codes for Network Control Model subclass
typedef enum typedef enum
@ -62,8 +99,65 @@ typedef enum
NCM_SET_CRC_MODE = 0x8A, NCM_SET_CRC_MODE = 0x8A,
} ncm_request_code_t; } ncm_request_code_t;
#ifdef __cplusplus #define NTH16_SIGNATURE 0x484D434E
} #define NDP16_SIGNATURE_NCM0 0x304D434E
#endif #define NDP16_SIGNATURE_NCM1 0x314D434E
typedef struct TU_ATTR_PACKED {
uint16_t wLength;
uint16_t bmNtbFormatsSupported;
uint32_t dwNtbInMaxSize;
uint16_t wNdbInDivisor;
uint16_t wNdbInPayloadRemainder;
uint16_t wNdbInAlignment;
uint16_t wReserved;
uint32_t dwNtbOutMaxSize;
uint16_t wNdbOutDivisor;
uint16_t wNdbOutPayloadRemainder;
uint16_t wNdbOutAlignment;
uint16_t wNtbOutMaxDatagrams;
} ntb_parameters_t;
typedef struct TU_ATTR_PACKED {
uint32_t dwSignature;
uint16_t wHeaderLength;
uint16_t wSequence;
uint16_t wBlockLength;
uint16_t wNdpIndex;
} nth16_t;
typedef struct TU_ATTR_PACKED {
uint16_t wDatagramIndex;
uint16_t wDatagramLength;
} ndp16_datagram_t;
typedef struct TU_ATTR_PACKED {
uint32_t dwSignature;
uint16_t wLength;
uint16_t wNextNdpIndex;
//ndp16_datagram_t datagram[];
} ndp16_t;
typedef union TU_ATTR_PACKED {
struct {
nth16_t nth;
ndp16_t ndp;
ndp16_datagram_t ndp_datagram[CFG_TUD_NCM_IN_MAX_DATAGRAMS_PER_NTB + 1];
};
uint8_t data[CFG_TUD_NCM_IN_NTB_MAX_SIZE];
} xmit_ntb_t;
typedef union TU_ATTR_PACKED {
struct {
nth16_t nth;
// only the header is at a guaranteed position
};
uint8_t data[CFG_TUD_NCM_OUT_NTB_MAX_SIZE];
} recv_ntb_t;
struct ncm_notify_t {
tusb_control_request_t header;
uint32_t downlink, uplink;
};
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -28,14 +28,13 @@
#ifndef _TUSB_NET_DEVICE_H_ #ifndef _TUSB_NET_DEVICE_H_
#define _TUSB_NET_DEVICE_H_ #define _TUSB_NET_DEVICE_H_
#include <stdint.h>
#include "class/cdc/cdc.h" #include "class/cdc/cdc.h"
#if CFG_TUD_ECM_RNDIS && CFG_TUD_NCM #if CFG_TUD_ECM_RNDIS && CFG_TUD_NCM
#error "Cannot enable both ECM_RNDIS and NCM network drivers" #error "Cannot enable both ECM_RNDIS and NCM network drivers"
#endif #endif
#include "ncm.h"
/* declared here, NOT in usb_descriptors.c, so that the driver can intelligently ZLP as needed */ /* declared here, NOT in usb_descriptors.c, so that the driver can intelligently ZLP as needed */
#define CFG_TUD_NET_ENDPOINT_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #define CFG_TUD_NET_ENDPOINT_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
@ -44,21 +43,13 @@
#define CFG_TUD_NET_MTU 1514 #define CFG_TUD_NET_MTU 1514
#endif #endif
#ifndef CFG_TUD_NCM_IN_NTB_MAX_SIZE
#define CFG_TUD_NCM_IN_NTB_MAX_SIZE 3200
#endif
#ifndef CFG_TUD_NCM_OUT_NTB_MAX_SIZE // Table 4.3 Data Class Interface Protocol Codes
#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200 typedef enum
#endif {
NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK = 0x01
} ncm_data_interface_protocol_code_t;
#ifndef CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB
#define CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB 8
#endif
#ifndef CFG_TUD_NCM_ALIGNMENT
#define CFG_TUD_NCM_ALIGNMENT 4
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -96,8 +87,6 @@ void tud_network_init_cb(void);
// TODO removed later since it is not part of tinyusb stack // TODO removed later since it is not part of tinyusb stack
extern uint8_t tud_network_mac_address[6]; extern uint8_t tud_network_mac_address[6];
//------------- NCM -------------//
// callback to client providing optional indication of internal state of network driver // callback to client providing optional indication of internal state of network driver
void tud_network_link_state_cb(bool state); void tud_network_link_state_cb(bool state);