Skip to content

Commit ac361d6

Browse files
author
Cruz Monrreal
authored
Merge pull request #8739 from tymoteuszblochmobica/master
L3IP Interface Implementation
2 parents c1226ff + fc88922 commit ac361d6

16 files changed

+1217
-172
lines changed

features/lwipstack/LWIPInterface.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,60 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
409409
#endif //LWIP_ETHERNET
410410
}
411411

412+
413+
nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetworkStack::Interface **interface_out)
414+
{
415+
#if LWIP_L3IP
416+
Interface *interface = new (std::nothrow) Interface();
417+
if (!interface) {
418+
return NSAPI_ERROR_NO_MEMORY;
419+
}
420+
interface->l3ip = &l3ip;
421+
interface->memory_manager = &memory_manager;
422+
interface->ppp = false;
423+
424+
425+
426+
// interface->netif.hwaddr_len = 0; should we set?
427+
428+
if (!netif_add(&interface->netif,
429+
#if LWIP_IPV4
430+
0, 0, 0,
431+
#endif
432+
interface, &LWIP::Interface::emac_if_init, ip_input)) {
433+
return NSAPI_ERROR_DEVICE_ERROR;
434+
}
435+
436+
if (default_if) {
437+
netif_set_default(&interface->netif);
438+
default_interface = interface;
439+
}
440+
441+
netif_set_link_callback(&interface->netif, &LWIP::Interface::netif_link_irq);
442+
netif_set_status_callback(&interface->netif, &LWIP::Interface::netif_status_irq);
443+
444+
*interface_out = interface;
445+
446+
447+
//lwip_add_random_seed(seed); to do?
448+
449+
return NSAPI_ERROR_OK;
450+
451+
#else
452+
return NSAPI_ERROR_UNSUPPORTED;
453+
454+
#endif //LWIP_L3IP
455+
}
456+
457+
nsapi_error_t LWIP::remove_l3ip_interface(OnboardNetworkStack::Interface **interface_out)
458+
{
459+
#if LWIP_L3IP
460+
return NSAPI_ERROR_OK;
461+
#else
462+
return NSAPI_ERROR_UNSUPPORTED;
463+
464+
#endif //LWIP_L3IP
465+
}
412466
/* Internal API to preserve existing PPP functionality - revise to better match mbed_ipstak_add_ethernet_interface later */
413467
nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, nsapi_ip_stack_t stack, LWIP::Interface **interface_out)
414468
{
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "lwip/tcpip.h"
19+
#include "lwip/tcp.h"
20+
#include "lwip/ip.h"
21+
#include "netif/etharp.h"
22+
#include "lwip/ethip6.h"
23+
#include "netsocket/nsapi_types.h"
24+
#include "netsocket/L3IP.h"
25+
26+
#include "LWIPStack.h"
27+
28+
#if LWIP_L3IP
29+
30+
err_t LWIP::Interface::l3ip_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
31+
{
32+
/* Increase reference counter since lwip stores handle to pbuf and frees
33+
it after output */
34+
pbuf_ref(p);
35+
36+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
37+
bool ret = mbed_if->l3ip->link_out(p);
38+
return ret ? ERR_OK : ERR_IF;
39+
}
40+
41+
void LWIP::Interface::l3ip_input(net_stack_mem_buf_t *buf)
42+
{
43+
struct pbuf *p = static_cast<struct pbuf *>(buf);
44+
45+
/* pass all packets to IP stack input */
46+
if (netif.input(p, &netif) != ERR_OK) {
47+
LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n"));
48+
49+
pbuf_free(p);
50+
}
51+
}
52+
53+
void LWIP::Interface::l3ip_state_change(bool up)
54+
{
55+
if (up) {
56+
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &netif, 1);
57+
} else {
58+
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &netif, 1);
59+
}
60+
}
61+
62+
#if LWIP_IGMP
63+
64+
#include "lwip/igmp.h"
65+
/**
66+
* IPv4 address filtering setup.
67+
*
68+
* \param[in] netif the lwip network interface structure
69+
* \param[in] group IPv4 group to modify
70+
* \param[in] action
71+
* \return ERR_OK or error code
72+
*/
73+
err_t LWIP::Interface::l3ip_multicast_ipv4_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action)
74+
{
75+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
76+
SocketAddress addr(&group, NSAPI_IPv6);
77+
switch (action) {
78+
case NETIF_ADD_MAC_FILTER: {
79+
mbed_if->l3ip->add_ipv4_multicast_group(addr.get_ip_address());
80+
return ERR_OK;
81+
}
82+
case NETIF_DEL_MAC_FILTER:
83+
mbed_if->l3ip->remove_ipv4_multicast_group(addr.get_ip_address());
84+
return ERR_OK;
85+
default:
86+
return ERR_ARG;
87+
}
88+
89+
}
90+
#endif
91+
92+
#if LWIP_IPV6_MLD
93+
94+
#include "lwip/mld6.h"
95+
/**
96+
* IPv6 address filtering setup.
97+
*
98+
* \param[in] netif the lwip network interface structure
99+
* \param[in] group IPv6 group to modify
100+
* \param[in] action
101+
* \return ERR_OK or error code
102+
*/
103+
err_t LWIP::Interface::l3ip_multicast_ipv6_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action)
104+
{
105+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
106+
SocketAddress addr(&group, NSAPI_IPv6);
107+
switch (action) {
108+
case NETIF_ADD_MAC_FILTER: {
109+
110+
mbed_if->l3ip->add_ipv6_multicast_group(addr.get_ip_address());
111+
return ERR_OK;
112+
}
113+
case NETIF_DEL_MAC_FILTER:
114+
mbed_if->l3ip->remove_ipv6_multicast_group(addr.get_ip_address());
115+
return ERR_OK;
116+
default:
117+
return ERR_ARG;
118+
}
119+
120+
}
121+
#endif
122+
123+
err_t LWIP::Interface::l3ip_if_init(struct netif *netif)
124+
{
125+
int err = ERR_OK;
126+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
127+
128+
mbed_if->l3ip->set_memory_manager(*mbed_if->memory_manager);
129+
mbed_if->l3ip->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::l3ip_input));
130+
mbed_if->l3ip->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::l3ip_state_change));
131+
132+
/* Interface capabilities */
133+
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
134+
135+
if (!mbed_if->l3ip->power_up()) {
136+
err = ERR_IF;
137+
}
138+
139+
netif->mtu = mbed_if->l3ip->get_mtu_size();
140+
mbed_if->l3ip->get_ifname(netif->name, NSAPI_INTERFACE_NAME_SIZE);
141+
142+
#if LWIP_IPV4
143+
netif->output = &LWIP::Interface::l3ip_output;
144+
#if LWIP_IGMP
145+
netif->igmp_mac_filter = &LWIP::Interface::l3ip_multicast_ipv4_filter;
146+
netif->flags |= NETIF_FLAG_IGMP;
147+
#endif /* LWIP_IGMP */
148+
#endif /* LWIP_IPV4 */
149+
#if LWIP_IPV6
150+
//netif->output_ip6 = ethip6_output;//to be done
151+
#if LWIP_IPV6_MLD
152+
netif->mld_mac_filter = &LWIP::Interface::l3ip_multicast_ipv6_filter;
153+
netif->flags |= NETIF_FLAG_MLD6;
154+
#else
155+
// Would need to enable all multicasts here - no API in fsl_enet to do that
156+
#error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system"
157+
#endif
158+
#endif
159+
160+
netif->linkoutput = NULL;
161+
162+
return err;
163+
}
164+
165+
#endif
166+

features/lwipstack/LWIPMemoryManager.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "pbuf.h"
1717
#include "LWIPMemoryManager.h"
1818

19-
emac_mem_buf_t *LWIPMemoryManager::alloc_heap(uint32_t size, uint32_t align)
19+
net_stack_mem_buf_t *LWIPMemoryManager::alloc_heap(uint32_t size, uint32_t align)
2020
{
2121
struct pbuf *pbuf = pbuf_alloc(PBUF_RAW, size + align, PBUF_RAM);
2222
if (pbuf == NULL) {
@@ -25,10 +25,10 @@ emac_mem_buf_t *LWIPMemoryManager::alloc_heap(uint32_t size, uint32_t align)
2525

2626
align_memory(pbuf, align);
2727

28-
return static_cast<emac_mem_buf_t *>(pbuf);
28+
return static_cast<net_stack_mem_buf_t *>(pbuf);
2929
}
3030

31-
emac_mem_buf_t *LWIPMemoryManager::alloc_pool(uint32_t size, uint32_t align)
31+
net_stack_mem_buf_t *LWIPMemoryManager::alloc_pool(uint32_t size, uint32_t align)
3232
{
3333
uint32_t total_align = count_total_align(size, align);
3434

@@ -39,7 +39,7 @@ emac_mem_buf_t *LWIPMemoryManager::alloc_pool(uint32_t size, uint32_t align)
3939

4040
align_memory(pbuf, align);
4141

42-
return static_cast<emac_mem_buf_t *>(pbuf);
42+
return static_cast<net_stack_mem_buf_t *>(pbuf);
4343
}
4444

4545
uint32_t LWIPMemoryManager::get_pool_alloc_unit(uint32_t align) const
@@ -48,56 +48,56 @@ uint32_t LWIPMemoryManager::get_pool_alloc_unit(uint32_t align) const
4848
return alloc_unit;
4949
}
5050

51-
void LWIPMemoryManager::free(emac_mem_buf_t *buf)
51+
void LWIPMemoryManager::free(net_stack_mem_buf_t *buf)
5252
{
5353
pbuf_free(static_cast<struct pbuf *>(buf));
5454
}
5555

56-
uint32_t LWIPMemoryManager::get_total_len(const emac_mem_buf_t *buf) const
56+
uint32_t LWIPMemoryManager::get_total_len(const net_stack_mem_buf_t *buf) const
5757
{
5858
return (static_cast<const struct pbuf *>(buf))->tot_len;
5959
}
6060

61-
void LWIPMemoryManager::copy(emac_mem_buf_t *to_buf, const emac_mem_buf_t *from_buf)
61+
void LWIPMemoryManager::copy(net_stack_mem_buf_t *to_buf, const net_stack_mem_buf_t *from_buf)
6262
{
6363
pbuf_copy(static_cast<struct pbuf *>(to_buf), static_cast<const struct pbuf *>(from_buf));
6464
}
6565

66-
void LWIPMemoryManager::copy_to_buf(emac_mem_buf_t *to_buf, const void *ptr, uint32_t len)
66+
void LWIPMemoryManager::copy_to_buf(net_stack_mem_buf_t *to_buf, const void *ptr, uint32_t len)
6767
{
6868
pbuf_take(static_cast<struct pbuf *>(to_buf), ptr, len);
6969
}
7070

71-
uint32_t LWIPMemoryManager::copy_from_buf(void *ptr, uint32_t len, const emac_mem_buf_t *from_buf) const
71+
uint32_t LWIPMemoryManager::copy_from_buf(void *ptr, uint32_t len, const net_stack_mem_buf_t *from_buf) const
7272
{
7373
return pbuf_copy_partial(static_cast<const struct pbuf *>(from_buf), ptr, len, 0);
7474
}
7575

76-
void LWIPMemoryManager::cat(emac_mem_buf_t *to_buf, emac_mem_buf_t *cat_buf)
76+
void LWIPMemoryManager::cat(net_stack_mem_buf_t *to_buf, net_stack_mem_buf_t *cat_buf)
7777
{
7878
pbuf_cat(static_cast<struct pbuf *>(to_buf), static_cast<struct pbuf *>(cat_buf));
7979
}
8080

81-
emac_mem_buf_t *LWIPMemoryManager::get_next(const emac_mem_buf_t *buf) const
81+
net_stack_mem_buf_t *LWIPMemoryManager::get_next(const net_stack_mem_buf_t *buf) const
8282
{
8383
if (!buf) {
8484
return NULL;
8585
}
8686
struct pbuf *next = (static_cast<const struct pbuf *>(buf))->next;
87-
return static_cast<emac_mem_buf_t *>(next);
87+
return static_cast<net_stack_mem_buf_t *>(next);
8888
}
8989

90-
void *LWIPMemoryManager::get_ptr(const emac_mem_buf_t *buf) const
90+
void *LWIPMemoryManager::get_ptr(const net_stack_mem_buf_t *buf) const
9191
{
9292
return (static_cast<const struct pbuf *>(buf))->payload;
9393
}
9494

95-
uint32_t LWIPMemoryManager::get_len(const emac_mem_buf_t *buf) const
95+
uint32_t LWIPMemoryManager::get_len(const net_stack_mem_buf_t *buf) const
9696
{
9797
return (static_cast<const struct pbuf *>(buf))->len;
9898
}
9999

100-
void LWIPMemoryManager::set_len(emac_mem_buf_t *buf, uint32_t len)
100+
void LWIPMemoryManager::set_len(net_stack_mem_buf_t *buf, uint32_t len)
101101
{
102102
struct pbuf *pbuf = static_cast<struct pbuf *>(buf);
103103
pbuf->len = len;

0 commit comments

Comments
 (0)