FrameDecider.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @file FrameDecider.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Mudule which decides to which peers frames from the device are to be
  32. * forwarded.
  33. */
  34. #ifndef BADVPN_CLIENT_FRAMEDECIDER_H
  35. #define BADVPN_CLIENT_FRAMEDECIDER_H
  36. #include <stdint.h>
  37. #include <structure/LinkedList2.h>
  38. #include <structure/LinkedList3.h>
  39. #include <structure/CAvl.h>
  40. #include <base/DebugObject.h>
  41. #include <base/BLog.h>
  42. #include <system/BReactor.h>
  43. struct _FrameDeciderPeer;
  44. struct _FrameDecider_mac_entry {
  45. struct _FrameDeciderPeer *peer;
  46. LinkedList2Node list_node; // node in FrameDeciderPeer.mac_entries_free or FrameDeciderPeer.mac_entries_used
  47. // defined when used:
  48. uint8_t mac[6];
  49. // node in FrameDecider.macs_tree, indexed by mac
  50. struct _FrameDecider_mac_entry *tree_child[2];
  51. struct _FrameDecider_mac_entry *tree_parent;
  52. int8_t tree_balance;
  53. };
  54. struct _FrameDecider_group_entry {
  55. struct _FrameDeciderPeer *peer;
  56. LinkedList2Node list_node; // node in FrameDeciderPeer.group_entries_free or FrameDeciderPeer.group_entries_used
  57. BTimer timer; // timer for removing the group entry, running when used
  58. // defined when used:
  59. // basic group data
  60. uint32_t group; // group address
  61. // node in FrameDeciderPeer.groups_tree, indexed by group
  62. struct _FrameDecider_group_entry *tree_child[2];
  63. struct _FrameDecider_group_entry *tree_parent;
  64. int8_t tree_balance;
  65. // all that folows is managed by add_to_multicast() and remove_from_multicast()
  66. LinkedList3Node sig_list_node; // node in list of group entries with the same sig
  67. btime_t timer_endtime;
  68. int is_master;
  69. // defined when used and we are master:
  70. struct {
  71. uint32_t sig; // last 23 bits of group address
  72. // node in FrameDecider.multicast_tree, indexed by sig
  73. struct _FrameDecider_group_entry *tree_child[2];
  74. struct _FrameDecider_group_entry *tree_parent;
  75. int8_t tree_balance;
  76. } master;
  77. };
  78. typedef struct _FrameDecider_mac_entry FDMacsTree_entry;
  79. typedef struct _FrameDecider_mac_entry *FDMacsTree_link;
  80. typedef const uint8_t *FDMacsTree_key;
  81. #include "FrameDecider_macs_tree.h"
  82. #include <structure/CAvl_decl.h>
  83. typedef struct _FrameDecider_group_entry FDGroupsTree_entry;
  84. typedef struct _FrameDecider_group_entry *FDGroupsTree_link;
  85. #include "FrameDecider_groups_tree.h"
  86. #include <structure/CAvl_decl.h>
  87. typedef struct _FrameDecider_group_entry FDMulticastTree_entry;
  88. typedef struct _FrameDecider_group_entry *FDMulticastTree_link;
  89. #include "FrameDecider_multicast_tree.h"
  90. #include <structure/CAvl_decl.h>
  91. /**
  92. * Object that represents a local device.
  93. */
  94. typedef struct {
  95. int max_peer_macs;
  96. int max_peer_groups;
  97. btime_t igmp_group_membership_interval;
  98. btime_t igmp_last_member_query_time;
  99. BReactor *reactor;
  100. LinkedList2 peers_list;
  101. FDMacsTree macs_tree;
  102. FDMulticastTree multicast_tree;
  103. int decide_state;
  104. LinkedList2Iterator decide_flood_it;
  105. struct _FrameDeciderPeer *decide_unicast_peer;
  106. LinkedList3Iterator decide_multicast_it;
  107. DebugObject d_obj;
  108. } FrameDecider;
  109. /**
  110. * Object that represents a peer that a local device can send frames to.
  111. */
  112. typedef struct _FrameDeciderPeer {
  113. FrameDecider *d;
  114. void *user;
  115. BLog_logfunc logfunc;
  116. struct _FrameDecider_mac_entry *mac_entries;
  117. struct _FrameDecider_group_entry *group_entries;
  118. LinkedList2Node list_node; // node in FrameDecider.peers_list
  119. LinkedList2 mac_entries_free;
  120. LinkedList2 mac_entries_used;
  121. LinkedList2 group_entries_free;
  122. LinkedList2 group_entries_used;
  123. FDGroupsTree groups_tree;
  124. DebugObject d_obj;
  125. } FrameDeciderPeer;
  126. /**
  127. * Initializes the object.
  128. *
  129. * @param o the object
  130. * @param max_peer_macs maximum number of MAC addresses a peer may posess. Must be >0.
  131. * @param max_peer_groups maximum number of multicast groups a peer may belong to. Must be >0.
  132. * @param igmp_group_membership_interval IGMP Group Membership Interval value. When a join
  133. * is detected for a peer in {@link FrameDeciderPeer_Analyze}, this is how long we wait
  134. * for another join before we remove the group from the peer. Note that the group may
  135. * be removed sooner if the peer fails to respond to a Group-Specific Query (see below).
  136. * @param igmp_last_member_query_time IGMP Last Member Query Time value. When a Group-Specific
  137. * Query is detected in {@link FrameDecider_AnalyzeAndDecide}, this is how long we wait for a peer
  138. * belonging to the group to send a join before we remove the group from it.
  139. */
  140. void FrameDecider_Init (FrameDecider *o, int max_peer_macs, int max_peer_groups, btime_t igmp_group_membership_interval, btime_t igmp_last_member_query_time, BReactor *reactor);
  141. /**
  142. * Frees the object.
  143. * There must be no {@link FrameDeciderPeer} objects using this decider.
  144. *
  145. * @param o the object
  146. */
  147. void FrameDecider_Free (FrameDecider *o);
  148. /**
  149. * Analyzes a frame read from the local device and starts deciding which peers
  150. * the frame should be forwarded to.
  151. *
  152. * @param o the object
  153. * @param frame frame data
  154. * @param frame_len frame length. Must be >=0.
  155. */
  156. void FrameDecider_AnalyzeAndDecide (FrameDecider *o, const uint8_t *frame, int frame_len);
  157. /**
  158. * Returns the next peer that the frame submitted to {@link FrameDecider_AnalyzeAndDecide} should be
  159. * forwarded to.
  160. *
  161. * @param o the object
  162. * @return peer to forward the frame to, or NULL if no more
  163. */
  164. FrameDeciderPeer * FrameDecider_NextDestination (FrameDecider *o);
  165. /**
  166. * Initializes the object.
  167. *
  168. * @param o the object
  169. * @param d decider this peer will belong to
  170. * @param user argument to log function
  171. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  172. * @return 1 on success, 0 on failure
  173. */
  174. int FrameDeciderPeer_Init (FrameDeciderPeer *o, FrameDecider *d, void *user, BLog_logfunc logfunc) WARN_UNUSED;
  175. /**
  176. * Frees the object.
  177. *
  178. * @param o the object
  179. */
  180. void FrameDeciderPeer_Free (FrameDeciderPeer *o);
  181. /**
  182. * Analyzes a frame received from the peer.
  183. *
  184. * @param o the object
  185. * @param frame frame data
  186. * @param frame_len frame length. Must be >=0.
  187. */
  188. void FrameDeciderPeer_Analyze (FrameDeciderPeer *o, const uint8_t *frame, int frame_len);
  189. #endif