1  /*
2   * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3   *
4   * HDF is dual licensed: you can use it either under the terms of
5   * the GPL, or the BSD license, at your option.
6   * See the LICENSE file in the root of this repository for complete details.
7   */
8  
9  #include "net_device.h"
10  #include "securec.h"
11  
12  #include "osal_mem.h"
13  #include "net_device_impl.h"
14  #include "net_device_adapter.h"
15  
16  #define HDF_LOG_TAG "NetDevice"
17  
18  static struct NetDeviceImpl *g_netDeviceImplTable[MAX_NETDEVICE_COUNT] = {NULL};
19  
FindAvailableTable(uint32_t * index)20  static bool FindAvailableTable(uint32_t *index)
21  {
22      uint32_t i;
23  
24      if (index == NULL) {
25          HDF_LOGE("%s Find Available table index error!", __func__);
26          return false;
27      }
28      for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
29          if (g_netDeviceImplTable[i] == NULL) {
30              *index = i;
31              return true;
32          }
33      }
34      return false;
35  }
36  
AddNetDeviceImplToTable(uint32_t index,struct NetDeviceImpl * netDeviceImpl)37  static bool AddNetDeviceImplToTable(uint32_t index, struct NetDeviceImpl *netDeviceImpl)
38  {
39      if (index >= MAX_NETDEVICE_COUNT) {
40          HDF_LOGE("%s error because of not enough space!", __func__);
41          return false;
42      }
43      g_netDeviceImplTable[index] = netDeviceImpl;
44      return true;
45  }
46  
DeleteNetDeviceImplFromTable(const struct NetDeviceImpl * netDeviceImpl)47  static void DeleteNetDeviceImplFromTable(const struct NetDeviceImpl *netDeviceImpl)
48  {
49      int32_t i;
50  
51      for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
52          if (g_netDeviceImplTable[i] == netDeviceImpl) {
53              g_netDeviceImplTable[i] = NULL;
54              return;
55          }
56      }
57      return;
58  }
59  
InitNetDeviceImpl(NetDevice * nd,NetIfCategory ifCategory)60  static struct NetDeviceImpl *InitNetDeviceImpl(NetDevice *nd, NetIfCategory ifCategory)
61  {
62      struct NetDeviceImpl *ndImpl = NULL;
63      if (nd == NULL) {
64          return NULL;
65      }
66      ndImpl = (struct NetDeviceImpl *)OsalMemCalloc(sizeof(struct NetDeviceImpl));
67      if (ndImpl == NULL) {
68          HDF_LOGE("%s fail: OsalMemCalloc fail!", __func__);
69          return NULL;
70      }
71  
72      ndImpl->netDevice = nd;
73      if (RegisterNetDeviceImpl(ndImpl) != HDF_SUCCESS) {
74          HDF_LOGE("%s fail: resiter lite impl fail!", __func__);
75          OsalMemFree(ndImpl);
76          ndImpl = NULL;
77      }
78      return ndImpl;
79  }
80  
DeInitNetDeviceImpl(struct NetDeviceImpl * netDeviceImpl)81  static void DeInitNetDeviceImpl(struct NetDeviceImpl *netDeviceImpl)
82  {
83      if (netDeviceImpl == NULL) {
84          HDF_LOGE("%s success : already free!", __func__);
85          return;
86      }
87  
88      /* release osPrivate */
89      if (netDeviceImpl->interFace != NULL && netDeviceImpl->interFace->deInit != NULL) {
90          netDeviceImpl->interFace->deInit(netDeviceImpl);
91      }
92  
93      /* step 1 : release interFace. step 2 : release netdevice */
94      if (netDeviceImpl->netDevice != NULL) {
95          UnRegisterNetDeviceImpl(netDeviceImpl);
96          OsalMemFree(netDeviceImpl->netDevice);
97          netDeviceImpl->netDevice = NULL;
98      }
99  
100      /* last release netDeviceImpl */
101      OsalMemFree(netDeviceImpl);
102      HDF_LOGI("%s success!", __func__);
103      return;
104  }
105  
GetImplByNetDevice(const struct NetDevice * netDevice)106  static struct NetDeviceImpl *GetImplByNetDevice(const struct NetDevice *netDevice)
107  {
108      struct NetDeviceImpl *ndImpl = NULL;
109      int32_t i;
110  
111      for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
112          if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
113              if (netDevice == g_netDeviceImplTable[i]->netDevice) {
114                  ndImpl = g_netDeviceImplTable[i];
115                  return ndImpl;
116              }
117          }
118      }
119      HDF_LOGE("%s Get Impl by netdevice failed", __func__);
120      return ndImpl;
121  }
122  
123  #ifdef CONFIG_DRIVERS_HDF_NETDEV_EXT
GetLinuxInfByNetDevice(const struct NetDevice * netDevice)124  struct net_device *GetLinuxInfByNetDevice(const struct NetDevice *netDevice)
125  {
126      struct NetDeviceImpl *impl = NULL;
127      struct FullNetDevicePriv *priv = NULL;
128  
129      impl = GetImplByNetDevice(netDevice);
130      if (impl == NULL || impl->osPrivate == NULL) {
131          return NULL;
132      }
133  
134      priv = (struct FullNetDevicePriv *)impl->osPrivate;
135      return priv->dev;
136  }
137  
GetHdfNetDeviceByLinuxInf(struct net_device * dev)138  struct NetDevice *GetHdfNetDeviceByLinuxInf(struct net_device *dev)
139  {
140      struct NetDevice *netDev = NULL;
141      struct FullNetDevicePriv *priv = (struct FullNetDevicePriv *)netdev_priv(dev);
142      netDev = priv->impl->netDevice;
143      return netDev;
144  }
145  #endif
146  
NetDeviceInit(const char * ifName,uint32_t len,NetLinkType type,NetIfCategory ifCategory)147  struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetLinkType type, NetIfCategory ifCategory)
148  {
149      NetDevice *netDevice = NULL;
150      struct NetDeviceImpl *ndImpl = NULL;
151      uint32_t index = 0;
152      int32_t ret;
153  
154      if ((ifName == NULL) || (strlen(ifName) != len) || (strlen(ifName) > IFNAMSIZ - 1)) {
155          HDF_LOGE("%s fail: ifName = null or len not right!", __func__);
156          return NULL;
157      }
158      netDevice = (NetDevice *)OsalMemCalloc(sizeof(NetDevice));
159      if (netDevice == NULL) {
160          HDF_LOGE("%s fail: OsalMemCalloc fail!", __func__);
161          return NULL;
162      }
163      if (strcpy_s(netDevice->name, IFNAMSIZ, ifName) != EOK) {
164          HDF_LOGE("%s fail: strcpy_s fail!", __func__);
165          OsalMemFree(netDevice);
166          return NULL;
167      }
168      netDevice->netifCateg = ifCategory;
169      ndImpl = InitNetDeviceImpl(netDevice, ifCategory);
170      if (ndImpl == NULL) {
171          HDF_LOGE("%s fail: InitNetDeviceImpl fail!", __func__);
172          OsalMemFree(netDevice);
173          return NULL;
174      }
175      if (FindAvailableTable(&index)) {
176          AddNetDeviceImplToTable(index, ndImpl);
177      } else {
178          DeInitNetDeviceImpl(ndImpl);
179          HDF_LOGE("%s fail: Not extra table.", __func__);
180          return NULL;
181      }
182      /* INIT OSPrivate */
183      ret = HDF_FAILURE;
184      if (ndImpl->interFace != NULL && ndImpl->interFace->init != NULL) {
185          ret = ndImpl->interFace->init(ndImpl);
186      }
187      if (ret != HDF_SUCCESS) {
188          HDF_LOGE("%s fail : interface->init fail!", __func__);
189          DeleteNetDeviceImplFromTable(ndImpl);
190          DeInitNetDeviceImpl(ndImpl);
191          return NULL;
192      }
193      netDevice->linkLayerType = type;
194      HDF_LOGI("Init Net Device success!");
195      return netDevice;
196  }
197  
NetDeviceDeInit(struct NetDevice * netDevice)198  int32_t NetDeviceDeInit(struct NetDevice *netDevice)
199  {
200      struct NetDeviceImpl *ndImpl = NULL;
201  
202      if (netDevice == NULL) {
203          HDF_LOGI("%s success: already deinit!", __func__);
204          return HDF_SUCCESS;
205      }
206      ndImpl = GetImplByNetDevice(netDevice);
207      if (ndImpl == NULL) {
208          HDF_LOGI("%s success: already free.", __func__);
209          OsalMemFree(netDevice);
210          return HDF_SUCCESS;
211      }
212      DeleteNetDeviceImplFromTable(ndImpl);
213      DeInitNetDeviceImpl(ndImpl);
214      return HDF_SUCCESS;
215  }
216  
NetDeviceAdd(struct NetDevice * netDevice)217  int32_t NetDeviceAdd(struct NetDevice *netDevice)
218  {
219      struct NetDeviceImplOp *op = NULL;
220      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
221  
222      if (ndImpl == NULL) {
223          HDF_LOGE("%s fail: netDevice not exist!", __func__);
224          return HDF_ERR_INVALID_PARAM;
225      }
226      op = ndImpl->interFace;
227      if (op == NULL || op->add == NULL) {
228          HDF_LOGE("%s fail: Impl Add not exist.", __func__);
229          return HDF_ERR_INVALID_PARAM;
230      }
231      return op->add(ndImpl);
232  }
233  
NetDeviceDelete(struct NetDevice * netDevice)234  int32_t NetDeviceDelete(struct NetDevice *netDevice)
235  {
236      struct NetDeviceImplOp *op = NULL;
237      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
238  
239      if (ndImpl == NULL) {
240          HDF_LOGE("%s fail: netDevice not exist!", __func__);
241          return HDF_ERR_INVALID_PARAM;
242      }
243      op = ndImpl->interFace;
244      if (op == NULL || op->delete == NULL) {
245          HDF_LOGE("%s fail: Impl op Delete exist.", __func__);
246          return HDF_ERR_INVALID_PARAM;
247      }
248      return op->delete(ndImpl);
249  }
250  
NetDeviceGetInstByName(const char * name)251  struct NetDevice *NetDeviceGetInstByName(const char *name)
252  {
253      int32_t i;
254  
255      if (name == NULL) {
256          HDF_LOGE("%s fail: name = NULL.", __func__);
257          return NULL;
258      }
259      for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
260          if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
261              if (strcmp(g_netDeviceImplTable[i]->netDevice->name, name) == 0) {
262                  return g_netDeviceImplTable[i]->netDevice;
263              }
264          }
265      }
266      HDF_LOGE("%s fail: %s: name not exist.", __func__, name);
267      return NULL;
268  }
269  
NetDeviceIsAnyInstRunning(void)270  bool NetDeviceIsAnyInstRunning(void)
271  {
272      struct NetDevice *netDev = NULL;
273      int32_t i;
274  
275      for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
276          if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
277              netDev = g_netDeviceImplTable[i]->netDevice;
278              if ((GET_NET_DEV_FLAGS(netDev) & NET_DEVICE_IFF_RUNNING) != 0) {
279                  return true;
280              }
281          }
282      }
283      return false;
284  }
285  
NetDeviceIsInstRunning(const struct NetDevice * netDevice)286  bool NetDeviceIsInstRunning(const struct NetDevice *netDevice)
287  {
288      if (netDevice == NULL) {
289          return false;
290      }
291      if ((GET_NET_DEV_FLAGS(netDevice) & NET_DEVICE_IFF_RUNNING) != 0) {
292          return true;
293      }
294      return false;
295  }
296  
NetDevGetRegisterCount(void)297  uint32_t NetDevGetRegisterCount(void)
298  {
299      uint32_t count = 0;
300      int32_t i;
301  
302      for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
303          if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
304              count++;
305          }
306      }
307      return count;
308  }
309  
NetDeviceGetCap(void)310  uint32_t NetDeviceGetCap(void)
311  {
312      return MAX_NETDEVICE_COUNT;
313  }
314  
NetDeviceGetInstByIndex(uint32_t index)315  struct NetDevice *NetDeviceGetInstByIndex(uint32_t index)
316  {
317      if (index < MAX_NETDEVICE_COUNT) {
318          if (g_netDeviceImplTable[index] != NULL) {
319              return g_netDeviceImplTable[index]->netDevice;
320          } else {
321              return NULL;
322          }
323      }
324      return NULL;
325  }
326  
NetIfSetAddr(const struct NetDevice * netDevice,const IpV4Addr * ipAddr,const IpV4Addr * netMask,const IpV4Addr * gw)327  int32_t NetIfSetAddr(const struct NetDevice *netDevice, const IpV4Addr *ipAddr, const IpV4Addr *netMask,
328      const IpV4Addr *gw)
329  {
330      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
331      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setIpAddr != NULL) {
332          return ndImpl->interFace->setIpAddr(ndImpl, ipAddr, netMask, gw);
333      }
334      HDF_LOGE("%s: netDevice not init or already free.", __func__);
335      return HDF_ERR_INVALID_PARAM;
336  }
337  
NetIfRxImpl(const struct NetDevice * netDevice,NetBuf * buff,ReceiveFlag flag)338  static int32_t NetIfRxImpl(const struct NetDevice *netDevice, NetBuf *buff, ReceiveFlag flag)
339  {
340      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
341      ProcessingResult ret = PROCESSING_CONTINUE;
342  
343      if (ndImpl == NULL || ndImpl->interFace == NULL || ndImpl->interFace->receive == NULL) {
344          HDF_LOGE("%s: NetIfRxImpl fail : netdevice not exist!", __func__);
345          return HDF_ERR_INVALID_PARAM;
346      }
347  
348      /* to do driver special process */
349      if (netDevice->netDeviceIf != NULL && netDevice->netDeviceIf->specialEtherTypeProcess != NULL) {
350          ret = netDevice->netDeviceIf->specialEtherTypeProcess(netDevice, buff);
351      }
352      /* Sent to TCP/IP Stack. */
353      if (ret == PROCESSING_CONTINUE) {
354          return ndImpl->interFace->receive(ndImpl, buff, flag);
355      } else if (ret == PROCESSING_COMPLETE) {
356          HDF_LOGI("NetIfRxImpl specialEtherType Process not need TCP/IP stack!");
357          return HDF_SUCCESS;
358      } else {
359          HDF_LOGE("%s: NetIfRxImpl specialEtherType Process error", __func__);
360          return HDF_FAILURE;
361      }
362  }
363  
NetIfSetMacAddr(struct NetDevice * netDevice,const unsigned char * macAddr,unsigned char length)364  int32_t NetIfSetMacAddr(struct NetDevice *netDevice, const unsigned char *macAddr, unsigned char length)
365  {
366      HDF_STATUS ret;
367      struct NetDeviceImpl *ndImpl = NULL;
368  
369      if (macAddr == NULL || length != MAC_ADDR_SIZE) {
370          HDF_LOGE("%s fail: input param error!", __func__);
371          return HDF_ERR_INVALID_PARAM;
372      }
373      if (memcpy_s(netDevice->macAddr, MAC_ADDR_SIZE, macAddr, MAC_ADDR_SIZE) != EOK) {
374          HDF_LOGE("%s fail : memcpy_s fail!", __func__);
375          return HDF_FAILURE;
376      }
377      if (netDevice->netDeviceIf != NULL && netDevice->netDeviceIf->setMacAddr != NULL) {
378          ret = netDevice->netDeviceIf->setMacAddr(netDevice, (void*)macAddr);
379          if (ret != HDF_SUCCESS) {
380              HDF_LOGE("%s fail : setMacAddr fail!", __func__);
381              return ret;
382          }
383      }
384      ndImpl = GetImplByNetDevice(netDevice);
385      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->changeMacAddr != NULL) {
386          return ndImpl->interFace->changeMacAddr(ndImpl);
387      }
388      return HDF_FAILURE;
389  }
390  
NetIfRx(const struct NetDevice * netDevice,NetBuf * buff)391  int32_t NetIfRx(const struct NetDevice *netDevice, NetBuf *buff)
392  {
393      return NetIfRxImpl(netDevice, buff, IN_INTERRUPT);
394  }
395  
NetIfRxNi(const struct NetDevice * netDevice,NetBuf * buff)396  int32_t NetIfRxNi(const struct NetDevice *netDevice, NetBuf *buff)
397  {
398      return NetIfRxImpl(netDevice, buff, NO_IN_INTERRUPT);
399  }
400  
NetIfSetStatus(const struct NetDevice * netDevice,NetIfStatus status)401  int32_t NetIfSetStatus(const struct NetDevice *netDevice, NetIfStatus status)
402  {
403      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
404      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setStatus != NULL) {
405          return ndImpl->interFace->setStatus(ndImpl, status);
406      }
407      HDF_LOGE("%s: netDevice not init or already free.", __func__);
408      return HDF_ERR_INVALID_PARAM;
409  }
410  
411  #if defined(CONFIG_DRIVERS_HDF_IMX8MM_ETHERNET)
412  #define NAPI_ADD_NUM   (64)
NetIfNapiAdd(struct NetDevice * netDevice,struct napi_struct * napi,int (* poll)(struct napi_struct *,int),int weight)413  void NetIfNapiAdd(struct NetDevice *netDevice, struct napi_struct *napi,
414      int (*poll)(struct napi_struct *, int), int weight)
415  {
416      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
417  
418      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->netif_napi_add != NULL) {
419          ndImpl->interFace->netif_napi_add(ndImpl, napi, poll, NAPI_ADD_NUM);
420      }
421      HDF_LOGE("%s: NetIfNapiAdd failed.", __func__);
422  }
423  
NetIfGetTxQueue(struct NetDevice * netDevice,unsigned int index)424  struct netdev_queue *NetIfGetTxQueue(struct NetDevice *netDevice, unsigned int index)
425  {
426      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
427  
428      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->get_tx_queue != NULL) {
429          return ndImpl->interFace->get_tx_queue(ndImpl, index);
430      }
431      HDF_LOGE("%s: NetIfGetTxqueue failed.", __func__);
432      return NULL;
433  }
434  
NetIfTypeTrans(struct NetDevice * netDevice,struct sk_buff * skb)435  __be16 NetIfTypeTrans(struct NetDevice *netDevice, struct sk_buff *skb)
436  {
437      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
438  
439      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->type_trans != NULL) {
440          return ndImpl->interFace->type_trans(ndImpl, skb);
441      }
442      HDF_LOGE("%s: NetIfEnteRx failed.", __func__);
443      return NULL;
444  }
445  
NetIfEnteAllocBuf(struct NetDevice * netDevice,uint32_t length)446  struct sk_buff *NetIfEnteAllocBuf(struct NetDevice *netDevice, uint32_t length)
447  {
448      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
449  
450      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->alloc_buf != NULL) {
451          return ndImpl->interFace->alloc_buf(ndImpl, length);
452      }
453      HDF_LOGE("%s: NetIfEnteAllocBuf failed.", __func__);
454      return NULL;
455  }
456  
NetIfStartQueue(struct NetDevice * netDevice)457  void NetIfStartQueue(struct NetDevice *netDevice)
458  {
459      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
460  
461      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->start_queue != NULL) {
462          ndImpl->interFace->start_queue(ndImpl);
463      }
464      HDF_LOGE("%s: NetIfStartAllqueue failed.", __func__);
465  }
466  
NetIfDisableTx(struct NetDevice * netDevice)467  void NetIfDisableTx(struct NetDevice *netDevice)
468  {
469      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
470  
471      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->disable_tx != NULL) {
472          ndImpl->interFace->disable_tx(ndImpl);
473      }
474      HDF_LOGE("%s: NetIfDisableTx failed.", __func__);
475  }
476  
NetIfSetDev(struct NetDevice * netDevice,struct device * dev)477  void NetIfSetDev(struct NetDevice *netDevice, struct device *dev)
478  {
479      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
480  
481      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->set_dev != NULL) {
482          ndImpl->interFace->set_dev(ndImpl, dev);
483      }
484      HDF_LOGE("%s: NetIfSetDev failed.", __func__);
485  }
486  
NetIfWakeQueue(struct NetDevice * netDevice)487  void NetIfWakeQueue(struct NetDevice *netDevice)
488  {
489      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
490  
491      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->wake_queue != NULL) {
492          ndImpl->interFace->wake_queue(ndImpl);
493      }
494      HDF_LOGE("%s: NetIfWakequeue failed.", __func__);
495  }
496  
NetIfOfPhyConnect(struct NetDevice * netDevice,struct device_node * phy_np,void (* hndlr)(struct net_device *),u32 flags,phy_interface_t iface)497  struct phy_device *NetIfOfPhyConnect(struct NetDevice *netDevice,
498                                       struct device_node *phy_np,
499                                       void (*hndlr)(struct net_device *),
500                                       u32 flags,
501                                       phy_interface_t iface)
502  {
503      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
504  
505      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->of_phyconnect != NULL) {
506          return ndImpl->interFace->of_phyconnect(ndImpl, phy_np, hndlr, 0, iface);
507      }
508      HDF_LOGE("%s: NetIfOfPhyConnect failed.", __func__);
509      return NULL;
510  }
511  #endif
512  
NetIfSetLinkStatus(const struct NetDevice * netDevice,NetIfLinkStatus status)513  int32_t NetIfSetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus status)
514  {
515      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
516      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setLinkStatus != NULL) {
517          return ndImpl->interFace->setLinkStatus(ndImpl, status);
518      }
519      HDF_LOGE("%s: netDevice not init or already free.", __func__);
520      return HDF_ERR_INVALID_PARAM;
521  }
522  
NetIfGetLinkStatus(const struct NetDevice * netDevice,NetIfLinkStatus * status)523  int32_t NetIfGetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus *status)
524  {
525      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
526      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->getLinkStatus != NULL) {
527          return ndImpl->interFace->getLinkStatus(ndImpl, status);
528      }
529      HDF_LOGE("%s: netDevice not init or already free.", __func__);
530      return HDF_ERR_INVALID_PARAM;
531  }
532  
NetIfDhcpsStart(const struct NetDevice * netDevice,char * ip,uint16_t ipNum)533  int32_t NetIfDhcpsStart(const struct NetDevice *netDevice, char *ip, uint16_t ipNum)
534  {
535      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
536      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpsStart != NULL) {
537          return ndImpl->interFace->dhcpsStart(ndImpl, ip, ipNum);
538      }
539      HDF_LOGE("%s: netDevice not init or already free.", __func__);
540      return HDF_ERR_INVALID_PARAM;
541  }
542  
NetIfDhcpsStop(const struct NetDevice * netDevice)543  int32_t NetIfDhcpsStop(const struct NetDevice *netDevice)
544  {
545      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
546      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpsStop != NULL) {
547          return ndImpl->interFace->dhcpsStop(ndImpl);
548      }
549      HDF_LOGE("%s: netDevice not init or already free.", __func__);
550      return HDF_ERR_INVALID_PARAM;
551  }
552  
NetIfDhcpStart(const struct NetDevice * netDevice)553  int32_t NetIfDhcpStart(const struct NetDevice *netDevice)
554  {
555      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
556      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpStart != NULL) {
557          return ndImpl->interFace->dhcpStart(ndImpl);
558      }
559      HDF_LOGE("%s: netDevice not init or already free.", __func__);
560      return HDF_ERR_INVALID_PARAM;
561  }
562  
NetIfDhcpStop(const struct NetDevice * netDevice)563  int32_t NetIfDhcpStop(const struct NetDevice *netDevice)
564  {
565      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
566      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpStop != NULL) {
567          return ndImpl->interFace->dhcpStop(ndImpl);
568      }
569      HDF_LOGE("%s: netDevice not init or already free.", __func__);
570      return HDF_ERR_INVALID_PARAM;
571  }
572  
NetIfDhcpIsBound(const struct NetDevice * netDevice)573  int32_t NetIfDhcpIsBound(const struct NetDevice *netDevice)
574  {
575      struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
576      if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpIsBound != NULL) {
577          return ndImpl->interFace->dhcpIsBound(ndImpl);
578      }
579      HDF_LOGE("%s: netDevice not init or already free.", __func__);
580      return HDF_ERR_INVALID_PARAM;
581  }
582  
583  /*
584   * Alloc a net buffer for the net device and reserve headroom depended on net device setting
585   *
586   * @param  : dev The net device
587   *           size The net buffer size
588   * @return : A new net buffer on success or NULL on fail
589   */
NetBufDevAlloc(const struct NetDevice * dev,uint32_t size)590  NetBuf *NetBufDevAlloc(const struct NetDevice *dev, uint32_t size)
591  {
592      uint32_t reserve = 0;
593      NetBuf *nb = NULL;
594  
595      if (dev != NULL) {
596          reserve = dev->neededHeadRoom + dev->neededTailRoom;
597      }
598  
599      size += reserve;
600  
601      nb = NetBufAlloc(size);
602      if (nb == NULL) {
603          return NULL;
604      }
605  
606      if (dev != NULL) {
607          nb->dev = (void *)dev;
608          NetBufPop(nb, E_TAIL_BUF, dev->neededHeadRoom);
609          NetBufPop(nb, E_DATA_BUF, dev->neededHeadRoom);
610      }
611  
612      return nb;
613  }
614