datapath.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. /*
  2. * Copyright (c) 2007-2013 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/lockdep.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "flow_netlink.h"
  56. #include "vport-internal_dev.h"
  57. #include "vport-netdev.h"
  58. int ovs_net_id __read_mostly;
  59. static void ovs_notify(struct genl_family *family,
  60. struct sk_buff *skb, struct genl_info *info)
  61. {
  62. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  63. 0, info->nlhdr, GFP_KERNEL);
  64. }
  65. /**
  66. * DOC: Locking:
  67. *
  68. * All writes e.g. Writes to device state (add/remove datapath, port, set
  69. * operations on vports, etc.), Writes to other state (flow table
  70. * modifications, set miscellaneous datapath parameters, etc.) are protected
  71. * by ovs_lock.
  72. *
  73. * Reads are protected by RCU.
  74. *
  75. * There are a few special cases (mostly stats) that have their own
  76. * synchronization but they nest under all of above and don't interact with
  77. * each other.
  78. *
  79. * The RTNL lock nests inside ovs_mutex.
  80. */
  81. static DEFINE_MUTEX(ovs_mutex);
  82. void ovs_lock(void)
  83. {
  84. mutex_lock(&ovs_mutex);
  85. }
  86. void ovs_unlock(void)
  87. {
  88. mutex_unlock(&ovs_mutex);
  89. }
  90. #ifdef CONFIG_LOCKDEP
  91. int lockdep_ovsl_is_held(void)
  92. {
  93. if (debug_locks)
  94. return lockdep_is_held(&ovs_mutex);
  95. else
  96. return 1;
  97. }
  98. #endif
  99. static struct vport *new_vport(const struct vport_parms *);
  100. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  101. const struct dp_upcall_info *);
  102. static int queue_userspace_packet(struct net *, int dp_ifindex,
  103. struct sk_buff *,
  104. const struct dp_upcall_info *);
  105. /* Must be called with rcu_read_lock or ovs_mutex. */
  106. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  107. {
  108. struct datapath *dp = NULL;
  109. struct net_device *dev;
  110. rcu_read_lock();
  111. dev = dev_get_by_index_rcu(net, dp_ifindex);
  112. if (dev) {
  113. struct vport *vport = ovs_internal_dev_get_vport(dev);
  114. if (vport)
  115. dp = vport->dp;
  116. }
  117. rcu_read_unlock();
  118. return dp;
  119. }
  120. /* Must be called with rcu_read_lock or ovs_mutex. */
  121. const char *ovs_dp_name(const struct datapath *dp)
  122. {
  123. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  124. return vport->ops->get_name(vport);
  125. }
  126. static int get_dpifindex(struct datapath *dp)
  127. {
  128. struct vport *local;
  129. int ifindex;
  130. rcu_read_lock();
  131. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  132. if (local)
  133. ifindex = netdev_vport_priv(local)->dev->ifindex;
  134. else
  135. ifindex = 0;
  136. rcu_read_unlock();
  137. return ifindex;
  138. }
  139. static void destroy_dp_rcu(struct rcu_head *rcu)
  140. {
  141. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  142. ovs_flow_tbl_destroy(&dp->table);
  143. free_percpu(dp->stats_percpu);
  144. release_net(ovs_dp_get_net(dp));
  145. kfree(dp->ports);
  146. kfree(dp);
  147. }
  148. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  149. u16 port_no)
  150. {
  151. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  152. }
  153. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  154. {
  155. struct vport *vport;
  156. struct hlist_head *head;
  157. head = vport_hash_bucket(dp, port_no);
  158. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  159. if (vport->port_no == port_no)
  160. return vport;
  161. }
  162. return NULL;
  163. }
  164. /* Called with ovs_mutex. */
  165. static struct vport *new_vport(const struct vport_parms *parms)
  166. {
  167. struct vport *vport;
  168. vport = ovs_vport_add(parms);
  169. if (!IS_ERR(vport)) {
  170. struct datapath *dp = parms->dp;
  171. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  172. hlist_add_head_rcu(&vport->dp_hash_node, head);
  173. }
  174. return vport;
  175. }
  176. void ovs_dp_detach_port(struct vport *p)
  177. {
  178. ASSERT_OVSL();
  179. /* First drop references to device. */
  180. hlist_del_rcu(&p->dp_hash_node);
  181. /* Then destroy it. */
  182. ovs_vport_del(p);
  183. }
  184. /* Must be called with rcu_read_lock. */
  185. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  186. {
  187. struct datapath *dp = p->dp;
  188. struct sw_flow *flow;
  189. struct dp_stats_percpu *stats;
  190. struct sw_flow_key key;
  191. u64 *stats_counter;
  192. u32 n_mask_hit;
  193. int error;
  194. stats = this_cpu_ptr(dp->stats_percpu);
  195. /* Extract flow from 'skb' into 'key'. */
  196. error = ovs_flow_extract(skb, p->port_no, &key);
  197. if (unlikely(error)) {
  198. kfree_skb(skb);
  199. return;
  200. }
  201. /* Look up flow. */
  202. flow = ovs_flow_tbl_lookup(&dp->table, &key, &n_mask_hit);
  203. if (unlikely(!flow)) {
  204. struct dp_upcall_info upcall;
  205. upcall.cmd = OVS_PACKET_CMD_MISS;
  206. upcall.key = &key;
  207. upcall.userdata = NULL;
  208. upcall.portid = p->upcall_portid;
  209. ovs_dp_upcall(dp, skb, &upcall);
  210. consume_skb(skb);
  211. stats_counter = &stats->n_missed;
  212. goto out;
  213. }
  214. OVS_CB(skb)->flow = flow;
  215. OVS_CB(skb)->pkt_key = &key;
  216. stats_counter = &stats->n_hit;
  217. ovs_flow_used(OVS_CB(skb)->flow, skb);
  218. ovs_execute_actions(dp, skb);
  219. out:
  220. /* Update datapath statistics. */
  221. u64_stats_update_begin(&stats->sync);
  222. (*stats_counter)++;
  223. stats->n_mask_hit += n_mask_hit;
  224. u64_stats_update_end(&stats->sync);
  225. }
  226. static struct genl_family dp_packet_genl_family = {
  227. .id = GENL_ID_GENERATE,
  228. .hdrsize = sizeof(struct ovs_header),
  229. .name = OVS_PACKET_FAMILY,
  230. .version = OVS_PACKET_VERSION,
  231. .maxattr = OVS_PACKET_ATTR_MAX,
  232. .netnsok = true,
  233. .parallel_ops = true,
  234. };
  235. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  236. const struct dp_upcall_info *upcall_info)
  237. {
  238. struct dp_stats_percpu *stats;
  239. int dp_ifindex;
  240. int err;
  241. if (upcall_info->portid == 0) {
  242. err = -ENOTCONN;
  243. goto err;
  244. }
  245. dp_ifindex = get_dpifindex(dp);
  246. if (!dp_ifindex) {
  247. err = -ENODEV;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  254. if (err)
  255. goto err;
  256. return 0;
  257. err:
  258. stats = this_cpu_ptr(dp->stats_percpu);
  259. u64_stats_update_begin(&stats->sync);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->sync);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct net *net, int dp_ifindex,
  265. struct sk_buff *skb,
  266. const struct dp_upcall_info *upcall_info)
  267. {
  268. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  269. struct dp_upcall_info later_info;
  270. struct sw_flow_key later_key;
  271. struct sk_buff *segs, *nskb;
  272. int err;
  273. segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
  274. if (IS_ERR(segs))
  275. return PTR_ERR(segs);
  276. /* Queue all of the segments. */
  277. skb = segs;
  278. do {
  279. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  280. if (err)
  281. break;
  282. if (skb == segs && gso_type & SKB_GSO_UDP) {
  283. /* The initial flow key extracted by ovs_flow_extract()
  284. * in this case is for a first fragment, so we need to
  285. * properly mark later fragments.
  286. */
  287. later_key = *upcall_info->key;
  288. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  289. later_info = *upcall_info;
  290. later_info.key = &later_key;
  291. upcall_info = &later_info;
  292. }
  293. } while ((skb = skb->next));
  294. /* Free all of the segments. */
  295. skb = segs;
  296. do {
  297. nskb = skb->next;
  298. if (err)
  299. kfree_skb(skb);
  300. else
  301. consume_skb(skb);
  302. } while ((skb = nskb));
  303. return err;
  304. }
  305. static size_t key_attr_size(void)
  306. {
  307. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  308. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  309. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  310. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  311. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  312. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  313. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  314. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  315. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  316. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  317. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  318. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  319. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  321. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  322. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  323. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  324. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  325. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  326. }
  327. static size_t upcall_msg_size(const struct sk_buff *skb,
  328. const struct nlattr *userdata)
  329. {
  330. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  331. + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
  332. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  333. /* OVS_PACKET_ATTR_USERDATA */
  334. if (userdata)
  335. size += NLA_ALIGN(userdata->nla_len);
  336. return size;
  337. }
  338. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  339. struct sk_buff *skb,
  340. const struct dp_upcall_info *upcall_info)
  341. {
  342. struct ovs_header *upcall;
  343. struct sk_buff *nskb = NULL;
  344. struct sk_buff *user_skb; /* to be queued to userspace */
  345. struct nlattr *nla;
  346. int err;
  347. if (vlan_tx_tag_present(skb)) {
  348. nskb = skb_clone(skb, GFP_ATOMIC);
  349. if (!nskb)
  350. return -ENOMEM;
  351. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  352. if (!nskb)
  353. return -ENOMEM;
  354. nskb->vlan_tci = 0;
  355. skb = nskb;
  356. }
  357. if (nla_attr_size(skb->len) > USHRT_MAX) {
  358. err = -EFBIG;
  359. goto out;
  360. }
  361. user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
  362. if (!user_skb) {
  363. err = -ENOMEM;
  364. goto out;
  365. }
  366. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  367. 0, upcall_info->cmd);
  368. upcall->dp_ifindex = dp_ifindex;
  369. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  370. ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  371. nla_nest_end(user_skb, nla);
  372. if (upcall_info->userdata)
  373. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  374. nla_len(upcall_info->userdata),
  375. nla_data(upcall_info->userdata));
  376. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  377. skb_copy_and_csum_dev(skb, nla_data(nla));
  378. genlmsg_end(user_skb, upcall);
  379. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  380. out:
  381. kfree_skb(nskb);
  382. return err;
  383. }
  384. static void clear_stats(struct sw_flow *flow)
  385. {
  386. flow->used = 0;
  387. flow->tcp_flags = 0;
  388. flow->packet_count = 0;
  389. flow->byte_count = 0;
  390. }
  391. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  392. {
  393. struct ovs_header *ovs_header = info->userhdr;
  394. struct nlattr **a = info->attrs;
  395. struct sw_flow_actions *acts;
  396. struct sk_buff *packet;
  397. struct sw_flow *flow;
  398. struct datapath *dp;
  399. struct ethhdr *eth;
  400. int len;
  401. int err;
  402. err = -EINVAL;
  403. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  404. !a[OVS_PACKET_ATTR_ACTIONS])
  405. goto err;
  406. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  407. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  408. err = -ENOMEM;
  409. if (!packet)
  410. goto err;
  411. skb_reserve(packet, NET_IP_ALIGN);
  412. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  413. skb_reset_mac_header(packet);
  414. eth = eth_hdr(packet);
  415. /* Normally, setting the skb 'protocol' field would be handled by a
  416. * call to eth_type_trans(), but it assumes there's a sending
  417. * device, which we may not have. */
  418. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  419. packet->protocol = eth->h_proto;
  420. else
  421. packet->protocol = htons(ETH_P_802_2);
  422. /* Build an sw_flow for sending this packet. */
  423. flow = ovs_flow_alloc();
  424. err = PTR_ERR(flow);
  425. if (IS_ERR(flow))
  426. goto err_kfree_skb;
  427. err = ovs_flow_extract(packet, -1, &flow->key);
  428. if (err)
  429. goto err_flow_free;
  430. err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
  431. if (err)
  432. goto err_flow_free;
  433. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  434. err = PTR_ERR(acts);
  435. if (IS_ERR(acts))
  436. goto err_flow_free;
  437. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  438. &flow->key, 0, &acts);
  439. rcu_assign_pointer(flow->sf_acts, acts);
  440. if (err)
  441. goto err_flow_free;
  442. OVS_CB(packet)->flow = flow;
  443. OVS_CB(packet)->pkt_key = &flow->key;
  444. packet->priority = flow->key.phy.priority;
  445. packet->mark = flow->key.phy.skb_mark;
  446. rcu_read_lock();
  447. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  448. err = -ENODEV;
  449. if (!dp)
  450. goto err_unlock;
  451. local_bh_disable();
  452. err = ovs_execute_actions(dp, packet);
  453. local_bh_enable();
  454. rcu_read_unlock();
  455. ovs_flow_free(flow, false);
  456. return err;
  457. err_unlock:
  458. rcu_read_unlock();
  459. err_flow_free:
  460. ovs_flow_free(flow, false);
  461. err_kfree_skb:
  462. kfree_skb(packet);
  463. err:
  464. return err;
  465. }
  466. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  467. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  468. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  469. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  470. };
  471. static const struct genl_ops dp_packet_genl_ops[] = {
  472. { .cmd = OVS_PACKET_CMD_EXECUTE,
  473. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  474. .policy = packet_policy,
  475. .doit = ovs_packet_cmd_execute
  476. }
  477. };
  478. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  479. struct ovs_dp_megaflow_stats *mega_stats)
  480. {
  481. int i;
  482. memset(mega_stats, 0, sizeof(*mega_stats));
  483. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  484. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  485. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  486. for_each_possible_cpu(i) {
  487. const struct dp_stats_percpu *percpu_stats;
  488. struct dp_stats_percpu local_stats;
  489. unsigned int start;
  490. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  491. do {
  492. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  493. local_stats = *percpu_stats;
  494. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  495. stats->n_hit += local_stats.n_hit;
  496. stats->n_missed += local_stats.n_missed;
  497. stats->n_lost += local_stats.n_lost;
  498. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  499. }
  500. }
  501. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  502. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  503. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  504. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  505. };
  506. static struct genl_family dp_flow_genl_family = {
  507. .id = GENL_ID_GENERATE,
  508. .hdrsize = sizeof(struct ovs_header),
  509. .name = OVS_FLOW_FAMILY,
  510. .version = OVS_FLOW_VERSION,
  511. .maxattr = OVS_FLOW_ATTR_MAX,
  512. .netnsok = true,
  513. .parallel_ops = true,
  514. };
  515. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  516. .name = OVS_FLOW_MCGROUP
  517. };
  518. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  519. {
  520. return NLMSG_ALIGN(sizeof(struct ovs_header))
  521. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  522. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  523. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  524. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  525. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  526. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  527. }
  528. /* Called with ovs_mutex. */
  529. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  530. struct sk_buff *skb, u32 portid,
  531. u32 seq, u32 flags, u8 cmd)
  532. {
  533. const int skb_orig_len = skb->len;
  534. struct nlattr *start;
  535. struct ovs_flow_stats stats;
  536. struct ovs_header *ovs_header;
  537. struct nlattr *nla;
  538. unsigned long used;
  539. u8 tcp_flags;
  540. int err;
  541. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  542. if (!ovs_header)
  543. return -EMSGSIZE;
  544. ovs_header->dp_ifindex = get_dpifindex(dp);
  545. /* Fill flow key. */
  546. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  547. if (!nla)
  548. goto nla_put_failure;
  549. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  550. if (err)
  551. goto error;
  552. nla_nest_end(skb, nla);
  553. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  554. if (!nla)
  555. goto nla_put_failure;
  556. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  557. if (err)
  558. goto error;
  559. nla_nest_end(skb, nla);
  560. spin_lock_bh(&flow->lock);
  561. used = flow->used;
  562. stats.n_packets = flow->packet_count;
  563. stats.n_bytes = flow->byte_count;
  564. tcp_flags = (u8)ntohs(flow->tcp_flags);
  565. spin_unlock_bh(&flow->lock);
  566. if (used &&
  567. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  568. goto nla_put_failure;
  569. if (stats.n_packets &&
  570. nla_put(skb, OVS_FLOW_ATTR_STATS,
  571. sizeof(struct ovs_flow_stats), &stats))
  572. goto nla_put_failure;
  573. if (tcp_flags &&
  574. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  575. goto nla_put_failure;
  576. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  577. * this is the first flow to be dumped into 'skb'. This is unusual for
  578. * Netlink but individual action lists can be longer than
  579. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  580. * The userspace caller can always fetch the actions separately if it
  581. * really wants them. (Most userspace callers in fact don't care.)
  582. *
  583. * This can only fail for dump operations because the skb is always
  584. * properly sized for single flows.
  585. */
  586. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  587. if (start) {
  588. const struct sw_flow_actions *sf_acts;
  589. sf_acts = rcu_dereference_check(flow->sf_acts,
  590. lockdep_ovsl_is_held());
  591. err = ovs_nla_put_actions(sf_acts->actions,
  592. sf_acts->actions_len, skb);
  593. if (!err)
  594. nla_nest_end(skb, start);
  595. else {
  596. if (skb_orig_len)
  597. goto error;
  598. nla_nest_cancel(skb, start);
  599. }
  600. } else if (skb_orig_len)
  601. goto nla_put_failure;
  602. return genlmsg_end(skb, ovs_header);
  603. nla_put_failure:
  604. err = -EMSGSIZE;
  605. error:
  606. genlmsg_cancel(skb, ovs_header);
  607. return err;
  608. }
  609. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  610. {
  611. const struct sw_flow_actions *sf_acts;
  612. sf_acts = ovsl_dereference(flow->sf_acts);
  613. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  614. }
  615. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  616. struct datapath *dp,
  617. u32 portid, u32 seq, u8 cmd)
  618. {
  619. struct sk_buff *skb;
  620. int retval;
  621. skb = ovs_flow_cmd_alloc_info(flow);
  622. if (!skb)
  623. return ERR_PTR(-ENOMEM);
  624. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  625. BUG_ON(retval < 0);
  626. return skb;
  627. }
  628. static struct sw_flow *__ovs_flow_tbl_lookup(struct flow_table *tbl,
  629. const struct sw_flow_key *key)
  630. {
  631. u32 __always_unused n_mask_hit;
  632. return ovs_flow_tbl_lookup(tbl, key, &n_mask_hit);
  633. }
  634. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  635. {
  636. struct nlattr **a = info->attrs;
  637. struct ovs_header *ovs_header = info->userhdr;
  638. struct sw_flow_key key, masked_key;
  639. struct sw_flow *flow = NULL;
  640. struct sw_flow_mask mask;
  641. struct sk_buff *reply;
  642. struct datapath *dp;
  643. struct sw_flow_actions *acts = NULL;
  644. struct sw_flow_match match;
  645. int error;
  646. /* Extract key. */
  647. error = -EINVAL;
  648. if (!a[OVS_FLOW_ATTR_KEY])
  649. goto error;
  650. ovs_match_init(&match, &key, &mask);
  651. error = ovs_nla_get_match(&match,
  652. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  653. if (error)
  654. goto error;
  655. /* Validate actions. */
  656. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  657. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  658. error = PTR_ERR(acts);
  659. if (IS_ERR(acts))
  660. goto error;
  661. ovs_flow_mask_key(&masked_key, &key, &mask);
  662. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  663. &masked_key, 0, &acts);
  664. if (error) {
  665. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  666. goto err_kfree;
  667. }
  668. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  669. error = -EINVAL;
  670. goto error;
  671. }
  672. ovs_lock();
  673. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  674. error = -ENODEV;
  675. if (!dp)
  676. goto err_unlock_ovs;
  677. /* Check if this is a duplicate flow */
  678. flow = __ovs_flow_tbl_lookup(&dp->table, &key);
  679. if (!flow) {
  680. /* Bail out if we're not allowed to create a new flow. */
  681. error = -ENOENT;
  682. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  683. goto err_unlock_ovs;
  684. /* Allocate flow. */
  685. flow = ovs_flow_alloc();
  686. if (IS_ERR(flow)) {
  687. error = PTR_ERR(flow);
  688. goto err_unlock_ovs;
  689. }
  690. clear_stats(flow);
  691. flow->key = masked_key;
  692. flow->unmasked_key = key;
  693. rcu_assign_pointer(flow->sf_acts, acts);
  694. /* Put flow in bucket. */
  695. error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
  696. if (error) {
  697. acts = NULL;
  698. goto err_flow_free;
  699. }
  700. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  701. info->snd_seq, OVS_FLOW_CMD_NEW);
  702. } else {
  703. /* We found a matching flow. */
  704. struct sw_flow_actions *old_acts;
  705. /* Bail out if we're not allowed to modify an existing flow.
  706. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  707. * because Generic Netlink treats the latter as a dump
  708. * request. We also accept NLM_F_EXCL in case that bug ever
  709. * gets fixed.
  710. */
  711. error = -EEXIST;
  712. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  713. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  714. goto err_unlock_ovs;
  715. /* The unmasked key has to be the same for flow updates. */
  716. error = -EINVAL;
  717. if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
  718. OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
  719. goto err_unlock_ovs;
  720. }
  721. /* Update actions. */
  722. old_acts = ovsl_dereference(flow->sf_acts);
  723. rcu_assign_pointer(flow->sf_acts, acts);
  724. ovs_nla_free_flow_actions(old_acts);
  725. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  726. info->snd_seq, OVS_FLOW_CMD_NEW);
  727. /* Clear stats. */
  728. if (a[OVS_FLOW_ATTR_CLEAR]) {
  729. spin_lock_bh(&flow->lock);
  730. clear_stats(flow);
  731. spin_unlock_bh(&flow->lock);
  732. }
  733. }
  734. ovs_unlock();
  735. if (!IS_ERR(reply))
  736. ovs_notify(&dp_flow_genl_family, reply, info);
  737. else
  738. genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
  739. 0, PTR_ERR(reply));
  740. return 0;
  741. err_flow_free:
  742. ovs_flow_free(flow, false);
  743. err_unlock_ovs:
  744. ovs_unlock();
  745. err_kfree:
  746. kfree(acts);
  747. error:
  748. return error;
  749. }
  750. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  751. {
  752. struct nlattr **a = info->attrs;
  753. struct ovs_header *ovs_header = info->userhdr;
  754. struct sw_flow_key key;
  755. struct sk_buff *reply;
  756. struct sw_flow *flow;
  757. struct datapath *dp;
  758. struct sw_flow_match match;
  759. int err;
  760. if (!a[OVS_FLOW_ATTR_KEY]) {
  761. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  762. return -EINVAL;
  763. }
  764. ovs_match_init(&match, &key, NULL);
  765. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  766. if (err)
  767. return err;
  768. ovs_lock();
  769. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  770. if (!dp) {
  771. err = -ENODEV;
  772. goto unlock;
  773. }
  774. flow = __ovs_flow_tbl_lookup(&dp->table, &key);
  775. if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
  776. err = -ENOENT;
  777. goto unlock;
  778. }
  779. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  780. info->snd_seq, OVS_FLOW_CMD_NEW);
  781. if (IS_ERR(reply)) {
  782. err = PTR_ERR(reply);
  783. goto unlock;
  784. }
  785. ovs_unlock();
  786. return genlmsg_reply(reply, info);
  787. unlock:
  788. ovs_unlock();
  789. return err;
  790. }
  791. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  792. {
  793. struct nlattr **a = info->attrs;
  794. struct ovs_header *ovs_header = info->userhdr;
  795. struct sw_flow_key key;
  796. struct sk_buff *reply;
  797. struct sw_flow *flow;
  798. struct datapath *dp;
  799. struct sw_flow_match match;
  800. int err;
  801. ovs_lock();
  802. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  803. if (!dp) {
  804. err = -ENODEV;
  805. goto unlock;
  806. }
  807. if (!a[OVS_FLOW_ATTR_KEY]) {
  808. err = ovs_flow_tbl_flush(&dp->table);
  809. goto unlock;
  810. }
  811. ovs_match_init(&match, &key, NULL);
  812. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  813. if (err)
  814. goto unlock;
  815. flow = __ovs_flow_tbl_lookup(&dp->table, &key);
  816. if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
  817. err = -ENOENT;
  818. goto unlock;
  819. }
  820. reply = ovs_flow_cmd_alloc_info(flow);
  821. if (!reply) {
  822. err = -ENOMEM;
  823. goto unlock;
  824. }
  825. ovs_flow_tbl_remove(&dp->table, flow);
  826. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  827. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  828. BUG_ON(err < 0);
  829. ovs_flow_free(flow, true);
  830. ovs_unlock();
  831. ovs_notify(&dp_flow_genl_family, reply, info);
  832. return 0;
  833. unlock:
  834. ovs_unlock();
  835. return err;
  836. }
  837. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  838. {
  839. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  840. struct table_instance *ti;
  841. struct datapath *dp;
  842. rcu_read_lock();
  843. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  844. if (!dp) {
  845. rcu_read_unlock();
  846. return -ENODEV;
  847. }
  848. ti = rcu_dereference(dp->table.ti);
  849. for (;;) {
  850. struct sw_flow *flow;
  851. u32 bucket, obj;
  852. bucket = cb->args[0];
  853. obj = cb->args[1];
  854. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  855. if (!flow)
  856. break;
  857. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  858. NETLINK_CB(cb->skb).portid,
  859. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  860. OVS_FLOW_CMD_NEW) < 0)
  861. break;
  862. cb->args[0] = bucket;
  863. cb->args[1] = obj;
  864. }
  865. rcu_read_unlock();
  866. return skb->len;
  867. }
  868. static const struct genl_ops dp_flow_genl_ops[] = {
  869. { .cmd = OVS_FLOW_CMD_NEW,
  870. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  871. .policy = flow_policy,
  872. .doit = ovs_flow_cmd_new_or_set
  873. },
  874. { .cmd = OVS_FLOW_CMD_DEL,
  875. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  876. .policy = flow_policy,
  877. .doit = ovs_flow_cmd_del
  878. },
  879. { .cmd = OVS_FLOW_CMD_GET,
  880. .flags = 0, /* OK for unprivileged users. */
  881. .policy = flow_policy,
  882. .doit = ovs_flow_cmd_get,
  883. .dumpit = ovs_flow_cmd_dump
  884. },
  885. { .cmd = OVS_FLOW_CMD_SET,
  886. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  887. .policy = flow_policy,
  888. .doit = ovs_flow_cmd_new_or_set,
  889. },
  890. };
  891. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  892. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  893. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  894. };
  895. static struct genl_family dp_datapath_genl_family = {
  896. .id = GENL_ID_GENERATE,
  897. .hdrsize = sizeof(struct ovs_header),
  898. .name = OVS_DATAPATH_FAMILY,
  899. .version = OVS_DATAPATH_VERSION,
  900. .maxattr = OVS_DP_ATTR_MAX,
  901. .netnsok = true,
  902. .parallel_ops = true,
  903. };
  904. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  905. .name = OVS_DATAPATH_MCGROUP
  906. };
  907. static size_t ovs_dp_cmd_msg_size(void)
  908. {
  909. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  910. msgsize += nla_total_size(IFNAMSIZ);
  911. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  912. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  913. return msgsize;
  914. }
  915. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  916. u32 portid, u32 seq, u32 flags, u8 cmd)
  917. {
  918. struct ovs_header *ovs_header;
  919. struct ovs_dp_stats dp_stats;
  920. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  921. int err;
  922. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  923. flags, cmd);
  924. if (!ovs_header)
  925. goto error;
  926. ovs_header->dp_ifindex = get_dpifindex(dp);
  927. rcu_read_lock();
  928. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  929. rcu_read_unlock();
  930. if (err)
  931. goto nla_put_failure;
  932. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  933. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  934. &dp_stats))
  935. goto nla_put_failure;
  936. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  937. sizeof(struct ovs_dp_megaflow_stats),
  938. &dp_megaflow_stats))
  939. goto nla_put_failure;
  940. return genlmsg_end(skb, ovs_header);
  941. nla_put_failure:
  942. genlmsg_cancel(skb, ovs_header);
  943. error:
  944. return -EMSGSIZE;
  945. }
  946. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  947. u32 seq, u8 cmd)
  948. {
  949. struct sk_buff *skb;
  950. int retval;
  951. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  952. if (!skb)
  953. return ERR_PTR(-ENOMEM);
  954. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  955. if (retval < 0) {
  956. kfree_skb(skb);
  957. return ERR_PTR(retval);
  958. }
  959. return skb;
  960. }
  961. /* Called with ovs_mutex. */
  962. static struct datapath *lookup_datapath(struct net *net,
  963. struct ovs_header *ovs_header,
  964. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  965. {
  966. struct datapath *dp;
  967. if (!a[OVS_DP_ATTR_NAME])
  968. dp = get_dp(net, ovs_header->dp_ifindex);
  969. else {
  970. struct vport *vport;
  971. rcu_read_lock();
  972. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  973. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  974. rcu_read_unlock();
  975. }
  976. return dp ? dp : ERR_PTR(-ENODEV);
  977. }
  978. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  979. {
  980. struct nlattr **a = info->attrs;
  981. struct vport_parms parms;
  982. struct sk_buff *reply;
  983. struct datapath *dp;
  984. struct vport *vport;
  985. struct ovs_net *ovs_net;
  986. int err, i;
  987. err = -EINVAL;
  988. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  989. goto err;
  990. ovs_lock();
  991. err = -ENOMEM;
  992. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  993. if (dp == NULL)
  994. goto err_unlock_ovs;
  995. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  996. /* Allocate table. */
  997. err = ovs_flow_tbl_init(&dp->table);
  998. if (err)
  999. goto err_free_dp;
  1000. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1001. if (!dp->stats_percpu) {
  1002. err = -ENOMEM;
  1003. goto err_destroy_table;
  1004. }
  1005. for_each_possible_cpu(i) {
  1006. struct dp_stats_percpu *dpath_stats;
  1007. dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
  1008. u64_stats_init(&dpath_stats->sync);
  1009. }
  1010. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1011. GFP_KERNEL);
  1012. if (!dp->ports) {
  1013. err = -ENOMEM;
  1014. goto err_destroy_percpu;
  1015. }
  1016. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1017. INIT_HLIST_HEAD(&dp->ports[i]);
  1018. /* Set up our datapath device. */
  1019. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1020. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1021. parms.options = NULL;
  1022. parms.dp = dp;
  1023. parms.port_no = OVSP_LOCAL;
  1024. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1025. vport = new_vport(&parms);
  1026. if (IS_ERR(vport)) {
  1027. err = PTR_ERR(vport);
  1028. if (err == -EBUSY)
  1029. err = -EEXIST;
  1030. goto err_destroy_ports_array;
  1031. }
  1032. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1033. info->snd_seq, OVS_DP_CMD_NEW);
  1034. err = PTR_ERR(reply);
  1035. if (IS_ERR(reply))
  1036. goto err_destroy_local_port;
  1037. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1038. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1039. ovs_unlock();
  1040. ovs_notify(&dp_datapath_genl_family, reply, info);
  1041. return 0;
  1042. err_destroy_local_port:
  1043. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1044. err_destroy_ports_array:
  1045. kfree(dp->ports);
  1046. err_destroy_percpu:
  1047. free_percpu(dp->stats_percpu);
  1048. err_destroy_table:
  1049. ovs_flow_tbl_destroy(&dp->table);
  1050. err_free_dp:
  1051. release_net(ovs_dp_get_net(dp));
  1052. kfree(dp);
  1053. err_unlock_ovs:
  1054. ovs_unlock();
  1055. err:
  1056. return err;
  1057. }
  1058. /* Called with ovs_mutex. */
  1059. static void __dp_destroy(struct datapath *dp)
  1060. {
  1061. int i;
  1062. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1063. struct vport *vport;
  1064. struct hlist_node *n;
  1065. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1066. if (vport->port_no != OVSP_LOCAL)
  1067. ovs_dp_detach_port(vport);
  1068. }
  1069. list_del_rcu(&dp->list_node);
  1070. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1071. * all port in datapath are destroyed first before freeing datapath.
  1072. */
  1073. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1074. call_rcu(&dp->rcu, destroy_dp_rcu);
  1075. }
  1076. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1077. {
  1078. struct sk_buff *reply;
  1079. struct datapath *dp;
  1080. int err;
  1081. ovs_lock();
  1082. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1083. err = PTR_ERR(dp);
  1084. if (IS_ERR(dp))
  1085. goto unlock;
  1086. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1087. info->snd_seq, OVS_DP_CMD_DEL);
  1088. err = PTR_ERR(reply);
  1089. if (IS_ERR(reply))
  1090. goto unlock;
  1091. __dp_destroy(dp);
  1092. ovs_unlock();
  1093. ovs_notify(&dp_datapath_genl_family, reply, info);
  1094. return 0;
  1095. unlock:
  1096. ovs_unlock();
  1097. return err;
  1098. }
  1099. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1100. {
  1101. struct sk_buff *reply;
  1102. struct datapath *dp;
  1103. int err;
  1104. ovs_lock();
  1105. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1106. err = PTR_ERR(dp);
  1107. if (IS_ERR(dp))
  1108. goto unlock;
  1109. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1110. info->snd_seq, OVS_DP_CMD_NEW);
  1111. if (IS_ERR(reply)) {
  1112. err = PTR_ERR(reply);
  1113. genl_set_err(&dp_datapath_genl_family, sock_net(skb->sk), 0,
  1114. 0, err);
  1115. err = 0;
  1116. goto unlock;
  1117. }
  1118. ovs_unlock();
  1119. ovs_notify(&dp_datapath_genl_family, reply, info);
  1120. return 0;
  1121. unlock:
  1122. ovs_unlock();
  1123. return err;
  1124. }
  1125. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1126. {
  1127. struct sk_buff *reply;
  1128. struct datapath *dp;
  1129. int err;
  1130. ovs_lock();
  1131. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1132. if (IS_ERR(dp)) {
  1133. err = PTR_ERR(dp);
  1134. goto unlock;
  1135. }
  1136. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1137. info->snd_seq, OVS_DP_CMD_NEW);
  1138. if (IS_ERR(reply)) {
  1139. err = PTR_ERR(reply);
  1140. goto unlock;
  1141. }
  1142. ovs_unlock();
  1143. return genlmsg_reply(reply, info);
  1144. unlock:
  1145. ovs_unlock();
  1146. return err;
  1147. }
  1148. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1149. {
  1150. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1151. struct datapath *dp;
  1152. int skip = cb->args[0];
  1153. int i = 0;
  1154. rcu_read_lock();
  1155. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1156. if (i >= skip &&
  1157. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1158. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1159. OVS_DP_CMD_NEW) < 0)
  1160. break;
  1161. i++;
  1162. }
  1163. rcu_read_unlock();
  1164. cb->args[0] = i;
  1165. return skb->len;
  1166. }
  1167. static const struct genl_ops dp_datapath_genl_ops[] = {
  1168. { .cmd = OVS_DP_CMD_NEW,
  1169. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1170. .policy = datapath_policy,
  1171. .doit = ovs_dp_cmd_new
  1172. },
  1173. { .cmd = OVS_DP_CMD_DEL,
  1174. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1175. .policy = datapath_policy,
  1176. .doit = ovs_dp_cmd_del
  1177. },
  1178. { .cmd = OVS_DP_CMD_GET,
  1179. .flags = 0, /* OK for unprivileged users. */
  1180. .policy = datapath_policy,
  1181. .doit = ovs_dp_cmd_get,
  1182. .dumpit = ovs_dp_cmd_dump
  1183. },
  1184. { .cmd = OVS_DP_CMD_SET,
  1185. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1186. .policy = datapath_policy,
  1187. .doit = ovs_dp_cmd_set,
  1188. },
  1189. };
  1190. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1191. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1192. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1193. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1194. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1195. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1196. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1197. };
  1198. struct genl_family dp_vport_genl_family = {
  1199. .id = GENL_ID_GENERATE,
  1200. .hdrsize = sizeof(struct ovs_header),
  1201. .name = OVS_VPORT_FAMILY,
  1202. .version = OVS_VPORT_VERSION,
  1203. .maxattr = OVS_VPORT_ATTR_MAX,
  1204. .netnsok = true,
  1205. .parallel_ops = true,
  1206. };
  1207. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1208. .name = OVS_VPORT_MCGROUP
  1209. };
  1210. /* Called with ovs_mutex or RCU read lock. */
  1211. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1212. u32 portid, u32 seq, u32 flags, u8 cmd)
  1213. {
  1214. struct ovs_header *ovs_header;
  1215. struct ovs_vport_stats vport_stats;
  1216. int err;
  1217. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1218. flags, cmd);
  1219. if (!ovs_header)
  1220. return -EMSGSIZE;
  1221. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1222. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1223. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1224. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1225. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1226. goto nla_put_failure;
  1227. ovs_vport_get_stats(vport, &vport_stats);
  1228. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1229. &vport_stats))
  1230. goto nla_put_failure;
  1231. err = ovs_vport_get_options(vport, skb);
  1232. if (err == -EMSGSIZE)
  1233. goto error;
  1234. return genlmsg_end(skb, ovs_header);
  1235. nla_put_failure:
  1236. err = -EMSGSIZE;
  1237. error:
  1238. genlmsg_cancel(skb, ovs_header);
  1239. return err;
  1240. }
  1241. /* Called with ovs_mutex or RCU read lock. */
  1242. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1243. u32 seq, u8 cmd)
  1244. {
  1245. struct sk_buff *skb;
  1246. int retval;
  1247. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1248. if (!skb)
  1249. return ERR_PTR(-ENOMEM);
  1250. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1251. BUG_ON(retval < 0);
  1252. return skb;
  1253. }
  1254. /* Called with ovs_mutex or RCU read lock. */
  1255. static struct vport *lookup_vport(struct net *net,
  1256. struct ovs_header *ovs_header,
  1257. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1258. {
  1259. struct datapath *dp;
  1260. struct vport *vport;
  1261. if (a[OVS_VPORT_ATTR_NAME]) {
  1262. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1263. if (!vport)
  1264. return ERR_PTR(-ENODEV);
  1265. if (ovs_header->dp_ifindex &&
  1266. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1267. return ERR_PTR(-ENODEV);
  1268. return vport;
  1269. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1270. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1271. if (port_no >= DP_MAX_PORTS)
  1272. return ERR_PTR(-EFBIG);
  1273. dp = get_dp(net, ovs_header->dp_ifindex);
  1274. if (!dp)
  1275. return ERR_PTR(-ENODEV);
  1276. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1277. if (!vport)
  1278. return ERR_PTR(-ENODEV);
  1279. return vport;
  1280. } else
  1281. return ERR_PTR(-EINVAL);
  1282. }
  1283. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1284. {
  1285. struct nlattr **a = info->attrs;
  1286. struct ovs_header *ovs_header = info->userhdr;
  1287. struct vport_parms parms;
  1288. struct sk_buff *reply;
  1289. struct vport *vport;
  1290. struct datapath *dp;
  1291. u32 port_no;
  1292. int err;
  1293. err = -EINVAL;
  1294. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1295. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1296. goto exit;
  1297. ovs_lock();
  1298. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1299. err = -ENODEV;
  1300. if (!dp)
  1301. goto exit_unlock;
  1302. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1303. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1304. err = -EFBIG;
  1305. if (port_no >= DP_MAX_PORTS)
  1306. goto exit_unlock;
  1307. vport = ovs_vport_ovsl(dp, port_no);
  1308. err = -EBUSY;
  1309. if (vport)
  1310. goto exit_unlock;
  1311. } else {
  1312. for (port_no = 1; ; port_no++) {
  1313. if (port_no >= DP_MAX_PORTS) {
  1314. err = -EFBIG;
  1315. goto exit_unlock;
  1316. }
  1317. vport = ovs_vport_ovsl(dp, port_no);
  1318. if (!vport)
  1319. break;
  1320. }
  1321. }
  1322. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1323. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1324. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1325. parms.dp = dp;
  1326. parms.port_no = port_no;
  1327. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1328. vport = new_vport(&parms);
  1329. err = PTR_ERR(vport);
  1330. if (IS_ERR(vport))
  1331. goto exit_unlock;
  1332. err = 0;
  1333. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1334. OVS_VPORT_CMD_NEW);
  1335. if (IS_ERR(reply)) {
  1336. err = PTR_ERR(reply);
  1337. ovs_dp_detach_port(vport);
  1338. goto exit_unlock;
  1339. }
  1340. ovs_notify(&dp_vport_genl_family, reply, info);
  1341. exit_unlock:
  1342. ovs_unlock();
  1343. exit:
  1344. return err;
  1345. }
  1346. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1347. {
  1348. struct nlattr **a = info->attrs;
  1349. struct sk_buff *reply;
  1350. struct vport *vport;
  1351. int err;
  1352. ovs_lock();
  1353. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1354. err = PTR_ERR(vport);
  1355. if (IS_ERR(vport))
  1356. goto exit_unlock;
  1357. if (a[OVS_VPORT_ATTR_TYPE] &&
  1358. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1359. err = -EINVAL;
  1360. goto exit_unlock;
  1361. }
  1362. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1363. if (!reply) {
  1364. err = -ENOMEM;
  1365. goto exit_unlock;
  1366. }
  1367. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1368. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1369. if (err)
  1370. goto exit_free;
  1371. }
  1372. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1373. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1374. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1375. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1376. BUG_ON(err < 0);
  1377. ovs_unlock();
  1378. ovs_notify(&dp_vport_genl_family, reply, info);
  1379. return 0;
  1380. exit_free:
  1381. kfree_skb(reply);
  1382. exit_unlock:
  1383. ovs_unlock();
  1384. return err;
  1385. }
  1386. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1387. {
  1388. struct nlattr **a = info->attrs;
  1389. struct sk_buff *reply;
  1390. struct vport *vport;
  1391. int err;
  1392. ovs_lock();
  1393. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1394. err = PTR_ERR(vport);
  1395. if (IS_ERR(vport))
  1396. goto exit_unlock;
  1397. if (vport->port_no == OVSP_LOCAL) {
  1398. err = -EINVAL;
  1399. goto exit_unlock;
  1400. }
  1401. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1402. info->snd_seq, OVS_VPORT_CMD_DEL);
  1403. err = PTR_ERR(reply);
  1404. if (IS_ERR(reply))
  1405. goto exit_unlock;
  1406. err = 0;
  1407. ovs_dp_detach_port(vport);
  1408. ovs_notify(&dp_vport_genl_family, reply, info);
  1409. exit_unlock:
  1410. ovs_unlock();
  1411. return err;
  1412. }
  1413. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1414. {
  1415. struct nlattr **a = info->attrs;
  1416. struct ovs_header *ovs_header = info->userhdr;
  1417. struct sk_buff *reply;
  1418. struct vport *vport;
  1419. int err;
  1420. rcu_read_lock();
  1421. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1422. err = PTR_ERR(vport);
  1423. if (IS_ERR(vport))
  1424. goto exit_unlock;
  1425. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1426. info->snd_seq, OVS_VPORT_CMD_NEW);
  1427. err = PTR_ERR(reply);
  1428. if (IS_ERR(reply))
  1429. goto exit_unlock;
  1430. rcu_read_unlock();
  1431. return genlmsg_reply(reply, info);
  1432. exit_unlock:
  1433. rcu_read_unlock();
  1434. return err;
  1435. }
  1436. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1437. {
  1438. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1439. struct datapath *dp;
  1440. int bucket = cb->args[0], skip = cb->args[1];
  1441. int i, j = 0;
  1442. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1443. if (!dp)
  1444. return -ENODEV;
  1445. rcu_read_lock();
  1446. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1447. struct vport *vport;
  1448. j = 0;
  1449. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1450. if (j >= skip &&
  1451. ovs_vport_cmd_fill_info(vport, skb,
  1452. NETLINK_CB(cb->skb).portid,
  1453. cb->nlh->nlmsg_seq,
  1454. NLM_F_MULTI,
  1455. OVS_VPORT_CMD_NEW) < 0)
  1456. goto out;
  1457. j++;
  1458. }
  1459. skip = 0;
  1460. }
  1461. out:
  1462. rcu_read_unlock();
  1463. cb->args[0] = i;
  1464. cb->args[1] = j;
  1465. return skb->len;
  1466. }
  1467. static const struct genl_ops dp_vport_genl_ops[] = {
  1468. { .cmd = OVS_VPORT_CMD_NEW,
  1469. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1470. .policy = vport_policy,
  1471. .doit = ovs_vport_cmd_new
  1472. },
  1473. { .cmd = OVS_VPORT_CMD_DEL,
  1474. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1475. .policy = vport_policy,
  1476. .doit = ovs_vport_cmd_del
  1477. },
  1478. { .cmd = OVS_VPORT_CMD_GET,
  1479. .flags = 0, /* OK for unprivileged users. */
  1480. .policy = vport_policy,
  1481. .doit = ovs_vport_cmd_get,
  1482. .dumpit = ovs_vport_cmd_dump
  1483. },
  1484. { .cmd = OVS_VPORT_CMD_SET,
  1485. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1486. .policy = vport_policy,
  1487. .doit = ovs_vport_cmd_set,
  1488. },
  1489. };
  1490. struct genl_family_and_ops {
  1491. struct genl_family *family;
  1492. const struct genl_ops *ops;
  1493. int n_ops;
  1494. const struct genl_multicast_group *group;
  1495. };
  1496. static const struct genl_family_and_ops dp_genl_families[] = {
  1497. { &dp_datapath_genl_family,
  1498. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1499. &ovs_dp_datapath_multicast_group },
  1500. { &dp_vport_genl_family,
  1501. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1502. &ovs_dp_vport_multicast_group },
  1503. { &dp_flow_genl_family,
  1504. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1505. &ovs_dp_flow_multicast_group },
  1506. { &dp_packet_genl_family,
  1507. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1508. NULL },
  1509. };
  1510. static void dp_unregister_genl(int n_families)
  1511. {
  1512. int i;
  1513. for (i = 0; i < n_families; i++)
  1514. genl_unregister_family(dp_genl_families[i].family);
  1515. }
  1516. static int dp_register_genl(void)
  1517. {
  1518. int n_registered;
  1519. int err;
  1520. int i;
  1521. n_registered = 0;
  1522. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1523. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1524. f->family->ops = f->ops;
  1525. f->family->n_ops = f->n_ops;
  1526. f->family->mcgrps = f->group;
  1527. f->family->n_mcgrps = f->group ? 1 : 0;
  1528. err = genl_register_family(f->family);
  1529. if (err)
  1530. goto error;
  1531. n_registered++;
  1532. }
  1533. return 0;
  1534. error:
  1535. dp_unregister_genl(n_registered);
  1536. return err;
  1537. }
  1538. static int __net_init ovs_init_net(struct net *net)
  1539. {
  1540. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1541. INIT_LIST_HEAD(&ovs_net->dps);
  1542. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1543. return 0;
  1544. }
  1545. static void __net_exit ovs_exit_net(struct net *net)
  1546. {
  1547. struct datapath *dp, *dp_next;
  1548. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1549. ovs_lock();
  1550. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1551. __dp_destroy(dp);
  1552. ovs_unlock();
  1553. cancel_work_sync(&ovs_net->dp_notify_work);
  1554. }
  1555. static struct pernet_operations ovs_net_ops = {
  1556. .init = ovs_init_net,
  1557. .exit = ovs_exit_net,
  1558. .id = &ovs_net_id,
  1559. .size = sizeof(struct ovs_net),
  1560. };
  1561. static int __init dp_init(void)
  1562. {
  1563. int err;
  1564. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1565. pr_info("Open vSwitch switching datapath\n");
  1566. err = ovs_flow_init();
  1567. if (err)
  1568. goto error;
  1569. err = ovs_vport_init();
  1570. if (err)
  1571. goto error_flow_exit;
  1572. err = register_pernet_device(&ovs_net_ops);
  1573. if (err)
  1574. goto error_vport_exit;
  1575. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1576. if (err)
  1577. goto error_netns_exit;
  1578. err = dp_register_genl();
  1579. if (err < 0)
  1580. goto error_unreg_notifier;
  1581. return 0;
  1582. error_unreg_notifier:
  1583. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1584. error_netns_exit:
  1585. unregister_pernet_device(&ovs_net_ops);
  1586. error_vport_exit:
  1587. ovs_vport_exit();
  1588. error_flow_exit:
  1589. ovs_flow_exit();
  1590. error:
  1591. return err;
  1592. }
  1593. static void dp_cleanup(void)
  1594. {
  1595. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1596. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1597. unregister_pernet_device(&ovs_net_ops);
  1598. rcu_barrier();
  1599. ovs_vport_exit();
  1600. ovs_flow_exit();
  1601. }
  1602. module_init(dp_init);
  1603. module_exit(dp_cleanup);
  1604. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1605. MODULE_LICENSE("GPL");