So guys, we will start our implementation from the main function.
And note that I'm in the same file that is userspace dot C.
So first we will going to implement how to send message to the kernel space through the main menu.
So as we discussed in the flow chart, the very first thing that we will going to do is to create a
net link socket.
You can create a net link socket using an API, create netlink socket, right?
And you have to pass the protocol number as an argument to this API.
And in our case this value is 31.
So this API just create a socket and returns a numeric integer which represents a socket file descriptor.
If the socket file descriptor is minus one, it means that socket creation has failed and you can print
the error code, which could tell you the reason for the failure of this socket creation.
Then we need to take some couple of variables.
For example, we need to take the source address.
Now, in this communication between user space and kernel space, who is the source?
The source is this user space program, right?
So in the source address, we will have to specify the identity of this user space program and note
the data structure of the source address is of the type sock address underscore URL.
Right.
So these are the type of socket addresses which are used for netlink sockets.
We will see how to specify and what information to specify as a source address in this data structure
shortly.
And let and let take a netlink header pointer which we will be using later.
And let us initialize the source address.
Now let us see what fields we have to fill in the source address.
The first field that we have to field is the Netlink family.
In our case, the Netlink family is constant and it is this value that is af underscore netlink.
And the second value which we need to specify, is the process id of this process.
So process ID you can obtain from the standard API get PID right.
So in the source address data structure, we only need to fill these two fields so you can see that
get PID is a process ID which is unique, which is a unique identity of a process system wide.
And now we have to bind this netlink socket.
So you can see the bind system call takes three arguments.
The first argument is the netlink socket file descriptor which we have already created here.
The second argument is the source address which contains the identity of the process.
And the third argument is the size of the data structure which you are passing as the second argument.
So using bind system call, this userspace program is actually telling the underlying operating system
that hey, operating system, I am interested in receiving a netlink messages which are being sent to
the userspace using this protocol number.
Right?
So through the second field in the bind system call, the userspace program is telling its identity
to the operating system and it is telling the operating system that any message which is generated by
the kernel subsystem using a netlink protocol number equal to the Netlink test protocol, which is 31,
then I am interested in those messages.
Please send it to me.
So using bind system call the operating system has been told that any netlink messages which is generated
by any kernel subsystem using protocol number equal to the netlink test protocol.
The operating system is supposed to hand over those messages to the process.
Who's process ID is specified as the second argument in this bind system call?
That is this process itself.
So not many people understand the mechanics of the bind system call.
It is the way of telling the operating system that in what type of messages the user space program is
interested in.
We use bind system call not only in netlink sockets but everywhere, wherever you use sockets.
So enough lecture on bind system call.
Now let us move forward.
Now we can start our infinite loop of the main thread and inside that infinite loop we can print the
main menu.
Right.
You must have already written the program with some main menu kind of thing.
Right?
And then simply, we are asking the user to input his choice number and based on the choice number,
we can do switch on these choices, right?
So if the user presses the case, number one, we can simply invoke this code.
So you can see that I am taking a character array and I am asking the user to input some string.
We will going to send this string to the kernel space and finally we will invoke the greet kernel passing
the socket using which we will going to send this message to the kernel space.
The actual message which we are interested to send to the kernel space and the length of the message.
Right.
So we will discuss the implementation of this greet kernel API.
It's very easy.
And however, if the user chooses option number two, then we can simply exit our program before exiting
our program.
We should destroy our socket and every resource that your program occupies.
So I have completed the implementation of the sending side of our user space program.
And now let us discuss the implementation of this greet kernel API.
It is very simple.
This greet kernel API is simply a wrapper API over send netlink message to kernel API which we have
already discussed.
So if you want to implement this greet kernel API, you can see that it's very simple.
This API simply invoke send netlink message to kernel and you can see that we are passing the netlink
message type, which is our own custom type as Netlink message greet you can has defined this value
to one and because we also need a response from the kernel space, therefore we are specifying the acknowledgment
flag.
Right.
So remember this netlink message greet is a user defined constant value and you can just hash define
it to one.
This value will go as a netlink message type value in the netlink header to the kernel space.
So with this, the implementation of the sending side of our user space program is complete.
Now let us try to compile and see how this program behaves or works.
Now, guys, we will see that whether our user space program is able to send message to the kernel space.
So on the right hand side, in a separate window, I will monitor the content of the file kernel dot
log into which all the print statements goes.
And on the left hand side I will simply compile and run our user space program so you can see that our
user space program present a main menu.
I will choose the option number one to greet kernel.
And now this user space program is asking me to input some string.
So I will simply say hello, how are you?
Kernel?
Right.
And just press enter and you can see on the right hand side that our kernel space is receiving messages
and the message is hello, how are you kernel.
So this is the message that I have received.
So it simply means that the sending of a message from a user space to kernel space is now fully working.
Let us try some more option.
Now let us try one more attempt to send message to the kernel space and it works now.
This time I have received I am fine message to the kernel space.
Right.
So now in the next lecture video we will see how our user space program can receive messages from the
kernel.
At this point of time our kernel space is sending message or reply back to the user space, but our
user space doesn't have a provision or the code to actually receive the messages from the kernel space.
Right?
So in the next lecture video we will going to fix up the receiving part of our user space program.