项目作者: cogon

项目描述 :
高级语言:
项目地址: git://github.com/cogon/documents.git
创建时间: 2018-04-04T14:25:56Z
项目社区:https://github.com/cogon/documents

开源协议:Apache License 2.0

下载


lwip in freertos add tap driver

* api:

  1. 挂载tap驱动到lwip
  • netif_add(struct netif netif, ip4_addr_t ipaddr, ip4_addr_t netmask, ip4_addr_t gw,
    1. void *stat, netif_init_fn init, netif_input_fn input);
  • init中打开tap句柄, 打开os的sys_thread_new
    设置netif->output = etharp_output

    netif->linkoutput = send_packet ; lwip 出口最终调用

    netif->input = input ; input一般是tcpip_input() 是receive_packet后,调用的协议栈的入口。

  1. lwip 中启动接受服务

    • sys_thread_new(“tcpecho_thread”, tcpecho_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  2. 操作系统需要提供的接口

    • sys_thread_new

      • 创建线程
    • sys_sem_new sys_arch_sem_wait

      • 信号量(select通过这个实现)
    • sys_mbox_new sys_arch_mbox_tryfetch

4.lwip 流程

  1. etharp_output(){
  2. if (多播和广播)
  3. ethernet_output()
  4. if (静态arp)
  5. etharp_output_to_arp_index()
  6. etharp_query(){
  7. 查询arp
  8. if(is_new_entry)
  9. etharp_requst()
  10. if(state==ETHARP_STATE_PENDING){
  11. 拷贝当前包放入etharp_q_entry 队列
  12. }

* 主线程

  • tcpip_thread->tcpip_thread_handle_msg() :收到邮件就处理.调用msg中设置的回调函数
  • tcpip_input()接受的包 和 lwip_send() 应用线程发送的包 都会将包放到主线程的mailbox里. sys_mbox_post()

  • tcpip_input() -> sys_mbox_trypost()

    1. mac注册的回调函数是 :
    2. ethernet_input(){
    3. if(ip包)
    4. ip_input()
    5. if(arp包)
    6. etharp_intpu()
    7. }
    8. 不带mac回调函数是: ip_intpu(){
    9. if(inet_chksum()){
    10. return
    11. }
    12. if(不是本地接受地址){
    13. ip4_forward()
    14. return
    15. }
    16. if(分片数据){
    17. ip4_reass()
    18. return
    19. }
    20. 往上层送
    21. if(udp) upd_input
    22. if(tcp) tcp_input
    23. if(icmp) icmp_intpu
    24. if(igmp) igmp_intpu
    25. }
    26. `
  • lwip_send() -> netconn_send(){ 注册的回调函数是 lwip_netconn_do_send() }-> netconn_apimsg() -> tcpip_send_msg_wait_sem() -> sys_mbox_post()