/projects/reference_router/test/both_router_table/run.py

https://github.com/Caustic/netfpga
Python | 114 lines | 80 code | 16 blank | 18 comment | 17 complexity | 67dac70042c9f7513808986118baa2fe MD5 | raw file
  1. #!/bin/env python
  2. from NFTest import *
  3. from NFTest.hwRegLib import *
  4. from RegressRouterLib import *
  5. phy2loop0 = ('../connections/2phy', [])
  6. nftest_init(sim_loop = [], hw_config = [phy2loop0])
  7. nftest_start()
  8. NUM_PORTS = 4
  9. def get_dest_MAC(i):
  10. i_plus_one = i + 1
  11. if i == 0:
  12. return "00:ca:fe:00:00:02"
  13. if i == 1:
  14. return "00:ca:fe:00:00:01"
  15. if i == 2:
  16. return "00:ca:fe:00:00:04"
  17. if i == 3:
  18. return "00:ca:fe:00:00:03"
  19. routerMAC = ["00:ca:fe:00:00:01", "00:ca:fe:00:00:02", "00:ca:fe:00:00:03", "00:ca:fe:00:00:04"]
  20. routerIP = ["192.168.0.40", "192.168.1.40", "192.168.2.40", "192.168.3.40"]
  21. ALLSPFRouters = "224.0.0.5"
  22. # Clear all tables in a hardware test (not needed in software)
  23. if isHW():
  24. nftest_invalidate_all_tables()
  25. # Write the mac and IP addresses
  26. for port in range(4):
  27. nftest_add_dst_ip_filter_entry (port, routerIP[port])
  28. nftest_set_router_MAC ('nf2c%d'%port, routerMAC[port])
  29. nftest_add_dst_ip_filter_entry (4, ALLSPFRouters)
  30. # router mac 0
  31. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_0_HI_REG(), 0xca)
  32. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_0_LO_REG(), 0xfe000001)
  33. # router mac 1
  34. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_1_HI_REG(), 0xca)
  35. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_1_LO_REG(), 0xfe000002)
  36. # router mac 2
  37. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_2_HI_REG(), 0xca)
  38. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_2_LO_REG(), 0xfe000003)
  39. # router mac 3
  40. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_3_HI_REG(), 0xca)
  41. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_MAC_3_LO_REG(), 0xfe000004)
  42. # add LPM and ARP entries for each port
  43. for i in range(NUM_PORTS):
  44. i_plus_1 = i + 1
  45. subnetIP = "192.168." + str(i_plus_1) + ".1"
  46. subnetMask = "255.255.255.225"
  47. nextHopIP = "192.168.5." + str(i_plus_1)
  48. outPort = 1 << (2 * i)
  49. nextHopMAC = get_dest_MAC(i)
  50. # add an entry in the routing table
  51. nftest_add_LPM_table_entry(i, subnetIP, subnetMask, nextHopIP, outPort)
  52. # add and entry in the ARP table
  53. nftest_add_ARP_table_entry(i, nextHopIP, nextHopMAC)
  54. # ARP table
  55. mac_hi = 0xca
  56. mac_lo = [0xfe000002, 0xfe000001, 0xfe000004, 0xfe000003]
  57. router_ip = [0xc0a80501, 0xc0a80502, 0xc0a80503, 0xc0a80504]
  58. for i in range(31):
  59. nftest_regwrite(reg_defines.ROUTER_OP_LUT_ARP_TABLE_RD_ADDR_REG(), i)
  60. # ARP MAC
  61. if i < 4:
  62. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_HI_REG(), mac_hi)
  63. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_LO_REG(), mac_lo[i])
  64. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ARP_TABLE_ENTRY_NEXT_HOP_IP_REG(), router_ip[i])
  65. else:
  66. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_HI_REG(), 0)
  67. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_LO_REG(), 0)
  68. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ARP_TABLE_ENTRY_NEXT_HOP_IP_REG(), 0)
  69. # Routing table
  70. router_ip = [0xc0a80101, 0xc0a80201, 0xc0a80301, 0xc0a80401]
  71. subnet_mask = [0xffffffe1, 0xffffffe1, 0xffffffe1, 0xffffffe1]
  72. arp_port = [1, 4]
  73. next_hop_ip = [0xc0a80501, 0xc0a80502, 0xc0a80503, 0xc0a80504]
  74. for i in range(31):
  75. nftest_regwrite(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_RD_ADDR_REG(), i)
  76. if i < 2:
  77. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_OUTPUT_PORT_REG(), arp_port[i])
  78. if i < 4:
  79. # Router IP
  80. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_IP_REG(), router_ip[i])
  81. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_NEXT_HOP_IP_REG(), next_hop_ip[i])
  82. # Router subnet mask
  83. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_MASK_REG(), subnet_mask[i])
  84. else:
  85. # Router IP
  86. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_IP_REG(), 0)
  87. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_NEXT_HOP_IP_REG(), 0)
  88. # Router subnet mask
  89. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_ROUTE_TABLE_ENTRY_MASK_REG(), 0xffffffff)
  90. # IP filter
  91. filter = [0xc0a80028, 0xc0a80128, 0xc0a80228, 0xc0a80328, 0xe0000005]
  92. for i in range(31):
  93. nftest_regwrite(reg_defines.ROUTER_OP_LUT_DST_IP_FILTER_TABLE_RD_ADDR_REG(), i)
  94. if i < 5:
  95. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_DST_IP_FILTER_TABLE_ENTRY_IP_REG(), filter[i])
  96. else:
  97. nftest_regread_expect(reg_defines.ROUTER_OP_LUT_DST_IP_FILTER_TABLE_ENTRY_IP_REG(), 0)
  98. nftest_finish()