bridgeif_fdb.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @file
  3. * lwIP netif implementing an FDB for IEEE 802.1D MAC Bridge
  4. */
  5. /*
  6. * Copyright (c) 2017 Simon Goldschmidt.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  34. *
  35. */
  36. /**
  37. * @defgroup bridgeif_fdb FDB example code
  38. * @ingroup bridgeif
  39. * This file implements an example for an FDB (Forwarding DataBase)
  40. */
  41. #include "netif/bridgeif.h"
  42. #include "lwip/sys.h"
  43. #include "lwip/mem.h"
  44. #include "lwip/timeouts.h"
  45. #include <string.h>
  46. #define BRIDGEIF_AGE_TIMER_MS 1000
  47. #define BR_FDB_TIMEOUT_SEC (60*5) /* 5 minutes FDB timeout */
  48. typedef struct bridgeif_dfdb_entry_s {
  49. u8_t used;
  50. u8_t port;
  51. u32_t ts;
  52. struct eth_addr addr;
  53. } bridgeif_dfdb_entry_t;
  54. typedef struct bridgeif_dfdb_s {
  55. u16_t max_fdb_entries;
  56. bridgeif_dfdb_entry_t *fdb;
  57. } bridgeif_dfdb_t;
  58. /**
  59. * @ingroup bridgeif_fdb
  60. * A real simple and slow implementation of an auto-learning forwarding database that
  61. * remembers known src mac addresses to know which port to send frames destined for that
  62. * mac address.
  63. *
  64. * ATTENTION: This is meant as an example only, in real-world use, you should
  65. * provide a better implementation :-)
  66. */
  67. void
  68. bridgeif_fdb_update_src(void *fdb_ptr, struct eth_addr *src_addr, u8_t port_idx)
  69. {
  70. int i;
  71. bridgeif_dfdb_t *fdb = (bridgeif_dfdb_t *)fdb_ptr;
  72. BRIDGEIF_DECL_PROTECT(lev);
  73. BRIDGEIF_READ_PROTECT(lev);
  74. for (i = 0; i < fdb->max_fdb_entries; i++) {
  75. bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
  76. if (e->used && e->ts) {
  77. if (!memcmp(&e->addr, src_addr, sizeof(struct eth_addr))) {
  78. LWIP_DEBUGF(BRIDGEIF_FDB_DEBUG, ("br: update src %02x:%02x:%02x:%02x:%02x:%02x (from %d) @ idx %d\n",
  79. src_addr->addr[0], src_addr->addr[1], src_addr->addr[2], src_addr->addr[3], src_addr->addr[4], src_addr->addr[5],
  80. port_idx, i));
  81. BRIDGEIF_WRITE_PROTECT(lev);
  82. e->ts = BR_FDB_TIMEOUT_SEC;
  83. e->port = port_idx;
  84. BRIDGEIF_WRITE_UNPROTECT(lev);
  85. BRIDGEIF_READ_UNPROTECT(lev);
  86. return;
  87. }
  88. }
  89. }
  90. /* not found, allocate new entry from free */
  91. for (i = 0; i < fdb->max_fdb_entries; i++) {
  92. bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
  93. if (!e->used || !e->ts) {
  94. BRIDGEIF_WRITE_PROTECT(lev);
  95. /* check again when protected */
  96. if (!e->used || !e->ts) {
  97. LWIP_DEBUGF(BRIDGEIF_FDB_DEBUG, ("br: create src %02x:%02x:%02x:%02x:%02x:%02x (from %d) @ idx %d\n",
  98. src_addr->addr[0], src_addr->addr[1], src_addr->addr[2], src_addr->addr[3], src_addr->addr[4], src_addr->addr[5],
  99. port_idx, i));
  100. memcpy(&e->addr, src_addr, sizeof(struct eth_addr));
  101. e->ts = BR_FDB_TIMEOUT_SEC;
  102. e->port = port_idx;
  103. e->used = 1;
  104. BRIDGEIF_WRITE_UNPROTECT(lev);
  105. BRIDGEIF_READ_UNPROTECT(lev);
  106. return;
  107. }
  108. BRIDGEIF_WRITE_UNPROTECT(lev);
  109. }
  110. }
  111. BRIDGEIF_READ_UNPROTECT(lev);
  112. /* not found, no free entry -> flood */
  113. }
  114. /**
  115. * @ingroup bridgeif_fdb
  116. * Walk our list of auto-learnt fdb entries and return a port to forward or BR_FLOOD if unknown
  117. */
  118. bridgeif_portmask_t
  119. bridgeif_fdb_get_dst_ports(void *fdb_ptr, struct eth_addr *dst_addr)
  120. {
  121. int i;
  122. bridgeif_dfdb_t *fdb = (bridgeif_dfdb_t *)fdb_ptr;
  123. BRIDGEIF_DECL_PROTECT(lev);
  124. BRIDGEIF_READ_PROTECT(lev);
  125. for (i = 0; i < fdb->max_fdb_entries; i++) {
  126. bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
  127. if (e->used && e->ts) {
  128. if (!memcmp(&e->addr, dst_addr, sizeof(struct eth_addr))) {
  129. bridgeif_portmask_t ret = (bridgeif_portmask_t)(1 << e->port);
  130. BRIDGEIF_READ_UNPROTECT(lev);
  131. return ret;
  132. }
  133. }
  134. }
  135. BRIDGEIF_READ_UNPROTECT(lev);
  136. return BR_FLOOD;
  137. }
  138. /**
  139. * @ingroup bridgeif_fdb
  140. * Aging implementation of our simple fdb
  141. */
  142. static void
  143. bridgeif_fdb_age_one_second(void *fdb_ptr)
  144. {
  145. int i;
  146. bridgeif_dfdb_t *fdb;
  147. BRIDGEIF_DECL_PROTECT(lev);
  148. fdb = (bridgeif_dfdb_t *)fdb_ptr;
  149. BRIDGEIF_READ_PROTECT(lev);
  150. for (i = 0; i < fdb->max_fdb_entries; i++) {
  151. bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
  152. if (e->used && e->ts) {
  153. BRIDGEIF_WRITE_PROTECT(lev);
  154. /* check again when protected */
  155. if (e->used && e->ts) {
  156. if (--e->ts == 0) {
  157. e->used = 0;
  158. }
  159. }
  160. BRIDGEIF_WRITE_UNPROTECT(lev);
  161. }
  162. }
  163. BRIDGEIF_READ_UNPROTECT(lev);
  164. }
  165. /** Timer callback for fdb aging, called once per second */
  166. static void
  167. bridgeif_age_tmr(void *arg)
  168. {
  169. bridgeif_dfdb_t *fdb = (bridgeif_dfdb_t *)arg;
  170. LWIP_ASSERT("invalid arg", arg != NULL);
  171. bridgeif_fdb_age_one_second(fdb);
  172. sys_timeout(BRIDGEIF_AGE_TIMER_MS, bridgeif_age_tmr, arg);
  173. }
  174. /**
  175. * @ingroup bridgeif_fdb
  176. * Init our simple fdb list
  177. */
  178. void *
  179. bridgeif_fdb_init(u16_t max_fdb_entries)
  180. {
  181. bridgeif_dfdb_t *fdb;
  182. size_t alloc_len_sizet = sizeof(bridgeif_dfdb_t) + (max_fdb_entries * sizeof(bridgeif_dfdb_entry_t));
  183. mem_size_t alloc_len = (mem_size_t)alloc_len_sizet;
  184. LWIP_ASSERT("alloc_len == alloc_len_sizet", alloc_len == alloc_len_sizet);
  185. LWIP_DEBUGF(BRIDGEIF_DEBUG, ("bridgeif_fdb_init: allocating %d bytes for private FDB data\n", (int)alloc_len));
  186. fdb = (bridgeif_dfdb_t *)mem_calloc(1, alloc_len);
  187. if (fdb == NULL) {
  188. return NULL;
  189. }
  190. fdb->max_fdb_entries = max_fdb_entries;
  191. fdb->fdb = (bridgeif_dfdb_entry_t *)(fdb + 1);
  192. sys_timeout(BRIDGEIF_AGE_TIMER_MS, bridgeif_age_tmr, fdb);
  193. return fdb;
  194. }