~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/ipvs/ip_vs_proto.c

Version: ~ [ linux-5.13-rc5 ] ~ [ linux-5.12.9 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.42 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.124 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.193 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.235 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.271 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.271 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * ip_vs_proto.c: transport protocol load balancing support for IPVS
  3  *
  4  * Version:     $Id: ip_vs_proto.c,v 1.2 2003/04/18 09:03:16 wensong Exp $
  5  *
  6  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
  7  *              Julian Anastasov <ja@ssi.bg>
  8  *
  9  *              This program is free software; you can redistribute it and/or
 10  *              modify it under the terms of the GNU General Public License
 11  *              as published by the Free Software Foundation; either version
 12  *              2 of the License, or (at your option) any later version.
 13  *
 14  * Changes:
 15  *
 16  */
 17 
 18 #include <linux/config.h>
 19 #include <linux/module.h>
 20 #include <linux/types.h>
 21 #include <linux/kernel.h>
 22 #include <linux/errno.h>
 23 #include <linux/skbuff.h>
 24 #include <linux/in.h>
 25 #include <linux/ip.h>
 26 #include <linux/init.h>
 27 #include <net/protocol.h>
 28 #include <net/tcp.h>
 29 #include <net/udp.h>
 30 #include <asm/system.h>
 31 #include <linux/stat.h>
 32 #include <linux/proc_fs.h>
 33 
 34 #include <net/ip_vs.h>
 35 
 36 
 37 /*
 38  * IPVS protocols can only be registered/unregistered when the ipvs
 39  * module is loaded/unloaded, so no lock is needed in accessing the
 40  * ipvs protocol table.
 41  */
 42 
 43 #define IP_VS_PROTO_TAB_SIZE            32      /* must be power of 2 */
 44 #define IP_VS_PROTO_HASH(proto)         ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
 45 
 46 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
 47 
 48 
 49 /*
 50  *      register an ipvs protocol
 51  */
 52 int register_ip_vs_protocol(struct ip_vs_protocol *pp)
 53 {
 54         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
 55 
 56         pp->next = ip_vs_proto_table[hash];
 57         ip_vs_proto_table[hash] = pp;
 58 
 59         if (pp->init != NULL)
 60                 pp->init(pp);
 61 
 62         return 0;
 63 }
 64 
 65 
 66 /*
 67  *      unregister an ipvs protocol
 68  */
 69 int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
 70 {
 71         struct ip_vs_protocol **pp_p;
 72         unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
 73 
 74         pp_p = &ip_vs_proto_table[hash];
 75         for (; *pp_p; pp_p = &(*pp_p)->next) {
 76                 if (*pp_p == pp) {
 77                         *pp_p = pp->next;
 78                         if (pp->exit != NULL)
 79                                 pp->exit(pp);
 80                         return 0;
 81                 }
 82         }
 83 
 84         return -ESRCH;
 85 }
 86 
 87 
 88 /*
 89  *      get ip_vs_protocol object by its proto.
 90  */
 91 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
 92 {
 93         struct ip_vs_protocol *pp;
 94         unsigned hash = IP_VS_PROTO_HASH(proto);
 95 
 96         for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
 97                 if (pp->protocol == proto)
 98                         return pp;
 99         }
100 
101         return NULL;
102 }
103 
104 
105 /*
106  *      Propagate event for state change to all protocols
107  */
108 void ip_vs_protocol_timeout_change(int flags)
109 {
110         struct ip_vs_protocol *pp;
111         int i;
112 
113         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
114                 for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
115                         if (pp->timeout_change)
116                                 pp->timeout_change(pp, flags);
117                 }
118         }
119 }
120 
121 
122 int *
123 ip_vs_create_timeout_table(int *table, int size)
124 {
125         int *t;
126 
127         t = kmalloc(size, GFP_ATOMIC);
128         if (t == NULL)
129                 return NULL;
130         memcpy(t, table, size);
131         return t;
132 }
133 
134 
135 /*
136  *      Set timeout value for state specified by name
137  */
138 int
139 ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to)
140 {
141         int i;
142 
143         if (!table || !name || !to)
144                 return -EINVAL;
145 
146         for (i = 0; i < num; i++) {
147                 if (strcmp(names[i], name))
148                         continue;
149                 table[i] = to * HZ;
150                 return 0;
151         }
152         return -ENOENT;
153 }
154 
155 
156 const char * ip_vs_state_name(__u16 proto, int state)
157 {
158         struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
159 
160         if (pp == NULL || pp->state_name == NULL)
161                 return "ERR!";
162         return pp->state_name(state);
163 }
164 
165 
166 void
167 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
168                           const struct sk_buff *skb,
169                           int offset,
170                           const char *msg)
171 {
172         char buf[128];
173         __u16 ports[2];
174         struct iphdr iph;
175 
176         if (skb_copy_bits(skb, offset, &iph, sizeof(iph)) < 0)
177                 sprintf(buf, "%s TRUNCATED", pp->name);
178         else if (iph.frag_off & __constant_htons(IP_OFFSET))
179                 sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u frag",
180                         pp->name, NIPQUAD(iph.saddr),
181                         NIPQUAD(iph.daddr));
182         else if (skb_copy_bits(skb, offset + iph.ihl*4, ports, sizeof(ports)) < 0)
183                 sprintf(buf, "%s TRUNCATED %u.%u.%u.%u->%u.%u.%u.%u",
184                         pp->name,
185                         NIPQUAD(iph.saddr),
186                         NIPQUAD(iph.daddr));
187         else
188                 sprintf(buf, "%s %u.%u.%u.%u:%u->%u.%u.%u.%u:%u",
189                         pp->name,
190                         NIPQUAD(iph.saddr),
191                         ntohs(ports[0]),
192                         NIPQUAD(iph.daddr),
193                         ntohs(ports[1]));
194 
195         printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
196 }
197 
198 
199 int ip_vs_protocol_init(void)
200 {
201         char protocols[64];
202 #define REGISTER_PROTOCOL(p)                    \
203         do {                                    \
204                 register_ip_vs_protocol(p);     \
205                 strcat(protocols, ", ");        \
206                 strcat(protocols, (p)->name);   \
207         } while (0)
208 
209         protocols[0] = '\0';
210         protocols[2] = '\0';
211 #ifdef CONFIG_IP_VS_PROTO_TCP
212         REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
213 #endif
214 #ifdef CONFIG_IP_VS_PROTO_UDP
215         REGISTER_PROTOCOL(&ip_vs_protocol_udp);
216 #endif
217 #ifdef CONFIG_IP_VS_PROTO_ICMP
218         REGISTER_PROTOCOL(&ip_vs_protocol_icmp);
219 #endif
220 #ifdef CONFIG_IP_VS_PROTO_AH
221         REGISTER_PROTOCOL(&ip_vs_protocol_ah);
222 #endif
223 #ifdef CONFIG_IP_VS_PROTO_ESP
224         REGISTER_PROTOCOL(&ip_vs_protocol_esp);
225 #endif
226         IP_VS_INFO("Registered protocols (%s)\n", &protocols[2]);
227 
228         return 0;
229 }
230 
231 
232 void ip_vs_protocol_cleanup(void)
233 {
234         struct ip_vs_protocol *pp;
235         int i;
236 
237         /* unregister all the ipvs protocols */
238         for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
239                 while ((pp = ip_vs_proto_table[i]) != NULL)
240                         unregister_ip_vs_protocol(pp);
241         }
242 }
243 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp