目录
1.写在前面
2.安装工作
2.1.mininet安装
2.2 cmake安装
2.3 xterm安装
2.4 wireshark安装
3.作业要求:(交换机转发实验)
4.C语言完成函数编写工作
附件展示:
4.1 iface_info_t *lookup_port(u8 mac[ETH_ALEN]);
4.2 void insert_mac_port(u8 mac[ETH_ALEN], iface_info_t *iface);
4.3 int sweep_aged_mac_port_entry();
4.4 void broadcast_packet(iface_info_t *iface, const char *packet, int len);
4.5 void handle_packet(iface_info_t *iface, char *packet, int len);
5.验证函数,检验实验结果
5.1 在当前目录下执行命令make,会生成一些必要的文件包括最重要的switch文件
5.2 打开mininet
5.3 利用xterm打开s1终端,并且在s1终端中执行switch(上面生成的那个)
5.4 wireshark分别监听h2和h3两个主机(&表示后台运行)
5.5 用h1 分别ping h2和h3 如果在wireshark监听中只看到了自己节点和h1节点发送的数据,表明我们实验成功了
这个是我们的一个课程大作业,需要组队完成,但是奈何没找到队友,只有自己solo了,这个作业时间大概给了五周还是六周,但是实际上没有太多工作量,这个是好几个实验选择做的,我就多做了几个,写博客记录一下自己的实验过程。mininet这个工具我以前没有用过,所以也出了不少问题。也算是调试的一个心理路程。
github:https://github.com/Suyebiubiu/Switch-based-Mininet
这个实验主要是需要在linux中运行的,我的系统用的Ubuntu,mininet应该是必须要在linux中运行,可以快速搭建模拟网络的平台,推荐使用Ubuntu系统,版本号从14.04到最新的都可以,64位或者32位都行。如果物理机是windows系统的话,可以使用虚拟机方式安装 Linux系统,推荐使用VirtualBox虚拟机 。运行Mininet环境时需要root权限,Mininet脚本只能使用 Python2来解释运行
mac.c中的方法
// lookup the mac address in mac_port table // FDB中寻找目的地址+port是否存在? iface_info_t *lookup_port(u8 mac[ETH_ALEN]) { // iface_info_t *iface = NULL; // fprintf(stdout, "TODO: implement this function please.\n"); // return iface; iface_info_t *iface = NULL; mac_port_entry_t *entry,*q; for (int i = 0; i < HASH_8BITS; i++) { list_for_each_entry_safe(entry, q, &mac_port_map.hash_table[i], list) { int cmp = memcmp((void*)entry->mac,(void*)mac,sizeof(u8)*ETH_ALEN); //ETH_ALEN==6 if(cmp==0) return entry->iface; } } return iface; }mac.c中的方法
// insert the mac -> iface mapping into mac_port table //FDB插入packet源地址+port void insert_mac_port(u8 mac[ETH_ALEN], iface_info_t *iface) { // fprintf(stdout, "TODO: implement this function please.\n");' mac_port_entry_t *entry = malloc(sizeof(mac_port_entry_t)); bzero(entry, sizeof(mac_port_entry_t)); time_t now = time(NULL); entry->visited = now; memcpy(entry->mac,mac,sizeof(u8)*ETH_ALEN); // memcpy(entry->iface,iface,sizeof(iface_info_t)); entry->iface=iface; list_add_tail(&entry->list,&mac_port_map.hash_table[0]); //我加在第一张hash table可以? 为什么hash_table存储了所有的entry? }mac.c中的方法
// sweeping mac_port table, remove the entry which has not been visited in the // last 30 seconds. int sweep_aged_mac_port_entry() { // int n = 0; // fprintf(stdout, "TODO: implement this function please.\n"); // return n; int n=0; mac_port_entry_t *entry, *q; time_t now = time(NULL); for (int i = 0; i < HASH_8BITS; i++) { list_for_each_entry_safe(entry,q, &mac_port_map.hash_table[i],list) { if((int)(now - entry->visited) >= MAC_PORT_TIMEOUT){ n = entry->iface->index; list_delete_entry(&entry->list); free(entry); return n; } } } return n; }packet.c中的方法
// broadcast the packet among all the interfaces except the one receiving the // packet, and free memory of the packet //在所有的接口中广播(不包含当前接口) void broadcast_packet(iface_info_t *iface, char *packet, int len) { iface_info_t *tx_iface = NULL; list_for_each_entry(tx_iface, &instance->iface_list, list) { if (tx_iface->index == iface->index) continue; iface_send_packet(tx_iface, packet, len); } }main.c中的方法
// handle packet // 1. if the dest mac address is found in mac_port table, forward it; otherwise, // broadcast it. // 2. put the src mac -> iface mapping into mac hash table. void handle_packet(iface_info_t *iface, char *packet, int len) { //得到头部信息 struct ether_header *eh = (struct ether_header *)packet; //fdb中寻找目的地址mac iface_info_t *tx_iface = lookup_port(eh->ether_dhost); if (tx_iface) { iface_send_packet(tx_iface, packet, len); } else { broadcast_packet(iface, packet, len); } //存入源mac+port if (!lookup_port(eh->ether_shost)) { insert_mac_port(eh->ether_shost, iface); } }
大功告成!撒花✿✿ヽ(°▽°)ノ✿!!!!!!!!!!!!!!!!!
