dette er en test - Webstorage

Download Report

Transcript dette er en test - Webstorage

Protocol Design
Lesson 4
IHA præsentation
1
Outline for today
• Guidelines for implementing protocols
• Protocol Design Patterns
IHA præsentation
2
Guidelines for implementing protocols
IHA præsentation
3
Guidelines for implementing protocols
• Where is the protocol entity positioned in the complete
protocol stack
• Clear picture of the services our protocol provides
• Procedure/functionality descriptions
• Clear picture of which other protocol entities our
protocol interfaces to
• Service Access Points, service primitives or other
• Definition of messages/PDUs
• ASN.1, Augmented BNF or other
IHA præsentation
4
Guidelines for implementing protocols
• Consider Task and module decomposition
• Shall our protocol run as separate task?
• Interaction with the Operating System
• Memory management
• Dynamic memory allocation needed?
• Timer management
• Are timers needed?
• (Inter process communication)
• Queues, etc.
• (Security management)
• Passwords, authentication where are they stored?
IHA præsentation
5
Guidelines for implementing protocols
• Protocol states
• Finite State Machine (State-event machines)
• Identify all states and all internal events
– How do we do this?
– Message sequence charts (sequence diagrams) for
normal scenarios
– MSCs for timer expiry scenarios
– MSC for abnormal scenarios
IHA præsentation
6
Guidelines
• Task and module decomposition ?
• Protocol functions that need to manage their own timers
may have their own task
• Protocol functions that need to have their own
transmit/receive messages to/from peer protocol
entities may also have their own task
• However, consider context switching overhead
IHA præsentation
7
Traditionally……
Tasks
Guidelines
•Designing for Reentrancy
Application
Presentation
Session
UDP
TCP
Transport
Service
Users
ip_send() to send packets
Network
IP
Data Link
Service
Physical
IHA præsentation
9
Guidelines for implementing protocols
• We have protocol related design patterns?
• Are they useful?
• Can they handle all kind of protocols?
IHA præsentation
10
Protocol Design Patterns
IHA præsentation
11
• Protocol Design Patterns
• Protocol Layer Design Pattern
• Protocol Packet Design Pattern
• Protocol Stack Design Pattern
• Receive Protocol Handler
• Transmit Protocol Handler
• State Pattern
IHA præsentation
12
Protocol Layer Design Pattern
• Motivation
• A typical protocol layer interfaces with an upper and a lower layer of
the protocol stack
• In most designs there is a lot of dependency bewteen different
layers of the protocol
=> inflexibility
The Protocol Layer Design Pattern addresses these limitations by
decopling the individual protocol layers
IHA præsentation
13
Protocol Layer Design Pattern
Structure
Transmit
Handle Recieve
Layer N+1
• Communication between layers takes place using
standard interfaces defined by a Protocol Layer base
class
• All implementations of the protocol layer inherit from
this class
• The inheriting class should implement the standard
interfaces:
• Transmit is invoked by the upper layer to transfer
a packet to the lower layer
Transmit
Handle Recieve
Layer N
• Handle Receive is invoked by the lower layer to
transfer a packet to the upper layer
Layer N-1
IHA præsentation
14
Protocol Layer Design Pattern
Example
Transmitting Direction:
Network Layer
1. The application invokes the Network layer's Transmit
Transmit
Handle Recieve
method.
Transmit
Handle Recieve
DataLink Layer
Physical Layer
2. The Network layer performs its actions and invokes the
Transmit method for the lower layer.
3. This invokes the Datalink layers transmit method. The
Datalink layer performs the layer specific actions and
invokes the lower layer's Transmit method.
4.The Physical layer's Transmit method is invoked. This
layer programs the appropriate hardware device and
transmits the message.
IHA præsentation
15
Protocol Layer Design Pattern
Participants
• Protocol Layer: This is the base class for all protocol layers. The individual
layers interface with each other via pointers to this class. The actual type of the
upper layer and lower layer classes is not known to the implementers of a
certain layer.
• Protocol Packet: This class manages addition and removal of headers and
trailers for various protocol layers.
IHA præsentation
16
Protocol Layer Design Pattern
Consequences
• The implementation of one layer is completely
decoupled from the adjacent layers
Layer N+1
Transmit
Handle Recieve
• Layers can be added and removed without needing
any changes to the code for individual layers
layer without any changes to the IP or physical layer code
• A single layer could interface with multiple upper and
lower layer protocols using the same interface
Transmit
Layer N
Handle Recieve
• an IPsec layer can be added between IP and physical
• an IP layer could interface with an ATM or Ethernet
physical layer. No changes to the IP layer needed.
Layer N-1
IHA præsentation
17
Protocol Layer Design Pattern
#ifndef PROTOCOL_LAYER_H
#define PROTOCOL_LAYER_H
#include <stdio.h>
class Protocol_Packet;
class Protocol_Layer
{
Protocol_Layer *m_p_Lower_Layer;
Protocol_Layer *m_p_Upper_Layer;
public:
Protocol_Layer()
{
m_p_Lower_Layer = NULL;
m_p_Upper_Layer = NULL;
}
virtual void Transmit(Protocol_Packet *p_Packet) = 0;
virtual void Handle_Receive(Protocol_Packet *p_Packet) = 0;
void Set_Upper_Layer(Protocol_Layer *p_Layer)
{ m_p_Upper_Layer = p_Layer; }
void Set_Lower_Layer(Protocol_Layer *p_Layer)
{ m_p_Lower_Layer = p_Layer; }
Protocol_Layer *Get_Upper_Layer() const
{ return m_p_Upper_Layer; }
Protocol_Layer *Get_Lower_Layer() const
{ return m_p_Lower_Layer; }
};
#endif
IHA præsentation
18
Protocol Packet Design Pattern
• Motivation
•
•
A protocol stack generally handles multiple layers of a protocol
Each layer adds its own headers and trailers
• Size of the buffer containing the message keeps changing
•
In most implementations this results in each layers allocating a new buffer to
adjust the changed buffer size
The Protocol Packet Design Pattern addresses this issue with a
simple and efficient buffering architecture
IHA præsentation
19
Protocol Packet Design Pattern
Structure
This pattern is implemented by just one class, the Protocol Packet. This class works on a
raw buffer that is capable of holding the entire packet with protocol headers added for all
the layers in the protocol stack. The raw buffer is dynamically partitioned into three
regions:
• Header
• Body
• Trailer
As the message moves from one layer the other the location of the different regions is
adjusted. Let see:
IHA præsentation
20
Protocol Packet Design Pattern
Transmitting a Packet
Layer 1 Header Region
Layer 2 Header Region
Layer 3 Header Region
Application Body Region
Layer 3 Body Region
Layer 2 Body Region
Layer 1 Body Region
Transmitted Body Region
Layer 3 Trailer Region
Layer 2 Trailer Region
Layer 1 Trailer Region
1.
The Protocol Packet object is constructed with just the application body. Notice that the body does not start from the
first byte of the buffer. The application body is placed at an offset, leaving enough space for the protocol headers. At
this point, the header and trailer regions are of zero size.
2.
The packet is passed to Layer 3. This layer adds its own headers and trailers regions into the same buffer.
3.
Layer 2 adds its own headers and trailers regions. The previous header and trailer regions get merged into the body
4.
Layer 1 adds headers and trailers. Again, the body region grows to accommodate the headers and trailers for Layer 2
5.
Finally, zero length header and trailers are added, resulting in the entire packet moving to the body region. At this point
the header and trailer regions are of 0 length.
IHA præsentation
21
Protocol Packet Design Pattern
Receiving a Packet
Layer 1 Header Region
Layer 2 Header Region
Layer 3 Header Region
Received Bytes
(Body Region)
Layer 1 Body Region
Layer 2 Body Region
Layer 3 Body Region
Application Body Region
Layer 3 Trailer Region
Layer 2 Trailer Region
Layer 1 Trailer Region
1. The received packet is created with all the bytes in the body region of the message. At this point, the header and
trailer regions are of zero length.
2. Layer 1 extracts its headers and trailer regions. The two regions have been carved out of the received body
region. The size of the body region is reduced.
3. Layer 2 also extracts its own header and trailer regions. Again shrinking the body region.
4. Layer 3 similarly extracts its header and trailer regions. Shrinking the body to the original application body.
5. The application extracts a zero length header and trailer. This leaves only the packet with only the body region.
IHA præsentation
22
Protocol Packet Design Pattern
Participants
This pattern is implemented by just one class, the Protocol Packet. The class
has three internal constituent regions. These regions are defined by the Region
private structure.
Collaboration
The Protocol Packet class contains the header, body and trailer regions. This
relationship is shown in the following collaboration graph:
IHA præsentation
23
Protocol Packet Design Pattern
class Protocol_Packet
{
enum { MAXIMUM_PACKET_LENGTH = 1500};
struct Region
{
int offset;
int length;
};
Region m_header;
Region m_body;
Region m_trailer;
char m_buffer[MAXIMUM_PACKET_LENGTH];
…
}
Protocol Packet Design Pattern
Consequences
• Using this pattern provides allows efficient handling of packets as different
layers are added or extracted.
• A single buffer is used across layers. This reduces the overhead in buffer
processing
• In addition, this pattern brings uniformity to the design of the protocol stack.
IHA præsentation
25
Protocol Packet Design Pattern
Implementation
The Protocol Packet class consists of the following methods:
•Add_Header: Add the header to the transmit packet.
•Add_Trailer: Add the trailer to the transmit packet.
•Extract_Header: Extract the header from the received packet.
•Extract_Trailer: Extract the trailer from the received packet.
•Get_Header: Get a pointer to the current packet header.
•Get_Body: Get a pointer to the current packet body.
•Get_Trailer: Get a pointer to the current packet trailer.
•Get_Length: Get the total length. The length includes the header, body and trailer.
IHA præsentation
26
Protocol Stack Design Pattern
We have already seen that Protocol Layer and Protocol Packet
provide a standardized interface between different layers of a
protocol.
The Protocol Stack design pattern takes advantage of the layer
decoupling and provides a mechanism for dynamic insertion and
removal of protocol layers from a stack
IHA præsentation
27
Protocol Stack Design Pattern
•
Motivation
• Protocol stacks tend to be rigid in design and protocol layers cannot be dynamically
added or removed from a protocol stack
• Limits the use of protocol stacks in the even changing world of protocol standards
Example:
The user has enabled encryption and this requires the sandwiching of the encryption
layer between the network layer and the data-link layer
The Protocol Stack Design Pattern addresses this issue and
introduces a flexible architecture for dynamic addition and removal
of protocol layers
IHA præsentation
28
Protocol Stack Design Pattern
Structure
The Protocol Stack Design Pattern is implemented by the Protocol Stack class.
This class maintains a doubly linked list of active layers.
Participants
The key actors of this design pattern:
• Protocol Stack: This class maintains a doubly linked list of Protocol layers. It supports
dynamic addition and removal of protocol layers.
• Protocol Layer: This is the base class for all protocol layers. The individual layers
interface with each other via pointers to this class. The actual type of the upper layer and
lower layer classes is not known to the implementers of a certain layer.
IHA præsentation
29
Protocol Stack Design Pattern
Consequences
The Protocol Stack design pattern breaks down the rigid protocol layer
structure and provides a very flexible solution where layers can be dynamically
added and removed from the stack.
Examples
IHA præsentation
30
Protocol Stack Design Pattern
Transmit
Handle Recieve
Network Layer
A debug pass-through layer that displays the
messages being exchanged between the datalink
layer and the physical layer
Transmit
Handle Recieve
Datalink Layer
Transmit
Handle Recieve
Debug Layer
Physical Layer
IHA præsentation
31
Protocol Stack Design Pattern
Transmit
Handle Recieve
Network Layer
A loopback layer that facilitates the testing of the
datalink and network layers by just looping back
all transmitted messages back for receive
Transmit
Handle Recieve
Datalink Layer
Loopback Layer
IHA præsentation
32
Protocol Stack Design Pattern
Transmit
Handle Recieve
Echo-back Layer
An echo-back layer allows the protocol stack to
emulate a node by just echoing back all higher
layer messages back for transmission.
Transmit
Handle Recieve
Network Layer
Transmit
Handle Recieve
Datalink Layer
Physical Layer
IHA præsentation
33
Protocol Stack Design Pattern
Transmit
Handle Recieve
Network Layer
Transmit
Handle Recieve
Datalink Layer
An encryption layer sandwiched between the
datalink and physical layers. This layers encrypts
and decrypts data that is passed between these
layers
Transmit
Handle Recieve
Encryption Layer
Physical Layer
IHA præsentation
34
Protocol Stack Design Pattern
Implementation
The Protocol Stack is implemented as a single class. The class maintains a
doubly linked list of Protocol Layers. Important methods of the class are:
• Handle_Transmit: This handler is invoked by the application to transmit
messages using the protocol stack.
• Handle_Recieve: This handler is invoked by the device to pass received
messages to the protocol stack.
• Add_Layer: Add a protocol layer at a specific position in the protocol stack.
• Remove_Layer: Remove a layer from the protocol stack.
IHA præsentation
35
Protocol Stack Design Pattern
Discussion:
Any issues in implementing these patterns?
IHA præsentation
36
Protocol Stack Design Pattern
UMTS:
control
L3
control
control
control
control
RRC
PDCP
PDCP
L2/PDCP
BMC
L2/BMC
RLC
RLC
RLC
RLC
RLC
RLC
RLC
L2/RLC
RLC
Logical
Channels
MAC
L2/MAC
Transport
Channels
PHY
L1
Protocol Stack Design Pattern
DECT
Protocol Stack Design Pattern
ZigBee
Protocol Stack Design Pattern
Timeout
– send NWK PDU
Timer running
Network
When is control released
so we can handle timeout?
Link
Handle Receive
Transmit
MAC
010010100111000000
PHY
IHA præsentation
40
Protocol Stack Design Pattern
Receive Protocol Handler Pattern
• Motivation
• Different sliding window protocols have a lot of
similarity. This similarity can be captured in a common
design pattern for their implementation. Here we will
focus on the receive side of the protocol.
• Applicability
• Receive Protocol Handler Pattern can be used to
implement protocols at any layer.
IHA præsentation
42
Receive Protocol Handler Pattern
• Structure
This pattern is implemented by just one class, Receive Protocol
Handler. This class receives the packets from the other end and
performs the following operations:
• Check validity of the received packet
• Ask Transmit Protocol Handler to acknowledge the received
packet
• Check if the remote end has acknowledged that was sent by
Transmit Protocol Handler.
• Inform Transmit Protocol Handler about acknowledged packet.
IHA præsentation
43
Receive Protocol Handler Pattern
• To achieve this functionality, the following sequence numbers
are maintained:
• Next Expected Sequence Number: Transmit sequence number
expected in the next packet from the remote end.
• Last Acknowledged Sequence Number: Last receive sequence
number received from the remote end. This sequence number is
used by the remote end to acknowledge packets.
• Participants
• The Transmit and Receive Protocol Handlers are the main
participants in this pattern. The received messages are added to
the Receive Queue. The received message will be picked by the
next higher layer.
IHA præsentation
44
Transmit Protocol Handler
• Motivation
• Different sliding window protocols have a lot of
similarity. This similarity can be captured in a common
design pattern for their implementation. Here we will
focus on the transmit side of the protocol.
• Applicability
• Transmit Protocol Handler Pattern can be used to
implement protocols at any layer.
IHA præsentation
45
Transmit Protocol Handler
• Structure
• This pattern provides a framework for implementing a
sliding window protocol.
• The Transmit Protocol Handler receives a packet from
the higher layer and transmits it to the lower layer after
assigning a sequence number
• The packet is also stored in an internal retransmission
buffer.
• The packet is removed from the retransmission queue if
the remote end acknowledges the packet.
• The Transmit Protocol Handler retransmits the packet if
it times out for an acknowledgement.
IHA præsentation
46
Transmit Protocol Handler
• Participants
• The key actors of this design pattern:
• Transmit_Protocol_Handler: Class that manages the
transmit end of the protocol. This class interfaces with
the receive end and the retransmission queue.
• Transmit_Queue: Enqueues messages that wait for
transmission when the window is full.
• Retransmission_Buffer: Manages buffers until an
acknowledgement is received from the other end. The
messages are retransmitted If no ack is received, .
IHA præsentation
47
Example – Data Link Layer
Protocol_Layer
1
-m_p_Lower_Layer
-m_p_Upper_Layer
+Protocol_Layer()
+Transmit()
+Handle_Receive()
+Set_Upper_Layer()
+Set_Lower_Layer()
+Get_Upper_Layer()
+Get_Lower_Layer()
1
DataLink_Layer
-m_transmit_Protocol_Handler
-m_receive_Protocol_Handler
+DataLink_Layer()
+Tranmit()
+Handle_Receive()
1
1
1
1
1
Transmit_Protocol_Handler
-m_next_Transmit_Sequence_Number
-m_next_Receive_Sequence_Number
-m_Retransmission_Buffer
-m_Transmit_Queue
-m_p_Layer
+Transmit_Protocol_Handler()
+Handle_Transmit_Request()
+Handle_Send_Ack_Request()
+Handle_Received_Ack_Notification()
+Transmit_Packet()
1
Receive_Protocol_Handler
*
*
-m_next_Expected_Sequence_Number
-m_last_Acknowledged_Sequence_Number
-m_p_Transmit_Protocol_Handler
-m_p_Layer
+Handle_Received_Packet()
+Receive_Protocol_Handler()
Example – Data Link Layer
• A packet is received from the upper layer
1. The upper layer uses its "Lower Layer" pointer to invoke the
Transmit method for the lower layer. The "Protocol Packet" to
be transmitted is passed to the Transmit method.
2. This invokes the Datalink Layer's Transmit method.
3. The Datalink Layer passes the "Protocol Packet" to the
"Transmit Protocol Handler" object.
4. The "Transmit Protocol Handler" processes the "Protocol
Packet" and adds the datalink layer header to the packet.
5. The "Transmit Protocol Handler" uses its parent layer to obtain
a pointer to the lower layer.
6. The "Protocol Packet" is passed to the lower layer by invoking
the Transmit method.
IHA præsentation
49
Example – Data Link Layer
• A packet is received from the lower layer
1. The lower layer uses its "Upper Layer" pointer to invoke the
"Handle Receive" method for the upper layer. The received
"Protocol Packet" is passed to the "Handle Receive" method.
2. This invokes the Datalink Layer's "Handle Receive" method.
3. The Datalink Layer passes the "Protocol Packet" to the
"Receive Protocol Handler" object.
4. The "Receive Protocol Handler" object uses the parent layer to
obtain a pointer to the upper layer.
5. The "Protocol Packet" is passed to the higher layer by invoking
the "Handle Receive" method.
IHA præsentation
50
State Pattern