datapath.c 51 KB

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