-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlab6-Network Driver.html
523 lines (248 loc) · 25.7 KB
/
lab6-Network Driver.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Lab6: Network Driver · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-intopic-toc/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="prev" href="lab5-File system, Spawn and Shell.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
序言
</a>
</li>
<li class="chapter " data-level="1.2" data-path="introduction.html">
<a href="introduction.html">
introduction
</a>
</li>
<li class="chapter " data-level="1.3" data-path="preparation.html">
<a href="preparation.html">
准备工作
</a>
</li>
<li class="chapter " data-level="1.4" >
<span>
JOS
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="lab1-Booting a PC.html">
<a href="lab1-Booting a PC.html">
Lab1: Booting a PC
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="lab2-Memory Management.html">
<a href="lab2-Memory Management.html">
Lab2: Memory Management
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="Lab3-User Environments.html">
<a href="Lab3-User Environments.html">
Lab3: User Environments
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="lab4-Preemptive Multitasking.html">
<a href="lab4-Preemptive Multitasking.html">
Lab4: Preemptive Multitasking
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="lab5-File system, Spawn and Shell.html">
<a href="lab5-File system, Spawn and Shell.html">
Lab5: File system, Spawn and Shell
</a>
</li>
<li class="chapter active" data-level="1.4.6" data-path="lab6-Network Driver.html">
<a href="lab6-Network Driver.html">
Lab6: Network Driver
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" >
<span>
xv6
</span>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="hw-boot xv6.html">
<a href="hw-boot xv6.html">
boot xv6
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="hw-syscall.html">
<a href="hw-syscall.html">
syscall
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="hw-alarmtest.html">
<a href="hw-alarmtest.html">
alarmtest
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >Lab6: Network Driver</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="lab-6-network-driver">Lab 6: Network Driver</h1>
<p>这一部分感觉最重要的就是对硬件的驱动过程有了一个更加深刻的认识,本实验相当于是去实现一个网卡的驱动,并且在这个驱动上,开发相应的应用程序(<code>echosrv</code>和<code>httpd</code>)。</p>
<p>首先是<code>pci_init()</code>,这一部分主要就是扫描PIC总线上的一些设备,与系统内核中带有的驱动程序进行匹配。</p>
<p>通过PCI的扫描,我们发现qemu提供下面几种设别:</p>
<p><img src="lab6-Network Driver/pci.png" alt=""></p>
<p>本实验主要是实现网卡E1000的驱动,通过查阅相关<a href="https://pdos.csail.mit.edu/6.828/2018/readings/hardware/8254x_GBe_SDM.pdf" target="_blank">intel网卡的手册</a>,发现Vendor ID和Device ID如下所示:</p>
<p><img src="lab6-Network Driver/id.png" alt=""></p>
<p>PCI主要就是通过上面的两个变量进行匹配的,一旦匹配,说明系统中存在该设备的驱动,就能够利用该设备了。</p>
<p>其他的设备由于没有在系统中完成相关的驱动程序,所以也没有设置Vendor ID和Device ID。</p>
<p>有了设备后,为了能够方便对设备进行操作,我们使用lab1提到的MMIO(VGA)和lab4中对多核CPU lapic的初始化。MMIO主要的作用就是像是操作内存一样的去操作硬件,实际上访问内存地址的时候,访问的是硬件的寄存器地址。</p>
<p>但是MMIO地址空间和大小并不是系统进行决定的,而是和设备进行协商进行确定(感觉是要是知道MMIO空间的大小),因此主要工作就是初始化这个结构体:</p>
<pre><code class="lang-c"><span class="hljs-keyword">struct</span> pci_func {
<span class="hljs-keyword">struct</span> pci_bus *bus; <span class="hljs-comment">// Primary bus for bridges</span>
<span class="hljs-keyword">uint32_t</span> dev;
<span class="hljs-keyword">uint32_t</span> func;
<span class="hljs-keyword">uint32_t</span> dev_id;
<span class="hljs-keyword">uint32_t</span> dev_class;
<span class="hljs-keyword">uint32_t</span> reg_base[<span class="hljs-number">6</span>];<span class="hljs-comment">//最多6个MMIO区域</span>
<span class="hljs-keyword">uint32_t</span> reg_size[<span class="hljs-number">6</span>];<span class="hljs-comment">//每个MMIO区域的大小</span>
<span class="hljs-keyword">uint8_t</span> irq_line;<span class="hljs-comment">//中断的地址线,上面PCI线扫描显示网卡的中断使用的是11</span>
};
</code></pre>
<p>然后系统通过这个结构体的信息,来初始化e1000信息,这些初始化的信息是直接写到硬件中的(虽然感觉像是操作内存变量)。</p>
<pre><code class="lang-c">pci_e1000_attach(<span class="hljs-keyword">struct</span> pci_func * pcif)
{
pci_func_enable(pcif);
init_desc();
e1000 = mmio_map_region(pcif->reg_base[<span class="hljs-number">0</span>], pcif->reg_size[<span class="hljs-number">0</span>]);
cprintf(<span class="hljs-string">"e1000: bar0 %x size0 %x\n"</span>, pcif->reg_base[<span class="hljs-number">0</span>], pcif->reg_size[<span class="hljs-number">0</span>]);
e1000[TDBAL/<span class="hljs-number">4</span>] = PADDR(tx_d);
e1000[TDBAH/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
e1000[TDLEN/<span class="hljs-number">4</span>] = <span class="hljs-function">TXRING_LEN * <span class="hljs-title">sizeof</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> tx_desc)</span></span>;
e1000[TDH/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
e1000[TDT/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
e1000[TCTL/<span class="hljs-number">4</span>] = TCTL_EN | TCTL_PSP | (TCTL_CT & (<span class="hljs-number">0x10</span> << <span class="hljs-number">4</span>)) | (TCTL_COLD & (<span class="hljs-number">0x40</span> << <span class="hljs-number">12</span>));
e1000[TIPG/<span class="hljs-number">4</span>] = <span class="hljs-number">10</span> | (<span class="hljs-number">8</span> << <span class="hljs-number">10</span>) | (<span class="hljs-number">12</span> << <span class="hljs-number">20</span>);
<span class="hljs-comment">//接收端的设置</span>
e1000[RA/<span class="hljs-number">4</span>] = mac[<span class="hljs-number">0</span>];
e1000[RA/<span class="hljs-number">4</span>+<span class="hljs-number">1</span>] = mac[<span class="hljs-number">1</span>];
e1000[RA/<span class="hljs-number">4</span>+<span class="hljs-number">1</span>] |= RAV;
cprintf(<span class="hljs-string">"e1000: mac address %x:%x\n"</span>, mac[<span class="hljs-number">1</span>], mac[<span class="hljs-number">0</span>]);
<span class="hljs-built_in">memset</span>((<span class="hljs-keyword">void</span>*)&e1000[MTA/<span class="hljs-number">4</span>], <span class="hljs-number">0</span>, <span class="hljs-number">128</span> * <span class="hljs-number">4</span>);
e1000[ICS/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
e1000[IMS/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
<span class="hljs-comment">//e1000[IMC/4] = 0xFFFF;</span>
e1000[RDBAL/<span class="hljs-number">4</span>] = PADDR(rx_d);
e1000[RDBAH/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
e1000[RDLEN/<span class="hljs-number">4</span>] = <span class="hljs-function">RXRING_LEN * <span class="hljs-title">sizeof</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> rx_desc)</span></span>;
e1000[RDH/<span class="hljs-number">4</span>] = <span class="hljs-number">0</span>;
e1000[RDT/<span class="hljs-number">4</span>] = RXRING_LEN - <span class="hljs-number">1</span>;
e1000[RCTL/<span class="hljs-number">4</span>] = RCTL_EN | RCTL_LBM_NO | RCTL_SECRC | RCTL_BSIZE | RCTL_BAM;
cprintf(<span class="hljs-string">"e1000: status %x\n"</span>, e1000[STATUS/<span class="hljs-number">4</span>]);
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
}
</code></pre>
<p>还需要说明的就是E1000网卡使用的是DMA技术与JOS进行交互。</p>
<p>也就是说,往内存里读写网络数据包不需要通过CPU,实际上还是通过MMIO这块区域进行操作。本实验基本上没有涉及DMA的代码,因此了解就好了。</p>
<p>因此,我们就能很轻松的使用这张网卡了。</p>
<p>下面是一些系统调用的实现。</p>
<p>首先是实现系统的定时中断。JOS中设置的是每250ms来让server进程检测是否有包要接受或者发送。(Linux系统中是4ms,精度可以调的更高,但是带来的开销也是越大的。从这里就可以看到Linux并不是一个实时的操作系统,而是一个分时操作系统,涉及到时间操作的精度并不是很高)。</p>
<p>然后实现发包,为什么呢?因为要是没有实现发包,那么收包也没有东西可以收。</p>
<p>这一部分的测试在<code>/net/testoutput.c</code>中,可以结合着测试的样例和具体的实现原理来阅读代码。</p>
<p>然后实现的是input的模块。</p>
<p>有了上面的四个模块基础之后,之后将会在这四个模块的基础之上,实现两个应用:<code>echosrv</code>和<code>httpd</code>。</p>
<p><code>echosrv</code>主要的作用就是收到一个连接的信息后,返回相同的信息。</p>
<p>服务端:</p>
<pre><code>make E1000_DEBUG=TX,TXERR,RX,RXERR,RXFILTER run-echosrv
</code></pre><p>客户端:</p>
<pre><code>make nc-7
</code></pre><p>下面说一说<code>echosrv</code>的建立连接的原理:</p>
<p>首先是向网络服务进程发送一个IPC请求,然后一直处于接受状态:</p>
<pre><code class="lang-c"><span class="hljs-keyword">int</span> res = ipc_recv(<span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>);
</code></pre>
<p>该等待是基于事件进行触发的,因此如果没有请求来,那么根本不会占用任何的计算资源(因为ipc_recv是将进程的状态设置为了curenv->env_status = ENV_NOT_RUNNABLE,因此每一次轮询都不会执行该进程)。</p>
<p>同理,<code>httpd</code>也是类似的作用,使用的是80端口进行服务,并且能通过url进行文件的访问。</p>
<p>这个实现的框架标记了实现的过程,结构如下:</p>
<p><img src="lab6-Network Driver/diag.png" alt=""></p>
<p><img src="lab6-Network Driver/result.png" alt=""></p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="lab5-File system, Spawn and Shell.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Lab5: File system, Spawn and Shell">
<i class="fa fa-angle-left"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Lab6: Network Driver","level":"1.4.6","depth":2,"next":{"title":"xv6","level":"1.5","depth":1,"ref":"","articles":[{"title":"boot xv6","level":"1.5.1","depth":2,"path":"hw-boot xv6.md","ref":"hw-boot xv6.md","articles":[]},{"title":"syscall","level":"1.5.2","depth":2,"path":"hw-syscall.md","ref":"hw-syscall.md","articles":[]},{"title":"alarmtest","level":"1.5.3","depth":2,"path":"hw-alarmtest.md","ref":"hw-alarmtest.md","articles":[]}]},"previous":{"title":"Lab5: File system, Spawn and Shell","level":"1.4.5","depth":2,"path":"lab5-File system, Spawn and Shell.md","ref":"./lab5-File system, Spawn and Shell.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["callouts@git+https://github.com/Simran-B/gitbook-plugin-callouts.git","intopic-toc","theme-comscore","-lunr","-search","search-pro","github","splitter","-sharing","mathjax-pro"],"pluginsConfig":{"callouts":{"showTypeInHeader":true},"github":{"url":"https://github.com/KnowledgeHive/ddia"},"intopic-toc":{"isCollapsed":false,"isScrollspyActive":true,"label":{"de":"导航","en":"导航","ch":"导航"},"maxDepth":6,"mode":"nested","selector":".markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section h5, .markdown-section h6","visible":true},"splitter":{},"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"theme-comscore":{},"mathjax-pro":{"forceSVG":false,"version":"2.7.7"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lab6-Network Driver.md","mtime":"2021-02-19T16:35:04.479Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-02-21T03:30:06.713Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-intopic-toc/anchor.min.js"></script>
<script src="gitbook/gitbook-plugin-intopic-toc/gumshoe.polyfills.min.js"></script>
<script src="gitbook/gitbook-plugin-intopic-toc/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
<script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
<script src="gitbook/gitbook-plugin-github/plugin.js"></script>
<script src="gitbook/gitbook-plugin-splitter/splitter.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="gitbook/gitbook-plugin-mathjax-pro/plugin.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
<script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
</body>
</html>