Distributed Systems
Protocol design
1
Contents
●
What is a protocol?
●
Syntax
●
Semantics
●
Synchronization
●
TFTP example
●
Example: Sockets
●
Example: Protocol Buffers
Distributed Systems :: Protocol Design 2
Protocol
Definition:
●
Set of rules that allows communication between
two or more entities
It involves three aspects:
●
Syntax
●
Semantics
●
Synchronization / timing
Usually written in a non-formal language (ie. human
language)
(we are going to see with a real example)
Distributed Systems :: Protocol Design 3
TFTP
An example
Trivial File Transfer Protocol
●
Very simple
●
Reliable (ACK based)
●
Encapsulated over UDP
●
Still commonly used
●
Firmware upload
Distributed Systems :: Protocol Design 4
Syntax
TFTP
Specifies the structure of messages:
●
Fields, data types, lengths (# bits/bytes)
TFTP message formats:
Type Op # Format without header
2 bytes string 1 byte string 1 byte
-----------------------------------------------
RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
WRQ -----------------------------------------------
2 bytes 2 bytes n bytes
---------------------------------
DATA | 03 | Block # | Data |
---------------------------------
2 bytes 2 bytes
-------------------
ACK | 04 | Block # |
--------------------
2 bytes 2 bytes string 1 byte
----------------------------------------
ERROR | 05 | ErrorCode | ErrMsg | 0 |
----------------------------------------
Distributed Systems :: Protocol Design 5
Semantics
TFTP
Specifies meaning of fields, allowed values, etc.
TFTP message types:
opcode operation
1 Read request (RRQ)
2 Write request (WRQ)
3 Data (DATA)
4 Acknowledgment (ACK)
5 Error (ERROR)
TFTP Error Codes:
Value Meaning
0 Not defined, see error message (if any).
1 File not found.
2 Access violation.
3 Disk full or allocation exceeded.
4 Illegal TFTP operation.
5 Unknown transfer ID.
6 File already exists.
7 No such user.
Distributed Systems :: Protocol Design 6
Synchronization
TFTP
Specifies valid message interchange patterns,
communication phases, timers, states, etc.
TFTP upload file transfer:
1. Host A sends a "WRQ" to host B with source=A's TID,
destination = 69.
2. Host B sends a "ACK" (with block number=0) to host A with
source = B's TID, destination= A's TID.
3. Host A sends a “WRQ” (block number=1) with 512B in the DATA field
(if it is not the last message).
Distributed Systems :: Protocol Design 7
Synchronization
TFTP
Distributed Systems :: Protocol Design 8
Synchronization
TFTP
Distributed Systems :: Protocol Design 9
Synchronization
TFTP
Distributed Systems :: Protocol Design 10
Synchronization
TFTP
Distributed Systems :: Protocol Design 11
Synchronization
TFTP
Distributed Systems :: Protocol Design 12
Synchronization
TFTP
Distributed Systems :: Protocol Design 13
Synchronization
TFTP DIY
And to read a
512-byte file?
Distributed Systems :: Protocol Design 14
TFTP example DIY
Install server and client:
$ sudo apt install tftpd-hpa tftp
Put a file on server directory:
/srv/tftp/a-file
$ echo hi | sudo tee /srv/tftp/example-file
Download file with the client:
$ tftp 0.0.0.0
tftp> trace
Packet tracing on.
tftp> mode binary
tftp> get example-file
sent RRQ <file=example-file, mode=octet>
received DATA <block=1, 3 bytes>
Received 3 bytes in 0.0 seconds
tftp>
Distributed Systems :: Protocol Design 15
TFTP example DIY
Distributed Systems :: Protocol Design 16
Standard (RFC1350): TFTP
https://datatracker.ietf.org/doc/html/rfc1350
Distributed Systems :: Protocol Design 17
Other examples
●
IP
https://datatracker.ietf.org/doc/html/rfc791
●
TCP
https://datatracker.ietf.org/doc/html/rfc9293
●
ICMP
https://datatracker.ietf.org/doc/html/rfc792
Distributed Systems :: Protocol Design 18
Design the protocol
Exercise DIY
Instawhat is a social network where a user can
post a photograph they found on the internet by
providing its public URL. For that photograph,
other users can comment, rate, or give it a "like."
Additionally, the user who posted the photo can
delete it from the social network, although it
will still exist on the external site. The social
network only allows you to see the last 20
photos published by any user.
Distributed Systems :: Protocol Design 19
Steps to design a protocol
A protocol design is not much different to an API or class
interface design.
●
Functionality overview. What is the protocol for?
●
Semantics
●
Involved entities and their relations.
●
Services provided by each entity.
●
Synchronization
●
Request/reply/ack patterns (if required) per each service.
●
Syntax
●
Data types and formats for any of the fields and messages.
Distributed Systems :: Protocol Design 20
Other considerations
Some non-functional aspects affecting the
design:
●
Security
●
Confidentiality: encryption, entity validation
●
Integrity: error detection/correction
●
Extendability
●
Efficiency
●
Marshaling formats: binary, XML, JSON, text.
(python example)
Distributed Systems :: Protocol Design 21
A marshaling example:
Google Protocol Buffers
Mechanism for serializing structured data (Google)
●
Binary marshalling: small messages and fast
processing
●
Backward compatibility: new protocol version should
work with legacy programs.
●
Multi-language support: Java, Python, Objective-C, C++, etc.
[https://protobuf.dev/]
Source: dzone
JSON request was 789 bytes versus the
Protobuf at 518 bytes.
Distributed Systems :: Protocol Design 22
Protocol Buffers
●
Manage marshaling from the programming
language structures to binary sequences and
vice-versa.
●
Builtin types:
●
bool, string, int32, int64, float, double, etc.
●
Enumerations, nested, etc.
Distributed Systems :: Protocol Design 23
Protocol Buffers
specification input data
syntax = "proto3"; {
"userName": "Martin",
message Person { "favouriteNumber": 1337,
string user_name = 1; "interests": [
int64 favourite_number = 2; "daydreaming", "hacking"
repeated string interests = 3; ]
} }
Python
Distributed Systems :: Protocol Design 24
Protocol Buffers
specification input data
syntax = "proto3"; {
"userName": "Martin",
message Person { "favouriteNumber": 1337,
string user_name = 1; "interests": [
int64 favourite_number = 2; "daydreaming", "hacking"
repeated string interests = 3; ]
} }
marshaling
Source: massivetechinterview.blogspot.com.es
https://protobuf.dev/programming-guides/proto3/
Distributed Systems :: Protocol Design 25
Python struct
Example DIY
Description: UDP client issuing sensor readings to server
● [examples:sockets.struct]
See and play with:
Run server: ● udp-server.py
● udp-client.py
socket.struct$ ./udp-server.py
New message ('127.0.0.1', 36137)
Sensor 8 (2) value:16.30 bar
Run client:
socket.struct$ ./udp-client.py localhost
b'\x00\x08\x02A\x82ff\x03bar'
Distributed Systems :: Protocol Design 26
Protocol Buffers
Example DIY
Description: UDP client issuing sensor readings to server
● [examples:sockets.protobuf]
Compile:
See and play with:
● sensor.proto
socket.protobuf$ make
protoc -I . --python_out=. sensor.proto ● udp-server.py
● udp-client.py
Run server:
socket.protobuf$ ./udp-server.py
sensor: ('127.0.0.1', 53957),
raw-data: b'\x08\x01\x10\x01\x1d\xcd\xccL>"\x05kg/m3'
Sensor 1 (HUMIDITY) value:0.20 kg/m3
Run client:
socket.protobuf$ ./udp-client.py localhost
Distributed Systems :: Protocol Design 27
What you have learned?
●
Open and public protocols decouple implementations
●
Provide transparency
●
Ensure interoperability
●
Protocols are contracts among services and clients
●
Protocol specifications require:
●
Syntax, Semantics and Synchronization
●
Marshalling format election impacts on efficiency
Distributed Systems :: Protocol Design 28
References
G. Coulouris, Distributed Systems: Concepts and
Design, Addison Wesley 2011
●
Section 4.3 – External data representation and
marshalling
29
Distributed Systems :: Protocol Design 2929