Find the answer to your Linux question:
Results 1 to 4 of 4
Hello! I'm pretty new to kernel development so apologies if this post smacks entirely of me being a newbie. I'm attempting to create my own distributed filesystem for a bit ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2013
    Location
    UK
    Posts
    2

    TCP sockets in a kernel module


    Hello!

    I'm pretty new to kernel development so apologies if this post smacks entirely of me being a newbie.

    I'm attempting to create my own distributed filesystem for a bit of experimentation, but I keep having issues with getting sockets to work in the kernel. Using a previous post from here ( 55923-kernel-sockets.html#post321137 ) I have got a bit of socket code in my implementation which compiles fine but when running appears to do bupcus. I've linked my exact implementation below. It seems to just sit forever not being able to connect the socket to the remote process listening on the port.

    My server process is sitting on another machine, but using wireshark I can see that absolutely no packets ever actually come out of the client machine, running eth1 in promiscuous mode. Interestingly, if I try and run wireshark on the client machine, the entire thing locks up with what I can only assume is a kernel panic as not even a REISUB will get it back again.

    If anyone here can give me a hand with this, I would be very grateful as I feel like I've been bashing my head against a rock for the last month with this.

    Calling code...
    Code:
    static void naffs_create_debugging_files(struct super_block *sb,
                                            struct dentry *root,
                                            naffs_mount_opts* mnt_opts)
    {
    	/*int server_present = 0;*/
    
    	struct socket 	*my_sock;
    	/* Testing only - This message should be generated from the options! */
    	char 			message[] = "<?mount ?>\n<export dir=\"/export\">\n<*>\n";
    	char 			response[RECV_BUFFER];
    	unsigned int 	ip_addr;
    	int			port = SERVER_PORT;
    	int			retval;
    	naffs_packet	response_pkt;
    	naffs_fs_entry  *root_file;
    	
    	NAFFS_KDEBUG(KERN_DEBUG, "Trace: entered function\n");
    	
    	ip_addr = mnt_opts->ip_addr = NAFFS_inet_addr(mnt_opts->ip_addr_str);
    	
    	printk("IP Address string: %s", mnt_opts->ip_addr_str);
    	
    	my_sock = naffs_init_socket(ip_addr, port);
    	if(my_sock == NULL) {
    		NAFFS_KDEBUG(KERN_ERR, "Mount packet socket failed!");
    		goto create_file;
    	}
    	
    	retval = naffs_send_socket(my_sock, message, 0);
    	if(retval <= 0) {
    		NAFFS_KDEBUG(KERN_ERR, "Didn't send any socket data.");
    		goto create_file;
    	}
    	
    	retval = naffs_recv_socket(my_sock, response, RECV_BUFFER, 0);
    	if(retval <= 0) {
    		NAFFS_KDEBUG(KERN_ERR, "Didn't receive any socket data!");
    	}
    	else {
    	
    		retval = NAFFS_Parse_Response(&response_pkt, sb, root);
    	}
    
    ...
    The initialization function where it fails to connect - It fails on the call to socket->ops->connect()
    Code:
    struct socket* naffs_init_socket(unsigned int netaddr, int port) {
    	struct socket * client_sock;
    	struct sockaddr_in sin;
    	int retval, i;
    	
    	/* Create a stream-orientated TCP socket */
    	retval = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &client_sock);
    	if (retval < 0) {
    		NAFFS_KDEBUG(KERN_ERR, "Couldn't create TCP socket");
    		return NULL;
    	}
    	
    	/* Bind to our socket */
    	sin.sin_family = AF_INET;
    	sin.sin_addr.s_addr = htonl(netaddr);
    	sin.sin_port = htons(port);
    	
    	/* 
    	 * Now attempt to connect to the server. Retry a few times if we don't
    	 * get through the first time.
    	 */
    	for(i = 0; i < 5; i++) {
    		retval = client_sock->ops->connect(client_sock, (struct sockaddr*) &sin, sizeof(sin), 0);
    		if (retval < 0) {
    			NAFFS_KDEBUG(KERN_DEBUG, "Connection to server failed, trying again...");
    		}
    	}
    	
    	if (retval < 0) {
    		NAFFS_KDEBUG(KERN_ERR, "Giving up trying to connect to the server.");
    		return NULL;
    	}
    	
    	return client_sock;
    }

    Again, if anyone can help me out here I shall be incredibly grateful and will bestow upon you many many awesome points.
    Last edited by atreyu; 05-17-2013 at 12:42 AM. Reason: linkified LFO link on behalf of OP

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,512
    You might want to start with a fuse driver (user-space file system driver) so you can do this in user space and not in the kernel, at least until you are more experienced with kernel structures and programming.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    May 2013
    Location
    UK
    Posts
    2
    Quote Originally Posted by Rubberman View Post
    You might want to start with a fuse driver (user-space file system driver) so you can do this in user space and not in the kernel, at least until you are more experienced with kernel structures and programming.
    If I was producing this for the purpose of creating a fully fledged distributed file system, FUSE would've been the way I'd have gone. But filesystem specifics aren't an issue, as I have full integration with the VFS. It's more an issue with actually using sockets at the kernel level, and the whole point of this exercise was to get some experience with kernel programming in order to better my job prospects. I picked a distributed file system as it gives me both the largest challenge and also opportunity to dabble with lots of different bits at once.


    -S

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,512
    Ok. Thanks for the clarification of you intentions. Since I have not had to resort to kernel-level socket programming I'll have to defer to other "experts". Have you gone to www.kernel.org or The Linux Documentation Project for more information yet?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •