lwip: Add customizable netif up/down hooks

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
Post Reply
ulikoehler
Posts: 71
Joined: Tue Mar 17, 2015 2:32 am
Been thanked: 3 times

lwip: Add customizable netif up/down hooks

Post by ulikoehler »

Hi,
One of the things I have been missing for a long time in lwipthread.c is a set of hooks that are called once the netif goes up/down. I use them e.g. to init/deinit SNTP.

To add them without impact on existing applications we can use weak symbols. Somewhere above lwip_thread add:

Code: Select all

/**
 * Redefine this hook to execute custom logic when the lwip netif
 * comes up
 */
__attribute__((weak)) void lwipNetifUpHook(struct netif* thisif) {
  (void)thisif;
}

/**
 * Redefine this hook to execute custom logic when the lwip netif
 * goes down
 */
__attribute__((weak)) void lwipNetifDownHook(struct netif* thisif) {
  (void)thisif;
}


and then replace the if(mask & PERIODIC_TIMER_ID) block with this (only 2 lines added where the code is called):

Code: Select all

    if (mask & PERIODIC_TIMER_ID) {
      bool current_link_status = macPollLinkStatus(&ETHD1);
      if (current_link_status != netif_is_link_up(&thisif)) {
        if (current_link_status) { //Interface is coming up
          lwipNetifUpHook(&thisif);
          tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
                                     &thisif, 0);
#if LWIP_DHCP
          dhcp_start(&thisif);
#endif
        }
        else { //Interface is going down
          lwipNetifDownHook(&thisif);
          tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
                                     &thisif, 0);
#if LWIP_DHCP
          dhcp_stop(&thisif);
#endif
        }
      }
    }


Besides solving the main issue of the hooks not being easily accessible without lwipthread.c modification, this also means that an application using it in an unmodified manner now has access to the netif object. I don't remember off my head what I used that for, but I think it was related with the DHCP server. I have described the older method of adding thisif to global scope on my blog: http://techoverflow.net/blog/2015/03/16/accessing-lwip-struct-netif-instance-in-chibios/

Best regards, Uli
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip: Add customizable netif up/down hooks

Post by Giovanni »

Hi,

Thanks for the code, the only problem I can see is that attributes are GCC-specific, this would not work with other compilers. May be we could use an event source object for this kind of things.

Giovanni
ulikoehler
Posts: 71
Joined: Tue Mar 17, 2015 2:32 am
Been thanked: 3 times

Re: lwip: Add customizable netif up/down hooks

Post by ulikoehler »

I saw you using attributes in os/hal/osal-less/ARMCMx/osal.c here:

Code: Select all

#if !defined(__DOXYGEN__)
__attribute__((weak, noreturn))
#endif
void osalSysHalt(const char *reason) {

  osalSysDisable();
  osal_halt_msg = reason;
  while (true) {
  }
}

so I thought there was some rationale behind using it outside of the GCC tree. But I guess you are right, it is gcc-specific after all.

Great idea with the event source. I'll implement it once I'll get around to it.

Best regards, Uli
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip: Add customizable netif up/down hooks

Post by Giovanni »

That attribute should not be there, it is definitely an issue.

Giovanni
ulikoehler
Posts: 71
Joined: Tue Mar 17, 2015 2:32 am
Been thanked: 3 times

Re: lwip: Add customizable netif up/down hooks

Post by ulikoehler »

AFAIK one issue with event sources is that the flags get ORed to the thread that calls chEvtRegister(). I think this would often mean (e.g. in my case) that I'd need another thread just for a few simple callbacks (because the main thread is ).

What do you think about adding this somewhere (would also solve the osalSysHalt() issue)?

Code: Select all

#ifdef __GNUC__
#define __weak __attribute__((weak))
#endif

(I don't really know where this should be added, maybe chtypes.h / niltypes.h. I added in in lwipthreads.h)

RVDS supports weak natively, see http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka14171.html
IAR
IAR supports it too: http://supp.iar.com/FilesPublic/UPDINFO/004916/arm/doc/EWARM_DevelopmentGuide.ENU.pdf

Then we could use my initial proposal of weak functions:

Code: Select all

/**
 * Redefine this hook to execute custom logic when the lwip netif
 * comes up
 */
__weak void lwipNetifUpHook(struct netif* thisif) {
  (void)thisif;
}

/**
 * Redefine this hook to execute custom logic when the lwip netif
 * goes down
 */
__weak void lwipNetifDownHook(struct netif* thisif) {
  (void)thisif;
}


Uli
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip: Add customizable netif up/down hooks

Post by Giovanni »

Hi,

There is a lot of changes waiting to be integrated on the lwIP layer, currently I am focusing on other things. When the next lwIP version will be released (aren't they very late?) I plan to open a dev cycle on that.

Giovanni
ulikoehler
Posts: 71
Joined: Tue Mar 17, 2015 2:32 am
Been thanked: 3 times

Re: lwip: Add customizable netif up/down hooks

Post by ulikoehler »

Yeah, they released the last version 2012 I think. However, in the git commit log I can't really see a clear indication that they are planning to release soon.

It's OK with me to delay that as I'll have to roll my own lwipthread.c anyway until pretty much all the changes will get merged. However, I wouldn't really expect a release before 2017...

Best regards, Uli
Post Reply