• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 
NetIfSetLinkStatus(const struct NetDevice * netDevice,NetIfLinkStatus status)411 int32_t NetIfSetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus status)
412 {
413     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
414     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setLinkStatus != NULL) {
415         return ndImpl->interFace->setLinkStatus(ndImpl, status);
416     }
417     HDF_LOGE("%s: netDevice not init or already free.", __func__);
418     return HDF_ERR_INVALID_PARAM;
419 }
420 
NetIfGetLinkStatus(const struct NetDevice * netDevice,NetIfLinkStatus * status)421 int32_t NetIfGetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus *status)
422 {
423     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
424     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->getLinkStatus != NULL) {
425         return ndImpl->interFace->getLinkStatus(ndImpl, status);
426     }
427     HDF_LOGE("%s: netDevice not init or already free.", __func__);
428     return HDF_ERR_INVALID_PARAM;
429 }
430 
NetIfDhcpsStart(const struct NetDevice * netDevice,char * ip,uint16_t ipNum)431 int32_t NetIfDhcpsStart(const struct NetDevice *netDevice, char *ip, uint16_t ipNum)
432 {
433     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
434     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpsStart != NULL) {
435         return ndImpl->interFace->dhcpsStart(ndImpl, ip, ipNum);
436     }
437     HDF_LOGE("%s: netDevice not init or already free.", __func__);
438     return HDF_ERR_INVALID_PARAM;
439 }
440 
NetIfDhcpsStop(const struct NetDevice * netDevice)441 int32_t NetIfDhcpsStop(const struct NetDevice *netDevice)
442 {
443     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
444     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpsStop != NULL) {
445         return ndImpl->interFace->dhcpsStop(ndImpl);
446     }
447     HDF_LOGE("%s: netDevice not init or already free.", __func__);
448     return HDF_ERR_INVALID_PARAM;
449 }
450 
NetIfDhcpStart(const struct NetDevice * netDevice)451 int32_t NetIfDhcpStart(const struct NetDevice *netDevice)
452 {
453     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
454     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpStart != NULL) {
455         return ndImpl->interFace->dhcpStart(ndImpl);
456     }
457     HDF_LOGE("%s: netDevice not init or already free.", __func__);
458     return HDF_ERR_INVALID_PARAM;
459 }
460 
NetIfDhcpStop(const struct NetDevice * netDevice)461 int32_t NetIfDhcpStop(const struct NetDevice *netDevice)
462 {
463     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
464     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpStop != NULL) {
465         return ndImpl->interFace->dhcpStop(ndImpl);
466     }
467     HDF_LOGE("%s: netDevice not init or already free.", __func__);
468     return HDF_ERR_INVALID_PARAM;
469 }
470 
NetIfDhcpIsBound(const struct NetDevice * netDevice)471 int32_t NetIfDhcpIsBound(const struct NetDevice *netDevice)
472 {
473     struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
474     if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpIsBound != NULL) {
475         return ndImpl->interFace->dhcpIsBound(ndImpl);
476     }
477     HDF_LOGE("%s: netDevice not init or already free.", __func__);
478     return HDF_ERR_INVALID_PARAM;
479 }
480 
481 /*
482  * Alloc a net buffer for the net device and reserve headroom depended on net device setting
483  *
484  * @param  : dev The net device
485  *           size The net buffer size
486  * @return : A new net buffer on success or NULL on fail
487  */
NetBufDevAlloc(const struct NetDevice * dev,uint32_t size)488 NetBuf *NetBufDevAlloc(const struct NetDevice *dev, uint32_t size)
489 {
490     uint32_t reserve = 0;
491     NetBuf *nb = NULL;
492 
493     if (dev != NULL) {
494         reserve = dev->neededHeadRoom + dev->neededTailRoom;
495     }
496 
497     size += reserve;
498 
499     nb = NetBufAlloc(size);
500     if (nb == NULL) {
501         return NULL;
502     }
503 
504     if (dev != NULL) {
505         nb->dev = (void *)dev;
506         NetBufPop(nb, E_TAIL_BUF, dev->neededHeadRoom);
507         NetBufPop(nb, E_DATA_BUF, dev->neededHeadRoom);
508     }
509 
510     return nb;
511 }
512