Prototype design and implementation of layer 2 switch

1, Overview

The UA programming of FAST architecture is very simple and has its fixed routine. There are two core parts: one is to initialize the environment in the main function, register the callback function of UA and configure the default rules of hardware; Second, concentrate on processing packet data in callback function to realize complete business function.

The packet reception of layer-2 switching is sent to the processing function by the system callback, and all logic related to subsequent switching is completed and implemented in this function.

2, Compile and verify the sample code

1) UA sample code

Network disk download address:

Extraction code: ehd7

Directory: FAST open source community / teaching cases / serial open class / prototype design and implementation of layer 2 switch

2) Code file description

The directory of layer 2 exchange code is: / home/hnxs/l2switch /, which includes two files, one of which is main_ul2switch.c mainly includes the platform processing code of UA and the empty callback function. The other is the Makefile file of C compilation file, which mainly explains how to compile and generate layer-2 exchange executable commands.

3) Description of compiled file

  gcc -o ul2switch main_ul2switch.c -lua -lreg -lpthread
  rm -rf ul2switch

The compilation of layer-2 switch requires the support of libreg and libua of FAST and libpthread of the system.

4) Compile and enter the following command in / home/hnxs/l2switch / Directory:

root@HNXS:/home/hnxs/l2switch# make

The system output is as follows:

gcc -o ul2switch main_ul2switch.c -lua -lreg -lpthread

The current directory will generate one more ul2switch file

root@HNXS:/home/hnxs/l2switch# ls
main_ul2switch.c  Makefile  ul2switch

5) To perform the verification, enter the following command in the / home/hnxs/l2switch / Directory:

root@HNXS:/home/hnxs/l2switch# ./ul2switch
fastU->REG Version:20180827,OpenBox HW Version:2020210329
fastU->Register UA to FAST Kernel! Wait Reply......
fastU->UA->pid:3069,mid:129,Register OK!
fastU->libua version:20180827

The execution result of the above platform code is displayed normally.

3, Sort out the code flow

1) main function of C program

/*UA Module initialization*/
/*Configure hardware default rules and send all hardware messages to the process with module ID of mid for processing*/
/*The startup thread receives the message assigned to the UA process*/
/*The main process enters the pause state, and the data processing is mainly in the callback function*/

2) Create UA and register callback

void ua_init(u8 mid)
  int ret = 0;
  /*Register with the system and process all messages with message module ID mid*/
  if((ret=fast_ua_init(mid,callback)))//UA module instantiation (input parameter 1: receiving module ID number, input parameter 2: callback processing function of receiving message)
    exit (ret);//If initialization fails, you need to print the failure information and exit the program!

3) callback handler

int callback(struct fast_packet *pkt,int pkt_len)
  return 0;

4, Verify data IO function

1) Print and receive packet metadata information

For the data format of FAST grouping, please refer to the first article< Prototype design and implementation of layer 2 switch >Description. At the beginning of the callback function, print the metadata information of FAST packet and the source and destination MAC address information of Ethernet protocol.


2) Call the send function to send the packet

Call the packet sending function of FAST to send a received packet from the specified port. Pay special attention to the setting of the metadata field.

void pkt_send_normal(struct fast_packet *pkt,int pkt_len)
  pkt->um.pktsrc = 1;/*The message source is CPU input, from the perspective of hardware*/
  pkt->um.pktdst = 0;/*The purpose of the message is hardware output*/
  pkt->um.dstmid = 5;/*Output directly from hardware GOE module without going through analysis, table lookup and other modules*/
  fast_ua_send(pkt,pkt_len);/*Call FAST API function to send*/

Before calling this function, you must set pkt - > um Assign a value to the output field to specify the output port number of the packet.

5, Summary and next steps

1) Core function callback

The callback function is the core function of the whole UA and the starting point of user service implementation. Although we have only done two things in this function today, one is to print the basic information of the received packet, and the other is to send the packet to the specified port. However, today we have implemented a prototype system with the simplest packet forwarding function on this platform.

2) Importance of notes and backups

I won't say much about re nature, but I just want to emphasize it here.

3) Realize simple switching logic function

On such a simple platform, it can quickly realize the packet receiving of hardware port and the packet sending of designated port. Has most of the problems of network function been solved? Do you only need to deal with the logic of the service, or do you only need to deal with the logic of the service? Is it ordinary layer 2 switching or SDN switching? Is it an ordinary layer 3 route, a lisp route or a segment route? Is everything possible?

Don't be happy too early. This is only the first step of the long march. The distance between the starting point and the ending point must be measured by your own steps, and no step can be less. So let's start with a simple layer-2 switch. The next article officially enters the design of packet switching.

You and students are welcome to join the FAST open source project group for communication and discussion, and experience different system design processes together. Please add wechat 15116127200 first and then invite to the group.

Focus on FAST open source official account


Added by abie10 on Wed, 09 Feb 2022 14:47:16 +0200