Skip to content

Commit

Permalink
modify switch lab again
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzhif committed Apr 29, 2024
1 parent 3529a7b commit 7303a33
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 189 deletions.
Binary file modified docs/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/doctrees/lab-manuals/switch.doctree
Binary file not shown.
Binary file modified docs/html/_downloads/351bad4fb3354110d8e0e1a2f06f64d2/sr.zip
Binary file not shown.
Binary file modified docs/html/_downloads/ae817651748e51cb80f89ec3d5797c56/switch.zip
Binary file not shown.
90 changes: 30 additions & 60 deletions docs/html/_sources/lab-manuals/switch.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Overview
========


交换机工作在数据链路层,在交换机收到数据帧后,根据帧的目的MAC地址和交换机内部的帧交换表对帧进行处理,包括:明确的转发,即交换机知道应当从哪个/哪些端口转发该帧(单播,多播,广播);盲目的转发,即交换机不知道应当从哪个端口转发帧,只能将其通过除进入交换机的接口外的其他所有接口转发(也称为泛洪);明确的丢弃,即交换机知道不应该转发该帧,将其丢弃。交换机是一种即插即用的设备,其内部的帧交换表是通过自学习算法自动地逐渐建立起来的。
交换机工作在数据链路层,在交换机收到数据帧后,根据帧的目的MAC地址和交换机内部的转发表对帧进行处理,包括:明确的转发,即交换机知道应当从哪个/哪些端口转发该帧(单播,多播,广播);盲目的转发,即交换机不知道应当从哪个端口转发帧,只能将其通过除进入交换机的接口外的其他所有接口转发(也称为泛洪);明确的丢弃,即交换机知道不应该转发该帧,将其丢弃。交换机是一种即插即用的设备,其内部的帧交换表是通过自学习算法自动地逐渐建立起来的。

在这个实验中,你的任务是实现一个搭建在OpenNetLab上的交换机,该交换机具有自学习功能,可以逐渐学习MAC地址对应的端口以建立帧交换表
在这个实验中,你的任务是实现一个搭建在OpenNetLab上的交换机,该交换机具有自学习功能,可以逐渐学习MAC地址对应的端口以建立转发表


Getting Started
Expand All @@ -26,7 +26,6 @@ Getting Started

- :file:`main.py` :本地调试运行文件。
- :file:`framegenerator.py` :帧发送方文件,无需修改。
- :file:`endpoint.py` :帧接收方文件, `TODO` 部分待编写。
- :file:`switch.py` : 自学习的MAC交换机文件,TODO部分待编写。
- :file:`testcases.json` :本地评测配置文件。
- :file:`tester` :本地评测运行文件,使用说明见相关文档。
Expand All @@ -40,69 +39,45 @@ Getting Started
Tasks
-----

在这个实验中,framegenerator从待发送的帧列表中获取endpoint的MAC地址,并向其发送帧,你需要按照如下规则实现endpoint.py中的put方法和switch.py中的recv方法的设计
在这个实验中,framegenerator负责向switch发送帧,switch负责根据转发表完成帧的转发,你需要按照如下规则实现switch.py中的recv方法及相关细节的设计

- 在endpoint.py中,put方法表示接收来自switch的帧,并根据帧的frame_type进行相应处理。
- 首先,你需要根据实际需求,选择合适的数据结构实现switch的forward_table(转发表),要求表项包含TTL字段,即用于记录表项的超时时间
- recv方法表示交换机接收到帧后的处理过程,具体包括:

- 若frame_type为DATA,打印收到的数据帧Id
- 若frame_type为BROADCAST

- 检查目的mac地址是否与endpoint的mac地址相同,若相同则返回一个ACK帧给switch
- 否则,不做处理

- 在switch.py中,recv方法表示接收来自framegenerator的数据帧和endpoint确认帧,并根据帧的frame_type进行相应处理。

- 首先,你需要根据实际需求,选择合适的数据结构先实现switch的forward_table(转发表),要求至少包含mac地址和对应的端口号
- 若frame_type为DATA
- 在交换机表中添加(或刷新)发送者的MAC地址和收到该帧接口的表项
- 使用该帧的目的MAC地址在转发表中检索
- 如果存在该地址的表项

- 检查forward_table中是否存在mac地址和对应的端口号
- 若存在,则将帧转发到对应的端口
- 否则,将广播帧转发到除源端口外的所有端口
- 如果表项的接口是收到帧的接口,丢弃该帧,不转发
- 否则,按表项中的接口转发帧

- 若frame_type为ACK

- 将帧的mac地址和对应的端口号添加到forward_table中
- 如果不存在该地址的表项,则泛洪,即将该帧从除了到达接口外的所有接口转发


该实验 `TODO` 部分的伪代码如下:

.. code-block:: text
# endpoint
function put(frame){
# process the reception of frames
receive frame from switch
if frame_type is DATA{
print frame_id
}
else if frame_type is BROADCAST{
check the destination mac_addr
if mac_addr is the same as endpoint's mac_addr{
return an ACK frame to switch
}
else{
pass
}
}
# switch
# switch.py
forward_table to be define #根据需求选择合适的数据结构实现转发表
function recv(frame){
function recv(frame,port_in) {
# process the reception of frames
receive frame from framegenerator or endpoint
if frame_type is DATA{
check forward_table
if mac_addr and it's port in forward_table{
forward the frame to the port
add or refresh the entry in forward_table according to the frame's source MAC address and port_in
if the frame's destination MAC address is in forward_table {
if the port_in is the same as the port in forward_table {
drop the frame
}
else{
forward BROADCAST frame to all the port except the source port
else {
forward the frame according to the port in forward_table
}
else {
flood the frame to all ports except port_in
}
else if frame_type is ACK{
add the entry to forward_table
}
function timeout_callback(mac_addr) {
# process the timeout of the entry in forward_table
remove the entry in forward_table according to the mac_addr
}
------------
Expand All @@ -128,7 +103,7 @@ Tips

客户端的进程;

.. py:attribute:: self.outs
.. py:attribute:: self.log
:noindex:

交换机转发帧的端口日志记录;
Expand All @@ -138,11 +113,6 @@ Tips

交换机的输出端口;

.. py:attribute:: self.frame_type
:noindex:

数据帧的类型,有DATA,BROADCAST,ACK三种;

.. py:function:: run(self, env)
:noindex:

Expand All @@ -157,19 +127,19 @@ Tips

@参数: msg - 需要打印的信息

.. py:function:: forward(self, port_num, frame)
.. py:function:: forward(self, port_id frame)
:noindex:

向给定端口转发帧,并记录转发的端口信息。

@参数: port_num - 转发端口号
@参数: port_id - 转发端口号

@参数: frame - 数据帧




2.framegenerator、switch、endpoint之间通过Wire或Cable完成帧的传输,其中framegenerator和switch之间有一条Wire,endpoint和switch之间各有一条Cable(可以进行双向传输)。
2.framegenerator、switch之间通过Cable完成帧的传输,switch的每个端口都连接一条Cable(可以进行双向传输)。

3.若switch.log与expected_res一致,则说明设计成功。

Expand Down
98 changes: 30 additions & 68 deletions docs/html/lab-manuals/switch.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,15 @@
<h1>Switch Lab<a class="headerlink" href="#switch-lab" title="Link to this heading"></a></h1>
<section id="overview">
<h2>Overview<a class="headerlink" href="#overview" title="Link to this heading"></a></h2>
<p>交换机工作在数据链路层,在交换机收到数据帧后,根据帧的目的MAC地址和交换机内部的帧交换表对帧进行处理,包括:明确的转发,即交换机知道应当从哪个/哪些端口转发该帧(单播,多播,广播);盲目的转发,即交换机不知道应当从哪个端口转发帧,只能将其通过除进入交换机的接口外的其他所有接口转发(也称为泛洪);明确的丢弃,即交换机知道不应该转发该帧,将其丢弃。交换机是一种即插即用的设备,其内部的帧交换表是通过自学习算法自动地逐渐建立起来的。</p>
<p>在这个实验中,你的任务是实现一个搭建在OpenNetLab上的交换机,该交换机具有自学习功能,可以逐渐学习MAC地址对应的端口以建立帧交换表</p>
<p>交换机工作在数据链路层,在交换机收到数据帧后,根据帧的目的MAC地址和交换机内部的转发表对帧进行处理,包括:明确的转发,即交换机知道应当从哪个/哪些端口转发该帧(单播,多播,广播);盲目的转发,即交换机不知道应当从哪个端口转发帧,只能将其通过除进入交换机的接口外的其他所有接口转发(也称为泛洪);明确的丢弃,即交换机知道不应该转发该帧,将其丢弃。交换机是一种即插即用的设备,其内部的帧交换表是通过自学习算法自动地逐渐建立起来的。</p>
<p>在这个实验中,你的任务是实现一个搭建在OpenNetLab上的交换机,该交换机具有自学习功能,可以逐渐学习MAC地址对应的端口以建立转发表</p>
<section id="getting-started">
<h3>Getting Started<a class="headerlink" href="#getting-started" title="Link to this heading"></a></h3>
<ol class="arabic simple">
<li><p><a class="reference download internal" download="" href="../_downloads/ae817651748e51cb80f89ec3d5797c56/switch.zip"><code class="xref download docutils literal notranslate"><span class="pre">下载实验资源</span></code></a> ,解压后进入文件夹,其中包含基础的实验代码模板。实验代码包含如下文件:</p>
<ul class="simple">
<li><p><code class="file docutils literal notranslate"><span class="pre">main.py</span></code> :本地调试运行文件。</p></li>
<li><p><code class="file docutils literal notranslate"><span class="pre">framegenerator.py</span></code> :帧发送方文件,无需修改。</p></li>
<li><p><code class="file docutils literal notranslate"><span class="pre">endpoint.py</span></code> :帧接收方文件, <cite>TODO</cite> 部分待编写。</p></li>
<li><p><code class="file docutils literal notranslate"><span class="pre">switch.py</span></code> : 自学习的MAC交换机文件,TODO部分待编写。</p></li>
<li><p><code class="file docutils literal notranslate"><span class="pre">testcases.json</span></code> :本地评测配置文件。</p></li>
<li><p><code class="file docutils literal notranslate"><span class="pre">tester</span></code> :本地评测运行文件,使用说明见相关文档。</p></li>
Expand All @@ -116,79 +115,48 @@ <h3>Getting Started<a class="headerlink" href="#getting-started" title="Link to
</section>
<section id="tasks">
<h3>Tasks<a class="headerlink" href="#tasks" title="Link to this heading"></a></h3>
<p>在这个实验中,framegenerator从待发送的帧列表中获取endpoint的MAC地址,并向其发送帧,你需要按照如下规则实现endpoint.py中的put方法和switch.py中的recv方法的设计</p>
<p>在这个实验中,framegenerator负责向switch发送帧,switch负责根据转发表完成帧的转发,你需要按照如下规则实现switch.py中的recv方法及相关细节的设计</p>
<ul>
<li><p>在endpoint.py中,put方法表示接收来自switch的帧,并根据帧的frame_type进行相应处理。</p>
<ul class="simple">
<li><p>若frame_type为DATA,打印收到的数据帧Id</p></li>
<li><p>若frame_type为BROADCAST</p>
<ul>
<li><p>检查目的mac地址是否与endpoint的mac地址相同,若相同则返回一个ACK帧给switch</p></li>
<li><p>否则,不做处理</p></li>
</ul>
</li>
</ul>
</li>
<li><p>在switch.py中,recv方法表示接收来自framegenerator的数据帧和endpoint确认帧,并根据帧的frame_type进行相应处理。</p>
<li><p>首先,你需要根据实际需求,选择合适的数据结构实现switch的forward_table(转发表),要求表项包含TTL字段,即用于记录表项的超时时间</p></li>
<li><p>recv方法表示交换机接收到帧后的处理过程,具体包括:</p>
<blockquote>
<div><ul>
<li><p>首先,你需要根据实际需求,选择合适的数据结构先实现switch的forward_table(转发表),要求至少包含mac地址和对应的端口号</p></li>
<li><p>若frame_type为DATA</p>
<li><p>在交换机表中添加(或刷新)发送者的MAC地址和收到该帧接口的表项</p></li>
<li><p>使用该帧的目的MAC地址在转发表中检索</p></li>
<li><p>如果存在该地址的表项</p>
<blockquote>
<div><ul class="simple">
<li><p>检查forward_table中是否存在mac地址和对应的端口号</p></li>
<li><p>若存在,则将帧转发到对应的端口</p></li>
<li><p>否则,将广播帧转发到除源端口外的所有端口</p></li>
</ul>
</div></blockquote>
</li>
<li><p>若frame_type为ACK</p>
<blockquote>
<div><ul class="simple">
<li><p>将帧的mac地址和对应的端口号添加到forward_table中</p></li>
<li><p>如果表项的接口是收到帧的接口,丢弃该帧,不转发</p></li>
<li><p>否则,按表项中的接口转发帧</p></li>
</ul>
</div></blockquote>
</li>
<li><p>如果不存在该地址的表项,则泛洪,即将该帧从除了到达接口外的所有接口转发</p></li>
</ul>
</div></blockquote>
</li>
</ul>
<p>该实验 <cite>TODO</cite> 部分的伪代码如下:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span># endpoint
function put(frame){
# process the reception of frames
receive frame from switch
if frame_type is DATA{
print frame_id
}
else if frame_type is BROADCAST{
check the destination mac_addr
if mac_addr is the same as endpoint&#39;s mac_addr{
return an ACK frame to switch
}
else{
pass
}
}


# switch
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span># switch.py
forward_table to be define #根据需求选择合适的数据结构实现转发表
function recv(frame){
function recv(frame,port_in) {
# process the reception of frames
receive frame from framegenerator or endpoint
if frame_type is DATA{
check forward_table
if mac_addr and it&#39;s port in forward_table{
forward the frame to the port
add or refresh the entry in forward_table according to the frame&#39;s source MAC address and port_in
if the frame&#39;s destination MAC address is in forward_table {
if the port_in is the same as the port in forward_table {
drop the frame
}
else{
forward BROADCAST frame to all the port except the source port
else {
forward the frame according to the port in forward_table
}
else {
flood the frame to all ports except port_in
}
else if frame_type is ACK{
add the entry to forward_table
}

function timeout_callback(mac_addr) {
# process the timeout of the entry in forward_table
remove the entry in forward_table according to the mac_addr
}
</pre></div>
</div>
</section>
Expand Down Expand Up @@ -217,7 +185,7 @@ <h2>Tips<a class="headerlink" href="#tips" title="Link to this heading"></a><

<dl class="py attribute">
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">self.</span></span><span class="sig-name descname"><span class="pre">outs</span></span></dt>
<span class="sig-prename descclassname"><span class="pre">self.</span></span><span class="sig-name descname"><span class="pre">log</span></span></dt>
<dd><p>交换机转发帧的端口日志记录;</p>
</dd></dl>

Expand All @@ -227,12 +195,6 @@ <h2>Tips<a class="headerlink" href="#tips" title="Link to this heading"></a><
<dd><p>交换机的输出端口;</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">self.</span></span><span class="sig-name descname"><span class="pre">frame_type</span></span></dt>
<dd><p>数据帧的类型,有DATA,BROADCAST,ACK三种;</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">env</span></span></em><span class="sig-paren">)</span></dt>
Expand All @@ -249,13 +211,13 @@ <h2>Tips<a class="headerlink" href="#tips" title="Link to this heading"></a><

<dl class="py function">
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">forward</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">port_num</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">frame</span></span></em><span class="sig-paren">)</span></dt>
<span class="sig-name descname"><span class="pre">forward</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">port_id</span> <span class="pre">frame</span></span></em><span class="sig-paren">)</span></dt>
<dd><p>向给定端口转发帧,并记录转发的端口信息。</p>
<p>&#64;参数: port_num - 转发端口号</p>
<p>&#64;参数: port_id - 转发端口号</p>
<p>&#64;参数: frame - 数据帧</p>
</dd></dl>

<p>2.framegenerator、switch、endpoint之间通过Wire或Cable完成帧的传输,其中framegenerator和switch之间有一条Wire,endpoint和switch之间各有一条Cable(可以进行双向传输)。</p>
<p>2.framegenerator、switch之间通过Cable完成帧的传输,switch的每个端口都连接一条Cable(可以进行双向传输)。</p>
<p>3.若switch.log与expected_res一致,则说明设计成功。</p>
</section>
<hr class="docutils" />
Expand Down
2 changes: 1 addition & 1 deletion docs/html/searchindex.js

Large diffs are not rendered by default.

Binary file modified lab-manuals/resources/sr.zip
Binary file not shown.
Binary file modified lab-manuals/resources/switch.zip
Binary file not shown.
Loading

0 comments on commit 7303a33

Please sign in to comment.