SONiC
=====
###### tags: `Networks`
contributed by < Dung-Ru Tsai >
- SONiC: Software for Open Networking in the Cloud https://github.com/Azure/SONiC/blob/master/sourcecode.md
- open source network operating system
- Official site: https://azure.github.io/SONiC/
- [OCP Summit 2016 SONiC Presentation Slides](https://github.com/Azure/SONiC/wiki/files/talks/SAI-SONiC-OCP-Summit-Mar16-shared.pdf)
# SONiC Feature:
- Use best-of-breed switching hardware for the various tiers of the network.
- Deploy new features without impacting end users.
- Roll out updates securely and reliably across the fleet in hours instead of weeks.
- Utilize cloud-scale deep telemetry and fully automated failure mitigation.
- Enable our Software-Defined Networking software to easily control all hardware elements in the network using a unified structure to eliminate duplication and reduce failures.
# Abbreviations:
- SAI: [Switch Abstraction Interface](https://github.com/opencomputeproject/SAI)
- SWSS: [SWitch State Service](https://github.com/Azure/sonic-swss)
- MPLS: Multi-Protocol Label Switching
- ACI: [Access Control List](https://tools.ietf.org/id/draft-ietf-netmod-acl-model-17.html)
- BGP: Border Gateway Protocol
- 802.1BR: Bridge Port Extension
- segment routing: https://www.segment-routing.net/
- BFD: Bidirectional Forwarding Detection
- LLDP: Link Layer Discovery Protocol
- NBIs: North Bound Interfaces
-
# SONiC System [Architecture](https://github.com/Azure/SONiC/wiki/Architecture)

## Syncd container:
syncd’s container goal is to provide a mechanism to allow the synchronization of the switch’s network state
with the switch’s actual hardware/ASIC.
This includes the initialization, the configuration and the collection of the switch’s ASIC current status.
### Main logical components:
- syncd: Process in charge of executing the synchronization logic mentioned above. At compilation time, syncd links with the ASIC SDK library provided by the hardware-vendor, and injects state to the ASICs by invoking the interfaces provided for such effect.
- SAI API: The Switch Abstraction Interface (SAI) defines the API to provide a vendor-independent way of controlling forwarding elements, such as a switching ASIC, an NPU or a software switch in a uniform manner.
- ASIC SDK: Hardware vendors are expected to provide a SAI-friendly implementation of the SDK required to drive their ASICs. This implementation is typically provided in the form of a dynamic-linkedlibrary which hooks up to a driving process (syncd in this case) responsible of driving its execution.
# SAI
The SAI interface provides a programmatic abstraction of a network forwarding element.
[Switch Abstraction Interface v0.9.2](https://github.com/opencomputeproject/SAI/blob/master/doc/spec.md)
[SAI Spec](https://github.com/opencomputeproject/SAI/blob/master/doc/spec.md)


- **Adapter** is a pluggable code module, supplied by either a vendor or control plane stack owner, that contains either ASIC SDK code itself or client module for ASIC SDK hosted in external process and implements the interfaces described in this specification; for all practical purposes it is equivalent of a **“user-mode driver”**.
- **Adapter host** is a Microsoft or vendor-supplied component that loads the adapter and exposes its functionality to the control plane stack.
- **Control Plane stack** (**Control stack**) is a set of software components that interacts with Adapter Host and provides higher level programming support to network control applications.
- **Switching Entity** (**Forwarding Element**) is an instance of a switching object; it represents a physical ASIC or software switch.
### SAI Design Note:
**Switching adapters** are user-mode drivers, typically supplied by ASIC vendors. Adapters are registered with the switching stack and then can be loaded as needed. It is a responsibility of the adapter to discover and bind to the specified underlying hardware, including loading of or attaching to kernel-mode drivers if needed.
**Adapters are expected to be as simple as possible, ideally simple wrappers around vendor’s SDKs.** Our design strives to push the bookkeeping complexity from adapter into the adapter host wherever possible.
The adapter module is loaded into a hosting process **(“adapter host”)** and then initialized. During initialization the adapter initiates discovery process of the specified instance of a switching entity. A switching entity is a top-level object in this API.
There cloud be multiple Adapter Hosts running at the same time. **Each Adapter Host is responsible for managing a portion of functions within the ASIC.** For example, an IP routing Adapter Host is responsible for syncing IP routes to the ASIC, while a counter Adapter Host is responsible for reading counters from
the ASIC.
Code Download:
`git clone https://github.com/opencomputeproject/SAI.git --recursive `
Code Trace:
```clike=
sai_api_initialize()
sai_api_query()
sai_api_uninitialize()
```
### Example`test/saithrift/src/saiserver.cpp`
- `sai_api_initialize => create_sai_adapter()` will init the Host adapter.
> Not sure what is it exactly doing
- We need prepare the `sai_service_method_table_t`
```clike=
sai_api_initialize(0, (service_method_table_t *)&test_services);
sai_api_query(SAI_API_SWITCH, (void**)&sai_switch_api);
```
- After initialization, sai_api_query() can be used for retrieval of various methods tables for SAI functionalities.
- The first query always be **SAI_API_SWITCH**.
- `sai_api_query` get the HW function' API.
## [SAI-P4-BM](https://github.com/Mellanox/SAI-P4-BM) (real implement case)

- [ ] I think it is the example from [SONiC P4 Software Switch](https://github.com/Azure/SONiC/wiki/SONiC-P4-Software-Switch)
### p4-swtch module: (Hardware Describe Model)
Dependecies:
- [behavioral model](https://github.com/p4lang/behavioral-model/tree/75ce6338139d71102c9e056f3d27d7c0047c1bf7)
- Check the travis folder to install dependencies.
- [Apache Thrift](https://github.com/apache/thrift)
- [Debian/Ubuntu install](https://thrift.apache.org/docs/install/debian.html)
- Please use [0.12.0](https://github.com/apache/thrift/tree/0.12.0)
- `./configure --with-qt5=no`
:::warning
Missing libffi-dev
vim /etc/apt/sources.list
`deb http://archive.ubuntu.com/ubuntu vivid main restricted universe
`sudo apt-get update`
`sudo apt-get install libffi-dev`
:::
P4-Switch install
```bash=
./autogen
./configure --without-targets
make && sudo make install
```
### SAI adapter:
SAI Adapter [install instructions](https://github.com/opencomputeproject/SAI/tree/master/bm/sai_adapter)
1. install_deps.sh will install python depencdecies.
Python depencdecies
```bash=
python3 -m pip install --user ctypesgen
sudo apt install python-thrift python-scapy
```
2. `./autogen.sh`
3. `./configure`
4. `make`
5. `make install` will install sai lib to default lib folder (usually /usr/local/lib/).
## Low Level Implement: [Mellanox src](https://github.com/Mellanox/SAI-P4-BM/tree/master/sai_adapter/src), [OCP SAI](https://github.com/opencomputeproject/SAI/tree/master/bm/sai_adapter)
- `bm/sai_adapter/inc/sai_adapter_interface.h`
- Host interface: `bm/sai_adapter/src/saihostif.cpp`
## Upper layer API export
[Switch Abstraction Interface v0.9.2](https://github.com/opencomputeproject/SAI/blob/master/doc/spec.md)
# The following materials are worth reading:
- [High Performance Browser Networking](https://hpbn.co/)
- http://www.tcpipguide.com/ to dive deeper to the TCP if interested
- [diagnosing-network-issues-with-mtr](https://www.linode.com/docs/networking/diagnostics/diagnosing-network-issues-with-mtr/)
- [High Performance Browser Networking](https://learning.oreilly.com/library/view/high-performance-browser/9781449344757/)
- [Computer Networks (TCP)](https://www.oreilly.com/library/view/computer-networks-5th/9780123850591/)
- [HTTP The Definitive Guide](https://www.oreilly.com/library/view/http-the-definitive/1565925092/)
- [DevOps Troubleshooting: Linux® Server Best Practices](https://learning.oreilly.com/library/view/devops-troubleshooting-linux/9780133035513/)