WEBVTT

00:06.550 --> 00:14.230
So guys, now let us write a one unified single API which will take the responsibility to send any type

00:14.230 --> 00:19.360
of message from our userspace program to kernel space via netlink socket.

00:19.360 --> 00:19.950
Right.

00:19.960 --> 00:23.860
So the name of the API is send netlink message to kernel.

00:24.100 --> 00:28.930
So as you can see that this API accepts five argument.

00:28.960 --> 00:35.800
The first argument is the file descriptor of the netlink socket which the user space program will create

00:35.800 --> 00:37.990
when the user space program boots up.

00:38.440 --> 00:39.190
Right.

00:39.220 --> 00:45.730
The second argument is the payload which the user space program will send to the kernel space.

00:45.850 --> 00:48.640
The third argument is the size of the payload.

00:48.640 --> 00:55.690
The fourth argument is the type of the netlink message which this user space program is sending to the

00:55.690 --> 00:56.710
kernel space.

00:56.710 --> 01:01.120
So the fourth field is the netlink message type field of the Netlink header.

01:01.120 --> 01:06.340
In our case, this field will be equal to the enumeration, which is great.

01:06.370 --> 01:07.180
Right?

01:07.180 --> 01:14.230
And finally, the fifth field is the flags, which user space program must specify in order to give

01:14.260 --> 01:19.120
directions to the kernel space regarding what to do with the message.

01:19.570 --> 01:24.670
So now let us discuss the implementation of this API in four simple steps.

01:26.050 --> 01:29.470
First, we will take an overview of these four simple steps.

01:29.470 --> 01:34.810
Then in the next lecture video, we will go through the actual implementation of this API.

01:35.450 --> 01:42.410
So as a part of writing this API, the very first step that we need to do is to prepare a message of

01:42.410 --> 01:45.980
type Netlink message header followed by the payload.

01:45.980 --> 01:46.730
Right.

01:47.120 --> 01:53.570
Remember any message that is exchanged between user space and kernel space should be exchanged by using

01:53.570 --> 01:56.210
netlink message header followed by payload.

01:56.390 --> 01:59.330
So now this diagram is not new to you.

01:59.480 --> 02:02.750
We will reformat the message in the following format.

02:02.750 --> 02:05.840
That is netlink message header followed by payload.

02:06.950 --> 02:12.710
Then in the next step we have to use the intermediate data structure called Iok.

02:12.920 --> 02:15.920
Iok stands for input output vector.

02:15.920 --> 02:21.770
So this data structure, as you can see, is a very simple data structure comprises of only two fields.

02:21.800 --> 02:29.270
The first field is a EOF underscore base and it is of data type void star and the second field is of

02:29.270 --> 02:31.000
data type sys underscore tty.

02:31.220 --> 02:31.940
Right.

02:32.030 --> 02:38.730
So what we will going to do is that we will store the address of the netlink message header in this

02:38.760 --> 02:45.180
eof underscore base field and the total length of this message that is starting from the Netlink message

02:45.180 --> 02:50.180
header to the end of the payload, we will going to specify in Ireland.

02:50.430 --> 02:57.720
So basically we have encapsulated the netlink message which we will going to send to the kernel space

02:57.720 --> 03:01.470
indirectly inside the vector data structure.

03:01.920 --> 03:04.170
But the story doesn't ends here.

03:04.170 --> 03:06.350
We will do one more level of wrapping.

03:06.360 --> 03:13.560
Now in the step three, we will going to wrap this vector data structure inside inside another structure

03:13.560 --> 03:15.390
called struct message header.

03:15.390 --> 03:15.960
Right?

03:15.960 --> 03:19.470
So let us call this structure as outer message header.

03:19.710 --> 03:24.290
So in this type of messages, we have to fill these four fields.

03:24.300 --> 03:30.780
So the first field is nothing, but it is the destination address which we will going to specify now

03:30.780 --> 03:36.300
how to specify the destination address that we will going to discuss when we will walk through the code.

03:36.390 --> 03:39.690
The second field contains the size of the destination address.

03:39.690 --> 03:44.640
Again, this We will discuss when we will be discussing the code in the third field.

03:44.640 --> 03:50.490
We have to supply the address of the vector data structure which we have defined in step two.

03:50.490 --> 03:51.270
Right.

03:51.270 --> 03:58.650
And in the fourth field we have to specify how many units of vector data structure we are sending to

03:58.650 --> 03:59.790
the kernel space.

03:59.790 --> 04:07.650
So in this example, we have taken only one vector data structure and therefore you can specify one

04:07.650 --> 04:08.730
in this field.

04:09.030 --> 04:16.080
Now to discuss about this destination address, this destination address is of data type struct soc

04:16.140 --> 04:18.200
address underscore channel.

04:18.210 --> 04:24.540
So this is a socket address data structure which is used for netlink sockets.

04:25.560 --> 04:29.940
And inside this data structure we have to specify only two fields.

04:29.940 --> 04:34.380
The Netlink family, which is the address family for the netlink socket.

04:34.380 --> 04:41.820
So always we have to specify a F underscore netlink and we have to specify the port ID of the destination

04:41.820 --> 04:42.600
process.

04:42.630 --> 04:49.800
Now in this example, because we are communicating from userspace to kernel space for kernel space,

04:49.800 --> 04:53.070
the port ID is always zero, right.

04:53.760 --> 05:00.300
So we have to specify only these two fields in the destination address and watch the data structure

05:00.300 --> 05:03.000
that is used as a destination address.

05:03.570 --> 05:08.070
This data structure is provided by the netlink socket header files.

05:09.600 --> 05:16.440
Now as a final step in step number four, we are good to go to send our user space message to our kernel

05:16.440 --> 05:18.540
space via netlink socket.

05:18.570 --> 05:24.450
The first argument we have to specify the file descriptor of the socket which our user space program

05:24.450 --> 05:26.020
has already created.

05:26.040 --> 05:30.540
The second argument is the address of this outer message header.

05:30.810 --> 05:34.740
And the third argument you can always specify as zero.

05:35.420 --> 05:42.350
So now that we have discussed the high level overview regarding the steps for sending the message from

05:42.350 --> 05:44.340
the user space to kernel space.

05:44.360 --> 05:50.990
Now let us discuss the step by step implementation and discuss the implementation of this API.

05:51.020 --> 05:53.690
We will have to use this API every time.

05:53.690 --> 05:58.640
We will going to send message from our user space program to kernel space.

05:58.640 --> 06:01.980
And you can see that this API is very generic.

06:02.000 --> 06:09.110
We can send any type of message or payload from user space to kernel space using this function.

06:09.110 --> 06:09.800
Right.
