All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mac_proxy.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cvmx.h>
4 #include <cvmx-wqe.h>
5 #include <cvmx-rwlock.h>
6 
7 #define PORT_NOT_INIT 9999 // port value when not initialized
8 //#define NROFHASHENTRIES_2POW 8 // 256 entries in hash table (2^8=256)
9 
10 #define NEIGHBOR_NS_TIME 10000 // milliseconds (10 seconds)
11 #define NEIGHBOR_USED_TIME 240000 // milliseconds (4 minutes)
12 #define NEIGHBOR_PREF_TIME 600000 // milliseconds (10 minutes)
13 #define NEIGHBOR_VALID_TIME 1200000 // milliseconds (20 minutes)
14 #define NEIGHBOR_PENDING_TIME 60000 // milliseconds (1 minute)
15 #define DESTINATION_CACHE_TIME NEIGHBOR_USED_TIME-60000 // milliseconds (3 minutes)
16  // should be smaller then neigbor_used_time
17 /*
18 #define NEIGHBOR_NS_TIME 1000 // time in milliseconds (1 second)
19 #define NEIGHBOR_USED_TIME 30000 // time in milliseconds (30 seconds)
20 #define NEIGHBOR_PREF_TIME 60000 // time in milliseconds (1 minutes)
21 #define NEIGHBOR_VALID_TIME 120000 // time in milliseconds (2 minutes)
22 #define NEIGHBOR_PENDING_TIME 600 // 1 minute pending
23 #define DESTINATION_CACHE_TIME 18000 // time in milliseconds (0,5 minutes)
24 */
25 
26 
27 class CMACProxy
28 {
29  public:
30 
31  struct TPortConfig
32  {
33  uint16_t uiPortNr; // GBE0, GBE1..
34  uint64_t ui64Mac;
35  uint16_t ui16Vlan;
37  uint8_t ui8MaskBits;
39 
40  };
41 
42  CMACProxy(); // constructor
43  void Init(uint32_t ui32PortNr, uint16_t ui16NrOfCacheEntries, uint16_t ui16NrOfInterfaceEntries, uint16_t ui16NrOfRouterEntries, uint16_t ui16NrOfPrefixEntries,
44  CCoremask *pcCormask, bool bUseATOMIC_LOCK, bool bSlaacEnabled);
45  void Shutdown(void);
46  void ResetTables(void);
47  void ProcessTables();
48  bool AddVlan(uint16_t ui16Vlan);
49  bool RemoveVlan(uint16_t ui16Vlan);
50  bool AddIpAddress(uint16_t ui16Vlan, TIPAddr *ptAddr, uint8_t bMaskBits, TIPAddr *ptGateway);
51  bool RemoveIpAddress(uint16_t ui16Vlan, TIPAddr *ptAddr);
52  int GetPortNr();
53  bool GetMac(TIPAddr *ptSrcIp, TIPAddr *ptDstIp, uint16_t vlan, TMac *ptMac);
54  void HandleICMP(TICMP6Header *pIcmp, int iLen, TIPv6Header *pIpHeader, uint16_t vlan);
55  bool SearchForVlanInIP(TIPAddr *ptAddr, uint16_t *pVlan, TIPAddr *ptGateway);
56  bool CheckIncomingIp(TIPAddr *ptAddr, uint16_t Vlan);
57  void ShowTables();
58  bool InterfaceIpIsOwn(TIPAddr *ptAddr, uint16_t *pVlan, int *piStartIndex);
59  bool InterfaceIpIsOwn(TIPAddr *ptAddr);
60  bool IsLocal(TIPAddr *ptAddr);
61  bool PrefixTableFindBestMatch(const TIPAddr *ptDstIp, uint16_t *pui16Vlan, uint8_t *pui8MaxBits, TIPAddr *ptMask);
62  bool InterfaceTableFindIp(const TIPAddr *ptDstIp, TIPAddr *ptMask, uint16_t ui16Vlan, TIPAddr *ptIpIf);
63  bool GetPortConfig(int iIndex, int *piNextIndex, CMACProxy::TPortConfig *pConfig);
64 
65 // static uint16_t CheckSum (TIPv6Header *ph, uint8_t *pbuffer, bool bDebug);
66 
67  private:
68 
69  static uint32_t PseudoHeaderChecksum(TIPv6Header *ph);
70  static uint16_t CheckSum (TIPv6Header *ph, uint8_t *pbuffer);
71  bool SendICMPEchoReply(TICMP6Header *pIcmp, int iLen, TIPAddr * pSrc);
72  void BitsToMask(uint8_t bits, TIPAddr *ptMask);
73  bool IsGlobal(TIPAddr *ptAddr);
74  bool IsUnspecified(TIPAddr *ptAddr);
75  bool IsSolicitedMulticast(TIPAddr *ptAddr);
76  bool IsMulticast(TIPAddr *ptAddr);
77 
78  // Configured addresses on adapter
79 
80  bool InterfaceAddIp(TIPAddr *ptIp, uint16_t ui16Vlan, uint32_t lifeTime, bool bIsTentative, bool bMulticast, bool bUserAssigned);
81  bool InterfaceCheckIp(TIPAddr *ptIp, uint16_t ui16Vlan);
82  bool InterfaceRemoveIp(TIPAddr *ptIp, uint16_t ui16Vlan);
83  void InterfaceRemoveVlan(uint16_t ui16Vlan);
84  bool InterfaceAddSlaacLinkLocal(uint16_t ui16Vlan);
85  bool InterfaceAddSlaacLinkLocalPrivate(uint16_t ui16Vlan);
86  bool InterfaceAddMulticastAddresses(uint16_t ui16Vlan);
87  bool InterfaceAddSollictatedNodeAddress(TIPAddr *ptIfIp, uint16_t ui16Vlan, bool bUserAssigned);
88  bool InterfaceRemoveSollictatedNodeAddress(TIPAddr *ptIfIp, uint16_t ui16Vlan);
89  void InterfaceDeletePrefix(uint32_t Vlan, TIPAddr *ptPrefix, uint8_t PrefixLength);
90  void InterfaceDeleteIp(TIPAddr *ptAddr);
91  bool InterfaceAddGlobal(uint64_t ui64MyMac, TIPAddr *ptPrefix, uint16_t Vlan, uint8_t PrefixLength, uint32_t lifeTime);
92  bool InterfaceIpIsTentative(TIPAddr *ptAddr);
93  bool InterfaceGetLocal(uint16_t Vlan, TIPAddr *ptLocalIp);
94  void InterfaceReset();
95  void InterfacetableProcess();
96  void InterfacetablePrint();
97  uint64_t InterfaceGetMAC(void);
98 
99 
100  // Destination cache
101 
102  bool DestinationTableAdd(TIPAddr *ptDst, uint16_t Vlan, TMac *ptMac);
103  void DestinationTableRemove(TIPAddr *ptDst, uint16_t Vlan);
104  void DestinationTableRemove(uint16_t Vlan);
105  bool DestinationTableFind(TIPAddr *ptDst, uint16_t Vlan, TMac *ptMac);
106  void DestinationTableClear();
107  void DestinationTableProcess();
108  void DestinationTablePrint();
109 
110  // Prefix table
111 
112  bool PrefixTableAdd(TIPAddr *ptPrefix, uint16_t Vlan, uint8_t PrefixLength, uint32_t lifeTime );
113  bool PrefixTableAdd(TIPAddr *ptPrefix, uint16_t Vlan, uint8_t PrefixLength, TIPAddr *ptGateway);
114  void PrefixTableRemove(TIPAddr *pIp, uint16_t Vlan);
115  void PrefixTableRemove(uint16_t Vlan);
116  void PrefixTableDelete(TIPAddr *ptPrefix, uint8_t PrefixLength, uint16_t Vlan);
117  void PrefixTableReset();
118  bool PrefixIsLocal(TIPAddr *ptIp, uint16_t Vlan);
119  bool PrefixTableFind(uint16_t Vlan, TIPAddr *ptSrc, TIPAddr *ptGateway,uint16_t *pMTU, uint8_t *pHopLimit);
120  void PrefixTableProcess();
121  void PrefixTablePrint();
122 
123  // Subnet table
124 
125  bool NeighborTableAdd(TIPAddr *ptDst, TIPAddr *ptSrc, uint16_t Vlan, TMac tMac);
126  void NeighborTableRemove(TIPAddr *ptDst,uint16_t Vlan);
127  void NeighborTableRemove(uint16_t Vlan);
128  bool NeighborTableFind(TIPAddr *ptDst, TIPAddr *ptSrc, uint16_t Vlan, TMac *ptMac);
129  void NeighborTableClear();
130  void NeighborTableProcess();
131  void NeighborTablePrint();
132 
133  // Routing table
134 
135  bool RoutingTableAdd(TIPAddr *ptIp, uint16_t Vlan, TCMP6RouterAdvertisement *pRA);
136  void RoutingTableRemove(uint16_t Vlan);
137  void RoutingTableRemove(TIPAddr *ptIp, uint16_t Vlan);
138  bool RoutingTableChange(TIPAddr *ptIp, uint16_t Vlan, uint16_t Mtu);
139  void RoutingTableChange(TIPAddr *ptIp, uint16_t Vlan,TIPAddr *ptPrefix);
140  void RoutingTableClear();
141  bool RoutingTableFind(uint16_t Vlan, TIPAddr *ptGateway,uint16_t *pMTU, uint8_t *pHopLimit);
142  bool RoutingTableGetTimerValues(uint16_t Vlan, uint32_t *pRetranstime, uint32_t *pReachableTime );
143  void RoutingTableProcess();
144  void RoutingTablePrint();
145 
146 
147  bool SendNS(TIPAddr *ptSrcLocalLink,TIPAddr *ptDstLocalLink, uint16_t vlan, bool bDAD);
148  bool SendRS(TIPAddr *ptDstLocalLink, uint16_t vlan);
149  bool SendNA(TIPAddr *pSrc,uint16_t vlan);
150  bool SendNA(TIPAddr *pDst, TIPAddr *pSrc, TMac tDstMac, uint16_t vlan, bool bSolicitated);
151  bool ProcessEchoRequest(uint64_t ui64Header, int iTotalLength, TIPv6Header *pIp, uint16_t vlan);
152  void ReceiveRA(uint64_t ui64Header, int iTotalLength, TIPv6Header *pIp, uint16_t vlan);
153  void ReceiveNA(uint64_t ui64Header, int iTotalLength, TIPv6Header *pIp, uint16_t vlan);
154  void ProcessNS(uint64_t ui64Header, int iTotalLength, TIPv6Header *pIp, uint16_t vlan);
155 
156 
157  // I P S t a t e
158  // -------------
159  //
160  enum EIPState
161  {
162  EIP_PENDING, // ns pending
163  EIP_INVALID, // don't use
164  EIP_VALID, // we can use it
165  EIP_EXPIRED, // we still can use it but expired
166  };
167 
168 
169  // T D e s t i n a t i o n C a c h e
170  // ---------------------------------
171  // Contains information about destination to which traffic has
172  // been sent to recently
173  // Holds the mac address of the next hop location for a destination ip
174  //
175  //
176  struct TDestinationCacheEntry
177  {
178  TIPAddr tIpAddr;
179  uint16_t ui16Vlan; // vlan for this entry ,use 0 is for no vlan;
180  uint64_t CacheTimeout; // timertick value for timeout of the cache entry
181  TMac tNextHopMac; // next hop mac address
182  };
183 
184  uint32_t m_ui32DestCacheEntryCount;
185 
186 
187  struct TCompare
188  {
189  uint64_t ui64DstAddrHigh;
190  uint64_t ui64DstAddrLow;
191  uint16_t ui16Vlan;
192 
193  bool operator==(const TCompare& tCompare) const
194  {
195  return ((tCompare.ui64DstAddrHigh == ui64DstAddrHigh) && (tCompare.ui64DstAddrLow == ui64DstAddrLow) && (tCompare.ui16Vlan == ui16Vlan)) ? true : false;
196  }
197  bool operator!=(const TCompare& tCompare) const
198  {
199  return ((tCompare.ui64DstAddrHigh != ui64DstAddrHigh) || (tCompare.ui64DstAddrLow != ui64DstAddrLow) || (tCompare.ui16Vlan != ui16Vlan)) ? true : false;
200  }
201  };
202 
203 
204  // T N e i g h b o r C a c h e
205  // ---------------------------
206  // Contains on-link information
207  // Equivalent to ARP cache
208  // Stores the mac adress for an known ip address
209  //
210  struct TNeighborCacheEntry
211  {
212  TIPAddr tIpFrom; // ip address where we received the Mac from (used for neighbor solicitate)
213  TIPAddr tIpAddr; // destination ip
214  uint16_t ui16Vlan; // vlan for this entry ,use 0 for no vlan;
215  uint64_t NSRateTimeout; // timertick value to wait for the nex Neighbor Solicitate message
216  uint64_t LastUseTime; // timertick value will be reset every query
217  uint64_t LifeTimeout; // prefered and valid timer
218  // bool bInfinitTimeout; // if timeout == 0XFFFFFFFF then true
219  TMac tMac; // link layer address for ip
220  EIPState eIpState;
221 
222  }*m_ptNeighborList;
223 
224  uint32_t m_ui32NeigborCacheEntryCount;
225 
226 
227  // T D e f a u l t R o u t e r L i s t
228  // -----------------------------------
229  // Contains a list of default routers
230  // Maintained by the router
231  //
232  struct TDefaultRouterListEntry
233  {
234  bool bInUse; // true when entry is used
235  TIPAddr tIpAddr;
236  uint16_t ui16Vlan; // vlan for this entry ,use 0 is for no vlan;
237  uint16_t MTU;
238  uint8_t Flags; // auto configuration flags : 8 bits M,O,H,P1,P0,R2,R1,R0 :P
239  uint8_t HopLimit; // current recommended hop limit
240  uint32_t ReachableTime; // Reacheable time of neighbor (milliseconds)
241  uint32_t RetransTime; // timer for retransmitting neighbor solicitation messages (milliseconds)
242  uint64_t LifeTimeout; // timertick timeout value
243  bool bDefaultRouter; // Use as default router
244 
245  } *m_ptRouterList;
246 
247  uint32_t m_ui32DefaultRouterListCount;
248 
249 
250  // T P r e f i x L i s t
251  // ---------------------
252  // A list with prefixes for current interface
253  // Used to determine if a gateway has to be used
254  // This is as small list, so no hash needed
255  // Also used with the src ip to determine the gateway to be used
256  // Depends on the nr of matching prefixes
257  // Only gateway configured for static prefixes (bIfnitTimeout)
258  // No gateway configured for src ip then use default router
259  //
260  struct TPrefixListEntry
261  {
262  bool bInUse;
263  uint16_t ui16Vlan; // vlan for this entry ,use 0 for no vlan;
264  TIPAddr tPrefix; // used prefix
265  TIPAddr tPrefixMask; // mask constructed from the prefix length
266  uint8_t PrefixLength; // nr of prefix bits
267  uint64_t LifeTimeout; // timertick timout value
268  bool bInfinitTimeout; // if timeout was == 0XFFFFFFFF then true
269  TIPAddr tGateWay; // static gateway address, only used for infinit timeout
270  uint16_t MTU; // only used for infinit timeout
271  uint16_t HopLimit; // only used for infinit timeout
272 
273  } *m_ptPrefixList;
274 
275  uint32_t m_ui32PrefixListCount;
276 
277 
278  // On-link
279  // -------
280  // 1) covered by one of the on-link prefixes assigned to the link
281  // 2) target address of a redirect message sent by a router
282  // 3) target address of a Neighbor Advertisement
283  // 4) source address of a Neighbor Discovery message received
284 
285 
286  enum EAddressState
287  {
288  EAS_TENTATIVE0 = 0,
289  EAS_TENTATIVE1 = 1,
290  EAS_TENTATIVE2 = 2,
291  EAS_VALID = 3,
292  };
293 
294 
295  // T I n t e r f a c e I n f o ( )
296  // -------------------------------
297  // Hold the configured and SLAAC addresses assigned to
298  // the current interface
299  //
300  struct TInterfaceInfo
301  {
302  bool bInUse; // true when entry is used
303  bool bIsMulticast; // multicast address
304  // bool bIsLocal; // local address
305  bool bIsUserAddr; // address assigned by user
306  TIPAddr tIpAddr; // Ip address
307  uint16_t ui16Vlan; // vlan for this entry ,use 0 for no vlan;
308  uint64_t LifeTimeout; // timertick timeout value
309  bool bInfinitTimeout; // if timeout was == 0XFFFFFFFF then true
310  EAddressState eAddressState; // tentative until DAD (Duplicate Address Detection) passed
311  uint64_t StateTimeout; // timertick state timeout value
312 
313  } *m_ptInterfaceInfo;
314 
315 
316  // T G l o b a l V a r ( )
317  // -----------------------
318  //
319  struct TGlobalVar
320  {
321  cvmx_rwlock_wp_lock_t tInterface_lock; // Lock for Interface table
322  cvmx_rwlock_wp_lock_t tRouterList_lock; // Lock for RouterList table
323  cvmx_rwlock_wp_lock_t tPrefixList_lock; // Lock for PrefixList table
324  int CurrNeighborRow; // processing current row in neighbor table
325  int CurrDestinationRow; // processing current row in destination table
326 
327  }*m_pGlobalVarStorage;
328 
329  uint64_t m_ui64TicksPerMs;
330 
331  uint32_t m_ui32PortNr;
332  uint32_t m_ui32IpOffset; // Offset for start of data of IP packet in pWqe->packet_data
333  uint16_t m_ui16NrOfCacheEntriesPow2;
334  uint16_t m_ui16NrOfInterfaceEntries;
335  uint16_t m_ui16NrOfRouterEntries;
336  uint16_t m_ui16NrOfPrefixEntries;
337 
338  bool m_bUseATOMIC_LOCK;
339  bool m_bSlaacEnabled;
340 
341  uint64_t m_uxOneMiliSecond;
342 
343  CTashTable<TCompare, sizeof(TNeighborCacheEntry)> m_cNeighborCacheHash; // Hash table for NeighborCache
344  CTashTable<TCompare, sizeof(TDestinationCacheEntry)> m_cDestinationCacheHash; // Hash table for DestinationCache
345 
346  CCoremask *m_pcCoremask;
347 };
348 
bool GetPortConfig(int iIndex, int *piNextIndex, CMACProxy::TPortConfig *pConfig)
void Shutdown(void)
bool IsLocal(TIPAddr *ptAddr)
bool GetMac(TIPAddr *ptSrcIp, TIPAddr *ptDstIp, uint16_t vlan, TMac *ptMac)
void ProcessTables()
Struct contains an IPV4 or an IPV6 address.
Definition: packet-defines.h:7
bool AddIpAddress(uint16_t ui16Vlan, TIPAddr *ptAddr, uint8_t bMaskBits, TIPAddr *ptGateway)
void HandleICMP(TICMP6Header *pIcmp, int iLen, TIPv6Header *pIpHeader, uint16_t vlan)
void Init(uint32_t ui32PortNr, uint16_t ui16NrOfCacheEntries, uint16_t ui16NrOfInterfaceEntries, uint16_t ui16NrOfRouterEntries, uint16_t ui16NrOfPrefixEntries, CCoremask *pcCormask, bool bUseATOMIC_LOCK, bool bSlaacEnabled)
int GetPortNr()
bool AddVlan(uint16_t ui16Vlan)
void ResetTables(void)
bool CheckIncomingIp(TIPAddr *ptAddr, uint16_t Vlan)
bool SearchForVlanInIP(TIPAddr *ptAddr, uint16_t *pVlan, TIPAddr *ptGateway)
bool RemoveVlan(uint16_t ui16Vlan)
MAC address.
Definition: sir_ethernet.h:27
bool PrefixTableFindBestMatch(const TIPAddr *ptDstIp, uint16_t *pui16Vlan, uint8_t *pui8MaxBits, TIPAddr *ptMask)
void ShowTables()
bool InterfaceTableFindIp(const TIPAddr *ptDstIp, TIPAddr *ptMask, uint16_t ui16Vlan, TIPAddr *ptIpIf)
bool InterfaceIpIsOwn(TIPAddr *ptAddr, uint16_t *pVlan, int *piStartIndex)
bool RemoveIpAddress(uint16_t ui16Vlan, TIPAddr *ptAddr)