15-441 spring 06 project 2: network layer...2 forwarding •finds host b by looking up in a...

30
15-441 Spring 06 Project 2: Network Layer Mike Cui

Upload: others

Post on 05-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

15-441 Spring 06Project 2: Network Layer

Mike Cui

Page 2: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

1

Network Layer

• Gets data from host A to host B– Global addressing uniquely identifies A and B

– Data finds its way to host B, directly orindirectly

– Don’t care how how they are connected, aslong as they are.

• Where is host B, and how to get there?

Page 3: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

2

Forwarding• Finds host B by looking up in a forwarding table.

1.1.2.1

1.1.1.11 2

1.1.3.11

1

21.1.3.1

11.1.2.1

IFDest

11.1.3.1

11.1.1.1

IFDest

11.1.2.1

11.1.1.1

IFDest

Who makes theseforwarding tables?

Page 4: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

3

Routing

• Finds the paths to host B

• Fills in forwarding tables with the “best”path

• How?– Static

• Manually set it (part 1)

– Dynamic• Use a routing algorithm (part 2)

Page 5: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

4

Your Mission

• Implement the network layer for asimulated operating system kernel– IPv4 (RFC 791)

– Relay bytes between transport and physicallayers

– Forward packets between hosts

– Not required: fragmentation and reassembly

• Implement a simple routing daemon– Using OSPF protocol

Page 6: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

5

IPv4• You must handle

– Checksum– Header length– Packet length– Source and destination address– Protocol number– TTL– IP version number

• Your implementation need not handle– Fragmentation– Options– Multicast/broadcast– ToS

Page 7: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

6

When You Are All Done

Host

Host Host

routed

routed App App routed

SocketTransportNetwork

LinkPhysical

SocketTransportNetwork

LinkPhysical

SocketTransportNetwork

LinkPhysical

Page 8: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

7

Page 9: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

8

Simulator: Logical View

Host

Host Host

AppApp

App App App App

SocketTransportNetwork

LinkPhysical

SocketTransportNetwork

LinkPhysical

SocketTransportNetwork

LinkPhysical

Page 10: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

9

Simulator: Implementation

SimulatorKernel

SimulatorKernel

SimulatorKernel

App

App

AppApp

App

Legend (Shapes)

Unix Process

Unix IPC

App

Page 11: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

10

Simulator: Full Picture

SocketTransportNetwork

LinkPhysical

SocketTransportNetwork

LinkPhysical

SocketTransportNetwork

LinkPhysical

Legend (Shapes)

Unix Process

Unix IPC

Legend (Colors)

User

Kernel

NetworkStack

Page 12: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

11

Link

Sending a Packet

Transport

ip_output()

ForwardingTable

InterfaceList

IP_NOROUTE

ifp->if_start()

Add IP header

!IP_NOROUTE

Page 13: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

12

Receiving a Packet

Transport

ForwardingTable

Link

ip_input()

InterfaceList

For me?

Yes!

ip_forward()

No…

ifp->if_start()

Strip IP header

Modify IP header(TTL, checksum)

udp_receive()

Page 14: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

13

• Linked list of fixed-size (512 byte) buffers

• Why?– Dynamic, variable-sized memory allocation is

expensive• Takes time, function of size of heap

• Wastes space due to fragmentation

pbuf

512 512 512 512

Page 15: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

14

Inside a pbufp_next

p_nextpkt

p_data

p_len

p_type

p_flags

….

Next pbuf of this packet

Next packet in list of packets

First byte of data in this pbuf

Length of data in this pbuf

There’s room togrow!

User-definedp_dat

Page 16: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

15

pbuf Chain

Packet 1

Packet 2

p_len is the length of a pbuf

p_pktlen() is total lengthof data in the packet

sizeof(struct pbuf)

p_nextpkt

p_next

Page 17: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

16

IP Interface• When ip_input() is called…

– p_data points to beginning of IP header– Strip off IP header before passing onto transport

layer

• When ip_output() is called…– p_data points be beginning of IP payload– Prepend an IP header before handing to link layer– Can assume there’s enough room to prepend a IP

header• Should not need to allocate more pbufs

• Helper functions in pbuf.h– p_prepend, p_strip

Page 18: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

17

Connecting to the Simulator• #include <project2/include/Socket.h>

• Use Socket-API functions with first letter capitalized– Socket(), Bind(), Connect() …

• Make sure to Close()– Simulator isn’t an operating system, it doesn’t clean up after you

Page 19: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

18

Testing Your Network Layer

• Use fdconfig to set up static routes

• Try UDP applications– unreliable-server,unreliable-client

– Your P1 TFTP server (single client)

Page 20: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

19

Simulator Internals• Multithreaded implementation

– System call interface• User processes must register with simulator

• One thread to handle registration requests

• One thread per process to handle system calls

– Network devices• One thread per network device

• Wakes up when packet arrives

• What does this mean for you?– Your code will be executed by multiple threads..

– Your code must be re-entrant!

Page 21: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

20

Concurrency Reminder

• What you thinkticket = next_ticket++; /* 0 ⇒ 1 */

• What really happens (in general)ticket = temp = next_ticket; /* 0 */

++temp; /* invisible to other threads */

next_ticket = temp; /* 1 is visible */

Page 22: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

21

Murphy's Law (Of Threading)• The world may arbitrarily interleave execution

– Multiprocessor• N threads executing instructions at the same time

• Of course effects are interleaved!

– Uniprocessor• Only one thread running at a time...

• But N threads runnable, timer counting down toward zero...

• The world will choose the most painfulinterleaving– “Once chance in a million” happens every minute

Page 23: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

22

Your Hope

21Final Value

2n_tkt = tmp

2++tmp;

1tkt = tmp =n_tkt;

1n_tkt = tmp;

1++tmp;

0tkt = tmp =n_tkt;

T1T0

Page 24: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

23

Your Bad Luck

11Final Value

1n_tkt = tmp

1n_tkt = tmp;

1++tmp;

1++tmp;

0tkt = tmp =n_tkt;

0tkt = tmp =n_tkt;

T1T0

Two threads have the same “ticket” !

Page 25: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

24

What To Do

• What you thinkMUTEX_LOCK(m);

ticket = next_ticket++;

MUTEX_UNLOCK(m);

• Now no other thread's execution of the“critical section” can be interleaved withyours

Page 26: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

25

IP Dataflow Revisited

Transport

ip_output()

Link ifp->if_start()

ip_input() ip_forward()

Two threads of execution!

Problem! Link layercan only send outone packet at a time.

Page 27: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

26

One at a Time• Only one thread can send through a particular

device at a time– Otherwise the device will fail and cause kernel to

panic.

• Need mutual exclusion– Use a mutex (pthread_mutex_t) for each device– Declared in if.h, Mutex wrappers in systm.h

MUTEX_LOCK(&ifp->if_mutex);

ifp->start(ippkt);…

MUTEX_UNLOCK(&ifp->if_mutex);

Critical section! Mutexensures that only onethread can enter at a time.

Page 28: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

27

IP Dataflow Revisited (again)

Transport

Link ifp->if_start()

Transport

Socket Socket

Link ifp->if_start()

App App

ip_output()Two threads can call ip_output()concurrently, that should work!Make sure it does!

Page 29: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

28

Many at a Time

• More than one thread could invoke an IPlayer function at the same time– Each invocation has to be independent of one

another

– Each invocation needs to have its own state• Stack variables are independent, global variables

are shared

– Shared state needs to be protected

Page 30: 15-441 Spring 06 Project 2: Network Layer...2 Forwarding •Finds host B by looking up in a forwarding table. 1.1.2.1 1.1.1.1 1 2 1.1.3.1 1 1 1.1.3.1 2 1.1.2.1 1 Dest IF 1.1.3.1 1

29

Debugging Multiple Threads

• Using gdb– info thread lists all running threads

– thread n switches to a specific thread

– bt to get stack trace for the current thread

– Look for the function thread_name in stacktrace, name of thread is in the argument

• “link n:i to k:j” device thread for interface i on noden to interface j on node k

• “user_pid” system call handling thread for userprocess pid.