datapath.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  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 "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  58. static void rehash_flow_table(struct work_struct *work);
  59. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  60. int ovs_net_id __read_mostly;
  61. static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
  62. struct genl_multicast_group *grp)
  63. {
  64. genl_notify(skb, genl_info_net(info), info->snd_portid,
  65. grp->id, info->nlhdr, GFP_KERNEL);
  66. }
  67. /**
  68. * DOC: Locking:
  69. *
  70. * All writes e.g. Writes to device state (add/remove datapath, port, set
  71. * operations on vports, etc.), Writes to other state (flow table
  72. * modifications, set miscellaneous datapath parameters, etc.) are protected
  73. * by ovs_lock.
  74. *
  75. * Reads are protected by RCU.
  76. *
  77. * There are a few special cases (mostly stats) that have their own
  78. * synchronization but they nest under all of above and don't interact with
  79. * each other.
  80. *
  81. * The RTNL lock nests inside ovs_mutex.
  82. */
  83. static DEFINE_MUTEX(ovs_mutex);
  84. void ovs_lock(void)
  85. {
  86. mutex_lock(&ovs_mutex);
  87. }
  88. void ovs_unlock(void)
  89. {
  90. mutex_unlock(&ovs_mutex);
  91. }
  92. #ifdef CONFIG_LOCKDEP
  93. int lockdep_ovsl_is_held(void)
  94. {
  95. if (debug_locks)
  96. return lockdep_is_held(&ovs_mutex);
  97. else
  98. return 1;
  99. }
  100. #endif
  101. static struct vport *new_vport(const struct vport_parms *);
  102. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  103. const struct dp_upcall_info *);
  104. static int queue_userspace_packet(struct net *, int dp_ifindex,
  105. struct sk_buff *,
  106. const struct dp_upcall_info *);
  107. /* Must be called with rcu_read_lock or ovs_mutex. */
  108. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  109. {
  110. struct datapath *dp = NULL;
  111. struct net_device *dev;
  112. rcu_read_lock();
  113. dev = dev_get_by_index_rcu(net, dp_ifindex);
  114. if (dev) {
  115. struct vport *vport = ovs_internal_dev_get_vport(dev);
  116. if (vport)
  117. dp = vport->dp;
  118. }
  119. rcu_read_unlock();
  120. return dp;
  121. }
  122. /* Must be called with rcu_read_lock or ovs_mutex. */
  123. const char *ovs_dp_name(const struct datapath *dp)
  124. {
  125. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  126. return vport->ops->get_name(vport);
  127. }
  128. static int get_dpifindex(struct datapath *dp)
  129. {
  130. struct vport *local;
  131. int ifindex;
  132. rcu_read_lock();
  133. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  134. if (local)
  135. ifindex = netdev_vport_priv(local)->dev->ifindex;
  136. else
  137. ifindex = 0;
  138. rcu_read_unlock();
  139. return ifindex;
  140. }
  141. static void destroy_dp_rcu(struct rcu_head *rcu)
  142. {
  143. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  144. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
  145. free_percpu(dp->stats_percpu);
  146. release_net(ovs_dp_get_net(dp));
  147. kfree(dp->ports);
  148. kfree(dp);
  149. }
  150. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  151. u16 port_no)
  152. {
  153. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  154. }
  155. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  156. {
  157. struct vport *vport;
  158. struct hlist_head *head;
  159. head = vport_hash_bucket(dp, port_no);
  160. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  161. if (vport->port_no == port_no)
  162. return vport;
  163. }
  164. return NULL;
  165. }
  166. /* Called with ovs_mutex. */
  167. static struct vport *new_vport(const struct vport_parms *parms)
  168. {
  169. struct vport *vport;
  170. vport = ovs_vport_add(parms);
  171. if (!IS_ERR(vport)) {
  172. struct datapath *dp = parms->dp;
  173. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  174. hlist_add_head_rcu(&vport->dp_hash_node, head);
  175. }
  176. return vport;
  177. }
  178. void ovs_dp_detach_port(struct vport *p)
  179. {
  180. ASSERT_OVSL();
  181. /* First drop references to device. */
  182. hlist_del_rcu(&p->dp_hash_node);
  183. /* Then destroy it. */
  184. ovs_vport_del(p);
  185. }
  186. /* Must be called with rcu_read_lock. */
  187. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  188. {
  189. struct datapath *dp = p->dp;
  190. struct sw_flow *flow;
  191. struct dp_stats_percpu *stats;
  192. struct sw_flow_key key;
  193. u64 *stats_counter;
  194. int error;
  195. stats = this_cpu_ptr(dp->stats_percpu);
  196. /* Extract flow from 'skb' into 'key'. */
  197. error = ovs_flow_extract(skb, p->port_no, &key);
  198. if (unlikely(error)) {
  199. kfree_skb(skb);
  200. return;
  201. }
  202. /* Look up flow. */
  203. flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
  204. if (unlikely(!flow)) {
  205. struct dp_upcall_info upcall;
  206. upcall.cmd = OVS_PACKET_CMD_MISS;
  207. upcall.key = &key;
  208. upcall.userdata = NULL;
  209. upcall.portid = p->upcall_portid;
  210. ovs_dp_upcall(dp, skb, &upcall);
  211. consume_skb(skb);
  212. stats_counter = &stats->n_missed;
  213. goto out;
  214. }
  215. OVS_CB(skb)->flow = flow;
  216. OVS_CB(skb)->pkt_key = &key;
  217. stats_counter = &stats->n_hit;
  218. ovs_flow_used(OVS_CB(skb)->flow, skb);
  219. ovs_execute_actions(dp, skb);
  220. out:
  221. /* Update datapath statistics. */
  222. u64_stats_update_begin(&stats->sync);
  223. (*stats_counter)++;
  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_flow_to_nlattrs(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. /* Called with ovs_mutex. */
  385. static int flush_flows(struct datapath *dp)
  386. {
  387. struct flow_table *old_table;
  388. struct flow_table *new_table;
  389. old_table = ovsl_dereference(dp->table);
  390. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  391. if (!new_table)
  392. return -ENOMEM;
  393. rcu_assign_pointer(dp->table, new_table);
  394. ovs_flow_tbl_destroy(old_table, true);
  395. return 0;
  396. }
  397. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
  398. {
  399. struct sw_flow_actions *acts;
  400. int new_acts_size;
  401. int req_size = NLA_ALIGN(attr_len);
  402. int next_offset = offsetof(struct sw_flow_actions, actions) +
  403. (*sfa)->actions_len;
  404. if (req_size <= (ksize(*sfa) - next_offset))
  405. goto out;
  406. new_acts_size = ksize(*sfa) * 2;
  407. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  408. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  409. return ERR_PTR(-EMSGSIZE);
  410. new_acts_size = MAX_ACTIONS_BUFSIZE;
  411. }
  412. acts = ovs_flow_actions_alloc(new_acts_size);
  413. if (IS_ERR(acts))
  414. return (void *)acts;
  415. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  416. acts->actions_len = (*sfa)->actions_len;
  417. kfree(*sfa);
  418. *sfa = acts;
  419. out:
  420. (*sfa)->actions_len += req_size;
  421. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  422. }
  423. static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
  424. {
  425. struct nlattr *a;
  426. a = reserve_sfa_size(sfa, nla_attr_size(len));
  427. if (IS_ERR(a))
  428. return PTR_ERR(a);
  429. a->nla_type = attrtype;
  430. a->nla_len = nla_attr_size(len);
  431. if (data)
  432. memcpy(nla_data(a), data, len);
  433. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  434. return 0;
  435. }
  436. static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
  437. {
  438. int used = (*sfa)->actions_len;
  439. int err;
  440. err = add_action(sfa, attrtype, NULL, 0);
  441. if (err)
  442. return err;
  443. return used;
  444. }
  445. static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
  446. {
  447. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
  448. a->nla_len = sfa->actions_len - st_offset;
  449. }
  450. static int validate_and_copy_actions(const struct nlattr *attr,
  451. const struct sw_flow_key *key, int depth,
  452. struct sw_flow_actions **sfa);
  453. static int validate_and_copy_sample(const struct nlattr *attr,
  454. const struct sw_flow_key *key, int depth,
  455. struct sw_flow_actions **sfa)
  456. {
  457. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  458. const struct nlattr *probability, *actions;
  459. const struct nlattr *a;
  460. int rem, start, err, st_acts;
  461. memset(attrs, 0, sizeof(attrs));
  462. nla_for_each_nested(a, attr, rem) {
  463. int type = nla_type(a);
  464. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  465. return -EINVAL;
  466. attrs[type] = a;
  467. }
  468. if (rem)
  469. return -EINVAL;
  470. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  471. if (!probability || nla_len(probability) != sizeof(u32))
  472. return -EINVAL;
  473. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  474. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  475. return -EINVAL;
  476. /* validation done, copy sample action. */
  477. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
  478. if (start < 0)
  479. return start;
  480. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
  481. if (err)
  482. return err;
  483. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
  484. if (st_acts < 0)
  485. return st_acts;
  486. err = validate_and_copy_actions(actions, key, depth + 1, sfa);
  487. if (err)
  488. return err;
  489. add_nested_action_end(*sfa, st_acts);
  490. add_nested_action_end(*sfa, start);
  491. return 0;
  492. }
  493. static int validate_tp_port(const struct sw_flow_key *flow_key)
  494. {
  495. if (flow_key->eth.type == htons(ETH_P_IP)) {
  496. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  497. return 0;
  498. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  499. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  500. return 0;
  501. }
  502. return -EINVAL;
  503. }
  504. static int validate_and_copy_set_tun(const struct nlattr *attr,
  505. struct sw_flow_actions **sfa)
  506. {
  507. struct sw_flow_match match;
  508. struct sw_flow_key key;
  509. int err, start;
  510. ovs_match_init(&match, &key, NULL);
  511. err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
  512. if (err)
  513. return err;
  514. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
  515. if (start < 0)
  516. return start;
  517. err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
  518. sizeof(match.key->tun_key));
  519. add_nested_action_end(*sfa, start);
  520. return err;
  521. }
  522. static int validate_set(const struct nlattr *a,
  523. const struct sw_flow_key *flow_key,
  524. struct sw_flow_actions **sfa,
  525. bool *set_tun)
  526. {
  527. const struct nlattr *ovs_key = nla_data(a);
  528. int key_type = nla_type(ovs_key);
  529. /* There can be only one key in a action */
  530. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  531. return -EINVAL;
  532. if (key_type > OVS_KEY_ATTR_MAX ||
  533. (ovs_key_lens[key_type] != nla_len(ovs_key) &&
  534. ovs_key_lens[key_type] != -1))
  535. return -EINVAL;
  536. switch (key_type) {
  537. const struct ovs_key_ipv4 *ipv4_key;
  538. const struct ovs_key_ipv6 *ipv6_key;
  539. int err;
  540. case OVS_KEY_ATTR_PRIORITY:
  541. case OVS_KEY_ATTR_SKB_MARK:
  542. case OVS_KEY_ATTR_ETHERNET:
  543. break;
  544. case OVS_KEY_ATTR_TUNNEL:
  545. *set_tun = true;
  546. err = validate_and_copy_set_tun(a, sfa);
  547. if (err)
  548. return err;
  549. break;
  550. case OVS_KEY_ATTR_IPV4:
  551. if (flow_key->eth.type != htons(ETH_P_IP))
  552. return -EINVAL;
  553. if (!flow_key->ip.proto)
  554. return -EINVAL;
  555. ipv4_key = nla_data(ovs_key);
  556. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  557. return -EINVAL;
  558. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  559. return -EINVAL;
  560. break;
  561. case OVS_KEY_ATTR_IPV6:
  562. if (flow_key->eth.type != htons(ETH_P_IPV6))
  563. return -EINVAL;
  564. if (!flow_key->ip.proto)
  565. return -EINVAL;
  566. ipv6_key = nla_data(ovs_key);
  567. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  568. return -EINVAL;
  569. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  570. return -EINVAL;
  571. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  572. return -EINVAL;
  573. break;
  574. case OVS_KEY_ATTR_TCP:
  575. if (flow_key->ip.proto != IPPROTO_TCP)
  576. return -EINVAL;
  577. return validate_tp_port(flow_key);
  578. case OVS_KEY_ATTR_UDP:
  579. if (flow_key->ip.proto != IPPROTO_UDP)
  580. return -EINVAL;
  581. return validate_tp_port(flow_key);
  582. case OVS_KEY_ATTR_SCTP:
  583. if (flow_key->ip.proto != IPPROTO_SCTP)
  584. return -EINVAL;
  585. return validate_tp_port(flow_key);
  586. default:
  587. return -EINVAL;
  588. }
  589. return 0;
  590. }
  591. static int validate_userspace(const struct nlattr *attr)
  592. {
  593. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  594. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  595. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  596. };
  597. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  598. int error;
  599. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  600. attr, userspace_policy);
  601. if (error)
  602. return error;
  603. if (!a[OVS_USERSPACE_ATTR_PID] ||
  604. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  605. return -EINVAL;
  606. return 0;
  607. }
  608. static int copy_action(const struct nlattr *from,
  609. struct sw_flow_actions **sfa)
  610. {
  611. int totlen = NLA_ALIGN(from->nla_len);
  612. struct nlattr *to;
  613. to = reserve_sfa_size(sfa, from->nla_len);
  614. if (IS_ERR(to))
  615. return PTR_ERR(to);
  616. memcpy(to, from, totlen);
  617. return 0;
  618. }
  619. static int validate_and_copy_actions(const struct nlattr *attr,
  620. const struct sw_flow_key *key,
  621. int depth,
  622. struct sw_flow_actions **sfa)
  623. {
  624. const struct nlattr *a;
  625. int rem, err;
  626. if (depth >= SAMPLE_ACTION_DEPTH)
  627. return -EOVERFLOW;
  628. nla_for_each_nested(a, attr, rem) {
  629. /* Expected argument lengths, (u32)-1 for variable length. */
  630. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  631. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  632. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  633. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  634. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  635. [OVS_ACTION_ATTR_SET] = (u32)-1,
  636. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  637. };
  638. const struct ovs_action_push_vlan *vlan;
  639. int type = nla_type(a);
  640. bool skip_copy;
  641. if (type > OVS_ACTION_ATTR_MAX ||
  642. (action_lens[type] != nla_len(a) &&
  643. action_lens[type] != (u32)-1))
  644. return -EINVAL;
  645. skip_copy = false;
  646. switch (type) {
  647. case OVS_ACTION_ATTR_UNSPEC:
  648. return -EINVAL;
  649. case OVS_ACTION_ATTR_USERSPACE:
  650. err = validate_userspace(a);
  651. if (err)
  652. return err;
  653. break;
  654. case OVS_ACTION_ATTR_OUTPUT:
  655. if (nla_get_u32(a) >= DP_MAX_PORTS)
  656. return -EINVAL;
  657. break;
  658. case OVS_ACTION_ATTR_POP_VLAN:
  659. break;
  660. case OVS_ACTION_ATTR_PUSH_VLAN:
  661. vlan = nla_data(a);
  662. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  663. return -EINVAL;
  664. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  665. return -EINVAL;
  666. break;
  667. case OVS_ACTION_ATTR_SET:
  668. err = validate_set(a, key, sfa, &skip_copy);
  669. if (err)
  670. return err;
  671. break;
  672. case OVS_ACTION_ATTR_SAMPLE:
  673. err = validate_and_copy_sample(a, key, depth, sfa);
  674. if (err)
  675. return err;
  676. skip_copy = true;
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. if (!skip_copy) {
  682. err = copy_action(a, sfa);
  683. if (err)
  684. return err;
  685. }
  686. }
  687. if (rem > 0)
  688. return -EINVAL;
  689. return 0;
  690. }
  691. static void clear_stats(struct sw_flow *flow)
  692. {
  693. flow->used = 0;
  694. flow->tcp_flags = 0;
  695. flow->packet_count = 0;
  696. flow->byte_count = 0;
  697. }
  698. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  699. {
  700. struct ovs_header *ovs_header = info->userhdr;
  701. struct nlattr **a = info->attrs;
  702. struct sw_flow_actions *acts;
  703. struct sk_buff *packet;
  704. struct sw_flow *flow;
  705. struct datapath *dp;
  706. struct ethhdr *eth;
  707. int len;
  708. int err;
  709. err = -EINVAL;
  710. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  711. !a[OVS_PACKET_ATTR_ACTIONS])
  712. goto err;
  713. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  714. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  715. err = -ENOMEM;
  716. if (!packet)
  717. goto err;
  718. skb_reserve(packet, NET_IP_ALIGN);
  719. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  720. skb_reset_mac_header(packet);
  721. eth = eth_hdr(packet);
  722. /* Normally, setting the skb 'protocol' field would be handled by a
  723. * call to eth_type_trans(), but it assumes there's a sending
  724. * device, which we may not have. */
  725. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  726. packet->protocol = eth->h_proto;
  727. else
  728. packet->protocol = htons(ETH_P_802_2);
  729. /* Build an sw_flow for sending this packet. */
  730. flow = ovs_flow_alloc();
  731. err = PTR_ERR(flow);
  732. if (IS_ERR(flow))
  733. goto err_kfree_skb;
  734. err = ovs_flow_extract(packet, -1, &flow->key);
  735. if (err)
  736. goto err_flow_free;
  737. err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
  738. if (err)
  739. goto err_flow_free;
  740. acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  741. err = PTR_ERR(acts);
  742. if (IS_ERR(acts))
  743. goto err_flow_free;
  744. err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
  745. rcu_assign_pointer(flow->sf_acts, acts);
  746. if (err)
  747. goto err_flow_free;
  748. OVS_CB(packet)->flow = flow;
  749. OVS_CB(packet)->pkt_key = &flow->key;
  750. packet->priority = flow->key.phy.priority;
  751. packet->mark = flow->key.phy.skb_mark;
  752. rcu_read_lock();
  753. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  754. err = -ENODEV;
  755. if (!dp)
  756. goto err_unlock;
  757. local_bh_disable();
  758. err = ovs_execute_actions(dp, packet);
  759. local_bh_enable();
  760. rcu_read_unlock();
  761. ovs_flow_free(flow, false);
  762. return err;
  763. err_unlock:
  764. rcu_read_unlock();
  765. err_flow_free:
  766. ovs_flow_free(flow, false);
  767. err_kfree_skb:
  768. kfree_skb(packet);
  769. err:
  770. return err;
  771. }
  772. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  773. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  774. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  775. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  776. };
  777. static struct genl_ops dp_packet_genl_ops[] = {
  778. { .cmd = OVS_PACKET_CMD_EXECUTE,
  779. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  780. .policy = packet_policy,
  781. .doit = ovs_packet_cmd_execute
  782. }
  783. };
  784. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  785. {
  786. struct flow_table *table;
  787. int i;
  788. table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
  789. stats->n_flows = ovs_flow_tbl_count(table);
  790. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  791. for_each_possible_cpu(i) {
  792. const struct dp_stats_percpu *percpu_stats;
  793. struct dp_stats_percpu local_stats;
  794. unsigned int start;
  795. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  796. do {
  797. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  798. local_stats = *percpu_stats;
  799. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  800. stats->n_hit += local_stats.n_hit;
  801. stats->n_missed += local_stats.n_missed;
  802. stats->n_lost += local_stats.n_lost;
  803. }
  804. }
  805. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  806. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  807. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  808. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  809. };
  810. static struct genl_family dp_flow_genl_family = {
  811. .id = GENL_ID_GENERATE,
  812. .hdrsize = sizeof(struct ovs_header),
  813. .name = OVS_FLOW_FAMILY,
  814. .version = OVS_FLOW_VERSION,
  815. .maxattr = OVS_FLOW_ATTR_MAX,
  816. .netnsok = true,
  817. .parallel_ops = true,
  818. };
  819. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  820. .name = OVS_FLOW_MCGROUP
  821. };
  822. static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
  823. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  824. {
  825. const struct nlattr *a;
  826. struct nlattr *start;
  827. int err = 0, rem;
  828. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  829. if (!start)
  830. return -EMSGSIZE;
  831. nla_for_each_nested(a, attr, rem) {
  832. int type = nla_type(a);
  833. struct nlattr *st_sample;
  834. switch (type) {
  835. case OVS_SAMPLE_ATTR_PROBABILITY:
  836. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
  837. return -EMSGSIZE;
  838. break;
  839. case OVS_SAMPLE_ATTR_ACTIONS:
  840. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  841. if (!st_sample)
  842. return -EMSGSIZE;
  843. err = actions_to_attr(nla_data(a), nla_len(a), skb);
  844. if (err)
  845. return err;
  846. nla_nest_end(skb, st_sample);
  847. break;
  848. }
  849. }
  850. nla_nest_end(skb, start);
  851. return err;
  852. }
  853. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  854. {
  855. const struct nlattr *ovs_key = nla_data(a);
  856. int key_type = nla_type(ovs_key);
  857. struct nlattr *start;
  858. int err;
  859. switch (key_type) {
  860. case OVS_KEY_ATTR_IPV4_TUNNEL:
  861. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  862. if (!start)
  863. return -EMSGSIZE;
  864. err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
  865. nla_data(ovs_key));
  866. if (err)
  867. return err;
  868. nla_nest_end(skb, start);
  869. break;
  870. default:
  871. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  872. return -EMSGSIZE;
  873. break;
  874. }
  875. return 0;
  876. }
  877. static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
  878. {
  879. const struct nlattr *a;
  880. int rem, err;
  881. nla_for_each_attr(a, attr, len, rem) {
  882. int type = nla_type(a);
  883. switch (type) {
  884. case OVS_ACTION_ATTR_SET:
  885. err = set_action_to_attr(a, skb);
  886. if (err)
  887. return err;
  888. break;
  889. case OVS_ACTION_ATTR_SAMPLE:
  890. err = sample_action_to_attr(a, skb);
  891. if (err)
  892. return err;
  893. break;
  894. default:
  895. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  896. return -EMSGSIZE;
  897. break;
  898. }
  899. }
  900. return 0;
  901. }
  902. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  903. {
  904. return NLMSG_ALIGN(sizeof(struct ovs_header))
  905. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  906. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  907. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  908. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  909. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  910. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  911. }
  912. /* Called with ovs_mutex. */
  913. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  914. struct sk_buff *skb, u32 portid,
  915. u32 seq, u32 flags, u8 cmd)
  916. {
  917. const int skb_orig_len = skb->len;
  918. struct nlattr *start;
  919. struct ovs_flow_stats stats;
  920. struct ovs_header *ovs_header;
  921. struct nlattr *nla;
  922. unsigned long used;
  923. u8 tcp_flags;
  924. int err;
  925. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  926. if (!ovs_header)
  927. return -EMSGSIZE;
  928. ovs_header->dp_ifindex = get_dpifindex(dp);
  929. /* Fill flow key. */
  930. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  931. if (!nla)
  932. goto nla_put_failure;
  933. err = ovs_flow_to_nlattrs(&flow->unmasked_key,
  934. &flow->unmasked_key, skb);
  935. if (err)
  936. goto error;
  937. nla_nest_end(skb, nla);
  938. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  939. if (!nla)
  940. goto nla_put_failure;
  941. err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
  942. if (err)
  943. goto error;
  944. nla_nest_end(skb, nla);
  945. spin_lock_bh(&flow->lock);
  946. used = flow->used;
  947. stats.n_packets = flow->packet_count;
  948. stats.n_bytes = flow->byte_count;
  949. tcp_flags = flow->tcp_flags;
  950. spin_unlock_bh(&flow->lock);
  951. if (used &&
  952. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  953. goto nla_put_failure;
  954. if (stats.n_packets &&
  955. nla_put(skb, OVS_FLOW_ATTR_STATS,
  956. sizeof(struct ovs_flow_stats), &stats))
  957. goto nla_put_failure;
  958. if (tcp_flags &&
  959. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  960. goto nla_put_failure;
  961. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  962. * this is the first flow to be dumped into 'skb'. This is unusual for
  963. * Netlink but individual action lists can be longer than
  964. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  965. * The userspace caller can always fetch the actions separately if it
  966. * really wants them. (Most userspace callers in fact don't care.)
  967. *
  968. * This can only fail for dump operations because the skb is always
  969. * properly sized for single flows.
  970. */
  971. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  972. if (start) {
  973. const struct sw_flow_actions *sf_acts;
  974. sf_acts = rcu_dereference_check(flow->sf_acts,
  975. lockdep_ovsl_is_held());
  976. err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
  977. if (!err)
  978. nla_nest_end(skb, start);
  979. else {
  980. if (skb_orig_len)
  981. goto error;
  982. nla_nest_cancel(skb, start);
  983. }
  984. } else if (skb_orig_len)
  985. goto nla_put_failure;
  986. return genlmsg_end(skb, ovs_header);
  987. nla_put_failure:
  988. err = -EMSGSIZE;
  989. error:
  990. genlmsg_cancel(skb, ovs_header);
  991. return err;
  992. }
  993. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  994. {
  995. const struct sw_flow_actions *sf_acts;
  996. sf_acts = ovsl_dereference(flow->sf_acts);
  997. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  998. }
  999. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  1000. struct datapath *dp,
  1001. u32 portid, u32 seq, u8 cmd)
  1002. {
  1003. struct sk_buff *skb;
  1004. int retval;
  1005. skb = ovs_flow_cmd_alloc_info(flow);
  1006. if (!skb)
  1007. return ERR_PTR(-ENOMEM);
  1008. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  1009. BUG_ON(retval < 0);
  1010. return skb;
  1011. }
  1012. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  1013. {
  1014. struct nlattr **a = info->attrs;
  1015. struct ovs_header *ovs_header = info->userhdr;
  1016. struct sw_flow_key key, masked_key;
  1017. struct sw_flow *flow = NULL;
  1018. struct sw_flow_mask mask;
  1019. struct sk_buff *reply;
  1020. struct datapath *dp;
  1021. struct flow_table *table;
  1022. struct sw_flow_actions *acts = NULL;
  1023. struct sw_flow_match match;
  1024. int error;
  1025. /* Extract key. */
  1026. error = -EINVAL;
  1027. if (!a[OVS_FLOW_ATTR_KEY])
  1028. goto error;
  1029. ovs_match_init(&match, &key, &mask);
  1030. error = ovs_match_from_nlattrs(&match,
  1031. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  1032. if (error)
  1033. goto error;
  1034. /* Validate actions. */
  1035. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  1036. acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  1037. error = PTR_ERR(acts);
  1038. if (IS_ERR(acts))
  1039. goto error;
  1040. ovs_flow_key_mask(&masked_key, &key, &mask);
  1041. error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  1042. &masked_key, 0, &acts);
  1043. if (error) {
  1044. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  1045. goto err_kfree;
  1046. }
  1047. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  1048. error = -EINVAL;
  1049. goto error;
  1050. }
  1051. ovs_lock();
  1052. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1053. error = -ENODEV;
  1054. if (!dp)
  1055. goto err_unlock_ovs;
  1056. table = ovsl_dereference(dp->table);
  1057. /* Check if this is a duplicate flow */
  1058. flow = ovs_flow_lookup(table, &key);
  1059. if (!flow) {
  1060. struct sw_flow_mask *mask_p;
  1061. /* Bail out if we're not allowed to create a new flow. */
  1062. error = -ENOENT;
  1063. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  1064. goto err_unlock_ovs;
  1065. /* Expand table, if necessary, to make room. */
  1066. if (ovs_flow_tbl_need_to_expand(table)) {
  1067. struct flow_table *new_table;
  1068. new_table = ovs_flow_tbl_expand(table);
  1069. if (!IS_ERR(new_table)) {
  1070. rcu_assign_pointer(dp->table, new_table);
  1071. ovs_flow_tbl_destroy(table, true);
  1072. table = ovsl_dereference(dp->table);
  1073. }
  1074. }
  1075. /* Allocate flow. */
  1076. flow = ovs_flow_alloc();
  1077. if (IS_ERR(flow)) {
  1078. error = PTR_ERR(flow);
  1079. goto err_unlock_ovs;
  1080. }
  1081. clear_stats(flow);
  1082. flow->key = masked_key;
  1083. flow->unmasked_key = key;
  1084. /* Make sure mask is unique in the system */
  1085. mask_p = ovs_sw_flow_mask_find(table, &mask);
  1086. if (!mask_p) {
  1087. /* Allocate a new mask if none exsits. */
  1088. mask_p = ovs_sw_flow_mask_alloc();
  1089. if (!mask_p)
  1090. goto err_flow_free;
  1091. mask_p->key = mask.key;
  1092. mask_p->range = mask.range;
  1093. ovs_sw_flow_mask_insert(table, mask_p);
  1094. }
  1095. ovs_sw_flow_mask_add_ref(mask_p);
  1096. flow->mask = mask_p;
  1097. rcu_assign_pointer(flow->sf_acts, acts);
  1098. /* Put flow in bucket. */
  1099. ovs_flow_insert(table, flow);
  1100. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1101. info->snd_seq, OVS_FLOW_CMD_NEW);
  1102. } else {
  1103. /* We found a matching flow. */
  1104. struct sw_flow_actions *old_acts;
  1105. /* Bail out if we're not allowed to modify an existing flow.
  1106. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  1107. * because Generic Netlink treats the latter as a dump
  1108. * request. We also accept NLM_F_EXCL in case that bug ever
  1109. * gets fixed.
  1110. */
  1111. error = -EEXIST;
  1112. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  1113. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  1114. goto err_unlock_ovs;
  1115. /* The unmasked key has to be the same for flow updates. */
  1116. error = -EINVAL;
  1117. if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
  1118. OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
  1119. goto err_unlock_ovs;
  1120. }
  1121. /* Update actions. */
  1122. old_acts = ovsl_dereference(flow->sf_acts);
  1123. rcu_assign_pointer(flow->sf_acts, acts);
  1124. ovs_flow_deferred_free_acts(old_acts);
  1125. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1126. info->snd_seq, OVS_FLOW_CMD_NEW);
  1127. /* Clear stats. */
  1128. if (a[OVS_FLOW_ATTR_CLEAR]) {
  1129. spin_lock_bh(&flow->lock);
  1130. clear_stats(flow);
  1131. spin_unlock_bh(&flow->lock);
  1132. }
  1133. }
  1134. ovs_unlock();
  1135. if (!IS_ERR(reply))
  1136. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  1137. else
  1138. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1139. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  1140. return 0;
  1141. err_flow_free:
  1142. ovs_flow_free(flow, false);
  1143. err_unlock_ovs:
  1144. ovs_unlock();
  1145. err_kfree:
  1146. kfree(acts);
  1147. error:
  1148. return error;
  1149. }
  1150. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1151. {
  1152. struct nlattr **a = info->attrs;
  1153. struct ovs_header *ovs_header = info->userhdr;
  1154. struct sw_flow_key key;
  1155. struct sk_buff *reply;
  1156. struct sw_flow *flow;
  1157. struct datapath *dp;
  1158. struct flow_table *table;
  1159. struct sw_flow_match match;
  1160. int err;
  1161. if (!a[OVS_FLOW_ATTR_KEY]) {
  1162. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  1163. return -EINVAL;
  1164. }
  1165. ovs_match_init(&match, &key, NULL);
  1166. err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  1167. if (err)
  1168. return err;
  1169. ovs_lock();
  1170. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1171. if (!dp) {
  1172. err = -ENODEV;
  1173. goto unlock;
  1174. }
  1175. table = ovsl_dereference(dp->table);
  1176. flow = ovs_flow_lookup_unmasked_key(table, &match);
  1177. if (!flow) {
  1178. err = -ENOENT;
  1179. goto unlock;
  1180. }
  1181. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1182. info->snd_seq, OVS_FLOW_CMD_NEW);
  1183. if (IS_ERR(reply)) {
  1184. err = PTR_ERR(reply);
  1185. goto unlock;
  1186. }
  1187. ovs_unlock();
  1188. return genlmsg_reply(reply, info);
  1189. unlock:
  1190. ovs_unlock();
  1191. return err;
  1192. }
  1193. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1194. {
  1195. struct nlattr **a = info->attrs;
  1196. struct ovs_header *ovs_header = info->userhdr;
  1197. struct sw_flow_key key;
  1198. struct sk_buff *reply;
  1199. struct sw_flow *flow;
  1200. struct datapath *dp;
  1201. struct flow_table *table;
  1202. struct sw_flow_match match;
  1203. int err;
  1204. ovs_lock();
  1205. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1206. if (!dp) {
  1207. err = -ENODEV;
  1208. goto unlock;
  1209. }
  1210. if (!a[OVS_FLOW_ATTR_KEY]) {
  1211. err = flush_flows(dp);
  1212. goto unlock;
  1213. }
  1214. ovs_match_init(&match, &key, NULL);
  1215. err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  1216. if (err)
  1217. goto unlock;
  1218. table = ovsl_dereference(dp->table);
  1219. flow = ovs_flow_lookup_unmasked_key(table, &match);
  1220. if (!flow) {
  1221. err = -ENOENT;
  1222. goto unlock;
  1223. }
  1224. reply = ovs_flow_cmd_alloc_info(flow);
  1225. if (!reply) {
  1226. err = -ENOMEM;
  1227. goto unlock;
  1228. }
  1229. ovs_flow_remove(table, flow);
  1230. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  1231. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  1232. BUG_ON(err < 0);
  1233. ovs_flow_free(flow, true);
  1234. ovs_unlock();
  1235. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  1236. return 0;
  1237. unlock:
  1238. ovs_unlock();
  1239. return err;
  1240. }
  1241. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1242. {
  1243. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1244. struct datapath *dp;
  1245. struct flow_table *table;
  1246. rcu_read_lock();
  1247. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1248. if (!dp) {
  1249. rcu_read_unlock();
  1250. return -ENODEV;
  1251. }
  1252. table = rcu_dereference(dp->table);
  1253. for (;;) {
  1254. struct sw_flow *flow;
  1255. u32 bucket, obj;
  1256. bucket = cb->args[0];
  1257. obj = cb->args[1];
  1258. flow = ovs_flow_dump_next(table, &bucket, &obj);
  1259. if (!flow)
  1260. break;
  1261. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  1262. NETLINK_CB(cb->skb).portid,
  1263. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1264. OVS_FLOW_CMD_NEW) < 0)
  1265. break;
  1266. cb->args[0] = bucket;
  1267. cb->args[1] = obj;
  1268. }
  1269. rcu_read_unlock();
  1270. return skb->len;
  1271. }
  1272. static struct genl_ops dp_flow_genl_ops[] = {
  1273. { .cmd = OVS_FLOW_CMD_NEW,
  1274. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1275. .policy = flow_policy,
  1276. .doit = ovs_flow_cmd_new_or_set
  1277. },
  1278. { .cmd = OVS_FLOW_CMD_DEL,
  1279. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1280. .policy = flow_policy,
  1281. .doit = ovs_flow_cmd_del
  1282. },
  1283. { .cmd = OVS_FLOW_CMD_GET,
  1284. .flags = 0, /* OK for unprivileged users. */
  1285. .policy = flow_policy,
  1286. .doit = ovs_flow_cmd_get,
  1287. .dumpit = ovs_flow_cmd_dump
  1288. },
  1289. { .cmd = OVS_FLOW_CMD_SET,
  1290. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1291. .policy = flow_policy,
  1292. .doit = ovs_flow_cmd_new_or_set,
  1293. },
  1294. };
  1295. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1296. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1297. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1298. };
  1299. static struct genl_family dp_datapath_genl_family = {
  1300. .id = GENL_ID_GENERATE,
  1301. .hdrsize = sizeof(struct ovs_header),
  1302. .name = OVS_DATAPATH_FAMILY,
  1303. .version = OVS_DATAPATH_VERSION,
  1304. .maxattr = OVS_DP_ATTR_MAX,
  1305. .netnsok = true,
  1306. .parallel_ops = true,
  1307. };
  1308. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  1309. .name = OVS_DATAPATH_MCGROUP
  1310. };
  1311. static size_t ovs_dp_cmd_msg_size(void)
  1312. {
  1313. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1314. msgsize += nla_total_size(IFNAMSIZ);
  1315. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1316. return msgsize;
  1317. }
  1318. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1319. u32 portid, u32 seq, u32 flags, u8 cmd)
  1320. {
  1321. struct ovs_header *ovs_header;
  1322. struct ovs_dp_stats dp_stats;
  1323. int err;
  1324. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1325. flags, cmd);
  1326. if (!ovs_header)
  1327. goto error;
  1328. ovs_header->dp_ifindex = get_dpifindex(dp);
  1329. rcu_read_lock();
  1330. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1331. rcu_read_unlock();
  1332. if (err)
  1333. goto nla_put_failure;
  1334. get_dp_stats(dp, &dp_stats);
  1335. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1336. goto nla_put_failure;
  1337. return genlmsg_end(skb, ovs_header);
  1338. nla_put_failure:
  1339. genlmsg_cancel(skb, ovs_header);
  1340. error:
  1341. return -EMSGSIZE;
  1342. }
  1343. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1344. u32 seq, u8 cmd)
  1345. {
  1346. struct sk_buff *skb;
  1347. int retval;
  1348. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1349. if (!skb)
  1350. return ERR_PTR(-ENOMEM);
  1351. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1352. if (retval < 0) {
  1353. kfree_skb(skb);
  1354. return ERR_PTR(retval);
  1355. }
  1356. return skb;
  1357. }
  1358. /* Called with ovs_mutex. */
  1359. static struct datapath *lookup_datapath(struct net *net,
  1360. struct ovs_header *ovs_header,
  1361. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1362. {
  1363. struct datapath *dp;
  1364. if (!a[OVS_DP_ATTR_NAME])
  1365. dp = get_dp(net, ovs_header->dp_ifindex);
  1366. else {
  1367. struct vport *vport;
  1368. rcu_read_lock();
  1369. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1370. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1371. rcu_read_unlock();
  1372. }
  1373. return dp ? dp : ERR_PTR(-ENODEV);
  1374. }
  1375. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1376. {
  1377. struct nlattr **a = info->attrs;
  1378. struct vport_parms parms;
  1379. struct sk_buff *reply;
  1380. struct datapath *dp;
  1381. struct vport *vport;
  1382. struct ovs_net *ovs_net;
  1383. int err, i;
  1384. err = -EINVAL;
  1385. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1386. goto err;
  1387. ovs_lock();
  1388. err = -ENOMEM;
  1389. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1390. if (dp == NULL)
  1391. goto err_unlock_ovs;
  1392. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1393. /* Allocate table. */
  1394. err = -ENOMEM;
  1395. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1396. if (!dp->table)
  1397. goto err_free_dp;
  1398. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1399. if (!dp->stats_percpu) {
  1400. err = -ENOMEM;
  1401. goto err_destroy_table;
  1402. }
  1403. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1404. GFP_KERNEL);
  1405. if (!dp->ports) {
  1406. err = -ENOMEM;
  1407. goto err_destroy_percpu;
  1408. }
  1409. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1410. INIT_HLIST_HEAD(&dp->ports[i]);
  1411. /* Set up our datapath device. */
  1412. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1413. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1414. parms.options = NULL;
  1415. parms.dp = dp;
  1416. parms.port_no = OVSP_LOCAL;
  1417. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1418. vport = new_vport(&parms);
  1419. if (IS_ERR(vport)) {
  1420. err = PTR_ERR(vport);
  1421. if (err == -EBUSY)
  1422. err = -EEXIST;
  1423. goto err_destroy_ports_array;
  1424. }
  1425. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1426. info->snd_seq, OVS_DP_CMD_NEW);
  1427. err = PTR_ERR(reply);
  1428. if (IS_ERR(reply))
  1429. goto err_destroy_local_port;
  1430. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1431. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1432. ovs_unlock();
  1433. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1434. return 0;
  1435. err_destroy_local_port:
  1436. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1437. err_destroy_ports_array:
  1438. kfree(dp->ports);
  1439. err_destroy_percpu:
  1440. free_percpu(dp->stats_percpu);
  1441. err_destroy_table:
  1442. ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
  1443. err_free_dp:
  1444. release_net(ovs_dp_get_net(dp));
  1445. kfree(dp);
  1446. err_unlock_ovs:
  1447. ovs_unlock();
  1448. err:
  1449. return err;
  1450. }
  1451. /* Called with ovs_mutex. */
  1452. static void __dp_destroy(struct datapath *dp)
  1453. {
  1454. int i;
  1455. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1456. struct vport *vport;
  1457. struct hlist_node *n;
  1458. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1459. if (vport->port_no != OVSP_LOCAL)
  1460. ovs_dp_detach_port(vport);
  1461. }
  1462. list_del_rcu(&dp->list_node);
  1463. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1464. * all port in datapath are destroyed first before freeing datapath.
  1465. */
  1466. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1467. call_rcu(&dp->rcu, destroy_dp_rcu);
  1468. }
  1469. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1470. {
  1471. struct sk_buff *reply;
  1472. struct datapath *dp;
  1473. int err;
  1474. ovs_lock();
  1475. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1476. err = PTR_ERR(dp);
  1477. if (IS_ERR(dp))
  1478. goto unlock;
  1479. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1480. info->snd_seq, OVS_DP_CMD_DEL);
  1481. err = PTR_ERR(reply);
  1482. if (IS_ERR(reply))
  1483. goto unlock;
  1484. __dp_destroy(dp);
  1485. ovs_unlock();
  1486. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1487. return 0;
  1488. unlock:
  1489. ovs_unlock();
  1490. return err;
  1491. }
  1492. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1493. {
  1494. struct sk_buff *reply;
  1495. struct datapath *dp;
  1496. int err;
  1497. ovs_lock();
  1498. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1499. err = PTR_ERR(dp);
  1500. if (IS_ERR(dp))
  1501. goto unlock;
  1502. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1503. info->snd_seq, OVS_DP_CMD_NEW);
  1504. if (IS_ERR(reply)) {
  1505. err = PTR_ERR(reply);
  1506. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1507. ovs_dp_datapath_multicast_group.id, err);
  1508. err = 0;
  1509. goto unlock;
  1510. }
  1511. ovs_unlock();
  1512. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1513. return 0;
  1514. unlock:
  1515. ovs_unlock();
  1516. return err;
  1517. }
  1518. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1519. {
  1520. struct sk_buff *reply;
  1521. struct datapath *dp;
  1522. int err;
  1523. ovs_lock();
  1524. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1525. if (IS_ERR(dp)) {
  1526. err = PTR_ERR(dp);
  1527. goto unlock;
  1528. }
  1529. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1530. info->snd_seq, OVS_DP_CMD_NEW);
  1531. if (IS_ERR(reply)) {
  1532. err = PTR_ERR(reply);
  1533. goto unlock;
  1534. }
  1535. ovs_unlock();
  1536. return genlmsg_reply(reply, info);
  1537. unlock:
  1538. ovs_unlock();
  1539. return err;
  1540. }
  1541. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1542. {
  1543. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1544. struct datapath *dp;
  1545. int skip = cb->args[0];
  1546. int i = 0;
  1547. rcu_read_lock();
  1548. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1549. if (i >= skip &&
  1550. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1551. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1552. OVS_DP_CMD_NEW) < 0)
  1553. break;
  1554. i++;
  1555. }
  1556. rcu_read_unlock();
  1557. cb->args[0] = i;
  1558. return skb->len;
  1559. }
  1560. static struct genl_ops dp_datapath_genl_ops[] = {
  1561. { .cmd = OVS_DP_CMD_NEW,
  1562. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1563. .policy = datapath_policy,
  1564. .doit = ovs_dp_cmd_new
  1565. },
  1566. { .cmd = OVS_DP_CMD_DEL,
  1567. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1568. .policy = datapath_policy,
  1569. .doit = ovs_dp_cmd_del
  1570. },
  1571. { .cmd = OVS_DP_CMD_GET,
  1572. .flags = 0, /* OK for unprivileged users. */
  1573. .policy = datapath_policy,
  1574. .doit = ovs_dp_cmd_get,
  1575. .dumpit = ovs_dp_cmd_dump
  1576. },
  1577. { .cmd = OVS_DP_CMD_SET,
  1578. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1579. .policy = datapath_policy,
  1580. .doit = ovs_dp_cmd_set,
  1581. },
  1582. };
  1583. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1584. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1585. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1586. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1587. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1588. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1589. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1590. };
  1591. static struct genl_family dp_vport_genl_family = {
  1592. .id = GENL_ID_GENERATE,
  1593. .hdrsize = sizeof(struct ovs_header),
  1594. .name = OVS_VPORT_FAMILY,
  1595. .version = OVS_VPORT_VERSION,
  1596. .maxattr = OVS_VPORT_ATTR_MAX,
  1597. .netnsok = true,
  1598. .parallel_ops = true,
  1599. };
  1600. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1601. .name = OVS_VPORT_MCGROUP
  1602. };
  1603. /* Called with ovs_mutex or RCU read lock. */
  1604. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1605. u32 portid, u32 seq, u32 flags, u8 cmd)
  1606. {
  1607. struct ovs_header *ovs_header;
  1608. struct ovs_vport_stats vport_stats;
  1609. int err;
  1610. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1611. flags, cmd);
  1612. if (!ovs_header)
  1613. return -EMSGSIZE;
  1614. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1615. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1616. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1617. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1618. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1619. goto nla_put_failure;
  1620. ovs_vport_get_stats(vport, &vport_stats);
  1621. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1622. &vport_stats))
  1623. goto nla_put_failure;
  1624. err = ovs_vport_get_options(vport, skb);
  1625. if (err == -EMSGSIZE)
  1626. goto error;
  1627. return genlmsg_end(skb, ovs_header);
  1628. nla_put_failure:
  1629. err = -EMSGSIZE;
  1630. error:
  1631. genlmsg_cancel(skb, ovs_header);
  1632. return err;
  1633. }
  1634. /* Called with ovs_mutex or RCU read lock. */
  1635. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1636. u32 seq, u8 cmd)
  1637. {
  1638. struct sk_buff *skb;
  1639. int retval;
  1640. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1641. if (!skb)
  1642. return ERR_PTR(-ENOMEM);
  1643. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1644. BUG_ON(retval < 0);
  1645. return skb;
  1646. }
  1647. /* Called with ovs_mutex or RCU read lock. */
  1648. static struct vport *lookup_vport(struct net *net,
  1649. struct ovs_header *ovs_header,
  1650. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1651. {
  1652. struct datapath *dp;
  1653. struct vport *vport;
  1654. if (a[OVS_VPORT_ATTR_NAME]) {
  1655. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1656. if (!vport)
  1657. return ERR_PTR(-ENODEV);
  1658. if (ovs_header->dp_ifindex &&
  1659. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1660. return ERR_PTR(-ENODEV);
  1661. return vport;
  1662. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1663. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1664. if (port_no >= DP_MAX_PORTS)
  1665. return ERR_PTR(-EFBIG);
  1666. dp = get_dp(net, ovs_header->dp_ifindex);
  1667. if (!dp)
  1668. return ERR_PTR(-ENODEV);
  1669. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1670. if (!vport)
  1671. return ERR_PTR(-ENODEV);
  1672. return vport;
  1673. } else
  1674. return ERR_PTR(-EINVAL);
  1675. }
  1676. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1677. {
  1678. struct nlattr **a = info->attrs;
  1679. struct ovs_header *ovs_header = info->userhdr;
  1680. struct vport_parms parms;
  1681. struct sk_buff *reply;
  1682. struct vport *vport;
  1683. struct datapath *dp;
  1684. u32 port_no;
  1685. int err;
  1686. err = -EINVAL;
  1687. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1688. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1689. goto exit;
  1690. ovs_lock();
  1691. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1692. err = -ENODEV;
  1693. if (!dp)
  1694. goto exit_unlock;
  1695. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1696. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1697. err = -EFBIG;
  1698. if (port_no >= DP_MAX_PORTS)
  1699. goto exit_unlock;
  1700. vport = ovs_vport_ovsl(dp, port_no);
  1701. err = -EBUSY;
  1702. if (vport)
  1703. goto exit_unlock;
  1704. } else {
  1705. for (port_no = 1; ; port_no++) {
  1706. if (port_no >= DP_MAX_PORTS) {
  1707. err = -EFBIG;
  1708. goto exit_unlock;
  1709. }
  1710. vport = ovs_vport_ovsl(dp, port_no);
  1711. if (!vport)
  1712. break;
  1713. }
  1714. }
  1715. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1716. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1717. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1718. parms.dp = dp;
  1719. parms.port_no = port_no;
  1720. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1721. vport = new_vport(&parms);
  1722. err = PTR_ERR(vport);
  1723. if (IS_ERR(vport))
  1724. goto exit_unlock;
  1725. err = 0;
  1726. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1727. OVS_VPORT_CMD_NEW);
  1728. if (IS_ERR(reply)) {
  1729. err = PTR_ERR(reply);
  1730. ovs_dp_detach_port(vport);
  1731. goto exit_unlock;
  1732. }
  1733. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1734. exit_unlock:
  1735. ovs_unlock();
  1736. exit:
  1737. return err;
  1738. }
  1739. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1740. {
  1741. struct nlattr **a = info->attrs;
  1742. struct sk_buff *reply;
  1743. struct vport *vport;
  1744. int err;
  1745. ovs_lock();
  1746. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1747. err = PTR_ERR(vport);
  1748. if (IS_ERR(vport))
  1749. goto exit_unlock;
  1750. if (a[OVS_VPORT_ATTR_TYPE] &&
  1751. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1752. err = -EINVAL;
  1753. goto exit_unlock;
  1754. }
  1755. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1756. if (!reply) {
  1757. err = -ENOMEM;
  1758. goto exit_unlock;
  1759. }
  1760. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1761. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1762. if (err)
  1763. goto exit_free;
  1764. }
  1765. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1766. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1767. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1768. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1769. BUG_ON(err < 0);
  1770. ovs_unlock();
  1771. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1772. return 0;
  1773. exit_free:
  1774. kfree_skb(reply);
  1775. exit_unlock:
  1776. ovs_unlock();
  1777. return err;
  1778. }
  1779. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1780. {
  1781. struct nlattr **a = info->attrs;
  1782. struct sk_buff *reply;
  1783. struct vport *vport;
  1784. int err;
  1785. ovs_lock();
  1786. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1787. err = PTR_ERR(vport);
  1788. if (IS_ERR(vport))
  1789. goto exit_unlock;
  1790. if (vport->port_no == OVSP_LOCAL) {
  1791. err = -EINVAL;
  1792. goto exit_unlock;
  1793. }
  1794. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1795. info->snd_seq, OVS_VPORT_CMD_DEL);
  1796. err = PTR_ERR(reply);
  1797. if (IS_ERR(reply))
  1798. goto exit_unlock;
  1799. err = 0;
  1800. ovs_dp_detach_port(vport);
  1801. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1802. exit_unlock:
  1803. ovs_unlock();
  1804. return err;
  1805. }
  1806. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1807. {
  1808. struct nlattr **a = info->attrs;
  1809. struct ovs_header *ovs_header = info->userhdr;
  1810. struct sk_buff *reply;
  1811. struct vport *vport;
  1812. int err;
  1813. rcu_read_lock();
  1814. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1815. err = PTR_ERR(vport);
  1816. if (IS_ERR(vport))
  1817. goto exit_unlock;
  1818. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1819. info->snd_seq, OVS_VPORT_CMD_NEW);
  1820. err = PTR_ERR(reply);
  1821. if (IS_ERR(reply))
  1822. goto exit_unlock;
  1823. rcu_read_unlock();
  1824. return genlmsg_reply(reply, info);
  1825. exit_unlock:
  1826. rcu_read_unlock();
  1827. return err;
  1828. }
  1829. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1830. {
  1831. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1832. struct datapath *dp;
  1833. int bucket = cb->args[0], skip = cb->args[1];
  1834. int i, j = 0;
  1835. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1836. if (!dp)
  1837. return -ENODEV;
  1838. rcu_read_lock();
  1839. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1840. struct vport *vport;
  1841. j = 0;
  1842. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1843. if (j >= skip &&
  1844. ovs_vport_cmd_fill_info(vport, skb,
  1845. NETLINK_CB(cb->skb).portid,
  1846. cb->nlh->nlmsg_seq,
  1847. NLM_F_MULTI,
  1848. OVS_VPORT_CMD_NEW) < 0)
  1849. goto out;
  1850. j++;
  1851. }
  1852. skip = 0;
  1853. }
  1854. out:
  1855. rcu_read_unlock();
  1856. cb->args[0] = i;
  1857. cb->args[1] = j;
  1858. return skb->len;
  1859. }
  1860. static struct genl_ops dp_vport_genl_ops[] = {
  1861. { .cmd = OVS_VPORT_CMD_NEW,
  1862. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1863. .policy = vport_policy,
  1864. .doit = ovs_vport_cmd_new
  1865. },
  1866. { .cmd = OVS_VPORT_CMD_DEL,
  1867. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1868. .policy = vport_policy,
  1869. .doit = ovs_vport_cmd_del
  1870. },
  1871. { .cmd = OVS_VPORT_CMD_GET,
  1872. .flags = 0, /* OK for unprivileged users. */
  1873. .policy = vport_policy,
  1874. .doit = ovs_vport_cmd_get,
  1875. .dumpit = ovs_vport_cmd_dump
  1876. },
  1877. { .cmd = OVS_VPORT_CMD_SET,
  1878. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1879. .policy = vport_policy,
  1880. .doit = ovs_vport_cmd_set,
  1881. },
  1882. };
  1883. struct genl_family_and_ops {
  1884. struct genl_family *family;
  1885. struct genl_ops *ops;
  1886. int n_ops;
  1887. struct genl_multicast_group *group;
  1888. };
  1889. static const struct genl_family_and_ops dp_genl_families[] = {
  1890. { &dp_datapath_genl_family,
  1891. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1892. &ovs_dp_datapath_multicast_group },
  1893. { &dp_vport_genl_family,
  1894. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1895. &ovs_dp_vport_multicast_group },
  1896. { &dp_flow_genl_family,
  1897. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1898. &ovs_dp_flow_multicast_group },
  1899. { &dp_packet_genl_family,
  1900. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1901. NULL },
  1902. };
  1903. static void dp_unregister_genl(int n_families)
  1904. {
  1905. int i;
  1906. for (i = 0; i < n_families; i++)
  1907. genl_unregister_family(dp_genl_families[i].family);
  1908. }
  1909. static int dp_register_genl(void)
  1910. {
  1911. int n_registered;
  1912. int err;
  1913. int i;
  1914. n_registered = 0;
  1915. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1916. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1917. err = genl_register_family_with_ops(f->family, f->ops,
  1918. f->n_ops);
  1919. if (err)
  1920. goto error;
  1921. n_registered++;
  1922. if (f->group) {
  1923. err = genl_register_mc_group(f->family, f->group);
  1924. if (err)
  1925. goto error;
  1926. }
  1927. }
  1928. return 0;
  1929. error:
  1930. dp_unregister_genl(n_registered);
  1931. return err;
  1932. }
  1933. static void rehash_flow_table(struct work_struct *work)
  1934. {
  1935. struct datapath *dp;
  1936. struct net *net;
  1937. ovs_lock();
  1938. rtnl_lock();
  1939. for_each_net(net) {
  1940. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1941. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1942. struct flow_table *old_table = ovsl_dereference(dp->table);
  1943. struct flow_table *new_table;
  1944. new_table = ovs_flow_tbl_rehash(old_table);
  1945. if (!IS_ERR(new_table)) {
  1946. rcu_assign_pointer(dp->table, new_table);
  1947. ovs_flow_tbl_destroy(old_table, true);
  1948. }
  1949. }
  1950. }
  1951. rtnl_unlock();
  1952. ovs_unlock();
  1953. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1954. }
  1955. static int __net_init ovs_init_net(struct net *net)
  1956. {
  1957. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1958. INIT_LIST_HEAD(&ovs_net->dps);
  1959. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1960. return 0;
  1961. }
  1962. static void __net_exit ovs_exit_net(struct net *net)
  1963. {
  1964. struct datapath *dp, *dp_next;
  1965. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1966. ovs_lock();
  1967. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1968. __dp_destroy(dp);
  1969. ovs_unlock();
  1970. cancel_work_sync(&ovs_net->dp_notify_work);
  1971. }
  1972. static struct pernet_operations ovs_net_ops = {
  1973. .init = ovs_init_net,
  1974. .exit = ovs_exit_net,
  1975. .id = &ovs_net_id,
  1976. .size = sizeof(struct ovs_net),
  1977. };
  1978. static int __init dp_init(void)
  1979. {
  1980. int err;
  1981. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1982. pr_info("Open vSwitch switching datapath\n");
  1983. err = ovs_flow_init();
  1984. if (err)
  1985. goto error;
  1986. err = ovs_vport_init();
  1987. if (err)
  1988. goto error_flow_exit;
  1989. err = register_pernet_device(&ovs_net_ops);
  1990. if (err)
  1991. goto error_vport_exit;
  1992. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1993. if (err)
  1994. goto error_netns_exit;
  1995. err = dp_register_genl();
  1996. if (err < 0)
  1997. goto error_unreg_notifier;
  1998. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1999. return 0;
  2000. error_unreg_notifier:
  2001. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2002. error_netns_exit:
  2003. unregister_pernet_device(&ovs_net_ops);
  2004. error_vport_exit:
  2005. ovs_vport_exit();
  2006. error_flow_exit:
  2007. ovs_flow_exit();
  2008. error:
  2009. return err;
  2010. }
  2011. static void dp_cleanup(void)
  2012. {
  2013. cancel_delayed_work_sync(&rehash_flow_wq);
  2014. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2015. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2016. unregister_pernet_device(&ovs_net_ops);
  2017. rcu_barrier();
  2018. ovs_vport_exit();
  2019. ovs_flow_exit();
  2020. }
  2021. module_init(dp_init);
  2022. module_exit(dp_cleanup);
  2023. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2024. MODULE_LICENSE("GPL");