if: Multiple TX queue support step 1 of many; introduce ifaltq subqueue
[dragonfly.git] / sys / net / ef / if_ef.c
1 /*-
2  * Copyright (c) 1999, 2000 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/net/if_ef.c,v 1.2.2.4 2001/02/22 09:27:04 bp Exp $
27  */
28
29 #include "opt_inet.h"
30 #include "opt_ipx.h"
31 #include "opt_ef.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sockio.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/thread2.h>
43
44 #include <net/ethernet.h>
45 #include <net/if_llc.h>
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/ifq_var.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <net/bpf.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/if_ether.h>
59 #endif
60
61 #ifdef IPX
62 #include <netproto/ipx/ipx.h>
63 #include <netproto/ipx/ipx_if.h>
64 #endif
65
66 /* If none of the supported layers is enabled explicitly enable them all */
67 #if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
68     !defined(ETHER_SNAP)
69 #define ETHER_II        1
70 #define ETHER_8023      1
71 #define ETHER_8022      1
72 #define ETHER_SNAP      1
73 #endif
74
75 /* internal frame types */
76 #define ETHER_FT_EII            0       /* Ethernet_II - default */
77 #define ETHER_FT_8023           1       /* 802.3 (Novell) */
78 #define ETHER_FT_8022           2       /* 802.2 */
79 #define ETHER_FT_SNAP           3       /* SNAP */
80 #define EF_NFT                  4       /* total number of frame types */
81
82 #ifdef EF_DEBUG
83 #define EFDEBUG(format, args...) kprintf("%s: "format, __func__ ,## args)
84 #else
85 #define EFDEBUG(format, args...)
86 #endif
87
88 #define EFERROR(format, args...) kprintf("%s: "format, __func__ ,## args)
89
90 struct efnet {
91         struct arpcom   ef_ac;
92         struct ifnet *  ef_ifp;
93         int             ef_frametype;
94 };
95
96 struct ef_link {
97         SLIST_ENTRY(ef_link) el_next;
98         struct ifnet    *el_ifp;                /* raw device for this clones */
99         struct efnet    *el_units[EF_NFT];      /* our clones */
100 };
101
102 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
103 static int efcount;
104
105 extern int (*ef_inputp)(struct ifnet*, const struct ether_header *eh,
106                 struct mbuf *m);
107 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
108                 struct sockaddr *dst, short *tp, int *hlen);
109
110 /*
111 static void ef_reset (struct ifnet *);
112 */
113 static int ef_attach(struct efnet *sc);
114 static int ef_detach(struct efnet *sc);
115 static void ef_init(void *);
116 static int ef_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
117 static void ef_start(struct ifnet *, struct ifaltq_subque *);
118 static int ef_input(struct ifnet*, const struct ether_header *, struct mbuf *);
119 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
120                 struct sockaddr *dst, short *tp, int *hlen);
121
122 static int ef_load(void);
123 static int ef_unload(void);
124
125 /*
126  * Install the interface, most of structure initialization done in ef_clone()
127  */
128 static int
129 ef_attach(struct efnet *sc)
130 {
131         struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
132
133         ifp->if_start = ef_start;
134         ifp->if_watchdog = NULL;
135         ifp->if_init = ef_init;
136         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
137         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
138         /*
139          * Attach the interface
140          */
141         ether_ifattach(ifp, IF_LLADDR(sc->ef_ifp), NULL);
142
143         ifp->if_resolvemulti = 0;
144         ifp->if_type = IFT_XETHER;
145         ifp->if_flags |= IFF_RUNNING;
146
147         EFDEBUG("%s: attached\n", ifp->if_xname);
148         return 1;
149 }
150
151 /*
152  * This is for _testing_only_, just removes interface from interfaces list
153  */
154 static int
155 ef_detach(struct efnet *sc)
156 {
157         ether_ifdetach(&sc->ef_ac.ac_if);
158         return 0;
159 }
160
161 static void
162 ef_init(void *foo __unused)
163 {
164 }
165
166 static int
167 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
168 {
169         struct efnet *sc = ifp->if_softc;
170         struct ifaddr *ifa = (struct ifaddr*)data;
171         int error;
172
173         EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
174         error = 0;
175         crit_enter();
176         switch (cmd) {
177             case SIOCSIFADDR:
178                 if (sc->ef_frametype == ETHER_FT_8023 && 
179                     ifa->ifa_addr->sa_family != AF_IPX) {
180                         error = EAFNOSUPPORT;
181                         break;
182                 }
183                 ifp->if_flags |= IFF_UP; 
184                 /* FALL THROUGH */
185             case SIOCGIFADDR:
186             case SIOCSIFMTU:
187                 error = ether_ioctl(ifp, cmd, data);
188                 break;
189             case SIOCSIFFLAGS:
190                 error = 0;
191                 break;
192             default:
193                 error = EINVAL;
194         }
195         crit_exit();
196         return error;
197 }
198
199 /*
200  * Currently packet prepared in the ether_output(), but this can be a better
201  * place.
202  */
203 static void
204 ef_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
205 {
206         struct efnet *sc = (struct efnet*)ifp->if_softc;
207         struct ifnet *p;
208         struct mbuf *m;
209         struct ifaltq_subque *p_ifsq;
210
211         ifsq_set_oactive(ifsq);
212         p = sc->ef_ifp;
213         p_ifsq = ifq_get_subq_default(&p->if_snd);
214
215         EFDEBUG("\n");
216         for (;;) {
217                 m = ifsq_dequeue(ifsq, NULL);
218                 if (m == NULL)
219                         break;
220
221                 BPF_MTAP(ifp, m);
222
223                 ifsq_enqueue(p_ifsq, m, NULL);
224                 if (!ifsq_is_oactive(p_ifsq)) {
225                         p->if_start(p, p_ifsq);
226                         ifp->if_opackets++;
227                 }
228         }
229         ifsq_clr_oactive(ifsq);
230         return;
231 }
232
233 /*
234  * Inline functions do not put additional overhead to procedure call or
235  * parameter passing but simplify the code
236  */
237 static __inline int
238 ef_inputEII(struct mbuf *m, struct llc* l, u_short ether_type)
239 {
240         int isr;
241
242         switch(ether_type) {
243 #ifdef IPX
244         case ETHERTYPE_IPX:
245                 isr = NETISR_IPX;
246                 break;
247 #endif
248 #ifdef INET
249         case ETHERTYPE_IP:
250 #ifdef notyet
251                 if (ipflow_fastforward(m))
252                         return (0);
253 #endif
254                 isr = NETISR_IP;
255                 break;
256         case ETHERTYPE_ARP:
257                 isr = NETISR_ARP;
258                 break;
259 #endif
260         default:
261                 return (EPROTONOSUPPORT);
262         }
263         netisr_queue(isr, m);
264         return (0);
265 }
266
267 static __inline int
268 ef_inputSNAP(struct mbuf *m, struct llc* l, u_short ether_type)
269 {
270         int isr;
271
272         switch(ether_type) {
273 #ifdef IPX
274         case ETHERTYPE_IPX:
275                 m_adj(m, 8);
276                 isr = NETISR_IPX;
277                 break;
278 #endif
279         default:
280                 return (EPROTONOSUPPORT);
281         }
282         netisr_queue(isr, m);
283         return (0);
284 }
285
286 static __inline int
287 ef_input8022(struct mbuf *m, struct llc* l, u_short ether_type)
288 {
289         int isr;
290
291         switch(ether_type) {
292 #ifdef IPX
293         case 0xe0:
294                 m_adj(m, 3);
295                 isr = NETISR_IPX;
296                 break;
297 #endif
298         default:
299                 return (EPROTONOSUPPORT);
300         }
301         netisr_queue(isr, m);
302         return (0);
303 }
304
305 /*
306  * Called from ether_input()
307  */
308 static int
309 ef_input(struct ifnet *ifp, const struct ether_header *eh, struct mbuf *m)
310 {
311         u_short ether_type;
312         int ft = -1;
313         struct efnet *efp;
314         struct ifnet *eifp;
315         struct llc *l;
316         struct ef_link *efl;
317         int isr;
318
319         ether_type = ntohs(eh->ether_type);
320         if (ether_type < ETHERMTU) {
321                 l = mtod(m, struct llc*);
322                 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
323                         /* 
324                          * Novell's "802.3" frame
325                          */
326                         ft = ETHER_FT_8023;
327                 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
328                         /*
329                          * 802.2/SNAP
330                          */
331                         ft = ETHER_FT_SNAP;
332                         ether_type = ntohs(l->llc_un.type_snap.ether_type);
333                 } else if (l->llc_dsap == l->llc_ssap) {
334                         /*
335                          * 802.3/802.2
336                          */
337                         ft = ETHER_FT_8022;
338                         ether_type = l->llc_ssap;
339                 }
340         } else
341                 ft = ETHER_FT_EII;
342
343         if (ft == -1) {
344                 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
345                 return EPROTONOSUPPORT;
346         }
347
348         /*
349          * Check if interface configured for the given frame
350          */
351         efp = NULL;
352         SLIST_FOREACH(efl, &efdev, el_next) {
353                 if (efl->el_ifp == ifp) {
354                         efp = efl->el_units[ft];
355                         break;
356                 }
357         }
358         if (efp == NULL) {
359                 EFDEBUG("Can't find if for %d\n", ft);
360                 return EPROTONOSUPPORT;
361         }
362         eifp = &efp->ef_ac.ac_if;
363         if ((eifp->if_flags & IFF_UP) == 0)
364                 return EPROTONOSUPPORT;
365         eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
366         m->m_pkthdr.rcvif = eifp;
367
368         if (eifp->if_bpf) {
369                 bpf_gettoken();
370                 if (eifp->if_bpf)
371                         bpf_ptap(eifp->if_bpf, m, eh, ETHER_HDR_LEN);
372                 bpf_reltoken();
373         }
374
375         /*
376          * Now we ready to adjust mbufs and pass them to protocol intr's
377          */
378         switch(ft) {
379         case ETHER_FT_EII:
380                 return (ef_inputEII(m, l, ether_type));
381                 break;
382 #ifdef IPX
383         case ETHER_FT_8023:             /* only IPX can be here */
384                 isr = NETISR_IPX;
385                 break;
386 #endif
387         case ETHER_FT_SNAP:
388                 return (ef_inputSNAP(m, l, ether_type));
389                 break;
390         case ETHER_FT_8022:
391                 return (ef_input8022(m, l, ether_type));
392                 break;
393         default:
394                 EFDEBUG("No support for frame %d and proto %04x\n",
395                         ft, ether_type);
396                 return (EPROTONOSUPPORT);
397         }
398         netisr_queue(isr, m);
399         return (0);
400 }
401
402 static int
403 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
404         int *hlen)
405 {
406         struct efnet *sc = (struct efnet*)ifp->if_softc;
407         struct mbuf *m = *mp;
408         u_char *cp;
409         short type;
410
411         if (ifp->if_type != IFT_XETHER)
412                 return ENETDOWN;
413         switch (sc->ef_frametype) {
414             case ETHER_FT_EII:
415 #ifdef IPX
416                 type = htons(ETHERTYPE_IPX);
417 #else
418                 return EPFNOSUPPORT;
419 #endif
420                 break;
421             case ETHER_FT_8023:
422                 type = htons(m->m_pkthdr.len);
423                 break;
424             case ETHER_FT_8022:
425                 M_PREPEND(m, ETHER_HDR_LEN + 3, MB_WAIT);
426                 if (m == NULL) {
427                         *mp = NULL;
428                         return ENOBUFS;
429                 }
430                 /*
431                  * Ensure that ethernet header and next three bytes
432                  * will fit into single mbuf
433                  */
434                 m = m_pullup(m, ETHER_HDR_LEN + 3);
435                 if (m == NULL) {
436                         *mp = NULL;
437                         return ENOBUFS;
438                 }
439                 m_adj(m, ETHER_HDR_LEN);
440                 type = htons(m->m_pkthdr.len);
441                 cp = mtod(m, u_char *);
442                 *cp++ = 0xE0;
443                 *cp++ = 0xE0;
444                 *cp++ = 0x03;
445                 *hlen += 3;
446                 break;
447             case ETHER_FT_SNAP:
448                 M_PREPEND(m, 8, MB_WAIT);
449                 if (m == NULL) {
450                         *mp = NULL;
451                         return ENOBUFS;
452                 }
453                 type = htons(m->m_pkthdr.len);
454                 cp = mtod(m, u_char *);
455                 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
456                 *hlen += 8;
457                 break;
458             default:
459                 return EPFNOSUPPORT;
460         }
461         *mp = m;
462         *tp = type;
463         return 0;
464 }
465
466 /*
467  * Create clone from the given interface
468  */
469 static int
470 ef_clone(struct ef_link *efl, int ft)
471 {
472         struct efnet *efp;
473         struct ifnet *eifp;
474         struct ifnet *ifp = efl->el_ifp;
475
476         efp = (struct efnet*)kmalloc(sizeof(struct efnet), M_IFADDR,
477             M_WAITOK | M_ZERO);
478         efp->ef_ifp = ifp;
479         efp->ef_frametype = ft;
480         eifp = &efp->ef_ac.ac_if;
481         ksnprintf(eifp->if_xname, IFNAMSIZ,
482             "%sf%d", ifp->if_xname, efp->ef_frametype);
483         eifp->if_dname = "ef";
484         eifp->if_dunit = IF_DUNIT_NONE;
485         eifp->if_softc = efp;
486         if (ifp->if_ioctl)
487                 eifp->if_ioctl = ef_ioctl;
488         efl->el_units[ft] = efp;
489         return 0;
490 }
491
492 static int
493 ef_load(void)
494 {
495         struct ifnet *ifp;
496         struct efnet *efp;
497         struct ef_link *efl = NULL;
498         int error = 0, d;
499
500         TAILQ_FOREACH(ifp, &ifnet, if_link) {
501                 if (ifp->if_type != IFT_ETHER) continue;
502                 EFDEBUG("Found interface %s\n", ifp->if_xname);
503                 efl = (struct ef_link*)kmalloc(sizeof(struct ef_link), 
504                     M_IFADDR, M_WAITOK | M_ZERO);
505
506                 efl->el_ifp = ifp;
507 #ifdef ETHER_II
508                 error = ef_clone(efl, ETHER_FT_EII);
509                 if (error) break;
510 #endif
511 #ifdef ETHER_8023
512                 error = ef_clone(efl, ETHER_FT_8023);
513                 if (error) break;
514 #endif
515 #ifdef ETHER_8022
516                 error = ef_clone(efl, ETHER_FT_8022);
517                 if (error) break;
518 #endif
519 #ifdef ETHER_SNAP
520                 error = ef_clone(efl, ETHER_FT_SNAP);
521                 if (error) break;
522 #endif
523                 efcount++;
524                 SLIST_INSERT_HEAD(&efdev, efl, el_next);
525         }
526         if (error) {
527                 if (efl)
528                         SLIST_INSERT_HEAD(&efdev, efl, el_next);
529                 SLIST_FOREACH(efl, &efdev, el_next) {
530                         for (d = 0; d < EF_NFT; d++)
531                                 if (efl->el_units[d])
532                                         kfree(efl->el_units[d], M_IFADDR);
533                         kfree(efl, M_IFADDR);
534                 }
535                 return error;
536         }
537         SLIST_FOREACH(efl, &efdev, el_next) {
538                 for (d = 0; d < EF_NFT; d++) {
539                         efp = efl->el_units[d];
540                         if (efp)
541                                 ef_attach(efp);
542                 }
543         }
544         ef_inputp = ef_input;
545         ef_outputp = ef_output;
546         EFDEBUG("Loaded\n");
547         return 0;
548 }
549
550 static int
551 ef_unload(void)
552 {
553         struct efnet *efp;
554         struct ef_link *efl;
555         int d;
556
557         ef_inputp = NULL;
558         ef_outputp = NULL;
559         SLIST_FOREACH(efl, &efdev, el_next) {
560                 for (d = 0; d < EF_NFT; d++) {
561                         efp = efl->el_units[d];
562                         if (efp) {
563                                 ef_detach(efp);
564                         }
565                 }
566         }
567         EFDEBUG("Unloaded\n");
568         return 0;
569 }
570
571 static int 
572 if_ef_modevent(module_t mod, int type, void *data)
573 {
574         switch ((modeventtype_t)type) {
575             case MOD_LOAD:
576                 return ef_load();
577             case MOD_UNLOAD:
578                 return ef_unload();
579             default:
580                 break;
581         }
582         return 0;
583 }
584
585 static moduledata_t if_ef_mod = {
586         "if_ef", if_ef_modevent, NULL
587 };
588
589 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);