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: https://pan.baidu.com/s/13zmKXeMnpUMsCiL5GAI7Vg

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

default:
  gcc -o ul2switch main_ul2switch.c -lua -lreg -lpthread
clean:
  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
fastU->fast_ua_recv......

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*/
ua_init(mid);
/*Configure hardware default rules and send all hardware messages to the process with module ID of mid for processing*/
fast_reg_wr(FAST_ACTION_REG_ADDR|FAST_DEFAULT_RULE_ADDR,ACTION_SET_MID<<28|mid);
/*The startup thread receives the message assigned to the UA process*/
fast_ua_recv();
/*The main process enters the pause state, and the data processing is mainly in the callback function*/
pause();

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)
  {
    perror("fast_ua_init!\n");
    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.

xprintf("inport:%d,dstmid:%d,len:%d,dmac:%02X:%02X:%02X:%02X:%02X:%02X,smac:%02X:%02X:%02X:%02X:%02X:%02X\n",
pkt->um.inport,pkt->um.dstmid,pkt_len,pkt->data[0],pkt->data[1],pkt->data[2],pkt->data[3],pkt->data[4],pkt->data[5],pkt->data[6],pkt->data[7],pkt->data[8],pkt->data[9],pkt->data[10],pkt->data[11]);

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)
{
  xprintf("pkt_send_normal->%p,outport:%d,len:%d\n",pkt,pkt->um.outport,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