Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/net if_initalize() and if_attach() failed when resource ...



details:   https://anonhg.NetBSD.org/src/rev/98124180d65f
branches:  trunk
changeset: 827302:98124180d65f
user:      msaitoh <msaitoh%NetBSD.org@localhost>
date:      Mon Oct 23 09:21:20 2017 +0000

description:
if_initalize() and if_attach() failed when resource allocation failed
(e.g. allocating softint). Without this change, it panics. It's bad because
resource shortage really occured when a lot of pseudo interface is created.
To avoid this problem, don't panic and change return value of if_initialize()
and if_attach() to int. Caller fanction will be recover from error cleanly by
checking the return value.

diffstat:

 sys/net/if.c |  38 ++++++++++++++++++++++++++++++--------
 sys/net/if.h |   7 ++++---
 2 files changed, 34 insertions(+), 11 deletions(-)

diffs (115 lines):

diff -r 5e10dec40bb1 -r 98124180d65f sys/net/if.c
--- a/sys/net/if.c      Mon Oct 23 08:08:53 2017 +0000
+++ b/sys/net/if.c      Mon Oct 23 09:21:20 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if.c,v 1.395 2017/06/27 12:17:27 roy Exp $     */
+/*     $NetBSD: if.c,v 1.396 2017/10/23 09:21:20 msaitoh Exp $ */
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.395 2017/06/27 12:17:27 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.396 2017/10/23 09:21:20 msaitoh Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_inet.h"
@@ -670,9 +670,11 @@
  *     ether_ifattach(ifp, enaddr);
  *     if_register(ifp);
  */
-void
+int
 if_initialize(ifnet_t *ifp)
 {
+       int rv = 0;
+
        KASSERT(if_indexlim > 0);
        TAILQ_INIT(&ifp->if_addrlist);
 
@@ -711,8 +713,10 @@
        if (if_is_link_state_changeable(ifp)) {
                ifp->if_link_si = softint_establish(SOFTINT_NET,
                    if_link_state_change_si, ifp);
-               if (ifp->if_link_si == NULL)
-                       panic("%s: softint_establish() failed", __func__);
+               if (ifp->if_link_si == NULL) {
+                       rv = ENOMEM;
+                       goto fail;
+               }
        }
 
        PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
@@ -724,6 +728,18 @@
        IFNET_LOCK();
        if_getindex(ifp);
        IFNET_UNLOCK();
+
+       return 0;
+
+fail:
+       IF_AFDATA_LOCK_DESTROY(ifp);
+
+       pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
+       (void)pfil_head_destroy(ifp->if_pfil);
+
+       IFQ_LOCK_DESTROY(&ifp->if_snd);
+
+       return rv;
 }
 
 /*
@@ -1094,13 +1110,19 @@
  * migrate softint-based if_input without much changes. If you don't
  * want to enable it, use if_initialize instead.
  */
-void
+int
 if_attach(ifnet_t *ifp)
 {
-
-       if_initialize(ifp);
+       int rv;
+
+       rv = if_initialize(ifp);
+       if (rv != 0)
+               return rv;
+
        ifp->if_percpuq = if_percpuq_create(ifp);
        if_register(ifp);
+
+       return 0;
 }
 
 void
diff -r 5e10dec40bb1 -r 98124180d65f sys/net/if.h
--- a/sys/net/if.h      Mon Oct 23 08:08:53 2017 +0000
+++ b/sys/net/if.h      Mon Oct 23 09:21:20 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if.h,v 1.240 2017/06/27 12:17:27 roy Exp $     */
+/*     $NetBSD: if.h,v 1.241 2017/10/23 09:21:20 msaitoh Exp $ */
 
 /*-
  * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
@@ -923,6 +923,7 @@
 
 #define IFQ_LOCK_INIT(ifq)     (ifq)->ifq_lock =                       \
            mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET)
+#define IFQ_LOCK_DESTROY(ifq)  mutex_obj_free((ifq)->ifq_lock)
 #define IFQ_LOCK(ifq)          mutex_enter((ifq)->ifq_lock)
 #define IFQ_UNLOCK(ifq)                mutex_exit((ifq)->ifq_lock)
 
@@ -946,9 +947,9 @@
     const struct sockaddr_dl *);
 void   if_set_sadl(struct ifnet *, const void *, u_char, bool);
 void   if_alloc_sadl(struct ifnet *);
-void   if_initialize(struct ifnet *);
+int    if_initialize(struct ifnet *);
 void   if_register(struct ifnet *);
-void   if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */
+int    if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */
 void   if_attachdomain(void);
 void   if_deactivate(struct ifnet *);
 bool   if_is_deactivated(const struct ifnet *);



Home | Main Index | Thread Index | Old Index