Krishna iResearch Intelligent Cloud Platform - KingCobra - Byzantine request servicing software
 All Namespaces
kingcobra_main_kernelspace.c
1 /***************************************************************************************
2 KingCobra - BFT request servicing software on cloud with queues and arbiters
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 --------------------------------------------------------------------------------------------------
18 Copyright (C):
19 Srinivasan Kannan (alias) Ka.Shrinivaasan (alias) Shrinivas Kannan
20 Independent Open Source Developer, Researcher and Consultant
21 Ph: 9789346927, 9003082186, 9791165980
22 Open Source Products Profile(Krishna iResearch): http://sourceforge.net/users/ka_shrinivaasan, https://www.ohloh.net/accounts/ka_shrinivaasan
23 Personal website(research): https://sites.google.com/site/kuja27/
24 emails: ka.shrinivaasan@gmail.com, shrinivas.kannan@gmail.com, kashrinivaasan@live.com
25 --------------------------------------------------------------------------------------------------
26 
27 *****************************************************************************************/
28 
29 /*
30 #include <linux/string.h>
31 #include <linux/module.h>
32 #include <linux/virgo_queue.h>
33 #include <linux/string.h>
34 #include <kstrtox.h>
35 #include <linux/ctype.h>
36 
37 
38 static int __init kingcobra_kernelspace_init(void);
39 static void __exit kingcobra_kernelspace_exit(void);
40 void kingcobra_servicerequest_kernelspace(void* args);
41 */
42 
43 #include <linux/kingcobra.h>
44 
45 static int __init
46 kingcobra_kernelspace_init(void)
47 {
48  printk(KERN_INFO "kingcobra_kernelspace_init(): initializing KingCobra kernel module\n");
49  loff_t bytesread=0;
50  loff_t pos=0;
51  /*mm_segment_t fs;*/
52  fs=get_fs();
53  set_fs(get_ds());
54  if(kingcobra_disk_persistence==1)
55  {
56  printk(KERN_INFO "kingcobra_kernelspace_init(): Initializing disk persistence file for KingCobra\n");
57  request_reply_queue=filp_open("/var/log/kingcobra/REQUEST_REPLY.queue", O_WRONLY|O_APPEND, 0777);
58  }
59  return 0;
60 }
61 EXPORT_SYMBOL(kingcobra_kernelspace_init);
62 
63 
64 static void __exit
65 kingcobra_kernelspace_exit(void)
66 {
67  printk(KERN_INFO "kingcobra_kernelspace_exit(): exiting KingCobra kernel module \n");
68  filp_close(request_reply_queue,NULL);
69  set_fs(fs);
70  do_exit(1);
71 }
72 EXPORT_SYMBOL(kingcobra_kernelspace_exit);
73 
74 
75 void kingcobra_servicerequest_kernelspace(void* args)
76 {
77  printk(KERN_INFO "kingcobra_servicerequest_kernelspace(): KingCobra service request received from kernel KingCobra workqueue: %s\n",(char*)args);
78  if(kingcobra_disk_persistence==1)
79  {
80  char buf[256];
81  /*loff_t pos;*/
82  sprintf(buf, "%s $$\n",(char*)args);
83  printk(KERN_INFO "kingcobra_servicerequest_kernelspace(): disk persistence enabled, writing incoming request to KingCobra disk file:%s\n",buf);
84  vfs_write(request_reply_queue, buf, 256, &request_reply_queue_pos);
85  request_reply_queue_pos+=256;
86  }
87  long client_ip_l=parse_ip_address((char*)args);
88  char* logicaltimestamp=parse_timestamp((char*)args);
89  char* response=kmalloc(KCOBRA_BUF_SIZE,GFP_ATOMIC);
90  sprintf(response, "REPLY#%x#%s :--- from kingcobra_servicerequest_kernelspace() to client",client_ip_l,logicaltimestamp);
91  reply_to_publisher(client_ip_l,response);
92 }
93 EXPORT_SYMBOL(kingcobra_servicerequest_kernelspace);
94 
95 char* parse_timestamp(char* request)
96 {
97  char* delim="#";
98  char* timestamp=NULL;
99  char* request_dup=kstrdup(request,GFP_ATOMIC);
100  strsep(&request_dup,delim);
101  strsep(&request_dup,delim);
102  timestamp=kstrdup(strsep(&request_dup,delim),GFP_ATOMIC);
103  printk(KERN_INFO "parse_timestamp(): timestamp parsed from request header = %s\n",timestamp);
104  return timestamp;
105 }
106 
107 long parse_ip_address(char* request)
108 {
109  char* delim="#";
110  char* token=NULL;
111  char* request_dup=kstrdup(request,GFP_ATOMIC);
112  printk(KERN_INFO "parse_ip_address(): request_dup = %s\n",request_dup);
113  char* request_header=strsep(&request_dup,delim);
114  printk(KERN_INFO "parse_ip_address(): request_header= %s\n", request_header);
115  token=strsep(&request_dup,delim);
116  printk(KERN_INFO "parse_ip_address(): token = %s\n",token);
117  unsigned long ll2;
118  kstrtoll(token,16,&ll2);
119  printk(KERN_INFO "parse_ip_address(): ll2=%d\n",ll2);
120  return ll2;
121 }
122 
123 void reply_to_publisher(long client_ip_l, char *response)
124 {
125 
126  int nr;
127  struct kvec iov;
128  struct msghdr msg;
129  int error;
130  struct socket *sock;
131  struct sockaddr_in sin;
132  int sfd, s, j;
133  size_t len;
134  ssize_t nread;
135  char buf[KCOBRA_BUF_SIZE];
136 
137  /*
138  struct hostport* leastloadedhostport = get_least_loaded_hostport_from_cloud();
139  s = getaddrinfo(leastloadedhostport->host, leastloadedhostport->port, &hints, &result);
140  */
141 
142  char* hostip;
143  char* port;
144  sin.sin_family=AF_INET;
145  /*in4_pton(hostip, strlen(hostip), &sin.sin_addr.s_addr, '\0',NULL);*/
146  sin.sin_addr.s_addr=client_ip_l;
147  sin.sin_port=htons(10000);
148 
149  iov.iov_base=buf;
150  iov.iov_len=sizeof(buf);
151  msg.msg_name = (struct sockaddr *) &sin;
152  msg.msg_namelen = sizeof(struct sockaddr);
153  msg.msg_iov = (struct iovec *) &iov;
154  msg.msg_iovlen = 1;
155  msg.msg_control = NULL;
156  msg.msg_controllen = 0;
157  msg.msg_flags = 0;
158  nr=1;
159 
160 
161  strcpy(iov.iov_base, response);
162  error = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
163  printk(KERN_INFO "reply_to_publisher() : created client kernel socket\n");
164  kernel_connect(sock, (struct sockaddr*)&sin, sizeof(sin) , 0);
165  printk(KERN_INFO "reply_to_publisher() : connected kernel client to virgo cloudexec kernel service\n ");
166  kernel_sendmsg(sock, &msg, &iov, nr, KCOBRA_BUF_SIZE);
167  printk(KERN_INFO "reply_to_publisher() : sent message: %s \n", buf);
168  len = kernel_recvmsg(sock, &msg, &iov, nr, KCOBRA_BUF_SIZE, msg.msg_flags);
169  printk(KERN_INFO "reply_to_publisher() : received message: %s \n", buf);
170  le32_to_cpus(buf);
171  printk(KERN_INFO "reply_to_publisher() : le32_to_cpus(buf): %s \n", buf);
172  sock_release(sock);
173  printk(KERN_INFO "reply_to_publisher() : reply_to_publisher() client socket_release() invoked\n");
174 
175  return len;
176 }
177 
178 
179 MODULE_LICENSE("GPL");
180 module_init(kingcobra_kernelspace_init);
181 module_exit(kingcobra_kernelspace_exit);